od.c
10.5 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (C) 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "od.h"
#define _OCTAL_BYTE_LENGTH (3)
#define _INT_BYTE_LENGTH (3)
#define _HEX_BYTE_LENGTH (2)
static inline void _address_format(char *format, uint16_t flags)
{
switch (flags & OD_FLAGS_ADDRESS_MASK) {
case OD_FLAGS_ADDRESS_OCTAL:
strncpy(format, "%09o", sizeof("%09o"));
break;
case OD_FLAGS_ADDRESS_HEX:
strncpy(format, "%06x", sizeof("%06x"));
break;
case OD_FLAGS_ADDRESS_DECIMAL:
strncpy(format, "%07d", sizeof("%07d"));
break;
default:
break;
}
}
static inline uint8_t _length(uint16_t flags)
{
if (flags & OD_FLAGS_BYTES_CHAR) {
return 1;
}
switch (flags & OD_FLAGS_LENGTH_MASK) {
case OD_FLAGS_LENGTH_1:
return 1;
case OD_FLAGS_LENGTH_SHORT:
return sizeof(short);
case OD_FLAGS_LENGTH_2:
return 2;
case OD_FLAGS_LENGTH_LONG:
return sizeof(long);
case OD_FLAGS_LENGTH_8:
return 8;
case OD_FLAGS_LENGTH_4:
default:
return 4;
}
}
static inline void _bytes_format(char *format, uint16_t flags)
{
if (flags & OD_FLAGS_BYTES_CHAR) {
strncpy(format, " %c", sizeof(" %c"));
return;
}
switch (flags & (OD_FLAGS_BYTES_MASK | OD_FLAGS_LENGTH_MASK)) {
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_1:
strncpy(format, " %03o", sizeof(" %03o"));
break;
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_2:
strncpy(format, " %06" PRIo16, sizeof(" %06" PRIo16));
break;
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_4:
strncpy(format, " %012" PRIo32, sizeof(" %012" PRIo32));
break;
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_8:
strncpy(format, " %024" PRIo64, sizeof(" %024" PRIo64));
break;
#if !defined(__MACH__)
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%0%do", sizeof(short) * _OCTAL_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%0%dlo", sizeof(long) * _OCTAL_BYTE_LENGTH);
break;
#else /* !defined(__MACH__) */
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %lu", sizeof(short) * _OCTAL_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_OCTAL | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %lu", sizeof(long) * _OCTAL_BYTE_LENGTH);
break;
#endif /* !defined(__MACH__) */
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_1:
strncpy(format, " %4d", sizeof(" %4d"));
break;
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_2:
strncpy(format, " %6" PRId16, sizeof(" %6" PRId16));
break;
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_4:
strncpy(format, " %12" PRId32, sizeof(" %12" PRId32));
break;
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_8:
strncpy(format, " %24" PRId64, sizeof(" %24" PRId64));
break;
#if !defined(__MACH__)
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%%dd", sizeof(short) * _INT_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%%dld", sizeof(long) * _INT_BYTE_LENGTH);
break;
#else /* !defined(__MACH__) */
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%%ld", sizeof(short) * _INT_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_INT | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%%ld", sizeof(long) * _INT_BYTE_LENGTH);
break;
#endif /* !defined(__MACH__) */
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_1:
strncpy(format, " %3u", sizeof(" %3u"));
break;
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_2:
strncpy(format, " %6" PRIu16, sizeof(" %6" PRIu16));
break;
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_4:
strncpy(format, " %12" PRIu32, sizeof(" %12" PRIu32));
break;
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_8:
strncpy(format, " %24" PRIu64, sizeof(" %24" PRIu64));
break;
#if !defined(__MACH__)
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%%uu", (unsigned)sizeof(short) * _INT_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%%ulu", sizeof(long) * _INT_BYTE_LENGTH);
break;
#else /* !defined(__MACH__) */
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%%lu", sizeof(short) * _INT_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_UINT | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%%lu", sizeof(long) * _INT_BYTE_LENGTH);
break;
#endif /* !defined(__MACH__) */
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_1:
strncpy(format, " %02x", sizeof(" %02x"));
break;
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_2:
strncpy(format, " %04" PRIx16, sizeof(" %04" PRIx16));
break;
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_4:
strncpy(format, " %08" PRIx32, sizeof(" %08" PRIx32));
break;
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_8:
strncpy(format, " %016" PRIx64, sizeof(" %016" PRIx64));
break;
#if !defined(__MACH__)
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%0%ux", (unsigned)sizeof(short) * _HEX_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%0%ulx", (unsigned)sizeof(long) * _HEX_BYTE_LENGTH);
break;
#else /* !defined(__MACH__) */
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_SHORT:
sprintf(format, " %%0%lx", sizeof(short) * _HEX_BYTE_LENGTH);
break;
case OD_FLAGS_BYTES_HEX | OD_FLAGS_LENGTH_LONG:
sprintf(format, " %%0%lx", sizeof(long) * _HEX_BYTE_LENGTH);
break;
#endif /* !defined(__MACH__) */
default:
break;
}
}
static void _print_date(const void *data, size_t offset, char *format, uint8_t length,
uint16_t flags)
{
switch (length) {
case 1:
if (flags & OD_FLAGS_BYTES_CHAR) {
switch (((signed char *)data)[offset]) {
case '\0':
printf(" \\0");
return;
case '\a':
printf(" \\a");
return;
case '\b':
printf(" \\b");
return;
case '\f':
printf(" \\f");
return;
case '\n':
printf(" \\n");
return;
case '\r':
printf(" \\r");
return;
case '\t':
printf(" \\t");
return;
case '\v':
printf(" \\v");
return;
default:
if (((signed char *)data)[offset] < 0) {
printf(" %03o", ((unsigned char *)data)[offset]);
return;
}
else if (((signed char *)data)[offset] < 32) {
printf(" %03o", ((char *)data)[offset]);
return;
}
break;
}
}
if (flags & OD_FLAGS_BYTES_INT) {
printf(format, ((int8_t *)data)[offset]);
}
else {
printf(format, ((uint8_t *)data)[offset]);
}
break;
case 2:
if (flags & OD_FLAGS_BYTES_INT) {
printf(format, ((int16_t *)data)[offset]);
}
else {
printf(format, ((uint16_t *)data)[offset]);
}
break;
case 4:
default:
if (flags & OD_FLAGS_BYTES_INT) {
printf(format, ((int32_t *)data)[offset]);
}
else {
printf(format, ((uint32_t *)data)[offset]);
}
break;
case 8:
#ifndef MODULE_NEWLIB
if (flags & OD_FLAGS_BYTES_INT) {
printf(format, ((int64_t *)data)[offset]);
}
else {
printf(format, ((uint64_t *)data)[offset]);
}
#else
printf("%s", format);
#endif
break;
}
}
static int _log10(uint8_t a)
{
int res = 0;
while (a > 0) {
a /= 10;
++res;
}
return ++res;
}
void od(const void *data, size_t data_len, uint8_t width, uint16_t flags)
{
char address_format[5];
uint8_t date_length = _length(flags);
char bytes_format[_log10(date_length) + 7];
if (data_len == 0) {
return;
}
_address_format(address_format, flags);
_bytes_format(bytes_format, flags);
if (width == 0) {
width = OD_WIDTH_DEFAULT;
}
if (width < date_length) {
width = 1;
}
else {
width = (width / date_length);
}
if (data_len % date_length) {
data_len = (data_len / date_length) + 1;
}
else {
data_len = data_len / date_length;
}
if ((flags & OD_FLAGS_ADDRESS_MASK) != OD_FLAGS_ADDRESS_NONE) {
printf(address_format, 0);
}
for (size_t i = 0; i < data_len; i++) {
_print_date(data, i, bytes_format, date_length, flags);
if ((((i + 1) % width) == 0) || i == (data_len - 1)) {
printf("\n");
if (i != (data_len - 1)) {
if ((flags & OD_FLAGS_ADDRESS_MASK) != OD_FLAGS_ADDRESS_NONE) {
printf(address_format, date_length * (i + 1));
}
}
}
}
}
/** @} */