serializer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2013 Hugh 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 {
  26. SERIALIZE_SEEK_START,
  27. SERIALIZE_SEEK_CURRENT,
  28. SERIALIZE_SEEK_END
  29. };
  30. struct serializer {
  31. void *data;
  32. size_t (*read)(void *, void *, size_t);
  33. size_t (*write)(void *, const void *, size_t);
  34. int64_t (*seek)(void *, int64_t, enum serialize_seek_type);
  35. int64_t (*get_pos)(void *);
  36. };
  37. static inline size_t s_read(struct serializer *s, void *data, size_t size)
  38. {
  39. if (s && s->read && data && size)
  40. return s->read(s->data, (void *)data, size);
  41. return 0;
  42. }
  43. static inline size_t s_write(struct serializer *s, const void *data,
  44. size_t size)
  45. {
  46. if (s && s->write && data && size)
  47. return s->write(s->data, (void *)data, size);
  48. return 0;
  49. }
  50. static inline size_t serialize(struct serializer *s, void *data, size_t len)
  51. {
  52. if (s) {
  53. if (s->write)
  54. return s->write(s->data, data, len);
  55. else if (s->read)
  56. return s->read(s->data, data, len);
  57. }
  58. return 0;
  59. }
  60. static inline int64_t serializer_seek(struct serializer *s, int64_t offset,
  61. enum serialize_seek_type seek_type)
  62. {
  63. if (s && s->seek)
  64. return s->seek(s->data, offset, seek_type);
  65. return -1;
  66. }
  67. static inline int64_t serializer_get_pos(struct serializer *s)
  68. {
  69. if (s && s->get_pos)
  70. return s->get_pos(s->data);
  71. return -1;
  72. }
  73. /* formatted this to be similar to the AVIO layout that ffmpeg uses */
  74. static inline void s_w8(struct serializer *s, uint8_t u8)
  75. {
  76. s_write(s, &u8, sizeof(uint8_t));
  77. }
  78. static inline void s_wl16(struct serializer *s, uint16_t u16)
  79. {
  80. s_w8(s, (uint8_t)u16);
  81. s_w8(s, u16 >> 8);
  82. }
  83. static inline void s_wl24(struct serializer *s, uint32_t u24)
  84. {
  85. s_w8(s, (uint8_t)u24);
  86. s_wl16(s, (uint16_t)(u24 >> 8));
  87. }
  88. static inline void s_wl32(struct serializer *s, uint32_t u32)
  89. {
  90. s_wl16(s, (uint16_t)u32);
  91. s_wl16(s, (uint16_t)(u32 >> 16));
  92. }
  93. static inline void s_wl64(struct serializer *s, uint64_t u64)
  94. {
  95. s_wl32(s, (uint32_t)u64);
  96. s_wl32(s, (uint32_t)(u64 >> 32));
  97. }
  98. static inline void s_wlf(struct serializer *s, float f)
  99. {
  100. s_wl32(s, *(uint32_t *)&f);
  101. }
  102. static inline void s_wld(struct serializer *s, double d)
  103. {
  104. s_wl64(s, *(uint64_t *)&d);
  105. }
  106. static inline void s_wb16(struct serializer *s, uint16_t u16)
  107. {
  108. s_w8(s, u16 >> 8);
  109. s_w8(s, (uint8_t)u16);
  110. }
  111. static inline void s_wb24(struct serializer *s, uint32_t u24)
  112. {
  113. s_wb16(s, (uint16_t)(u24 >> 8));
  114. s_w8(s, (uint8_t)u24);
  115. }
  116. static inline void s_wb32(struct serializer *s, uint32_t u32)
  117. {
  118. s_wb16(s, (uint16_t)(u32 >> 16));
  119. s_wb16(s, (uint16_t)u32);
  120. }
  121. static inline void s_wb64(struct serializer *s, uint64_t u64)
  122. {
  123. s_wb32(s, (uint32_t)(u64 >> 32));
  124. s_wb32(s, (uint32_t)u64);
  125. }
  126. static inline void s_wbf(struct serializer *s, float f)
  127. {
  128. s_wb32(s, *(uint32_t *)&f);
  129. }
  130. static inline void s_wbd(struct serializer *s, double d)
  131. {
  132. s_wb64(s, *(uint64_t *)&d);
  133. }
  134. #ifdef __cplusplus
  135. }
  136. #endif