hexdump.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * lib/hexdump.c
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation. See README and COPYING for
  7. * more details.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/ctype.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. const char hex_asc[] = "0123456789abcdef";
  14. EXPORT_SYMBOL(hex_asc);
  15. /**
  16. * hex_to_bin - convert a hex digit to its real value
  17. * @ch: ascii character represents hex digit
  18. *
  19. * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  20. * input.
  21. */
  22. int hex_to_bin(char ch)
  23. {
  24. if ((ch >= '0') && (ch <= '9'))
  25. return ch - '0';
  26. ch = tolower(ch);
  27. if ((ch >= 'a') && (ch <= 'f'))
  28. return ch - 'a' + 10;
  29. return -1;
  30. }
  31. EXPORT_SYMBOL(hex_to_bin);
  32. /**
  33. * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  34. * @buf: data blob to dump
  35. * @len: number of bytes in the @buf
  36. * @rowsize: number of bytes to print per line; must be 16 or 32
  37. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  38. * @linebuf: where to put the converted data
  39. * @linebuflen: total size of @linebuf, including space for terminating NUL
  40. * @ascii: include ASCII after the hex output
  41. *
  42. * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  43. * 16 or 32 bytes of input data converted to hex + ASCII output.
  44. *
  45. * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  46. * to a hex + ASCII dump at the supplied memory location.
  47. * The converted output is always NUL-terminated.
  48. *
  49. * E.g.:
  50. * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  51. * linebuf, sizeof(linebuf), true);
  52. *
  53. * example output buffer:
  54. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  55. */
  56. void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
  57. int groupsize, char *linebuf, size_t linebuflen,
  58. bool ascii)
  59. {
  60. const u8 *ptr = buf;
  61. u8 ch;
  62. int j, lx = 0;
  63. int ascii_column;
  64. if (rowsize != 16 && rowsize != 32)
  65. rowsize = 16;
  66. if (!len)
  67. goto nil;
  68. if (len > rowsize) /* limit to one line at a time */
  69. len = rowsize;
  70. if ((len % groupsize) != 0) /* no mixed size output */
  71. groupsize = 1;
  72. switch (groupsize) {
  73. case 8: {
  74. const u64 *ptr8 = buf;
  75. int ngroups = len / groupsize;
  76. for (j = 0; j < ngroups; j++)
  77. lx += scnprintf(linebuf + lx, linebuflen - lx,
  78. "%s%16.16llx", j ? " " : "",
  79. (unsigned long long)*(ptr8 + j));
  80. ascii_column = 17 * ngroups + 2;
  81. break;
  82. }
  83. case 4: {
  84. const u32 *ptr4 = buf;
  85. int ngroups = len / groupsize;
  86. for (j = 0; j < ngroups; j++)
  87. lx += scnprintf(linebuf + lx, linebuflen - lx,
  88. "%s%8.8x", j ? " " : "", *(ptr4 + j));
  89. ascii_column = 9 * ngroups + 2;
  90. break;
  91. }
  92. case 2: {
  93. const u16 *ptr2 = buf;
  94. int ngroups = len / groupsize;
  95. for (j = 0; j < ngroups; j++)
  96. lx += scnprintf(linebuf + lx, linebuflen - lx,
  97. "%s%4.4x", j ? " " : "", *(ptr2 + j));
  98. ascii_column = 5 * ngroups + 2;
  99. break;
  100. }
  101. default:
  102. for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
  103. ch = ptr[j];
  104. linebuf[lx++] = hex_asc_hi(ch);
  105. linebuf[lx++] = hex_asc_lo(ch);
  106. linebuf[lx++] = ' ';
  107. }
  108. if (j)
  109. lx--;
  110. ascii_column = 3 * rowsize + 2;
  111. break;
  112. }
  113. if (!ascii)
  114. goto nil;
  115. while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
  116. linebuf[lx++] = ' ';
  117. for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
  118. ch = ptr[j];
  119. linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  120. }
  121. nil:
  122. linebuf[lx++] = '\0';
  123. }
  124. EXPORT_SYMBOL(hex_dump_to_buffer);
  125. /**
  126. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  127. * @level: kernel log level (e.g. KERN_DEBUG)
  128. * @prefix_str: string to prefix each line with;
  129. * caller supplies trailing spaces for alignment if desired
  130. * @prefix_type: controls whether prefix of an offset, address, or none
  131. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  132. * @rowsize: number of bytes to print per line; must be 16 or 32
  133. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  134. * @buf: data blob to dump
  135. * @len: number of bytes in the @buf
  136. * @ascii: include ASCII after the hex output
  137. *
  138. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  139. * to the kernel log at the specified kernel log level, with an optional
  140. * leading prefix.
  141. *
  142. * print_hex_dump() works on one "line" of output at a time, i.e.,
  143. * 16 or 32 bytes of input data converted to hex + ASCII output.
  144. * print_hex_dump() iterates over the entire input @buf, breaking it into
  145. * "line size" chunks to format and print.
  146. *
  147. * E.g.:
  148. * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
  149. * 16, 1, frame->data, frame->len, true);
  150. *
  151. * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  152. * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  153. * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  154. * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
  155. */
  156. void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
  157. int rowsize, int groupsize,
  158. const void *buf, size_t len, bool ascii)
  159. {
  160. const u8 *ptr = buf;
  161. int i, linelen, remaining = len;
  162. unsigned char linebuf[32 * 3 + 2 + 32 + 1];
  163. if (rowsize != 16 && rowsize != 32)
  164. rowsize = 16;
  165. for (i = 0; i < len; i += rowsize) {
  166. linelen = min(remaining, rowsize);
  167. remaining -= rowsize;
  168. hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  169. linebuf, sizeof(linebuf), ascii);
  170. switch (prefix_type) {
  171. case DUMP_PREFIX_ADDRESS:
  172. printk("%s%s%p: %s\n",
  173. level, prefix_str, ptr + i, linebuf);
  174. break;
  175. case DUMP_PREFIX_OFFSET:
  176. printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
  177. break;
  178. default:
  179. printk("%s%s%s\n", level, prefix_str, linebuf);
  180. break;
  181. }
  182. }
  183. }
  184. EXPORT_SYMBOL(print_hex_dump);
  185. /**
  186. * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
  187. * @prefix_str: string to prefix each line with;
  188. * caller supplies trailing spaces for alignment if desired
  189. * @prefix_type: controls whether prefix of an offset, address, or none
  190. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  191. * @buf: data blob to dump
  192. * @len: number of bytes in the @buf
  193. *
  194. * Calls print_hex_dump(), with log level of KERN_DEBUG,
  195. * rowsize of 16, groupsize of 1, and ASCII output included.
  196. */
  197. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  198. const void *buf, size_t len)
  199. {
  200. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
  201. buf, len, true);
  202. }
  203. EXPORT_SYMBOL(print_hex_dump_bytes);