2 * Copyright (c) 2012-2018 Rob Clark <robdclark@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 /* old-style program binary XOR'd ascii w/ 0xff */
36 static inline const char *tab(int lvl
)
38 const char *TAB
= "\t\t\t\t\t\t\t\t\0";
39 return &TAB
[strlen(TAB
) - lvl
];
42 /* convert float to dword */
43 static inline float d2f(uint32_t d
)
54 static inline void dump_hex(const void *buf
, int sz
)
56 uint8_t *ptr
= (uint8_t *)buf
;
57 uint8_t *end
= ptr
+ sz
;
63 printf((i
% 8) ? " " : "\t");
85 dump_float(const void *buf
, int sz
)
87 uint8_t *ptr
= (uint8_t *)buf
;
88 uint8_t *end
= ptr
+ sz
- 3;
94 printf((i
% 8) ? " " : "\t");
101 printf("%8f", d2f(d
));
115 #define is_ok_ascii(c) \
116 (isascii(c) && ((c == '\t') || !iscntrl(c)))
119 clean_ascii(char *buf
, int sz
)
121 uint8_t *ptr
= (uint8_t *)buf
;
122 uint8_t *end
= ptr
+ sz
;
124 *(ptr
++) ^= ASCII_XOR
;
129 dump_ascii(const void *buf
, int sz
)
131 uint8_t *ptr
= (uint8_t *)buf
;
132 uint8_t *end
= ptr
+ sz
;
135 uint8_t c
= *(ptr
++) ^ ASCII_XOR
;
138 } else if (c
== '\0') {
139 printf("\n\t-----------------------------------\n\t");
140 } else if (is_ok_ascii(c
)) {
150 dump_hex_ascii(const void *buf
, int sz
, int level
)
152 uint8_t *ptr
= (uint8_t *)buf
;
153 uint8_t *end
= ptr
+ sz
;
154 uint8_t *ascii
= ptr
;
157 printf("%s-----------------------------------------------\n", tab(level
));
158 printf("%s%d (0x%x) bytes\n", tab(level
), sz
, sz
);
166 printf("%s%06x: ", tab(level
), (uint32_t)(ptr
- (uint8_t *)buf
));
179 for (j
= 0; j
< 16; j
++) {
180 uint8_t c
= *(ascii
++);
182 printf("%c", (isascii(c
) && !iscntrl(c
)) ? c
: '.');
191 for (int j
= 4 - (i
% 4); j
> 0; j
--) {
195 while (ascii
< end
) {
196 uint8_t c
= *(ascii
++);
198 printf("%c", (isascii(c
) && !iscntrl(c
)) ? c
: '.');
204 #endif /* __UTIL_H__ */