1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var bufPool = [];
|
13 |
|
14 |
|
15 | var TEMP_BUF_MAXIMUM_LENGTH = 20;
|
16 |
|
17 |
|
18 | var MIN_EXACT_INT64 = -0x8000000000000000;
|
19 |
|
20 |
|
21 | var MAX_EXACT_INT64 = 0x7ffffffffffffc00;
|
22 |
|
23 |
|
24 | var MAX_EXACT_UINT64 = 0xfffffffffffff800;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var BIT_32 = 0x100000000;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var BIT_64 = 0x10000000000000000;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function lowestBit(num) {
|
46 | return num & -num;
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function isLossyToAdd(accum, num) {
|
55 | if (num === 0) {
|
56 | return false;
|
57 | }
|
58 |
|
59 | var lowBit = lowestBit(num);
|
60 | var added = accum + lowBit;
|
61 |
|
62 | if (added === accum) {
|
63 | return true;
|
64 | }
|
65 |
|
66 | if (added - lowBit !== accum) {
|
67 | return true;
|
68 | }
|
69 |
|
70 | return false;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export function alloc(length) {
|
84 | var result = bufPool[length];
|
85 |
|
86 | if (result) {
|
87 | bufPool[length] = undefined;
|
88 | } else {
|
89 | result = new Uint8Array(length);
|
90 | }
|
91 |
|
92 | result.fill(0);
|
93 | return result;
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | export function free(buffer) {
|
100 | var length = buffer.length;
|
101 |
|
102 | if (length < TEMP_BUF_MAXIMUM_LENGTH) {
|
103 | bufPool[length] = buffer;
|
104 | }
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | export function resize(buffer, length) {
|
114 | if (length === buffer.length) {
|
115 | return buffer;
|
116 | }
|
117 |
|
118 | var newBuf = alloc(length);
|
119 |
|
120 | for (var i = 0; i <= buffer.length; i++) {
|
121 | newBuf[i] = buffer[i];
|
122 | }
|
123 |
|
124 | free(buffer);
|
125 | return newBuf;
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export function readInt(buffer) {
|
132 | var length = buffer.length;
|
133 | var positive = buffer[length - 1] < 0x80;
|
134 | var result = positive ? 0 : -1;
|
135 | var lossy = false;
|
136 |
|
137 |
|
138 | if (length < 7) {
|
139 |
|
140 |
|
141 | for (var i = length - 1; i >= 0; i--) {
|
142 | result = result * 0x100 + buffer[i];
|
143 | }
|
144 | } else {
|
145 | for (var _i = length - 1; _i >= 0; _i--) {
|
146 | var one = buffer[_i];
|
147 | result *= 0x100;
|
148 |
|
149 | if (isLossyToAdd(result, one)) {
|
150 | lossy = true;
|
151 | }
|
152 |
|
153 | result += one;
|
154 | }
|
155 | }
|
156 |
|
157 | return {
|
158 | value: result,
|
159 | lossy: lossy
|
160 | };
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | export function readUInt(buffer) {
|
167 | var length = buffer.length;
|
168 | var result = 0;
|
169 | var lossy = false;
|
170 |
|
171 | if (length < 7) {
|
172 |
|
173 | for (var i = length - 1; i >= 0; i--) {
|
174 | result = result * 0x100 + buffer[i];
|
175 | }
|
176 | } else {
|
177 | for (var _i2 = length - 1; _i2 >= 0; _i2--) {
|
178 | var one = buffer[_i2];
|
179 | result *= 0x100;
|
180 |
|
181 | if (isLossyToAdd(result, one)) {
|
182 | lossy = true;
|
183 | }
|
184 |
|
185 | result += one;
|
186 | }
|
187 | }
|
188 |
|
189 | return {
|
190 | value: result,
|
191 | lossy: lossy
|
192 | };
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | export function writeInt64(value, buffer) {
|
199 | if (value < MIN_EXACT_INT64 || value > MAX_EXACT_INT64) {
|
200 | throw new Error("Value out of range.");
|
201 | }
|
202 |
|
203 | if (value < 0) {
|
204 | value += BIT_64;
|
205 | }
|
206 |
|
207 | writeUInt64(value, buffer);
|
208 | }
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | export function writeUInt64(value, buffer) {
|
214 | if (value < 0 || value > MAX_EXACT_UINT64) {
|
215 | throw new Error("Value out of range.");
|
216 | }
|
217 |
|
218 | var lowWord = value % BIT_32;
|
219 | var highWord = Math.floor(value / BIT_32);
|
220 | buffer[0] = lowWord & 0xff;
|
221 | buffer[1] = lowWord >> 8 & 0xff;
|
222 | buffer[2] = lowWord >> 16 & 0xff;
|
223 | buffer[3] = lowWord >> 24 & 0xff;
|
224 | buffer[4] = highWord & 0xff;
|
225 | buffer[5] = highWord >> 8 & 0xff;
|
226 | buffer[6] = highWord >> 16 & 0xff;
|
227 | buffer[7] = highWord >> 24 & 0xff;
|
228 | } |
\ | No newline at end of file |