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