Passed
Push — master ( 39dea5...dace4e )
by Rafael S.
01:52
created
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview The byte-data API.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/** @module byteData */
31
32
import endianness from './lib/endianness.js';
33
import utf8BufferSize from './lib/utf8-buffer-size.js';
34
import {reader_, setUp_, writer_} from './lib/packer.js';
35
import {validateNotUndefined, validateValueType} from './lib/validation.js';
36
37
/**
38
 * Read a string of UTF-8 characters from a byte buffer.
39
 * @see https://encoding.spec.whatwg.org/#the-encoding
40
 * @see https://stackoverflow.com/a/34926911
41
 * @param {!Uint8Array|!Array<!number>} buffer A byte buffer.
42
 * @param {number=} index The index to read.
43
 * @param {?number=} len The number of bytes to read.
44
 * @return {string}
45
 */
46
export function unpackString(buffer, index=0, len=null) {
47
  len = len !== null ? index + len : buffer.length;
48
  /** @type {string} */
49
  let str = "";
50
  while(index < len) {
51
    /** @type {number} */
52
    let lowerBoundary = 0x80;
53
    /** @type {number} */
54
    let upperBoundary = 0xBF;
55
    /** @type {boolean} */
56
    let replace = false;
57
    /** @type {number} */
58
    let charCode = buffer[index++];
59
    if (charCode >= 0x00 && charCode <= 0x7F) {
60
      str += String.fromCharCode(charCode);
61
    } else {
62
      /** @type {number} */
63
      let count = 0;
64
      if (charCode >= 0xC2 && charCode <= 0xDF) {
65
        count = 1;
66
      } else if (charCode >= 0xE0 && charCode <= 0xEF ) {
67
        count = 2;
68
        if (buffer[index] === 0xE0) {
69
          lowerBoundary = 0xA0;
70
        }
71
        if (buffer[index] === 0xED) {
72
          upperBoundary = 0x9F;
73
        }
74
      } else if (charCode >= 0xF0 && charCode <= 0xF4 ) {
75
        count = 3;
76
        if (buffer[index] === 0xF0) {
77
          lowerBoundary = 0x90;
78
        }
79
        if (buffer[index] === 0xF4) {
80
          upperBoundary = 0x8F;
81
        }
82
      } else {
83
        replace = true;
84
      }
85
      charCode = charCode & (1 << (8 - count - 1)) - 1;
86
      for (let i = 0; i < count; i++) {
87
        if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) {
88
          replace = true;
89
        }
90
        charCode = (charCode << 6) | (buffer[index] & 0x3f);
91
        index++;
92
      }
93
      if (replace) {
94
        str += String.fromCharCode(0xFFFD);
95
      } 
96
      else if (charCode <= 0xffff) {
97
        str += String.fromCharCode(charCode);
98
      } else {
99
        charCode -= 0x10000;
100
        str += String.fromCharCode(
101
          ((charCode >> 10) & 0x3ff) + 0xd800,
102
          (charCode & 0x3ff) + 0xdc00);
103
      }
104
    }
105
  }
106
  return str;
107
}
108
109
/**
110
 * Write a string of UTF-8 characters as a byte buffer.
111
 * @see https://encoding.spec.whatwg.org/#utf-8-encoder
112
 * @param {string} str The string to pack.
113
 * @return {!Uint8Array} The packed string.
114
 */
115
export function packString(str) {
116
  /** @type {!Uint8Array} */
117
  let bytes = new Uint8Array(utf8BufferSize(str));
118
  let bufferIndex = 0;
119
  for (let i = 0; i < str.length; i++) {
120
    /** @type {number} */
121
    let codePoint = str.codePointAt(i);
122
    if (codePoint < 128) {
123
      bytes[bufferIndex] = codePoint;
124
      bufferIndex++;
125
    } else {
126
      /** @type {number} */
127
      let count = 0;
128
      /** @type {number} */
129
      let offset = 0;
130
      if (codePoint <= 0x07FF) {
131
        count = 1;
132
        offset = 0xC0;
133
      } else if(codePoint <= 0xFFFF) {
134
        count = 2;
135
        offset = 0xE0;
136
      } else if(codePoint <= 0x10FFFF) {
137
        count = 3;
138
        offset = 0xF0;
139
        i++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable i here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
140
      }
141
      bytes[bufferIndex] = (codePoint >> (6 * count)) + offset;
142
      bufferIndex++;
143
      while (count > 0) {
144
        bytes[bufferIndex] = 0x80 | (codePoint >> (6 * (count - 1)) & 0x3F);
145
        bufferIndex++;
146
        count--;
147
      }
148
    }
149
  }
150
  return bytes;
151
}
152
153
/**
154
 * Write a string of UTF-8 characters to a byte buffer.
155
 * @param {string} str The string to pack.
156
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
157
 * @param {number=} index The index to write in the buffer.
158
 * @return {number} The next index to write in the buffer.
159
 */
