Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

89 lines
1.9 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef UINT32_H
  3. #define UINT32_H
  4. #include <inttypes.h> // uint32_t, int32_t
  5. #include <stddef.h> // size_t
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. typedef uint32_t uint32;
  10. typedef int32_t int32;
  11. #if (defined(__i386__) || defined(__x86_64__)) && !defined(NO_UINT32_MACROS)
  12. static inline void uint32_pack(char* out,uint32 in) {
  13. *(uint32*)out=in;
  14. }
  15. static inline void uint32_unpack(const char *in,uint32* out) {
  16. *out=*(const uint32*)in;
  17. }
  18. static inline uint32 uint32_read(const char* in) {
  19. return *(const uint32*)in;
  20. }
  21. #if defined(__x86_64__) && defined(__GNUC__)
  22. static inline void uint32_pack_big(char *out,uint32 in) {
  23. *(uint32*)out=__builtin_bswap32(in);
  24. }
  25. static inline void uint32_unpack_big(const char *in,uint32* out) {
  26. *out=__builtin_bswap32(*(const uint32*)in);
  27. }
  28. static inline uint32 uint32_read_big(const char *in) {
  29. return __builtin_bswap32(*(const uint32*)in);
  30. }
  31. #else
  32. void uint32_pack_big(char *out,uint32 in);
  33. void uint32_unpack_big(const char *in,uint32* out);
  34. uint32 uint32_read_big(const char *in);
  35. #endif
  36. #else
  37. void uint32_pack(char *out,uint32 in);
  38. void uint32_pack_big(char *out,uint32 in);
  39. void uint32_unpack(const char *in,uint32* out);
  40. void uint32_unpack_big(const char *in,uint32* out);
  41. uint32 uint32_read(const char *in);
  42. uint32 uint32_read_big(const char *in);
  43. #endif
  44. static inline size_t fmt_uint32(char* out, uint32 in) {
  45. if (out) uint32_pack(out,in);
  46. return 4;
  47. }
  48. static inline size_t fmt_uint32_big(char* out, uint32 in) {
  49. if (out) uint32_pack_big(out,in);
  50. return 4;
  51. }
  52. static inline size_t scan_uint32(const char* in, size_t len, uint32_t* b) {
  53. if (len<4) return 0;
  54. *b = uint32_read(in);
  55. return 4;
  56. }
  57. static inline size_t scan_uint32_big(const char* in, size_t len, uint32_t* b) {
  58. if (len<4) return 0;
  59. *b = uint32_read_big(in);
  60. return 4;
  61. }
  62. #ifdef __cplusplus
  63. }
  64. #endif
  65. #endif