PbReader.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "PbReader.h"
  2. #include <iostream>
  3. PbReader::PbReader(std::vector<uint8_t> const &rawData) : rawData(rawData)
  4. {
  5. maxPosition = rawData.size();
  6. }
  7. template <typename T>
  8. T PbReader::decodeVarInt()
  9. {
  10. uint8_t byte;
  11. uint_fast8_t bitpos = 0;
  12. uint64_t storago = 0;
  13. do
  14. {
  15. byte = this->rawData[pos];
  16. pos++;
  17. storago |= (uint64_t)(byte & 0x7F) << bitpos;
  18. bitpos = (uint_fast8_t)(bitpos + 7);
  19. } while (byte & 0x80);
  20. return static_cast<T>(storago);
  21. }
  22. template <typename T>
  23. T PbReader::decodeFixed()
  24. {
  25. pos += sizeof(T);
  26. return *(T*)(&this->rawData[pos - sizeof(T)]);
  27. }
  28. template int32_t PbReader::decodeFixed();
  29. template int64_t PbReader::decodeFixed();
  30. template uint32_t PbReader::decodeVarInt();
  31. template int64_t PbReader::decodeVarInt();
  32. template int32_t PbReader::decodeVarInt();
  33. template bool PbReader::decodeVarInt();
  34. void PbReader::resetMaxPosition()
  35. {
  36. maxPosition = rawData.size();
  37. }
  38. void PbReader::decodeString(std::string &target)
  39. {
  40. nextFieldLength = decodeVarInt<uint32_t>();
  41. target.resize(nextFieldLength);
  42. // std::cout << "rawData.size() = " << rawData.size() << " pos = " << pos << " nextFieldLength =" << nextFieldLength;
  43. // printf("\n%d, \n", currentTag);
  44. // if (pos + nextFieldLength >= rawData.size())
  45. // {
  46. // std::cout << " \nBAD -- pos + nextFieldLength >= rawData.size() MSVC IS LITERLALLY SHAKING AND CRYING RN";
  47. // }
  48. // std::cout << std::endl;
  49. std::copy(rawData.begin() + pos, rawData.begin() + pos + nextFieldLength, target.begin());
  50. pos += nextFieldLength;
  51. }
  52. void PbReader::decodeVector(std::vector<uint8_t> &target)
  53. {
  54. nextFieldLength = decodeVarInt<uint32_t>();
  55. target.resize(nextFieldLength);
  56. std::copy(rawData.begin() + pos, rawData.begin() + pos + nextFieldLength, target.begin());
  57. pos += nextFieldLength;
  58. }
  59. bool PbReader::next()
  60. {
  61. if (pos >= maxPosition)
  62. return false;
  63. currentWireValue = decodeVarInt<uint32_t>();
  64. currentTag = currentWireValue >> 3U;
  65. currentWireType = PbWireType(currentWireValue & 0x07U);
  66. return true;
  67. }
  68. int64_t PbReader::decodeZigzag(uint64_t value)
  69. {
  70. return static_cast<int64_t>((value >> 1U) ^ static_cast<uint64_t>(-static_cast<int64_t>(value & 1U)));
  71. }
  72. template <typename T>
  73. T PbReader::decodeSVarInt()
  74. {
  75. skipVarIntDump = decodeVarInt<uint64_t>();
  76. return static_cast<T>(decodeZigzag(skipVarIntDump));
  77. }
  78. template int32_t PbReader::decodeSVarInt();
  79. template int64_t PbReader::decodeSVarInt();
  80. void PbReader::skip()
  81. {
  82. switch (currentWireType)
  83. {
  84. case PbWireType::varint:
  85. skipVarIntDump = decodeVarInt<uint64_t>();
  86. break;
  87. case PbWireType::fixed64:
  88. pos += 8;
  89. break;
  90. case PbWireType::length_delimited:
  91. nextFieldLength = decodeVarInt<uint32_t>();
  92. pos += nextFieldLength;
  93. break;
  94. case PbWireType::fixed32:
  95. pos += 4;
  96. break;
  97. default:
  98. break;
  99. }
  100. }