Skip to content

Commit

Permalink
lib: add error checking to hex2bin
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Mimi Zohar committed Sep 21, 2011
1 parent 6bce98e commit b780498
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
}

extern int hex_to_bin(char ch);
extern void hex2bin(u8 *dst, const char *src, size_t count);
extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);

/*
* General tracing related utility functions - trace_printk(),
Expand Down
15 changes: 11 additions & 4 deletions lib/hexdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ EXPORT_SYMBOL(hex_to_bin);
* @dst: binary result
* @src: ascii hexadecimal string
* @count: result length
*
* Return 0 on success, -1 in case of bad input.
*/
void hex2bin(u8 *dst, const char *src, size_t count)
int hex2bin(u8 *dst, const char *src, size_t count)
{
while (count--) {
*dst = hex_to_bin(*src++) << 4;
*dst += hex_to_bin(*src++);
dst++;
int hi = hex_to_bin(*src++);
int lo = hex_to_bin(*src++);

if ((hi < 0) || (lo < 0))
return -1;

*dst++ = (hi << 4) | lo;
}
return 0;
}
EXPORT_SYMBOL(hex2bin);

Expand Down

0 comments on commit b780498

Please sign in to comment.