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 utf8BufferSize from 'utf8-buffer-size'; |
||
| 34 | import Packer from './lib/packer.js'; |
||
| 35 | import {validateNotUndefined, validateValueType} from './lib/validation.js'; |
||
| 36 | |||
| 37 | let packer = new Packer(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Read a string of UTF-8 characters from a byte buffer. |
||
| 41 | * @see https://encoding.spec.whatwg.org/#the-encoding |
||
| 42 | * @see https://stackoverflow.com/a/34926911 |
||
| 43 | * @param {!Uint8Array|!Array<!number>} buffer A byte buffer. |
||
| 44 | * @param {number=} index The index to read. |
||
| 45 | * @param {?number=} len The number of bytes to read. |
||
| 46 | * If len is undefined will read until the end of the buffer. |
||
| 47 | * @return {string} |
||
| 48 | */ |
||
| 49 | export function unpackString(buffer, index=0, len=undefined) { |
||
| 50 | len = len !== undefined ? index + len : buffer.length; |
||
| 51 | /** @type {string} */ |
||
| 52 | let str = ""; |
||
| 53 | while(index < len) { |
||
| 54 | /** @type {number} */ |
||
| 55 | let lowerBoundary = 0x80; |
||
| 56 | /** @type {number} */ |
||
| 57 | let upperBoundary = 0xBF; |
||
| 58 | /** @type {boolean} */ |
||
| 59 | let replace = false; |
||
| 60 | /** @type {number} */ |
||
| 61 | let charCode = buffer[index++]; |
||
| 62 | if (charCode >= 0x00 && charCode <= 0x7F) { |
||
| 63 | str += String.fromCharCode(charCode); |
||
| 64 | } else { |
||
| 65 | /** @type {number} */ |
||
| 66 | let count = 0; |
||
| 67 | if (charCode >= 0xC2 && charCode <= 0xDF) { |
||
| 68 | count = 1; |
||
| 69 | } else if (charCode >= 0xE0 && charCode <= 0xEF ) { |
||
| 70 | count = 2; |
||
| 71 | if (buffer[index] === 0xE0) { |
||
| 72 | lowerBoundary = 0xA0; |
||
| 73 | } |
||
| 74 | if (buffer[index] === 0xED) { |
||
| 75 | upperBoundary = 0x9F; |
||
| 76 | } |
||
| 77 | } else if (charCode >= 0xF0 && charCode <= 0xF4 ) { |
||
| 78 | count = 3; |
||
| 79 | if (buffer[index] === 0xF0) { |
||
| 80 | lowerBoundary = 0x90; |
||
| 81 | } |
||
| 82 | if (buffer[index] === 0xF4) { |
||
| 83 | upperBoundary = 0x8F; |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | replace = true; |
||
| 87 | } |
||
| 88 | charCode = charCode & (1 << (8 - count - 1)) - 1; |
||
| 89 | for (let i = 0; i < count; i++) { |
||
| 90 | if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) { |
||
| 91 | replace = true; |
||
| 92 | } |
||
| 93 | charCode = (charCode << 6) | (buffer[index] & 0x3f); |
||
| 94 | index++; |
||
| 95 | } |
||
| 96 | if (replace) { |
||
| 97 | str += String.fromCharCode(0xFFFD); |
||
| 98 | } |
||
| 99 | else if (charCode <= 0xffff) { |
||
| 100 | str += String.fromCharCode(charCode); |
||
| 101 | } else { |
||
| 102 | charCode -= 0x10000; |
||
| 103 | str += String.fromCharCode( |
||
| 104 | ((charCode >> 10) & 0x3ff) + 0xd800, |
||
| 105 | (charCode & 0x3ff) + 0xdc00); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | return str; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Write a string of UTF-8 characters as a byte buffer. |
||
| 114 | * @see https://encoding.spec.whatwg.org/#utf-8-encoder |
||
| 115 | * @param {string} str The string to pack. |
||
| 116 | * @return {!Uint8Array} The packed string. |
||
| 117 | */ |
||
| 118 | export function packString(str) { |
||
| 119 | /** @type {!Uint8Array} */ |
||
| 120 | let bytes = new Uint8Array(utf8BufferSize(str)); |
||
| 121 | let bufferIndex = 0; |
||
| 122 | for (let i = 0, len = str.length; i < len; i++) { |
||
| 123 | /** @type {number} */ |
||
| 124 | let codePoint = str.codePointAt(i); |
||
| 125 | if (codePoint < 128) { |
||
| 126 | bytes[bufferIndex] = codePoint; |
||
| 127 | bufferIndex++; |
||
| 128 | } else { |
||
| 129 | /** @type {number} */ |
||
| 130 | let count = 0; |
||
| 131 | /** @type {number} */ |
||
| 132 | let offset = 0; |
||
| 133 | if (codePoint <= 0x07FF) { |
||
| 134 | count = 1; |
||
| 135 | offset = 0xC0; |
||
| 136 | } else if(codePoint <= 0xFFFF) { |
||
| 137 | count = 2; |
||
| 138 | offset = 0xE0; |
||
| 139 | } else if(codePoint <= 0x10FFFF) { |
||
| 140 | count = 3; |
||
| 141 | offset = 0xF0; |
||
| 142 | i++; |
||
|
0 ignored issues
–
show
Complexity
Coding Style
introduced
by
Loading history...
|
|||
| 143 | } |
||
| 144 | bytes[bufferIndex] = (codePoint >> (6 * count)) + offset; |
||
| 145 | bufferIndex++; |
||
| 146 | while (count > 0) { |
||
| 147 | bytes[bufferIndex] = 0x80 | (codePoint >> (6 * (count - 1)) & 0x3F); |
||
| 148 | bufferIndex++; |
||
| 149 | count--; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return bytes; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Write a string of UTF-8 characters to a byte buffer. |
||
| 158 | * @param {string} str The string to pack. |
||
| 159 | * @param {!Uint8Array|!Array<number>} buffer The output buffer. |
||
| 160 | * @param {number=} index The buffer index to start writing. |
||
| 161 | * Assumes zero if undefined. |
||
| 162 | * @return {number} The next index to write in the buffer. |
||
| 163 | */ |
||
| 164 | export function packStringTo(str, buffer, index=0) { |
||
| 165 | /** @type {!Uint8Array} */ |
||
| 166 | let bytes = packString(str); |
||
| 167 | for (let i = 0, len = bytes.length; i < len; i++) { |
||
| 168 | buffer[index++] = bytes[i]; |
||
| 169 | } |
||
| 170 | return index; |
||
| 171 | } |
||
| 172 | |||
| 173 | // Numbers |
||
| 174 | /** |
||
| 175 | * Pack a number as a byte buffer. |
||
| 176 | * @param {number} value The number. |
||
| 177 | * @param {!Object} theType The type definition. |
||
| 178 | * @return {!Array<number>} The packed value. |
||
| 179 | * @throws {Error} If the type definition is not valid. |
||
| 180 | * @throws {Error} If the value is not valid. |
||
| 181 | */ |
||
| 182 | export function pack(value, theType) { |
||
| 183 | /** @type {!Array<!number>} */ |
||
| 184 | let output = []; |
||
| 185 | packTo(value, theType, output); |
||
| 186 | return output; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Pack a number to a byte buffer. |
||
| 191 | * @param {number} value The value. |
||
| 192 | * @param {!Object} theType The type definition. |
||
| 193 | * @param {!Uint8Array|!Array<number>} buffer The output buffer. |
||
| 194 | * @param {number=} index The buffer index to write. Assumes 0 if undefined. |
||
| 195 | * @return {number} The next index to write. |
||
| 196 | * @throws {Error} If the type definition is not valid. |
||
| 197 | * @throws {Error} If the value is not valid. |
||
| 198 | */ |
||
| 199 | export function packTo(value, theType, buffer, index=0) { |
||
| 200 | return packArrayTo([value], theType, buffer, index); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Pack an array of numbers as a byte buffer. |
||
| 205 | * @param {!Array<number>|!TypedArray} values The values. |
||
| 206 | * @param {!Object} theType The type definition. |
||
| 207 | * @return {!Array<number>} The packed values. |
||
| 208 | * @throws {Error} If the type definition is not valid. |
||
| 209 | * @throws {Error} If any of the values are not valid. |
||
| 210 | */ |
||
| 211 | export function packArray(values, theType) { |
||
| 212 | /** @type {!Array<!number>} */ |
||
| 213 | let output = []; |
||
| 214 | packArrayTo(values, theType, output); |
||
| 215 | return output; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Pack a array of numbers to a byte buffer. |
||
| 220 | * @param {!Array<number>|!TypedArray} values The value. |
||
| 221 | * @param {!Object} theType The type definition. |
||
| 222 | * @param {!Uint8Array|!Array<number>} buffer The output buffer. |
||
| 223 | * @param {number=} index The buffer index to start writing. |
||
| 224 | * Assumes zero if undefined. |
||
| 225 | * @return {number} The next index to write. |
||
| 226 | * @throws {Error} If the type definition is not valid. |
||
| 227 | * @throws {Error} If the value is not valid. |
||
| 228 | */ |
||
| 229 | export function packArrayTo(values, theType, buffer, index=0) { |
||
| 230 | packer.setUp(theType); |
||
| 231 | for (let i = 0, valuesLen = values.length; i < valuesLen; i++) { |
||
| 232 | validateNotUndefined(values[i]); |
||
| 233 | validateValueType(values[i]); |
||
| 234 | /** @type {number} */ |
||
| 235 | let len = index + theType.offset; |
||
| 236 | while (index < len) { |
||
| 237 | index = packer.write(buffer, values[i], index); |
||
| 238 | } |
||
| 239 | if (theType.be) { |
||
| 240 | endianness( |
||
| 241 | buffer, theType.offset, index - theType.offset, index); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | return index; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Unpack a number from a byte buffer. |
||
| 249 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 250 | * @param {!Object} theType The type definition. |
||
| 251 | * @param {number=} index The buffer index to read. Assumes zero if undefined. |
||
| 252 | * @return {number} |
||
| 253 | * @throws {Error} If the type definition is not valid |
||
| 254 | * @throws {Error} On bad buffer length. |
||
| 255 | */ |
||
| 256 | export function unpack(buffer, theType, index=0) { |
||
| 257 | packer.setUp(theType); |
||
| 258 | if ((theType.offset + index) > buffer.length) { |
||
| 259 | throw Error('Bad buffer length.'); |
||
| 260 | } |
||
| 261 | if (theType.be) { |
||
| 262 | endianness(buffer, theType.offset, index, index + theType.offset); |
||
| 263 | } |
||
| 264 | /** @type {number} */ |
||
| 265 | let value = packer.read(buffer, index); |
||
| 266 | if (theType.be) { |
||
| 267 | endianness(buffer, theType.offset, index, index + theType.offset); |
||
| 268 | } |
||
| 269 | return value; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Unpack an array of numbers from a byte buffer. |
||
| 274 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 275 | * @param {!Object} theType The type definition. |
||
| 276 | * @param {number=} index The buffer index to start reading. |
||
| 277 | * Assumes zero if undefined. |
||
| 278 | * @param {number=} end The buffer index to stop reading. |
||
| 279 | * Assumes the buffer length if undefined. |
||
| 280 | * @return {!Array<number>} |
||
| 281 | * @throws {Error} If the type definition is not valid |
||
| 282 | */ |
||
| 283 | export function unpackArray(buffer, theType, index=0, end=buffer.length) { |
||
| 284 | /** @type {!Array<!number>} */ |
||
| 285 | let output = []; |
||
| 286 | unpackArrayTo(buffer, theType, output, index, end); |
||
| 287 | return output; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Unpack a array of numbers to a typed array. |
||
| 292 | * @param {!Uint8Array|!Array<!number>} buffer The byte buffer. |
||
| 293 | * @param {!Object} theType The type definition. |
||
| 294 | * @param {!TypedArray|!Array<!number>} output The output array. |
||
| 295 | * @param {number=} index The buffer index to start reading. |
||
| 296 | * Assumes zero if undefined. |
||
| 297 | * @param {number=} end The buffer index to stop reading. |
||
| 298 | * Assumes the buffer length if undefined. |
||
| 299 | * @throws {Error} If the type definition is not valid |
||
| 300 | */ |
||
| 301 | export function unpackArrayTo( |
||
| 302 | buffer, theType, output, index=0, end=buffer.length) { |
||
| 303 | packer.setUp(theType); |
||
| 304 | while ((end - index) % theType.offset) { |
||
| 305 | end--; |
||
| 306 | } |
||
| 307 | for (let i = 0; index < end; index += theType.offset, i++) { |
||
|
0 ignored issues
–
show
|
|||
| 308 | output[i] = unpack(buffer, theType, index); |
||
| 309 | } |
||
| 310 | } |
||
| 311 |