• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc.  All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9//     * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11//     * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15//     * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31/**
32 * @fileoverview BinaryEncode defines methods for encoding Javascript values
33 * into arrays of bytes compatible with the Protocol Buffer wire format.
34 *
35 * @author aappleby@google.com (Austin Appleby)
36 */
37
38goog.provide('jspb.BinaryEncoder');
39
40goog.require('goog.asserts');
41goog.require('jspb.BinaryConstants');
42goog.require('jspb.utils');
43
44
45
46/**
47 * BinaryEncoder implements encoders for all the wire types specified in
48 * https://developers.google.com/protocol-buffers/docs/encoding.
49 *
50 * @constructor
51 * @struct
52 */
53jspb.BinaryEncoder = function() {
54  /** @private {!Array<number>} */
55  this.buffer_ = [];
56};
57
58
59/**
60 * @return {number}
61 */
62jspb.BinaryEncoder.prototype.length = function() {
63  return this.buffer_.length;
64};
65
66
67/**
68 * @return {!Array<number>}
69 */
70jspb.BinaryEncoder.prototype.end = function() {
71  var buffer = this.buffer_;
72  this.buffer_ = [];
73  return buffer;
74};
75
76
77/**
78 * Encodes a 64-bit integer in 32:32 split representation into its wire-format
79 * varint representation and stores it in the buffer.
80 * @param {number} lowBits The low 32 bits of the int.
81 * @param {number} highBits The high 32 bits of the int.
82 */
83jspb.BinaryEncoder.prototype.writeSplitVarint64 = function(lowBits, highBits) {
84  goog.asserts.assert(lowBits == Math.floor(lowBits));
85  goog.asserts.assert(highBits == Math.floor(highBits));
86  goog.asserts.assert((lowBits >= 0) &&
87                      (lowBits < jspb.BinaryConstants.TWO_TO_32));
88  goog.asserts.assert((highBits >= 0) &&
89                      (highBits < jspb.BinaryConstants.TWO_TO_32));
90
91  // Break the binary representation into chunks of 7 bits, set the 8th bit
92  // in each chunk if it's not the final chunk, and append to the result.
93  while (highBits > 0 || lowBits > 127) {
94    this.buffer_.push((lowBits & 0x7f) | 0x80);
95    lowBits = ((lowBits >>> 7) | (highBits << 25)) >>> 0;
96    highBits = highBits >>> 7;
97  }
98  this.buffer_.push(lowBits);
99};
100
101
102/**
103 * Encodes a 64-bit integer in 32:32 split representation into its wire-format
104 * fixed representation and stores it in the buffer.
105 * @param {number} lowBits The low 32 bits of the int.
106 * @param {number} highBits The high 32 bits of the int.
107 */
108jspb.BinaryEncoder.prototype.writeSplitFixed64 = function(lowBits, highBits) {
109  goog.asserts.assert(lowBits == Math.floor(lowBits));
110  goog.asserts.assert(highBits == Math.floor(highBits));
111  goog.asserts.assert((lowBits >= 0) &&
112                      (lowBits < jspb.BinaryConstants.TWO_TO_32));
113  goog.asserts.assert((highBits >= 0) &&
114                      (highBits < jspb.BinaryConstants.TWO_TO_32));
115  this.writeUint32(lowBits);
116  this.writeUint32(highBits);
117};
118
119
120/**
121 * Encodes a 32-bit unsigned integer into its wire-format varint representation
122 * and stores it in the buffer.
123 * @param {number} value The integer to convert.
124 */
125jspb.BinaryEncoder.prototype.writeUnsignedVarint32 = function(value) {
126  goog.asserts.assert(value == Math.floor(value));
127  goog.asserts.assert((value >= 0) &&
128                      (value < jspb.BinaryConstants.TWO_TO_32));
129
130  while (value > 127) {
131    this.buffer_.push((value & 0x7f) | 0x80);
132    value = value >>> 7;
133  }
134
135  this.buffer_.push(value);
136};
137
138
139/**
140 * Encodes a 32-bit signed integer into its wire-format varint representation
141 * and stores it in the buffer.
142 * @param {number} value The integer to convert.
143 */
144jspb.BinaryEncoder.prototype.writeSignedVarint32 = function(value) {
145  goog.asserts.assert(value == Math.floor(value));
146  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
147                      (value < jspb.BinaryConstants.TWO_TO_31));
148
149  // Use the unsigned version if the value is not negative.
150  if (value >= 0) {
151    this.writeUnsignedVarint32(value);
152    return;
153  }
154
155  // Write nine bytes with a _signed_ right shift so we preserve the sign bit.
156  for (var i = 0; i < 9; i++) {
157    this.buffer_.push((value & 0x7f) | 0x80);
158    value = value >> 7;
159  }
160
161  // The above loop writes out 63 bits, so the last byte is always the sign bit
162  // which is always set for negative numbers.
163  this.buffer_.push(1);
164};
165
166
167/**
168 * Encodes a 64-bit unsigned integer into its wire-format varint representation
169 * and stores it in the buffer. Integers that are not representable in 64 bits
170 * will be truncated.
171 * @param {number} value The integer to convert.
172 */
173jspb.BinaryEncoder.prototype.writeUnsignedVarint64 = function(value) {
174  goog.asserts.assert(value == Math.floor(value));
175  goog.asserts.assert((value >= 0) &&
176                      (value < jspb.BinaryConstants.TWO_TO_64));
177  jspb.utils.splitInt64(value);
178  this.writeSplitVarint64(jspb.utils.split64Low,
179                          jspb.utils.split64High);
180};
181
182
183/**
184 * Encodes a 64-bit signed integer into its wire-format varint representation
185 * and stores it in the buffer. Integers that are not representable in 64 bits
186 * will be truncated.
187 * @param {number} value The integer to convert.
188 */
189jspb.BinaryEncoder.prototype.writeSignedVarint64 = function(value) {
190  goog.asserts.assert(value == Math.floor(value));
191  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
192                      (value < jspb.BinaryConstants.TWO_TO_63));
193  jspb.utils.splitInt64(value);
194  this.writeSplitVarint64(jspb.utils.split64Low,
195                          jspb.utils.split64High);
196};
197
198
199/**
200 * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
201 * representation and stores it in the buffer.
202 * @param {number} value The integer to convert.
203 */
204jspb.BinaryEncoder.prototype.writeZigzagVarint32 = function(value) {
205  goog.asserts.assert(value == Math.floor(value));
206  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
207                      (value < jspb.BinaryConstants.TWO_TO_31));
208  this.writeUnsignedVarint32(((value << 1) ^ (value >> 31)) >>> 0);
209};
210
211
212/**
213 * Encodes a JavaScript integer into its wire-format, zigzag-encoded varint
214 * representation and stores it in the buffer. Integers not representable in 64
215 * bits will be truncated.
216 * @param {number} value The integer to convert.
217 */
218jspb.BinaryEncoder.prototype.writeZigzagVarint64 = function(value) {
219  goog.asserts.assert(value == Math.floor(value));
220  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
221                      (value < jspb.BinaryConstants.TWO_TO_63));
222  jspb.utils.splitZigzag64(value);
223  this.writeSplitVarint64(jspb.utils.split64Low,
224                          jspb.utils.split64High);
225};
226
227
228/**
229 * Encodes a JavaScript decimal string into its wire-format, zigzag-encoded
230 * varint representation and stores it in the buffer. Integers not representable
231 * in 64 bits will be truncated.
232 * @param {string} value The integer to convert.
233 */
234jspb.BinaryEncoder.prototype.writeZigzagVarint64String = function(value) {
235  this.writeZigzagVarintHash64(jspb.utils.decimalStringToHash64(value));
236};
237
238
239/**
240 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
241 * buffer as a zigzag varint.
242 * @param {string} hash The hash to write.
243 */
244jspb.BinaryEncoder.prototype.writeZigzagVarintHash64 = function(hash) {
245  var self = this;
246  jspb.utils.splitHash64(hash);
247  jspb.utils.toZigzag64(
248      jspb.utils.split64Low, jspb.utils.split64High, function(lo, hi) {
249        self.writeSplitVarint64(lo >>> 0, hi >>> 0);
250      });
251};
252
253
254/**
255 * Writes an 8-bit unsigned integer to the buffer. Numbers outside the range
256 * [0,2^8) will be truncated.
257 * @param {number} value The value to write.
258 */
259jspb.BinaryEncoder.prototype.writeUint8 = function(value) {
260  goog.asserts.assert(value == Math.floor(value));
261  goog.asserts.assert((value >= 0) && (value < 256));
262  this.buffer_.push((value >>> 0) & 0xFF);
263};
264
265
266/**
267 * Writes a 16-bit unsigned integer to the buffer. Numbers outside the
268 * range [0,2^16) will be truncated.
269 * @param {number} value The value to write.
270 */
271jspb.BinaryEncoder.prototype.writeUint16 = function(value) {
272  goog.asserts.assert(value == Math.floor(value));
273  goog.asserts.assert((value >= 0) && (value < 65536));
274  this.buffer_.push((value >>> 0) & 0xFF);
275  this.buffer_.push((value >>> 8) & 0xFF);
276};
277
278
279/**
280 * Writes a 32-bit unsigned integer to the buffer. Numbers outside the
281 * range [0,2^32) will be truncated.
282 * @param {number} value The value to write.
283 */
284jspb.BinaryEncoder.prototype.writeUint32 = function(value) {
285  goog.asserts.assert(value == Math.floor(value));
286  goog.asserts.assert((value >= 0) &&
287                      (value < jspb.BinaryConstants.TWO_TO_32));
288  this.buffer_.push((value >>> 0) & 0xFF);
289  this.buffer_.push((value >>> 8) & 0xFF);
290  this.buffer_.push((value >>> 16) & 0xFF);
291  this.buffer_.push((value >>> 24) & 0xFF);
292};
293
294
295/**
296 * Writes a 64-bit unsigned integer to the buffer. Numbers outside the
297 * range [0,2^64) will be truncated.
298 * @param {number} value The value to write.
299 */
300jspb.BinaryEncoder.prototype.writeUint64 = function(value) {
301  goog.asserts.assert(value == Math.floor(value));
302  goog.asserts.assert((value >= 0) &&
303                      (value < jspb.BinaryConstants.TWO_TO_64));
304  jspb.utils.splitUint64(value);
305  this.writeUint32(jspb.utils.split64Low);
306  this.writeUint32(jspb.utils.split64High);
307};
308
309
310/**
311 * Writes an 8-bit integer to the buffer. Numbers outside the range
312 * [-2^7,2^7) will be truncated.
313 * @param {number} value The value to write.
314 */
315jspb.BinaryEncoder.prototype.writeInt8 = function(value) {
316  goog.asserts.assert(value == Math.floor(value));
317  goog.asserts.assert((value >= -128) && (value < 128));
318  this.buffer_.push((value >>> 0) & 0xFF);
319};
320
321
322/**
323 * Writes a 16-bit integer to the buffer. Numbers outside the range
324 * [-2^15,2^15) will be truncated.
325 * @param {number} value The value to write.
326 */
327jspb.BinaryEncoder.prototype.writeInt16 = function(value) {
328  goog.asserts.assert(value == Math.floor(value));
329  goog.asserts.assert((value >= -32768) && (value < 32768));
330  this.buffer_.push((value >>> 0) & 0xFF);
331  this.buffer_.push((value >>> 8) & 0xFF);
332};
333
334
335/**
336 * Writes a 32-bit integer to the buffer. Numbers outside the range
337 * [-2^31,2^31) will be truncated.
338 * @param {number} value The value to write.
339 */
340jspb.BinaryEncoder.prototype.writeInt32 = function(value) {
341  goog.asserts.assert(value == Math.floor(value));
342  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
343                      (value < jspb.BinaryConstants.TWO_TO_31));
344  this.buffer_.push((value >>> 0) & 0xFF);
345  this.buffer_.push((value >>> 8) & 0xFF);
346  this.buffer_.push((value >>> 16) & 0xFF);
347  this.buffer_.push((value >>> 24) & 0xFF);
348};
349
350
351/**
352 * Writes a 64-bit integer to the buffer. Numbers outside the range
353 * [-2^63,2^63) will be truncated.
354 * @param {number} value The value to write.
355 */
356jspb.BinaryEncoder.prototype.writeInt64 = function(value) {
357  goog.asserts.assert(value == Math.floor(value));
358  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
359                      (value < jspb.BinaryConstants.TWO_TO_63));
360  jspb.utils.splitInt64(value);
361  this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High);
362};
363
364
365/**
366 * Writes a 64-bit integer decimal strings to the buffer. Numbers outside the
367 * range [-2^63,2^63) will be truncated.
368 * @param {string} value The value to write.
369 */
370jspb.BinaryEncoder.prototype.writeInt64String = function(value) {
371  goog.asserts.assert(value == Math.floor(value));
372  goog.asserts.assert((+value >= -jspb.BinaryConstants.TWO_TO_63) &&
373                      (+value < jspb.BinaryConstants.TWO_TO_63));
374  jspb.utils.splitHash64(jspb.utils.decimalStringToHash64(value));
375  this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High);
376};
377
378
379/**
380 * Writes a single-precision floating point value to the buffer. Numbers
381 * requiring more than 32 bits of precision will be truncated.
382 * @param {number} value The value to write.
383 */
384jspb.BinaryEncoder.prototype.writeFloat = function(value) {
385  goog.asserts.assert(
386      value === Infinity || value === -Infinity || isNaN(value) ||
387      ((value >= -jspb.BinaryConstants.FLOAT32_MAX) &&
388       (value <= jspb.BinaryConstants.FLOAT32_MAX)));
389  jspb.utils.splitFloat32(value);
390  this.writeUint32(jspb.utils.split64Low);
391};
392
393
394/**
395 * Writes a double-precision floating point value to the buffer. As this is
396 * the native format used by JavaScript, no precision will be lost.
397 * @param {number} value The value to write.
398 */
399jspb.BinaryEncoder.prototype.writeDouble = function(value) {
400  goog.asserts.assert(
401      value === Infinity || value === -Infinity || isNaN(value) ||
402      ((value >= -jspb.BinaryConstants.FLOAT64_MAX) &&
403       (value <= jspb.BinaryConstants.FLOAT64_MAX)));
404  jspb.utils.splitFloat64(value);
405  this.writeUint32(jspb.utils.split64Low);
406  this.writeUint32(jspb.utils.split64High);
407};
408
409
410/**
411 * Writes a boolean value to the buffer as a varint. We allow numbers as input
412 * because the JSPB code generator uses 0/1 instead of true/false to save space
413 * in the string representation of the proto.
414 * @param {boolean|number} value The value to write.
415 */
416jspb.BinaryEncoder.prototype.writeBool = function(value) {
417  goog.asserts.assert(typeof value === 'boolean' || typeof value === 'number');
418  this.buffer_.push(value ? 1 : 0);
419};
420
421
422/**
423 * Writes an enum value to the buffer as a varint.
424 * @param {number} value The value to write.
425 */
426jspb.BinaryEncoder.prototype.writeEnum = function(value) {
427  goog.asserts.assert(value == Math.floor(value));
428  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
429                      (value < jspb.BinaryConstants.TWO_TO_31));
430  this.writeSignedVarint32(value);
431};
432
433
434/**
435 * Writes an arbitrary byte array to the buffer.
436 * @param {!Uint8Array} bytes The array of bytes to write.
437 */
438jspb.BinaryEncoder.prototype.writeBytes = function(bytes) {
439  this.buffer_.push.apply(this.buffer_, bytes);
440};
441
442
443/**
444 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
445 * buffer as a varint.
446 * @param {string} hash The hash to write.
447 */
448jspb.BinaryEncoder.prototype.writeVarintHash64 = function(hash) {
449  jspb.utils.splitHash64(hash);
450  this.writeSplitVarint64(jspb.utils.split64Low,
451                          jspb.utils.split64High);
452};
453
454
455/**
456 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
457 * buffer as a fixed64.
458 * @param {string} hash The hash to write.
459 */
460jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) {
461  jspb.utils.splitHash64(hash);
462  this.writeUint32(jspb.utils.split64Low);
463  this.writeUint32(jspb.utils.split64High);
464};
465
466
467/**
468 * Writes a UTF16 Javascript string to the buffer encoded as UTF8.
469 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates.
470 * @param {string} value The string to write.
471 * @return {number} The number of bytes used to encode the string.
472 */
473jspb.BinaryEncoder.prototype.writeString = function(value) {
474  var oldLength = this.buffer_.length;
475
476  for (var i = 0; i < value.length; i++) {
477
478    var c = value.charCodeAt(i);
479
480    if (c < 128) {
481      this.buffer_.push(c);
482    } else if (c < 2048) {
483      this.buffer_.push((c >> 6) | 192);
484      this.buffer_.push((c & 63) | 128);
485    } else if (c < 65536) {
486      // Look for surrogates
487      if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) {
488        var second = value.charCodeAt(i + 1);
489        if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
490          // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
491          c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
492
493          this.buffer_.push((c >> 18) | 240);
494          this.buffer_.push(((c >> 12) & 63 ) | 128);
495          this.buffer_.push(((c >> 6) & 63) | 128);
496          this.buffer_.push((c & 63) | 128);
497          i++;
498        }
499      }
500      else {
501        this.buffer_.push((c >> 12) | 224);
502        this.buffer_.push(((c >> 6) & 63) | 128);
503        this.buffer_.push((c & 63) | 128);
504      }
505    }
506  }
507
508  var length = this.buffer_.length - oldLength;
509  return length;
510};
511