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 | #else␊ |
60 | void * memcpy(void * dst, const void * src, size_t len)␊ |
61 | {␊ |
62 | ␉asm volatile ( "cld \n\t"␊ |
63 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
64 | ␉␉"shrl $2, %%ecx \n\t"␊ |
65 | ␉␉"rep; movsl \n\t"␊ |
66 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
67 | ␉␉"andl $3, %%ecx \n\t"␊ |
68 | ␉␉"rep; movsb \n\t"␊ |
69 | ␉␉: "=D" (dst)␊ |
70 | ␉␉: "c" (len), "D" (dst), "S" (src)␊ |
71 | ␉␉: "memory", "%edx" );␊ |
72 | ␊ |
73 | ␉return dst;␊ |
74 | }␊ |
75 | ␊ |
76 | void bcopy(const void * src, void * dst, size_t len)␊ |
77 | {␊ |
78 | ␉asm volatile ( "cld \n\t"␊ |
79 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
80 | ␉␉"shrl $2, %%ecx \n\t"␊ |
81 | ␉␉"rep; movsl \n\t"␊ |
82 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
83 | ␉␉"andl $3, %%ecx \n\t"␊ |
84 | ␉␉"rep; movsb \n\t"␊ |
85 | ␉␉:␊ |
86 | ␉␉: "c" (len), "D" (dst), "S" (src)␊ |
87 | ␉␉: "memory", "%edx" );␊ |
88 | }␊ |
89 | ␊ |
90 | void bzero(void * dst, size_t len)␊ |
91 | {␊ |
92 | ␉asm volatile ( "xorl %%eax, %%eax \n\t"␊ |
93 | ␉␉"cld \n\t"␊ |
94 | ␉␉"movl %%ecx, %%edx \n\t"␊ |
95 | ␉␉"shrl $2, %%ecx \n\t"␊ |
96 | ␉␉"rep; stosl \n\t"␊ |
97 | ␉␉"movl %%edx, %%ecx \n\t"␊ |
98 | ␉␉"andl $3, %%ecx \n\t"␊ |
99 | ␉␉"rep; stosb \n\t"␊ |
100 | ␉␉:␊ |
101 | ␉␉: "c" (len), "D" (dst)␊ |
102 | ␉␉: "memory", "%eax" );␊ |
103 | }␊ |
104 | #endif␊ |
105 | ␊ |
106 | /* #if DONT_USE_GCC_BUILT_IN_STRLEN */␊ |
107 | ␊ |
108 | #define tolower(c) ((int)((c) & ~0x20))␊ |
109 | #define toupper(c) ((int)((c) | 0x20))␊ |
110 | ␊ |
111 | size_t strlen(const char * s)␊ |
112 | {␊ |
113 | ␉const char* save = s;␊ |
114 | ␉while (*s++);␊ |
115 | ␉return (--s) - save;␊ |
116 | }␊ |
117 | ␊ |
118 | /*#endif*/␊ |
119 | ␊ |
120 | ␊ |
121 | //==========================================================================␊ |
122 | /* NOTE: Moved from ntfs.c */␊ |
123 | int memcmp(const void *p1, const void *p2, size_t len)␊ |
124 | {␊ |
125 | ␉while (len--)␊ |
126 | ␉{␊ |
127 | ␉␉if (*(const char*)(p1++) != *(const char*)(p2++))␊ |
128 | ␉␉{␊ |
129 | ␉␉␉return -1;␊ |
130 | ␉␉}␊ |
131 | ␉}␊ |
132 | return 0;␊ |
133 | }␊ |
134 | ␊ |
135 | ␊ |
136 | //==========================================================================␊ |
137 | ␊ |
138 | int strcmp(const char * s1, const char * s2)␊ |
139 | {␊ |
140 | ␉while (*s1 && (*s1 == *s2))␊ |
141 | ␉{␊ |
142 | ␉␉s1++;␊ |
143 | ␉␉s2++;␊ |
144 | ␉}␊ |
145 | ␊ |
146 | ␉return (*s1 - *s2);␊ |
147 | }␊ |
148 | ␊ |
149 | /* Derived from FreeBSD source */␊ |
150 | int strncmp(const char * s1, const char * s2, size_t n)␊ |
151 | {␊ |
152 | ␉if (!n)␊ |
153 | ␉␉return 0;␊ |
154 | ␉do {␊ |
155 | ␉␉if (*s1 != *s2++)␊ |
156 | ␉␉{␊ |
157 | ␉␉␉return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));␊ |
158 | ␉␉}␊ |
159 | ␊ |
160 | ␉␉if (!*s1++)␊ |
161 | ␉␉{␊ |
162 | ␉␉␉break;␊ |
163 | ␉␉}␊ |
164 | ␉} while (--n);␊ |
165 | ␉return 0;␊ |
166 | }␊ |
167 | ␊ |
168 | char *strcpy(char *s1, const char *s2)␊ |
169 | {␊ |
170 | ␉register char *ret = s1;␊ |
171 | ␉while ((*s1++ = *s2++))␊ |
172 | ␉␉continue;␊ |
173 | ␉return ret;␊ |
174 | }␊ |
175 | ␊ |
176 | char *stpcpy(char *s1, const char *s2)␊ |
177 | {␊ |
178 | ␉while ((*s1++ = *s2++))␊ |
179 | ␉{␊ |
180 | ␉␉continue;␊ |
181 | ␉}␊ |
182 | ␉return --s1;␊ |
183 | }␊ |
184 | ␊ |
185 | char *strncpy(char *s1, const char *s2, size_t n)␊ |
186 | {␊ |
187 | ␉register char *ret = s1;␊ |
188 | ␉while (n && (*s1++ = *s2++))␊ |
189 | ␉--n;␊ |
190 | ␊ |
191 | ␉if (n > 0) {␊ |
192 | ␉␉bzero(s1, n);␊ |
193 | ␉}␊ |
194 | ␉return ret;␊ |
195 | }␊ |
196 | ␊ |
197 | char *stpncpy(char *s1, const char *s2, size_t n)␊ |
198 | {␊ |
199 | ␉while (n && (*s1++ = *s2++))␊ |
200 | ␉␉--n;␊ |
201 | ␉if (n > 0)␊ |
202 | ␉{␊ |
203 | ␉␉bzero(s1, n);␊ |
204 | ␉}␊ |
205 | ␉return s1;␊ |
206 | }␊ |
207 | ␊ |
208 | char *strstr(const char *in, const char *str)␊ |
209 | {␊ |
210 | char c;␊ |
211 | size_t len;␊ |
212 | ␊ |
213 | c = *str++;␊ |
214 | if (!c)␊ |
215 | return (char *) in;␉// Trivial empty string case␊ |
216 | ␊ |
217 | len = strlen(str);␊ |
218 | do {␊ |
219 | char sc;␊ |
220 | ␊ |
221 | do {␊ |
222 | sc = *in++;␊ |
223 | if (!sc)␊ |
224 | return (char *) 0;␊ |
225 | } while (sc != c);␊ |
226 | } while (strncmp(in, str, len) != 0);␊ |
227 | ␊ |
228 | return (char *) (in - 1);␊ |
229 | }␊ |
230 | ␊ |
231 | ␊ |
232 | //==========================================================================␊ |
233 | ␊ |
234 | int ptol(const char *str)␊ |
235 | {␊ |
236 | ␉register int c = *str;␊ |
237 | ␊ |
238 | ␉if (c <= '7' && c >= '0')␊ |
239 | ␉{␊ |
240 | ␉␉c -= '0';␊ |
241 | ␉}␊ |
242 | ␉else if (c <= 'h' && c >= 'a')␊ |
243 | ␉{␊ |
244 | ␉␉c -= 'a';␊ |
245 | ␉}␊ |
246 | ␉else␊ |
247 | ␉{␊ |
248 | ␉␉c = 0;␊ |
249 | ␉}␊ |
250 | ␊ |
251 | ␉return c;␊ |
252 | }␊ |
253 | ␊ |
254 | ␊ |
255 | //==========================================================================␊ |
256 | ␊ |
257 | int atoi(const char *str)␊ |
258 | {␊ |
259 | ␉register int sum = 0;␊ |
260 | ␊ |
261 | ␉while (*str == ' ' || *str == '\t')␊ |
262 | ␉{␊ |
263 | ␉␉str++;␊ |
264 | ␉}␊ |
265 | ␊ |
266 | ␉while (*str >= '0' && *str <= '9')␊ |
267 | ␉{␊ |
268 | ␉␉sum *= 10;␊ |
269 | ␉␉sum += *str++ - '0';␊ |
270 | ␉}␊ |
271 | ␉return sum;␊ |
272 | }␊ |
273 | /*␊ |
274 | * Appends src to string dst of size siz (unlike strncat, siz is the␊ |
275 | * full size of dst, not space left). At most siz-1 characters␊ |
276 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)).␊ |
277 | * Returns strlen(src) + MIN(siz, strlen(initial dst)).␊ |
278 | * If retval >= siz, truncation occurred.␊ |
279 | */␊ |
280 | size_t strlcat(char *dst, const char *src, size_t siz)␊ |
281 | {␊ |
282 | ␉char *d = dst;␊ |
283 | ␉const char *s = src;␊ |
284 | ␉size_t n = siz;␊ |
285 | ␉size_t dlen;␊ |
286 | ␊ |
287 | ␉/* Find the end of dst and adjust bytes left but don't go past end */␊ |
288 | ␉while (n-- != 0 && *d != '\0')␊ |
289 | ␉{␊ |
290 | ␉␉d++;␊ |
291 | ␉}␊ |
292 | ␊ |
293 | ␉dlen = d - dst;␊ |
294 | ␉n = siz - dlen;␊ |
295 | ␊ |
296 | ␉if (n == 0)␊ |
297 | ␉{␊ |
298 | ␉␉return(dlen + strlen(s));␊ |
299 | ␉}␊ |
300 | ␊ |
301 | ␉while (*s != '\0')␊ |
302 | ␉{␊ |
303 | ␉␉if (n != 1)␊ |
304 | ␉␉{␊ |
305 | ␉␉␉*d++ = *s;␊ |
306 | ␉␉␉n--;␊ |
307 | ␉␉}␊ |
308 | ␉␉s++;␊ |
309 | ␉}␊ |
310 | ␉*d = '\0';␊ |
311 | ␊ |
312 | ␉return(dlen + (s - src)); /* count does not include NUL */␊ |
313 | }␊ |
314 | ␊ |
315 | char *strncat(char *s1, const char *s2, size_t n)␊ |
316 | {␊ |
317 | ␉register char *ret = s1;␊ |
318 | ␉while (*s1)␊ |
319 | ␉␉s1++;␊ |
320 | ␉while (n-- && (*s1++ = *s2++));␊ |
321 | ␉return ret;␊ |
322 | }␊ |
323 | ␊ |
324 | char *strcat(char *s1, const char *s2)␊ |
325 | {␊ |
326 | ␉register char *ret = s1;␊ |
327 | ␉while (*s1)␊ |
328 | ␉{␊ |
329 | ␉␉s1++;␊ |
330 | ␉}␊ |
331 | ␉while ((*s1++ = *s2++));␊ |
332 | ␉return ret;␊ |
333 | }␊ |
334 | ␊ |
335 | char *strdup(const char *s1)␊ |
336 | {␊ |
337 | ␉return strcpy(malloc(strlen(s1) + 1), s1);␊ |
338 | }␊ |
339 | ␊ |
340 | #if STRNCASECMP␊ |
341 | int strncasecmp(const char *s1, const char *s2, size_t len)␊ |
342 | {␊ |
343 | ␉register int n = len;␊ |
344 | ␉while (--n >= 0 && tolower(*s1) == tolower(*s2++))␊ |
345 | ␉␉if (*s1++ == '\0')␊ |
346 | ␉␉␉return(0);␊ |
347 | ␉return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));␊ |
348 | }␊ |
349 | #endif␊ |
350 | ␊ |
351 | char *strchr(const char *str, int c)␊ |
352 | {␊ |
353 | do␊ |
354 | {␊ |
355 | if(*str == c)␊ |
356 | return (char*)str;␊ |
357 | }␊ |
358 | while(*(str++));␊ |
359 | ␊ |
360 | return 0;␊ |
361 | }␊ |
362 | ␊ |
363 | char *strbreak(const char *str, char **next, long *len)␊ |
364 | {␊ |
365 | char *start = (char*)str, *end;␊ |
366 | bool quoted = false;␊ |
367 | ␊ |
368 | if ( !start || !len )␊ |
369 | return 0;␊ |
370 | ␊ |
371 | *len = 0;␊ |
372 | ␊ |
373 | while ( isspace(*start) )␊ |
374 | start++;␊ |
375 | ␊ |
376 | if (*start == '"')␊ |
377 | {␊ |
378 | start++;␊ |
379 | ␊ |
380 | end = strchr(start, '"');␊ |
381 | if(end)␊ |
382 | quoted = true;␊ |
383 | else␊ |
384 | end = strchr(start, '\0');␊ |
385 | }␊ |
386 | else␊ |
387 | {␊ |
388 | for ( end = start; *end && !isspace(*end); end++ )␊ |
389 | {}␊ |
390 | }␊ |
391 | ␊ |
392 | *len = end - start;␊ |
393 | ␊ |
394 | if(next)␊ |
395 | *next = quoted ? end+1 : end;␊ |
396 | ␊ |
397 | return start;␊ |
398 | }␊ |
399 | ␊ |
400 | /* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */␊ |
401 | uint8_t checksum8( void *start, unsigned int length )␊ |
402 | {␊ |
403 | ␉uint8_t␉csum = 0;␊ |
404 | ␉uint8_t␉*cp = (uint8_t *) start;␊ |
405 | ␉unsigned int i;␊ |
406 | ␊ |
407 | ␉for ( i = 0; i < length; i++)␊ |
408 | ␉{␊ |
409 | ␉␉csum += *cp++;␊ |
410 | ␉}␊ |
411 | ␉return csum;␊ |
412 | }␊ |
413 | ␊ |
414 | |