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 | ␉␉{␊ |
149 | ␉␉␉return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));␊ |
150 | ␉␉}␊ |
151 | ␊ |
152 | ␉␉if (!*s1++)␊ |
153 | ␉␉{␊ |
154 | ␉␉␉break;␊ |
155 | ␉␉}␊ |
156 | ␉} while (--n);␊ |
157 | ␉return 0;␊ |
158 | }␊ |
159 | ␊ |
160 | char *strcpy(char *s1, const char *s2)␊ |
161 | {␊ |
162 | ␉register char *ret = s1;␊ |
163 | ␉while ((*s1++ = *s2++))␊ |
164 | ␉␉continue;␊ |
165 | ␉return ret;␊ |
166 | }␊ |
167 | ␊ |
168 | char *stpcpy(char *s1, const char *s2)␊ |
169 | {␊ |
170 | ␉while ((*s1++ = *s2++))␊ |
171 | ␉{␊ |
172 | ␉␉continue;␊ |
173 | ␉}␊ |
174 | ␉return --s1;␊ |
175 | }␊ |
176 | ␊ |
177 | char *strncpy(char *s1, const char *s2, size_t n)␊ |
178 | {␊ |
179 | ␉register char *ret = s1;␊ |
180 | ␉while (n && (*s1++ = *s2++))␊ |
181 | ␉--n;␊ |
182 | ␊ |
183 | ␉if (n > 0) {␊ |
184 | ␉␉bzero(s1, n);␊ |
185 | ␉}␊ |
186 | ␉return ret;␊ |
187 | }␊ |
188 | ␊ |
189 | char *stpncpy(char *s1, const char *s2, size_t n)␊ |
190 | {␊ |
191 | ␉while (n && (*s1++ = *s2++))␊ |
192 | ␉␉--n;␊ |
193 | ␉if (n > 0)␊ |
194 | ␉{␊ |
195 | ␉␉bzero(s1, n);␊ |
196 | ␉}␊ |
197 | ␉return s1;␊ |
198 | }␊ |
199 | ␊ |
200 | char *strstr(const char *in, const char *str)␊ |
201 | {␊ |
202 | char c;␊ |
203 | size_t len;␊ |
204 | ␊ |
205 | c = *str++;␊ |
206 | if (!c)␊ |
207 | return (char *) in;␉// Trivial empty string case␊ |
208 | ␊ |
209 | len = strlen(str);␊ |
210 | do {␊ |
211 | char sc;␊ |
212 | ␊ |
213 | do {␊ |
214 | sc = *in++;␊ |
215 | if (!sc)␊ |
216 | return (char *) 0;␊ |
217 | } while (sc != c);␊ |
218 | } while (strncmp(in, str, len) != 0);␊ |
219 | ␊ |
220 | return (char *) (in - 1);␊ |
221 | }␊ |
222 | ␊ |
223 | int ptol(const char *str)␊ |
224 | {␊ |
225 | ␉register int c = *str;␊ |
226 | ␊ |
227 | ␉if (c <= '7' && c >= '0')␊ |
228 | ␉␉c -= '0';␊ |
229 | ␉else if (c <= 'h' && c >= 'a')␊ |
230 | ␉␉c -= 'a';␊ |
231 | ␉else c = 0;␊ |
232 | ␉return c;␊ |
233 | }␊ |
234 | ␊ |
235 | int 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-- && (*s1++ = *s2++));␊ |
253 | ␉return ret;␊ |
254 | }␊ |
255 | ␊ |
256 | char *strcat(char *s1, const char *s2)␊ |
257 | {␊ |
258 | ␉register char *ret = s1;␊ |
259 | ␉while (*s1)␊ |
260 | ␉{␊ |
261 | ␉␉s1++;␊ |
262 | ␉}␊ |
263 | ␉while ((*s1++ = *s2++));␊ |
264 | ␉return ret;␊ |
265 | }␊ |
266 | ␊ |
267 | char *strdup(const char *s1)␊ |
268 | {␊ |
269 | ␉return strcpy(malloc(strlen(s1) + 1), s1);␊ |
270 | }␊ |
271 | ␊ |
272 | #if STRNCASECMP␊ |
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 | ␉{␊ |
341 | ␉␉csum += *cp++;␊ |
342 | ␉}␊ |
343 | ␉return csum;␊ |
344 | }␊ |
345 | ␊ |
346 | |