Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * arch/arm/boot/compressed/string.c
0004  *
0005  * Small subset of simple string routines
0006  */
0007 
0008 #define __NO_FORTIFY
0009 #include <linux/string.h>
0010 
0011 /*
0012  * The decompressor is built without KASan but uses the same redirects as the
0013  * rest of the kernel when CONFIG_KASAN is enabled, defining e.g. memcpy()
0014  * to __memcpy() but since we are not linking with the main kernel string
0015  * library in the decompressor, that will lead to link failures.
0016  *
0017  * Undefine KASan's versions, define the wrapped functions and alias them to
0018  * the right names so that when e.g. __memcpy() appear in the code, it will
0019  * still be linked to this local version of memcpy().
0020  */
0021 #ifdef CONFIG_KASAN
0022 #undef memcpy
0023 #undef memmove
0024 #undef memset
0025 void *__memcpy(void *__dest, __const void *__src, size_t __n) __alias(memcpy);
0026 void *__memmove(void *__dest, __const void *__src, size_t count) __alias(memmove);
0027 void *__memset(void *s, int c, size_t count) __alias(memset);
0028 #endif
0029 
0030 void *memcpy(void *__dest, __const void *__src, size_t __n)
0031 {
0032     int i = 0;
0033     unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
0034 
0035     for (i = __n >> 3; i > 0; i--) {
0036         *d++ = *s++;
0037         *d++ = *s++;
0038         *d++ = *s++;
0039         *d++ = *s++;
0040         *d++ = *s++;
0041         *d++ = *s++;
0042         *d++ = *s++;
0043         *d++ = *s++;
0044     }
0045 
0046     if (__n & 1 << 2) {
0047         *d++ = *s++;
0048         *d++ = *s++;
0049         *d++ = *s++;
0050         *d++ = *s++;
0051     }
0052 
0053     if (__n & 1 << 1) {
0054         *d++ = *s++;
0055         *d++ = *s++;
0056     }
0057 
0058     if (__n & 1)
0059         *d++ = *s++;
0060 
0061     return __dest;
0062 }
0063 
0064 void *memmove(void *__dest, __const void *__src, size_t count)
0065 {
0066     unsigned char *d = __dest;
0067     const unsigned char *s = __src;
0068 
0069     if (__dest == __src)
0070         return __dest;
0071 
0072     if (__dest < __src)
0073         return memcpy(__dest, __src, count);
0074 
0075     while (count--)
0076         d[count] = s[count];
0077     return __dest;
0078 }
0079 
0080 size_t strlen(const char *s)
0081 {
0082     const char *sc = s;
0083 
0084     while (*sc != '\0')
0085         sc++;
0086     return sc - s;
0087 }
0088 
0089 size_t strnlen(const char *s, size_t count)
0090 {
0091     const char *sc;
0092 
0093     for (sc = s; count-- && *sc != '\0'; ++sc)
0094         /* nothing */;
0095     return sc - s;
0096 }
0097 
0098 int memcmp(const void *cs, const void *ct, size_t count)
0099 {
0100     const unsigned char *su1 = cs, *su2 = ct, *end = su1 + count;
0101     int res = 0;
0102 
0103     while (su1 < end) {
0104         res = *su1++ - *su2++;
0105         if (res)
0106             break;
0107     }
0108     return res;
0109 }
0110 
0111 int strcmp(const char *cs, const char *ct)
0112 {
0113     unsigned char c1, c2;
0114     int res = 0;
0115 
0116     do {
0117         c1 = *cs++;
0118         c2 = *ct++;
0119         res = c1 - c2;
0120         if (res)
0121             break;
0122     } while (c1);
0123     return res;
0124 }
0125 
0126 void *memchr(const void *s, int c, size_t count)
0127 {
0128     const unsigned char *p = s;
0129 
0130     while (count--)
0131         if ((unsigned char)c == *p++)
0132             return (void *)(p - 1);
0133     return NULL;
0134 }
0135 
0136 char *strchr(const char *s, int c)
0137 {
0138     while (*s != (char)c)
0139         if (*s++ == '\0')
0140             return NULL;
0141     return (char *)s;
0142 }
0143 
0144 char *strrchr(const char *s, int c)
0145 {
0146     const char *last = NULL;
0147     do {
0148         if (*s == (char)c)
0149             last = s;
0150     } while (*s++);
0151     return (char *)last;
0152 }
0153 
0154 #undef memset
0155 
0156 void *memset(void *s, int c, size_t count)
0157 {
0158     char *xs = s;
0159     while (count--)
0160         *xs++ = c;
0161     return s;
0162 }