Passed
Push — master ( 92462e...a1a5d5 )
by Rafael S.
02:26
created

index.js ➔ throwValueError_   A

Complexity

Conditions 5
Paths 0

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 0
nop 4
dl 0
loc 8
rs 9.3333
c 0
b 0
f 0
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 Functions to read and write numbers and strings as bytes.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/** @module byteData */
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
import {validateIsNumber} from './lib/validation.js';
36
37
/**
38
 * Throw a value error.
39
 * @throws {Error} A Error with a message based on the input params.
40
 */
41
function throwValueError_(e, value, i, fp) {
42
  if (!fp && (
43
      value === Infinity || value === -Infinity || value !== value)) {
44
    throw new Error('Argument is not a integer at input index ' + i);
45
  } else {
46
    throw new Error(e.message + ' at input index ' + i);
47
  }
48
}
49
50
/**
51
 * Read a string of UTF-8 characters from a byte buffer.
52
 * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
53
 * @param {number=} index The buffer index to start reading.
54
 * @param {number=} end The buffer index to stop reading, non inclusive.
55
 *   Assumes buffer length if undefined.
56
 * @return {string}
57
 */
58
export function unpackString(buffer, index=0, end=buffer.length) {
59
  return unpackUTF8(buffer, index, end);
60
}
61
62
/**
63
 * Write a string of UTF-8 characters as a byte buffer.
64
 * @param {string} str The string to pack.
65
 * @return {!Array<number>} The UTF-8 string bytes.
66
 */ 
67
export function packString(str) {
68
  /** @type {!Array<number>} */
69
  let buffer = [];
70
  packUTF8(str, buffer, 0);
71
  return buffer;
72
}
73
74
/**
75
 * Write a string of UTF-8 characters to a byte buffer.
76
 * @param {string} str The string to pack.
77
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
78
 * @param {number=} index The buffer index to start writing.
79
 *   Assumes zero if undefined.
80
 * @return {number} The next index to write in the buffer.
81
 */
82
export function packStringTo(str, buffer, index=0) {
83
  return packUTF8(str, buffer, index);
84
}
85
86
// Numbers
87
/**
88
 * Pack a array of numbers to a byte buffer.
89
 * All other packing functions are interfaces to this function.
90
 * @param {!Array<number>|!TypedArray} values The value.
91
 * @param {!Object} theType The type definition.
92
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
93
 * @param {number=} index The buffer index to start writing.
94
 *   Assumes zero if undefined.
95
 * @return {number} The next index to write.
96
 * @throws {Error} If the type definition is not valid.
97
 * @throws {Error} If the value is not valid.
98
 */
99
export function packArrayTo(values, theType, buffer, index=0) {
100
  theType = theType || {};
101
  /** @type {NumberBuffer} */
102
  let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed);
103
  /** @type {number} */
104
  let i = 0;
105
  /** @type {number} */
106
  let start = index;
107
  try {
108
    for (let valuesLen = values.length; i < valuesLen; i++) {
109
      validateIsNumber(values[i]);
110
      index = packer.pack(buffer, values[i], index);
111
    }
112
    if (theType.be) {
113
      endianness(buffer, packer.offset, start, index);
114
    }
115
  } catch (e) {
116
    throwValueError_(e, values[i], i, theType.fp);
117
  }
118
  return index;
119
}
120
121
/**
122
 * Unpack a array of numbers to a typed array.
123
 * All other unpacking functions are interfaces to this function.
124
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
125
 * @param {!Object} theType The type definition.
126
 * @param {!TypedArray|!Array<number>} output The output array.
127
 * @param {number=} start The buffer index to start reading.
128
 *   Assumes zero if undefined.
129
 * @param {number=} end The buffer index to stop reading.
130
 *   Assumes the buffer length if undefined.
131
 * @param {boolean=} safe If set to false, extra bytes in the end of
132
 *   the array are ignored and input buffers with insufficient bytes will
133
 *   write nothing to the output array. If safe is set to true the function
134
 *   will throw a 'Bad buffer length' error. Defaults to false.
135
 * @throws {Error} If the type definition is not valid
136
 * @throws {Error} On overflow
137
 */
