reader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. const {checkState} = goog.require('protobuf.internal.checks');
  9. /******************************************************************************
  10. * OPTIONAL FUNCTIONS
  11. ******************************************************************************/
  12. /**
  13. * Reads a boolean value from the binary bytes.
  14. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  15. * @param {number} start Start of the data.
  16. * @return {boolean}
  17. * @package
  18. */
  19. function readBool(bufferDecoder, start) {
  20. const {lowBits, highBits} = bufferDecoder.getVarint(start);
  21. return lowBits !== 0 || highBits !== 0;
  22. }
  23. /**
  24. * Reads a ByteString value from the binary bytes.
  25. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  26. * @param {number} start Start of the data.
  27. * @return {!ByteString}
  28. * @package
  29. */
  30. function readBytes(bufferDecoder, start) {
  31. return readDelimited(bufferDecoder, start).asByteString();
  32. }
  33. /**
  34. * Reads a int32 value from the binary bytes encoded as varint.
  35. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  36. * @param {number} start Start of the data.
  37. * @return {number}
  38. * @package
  39. */
  40. function readInt32(bufferDecoder, start) {
  41. // Negative 32 bit integers are encoded with 64 bit values.
  42. // Clients are expected to truncate back to 32 bits.
  43. // This is why we are dropping the upper bytes here.
  44. return bufferDecoder.getUnsignedVarint32At(start) | 0;
  45. }
  46. /**
  47. * Reads a int32 value from the binary bytes encoded as varint.
  48. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  49. * @param {number} start Start of the data.
  50. * @return {!Int64}
  51. * @package
  52. */
  53. function readInt64(bufferDecoder, start) {
  54. const {lowBits, highBits} = bufferDecoder.getVarint(start);
  55. return Int64.fromBits(lowBits, highBits);
  56. }
  57. /**
  58. * Reads a fixed int32 value from the binary bytes.
  59. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  60. * @param {number} start Start of the data.
  61. * @return {number}
  62. * @package
  63. */
  64. function readFixed32(bufferDecoder, start) {
  65. return bufferDecoder.getUint32(start);
  66. }
  67. /**
  68. * Reads a float value from the binary bytes.
  69. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  70. * @param {number} start Start of the data.
  71. * @return {number}
  72. * @package
  73. */
  74. function readFloat(bufferDecoder, start) {
  75. return bufferDecoder.getFloat32(start);
  76. }
  77. /**
  78. * Reads a fixed int64 value from the binary bytes.
  79. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  80. * @param {number} start Start of the data.
  81. * @return {!Int64}
  82. * @package
  83. */
  84. function readSfixed64(bufferDecoder, start) {
  85. const lowBits = bufferDecoder.getInt32(start);
  86. const highBits = bufferDecoder.getInt32(start + 4);
  87. return Int64.fromBits(lowBits, highBits);
  88. }
  89. /**
  90. * Reads a sint32 value from the binary bytes encoded as varint.
  91. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  92. * @param {number} start Start of the data.
  93. * @return {number}
  94. * @package
  95. */
  96. function readSint32(bufferDecoder, start) {
  97. const bits = bufferDecoder.getUnsignedVarint32At(start);
  98. // Truncate upper bits and convert from zig zag to signd int
  99. return (bits >>> 1) ^ -(bits & 0x01);
  100. }
  101. /**
  102. * Reads a sint64 value from the binary bytes encoded as varint.
  103. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  104. * @param {number} start Start of the data.
  105. * @return {!Int64}
  106. * @package
  107. */
  108. function readSint64(bufferDecoder, start) {
  109. const {lowBits, highBits} = bufferDecoder.getVarint(start);
  110. const sign = -(lowBits & 0x01);
  111. const decodedLowerBits = ((lowBits >>> 1) | (highBits & 0x01) << 31) ^ sign;
  112. const decodedUpperBits = (highBits >>> 1) ^ sign;
  113. return Int64.fromBits(decodedLowerBits, decodedUpperBits);
  114. }
  115. /**
  116. * Read a subarray of bytes representing a length delimited field.
  117. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  118. * @param {number} start Start of the data.
  119. * @return {!BufferDecoder}
  120. * @package
  121. */
  122. function readDelimited(bufferDecoder, start) {
  123. const unsignedLength = bufferDecoder.getUnsignedVarint32At(start);
  124. return bufferDecoder.subBufferDecoder(bufferDecoder.cursor(), unsignedLength);
  125. }
  126. /**
  127. * Reads a string value from the binary bytes.
  128. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  129. * @param {number} start Start of the data.
  130. * @return {string}
  131. * @package
  132. */
  133. function readString(bufferDecoder, start) {
  134. return readDelimited(bufferDecoder, start).asString();
  135. }
  136. /**
  137. * Reads a uint32 value from the binary bytes encoded as varint.
  138. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  139. * @param {number} start Start of the data.
  140. * @return {number}
  141. * @package
  142. */
  143. function readUint32(bufferDecoder, start) {
  144. return bufferDecoder.getUnsignedVarint32At(start);
  145. }
  146. /**
  147. * Reads a double value from the binary bytes.
  148. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  149. * @param {number} start Start of the data.
  150. * @return {number}
  151. * @package
  152. */
  153. function readDouble(bufferDecoder, start) {
  154. return bufferDecoder.getFloat64(start);
  155. }
  156. /**
  157. * Reads a fixed int32 value from the binary bytes.
  158. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  159. * @param {number} start Start of the data.
  160. * @return {number}
  161. * @package
  162. */
  163. function readSfixed32(bufferDecoder, start) {
  164. return bufferDecoder.getInt32(start);
  165. }
  166. /******************************************************************************
  167. * REPEATED FUNCTIONS
  168. ******************************************************************************/
  169. /**
  170. * Reads a packed bool field, which consists of a length header and a list of
  171. * unsigned varints.
  172. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  173. * @param {number} start Start of the data.
  174. * @return {!Array<boolean>}
  175. * @package
  176. */
  177. function readPackedBool(bufferDecoder, start) {
  178. return readPacked(bufferDecoder, start, readBool);
  179. }
  180. /**
  181. * Reads a packed double field, which consists of a length header and a list of
  182. * fixed64.
  183. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  184. * @param {number} start Start of the data.
  185. * @return {!Array<number>}
  186. * @package
  187. */
  188. function readPackedDouble(bufferDecoder, start) {
  189. return readPacked(bufferDecoder, start, readDouble);
  190. }
  191. /**
  192. * Reads a packed fixed32 field, which consists of a length header and a list of
  193. * fixed32.
  194. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  195. * @param {number} start Start of the data.
  196. * @return {!Array<number>}
  197. * @package
  198. */
  199. function readPackedFixed32(bufferDecoder, start) {
  200. return readPacked(bufferDecoder, start, readFixed32);
  201. }
  202. /**
  203. * Reads a packed float field, which consists of a length header and a list of
  204. * fixed64.
  205. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  206. * @param {number} start Start of the data.
  207. * @return {!Array<number>}
  208. * @package
  209. */
  210. function readPackedFloat(bufferDecoder, start) {
  211. return readPacked(bufferDecoder, start, readFloat);
  212. }
  213. /**
  214. * Reads a packed int32 field, which consists of a length header and a list of
  215. * varint.
  216. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  217. * @param {number} start Start of the data.
  218. * @return {!Array<number>}
  219. * @package
  220. */
  221. function readPackedInt32(bufferDecoder, start) {
  222. return readPacked(bufferDecoder, start, readInt32);
  223. }
  224. /**
  225. * Reads a packed int64 field, which consists of a length header and a list
  226. * of int64.
  227. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  228. * @param {number} start Start of the data.
  229. * @return {!Array<!Int64>}
  230. * @package
  231. */
  232. function readPackedInt64(bufferDecoder, start) {
  233. return readPacked(bufferDecoder, start, readInt64);
  234. }
  235. /**
  236. * Reads a packed sfixed32 field, which consists of a length header and a list
  237. * of sfixed32.
  238. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  239. * @param {number} start Start of the data.
  240. * @return {!Array<number>}
  241. * @package
  242. */
  243. function readPackedSfixed32(bufferDecoder, start) {
  244. return readPacked(bufferDecoder, start, readSfixed32);
  245. }
  246. /**
  247. * Reads a packed sfixed64 field, which consists of a length header and a list
  248. * of sfixed64.
  249. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  250. * @param {number} start Start of the data.
  251. * @return {!Array<!Int64>}
  252. * @package
  253. */
  254. function readPackedSfixed64(bufferDecoder, start) {
  255. return readPacked(bufferDecoder, start, readSfixed64);
  256. }
  257. /**
  258. * Reads a packed sint32 field, which consists of a length header and a list of
  259. * varint.
  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 readPackedSint32(bufferDecoder, start) {
  266. return readPacked(bufferDecoder, start, readSint32);
  267. }
  268. /**
  269. * Reads a packed sint64 field, which consists of a length header and a list
  270. * of sint64.
  271. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  272. * @param {number} start Start of the data.
  273. * @return {!Array<!Int64>}
  274. * @package
  275. */
  276. function readPackedSint64(bufferDecoder, start) {
  277. return readPacked(bufferDecoder, start, readSint64);
  278. }
  279. /**
  280. * Reads a packed uint32 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 readPackedUint32(bufferDecoder, start) {
  288. return readPacked(bufferDecoder, start, readUint32);
  289. }
  290. /**
  291. * Read packed values.
  292. * @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
  293. * @param {number} start Start of the data.
  294. * @param {function(!BufferDecoder, number):T} valueFunction
  295. * @return {!Array<T>}
  296. * @package
  297. * @template T
  298. */
  299. function readPacked(bufferDecoder, start, valueFunction) {
  300. const /** !Array<T> */ result = [];
  301. const unsignedLength = bufferDecoder.getUnsignedVarint32At(start);
  302. const dataStart = bufferDecoder.cursor();
  303. while (bufferDecoder.cursor() < dataStart + unsignedLength) {
  304. checkState(bufferDecoder.cursor() > 0);
  305. result.push(valueFunction(bufferDecoder, bufferDecoder.cursor()));
  306. }
  307. return result;
  308. }
  309. exports = {
  310. readBool,
  311. readBytes,
  312. readDelimited,
  313. readDouble,
  314. readFixed32,
  315. readFloat,
  316. readInt32,
  317. readInt64,
  318. readSint32,
  319. readSint64,
  320. readSfixed32,
  321. readSfixed64,
  322. readString,
  323. readUint32,
  324. readPackedBool,
  325. readPackedDouble,
  326. readPackedFixed32,
  327. readPackedFloat,
  328. readPackedInt32,
  329. readPackedInt64,
  330. readPackedSfixed32,
  331. readPackedSfixed64,
  332. readPackedSint32,
  333. readPackedSint64,
  334. readPackedUint32,
  335. };