hexdump.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
  4. *
  5. */
  6. #ifndef HEXDUMP_H
  7. #define HEXDUMP_H
  8. #include <linux/ctype.h>
  9. #include <linux/types.h>
  10. enum dump_prefix_t {
  11. DUMP_PREFIX_NONE,
  12. DUMP_PREFIX_ADDRESS,
  13. DUMP_PREFIX_OFFSET
  14. };
  15. extern const char hex_asc[];
  16. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  17. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  18. static inline char *hex_byte_pack(char *buf, u8 byte)
  19. {
  20. *buf++ = hex_asc_hi(byte);
  21. *buf++ = hex_asc_lo(byte);
  22. return buf;
  23. }
  24. /**
  25. * hex_to_bin - convert a hex digit to its real value
  26. * @ch: ascii character represents hex digit
  27. *
  28. * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  29. * input.
  30. */
  31. static inline int hex_to_bin(char ch)
  32. {
  33. if ((ch >= '0') && (ch <= '9'))
  34. return ch - '0';
  35. ch = tolower(ch);
  36. if ((ch >= 'a') && (ch <= 'f'))
  37. return ch - 'a' + 10;
  38. return -1;
  39. }
  40. /**
  41. * hex2bin - convert an ascii hexadecimal string to its binary representation
  42. * @dst: binary result
  43. * @src: ascii hexadecimal string
  44. * @count: result length
  45. *
  46. * Return 0 on success, -1 in case of bad input.
  47. */
  48. static inline int hex2bin(u8 *dst, const char *src, size_t count)
  49. {
  50. while (count--) {
  51. int hi = hex_to_bin(*src++);
  52. int lo = hex_to_bin(*src++);
  53. if ((hi < 0) || (lo < 0))
  54. return -1;
  55. *dst++ = (hi << 4) | lo;
  56. }
  57. return 0;
  58. }
  59. /**
  60. * bin2hex - convert binary data to an ascii hexadecimal string
  61. * @dst: ascii hexadecimal result
  62. * @src: binary data
  63. * @count: binary data length
  64. */
  65. static inline char *bin2hex(char *dst, const void *src, size_t count)
  66. {
  67. const unsigned char *_src = src;
  68. while (count--)
  69. dst = hex_byte_pack(dst, *_src++);
  70. return dst;
  71. }
  72. /**
  73. * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  74. * @buf: data blob to dump
  75. * @len: number of bytes in the @buf
  76. * @rowsize: number of bytes to print per line; max 64
  77. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  78. * @linebuf: where to put the converted data
  79. * @linebuflen: total size of @linebuf, including space for terminating NUL
  80. * @ascii: include ASCII after the hex output
  81. *
  82. * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  83. * 16 or 32 bytes of input data converted to hex + ASCII output.
  84. *
  85. * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  86. * to a hex + ASCII dump at the supplied memory location.
  87. * The converted output is always NUL-terminated.
  88. *
  89. * E.g.:
  90. * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  91. * linebuf, sizeof(linebuf), true);
  92. *
  93. * example output buffer:
  94. * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  95. *
  96. * Return:
  97. * The amount of bytes placed in the buffer without terminating NUL. If the
  98. * output was truncated, then the return value is the number of bytes
  99. * (excluding the terminating NUL) which would have been written to the final
  100. * string if enough space had been available.
  101. */
  102. int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
  103. char *linebuf, size_t linebuflen, bool ascii);
  104. /**
  105. * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  106. * @prefix_str: string to prefix each line with;
  107. * caller supplies trailing spaces for alignment if desired
  108. * @prefix_type: controls whether prefix of an offset, address, or none
  109. * is printed (see enum dump_prefix_t)
  110. * @rowsize: number of bytes to print per line; max 64
  111. * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  112. * @buf: data blob to dump
  113. * @len: number of bytes in the @buf
  114. * @ascii: include ASCII after the hex output
  115. * Returns: 0 if finished normally, -EINTR if Ctrl-C was pressed, -ENOSYS if not
  116. * supported
  117. *
  118. * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  119. * to the stdio, with an optional leading prefix.
  120. *
  121. * print_hex_dump() works on one "line" of output at a time, i.e.,
  122. * 16 or 32 bytes of input data converted to hex + ASCII output.
  123. * print_hex_dump() iterates over the entire input @buf, breaking it into
  124. * "line size" chunks to format and print.
  125. *
  126. * E.g.:
  127. * print_hex_dump("raw data: ", DUMP_PREFIX_ADDRESS, 16, 1, frame->data,
  128. * frame->len, true);
  129. *
  130. * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  131. * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
  132. * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  133. * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
  134. */
  135. int print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
  136. int groupsize, const void *buf, size_t len, bool ascii);
  137. /**
  138. * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
  139. * @prefix_str: string to prefix each line with;
  140. * caller supplies trailing spaces for alignment if desired
  141. * @prefix_type: controls whether prefix of an offset, address, or none
  142. * is printed (see enum dump_prefix_t)
  143. * @buf: data blob to dump
  144. * @len: number of bytes in the @buf
  145. *
  146. * Calls print_hex_dump(), rowsize of 16, groupsize of 1,
  147. * and ASCII output included.
  148. */
  149. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  150. const void *buf, size_t len);
  151. #endif /* HEXDUMP_H */