Passed
Branch v18.x (43731d)
by Rafael S.
02:04
created

index.js (2 issues)

Labels
Severity
1
/*
2
 * Copyright (c) 2017-2019 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 JavaScript binary parser for any browser or environment.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/** @module byte-data */
31
32
import endianness from 'endianness';
33
import {pack as packUTF8, unpack as unpackUTF8} from 'utf8-buffer';
34
import NumberBuffer from './lib/number-buffer.js';
35
36
/**
37
 * Read a string of UTF-8 characters from a byte buffer.
38
 * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
39
 * @param {number=} index The buffer index to start reading.
40
 * @param {number=} end The buffer index to stop reading, non inclusive.
41
 *   Assumes buffer length if undefined.
42
 * @return {string}
43
 */
44
export function unpackString(buffer, index=0, end=buffer.length) {
45
  return unpackUTF8(buffer, index, end);
46
}
47
48
/**
49
 * Write a string of UTF-8 characters as a byte buffer.
50
 * @param {string} str The string to pack.
51
 * @return {!Array<number>} The UTF-8 string bytes.
52
 */ 
53
export function packString(str) {
54
  /** @type {!Array<number>} */
55
  let buffer = [];
56
  packUTF8(str, buffer, 0);
57
  return buffer;
58
}
59
60
/**
61
 * Write a string of UTF-8 characters to a byte buffer.
62
 * @param {string} str The string to pack.
63
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
64
 * @param {number=} index The buffer index to start writing.
65
 *   Assumes zero if undefined.
66
 * @return {number} The next index to write in the buffer.
67
 */
68
export function packStringTo(str, buffer, index=0) {
69
  return packUTF8(str, buffer, index);
70
}
71
72
// Numbers
73
/**
74
 * Pack a array of numbers to a byte buffer.
75
 * All other packing functions are interfaces to this function.
76
 * @param {!Array<number>|!TypedArray} values The value.
77
 * @param {!Object} theType The type definition.
78
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
79
 * @param {number=} index The buffer index to start writing.
80
 *   Assumes zero if undefined.
81
 * @return {number} The next index to write.
82
 * @throws {Error} If the type definition is not valid.
83
 * @throws {RangeError} On overflow.
84
 * @throws {TypeError} If input is not valid.
85
 */
86
export function packArrayTo(values, theType, buffer, index=0) {
87
  theType = theType || {};
88
  /** @type {NumberBuffer} */
89
  let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed);
90
  /** @type {number} */
91
  let i = 0;
92
  /** @type {number} */
93
  let start = index;
94
  try {
95
    for (let valuesLen = values.length; i < valuesLen; i++) {
96
      index = packer.pack(buffer, values[i], index);
97
    }
98
    if (theType.be) {
99
      endianness(buffer, packer.offset, start, index);
100
    }
101
  } catch (e) {
102
    throwValueError_(e, values[i], i, theType.fp);
0 ignored issues
show
The call to throwValueError_ seems to have too many arguments starting with theType.fp.
Loading history...
103
  }
104
  return index;
105
}
106
107
/**
108
 * Unpack a array of numbers to a typed array.
109
 * All other unpacking functions are interfaces to this function.
110
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
111
 * @param {!Object} theType The type definition.
112
 * @param {!TypedArray|!Array<number>} output The output array.
113
 * @param {number=} start The buffer index to start reading.
114
 *   Assumes zero if undefined.
115
 * @param {number=} end The buffer index to stop reading.
116
 *   Assumes the buffer length if undefined.
117
 * @param {boolean=} safe If set to false, extra bytes in the end of
118
 *   the array are ignored and input buffers with insufficient bytes will
119
 *   write nothing to the output array. If safe is set to true the function
120
 *   will throw a 'Bad buffer length' error. Defaults to false.
121
 * @throws {Error} If the type definition is not valid
122
 * @throws {RangeError} On overflow
123
 */
124
export function unpackArrayTo(
125
    buffer, theType, output, start=0, end=buffer.length, safe=false) {
126
  theType = theType || {};
127
  /** @type {NumberBuffer} */
128
  let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed);
129
  /** @type {number} */
130
  let offset = packer.offset;
131
  // getUnpackLen_ will either fix the length of the input buffer
132
  // according to the byte offset of the type (on unsafe mode) or
133
  // throw a Error if the input buffer has a bad length (on safe mode)
134
  end = getUnpackLen_(buffer, start, end, offset, safe);
135
  /** @type {number} */
136
  let index = 0;
137
  let j = start;
