1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
10 #include <linux/ctype.h>
11 #include <linux/types.h>
19 extern const char hex_asc[];
20 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
21 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
23 static inline char *hex_byte_pack(char *buf, u8 byte)
25 *buf++ = hex_asc_hi(byte);
26 *buf++ = hex_asc_lo(byte);
31 * hex_to_bin - convert a hex digit to its real value
32 * @ch: ascii character represents hex digit
34 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
37 static inline int hex_to_bin(char ch)
39 if ((ch >= '0') && (ch <= '9'))
42 if ((ch >= 'a') && (ch <= 'f'))
48 * hex2bin - convert an ascii hexadecimal string to its binary representation
50 * @src: ascii hexadecimal string
51 * @count: result length
53 * Return 0 on success, -1 in case of bad input.
55 static inline int hex2bin(u8 *dst, const char *src, size_t count)
58 int hi = hex_to_bin(*src++);
59 int lo = hex_to_bin(*src++);
61 if ((hi < 0) || (lo < 0))
64 *dst++ = (hi << 4) | lo;
70 * bin2hex - convert binary data to an ascii hexadecimal string
71 * @dst: ascii hexadecimal result
73 * @count: binary data length
75 static inline char *bin2hex(char *dst, const void *src, size_t count)
77 const unsigned char *_src = src;
80 dst = hex_byte_pack(dst, *_src++);
84 int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
85 char *linebuf, size_t linebuflen, bool ascii);
86 void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
87 int groupsize, const void *buf, size_t len, bool ascii);
88 void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
89 const void *buf, size_t len);
91 #endif /* HEXDUMP_H */