hexdump.c 7.0 KB

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