hexdump.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 + 3)
  155. goto overflow2;
  156. ch = ptr[j];
  157. linebuf[lx++] = hex_asc_hi(ch);
  158. linebuf[lx++] = hex_asc_lo(ch);
  159. linebuf[lx++] = ' ';
  160. }
  161. if (j)
  162. lx--;
  163. }
  164. if (!ascii)
  165. goto nil;
  166. while (lx < ascii_column) {
  167. if (linebuflen < lx + 2)
  168. goto overflow2;
  169. linebuf[lx++] = ' ';
  170. }
  171. for (j = 0; j < len; j++) {
  172. if (linebuflen < lx + 2)
  173. goto overflow2;
  174. ch = ptr[j];
  175. linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  176. }
  177. nil:
  178. linebuf[lx] = '\0';
  179. return lx;
  180. overflow2:
  181. linebuf[lx++] = '\0';
  182. overflow1:
  183. return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
  184. }
  185. EXPORT_SYMBOL(hex_dump_to_buffer);
  186. #ifdef CONFIG_PRINTK
  187. /**
  188. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  189. * @level: kernel log level (e.g. KERN_DEBUG)
  190. * @prefix_str: string to prefix each line with;
  191. * caller supplies trailing spaces for alignment if desired
  192. * @prefix_type: controls whether prefix of an offset, address, or none
  193. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  194. * @rowsize: number of bytes to print per line; must be 16 or 32
  195. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  196. * @buf: data blob to dump
  197. * @len: number of bytes in the @buf
  198. * @ascii: include ASCII after the hex output
  199. *
  200. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  201. * to the kernel log at the specified kernel log level, with an optional
  202. * leading prefix.
  203. *
  204. * print_hex_dump() works on one "line" of output at a time, i.e.,
  205. * 16 or 32 bytes of input data converted to hex + ASCII output.
  206. * print_hex_dump() iterates over the entire input @buf, breaking it into
  207. * "line size" chunks to format and print.
  208. *
  209. * E.g.:
  210. * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
  211. * 16, 1, frame->data, frame->len, true);
  212. *
  213. * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  214. * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  215. * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  216. * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
  217. */
  218. void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
  219. int rowsize, int groupsize,
  220. const void *buf, size_t len, bool ascii)
  221. {
  222. const u8 *ptr = buf;
  223. int i, linelen, remaining = len;
  224. unsigned char linebuf[32 * 3 + 2 + 32 + 1];
  225. if (rowsize != 16 && rowsize != 32)
  226. rowsize = 16;
  227. for (i = 0; i < len; i += rowsize) {
  228. linelen = min(remaining, rowsize);
  229. remaining -= rowsize;
  230. hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  231. linebuf, sizeof(linebuf), ascii);
  232. switch (prefix_type) {
  233. case DUMP_PREFIX_ADDRESS:
  234. printk("%s%s%p: %s\n",
  235. level, prefix_str, ptr + i, linebuf);
  236. break;
  237. case DUMP_PREFIX_OFFSET:
  238. printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
  239. break;
  240. default:
  241. printk("%s%s%s\n", level, prefix_str, linebuf);
  242. break;
  243. }
  244. }
  245. }
  246. EXPORT_SYMBOL(print_hex_dump);
  247. #if !defined(CONFIG_DYNAMIC_DEBUG)
  248. /**
  249. * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
  250. * @prefix_str: string to prefix each line with;
  251. * caller supplies trailing spaces for alignment if desired
  252. * @prefix_type: controls whether prefix of an offset, address, or none
  253. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  254. * @buf: data blob to dump
  255. * @len: number of bytes in the @buf
  256. *
  257. * Calls print_hex_dump(), with log level of KERN_DEBUG,
  258. * rowsize of 16, groupsize of 1, and ASCII output included.
  259. */
  260. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  261. const void *buf, size_t len)
  262. {
  263. print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
  264. buf, len, true);
  265. }
  266. EXPORT_SYMBOL(print_hex_dump_bytes);
  267. #endif /* !defined(CONFIG_DYNAMIC_DEBUG) */
  268. #endif /* defined(CONFIG_PRINTK) */