-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathbuffer_decoder.js
343 lines (299 loc) · 8.86 KB
/
buffer_decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* @fileoverview A buffer implementation that can decode data for protobufs.
*/
goog.module('protobuf.binary.BufferDecoder');
const ByteString = goog.require('protobuf.ByteString');
const functions = goog.require('goog.functions');
const {POLYFILL_TEXT_ENCODING, checkCriticalPositionIndex, checkCriticalState, checkState} = goog.require('protobuf.internal.checks');
const {byteStringFromUint8ArrayUnsafe} = goog.require('protobuf.byteStringInternal');
const {concatenateByteArrays} = goog.require('protobuf.binary.uint8arrays');
const {decode} = goog.require('protobuf.binary.textencoding');
/**
* Returns a valid utf-8 decoder function based on TextDecoder if available or
* a polyfill.
* Some of the environments we run in do not have TextDecoder defined.
* TextDecoder is faster than our polyfill so we prefer it over the polyfill.
* @return {function(!DataView): string}
*/
function getStringDecoderFunction() {
if (goog.global['TextDecoder']) {
const textDecoder = new goog.global['TextDecoder']('utf-8', {fatal: true});
return bytes => textDecoder.decode(bytes);
}
if (POLYFILL_TEXT_ENCODING) {
return decode;
} else {
throw new Error(
'TextDecoder is missing. ' +
'Enable protobuf.defines.POLYFILL_TEXT_ENCODING.');
}
}
/** @type {function(): function(!DataView): string} */
const stringDecoderFunction =
functions.cacheReturnValue(() => getStringDecoderFunction());
/** @type {function(): !DataView} */
const emptyDataView =
functions.cacheReturnValue(() => new DataView(new ArrayBuffer(0)));
class BufferDecoder {
/**
* @param {!Array<!BufferDecoder>} bufferDecoders
* @return {!BufferDecoder}
*/
static merge(bufferDecoders) {
const uint8Arrays = bufferDecoders.map(b => b.asUint8Array());
const bytesArray = concatenateByteArrays(uint8Arrays);
return BufferDecoder.fromArrayBuffer(bytesArray.buffer);
}
/**
* @param {!ArrayBuffer} arrayBuffer
* @return {!BufferDecoder}
*/
static fromArrayBuffer(arrayBuffer) {
return new BufferDecoder(
new DataView(arrayBuffer), 0, arrayBuffer.byteLength);
}
/**
* @param {!DataView} dataView
* @param {number} startIndex
* @param {number} length
* @private
*/
constructor(dataView, startIndex, length) {
/** @private @const {!DataView} */
this.dataView_ = dataView;
/** @private @const {number} */
this.startIndex_ = startIndex;
/** @private @const {number} */
this.endIndex_ = startIndex + length;
/** @private {number} */
this.cursor_ = startIndex;
}
/**
* Returns the start index of the underlying buffer.
* @return {number}
*/
startIndex() {
return this.startIndex_;
}
/**
* Returns the end index of the underlying buffer.
* @return {number}
*/
endIndex() {
return this.endIndex_;
}
/**
* Returns the length of the underlying buffer.
* @return {number}
*/
length() {
return this.endIndex_ - this.startIndex_;
}
/**
* Returns the start position of the next data, i.e. end position of the last
* read data + 1.
* @return {number}
*/
cursor() {
return this.cursor_;
}
/**
* Sets the cursor to the specified position.
* @param {number} position
*/
setCursor(position) {
this.cursor_ = position;
}
/**
* Returns if there is more data to read after the current cursor position.
* @return {boolean}
*/
hasNext() {
return this.cursor_ < this.endIndex_;
}
/**
* Returns a float32 from a given index
* @param {number} index
* @return {number}
*/
getFloat32(index) {
this.cursor_ = index + 4;
return this.dataView_.getFloat32(index, true);
}
/**
* Returns a float64 from a given index
* @param {number} index
* @return {number}
*/
getFloat64(index) {
this.cursor_ = index + 8;
return this.dataView_.getFloat64(index, true);
}
/**
* Returns an int32 from a given index
* @param {number} index
* @return {number}
*/
getInt32(index) {
this.cursor_ = index + 4;
return this.dataView_.getInt32(index, true);
}
/**
* Returns a uint32 from a given index
* @param {number} index
* @return {number}
*/
getUint32(index) {
this.cursor_ = index + 4;
return this.dataView_.getUint32(index, true);
}
/**
* Returns two JS numbers each representing 32 bits of a 64 bit number. Also
* sets the cursor to the start of the next block of data.
* @param {number} index
* @return {{lowBits: number, highBits: number}}
*/
getVarint(index) {
this.cursor_ = index;
let lowBits = 0;
let highBits = 0;
for (let shift = 0; shift < 28; shift += 7) {
const b = this.dataView_.getUint8(this.cursor_++);
lowBits |= (b & 0x7F) << shift;
if ((b & 0x80) === 0) {
return {lowBits, highBits};
}
}
const middleByte = this.dataView_.getUint8(this.cursor_++);
// last four bits of the first 32 bit number
lowBits |= (middleByte & 0x0F) << 28;
// 3 upper bits are part of the next 32 bit number
highBits = (middleByte & 0x70) >> 4;
if ((middleByte & 0x80) === 0) {
return {lowBits, highBits};
}
for (let shift = 3; shift <= 31; shift += 7) {
const b = this.dataView_.getUint8(this.cursor_++);
highBits |= (b & 0x7F) << shift;
if ((b & 0x80) === 0) {
return {lowBits, highBits};
}
}
checkCriticalState(false, 'Data is longer than 10 bytes');
return {lowBits, highBits};
}
/**
* Returns an unsigned int32 number at the current cursor position. The upper
* bits are discarded if the varint is longer than 32 bits. Also sets the
* cursor to the start of the next block of data.
* @return {number}
*/
getUnsignedVarint32() {
let b = this.dataView_.getUint8(this.cursor_++);
let result = b & 0x7F;
if ((b & 0x80) === 0) {
return result;
}
b = this.dataView_.getUint8(this.cursor_++);
result |= (b & 0x7F) << 7;
if ((b & 0x80) === 0) {
return result;
}
b = this.dataView_.getUint8(this.cursor_++);
result |= (b & 0x7F) << 14;
if ((b & 0x80) === 0) {
return result;
}
b = this.dataView_.getUint8(this.cursor_++);
result |= (b & 0x7F) << 21;
if ((b & 0x80) === 0) {
return result;
}
// Extract only last 4 bits
b = this.dataView_.getUint8(this.cursor_++);
result |= (b & 0x0F) << 28;
for (let readBytes = 5; ((b & 0x80) !== 0) && readBytes < 10; readBytes++) {
b = this.dataView_.getUint8(this.cursor_++);
}
checkCriticalState((b & 0x80) === 0, 'Data is longer than 10 bytes');
// Result can be have 32 bits, convert it to unsigned
return result >>> 0;
}
/**
* Returns an unsigned int32 number at the specified index. The upper bits are
* discarded if the varint is longer than 32 bits. Also sets the cursor to the
* start of the next block of data.
* @param {number} index
* @return {number}
*/
getUnsignedVarint32At(index) {
this.cursor_ = index;
return this.getUnsignedVarint32();
}
/**
* Seeks forward by the given amount.
* @param {number} skipAmount
* @package
*/
skip(skipAmount) {
this.cursor_ += skipAmount;
checkCriticalPositionIndex(this.cursor_, this.endIndex_);
}
/**
* Skips over a varint from the current cursor position.
* @package
*/
skipVarint() {
const startIndex = this.cursor_;
while (this.dataView_.getUint8(this.cursor_++) & 0x80) {
}
checkCriticalPositionIndex(this.cursor_, startIndex + 10);
}
/**
* @param {number} startIndex
* @param {number} length
* @return {!BufferDecoder}
*/
subBufferDecoder(startIndex, length) {
checkState(
startIndex >= this.startIndex(),
`Current start: ${this.startIndex()}, subBufferDecoder start: ${
startIndex}`);
checkState(length >= 0, `Length: ${length}`);
checkState(
startIndex + length <= this.endIndex(),
`Current end: ${this.endIndex()}, subBufferDecoder start: ${
startIndex}, subBufferDecoder length: ${length}`);
return new BufferDecoder(this.dataView_, startIndex, length);
}
/**
* Returns the buffer as a string.
* @return {string}
*/
asString() {
// TODO: Remove this check when we no longer need to support IE
const stringDataView = this.length() === 0 ?
emptyDataView() :
new DataView(this.dataView_.buffer, this.startIndex_, this.length());
return stringDecoderFunction()(stringDataView);
}
/**
* Returns the buffer as a ByteString.
* @return {!ByteString}
*/
asByteString() {
return byteStringFromUint8ArrayUnsafe(this.asUint8Array());
}
/**
* Returns the DataView as an Uint8Array. DO NOT MODIFY or expose the
* underlying buffer.
*
* @package
* @return {!Uint8Array}
*/
asUint8Array() {
return new Uint8Array(
this.dataView_.buffer, this.startIndex_, this.length());
}
}
exports = BufferDecoder;