diff options
author | Jett Rink <jettrink@chromium.org> | 2020-11-11 10:30:22 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-11-11 18:37:37 +0000 |
commit | 45f7ed217d54b45dbf8460d2c29471a00c1a6ccc (patch) | |
tree | b404302f369ac998c17b5790b77fca3014109117 | |
parent | e2dfdb09d970143b521a9c6f316ebe09159988ed (diff) | |
download | chrome-ec-45f7ed217d54b45dbf8460d2c29471a00c1a6ccc.tar.gz |
zephyr: add strtoull function for 64-bit input
We do need a true 64-bit input function for common platform/ec code, and
zephyr doesn't have this. Add the support to the shim
BRANCH=none
BUG=b:172592963,b:172512307
TEST=build EC and volteer and posix_ec
Signed-off-by: Jett Rink <jettrink@chromium.org>
Change-Id: I81f69fdbe03916f3a54091ce6c077db32ed3a73c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2532679
Commit-Queue: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
-rw-r--r-- | include/util.h | 3 | ||||
-rw-r--r-- | zephyr/shim/src/util.c | 50 |
2 files changed, 50 insertions, 3 deletions
diff --git a/include/util.h b/include/util.h index ffb4c0e10d..17c1b3aed8 100644 --- a/include/util.h +++ b/include/util.h @@ -125,10 +125,7 @@ int strncmp(const char *s1, const char *s2, size_t n); /* Like strtol(), but for integers. */ int strtoi(const char *nptr, char **endptr, int base); -/* TODO(b/172592963): This should be unsigned long, and conflicts with Zephyr */ -#ifndef CONFIG_ZEPHYR unsigned long long int strtoull(const char *nptr, char **endptr, int base); -#endif /* Like strncpy(), but guarantees null termination. */ char *strzcpy(char *dest, const char *src, int len); diff --git a/zephyr/shim/src/util.c b/zephyr/shim/src/util.c index b64907e2e1..98e61f9c73 100644 --- a/zephyr/shim/src/util.c +++ b/zephyr/shim/src/util.c @@ -49,3 +49,53 @@ int parse_bool(const char *s, int *dest) /* dunno */ return 0; } + +static int find_base(int base, int *c, const char **nptr) +{ + if ((base == 0 || base == 16) && *c == '0' && + (**nptr == 'x' || **nptr == 'X')) { + *c = (*nptr)[1]; + (*nptr) += 2; + base = 16; + } else if (base == 0) { + base = *c == '0' ? 8 : 10; + } + return base; +} + +unsigned long long int strtoull(const char *nptr, char **endptr, int base) +{ + uint64_t result = 0; + int c = '\0'; + + while ((c = *nptr++) && isspace(c)) + ; + + if (c == '+') { + c = *nptr++; + } else if (c == '-') { + if (endptr) + *endptr = (char *)nptr - 1; + return result; + } + + base = find_base(base, &c, &nptr); + + while (c) { + if (c >= '0' && c < '0' + MIN(base, 10)) + result = result * base + (c - '0'); + else if (c >= 'A' && c < 'A' + base - 10) + result = result * base + (c - 'A' + 10); + else if (c >= 'a' && c < 'a' + base - 10) + result = result * base + (c - 'a' + 10); + else + break; + + c = *nptr++; + } + + if (endptr) + *endptr = (char *)nptr - 1; + return result; +} +BUILD_ASSERT(sizeof(unsigned long long int) == sizeof(uint64_t)); |