1
0

serializer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /*
  18. * General programmable serialization functions. (A shared interface to
  19. * various reading/writing to/from different inputs/outputs)
  20. *
  21. * TODO: Not currently implemented
  22. */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. enum serialize_seek_type {
  27. SERIALIZE_SEEK_START,
  28. SERIALIZE_SEEK_CURRENT,
  29. SERIALIZE_SEEK_END
  30. };
  31. struct serializer {
  32. void *param;
  33. size_t (*serialize)(struct serializer, void *, size_t);
  34. uint64_t (*seek)(struct serializer, int64_t, enum serialize_seek_type);
  35. uint64_t (*getpos)(struct serializer);
  36. };
  37. static inline size_t serialize(struct serializer *s, void *data, size_t len)
  38. {
  39. if (s->serialize)
  40. return s->serialize(s, data, len);
  41. return 0;
  42. }
  43. static inline uint64_t serializer_seek(struct serializer *s, int64_t offset,
  44. enum serialize_seek_type seek_type)
  45. {
  46. if (s->seek)
  47. return s->seek(s, offset, seek_type);
  48. return 0;
  49. }
  50. static inline uint64_t serializer_getpos(struct serializer *s)
  51. {
  52. if (s->getpos)
  53. return s->getpos(s);
  54. return 0;
  55. }
  56. static inline void serializer_write_u8(struct serializer *s, uint8_t u8)
  57. {
  58. serialize(s, &u8, sizeof(uint8_t));
  59. }
  60. static inline void serializer_write_i8(struct serializer *s, int8_t i8)
  61. {
  62. serialize(s, &i8, sizeof(int8_t));
  63. }
  64. static inline void serializer_write_u16(struct serializer *s, uint16_t u16)
  65. {
  66. serialize(s, &u16, sizeof(uint16_t));
  67. }
  68. static inline void serializer_write_i16(struct serializer *s, int16_t i16)
  69. {
  70. serialize(s, &i16, sizeof(int16_t));
  71. }
  72. static inline void serializer_write_u32(struct serializer *s, uint32_t u32)
  73. {
  74. serialize(s, &u32, sizeof(uint32_t));
  75. }
  76. static inline void serializer_write_i32(struct serializer *s, int32_t i32)
  77. {
  78. serialize(s, &i32, sizeof(int32_t));
  79. }
  80. static inline void serializer_write_u64(struct serializer *s, uint32_t u64)
  81. {
  82. serialize(s, &u64, sizeof(uint64_t));
  83. }
  84. static inline void serializer_write_i64(struct serializer *s, int32_t i64)
  85. {
  86. serialize(s, &i64, sizeof(int64_t));
  87. }
  88. static inline void serializer_write_float(struct serializer *s, float f)
  89. {
  90. serialize(s, &f, sizeof(float));
  91. }
  92. static inline void serializer_write_double(struct serializer *s, double d)
  93. {
  94. serialize(s, &d, sizeof(double));
  95. }
  96. #ifdef __cplusplus
  97. }
  98. #endif