rochars /
byte-data
| 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 Packer from './lib/packer.js'; |
||
| 34 | import {validateNotUndefined, validateValueType} from './lib/validation.js'; |
||
| 35 | import {pack as packUTF8, unpack as unpackUTF8} from './lib/utf8-buffer.js'; |
||
| 36 | |||
| 37 | /** @type {Packer} */ |
||
| 38 | let packer = new Packer(); |
||
| 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 index to read. |
||
| 44 | * @param {?number=} len The number of bytes to read. |
||
| 45 | * If len is undefined will read until the end of the buffer. |
||
| 46 | * @return {string} |
||
| 47 | */ |
||
| 48 | export function unpackString(buffer, index=0, len=undefined) { |
||
| 49 | return unpackUTF8(buffer, index, len); |
||
| 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 packed string. |
||
| 56 | */ |
||
| 57 | export function packString(str) { |
||
| 58 | return packUTF8(str); |
||
| 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 | /** @type {!Uint8Array} */ |
||
| 71 | let bytes = packString(str); |
||
| 72 | let len = bytes.length; |
||
| 73 | for (let i = 0; i < len; i++) { |
||
| 74 | buffer[index++] = bytes[i]; |
||
| 75 | } |
||
| 76 | return index; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Numbers |
||
| 80 | /** |
||
| 81 | * Pack a number as a byte buffer. |
||
| 82 | * @param {number} value The number. |
||
| 83 | * @param {!Object} theType The type definition. |
||
| 84 | * @return {!Array<number>} The packed value. |
||
| 85 | * @throws {Error} If the type definition is not valid. |
||
| 86 | * @throws {Error} If the value is not valid. |
||
| 87 | */ |
||
| 88 | export function pack(value, theType) { |
||
| 89 | /** @type {!Array<!number>} */ |
||
| 90 | let output = []; |
||
| 91 | packTo(value, theType, output); |
||
| 92 | return output; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Pack a number to a byte buffer. |
||
| 97 | * @param {number} value The value. |
||
| 98 | * @param {!Object} theType The type definition. |
||
| 99 | * @param {!Uint8Array|!Array<number>} buffer The output buffer. |
||
| 100 | * @param {number=} index The buffer index to write. Assumes 0 if undefined. |
||
| 101 | * @return {number} The next index to write. |
||
| 102 | * @throws {Error} If the type definition is not valid. |
||
| 103 | * @throws {Error} If the value is not valid. |
||
| 104 | */ |
||
| 105 | export function packTo(value, theType, buffer, index=0) { |
||
| 106 | return packArrayTo([value], theType, buffer, index); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Pack an array of numbers as a byte buffer. |
||
| 111 | * @param {!Array<number>|!TypedArray} values The values. |
||
| 112 | * @param {!Object} theType The type definition. |
||
| 113 | * @return {!Array<number>} The packed values. |
||
| 114 | * @throws {Error} If the type definition is not valid. |
||
| 115 | * @throws {Error} If any of the values are not valid. |
||
| 116 | */ |
||
| 117 | export function packArray(values, theType) { |
||
| 118 | /** @type {!Array<!number>} */ |
||
| 119 | let output = []; |
||
| 120 | packArrayTo(values, theType, output); |
||
| 121 | return output; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Pack a array of numbers to a byte buffer. |
||
| 126 | * @param {!Array<number>|!TypedArray} values The value. |
||
| 127 | * @param {!Object} theType The type definition. |
||
| 128 | * @param {!Uint8Array|!Array<number>} buffer The output buffer. |
||
| 129 | * @param {number=} index The buffer index to start writing. |
||
| 130 | * Assumes zero if undefined. |
||
| 131 | * @return {number} The next index to write. |
||
| 132 | * @throws {Error} If the type definition is not valid. |
||
| 133 | * @throws {Error} If the value is not valid. |
||
| 134 | */ |
||
| 135 | export function packArrayTo(values, theType, buffer, index=0) { |
||
| 136 | packer.setUp(theType); |
||
| 137 | let valuesLen = values.length; |
||
| 138 | for (let i = 0; i < valuesLen; i++) { |
||
| 139 | validateNotUndefined(values[i]); |
||
| 140 | validateValueType(values[i]); |
||
| 141 | /** @type {number} */ |
||
| 142 | let len = index + packer.offset; |
||
| 143 | while (index < len) { |
||
| 144 | index = packer.write(buffer, values[i], index); |
||
| 145 | } |
||
| 146 | if (theType.be) { |
||
| 147 | endianness( |
||
| 148 | buffer, packer.offset, index - packer.offset, index); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | return index; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Unpack a number from a byte buffer. |
||
| 156 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 157 | * @param {!Object} theType The type definition. |
||
| 158 | * @param {number=} index The buffer index to read. Assumes zero if undefined. |
||
| 159 | * @return {number} |
||
| 160 | * @throws {Error} If the type definition is not valid |
||
| 161 | * @throws {Error} On bad buffer length. |
||
| 162 | */ |
||
| 163 | export function unpack(buffer, theType, index=0) { |
||
| 164 | packer.setUp(theType); |
||
| 165 | if ((packer.offset + index) > buffer.length) { |
||
| 166 | throw Error('Bad buffer length.'); |
||
| 167 | } |
||
| 168 | if (theType.be) { |
||
| 169 | endianness(buffer, packer.offset, index, index + packer.offset); |
||
| 170 | } |
||
| 171 | /** @type {number} */ |
||
| 172 | let value = packer.read(buffer, index); |
||
| 173 | if (theType.be) { |
||
| 174 | endianness(buffer, packer.offset, index, index + packer.offset); |
||
| 175 | } |
||
| 176 | return value; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Unpack an array of numbers from a byte buffer. |
||
| 181 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 182 | * @param {!Object} theType The type definition. |
||
| 183 | * @param {number=} index The buffer index to start reading. |
||
| 184 | * Assumes zero if undefined. |
||
| 185 | * @param {number=} end The buffer index to stop reading. |
||
| 186 | * Assumes the buffer length if undefined. |
||
| 187 | * @return {!Array<number>} |
||
| 188 | * @throws {Error} If the type definition is not valid |
||
| 189 | */ |
||
| 190 | export function unpackArray(buffer, theType, index=0, end=buffer.length) { |
||
| 191 | /** @type {!Array<!number>} */ |
||
| 192 | let output = []; |
||
| 193 | unpackArrayTo(buffer, theType, output, index, end); |
||
| 194 | return output; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Unpack a array of numbers to a typed array. |
||
| 199 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 200 | * @param {!Object} theType The type definition. |
||
| 201 | * @param {!TypedArray|!Array<!number>} output The output array. |
||
| 202 | * @param {number=} index The buffer index to start reading. |
||
| 203 | * Assumes zero if undefined. |
||
| 204 | * @param {number=} end The buffer index to stop reading. |
||
| 205 | * Assumes the buffer length if undefined. |
||
| 206 | * @throws {Error} If the type definition is not valid |
||
| 207 | */ |
||
| 208 | export function unpackArrayTo( |
||
| 209 | buffer, theType, output, index=0, end=buffer.length) { |
||
| 210 | packer.setUp(theType); |
||
| 211 | /** @type {number} */ |
||
| 212 | let originalIndex = index; |
||
| 213 | while ((end - index) % packer.offset) { |
||
| 214 | end--; |
||
| 215 | } |
||
| 216 | if (theType.be) { |
||
| 217 | endianness(buffer, packer.offset, index, end); |
||
| 218 | } |
||
| 219 | for (let i = 0; index < end; index += packer.offset) { |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 220 | output[i] = packer.read(buffer, index); |
||
| 221 | i++; |
||
| 222 | } |
||
| 223 | if (theType.be) { |
||
| 224 | endianness(buffer, packer.offset, originalIndex, end); |
||
| 225 | } |
||
| 226 | } |
||
| 227 |