hexdump.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * lib/hexdump.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation. See README and COPYING for
  8. * more details.
  9. */
  10. #include <common.h>
  11. #include <hexdump.h>
  12. #include <linux/ctype.h>
  13. #include <linux/compat.h>
  14. #include <linux/log2.h>
  15. #include <asm/unaligned.h>
  16. const char hex_asc[] = "0123456789abcdef";
  17. const char hex_asc_upper[] = "0123456789ABCDEF";
  18. #ifdef CONFIG_HEXDUMP
  19. /**
  20. * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  21. * @buf: data blob to dump
  22. * @len: number of bytes in the @buf
  23. * @rowsize: number of bytes to print per line; must be 16 or 32
  24. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  25. * @linebuf: where to put the converted data
  26. * @linebuflen: total size of @linebuf, including space for terminating NUL
  27. * @ascii: include ASCII after the hex output
  28. *
  29. * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  30. * 16 or 32 bytes of input data converted to hex + ASCII output.
  31. *
  32. * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  33. * to a hex + ASCII dump at the supplied memory location.
  34. * The converted output is always NUL-terminated.
  35. *
  36. * E.g.:
  37. * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  38. * linebuf, sizeof(linebuf), true);
  39. *
  40. * example output buffer:
  41. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  42. *
  43. * Return:
  44. * The amount of bytes placed in the buffer without terminating NUL. If the
  45. * output was truncated, then the return value is the number of bytes
  46. * (excluding the terminating NUL) which would have been written to the final
  47. * string if enough space had been available.
  48. */
  49. int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
  50. char *linebuf, size_t linebuflen, bool ascii)
  51. {
  52. const u8 *ptr = buf;
  53. int ngroups;
  54. u8 ch;
  55. int j, lx = 0;
  56. int ascii_column;
  57. int ret;
  58. if (rowsize != 16 && rowsize != 32)
  59. rowsize = 16;
  60. if (len > rowsize) /* limit to one line at a time */
  61. len = rowsize;
  62. if (!is_power_of_2(groupsize) || groupsize > 8)
  63. groupsize = 1;
  64. if ((len % groupsize) != 0) /* no mixed size output */
  65. groupsize = 1;
  66. ngroups = len / groupsize;
  67. ascii_column = rowsize * 2 + rowsize / groupsize + 1;
  68. if (!linebuflen)
  69. goto overflow1;
  70. if (!len)
  71. goto nil;
  72. if (groupsize == 8) {
  73. const u64 *ptr8 = buf;
  74. for (j = 0; j < ngroups; j++) {
  75. ret = snprintf(linebuf + lx, linebuflen - lx,
  76. "%s%16.16llx", j ? " " : "",
  77. get_unaligned(ptr8 + j));
  78. if (ret >= linebuflen - lx)
  79. goto overflow1;
  80. lx += ret;
  81. }
  82. } else if (groupsize == 4) {
  83. const u32 *ptr4 = buf;
  84. for (j = 0; j < ngroups; j++) {
  85. ret = snprintf(linebuf + lx, linebuflen - lx,
  86. "%s%8.8x", j ? " " : "",
  87. get_unaligned(ptr4 + j));
  88. if (ret >= linebuflen - lx)
  89. goto overflow1;
  90. lx += ret;
  91. }
  92. } else if (groupsize == 2) {
  93. const u16 *ptr2 = buf;
  94. for (j = 0; j < ngroups; j++) {
  95. ret = snprintf(linebuf + lx, linebuflen - lx,
  96. "%s%4.4x", j ? " " : "",
  97. get_unaligned(ptr2 + j));
  98. if (ret >= linebuflen - lx)
  99. goto overflow1;
  100. lx += ret;
  101. }
  102. } else {
  103. for (j = 0; j < len; j++) {
  104. if (linebuflen < lx + 2)
  105. goto overflow2;
  106. ch = ptr[j];
  107. linebuf[lx++] = hex_asc_hi(ch);
  108. if (linebuflen < lx + 2)
  109. goto overflow2;
  110. linebuf[lx++] = hex_asc_lo(ch);
  111. if (linebuflen < lx + 2)
  112. goto overflow2;
  113. linebuf[lx++] = ' ';
  114. }
  115. if (j)
  116. lx--;
  117. }
  118. if (!ascii)
  119. goto nil;
  120. while (lx < ascii_column) {
  121. if (linebuflen < lx + 2)
  122. goto overflow2;
  123. linebuf[lx++] = ' ';
  124. }
  125. for (j = 0; j < len; j++) {
  126. if (linebuflen < lx + 2)
  127. goto overflow2;
  128. ch = ptr[j];
  129. linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  130. }
  131. nil:
  132. linebuf[lx] = '\0';
  133. return lx;
  134. overflow2:
  135. linebuf[lx++] = '\0';
  136. overflow1:
  137. return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
  138. }
  139. /**
  140. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  141. * @prefix_str: string to prefix each line with;
  142. * caller supplies trailing spaces for alignment if desired
  143. * @prefix_type: controls whether prefix of an offset, address, or none
  144. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  145. * @rowsize: number of bytes to print per line; must be 16 or 32
  146. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  147. * @buf: data blob to dump
  148. * @len: number of bytes in the @buf
  149. * @ascii: include ASCII after the hex output
  150. *
  151. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  152. * to the stdio, with an optional leading prefix.
  153. *
  154. * print_hex_dump() works on one "line" of output at a time, i.e.,
  155. * 16 or 32 bytes of input data converted to hex + ASCII output.
  156. * print_hex_dump() iterates over the entire input @buf, breaking it into
  157. * "line size" chunks to format and print.
  158. *
  159. * E.g.:
  160. * print_hex_dump("raw data: ", DUMP_PREFIX_ADDRESS, 16, 1, frame->data,
  161. * frame->len, true);
  162. *
  163. * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  164. * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  165. * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  166. * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
  167. */
  168. void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
  169. int groupsize, const void *buf, size_t len, bool ascii)
  170. {
  171. const u8 *ptr = buf;
  172. int i, linelen, remaining = len;
  173. char linebuf[32 * 3 + 2 + 32 + 1];
  174. if (rowsize != 16 && rowsize != 32)
  175. rowsize = 16;
  176. for (i = 0; i < len; i += rowsize) {
  177. linelen = min(remaining, rowsize);
  178. remaining -= rowsize;
  179. hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  180. linebuf, sizeof(linebuf), ascii);
  181. switch (prefix_type) {
  182. case DUMP_PREFIX_ADDRESS:
  183. printf("%s%p: %s\n", prefix_str, ptr + i, linebuf);
  184. break;
  185. case DUMP_PREFIX_OFFSET:
  186. printf("%s%.8x: %s\n", prefix_str, i, linebuf);
  187. break;
  188. default:
  189. printf("%s%s\n", prefix_str, linebuf);
  190. break;
  191. }
  192. }
  193. }
  194. /**
  195. * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
  196. * @prefix_str: string to prefix each line with;
  197. * caller supplies trailing spaces for alignment if desired
  198. * @prefix_type: controls whether prefix of an offset, address, or none
  199. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  200. * @buf: data blob to dump
  201. * @len: number of bytes in the @buf
  202. *
  203. * Calls print_hex_dump(), rowsize of 16, groupsize of 1,
  204. * and ASCII output included.
  205. */
  206. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  207. const void *buf, size_t len)
  208. {
  209. print_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true);
  210. }
  211. #else
  212. /*
  213. * Some code in U-Boot copy-pasted from Linux kernel uses both
  214. * functions below so to keep stuff compilable we keep these stubs here.
  215. */
  216. void print_hex_dump(const char *prefix_str, int prefix_type,
  217. int rowsize, int groupsize, const void *buf,
  218. size_t len, bool ascii)
  219. {
  220. }
  221. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  222. const void *buf, size_t len)
  223. {
  224. }
  225. #endif /* CONFIG_HEXDUMP */