DataDeserializer.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file DataDeserializer.h
  13. @brief The class \ref kNet::DataDeserializer DataDeserializer. */
  14. #include "kNetBuildConfig.h"
  15. #include "kNet/Types.h"
  16. #include "BasicSerializedDataTypes.h"
  17. #include "SerializedDataIterator.h"
  18. namespace kNet
  19. {
  20. /// DataDeserializer is an utility class that walks through and deserializes data in a stream of raw bytes. The stream
  21. /// itself does not contain information about what types of data is contained within, but the user of DataDeserializer must
  22. /// know the contents of the data.
  23. /// DataDeserializer never copies the data it is given to read into an internal memory buffer, but instead it reads
  24. /// the given existing memory buffers. DataDeserializer maintains an internal bit offset position to keep track the position
  25. /// that is currently being read.
  26. class DataDeserializer
  27. {
  28. public:
  29. /// Constructs a DataDeserializer that reads its data from the given buffer.
  30. /// DataDeserializer will not copy the contents of the buffer to its own memory area, so
  31. /// be sure to keep the data alive and unmoved for the duration DataDeserializer exists.
  32. /// @param data A pointer to the data to deserialize. This may be null, but only if size == 0. If data == 0 and size > 0,
  33. /// an exception is thrown.
  34. /// @param size The number of bytes in the input buffer.
  35. DataDeserializer(const char *data, size_t size);
  36. /// Constructs a DataDeserializer that reads its data from the given buffer.
  37. /// DataDeserializer will not copy the contents of the buffer to its own memory area, so
  38. /// be sure to keep the data alive and unmoved for the duration DataDeserializer exists.
  39. /// @param data A pointer to the data to deserialize. This may be null, but only if size == 0. If data == 0 and size > 0,
  40. /// an exception is thrown.
  41. /// @param size The number of bytes in the input buffer.
  42. /// @param msgTemplate A pointer to an existing message template structure, which is used
  43. /// to validate that deserialization of the data proceeds in the defined order.
  44. /// DataDeserializer does not make a copy of this description, but dereferences
  45. /// it directly. Be sure to keep it alive for the duration that DataDeserializer exists.
  46. /// Do not pass in a zero pointer here.
  47. DataDeserializer(const char *data, size_t size, const SerializedMessageDesc *msgTemplate);
  48. /// Moves the bit offset position counter to the beginning of the data buffer.
  49. void ResetTraversal();
  50. /// Deserializes a single value of type T off the stream and advances the internal read offset.
  51. template<typename T>
  52. T Read();
  53. static const u32 VLEReadError = 0xFFFFFFFF;
  54. /// Reads a variable-length encoded integer off the stream and advances the internal read offset.
  55. template<typename VLEType>
  56. u32 ReadVLE();
  57. /// Deserializes an array of values of type T off the stream and advances the internal read offset.
  58. /// @param dst [out] Pointer to an array to receive the read data.
  59. /// @param numElems The number of elements to read. The array dst must be able to hold that many elements.
  60. template<typename T>
  61. void ReadArray(T *dst, size_t numElems);
  62. /// Reads an ASCII string from the stream. If we are using a template, the template tells how the string is stored.
  63. /// Otherwise, we are assuming the string was stored length-prepended, using a single byte to denote the length (no null padding).
  64. /// Use the 's8' data type with dynamicCount set in the message template description for a string variable.
  65. /// The returned string will only contain ascii values in the range [32, 253], 0x0D, 0x0A, 0x09. Other values will
  66. /// be replaced with a space bar character (0x20). Because of this string validation method, do not use this function
  67. /// to extract binary data of any kind (base64-encoded is fine).
  68. std::string ReadString();
  69. /// Reads the given amount of bits and packs them into a u32, which is returned.
  70. /// @param numBits the number of bits to read, [1, 32].
  71. u32 ReadBits(int numBits);
  72. float ReadUnsignedFixedPoint(int numIntegerBits, int numDecimalBits);
  73. float ReadSignedFixedPoint(int numIntegerBits, int numDecimalBits);
  74. float ReadQuantizedFloat(float minRange, float maxRange, int numBits);
  75. float ReadMiniFloat(bool signBit, int exponentBits, int mantissaBits, int exponentBias);
  76. void ReadNormalizedVector2D(int numBits, float &x, float &y);
  77. void ReadVector2D(int magnitudeIntegerBits, int magnitudeDecimalBits, int directionBits, float &x, float &y);
  78. void ReadNormalizedVector3D(int numBitsYaw, int numBitsPitch, float &x, float &y, float &z);
  79. void ReadVector3D(int numBitsYaw, int numBitsPitch, int magnitudeIntegerBits, int magnitudeDecimalBits, float &x, float &y, float &z);
  80. void ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2);
  81. void ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3);
  82. void ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3, int &val4, int max4);
  83. void ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3, int &val4, int max4, int &val5, int max5);
  84. u32 GetDynamicElemCount();
  85. /// @return The number of bytes left in the stream to read.
  86. u32 BytesLeft() const { return (u32)((elemOfs >= size) ? 0 : (size - elemOfs)); }
  87. /// @return The number of bits left in the stream to read.
  88. u32 BitsLeft() const { return (u32)((elemOfs >= size) ? 0 : ((size - elemOfs)*8 - bitOfs)); }
  89. /// @return The current byte of the stream that will be read next.
  90. u32 BytePos() const { return (u32)elemOfs; }
  91. /// @return The bit index, [0, 7] of the byte that will be read next. The reading will proceed from LSBit to MSBit, i.e. 0 -> 7.
  92. u32 BitPos() const { return bitOfs; }
  93. /// @return The number of bits read so far in total.
  94. size_t BitsReadTotal() const { return elemOfs * 8 + bitOfs; }
  95. /// @return A pointer in the byte stream at the current read position.
  96. const char *CurrentData() const { return data + BytePos(); }
  97. /// Advances the read pointer with the given amount of bits. Can only be used in nontemplate read mode.
  98. void SkipBits(int numBits);
  99. /// Advances the read pointer with the given amount of bytes. Can only be used in nontemplate read mode.
  100. void SkipBytes(int numBytes) { SkipBits(numBytes * 8); }
  101. private:
  102. /// The data pointer to read from.
  103. const char *data;
  104. /// The length of the read buffer in bytes.
  105. size_t size;
  106. /// The element we're reading next from in the data buffer.
  107. size_t elemOfs;
  108. /// The current bit index of the byte we're reading, [0, 7].
  109. int bitOfs;
  110. Ptr(SerializedDataIterator) iter;
  111. u32 ReadBitsToU32(int count);
  112. DataDeserializer(const DataDeserializer &);
  113. void operator =(const DataDeserializer &);
  114. };
  115. template<typename T>
  116. T DataDeserializer::Read()
  117. {
  118. assert(!iter || iter->NextElementType() == SerializedDataTypeTraits<T>::type);
  119. T value;
  120. u8 *data = reinterpret_cast<u8*>(&value);
  121. for(size_t i = 0; i < sizeof(value); ++i)
  122. data[i] = (u8)ReadBitsToU32(8);
  123. if (iter)
  124. iter->ProceedToNextVariable();
  125. return value;
  126. }
  127. template<> std::string DataDeserializer::Read<std::string>();
  128. template<> bool DataDeserializer::Read<bit>();
  129. template<typename VLEType>
  130. u32 DataDeserializer::ReadVLE()
  131. {
  132. const u32 cStreamEndError = 0xFFFFFFFF;
  133. if (BitsLeft() < VLEType::numBits1 + 1)
  134. return cStreamEndError;
  135. u32 sml = ReadBits(VLEType::numBits1 + 1);
  136. if ((sml & (1 << VLEType::numBits1)) == 0)
  137. return sml;
  138. sml &= (1 << VLEType::numBits1) - 1;
  139. const u32 numBitsMed = VLEType::numBits2 + (VLEType::numBits3 == 0 ? 0 : 1);
  140. if (BitsLeft() < numBitsMed)
  141. return cStreamEndError;
  142. u32 med = ReadBits(numBitsMed);
  143. if ((med & (1 << VLEType::numBits2)) == 0)
  144. return sml | (med << VLEType::numBits1);
  145. if (BitsLeft() < (u32)VLEType::numBits3)
  146. return cStreamEndError;
  147. med &= (1 << VLEType::numBits2) - 1;
  148. u32 large = ReadBits(VLEType::numBits3);
  149. return sml | (med << VLEType::numBits1) | (large << (VLEType::numBits1 + VLEType::numBits2));
  150. }
  151. template<typename T>
  152. void DataDeserializer::ReadArray(T *dst, size_t numElems)
  153. {
  154. for(size_t i = 0; i < numElems; ++i)
  155. dst[i] = Read<T>();
  156. // The above will move on to the next variable in the template accordingly, but if the number
  157. // of elements to read is zero, it needs to be assumed that this was a dynamic block and
  158. // the size was 0.
  159. if (numElems == 0 && iter)
  160. iter->ProceedToNextVariable();
  161. }
  162. template<>
  163. void DataDeserializer::ReadArray(bit *dst, size_t numElems);
  164. } // ~kNet