Passed
Push — master ( 1bcff0...a616ee )
by Rafael S.
02:01
created

index.js ➔ packArrayTo   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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