BlobReader.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #pragma once
  2. #include "../CommonDef.h"
  3. #include "MetadataUtil.h"
  4. namespace hybridclr
  5. {
  6. namespace metadata
  7. {
  8. class BlobReader
  9. {
  10. public:
  11. BlobReader(const byte* buf, uint32_t length) : _buf(buf), _length(length), _readPos(0)
  12. {
  13. }
  14. const byte* GetData() const
  15. {
  16. return _buf;
  17. }
  18. uint32_t GetLength() const
  19. {
  20. return _length;
  21. }
  22. uint32_t GetReadPosition() const
  23. {
  24. return _readPos;
  25. }
  26. const byte* GetDataOfReadPosition() const
  27. {
  28. return _buf + _readPos;
  29. }
  30. bool IsEmpty() const
  31. {
  32. return _readPos >= _length;
  33. }
  34. bool NonEmpty() const
  35. {
  36. return _readPos < _length;
  37. }
  38. int32_t ReadCompressedInt32()
  39. {
  40. uint32_t unsignedValue = ReadCompressedUint32();
  41. uint32_t value = unsignedValue >> 1;
  42. if (!(unsignedValue & 0x1))
  43. {
  44. return value;
  45. }
  46. if (value < 0x40)
  47. {
  48. return value - 0x40;
  49. }
  50. if (value < 0x2000)
  51. {
  52. return value - 0x2000;
  53. }
  54. if (value < 0x10000000)
  55. {
  56. return value - 0x10000000;
  57. }
  58. IL2CPP_ASSERT(value < 0x20000000);
  59. return value - 0x20000000;
  60. }
  61. static uint32_t ReadCompressedUint32(const byte* buf, uint32_t& lengthSize)
  62. {
  63. uint32_t firstByte = buf[0];
  64. if (firstByte < 128)
  65. {
  66. lengthSize = 1;
  67. return firstByte;
  68. }
  69. else if (firstByte < 192)
  70. {
  71. lengthSize = 2;
  72. return ((firstByte & 0x3f) << 8) | buf[1];
  73. }
  74. else if (firstByte < 224)
  75. {
  76. lengthSize = 4;
  77. return ((firstByte & 0x1f) << 24) | (((uint32_t)buf[1]) << 16) | ((uint32_t)buf[2] << 8) | (uint32_t)buf[3];
  78. }
  79. else
  80. {
  81. RaiseExecutionEngineException("bad metadata data. ReadEncodeLength fail");
  82. return 0;
  83. }
  84. }
  85. uint32_t ReadCompressedUint32()
  86. {
  87. uint32_t lengthSize;
  88. uint32_t value = ReadCompressedUint32(_buf + _readPos, lengthSize);
  89. _readPos += lengthSize;
  90. return value;
  91. }
  92. uint8_t ReadByte()
  93. {
  94. IL2CPP_ASSERT(_readPos < _length);
  95. return _buf[_readPos++];
  96. }
  97. uint16_t Read16()
  98. {
  99. IL2CPP_ASSERT(_readPos + 2 <= _length);
  100. uint16_t value = GetU2LittleEndian(_buf + _readPos);
  101. _readPos += 2;
  102. return value;
  103. }
  104. uint32_t Read32()
  105. {
  106. IL2CPP_ASSERT(_readPos + 4 <= _length);
  107. uint32_t value = (uint32_t)GetI4LittleEndian(_buf + _readPos);
  108. _readPos += 4;
  109. return value;
  110. }
  111. bool TryRead32(uint32_t& value)
  112. {
  113. if (_readPos + 4 <= _length)
  114. {
  115. value = Read32();
  116. return true;
  117. }
  118. return false;
  119. }
  120. uint64_t Read64()
  121. {
  122. IL2CPP_ASSERT(_readPos + 8 <= _length);
  123. uint64_t value = (uint64_t)GetI8LittleEndian(_buf + _readPos);
  124. _readPos += 8;
  125. return value;
  126. }
  127. float ReadFloat()
  128. {
  129. uint32_t x = Read32();
  130. return *(float*)&x;
  131. }
  132. double ReadDouble()
  133. {
  134. uint64_t x = Read64();
  135. return *(double*)&x;
  136. }
  137. //template<typename T>
  138. //T Read()
  139. //{
  140. // IL2CPP_ASSERT(_readPos + sizeof(T) <= _length);
  141. // T value = *(T*)(_buf + _readPos);
  142. // _readPos += sizeof(T);
  143. // return value;
  144. //}
  145. uint8_t PeekByte()
  146. {
  147. IL2CPP_ASSERT(_readPos < _length);
  148. return _buf[_readPos];
  149. }
  150. void SkipByte()
  151. {
  152. IL2CPP_ASSERT(_readPos < _length);
  153. ++_readPos;
  154. }
  155. void SkipBytes(uint32_t len)
  156. {
  157. IL2CPP_ASSERT(_readPos + len <= _length);
  158. const byte* data = _buf + _readPos;
  159. _readPos += len;
  160. }
  161. const byte* GetAndSkipCurBytes(uint32_t len)
  162. {
  163. IL2CPP_ASSERT(_readPos + len <= _length);
  164. const byte* data = _buf + _readPos;
  165. _readPos += len;
  166. return data;
  167. }
  168. private:
  169. const byte* const _buf;
  170. const uint32_t _length;
  171. uint32_t _readPos;
  172. };
  173. }
  174. }