[apps/tidep0084.git] / example / iot-gateway / node_modules / bytebuffer / src / encodings / debug.js
1 //? if (DEBUG) {
2 // encodings/debug
4 /**
5 * Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:
6 * * `<` : offset,
7 * * `'` : markedOffset,
8 * * `>` : limit,
9 * * `|` : offset and limit,
10 * * `[` : offset and markedOffset,
11 * * `]` : markedOffset and limit,
12 * * `!` : offset, markedOffset and limit
13 * @param {boolean=} columns If `true` returns two columns hex + ascii, defaults to `false`
14 * @returns {string|!Array.<string>} Debug string or array of lines if `asArray = true`
15 * @expose
16 * @example `>00'01 02<03` contains four bytes with `limit=0, markedOffset=1, offset=3`
17 * @example `00[01 02 03>` contains four bytes with `offset=markedOffset=1, limit=4`
18 * @example `00|01 02 03` contains four bytes with `offset=limit=1, markedOffset=-1`
19 * @example `|` contains zero bytes with `offset=limit=0, markedOffset=-1`
20 */
21 ByteBufferPrototype.toDebug = function(columns) {
22 var i = -1,
23 //? if (NODE)
24 k = this.buffer.length,
25 //? else
26 k = this.buffer.byteLength,
27 b,
28 hex = "",
29 asc = "",
30 out = "";
31 while (i<k) {
32 if (i !== -1) {
33 //? if (NODE)
34 b = this.buffer[i];
35 //? else if (DATAVIEW)
36 b = this.view.getUint8(i);
37 //? else
38 b = this.view[i];
39 if (b < 0x10) hex += "0"+b.toString(16).toUpperCase();
40 else hex += b.toString(16).toUpperCase();
41 if (columns)
42 asc += b > 32 && b < 127 ? String.fromCharCode(b) : '.';
43 }
44 ++i;
45 if (columns) {
46 if (i > 0 && i % 16 === 0 && i !== k) {
47 while (hex.length < 3*16+3) hex += " ";
48 out += hex+asc+"\n";
49 hex = asc = "";
50 }
51 }
52 if (i === this.offset && i === this.limit)
53 hex += i === this.markedOffset ? "!" : "|";
54 else if (i === this.offset)
55 hex += i === this.markedOffset ? "[" : "<";
56 else if (i === this.limit)
57 hex += i === this.markedOffset ? "]" : ">";
58 else
59 hex += i === this.markedOffset ? "'" : (columns || (i !== 0 && i !== k) ? " " : "");
60 }
61 if (columns && hex !== " ") {
62 while (hex.length < 3*16+3)
63 hex += " ";
64 out += hex + asc + "\n";
65 }
66 return columns ? out : hex;
67 };
69 /**
70 * Decodes a hex encoded string with marked offsets to a ByteBuffer.
71 * @param {string} str Debug string to decode (not be generated with `columns = true`)
72 * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
73 * {@link ByteBuffer.DEFAULT_ENDIAN}.
74 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
75 * {@link ByteBuffer.DEFAULT_NOASSERT}.
76 * @returns {!ByteBuffer} ByteBuffer
77 * @expose
78 * @see ByteBuffer#toDebug
79 */
80 ByteBuffer.fromDebug = function(str, littleEndian, noAssert) {
81 /*?
82 // "<60 61 62 63>"; // 13 = 4
83 // "60<61 62]63" // 11 = 4
84 // "<61 61 61>"; // 10 = 3 => C = ((L+1)/3) | 0
85 // "61<61>61"; // 8 = 3
86 // "<61 61>"; // 7 = 2
87 */
88 var k = str.length,
89 bb = new ByteBuffer(((k+1)/3)|0, littleEndian, noAssert);
90 var i = 0, j = 0, ch, b,
91 rs = false, // Require symbol next
92 ho = false, hm = false, hl = false, // Already has offset (ho), markedOffset (hm), limit (hl)?
93 fail = false;
94 while (i<k) {
95 switch (ch = str.charAt(i++)) {
96 case '!':
97 if (!noAssert) {
98 if (ho || hm || hl) {
99 fail = true;
100 break;
101 }
102 ho = hm = hl = true;
103 }
104 bb.offset = bb.markedOffset = bb.limit = j;
105 rs = false;
106 break;
107 case '|':
108 if (!noAssert) {
109 if (ho || hl) {
110 fail = true;
111 break;
112 }
113 ho = hl = true;
114 }
115 bb.offset = bb.limit = j;
116 rs = false;
117 break;
118 case '[':
119 if (!noAssert) {
120 if (ho || hm) {
121 fail = true;
122 break;
123 }
124 ho = hm = true;
125 }
126 bb.offset = bb.markedOffset = j;
127 rs = false;
128 break;
129 case '<':
130 if (!noAssert) {
131 if (ho) {
132 fail = true;
133 break;
134 }
135 ho = true;
136 }
137 bb.offset = j;
138 rs = false;
139 break;
140 case ']':
141 if (!noAssert) {
142 if (hl || hm) {
143 fail = true;
144 break;
145 }
146 hl = hm = true;
147 }
148 bb.limit = bb.markedOffset = j;
149 rs = false;
150 break;
151 case '>':
152 if (!noAssert) {
153 if (hl) {
154 fail = true;
155 break;
156 }
157 hl = true;
158 }
159 bb.limit = j;
160 rs = false;
161 break;
162 case "'":
163 if (!noAssert) {
164 if (hm) {
165 fail = true;
166 break;
167 }
168 hm = true;
169 }
170 bb.markedOffset = j;
171 rs = false;
172 break;
173 case ' ':
174 rs = false;
175 break;
176 default:
177 if (!noAssert) {
178 if (rs) {
179 fail = true;
180 break;
181 }
182 }
183 b = parseInt(ch+str.charAt(i++), 16);
184 if (!noAssert) {
185 if (isNaN(b) || b < 0 || b > 255)
186 throw TypeError("Illegal str: Not a debug encoded string");
187 }
188 //? if (NODE)
189 bb.buffer[j++] = b;
190 //? else if (DATAVIEW)
191 bb.view.setUint8(j++, b);
192 //? else
193 bb.view[j++] = b;
194 rs = true;
195 }
196 if (fail)
197 throw TypeError("Illegal str: Invalid symbol at "+i);
198 }
199 if (!noAssert) {
200 if (!ho || !hl)
201 throw TypeError("Illegal str: Missing offset or limit");
202 //? if (NODE)
203 if (j<bb.buffer.length)
204 //? else
205 if (j<bb.buffer.byteLength)
206 throw TypeError("Illegal str: Not a debug encoded string (is it hex?) "+j+" < "+k);
207 }
208 return bb;
209 };
211 //? }