Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdlib>
  3.  
  4. const uint8_t GShift[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  5. const uint8_t GMask [8]={0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f};
  6. const uint32_t MAX_NAME_INDEX = 410;
  7.  
  8. struct BitReader {
  9. private:
  10. uint8_t shift(uint8_t count) {
  11. return 1 << count;
  12. }
  13. public:
  14. BitReader(const uint8_t* src_buffer = nullptr, int64_t n_bits = 0) : num(n_bits), pos(0) {
  15. buffer = new uint8_t[(n_bits + 7) >> 3];
  16.  
  17. if (src_buffer != nullptr) {
  18. memcpy(buffer, src_buffer, (n_bits + 7) >> 3);
  19.  
  20. if (num & 7) {
  21. buffer[num >> 3] &= GMask[num & 7];
  22. }
  23. }
  24. }
  25.  
  26. uint32_t read_int(uint32_t max_value) {
  27. uint32_t val = 0;
  28.  
  29. serialize_int(val, max_value);
  30.  
  31. return val;
  32. }
  33.  
  34. // should return data here, but I haven't imlemented the hardcoded stuff, so shrug
  35. void read_fname() {
  36. bool bHardcoded;
  37. serialize_bits(&bHardcoded, 1);
  38. if (bHardcoded) {
  39. uint32_t name_index = read_int(MAX_NAME_INDEX + 1);
  40. } else {
  41. char* attach_socket;
  42. read_fstring(attach_socket);
  43. uint32_t num;
  44. serialize_bits(&num, 32);
  45. }
  46. }
  47.  
  48. // "UnrealEngine/Engine/Source/Runtime/Core/Private/Containers/String.cpp"
  49. void read_fstring(char* & bytes) {
  50. int32_t save_num;
  51. serialize_bits(&save_num, 32);
  52.  
  53. bool LoadUCS2Char = save_num < 0;
  54. if (LoadUCS2Char) {
  55. save_num = -save_num;
  56. }
  57.  
  58. if (save_num) {
  59. if (LoadUCS2Char) {
  60. // uint16_t bytes2 = new uint16_t[save_num];
  61. // serialize(bytes2, save_num * 2);
  62. // bytes2[save_num * 2 - 1] = 0;
  63.  
  64. // pretty sure there aren't any of these in the PUBG data.
  65. printf("LOADUSC2CHAR :( \n");
  66. exit(1);
  67. } else {
  68. bytes = new char[save_num];
  69. serialize(bytes, save_num);
  70.  
  71. bytes[save_num-1] = 0;
  72. }
  73. } else {
  74. bytes = NULL;
  75. }
  76. }
  77.  
  78. void serialize( void* dst, int64_t n_bytes ) {
  79. serialize_bits(dst, n_bytes*8);
  80. }
  81.  
  82. uint8_t read_bit() {
  83. uint8_t bit = 0 ;
  84. int64_t local_pos = pos;
  85. const int64_t local_num = num;
  86. if (local_pos >= local_num) {
  87. // SetOverflowed(1);
  88. //UE_LOG( LogNetSerialization, Error, TEXT( "FBitReader::SerializeInt: LocalPos >= LocalNum" ) );
  89. } else {
  90. bit = !!(buffer[local_pos>>3] & shift(local_pos&7));
  91. pos++;
  92. }
  93.  
  94. return bit;
  95. }
  96.  
  97. uint64_t bits_left() {
  98. return (num - pos);
  99. }
  100.  
  101. void serialize_vector(Vector & value, uint32_t scale, int32_t max_bits) {
  102. uint32_t bits = 0;
  103.  
  104. serialize_int(bits, max_bits);
  105. int32_t bias = 1 << (bits + 1);
  106. uint32_t max = 1 << (bits + 2);
  107.  
  108. uint32_t dx;
  109. uint32_t dy;
  110. uint32_t dz;
  111.  
  112. serialize_int(dx, max);
  113. serialize_int(dy, max);
  114. serialize_int(dz, max);
  115.  
  116. float factor = (float)scale;
  117. value.x = (float)(static_cast<int32_t>(dx)-bias) / factor;
  118. value.y = (float)(static_cast<int32_t>(dy)-bias) / factor;
  119. value.z = (float)(static_cast<int32_t>(dz)-bias) / factor;
  120. }
  121.  
  122. void read_compressed_float(float &Value, uint32_t MaxValue, uint32_t NumBits) {
  123. uint32_t MaxBitValue = (1 << (NumBits - 1)) - 1; // 0111 1111 - Max abs value we will serialize
  124. uint32_t Bias = (1 << (NumBits - 1)) - 1; // 0111 1111 - Max abs value we will serialize
  125. uint32_t SerIntMax = (1 << (NumBits - 0)); // 1 0000 0000 - What we pass into SerializeInt
  126. uint32_t MaxDelta = (1 << (NumBits - 0)) - 1; // 1111 1111 - Max delta is
  127.  
  128. uint32_t Delta;
  129. serialize_int(Delta, SerIntMax);
  130. float UnscaledValue = (float)(((uint32_t) Delta) - Bias );
  131.  
  132. if ( MaxValue > MaxBitValue ) {
  133. // We have to scale down, scale needs to be a float:
  134. const float InvScale = MaxValue / (float)MaxBitValue;
  135. Value = UnscaledValue * InvScale;
  136. } else {
  137. uint32_t scale = MaxBitValue / MaxValue;
  138. const float InvScale = 1.f / (float) scale;
  139. Value = UnscaledValue * InvScale;
  140. }
  141. }
  142.  
  143. void serialize_fixed_vector(Vector & value, uint32_t max_value, int32_t num_bits) {
  144. read_compressed_float(value.x, max_value, num_bits);
  145. read_compressed_float(value.y, max_value, num_bits);
  146. read_compressed_float(value.z, max_value, num_bits);
  147. }
  148.  
  149. void serialize_vector_full(Vector & value) {
  150. float x;
  151. float y;
  152. float z;
  153.  
  154. serialize_float(&x);
  155. serialize_float(&y);
  156. serialize_float(&z);
  157.  
  158. value.x = x;
  159. value.y = y;
  160. value.z = z;
  161. }
  162.  
  163. void serialize_compressed_short_vector(Vector & value) {
  164. uint16_t short_pitch;
  165. uint16_t short_yaw;
  166. uint16_t short_roll;
  167.  
  168. if (read_bit()) {
  169. serialize_bits(&short_pitch, 16);
  170. } else {
  171. short_pitch = 0;
  172. }
  173.  
  174. if (read_bit()) {
  175. serialize_bits(&short_yaw, 16);
  176. } else {
  177. short_yaw = 0;
  178. }
  179.  
  180. if (read_bit()) {
  181. serialize_bits(&short_roll, 16);
  182. } else {
  183. short_roll = 0;
  184. }
  185.  
  186. value.x = (short_pitch * 360.f / 65536.f);
  187. value.y = (short_yaw * 360.f / 65536.f);
  188. value.z = (short_roll * 360.f / 65536.f);
  189. }
  190.  
  191. void serialize_compressed_byte_vector(Vector & value) {
  192. uint8_t short_pitch;
  193. uint8_t short_yaw;
  194. uint8_t short_roll;
  195.  
  196. if (read_bit()) {
  197. serialize_bits(&short_pitch, 8);
  198. } else {
  199. short_pitch = 0;
  200. }
  201.  
  202. if (read_bit()) {
  203. serialize_bits(&short_yaw, 8);
  204. } else {
  205. short_yaw = 0;
  206. }
  207.  
  208. if (read_bit()) {
  209. serialize_bits(&short_roll, 8);
  210. } else {
  211. short_roll = 0;
  212. }
  213.  
  214. value.x = (short_pitch * 360.f / 256.f);
  215. value.y = (short_yaw * 360.f / 256.f);
  216. value.z = (short_roll * 360.f / 256.f);
  217. }
  218.  
  219.  
  220.  
  221. void serialize_int_packed(uint32_t& value) {
  222. value = 0;
  223. uint8_t count = 0;
  224. uint8_t more = 1;
  225.  
  226. while(more) {
  227. uint8_t next_byte;
  228. serialize(&next_byte, 1); // Read next byte
  229.  
  230. more = next_byte & 1; // Check 1 bit to see if theres more after this
  231. next_byte = next_byte >> 1; // Shift to get actual 7 bit value
  232. value += next_byte << (7 * count++); // Add to total value
  233. }
  234. }
  235.  
  236. void serialize_float(float* value) {
  237. serialize(value, sizeof(float));
  238. }
  239.  
  240.  
  241. void appBitsCpy( uint8_t* Dest, int32_t DestBit, uint8_t* Src, int32_t SrcBit, int32_t BitCount) {
  242. if(BitCount == 0) return;
  243.  
  244. // Special case - always at least one bit to copy,
  245. // a maximum of 2 bytes to read, 2 to write - only touch bytes that are actually used.
  246. if (BitCount <= 8) {
  247. uint32_t DestIndex = DestBit / 8;
  248. uint32_t SrcIndex = SrcBit / 8;
  249. uint32_t LastDest =( DestBit+BitCount-1 )/8;
  250. uint32_t LastSrc =( SrcBit +BitCount-1 )/8;
  251. uint32_t ShiftSrc = SrcBit & 7;
  252. uint32_t ShiftDest = DestBit & 7;
  253. uint32_t FirstMask = 0xFF << ShiftDest;
  254. uint32_t LastMask = 0xFE << ((DestBit + BitCount-1) & 7) ; // Pre-shifted left by 1.
  255. uint32_t Accu;
  256.  
  257. if (SrcIndex == LastSrc) {
  258. Accu = (Src[SrcIndex] >> ShiftSrc);
  259. } else {
  260. Accu =( (Src[SrcIndex] >> ShiftSrc) | (Src[LastSrc] << (8 - ShiftSrc)));
  261. }
  262.  
  263. if (DestIndex == LastDest) {
  264. uint32_t MultiMask = FirstMask & ~LastMask;
  265. Dest[DestIndex] = ( ( Dest[DestIndex] & ~MultiMask ) | ((Accu << ShiftDest) & MultiMask) );
  266. } else {
  267. Dest[DestIndex] = (uint8_t)( ( Dest[DestIndex] & ~FirstMask ) | (( Accu << ShiftDest) & FirstMask) ) ;
  268. Dest[LastDest ] = (uint8_t)( ( Dest[LastDest ] & LastMask ) | (( Accu >> (8 - ShiftDest)) & ~LastMask) ) ;
  269. }
  270.  
  271. return;
  272. }
  273.  
  274. // Main copier, uses byte sized shifting. Minimum size is 9 bits, so at least 2 reads and 2 writes.
  275. uint32_t DestIndex = DestBit/8;
  276. uint32_t FirstSrcMask = 0xFF << ( DestBit & 7);
  277. uint32_t LastDest = ( DestBit+BitCount )/8;
  278. uint32_t LastSrcMask = 0xFF << ((DestBit + BitCount) & 7);
  279. uint32_t SrcIndex = SrcBit/8;
  280. uint32_t LastSrc = ( SrcBit+BitCount )/8;
  281. int32_t ShiftCount = (DestBit & 7) - (SrcBit & 7);
  282. int32_t DestLoop = LastDest-DestIndex;
  283. int32_t SrcLoop = LastSrc -SrcIndex;
  284. uint32_t FullLoop;
  285. uint32_t BitAccu;
  286.  
  287. // Lead-in needs to read 1 or 2 source bytes depending on alignment.
  288. if (ShiftCount>=0) {
  289. FullLoop = std::max(DestLoop, SrcLoop);
  290. BitAccu = Src[SrcIndex] << ShiftCount;
  291. ShiftCount += 8; //prepare for the inner loop.
  292. } else {
  293. ShiftCount +=8; // turn shifts -7..-1 into +1..+7
  294. FullLoop = std::max(DestLoop, SrcLoop-1);
  295. BitAccu = Src[SrcIndex] << ShiftCount;
  296. SrcIndex++;
  297. ShiftCount += 8; // Prepare for inner loop.
  298. BitAccu = ( ( (uint32_t)Src[SrcIndex] << ShiftCount ) + (BitAccu)) >> 8;
  299. }
  300.  
  301. // Lead-in - first copy.
  302. Dest[DestIndex] = (uint8_t) (( BitAccu & FirstSrcMask) | ( Dest[DestIndex] & ~FirstSrcMask ) );
  303.  
  304. SrcIndex++;
  305. DestIndex++;
  306.  
  307. // Fast inner loop.
  308. for (; FullLoop>1; FullLoop--) { // ShiftCount ranges from 8 to 15 - all reads are relevant.
  309.  
  310. BitAccu = (( (uint32_t)Src[SrcIndex] << ShiftCount ) + (BitAccu)) >> 8; // Copy in the new, discard the old.
  311. SrcIndex++;
  312. Dest[DestIndex] = (uint8_t) BitAccu; // Copy low 8 bits.
  313. DestIndex++;
  314. }
  315.  
  316. // Lead-out.
  317. if (LastSrcMask != 0xFF) {
  318. if ((uint32_t)(SrcBit + BitCount - 1) / 8 == SrcIndex) {// Last legal byte ?
  319. BitAccu = ( ( (uint32_t)Src[SrcIndex] << ShiftCount ) + (BitAccu)) >> 8;
  320. } else {
  321. BitAccu = BitAccu >> 8;
  322. }
  323.  
  324. Dest[DestIndex] = (uint8_t)( ( Dest[DestIndex] & LastSrcMask ) | (BitAccu & ~LastSrcMask) );
  325. }
  326. }
  327.  
  328. void serialize_bits(void* dst, int64_t n_bits) {
  329. if (pos + n_bits > num) {
  330. printf("%lld + %lld = %lld > %lld OVERFLOWEDDDDD\n", pos, n_bits, pos + n_bits, num);
  331. exit(1);
  332. }
  333. // if (!IsError()) {
  334. // // SetOverflowed(LengthBits);
  335. // //UE_LOG( LogNetSerialization, Error, TEXT( "FBitReader::SerializeBits: Pos + LengthBits > Num" ) );
  336. // }
  337. // FMemory::Memzero( Dest, (LengthBits+7)>>3 );
  338. // return;
  339. // }
  340. //for( int32 i=0; i<LengthBits; i++,Pos++ )
  341. // if( Buffer(Pos>>3) & GShift[Pos&7] )
  342. // ((uint8*)Dest)[i>>3] |= GShift[i&7];
  343. if (n_bits == 1) {
  344. ((uint8_t*)dst)[0] = 0;
  345. if (buffer[pos >> 3] & shift(pos & 7)) {
  346. ((uint8_t*)dst)[0] |= 0x01;
  347. }
  348. pos++;
  349. } else if (n_bits != 0) {
  350. ((uint8_t*)dst)[((n_bits+7) >> 3) - 1] = 0;
  351. appBitsCpy((uint8_t*)dst, 0, buffer, pos, n_bits);
  352. pos += n_bits;
  353. }
  354. }
  355.  
  356. void serialize_int(uint32_t& out_value, uint32_t max_value) {
  357. uint32_t value = 0;
  358. int64_t local_pos = pos;
  359.  
  360. const int64_t local_num = num;
  361.  
  362. for (uint32_t mask = 1; (value + mask) < max_value && mask; mask *= 2, local_pos++) {
  363. if (local_pos >= local_num) {
  364. break;
  365. }
  366.  
  367. if (buffer[local_pos >> 3] & shift(local_pos & 7)) {
  368. value |= mask;
  369. }
  370. }
  371.  
  372. // Now write back
  373. pos = local_pos;
  374. out_value = value;
  375. }
  376.  
  377. void set_data(BitReader & src, int64_t n_bits ) {
  378. num = n_bits;
  379. pos = 0;
  380.  
  381. delete[] buffer;
  382. buffer = new uint8_t[(n_bits+7) >>3];
  383. src.serialize_bits(buffer, n_bits);
  384. }
  385.  
  386. void append_data(BitReader & src, int64_t n_bits) {
  387.  
  388. int64_t oldnum_bytes = (num + 7) >> 3;
  389. num += n_bits;
  390.  
  391. uint8_t* tmp = new uint8_t[(num + 7) >> 3];
  392.  
  393. this->serialize_bits(tmp, oldnum_bytes * 8);
  394. src.serialize_bits(tmp + oldnum_bytes, n_bits);
  395.  
  396. delete[] buffer;
  397. buffer = tmp;
  398.  
  399. pos = 0;
  400. if (num & 7) {
  401. buffer[num >> 3] &= GMask[num & 7];
  402. }
  403. }
  404.  
  405. int64_t get_pos() {
  406. return pos;
  407. }
  408.  
  409. int64_t get_num() {
  410. return num;
  411. }
  412.  
  413. ~BitReader() {
  414. if (buffer) {
  415. delete[] buffer;
  416. }
  417. }
  418.  
  419. protected:
  420. uint8_t* buffer;
  421. int64_t num;
  422. int64_t pos;
  423. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement