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