hexdump.c 8.5 KB

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