1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | */␊ |
24 | /* string operations */␊ |
25 | ␊ |
26 | #include "libsa.h"␊ |
27 | ␊ |
28 | void *memset(void * dst, int val, size_t len)␊ |
29 | {␊ |
30 | ␉asm volatile ( "rep; stosb"␊ |
31 | ␉␉: "=c" (len), "=D" (dst)␊ |
32 | ␉␉: "0" (len), "1" (dst), "a" (val)␊ |
33 | ␉␉: "memory" );␊ |
34 | ␊ |
35 | ␉return dst;␊ |
36 | }␊ |
37 | ␊ |
38 | #if 0␊ |
39 | void * memcpy(void * dst, const void * src, size_t len)␊ |
40 | {␊ |
41 | ␉asm volatile ( "rep; movsb"␊ |
42 | ␉␉: "=c" (len), "=D" (dst), "=S" (src)␊ |
43 | ␉␉: "0" (len), "1" (dst), "2" (src)␊ |
44 | ␉␉: "memory" );␊ |
45 | ␊ |
46 | ␉return dst;␊ |
47 | }␊ |
48 | ␊ |
49 | void bcopy(const void * src, void * dst, size_t len)␊ |
50 | {␊ |
51 | ␉memcpy(dst, src, len);␊ |
52 | }␊ |
53 | ␊ |
54 | void bzero(void * dst, size_t len)␊ |
55 | {␊ |
56 | ␉memset(dst, 0, len);␊ |
57 | }␊ |
58 | ␊ |
59 | void __bzero(void * dst, size_t len)␊ |
60 | {␊ |
61 | ␉memset(dst, 0, len);␊ |
62 | }␊ |
63 | ␊ |
64 | #else␊ |
65 | void * memcpy(void * dst, const void * src, size_t len)␊ |
66 | {␊ |
67 | ␉asm volatile ( "cld \n\t"␊ |
68 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
69 | ␉␉"shrl $2, %%ecx \n\t"␊ |
70 | ␉␉"rep; movsl \n\t"␊ |
71 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
72 | ␉␉"andl $3, %%ecx \n\t"␊ |
73 | ␉␉"rep; movsb \n\t"␊ |
74 | ␉␉: "=D" (dst)␊ |
75 | ␉␉: "c" (len), "D" (dst), "S" (src)␊ |
76 | ␉␉: "memory", "%edx" );␊ |
77 | ␊ |
78 | ␉return dst;␊ |
79 | }␊ |
80 | ␊ |
81 | void bcopy(const void * src, void * dst, size_t len)␊ |
82 | {␊ |
83 | ␉asm volatile ( "cld \n\t"␊ |
84 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
85 | ␉␉"shrl $2, %%ecx \n\t"␊ |
86 | ␉␉"rep; movsl \n\t"␊ |
87 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
88 | ␉␉"andl $3, %%ecx \n\t"␊ |
89 | ␉␉"rep; movsb \n\t"␊ |
90 | ␉␉:␊ |
91 | ␉␉: "c" (len), "D" (dst), "S" (src)␊ |
92 | ␉␉: "memory", "%edx" );␊ |
93 | }␊ |
94 | ␊ |
95 | void bzero(void * dst, size_t len)␊ |
96 | {␊ |
97 | ␉asm volatile ( "xorl %%eax, %%eax \n\t"␊ |
98 | ␉␉"cld \n\t"␊ |
99 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
100 | ␉␉"shrl $2, %%ecx \n\t"␊ |
101 | ␉␉"rep; stosl \n\t"␊ |
102 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
103 | ␉␉"andl $3, %%ecx \n\t"␊ |
104 | ␉␉"rep; stosb \n\t"␊ |
105 | ␉␉:␊ |
106 | ␉␉: "c" (len), "D" (dst)␊ |
107 | ␉␉: "memory", "%eax" );␊ |
108 | }␊ |
109 | ␊ |
110 | void __bzero(void * dst, size_t len)␊ |
111 | {␊ |
112 | ␉asm volatile ( "xorl %%eax, %%eax \n\t"␊ |
113 | ␉␉"cld \n\t"␊ |
114 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
115 | ␉␉"shrl $2, %%ecx \n\t"␊ |
116 | ␉␉"rep; stosl \n\t"␊ |
117 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
118 | ␉␉"andl $3, %%ecx \n\t"␊ |
119 | ␉␉"rep; stosb \n\t"␊ |
120 | ␉␉:␊ |
121 | ␉␉: "c" (len), "D" (dst)␊ |
122 | ␉␉: "memory", "%eax" );␊ |
123 | }␊ |
124 | ␊ |
125 | #endif␊ |
126 | ␊ |
127 | /* #if DONT_USE_GCC_BUILT_IN_STRLEN */␊ |
128 | ␊ |
129 | #define tolower(c) ((int)((c) & ~0x20))␊ |
130 | #define toupper(c) ((int)((c) | 0x20))␊ |
131 | ␊ |
132 | size_t strlen(const char * s)␊ |
133 | {␊ |
134 | ␉const char* save = s;␊ |
135 | ␉while (*s++);␊ |
136 | ␉return (--s) - save;␊ |
137 | }␊ |
138 | ␊ |
139 | /*#endif*/␊ |
140 | ␊ |
141 | ␊ |
142 | //==========================================================================␊ |
143 | /* NOTE: Moved from ntfs.c */␊ |
144 | int memcmp(const void *p1, const void *p2, size_t len)␊ |
145 | {␊ |
146 | ␉while (len--)␊ |
147 | ␉{␊ |
148 | ␉␉if (*(const char*)(p1++) != *(const char*)(p2++))␊ |
149 | ␉␉{␊ |
150 | ␉␉␉return -1;␊ |
151 | ␉␉}␊ |
152 | ␉}␊ |
153 | return 0;␊ |
154 | }␊ |
155 | ␊ |
156 | ␊ |
157 | //==========================================================================␊ |
158 | ␊ |
159 | int strcmp(const char * s1, const char * s2)␊ |
160 | {␊ |
161 | ␉while (*s1 && (*s1 == *s2))␊ |
162 | ␉{␊ |
163 | ␉␉s1++;␊ |
164 | ␉␉s2++;␊ |
165 | ␉}␊ |
166 | ␊ |
167 | ␉return (*s1 - *s2);␊ |
168 | }␊ |
169 | ␊ |
170 | /* Derived from FreeBSD source */␊ |
171 | int strncmp(const char * s1, const char * s2, size_t n)␊ |
172 | {␊ |
173 | ␉if (!n)␊ |
174 | ␉␉return 0;␊ |
175 | ␉do {␊ |
176 | ␉␉if (*s1 != *s2++)␊ |
177 | ␉␉{␊ |
178 | ␉␉␉return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));␊ |
179 | ␉␉}␊ |
180 | ␊ |
181 | ␉␉if (!*s1++)␊ |
182 | ␉␉{␊ |
183 | ␉␉␉break;␊ |
184 | ␉␉}␊ |
185 | ␉} while (--n);␊ |
186 | ␉return 0;␊ |
187 | }␊ |
188 | ␊ |
189 | char *strcpy(char *s1, const char *s2)␊ |
190 | {␊ |
191 | ␉register char *ret = s1;␊ |
192 | ␉while ((*s1++ = *s2++))␊ |
193 | ␉␉continue;␊ |
194 | ␉return ret;␊ |
195 | }␊ |
196 | ␊ |
197 | char *stpcpy(char *s1, const char *s2)␊ |
198 | {␊ |
199 | ␉while ((*s1++ = *s2++))␊ |
200 | ␉{␊ |
201 | ␉␉continue;␊ |
202 | ␉}␊ |
203 | ␉return --s1;␊ |
204 | }␊ |
205 | ␊ |
206 | char *strncpy(char *s1, const char *s2, size_t n)␊ |
207 | {␊ |
208 | ␉register char *ret = s1;␊ |
209 | ␉while (n && (*s1++ = *s2++))␊ |
210 | ␉--n;␊ |
211 | ␊ |
212 | ␉if (n > 0) {␊ |
213 | ␉␉bzero(s1, n);␊ |
214 | ␉}␊ |
215 | ␉return ret;␊ |
216 | }␊ |
217 | ␊ |
218 | char *stpncpy(char *s1, const char *s2, size_t n)␊ |
219 | {␊ |
220 | ␉while (n && (*s1++ = *s2++))␊ |
221 | ␉␉--n;␊ |
222 | ␉if (n > 0)␊ |
223 | ␉{␊ |
224 | ␉␉bzero(s1, n);␊ |
225 | ␉}␊ |
226 | ␉return s1;␊ |
227 | }␊ |
228 | ␊ |
229 | char *strstr(const char *in, const char *str)␊ |
230 | {␊ |
231 | char c;␊ |
232 | size_t len;␊ |
233 | ␊ |
234 | c = *str++;␊ |
235 | if (!c)␊ |
236 | return (char *) in;␉// Trivial empty string case␊ |
237 | ␊ |
238 | len = strlen(str);␊ |
239 | do {␊ |
240 | char sc;␊ |
241 | ␊ |
242 | do {␊ |
243 | sc = *in++;␊ |
244 | if (!sc)␊ |
245 | return (char *) 0;␊ |
246 | } while (sc != c);␊ |
247 | } while (strncmp(in, str, len) != 0);␊ |
248 | ␊ |
249 | return (char *) (in - 1);␊ |
250 | }␊ |
251 | ␊ |
252 | ␊ |
253 | //==========================================================================␊ |
254 | ␊ |
255 | int ptol(const char *str)␊ |
256 | {␊ |
257 | ␉register int c = *str;␊ |
258 | ␊ |
259 | ␉if (c <= '7' && c >= '0')␊ |
260 | ␉{␊ |
261 | ␉␉c -= '0';␊ |
262 | ␉}␊ |
263 | ␉else if (c <= 'h' && c >= 'a')␊ |
264 | ␉{␊ |
265 | ␉␉c -= 'a';␊ |
266 | ␉}␊ |
267 | ␉else␊ |
268 | ␉{␊ |
269 | ␉␉c = 0;␊ |
270 | ␉}␊ |
271 | ␊ |
272 | ␉return c;␊ |
273 | }␊ |
274 | ␊ |
275 | ␊ |
276 | //==========================================================================␊ |
277 | ␊ |
278 | int atoi(const char *str)␊ |
279 | {␊ |
280 | ␉register int sum = 0;␊ |
281 | ␊ |
282 | ␉while (*str == ' ' || *str == '\t')␊ |
283 | ␉{␊ |
284 | ␉␉str++;␊ |
285 | ␉}␊ |
286 | ␊ |
287 | ␉while (*str >= '0' && *str <= '9')␊ |
288 | ␉{␊ |
289 | ␉␉sum *= 10;␊ |
290 | ␉␉sum += *str++ - '0';␊ |
291 | ␉}␊ |
292 | ␉return sum;␊ |
293 | }␊ |
294 | /*␊ |
295 | * Appends src to string dst of size siz (unlike strncat, siz is the␊ |
296 | * full size of dst, not space left). At most siz-1 characters␊ |
297 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)).␊ |
298 | * Returns strlen(src) + MIN(siz, strlen(initial dst)).␊ |
299 | * If retval >= siz, truncation occurred.␊ |
300 | */␊ |
301 | size_t strlcat(char *dst, const char *src, size_t siz)␊ |
302 | {␊ |
303 | ␉char *d = dst;␊ |
304 | ␉const char *s = src;␊ |
305 | ␉size_t n = siz;␊ |
306 | ␉size_t dlen;␊ |
307 | ␊ |
308 | ␉/* Find the end of dst and adjust bytes left but don't go past end */␊ |
309 | ␉while (n-- != 0 && *d != '\0')␊ |
310 | ␉{␊ |
311 | ␉␉d++;␊ |
312 | ␉}␊ |
313 | ␊ |
314 | ␉dlen = d - dst;␊ |
315 | ␉n = siz - dlen;␊ |
316 | ␊ |
317 | ␉if (n == 0)␊ |
318 | ␉{␊ |
319 | ␉␉return(dlen + strlen(s));␊ |
320 | ␉}␊ |
321 | ␊ |
322 | ␉while (*s != '\0')␊ |
323 | ␉{␊ |
324 | ␉␉if (n != 1)␊ |
325 | ␉␉{␊ |
326 | ␉␉␉*d++ = *s;␊ |
327 | ␉␉␉n--;␊ |
328 | ␉␉}␊ |
329 | ␉␉s++;␊ |
330 | ␉}␊ |
331 | ␉*d = '\0';␊ |
332 | ␊ |
333 | ␉return(dlen + (s - src)); /* count does not include NUL */␊ |
334 | }␊ |
335 | ␊ |
336 | char *strncat(char *s1, const char *s2, size_t n)␊ |
337 | {␊ |
338 | ␉register char *ret = s1;␊ |
339 | ␉while (*s1)␊ |
340 | ␉␉s1++;␊ |
341 | ␉while (n-- && (*s1++ = *s2++));␊ |
342 | ␉return ret;␊ |
343 | }␊ |
344 | ␊ |
345 | char *strcat(char *s1, const char *s2)␊ |
346 | {␊ |
347 | ␉register char *ret = s1;␊ |
348 | ␉while (*s1)␊ |
349 | ␉{␊ |
350 | ␉␉s1++;␊ |
351 | ␉}␊ |
352 | ␉while ((*s1++ = *s2++));␊ |
353 | ␉return ret;␊ |
354 | }␊ |
355 | ␊ |
356 | char *strdup(const char *s1)␊ |
357 | {␊ |
358 | ␉return strcpy(malloc(strlen(s1) + 1), s1);␊ |
359 | }␊ |
360 | ␊ |
361 | #if STRNCASECMP␊ |
362 | int strncasecmp(const char *s1, const char *s2, size_t len)␊ |
363 | {␊ |
364 | ␉register int n = len;␊ |
365 | ␉while (--n >= 0 && tolower(*s1) == tolower(*s2++))␊ |
366 | ␉␉if (*s1++ == '\0')␊ |
367 | ␉␉␉return(0);␊ |
368 | ␉return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));␊ |
369 | }␊ |
370 | #endif␊ |
371 | ␊ |
372 | char *strchr(const char *str, int c)␊ |
373 | {␊ |
374 | do␊ |
375 | {␊ |
376 | if(*str == c)␊ |
377 | return (char*)str;␊ |
378 | }␊ |
379 | while(*(str++));␊ |
380 | ␊ |
381 | return 0;␊ |
382 | }␊ |
383 | ␊ |
384 | char *strbreak(const char *str, char **next, long *len)␊ |
385 | {␊ |
386 | char *start = (char*)str, *end;␊ |
387 | bool quoted = false;␊ |
388 | ␊ |
389 | if ( !start || !len )␊ |
390 | return 0;␊ |
391 | ␊ |
392 | *len = 0;␊ |
393 | ␊ |
394 | while ( isspace(*start) )␊ |
395 | start++;␊ |
396 | ␊ |
397 | if (*start == '"')␊ |
398 | {␊ |
399 | start++;␊ |
400 | ␊ |
401 | end = strchr(start, '"');␊ |
402 | if(end)␊ |
403 | quoted = true;␊ |
404 | else␊ |
405 | end = strchr(start, '\0');␊ |
406 | }␊ |
407 | else␊ |
408 | {␊ |
409 | for ( end = start; *end && !isspace(*end); end++ )␊ |
410 | {}␊ |
411 | }␊ |
412 | ␊ |
413 | *len = end - start;␊ |
414 | ␊ |
415 | if(next)␊ |
416 | *next = quoted ? end+1 : end;␊ |
417 | ␊ |
418 | return start;␊ |
419 | }␊ |
420 | ␊ |
421 | /* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */␊ |
422 | uint8_t checksum8( void *start, unsigned int length )␊ |
423 | {␊ |
424 | ␉uint8_t␉csum = 0;␊ |
425 | ␉uint8_t␉*cp = (uint8_t *) start;␊ |
426 | ␉unsigned int i;␊ |
427 | ␊ |
428 | ␉for ( i = 0; i < length; i++)␊ |
429 | ␉{␊ |
430 | ␉␉csum += *cp++;␊ |
431 | ␉}␊ |
432 | ␉return csum;␊ |
433 | }␊ |
434 | ␊ |
435 | |