• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012-2018 Rob Clark <robdclark@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * SOFTWARE.
22  */
23 
24 #ifndef __UTIL_H__
25 #define __UTIL_H__
26 
27 #include <ctype.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 
31 /* old-style program binary XOR'd ascii w/ 0xff */
32 #ifndef ASCII_XOR
33 #  define ASCII_XOR 0
34 #endif
35 
tab(int lvl)36 static inline const char *tab(int lvl)
37 {
38 	const char *TAB = "\t\t\t\t\t\t\t\t\0";
39 	return &TAB[strlen(TAB) - lvl];
40 }
41 
42 /* convert float to dword */
d2f(uint32_t d)43 static inline float d2f(uint32_t d)
44 {
45 	union {
46 		float f;
47 		uint32_t d;
48 	} u = {
49 		.d = d,
50 	};
51 	return u.f;
52 }
53 
dump_hex(const void * buf,int sz)54 static inline void dump_hex(const void *buf, int sz)
55 {
56 	uint8_t *ptr = (uint8_t *)buf;
57 	uint8_t *end = ptr + sz;
58 	int i = 0;
59 
60 	while (ptr < end) {
61 		uint32_t d = 0;
62 
63 		printf((i % 8) ? " " : "\t");
64 
65 		d |= *(ptr++) <<  0;
66 		d |= *(ptr++) <<  8;
67 		d |= *(ptr++) << 16;
68 		d |= *(ptr++) << 24;
69 
70 		printf("%08x", d);
71 
72 		if ((i % 8) == 7) {
73 			printf("\n");
74 		}
75 
76 		i++;
77 	}
78 
79 	if (i % 8) {
80 		printf("\n");
81 	}
82 }
83 
84 static inline void
dump_float(const void * buf,int sz)85 dump_float(const void *buf, int sz)
86 {
87 	uint8_t *ptr = (uint8_t *)buf;
88 	uint8_t *end = ptr + sz - 3;
89 	int i = 0;
90 
91 	while (ptr < end) {
92 		uint32_t d = 0;
93 
94 		printf((i % 8) ? " " : "\t");
95 
96 		d |= *(ptr++) <<  0;
97 		d |= *(ptr++) <<  8;
98 		d |= *(ptr++) << 16;
99 		d |= *(ptr++) << 24;
100 
101 		printf("%8f", d2f(d));
102 
103 		if ((i % 8) == 7) {
104 			printf("\n");
105 		}
106 
107 		i++;
108 	}
109 
110 	if (i % 8) {
111 		printf("\n");
112 	}
113 }
114 
115 #define is_ok_ascii(c) \
116 	(isascii(c) && ((c == '\t') || !iscntrl(c)))
117 
118 static inline void
clean_ascii(char * buf,int sz)119 clean_ascii(char *buf, int sz)
120 {
121 	uint8_t *ptr = (uint8_t *)buf;
122 	uint8_t *end = ptr + sz;
123 	while (ptr < end) {
124 		*(ptr++) ^= ASCII_XOR;
125 	}
126 }
127 
128 static inline void
dump_ascii(const void * buf,int sz)129 dump_ascii(const void *buf, int sz)
130 {
131 	uint8_t *ptr = (uint8_t *)buf;
132 	uint8_t *end = ptr + sz;
133 	printf("\t");
134 	while (ptr < end) {
135 		uint8_t c = *(ptr++) ^ ASCII_XOR;
136 		if (c == '\n') {
137 			printf("\n\t");
138 		} else if (c == '\0') {
139 			printf("\n\t-----------------------------------\n\t");
140 		} else if (is_ok_ascii(c)) {
141 			printf("%c", c);
142 		} else {
143 			printf("?");
144 		}
145 	}
146 	printf("\n");
147 }
148 
149 static inline void
dump_hex_ascii(const void * buf,int sz,int level)150 dump_hex_ascii(const void *buf, int sz, int level)
151 {
152 	uint8_t *ptr = (uint8_t *)buf;
153 	uint8_t *end = ptr + sz;
154 	uint8_t *ascii = ptr;
155 	int i = 0;
156 
157 	printf("%s-----------------------------------------------\n", tab(level));
158 	printf("%s%d (0x%x) bytes\n", tab(level), sz, sz);
159 
160 	while (ptr < end) {
161 		uint32_t d = 0;
162 
163 		if (i % 4) {
164 			printf(" ");
165 		} else {
166 			printf("%s%06x: ", tab(level), (uint32_t)(ptr - (uint8_t *)buf));
167 		}
168 
169 		d |= *(ptr++) <<  0;
170 		d |= *(ptr++) <<  8;
171 		d |= *(ptr++) << 16;
172 		d |= *(ptr++) << 24;
173 
174 		printf("%08x", d);
175 
176 		if ((i % 4) == 3) {
177 			int j;
178 			printf("\t|");
179 			for (j = 0; j < 16; j++) {
180 				uint8_t c = *(ascii++);
181 				c ^= ASCII_XOR;
182 				printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
183 			}
184 			printf("|\n");
185 		}
186 
187 		i++;
188 	}
189 
190 	if (i % 4) {
191 		for (int j = 4 - (i % 4); j > 0; j--) {
192 			printf("         ");
193 		}
194 		printf("\t|");
195 		while (ascii < end) {
196 			uint8_t c = *(ascii++);
197 			c ^= ASCII_XOR;
198 			printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
199 		}
200 		printf("|\n");
201 	}
202 }
203 
204 #endif /* __UTIL_H__ */
205