Passed
Push — master ( 25f292...8ce9b5 )
by Rafael S.
02:03
created

index.js ➔ unpackArrayTo   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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