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 | ␉int n = 0;␊ |
135 | ␉while (*s++) n++;␊ |
136 | ␉return(n);␊ |
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 | int strncmp(const char * s1, const char * s2, size_t len)␊ |
163 | {␊ |
164 | ␉register int n = len;␊ |
165 | ␉while (--n >= 0 && *s1 == *s2++)␊ |
166 | ␉␉if (*s1++ == '\0')␊ |
167 | ␉␉␉return(0);␊ |
168 | ␉return(n<0 ? 0 : *s1 - *--s2);␊ |
169 | }␊ |
170 | ␊ |
171 | char *␊ |
172 | strcpy(char * s1, const char * s2)␊ |
173 | {␊ |
174 | ␉register char *ret = s1;␊ |
175 | ␉while (*s1++ = *s2++)␊ |
176 | ␉␉continue;␊ |
177 | ␉return ret;␊ |
178 | }␊ |
179 | ␊ |
180 | char *␊ |
181 | strncpy(char * s1, const char * s2, size_t n)␊ |
182 | {␊ |
183 | ␉register char *ret = s1;␊ |
184 | ␉while (n && (*s1++ = *s2++))␊ |
185 | ␉␉n--;␊ |
186 | ␉return ret;␊ |
187 | }␊ |
188 | ␊ |
189 | size_t␊ |
190 | strlcpy(char * s1, const char * s2, size_t n)␊ |
191 | {␊ |
192 | ␉while (n && (*s1++ = *s2++))␊ |
193 | ␉␉n--;␊ |
194 | ␉if (!n) *--s1=0;␊ |
195 | ␉return strlen(s2);␊ |
196 | }␊ |
197 | ␊ |
198 | char *␊ |
199 | strstr(const char *in, const char *str)␊ |
200 | {␊ |
201 | char c;␊ |
202 | size_t len;␊ |
203 | ␊ |
204 | c = *str++;␊ |
205 | if (!c)␊ |
206 | return (char *) in;␉// Trivial empty string case␊ |
207 | ␊ |
208 | len = strlen(str);␊ |
209 | do {␊ |
210 | char sc;␊ |
211 | ␊ |
212 | do {␊ |
213 | sc = *in++;␊ |
214 | if (!sc)␊ |
215 | return (char *) 0;␊ |
216 | } while (sc != c);␊ |
217 | } while (strncmp(in, str, len) != 0);␊ |
218 | ␊ |
219 | return (char *) (in - 1);␊ |
220 | }␊ |
221 | ␊ |
222 | int␊ |
223 | 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␊ |
236 | atoi(const char *str)␊ |
237 | {␊ |
238 | ␉register int sum = 0;␊ |
239 | ␉while (*str == ' ' || *str == '\t')␊ |
240 | ␉␉str++;␊ |
241 | ␉while (*str >= '0' && *str <= '9') {␊ |
242 | ␉␉sum *= 10;␊ |
243 | ␉␉sum += *str++ - '0';␊ |
244 | ␉}␊ |
245 | ␉return sum;␊ |
246 | }␊ |
247 | ␊ |
248 | char *strncat(char *s1, const char *s2, size_t n)␊ |
249 | {␊ |
250 | ␉register char *ret = s1;␊ |
251 | ␉while (*s1)␊ |
252 | ␉␉s1++;␊ |
253 | ␉while (n-- && *s2)␊ |
254 | ␉␉*s1++ = *s2++;␊ |
255 | ␉*s1 = '\0';␊ |
256 | ␉return ret;␊ |
257 | }␊ |
258 | ␊ |
259 | char *strcat(char *s1, const char *s2)␊ |
260 | {␊ |
261 | ␉return(strncat(s1, s2, strlen(s2)));␊ |
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 | |