Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/iFabio/Chameleon/i386/libsa/string.c

Source at commit 298 created 12 years 10 months ago.
By ifabio, Latest trunk. Also add the Azi modules (Nvidia,AMD,ATI,Intel), added strings into localizable.strings. preparing the "make dmg".
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
28void * 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
39void * 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
49void bcopy(const void * src, void * dst, size_t len)
50{
51memcpy(dst, src, len);
52}
53
54void bzero(void * dst, size_t len)
55{
56 memset(dst, 0, len);
57}
58
59void __bzero(void * dst, size_t len)
60{
61 memset(dst, 0, len);
62}
63
64#else
65void * 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
81void 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
95void 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
110void __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
132size_t strlen(const char * s)
133{
134int n = 0;
135while (*s++) n++;
136return(n);
137}
138
139/*#endif*/
140
141/* NOTE: Moved from ntfs.c */
142int
143memcmp(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
152int
153strcmp(const char * s1, const char * s2)
154{
155while (*s1 && (*s1 == *s2)) {
156s1++;
157s2++;
158}
159return (*s1 - *s2);
160}
161
162int strncmp(const char * s1, const char * s2, size_t len)
163{
164register int n = len;
165while (--n >= 0 && *s1 == *s2++)
166if (*s1++ == '\0')
167return(0);
168return(n<0 ? 0 : *s1 - *--s2);
169}
170
171char *
172strcpy(char * s1, const char * s2)
173{
174register char *ret = s1;
175while (*s1++ = *s2++)
176continue;
177return ret;
178}
179
180char *
181strncpy(char * s1, const char * s2, size_t n)
182{
183register char *ret = s1;
184while (n && (*s1++ = *s2++))
185n--;
186return ret;
187}
188
189char *
190strlcpy(char * s1, const char * s2, size_t n)
191{
192register char *ret = s1;
193while (n && (*s1++ = *s2++))
194n--;
195if (!n) *--s1=0;
196return ret;
197}
198
199char *
200strstr(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
223int
224ptol(const char *str)
225{
226register int c = *str;
227
228if (c <= '7' && c >= '0')
229c -= '0';
230else if (c <= 'h' && c >= 'a')
231c -= 'a';
232else c = 0;
233return c;
234}
235
236int
237atoi(const char *str)
238{
239register int sum = 0;
240while (*str == ' ' || *str == '\t')
241str++;
242while (*str >= '0' && *str <= '9') {
243sum *= 10;
244sum += *str++ - '0';
245}
246return sum;
247}
248
249char *strncat(char *s1, const char *s2, size_t n)
250{
251register char *ret = s1;
252while (*s1)
253s1++;
254while (n-- && *s2)
255*s1++ = *s2++;
256*s1 = '\0';
257return ret;
258}
259
260char *strcat(char *s1, const char *s2)
261{
262return(strncat(s1, s2, strlen(s2)));
263}
264
265char *strdup(const char *s1)
266{
267return strcpy(malloc(strlen(s1) + 1), s1);
268}
269
270#if STRNCASECMP
271int strncasecmp(const char *s1, const char *s2, size_t len)
272{
273register int n = len;
274while (--n >= 0 && tolower(*s1) == tolower(*s2++))
275if (*s1++ == '\0')
276return(0);
277return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
278}
279#endif
280
281char* strchr(const char *str, int c)
282{
283 do
284 {
285 if(*str == c)
286 return (char*)str;
287 }
288 while(*(str++));
289
290 return 0;
291}
292
293char* strbreak(const char *str, char **next, long *len)
294{
295 char *start = (char*)str, *end;
296 bool quoted = false;
297
298 if ( !start || !len )
299 return 0;
300
301 *len = 0;
302
303 while ( isspace(*start) )
304 start++;
305
306 if (*start == '"')
307 {
308 start++;
309
310 end = strchr(start, '"');
311 if(end)
312 quoted = true;
313 else
314 end = strchr(start, '\0');
315 }
316 else
317 {
318 for ( end = start; *end && !isspace(*end); end++ )
319 {}
320 }
321
322 *len = end - start;
323
324 if(next)
325 *next = quoted ? end+1 : end;
326
327 return start;
328}
329
330/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
331uint8_t checksum8( void * start, unsigned int length )
332{
333 uint8_t csum = 0;
334 uint8_t * cp = (uint8_t *) start;
335 unsigned int i;
336
337 for ( i = 0; i < length; i++)
338 csum += *cp++;
339
340 return csum;
341}
342
343

Archive Download this file

Revision: 298