BinaryDecoder.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(boost::format("Invalid value for bool: %1%") % v);
  68. }
  69. int32_t BinaryDecoder::decodeInt() {
  70. auto val = doDecodeLong();
  71. if (val < INT32_MIN || val > INT32_MAX) {
  72. throw Exception(
  73. boost::format("Value out of range for Avro int: %1%") % val);
  74. }
  75. return static_cast<int32_t>(val);
  76. }
  77. int64_t BinaryDecoder::decodeLong() {
  78. return doDecodeLong();
  79. }
  80. float BinaryDecoder::decodeFloat() {
  81. float result;
  82. in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(float));
  83. return result;
  84. }
  85. double BinaryDecoder::decodeDouble() {
  86. double result;
  87. in_.readBytes(reinterpret_cast<uint8_t *>(&result), sizeof(double));
  88. return result;
  89. }
  90. size_t BinaryDecoder::doDecodeLength() {
  91. ssize_t len = decodeInt();
  92. if (len < 0) {
  93. throw Exception(
  94. boost::format("Cannot have negative length: %1%") % len);
  95. }
  96. return len;
  97. }
  98. void BinaryDecoder::drain() {
  99. in_.drain(false);
  100. }
  101. void BinaryDecoder::decodeString(std::string &value) {
  102. size_t len = doDecodeLength();
  103. value.resize(len);
  104. if (len > 0) {
  105. in_.readBytes(const_cast<uint8_t *>(
  106. reinterpret_cast<const uint8_t *>(value.c_str())),
  107. len);
  108. }
  109. }
  110. void BinaryDecoder::skipString() {
  111. size_t len = doDecodeLength();
  112. in_.skipBytes(len);
  113. }
  114. void BinaryDecoder::decodeBytes(std::vector<uint8_t> &value) {
  115. size_t len = doDecodeLength();
  116. value.resize(len);
  117. if (len > 0) {
  118. in_.readBytes(value.data(), len);
  119. }
  120. }
  121. void BinaryDecoder::skipBytes() {
  122. size_t len = doDecodeLength();
  123. in_.skipBytes(len);
  124. }
  125. void BinaryDecoder::decodeFixed(size_t n, std::vector<uint8_t> &value) {
  126. value.resize(n);
  127. if (n > 0) {
  128. in_.readBytes(value.data(), n);
  129. }
  130. }
  131. void BinaryDecoder::skipFixed(size_t n) {
  132. in_.skipBytes(n);
  133. }
  134. size_t BinaryDecoder::decodeEnum() {
  135. return static_cast<size_t>(doDecodeLong());
  136. }
  137. size_t BinaryDecoder::arrayStart() {
  138. return doDecodeItemCount();
  139. }
  140. size_t BinaryDecoder::doDecodeItemCount() {
  141. auto result = doDecodeLong();
  142. if (result < 0) {
  143. doDecodeLong();
  144. return static_cast<size_t>(-result);
  145. }
  146. return static_cast<size_t>(result);
  147. }
  148. size_t BinaryDecoder::arrayNext() {
  149. return static_cast<size_t>(doDecodeLong());
  150. }
  151. size_t BinaryDecoder::skipArray() {
  152. for (;;) {
  153. auto r = doDecodeLong();
  154. if (r < 0) {
  155. auto n = static_cast<size_t>(doDecodeLong());
  156. in_.skipBytes(n);
  157. } else {
  158. return static_cast<size_t>(r);
  159. }
  160. }
  161. }
  162. size_t BinaryDecoder::mapStart() {
  163. return doDecodeItemCount();
  164. }
  165. size_t BinaryDecoder::mapNext() {
  166. return doDecodeItemCount();
  167. }
  168. size_t BinaryDecoder::skipMap() {
  169. return skipArray();
  170. }
  171. size_t BinaryDecoder::decodeUnionIndex() {
  172. return static_cast<size_t>(doDecodeLong());
  173. }
  174. int64_t BinaryDecoder::doDecodeLong() {
  175. uint64_t encoded = 0;
  176. int shift = 0;
  177. uint8_t u;
  178. do {
  179. if (shift >= 64) {
  180. throw Exception("Invalid Avro varint");
  181. }
  182. u = in_.read();
  183. encoded |= static_cast<uint64_t>(u & 0x7f) << shift;
  184. shift += 7;
  185. } while (u & 0x80);
  186. return decodeZigzag64(encoded);
  187. }
  188. } // namespace avro