Chameleon

Chameleon Svn Source Tree

Root/branches/meklort/i386/libsa/string.c

Source at commit 690 created 13 years 4 months ago.
By meklort, Compiles again. Only tested with Xcode 4.
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
59
60#else
61void * 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
77void 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
91void 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
112int strlen(const char * s)
113{
114int n = 0;
115while (*s++) n++;
116return(n);
117}
118
119/*#endif*/
120
121/* NOTE: Moved from ntfs.c */
122int
123memcmp(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
132int
133strcmp(const char * s1, const char * s2)
134{
135while (*s1 && (*s1 == *s2)) {
136s1++;
137s2++;
138}
139return (*s1 - *s2);
140}
141
142int strncmp(const char * s1, const char * s2, size_t len)
143{
144register int n = len;
145while (--n >= 0 && *s1 == *s2++)
146if (*s1++ == '\0')
147return(0);
148return(n<0 ? 0 : *s1 - *--s2);
149}
150
151char *
152strcpy(char * s1, const char * s2)
153{
154register char *ret = s1;
155while (*s1++ = *s2++)
156continue;
157return ret;
158}
159
160char *
161strncpy(char * s1, const char * s2, size_t n)
162{
163register char *ret = s1;
164while (n && (*s1++ = *s2++))
165n--;
166return ret;
167}
168
169char *
170strlcpy(char * s1, const char * s2, size_t n)
171{
172register char *ret = s1;
173while (n && (*s1++ = *s2++))
174n--;
175if (!n) *--s1=0;
176return ret;
177}
178
179char *
180strstr(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
203int
204ptol(const char *str)
205{
206register int c = *str;
207
208if (c <= '7' && c >= '0')
209c -= '0';
210else if (c <= 'h' && c >= 'a')
211c -= 'a';
212else c = 0;
213return c;
214}
215
216int
217atoi(const char *str)
218{
219register int sum = 0;
220while (*str == ' ' || *str == '\t')
221str++;
222while (*str >= '0' && *str <= '9') {
223sum *= 10;
224sum += *str++ - '0';
225}
226return sum;
227}
228
229char *strncat(char *s1, const char *s2, size_t n)
230{
231register char *ret = s1;
232while (*s1)
233s1++;
234while (n-- && *s2)
235*s1++ = *s2++;
236*s1 = '\0';
237return ret;
238}
239
240char *strcat(char *s1, const char *s2)
241{
242return(strncat(s1, s2, strlen(s2)));
243}
244
245char *strdup(const char *s1)
246{
247return strcpy(malloc(strlen(s1) + 1), s1);
248}
249
250#if STRNCASECMP
251int strncasecmp(const char *s1, const char *s2, size_t len)
252{
253register int n = len;
254while (--n >= 0 && tolower(*s1) == tolower(*s2++))
255if (*s1++ == '\0')
256return(0);
257return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
258}
259#endif
260
261char* 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
273char* 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 */
311uint8_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

Archive Download this file

Revision: 690