1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | "use strict";
|
11 |
|
12 | import Long from "@xtuc/long";
|
13 | import * as bits from "./bits";
|
14 | import * as bufs from "./bufs";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | var MIN_INT32 = -0x80000000;
|
22 |
|
23 |
|
24 | var MAX_INT32 = 0x7fffffff;
|
25 |
|
26 |
|
27 | var MAX_UINT32 = 0xffffffff;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function signedBitCount(buffer) {
|
66 | return bits.highOrder(bits.getSign(buffer) ^ 1, buffer) + 2;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function unsignedBitCount(buffer) {
|
87 | var result = bits.highOrder(1, buffer) + 1;
|
88 | return result ? result : 1;
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function encodeBufferCommon(buffer, signed) {
|
97 | var signBit;
|
98 | var bitCount;
|
99 |
|
100 | if (signed) {
|
101 | signBit = bits.getSign(buffer);
|
102 | bitCount = signedBitCount(buffer);
|
103 | } else {
|
104 | signBit = 0;
|
105 | bitCount = unsignedBitCount(buffer);
|
106 | }
|
107 |
|
108 | var byteCount = Math.ceil(bitCount / 7);
|
109 | var result = bufs.alloc(byteCount);
|
110 |
|
111 | for (var i = 0; i < byteCount; i++) {
|
112 | var payload = bits.extract(buffer, i * 7, 7, signBit);
|
113 | result[i] = payload | 0x80;
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 | result[byteCount - 1] &= 0x7f;
|
119 | return result;
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function encodedLength(encodedBuffer, index) {
|
128 | var result = 0;
|
129 |
|
130 | while (encodedBuffer[index + result] >= 0x80) {
|
131 | result++;
|
132 | }
|
133 |
|
134 | result++;
|
135 |
|
136 | if (index + result > encodedBuffer.length) {
|
137 |
|
138 | }
|
139 |
|
140 | return result;
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function decodeBufferCommon(encodedBuffer, index, signed) {
|
149 | index = index === undefined ? 0 : index;
|
150 | var length = encodedLength(encodedBuffer, index);
|
151 | var bitLength = length * 7;
|
152 | var byteLength = Math.ceil(bitLength / 8);
|
153 | var result = bufs.alloc(byteLength);
|
154 | var outIndex = 0;
|
155 |
|
156 | while (length > 0) {
|
157 | bits.inject(result, outIndex, 7, encodedBuffer[index]);
|
158 | outIndex += 7;
|
159 | index++;
|
160 | length--;
|
161 | }
|
162 |
|
163 | var signBit;
|
164 | var signByte;
|
165 |
|
166 | if (signed) {
|
167 |
|
168 | var lastByte = result[byteLength - 1];
|
169 | var endBit = outIndex % 8;
|
170 |
|
171 | if (endBit !== 0) {
|
172 | var shift = 32 - endBit;
|
173 |
|
174 | lastByte = result[byteLength - 1] = lastByte << shift >> shift & 0xff;
|
175 | }
|
176 |
|
177 | signBit = lastByte >> 7;
|
178 | signByte = signBit * 0xff;
|
179 | } else {
|
180 | signBit = 0;
|
181 | signByte = 0;
|
182 | }
|
183 |
|
184 |
|
185 |
|
186 | while (byteLength > 1 && result[byteLength - 1] === signByte && (!signed || result[byteLength - 2] >> 7 === signBit)) {
|
187 | byteLength--;
|
188 | }
|
189 |
|
190 | result = bufs.resize(result, byteLength);
|
191 | return {
|
192 | value: result,
|
193 | nextIndex: index
|
194 | };
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | function encodeIntBuffer(buffer) {
|
202 | return encodeBufferCommon(buffer, true);
|
203 | }
|
204 |
|
205 | function decodeIntBuffer(encodedBuffer, index) {
|
206 | return decodeBufferCommon(encodedBuffer, index, true);
|
207 | }
|
208 |
|
209 | function encodeInt32(num) {
|
210 | var buf = new Uint8Array(4);
|
211 | buf[0] = num & 0xff;
|
212 | buf[1] = num >> 8 & 0xff;
|
213 | buf[2] = num >> 16 & 0xff;
|
214 | buf[3] = num >> 24 & 0xff;
|
215 | var result = encodeIntBuffer(buf);
|
216 | return result;
|
217 | }
|
218 |
|
219 | function decodeInt32(encodedBuffer, index) {
|
220 | var result = decodeIntBuffer(encodedBuffer, index);
|
221 | var parsed = bufs.readInt(result.value);
|
222 | var value = parsed.value;
|
223 | bufs.free(result.value);
|
224 |
|
225 | if (value < MIN_INT32 || value > MAX_INT32) {
|
226 | throw new Error("integer too large");
|
227 | }
|
228 |
|
229 | return {
|
230 | value: value,
|
231 | nextIndex: result.nextIndex
|
232 | };
|
233 | }
|
234 |
|
235 | function encodeInt64(num) {
|
236 | var buf = bufs.alloc(8);
|
237 | bufs.writeInt64(num, buf);
|
238 | var result = encodeIntBuffer(buf);
|
239 | bufs.free(buf);
|
240 | return result;
|
241 | }
|
242 |
|
243 | function decodeInt64(encodedBuffer, index) {
|
244 | var result = decodeIntBuffer(encodedBuffer, index);
|
245 |
|
246 | var length = result.value.length;
|
247 |
|
248 | if (result.value[length - 1] >> 7) {
|
249 | result.value = bufs.resize(result.value, 8);
|
250 | result.value.fill(255, length);
|
251 | }
|
252 |
|
253 | var value = Long.fromBytesLE(result.value, false);
|
254 | bufs.free(result.value);
|
255 | return {
|
256 | value: value,
|
257 | nextIndex: result.nextIndex,
|
258 | lossy: false
|
259 | };
|
260 | }
|
261 |
|
262 | function encodeUIntBuffer(buffer) {
|
263 | return encodeBufferCommon(buffer, false);
|
264 | }
|
265 |
|
266 | function decodeUIntBuffer(encodedBuffer, index) {
|
267 | return decodeBufferCommon(encodedBuffer, index, false);
|
268 | }
|
269 |
|
270 | function encodeUInt32(num) {
|
271 | var buf = new Uint8Array(4);
|
272 | buf[0] = num & 0xff;
|
273 | buf[1] = num >> 8 & 0xff;
|
274 | buf[2] = num >> 16 & 0xff;
|
275 | buf[3] = num >> 24 & 0xff;
|
276 | var result = encodeUIntBuffer(buf);
|
277 | return result;
|
278 | }
|
279 |
|
280 | function decodeUInt32(encodedBuffer, index) {
|
281 | var result = decodeUIntBuffer(encodedBuffer, index);
|
282 | var parsed = bufs.readUInt(result.value);
|
283 | var value = parsed.value;
|
284 | bufs.free(result.value);
|
285 |
|
286 | if (value > MAX_UINT32) {
|
287 | throw new Error("integer too large");
|
288 | }
|
289 |
|
290 | return {
|
291 | value: value,
|
292 | nextIndex: result.nextIndex
|
293 | };
|
294 | }
|
295 |
|
296 | function encodeUInt64(num) {
|
297 | var buf = bufs.alloc(8);
|
298 | bufs.writeUInt64(num, buf);
|
299 | var result = encodeUIntBuffer(buf);
|
300 | bufs.free(buf);
|
301 | return result;
|
302 | }
|
303 |
|
304 | function decodeUInt64(encodedBuffer, index) {
|
305 | var result = decodeUIntBuffer(encodedBuffer, index);
|
306 | var value = Long.fromBytesLE(result.value, true);
|
307 | bufs.free(result.value);
|
308 | return {
|
309 | value: value,
|
310 | nextIndex: result.nextIndex,
|
311 | lossy: false
|
312 | };
|
313 | }
|
314 |
|
315 | export default {
|
316 | decodeInt32: decodeInt32,
|
317 | decodeInt64: decodeInt64,
|
318 | decodeIntBuffer: decodeIntBuffer,
|
319 | decodeUInt32: decodeUInt32,
|
320 | decodeUInt64: decodeUInt64,
|
321 | decodeUIntBuffer: decodeUIntBuffer,
|
322 | encodeInt32: encodeInt32,
|
323 | encodeInt64: encodeInt64,
|
324 | encodeIntBuffer: encodeIntBuffer,
|
325 | encodeUInt32: encodeUInt32,
|
326 | encodeUInt64: encodeUInt64,
|
327 | encodeUIntBuffer: encodeUIntBuffer
|
328 | }; |
\ | No newline at end of file |