Guest User

Untitled

a guest
Apr 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. inline void put_uint32_le(unsigned char *dst, uint32_t n)
  2. {
  3. *reinterpret_cast<uint32_t *> (dst) = n;
  4. }
  5.  
  6. inline void put_uint32_le(unsigned char *dst, uint32_t n)
  7. {
  8. dst[0] = static_cast<unsigned char> (n & 255);
  9. dst[1] = static_cast<unsigned char> ((n >> 8) & 255);
  10. dst[2] = static_cast<unsigned char> ((n >> 16) & 255);
  11. dst[3] = static_cast<unsigned char> (n >> 24);
  12. }
  13.  
  14. inline void put_uint32_le(unsigned char *dst, uint32_t n)
  15. {
  16. #ifdef WE_HAVE_LITTLE_ENDIAN_PROCESSOR
  17. *reinterpret_cast<uint32_t *> (dst) = n;
  18. #else
  19. dst[0] = static_cast<unsigned char> (n & 255);
  20. dst[1] = static_cast<unsigned char> ((n >> 8) & 255);
  21. dst[2] = static_cast<unsigned char> ((n >> 16) & 255);
  22. dst[3] = static_cast<unsigned char> (n >> 24);
  23. #endif
  24. }
  25.  
  26. union
  27. {
  28. uint32_t v;
  29. uint8_t c[4];
  30. }value;
  31.  
  32. #include <string.h>
  33. #include <stdint.h>
  34.  
  35. inline uint16_t get_uint16_le(const unsigned char *src)
  36. {
  37. return *reinterpret_cast<const uint16_t *> (src);
  38. }
  39.  
  40. inline void put_uint32_le(unsigned char *dst, uint_fast32_t n)
  41. {
  42. memcpy(dst, &n, 4);
  43. // *reinterpret_cast<uint32_t *> (dst) = n;
  44. }
  45.  
  46. extern const uint32_t crc32_table[256];
  47.  
  48. static uint32_t calculate_crc32(const unsigned char *buf, size_t len)
  49. {
  50. uint32_t crc = 0;
  51.  
  52. while (len--)
  53. {
  54. crc ^= crc32_table[*(buf++)];
  55. }
  56.  
  57. return crc;
  58. }
  59.  
  60. void set_crc32(unsigned char *begin)
  61. {
  62. size_t sz = 3 + get_uint16_le(begin);
  63. put_uint32_le(begin + sz, calculate_crc32(begin, sz));
  64. }
Add Comment
Please, Sign In to add comment