Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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{
134const char* save = s;
135while (*s++);
136return (--s) - save;
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
162/* Derived from FreeBSD source */
163int strncmp(const char * s1, const char * s2, size_t n)
164{
165if (!n)
166return 0;
167do {
168if (*s1 != *s2++)
169{
170return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
171}
172
173if (!*s1++)
174{
175break;
176}
177} while (--n);
178return 0;
179}
180
181char *
182strcpy(char * s1, const char * s2)
183{
184register char *ret = s1;
185while ((*s1++ = *s2++))
186continue;
187return ret;
188}
189
190char *
191stpcpy(char * s1, const char * s2)
192{
193while ((*s1++ = *s2++))
194{
195continue;
196}
197return --s1;
198}
199
200char *
201strncpy(char * s1, const char * s2, size_t n)
202{
203register char *ret = s1;
204while (n && (*s1++ = *s2++))
205--n;
206
207if (n > 0) {
208bzero(s1, n);
209}
210return ret;
211}
212
213char *
214stpncpy(char * s1, const char * s2, size_t n)
215{
216while (n && (*s1++ = *s2++))
217--n;
218if (n > 0)
219{
220bzero(s1, n);
221}
222return s1;
223}
224
225char *
226strstr(const char *in, const char *str)
227{
228 char c;
229 size_t len;
230
231 c = *str++;
232 if (!c)
233 return (char *) in;// Trivial empty string case
234
235 len = strlen(str);
236 do {
237 char sc;
238
239 do {
240 sc = *in++;
241 if (!sc)
242 return (char *) 0;
243 } while (sc != c);
244 } while (strncmp(in, str, len) != 0);
245
246 return (char *) (in - 1);
247}
248
249int
250ptol(const char *str)
251{
252register int c = *str;
253
254if (c <= '7' && c >= '0')
255c -= '0';
256else if (c <= 'h' && c >= 'a')
257c -= 'a';
258else c = 0;
259return c;
260}
261
262int
263atoi(const char *str)
264{
265register int sum = 0;
266while (*str == ' ' || *str == '\t')
267str++;
268while (*str >= '0' && *str <= '9') {
269sum *= 10;
270sum += *str++ - '0';
271}
272return sum;
273}
274
275char *strncat(char *s1, const char *s2, size_t n)
276{
277register char *ret = s1;
278while (*s1)
279s1++;
280while (n-- && (*s1++ = *s2++));
281return ret;
282}
283
284char *strcat(char *s1, const char *s2)
285{
286register char *ret = s1;
287while (*s1)
288{
289s1++;
290}
291while ((*s1++ = *s2++));
292return ret;
293}
294
295char *strdup(const char *s1)
296{
297return strcpy(malloc(strlen(s1) + 1), s1);
298}
299
300#if STRNCASECMP
301int strncasecmp(const char *s1, const char *s2, size_t len)
302{
303register int n = len;
304while (--n >= 0 && tolower(*s1) == tolower(*s2++))
305if (*s1++ == '\0')
306return(0);
307return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
308}
309#endif
310
311char* strchr(const char *str, int c)
312{
313 do
314 {
315 if(*str == c)
316 return (char*)str;
317 }
318 while(*(str++));
319
320 return 0;
321}
322
323char* strbreak(const char *str, char **next, long *len)
324{
325 char *start = (char*)str, *end;
326 bool quoted = false;
327
328 if ( !start || !len )
329 return 0;
330
331 *len = 0;
332
333 while ( isspace(*start) )
334 start++;
335
336 if (*start == '"')
337 {
338 start++;
339
340 end = strchr(start, '"');
341 if(end)
342 quoted = true;
343 else
344 end = strchr(start, '\0');
345 }
346 else
347 {
348 for ( end = start; *end && !isspace(*end); end++ )
349 {}
350 }
351
352 *len = end - start;
353
354 if(next)
355 *next = quoted ? end+1 : end;
356
357 return start;
358}
359
360/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
361uint8_t checksum8( void * start, unsigned int length )
362{
363 uint8_t csum = 0;
364 uint8_t * cp = (uint8_t *) start;
365 unsigned int i;
366
367for ( i = 0; i < length; i++) {
368csum += *cp++;
369}
370return csum;
371}
372
373

Archive Download this file

Revision: 2458