diff options
author | Nadim Taha <ntaha@google.com> | 2017-02-02 18:48:03 -0800 |
---|---|---|
committer | Nadim Taha <ntaha@chromium.org> | 2017-02-17 01:46:31 +0000 |
commit | 69c3fc2378ee9026277c1cbaf6e8aff0b99ecf46 (patch) | |
tree | 548d9660cb450bdb29cb08be0fdaff740854d276 | |
parent | 61a5649e302915c617f4f70a1b3211805f363ed8 (diff) | |
download | chrome-ec-69c3fc2378ee9026277c1cbaf6e8aff0b99ecf46.tar.gz |
builtin: Expands string.h / stdint.h
Declares UINT8_MAX, INT8_MAX and defines strnlen(), strncpy(), strncmp() &
memchr(). Needed by a module I'm integrating into cr51.
BRANCH=none
BUG=none
TEST=make buildall -j
Change-Id: I894b0297216df1b945b36fc77cd3bc5c4ef8aa2b
Signed-off-by: Nadim Taha <ntaha@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/436786
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | builtin/stdint.h | 7 | ||||
-rw-r--r-- | builtin/string.h | 5 | ||||
-rw-r--r-- | common/util.c | 56 | ||||
-rw-r--r-- | include/util.h | 4 | ||||
-rw-r--r-- | test/utils.c | 47 |
5 files changed, 119 insertions, 0 deletions
diff --git a/builtin/stdint.h b/builtin/stdint.h index f80a46f1a5..b82268b409 100644 --- a/builtin/stdint.h +++ b/builtin/stdint.h @@ -23,6 +23,13 @@ typedef unsigned int uintptr_t; typedef uint8_t uint_least8_t; +#ifndef UINT8_MAX +#define UINT8_MAX (255U) +#endif +#ifndef INT8_MAX +#define INT8_MAX (127U) +#endif + #ifndef UINT16_MAX #define UINT16_MAX (65535U) #endif diff --git a/builtin/string.h b/builtin/string.h index 50a6d54664..21159a5eef 100644 --- a/builtin/string.h +++ b/builtin/string.h @@ -14,5 +14,10 @@ int memcmp(const void *s1, const void *s2, size_t len); void *memcpy(void *dest, const void *src, size_t len); void *memmove(void *dest, const void *src, size_t n); void *memset(void *dest, int c, size_t len); +void *memchr(const void *buffer, int c, size_t n); + +size_t strnlen(const char *s, size_t maxlen); +char *strncpy(char *dest, const char *src, size_t n); +int strncmp(const char *s1, const char *s2, size_t n); #endif /* __CROS_EC_STRINGS_H__ */ diff --git a/common/util.c b/common/util.c index e1b523de97..042523d2b9 100644 --- a/common/util.c +++ b/common/util.c @@ -18,6 +18,18 @@ int strlen(const char *s) } +size_t strnlen(const char *s, size_t maxlen) +{ + size_t len = 0; + + while (len < maxlen && *s) { + s++; + len++; + } + return len; +} + + int isspace(int c) { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; @@ -49,6 +61,7 @@ int tolower(int c) int strcasecmp(const char *s1, const char *s2) { int diff; + do { diff = tolower(*s1) - tolower(*s2); if (diff) @@ -310,6 +323,20 @@ void *memmove(void *dest, const void *src, size_t len) } +void *memchr(const void *buffer, int c, size_t n) +{ + char *current = (char *)buffer; + char *end = current + n; + + while (current != end) { + if (*current == c) + return current; + current++; + } + return NULL; +} + + void reverse(void *dest, size_t len) { int i; @@ -339,6 +366,35 @@ char *strzcpy(char *dest, const char *src, int len) } +char *strncpy(char *dest, const char *src, size_t n) +{ + char *d = dest; + + while (n && *src) { + *d++ = *src++; + n--; + } + if (n) + *d = '\0'; + return dest; +} + + +int strncmp(const char *s1, const char *s2, size_t n) +{ + while (n--) { + if (*s1 != *s2) + return *s1 - *s2; + if (!*s1) + break; + s1++; + s2++; + + } + return 0; +} + + int uint64divmod(uint64_t *n, int d) { uint64_t q = 0, mask; diff --git a/include/util.h b/include/util.h index e031148e45..62c9bceb25 100644 --- a/include/util.h +++ b/include/util.h @@ -69,9 +69,13 @@ int memcmp(const void *s1, const void *s2, size_t len); void *memcpy(void *dest, const void *src, size_t len); __visible void *memset(void *dest, int c, size_t len); void *memmove(void *dest, const void *src, size_t len); +void *memchr(const void *buffer, int c, size_t n); int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2, size_t size); int strlen(const char *s); +size_t strnlen(const char *s, size_t maxlen); +char *strncpy(char *dest, const char *src, size_t n); +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); diff --git a/test/utils.c b/test/utils.c index e7de7f8b3d..293c1507c4 100644 --- a/test/utils.c +++ b/test/utils.c @@ -231,6 +231,17 @@ static int test_memset(void) return EC_SUCCESS; } +static int test_memchr(void) +{ + char *buf = "1234"; + + TEST_ASSERT(memchr("123", '4', 8) == NULL); + TEST_ASSERT(memchr("123", '3', 2) == NULL); + TEST_ASSERT(memchr(buf, '3', 8) == buf + 2); + TEST_ASSERT(memchr(buf, '4', 4) == buf + 3); + return EC_SUCCESS; +} + static int test_strzcpy(void) { char dest[10]; @@ -245,11 +256,43 @@ static int test_strzcpy(void) return EC_SUCCESS; } +static int test_strncpy(void) +{ + char dest[10]; + + strncpy(dest, "test", 10); + TEST_ASSERT_ARRAY_EQ("test", dest, 5); + strncpy(dest, "12345", 6); + TEST_ASSERT_ARRAY_EQ("12345", dest, 6); + strncpy(dest, "testtesttest", 10); + TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10); + + return EC_SUCCESS; +} + +static int test_strncmp(void) +{ + TEST_ASSERT(strncmp("123", "123", 8) == 0); + TEST_ASSERT(strncmp("789", "456", 8) > 0); + TEST_ASSERT(strncmp("abc", "abd", 4) < 0); + TEST_ASSERT(strncmp("abc", "abd", 2) == 0); + return EC_SUCCESS; +} + static int test_strlen(void) { TEST_CHECK(strlen("this is a string") == 16); } +static int test_strnlen(void) +{ + TEST_ASSERT(strnlen("this is a string", 17) == 16); + TEST_ASSERT(strnlen("this is a string", 16) == 16); + TEST_ASSERT(strnlen("this is a string", 5) == 5); + + return EC_SUCCESS; +} + static int test_strcasecmp(void) { TEST_CHECK((strcasecmp("test string", "TEST strIng") == 0) && @@ -453,8 +496,12 @@ void run_test(void) RUN_TEST(test_memmove); RUN_TEST(test_memcpy); RUN_TEST(test_memset); + RUN_TEST(test_memchr); RUN_TEST(test_strzcpy); + RUN_TEST(test_strncpy); + RUN_TEST(test_strncmp); RUN_TEST(test_strlen); + RUN_TEST(test_strnlen); RUN_TEST(test_strcasecmp); RUN_TEST(test_strncasecmp); RUN_TEST(test_atoi); |