ByteReader.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include <stdint.h>
  3. #include "foundation/types.h"
  4. /* A simple byte-oriented reader.
  5. use this instead of manual parsing as this deals with memory alignment issues
  6. for you.
  7. memory alignment can be critical and annoying on some architectures (e.g. PowerPC)
  8. it also handles little-endian/big-endian issues
  9. Usually you just make one of these things on the stack, passing in your buffer and length
  10. S is signed and U is unsigned
  11. Show functions will give you data w/o moving the stream position
  12. Align versions of the functions will assume the stream is properly aligned
  13. LE versions of the functions treat the byte stream as little-endian oriented
  14. */
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. typedef struct bytereader_struct_t
  19. {
  20. size_t byte_length;
  21. const uint8_t *data_ptr;
  22. const void *data;
  23. } bytereader_value_t, bytereader_s, *bytereader_t;
  24. void bytereader_init(bytereader_t byte_reader, const void *data, size_t byte_length);
  25. static size_t bytereader_size(bytereader_t byte_reader) /* returns remaining bytes in stream */
  26. {
  27. return byte_reader->byte_length;
  28. }
  29. void bytereader_advance(bytereader_t byte_reader, size_t bytes); /* advances the byte stream */
  30. void bytereader_reset(bytereader_t byte_reader); /* reset the data pointer and size back to the original position */
  31. static const void *bytereader_pointer(bytereader_t byte_reader) /* returns a pointer to the current bitstream position */
  32. {
  33. return byte_reader->data_ptr;
  34. }
  35. /* returns the number of bytes to the next 0, or the end of the buffer */
  36. size_t bytereader_find_zero(bytereader_t byte_reader);
  37. /* n byte functions (basically memcpy) */
  38. void bytereader_show_n(bytereader_t byte_reader, void *destination, size_t bytes);
  39. void bytereader_read_n(bytereader_t byte_reader, void *destination, size_t bytes);
  40. /* 1 byte functions */
  41. uint8_t bytereader_show_u8(bytereader_t byte_reader);
  42. uint8_t bytereader_read_u8(bytereader_t byte_reader);
  43. int8_t bytereader_show_s8(bytereader_t byte_reader);
  44. int8_t bytereader_read_s8(bytereader_t byte_reader);
  45. /* 2 byte little endian functions */
  46. uint16_t bytereader_show_u16_le(bytereader_t byte_reader);
  47. uint16_t bytereader_read_u16_le(bytereader_t byte_reader);
  48. int16_t bytereader_show_s16_le(bytereader_t byte_reader);
  49. int16_t bytereader_read_s16_le(bytereader_t byte_reader);
  50. /* 2 byte big-endian functions */
  51. uint16_t bytereader_show_u16_be(bytereader_t byte_reader);
  52. uint16_t bytereader_read_u16_be(bytereader_t byte_reader);
  53. int16_t bytereader_show_s16_be(bytereader_t byte_reader);
  54. int16_t bytereader_read_s16_be(bytereader_t byte_reader);
  55. /* 4 byte big-endian functions */
  56. uint32_t bytereader_show_u32_be(bytereader_t byte_reader);
  57. uint32_t bytereader_read_u32_be(bytereader_t byte_reader);
  58. /* 4 byte little-endian functions */
  59. uint32_t bytereader_show_u32_le(bytereader_t byte_reader);
  60. uint32_t bytereader_read_u32_le(bytereader_t byte_reader);
  61. /* float functions */
  62. float bytereader_show_f32_be(bytereader_t byte_reader);
  63. float bytereader_read_f32_be(bytereader_t byte_reader);
  64. GUID bytereader_read_uuid_be(bytereader_t byte_reader);
  65. GUID bytereader_read_uuid_le(bytereader_t byte_reader);
  66. #ifdef __cplusplus
  67. }
  68. #endif