3 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) 2010 coresystems GmbH
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <libpayload.h>
39 * Compare two strings.
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.
47 int strcasecmp(const char *s1
, const char *s2
)
51 for (size_t i
= 0; 1; i
++) {
52 res
= tolower(s1
[i
]) - tolower(s2
[i
]);
53 if (res
|| (s1
[i
] == '\0'))
61 * Compare two strings with fixed length.
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.
68 int strncasecmp(const char *s1
, const char *s2
, size_t maxlen
)
72 for (size_t i
= 0; i
< maxlen
; i
++) {
73 res
= tolower(s1
[i
]) - tolower(s2
[i
]);
74 if (res
|| (s1
[i
] == '\0'))
82 * Compare two strings.
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.
90 int strcmp(const char *s1
, const char *s2
)
94 for (size_t i
= 0; 1; i
++) {
96 if (res
|| (s1
[i
] == '\0'))
104 * Compare two strings with fixed length.
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.
111 int strncmp(const char *s1
, const char *s2
, size_t maxlen
)
115 for (size_t i
= 0; i
< maxlen
; i
++) {
117 if (res
|| (s1
[i
] == '\0'))
125 * Copy a string with a maximum length.
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.
132 char *strncpy(char *d
, const char *s
, size_t n
)
134 /* Use +1 to get the NUL terminator. */
135 size_t max
= n
> strlen(s
) + 1 ? strlen(s
) + 1 : n
;
137 for (size_t i
= 0; i
< max
; i
++)
146 * @param d The destination memory.
147 * @param s The source string.
148 * @return A pointer to the destination memory.
150 char *strcpy(char *d
, const char *s
)
152 return strncpy(d
, s
, strlen(s
) + 1);
156 * Concatenates two strings with a maximum length.
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.
163 size_t strlcat(char *d
, const char *s
, size_t n
)
165 size_t sl
= strlen(s
);
166 size_t dl
= strlen(d
);
172 size_t max
= n
> (sl
+ dl
) ? sl
: (n
- dl
- 1);
174 for (size_t i
= 0; i
< max
; i
++)
182 * Find a character in a string.
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.
189 char *strchr(const char *s
, int c
)
193 for (; *p
!= 0; p
++) {
202 * Find a character in a string.
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.
210 char *strrchr(const char *s
, int c
)
212 char *p
= (char *)s
+ strlen(s
);
214 for (; p
>= s
; p
--) {
223 * Duplicate a string.
225 * @param s The string to duplicate.
226 * @return A pointer to the copy of the original string.
228 char *strdup(const char *s
)
230 size_t n
= strlen(s
);
231 char *p
= malloc(n
+ 1);
241 * Duplicate a string with a max length of size
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.
247 char *strndup(const char *s
, size_t size
)
249 size_t n
= strnlen(s
, size
);
250 char *p
= malloc(n
+ 1);
260 * Find a substring within a string.
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.
267 char *strstr(const char *h
, const char *n
)
269 size_t hn
= strlen(h
);
270 size_t nn
= strlen(n
);
275 for (size_t i
= 0; i
<= hn
- nn
; i
++)
276 if (!memcmp(&h
[i
], n
, nn
))
277 return (char *)&h
[i
];
285 * @param stringp reference of the string to separate.
286 * @param delim string containing all delimiters.
287 * @return Token string.
289 char *strsep(char **stringp
, const char *delim
)
293 if (!stringp
|| !*stringp
|| !**stringp
)
296 token
= walk
= *stringp
;
298 /* Walk, search for delimiters */
299 while (*walk
&& !strchr(delim
, *walk
))
313 /* Check that a character is in the valid range for the
317 static int _valid(char ch
, int base
)
319 char end
= (base
> 9) ? '9' : '0' + (base
- 1);
321 /* all bases will be some subset of the 0-9 range */
323 if (ch
>= '0' && ch
<= end
)
326 /* Bases > 11 will also have to match in the a-z range */
329 if (tolower(ch
) >= 'a' &&
330 tolower(ch
) <= 'a' + (base
- 11))
337 /* Return the "value" of the character in the given base */
339 static int _offset(char ch
, int base
)
341 if (ch
>= '0' && ch
<= '9')
344 return 10 + tolower(ch
) - 'a';
348 * Convert the initial portion of a string into a signed int
349 * @param ptr A pointer to the string to convert
350 * @param endptr A pointer to the unconverted part of the string
351 * @param base The base of the number to convert, or 0 for auto
352 * @return A signed integer representation of the string
355 long long int strtoll(const char *orig_ptr
, char **endptr
, int base
)
357 const char *ptr
= orig_ptr
;
360 /* Purge whitespace */
362 for ( ; *ptr
&& isspace(*ptr
); ptr
++);
369 unsigned long long uval
= strtoull(ptr
, endptr
, base
);
371 /* If the whole string is unparseable, endptr should point to start. */
372 if (endptr
&& *endptr
== ptr
)
373 *endptr
= (char *)orig_ptr
;
375 if (uval
> (unsigned long long)LLONG_MAX
+ !!is_negative
)
376 uval
= (unsigned long long)LLONG_MAX
+ !!is_negative
;
384 long int strtol(const char *ptr
, char **endptr
, int base
)
386 long long int val
= strtoll(ptr
, endptr
, base
);
394 long atol(const char *nptr
)
396 return strtol(nptr
, NULL
, 10);
400 * Convert the initial portion of a string into an unsigned int
401 * @param ptr A pointer to the string to convert
402 * @param endptr A pointer to the unconverted part of the string
403 * @param base The base of the number to convert, or 0 for auto
404 * @return An unsigned integer representation of the string
407 unsigned long long int strtoull(const char *ptr
, char **endptr
, int base
)
409 unsigned long long int ret
= 0;
412 *endptr
= (char *) ptr
;
414 /* Purge whitespace */
416 for ( ; *ptr
&& isspace(*ptr
); ptr
++);
421 /* Determine the base */
424 if (ptr
[0] == '0' && (ptr
[1] == 'x' || ptr
[1] == 'X'))
426 else if (ptr
[0] == '0') {
434 /* Base 16 allows the 0x on front - so skip over it */
437 if (ptr
[0] == '0' && (ptr
[1] == 'x' || ptr
[1] == 'X') &&
438 _valid(ptr
[2], base
))
442 for ( ; *ptr
&& _valid(*ptr
, base
); ptr
++)
443 ret
= (ret
* base
) + _offset(*ptr
, base
);
446 *endptr
= (char *) ptr
;
451 unsigned long int strtoul(const char *ptr
, char **endptr
, int base
)
453 unsigned long long val
= strtoull(ptr
, endptr
, base
);
460 * Determine the number of leading characters in s that match characters in a
461 * @param s A pointer to the string to analyse
462 * @param a A pointer to an array of characters that match the prefix
463 * @return The number of matching characters
465 size_t strspn(const char *s
, const char *a
)
468 size_t al
= strlen(a
);
469 for (i
= 0; s
[i
] != 0; i
++) {
471 for (size_t j
= 0; j
< al
; j
++) {
484 * Determine the number of leading characters in s that do not match characters in a
485 * @param s A pointer to the string to analyse
486 * @param a A pointer to an array of characters that do not match the prefix
487 * @return The number of not matching characters
489 size_t strcspn(const char *s
, const char *a
)
492 size_t al
= strlen(a
);
493 for (i
= 0; s
[i
] != 0; i
++) {
495 for (size_t j
= 0; j
< al
; j
++) {
508 * Extract first token in string str that is delimited by a character in tokens.
509 * Destroys str and eliminates the token delimiter.
510 * @param str A pointer to the string to tokenize.
511 * @param delim A pointer to an array of characters that delimit the token
512 * @param ptr A pointer to a string pointer to keep state of the tokenizer
513 * @return Pointer to token
515 char *strtok_r(char *str
, const char *delim
, char **ptr
)
517 /* start new tokenizing job or continue existing one? */
521 /* skip over prefix delimiters */
522 char *start
= str
+ strspn(str
, delim
);
524 if (start
[0] == '\0')
527 /* find first delimiter character */
528 char *end
= start
+ strcspn(start
, delim
);
537 * Extract first token in string str that is delimited by a character in tokens.
538 * Destroys str, eliminates the token delimiter and uses global state.
539 * @param str A pointer to the string to tokenize.
540 * @param delim A pointer to an array of characters that delimit the token
541 * @return Pointer to token
543 char *strtok(char *str
, const char *delim
)
545 static char *strtok_ptr
;
547 return strtok_r(str
, delim
, &strtok_ptr
);
551 * Print error message and error number
552 * @param s Error message to print
554 void perror(const char *s
)
556 printf("%s: %d\n", s
?s
:"(none)", errno
);
560 * Get a message string describing the given error number.
562 * @param errnum The error number to be interpreted
563 * @return A pointer to a string describing the given error number
565 char *strerror(int errnum
)
567 /* Reserve enough space for the string below + INT64_MIN in decimal + \0 */
568 static char errstr
[35];
569 snprintf(errstr
, sizeof(errstr
), "Unknown error %d", errnum
);
574 * Simple routine to convert UTF-16 to ASCII, giving up with ? if too high.
575 * A single code point may convert to ?? if not in the BMP.
576 * @param utf16_string A string encoded in UTF-16LE
577 * @param maxlen Maximum possible length of the string in code points
578 * @return Newly allocated ASCII string
580 char *utf16le_to_ascii(const uint16_t *utf16_string
, size_t maxlen
)
582 char *ascii_string
= xmalloc(maxlen
+ 1); /* +1 for trailing \0 */
583 ascii_string
[maxlen
] = '\0';
584 for (size_t i
= 0; i
< maxlen
; i
++) {
585 uint16_t wchar
= utf16_string
[i
];
586 ascii_string
[i
] = wchar
> 0x7f ? '?' : (char)wchar
;