hexdump.c 8.3 KB

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