libbin 1.0.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ #ifndef LIBBIN_C_H__
2
+ #define LIBBIN_C_H__
3
+ #include "./half.h"
4
+ #include "./pghalf.h"
5
+ #include "./libbin_endian.h"
6
+ #include "./data_types.h"
7
+
8
+
9
+ extern VALUE mLibBin;
10
+ extern VALUE cField;
11
+
12
+ #endif
@@ -0,0 +1,126 @@
1
+ #ifndef LIBBIN_ENDIAN_H__
2
+ #define LIBBIN_ENDIAN_H__
3
+
4
+ #include <stdint.h>
5
+
6
+ static inline void little_big_swap(void *addr, size_t sz) {
7
+ char *p = (char *)addr;
8
+ for (size_t i = 0, j = sz - 1; i < (sz >> 1); i++, j--) {
9
+ char tmp = p[i];
10
+ p[i] = p[j];
11
+ p[j] = tmp;
12
+ }
13
+ }
14
+ #define LITTLE_BIG_SWAP(val) little_big_swap(&(val), sizeof(val))
15
+
16
+ static inline unsigned is_little_endian(void)
17
+ {
18
+ const union { unsigned u; unsigned char c[4]; } one = { 1 };
19
+ return one.c[0];
20
+ }
21
+
22
+ /* https://stackoverflow.com/a/2182184 */
23
+ static inline uint8_t bswap_uint8(uint8_t x) {
24
+ return x;
25
+ }
26
+
27
+ static inline uint16_t bswap_uint16(uint16_t x) {
28
+ return ((( x & 0xff00u ) >> 8 ) |
29
+ (( x & 0x00ffu ) << 8 ));
30
+ }
31
+
32
+ static inline uint32_t bswap_uint32(uint32_t x) {
33
+ return ((( x & 0xff000000u ) >> 24 ) |
34
+ (( x & 0x00ff0000u ) >> 8 ) |
35
+ (( x & 0x0000ff00u ) << 8 ) |
36
+ (( x & 0x000000ffu ) << 24 ));
37
+ }
38
+
39
+ static inline uint64_t bswap_uint64(uint64_t x) {
40
+ return ((( x & 0xff00000000000000ull ) >> 56 ) |
41
+ (( x & 0x00ff000000000000ull ) >> 40 ) |
42
+ (( x & 0x0000ff0000000000ull ) >> 24 ) |
43
+ (( x & 0x000000ff00000000ull ) >> 8 ) |
44
+ (( x & 0x00000000ff000000ull ) << 8 ) |
45
+ (( x & 0x0000000000ff0000ull ) << 24 ) |
46
+ (( x & 0x000000000000ff00ull ) << 40 ) |
47
+ (( x & 0x00000000000000ffull ) << 56 ));
48
+ }
49
+
50
+
51
+
52
+ #define SWAP_CONVERT(TYPE, MAPPED_NAME, MAPPED_TYPE) \
53
+ do { \
54
+ union { TYPE t; MAPPED_TYPE m; } v = { .m = x }; \
55
+ v.m = bswap_ ## MAPPED_NAME(v.m); \
56
+ return v.t; \
57
+ } while (0)
58
+
59
+ #define CONVERT(TYPE, MAPPED_TYPE) \
60
+ do { \
61
+ union { TYPE t; MAPPED_TYPE m; } v = { .m = x }; \
62
+ return v.t; \
63
+ } while (0)
64
+
65
+ #define CONVERT_SWAP(TYPE, MAPPED_NAME, MAPPED_TYPE) \
66
+ do { \
67
+ union { TYPE t; MAPPED_TYPE m; } v = { .t = x }; \
68
+ v.m = bswap_ ## MAPPED_NAME(v.m); \
69
+ return v.m; \
70
+ } while(0)
71
+
72
+ #define UNPACKER_BE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
73
+ static inline TYPE unpack_ ## NAME ## _be(MAPPED_TYPE x) { \
74
+ if (is_little_endian()) \
75
+ SWAP_CONVERT(TYPE, MAPPED_NAME, MAPPED_TYPE); \
76
+ else \
77
+ CONVERT(TYPE, MAPPED_TYPE); \
78
+ }
79
+
80
+ #define UNPACKER_LE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
81
+ static inline TYPE unpack_ ## NAME ## _le(MAPPED_TYPE x) { \
82
+ if (is_little_endian()) \
83
+ CONVERT(TYPE, MAPPED_TYPE); \
84
+ else \
85
+ SWAP_CONVERT(TYPE, MAPPED_NAME, MAPPED_TYPE); \
86
+ }
87
+
88
+ #define PACKER_BE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
89
+ static inline MAPPED_TYPE pack_ ## NAME ## _be(TYPE x) { \
90
+ if (is_little_endian()) \
91
+ CONVERT_SWAP(TYPE, MAPPED_NAME, MAPPED_TYPE); \
92
+ else \
93
+ CONVERT(MAPPED_TYPE, TYPE); \
94
+ }
95
+
96
+ #define PACKER_LE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
97
+ static inline MAPPED_TYPE pack_ ## NAME ## _le(TYPE x) { \
98
+ if (is_little_endian()) \
99
+ CONVERT(MAPPED_TYPE, TYPE); \
100
+ else \
101
+ CONVERT_SWAP(TYPE, MAPPED_NAME, MAPPED_TYPE); \
102
+ }
103
+
104
+ #define CONVERTER_TYPE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
105
+ UNPACKER_BE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
106
+ UNPACKER_LE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
107
+ PACKER_BE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE) \
108
+ PACKER_LE(NAME, TYPE, MAPPED_NAME, MAPPED_TYPE)
109
+
110
+ #define CONVERTER(NAME, TYPE, SIZE) \
111
+ CONVERTER_TYPE(NAME, TYPE, uint ## SIZE, uint ## SIZE ## _t)
112
+
113
+ CONVERTER(uint8, uint8_t, 8)
114
+ CONVERTER(int8, int8_t, 8)
115
+ CONVERTER(uint16, uint16_t, 16)
116
+ CONVERTER(int16, int16_t, 16)
117
+ CONVERTER(uint32, uint32_t, 32)
118
+ CONVERTER(int32, int32_t, 32)
119
+ CONVERTER(uint64, uint64_t, 64)
120
+ CONVERTER(int64, int64_t, 64)
121
+ CONVERTER(float, float, 32)
122
+ CONVERTER(double, double, 64)
123
+ CONVERTER(half, uint16_t, 16)
124
+ CONVERTER(pghalf, uint16_t, 16)
125
+
126
+ #endif