f71d1c3af6d2b68bf3126b7d09c3a0bf39789fce
[coreboot.git] / payloads / libpayload / libc / string.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2008 Advanced Micro Devices, Inc.
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 <arch/types.h>
33
34 /**
35  * Calculate the length of a fixed-size string.
36  *
37  * @param str The input string.
38  * @param maxlen Return at most maxlen characters as length of the string.
39  * @return The length of the string, not including the final NUL character.
40  *         The maximum length returned is maxlen.
41  */
42 size_t strnlen(const char *str, size_t maxlen)
43 {
44         size_t len = 0;
45
46         /* NULL and empty strings have length 0. */
47         if (!str)
48                 return 0;
49
50         /* Loop until we find a NUL character, or maxlen is reached. */
51         while ((*str++ != '\0') && (len < maxlen))
52                 len++;
53
54         return len;
55 }
56
57 /**
58  * Calculate the length of a string.
59  *
60  * @param str The input string.
61  * @return The length of the string, not including the final NUL character.
62  */
63 size_t strlen(const char *str)
64 {
65         size_t len = 0;
66
67         /* NULL and empty strings have length 0. */
68         if (!str)
69                 return 0;
70
71         /* Loop until we find a NUL character. */
72         while (*str++ != '\0')
73                 len++;
74
75         return len;
76 }
77
78 /**
79  * Compare two strings.
80  *
81  * @param s1 The first string.
82  * @param s2 The second string.
83  * @return Returns a value less than zero, if s1 is shorter than s2. Returns
84  *         zero, if s1 equals s2. Returns a value greater than zero, if
85  *         s1 is longer than s2.
86  */
87 int strcmp(const char *s1, const char *s2)
88 {
89         char c1, c2;
90
91         /* Set c1 == c2, so that we can enter the while loop. */
92         c1 = 0;
93         c2 = 0;
94
95         /* Compare characters until they differ, or one of the strings ends. */
96         while (c1 == c2) {
97                 /* Read the next character from each string. */
98                 c1 = *s1++;
99                 c2 = *s2++;
100
101                 /* Return something negative (if s1 is shorter than s2), or
102                    zero (if s1 equals s2). */
103                 if (c1 == '\0')
104                         return c1 - c2;
105         }
106
107         /* Return someting positive (if s1 is longer than s2), or zero (if s1
108            and s2 are equal). */
109         return c1 - c2;
110 }
111
112 /**
113  * Compare two strings with fixed length.
114  *
115  * @param s1 The first string.
116  * @param s2 The second string.
117  * @param maxlen Return at most maxlen characters as length of the string.
118  * @return A non-zero value if s1 and s2 differ, or zero if s1 equals s2.
119  */
120 int strncmp(const char *s1, const char *s2, int maxlen)
121 {
122         int i;
123
124         for (i = 0; i < maxlen; i++) {
125                 if (s1[i] != s2[i])
126                         return s1[i] - s2[i];
127         }
128
129         return 0;
130 }
131
132 char *strncpy(char *d, const char *s, int n)
133 {
134         /* use +1 to get the null terminator */
135
136         int max = n > strlen(s) + 1 ? strlen(s) + 1 : n;
137         int i;
138
139         for(i = 0; i < max; i++)
140                 d[i] = (char) s[i];
141
142         return d;
143 }
144
145 char *strcpy(char *d, const char *s)
146 {
147         return strncpy(d, s, strlen(s));
148 }
149
150 char *strncat(char *d, const char *s, int n)
151 {
152         char *p = d + strlen(d);
153         int max = n > strlen(s) ? strlen(s) : n;
154         int i;
155
156         for(i = 0; i < max; i++)
157                 p[i] = s[i];
158
159         p[i] = '\0';
160         return d;
161 }
162
163 char * strchr(const char *s, int c)
164 {
165         char *p = (char *) s;
166
167         for( ; *p != 0; p++) {
168                 if (*p == c)
169                         return p;
170         }
171
172         return NULL;
173 }
174
175
176 char *strdup(const char *s)
177 {
178         int n = strlen(s);
179         char *p = malloc(n);
180
181         if (p != NULL)
182                 strncpy(p, s, n);
183
184         return p;
185 }
186
187 char *strstr(const char *h, const char *n)
188 {
189         int hn = strlen(h);
190         int nn = strlen(n);
191         int i;
192
193         for(i = 0; i <= hn - nn; i++)
194                 if (!strcmp(&h[i], n))
195                         return (char *) &h[i];
196
197         return NULL;
198 }
199
200