hexdump.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lib/hexdump.c
  4. */
  5. #include <linux/types.h>
  6. #include <linux/ctype.h>
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/minmax.h>
  10. #include <linux/export.h>
  11. #include <asm/unaligned.h>
  12. const char hex_asc[] = "0123456789abcdef";
  13. EXPORT_SYMBOL(hex_asc);
  14. const char hex_asc_upper[] = "0123456789ABCDEF";
  15. EXPORT_SYMBOL(hex_asc_upper);
  16. /**
  17. * hex_to_bin - convert a hex digit to its real value
  18. * @ch: ascii character represents hex digit
  19. *
  20. * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  21. * input.
  22. */
  23. int hex_to_bin(char ch)
  24. {
  25. if ((ch >= '0') && (ch <= '9'))
  26. return ch - '0';
  27. ch = tolower(ch);
  28. if ((ch >= 'a') && (ch <= 'f'))
  29. return ch - 'a' + 10;
  30. return -1;
  31. }
  32. EXPORT_SYMBOL(hex_to_bin);
  33. /**
  34. * hex2bin - convert an ascii hexadecimal string to its binary representation
  35. * @dst: binary result
  36. * @src: ascii hexadecimal string
  37. * @count: result length
  38. *
  39. * Return 0 on success, -EINVAL in case of bad input.
  40. */
  41. int hex2bin(u8 *dst, const char *src, size_t count)
  42. {
  43. while (count--) {
  44. int hi = hex_to_bin(*src++);
  45. int lo = hex_to_bin(*src++);
  46. if ((hi < 0) || (lo < 0))
  47. return -EINVAL;
  48. *dst++ = (hi << 4) | lo;
  49. }
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(hex2bin);
  53. /**
  54. * bin2hex - convert binary data to an ascii hexadecimal string
  55. * @dst: ascii hexadecimal result
  56. * @src: binary data
  57. * @count: binary data length
  58. */
  59. char *bin2hex(char *dst, const void *src, size_t count)
  60. {
  61. const unsigned char *_src = src;
  62. while (count--)
  63. dst = hex_byte_pack(dst, *_src++);
  64. return dst;
  65. }
  66. EXPORT_SYMBOL(bin2hex);
  67. /**
  68. * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  69. * @buf: data blob to dump
  70. * @len: number of bytes in the @buf
  71. * @rowsize: number of bytes to print per line; must be 16 or 32
  72. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  73. * @linebuf: where to put the converted data
  74. * @linebuflen: total size of @linebuf, including space for terminating NUL
  75. * @ascii: include ASCII after the hex output
  76. *
  77. * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  78. * 16 or 32 bytes of input data converted to hex + ASCII output.
  79. *
  80. * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  81. * to a hex + ASCII dump at the supplied memory location.
  82. * The converted output is always NUL-terminated.
  83. *
  84. * E.g.:
  85. * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  86. * linebuf, sizeof(linebuf), true);
  87. *
  88. * example output buffer:
  89. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  90. *
  91. * Return:
  92. * The amount of bytes placed in the buffer without terminating NUL. If the
  93. * output was truncated, then the return value is the number of bytes
  94. * (excluding the terminating NUL) which would have been written to the final
  95. * string if enough space had been available.
  96. */
  97. int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
  98. char *linebuf, size_t linebuflen, bool ascii)
  99. {
  100. const u8 *ptr = buf;
  101. int ngroups;
  102. u8 ch;
  103. int j, lx = 0;
  104. int ascii_column;
  105. int ret;
  106. if (rowsize != 16 && rowsize != 32)
  107. rowsize = 16;
  108. if (len > rowsize) /* limit to one line at a time */
  109. len = rowsize;
  110. if (!is_power_of_2(groupsize) || groupsize > 8)
  111. groupsize = 1;
  112. if ((len % groupsize) != 0) /* no mixed size output */
  113. groupsize = 1;
  114. ngroups = len / groupsize;
  115. ascii_column = rowsize * 2 + rowsize / groupsize + 1;
  116. if (!linebuflen)
  117. goto overflow1;
  118. if (!len)
  119. goto nil;
  120. if (groupsize == 8) {
  121. const u64 *ptr8 = buf;
  122. for (j = 0; j < ngroups; j++) {
  123. ret = snprintf(linebuf + lx, linebuflen - lx,
  124. "%s%16.16llx", j ? " " : "",
  125. get_unaligned(ptr8 + j));
  126. if (ret >= linebuflen - lx)
  127. goto overflow1;
  128. lx += ret;
  129. }
  130. } else if (groupsize == 4) {
  131. const u32 *ptr4 = buf;
  132. for (j = 0; j < ngroups; j++) {
  133. ret = snprintf(linebuf + lx, linebuflen - lx,
  134. "%s%8.8x", j ? " " : "",
  135. get_unaligned(ptr4 + j));
  136. if (ret >= linebuflen - lx)
  137. goto overflow1;
  138. lx += ret;
  139. }
  140. } else if (groupsize == 2) {
  141. const u16 *ptr2 = buf;
  142. for (j = 0; j < ngroups; j++) {
  143. ret = snprintf(linebuf + lx, linebuflen - lx,
  144. "%s%4.4x", j ? " " : "",
  145. get_unaligned(ptr2 + j));
  146. if (ret >= linebuflen - lx)
  147. goto overflow1;
  148. lx += ret;
  149. }
  150. } else {
  151. for (j = 0; j < len; j++) {
  152. if (linebuflen < lx + 2)
  153. goto overflow2;
  154. ch = ptr[j];
  155. linebuf[lx++] = hex_asc_hi(ch);
  156. if (linebuflen < lx + 2)
  157. goto overflow2;
  158. linebuf[lx++] = hex_asc_lo(ch);
  159. if (linebuflen < lx + 2)
  160. goto overflow2;
  161. linebuf[lx++] = ' ';
  162. }
  163. if (j)
  164. lx--;
  165. }
  166. if (!ascii)
  167. goto nil;
  168. while (lx < ascii_column) {
  169. if (linebuflen < lx + 2)
  170. goto overflow2;
  171. linebuf[lx++] = ' ';
  172. }
  173. for (j = 0; j < len; j++) {
  174. if (linebuflen < lx + 2)
  175. goto overflow2;
  176. ch = ptr[j];
  177. linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  178. }
  179. nil:
  180. linebuf[lx] = '\0';
  181. return lx;
  182. overflow2:
  183. linebuf[lx++] = '\0';
  184. overflow1:
  185. return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
  186. }
  187. EXPORT_SYMBOL(hex_dump_to_buffer);
  188. #ifdef CONFIG_PRINTK
  189. /**
  190. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  191. * @level: kernel log level (e.g. KERN_DEBUG)
  192. * @prefix_str: string to prefix each line with;
  193. * caller supplies trailing spaces for alignment if desired
  194. * @prefix_type: controls whether prefix of an offset, address, or none
  195. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  196. * @rowsize: number of bytes to print per line; must be 16 or 32
  197. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  198. * @buf: data blob to dump
  199. * @len: number of bytes in the @buf
  200. * @ascii: include ASCII after the hex output
  201. *
  202. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  203. * to the kernel log at the specified kernel log level, with an optional
  204. * leading prefix.
  205. *
  206. * print_hex_dump() works on one "line" of output at a time, i.e.,
  207. * 16 or 32 bytes of input data converted to hex + ASCII output.
  208. * print_hex_dump() iterates over the entire input @buf, breaking it into
  209. * "line size" chunks to format and print.
  210. *
  211. * E.g.:
  212. * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
  213. * 16, 1, frame->data, frame->len, true);
  214. *
  215. * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  216. * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  217. * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  218. * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
  219. */
  220. void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
  221. int rowsize, int groupsize,
  222. const void *buf, size_t len, bool ascii)
  223. {
  224. const u8 *ptr = buf;
  225. int i, linelen, remaining = len;
  226. unsigned char linebuf[32 * 3 + 2 + 32 + 1];
  227. if (rowsize != 16 && rowsize != 32)
  228. rowsize = 16;
  229. for (i = 0; i < len; i += rowsize) {
  230. linelen = min(remaining, rowsize);
  231. remaining -= rowsize;
  232. hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  233. linebuf, sizeof(linebuf), ascii);
  234. switch (prefix_type) {
  235. case DUMP_PREFIX_ADDRESS:
  236. printk("%s%s%p: %s\n",
  237. level, prefix_str, ptr + i, linebuf);
  238. break;
  239. case DUMP_PREFIX_OFFSET:
  240. printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
  241. break;
  242. default:
  243. printk("%s%s%s\n", level, prefix_str, linebuf);
  244. break;
  245. }
  246. }
  247. }
  248. EXPORT_SYMBOL(print_hex_dump);
  249. #endif /* defined(CONFIG_PRINTK) */