138
export function unpackArrayTo(
139
    buffer, theType, output, start=0, end=buffer.length, safe=false) {
140
  theType = theType || {};
141
  /** @type {NumberBuffer} */
142
  let packer = new NumberBuffer(
143
    theType.bits, theType.fp, theType.signed);
144
  let offset = packer.offset;
145
  /** @type {number} */
146
  let extra = (end - start) % offset;
147
  if (safe && (extra || buffer.length < offset)) {
148
    throw new Error('Bad buffer length');
149
  }
150
  end -= extra;
151
  /** @type {number} */
152
  let index = 0;
153
  try {
154
    if (theType.be) {
155
      endianness(buffer, offset, start, end);
156
    }
157
    for (let j = start; j < end; j += offset, index++) {
158
      output[index] = packer.unpack(buffer, j);
159
    }
160
    if (theType.be) {
161
      endianness(buffer, offset, start, end);
162
    }
163
  } catch (e) {
164
    throw new Error(e.message + ' at output index ' + index);
165
  }
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 {Error} If the value is not valid.
175
 */
176
export function pack(value, theType) {
177
  /** @type {!Array<number>} */
178
  let output = [];
179
  packTo(value, theType, output);
180
  return output;
181
}
182
183
/**
184
 * Pack a number to a byte buffer.
185
 * @param {number} value The value.
186
 * @param {!Object} theType The type definition.
187
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
188
 * @param {number=} index The buffer index to write. Assumes 0 if undefined.
189
 * @return {number} The next index to write.
190
 * @throws {Error} If the type definition is not valid.
191
 * @throws {Error} If the value is not valid.
192
 */
193
export function packTo(value, theType, buffer, index=0) {
194
  return packArrayTo([value], theType, buffer, index);
195
}
196
197
/**
198
 * Pack an array of numbers as a byte buffer.
199
 * @param {!Array<number>|!TypedArray} values The values.
200
 * @param {!Object} theType The type definition.
201
 * @return {!Array<number>} The packed values.
202
 * @throws {Error} If the type definition is not valid.
203
 * @throws {Error} If any of the values are not valid.
204
 */
205
export function packArray(values, theType) {
206
  /** @type {!Array<number>} */
207
  let output = [];
208
  packArrayTo(values, theType, output);
209
  return output;
210
}
211
212
/**
213
 * Unpack a number from a byte buffer.
214
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
215
 * @param {!Object} theType The type definition.
216
 * @param {number=} index The buffer index to read. Assumes zero if undefined.
217
 * @return {number}
218
 * @throws {Error} If the type definition is not valid
219
 * @throws {Error} On bad buffer length.
220
 * @throws {Error} On overflow
221
 */
222
export function unpack(buffer, theType, index=0) {
223
  return unpackArray(
224
    buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0];
225
}
226
227
/**
228
 * Unpack an array of numbers from a byte buffer.
229
 * @param {!Uint8Array|!Array<number>} buffer The byte buffer.
230
 * @param {!Object} theType The type definition.
231
 * @param {number=} start The buffer index to start reading.
232
 *   Assumes zero if undefined.
233
 * @param {number=} end The buffer index to stop reading.
234
 *   Assumes the buffer length if undefined.
235
 * @param {boolean=} safe If set to false, extra bytes in the end of
236
 *   the array are ignored and input buffers with insufficient bytes will
237
 *   output a empty array. If safe is set to true the function
238
 *   will throw a 'Bad buffer length' error. Defaults to false.
239
 * @return {!Array<number>}
240
 * @throws {Error} If the type definition is not valid
241
 * @throws {Error} On overflow
242
 */
243
export function unpackArray(
244
    buffer, theType, start=0, end=buffer.length, safe=false) {
245
  /** @type {!Array<number>} */
246
  let output = [];
247
  unpackArrayTo(buffer, theType, output, start, end, safe);
248
  return output;
249
}
250