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 | /* NOTE: Moved from ntfs.c */␊ |
142 | int␊ |
143 | memcmp(const void *p1, const void *p2, size_t len)␊ |
144 | {␊ |
145 | while (len--) {␊ |
146 | if (*(const char*)(p1++) != *(const char*)(p2++))␊ |
147 | return -1;␊ |
148 | }␊ |
149 | return 0;␊ |
150 | }␊ |
151 | ␊ |
152 | int␊ |
153 | strcmp(const char * s1, const char * s2)␊ |
154 | {␊ |
155 | ␉while (*s1 && (*s1 == *s2)) {␊ |
156 | ␉␉s1++;␊ |
157 | ␉␉s2++;␊ |
158 | ␉}␊ |
159 | ␉return (*s1 - *s2);␊ |
160 | }␊ |
161 | ␊ |
162 | /* Derived from FreeBSD source */␊ |
163 | int strncmp(const char * s1, const char * s2, size_t n)␊ |
164 | {␊ |
165 | ␉if (!n)␊ |
166 | ␉␉return 0;␊ |
167 | ␉do {␊ |
168 | ␉␉if (*s1 != *s2++)␊ |
169 | ␉␉{␊ |
170 | ␉␉␉return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));␊ |
171 | ␉␉}␊ |
172 | ␊ |
173 | ␉␉if (!*s1++)␊ |
174 | ␉␉{␊ |
175 | ␉␉␉break;␊ |
176 | ␉␉}␊ |
177 | ␉} while (--n);␊ |
178 | ␉return 0;␊ |
179 | }␊ |
180 | ␊ |
181 | char *␊ |
182 | strcpy(char * s1, const char * s2)␊ |
183 | {␊ |
184 | ␉register char *ret = s1;␊ |
185 | ␉while ((*s1++ = *s2++))␊ |
186 | ␉␉continue;␊ |
187 | ␉return ret;␊ |
188 | }␊ |
189 | ␊ |
190 | char *␊ |
191 | stpcpy(char * s1, const char * s2)␊ |
192 | {␊ |
193 | ␉while ((*s1++ = *s2++))␊ |
194 | ␉{␊ |
195 | ␉␉continue;␊ |
196 | ␉}␊ |
197 | ␉return --s1;␊ |
198 | }␊ |
199 | ␊ |
200 | char *␊ |
201 | strncpy(char * s1, const char * s2, size_t n)␊ |
202 | {␊ |
203 | ␉register char *ret = s1;␊ |
204 | ␉while (n && (*s1++ = *s2++))␊ |
205 | ␉--n;␊ |
206 | ␊ |
207 | ␉if (n > 0) {␊ |
208 | ␉␉bzero(s1, n);␊ |
209 | ␉}␊ |
210 | ␉return ret;␊ |
211 | }␊ |
212 | ␊ |
213 | char *␊ |
214 | stpncpy(char * s1, const char * s2, size_t n)␊ |
215 | {␊ |
216 | ␉while (n && (*s1++ = *s2++))␊ |
217 | ␉␉--n;␊ |
218 | ␉if (n > 0)␊ |
219 | ␉{␊ |
220 | ␉␉bzero(s1, n);␊ |
221 | ␉}␊ |
222 | ␉return s1;␊ |
223 | }␊ |
224 | ␊ |
225 | char *␊ |
226 | strstr(const char *in, const char *str)␊ |
227 | {␊ |
228 | char c;␊ |
229 | size_t len;␊ |
230 | ␊ |
231 | c = *str++;␊ |
232 | if (!c)␊ |
233 | return (char *) in;␉// Trivial empty string case␊ |
234 | ␊ |
235 | len = strlen(str);␊ |
236 | do {␊ |
237 | char sc;␊ |
238 | ␊ |
239 | do {␊ |
240 | sc = *in++;␊ |
241 | if (!sc)␊ |
242 | return (char *) 0;␊ |
243 | } while (sc != c);␊ |
244 | } while (strncmp(in, str, len) != 0);␊ |
245 | ␊ |
246 | return (char *) (in - 1);␊ |
247 | }␊ |
248 | ␊ |
249 | int␊ |
250 | ptol(const char *str)␊ |
251 | {␊ |
252 | ␉register int c = *str;␊ |
253 | ␊ |
254 | ␉if (c <= '7' && c >= '0')␊ |
255 | ␉␉c -= '0';␊ |
256 | ␉else if (c <= 'h' && c >= 'a')␊ |
257 | ␉␉c -= 'a';␊ |
258 | ␉else c = 0;␊ |
259 | ␉return c;␊ |
260 | }␊ |
261 | ␊ |
262 | int␊ |
263 | atoi(const char *str)␊ |
264 | {␊ |
265 | ␉register int sum = 0;␊ |
266 | ␉while (*str == ' ' || *str == '\t')␊ |
267 | ␉␉str++;␊ |
268 | ␉while (*str >= '0' && *str <= '9') {␊ |
269 | ␉␉sum *= 10;␊ |
270 | ␉␉sum += *str++ - '0';␊ |
271 | ␉}␊ |
272 | ␉return sum;␊ |
273 | }␊ |
274 | ␊ |
275 | char *strncat(char *s1, const char *s2, size_t n)␊ |
276 | {␊ |
277 | ␉register char *ret = s1;␊ |
278 | ␉while (*s1)␊ |
279 | ␉␉s1++;␊ |
280 | ␉while (n-- && (*s1++ = *s2++));␊ |
281 | ␉return ret;␊ |
282 | }␊ |
283 | ␊ |
284 | char *strcat(char *s1, const char *s2)␊ |
285 | {␊ |
286 | ␉register char *ret = s1;␊ |
287 | ␉while (*s1)␊ |
288 | ␉{␊ |
289 | ␉␉s1++;␊ |
290 | ␉}␊ |
291 | ␉while ((*s1++ = *s2++));␊ |
292 | ␉return ret;␊ |
293 | }␊ |
294 | ␊ |
295 | char *strdup(const char *s1)␊ |
296 | {␊ |
297 | ␉return strcpy(malloc(strlen(s1) + 1), s1);␊ |
298 | }␊ |
299 | ␊ |
300 | #if STRNCASECMP␊ |
301 | int strncasecmp(const char *s1, const char *s2, size_t len)␊ |
302 | {␊ |
303 | ␉register int n = len;␊ |
304 | ␉while (--n >= 0 && tolower(*s1) == tolower(*s2++))␊ |
305 | ␉␉if (*s1++ == '\0')␊ |
306 | ␉␉␉return(0);␊ |
307 | ␉return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));␊ |
308 | }␊ |
309 | #endif␊ |
310 | ␊ |
311 | char* strchr(const char *str, int c)␊ |
312 | {␊ |
313 | do␊ |
314 | {␊ |
315 | if(*str == c)␊ |
316 | return (char*)str;␊ |
317 | }␊ |
318 | while(*(str++));␊ |
319 | ␊ |
320 | return 0;␊ |
321 | } ␊ |
322 | ␊ |
323 | char* strbreak(const char *str, char **next, long *len)␊ |
324 | {␊ |
325 | char *start = (char*)str, *end;␊ |
326 | bool quoted = false;␊ |
327 | ␊ |
328 | if ( !start || !len )␊ |
329 | return 0;␊ |
330 | ␊ |
331 | *len = 0;␊ |
332 | ␊ |
333 | while ( isspace(*start) )␊ |
334 | start++;␊ |
335 | ␊ |
336 | if (*start == '"')␊ |
337 | {␊ |
338 | start++;␊ |
339 | ␊ |
340 | end = strchr(start, '"');␊ |
341 | if(end)␊ |
342 | quoted = true;␊ |
343 | else␊ |
344 | end = strchr(start, '\0');␊ |
345 | }␊ |
346 | else␊ |
347 | {␊ |
348 | for ( end = start; *end && !isspace(*end); end++ )␊ |
349 | {}␊ |
350 | }␊ |
351 | ␊ |
352 | *len = end - start;␊ |
353 | ␊ |
354 | if(next)␊ |
355 | *next = quoted ? end+1 : end;␊ |
356 | ␊ |
357 | return start;␊ |
358 | }␊ |
359 | ␊ |
360 | /* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */␊ |
361 | uint8_t checksum8( void * start, unsigned int length )␊ |
362 | {␊ |
363 | uint8_t csum = 0;␊ |
364 | uint8_t * cp = (uint8_t *) start;␊ |
365 | unsigned int i;␊ |
366 | ␊ |
367 | ␉for ( i = 0; i < length; i++) {␊ |
368 | ␉␉csum += *cp++;␊ |
369 | ␉}␊ |
370 | ␉return csum;␊ |
371 | }␊ |
372 | ␊ |
373 | |