coreboot-kgpe-d16/src/lib/hexstrtobin.c
Joel Kitching 393c71c213 add ctype.h header
Sometimes coreboot needs to compile external code (e.g.
vboot_reference) using its own set of system header files.  When
these headers don't line up with C Standard Library, it causes
problems.

Create ctype.h header file.  Relocate ctype.h functions from
string.h into ctype.h.  Update source files which call ctype.h
functions accordingly.

Note that ctype.h still lacks five functions which are not used
in coreboot source:
  isalnum, isalpha, iscntrl, isgraph, ispunct

BUG=b:124141368
TEST=make clean && make test-abuild
BRANCH=none

Change-Id: I31b5e8af49956ec024a392a73c3c9024b9a9c194
Signed-off-by: Joel Kitching <kitching@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/33525
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
2019-06-24 21:15:14 +00:00

45 lines
973 B
C

/*
* Copyright 2016 Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <ctype.h>
#include <lib.h>
#include <string.h>
size_t hexstrtobin(const char *str, uint8_t *buf, size_t len)
{
size_t count, ptr = 0;
uint8_t byte;
for (byte = count = 0; str && *str; str++) {
uint8_t c = *str;
if (!isxdigit(c))
continue;
if (isdigit(c))
c -= '0';
else
c = tolower(c) - 'a' + 10;
byte <<= 4;
byte |= c;
if (++count > 1) {
if (ptr >= len)
return ptr;
buf[ptr++] = byte;
byte = count = 0;
}
}
return ptr;
}