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{
30asm volatile ( "rep; stosb"
31: "=c" (len), "=D" (dst)
32: "0" (len), "1" (dst), "a" (val)
33: "memory" );
34
35return dst;
36}
37
38#if 0
39void * memcpy(void * dst, const void * src, size_t len)
40{
41asm volatile ( "rep; movsb"
42: "=c" (len), "=D" (dst), "=S" (src)
43: "0" (len), "1" (dst), "2" (src)
44: "memory" );
45
46return 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{
56memset(dst, 0, len);
57}
58
59void __bzero(void * dst, size_t len)
60{
61memset(dst, 0, len);
62}
63
64#else
65void * memcpy(void * dst, const void * src, size_t len)
66{
67asm 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
78return dst;
79}
80
81void bcopy(const void * src, void * dst, size_t len)
82{
83asm 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{
97asm 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{
112asm 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 *strcpy(char *s1, const char *s2)
182{
183register char *ret = s1;
184while ((*s1++ = *s2++))
185continue;
186return ret;
187}
188
189char *stpcpy(char *s1, const char *s2)
190{
191while ((*s1++ = *s2++))
192{
193continue;
194}
195return --s1;
196}
197
198char *strncpy(char *s1, const char *s2, size_t n)
199{
200register char *ret = s1;
201while (n && (*s1++ = *s2++))
202--n;
203
204if (n > 0) {
205bzero(s1, n);
206}
207return ret;
208}
209
210char *stpncpy(char *s1, const char *s2, size_t n)
211{
212while (n && (*s1++ = *s2++))
213--n;
214if (n > 0)
215{
216bzero(s1, n);
217}
218return s1;
219}
220
221char *strstr(const char *in, const char *str)
222{
223 char c;
224 size_t len;
225
226 c = *str++;
227 if (!c)
228 return (char *) in;// Trivial empty string case
229
230 len = strlen(str);
231 do {
232 char sc;
233
234 do {
235 sc = *in++;
236 if (!sc)
237 return (char *) 0;
238 } while (sc != c);
239 } while (strncmp(in, str, len) != 0);
240
241 return (char *) (in - 1);
242}
243
244int ptol(const char *str)
245{
246register int c = *str;
247
248if (c <= '7' && c >= '0')
249c -= '0';
250else if (c <= 'h' && c >= 'a')
251c -= 'a';
252else c = 0;
253return c;
254}
255
256int atoi(const char *str)
257{
258register int sum = 0;
259while (*str == ' ' || *str == '\t')
260str++;
261while (*str >= '0' && *str <= '9') {
262sum *= 10;
263sum += *str++ - '0';
264}
265return sum;
266}
267/*
268 * Appends src to string dst of size siz (unlike strncat, siz is the
269 * full size of dst, not space left). At most siz-1 characters
270 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
271 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
272 * If retval >= siz, truncation occurred.
273 */
274size_t strlcat(char *dst, const char *src, size_t siz)
275{
276char *d = dst;
277const char *s = src;
278size_t n = siz;
279size_t dlen;
280
281/* Find the end of dst and adjust bytes left but don't go past end */
282while (n-- != 0 && *d != '\0')
283{
284d++;
285}
286
287dlen = d - dst;
288n = siz - dlen;
289
290if (n == 0)
291{
292return(dlen + strlen(s));
293}
294
295while (*s != '\0')
296{
297if (n != 1)
298{
299*d++ = *s;
300n--;
301}
302s++;
303}
304*d = '\0';
305
306return(dlen + (s - src)); /* count does not include NUL */
307}
308
309char *strncat(char *s1, const char *s2, size_t n)
310{
311register char *ret = s1;
312while (*s1)
313s1++;
314while (n-- && (*s1++ = *s2++));
315return ret;
316}
317
318char *strcat(char *s1, const char *s2)
319{
320register char *ret = s1;
321while (*s1)
322{
323s1++;
324}
325while ((*s1++ = *s2++));
326return ret;
327}
328
329char *strdup(const char *s1)
330{
331return strcpy(malloc(strlen(s1) + 1), s1);
332}
333
334#if STRNCASECMP
335int strncasecmp(const char *s1, const char *s2, size_t len)
336{
337register int n = len;
338while (--n >= 0 && tolower(*s1) == tolower(*s2++))
339if (*s1++ == '\0')
340return(0);
341return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
342}
343#endif
344
345char *strchr(const char *str, int c)
346{
347 do
348 {
349 if(*str == c)
350 return (char*)str;
351 }
352 while(*(str++));
353
354 return 0;
355}
356
357char *strbreak(const char *str, char **next, long *len)
358{
359 char *start = (char*)str, *end;
360 bool quoted = false;
361
362 if ( !start || !len )
363 return 0;
364
365 *len = 0;
366
367 while ( isspace(*start) )
368 start++;
369
370 if (*start == '"')
371 {
372 start++;
373
374 end = strchr(start, '"');
375 if(end)
376 quoted = true;
377 else
378 end = strchr(start, '\0');
379 }
380 else
381 {
382 for ( end = start; *end && !isspace(*end); end++ )
383 {}
384 }
385
386 *len = end - start;
387
388 if(next)
389 *next = quoted ? end+1 : end;
390
391 return start;
392}
393
394/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
395uint8_t checksum8( void *start, unsigned int length )
396{
397uint8_tcsum = 0;
398uint8_t*cp = (uint8_t *) start;
399unsigned int i;
400
401for ( i = 0; i < length; i++)
402{
403csum += *cp++;
404}
405return csum;
406}
407
408

Archive Download this file

Revision: 2714