121042e41SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
299eaf3c4SRandy Dunlap /*
399eaf3c4SRandy Dunlap * lib/hexdump.c
499eaf3c4SRandy Dunlap */
599eaf3c4SRandy Dunlap
699eaf3c4SRandy Dunlap #include <linux/types.h>
799eaf3c4SRandy Dunlap #include <linux/ctype.h>
89888a588SAndy Shevchenko #include <linux/errno.h>
999eaf3c4SRandy Dunlap #include <linux/kernel.h>
10b296a6d5SAndy Shevchenko #include <linux/minmax.h>
118bc3bcc9SPaul Gortmaker #include <linux/export.h>
120f70fe60SHoracio Mijail Anton Quiles #include <asm/unaligned.h>
1399eaf3c4SRandy Dunlap
143fc95772SHarvey Harrison const char hex_asc[] = "0123456789abcdef";
153fc95772SHarvey Harrison EXPORT_SYMBOL(hex_asc);
16c26d436cSAndre Naujoks const char hex_asc_upper[] = "0123456789ABCDEF";
17c26d436cSAndre Naujoks EXPORT_SYMBOL(hex_asc_upper);
183fc95772SHarvey Harrison
1999eaf3c4SRandy Dunlap /**
2090378889SAndy Shevchenko * hex_to_bin - convert a hex digit to its real value
2190378889SAndy Shevchenko * @ch: ascii character represents hex digit
2290378889SAndy Shevchenko *
2390378889SAndy Shevchenko * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
2490378889SAndy Shevchenko * input.
25e5be1576SMikulas Patocka *
26e5be1576SMikulas Patocka * This function is used to load cryptographic keys, so it is coded in such a
27e5be1576SMikulas Patocka * way that there are no conditions or memory accesses that depend on data.
28e5be1576SMikulas Patocka *
29e5be1576SMikulas Patocka * Explanation of the logic:
30e5be1576SMikulas Patocka * (ch - '9' - 1) is negative if ch <= '9'
31e5be1576SMikulas Patocka * ('0' - 1 - ch) is negative if ch >= '0'
32e5be1576SMikulas Patocka * we "and" these two values, so the result is negative if ch is in the range
33e5be1576SMikulas Patocka * '0' ... '9'
34e5be1576SMikulas Patocka * we are only interested in the sign, so we do a shift ">> 8"; note that right
35e5be1576SMikulas Patocka * shift of a negative value is implementation-defined, so we cast the
36e5be1576SMikulas Patocka * value to (unsigned) before the shift --- we have 0xffffff if ch is in
37e5be1576SMikulas Patocka * the range '0' ... '9', 0 otherwise
38e5be1576SMikulas Patocka * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
39e5be1576SMikulas Patocka * in the range '0' ... '9', 0 otherwise
40e5be1576SMikulas Patocka * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
41e5be1576SMikulas Patocka * ... '9', -1 otherwise
42e5be1576SMikulas Patocka * the next line is similar to the previous one, but we need to decode both
43e5be1576SMikulas Patocka * uppercase and lowercase letters, so we use (ch & 0xdf), which converts
44e5be1576SMikulas Patocka * lowercase to uppercase
4590378889SAndy Shevchenko */
hex_to_bin(unsigned char ch)46e5be1576SMikulas Patocka int hex_to_bin(unsigned char ch)
4790378889SAndy Shevchenko {
48e5be1576SMikulas Patocka unsigned char cu = ch & 0xdf;
49e5be1576SMikulas Patocka return -1 +
50e5be1576SMikulas Patocka ((ch - '0' + 1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
51e5be1576SMikulas Patocka ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
5290378889SAndy Shevchenko }
5390378889SAndy Shevchenko EXPORT_SYMBOL(hex_to_bin);
5490378889SAndy Shevchenko
5590378889SAndy Shevchenko /**
56dc88e460SMimi Zohar * hex2bin - convert an ascii hexadecimal string to its binary representation
57dc88e460SMimi Zohar * @dst: binary result
58dc88e460SMimi Zohar * @src: ascii hexadecimal string
59dc88e460SMimi Zohar * @count: result length
60b7804983SMimi Zohar *
619888a588SAndy Shevchenko * Return 0 on success, -EINVAL in case of bad input.
62dc88e460SMimi Zohar */
hex2bin(u8 * dst,const char * src,size_t count)63b7804983SMimi Zohar int hex2bin(u8 *dst, const char *src, size_t count)
64dc88e460SMimi Zohar {
65dc88e460SMimi Zohar while (count--) {
66*e4d8a299SMikulas Patocka int hi, lo;
67b7804983SMimi Zohar
68*e4d8a299SMikulas Patocka hi = hex_to_bin(*src++);
69*e4d8a299SMikulas Patocka if (unlikely(hi < 0))
70*e4d8a299SMikulas Patocka return -EINVAL;
71*e4d8a299SMikulas Patocka lo = hex_to_bin(*src++);
72*e4d8a299SMikulas Patocka if (unlikely(lo < 0))
739888a588SAndy Shevchenko return -EINVAL;
74b7804983SMimi Zohar
75b7804983SMimi Zohar *dst++ = (hi << 4) | lo;
76dc88e460SMimi Zohar }
77b7804983SMimi Zohar return 0;
78dc88e460SMimi Zohar }
79dc88e460SMimi Zohar EXPORT_SYMBOL(hex2bin);
80dc88e460SMimi Zohar
81dc88e460SMimi Zohar /**
8253d91c5cSDavid Howells * bin2hex - convert binary data to an ascii hexadecimal string
8353d91c5cSDavid Howells * @dst: ascii hexadecimal result
8453d91c5cSDavid Howells * @src: binary data
8553d91c5cSDavid Howells * @count: binary data length
8653d91c5cSDavid Howells */
bin2hex(char * dst,const void * src,size_t count)8753d91c5cSDavid Howells char *bin2hex(char *dst, const void *src, size_t count)
8853d91c5cSDavid Howells {
8953d91c5cSDavid Howells const unsigned char *_src = src;
9053d91c5cSDavid Howells
9153d91c5cSDavid Howells while (count--)
9253d91c5cSDavid Howells dst = hex_byte_pack(dst, *_src++);
9353d91c5cSDavid Howells return dst;
9453d91c5cSDavid Howells }
9553d91c5cSDavid Howells EXPORT_SYMBOL(bin2hex);
9653d91c5cSDavid Howells
9753d91c5cSDavid Howells /**
9899eaf3c4SRandy Dunlap * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
9999eaf3c4SRandy Dunlap * @buf: data blob to dump
10099eaf3c4SRandy Dunlap * @len: number of bytes in the @buf
101c7909234SRandy Dunlap * @rowsize: number of bytes to print per line; must be 16 or 32
102c7909234SRandy Dunlap * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
10399eaf3c4SRandy Dunlap * @linebuf: where to put the converted data
10499eaf3c4SRandy Dunlap * @linebuflen: total size of @linebuf, including space for terminating NUL
105c7909234SRandy Dunlap * @ascii: include ASCII after the hex output
10699eaf3c4SRandy Dunlap *
10799eaf3c4SRandy Dunlap * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
108c7909234SRandy Dunlap * 16 or 32 bytes of input data converted to hex + ASCII output.
10999eaf3c4SRandy Dunlap *
11099eaf3c4SRandy Dunlap * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
11199eaf3c4SRandy Dunlap * to a hex + ASCII dump at the supplied memory location.
11299eaf3c4SRandy Dunlap * The converted output is always NUL-terminated.
11399eaf3c4SRandy Dunlap *
11499eaf3c4SRandy Dunlap * E.g.:
115c7909234SRandy Dunlap * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
116db0fd97cSJoe Perches * linebuf, sizeof(linebuf), true);
11799eaf3c4SRandy Dunlap *
11899eaf3c4SRandy Dunlap * example output buffer:
11999eaf3c4SRandy Dunlap * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
120114fc1afSAndy Shevchenko *
121114fc1afSAndy Shevchenko * Return:
122114fc1afSAndy Shevchenko * The amount of bytes placed in the buffer without terminating NUL. If the
123114fc1afSAndy Shevchenko * output was truncated, then the return value is the number of bytes
124114fc1afSAndy Shevchenko * (excluding the terminating NUL) which would have been written to the final
125114fc1afSAndy Shevchenko * string if enough space had been available.
12699eaf3c4SRandy Dunlap */
hex_dump_to_buffer(const void * buf,size_t len,int rowsize,int groupsize,char * linebuf,size_t linebuflen,bool ascii)127114fc1afSAndy Shevchenko int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
128114fc1afSAndy Shevchenko char *linebuf, size_t linebuflen, bool ascii)
12999eaf3c4SRandy Dunlap {
13099eaf3c4SRandy Dunlap const u8 *ptr = buf;
1315d909c8dSAndy Shevchenko int ngroups;
13299eaf3c4SRandy Dunlap u8 ch;
13399eaf3c4SRandy Dunlap int j, lx = 0;
134c7909234SRandy Dunlap int ascii_column;
135114fc1afSAndy Shevchenko int ret;
13699eaf3c4SRandy Dunlap
137c7909234SRandy Dunlap if (rowsize != 16 && rowsize != 32)
138c7909234SRandy Dunlap rowsize = 16;
139c7909234SRandy Dunlap
140c7909234SRandy Dunlap if (len > rowsize) /* limit to one line at a time */
141c7909234SRandy Dunlap len = rowsize;
1425d909c8dSAndy Shevchenko if (!is_power_of_2(groupsize) || groupsize > 8)
1435d909c8dSAndy Shevchenko groupsize = 1;
144c7909234SRandy Dunlap if ((len % groupsize) != 0) /* no mixed size output */
145c7909234SRandy Dunlap groupsize = 1;
146c7909234SRandy Dunlap
1475d909c8dSAndy Shevchenko ngroups = len / groupsize;
1485d909c8dSAndy Shevchenko ascii_column = rowsize * 2 + rowsize / groupsize + 1;
149114fc1afSAndy Shevchenko
150114fc1afSAndy Shevchenko if (!linebuflen)
151114fc1afSAndy Shevchenko goto overflow1;
152114fc1afSAndy Shevchenko
153114fc1afSAndy Shevchenko if (!len)
154114fc1afSAndy Shevchenko goto nil;
155114fc1afSAndy Shevchenko
1565d909c8dSAndy Shevchenko if (groupsize == 8) {
157c7909234SRandy Dunlap const u64 *ptr8 = buf;
158c7909234SRandy Dunlap
159114fc1afSAndy Shevchenko for (j = 0; j < ngroups; j++) {
160114fc1afSAndy Shevchenko ret = snprintf(linebuf + lx, linebuflen - lx,
161c67ae69bSLi Zefan "%s%16.16llx", j ? " " : "",
1620f70fe60SHoracio Mijail Anton Quiles get_unaligned(ptr8 + j));
163114fc1afSAndy Shevchenko if (ret >= linebuflen - lx)
164114fc1afSAndy Shevchenko goto overflow1;
165114fc1afSAndy Shevchenko lx += ret;
166114fc1afSAndy Shevchenko }
1675d909c8dSAndy Shevchenko } else if (groupsize == 4) {
168c7909234SRandy Dunlap const u32 *ptr4 = buf;
169c7909234SRandy Dunlap
170114fc1afSAndy Shevchenko for (j = 0; j < ngroups; j++) {
171114fc1afSAndy Shevchenko ret = snprintf(linebuf + lx, linebuflen - lx,
172114fc1afSAndy Shevchenko "%s%8.8x", j ? " " : "",
1730f70fe60SHoracio Mijail Anton Quiles get_unaligned(ptr4 + j));
174114fc1afSAndy Shevchenko if (ret >= linebuflen - lx)
175114fc1afSAndy Shevchenko goto overflow1;
176114fc1afSAndy Shevchenko lx += ret;
177114fc1afSAndy Shevchenko }
1785d909c8dSAndy Shevchenko } else if (groupsize == 2) {
179c7909234SRandy Dunlap const u16 *ptr2 = buf;
180c7909234SRandy Dunlap
181114fc1afSAndy Shevchenko for (j = 0; j < ngroups; j++) {
182114fc1afSAndy Shevchenko ret = snprintf(linebuf + lx, linebuflen - lx,
183114fc1afSAndy Shevchenko "%s%4.4x", j ? " " : "",
1840f70fe60SHoracio Mijail Anton Quiles get_unaligned(ptr2 + j));
185114fc1afSAndy Shevchenko if (ret >= linebuflen - lx)
186114fc1afSAndy Shevchenko goto overflow1;
187114fc1afSAndy Shevchenko lx += ret;
188114fc1afSAndy Shevchenko }
1895d909c8dSAndy Shevchenko } else {
190114fc1afSAndy Shevchenko for (j = 0; j < len; j++) {
1919f029f54SAndy Shevchenko if (linebuflen < lx + 2)
192114fc1afSAndy Shevchenko goto overflow2;
19399eaf3c4SRandy Dunlap ch = ptr[j];
1943fc95772SHarvey Harrison linebuf[lx++] = hex_asc_hi(ch);
1959f029f54SAndy Shevchenko if (linebuflen < lx + 2)
1969f029f54SAndy Shevchenko goto overflow2;
1973fc95772SHarvey Harrison linebuf[lx++] = hex_asc_lo(ch);
1989f029f54SAndy Shevchenko if (linebuflen < lx + 2)
1999f029f54SAndy Shevchenko goto overflow2;
20099eaf3c4SRandy Dunlap linebuf[lx++] = ' ';
20199eaf3c4SRandy Dunlap }
202c67ae69bSLi Zefan if (j)
203c67ae69bSLi Zefan lx--;
204c7909234SRandy Dunlap }
205c7909234SRandy Dunlap if (!ascii)
206c7909234SRandy Dunlap goto nil;
207c7909234SRandy Dunlap
208114fc1afSAndy Shevchenko while (lx < ascii_column) {
209114fc1afSAndy Shevchenko if (linebuflen < lx + 2)
210114fc1afSAndy Shevchenko goto overflow2;
211c7909234SRandy Dunlap linebuf[lx++] = ' ';
212114fc1afSAndy Shevchenko }
213114fc1afSAndy Shevchenko for (j = 0; j < len; j++) {
214114fc1afSAndy Shevchenko if (linebuflen < lx + 2)
215114fc1afSAndy Shevchenko goto overflow2;
216db0fd97cSJoe Perches ch = ptr[j];
217db0fd97cSJoe Perches linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
218db0fd97cSJoe Perches }
219c7909234SRandy Dunlap nil:
220114fc1afSAndy Shevchenko linebuf[lx] = '\0';
221114fc1afSAndy Shevchenko return lx;
222114fc1afSAndy Shevchenko overflow2:
22399eaf3c4SRandy Dunlap linebuf[lx++] = '\0';
224114fc1afSAndy Shevchenko overflow1:
225114fc1afSAndy Shevchenko return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
22699eaf3c4SRandy Dunlap }
22799eaf3c4SRandy Dunlap EXPORT_SYMBOL(hex_dump_to_buffer);
22899eaf3c4SRandy Dunlap
229ac83ed68SJoe Perches #ifdef CONFIG_PRINTK
23099eaf3c4SRandy Dunlap /**
23199eaf3c4SRandy Dunlap * print_hex_dump - print a text hex dump to syslog for a binary blob of data
23299eaf3c4SRandy Dunlap * @level: kernel log level (e.g. KERN_DEBUG)
233c7909234SRandy Dunlap * @prefix_str: string to prefix each line with;
234c7909234SRandy Dunlap * caller supplies trailing spaces for alignment if desired
23599eaf3c4SRandy Dunlap * @prefix_type: controls whether prefix of an offset, address, or none
23699eaf3c4SRandy Dunlap * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
237c7909234SRandy Dunlap * @rowsize: number of bytes to print per line; must be 16 or 32
238c7909234SRandy Dunlap * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
23999eaf3c4SRandy Dunlap * @buf: data blob to dump
24099eaf3c4SRandy Dunlap * @len: number of bytes in the @buf
241c7909234SRandy Dunlap * @ascii: include ASCII after the hex output
24299eaf3c4SRandy Dunlap *
24399eaf3c4SRandy Dunlap * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
24499eaf3c4SRandy Dunlap * to the kernel log at the specified kernel log level, with an optional
24599eaf3c4SRandy Dunlap * leading prefix.
24699eaf3c4SRandy Dunlap *
247c7909234SRandy Dunlap * print_hex_dump() works on one "line" of output at a time, i.e.,
248c7909234SRandy Dunlap * 16 or 32 bytes of input data converted to hex + ASCII output.
249c7909234SRandy Dunlap * print_hex_dump() iterates over the entire input @buf, breaking it into
250c7909234SRandy Dunlap * "line size" chunks to format and print.
25199eaf3c4SRandy Dunlap *
252c7909234SRandy Dunlap * E.g.:
253c7909234SRandy Dunlap * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
254db0fd97cSJoe Perches * 16, 1, frame->data, frame->len, true);
255c7909234SRandy Dunlap *
256c7909234SRandy Dunlap * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
25799eaf3c4SRandy Dunlap * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
258c7909234SRandy Dunlap * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
259c7909234SRandy Dunlap * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
26099eaf3c4SRandy Dunlap */
print_hex_dump(const char * level,const char * prefix_str,int prefix_type,int rowsize,int groupsize,const void * buf,size_t len,bool ascii)261c7909234SRandy Dunlap void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
262c7909234SRandy Dunlap int rowsize, int groupsize,
2636a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii)
26499eaf3c4SRandy Dunlap {
2656a0ed91eSArtem Bityutskiy const u8 *ptr = buf;
26699eaf3c4SRandy Dunlap int i, linelen, remaining = len;
267db0fd97cSJoe Perches unsigned char linebuf[32 * 3 + 2 + 32 + 1];
26899eaf3c4SRandy Dunlap
269c7909234SRandy Dunlap if (rowsize != 16 && rowsize != 32)
270c7909234SRandy Dunlap rowsize = 16;
271c7909234SRandy Dunlap
272c7909234SRandy Dunlap for (i = 0; i < len; i += rowsize) {
273c7909234SRandy Dunlap linelen = min(remaining, rowsize);
274c7909234SRandy Dunlap remaining -= rowsize;
275db0fd97cSJoe Perches
276c7909234SRandy Dunlap hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
277c7909234SRandy Dunlap linebuf, sizeof(linebuf), ascii);
27899eaf3c4SRandy Dunlap
27999eaf3c4SRandy Dunlap switch (prefix_type) {
28099eaf3c4SRandy Dunlap case DUMP_PREFIX_ADDRESS:
281db0fd97cSJoe Perches printk("%s%s%p: %s\n",
282db0fd97cSJoe Perches level, prefix_str, ptr + i, linebuf);
28399eaf3c4SRandy Dunlap break;
28499eaf3c4SRandy Dunlap case DUMP_PREFIX_OFFSET:
285c7909234SRandy Dunlap printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
28699eaf3c4SRandy Dunlap break;
28799eaf3c4SRandy Dunlap default:
288c7909234SRandy Dunlap printk("%s%s%s\n", level, prefix_str, linebuf);
28999eaf3c4SRandy Dunlap break;
29099eaf3c4SRandy Dunlap }
29199eaf3c4SRandy Dunlap }
29299eaf3c4SRandy Dunlap }
29399eaf3c4SRandy Dunlap EXPORT_SYMBOL(print_hex_dump);
294c7909234SRandy Dunlap
2957a555613SVladimir Kondratiev #endif /* defined(CONFIG_PRINTK) */
296