serializer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include "c99defs.h"
  18. /*
  19. * General programmable serialization functions. (A shared interface to
  20. * various reading/writing to/from different inputs/outputs)
  21. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. enum serialize_seek_type { SERIALIZE_SEEK_START, SERIALIZE_SEEK_CURRENT, SERIALIZE_SEEK_END };
  26. struct serializer {
  27. void *data;
  28. size_t (*read)(void *, void *, size_t);
  29. size_t (*write)(void *, const void *, size_t);
  30. int64_t (*seek)(void *, int64_t, enum serialize_seek_type);
  31. int64_t (*get_pos)(void *);
  32. };
  33. static inline size_t s_read(struct serializer *s, void *data, size_t size)
  34. {
  35. if (s && s->read && data && size)
  36. return s->read(s->data, (void *)data, size);
  37. return 0;
  38. }
  39. static inline size_t s_write(struct serializer *s, const void *data, size_t size)
  40. {
  41. if (s && s->write && data && size)
  42. return s->write(s->data, (void *)data, size);
  43. return 0;
  44. }
  45. static inline size_t serialize(struct serializer *s, void *data, size_t len)
  46. {
  47. if (s) {
  48. if (s->write)
  49. return s->write(s->data, data, len);
  50. else if (s->read)
  51. return s->read(s->data, data, len);
  52. }
  53. return 0;
  54. }
  55. static inline int64_t serializer_seek(struct serializer *s, int64_t offset, enum serialize_seek_type seek_type)
  56. {
  57. if (s && s->seek)
  58. return s->seek(s->data, offset, seek_type);
  59. return -1;
  60. }
  61. static inline int64_t serializer_get_pos(struct serializer *s)
  62. {
  63. if (s && s->get_pos)
  64. return s->get_pos(s->data);
  65. return -1;
  66. }
  67. /* formatted this to be similar to the AVIO layout that ffmpeg uses */
  68. static inline void s_w8(struct serializer *s, uint8_t u8)
  69. {
  70. s_write(s, &u8, sizeof(uint8_t));
  71. }
  72. static inline void s_wl16(struct serializer *s, uint16_t u16)
  73. {
  74. s_w8(s, (uint8_t)u16);
  75. s_w8(s, u16 >> 8);
  76. }
  77. static inline void s_wl24(struct serializer *s, uint32_t u24)
  78. {
  79. s_w8(s, (uint8_t)u24);
  80. s_wl16(s, (uint16_t)(u24 >> 8));
  81. }
  82. static inline void s_wl32(struct serializer *s, uint32_t u32)
  83. {
  84. s_wl16(s, (uint16_t)u32);
  85. s_wl16(s, (uint16_t)(u32 >> 16));
  86. }
  87. static inline void s_wl64(struct serializer *s, uint64_t u64)
  88. {
  89. s_wl32(s, (uint32_t)u64);
  90. s_wl32(s, (uint32_t)(u64 >> 32));
  91. }
  92. static inline void s_wlf(struct serializer *s, float f)
  93. {
  94. s_wl32(s, *(uint32_t *)&f);
  95. }
  96. static inline void s_wld(struct serializer *s, double d)
  97. {
  98. s_wl64(s, *(uint64_t *)&d);
  99. }
  100. static inline void s_wb16(struct serializer *s, uint16_t u16)
  101. {
  102. s_w8(s, u16 >> 8);
  103. s_w8(s, (uint8_t)u16);
  104. }
  105. static inline void s_wb24(struct serializer *s, uint32_t u24)
  106. {
  107. s_wb16(s, (uint16_t)(u24 >> 8));
  108. s_w8(s, (uint8_t)u24);
  109. }
  110. static inline void s_wb32(struct serializer *s, uint32_t u32)
  111. {
  112. s_wb16(s, (uint16_t)(u32 >> 16));
  113. s_wb16(s, (uint16_t)u32);
  114. }
  115. static inline void s_wb64(struct serializer *s, uint64_t u64)
  116. {
  117. s_wb32(s, (uint32_t)(u64 >> 32));
  118. s_wb32(s, (uint32_t)u64);
  119. }
  120. static inline void s_wbf(struct serializer *s, float f)
  121. {
  122. s_wb32(s, *(uint32_t *)&f);
  123. }
  124. static inline void s_wbd(struct serializer *s, double d)
  125. {
  126. s_wb64(s, *(uint64_t *)&d);
  127. }
  128. #ifdef __cplusplus
  129. }
  130. #endif