diff options
Diffstat (limited to 'roms/skiboot/libc/string')
-rw-r--r-- | roms/skiboot/libc/string/Makefile.inc | 23 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memchr.c | 28 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memcmp.c | 29 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memcpy.c | 36 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memcpy_from_ci.c | 41 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memmove.c | 36 | ||||
-rw-r--r-- | roms/skiboot/libc/string/memset.c | 54 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strcasecmp.c | 27 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strcat.c | 26 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strchr.c | 28 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strcmp.c | 25 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strcpy.c | 23 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strdup.c | 27 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strlen.c | 40 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strncasecmp.c | 30 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strncmp.c | 30 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strncpy.c | 33 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strrchr.c | 28 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strstr.c | 39 | ||||
-rw-r--r-- | roms/skiboot/libc/string/strtok.c | 48 |
20 files changed, 651 insertions, 0 deletions
diff --git a/roms/skiboot/libc/string/Makefile.inc b/roms/skiboot/libc/string/Makefile.inc new file mode 100644 index 000000000..2f038219f --- /dev/null +++ b/roms/skiboot/libc/string/Makefile.inc @@ -0,0 +1,23 @@ +# ***************************************************************************** +# * Copyright (c) 2004, 2016 IBM Corporation +# * All rights reserved. +# * This program and the accompanying materials +# * are made available under the terms of the BSD License +# * which accompanies this distribution, and is available at +# * http://www.opensource.org/licenses/bsd-license.php +# * +# * Contributors: +# * IBM Corporation - initial implementation +# ****************************************************************************/ + +SUBDIRS += $(LIBCDIR)/string + +STRING_OBJS = strcat.o strchr.o strrchr.o strcmp.o strcpy.o strlen.o \ + strncmp.o strncpy.o strstr.o memset.o memcpy.o memcpy_from_ci.o \ + memmove.o memchr.o memcmp.o strcasecmp.o strncasecmp.o \ + strtok.o strdup.o +STRING = $(LIBCDIR)/string/built-in.a +$(STRING): $(STRING_OBJS:%=$(LIBCDIR)/string/%) + +CFLAGS_SKIP_libc/string/ += -Os +CFLAGS_libc/string/ += -O2 diff --git a/roms/skiboot/libc/string/memchr.c b/roms/skiboot/libc/string/memchr.c new file mode 100644 index 000000000..db9a147c6 --- /dev/null +++ b/roms/skiboot/libc/string/memchr.c @@ -0,0 +1,28 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +void *memchr(const void *ptr, int c, size_t n); +void *memchr(const void *ptr, int c, size_t n) +{ + unsigned char ch = (unsigned char)c; + const unsigned char *p = ptr; + + while (n-- > 0) { + if (*p == ch) + return (void *)p; + p += 1; + } + + return NULL; +} diff --git a/roms/skiboot/libc/string/memcmp.c b/roms/skiboot/libc/string/memcmp.c new file mode 100644 index 000000000..b270b597b --- /dev/null +++ b/roms/skiboot/libc/string/memcmp.c @@ -0,0 +1,29 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +int memcmp(const void *ptr1, const void *ptr2, size_t n); +int memcmp(const void *ptr1, const void *ptr2, size_t n) +{ + const unsigned char *p1 = ptr1; + const unsigned char *p2 = ptr2; + + while (n-- > 0) { + if (*p1 != *p2) + return (*p1 - *p2); + p1 += 1; + p2 += 1; + } + + return 0; +} diff --git a/roms/skiboot/libc/string/memcpy.c b/roms/skiboot/libc/string/memcpy.c new file mode 100644 index 000000000..26f953d2e --- /dev/null +++ b/roms/skiboot/libc/string/memcpy.c @@ -0,0 +1,36 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> +#include <ccan/short_types/short_types.h> + +void *memcpy(void *dest, const void *src, size_t n); +void *memcpy(void *dest, const void *src, size_t n) +{ + void *ret = dest; + + while (n >= 8) { + *(uint64_t *)dest = *(uint64_t *)src; + dest += 8; + src += 8; + n -= 8; + } + + while (n > 0) { + *(uint8_t *)dest = *(uint8_t *)src; + dest += 1; + src += 1; + n -= 1; + } + + return ret; +} diff --git a/roms/skiboot/libc/string/memcpy_from_ci.c b/roms/skiboot/libc/string/memcpy_from_ci.c new file mode 100644 index 000000000..dd4d94e02 --- /dev/null +++ b/roms/skiboot/libc/string/memcpy_from_ci.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later +/* + * Copyright 2013-2016 IBM Corp + */ + +#include <ccan/short_types/short_types.h> +#include <io.h> +#include <string.h> + +void *memcpy_from_ci(void *destpp, const void *srcpp, size_t len) +{ + const size_t block = sizeof(uint64_t); + unsigned long int destp = (long int) destpp; + unsigned long int srcp = (long int) srcpp; + + /* Copy as many blocks as possible if srcp is block aligned */ + if ((srcp % block) == 0) { + while ((len - block) > -1) { + uint64_t v; + if (HAVE_BIG_ENDIAN) + v = in_be64((beint64_t*)srcp); + else + v = in_le64((leint64_t*)srcp); + *((uint64_t *) destp) = v; + srcp += block; + destp += block; + len -= block; + } + } + /* + * Byte-by-byte copy if srcp is not block aligned or len is/becomes + * less than one block + */ + while (len > 0) { + *((uint8_t*) destp) = in_8((uint8_t*)srcp); + srcp += 1; + destp += 1; + len--; + } + return destpp; +} diff --git a/roms/skiboot/libc/string/memmove.c b/roms/skiboot/libc/string/memmove.c new file mode 100644 index 000000000..76aef6c87 --- /dev/null +++ b/roms/skiboot/libc/string/memmove.c @@ -0,0 +1,36 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +void *memcpy(void *dest, const void *src, size_t n); +void *memmove(void *dest, const void *src, size_t n); +void *memmove(void *dest, const void *src, size_t n) +{ + /* Do the buffers overlap in a bad way? */ + if (src < dest && src + n >= dest) { + char *cdest; + const char *csrc; + int i; + + /* Copy from end to start */ + cdest = dest + n - 1; + csrc = src + n - 1; + for (i = 0; i < n; i++) { + *cdest-- = *csrc--; + } + return dest; + } else { + /* Normal copy is possible */ + return memcpy(dest, src, n); + } +} diff --git a/roms/skiboot/libc/string/memset.c b/roms/skiboot/libc/string/memset.c new file mode 100644 index 000000000..f96a0231f --- /dev/null +++ b/roms/skiboot/libc/string/memset.c @@ -0,0 +1,54 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#define CACHE_LINE_SIZE 128 + +#include <stddef.h> + +void *memset(void *dest, int c, size_t size); +void *memset(void *dest, int c, size_t size) +{ + unsigned char *d = (unsigned char *)dest; + unsigned long big_c = 0; + +#if defined(__powerpc__) || defined(__powerpc64__) + if (size > CACHE_LINE_SIZE && c==0) { + while ((unsigned long long)d % CACHE_LINE_SIZE) { + *d++ = (unsigned char)c; + size--; + } + while (size >= CACHE_LINE_SIZE) { + asm volatile ("dcbz 0,%0\n" : : "r"(d) : "memory"); + d+= CACHE_LINE_SIZE; + size-= CACHE_LINE_SIZE; + } + } +#endif + + if (c) { + big_c = c; + big_c |= (big_c << 8) | big_c; + big_c |= (big_c << 16) | big_c; + big_c |= (big_c << 32) | big_c; + } + while (size >= 8 && c == 0) { + *((unsigned long *)d) = big_c; + d+=8; + size-=8; + } + + while (size-- > 0) { + *d++ = (unsigned char)c; + } + + return dest; +} diff --git a/roms/skiboot/libc/string/strcasecmp.c b/roms/skiboot/libc/string/strcasecmp.c new file mode 100644 index 000000000..ba1aedb5f --- /dev/null +++ b/roms/skiboot/libc/string/strcasecmp.c @@ -0,0 +1,27 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <ctype.h> + +int strcasecmp(const char *s1, const char *s2); +int strcasecmp(const char *s1, const char *s2) +{ + while (*s1 != 0 && *s2 != 0) { + if (toupper(*s1) != toupper(*s2)) + break; + ++s1; + ++s2; + } + + return *s1 - *s2; +} + diff --git a/roms/skiboot/libc/string/strcat.c b/roms/skiboot/libc/string/strcat.c new file mode 100644 index 000000000..329cc88e1 --- /dev/null +++ b/roms/skiboot/libc/string/strcat.c @@ -0,0 +1,26 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +size_t strlen(const char *s); +char *strcpy(char *dst, const char *src); +char *strcat(char *dst, const char *src); +char *strcat(char *dst, const char *src) +{ + size_t p; + + p = strlen(dst); + strcpy(&dst[p], src); + + return dst; +} diff --git a/roms/skiboot/libc/string/strchr.c b/roms/skiboot/libc/string/strchr.c new file mode 100644 index 000000000..88f25f96b --- /dev/null +++ b/roms/skiboot/libc/string/strchr.c @@ -0,0 +1,28 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +char *strchr(const char *s, int c); +char *strchr(const char *s, int c) +{ + char cb = c; + + while (*s != 0) { + if (*s == cb) { + return (char *)s; + } + s += 1; + } + + return NULL; +} diff --git a/roms/skiboot/libc/string/strcmp.c b/roms/skiboot/libc/string/strcmp.c new file mode 100644 index 000000000..5afbae2a7 --- /dev/null +++ b/roms/skiboot/libc/string/strcmp.c @@ -0,0 +1,25 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +int strcmp(const char *s1, const char *s2); +int strcmp(const char *s1, const char *s2) +{ + while (*s1 != 0 && *s2 != 0) { + if (*s1 != *s2) + break; + s1 += 1; + s2 += 1; + } + + return *s1 - *s2; +} + diff --git a/roms/skiboot/libc/string/strcpy.c b/roms/skiboot/libc/string/strcpy.c new file mode 100644 index 000000000..514be1714 --- /dev/null +++ b/roms/skiboot/libc/string/strcpy.c @@ -0,0 +1,23 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +char *strcpy(char *dst, const char *src); +char *strcpy(char *dst, const char *src) +{ + char *ptr = dst; + + do { + *ptr++ = *src; + } while (*src++ != 0); + + return dst; +} diff --git a/roms/skiboot/libc/string/strdup.c b/roms/skiboot/libc/string/strdup.c new file mode 100644 index 000000000..b0a4b4d70 --- /dev/null +++ b/roms/skiboot/libc/string/strdup.c @@ -0,0 +1,27 @@ +/****************************************************************************** + * Copyright (c) 2012 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stdlib.h> + +size_t strlen(const char *s); +void *memcpy(void *dest, const void *src, size_t n); +char *strdup(const char *src); +char *strdup(const char *src) +{ + size_t len = strlen(src) + 1; + char *ret; + + ret = malloc(len); + if (ret) + memcpy(ret, src, len); + return ret; +} diff --git a/roms/skiboot/libc/string/strlen.c b/roms/skiboot/libc/string/strlen.c new file mode 100644 index 000000000..f3c5a8362 --- /dev/null +++ b/roms/skiboot/libc/string/strlen.c @@ -0,0 +1,40 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +size_t strlen(const char *s); +size_t strlen(const char *s) +{ + size_t len = 0; + + while (*s != 0) { + len += 1; + s += 1; + } + + return len; +} + +size_t strnlen(const char *s, size_t n); +size_t strnlen(const char *s, size_t n) +{ + size_t len = 0; + + while (*s != 0 && n) { + len += 1; + s += 1; + n--; + } + + return len; +} diff --git a/roms/skiboot/libc/string/strncasecmp.c b/roms/skiboot/libc/string/strncasecmp.c new file mode 100644 index 000000000..c6b158e60 --- /dev/null +++ b/roms/skiboot/libc/string/strncasecmp.c @@ -0,0 +1,30 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <ctype.h> + +int strncasecmp(const char *s1, const char *s2, size_t n); +int strncasecmp(const char *s1, const char *s2, size_t n) +{ + if (n < 1) + return 0; + + while (*s1 != 0 && *s2 != 0 && --n > 0) { + if (toupper(*s1) != toupper(*s2)) + break; + ++s1; + ++s2; + } + + return toupper(*s1) - toupper(*s2); +} + diff --git a/roms/skiboot/libc/string/strncmp.c b/roms/skiboot/libc/string/strncmp.c new file mode 100644 index 000000000..a5422c0dc --- /dev/null +++ b/roms/skiboot/libc/string/strncmp.c @@ -0,0 +1,30 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +int strncmp(const char *s1, const char *s2, size_t n); +int strncmp(const char *s1, const char *s2, size_t n) +{ + if (n < 1) + return 0; + + while (*s1 != 0 && *s2 != 0 && --n > 0) { + if (*s1 != *s2) + break; + s1 += 1; + s2 += 1; + } + + return *s1 - *s2; +} + diff --git a/roms/skiboot/libc/string/strncpy.c b/roms/skiboot/libc/string/strncpy.c new file mode 100644 index 000000000..621c89b64 --- /dev/null +++ b/roms/skiboot/libc/string/strncpy.c @@ -0,0 +1,33 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +char *strncpy(char *dst, const char *src, size_t n); +char *strncpy(char *dst, const char *src, size_t n) +{ + char *ret = dst; + + /* Copy string */ + while (*src != 0 && n > 0) { + *dst++ = *src++; + n -= 1; + } + + /* strncpy always clears the rest of destination string... */ + while (n > 0) { + *dst++ = 0; + n -= 1; + } + + return ret; +} diff --git a/roms/skiboot/libc/string/strrchr.c b/roms/skiboot/libc/string/strrchr.c new file mode 100644 index 000000000..262a68287 --- /dev/null +++ b/roms/skiboot/libc/string/strrchr.c @@ -0,0 +1,28 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008, 2019 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +char *strrchr(const char *s, int c); +char *strrchr(const char *s, int c) +{ + char *last = NULL; + char cb = c; + + while (*s != 0) { + if (*s == cb) + last = (char *)s; + s += 1; + } + + return last; +} diff --git a/roms/skiboot/libc/string/strstr.c b/roms/skiboot/libc/string/strstr.c new file mode 100644 index 000000000..cd9ccae9a --- /dev/null +++ b/roms/skiboot/libc/string/strstr.c @@ -0,0 +1,39 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +size_t strlen(const char *s); +int strncmp(const char *s1, const char *s2, size_t n); +char *strstr(const char *hay, const char *needle); +char *strstr(const char *hay, const char *needle) +{ + char *pos; + size_t hlen, nlen; + + if (hay == NULL || needle == NULL) + return NULL; + + hlen = strlen(hay); + nlen = strlen(needle); + if (nlen < 1) + return (char *)hay; + + for (pos = (char *)hay; pos < hay + hlen; pos++) { + if (strncmp(pos, needle, nlen) == 0) { + return pos; + } + } + + return NULL; +} + diff --git a/roms/skiboot/libc/string/strtok.c b/roms/skiboot/libc/string/strtok.c new file mode 100644 index 000000000..fcc3fce32 --- /dev/null +++ b/roms/skiboot/libc/string/strtok.c @@ -0,0 +1,48 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stddef.h> + +char *strtok(char *src, const char *pattern); +char *strtok(char *src, const char *pattern) +{ + static char *nxtTok; + char *retVal = NULL; + + if (!src) { + src = nxtTok; + if (!src) + return retVal; + } + + while (*src) { + const char *pp = pattern; + while (*pp) { + if (*pp == *src) { + break; + } + pp++; + } + if (!*pp) { + if (!retVal) + retVal = src; + else if (!src[-1]) + break; + } else + *src = '\0'; + src++; + } + + nxtTok = src; + + return retVal; +} |