hexdump.c 7.2 KB

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