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