160
export function packStringTo(str, buffer, index=0) {
161
  /** @type {!Uint8Array} */
162
  let bytes = packString(str);
163
  for (let i = 0; i < bytes.length; i++) {
164
    buffer[index++] = bytes[i];
165
  }
166
  return index;
167
}
168
169
// Numbers
170
/**
171
 * Pack a number as a byte buffer.
172
 * @param {number} value The number.
173
 * @param {!Object} theType The type definition.
174
 * @return {!Array<number>} The packed value.
175
 * @throws {Error} If the type definition is not valid.
176
 * @throws {Error} If the value is not valid.
177
 */
178
export function pack(value, theType) {
179
  /** @type {!Array<!number>} */
180
  let output = [];
181
  packTo(value, theType, output);
182
  return output;
183
}
184
185
/**
186
 * Pack a number to a byte buffer.
187
 * @param {number} value The value.
188
 * @param {!Object} theType The type definition.
189
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
190
 * @param {number=} index The index to write.
191
 * @return {number} The next index to write.
192
 * @throws {Error} If the type definition is not valid.
193
 * @throws {Error} If the value is not valid.
194
 */
195
export function packTo(value, theType, buffer, index=0) {
196
  return packArrayTo([value], theType, buffer, index);
197
}
198
199
/**
200
 * Pack an array of numbers as a byte buffer.
201
 * @param {!Array<number>|!TypedArray} values The values.
202
 * @param {!Object} theType The type definition.
203
 * @return {!Array<number>} The packed values.
204
 * @throws {Error} If the type definition is not valid.
205
 * @throws {Error} If any of the values are not valid.
206
 */
207
export function packArray(values, theType) {
208
  /** @type {!Array<!number>} */
209
  let output = [];
210
  packArrayTo(values, theType, output);
211
  return output;
212
}
213
214
/**
215
 * Pack a array of numbers to a byte buffer.
216
 * @param {!Array<number>|!TypedArray} values The value.
217
 * @param {!Object} theType The type definition.
218
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
219
 * @param {number=} index The buffer index to write.
220
 * @return {number} The next index to write.
221
 * @throws {Error} If the type definition is not valid.
222
 * @throws {Error} If the value is not valid.
223
 */
224
export function packArrayTo(values, theType, buffer, index=0) {
225
  setUp_(theType);
226
  for (let i=0; i < values.length; i++) {
227
    validateNotUndefined(values[i]);
228
    validateValueType(values[i]);
229
    /** @type {number} */
230
    let len = index + theType.offset;
231
    while (index < len) {
232
      index = writer_(buffer, values[i], index);
233
    }
234
    if (theType.be) {
235
      endianness(
236
        buffer, theType.offset, index - theType.offset, index);
237
    }
238
  }
239
  return index;
240
}
241
242
/**
243
 * Unpack a number from a byte buffer.
244
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
245
 * @param {!Object} theType The type definition.
246
 * @param {number=} index The buffer index to read.
247
 * @return {number}
248
 * @throws {Error} If the type definition is not valid
249
 */
250
export function unpack(buffer, theType, index=0) {
251
  setUp_(theType);
252
  if ((theType.offset + index) > buffer.length) {
253
    throw Error('Bad buffer length.');
254
  }
255
  if (theType.be) {
256
    endianness(buffer, theType.offset, index, index + theType.offset);
257
  }
258
  /** @type {number} */
259
  let value = reader_(buffer, index);
260
  if (theType.be) {
261
    endianness(buffer, theType.offset, index, index + theType.offset);
262
  }
263
  return value;
264
}
265
266
/**
267
 * Unpack an array of numbers from a byte buffer.
268
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
269
 * @param {!Object} theType The type definition.
270
 * @param {number=} index The start index. Assumes 0.
271
 * @param {?number=} end The end index. Assumes the buffer length.
272
 * @return {!Array<number>}
273
 * @throws {Error} If the type definition is not valid
274
 */
275
export function unpackArray(buffer, theType, index=0, end=buffer.length) {
276
  /** @type {!Array<!number>} */
277
  let output = [];
278
  unpackArrayTo(buffer, theType, output, index, end);
279
  return output;
280
}
281
282
/**
283
 * Unpack a array of numbers to a typed array.
284
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
285
 * @param {!Object} theType The type definition.
286
 * @param {!TypedArray|!Array<!number>} output The output array.
287
 * @param {number=} index The start index. Assumes 0.
288
 * @param {?number=} end The end index. Assumes the buffer length.
289
 * @throws {Error} If the type definition is not valid
290
 */
291
export function unpackArrayTo(buffer, theType, output, index=0, end=buffer.length) {
292
  setUp_(theType);
293
  while ((end - index) % theType.offset) {
294
      end--;
295
  }
296
  for (let i = 0; index < end; index += theType.offset, i++) {
0 ignored issues
show
The loop variable i is initialized by the loop but not used in the test. Consider using another type of loop if this is the intended behavior.
Loading history...
297
    output[i] = unpack(buffer, theType, index);
298
  }
299
}
300