Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2292