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