utils.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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. * @fileoverview This file contains helper code used by jspb.BinaryReader
  32. * and BinaryWriter.
  33. *
  34. * @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed
  35. * @author aappleby@google.com (Austin Appleby)
  36. */
  37. goog.provide('jspb.utils');
  38. goog.require('goog.asserts');
  39. goog.require('goog.crypt');
  40. goog.require('goog.crypt.base64');
  41. goog.require('goog.string');
  42. goog.require('jspb.BinaryConstants');
  43. /**
  44. * Javascript can't natively handle 64-bit data types, so to manipulate them we
  45. * have to split them into two 32-bit halves and do the math manually.
  46. *
  47. * Instead of instantiating and passing small structures around to do this, we
  48. * instead just use two global temporary values. This one stores the low 32
  49. * bits of a split value - for example, if the original value was a 64-bit
  50. * integer, this temporary value will contain the low 32 bits of that integer.
  51. * If the original value was a double, this temporary value will contain the
  52. * low 32 bits of the binary representation of that double, etcetera.
  53. * @type {number}
  54. */
  55. jspb.utils.split64Low = 0;
  56. /**
  57. * And correspondingly, this temporary variable will contain the high 32 bits
  58. * of whatever value was split.
  59. * @type {number}
  60. */
  61. jspb.utils.split64High = 0;
  62. /**
  63. * Splits an unsigned Javascript integer into two 32-bit halves and stores it
  64. * in the temp values above.
  65. * @param {number} value The number to split.
  66. */
  67. jspb.utils.splitUint64 = function(value) {
  68. // Extract low 32 bits and high 32 bits as unsigned integers.
  69. var lowBits = value >>> 0;
  70. var highBits = Math.floor((value - lowBits) /
  71. jspb.BinaryConstants.TWO_TO_32) >>> 0;
  72. jspb.utils.split64Low = lowBits;
  73. jspb.utils.split64High = highBits;
  74. };
  75. /**
  76. * Splits a signed Javascript integer into two 32-bit halves and stores it in
  77. * the temp values above.
  78. * @param {number} value The number to split.
  79. */
  80. jspb.utils.splitInt64 = function(value) {
  81. // Convert to sign-magnitude representation.
  82. var sign = (value < 0);
  83. value = Math.abs(value);
  84. // Extract low 32 bits and high 32 bits as unsigned integers.
  85. var lowBits = value >>> 0;
  86. var highBits = Math.floor((value - lowBits) /
  87. jspb.BinaryConstants.TWO_TO_32);
  88. highBits = highBits >>> 0;
  89. // Perform two's complement conversion if the sign bit was set.
  90. if (sign) {
  91. highBits = ~highBits >>> 0;
  92. lowBits = ~lowBits >>> 0;
  93. lowBits += 1;
  94. if (lowBits > 0xFFFFFFFF) {
  95. lowBits = 0;
  96. highBits++;
  97. if (highBits > 0xFFFFFFFF) highBits = 0;
  98. }
  99. }
  100. jspb.utils.split64Low = lowBits;
  101. jspb.utils.split64High = highBits;
  102. };
  103. /**
  104. * Converts a signed Javascript integer into zigzag format, splits it into two
  105. * 32-bit halves, and stores it in the temp values above.
  106. * @param {number} value The number to split.
  107. */
  108. jspb.utils.splitZigzag64 = function(value) {
  109. // Convert to sign-magnitude and scale by 2 before we split the value.
  110. var sign = (value < 0);
  111. value = Math.abs(value) * 2;
  112. jspb.utils.splitUint64(value);
  113. var lowBits = jspb.utils.split64Low;
  114. var highBits = jspb.utils.split64High;
  115. // If the value is negative, subtract 1 from the split representation so we
  116. // don't lose the sign bit due to precision issues.
  117. if (sign) {
  118. if (lowBits == 0) {
  119. if (highBits == 0) {
  120. lowBits = 0xFFFFFFFF;
  121. highBits = 0xFFFFFFFF;
  122. } else {
  123. highBits--;
  124. lowBits = 0xFFFFFFFF;
  125. }
  126. } else {
  127. lowBits--;
  128. }
  129. }
  130. jspb.utils.split64Low = lowBits;
  131. jspb.utils.split64High = highBits;
  132. };
  133. /**
  134. * Converts a floating-point number into 32-bit IEEE representation and stores
  135. * it in the temp values above.
  136. * @param {number} value
  137. */
  138. jspb.utils.splitFloat32 = function(value) {
  139. var sign = (value < 0) ? 1 : 0;
  140. value = sign ? -value : value;
  141. var exp;
  142. var mant;
  143. // Handle zeros.
  144. if (value === 0) {
  145. if ((1 / value) > 0) {
  146. // Positive zero.
  147. jspb.utils.split64High = 0;
  148. jspb.utils.split64Low = 0x00000000;
  149. } else {
  150. // Negative zero.
  151. jspb.utils.split64High = 0;
  152. jspb.utils.split64Low = 0x80000000;
  153. }
  154. return;
  155. }
  156. // Handle nans.
  157. if (isNaN(value)) {
  158. jspb.utils.split64High = 0;
  159. jspb.utils.split64Low = 0x7FFFFFFF;
  160. return;
  161. }
  162. // Handle infinities.
  163. if (value > jspb.BinaryConstants.FLOAT32_MAX) {
  164. jspb.utils.split64High = 0;
  165. jspb.utils.split64Low = ((sign << 31) | (0x7F800000)) >>> 0;
  166. return;
  167. }
  168. // Handle denormals.
  169. if (value < jspb.BinaryConstants.FLOAT32_MIN) {
  170. // Number is a denormal.
  171. mant = Math.round(value / Math.pow(2, -149));
  172. jspb.utils.split64High = 0;
  173. jspb.utils.split64Low = ((sign << 31) | mant) >>> 0;
  174. return;
  175. }
  176. exp = Math.floor(Math.log(value) / Math.LN2);
  177. mant = value * Math.pow(2, -exp);
  178. mant = Math.round(mant * jspb.BinaryConstants.TWO_TO_23);
  179. if (mant >= 0x1000000) {
  180. ++exp;
  181. }
  182. mant = mant & 0x7FFFFF;
  183. jspb.utils.split64High = 0;
  184. jspb.utils.split64Low = ((sign << 31) | ((exp + 127) << 23) | mant) >>> 0;
  185. };
  186. /**
  187. * Converts a floating-point number into 64-bit IEEE representation and stores
  188. * it in the temp values above.
  189. * @param {number} value
  190. */
  191. jspb.utils.splitFloat64 = function(value) {
  192. var sign = (value < 0) ? 1 : 0;
  193. value = sign ? -value : value;
  194. // Handle zeros.
  195. if (value === 0) {
  196. if ((1 / value) > 0) {
  197. // Positive zero.
  198. jspb.utils.split64High = 0x00000000;
  199. jspb.utils.split64Low = 0x00000000;
  200. } else {
  201. // Negative zero.
  202. jspb.utils.split64High = 0x80000000;
  203. jspb.utils.split64Low = 0x00000000;
  204. }
  205. return;
  206. }
  207. // Handle nans.
  208. if (isNaN(value)) {
  209. jspb.utils.split64High = 0x7FFFFFFF;
  210. jspb.utils.split64Low = 0xFFFFFFFF;
  211. return;
  212. }
  213. // Handle infinities.
  214. if (value > jspb.BinaryConstants.FLOAT64_MAX) {
  215. jspb.utils.split64High = ((sign << 31) | (0x7FF00000)) >>> 0;
  216. jspb.utils.split64Low = 0;
  217. return;
  218. }
  219. // Handle denormals.
  220. if (value < jspb.BinaryConstants.FLOAT64_MIN) {
  221. // Number is a denormal.
  222. var mant = value / Math.pow(2, -1074);
  223. var mantHigh = (mant / jspb.BinaryConstants.TWO_TO_32);
  224. jspb.utils.split64High = ((sign << 31) | mantHigh) >>> 0;
  225. jspb.utils.split64Low = (mant >>> 0);
  226. return;
  227. }
  228. // Compute the least significant exponent needed to represent the magnitude of
  229. // the value by repeadly dividing/multiplying by 2 until the magnitude
  230. // crosses 2. While tempting to use log math to find the exponent, at the
  231. // boundaries of precision, the result can be off by one.
  232. var maxDoubleExponent = 1023;
  233. var minDoubleExponent = -1022;
  234. var x = value;
  235. var exp = 0;
  236. if (x >= 2) {
  237. while (x >= 2 && exp < maxDoubleExponent) {
  238. exp++;
  239. x = x / 2;
  240. }
  241. } else {
  242. while (x < 1 && exp > minDoubleExponent) {
  243. x = x * 2;
  244. exp--;
  245. }
  246. }
  247. var mant = value * Math.pow(2, -exp);
  248. var mantHigh = (mant * jspb.BinaryConstants.TWO_TO_20) & 0xFFFFF;
  249. var mantLow = (mant * jspb.BinaryConstants.TWO_TO_52) >>> 0;
  250. jspb.utils.split64High =
  251. ((sign << 31) | ((exp + 1023) << 20) | mantHigh) >>> 0;
  252. jspb.utils.split64Low = mantLow;
  253. };
  254. /**
  255. * Converts an 8-character hash string into two 32-bit numbers and stores them
  256. * in the temp values above.
  257. * @param {string} hash
  258. */
  259. jspb.utils.splitHash64 = function(hash) {
  260. var a = hash.charCodeAt(0);
  261. var b = hash.charCodeAt(1);
  262. var c = hash.charCodeAt(2);
  263. var d = hash.charCodeAt(3);
  264. var e = hash.charCodeAt(4);
  265. var f = hash.charCodeAt(5);
  266. var g = hash.charCodeAt(6);
  267. var h = hash.charCodeAt(7);
  268. jspb.utils.split64Low = (a + (b << 8) + (c << 16) + (d << 24)) >>> 0;
  269. jspb.utils.split64High = (e + (f << 8) + (g << 16) + (h << 24)) >>> 0;
  270. };
  271. /**
  272. * Joins two 32-bit values into a 64-bit unsigned integer. Precision will be
  273. * lost if the result is greater than 2^52.
  274. * @param {number} bitsLow
  275. * @param {number} bitsHigh
  276. * @return {number}
  277. */
  278. jspb.utils.joinUint64 = function(bitsLow, bitsHigh) {
  279. return bitsHigh * jspb.BinaryConstants.TWO_TO_32 + (bitsLow >>> 0);
  280. };
  281. /**
  282. * Joins two 32-bit values into a 64-bit signed integer. Precision will be lost
  283. * if the result is greater than 2^52.
  284. * @param {number} bitsLow
  285. * @param {number} bitsHigh
  286. * @return {number}
  287. */
  288. jspb.utils.joinInt64 = function(bitsLow, bitsHigh) {
  289. // If the high bit is set, do a manual two's complement conversion.
  290. var sign = (bitsHigh & 0x80000000);
  291. if (sign) {
  292. bitsLow = (~bitsLow + 1) >>> 0;
  293. bitsHigh = ~bitsHigh >>> 0;
  294. if (bitsLow == 0) {
  295. bitsHigh = (bitsHigh + 1) >>> 0;
  296. }
  297. }
  298. var result = jspb.utils.joinUint64(bitsLow, bitsHigh);
  299. return sign ? -result : result;
  300. };
  301. /**
  302. * Converts split 64-bit values from standard two's complement encoding to
  303. * zig-zag encoding. Invokes the provided function to produce final result.
  304. *
  305. * @param {number} bitsLow
  306. * @param {number} bitsHigh
  307. * @param {function(number, number): T} convert Conversion function to produce
  308. * the result value, takes parameters (lowBits, highBits).
  309. * @return {T}
  310. * @template T
  311. */
  312. jspb.utils.toZigzag64 = function(bitsLow, bitsHigh, convert) {
  313. // See
  314. // https://engdoc.corp.google.com/eng/howto/protocolbuffers/developerguide/encoding.shtml?cl=head#types
  315. // 64-bit math is: (n << 1) ^ (n >> 63)
  316. //
  317. // To do this in 32 bits, we can get a 32-bit sign-flipping mask from the
  318. // high word.
  319. // Then we can operate on each word individually, with the addition of the
  320. // "carry" to get the most significant bit from the low word into the high
  321. // word.
  322. var signFlipMask = bitsHigh >> 31;
  323. bitsHigh = (bitsHigh << 1 | bitsLow >>> 31) ^ signFlipMask;
  324. bitsLow = (bitsLow << 1) ^ signFlipMask;
  325. return convert(bitsLow, bitsHigh);
  326. };
  327. /**
  328. * Joins two 32-bit values into a 64-bit unsigned integer and applies zigzag
  329. * decoding. Precision will be lost if the result is greater than 2^52.
  330. * @param {number} bitsLow
  331. * @param {number} bitsHigh
  332. * @return {number}
  333. */
  334. jspb.utils.joinZigzag64 = function(bitsLow, bitsHigh) {
  335. return jspb.utils.fromZigzag64(bitsLow, bitsHigh, jspb.utils.joinInt64);
  336. };
  337. /**
  338. * Converts split 64-bit values from zigzag encoding to standard two's
  339. * complement encoding. Invokes the provided function to produce final result.
  340. *
  341. * @param {number} bitsLow
  342. * @param {number} bitsHigh
  343. * @param {function(number, number): T} convert Conversion function to produce
  344. * the result value, takes parameters (lowBits, highBits).
  345. * @return {T}
  346. * @template T
  347. */
  348. jspb.utils.fromZigzag64 = function(bitsLow, bitsHigh, convert) {
  349. // 64 bit math is:
  350. // signmask = (zigzag & 1) ? -1 : 0;
  351. // twosComplement = (zigzag >> 1) ^ signmask;
  352. //
  353. // To work with 32 bit, we can operate on both but "carry" the lowest bit
  354. // from the high word by shifting it up 31 bits to be the most significant bit
  355. // of the low word.
  356. var signFlipMask = -(bitsLow & 1);
  357. bitsLow = ((bitsLow >>> 1) | (bitsHigh << 31)) ^ signFlipMask;
  358. bitsHigh = (bitsHigh >>> 1) ^ signFlipMask;
  359. return convert(bitsLow, bitsHigh);
  360. };
  361. /**
  362. * Joins two 32-bit values into a 32-bit IEEE floating point number and
  363. * converts it back into a Javascript number.
  364. * @param {number} bitsLow The low 32 bits of the binary number;
  365. * @param {number} bitsHigh The high 32 bits of the binary number.
  366. * @return {number}
  367. */
  368. jspb.utils.joinFloat32 = function(bitsLow, bitsHigh) {
  369. var sign = ((bitsLow >> 31) * 2 + 1);
  370. var exp = (bitsLow >>> 23) & 0xFF;
  371. var mant = bitsLow & 0x7FFFFF;
  372. if (exp == 0xFF) {
  373. if (mant) {
  374. return NaN;
  375. } else {
  376. return sign * Infinity;
  377. }
  378. }
  379. if (exp == 0) {
  380. // Denormal.
  381. return sign * Math.pow(2, -149) * mant;
  382. } else {
  383. return sign * Math.pow(2, exp - 150) *
  384. (mant + Math.pow(2, 23));
  385. }
  386. };
  387. /**
  388. * Joins two 32-bit values into a 64-bit IEEE floating point number and
  389. * converts it back into a Javascript number.
  390. * @param {number} bitsLow The low 32 bits of the binary number;
  391. * @param {number} bitsHigh The high 32 bits of the binary number.
  392. * @return {number}
  393. */
  394. jspb.utils.joinFloat64 = function(bitsLow, bitsHigh) {
  395. var sign = ((bitsHigh >> 31) * 2 + 1);
  396. var exp = (bitsHigh >>> 20) & 0x7FF;
  397. var mant = jspb.BinaryConstants.TWO_TO_32 * (bitsHigh & 0xFFFFF) + bitsLow;
  398. if (exp == 0x7FF) {
  399. if (mant) {
  400. return NaN;
  401. } else {
  402. return sign * Infinity;
  403. }
  404. }
  405. if (exp == 0) {
  406. // Denormal.
  407. return sign * Math.pow(2, -1074) * mant;
  408. } else {
  409. return sign * Math.pow(2, exp - 1075) *
  410. (mant + jspb.BinaryConstants.TWO_TO_52);
  411. }
  412. };
  413. /**
  414. * Joins two 32-bit values into an 8-character hash string.
  415. * @param {number} bitsLow
  416. * @param {number} bitsHigh
  417. * @return {string}
  418. */
  419. jspb.utils.joinHash64 = function(bitsLow, bitsHigh) {
  420. var a = (bitsLow >>> 0) & 0xFF;
  421. var b = (bitsLow >>> 8) & 0xFF;
  422. var c = (bitsLow >>> 16) & 0xFF;
  423. var d = (bitsLow >>> 24) & 0xFF;
  424. var e = (bitsHigh >>> 0) & 0xFF;
  425. var f = (bitsHigh >>> 8) & 0xFF;
  426. var g = (bitsHigh >>> 16) & 0xFF;
  427. var h = (bitsHigh >>> 24) & 0xFF;
  428. return String.fromCharCode(a, b, c, d, e, f, g, h);
  429. };
  430. /**
  431. * Individual digits for number->string conversion.
  432. * @const {!Array<string>}
  433. */
  434. jspb.utils.DIGITS = [
  435. '0', '1', '2', '3', '4', '5', '6', '7',
  436. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  437. ];
  438. /** @const @private {number} '0' */
  439. jspb.utils.ZERO_CHAR_CODE_ = 48;
  440. /** @const @private {number} 'a' */
  441. jspb.utils.A_CHAR_CODE_ = 97;
  442. /**
  443. * Losslessly converts a 64-bit unsigned integer in 32:32 split representation
  444. * into a decimal string.
  445. * @param {number} bitsLow The low 32 bits of the binary number;
  446. * @param {number} bitsHigh The high 32 bits of the binary number.
  447. * @return {string} The binary number represented as a string.
  448. */
  449. jspb.utils.joinUnsignedDecimalString = function(bitsLow, bitsHigh) {
  450. // Skip the expensive conversion if the number is small enough to use the
  451. // built-in conversions.
  452. if (bitsHigh <= 0x1FFFFF) {
  453. return '' + jspb.utils.joinUint64(bitsLow, bitsHigh);
  454. }
  455. // What this code is doing is essentially converting the input number from
  456. // base-2 to base-1e7, which allows us to represent the 64-bit range with
  457. // only 3 (very large) digits. Those digits are then trivial to convert to
  458. // a base-10 string.
  459. // The magic numbers used here are -
  460. // 2^24 = 16777216 = (1,6777216) in base-1e7.
  461. // 2^48 = 281474976710656 = (2,8147497,6710656) in base-1e7.
  462. // Split 32:32 representation into 16:24:24 representation so our
  463. // intermediate digits don't overflow.
  464. var low = bitsLow & 0xFFFFFF;
  465. var mid = (((bitsLow >>> 24) | (bitsHigh << 8)) >>> 0) & 0xFFFFFF;
  466. var high = (bitsHigh >> 16) & 0xFFFF;
  467. // Assemble our three base-1e7 digits, ignoring carries. The maximum
  468. // value in a digit at this step is representable as a 48-bit integer, which
  469. // can be stored in a 64-bit floating point number.
  470. var digitA = low + (mid * 6777216) + (high * 6710656);
  471. var digitB = mid + (high * 8147497);
  472. var digitC = (high * 2);
  473. // Apply carries from A to B and from B to C.
  474. var base = 10000000;
  475. if (digitA >= base) {
  476. digitB += Math.floor(digitA / base);
  477. digitA %= base;
  478. }
  479. if (digitB >= base) {
  480. digitC += Math.floor(digitB / base);
  481. digitB %= base;
  482. }
  483. // Convert base-1e7 digits to base-10, with optional leading zeroes.
  484. function decimalFrom1e7(digit1e7, needLeadingZeros) {
  485. var partial = digit1e7 ? String(digit1e7) : '';
  486. if (needLeadingZeros) {
  487. return '0000000'.slice(partial.length) + partial;
  488. }
  489. return partial;
  490. }
  491. return decimalFrom1e7(digitC, /*needLeadingZeros=*/ 0) +
  492. decimalFrom1e7(digitB, /*needLeadingZeros=*/ digitC) +
  493. // If the final 1e7 digit didn't need leading zeros, we would have
  494. // returned via the trivial code path at the top.
  495. decimalFrom1e7(digitA, /*needLeadingZeros=*/ 1);
  496. };
  497. /**
  498. * Losslessly converts a 64-bit signed integer in 32:32 split representation
  499. * into a decimal string.
  500. * @param {number} bitsLow The low 32 bits of the binary number;
  501. * @param {number} bitsHigh The high 32 bits of the binary number.
  502. * @return {string} The binary number represented as a string.
  503. */
  504. jspb.utils.joinSignedDecimalString = function(bitsLow, bitsHigh) {
  505. // If we're treating the input as a signed value and the high bit is set, do
  506. // a manual two's complement conversion before the decimal conversion.
  507. var negative = (bitsHigh & 0x80000000);
  508. if (negative) {
  509. bitsLow = (~bitsLow + 1) >>> 0;
  510. var carry = (bitsLow == 0) ? 1 : 0;
  511. bitsHigh = (~bitsHigh + carry) >>> 0;
  512. }
  513. var result = jspb.utils.joinUnsignedDecimalString(bitsLow, bitsHigh);
  514. return negative ? '-' + result : result;
  515. };
  516. /**
  517. * Convert an 8-character hash string representing either a signed or unsigned
  518. * 64-bit integer into its decimal representation without losing accuracy.
  519. * @param {string} hash The hash string to convert.
  520. * @param {boolean} signed True if we should treat the hash string as encoding
  521. * a signed integer.
  522. * @return {string}
  523. */
  524. jspb.utils.hash64ToDecimalString = function(hash, signed) {
  525. jspb.utils.splitHash64(hash);
  526. var bitsLow = jspb.utils.split64Low;
  527. var bitsHigh = jspb.utils.split64High;
  528. return signed ?
  529. jspb.utils.joinSignedDecimalString(bitsLow, bitsHigh) :
  530. jspb.utils.joinUnsignedDecimalString(bitsLow, bitsHigh);
  531. };
  532. /**
  533. * Converts an array of 8-character hash strings into their decimal
  534. * representations.
  535. * @param {!Array<string>} hashes The array of hash strings to convert.
  536. * @param {boolean} signed True if we should treat the hash string as encoding
  537. * a signed integer.
  538. * @return {!Array<string>}
  539. */
  540. jspb.utils.hash64ArrayToDecimalStrings = function(hashes, signed) {
  541. var result = new Array(hashes.length);
  542. for (var i = 0; i < hashes.length; i++) {
  543. result[i] = jspb.utils.hash64ToDecimalString(hashes[i], signed);
  544. }
  545. return result;
  546. };
  547. /**
  548. * Converts a signed or unsigned decimal string into its hash string
  549. * representation.
  550. * @param {string} dec
  551. * @return {string}
  552. */
  553. jspb.utils.decimalStringToHash64 = function(dec) {
  554. goog.asserts.assert(dec.length > 0);
  555. // Check for minus sign.
  556. var minus = false;
  557. if (dec[0] === '-') {
  558. minus = true;
  559. dec = dec.slice(1);
  560. }
  561. // Store result as a byte array.
  562. var resultBytes = [0, 0, 0, 0, 0, 0, 0, 0];
  563. // Set result to m*result + c.
  564. function muladd(m, c) {
  565. for (var i = 0; i < 8 && (m !== 1 || c > 0); i++) {
  566. var r = m * resultBytes[i] + c;
  567. resultBytes[i] = r & 0xFF;
  568. c = r >>> 8;
  569. }
  570. }
  571. // Negate the result bits.
  572. function neg() {
  573. for (var i = 0; i < 8; i++) {
  574. resultBytes[i] = (~resultBytes[i]) & 0xFF;
  575. }
  576. }
  577. // For each decimal digit, set result to 10*result + digit.
  578. for (var i = 0; i < dec.length; i++) {
  579. muladd(10, dec.charCodeAt(i) - jspb.utils.ZERO_CHAR_CODE_);
  580. }
  581. // If there's a minus sign, convert into two's complement.
  582. if (minus) {
  583. neg();
  584. muladd(1, 1);
  585. }
  586. return goog.crypt.byteArrayToString(resultBytes);
  587. };
  588. /**
  589. * Converts a signed or unsigned decimal string into two 32-bit halves, and
  590. * stores them in the temp variables listed above.
  591. * @param {string} value The decimal string to convert.
  592. */
  593. jspb.utils.splitDecimalString = function(value) {
  594. jspb.utils.splitHash64(jspb.utils.decimalStringToHash64(value));
  595. };
  596. /**
  597. * @param {number} nibble A 4-bit integer.
  598. * @return {string}
  599. * @private
  600. */
  601. jspb.utils.toHexDigit_ = function(nibble) {
  602. return String.fromCharCode(
  603. nibble < 10 ? jspb.utils.ZERO_CHAR_CODE_ + nibble :
  604. jspb.utils.A_CHAR_CODE_ - 10 + nibble);
  605. };
  606. /**
  607. * @param {number} hexCharCode
  608. * @return {number}
  609. * @private
  610. */
  611. jspb.utils.fromHexCharCode_ = function(hexCharCode) {
  612. if (hexCharCode >= jspb.utils.A_CHAR_CODE_) {
  613. return hexCharCode - jspb.utils.A_CHAR_CODE_ + 10;
  614. }
  615. return hexCharCode - jspb.utils.ZERO_CHAR_CODE_;
  616. };
  617. /**
  618. * Converts an 8-character hash string into its hexadecimal representation.
  619. * @param {string} hash
  620. * @return {string}
  621. */
  622. jspb.utils.hash64ToHexString = function(hash) {
  623. var temp = new Array(18);
  624. temp[0] = '0';
  625. temp[1] = 'x';
  626. for (var i = 0; i < 8; i++) {
  627. var c = hash.charCodeAt(7 - i);
  628. temp[i * 2 + 2] = jspb.utils.toHexDigit_(c >> 4);
  629. temp[i * 2 + 3] = jspb.utils.toHexDigit_(c & 0xF);
  630. }
  631. var result = temp.join('');
  632. return result;
  633. };
  634. /**
  635. * Converts a '0x<16 digits>' hex string into its hash string representation.
  636. * @param {string} hex
  637. * @return {string}
  638. */
  639. jspb.utils.hexStringToHash64 = function(hex) {
  640. hex = hex.toLowerCase();
  641. goog.asserts.assert(hex.length == 18);
  642. goog.asserts.assert(hex[0] == '0');
  643. goog.asserts.assert(hex[1] == 'x');
  644. var result = '';
  645. for (var i = 0; i < 8; i++) {
  646. var hi = jspb.utils.fromHexCharCode_(hex.charCodeAt(i * 2 + 2));
  647. var lo = jspb.utils.fromHexCharCode_(hex.charCodeAt(i * 2 + 3));
  648. result = String.fromCharCode(hi * 16 + lo) + result;
  649. }
  650. return result;
  651. };
  652. /**
  653. * Convert an 8-character hash string representing either a signed or unsigned
  654. * 64-bit integer into a Javascript number. Will lose accuracy if the result is
  655. * larger than 2^52.
  656. * @param {string} hash The hash string to convert.
  657. * @param {boolean} signed True if the has should be interpreted as a signed
  658. * number.
  659. * @return {number}
  660. */
  661. jspb.utils.hash64ToNumber = function(hash, signed) {
  662. jspb.utils.splitHash64(hash);
  663. var bitsLow = jspb.utils.split64Low;
  664. var bitsHigh = jspb.utils.split64High;
  665. return signed ? jspb.utils.joinInt64(bitsLow, bitsHigh) :
  666. jspb.utils.joinUint64(bitsLow, bitsHigh);
  667. };
  668. /**
  669. * Convert a Javascript number into an 8-character hash string. Will lose
  670. * precision if the value is non-integral or greater than 2^64.
  671. * @param {number} value The integer to convert.
  672. * @return {string}
  673. */
  674. jspb.utils.numberToHash64 = function(value) {
  675. jspb.utils.splitInt64(value);
  676. return jspb.utils.joinHash64(jspb.utils.split64Low,
  677. jspb.utils.split64High);
  678. };
  679. /**
  680. * Counts the number of contiguous varints in a buffer.
  681. * @param {!Uint8Array} buffer The buffer to scan.
  682. * @param {number} start The starting point in the buffer to scan.
  683. * @param {number} end The end point in the buffer to scan.
  684. * @return {number} The number of varints in the buffer.
  685. */
  686. jspb.utils.countVarints = function(buffer, start, end) {
  687. // Count how many high bits of each byte were set in the buffer.
  688. var count = 0;
  689. for (var i = start; i < end; i++) {
  690. count += buffer[i] >> 7;
  691. }
  692. // The number of varints in the buffer equals the size of the buffer minus
  693. // the number of non-terminal bytes in the buffer (those with the high bit
  694. // set).
  695. return (end - start) - count;
  696. };
  697. /**
  698. * Counts the number of contiguous varint fields with the given field number in
  699. * the buffer.
  700. * @param {!Uint8Array} buffer The buffer to scan.
  701. * @param {number} start The starting point in the buffer to scan.
  702. * @param {number} end The end point in the buffer to scan.
  703. * @param {number} field The field number to count.
  704. * @return {number} The number of matching fields in the buffer.
  705. */
  706. jspb.utils.countVarintFields = function(buffer, start, end, field) {
  707. var count = 0;
  708. var cursor = start;
  709. var tag = field * 8 + jspb.BinaryConstants.WireType.VARINT;
  710. if (tag < 128) {
  711. // Single-byte field tag, we can use a slightly quicker count.
  712. while (cursor < end) {
  713. // Skip the field tag, or exit if we find a non-matching tag.
  714. if (buffer[cursor++] != tag) return count;
  715. // Field tag matches, we've found a valid field.
  716. count++;
  717. // Skip the varint.
  718. while (1) {
  719. var x = buffer[cursor++];
  720. if ((x & 0x80) == 0) break;
  721. }
  722. }
  723. } else {
  724. while (cursor < end) {
  725. // Skip the field tag, or exit if we find a non-matching tag.
  726. var temp = tag;
  727. while (temp > 128) {
  728. if (buffer[cursor] != ((temp & 0x7F) | 0x80)) return count;
  729. cursor++;
  730. temp >>= 7;
  731. }
  732. if (buffer[cursor++] != temp) return count;
  733. // Field tag matches, we've found a valid field.
  734. count++;
  735. // Skip the varint.
  736. while (1) {
  737. var x = buffer[cursor++];
  738. if ((x & 0x80) == 0) break;
  739. }
  740. }
  741. }
  742. return count;
  743. };
  744. /**
  745. * Counts the number of contiguous fixed32 fields with the given tag in the
  746. * buffer.
  747. * @param {!Uint8Array} buffer The buffer to scan.
  748. * @param {number} start The starting point in the buffer to scan.
  749. * @param {number} end The end point in the buffer to scan.
  750. * @param {number} tag The tag value to count.
  751. * @param {number} stride The number of bytes to skip per field.
  752. * @return {number} The number of fields with a matching tag in the buffer.
  753. * @private
  754. */
  755. jspb.utils.countFixedFields_ =
  756. function(buffer, start, end, tag, stride) {
  757. var count = 0;
  758. var cursor = start;
  759. if (tag < 128) {
  760. // Single-byte field tag, we can use a slightly quicker count.
  761. while (cursor < end) {
  762. // Skip the field tag, or exit if we find a non-matching tag.
  763. if (buffer[cursor++] != tag) return count;
  764. // Field tag matches, we've found a valid field.
  765. count++;
  766. // Skip the value.
  767. cursor += stride;
  768. }
  769. } else {
  770. while (cursor < end) {
  771. // Skip the field tag, or exit if we find a non-matching tag.
  772. var temp = tag;
  773. while (temp > 128) {
  774. if (buffer[cursor++] != ((temp & 0x7F) | 0x80)) return count;
  775. temp >>= 7;
  776. }
  777. if (buffer[cursor++] != temp) return count;
  778. // Field tag matches, we've found a valid field.
  779. count++;
  780. // Skip the value.
  781. cursor += stride;
  782. }
  783. }
  784. return count;
  785. };
  786. /**
  787. * Counts the number of contiguous fixed32 fields with the given field number
  788. * in the buffer.
  789. * @param {!Uint8Array} buffer The buffer to scan.
  790. * @param {number} start The starting point in the buffer to scan.
  791. * @param {number} end The end point in the buffer to scan.
  792. * @param {number} field The field number to count.
  793. * @return {number} The number of matching fields in the buffer.
  794. */
  795. jspb.utils.countFixed32Fields = function(buffer, start, end, field) {
  796. var tag = field * 8 + jspb.BinaryConstants.WireType.FIXED32;
  797. return jspb.utils.countFixedFields_(buffer, start, end, tag, 4);
  798. };
  799. /**
  800. * Counts the number of contiguous fixed64 fields with the given field number
  801. * in the buffer.
  802. * @param {!Uint8Array} buffer The buffer to scan.
  803. * @param {number} start The starting point in the buffer to scan.
  804. * @param {number} end The end point in the buffer to scan.
  805. * @param {number} field The field number to count
  806. * @return {number} The number of matching fields in the buffer.
  807. */
  808. jspb.utils.countFixed64Fields = function(buffer, start, end, field) {
  809. var tag = field * 8 + jspb.BinaryConstants.WireType.FIXED64;
  810. return jspb.utils.countFixedFields_(buffer, start, end, tag, 8);
  811. };
  812. /**
  813. * Counts the number of contiguous delimited fields with the given field number
  814. * in the buffer.
  815. * @param {!Uint8Array} buffer The buffer to scan.
  816. * @param {number} start The starting point in the buffer to scan.
  817. * @param {number} end The end point in the buffer to scan.
  818. * @param {number} field The field number to count.
  819. * @return {number} The number of matching fields in the buffer.
  820. */
  821. jspb.utils.countDelimitedFields = function(buffer, start, end, field) {
  822. var count = 0;
  823. var cursor = start;
  824. var tag = field * 8 + jspb.BinaryConstants.WireType.DELIMITED;
  825. while (cursor < end) {
  826. // Skip the field tag, or exit if we find a non-matching tag.
  827. var temp = tag;
  828. while (temp > 128) {
  829. if (buffer[cursor++] != ((temp & 0x7F) | 0x80)) return count;
  830. temp >>= 7;
  831. }
  832. if (buffer[cursor++] != temp) return count;
  833. // Field tag matches, we've found a valid field.
  834. count++;
  835. // Decode the length prefix.
  836. var length = 0;
  837. var shift = 1;
  838. while (1) {
  839. temp = buffer[cursor++];
  840. length += (temp & 0x7f) * shift;
  841. shift *= 128;
  842. if ((temp & 0x80) == 0) break;
  843. }
  844. // Advance the cursor past the blob.
  845. cursor += length;
  846. }
  847. return count;
  848. };
  849. /**
  850. * String-ify bytes for text format. Should be optimized away in non-debug.
  851. * The returned string uses \xXX escapes for all values and is itself quoted.
  852. * [1, 31] serializes to '"\x01\x1f"'.
  853. * @param {jspb.ByteSource} byteSource The bytes to serialize.
  854. * @return {string} Stringified bytes for text format.
  855. */
  856. jspb.utils.debugBytesToTextFormat = function(byteSource) {
  857. var s = '"';
  858. if (byteSource) {
  859. var bytes = jspb.utils.byteSourceToUint8Array(byteSource);
  860. for (var i = 0; i < bytes.length; i++) {
  861. s += '\\x';
  862. if (bytes[i] < 16) s += '0';
  863. s += bytes[i].toString(16);
  864. }
  865. }
  866. return s + '"';
  867. };
  868. /**
  869. * String-ify a scalar for text format. Should be optimized away in non-debug.
  870. * @param {string|number|boolean} scalar The scalar to stringify.
  871. * @return {string} Stringified scalar for text format.
  872. */
  873. jspb.utils.debugScalarToTextFormat = function(scalar) {
  874. if (typeof scalar === 'string') {
  875. return goog.string.quote(scalar);
  876. } else {
  877. return scalar.toString();
  878. }
  879. };
  880. /**
  881. * Utility function: convert a string with codepoints 0--255 inclusive to a
  882. * Uint8Array. If any codepoints greater than 255 exist in the string, throws an
  883. * exception.
  884. * @param {string} str
  885. * @return {!Uint8Array}
  886. */
  887. jspb.utils.stringToByteArray = function(str) {
  888. var arr = new Uint8Array(str.length);
  889. for (var i = 0; i < str.length; i++) {
  890. var codepoint = str.charCodeAt(i);
  891. if (codepoint > 255) {
  892. throw new Error('Conversion error: string contains codepoint ' +
  893. 'outside of byte range');
  894. }
  895. arr[i] = codepoint;
  896. }
  897. return arr;
  898. };
  899. /**
  900. * Converts any type defined in jspb.ByteSource into a Uint8Array.
  901. * @param {!jspb.ByteSource} data
  902. * @return {!Uint8Array}
  903. * @suppress {invalidCasts}
  904. */
  905. jspb.utils.byteSourceToUint8Array = function(data) {
  906. if (data.constructor === Uint8Array) {
  907. return /** @type {!Uint8Array} */(data);
  908. }
  909. if (data.constructor === ArrayBuffer) {
  910. data = /** @type {!ArrayBuffer} */(data);
  911. return /** @type {!Uint8Array} */(new Uint8Array(data));
  912. }
  913. if (typeof Buffer != 'undefined' && data.constructor === Buffer) {
  914. return /** @type {!Uint8Array} */ (
  915. new Uint8Array(/** @type {?} */ (data)));
  916. }
  917. if (data.constructor === Array) {
  918. data = /** @type {!Array<number>} */(data);
  919. return /** @type {!Uint8Array} */(new Uint8Array(data));
  920. }
  921. if (data.constructor === String) {
  922. data = /** @type {string} */(data);
  923. return goog.crypt.base64.decodeStringToUint8Array(data);
  924. }
  925. goog.asserts.fail('Type not convertible to Uint8Array.');
  926. return /** @type {!Uint8Array} */(new Uint8Array(0));
  927. };