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 <linux/kernel.h> 17 #include <linux/errno.h> 18 #include <asm/asm.h> 19 #include "ctype.h" 20 #include "string.h" 21 22 #define KSTRTOX_OVERFLOW (1U << 31) 23 24 /* 25 * Undef these macros so that the functions that we provide 26 * here will have the correct names regardless of how string.h 27 * may have chosen to #define them. 28 */ 29 #undef memcpy 30 #undef memset 31 #undef memcmp 32 33 int memcmp(const void *s1, const void *s2, size_t len) 34 { 35 bool diff; 36 asm("repe; cmpsb" CC_SET(nz) 37 : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len)); 38 return diff; 39 } 40 41 int strcmp(const char *str1, const char *str2) 42 { 43 const unsigned char *s1 = (const unsigned char *)str1; 44 const unsigned char *s2 = (const unsigned char *)str2; 45 int delta = 0; 46 47 while (*s1 || *s2) { 48 delta = *s1 - *s2; 49 if (delta) 50 return delta; 51 s1++; 52 s2++; 53 } 54 return 0; 55 } 56 57 int strncmp(const char *cs, const char *ct, size_t count) 58 { 59 unsigned char c1, c2; 60 61 while (count) { 62 c1 = *cs++; 63 c2 = *ct++; 64 if (c1 != c2) 65 return c1 < c2 ? -1 : 1; 66 if (!c1) 67 break; 68 count--; 69 } 70 return 0; 71 } 72 73 size_t strnlen(const char *s, size_t maxlen) 74 { 75 const char *es = s; 76 while (*es && maxlen) { 77 es++; 78 maxlen--; 79 } 80 81 return (es - s); 82 } 83 84 unsigned int atou(const char *s) 85 { 86 unsigned int i = 0; 87 while (isdigit(*s)) 88 i = i * 10 + (*s++ - '0'); 89 return i; 90 } 91 92 /* Works only for digits and letters, but small and fast */ 93 #define TOLOWER(x) ((x) | 0x20) 94 95 static unsigned int simple_guess_base(const char *cp) 96 { 97 if (cp[0] == '0') { 98 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) 99 return 16; 100 else 101 return 8; 102 } else { 103 return 10; 104 } 105 } 106 107 /** 108 * simple_strtoull - convert a string to an unsigned long long 109 * @cp: The start of the string 110 * @endp: A pointer to the end of the parsed string will be placed here 111 * @base: The number base to use 112 */ 113 114 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 115 { 116 unsigned long long result = 0; 117 118 if (!base) 119 base = simple_guess_base(cp); 120 121 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 122 cp += 2; 123 124 while (isxdigit(*cp)) { 125 unsigned int value; 126 127 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 128 if (value >= base) 129 break; 130 result = result * base + value; 131 cp++; 132 } 133 if (endp) 134 *endp = (char *)cp; 135 136 return result; 137 } 138 139 long simple_strtol(const char *cp, char **endp, unsigned int base) 140 { 141 if (*cp == '-') 142 return -simple_strtoull(cp + 1, endp, base); 143 144 return simple_strtoull(cp, endp, base); 145 } 146 147 /** 148 * strlen - Find the length of a string 149 * @s: The string to be sized 150 */ 151 size_t strlen(const char *s) 152 { 153 const char *sc; 154 155 for (sc = s; *sc != '\0'; ++sc) 156 /* nothing */; 157 return sc - s; 158 } 159 160 /** 161 * strstr - Find the first substring in a %NUL terminated string 162 * @s1: The string to be searched 163 * @s2: The string to search for 164 */ 165 char *strstr(const char *s1, const char *s2) 166 { 167 size_t l1, l2; 168 169 l2 = strlen(s2); 170 if (!l2) 171 return (char *)s1; 172 l1 = strlen(s1); 173 while (l1 >= l2) { 174 l1--; 175 if (!memcmp(s1, s2, l2)) 176 return (char *)s1; 177 s1++; 178 } 179 return NULL; 180 } 181 182 /** 183 * strchr - Find the first occurrence of the character c in the string s. 184 * @s: the string to be searched 185 * @c: the character to search for 186 */ 187 char *strchr(const char *s, int c) 188 { 189 while (*s != (char)c) 190 if (*s++ == '\0') 191 return NULL; 192 return (char *)s; 193 } 194 195 static inline u64 __div_u64_rem(u64 dividend, u32 divisor, u32 *remainder) 196 { 197 union { 198 u64 v64; 199 u32 v32[2]; 200 } d = { dividend }; 201 u32 upper; 202 203 upper = d.v32[1]; 204 d.v32[1] = 0; 205 if (upper >= divisor) { 206 d.v32[1] = upper / divisor; 207 upper %= divisor; 208 } 209 asm ("divl %2" : "=a" (d.v32[0]), "=d" (*remainder) : 210 "rm" (divisor), "0" (d.v32[0]), "1" (upper)); 211 return d.v64; 212 } 213 214 static inline u64 __div_u64(u64 dividend, u32 divisor) 215 { 216 u32 remainder; 217 218 return __div_u64_rem(dividend, divisor, &remainder); 219 } 220 221 static inline char _tolower(const char c) 222 { 223 return c | 0x20; 224 } 225 226 static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) 227 { 228 if (*base == 0) { 229 if (s[0] == '0') { 230 if (_tolower(s[1]) == 'x' && isxdigit(s[2])) 231 *base = 16; 232 else 233 *base = 8; 234 } else 235 *base = 10; 236 } 237 if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x') 238 s += 2; 239 return s; 240 } 241 242 /* 243 * Convert non-negative integer string representation in explicitly given radix 244 * to an integer. 245 * Return number of characters consumed maybe or-ed with overflow bit. 246 * If overflow occurs, result integer (incorrect) is still returned. 247 * 248 * Don't you dare use this function. 249 */ 250 static unsigned int _parse_integer(const char *s, 251 unsigned int base, 252 unsigned long long *p) 253 { 254 unsigned long long res; 255 unsigned int rv; 256 257 res = 0; 258 rv = 0; 259 while (1) { 260 unsigned int c = *s; 261 unsigned int lc = c | 0x20; /* don't tolower() this line */ 262 unsigned int val; 263 264 if ('0' <= c && c <= '9') 265 val = c - '0'; 266 else if ('a' <= lc && lc <= 'f') 267 val = lc - 'a' + 10; 268 else 269 break; 270 271 if (val >= base) 272 break; 273 /* 274 * Check for overflow only if we are within range of 275 * it in the max base we support (16) 276 */ 277 if (unlikely(res & (~0ull << 60))) { 278 if (res > __div_u64(ULLONG_MAX - val, base)) 279 rv |= KSTRTOX_OVERFLOW; 280 } 281 res = res * base + val; 282 rv++; 283 s++; 284 } 285 *p = res; 286 return rv; 287 } 288 289 static int _kstrtoull(const char *s, unsigned int base, unsigned long long *res) 290 { 291 unsigned long long _res; 292 unsigned int rv; 293 294 s = _parse_integer_fixup_radix(s, &base); 295 rv = _parse_integer(s, base, &_res); 296 if (rv & KSTRTOX_OVERFLOW) 297 return -ERANGE; 298 if (rv == 0) 299 return -EINVAL; 300 s += rv; 301 if (*s == '\n') 302 s++; 303 if (*s) 304 return -EINVAL; 305 *res = _res; 306 return 0; 307 } 308 309 /** 310 * kstrtoull - convert a string to an unsigned long long 311 * @s: The start of the string. The string must be null-terminated, and may also 312 * include a single newline before its terminating null. The first character 313 * may also be a plus sign, but not a minus sign. 314 * @base: The number base to use. The maximum supported base is 16. If base is 315 * given as 0, then the base of the string is automatically detected with the 316 * conventional semantics - If it begins with 0x the number will be parsed as a 317 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be 318 * parsed as an octal number. Otherwise it will be parsed as a decimal. 319 * @res: Where to write the result of the conversion on success. 320 * 321 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. 322 * Used as a replacement for the obsolete simple_strtoull. Return code must 323 * be checked. 324 */ 325 int kstrtoull(const char *s, unsigned int base, unsigned long long *res) 326 { 327 if (s[0] == '+') 328 s++; 329 return _kstrtoull(s, base, res); 330 } 331