forked from guyzmo/avr_nrf_ancs_library
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpack_lib.h
193 lines (162 loc) · 6.26 KB
/
pack_lib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
helper functions to work with bytearrays
usage:
pack(unsigned char *buf, const char *fmt, ...);
similar to sprintf():
put all arguments in "..." into the buffer, according to the format
string "fmt", in a way that they can be easily retrieved with unpack()
unpack(const unsigned char *buf, const char *fmt, ...);
similar to sscanf():
retrieves data from the buffer according to "fmt". arguments in "..."
must be pointers to appropriate types.
format specifiers:
b = int8_t B = uint8_t
h = int16_t H = uint16_t
i = int32_t I = uint32_t
l = int64_t L = uint64_t
_ = skip one byte (mainly useful for unpacking)
example:
uint8_t a;
uint16_t b = 1337;
int32_t c = -12345678;
pack(buf, "BBHi", 255, 0, b, c);
unpack(buf, "B_Hi", &a, &b, &c);
assert(a == 255);
assert(b == 1337);
assert(c == -12345678);
source: http://sprunge.us/LEDM
license: WTFPL
by rob`` on freenode, ##c
*/
#ifndef _PACK_H_
#define _PACK_H_
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdint.h>
#ifndef NO_PACK
static size_t pack(unsigned char *buf, const char *fmt, ...);
#endif
#ifndef NO_UNPACK
static size_t unpack(const unsigned char *buf, const char *fmt, ...);
#endif
#ifndef NO_PACK
static void buffer_put(unsigned char *buffer, uint64_t value, size_t bytes);
#endif
#ifndef NO_UNPACK
static uintmax_t buffer_get(const unsigned char *buffer, size_t bytes);
#endif
#define PACK_UNPACK(c, name, CASE) \
static size_t name(c unsigned char *buf, const char *fmt, ...) \
{ \
va_list ap; \
size_t sz, total = 0; \
\
va_start(ap, fmt); \
\
for (; *fmt; fmt++) \
{ \
switch (*fmt) \
{ \
CASE('b', int, int8_t); \
CASE('B', int, uint8_t); \
\
CASE('h', int, int16_t); \
CASE('H', int, uint16_t); \
\
CASE('i', int32_t, int32_t); \
CASE('I', uint32_t, uint32_t); \
\
CASE('l', int64_t, int64_t); \
CASE('L', uint64_t, uint64_t); \
\
case '_': \
sz = 1; \
break; \
\
default: \
va_end(ap); \
return total; \
} \
\
buf += sz; \
total += sz; \
} \
\
va_end(ap); \
\
return total; \
}
#define CASE_PACK(chr, ap_type, type) \
case chr: \
{ \
type val = va_arg(ap, ap_type); \
sz = sizeof (type); \
buffer_put(buf, val, sz); \
} \
break
#define CASE_UNPACK(chr, ap_type, type) \
case chr: \
{ \
type *p = va_arg(ap, type*); \
sz = sizeof (type); \
*p = buffer_get(buf, sz); \
} \
break
#ifndef NO_PACK
/* define pack() */
PACK_UNPACK(, pack, CASE_PACK)
#endif
#ifndef NO_UNPACK
/* define unpack() */
PACK_UNPACK(const, unpack, CASE_UNPACK)
#endif
#ifdef PACK_LITTLE_ENDIAN
// LITTLE ENDIAN VERSION
#ifndef NO_PACK
static void buffer_put(unsigned char *buffer, uint64_t value, size_t bytes)
{
size_t i = 0;
while (bytes-(i++)) {
buffer[i-1] = value & 0xFF;
value >>= 8;
}
}
#endif
#ifndef NO_UNPACK
static uint64_t buffer_get(const unsigned char *buffer, size_t bytes)
{
uint64_t value = 0;
const unsigned char *end = buffer + bytes;
while (buffer < end) {
value <<= 8;
value += *(--end);
}
return value;
}
#endif
#else
// BIG ENDIAN version
#ifndef NO_PACK
static void buffer_put(unsigned char *buffer, uint64_t value, size_t bytes)
{
while (bytes--) {
buffer[bytes] = value & 0xFF;
value >>= 8;
}
}
#endif
#ifndef NO_UNPACK
static uint64_t buffer_get(const unsigned char *buffer, size_t bytes)
{
uint64_t value = 0;
const unsigned char *end = buffer + bytes;
while (buffer < end) {
value <<= 8;
value += *buffer++;
}
return value;
}
#endif // NO_UNPACK
#endif // ENDIAN
#endif // PACK_H