hexdump.c 6.1 KB

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