hexdump.c 8.3 KB

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