Skip to content

Commit

Permalink
LinuxKPI: add hex2bin()
Browse files Browse the repository at this point in the history
Add a hex2bin() implementation needed by a driver's debugfs code.

Reviewed by:	hselasky
Differential Revision: https://reviews.freebsd.org/D33798

(cherry picked from commit deb9bfb)
  • Loading branch information
Bjoern A. Zeeb authored and Bjoern A. Zeeb committed Jan 16, 2022
1 parent 845458a commit f88fcc5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sys/compat/linuxkpi/common/include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,37 @@ linux_ratelimited(linux_ratelimit_t *rl)
#define TAINT_WARN 0
#define test_taint(x) (0)

static inline int
_h2b(const char c)
{

if (c >= '0' && c <= '9')
return (c - '0');
if (c >= 'a' && c <= 'f')
return (10 + c - 'a');
if (c >= 'A' && c <= 'F')
return (10 + c - 'A');
return (-EINVAL);
}

static inline int
hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
{
int hi4, lo4;

while (binlen > 0) {
hi4 = _h2b(*hexsrc++);
lo4 = _h2b(*hexsrc++);
if (hi4 < 0 || lo4 < 0)
return (-EINVAL);

*bindst++ = (hi4 << 4) | lo4;
binlen--;
}

return (0);
}

/*
* Checking if an option is defined would be easy if we could do CPP inside CPP.
* The defined case whether -Dxxx or -Dxxx=1 are easy to deal with. In either
Expand Down

0 comments on commit f88fcc5

Please sign in to comment.