Passed
Push — master ( 0c37db...f2bf13 )
by Rafael S.
01:40
created

main.js (1 issue)

Severity
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 {reader_, setUp_, writer_} from './lib/packer.js';
34
import {validateNotUndefined, validateValueType} from './lib/validation.js';
35
36
const UTF8_ERROR = 'Invalid UTF-8 character.';
0 ignored issues
show
The constant UTF8_ERROR seems to be never used. Consider removing it.
Loading history...
37
38
/**
39
 * Read a string of UTF-8 characters from a byte buffer.
40
 * @see https://encoding.spec.whatwg.org/#the-encoding
41
 * @see https://stackoverflow.com/a/34926911
42
 * @param {!Uint8Array|!Array<!number>} buffer A byte buffer.
43
 * @param {number=} index The index to read.
44
 * @param {?number=} len The number of bytes to read.
45
 * @return {string}
46
 * @throws {Error} If read a value that is not UTF-8.
47
 */
48
export function unpackString(buffer, index=0, len=null) {
49
  len = len !== null ? index + len : buffer.length;
50
  /** @type {string} */
51
  let str = "";
52
  while(index < len) {
53
    /** @type {number} */
54
    let lowerBoundary = 0x80;
55
    /** @type {number} */
56
    let upperBoundary = 0xBF;
57
    /** @type {boolean} */
58
    let replace = false;
59
    /** @type {number} */
60
    let charCode = buffer[index++];
61
    if (charCode >= 0x00 && charCode <= 0x7F) {
62
      str += String.fromCharCode(charCode);
63
    } else {
64
      /** @type {number} */
65
      let count = 0;
66
      if (charCode >= 0xC2 && charCode <= 0xDF) {
67
        count = 1;
68
      } else if (charCode >= 0xE0 && charCode <= 0xEF ) {
69
        count = 2;
70
        if (buffer[index] === 0xE0) {
71
          lowerBoundary = 0xA0;
72
        }
73
        if (buffer[index] === 0xED) {
74
          upperBoundary = 0x9F;
75
        }
76
      } else if (charCode >= 0xF0 && charCode <= 0xF4 ) {
77
        count = 3;
78
        if (buffer[index] === 0xF0) {
79
          lowerBoundary = 0x90;
80
        }
81
        if (buffer[index] === 0xF4) {
82
          upperBoundary = 0x8F;
83
        }
84
      } else {
85
        //throw new Error(UTF8_ERROR);
86
        replace = true;
87
      }
88
      charCode = charCode & (1 << (8 - count - 1)) - 1;
89
      for (let i = 0; i < count; i++) {
90
        if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) {
91
          //throw new Error(UTF8_ERROR);
92
          replace = true;
93
          //break;
94
        }
95
        //else {
96
          charCode = (charCode << 6) | (buffer[index] & 0x3f);
97
        //}
98
        index++;
99
      }
100
      if (replace) {
101
        str += String.fromCharCode(0xFFFD);
102
      } 
103
      else if (charCode <= 0xffff) {
104
        str += String.fromCharCode(charCode);
105
      } else {
106
        charCode -= 0x10000;
107
        str += String.fromCharCode(
108
          ((charCode >> 10) & 0x3ff) + 0xd800,
109
          (charCode & 0x3ff) + 0xdc00);
110
      }
111
    }
112
  }
113
  return str;
114
}
115
116
/**
117
 * Write a string of UTF-8 characters as a byte buffer.
118
 * @see https://encoding.spec.whatwg.org/#utf-8-encoder
119
 * @param {string} str The string to pack.
120
 * @return {!Array<number>} The packed string.
121
 */
122
export function packString(str) {
123
  /** @type {!Array<!number>} */
124
  let bytes = [];
125
  for (let i = 0; i < str.length; i++) {
126
    /** @type {number} */
127
    let codePoint = str.codePointAt(i);
128
    if (codePoint < 128) {
129
      bytes.push(codePoint);
130
    } else {
131
      /** @type {number} */
132
      let count = 0;
133
      /** @type {number} */
134
      let offset = 0;
135
      if (codePoint <= 0x07FF) {
136
        count = 1;
137
        offset = 0xC0;
138
      } else if(codePoint <= 0xFFFF) {
139
        count = 2;
140
        offset = 0xE0;
141
      } else if(codePoint <= 0x10FFFF) {
142
        count = 3;
143
        offset = 0xF0;
144
        i++;
145
      }
146
      bytes.push((codePoint >> (6 * count)) + offset);
147
      while (count > 0) {
148
        bytes.push(0x80 | (codePoint >> (6 * (count - 1)) & 0x3F));
149
        count--;
150
      }
151
    }
152
  }
153
  return bytes;
154
}
155
156
/**
157
 * Write a string of UTF-8 characters to a byte buffer.
158
 * @param {string} str The string to pack.
159
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
160
 * @param {number=} index The index to write in the buffer.
161
 * @return {number} The next index to write in the buffer.
162
 */
