hexdump.c 8.5 KB

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