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