Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * lib/hexdump.c
0004  */
0005 
0006 #include <linux/types.h>
0007 #include <linux/ctype.h>
0008 #include <linux/errno.h>
0009 #include <linux/kernel.h>
0010 #include <linux/minmax.h>
0011 #include <linux/export.h>
0012 #include <asm/unaligned.h>
0013 
0014 const char hex_asc[] = "0123456789abcdef";
0015 EXPORT_SYMBOL(hex_asc);
0016 const char hex_asc_upper[] = "0123456789ABCDEF";
0017 EXPORT_SYMBOL(hex_asc_upper);
0018 
0019 /**
0020  * hex_to_bin - convert a hex digit to its real value
0021  * @ch: ascii character represents hex digit
0022  *
0023  * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
0024  * input.
0025  *
0026  * This function is used to load cryptographic keys, so it is coded in such a
0027  * way that there are no conditions or memory accesses that depend on data.
0028  *
0029  * Explanation of the logic:
0030  * (ch - '9' - 1) is negative if ch <= '9'
0031  * ('0' - 1 - ch) is negative if ch >= '0'
0032  * we "and" these two values, so the result is negative if ch is in the range
0033  *  '0' ... '9'
0034  * we are only interested in the sign, so we do a shift ">> 8"; note that right
0035  *  shift of a negative value is implementation-defined, so we cast the
0036  *  value to (unsigned) before the shift --- we have 0xffffff if ch is in
0037  *  the range '0' ... '9', 0 otherwise
0038  * we "and" this value with (ch - '0' + 1) --- we have a value 1 ... 10 if ch is
0039  *  in the range '0' ... '9', 0 otherwise
0040  * we add this value to -1 --- we have a value 0 ... 9 if ch is in the range '0'
0041  *  ... '9', -1 otherwise
0042  * the next line is similar to the previous one, but we need to decode both
0043  *  uppercase and lowercase letters, so we use (ch & 0xdf), which converts
0044  *  lowercase to uppercase
0045  */
0046 int hex_to_bin(unsigned char ch)
0047 {
0048     unsigned char cu = ch & 0xdf;
0049     return -1 +
0050         ((ch - '0' +  1) & (unsigned)((ch - '9' - 1) & ('0' - 1 - ch)) >> 8) +
0051         ((cu - 'A' + 11) & (unsigned)((cu - 'F' - 1) & ('A' - 1 - cu)) >> 8);
0052 }
0053 EXPORT_SYMBOL(hex_to_bin);
0054 
0055 /**
0056  * hex2bin - convert an ascii hexadecimal string to its binary representation
0057  * @dst: binary result
0058  * @src: ascii hexadecimal string
0059  * @count: result length
0060  *
0061  * Return 0 on success, -EINVAL in case of bad input.
0062  */
0063 int hex2bin(u8 *dst, const char *src, size_t count)
0064 {
0065     while (count--) {
0066         int hi, lo;
0067 
0068         hi = hex_to_bin(*src++);
0069         if (unlikely(hi < 0))
0070             return -EINVAL;
0071         lo = hex_to_bin(*src++);
0072         if (unlikely(lo < 0))
0073             return -EINVAL;
0074 
0075         *dst++ = (hi << 4) | lo;
0076     }
0077     return 0;
0078 }
0079 EXPORT_SYMBOL(hex2bin);
0080 
0081 /**
0082  * bin2hex - convert binary data to an ascii hexadecimal string
0083  * @dst: ascii hexadecimal result
0084  * @src: binary data
0085  * @count: binary data length
0086  */
0087 char *bin2hex(char *dst, const void *src, size_t count)
0088 {
0089     const unsigned char *_src = src;
0090 
0091     while (count--)
0092         dst = hex_byte_pack(dst, *_src++);
0093     return dst;
0094 }
0095 EXPORT_SYMBOL(bin2hex);
0096 
0097 /**
0098  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
0099  * @buf: data blob to dump
0100  * @len: number of bytes in the @buf
0101  * @rowsize: number of bytes to print per line; must be 16 or 32
0102  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
0103  * @linebuf: where to put the converted data
0104  * @linebuflen: total size of @linebuf, including space for terminating NUL
0105  * @ascii: include ASCII after the hex output
0106  *
0107  * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
0108  * 16 or 32 bytes of input data converted to hex + ASCII output.
0109  *
0110  * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
0111  * to a hex + ASCII dump at the supplied memory location.
0112  * The converted output is always NUL-terminated.
0113  *
0114  * E.g.:
0115  *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
0116  *          linebuf, sizeof(linebuf), true);
0117  *
0118  * example output buffer:
0119  * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
0120  *
0121  * Return:
0122  * The amount of bytes placed in the buffer without terminating NUL. If the
0123  * output was truncated, then the return value is the number of bytes
0124  * (excluding the terminating NUL) which would have been written to the final
0125  * string if enough space had been available.
0126  */
0127 int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
0128                char *linebuf, size_t linebuflen, bool ascii)
0129 {
0130     const u8 *ptr = buf;
0131     int ngroups;
0132     u8 ch;
0133     int j, lx = 0;
0134     int ascii_column;
0135     int ret;
0136 
0137     if (rowsize != 16 && rowsize != 32)
0138         rowsize = 16;
0139 
0140     if (len > rowsize)      /* limit to one line at a time */
0141         len = rowsize;
0142     if (!is_power_of_2(groupsize) || groupsize > 8)
0143         groupsize = 1;
0144     if ((len % groupsize) != 0) /* no mixed size output */
0145         groupsize = 1;
0146 
0147     ngroups = len / groupsize;
0148     ascii_column = rowsize * 2 + rowsize / groupsize + 1;
0149 
0150     if (!linebuflen)
0151         goto overflow1;
0152 
0153     if (!len)
0154         goto nil;
0155 
0156     if (groupsize == 8) {
0157         const u64 *ptr8 = buf;
0158 
0159         for (j = 0; j < ngroups; j++) {
0160             ret = snprintf(linebuf + lx, linebuflen - lx,
0161                        "%s%16.16llx", j ? " " : "",
0162                        get_unaligned(ptr8 + j));
0163             if (ret >= linebuflen - lx)
0164                 goto overflow1;
0165             lx += ret;
0166         }
0167     } else if (groupsize == 4) {
0168         const u32 *ptr4 = buf;
0169 
0170         for (j = 0; j < ngroups; j++) {
0171             ret = snprintf(linebuf + lx, linebuflen - lx,
0172                        "%s%8.8x", j ? " " : "",
0173                        get_unaligned(ptr4 + j));
0174             if (ret >= linebuflen - lx)
0175                 goto overflow1;
0176             lx += ret;
0177         }
0178     } else if (groupsize == 2) {
0179         const u16 *ptr2 = buf;
0180 
0181         for (j = 0; j < ngroups; j++) {
0182             ret = snprintf(linebuf + lx, linebuflen - lx,
0183                        "%s%4.4x", j ? " " : "",
0184                        get_unaligned(ptr2 + j));
0185             if (ret >= linebuflen - lx)
0186                 goto overflow1;
0187             lx += ret;
0188         }
0189     } else {
0190         for (j = 0; j < len; j++) {
0191             if (linebuflen < lx + 2)
0192                 goto overflow2;
0193             ch = ptr[j];
0194             linebuf[lx++] = hex_asc_hi(ch);
0195             if (linebuflen < lx + 2)
0196                 goto overflow2;
0197             linebuf[lx++] = hex_asc_lo(ch);
0198             if (linebuflen < lx + 2)
0199                 goto overflow2;
0200             linebuf[lx++] = ' ';
0201         }
0202         if (j)
0203             lx--;
0204     }
0205     if (!ascii)
0206         goto nil;
0207 
0208     while (lx < ascii_column) {
0209         if (linebuflen < lx + 2)
0210             goto overflow2;
0211         linebuf[lx++] = ' ';
0212     }
0213     for (j = 0; j < len; j++) {
0214         if (linebuflen < lx + 2)
0215             goto overflow2;
0216         ch = ptr[j];
0217         linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
0218     }
0219 nil:
0220     linebuf[lx] = '\0';
0221     return lx;
0222 overflow2:
0223     linebuf[lx++] = '\0';
0224 overflow1:
0225     return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
0226 }
0227 EXPORT_SYMBOL(hex_dump_to_buffer);
0228 
0229 #ifdef CONFIG_PRINTK
0230 /**
0231  * print_hex_dump - print a text hex dump to syslog for a binary blob of data
0232  * @level: kernel log level (e.g. KERN_DEBUG)
0233  * @prefix_str: string to prefix each line with;
0234  *  caller supplies trailing spaces for alignment if desired
0235  * @prefix_type: controls whether prefix of an offset, address, or none
0236  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
0237  * @rowsize: number of bytes to print per line; must be 16 or 32
0238  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
0239  * @buf: data blob to dump
0240  * @len: number of bytes in the @buf
0241  * @ascii: include ASCII after the hex output
0242  *
0243  * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
0244  * to the kernel log at the specified kernel log level, with an optional
0245  * leading prefix.
0246  *
0247  * print_hex_dump() works on one "line" of output at a time, i.e.,
0248  * 16 or 32 bytes of input data converted to hex + ASCII output.
0249  * print_hex_dump() iterates over the entire input @buf, breaking it into
0250  * "line size" chunks to format and print.
0251  *
0252  * E.g.:
0253  *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
0254  *          16, 1, frame->data, frame->len, true);
0255  *
0256  * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
0257  * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
0258  * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
0259  * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
0260  */
0261 void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
0262             int rowsize, int groupsize,
0263             const void *buf, size_t len, bool ascii)
0264 {
0265     const u8 *ptr = buf;
0266     int i, linelen, remaining = len;
0267     unsigned char linebuf[32 * 3 + 2 + 32 + 1];
0268 
0269     if (rowsize != 16 && rowsize != 32)
0270         rowsize = 16;
0271 
0272     for (i = 0; i < len; i += rowsize) {
0273         linelen = min(remaining, rowsize);
0274         remaining -= rowsize;
0275 
0276         hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
0277                    linebuf, sizeof(linebuf), ascii);
0278 
0279         switch (prefix_type) {
0280         case DUMP_PREFIX_ADDRESS:
0281             printk("%s%s%p: %s\n",
0282                    level, prefix_str, ptr + i, linebuf);
0283             break;
0284         case DUMP_PREFIX_OFFSET:
0285             printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
0286             break;
0287         default:
0288             printk("%s%s%s\n", level, prefix_str, linebuf);
0289             break;
0290         }
0291     }
0292 }
0293 EXPORT_SYMBOL(print_hex_dump);
0294 
0295 #endif /* defined(CONFIG_PRINTK) */