163
export function packStringTo(str, buffer, index=0) {
164
  /** @type {!Array<!number>} */
165
  let bytes = packString(str);
166
  for (let i = 0; i < bytes.length; i++) {
167
    buffer[index++] = bytes[i];
168
  }
169
  return index;
170
}
171
172
// Numbers
173
/**
174
 * Pack a number as a byte buffer.
175
 * @param {number} value The number.
176
 * @param {!Object} theType The type definition.
177
 * @return {!Array<number>} The packed value.
178
 * @throws {Error} If the type definition is not valid.
179
 * @throws {Error} If the value is not valid.
180
 */
181
export function pack(value, theType) {
182
  /** @type {!Array<!number>} */
183
  let output = [];
184
  packTo(value, theType, output);
185
  return output;
186
}
187
188
/**
189
 * Pack a number to a byte buffer.
190
 * @param {number} value The value.
191
 * @param {!Object} theType The type definition.
192
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
193
 * @param {number=} index The index to write.
194
 * @return {number} The next index to write.
195
 * @throws {Error} If the type definition is not valid.
196
 * @throws {Error} If the value is not valid.
197
 */
198
export function packTo(value, theType, buffer, index=0) {
199
  return packArrayTo([value], theType, buffer, index);
200
}
201
202
/**
203
 * Pack an array of numbers as a byte buffer.
204
 * @param {!Array<number>|!TypedArray} values The values.
205
 * @param {!Object} theType The type definition.
206
 * @return {!Array<number>} The packed values.
207
 * @throws {Error} If the type definition is not valid.
208
 * @throws {Error} If any of the values are not valid.
209
 */
210
export function packArray(values, theType) {
211
  /** @type {!Array<!number>} */
212
  let output = [];
213
  packArrayTo(values, theType, output);
214
  return output;
215
}
216
217
/**
218
 * Pack a array of numbers to a byte buffer.
219
 * @param {!Array<number>|!TypedArray} values The value.
220
 * @param {!Object} theType The type definition.
221
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
222
 * @param {number=} index The buffer index to write.
223
 * @return {number} The next index to write.
224
 * @throws {Error} If the type definition is not valid.
225
 * @throws {Error} If the value is not valid.
226
 */
227
export function packArrayTo(values, theType, buffer, index=0) {
228
  setUp_(theType);
229
  for (let i=0; i < values.length; i++) {
230
    validateNotUndefined(values[i]);
231
    validateValueType(values[i]);
232
    /** @type {number} */
233
    let len = index + theType.offset;
234
    while (index < len) {
235
      index = writer_(buffer, values[i], index);
236
    }
237
    if (theType.be) {
238
      endianness(
239
        buffer, theType.offset, index - theType.offset, index);
240
    }
241
  }
242
  return index;
243
}
244
245
/**
246
 * Unpack a number from a byte buffer.
247
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
248
 * @param {!Object} theType The type definition.
249
 * @param {number=} index The buffer index to read.
250
 * @return {number}
251
 * @throws {Error} If the type definition is not valid
252
 */
253
export function unpack(buffer, theType, index=0) {
254
  setUp_(theType);
255
  if ((theType.offset + index) > buffer.length) {
256
    throw Error('Bad buffer length.');
257
  }
258
  if (theType.be) {
259
    endianness(buffer, theType.offset, index, index + theType.offset);
260
  }
261
  /** @type {number} */
262
  let value = reader_(buffer, index);
263
  if (theType.be) {
264
    endianness(buffer, theType.offset, index, index + theType.offset);
265
  }
266
  return value;
267
}
268
269
/**
270
 * Unpack an array of numbers from a byte buffer.
271
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
272
 * @param {!Object} theType The type definition.
273
 * @param {number=} index The start index. Assumes 0.
274
 * @param {?number=} end The end index. Assumes the buffer length.
275
 * @return {!Array<number>}
276
 * @throws {Error} If the type definition is not valid
277
 */
278
export function unpackArray(buffer, theType, index=0, end=buffer.length) {
279
  /** @type {!Array<!number>} */
280
  let output = [];
281
  unpackArrayTo(buffer, theType, output, index, end);
282
  return output;
283
}
284
285
/**
286
 * Unpack a array of numbers to a typed array.
287
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
288
 * @param {!Object} theType The type definition.
289
 * @param {!TypedArray|!Array<!number>} output The output array.
290
 * @param {number=} index The start index. Assumes 0.
291
 * @param {?number=} end The end index. Assumes the buffer length.
292
 * @throws {Error} If the type definition is not valid
293
 */
294
export function unpackArrayTo(buffer, theType, output, index=0, end=buffer.length) {
295
  setUp_(theType);
296
  while ((end - index) % theType.offset) {
297
      end--;
298
  }
299
  for (let i = 0; index < end; index += theType.offset, i++) {
300
    output[i] = unpack(buffer, theType, index);
301
  }
302
}
303