Root/
Source at commit 1078 created 13 years 1 month ago. By azimutz, Put back message removed by mistake on r1061. | |
---|---|
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 | ␊ |
60 | #else␊ |
61 | void * memcpy(void * dst, const void * src, size_t len)␊ |
62 | {␊ |
63 | asm volatile ( "cld \n\t"␊ |
64 | "movl %%ecx, %%edx \n\t"␊ |
65 | "shrl $2, %%ecx \n\t"␊ |
66 | "rep; movsl \n\t"␊ |
67 | "movl %%edx, %%ecx \n\t"␊ |
68 | "andl $3, %%ecx \n\t"␊ |
69 | "rep; movsb \n\t"␊ |
70 | : "=D" (dst)␊ |
71 | : "c" (len), "D" (dst), "S" (src)␊ |
72 | : "memory", "%edx" );␊ |
73 | ␊ |
74 | return dst;␊ |
75 | }␊ |
76 | ␊ |
77 | void bcopy(const void * src, void * dst, size_t len)␊ |
78 | {␊ |
79 | asm volatile ( "cld \n\t"␊ |
80 | "movl %%ecx, %%edx \n\t"␊ |
81 | "shrl $2, %%ecx \n\t"␊ |
82 | "rep; movsl \n\t"␊ |
83 | "movl %%edx, %%ecx \n\t"␊ |
84 | "andl $3, %%ecx \n\t"␊ |
85 | "rep; movsb \n\t"␊ |
86 | :␊ |
87 | : "c" (len), "D" (dst), "S" (src)␊ |
88 | : "memory", "%edx" );␊ |
89 | }␊ |
90 | ␊ |
91 | void bzero(void * dst, size_t len)␊ |
92 | {␊ |
93 | asm volatile ( "xorl %%eax, %%eax \n\t"␊ |
94 | "cld \n\t"␊ |
95 | "movl %%ecx, %%edx \n\t"␊ |
96 | "shrl $2, %%ecx \n\t"␊ |
97 | "rep; stosl \n\t"␊ |
98 | "movl %%edx, %%ecx \n\t"␊ |
99 | "andl $3, %%ecx \n\t"␊ |
100 | "rep; stosb \n\t"␊ |
101 | : ␊ |
102 | : "c" (len), "D" (dst)␊ |
103 | : "memory", "%eax" );␊ |
104 | }␊ |
105 | #endif␊ |
106 | ␊ |
107 | /* #if DONT_USE_GCC_BUILT_IN_STRLEN */␊ |
108 | ␊ |
109 | #define tolower(c) ((int)((c) & ~0x20))␊ |
110 | #define toupper(c) ((int)((c) | 0x20))␊ |
111 | ␊ |
112 | int strlen(const char * s)␊ |
113 | {␊ |
114 | ␉int n = 0;␊ |
115 | ␉while (*s++) n++;␊ |
116 | ␉return(n);␊ |
117 | }␊ |
118 | ␊ |
119 | /*#endif*/␊ |
120 | ␊ |
121 | /* NOTE: Moved from ntfs.c */␊ |
122 | int␊ |
123 | memcmp(const void *p1, const void *p2, int len)␊ |
124 | {␊ |
125 | while (len--) {␊ |
126 | if (*(const char*)(p1++) != *(const char*)(p2++))␊ |
127 | return -1;␊ |
128 | }␊ |
129 | return 0;␊ |
130 | }␊ |
131 | ␊ |
132 | int␊ |
133 | strcmp(const char * s1, const char * s2)␊ |
134 | {␊ |
135 | ␉while (*s1 && (*s1 == *s2)) {␊ |
136 | ␉␉s1++;␊ |
137 | ␉␉s2++;␊ |
138 | ␉}␊ |
139 | ␉return (*s1 - *s2);␊ |
140 | }␊ |
141 | ␊ |
142 | int strncmp(const char * s1, const char * s2, size_t len)␊ |
143 | {␊ |
144 | ␉register int n = len;␊ |
145 | ␉while (--n >= 0 && *s1 == *s2++)␊ |
146 | ␉␉if (*s1++ == '\0')␊ |
147 | ␉␉␉return(0);␊ |
148 | ␉return(n<0 ? 0 : *s1 - *--s2);␊ |
149 | }␊ |
150 | ␊ |
151 | char *␊ |
152 | strcpy(char * s1, const char * s2)␊ |
153 | {␊ |
154 | ␉register char *ret = s1;␊ |
155 | ␉while (*s1++ = *s2++)␊ |
156 | ␉␉continue;␊ |
157 | ␉return ret;␊ |
158 | }␊ |
159 | ␊ |
160 | char *␊ |
161 | strncpy(char * s1, const char * s2, size_t n)␊ |
162 | {␊ |
163 | ␉register char *ret = s1;␊ |
164 | ␉while (n && (*s1++ = *s2++))␊ |
165 | ␉␉n--;␊ |
166 | ␉return ret;␊ |
167 | }␊ |
168 | ␊ |
169 | char *␊ |
170 | strlcpy(char * s1, const char * s2, size_t n)␊ |
171 | {␊ |
172 | ␉register char *ret = s1;␊ |
173 | ␉while (n && (*s1++ = *s2++))␊ |
174 | ␉␉n--;␊ |
175 | ␉if (!n) *--s1=0;␊ |
176 | ␉return ret;␊ |
177 | }␊ |
178 | ␊ |
179 | char *␊ |
180 | strstr(const char *in, const char *str)␊ |
181 | {␊ |
182 | char c;␊ |
183 | size_t len;␊ |
184 | ␊ |
185 | c = *str++;␊ |
186 | if (!c)␊ |
187 | return (char *) in;␉// Trivial empty string case␊ |
188 | ␊ |
189 | len = strlen(str);␊ |
190 | do {␊ |
191 | char sc;␊ |
192 | ␊ |
193 | do {␊ |
194 | sc = *in++;␊ |
195 | if (!sc)␊ |
196 | return (char *) 0;␊ |
197 | } while (sc != c);␊ |
198 | } while (strncmp(in, str, len) != 0);␊ |
199 | ␊ |
200 | return (char *) (in - 1);␊ |
201 | }␊ |
202 | ␊ |
203 | int␊ |
204 | ptol(const char *str)␊ |
205 | {␊ |
206 | ␉register int c = *str;␊ |
207 | ␊ |
208 | ␉if (c <= '7' && c >= '0')␊ |
209 | ␉␉c -= '0';␊ |
210 | ␉else if (c <= 'h' && c >= 'a')␊ |
211 | ␉␉c -= 'a';␊ |
212 | ␉else c = 0;␊ |
213 | ␉return c;␊ |
214 | }␊ |
215 | ␊ |
216 | int␊ |
217 | atoi(const char *str)␊ |
218 | {␊ |
219 | ␉register int sum = 0;␊ |
220 | ␉while (*str == ' ' || *str == '\t')␊ |
221 | ␉␉str++;␊ |
222 | ␉while (*str >= '0' && *str <= '9') {␊ |
223 | ␉␉sum *= 10;␊ |
224 | ␉␉sum += *str++ - '0';␊ |
225 | ␉}␊ |
226 | ␉return sum;␊ |
227 | }␊ |
228 | ␊ |
229 | char *strncat(char *s1, const char *s2, size_t n)␊ |
230 | {␊ |
231 | ␉register char *ret = s1;␊ |
232 | ␉while (*s1)␊ |
233 | ␉␉s1++;␊ |
234 | ␉while (n-- && *s2)␊ |
235 | ␉␉*s1++ = *s2++;␊ |
236 | ␉*s1 = '\0';␊ |
237 | ␉return ret;␊ |
238 | }␊ |
239 | ␊ |
240 | char *strcat(char *s1, const char *s2)␊ |
241 | {␊ |
242 | ␉return(strncat(s1, s2, strlen(s2)));␊ |
243 | }␊ |
244 | ␊ |
245 | char *strdup(const char *s1)␊ |
246 | {␊ |
247 | ␉return strcpy(malloc(strlen(s1) + 1), s1);␊ |
248 | }␊ |
249 | ␊ |
250 | #if STRNCASECMP␊ |
251 | int strncasecmp(const char *s1, const char *s2, size_t len)␊ |
252 | {␊ |
253 | ␉register int n = len;␊ |
254 | ␉while (--n >= 0 && tolower(*s1) == tolower(*s2++))␊ |
255 | ␉␉if (*s1++ == '\0')␊ |
256 | ␉␉␉return(0);␊ |
257 | ␉return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));␊ |
258 | }␊ |
259 | #endif␊ |
260 | ␊ |
261 | char* strchr(const char *str, int c)␊ |
262 | {␊ |
263 | do␊ |
264 | {␊ |
265 | if(*str == c)␊ |
266 | return (char*)str;␊ |
267 | }␊ |
268 | while(*(str++));␊ |
269 | ␊ |
270 | return 0;␊ |
271 | } ␊ |
272 | ␊ |
273 | char* strbreak(const char *str, char **next, long *len)␊ |
274 | {␊ |
275 | char *start = (char*)str, *end;␊ |
276 | bool quoted = false;␊ |
277 | ␊ |
278 | if ( !start || !len )␊ |
279 | return 0;␊ |
280 | ␊ |
281 | *len = 0;␊ |
282 | ␊ |
283 | while ( isspace(*start) )␊ |
284 | start++;␊ |
285 | ␊ |
286 | if (*start == '"')␊ |
287 | {␊ |
288 | start++;␊ |
289 | ␊ |
290 | end = strchr(start, '"');␊ |
291 | if(end)␊ |
292 | quoted = true;␊ |
293 | else␊ |
294 | end = strchr(start, '\0');␊ |
295 | }␊ |
296 | else␊ |
297 | {␊ |
298 | for ( end = start; *end && !isspace(*end); end++ )␊ |
299 | {}␊ |
300 | }␊ |
301 | ␊ |
302 | *len = end - start;␊ |
303 | ␊ |
304 | if(next)␊ |
305 | *next = quoted ? end+1 : end;␊ |
306 | ␊ |
307 | return start;␊ |
308 | }␊ |
309 | ␊ |
310 | /* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */␊ |
311 | uint8_t checksum8( void * start, unsigned int length )␊ |
312 | {␊ |
313 | uint8_t csum = 0;␊ |
314 | uint8_t * cp = (uint8_t *) start;␊ |
315 | unsigned int i;␊ |
316 | ␊ |
317 | for ( i = 0; i < length; i++)␊ |
318 | csum += *cp++;␊ |
319 | ␊ |
320 | return csum;␊ |
321 | }␊ |
322 | ␊ |
323 |