LinuxKPI: move hex2bin() from kernel.h to new hex.h

New Linux v7.0 drivers include hex.h.  Rather than adding a dummy
header, migrate the kernel.h hex2bin() into hex.h, where it belongs.
Care needs to be taken as the _h2b() helper function is still used by
other bits in kernel.h.

Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
Reviewed by:	emaste, dumbbell
Differential Revision: https://reviews.freebsd.org/D56391
This commit is contained in:
Bjoern A. Zeeb
2026-04-14 13:36:40 +00:00
parent 125b09de09
commit 98297ff3cf
2 changed files with 45 additions and 31 deletions
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2022-2026 Bjoern A. Zeeb
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#ifndef _LINUXKPI_LINUX_HEX_H_
#define _LINUXKPI_LINUX_HEX_H_
#include <linux/types.h>
#include <linux/errno.h>
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);
}
#endif /* _LINUXKPI_LINUX_HEX_H_ */
@@ -56,6 +56,7 @@
#include <linux/log2.h>
#include <linux/kconfig.h>
#include <linux/instruction_pointer.h>
#include <linux/hex.h>
#include <asm/byteorder.h>
#include <asm/cpufeature.h>
@@ -305,37 +306,6 @@ linux_ratelimited(linux_ratelimit_t *rl)
#define add_taint(x,y) do { \
} while (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);
}
static inline bool
mac_pton(const char *macin, uint8_t *macout)
{