42 lines
864 B
C
42 lines
864 B
C
#include <stdint.h>
|
|
#include <ctype.h>
|
|
|
|
int nsl_util_memcmp(const uint8_t *left, const uint8_t *right, size_t len);
|
|
|
|
static inline int nsl_util_fromhexdigit(int c)
|
|
{
|
|
c = tolower(c);
|
|
if (c >= '0' && c <= '9') {
|
|
return c - '0';
|
|
}
|
|
if (c >= 'a' && c <= 'f') {
|
|
return 10 + (c - 'a');
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static inline int nsl_util_fromhex(const char *src, unsigned char *dest, size_t len)
|
|
{
|
|
while (*src && len--) {
|
|
unsigned char a = nsl_util_fromhexdigit(*src++);
|
|
if (a < 0) {
|
|
return 0;
|
|
}
|
|
unsigned char b = nsl_util_fromhexdigit(*src++);
|
|
if (b < 0) {
|
|
return 0;
|
|
}
|
|
*dest++ = (a << 4) | b;
|
|
}
|
|
return !*src && !len;
|
|
}
|
|
|
|
static inline int nsl_util_min(size_t a, size_t b)
|
|
{
|
|
if (a > b) {
|
|
return a;
|
|
} else {
|
|
return b;
|
|
}
|
|
}
|