2 * This file is part of the libpayload project.
4 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2008 Advanced Micro Devices, Inc.
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>
34 * Calculate the length of a fixed-size string.
36 * @param str The input string.
37 * @param maxlen Return at most maxlen characters as length of the string.
38 * @return The length of the string, not including the final NUL character.
39 * The maximum length returned is maxlen.
41 size_t strnlen(const char *str, size_t maxlen)
45 /* NULL and empty strings have length 0. */
49 /* Loop until we find a NUL character, or maxlen is reached. */
50 while ((*str++ != '\0') && (len < maxlen))
57 * Calculate the length of a string.
59 * @param str The input string.
60 * @return The length of the string, not including the final NUL character.
62 size_t strlen(const char *str)
66 /* NULL and empty strings have length 0. */
70 /* Loop until we find a NUL character. */
71 while (*str++ != '\0')
78 * Compare two strings.
80 * @param s1 The first string.
81 * @param s2 The second string.
82 * @return Returns a value less than zero, if s1 is shorter than s2. Returns
83 * zero, if s1 equals s2. Returns a value greater than zero, if
84 * s1 is longer than s2.
86 int strcmp(const char *s1, const char *s2)
90 /* Set c1 == c2, so that we can enter the while loop. */
94 /* Compare characters until they differ, or one of the strings ends. */
96 /* Read the next character from each string. */
100 /* Return something negative (if s1 is shorter than s2), or
101 zero (if s1 equals s2). */
106 /* Return something positive (if s1 is longer than s2), or zero (if s1
107 and s2 are equal). */
112 * Compare two strings with fixed length.
114 * @param s1 The first string.
115 * @param s2 The second string.
116 * @param maxlen Return at most maxlen characters as length of the string.
117 * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
119 int strncmp(const char *s1, const char *s2, size_t maxlen)
123 for (i = 0; i < maxlen; i++) {
125 return s1[i] - s2[i];
132 * Copy a string with a maximum length.
134 * @param d The destination memory.
135 * @param s The source string.
136 * @param n Copy at most n characters as length of the string.
137 * @return A pointer to the destination memory.
139 char *strncpy(char *d, const char *s, size_t n)
141 /* Use +1 to get the NUL terminator. */
142 int max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
145 for (i = 0; i < max; i++)
154 * @param d The destination memory.
155 * @param s The source string.
156 * @return A pointer to the destination memory.
158 char *strcpy(char *d, const char *s)
160 return strncpy(d, s, strlen(s) + 1);
164 * Concatenates two strings with a maximum length.
166 * @param d The destination string.
167 * @param s The source string.
168 * @param n Not more than n characters from s will be appended to d.
169 * @return A pointer to the destination string.
171 char *strncat(char *d, const char *s, size_t n)
173 char *p = d + strlen(d);
175 int max = n > sl ? sl : n;
176 // int max = n > strlen(s) ? strlen(s) : n;
179 for (i = 0; i < max; i++)
187 * Concatenates two strings with a maximum length.
189 * @param d The destination string.
190 * @param s The source string.
191 * @param n Not more than n characters from s will be appended to d.
192 * @return A pointer to the destination string.
194 size_t strlcat(char *d, const char *s, size_t n)
200 int max = n > (sl + dl) ? sl : (n - dl - 1);
203 for (i = 0; i < max; i++)
211 * Find a character in a string.
213 * @param s The string.
214 * @param c The character.
215 * @return A pointer to the first occurence of the character in the
216 * string, or NULL if the character was not encountered within the string.
218 char *strchr(const char *s, int c)
222 for (; *p != 0; p++) {
231 * Find a character in a string.
233 * @param s The string.
234 * @param c The character.
235 * @return A pointer to the last occurence of the character in the
236 * string, or NULL if the character was not encountered within the string.
239 char *strrchr(const char *s, int c)
241 char *p = (char *)s + strlen(s);
243 for (; p >= s; p--) {
252 * Duplicate a string.
254 * @param s The string to duplicate.
255 * @return A pointer to the copy of the original string.
257 char *strdup(const char *s)
260 char *p = malloc(n + 1);
270 * Find a substring within a string.
272 * @param h The haystack string.
273 * @param n The needle string (substring).
274 * @return A pointer to the first occurence of the substring in
275 * the string, or NULL if the substring was not encountered within the string.
277 char *strstr(const char *h, const char *n)
283 for (i = 0; i <= hn - nn; i++)
284 if (!memcmp(&h[i], n, nn))
285 return (char *)&h[i];
293 * @param stringp reference of the string to separate.
294 * @param delim string containing all delimiters.
295 * @return Token string.
297 char *strsep(char **stringp, const char *delim)
301 if (!stringp || !*stringp || !**stringp)
304 token = walk = *stringp;
306 /* Walk, search for delimiters */
307 while(*walk && !strchr(delim, *walk))
321 /* Check that a character is in the valid range for the
325 static int _valid(char ch, int base)
327 char end = (base > 9) ? '9' : '0' + (base - 1);
329 /* all bases will be some subset of the 0-9 range */
331 if (ch >= '0' && ch <= end)
334 /* Bases > 11 will also have to match in the a-z range */
337 if (tolower(ch) >= 'a' &&
338 tolower(ch) <= 'a' + (base - 11))
345 /* Return the "value" of the character in the given base */
347 static int _offset(char ch, int base)
349 if (ch >= '0' && ch <= '9')
352 return tolower(ch) - 'a';
356 * Convert the initial portion of a string into an unsigned int
357 * @param ptr A pointer to the string to convert
358 * @param endptr A pointer to the unconverted part of the string
359 * @param base The base of the number to convert, or 0 for auto
360 * @return An unsigned integer representation of the string
363 unsigned int strtoul(const char *ptr, char **endptr, int base)
368 *endptr = (char *) ptr;
370 /* Purge whitespace */
372 for( ; *ptr && isspace(*ptr); ptr++);
377 /* Determine the base */
380 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
382 else if (ptr[0] == '0') {
390 /* Base 16 allows the 0x on front - so skip over it */
393 if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
397 /* If the first character isn't valid, then don't
400 if (!*ptr || !_valid(*ptr, base))
403 for( ; *ptr && _valid(*ptr, base); ptr++)
404 ret = (ret * base) + _offset(*ptr, base);
407 *endptr = (char *) ptr;