reader.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /**
  2. * @fileoverview Helper methods for reading data from the binary wire format.
  3. */
  4. goog.module('protobuf.binary.reader');
  5. const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
  6. const ByteString = goog.require('protobuf.ByteString');
  7. const Int64 = goog.require('protobuf.Int64');
  8. /******************************************************************************
  9. * OPTIONAL FUNCTIONS
  10. ******************************************************************************/
  11. /**
  12. * Reads a boolean from the binary bytes.
  13. * Also returns the first position after the boolean.
  14. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  15. * @param {number} index Start of the data.
  16. * @return {{value: boolean, nextCursor: number}}
  17. */
  18. function readBoolValue(bufferDecoder, index) {
  19. const {lowBits, highBits, dataStart} = bufferDecoder.getVarint(index);
  20. return {value: lowBits !== 0 || highBits !== 0, nextCursor: dataStart};
  21. }
  22. /**
  23. * Reads a boolean value from the binary bytes.
  24. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  25. * @param {number} start Start of the data.
  26. * @return {boolean}
  27. * @package
  28. */
  29. function readBool(bufferDecoder, start) {
  30. return readBoolValue(bufferDecoder, start).value;
  31. }
  32. /**
  33. * Reads a double value from the binary bytes.
  34. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  35. * @param {number} start Start of the data.
  36. * @return {!ByteString}
  37. * @package
  38. */
  39. function readBytes(bufferDecoder, start) {
  40. return readDelimited(bufferDecoder, start).asByteString();
  41. }
  42. /**
  43. * Reads a int32 value from the binary bytes encoded as varint.
  44. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  45. * @param {number} index Start of the data.
  46. * @return {{value: number, nextCursor: number}}
  47. * @package
  48. */
  49. function readInt32Value(bufferDecoder, index) {
  50. const {lowBits, dataStart} = bufferDecoder.getVarint(index);
  51. // Negative 32 bit integers are encoded with 64 bit values.
  52. // Clients are expected to truncate back to 32 bits.
  53. // This is why we are dropping the upper bytes here.
  54. return {value: lowBits | 0, nextCursor: dataStart};
  55. }
  56. /**
  57. * Reads a int32 value from the binary bytes encoded as varint.
  58. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  59. * @param {number} start Start of the data.
  60. * @return {number}
  61. * @package
  62. */
  63. function readInt32(bufferDecoder, start) {
  64. return readInt32Value(bufferDecoder, start).value;
  65. }
  66. /**
  67. * Reads a int32 value from the binary bytes encoded as varint.
  68. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  69. * @param {number} index Start of the data.
  70. * @return {{ value: !Int64, nextCursor: number}}
  71. * @package
  72. */
  73. function readInt64Value(bufferDecoder, index) {
  74. const {lowBits, highBits, dataStart} = bufferDecoder.getVarint(index);
  75. return {value: Int64.fromBits(lowBits, highBits), nextCursor: dataStart};
  76. }
  77. /**
  78. * Reads a int32 value from the binary bytes encoded as varint.
  79. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  80. * @param {number} start Start of the data.
  81. * @return {!Int64}
  82. * @package
  83. */
  84. function readInt64(bufferDecoder, start) {
  85. return readInt64Value(bufferDecoder, start).value;
  86. }
  87. /**
  88. * Reads a fixed int32 value from the binary bytes.
  89. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  90. * @param {number} start Start of the data.
  91. * @return {number}
  92. * @package
  93. */
  94. function readFixed32(bufferDecoder, start) {
  95. return bufferDecoder.getUint32(start);
  96. }
  97. /**
  98. * Reads a float value from the binary bytes.
  99. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  100. * @param {number} start Start of the data.
  101. * @return {number}
  102. * @package
  103. */
  104. function readFloat(bufferDecoder, start) {
  105. return bufferDecoder.getFloat32(start);
  106. }
  107. /**
  108. * Reads a fixed int64 value from the binary bytes.
  109. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  110. * @param {number} start Start of the data.
  111. * @return {!Int64}
  112. * @package
  113. */
  114. function readSfixed64(bufferDecoder, start) {
  115. const lowBits = bufferDecoder.getInt32(start);
  116. const highBits = bufferDecoder.getInt32(start + 4);
  117. return Int64.fromBits(lowBits, highBits);
  118. }
  119. /**
  120. * Reads a sint32 value from the binary bytes encoded as varint.
  121. * Also returns the first position after the boolean.
  122. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  123. * @param {number} index Start of the data.
  124. * @return {{value: number, nextCursor: number}}
  125. */
  126. function readSint32Value(bufferDecoder, index) {
  127. const {lowBits, dataStart} = bufferDecoder.getVarint(index);
  128. // Truncate upper bits and convert from zig zag to signd int
  129. return {value: (lowBits >>> 1) ^ -(lowBits & 0x01), nextCursor: dataStart};
  130. }
  131. /**
  132. * Reads a sint32 value from the binary bytes encoded as varint.
  133. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  134. * @param {number} start Start of the data.
  135. * @return {number}
  136. * @package
  137. */
  138. function readSint32(bufferDecoder, start) {
  139. return readSint32Value(bufferDecoder, start).value;
  140. }
  141. /**
  142. * Reads a sint64 value from the binary bytes encoded as varint.
  143. * Also returns the first position after the value.
  144. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  145. * @param {number} index Start of the data.
  146. * @return {{value: !Int64, nextCursor: number}}
  147. * @package
  148. */
  149. function readSint64Value(bufferDecoder, index) {
  150. const {lowBits, highBits, dataStart} = bufferDecoder.getVarint(index);
  151. const sign = -(lowBits & 0x01);
  152. const decodedLowerBits = ((lowBits >>> 1) | (highBits & 0x01) << 31) ^ sign;
  153. const decodedUpperBits = (highBits >>> 1) ^ sign;
  154. return {
  155. value: Int64.fromBits(decodedLowerBits, decodedUpperBits),
  156. nextCursor: dataStart
  157. };
  158. }
  159. /**
  160. * Reads a sint64 value from the binary bytes encoded as varint.
  161. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  162. * @param {number} start Start of the data.
  163. * @return {!Int64}
  164. * @package
  165. */
  166. function readSint64(bufferDecoder, start) {
  167. return readSint64Value(bufferDecoder, start).value;
  168. }
  169. /**
  170. * Read a subarray of bytes representing a length delimited field.
  171. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  172. * @param {number} start Start of the data.
  173. * @return {!BufferDecoder}
  174. * @package
  175. */
  176. function readDelimited(bufferDecoder, start) {
  177. const {lowBits, dataStart} = bufferDecoder.getVarint(start);
  178. const unsignedLength = lowBits >>> 0;
  179. return bufferDecoder.subBufferDecoder(dataStart, unsignedLength);
  180. }
  181. /**
  182. * Reads a string value from the binary bytes.
  183. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  184. * @param {number} start Start of the data.
  185. * @return {string}
  186. * @package
  187. */
  188. function readString(bufferDecoder, start) {
  189. return readDelimited(bufferDecoder, start).asString();
  190. }
  191. /**
  192. * Reads a uint32 value from the binary bytes encoded as varint.
  193. * Also returns the first position after the value.
  194. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  195. * @param {number} index Start of the data.
  196. * @return {{value: number, nextCursor: number}}
  197. */
  198. function readUint32Value(bufferDecoder, index) {
  199. const {lowBits, dataStart} = bufferDecoder.getVarint(index);
  200. return {value: lowBits >>> 0, nextCursor: dataStart};
  201. }
  202. /**
  203. * Reads a uint32 value from the binary bytes encoded as varint.
  204. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  205. * @param {number} start Start of the data.
  206. * @return {number}
  207. * @package
  208. */
  209. function readUint32(bufferDecoder, start) {
  210. return readUint32Value(bufferDecoder, start).value;
  211. }
  212. /**
  213. * Reads a double value from the binary bytes.
  214. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  215. * @param {number} start Start of the data.
  216. * @return {number}
  217. * @package
  218. */
  219. function readDouble(bufferDecoder, start) {
  220. return bufferDecoder.getFloat64(start);
  221. }
  222. /**
  223. * Reads a fixed int32 value from the binary bytes.
  224. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  225. * @param {number} start Start of the data.
  226. * @return {number}
  227. * @package
  228. */
  229. function readSfixed32(bufferDecoder, start) {
  230. return bufferDecoder.getInt32(start);
  231. }
  232. /******************************************************************************
  233. * REPEATED FUNCTIONS
  234. ******************************************************************************/
  235. /**
  236. * Reads a packed bool field, which consists of a length header and a list of
  237. * unsigned varints.
  238. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  239. * @param {number} start Start of the data.
  240. * @return {!Array<boolean>}
  241. * @package
  242. */
  243. function readPackedBool(bufferDecoder, start) {
  244. return readPackedVariableLength(bufferDecoder, start, readBoolValue);
  245. }
  246. /**
  247. * Reads a packed double field, which consists of a length header and a list of
  248. * fixed64.
  249. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  250. * @param {number} start Start of the data.
  251. * @return {!Array<number>}
  252. * @package
  253. */
  254. function readPackedDouble(bufferDecoder, start) {
  255. return readPackedFixed(bufferDecoder, start, 8, readDouble);
  256. }
  257. /**
  258. * Reads a packed fixed32 field, which consists of a length header and a list of
  259. * fixed32.
  260. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  261. * @param {number} start Start of the data.
  262. * @return {!Array<number>}
  263. * @package
  264. */
  265. function readPackedFixed32(bufferDecoder, start) {
  266. return readPackedFixed(bufferDecoder, start, 4, readFixed32);
  267. }
  268. /**
  269. * Reads a packed float field, which consists of a length header and a list of
  270. * fixed64.
  271. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  272. * @param {number} start Start of the data.
  273. * @return {!Array<number>}
  274. * @package
  275. */
  276. function readPackedFloat(bufferDecoder, start) {
  277. return readPackedFixed(bufferDecoder, start, 4, readFloat);
  278. }
  279. /**
  280. * Reads a packed int32 field, which consists of a length header and a list of
  281. * varint.
  282. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  283. * @param {number} start Start of the data.
  284. * @return {!Array<number>}
  285. * @package
  286. */
  287. function readPackedInt32(bufferDecoder, start) {
  288. return readPackedVariableLength(bufferDecoder, start, readInt32Value);
  289. }
  290. /**
  291. * Reads a packed int64 field, which consists of a length header and a list
  292. * of int64.
  293. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  294. * @param {number} start Start of the data.
  295. * @return {!Array<!Int64>}
  296. * @package
  297. */
  298. function readPackedInt64(bufferDecoder, start) {
  299. return readPackedVariableLength(bufferDecoder, start, readInt64Value);
  300. }
  301. /**
  302. * Reads a packed sfixed32 field, which consists of a length header and a list
  303. * of sfixed32.
  304. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  305. * @param {number} start Start of the data.
  306. * @return {!Array<number>}
  307. * @package
  308. */
  309. function readPackedSfixed32(bufferDecoder, start) {
  310. return readPackedFixed(bufferDecoder, start, 4, readSfixed32);
  311. }
  312. /**
  313. * Reads a packed sfixed64 field, which consists of a length header and a list
  314. * of sfixed64.
  315. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  316. * @param {number} start Start of the data.
  317. * @return {!Array<!Int64>}
  318. * @package
  319. */
  320. function readPackedSfixed64(bufferDecoder, start) {
  321. return readPackedFixed(bufferDecoder, start, 8, readSfixed64);
  322. }
  323. /**
  324. * Reads a packed sint32 field, which consists of a length header and a list of
  325. * varint.
  326. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  327. * @param {number} start Start of the data.
  328. * @return {!Array<number>}
  329. * @package
  330. */
  331. function readPackedSint32(bufferDecoder, start) {
  332. return readPackedVariableLength(bufferDecoder, start, readSint32Value);
  333. }
  334. /**
  335. * Reads a packed sint64 field, which consists of a length header and a list
  336. * of sint64.
  337. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  338. * @param {number} start Start of the data.
  339. * @return {!Array<!Int64>}
  340. * @package
  341. */
  342. function readPackedSint64(bufferDecoder, start) {
  343. return readPackedVariableLength(bufferDecoder, start, readSint64Value);
  344. }
  345. /**
  346. * Reads a packed uint32 field, which consists of a length header and a list of
  347. * varint.
  348. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  349. * @param {number} start Start of the data.
  350. * @return {!Array<number>}
  351. * @package
  352. */
  353. function readPackedUint32(bufferDecoder, start) {
  354. return readPackedVariableLength(bufferDecoder, start, readUint32Value);
  355. }
  356. /**
  357. * Read packed variable length values.
  358. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  359. * @param {number} start Start of the data.
  360. * @param {function(!BufferDecoder, number):{value:T, nextCursor: number}}
  361. * valueFunction
  362. * @return {!Array<T>}
  363. * @package
  364. * @template T
  365. */
  366. function readPackedVariableLength(bufferDecoder, start, valueFunction) {
  367. const /** !Array<T> */ result = [];
  368. const {lowBits, dataStart} = bufferDecoder.getVarint(start);
  369. let cursor = dataStart;
  370. const unsignedLength = lowBits >>> 0;
  371. while (cursor < dataStart + unsignedLength) {
  372. const {value, nextCursor} = valueFunction(bufferDecoder, cursor);
  373. cursor = nextCursor;
  374. result.push(value);
  375. }
  376. return result;
  377. }
  378. /**
  379. * Read a packed fixed values.
  380. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  381. * @param {number} start Start of the data.
  382. * @param {number} size End of the data.
  383. * @param {function(!BufferDecoder, number):T} valueFunction
  384. * @return {!Array<T>}
  385. * @package
  386. * @template T
  387. */
  388. function readPackedFixed(bufferDecoder, start, size, valueFunction) {
  389. const {lowBits, dataStart} = bufferDecoder.getVarint(start);
  390. const unsignedLength = lowBits >>> 0;
  391. const noOfEntries = unsignedLength / size;
  392. const /** !Array<T> */ result = new Array(noOfEntries);
  393. let cursor = dataStart;
  394. for (let i = 0; i < noOfEntries; i++) {
  395. result[i] = valueFunction(bufferDecoder, cursor);
  396. cursor += size;
  397. }
  398. return result;
  399. }
  400. exports = {
  401. readBool,
  402. readBytes,
  403. readDelimited,
  404. readDouble,
  405. readFixed32,
  406. readFloat,
  407. readInt32,
  408. readInt64,
  409. readSint32,
  410. readSint64,
  411. readSfixed32,
  412. readSfixed64,
  413. readString,
  414. readUint32,
  415. readPackedBool,
  416. readPackedDouble,
  417. readPackedFixed32,
  418. readPackedFloat,
  419. readPackedInt32,
  420. readPackedInt64,
  421. readPackedSfixed32,
  422. readPackedSfixed64,
  423. readPackedSint32,
  424. readPackedSint64,
  425. readPackedUint32,
  426. };