diff options
author | Mimi Zohar <zohar@linux.vnet.ibm.com> | 2011-09-20 11:23:49 -0400 |
---|---|---|
committer | Mimi Zohar <zohar@linux.vnet.ibm.com> | 2011-09-20 23:24:44 -0400 |
commit | b78049831ffed65f0b4e61f69df14f3ab17922cb (patch) | |
tree | 196c37c7eeab26e5e71d4735cb4dd7a0fa9951df /lib/hexdump.c | |
parent | 6bce98edc3365a8f780ff3944ac7992544c194fe (diff) |
lib: add error checking to hex2bin
hex2bin converts a hexadecimal string to its binary representation.
The original version of hex2bin did not do any error checking. This
patch adds error checking and returns the result.
Changelog v1:
- removed unpack_hex_byte()
- changed return code from boolean to int
Changelog:
- use the new unpack_hex_byte()
- add __must_check compiler option (Andy Shevchenko's suggestion)
- change function API to return error checking result
(based on Tetsuo Handa's initial patch)
Signed-off-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
Acked-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Diffstat (limited to 'lib/hexdump.c')
-rw-r--r-- | lib/hexdump.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/hexdump.c b/lib/hexdump.c index f5fe6ba7a3ab..51d5ae210244 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c | |||
@@ -38,14 +38,21 @@ EXPORT_SYMBOL(hex_to_bin); | |||
38 | * @dst: binary result | 38 | * @dst: binary result |
39 | * @src: ascii hexadecimal string | 39 | * @src: ascii hexadecimal string |
40 | * @count: result length | 40 | * @count: result length |
41 | * | ||
42 | * Return 0 on success, -1 in case of bad input. | ||
41 | */ | 43 | */ |
42 | void hex2bin(u8 *dst, const char *src, size_t count) | 44 | int hex2bin(u8 *dst, const char *src, size_t count) |
43 | { | 45 | { |
44 | while (count--) { | 46 | while (count--) { |
45 | *dst = hex_to_bin(*src++) << 4; | 47 | int hi = hex_to_bin(*src++); |
46 | *dst += hex_to_bin(*src++); | 48 | int lo = hex_to_bin(*src++); |
47 | dst++; | 49 | |
50 | if ((hi < 0) || (lo < 0)) | ||
51 | return -1; | ||
52 | |||
53 | *dst++ = (hi << 4) | lo; | ||
48 | } | 54 | } |
55 | return 0; | ||
49 | } | 56 | } |
50 | EXPORT_SYMBOL(hex2bin); | 57 | EXPORT_SYMBOL(hex2bin); |
51 | 58 | ||