shark_utils.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "shark_utils.h"
  2. void shark_encode_u16(shark_u8 *buff, shark_u16 value)
  3. {
  4. shark_encode_u8(buff, value);
  5. shark_encode_u8(buff + 1, value >> 8);
  6. }
  7. void shark_encode_u24(shark_u8 *buff, shark_u32 value)
  8. {
  9. shark_encode_u16(buff, value);
  10. shark_encode_u8(buff + 2, value >> 16);
  11. }
  12. void shark_encode_u32(shark_u8 *buff, shark_u32 value)
  13. {
  14. shark_encode_u24(buff, value);
  15. shark_encode_u8(buff + 3, value >> 24);
  16. }
  17. void shark_encode_u64(shark_u8 *buff, shark_u64 value)
  18. {
  19. memcpy(buff, &value, sizeof(value));
  20. }
  21. shark_u16 shark_decode_u16(const shark_u8 *buff)
  22. {
  23. return SHARK_U16(buff[1]) << 8 | shark_decode_u8(buff);
  24. }
  25. shark_u32 shark_decode_u24(const shark_u8 *buff)
  26. {
  27. return SHARK_U24(buff[2]) << 16 | shark_decode_u16(buff);
  28. }
  29. shark_u32 shark_decode_u32(const shark_u8 *buff)
  30. {
  31. return SHARK_U32(buff[3]) << 24 | shark_decode_u24(buff);
  32. }
  33. shark_u64 shark_decode_u64(const shark_u8 *buff)
  34. {
  35. shark_u64 value;
  36. memcpy(&value, buff, sizeof(buff));
  37. return value;
  38. }