format.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_FORMAT
  24. #define DFPSR_FORMAT
  25. #include "SafePointer.h"
  26. #include <cstdint>
  27. namespace dsr {
  28. // Helper functions for encoding and decoding binary file formats in buffers.
  29. // Read an unaligned unsigned integer with the first byte at source using little-endian byte order.
  30. uint16_t format_readU16_LE(SafePointer<const uint8_t> source);
  31. uint32_t format_readU24_LE(SafePointer<const uint8_t> source);
  32. uint32_t format_readU32_LE(SafePointer<const uint8_t> source);
  33. uint64_t format_readU64_LE(SafePointer<const uint8_t> source);
  34. // Read an unaligned signed integer in two's complement with the first byte at source using little-endian byte order.
  35. int16_t format_readI16_LE(SafePointer<const uint8_t> source);
  36. int32_t format_readI24_LE(SafePointer<const uint8_t> source);
  37. int32_t format_readI32_LE(SafePointer<const uint8_t> source);
  38. int64_t format_readI64_LE(SafePointer<const uint8_t> source);
  39. // Write an unaligned unsigned integer with the first byte at target using little-endian byte order.
  40. void format_writeU16_LE(SafePointer<uint8_t> target, uint16_t value);
  41. void format_writeU24_LE(SafePointer<uint8_t> target, uint32_t value);
  42. void format_writeU32_LE(SafePointer<uint8_t> target, uint32_t value);
  43. void format_writeU64_LE(SafePointer<uint8_t> target, uint64_t value);
  44. // Write an unaligned signed integer in two's complement with the first byte at target using little-endian byte order.
  45. void format_writeI16_LE(SafePointer<uint8_t> target, int16_t value);
  46. void format_writeI24_LE(SafePointer<uint8_t> target, int32_t value);
  47. void format_writeI32_LE(SafePointer<uint8_t> target, int32_t value);
  48. void format_writeI64_LE(SafePointer<uint8_t> target, int64_t value);
  49. // Convert bits interpreted as a 32-bit IEEE754 floating-point value into the native float representation.
  50. float format_bitsToF32_IEEE754(uint32_t bits);
  51. // Convert bits interpreted as a 64-bit IEEE754 floating-point value into the native double representation.
  52. double format_bitsToF64_IEEE754(uint64_t bits);
  53. }
  54. #endif