DataDeserializer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /** @file DataDeserializer.cpp
  12. @brief */
  13. #include <cassert>
  14. #include <cstring>
  15. #include <cmath>
  16. #include "kNet/DebugMemoryLeakCheck.h"
  17. #include "kNet/VLEPacker.h"
  18. #include "kNet/DataDeserializer.h"
  19. #include "kNet/NetException.h"
  20. using namespace std;
  21. namespace kNet
  22. {
  23. DataDeserializer::DataDeserializer(const char *data_, size_t size_)
  24. :data(data_), size(size_)
  25. {
  26. if (!data && size > 0)
  27. throw NetException("Specified a null input buffer to DataDeserializer, but a nonzero input size!");
  28. ResetTraversal();
  29. }
  30. DataDeserializer::DataDeserializer(const char *data_, size_t size_, const SerializedMessageDesc *msgTemplate)
  31. :data(data_), size(size_)
  32. {
  33. if (!data && size > 0)
  34. throw NetException("Specified a null input buffer to DataDeserializer, but a nonzero input size!");
  35. assert(msgTemplate);
  36. iter = new SerializedDataIterator(*msgTemplate);
  37. ResetTraversal();
  38. }
  39. void DataDeserializer::ResetTraversal()
  40. {
  41. elemOfs = 0;
  42. bitOfs = 0;
  43. if (iter)
  44. iter->ResetTraversal();
  45. }
  46. u32 DataDeserializer::ReadBitsToU32(int count)
  47. {
  48. if (BitsLeft() < (u32)count)
  49. throw NetException("Not enough bits left in DataDeserializer::ReadBits!");
  50. u32 var = 0;
  51. int bitsFilled = 0;
  52. while(count > 0)
  53. {
  54. int bitsToRead = std::min(std::min(8, count), 8 - bitOfs);
  55. u32 readMask = ((1 << bitsToRead) - 1) << bitOfs;
  56. assert(elemOfs < size); // We already counted above we should have enough bits to read.
  57. var |= (((u32)data[elemOfs] & readMask) >> bitOfs) << bitsFilled;
  58. bitsFilled += bitsToRead;
  59. bitOfs += bitsToRead;
  60. assert(bitOfs <= 8);
  61. // If bitOfs has accumulated a full byte, move elemOfs ahead by one.
  62. elemOfs += (bitOfs >> 3);
  63. bitOfs &= 7;
  64. count -= bitsToRead;
  65. }
  66. return var;
  67. }
  68. /// Need to have a message template to use this function.
  69. u32 DataDeserializer::GetDynamicElemCount()
  70. {
  71. const SerializedElementDesc *desc = iter->NextElementDesc();
  72. assert(desc);
  73. assert(desc->varyingCount == true);
  74. if (BitsLeft() < (u32)desc->count)
  75. throw NetException("Not enough bits left in DataDeserializer::GetDynamicElemCount!");
  76. u32 count = ReadBitsToU32(desc->count);
  77. iter->SetVaryingElemSize(count);
  78. return count;
  79. }
  80. template<>
  81. std::string DataDeserializer::Read<std::string>()
  82. {
  83. return ReadString();
  84. }
  85. template<>
  86. bool DataDeserializer::Read<bit>()
  87. {
  88. assert(!iter || iter->NextElementType() == SerialBit);
  89. bool value = (ReadBitsToU32(1) != 0);
  90. if (iter)
  91. iter->ProceedToNextVariable();
  92. return value;
  93. }
  94. u32 DataDeserializer::ReadBits(int numBits)
  95. {
  96. assert(!iter || iter->NextElementType() == SerialBit);
  97. u32 val = ReadBitsToU32(numBits);
  98. if (iter)
  99. iter->ProceedNVariables(numBits);
  100. return val;
  101. }
  102. float DataDeserializer::ReadUnsignedFixedPoint(int numIntegerBits, int numDecimalBits)
  103. {
  104. u32 fp = ReadBits(numIntegerBits + numDecimalBits);
  105. return fp / (float)(1 << numDecimalBits);
  106. }
  107. float DataDeserializer::ReadSignedFixedPoint(int numIntegerBits, int numDecimalBits)
  108. {
  109. // Reading a [0, 2k-1] range -> remap back to [-k, k-1] range.
  110. return ReadUnsignedFixedPoint(numIntegerBits, numDecimalBits) - (float)(1 << (numIntegerBits-1));
  111. }
  112. float DataDeserializer::ReadQuantizedFloat(float minRange, float maxRange, int numBits)
  113. {
  114. u32 val = ReadBits(numBits);
  115. return minRange + val * (maxRange-minRange) / (float)((1 << numBits) - 1);
  116. }
  117. float DataDeserializer::ReadMiniFloat(bool signBit, int exponentBits, int mantissaBits, int exponentBias)
  118. {
  119. assert(sizeof(float) == 4);
  120. assert(exponentBits > 0);
  121. assert(exponentBits <= 8);
  122. assert(mantissaBits > 0);
  123. assert(mantissaBits <= 23);
  124. bool sign = signBit ? Read<bit>() : false;
  125. u32 exponent = ReadBits(exponentBits);
  126. u32 mantissa = ReadBits(mantissaBits);
  127. // Shift back the decoded mantissa to proper position.
  128. mantissa <<= 23 - mantissaBits;
  129. // Reconstruct the float exponent.
  130. if (exponent == (u32)((1 << exponentBits) - 1)) // If the read exponent was all ones, reconstruct 11111111.
  131. exponent = 0xFF;
  132. else if (exponent != 0) // If the read exponent was not zero, it was a normal number.
  133. exponent = exponent - exponentBias + 127;
  134. // else exponent == 0, meaning a zero or a denormal.
  135. u32 value = (sign ? 0x80000000 : 0) | (exponent << 23) | mantissa;
  136. return *(float*)&value;
  137. }
  138. #define PI ((float)3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679)
  139. void DataDeserializer::ReadNormalizedVector2D(int numBits, float &x, float &y)
  140. {
  141. float angle = ReadQuantizedFloat(-PI, PI, numBits);
  142. x = cos(angle);
  143. y = sin(angle);
  144. }
  145. void DataDeserializer::ReadVector2D(int magnitudeIntegerBits, int magnitudeDecimalBits, int directionBits, float &x, float &y)
  146. {
  147. // Read the length in unsigned fixed point format.
  148. // The following line is effectively the same as calling ReadUnsignedFixedPoint, but manually perform it
  149. // to be precisely able to examine whether the length is zero.
  150. u32 fp = ReadBits(magnitudeIntegerBits + magnitudeDecimalBits);
  151. if (fp != 0) // If length is non-zero, the stream also contains the direction.
  152. {
  153. float length = fp / (float)(1 << magnitudeDecimalBits);
  154. // Read the direction in the stream.
  155. float angle = ReadQuantizedFloat(-PI, PI, directionBits);
  156. x = cos(angle) * length;
  157. y = sin(angle) * length;
  158. }
  159. else // Zero length, no direction present in the buffer.
  160. {
  161. x = y = 0.f;
  162. }
  163. }
  164. void DataDeserializer::ReadNormalizedVector3D(int numBitsYaw, int numBitsPitch, float &x, float &y, float &z)
  165. {
  166. float azimuth = ReadQuantizedFloat(-PI, PI, numBitsYaw);
  167. float inclination = ReadQuantizedFloat(-PI/2, PI/2, numBitsPitch);
  168. float cx = cos(inclination);
  169. x = cx * sin(azimuth);
  170. y = -sin(inclination);
  171. z = cx * cos(azimuth);
  172. }
  173. void DataDeserializer::ReadVector3D(int numBitsYaw, int numBitsPitch, int magnitudeIntegerBits, int magnitudeDecimalBits, float &x, float &y, float &z)
  174. {
  175. // Read the length in unsigned fixed point format.
  176. // The following line is effectively the same as calling ReadUnsignedFixedPoint, but manually perform it
  177. // to be precisely able to examine whether the length is zero.
  178. u32 fp = ReadBits(magnitudeIntegerBits + magnitudeDecimalBits);
  179. if (fp != 0) // If length is non-zero, the stream also contains the direction.
  180. {
  181. float length = fp / (float)(1 << magnitudeDecimalBits);
  182. float azimuth = ReadQuantizedFloat(-PI, PI, numBitsYaw);
  183. float inclination = ReadQuantizedFloat(-PI/2, PI/2, numBitsPitch);
  184. float cx = cos(inclination);
  185. x = cx * sin(azimuth) * length;
  186. y = -sin(inclination) * length;
  187. z = cx * cos(azimuth) * length;
  188. }
  189. else // length is zero, stream does not contain the direction.
  190. {
  191. x = y = z = 0.f;
  192. }
  193. }
  194. void DataDeserializer::ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2)
  195. {
  196. assert(max1 * max2 < (1 << numBits));
  197. u32 val = ReadBits(numBits);
  198. val2 = val % max2;
  199. val1 = val / max2;
  200. }
  201. void DataDeserializer::ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3)
  202. {
  203. assert(max1 * max2 * max3 < (1 << numBits));
  204. u32 val = ReadBits(numBits);
  205. val3 = val % max3;
  206. val /= max3;
  207. val2 = val % max2;
  208. val1 = val / max2;
  209. }
  210. void DataDeserializer::ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3, int &val4, int max4)
  211. {
  212. assert(max1 * max2 * max3 * max4 < (1 << numBits));
  213. u32 val = ReadBits(numBits);
  214. val4 = val % max4;
  215. val /= max4;
  216. val3 = val % max3;
  217. val /= max3;
  218. val2 = val % max2;
  219. val1 = val / max2;
  220. }
  221. void DataDeserializer::ReadArithmeticEncoded(int numBits, int &val1, int max1, int &val2, int max2, int &val3, int max3, int &val4, int max4, int &val5, int max5)
  222. {
  223. assert(max1 * max2 * max3 * max4 * max5 < (1 << numBits));
  224. u32 val = ReadBits(numBits);
  225. val5 = val % max5;
  226. val /= max5;
  227. val4 = val % max4;
  228. val /= max4;
  229. val3 = val % max3;
  230. val /= max3;
  231. val2 = val % max2;
  232. val1 = val / max2;
  233. }
  234. void DataDeserializer::SkipBits(int numBits)
  235. {
  236. assert(!iter);
  237. bitOfs += numBits;
  238. // Move all accumulated full bytes from bitOfs to elemOfs.
  239. elemOfs += (bitOfs >> 3);
  240. bitOfs &= 7;
  241. if (elemOfs > size)
  242. throw NetException("Not enough bits left in DataDeserializer::SkipBits!");
  243. if (elemOfs == size && bitOfs != 0)
  244. throw NetException("Not enough bits left in DataDeserializer::SkipBits(2)!");
  245. }
  246. std::string DataDeserializer::ReadString()
  247. {
  248. u32 length = (iter ? GetDynamicElemCount() : ReadVLE<VLE8_16_32>());
  249. if (BitsLeft() < length*8)
  250. throw NetException("Not enough bytes left in DataDeserializer::ReadString!");
  251. std::string str;
  252. if (bitOfs == 0)
  253. {
  254. str.append(data + elemOfs, length);
  255. elemOfs += length;
  256. }
  257. else
  258. {
  259. std::vector<u8> bytes(length+1);
  260. ReadArray<u8>(&bytes[0], length);
  261. str.append((char*)&bytes[0], length);
  262. }
  263. if (iter)
  264. for(u32 i = 0; i < length; ++i)
  265. iter->ProceedToNextVariable();
  266. // Perform string validation: Replace any offending values with the space bar character.
  267. // Valid values: 0x00 (null), 0x09 (tab), 0x0D, 0x0A (newlines), [32, 253] (characters)
  268. for(size_t i = 0; i < str.length(); ++i)
  269. if ((unsigned char)str[i] >= 254 || ((unsigned char)str[i] < 32 && str[i] != 0x0D && str[i] != 0x0A && str[i] != 0x09)) // Retain newlines and tab.
  270. str[i] = 0x20; // Space bar character
  271. return str;
  272. }
  273. } // ~kNet