Rev 1530 | Rev 1653 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1530 | Rev 1538 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
2 | * Copyright (C) 2005 Martin Decky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <string.h> |
29 | #include <string.h> |
30 | #include <unistd.h> |
30 | #include <unistd.h> |
31 | #include <ctype.h> |
31 | #include <ctype.h> |
32 | #include <limits.h> |
32 | #include <limits.h> |
- | 33 | #include <align.h> |
|
33 | 34 | ||
34 | 35 | ||
35 | /* Dummy implementation of mem/ functions */ |
36 | /* Dummy implementation of mem/ functions */ |
36 | 37 | ||
37 | void * memset(void *s, int c, size_t n) |
38 | void * memset(void *s, int c, size_t n) |
38 | { |
39 | { |
39 | char *os = s; |
40 | char *os = s; |
40 | while (n--) |
41 | while (n--) |
41 | *(os++) = c; |
42 | *(os++) = c; |
42 | return s; |
43 | return s; |
43 | } |
44 | } |
44 | 45 | ||
- | 46 | struct along {unsigned long n; } __attribute__ ((packed)); |
|
- | 47 | ||
- | 48 | static void * unaligned_memcpy(void *dst, const void *src, size_t n) |
|
- | 49 | { |
|
- | 50 | int i, j; |
|
- | 51 | struct along *adst = dst; |
|
- | 52 | const struct along *asrc = src; |
|
- | 53 | ||
- | 54 | for (i = 0; i < n/sizeof(unsigned long); i++) |
|
- | 55 | adst[i].n = asrc[i].n; |
|
- | 56 | ||
- | 57 | for (j = 0; j < n%sizeof(unsigned long); j++) |
|
- | 58 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
|
- | 59 | ||
- | 60 | return (char *)src; |
|
- | 61 | } |
|
- | 62 | ||
45 | void * memcpy(void *dst, const void *src, size_t n) |
63 | void * memcpy(void *dst, const void *src, size_t n) |
46 | { |
64 | { |
47 | int i, j; |
65 | int i, j; |
48 | 66 | ||
- | 67 | if (((long)dst & (sizeof(long)-1)) || ((long)src & (sizeof(long)-1))) |
|
- | 68 | return unaligned_memcpy(dst, src, n); |
|
- | 69 | ||
49 | for (i = 0; i < n/sizeof(unsigned long); i++) |
70 | for (i = 0; i < n/sizeof(unsigned long); i++) |
50 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
71 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
51 | 72 | ||
52 | for (j = 0; j < n%sizeof(unsigned long); j++) |
73 | for (j = 0; j < n%sizeof(unsigned long); j++) |
53 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
74 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
54 | 75 | ||
55 | return (char *)src; |
76 | return (char *)src; |
56 | } |
77 | } |
57 | 78 | ||
58 | void * memmove(void *dst, const void *src, size_t n) |
79 | void * memmove(void *dst, const void *src, size_t n) |
59 | { |
80 | { |
60 | int i, j; |
81 | int i, j; |
61 | 82 | ||
62 | if (src > dst) |
83 | if (src > dst) |
63 | return memcpy(dst, src, n); |
84 | return memcpy(dst, src, n); |
64 | 85 | ||
65 | for (j = (n%sizeof(unsigned long))-1; j >= 0; j--) |
86 | for (j = (n%sizeof(unsigned long))-1; j >= 0; j--) |
66 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
87 | ((unsigned char *)(((unsigned long *) dst) + i))[j] = ((unsigned char *)(((unsigned long *) src) + i))[j]; |
67 | 88 | ||
68 | for (i = n/sizeof(unsigned long)-1; i >=0 ; i--) |
89 | for (i = n/sizeof(unsigned long)-1; i >=0 ; i--) |
69 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
90 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
70 | 91 | ||
71 | return (char *)src; |
92 | return (char *)src; |
72 | } |
93 | } |
73 | 94 | ||
74 | 95 | ||
75 | /** Count the number of characters in the string, not including terminating 0. |
96 | /** Count the number of characters in the string, not including terminating 0. |
76 | * @param str string |
97 | * @param str string |
77 | * @return number of characters in string. |
98 | * @return number of characters in string. |
78 | */ |
99 | */ |
79 | size_t strlen(const char *str) |
100 | size_t strlen(const char *str) |
80 | { |
101 | { |
81 | size_t counter = 0; |
102 | size_t counter = 0; |
82 | 103 | ||
83 | while (str[counter] != 0) { |
104 | while (str[counter] != 0) { |
84 | counter++; |
105 | counter++; |
85 | } |
106 | } |
86 | 107 | ||
87 | return counter; |
108 | return counter; |
88 | } |
109 | } |
89 | 110 | ||
90 | int strcmp(const char *a,const char *b) |
111 | int strcmp(const char *a,const char *b) |
91 | { |
112 | { |
92 | int c=0; |
113 | int c=0; |
93 | 114 | ||
94 | while(a[c]&&b[c]&&(!(a[c]-b[c]))) c++; |
115 | while(a[c]&&b[c]&&(!(a[c]-b[c]))) c++; |
95 | 116 | ||
96 | return a[c]-b[c]; |
117 | return a[c]-b[c]; |
97 | 118 | ||
98 | } |
119 | } |
99 | 120 | ||
100 | 121 | ||
101 | 122 | ||
102 | /** Return pointer to the first occurence of character c in string |
123 | /** Return pointer to the first occurence of character c in string |
103 | * @param str scanned string |
124 | * @param str scanned string |
104 | * @param c searched character (taken as one byte) |
125 | * @param c searched character (taken as one byte) |
105 | * @return pointer to the matched character or NULL if it is not found in given string. |
126 | * @return pointer to the matched character or NULL if it is not found in given string. |
106 | */ |
127 | */ |
107 | char *strchr(const char *str, int c) |
128 | char *strchr(const char *str, int c) |
108 | { |
129 | { |
109 | while (*str != '\0') { |
130 | while (*str != '\0') { |
110 | if (*str == (char)c) |
131 | if (*str == (char)c) |
111 | return (char *)str; |
132 | return (char *)str; |
112 | str++; |
133 | str++; |
113 | } |
134 | } |
114 | 135 | ||
115 | return NULL; |
136 | return NULL; |
116 | } |
137 | } |
117 | 138 | ||
118 | /** Return pointer to the last occurence of character c in string |
139 | /** Return pointer to the last occurence of character c in string |
119 | * @param str scanned string |
140 | * @param str scanned string |
120 | * @param c searched character (taken as one byte) |
141 | * @param c searched character (taken as one byte) |
121 | * @return pointer to the matched character or NULL if it is not found in given string. |
142 | * @return pointer to the matched character or NULL if it is not found in given string. |
122 | */ |
143 | */ |
123 | char *strrchr(const char *str, int c) |
144 | char *strrchr(const char *str, int c) |
124 | { |
145 | { |
125 | char *retval = NULL; |
146 | char *retval = NULL; |
126 | 147 | ||
127 | while (*str != '\0') { |
148 | while (*str != '\0') { |
128 | if (*str == (char)c) |
149 | if (*str == (char)c) |
129 | retval = (char *)str; |
150 | retval = (char *)str; |
130 | str++; |
151 | str++; |
131 | } |
152 | } |
132 | 153 | ||
133 | return (char *)retval; |
154 | return (char *)retval; |
134 | } |
155 | } |
135 | 156 | ||
136 | /** Convert string to a number. |
157 | /** Convert string to a number. |
137 | * Core of strtol and strtoul functions. |
158 | * Core of strtol and strtoul functions. |
138 | * @param nptr pointer to string |
159 | * @param nptr pointer to string |
139 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
160 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
140 | * @param base zero or number between 2 and 36 inclusive |
161 | * @param base zero or number between 2 and 36 inclusive |
141 | * @param sgn its set to 1 if minus found |
162 | * @param sgn its set to 1 if minus found |
142 | * @return result of conversion. |
163 | * @return result of conversion. |
143 | */ |
164 | */ |
144 | static unsigned long _strtoul(const char *nptr, char **endptr, int base, char *sgn) |
165 | static unsigned long _strtoul(const char *nptr, char **endptr, int base, char *sgn) |
145 | { |
166 | { |
146 | unsigned char c; |
167 | unsigned char c; |
147 | unsigned long result = 0; |
168 | unsigned long result = 0; |
148 | unsigned long a, b; |
169 | unsigned long a, b; |
149 | const char *str = nptr; |
170 | const char *str = nptr; |
150 | const char *tmpptr; |
171 | const char *tmpptr; |
151 | 172 | ||
152 | while (isspace(*str)) |
173 | while (isspace(*str)) |
153 | str++; |
174 | str++; |
154 | 175 | ||
155 | if (*str == '-') { |
176 | if (*str == '-') { |
156 | *sgn = 1; |
177 | *sgn = 1; |
157 | ++str; |
178 | ++str; |
158 | } else if (*str == '+') |
179 | } else if (*str == '+') |
159 | ++str; |
180 | ++str; |
160 | 181 | ||
161 | if (base) { |
182 | if (base) { |
162 | if ((base == 1) || (base > 36)) { |
183 | if ((base == 1) || (base > 36)) { |
163 | /* FIXME: set errno to EINVAL */ |
184 | /* FIXME: set errno to EINVAL */ |
164 | return 0; |
185 | return 0; |
165 | } |
186 | } |
166 | if ((base == 16) && (*str == '0') && ((str[1] == 'x') || (str[1] == 'X'))) { |
187 | if ((base == 16) && (*str == '0') && ((str[1] == 'x') || (str[1] == 'X'))) { |
167 | str += 2; |
188 | str += 2; |
168 | } |
189 | } |
169 | } else { |
190 | } else { |
170 | base = 10; |
191 | base = 10; |
171 | 192 | ||
172 | if (*str == '0') { |
193 | if (*str == '0') { |
173 | base = 8; |
194 | base = 8; |
174 | if ((str[1] == 'X') || (str[1] == 'x')) { |
195 | if ((str[1] == 'X') || (str[1] == 'x')) { |
175 | base = 16; |
196 | base = 16; |
176 | str += 2; |
197 | str += 2; |
177 | } |
198 | } |
178 | } |
199 | } |
179 | } |
200 | } |
180 | 201 | ||
181 | tmpptr = str; |
202 | tmpptr = str; |
182 | 203 | ||
183 | while (*str) { |
204 | while (*str) { |
184 | c = *str; |
205 | c = *str; |
185 | c = ( c >= 'a'? c-'a'+10:(c >= 'A'?c-'A'+10:(c <= '9'?c-'0':0xff))); |
206 | c = ( c >= 'a'? c-'a'+10:(c >= 'A'?c-'A'+10:(c <= '9'?c-'0':0xff))); |
186 | if (c > base) { |
207 | if (c > base) { |
187 | break; |
208 | break; |
188 | } |
209 | } |
189 | 210 | ||
190 | a = (result & 0xff) * base + c; |
211 | a = (result & 0xff) * base + c; |
191 | b = (result >> 8) * base + (a >> 8); |
212 | b = (result >> 8) * base + (a >> 8); |
192 | 213 | ||
193 | if (b > (ULONG_MAX >> 8)) { |
214 | if (b > (ULONG_MAX >> 8)) { |
194 | /* overflow */ |
215 | /* overflow */ |
195 | /* FIXME: errno = ERANGE*/ |
216 | /* FIXME: errno = ERANGE*/ |
196 | return ULONG_MAX; |
217 | return ULONG_MAX; |
197 | } |
218 | } |
198 | 219 | ||
199 | result = (b << 8) + (a & 0xff); |
220 | result = (b << 8) + (a & 0xff); |
200 | ++str; |
221 | ++str; |
201 | } |
222 | } |
202 | 223 | ||
203 | if (str == tmpptr) { |
224 | if (str == tmpptr) { |
204 | /* no number was found => first invalid character is the first character of the string */ |
225 | /* no number was found => first invalid character is the first character of the string */ |
205 | /* FIXME: set errno to EINVAL */ |
226 | /* FIXME: set errno to EINVAL */ |
206 | str = nptr; |
227 | str = nptr; |
207 | result = 0; |
228 | result = 0; |
208 | } |
229 | } |
209 | 230 | ||
210 | if (endptr) |
231 | if (endptr) |
211 | *endptr = (char *)str; |
232 | *endptr = (char *)str; |
212 | 233 | ||
213 | if (nptr == str) { |
234 | if (nptr == str) { |
214 | /*FIXME: errno = EINVAL*/ |
235 | /*FIXME: errno = EINVAL*/ |
215 | return 0; |
236 | return 0; |
216 | } |
237 | } |
217 | 238 | ||
218 | return result; |
239 | return result; |
219 | } |
240 | } |
220 | 241 | ||
221 | /** Convert initial part of string to long int according to given base. |
242 | /** Convert initial part of string to long int according to given base. |
222 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
243 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
223 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one. |
244 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one. |
224 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8). |
245 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8). |
225 | * Otherwise the base 0 is taken as decimal. |
246 | * Otherwise the base 0 is taken as decimal. |
226 | * @param nptr pointer to string |
247 | * @param nptr pointer to string |
227 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
248 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
228 | * @param base zero or number between 2 and 36 inclusive |
249 | * @param base zero or number between 2 and 36 inclusive |
229 | * @return result of conversion. |
250 | * @return result of conversion. |
230 | */ |
251 | */ |
231 | long int strtol(const char *nptr, char **endptr, int base) |
252 | long int strtol(const char *nptr, char **endptr, int base) |
232 | { |
253 | { |
233 | char sgn = 0; |
254 | char sgn = 0; |
234 | unsigned long number = 0; |
255 | unsigned long number = 0; |
235 | 256 | ||
236 | number = _strtoul(nptr, endptr, base, &sgn); |
257 | number = _strtoul(nptr, endptr, base, &sgn); |
237 | 258 | ||
238 | if (number > LONG_MAX) { |
259 | if (number > LONG_MAX) { |
239 | if ((sgn) && (number == (unsigned long)(LONG_MAX) + 1)) { |
260 | if ((sgn) && (number == (unsigned long)(LONG_MAX) + 1)) { |
240 | /* FIXME: set 0 to errno */ |
261 | /* FIXME: set 0 to errno */ |
241 | return number; |
262 | return number; |
242 | } |
263 | } |
243 | /* FIXME: set ERANGE to errno */ |
264 | /* FIXME: set ERANGE to errno */ |
244 | return (sgn?LONG_MIN:LONG_MAX); |
265 | return (sgn?LONG_MIN:LONG_MAX); |
245 | } |
266 | } |
246 | 267 | ||
247 | return (sgn?-number:number); |
268 | return (sgn?-number:number); |
248 | } |
269 | } |
249 | 270 | ||
250 | 271 | ||
251 | /** Convert initial part of string to unsigned long according to given base. |
272 | /** Convert initial part of string to unsigned long according to given base. |
252 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
273 | * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-'). |
253 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one. |
274 | * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one. |
254 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8). |
275 | * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8). |
255 | * Otherwise the base 0 is taken as decimal. |
276 | * Otherwise the base 0 is taken as decimal. |
256 | * @param nptr pointer to string |
277 | * @param nptr pointer to string |
257 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
278 | * @param endptr if not NULL, function stores here pointer to the first invalid character |
258 | * @param base zero or number between 2 and 36 inclusive |
279 | * @param base zero or number between 2 and 36 inclusive |
259 | * @return result of conversion. |
280 | * @return result of conversion. |
260 | */ |
281 | */ |
261 | unsigned long strtoul(const char *nptr, char **endptr, int base) |
282 | unsigned long strtoul(const char *nptr, char **endptr, int base) |
262 | { |
283 | { |
263 | char sgn = 0; |
284 | char sgn = 0; |
264 | unsigned long number = 0; |
285 | unsigned long number = 0; |
265 | 286 | ||
266 | number = _strtoul(nptr, endptr, base, &sgn); |
287 | number = _strtoul(nptr, endptr, base, &sgn); |
267 | 288 | ||
268 | return (sgn?-number:number); |
289 | return (sgn?-number:number); |
269 | } |
290 | } |
270 | 291 | ||
271 | char *strcpy(char *dest, const char *src) |
292 | char *strcpy(char *dest, const char *src) |
272 | { |
293 | { |
273 | while (*(dest++) = *(src++)) |
294 | while (*(dest++) = *(src++)) |
274 | ; |
295 | ; |
275 | } |
296 | } |
276 | 297 | ||
277 | char *strncpy(char *dest, const char *src, size_t n) |
298 | char *strncpy(char *dest, const char *src, size_t n) |
278 | { |
299 | { |
279 | while ((*(dest++) = *(src++)) && --n) |
300 | while ((*(dest++) = *(src++)) && --n) |
280 | ; |
301 | ; |
281 | } |
302 | } |
282 | 303 |