xref: /openbmc/linux/arch/x86/boot/string.c (revision b34e08d5)
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 "boot.h"
16 
17 /*
18  * This file gets included in compressed/string.c which might pull in
19  * string_32.h and which in turn maps memcmp to __builtin_memcmp(). Undo
20  * that first.
21  */
22 #undef memcmp
23 int memcmp(const void *s1, const void *s2, size_t len)
24 {
25 	u8 diff;
26 	asm("repe; cmpsb; setnz %0"
27 	    : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
28 	return diff;
29 }
30 
31 int strcmp(const char *str1, const char *str2)
32 {
33 	const unsigned char *s1 = (const unsigned char *)str1;
34 	const unsigned char *s2 = (const unsigned char *)str2;
35 	int delta = 0;
36 
37 	while (*s1 || *s2) {
38 		delta = *s2 - *s1;
39 		if (delta)
40 			return delta;
41 		s1++;
42 		s2++;
43 	}
44 	return 0;
45 }
46 
47 int strncmp(const char *cs, const char *ct, size_t count)
48 {
49 	unsigned char c1, c2;
50 
51 	while (count) {
52 		c1 = *cs++;
53 		c2 = *ct++;
54 		if (c1 != c2)
55 			return c1 < c2 ? -1 : 1;
56 		if (!c1)
57 			break;
58 		count--;
59 	}
60 	return 0;
61 }
62 
63 size_t strnlen(const char *s, size_t maxlen)
64 {
65 	const char *es = s;
66 	while (*es && maxlen) {
67 		es++;
68 		maxlen--;
69 	}
70 
71 	return (es - s);
72 }
73 
74 unsigned int atou(const char *s)
75 {
76 	unsigned int i = 0;
77 	while (isdigit(*s))
78 		i = i * 10 + (*s++ - '0');
79 	return i;
80 }
81 
82 /* Works only for digits and letters, but small and fast */
83 #define TOLOWER(x) ((x) | 0x20)
84 
85 static unsigned int simple_guess_base(const char *cp)
86 {
87 	if (cp[0] == '0') {
88 		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
89 			return 16;
90 		else
91 			return 8;
92 	} else {
93 		return 10;
94 	}
95 }
96 
97 /**
98  * simple_strtoull - convert a string to an unsigned long long
99  * @cp: The start of the string
100  * @endp: A pointer to the end of the parsed string will be placed here
101  * @base: The number base to use
102  */
103 
104 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
105 {
106 	unsigned long long result = 0;
107 
108 	if (!base)
109 		base = simple_guess_base(cp);
110 
111 	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
112 		cp += 2;
113 
114 	while (isxdigit(*cp)) {
115 		unsigned int value;
116 
117 		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
118 		if (value >= base)
119 			break;
120 		result = result * base + value;
121 		cp++;
122 	}
123 	if (endp)
124 		*endp = (char *)cp;
125 
126 	return result;
127 }
128 
129 /**
130  * strlen - Find the length of a string
131  * @s: The string to be sized
132  */
133 size_t strlen(const char *s)
134 {
135 	const char *sc;
136 
137 	for (sc = s; *sc != '\0'; ++sc)
138 		/* nothing */;
139 	return sc - s;
140 }
141 
142 /**
143  * strstr - Find the first substring in a %NUL terminated string
144  * @s1: The string to be searched
145  * @s2: The string to search for
146  */
147 char *strstr(const char *s1, const char *s2)
148 {
149 	size_t l1, l2;
150 
151 	l2 = strlen(s2);
152 	if (!l2)
153 		return (char *)s1;
154 	l1 = strlen(s1);
155 	while (l1 >= l2) {
156 		l1--;
157 		if (!memcmp(s1, s2, l2))
158 			return (char *)s1;
159 		s1++;
160 	}
161 	return NULL;
162 }
163