1 | /* |
||
2 | * Copyright (c) 2018-2019 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 Encode and decode unsigned integers to and from byte buffers. |
||
27 | * @see https://github.com/rochars/uint-buffer |
||
28 | */ |
||
29 | |||
30 | /** @module uint-buffer */ |
||
31 | |||
32 | /** |
||
33 | * A class to write and read unsigned ints to and from byte buffers. |
||
34 | */ |
||
35 | export class UintBuffer { |
||
36 | |||
37 | /** |
||
38 | * @param {number} bits The number of bits used by the integer. |
||
39 | */ |
||
40 | View Code Duplication | constructor(bits) { |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
41 | /** |
||
42 | * The number of bits used by one number. |
||
43 | * @type {number} |
||
44 | */ |
||
45 | this.bits = bits; |
||
46 | /** |
||
47 | * The number of bytes used by one number. |
||
48 | * @type {number} |
||
49 | */ |
||
50 | this.bytes = bits < 8 ? 1 : Math.ceil(bits / 8); |
||
51 | /** |
||
52 | * @type {number} |
||
53 | * @protected |
||
54 | */ |
||
55 | this.max = Math.pow(2, bits) - 1; |
||
56 | /** |
||
57 | * @type {number} |
||
58 | * @protected |
||
59 | */ |
||
60 | this.min = 0; |
||
61 | /** @type {number} */ |
||
62 | let r = 8 - ((((bits - 1) | 7) + 1) - bits); |
||
63 | /** |
||
64 | * @type {number} |
||
65 | * @private |
||
66 | */ |
||
67 | this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Write one unsigned integer to a byte buffer. |
||
72 | * @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
||
73 | * @param {number} num The number. |
||
74 | * @param {number=} index The index being written in the byte buffer. |
||
75 | * @return {number} The next index to write on the byte buffer. |
||
76 | * @throws {TypeError} If num is not a number. |
||
77 | * @throws {RangeError} On overflow. |
||
78 | */ |
||
79 | pack(buffer, num, index=0) { |
||
80 | if (num !== num || typeof num != 'number') { |
||
81 | throw new TypeError(); |
||
82 | } |
||
83 | this.overflow(num); |
||
84 | buffer[index] = (num < 0 ? num + Math.pow(2, this.bits) : num) & 255; |
||
85 | index++; |
||
86 | /** @type {number} */ |
||
87 | let len = this.bytes; |
||
88 | for (let i = 2; i < len; i++) { |
||
89 | buffer[index] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255; |
||
90 | index++; |
||
91 | } |
||
92 | if (this.bits > 8) { |
||
93 | buffer[index] = Math.floor( |
||
94 | num / Math.pow(2, ((this.bytes - 1) * 8))) & this.lastByteMask_; |
||
95 | index++; |
||
96 | } |
||
97 | return index; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Read one unsigned integer from a byte buffer. |
||
102 | * @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
||
103 | * @param {number=} index The index to read. |
||
104 | * @return {number} The number. |
||
105 | * @throws {RangeError} On overflow. |
||
106 | */ |
||
107 | unpack(buffer, index=0) { |
||
108 | /** @type {number} */ |
||
109 | let num = this.unpackUnsafe(buffer, index); |
||
110 | this.overflow(num); |
||
111 | return num; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Read one unsigned integer from a byte buffer. |
||
116 | * Does not check for overflows. |
||
117 | * @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
||
118 | * @param {number} index The index to read. |
||
119 | * @return {number} |
||
120 | * @protected |
||
121 | */ |
||
122 | unpackUnsafe(buffer, index) { |
||
123 | /** @type {number} */ |
||
124 | let num = 0; |
||
125 | for(let x = 0; x < this.bytes; x++) { |
||
126 | num += buffer[index + x] * Math.pow(256, x); |
||
127 | } |
||
128 | return num; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Throws range error in case of overflow. |
||
133 | * @param {number} num The number. |
||
134 | * @throws {RangeError} On overflow. |
||
135 | * @protected |
||
136 | */ |
||
137 | overflow(num) { |
||
138 | if (num > this.max || num < this.min) { |
||
139 | throw new RangeError(); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 |