138
  try {
139
    if (theType.be) {
140
      endianness(buffer, offset, start, end);
141
    }
142
    for (; j < end; j += offset, index++) {
143
      output[index] = packer.unpack(buffer, j);
144
    }
145
    if (theType.be) {
146
      endianness(buffer, offset, start, end);
147
    }
148
  } catch (e) {
149
    throwValueError_(e, buffer.slice(j, j + offset), j, theType.fp);
0 ignored issues
show
The call to throwValueError_ seems to have too many arguments starting with theType.fp.
Loading history...
150
  }
151
}
152
153
/**
154
 * Pack a number to a byte buffer.
155
 * @param {number} value The value.
156
 * @param {!Object} theType The type definition.
157
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
158
 * @param {number=} index The buffer index to write. Assumes 0 if undefined.
159
 * @return {number} The next index to write.
160
 * @throws {Error} If the type definition is not valid.
161
 * @throws {RangeError} On overflow.
162
 * @throws {TypeError} If input is not valid.
163
 */
164
export function packTo(value, theType, buffer, index=0) {
165
  return packArrayTo([value], theType, buffer, index);
166
}
167
168
/**
169
 * Pack a number as a byte buffer.
170
 * @param {number} value The number.
171
 * @param {!Object} theType The type definition.
172
 * @return {!Array<number>} The packed value.
173
 * @throws {Error} If the type definition is not valid.
174
 * @throws {RangeError} On overflow.
175
 * @throws {TypeError} If input is not valid.
176
 */
177
export function pack(value, theType) {
178
  /** @type {!Array<number>} */
179
  let output = [];
180
  packTo(value, theType, output);
181
  return output;
182
}
183
184
/**
185
 * Pack an array of numbers as a byte buffer.
186
 * @param {!Array<number>|!TypedArray} values The values.
187
 * @param {!Object} theType The type definition.
188
 * @return {!Array<number>} The packed values.
189
 * @throws {Error} If the type definition is not valid.
190
 * @throws {RangeError} On overflow.
191
 * @throws {TypeError} If input is not valid.
192
 */
193
export function packArray(values, theType) {
194
  /** @type {!Array<number>} */
195
  let output = [];
196
  packArrayTo(values, theType, output);
197
  return output;
198
}
199
200
/**
201
 * Unpack an array of numbers from a byte buffer.
202
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
203
 * @param {!Object} theType The type definition.
204
 * @param {number=} start The buffer index to start reading.
205
 *   Assumes zero if undefined.
206
 * @param {number=} end The buffer index to stop reading.
207
 *   Assumes the buffer length if undefined.
208
 * @param {boolean=} safe If set to false, extra bytes in the end of
209
 *   the array are ignored and input buffers with insufficient bytes will
210
 *   output a empty array. If safe is set to true the function
211
 *   will throw a 'Bad buffer length' error. Defaults to false.
212
 * @return {!Array<number>}
213
 * @throws {Error} If the type definition is not valid
214
 * @throws {RangeError} On overflow
215
 */
216
export function unpackArray(
217
    buffer, theType, start=0, end=buffer.length, safe=false) {
218
  /** @type {!Array<number>} */
219
  let output = [];
220
  unpackArrayTo(buffer, theType, output, start, end, safe);
221
  return output;
222
}
223
224
/**
225
 * Unpack a number from a byte buffer.
226
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
227
 * @param {!Object} theType The type definition.
228
 * @param {number=} index The buffer index to read. Assumes zero if undefined.
229
 * @return {number}
230
 * @throws {Error} If the type definition is not valid
231
 * @throws {Error} On bad buffer length.
232
 * @throws {RangeError} On overflow
233
 */
234
export function unpack(buffer, theType, index=0) {
235
  return unpackArray(
236
    buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0];
237
}
238
239
/**
240
 * Throw a error with information about the problem.
241
 * @param {!Object} e The Error object that is being raised.
242
 * @param {*} value The value that caused the error.
243
 * @param {number} index The index of the value that caused the error.
244
 * @throws {RangeError|TypeError|Error} A Error with a message.
245
 * @private
246
 */
247
function throwValueError_(err, value, index) {
248
  err.message = err.constructor.name +
249
    ' at index ' + index + ': ' + value;
250
  throw err;
251
}
252
253
/**
254
 * Unpack a array of numbers to a typed array.
255
 * All other unpacking functions are interfaces to this function.
256
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
257
 * @param {number=} start The buffer index to start reading.
258
 * @param {number=} end The buffer index to stop reading.
259
 * @param {number=} offset The number of bytes used by the type.
260
 * @param {boolean=} safe True for size-safe buffer reading.
261
 * @throws {Error} On bad buffer length, if safe.
262
 * @private
263
 */
264
function getUnpackLen_(buffer, start, end, offset, safe) {
265
  /** @type {number} */
266
  let extra = (end - start) % offset;
267
  if (safe && (extra || buffer.length < offset)) {
268
    throw new Error('Bad buffer length');
269
  }
270
  return end - extra;
271
}
272