hexdump.c 6.1 KB

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