BinaryDecoder.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "Decoder.hh"
  19. #include "Exception.hh"
  20. #include "Zigzag.hh"
  21. #include <memory>
  22. namespace avro {
  23. using std::make_shared;
  24. class BinaryDecoder : public Decoder {
  25. StreamReader in_;
  26. void init(InputStream &is) final;
  27. void decodeNull() final;
  28. bool decodeBool() final;
  29. int32_t decodeInt() final;
  30. int64_t decodeLong() final;
  31. float decodeFloat() final;
  32. double decodeDouble() final;
  33. void decodeString(std::string &value) final;
  34. void skipString() final;
  35. void decodeBytes(std::vector<uint8_t> &value) final;
  36. void skipBytes() final;
  37. void decodeFixed(size_t n, std::vector<uint8_t> &value) final;
  38. void skipFixed(size_t n) final;
  39. size_t decodeEnum() final;
  40. size_t arrayStart() final;
  41. size_t arrayNext() final;
  42. size_t skipArray() final;
  43. size_t mapStart() final;
  44. size_t mapNext() final;
  45. size_t skipMap() final;
  46. size_t decodeUnionIndex() final;
  47. int64_t doDecodeLong();
  48. size_t doDecodeItemCount();
  49. size_t doDecodeLength();
  50. void drain() final;
  51. };
  52. DecoderPtr binaryDecoder() {
  53. return make_shared<BinaryDecoder>();
  54. }
  55. void BinaryDecoder::init(InputStream &is) {
  56. in_.reset(is);
  57. }
  58. void BinaryDecoder::decodeNull() {
  59. }
  60. bool BinaryDecoder::decodeBool() {
  61. auto v = in_.read();
  62. if (v == 0) {
  63. return false;
  64. } else if (v == 1) {
  65. return true;
  66. }
  67. throw Exception("Invalid value for bool: {}", v);
  68. }
  69. int32_t BinaryDecoder::decodeInt() {
  70. auto val = doDecodeLong();
  71. if (val < INT32_MIN || val > INT32_MAX) {
  72. throw Exception("Value out of range for Avro int: {}", val);
  73. }
  74. return static_cast<int32_t>(val);
  75. }
  76. int64_t BinaryDecoder::decodeLong() {
  77. return doDecodeLong();
  78. }
  79. float BinaryDecoder::decodeFloat() {
  80. float result;
  81. in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(float));
  82. return result;
  83. }
  84. double BinaryDecoder::decodeDouble() {
  85. double result;
  86. in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(double));
  87. return result;
  88. }
  89. size_t BinaryDecoder::doDecodeLength() {
  90. ssize_t len = decodeInt();
  91. if (len < 0) {
  92. throw Exception("Cannot have negative length: {}", len);
  93. }
  94. return len;
  95. }
  96. void BinaryDecoder::drain() {
  97. in_.drain(false);
  98. }
  99. void BinaryDecoder::decodeString(std::string &value) {
  100. size_t len = doDecodeLength();
  101. value.resize(len);
  102. if (len > 0) {
  103. in_.readBytes(const_cast<uint8_t *>(
  104. reinterpret_cast<const uint8_t *>(value.c_str())),
  105. len);
  106. }
  107. }
  108. void BinaryDecoder::skipString() {
  109. size_t len = doDecodeLength();
  110. in_.skipBytes(len);
  111. }
  112. void BinaryDecoder::decodeBytes(std::vector<uint8_t> &value) {
  113. size_t len = doDecodeLength();
  114. value.resize(len);
  115. if (len > 0) {
  116. in_.readBytes(value.data(), len);
  117. }
  118. }
  119. void BinaryDecoder::skipBytes() {
  120. size_t len = doDecodeLength();
  121. in_.skipBytes(len);
  122. }
  123. void BinaryDecoder::decodeFixed(size_t n, std::vector<uint8_t> &value) {
  124. value.resize(n);
  125. if (n > 0) {
  126. in_.readBytes(value.data(), n);
  127. }
  128. }
  129. void BinaryDecoder::skipFixed(size_t n) {
  130. in_.skipBytes(n);
  131. }
  132. size_t BinaryDecoder::decodeEnum() {
  133. return static_cast<size_t>(doDecodeLong());
  134. }
  135. size_t BinaryDecoder::arrayStart() {
  136. return doDecodeItemCount();
  137. }
  138. size_t BinaryDecoder::doDecodeItemCount() {
  139. auto result = doDecodeLong();
  140. if (result < 0) {
  141. doDecodeLong();
  142. return static_cast<size_t>(-result);
  143. }
  144. return static_cast<size_t>(result);
  145. }
  146. size_t BinaryDecoder::arrayNext() {
  147. return static_cast<size_t>(doDecodeLong());
  148. }
  149. size_t BinaryDecoder::skipArray() {
  150. for (;;) {
  151. auto r = doDecodeLong();
  152. if (r < 0) {
  153. auto n = static_cast<size_t>(doDecodeLong());
  154. in_.skipBytes(n);
  155. } else {
  156. return static_cast<size_t>(r);
  157. }
  158. }
  159. }
  160. size_t BinaryDecoder::mapStart() {
  161. return doDecodeItemCount();
  162. }
  163. size_t BinaryDecoder::mapNext() {
  164. return doDecodeItemCount();
  165. }
  166. size_t BinaryDecoder::skipMap() {
  167. return skipArray();
  168. }
  169. size_t BinaryDecoder::decodeUnionIndex() {
  170. return static_cast<size_t>(doDecodeLong());
  171. }
  172. int64_t BinaryDecoder::doDecodeLong() {
  173. uint64_t encoded = 0;
  174. int shift = 0;
  175. uint8_t u;
  176. do {
  177. if (shift >= 64) {
  178. throw Exception("Invalid Avro varint");
  179. }
  180. u = in_.read();
  181. encoded |= static_cast<uint64_t>(u & 0x7f) << shift;
  182. shift += 7;
  183. } while (u & 0x80);
  184. return decodeZigzag64(encoded);
  185. }
  186. } // namespace avro