|
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 Encode and decode int numbers to and from byte buffers. |
|
27
|
|
|
* @see https://github.com/rochars/byte-data |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* A class to write and read integer numbers to and from byte buffers. |
|
32
|
|
|
*/ |
|
33
|
|
|
export class IntParser { |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param {number} bits The number of bits used by the integer. |
|
37
|
|
|
*/ |
|
38
|
|
|
constructor(bits, signed, clamp) { |
|
39
|
|
|
/** |
|
40
|
|
|
* The number of bits used by one number. |
|
41
|
|
|
* @type {number} |
|
42
|
|
|
*/ |
|
43
|
|
|
this.bits = bits; |
|
44
|
|
|
/** |
|
45
|
|
|
* The number of bytes used by one number. |
|
46
|
|
|
* @type {number} |
|
47
|
|
|
*/ |
|
48
|
|
|
this.bytes = bits < 8 ? 1 : Math.ceil(bits / 8); |
|
49
|
|
|
/** |
|
50
|
|
|
* @type {number} |
|
51
|
|
|
* @protected |
|
52
|
|
|
*/ |
|
53
|
|
|
this.max = Math.pow(2, bits) - 1; |
|
54
|
|
|
/** |
|
55
|
|
|
* @type {number} |
|
56
|
|
|
* @protected |
|
57
|
|
|
*/ |
|
58
|
|
|
this.min = 0; |
|
59
|
|
|
/** @type {number} */ |
|
60
|
|
|
let r = 8 - ((((bits - 1) | 7) + 1) - bits); |
|
61
|
|
|
/** |
|
62
|
|
|
* @type {number} |
|
63
|
|
|
* @private |
|
64
|
|
|
*/ |
|
65
|
|
|
this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1; |
|
66
|
|
|
/** |
|
67
|
|
|
* @type {Function} |
|
68
|
|
|
* @protected |
|
69
|
|
|
*/ |
|
70
|
|
|
this.unpack = this.unpackUnsigned_; |
|
71
|
|
|
if (signed) { |
|
72
|
|
|
this.max = Math.pow(2, bits) / 2 - 1; |
|
73
|
|
|
this.min = -this.max - 1; |
|
74
|
|
|
this.unpack = this.unpackSigned_; |
|
75
|
|
|
} |
|
76
|
|
|
if (clamp) { |
|
77
|
|
|
this.overflow_ = this.overflowClamp_; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Write one unsigned integer to a byte buffer. |
|
83
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
|
84
|
|
|
* @param {number} num The number. Overflows are truncated. |
|
85
|
|
|
* @param {number=} index The index being written in the byte buffer. |
|
86
|
|
|
* @return {number} The next index to write on the byte buffer. |
|
87
|
|
|
*/ |
|
88
|
|
|
pack(buffer, num, index=0) { |
|
89
|
|
|
if (num !== num || num.constructor != Number) { |
|
90
|
|
|
throw new TypeError(); |
|
91
|
|
|
} |
|
92
|
|
|
num = this.overflow_(num); |
|
93
|
|
|
buffer[index] = (num < 0 ? num + Math.pow(2, this.bits) : num) & 255; |
|
94
|
|
|
index++; |
|
95
|
|
|
for (let i = 2, len = this.bytes; i < len; i++) { |
|
96
|
|
|
buffer[index] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255; |
|
97
|
|
|
index++; |
|
98
|
|
|
} |
|
99
|
|
|
if (this.bits > 8) { |
|
100
|
|
|
buffer[index] = Math.floor( |
|
101
|
|
|
num / Math.pow(2, ((this.bytes - 1) * 8))) & this.lastByteMask_; |
|
102
|
|
|
index++; |
|
103
|
|
|
} |
|
104
|
|
|
return index; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Read one unsigned integer from a byte buffer. |
|
109
|
|
|
* Does not check for overflows. |
|
110
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
|
111
|
|
|
* @param {number=} index The index to read. |
|
112
|
|
|
* @return {number} |
|
113
|
|
|
*/ |
|
114
|
|
|
unpack_(buffer, index=0) { |
|
115
|
|
|
/** @type {number} */ |
|
116
|
|
|
let num = 0; |
|
117
|
|
|
for(let x = 0; x < this.bytes; x++) { |
|
118
|
|
|
num += buffer[index + x] * Math.pow(256, x); |
|
119
|
|
|
} |
|
120
|
|
|
return num; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
unpackUnsigned_(buffer, index=0) { |
|
124
|
|
|
return this.overflow_(this.unpack_(buffer, index)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Read one two's complement signed integer from a byte buffer. |
|
129
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
|
130
|
|
|
* @param {number=} index The index to read. |
|
131
|
|
|
* @return {number} |
|
132
|
|
|
*/ |
|
133
|
|
|
unpackSigned_(buffer, index=0) { |
|
134
|
|
|
return this.overflow_(this.sign_(this.unpack_(buffer, index))); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Truncate values in case of overflow. |
|
139
|
|
|
* @param {number} num The number. |
|
140
|
|
|
* @private |
|
141
|
|
|
*/ |
|
142
|
|
|
overflow_(num) { |
|
143
|
|
|
if (num > this.max || num < this.min) { |
|
144
|
|
|
throw new RangeError(); |
|
145
|
|
|
} |
|
146
|
|
|
return num; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Truncate values in case of overflow. |
|
151
|
|
|
* @param {number} num The number. |
|
152
|
|
|
* @private |
|
153
|
|
|
*/ |
|
154
|
|
|
overflowClamp_(num) { |
|
155
|
|
|
if (num > this.max) { |
|
156
|
|
|
return this.max; |
|
157
|
|
|
} else if (num < this.min) { |
|
158
|
|
|
return this.min; |
|
159
|
|
|
} |
|
160
|
|
|
return num; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Sign a number. |
|
165
|
|
|
* @param {number} num The number. |
|
166
|
|
|
* @return {number} |
|
167
|
|
|
* @private |
|
168
|
|
|
*/ |
|
169
|
|
|
sign_(num) { |
|
170
|
|
|
if (num > this.max) { |
|
171
|
|
|
num -= (this.max * 2) + 2; |
|
172
|
|
|
} |
|
173
|
|
|
return num; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|