hexdump.c 6.9 KB

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