linux/lib/hexdump.c
<<
>>
Prefs
   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
  10#include <linux/types.h>
  11#include <linux/ctype.h>
  12#include <linux/errno.h>
  13#include <linux/kernel.h>
  14#include <linux/minmax.h>
  15#include <linux/export.h>
  16#include <asm/unaligned.h>
  17
  18const char hex_asc[] = "0123456789abcdef";
  19EXPORT_SYMBOL(hex_asc);
  20const char hex_asc_upper[] = "0123456789ABCDEF";
  21EXPORT_SYMBOL(hex_asc_upper);
  22
  23/**
  24 * hex_to_bin - convert a hex digit to its real value
  25 * @ch: ascii character represents hex digit
  26 *
  27 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  28 * input.
  29 */
  30int hex_to_bin(char ch)
  31{
  32        if ((ch >= '0') && (ch <= '9'))
  33                return ch - '0';
  34        ch = tolower(ch);
  35        if ((ch >= 'a') && (ch <= 'f'))
  36                return ch - 'a' + 10;
  37        return -1;
  38}
  39EXPORT_SYMBOL(hex_to_bin);
  40
  41/**
  42 * hex2bin - convert an ascii hexadecimal string to its binary representation
  43 * @dst: binary result
  44 * @src: ascii hexadecimal string
  45 * @count: result length
  46 *
  47 * Return 0 on success, -EINVAL in case of bad input.
  48 */
  49int hex2bin(u8 *dst, const char *src, size_t count)
  50{
  51        while (count--) {
  52                int hi = hex_to_bin(*src++);
  53                int lo = hex_to_bin(*src++);
  54
  55                if ((hi < 0) || (lo < 0))
  56                        return -EINVAL;
  57
  58                *dst++ = (hi << 4) | lo;
  59        }
  60        return 0;
  61}
  62EXPORT_SYMBOL(hex2bin);
  63
  64/**
  65 * bin2hex - convert binary data to an ascii hexadecimal string
  66 * @dst: ascii hexadecimal result
  67 * @src: binary data
  68 * @count: binary data length
  69 */
  70char *bin2hex(char *dst, const void *src, size_t count)
  71{
  72        const unsigned char *_src = src;
  73
  74        while (count--)
  75                dst = hex_byte_pack(dst, *_src++);
  76        return dst;
  77}
  78EXPORT_SYMBOL(bin2hex);
  79
  80/**
  81 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  82 * @buf: data blob to dump
  83 * @len: number of bytes in the @buf
  84 * @rowsize: number of bytes to print per line; must be 16 or 32
  85 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  86 * @linebuf: where to put the converted data
  87 * @linebuflen: total size of @linebuf, including space for terminating NUL
  88 * @ascii: include ASCII after the hex output
  89 *
  90 * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  91 * 16 or 32 bytes of input data converted to hex + ASCII output.
  92 *
  93 * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  94 * to a hex + ASCII dump at the supplied memory location.
  95 * The converted output is always NUL-terminated.
  96 *
  97 * E.g.:
  98 *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  99 *                      linebuf, sizeof(linebuf), true);
 100 *
 101 * example output buffer:
 102 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
 103 *
 104 * Return:
 105 * The amount of bytes placed in the buffer without terminating NUL. If the
 106 * output was truncated, then the return value is the number of bytes
 107 * (excluding the terminating NUL) which would have been written to the final
 108 * string if enough space had been available.
 109 */
 110int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
 111                       char *linebuf, size_t linebuflen, bool ascii)
 112{
 113        const u8 *ptr = buf;
 114        int ngroups;
 115        u8 ch;
 116        int j, lx = 0;
 117        int ascii_column;
 118        int ret;
 119
 120        if (rowsize != 16 && rowsize != 32)
 121                rowsize = 16;
 122
 123        if (len > rowsize)              /* limit to one line at a time */
 124                len = rowsize;
 125        if (!is_power_of_2(groupsize) || groupsize > 8)
 126                groupsize = 1;
 127        if ((len % groupsize) != 0)     /* no mixed size output */
 128                groupsize = 1;
 129
 130        ngroups = len / groupsize;
 131        ascii_column = rowsize * 2 + rowsize / groupsize + 1;
 132
 133        if (!linebuflen)
 134                goto overflow1;
 135
 136        if (!len)
 137                goto nil;
 138
 139        if (groupsize == 8) {
 140                const u64 *ptr8 = buf;
 141
 142                for (j = 0; j < ngroups; j++) {
 143                        ret = snprintf(linebuf + lx, linebuflen - lx,
 144                                       "%s%16.16llx", j ? " " : "",
 145                                       get_unaligned(ptr8 + j));
 146                        if (ret >= linebuflen - lx)
 147                                goto overflow1;
 148                        lx += ret;
 149                }
 150        } else if (groupsize == 4) {
 151                const u32 *ptr4 = buf;
 152
 153                for (j = 0; j < ngroups; j++) {
 154                        ret = snprintf(linebuf + lx, linebuflen - lx,
 155                                       "%s%8.8x", j ? " " : "",
 156                                       get_unaligned(ptr4 + j));
 157                        if (ret >= linebuflen - lx)
 158                                goto overflow1;
 159                        lx += ret;
 160                }
 161        } else if (groupsize == 2) {
 162                const u16 *ptr2 = buf;
 163
 164                for (j = 0; j < ngroups; j++) {
 165                        ret = snprintf(linebuf + lx, linebuflen - lx,
 166                                       "%s%4.4x", j ? " " : "",
 167                                       get_unaligned(ptr2 + j));
 168                        if (ret >= linebuflen - lx)
 169                                goto overflow1;
 170                        lx += ret;
 171                }
 172        } else {
 173                for (j = 0; j < len; j++) {
 174                        if (linebuflen < lx + 2)
 175                                goto overflow2;
 176                        ch = ptr[j];
 177                        linebuf[lx++] = hex_asc_hi(ch);
 178                        if (linebuflen < lx + 2)
 179                                goto overflow2;
 180                        linebuf[lx++] = hex_asc_lo(ch);
 181                        if (linebuflen < lx + 2)
 182                                goto overflow2;
 183                        linebuf[lx++] = ' ';
 184                }
 185                if (j)
 186                        lx--;
 187        }
 188        if (!ascii)
 189                goto nil;
 190
 191        while (lx < ascii_column) {
 192                if (linebuflen < lx + 2)
 193                        goto overflow2;
 194                linebuf[lx++] = ' ';
 195        }
 196        for (j = 0; j < len; j++) {
 197                if (linebuflen < lx + 2)
 198                        goto overflow2;
 199                ch = ptr[j];
 200                linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
 201        }
 202nil:
 203        linebuf[lx] = '\0';
 204        return lx;
 205overflow2:
 206        linebuf[lx++] = '\0';
 207overflow1:
 208        return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
 209}
 210EXPORT_SYMBOL(hex_dump_to_buffer);
 211
 212#ifdef CONFIG_PRINTK
 213/**
 214 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
 215 * @level: kernel log level (e.g. KERN_DEBUG)
 216 * @prefix_str: string to prefix each line with;
 217 *  caller supplies trailing spaces for alignment if desired
 218 * @prefix_type: controls whether prefix of an offset, address, or none
 219 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
 220 * @rowsize: number of bytes to print per line; must be 16 or 32
 221 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
 222 * @buf: data blob to dump
 223 * @len: number of bytes in the @buf
 224 * @ascii: include ASCII after the hex output
 225 *
 226 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
 227 * to the kernel log at the specified kernel log level, with an optional
 228 * leading prefix.
 229 *
 230 * print_hex_dump() works on one "line" of output at a time, i.e.,
 231 * 16 or 32 bytes of input data converted to hex + ASCII output.
 232 * print_hex_dump() iterates over the entire input @buf, breaking it into
 233 * "line size" chunks to format and print.
 234 *
 235 * E.g.:
 236 *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
 237 *                  16, 1, frame->data, frame->len, true);
 238 *
 239 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
 240 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
 241 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
 242 * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
 243 */
 244void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
 245                    int rowsize, int groupsize,
 246                    const void *buf, size_t len, bool ascii)
 247{
 248        const u8 *ptr = buf;
 249        int i, linelen, remaining = len;
 250        unsigned char linebuf[32 * 3 + 2 + 32 + 1];
 251
 252        if (rowsize != 16 && rowsize != 32)
 253                rowsize = 16;
 254
 255        for (i = 0; i < len; i += rowsize) {
 256                linelen = min(remaining, rowsize);
 257                remaining -= rowsize;
 258
 259                hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
 260                                   linebuf, sizeof(linebuf), ascii);
 261
 262                switch (prefix_type) {
 263                case DUMP_PREFIX_ADDRESS:
 264                        printk("%s%s%p: %s\n",
 265                               level, prefix_str, ptr + i, linebuf);
 266                        break;
 267                case DUMP_PREFIX_OFFSET:
 268                        printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
 269                        break;
 270                default:
 271                        printk("%s%s%s\n", level, prefix_str, linebuf);
 272                        break;
 273                }
 274        }
 275}
 276EXPORT_SYMBOL(print_hex_dump);
 277
 278#if !defined(CONFIG_DYNAMIC_DEBUG)
 279/**
 280 * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
 281 * @prefix_str: string to prefix each line with;
 282 *  caller supplies trailing spaces for alignment if desired
 283 * @prefix_type: controls whether prefix of an offset, address, or none
 284 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
 285 * @buf: data blob to dump
 286 * @len: number of bytes in the @buf
 287 *
 288 * Calls print_hex_dump(), with log level of KERN_DEBUG,
 289 * rowsize of 16, groupsize of 1, and ASCII output included.
 290 */
 291void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
 292                          const void *buf, size_t len)
 293{
 294        print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
 295                       buf, len, true);
 296}
 297EXPORT_SYMBOL(print_hex_dump_bytes);
 298#endif /* !defined(CONFIG_DYNAMIC_DEBUG) */
 299#endif /* defined(CONFIG_PRINTK) */
 300