^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * arch/arm/boot/compressed/string.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Small subset of simple string routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) void *memcpy(void *__dest, __const void *__src, size_t __n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) unsigned char *d = (unsigned char *)__dest, *s = (unsigned char *)__src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) for (i = __n >> 3; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (__n & 1 << 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (__n & 1 << 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (__n & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) *d++ = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return __dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) void *memmove(void *__dest, __const void *__src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned char *d = __dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const unsigned char *s = __src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (__dest == __src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return __dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (__dest < __src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return memcpy(__dest, __src, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) d[count] = s[count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return __dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) size_t strlen(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const char *sc = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) while (*sc != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) sc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return sc - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size_t strnlen(const char *s, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const char *sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) for (sc = s; count-- && *sc != '\0'; ++sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* nothing */;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return sc - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int memcmp(const void *cs, const void *ct, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) const unsigned char *su1 = cs, *su2 = ct, *end = su1 + count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) while (su1 < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) res = *su1++ - *su2++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int strcmp(const char *cs, const char *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned char c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) c1 = *cs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) c2 = *ct++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) res = c1 - c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) } while (c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void *memchr(const void *s, int c, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) const unsigned char *p = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if ((unsigned char)c == *p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return (void *)(p - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char *strchr(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) while (*s != (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (*s++ == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return (char *)s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char *strrchr(const char *s, int c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) const char *last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (*s == (char)c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) last = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } while (*s++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return (char *)last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #undef memset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void *memset(void *s, int c, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) char *xs = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *xs++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }