xref: /openbmc/linux/arch/x86/boot/string.c (revision 5d331b7f)
1 /* -*- linux-c -*- ------------------------------------------------------- *
2  *
3  *   Copyright (C) 1991, 1992 Linus Torvalds
4  *   Copyright 2007 rPath, Inc. - All Rights Reserved
5  *
6  *   This file is part of the Linux kernel, and is made available under
7  *   the terms of the GNU General Public License version 2.
8  *
9  * ----------------------------------------------------------------------- */
10 
11 /*
12  * Very basic string functions
13  */
14 
15 #include <linux/types.h>
16 #include <asm/asm.h>
17 #include "ctype.h"
18 #include "string.h"
19 
20 /*
21  * Undef these macros so that the functions that we provide
22  * here will have the correct names regardless of how string.h
23  * may have chosen to #define them.
24  */
25 #undef memcpy
26 #undef memset
27 #undef memcmp
28 
29 int memcmp(const void *s1, const void *s2, size_t len)
30 {
31 	bool diff;
32 	asm("repe; cmpsb" CC_SET(nz)
33 	    : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
34 	return diff;
35 }
36 
37 int strcmp(const char *str1, const char *str2)
38 {
39 	const unsigned char *s1 = (const unsigned char *)str1;
40 	const unsigned char *s2 = (const unsigned char *)str2;
41 	int delta = 0;
42 
43 	while (*s1 || *s2) {
44 		delta = *s1 - *s2;
45 		if (delta)
46 			return delta;
47 		s1++;
48 		s2++;
49 	}
50 	return 0;
51 }
52 
53 int strncmp(const char *cs, const char *ct, size_t count)
54 {
55 	unsigned char c1, c2;
56 
57 	while (count) {
58 		c1 = *cs++;
59 		c2 = *ct++;
60 		if (c1 != c2)
61 			return c1 < c2 ? -1 : 1;
62 		if (!c1)
63 			break;
64 		count--;
65 	}
66 	return 0;
67 }
68 
69 size_t strnlen(const char *s, size_t maxlen)
70 {
71 	const char *es = s;
72 	while (*es && maxlen) {
73 		es++;
74 		maxlen--;
75 	}
76 
77 	return (es - s);
78 }
79 
80 unsigned int atou(const char *s)
81 {
82 	unsigned int i = 0;
83 	while (isdigit(*s))
84 		i = i * 10 + (*s++ - '0');
85 	return i;
86 }
87 
88 /* Works only for digits and letters, but small and fast */
89 #define TOLOWER(x) ((x) | 0x20)
90 
91 static unsigned int simple_guess_base(const char *cp)
92 {
93 	if (cp[0] == '0') {
94 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
95 			return 16;
96 		else
97 			return 8;
98 	} else {
99 		return 10;
100 	}
101 }
102 
103 /**
104  * simple_strtoull - convert a string to an unsigned long long
105  * @cp: The start of the string
106  * @endp: A pointer to the end of the parsed string will be placed here
107  * @base: The number base to use
108  */
109 
110 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
111 {
112 	unsigned long long result = 0;
113 
114 	if (!base)
115 		base = simple_guess_base(cp);
116 
117 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
118 		cp += 2;
119 
120 	while (isxdigit(*cp)) {
121 		unsigned int value;
122 
123 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
124 		if (value >= base)
125 			break;
126 		result = result * base + value;
127 		cp++;
128 	}
129 	if (endp)
130 		*endp = (char *)cp;
131 
132 	return result;
133 }
134 
135 long simple_strtol(const char *cp, char **endp, unsigned int base)
136 {
137 	if (*cp == '-')
138 		return -simple_strtoull(cp + 1, endp, base);
139 
140 	return simple_strtoull(cp, endp, base);
141 }
142 
143 /**
144  * strlen - Find the length of a string
145  * @s: The string to be sized
146  */
147 size_t strlen(const char *s)
148 {
149 	const char *sc;
150 
151 	for (sc = s; *sc != '\0'; ++sc)
152 		/* nothing */;
153 	return sc - s;
154 }
155 
156 /**
157  * strstr - Find the first substring in a %NUL terminated string
158  * @s1: The string to be searched
159  * @s2: The string to search for
160  */
161 char *strstr(const char *s1, const char *s2)
162 {
163 	size_t l1, l2;
164 
165 	l2 = strlen(s2);
166 	if (!l2)
167 		return (char *)s1;
168 	l1 = strlen(s1);
169 	while (l1 >= l2) {
170 		l1--;
171 		if (!memcmp(s1, s2, l2))
172 			return (char *)s1;
173 		s1++;
174 	}
175 	return NULL;
176 }
177 
178 /**
179  * strchr - Find the first occurrence of the character c in the string s.
180  * @s: the string to be searched
181  * @c: the character to search for
182  */
183 char *strchr(const char *s, int c)
184 {
185 	while (*s != (char)c)
186 		if (*s++ == '\0')
187 			return NULL;
188 	return (char *)s;
189 }
190