hexdump.c 8.2 KB

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