Back to home page

LXR

 
 

    


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