hexdump.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/module.h>
  13. /**
  14. * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  15. * @buf: data blob to dump
  16. * @len: number of bytes in the @buf
  17. * @linebuf: where to put the converted data
  18. * @linebuflen: total size of @linebuf, including space for terminating NUL
  19. *
  20. * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  21. * 16 bytes of input data converted to hex + ASCII output.
  22. *
  23. * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  24. * to a hex + ASCII dump at the supplied memory location.
  25. * The converted output is always NUL-terminated.
  26. *
  27. * E.g.:
  28. * hex_dump_to_buffer(frame->data, frame->len, linebuf, sizeof(linebuf));
  29. *
  30. * example output buffer:
  31. * 40414243 44454647 48494a4b 4c4d4e4f @ABCDEFGHIJKLMNO
  32. */
  33. void hex_dump_to_buffer(const void *buf, size_t len, char *linebuf,
  34. size_t linebuflen)
  35. {
  36. const u8 *ptr = buf;
  37. u8 ch;
  38. int j, lx = 0;
  39. for (j = 0; (j < 16) && (j < len) && (lx + 3) < linebuflen; j++) {
  40. if (j && !(j % 4))
  41. linebuf[lx++] = ' ';
  42. ch = ptr[j];
  43. linebuf[lx++] = hex_asc(ch >> 4);
  44. linebuf[lx++] = hex_asc(ch & 0x0f);
  45. }
  46. if ((lx + 2) < linebuflen) {
  47. linebuf[lx++] = ' ';
  48. linebuf[lx++] = ' ';
  49. }
  50. for (j = 0; (j < 16) && (j < len) && (lx + 2) < linebuflen; j++)
  51. linebuf[lx++] = isprint(ptr[j]) ? ptr[j] : '.';
  52. linebuf[lx++] = '\0';
  53. }
  54. EXPORT_SYMBOL(hex_dump_to_buffer);
  55. /**
  56. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  57. * @level: kernel log level (e.g. KERN_DEBUG)
  58. * @prefix_type: controls whether prefix of an offset, address, or none
  59. * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  60. * @buf: data blob to dump
  61. * @len: number of bytes in the @buf
  62. *
  63. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  64. * to the kernel log at the specified kernel log level, with an optional
  65. * leading prefix.
  66. *
  67. * E.g.:
  68. * print_hex_dump(KERN_DEBUG, DUMP_PREFIX_ADDRESS, frame->data, frame->len);
  69. *
  70. * Example output using %DUMP_PREFIX_OFFSET:
  71. * 0009ab42: 40414243 44454647 48494a4b 4c4d4e4f @ABCDEFGHIJKLMNO
  72. * Example output using %DUMP_PREFIX_ADDRESS:
  73. * ffffffff88089af0: 70717273 74757677 78797a7b 7c7d7e7f pqrstuvwxyz{|}~.
  74. */
  75. void print_hex_dump(const char *level, int prefix_type, void *buf, size_t len)
  76. {
  77. u8 *ptr = buf;
  78. int i, linelen, remaining = len;
  79. unsigned char linebuf[100];
  80. for (i = 0; i < len; i += 16) {
  81. linelen = min(remaining, 16);
  82. remaining -= 16;
  83. hex_dump_to_buffer(ptr + i, linelen, linebuf, sizeof(linebuf));
  84. switch (prefix_type) {
  85. case DUMP_PREFIX_ADDRESS:
  86. printk("%s%*p: %s\n", level,
  87. (int)(2 * sizeof(void *)), ptr + i, linebuf);
  88. break;
  89. case DUMP_PREFIX_OFFSET:
  90. printk("%s%.8x: %s\n", level, i, linebuf);
  91. break;
  92. default:
  93. printk("%s%s\n", level, linebuf);
  94. break;
  95. }
  96. }
  97. }
  98. EXPORT_SYMBOL(print_hex_dump);