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