Debian Codegraph @

payloads/libpayload/libc/string.c

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2007 Uwe Hermann <[email protected]>
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2010 coresystems GmbH
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <libpayload.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <inttypes.h>
35 #include <limits.h>
36 #include <errno.h>
37
38 /**
39 * Compare two strings.
40 *
41 * @param s1 The first string.
42 * @param s2 The second string.
43 * @return Returns a value less than zero, if s1 is shorter than s2. Returns
44 * zero, if s1 equals s2. Returns a value greater than zero, if
45 * s1 is longer than s2.
46 */
47 int strcasecmp(const char *s1, const char *s2)
48 {
49 int res;
50
51 for (size_t i = 0; 1; i++) {
52 res = tolower(s1[i]) - tolower(s2[i]);
53 if (res || (s1[i] == '\0'))
54 break;
55 }
56
57 return res;
58 }
59
60 /**
61 * Compare two strings with fixed length.
62 *
63 * @param s1 The first string.
64 * @param s2 The second string.
65 * @param maxlen Return at most maxlen characters as length of the string.
66 * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
67 */
68 int strncasecmp(const char *s1, const char *s2, size_t maxlen)
69 {
70 int res = 0;
71
72 for (size_t i = 0; i < maxlen; i++) {
73 res = tolower(s1[i]) - tolower(s2[i]);
74 if (res || (s1[i] == '\0'))
75 break;
76 }
77
78 return res;
79 }
80
81 /**
82 * Compare two strings.
83 *
84 * @param s1 The first string.
85 * @param s2 The second string.
86 * @return Returns a value less than zero, if s1 is shorter than s2. Returns
87 * zero, if s1 equals s2. Returns a value greater than zero, if
88 * s1 is longer than s2.
89 */
90 int strcmp(const char *s1, const char *s2)
91 {
92 int res;
93
94 for (size_t i = 0; 1; i++) {
95 res = s1[i] - s2[i];
96 if (res || (s1[i] == '\0'))
97 break;
98 }
99
100 return res;
101 }
102
103 /**
104 * Compare two strings with fixed length.
105 *
106 * @param s1 The first string.
107 * @param s2 The second string.
108 * @param maxlen Return at most maxlen characters as length of the string.
109 * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
110 */
111 int strncmp(const char *s1, const char *s2, size_t maxlen)
112 {
113 int res = 0;
114
115 for (size_t i = 0; i < maxlen; i++) {
116 res = s1[i] - s2[i];
117 if (res || (s1[i] == '\0'))
118 break;
119 }
120
121 return res;
122 }
123
124 /**
125 * Copy a string with a maximum length.
126 *
127 * @param d The destination memory.
128 * @param s The source string.
129 * @param n Copy at most n characters as length of the string.
130 * @return A pointer to the destination memory.
131 */
132 char *strncpy(char *d, const char *s, size_t n)
133 {
134 /* Use +1 to get the NUL terminator. */
135 size_t max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
136
137 for (size_t i = 0; i < max; i++)
138 d[i] = (char)s[i];
139
140 return d;
141 }
142
143 /**
144 * Copy a string.
145 *
146 * @param d The destination memory.
147 * @param s The source string.
148 * @return A pointer to the destination memory.
149 */
150 char *strcpy(char *d, const char *s)
151 {
152 return strncpy(d, s, strlen(s) + 1);
153 }
154
155 /**
156 * Concatenates two strings with a maximum length.
157 *
158 * @param d The destination string.
159 * @param s The source string.
160 * @param n d will have at most n-1 characters (plus NUL) after invocation.
161 * @return The total length of the concatenated string.
162 */
163 size_t strlcat(char *d, const char *s, size_t n)
164 {
165 size_t sl = strlen(s);
166 size_t dl = strlen(d);
167
168 if (n <= dl + 1)
169 return sl + dl;
170
171 char *p = d + dl;
172 size_t max = n > (sl + dl) ? sl : (n - dl - 1);
173
174 for (size_t i = 0; i < max; i++)
175 p[i] = s[i];
176
177 p[max] = '\0';
178 return sl + dl;
179 }
180
181 /**
182 * Find a character in a string.
183 *
184 * @param s The string.
185 * @param c The character.
186 * @return A pointer to the first occurrence of the character in the
187 * string, or NULL if the character was not encountered within the string.
188 */
189 char *strchr(const char *s, int c)
190 {
191 char *p = (char *)s;
192
193 for (; *p != 0; p++) {
194 if (*p == c)
195 return p;
196 }
197
198 return NULL;
199 }
200
201 /**
202 * Find a character in a string.
203 *
204 * @param s The string.
205 * @param c The character.
206 * @return A pointer to the last occurrence of the character in the
207 * string, or NULL if the character was not encountered within the string.
208 */
209
210 char *strrchr(const char *s, int c)
211 {
212 char *p = (char *)s + strlen(s);
213
214 for (; p >= s; p--) {
215 if (*p == c)
216 return p;
217 }
218
219 return NULL;
220 }
221
222 /**
223 * Duplicate a string.
224 *
225 * @param s The string to duplicate.
226 * @return A pointer to the copy of the original string.
227 */
228 char *strdup(const char *s)
229 {
230 size_t n = strlen(s);
231 char *p = malloc(n + 1);
232
233 if (p != NULL) {
234 strncpy(p, s, n);
235 p[n] = 0;
236 }
237 return p;
238 }
239
240 /**
241 * Duplicate a string with a max length of size
242 *
243 * @param s The string to duplicate.
244 * @param size The max length of the string
245 * @return A pointer to the copy of the original string.
246 */
247 char *strndup(const char *s, size_t size)
248 {
249 size_t n = strnlen(s, size);
250 char *p = malloc(n + 1);
251
252 if (p != NULL) {
253 strncpy(p, s, n);
254 p[n] = 0;
255 }
256 return p;
257 }
258
259 /**
260 * Find a substring within a string.
261 *
262 * @param h The haystack string.
263 * @param n The needle string (substring).
264 * @return A pointer to the first occurrence of the substring in
265 * the string, or NULL if the substring was not encountered within the string.
266 */
267 char *strstr(const char *h, const char *n)
268 {
269 size_t hn = strlen(h);
270 size_t nn = strlen(n);
271
272 if (hn < nn)
273 return NULL;
274
275 for (size_t i = 0; i <= hn - nn; i++)
276 if (!memcmp(&h[i], n, nn))
277 return (char *)&h[i];
278
279 return NULL;
280 }
281
282 /**
283 * Separate strings.
284 *
285 * @param stringp reference of the string to separate.
286 * @param delim string containing all delimiters.
287 * @return Token string.
288 */
289 char *strsep(char **stringp, const char *delim)
290 {
291 char *walk, *token;
292
293 if (!stringp || !*stringp)
294 return NULL;
295
296 token = walk = *stringp;
297
298 /* Walk, search for delimiters */
299 while (*walk && !strchr(delim, *walk))
300 walk++;
301
302 if (*walk) {
303 /* NUL terminate */
304 *walk = '\0';
305 *stringp = walk + 1;
306 } else {
307 /* Set to NULL after last token. */
308 *stringp = NULL;
309 }
310
311 return token;
312 }
313
314 /* Check that a character is in the valid range for the
315 given base
316 */
317
318 static int _valid(char ch, int base)
319 {
320 char end = (base > 9) ? '9' : '0' + (base - 1);
321
322 /* all bases will be some subset of the 0-9 range */
323
324 if (ch >= '0' && ch <= end)
325 return 1;
326
327 /* Bases > 11 will also have to match in the a-z range */
328
329 if (base > 11) {
330 if (tolower(ch) >= 'a' &&
331 tolower(ch) <= 'a' + (base - 11))
332 return 1;
333 }
334
335 return 0;
336 }
337
338 /* Return the "value" of the character in the given base */
339
340 static int _offset(char ch, int base)
341 {
342 if (ch >= '0' && ch <= '9')
343 return ch - '0';
344 else
345 return 10 + tolower(ch) - 'a';
346 }
347
348 /**
349 * Convert the initial portion of a string into a signed int
350 * @param ptr A pointer to the string to convert
351 * @param endptr A pointer to the unconverted part of the string
352 * @param base The base of the number to convert, or 0 for auto
353 * @return A signed integer representation of the string
354 */
355
356 long long int strtoll(const char *orig_ptr, char **endptr, int base)
357 {
358 const char *ptr = orig_ptr;
359 int is_negative = 0;
360
361 /* Purge whitespace */
362
363 for ( ; *ptr && isspace(*ptr); ptr++);
364
365 if (ptr[0] == '-') {
366 is_negative = 1;
367 ptr++;
368 }
369
370 unsigned long long uval = strtoull(ptr, endptr, base);
371
372 /* If the whole string is unparseable, endptr should point to start. */
373 if (endptr && *endptr == ptr)
374 *endptr = (char *)orig_ptr;
375
376 if (uval > (unsigned long long)LLONG_MAX + !!is_negative)
377 uval = (unsigned long long)LLONG_MAX + !!is_negative;
378
379 if (is_negative)
380 return -uval;
381 else
382 return uval;
383 }
384
385 long int strtol(const char *ptr, char **endptr, int base)
386 {
387 long long int val = strtoll(ptr, endptr, base);
388 if (val > LONG_MAX)
389 return LONG_MAX;
390 if (val < LONG_MIN)
391 return LONG_MIN;
392 return val;
393 }
394
395 long atol(const char *nptr)
396 {
397 return strtol(nptr, NULL, 10);
398 }
399
400 /**
401 * Convert the initial portion of a string into an unsigned int
402 * @param ptr A pointer to the string to convert
403 * @param endptr A pointer to the unconverted part of the string
404 * @param base The base of the number to convert, or 0 for auto
405 * @return An unsigned integer representation of the string
406 */
407
408 unsigned long long int strtoull(const char *ptr, char **endptr, int base)
409 {
410 unsigned long long int ret = 0;
411
412 if (endptr != NULL)
413 *endptr = (char *) ptr;
414
415 /* Purge whitespace */
416
417 for ( ; *ptr && isspace(*ptr); ptr++);
418
419 if (!*ptr)
420 return 0;
421
422 /* Determine the base */
423
424 if (base == 0) {
425 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
426 base = 16;
427 else if (ptr[0] == '0') {
428 base = 8;
429 ptr++;
430 } else {
431 base = 10;
432 }
433 }
434
435 /* Base 16 allows the 0x on front - so skip over it */
436
437 if (base == 16) {
438 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X') &&
439 _valid(ptr[2], base))
440 ptr += 2;
441 }
442
443 for ( ; *ptr && _valid(*ptr, base); ptr++)
444 ret = (ret * base) + _offset(*ptr, base);
445
446 if (endptr != NULL)
447 *endptr = (char *) ptr;
448
449 return ret;
450 }
451
452 unsigned long int strtoul(const char *ptr, char **endptr, int base)
453 {
454 unsigned long long val = strtoull(ptr, endptr, base);
455 if (val > ULONG_MAX)
456 return ULONG_MAX;
457 return val;
458 }
459
460 /**
461 * Determine the number of leading characters in s that match characters in a
462 * @param s A pointer to the string to analyse
463 * @param a A pointer to an array of characters that match the prefix
464 * @return The number of matching characters
465 */
466 size_t strspn(const char *s, const char *a)
467 {
468 size_t i;
469 size_t al = strlen(a);
470 for (i = 0; s[i] != 0; i++) {
471 int found = 0;
472 for (size_t j = 0; j < al; j++) {
473 if (s[i] == a[j]) {
474 found = 1;
475 break;
476 }
477 }
478 if (!found)
479 break;
480 }
481 return i;
482 }
483
484 /**
485 * Determine the number of leading characters in s that do not match characters in a
486 * @param s A pointer to the string to analyse
487 * @param a A pointer to an array of characters that do not match the prefix
488 * @return The number of not matching characters
489 */
490 size_t strcspn(const char *s, const char *a)
491 {
492 size_t i;
493 size_t al = strlen(a);
494 for (i = 0; s[i] != 0; i++) {
495 int found = 0;
496 for (size_t j = 0; j < al; j++) {
497 if (s[i] == a[j]) {
498 found = 1;
499 break;
500 }
501 }
502 if (found)
503 break;
504 }
505 return i;
506 }
507
508 /**
509 * Extract first token in string str that is delimited by a character in tokens.
510 * Destroys str and eliminates the token delimiter.
511 * @param str A pointer to the string to tokenize.
512 * @param delim A pointer to an array of characters that delimit the token
513 * @param ptr A pointer to a string pointer to keep state of the tokenizer
514 * @return Pointer to token
515 */
516 char *strtok_r(char *str, const char *delim, char **ptr)
517 {
518 /* start new tokenizing job or continue existing one? */
519 if (str == NULL)
520 str = *ptr;
521
522 /* skip over prefix delimiters */
523 char *start = str + strspn(str, delim);
524
525 if (start[0] == '\0')
526 return NULL;
527
528 /* find first delimiter character */
529 char *end = start + strcspn(start, delim);
530 *ptr = end;
531 if (end[0] != '\0')
532 *(*ptr)++ = '\0';
533
534 return start;
535 }
536
537 /**
538 * Extract first token in string str that is delimited by a character in tokens.
539 * Destroys str, eliminates the token delimiter and uses global state.
540 * @param str A pointer to the string to tokenize.
541 * @param delim A pointer to an array of characters that delimit the token
542 * @return Pointer to token
543 */
544 char *strtok(char *str, const char *delim)
545 {
546 static char *strtok_ptr;
547
548 return strtok_r(str, delim, &strtok_ptr);
549 }
550
551 /* errno isn't actually used anywhere other than perror() below, and just here
552 for compatibility with libc-targeting code wanting to incidentally print it. */
553 int errno;
554
555 /**
556 * Print error message and error number
557 * @param s Error message to print
558 */
559 void perror(const char *s)
560 {
561 printf("%s: %d\n", s?s:"(none)", errno);
562 }
563
564 /**
565 * Get a message string describing the given error number.
566 *
567 * @param errnum The error number to be interpreted
568 * @return A pointer to a string describing the given error number
569 */
570 char *strerror(int errnum)
571 {
572 /* Reserve enough space for the string below + INT64_MIN in decimal + \0 */
573 static char errstr[35];
574 snprintf(errstr, sizeof(errstr), "Unknown error %d", errnum);
575 return errstr;
576 }
577
578 /*
579 * Simple routine to convert UTF-16 to ASCII, giving up with ? if too high.
580 * A single code point may convert to ?? if not in the BMP.
581 * @param utf16_string A string encoded in UTF-16LE
582 * @param maxlen Maximum possible length of the string in code points
583 * @return Newly allocated ASCII string
584 */
585 char *utf16le_to_ascii(const uint16_t *utf16_string, size_t maxlen)
586 {
587 char *ascii_string = xmalloc(maxlen + 1); /* +1 for trailing \0 */
588 ascii_string[maxlen] = '\0';
589 for (size_t i = 0; i < maxlen; i++) {
590 uint16_t wchar = utf16_string[i];
591 ascii_string[i] = wchar > 0x7f ? '?' : (char)wchar;
592 }
593 return ascii_string;
594 }