Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/libsa/string.c

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 memcmp(const void *p1, const void *p2, size_t len)
143{
144 while (len--) {
145 if (*(const char*)(p1++) != *(const char*)(p2++))
146 return -1;
147 }
148 return 0;
149}
150
151int strcmp(const char * s1, const char * s2)
152{
153while (*s1 && (*s1 == *s2)) {
154s1++;
155s2++;
156}
157return (*s1 - *s2);
158}
159
160int strncmp(const char * s1, const char * s2, size_t len)
161{
162register int n = len;
163while (--n >= 0 && *s1 == *s2++)
164if (*s1++ == '\0')
165return(0);
166return(n<0 ? 0 : *s1 - *--s2);
167}
168
169char * strcpy(char * s1, const char * s2)
170{
171register char *ret = s1;
172while ((*s1++ = *s2++))
173continue;
174return ret;
175}
176
177char * strncpy(char * s1, const char * s2, size_t n)
178{
179register char *ret = s1;
180while (n && (*s1++ = *s2++))
181n--;
182return ret;
183}
184
185char * strstr(const char *in, const char *str)
186{
187 char c;
188 size_t len;
189
190 c = *str++;
191 if (!c)
192 return (char *) in;// Trivial empty string case
193
194 len = strlen(str);
195 do {
196 char sc;
197
198 do {
199 sc = *in++;
200 if (!sc)
201 return (char *) 0;
202 } while (sc != c);
203 } while (strncmp(in, str, len) != 0);
204
205 return (char *) (in - 1);
206}
207
208int ptol(const char *str)
209{
210register int c = *str;
211
212if (c <= '7' && c >= '0')
213c -= '0';
214else if (c <= 'h' && c >= 'a')
215c -= 'a';
216else c = 0;
217return c;
218}
219
220int atoi(const char *str)
221{
222register int sum = 0;
223while (*str == ' ' || *str == '\t')
224str++;
225while (*str >= '0' && *str <= '9') {
226sum *= 10;
227sum += *str++ - '0';
228}
229return sum;
230}
231
232char *strncat(char *s1, const char *s2, size_t n)
233{
234register char *ret = s1;
235while (*s1)
236s1++;
237while (n-- && *s2)
238*s1++ = *s2++;
239*s1 = '\0';
240return ret;
241}
242
243char *strcat(char *s1, const char *s2)
244{
245return(strncat(s1, s2, strlen(s2)));
246}
247
248char *strdup(const char *s1)
249{
250return strcpy(malloc(strlen(s1) + 1), s1);
251}
252
253#if STRNCASECMP
254int strncasecmp(const char *s1, const char *s2, size_t len)
255{
256register int n = len;
257while (--n >= 0 && tolower(*s1) == tolower(*s2++))
258if (*s1++ == '\0')
259return(0);
260return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
261}
262#endif
263
264char* strchr(const char *str, int c)
265{
266 do
267 {
268 if(*str == c)
269 return (char*)str;
270 }
271 while(*(str++));
272
273 return 0;
274}
275
276char* strbreak(const char *str, char **next, long *len)
277{
278 char *start = (char*)str, *end;
279 bool quoted = false;
280
281 if ( !start || !len )
282 return 0;
283
284 *len = 0;
285
286 while ( isspace(*start) )
287 start++;
288
289 if (*start == '"')
290 {
291 start++;
292
293 end = strchr(start, '"');
294 if(end)
295 quoted = true;
296 else
297 end = strchr(start, '\0');
298 }
299 else
300 {
301 for ( end = start; *end && !isspace(*end); end++ )
302 {}
303 }
304
305 *len = end - start;
306
307 if(next)
308 *next = quoted ? end+1 : end;
309
310 return start;
311}
312
313/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
314uint8_t checksum8( void * start, unsigned int length )
315{
316 uint8_t csum = 0;
317 uint8_t * cp = (uint8_t *) start;
318 unsigned int i;
319
320 for ( i = 0; i < length; i++)
321 csum += *cp++;
322
323 return csum;
324}
325
326

Archive Download this file

Revision: 2037