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  // TODO(haberman): write lossless 64-bit zig-zag math.
236  this.writeZigzagVarint64(parseInt(value, 10));
237};
238
239
240/**
241 * Writes a 8-bit unsigned integer to the buffer. Numbers outside the range
242 * [0,2^8) will be truncated.
243 * @param {number} value The value to write.
244 */
245jspb.BinaryEncoder.prototype.writeUint8 = function(value) {
246  goog.asserts.assert(value == Math.floor(value));
247  goog.asserts.assert((value >= 0) && (value < 256));
248  this.buffer_.push((value >>> 0) & 0xFF);
249};
250
251
252/**
253 * Writes a 16-bit unsigned integer to the buffer. Numbers outside the
254 * range [0,2^16) will be truncated.
255 * @param {number} value The value to write.
256 */
257jspb.BinaryEncoder.prototype.writeUint16 = function(value) {
258  goog.asserts.assert(value == Math.floor(value));
259  goog.asserts.assert((value >= 0) && (value < 65536));
260  this.buffer_.push((value >>> 0) & 0xFF);
261  this.buffer_.push((value >>> 8) & 0xFF);
262};
263
264
265/**
266 * Writes a 32-bit unsigned integer to the buffer. Numbers outside the
267 * range [0,2^32) will be truncated.
268 * @param {number} value The value to write.
269 */
270jspb.BinaryEncoder.prototype.writeUint32 = function(value) {
271  goog.asserts.assert(value == Math.floor(value));
272  goog.asserts.assert((value >= 0) &&
273                      (value < jspb.BinaryConstants.TWO_TO_32));
274  this.buffer_.push((value >>> 0) & 0xFF);
275  this.buffer_.push((value >>> 8) & 0xFF);
276  this.buffer_.push((value >>> 16) & 0xFF);
277  this.buffer_.push((value >>> 24) & 0xFF);
278};
279
280
281/**
282 * Writes a 64-bit unsigned integer to the buffer. Numbers outside the
283 * range [0,2^64) will be truncated.
284 * @param {number} value The value to write.
285 */
286jspb.BinaryEncoder.prototype.writeUint64 = function(value) {
287  goog.asserts.assert(value == Math.floor(value));
288  goog.asserts.assert((value >= 0) &&
289                      (value < jspb.BinaryConstants.TWO_TO_64));
290  jspb.utils.splitUint64(value);
291  this.writeUint32(jspb.utils.split64Low);
292  this.writeUint32(jspb.utils.split64High);
293};
294
295
296/**
297 * Writes a 8-bit integer to the buffer. Numbers outside the range
298 * [-2^7,2^7) will be truncated.
299 * @param {number} value The value to write.
300 */
301jspb.BinaryEncoder.prototype.writeInt8 = function(value) {
302  goog.asserts.assert(value == Math.floor(value));
303  goog.asserts.assert((value >= -128) && (value < 128));
304  this.buffer_.push((value >>> 0) & 0xFF);
305};
306
307
308/**
309 * Writes a 16-bit integer to the buffer. Numbers outside the range
310 * [-2^15,2^15) will be truncated.
311 * @param {number} value The value to write.
312 */
313jspb.BinaryEncoder.prototype.writeInt16 = function(value) {
314  goog.asserts.assert(value == Math.floor(value));
315  goog.asserts.assert((value >= -32768) && (value < 32768));
316  this.buffer_.push((value >>> 0) & 0xFF);
317  this.buffer_.push((value >>> 8) & 0xFF);
318};
319
320
321/**
322 * Writes a 32-bit integer to the buffer. Numbers outside the range
323 * [-2^31,2^31) will be truncated.
324 * @param {number} value The value to write.
325 */
326jspb.BinaryEncoder.prototype.writeInt32 = function(value) {
327  goog.asserts.assert(value == Math.floor(value));
328  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
329                      (value < jspb.BinaryConstants.TWO_TO_31));
330  this.buffer_.push((value >>> 0) & 0xFF);
331  this.buffer_.push((value >>> 8) & 0xFF);
332  this.buffer_.push((value >>> 16) & 0xFF);
333  this.buffer_.push((value >>> 24) & 0xFF);
334};
335
336
337/**
338 * Writes a 64-bit integer to the buffer. Numbers outside the range
339 * [-2^63,2^63) will be truncated.
340 * @param {number} value The value to write.
341 */
342jspb.BinaryEncoder.prototype.writeInt64 = function(value) {
343  goog.asserts.assert(value == Math.floor(value));
344  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_63) &&
345                      (value < jspb.BinaryConstants.TWO_TO_63));
346  jspb.utils.splitInt64(value);
347  this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High);
348};
349
350
351/**
352 * Writes a 64-bit integer decimal strings to the buffer. Numbers outside the
353 * range [-2^63,2^63) will be truncated.
354 * @param {string} value The value to write.
355 */
356jspb.BinaryEncoder.prototype.writeInt64String = 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.splitHash64(jspb.utils.decimalStringToHash64(value));
361  this.writeSplitFixed64(jspb.utils.split64Low, jspb.utils.split64High);
362};
363
364
365/**
366 * Writes a single-precision floating point value to the buffer. Numbers
367 * requiring more than 32 bits of precision will be truncated.
368 * @param {number} value The value to write.
369 */
370jspb.BinaryEncoder.prototype.writeFloat = function(value) {
371  goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT32_MAX) &&
372                      (value <= jspb.BinaryConstants.FLOAT32_MAX));
373  jspb.utils.splitFloat32(value);
374  this.writeUint32(jspb.utils.split64Low);
375};
376
377
378/**
379 * Writes a double-precision floating point value to the buffer. As this is
380 * the native format used by JavaScript, no precision will be lost.
381 * @param {number} value The value to write.
382 */
383jspb.BinaryEncoder.prototype.writeDouble = function(value) {
384  goog.asserts.assert((value >= -jspb.BinaryConstants.FLOAT64_MAX) &&
385                      (value <= jspb.BinaryConstants.FLOAT64_MAX));
386  jspb.utils.splitFloat64(value);
387  this.writeUint32(jspb.utils.split64Low);
388  this.writeUint32(jspb.utils.split64High);
389};
390
391
392/**
393 * Writes a boolean value to the buffer as a varint. We allow numbers as input
394 * because the JSPB code generator uses 0/1 instead of true/false to save space
395 * in the string representation of the proto.
396 * @param {boolean|number} value The value to write.
397 */
398jspb.BinaryEncoder.prototype.writeBool = function(value) {
399  goog.asserts.assert(goog.isBoolean(value) || goog.isNumber(value));
400  this.buffer_.push(value ? 1 : 0);
401};
402
403
404/**
405 * Writes an enum value to the buffer as a varint.
406 * @param {number} value The value to write.
407 */
408jspb.BinaryEncoder.prototype.writeEnum = function(value) {
409  goog.asserts.assert(value == Math.floor(value));
410  goog.asserts.assert((value >= -jspb.BinaryConstants.TWO_TO_31) &&
411                      (value < jspb.BinaryConstants.TWO_TO_31));
412  this.writeSignedVarint32(value);
413};
414
415
416/**
417 * Writes an arbitrary byte array to the buffer.
418 * @param {!Uint8Array} bytes The array of bytes to write.
419 */
420jspb.BinaryEncoder.prototype.writeBytes = function(bytes) {
421  this.buffer_.push.apply(this.buffer_, bytes);
422};
423
424
425/**
426 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
427 * buffer as a varint.
428 * @param {string} hash The hash to write.
429 */
430jspb.BinaryEncoder.prototype.writeVarintHash64 = function(hash) {
431  jspb.utils.splitHash64(hash);
432  this.writeSplitVarint64(jspb.utils.split64Low,
433                          jspb.utils.split64High);
434};
435
436
437/**
438 * Writes a 64-bit hash string (8 characters @ 8 bits of data each) to the
439 * buffer as a fixed64.
440 * @param {string} hash The hash to write.
441 */
442jspb.BinaryEncoder.prototype.writeFixedHash64 = function(hash) {
443  jspb.utils.splitHash64(hash);
444  this.writeUint32(jspb.utils.split64Low);
445  this.writeUint32(jspb.utils.split64High);
446};
447
448
449/**
450 * Writes a UTF16 Javascript string to the buffer encoded as UTF8.
451 * TODO(aappleby): Add support for surrogate pairs, reject unpaired surrogates.
452 * @param {string} value The string to write.
453 * @return {number} The number of bytes used to encode the string.
454 */
455jspb.BinaryEncoder.prototype.writeString = function(value) {
456  var oldLength = this.buffer_.length;
457
458  for (var i = 0; i < value.length; i++) {
459
460    var c = value.charCodeAt(i);
461
462    if (c < 128) {
463      this.buffer_.push(c);
464    } else if (c < 2048) {
465      this.buffer_.push((c >> 6) | 192);
466      this.buffer_.push((c & 63) | 128);
467    } else if (c < 65536) {
468      // Look for surrogates
469      if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length) {
470        var second = value.charCodeAt(i + 1);
471        if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
472          // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
473          c = (c - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
474
475          this.buffer_.push((c >> 18) | 240);
476          this.buffer_.push(((c >> 12) & 63 ) | 128);
477          this.buffer_.push(((c >> 6) & 63) | 128);
478          this.buffer_.push((c & 63) | 128);
479          i++;
480        }
481      }
482      else {
483        this.buffer_.push((c >> 12) | 224);
484        this.buffer_.push(((c >> 6) & 63) | 128);
485        this.buffer_.push((c & 63) | 128);
486      }
487    }
488  }
489
490  var length = this.buffer_.length - oldLength;
491  return length;
492};
493