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