Chameleon

Chameleon Svn Source Tree

Root/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{
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
59#else
60void * memcpy(void * dst, const void * src, size_t len)
61{
62asm 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
73return dst;
74}
75
76void bcopy(const void * src, void * dst, size_t len)
77{
78asm 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{
92asm 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{
113const char* save = s;
114while (*s++);
115return (--s) - save;
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
141/* Derived from FreeBSD source */
142int strncmp(const char * s1, const char * s2, size_t n)
143{
144if (!n)
145return 0;
146do {
147if (*s1 != *s2++)
148{
149return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
150}
151
152if (!*s1++)
153{
154break;
155}
156} while (--n);
157return 0;
158}
159
160char *strcpy(char *s1, const char *s2)
161{
162register char *ret = s1;
163while ((*s1++ = *s2++))
164continue;
165return ret;
166}
167
168char *stpcpy(char *s1, const char *s2)
169{
170while ((*s1++ = *s2++))
171{
172continue;
173}
174return --s1;
175}
176
177char *strncpy(char *s1, const char *s2, size_t n)
178{
179register char *ret = s1;
180while (n && (*s1++ = *s2++))
181--n;
182
183if (n > 0) {
184bzero(s1, n);
185}
186return ret;
187}
188
189char *stpncpy(char *s1, const char *s2, size_t n)
190{
191while (n && (*s1++ = *s2++))
192--n;
193if (n > 0)
194{
195bzero(s1, n);
196}
197return s1;
198}
199
200char *strstr(const char *in, const char *str)
201{
202 char c;
203 size_t len;
204
205 c = *str++;
206 if (!c)
207 return (char *) in;// Trivial empty string case
208
209 len = strlen(str);
210 do {
211 char sc;
212
213 do {
214 sc = *in++;
215 if (!sc)
216 return (char *) 0;
217 } while (sc != c);
218 } while (strncmp(in, str, len) != 0);
219
220 return (char *) (in - 1);
221}
222
223int ptol(const char *str)
224{
225register int c = *str;
226
227if (c <= '7' && c >= '0')
228c -= '0';
229else if (c <= 'h' && c >= 'a')
230c -= 'a';
231else c = 0;
232return c;
233}
234
235int atoi(const char *str)
236{
237register int sum = 0;
238while (*str == ' ' || *str == '\t')
239str++;
240while (*str >= '0' && *str <= '9') {
241sum *= 10;
242sum += *str++ - '0';
243}
244return sum;
245}
246/*
247 * Appends src to string dst of size siz (unlike strncat, siz is the
248 * full size of dst, not space left). At most siz-1 characters
249 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
250 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
251 * If retval >= siz, truncation occurred.
252 */
253size_t strlcat(char *dst, const char *src, size_t siz)
254{
255char *d = dst;
256const char *s = src;
257size_t n = siz;
258size_t dlen;
259
260/* Find the end of dst and adjust bytes left but don't go past end */
261while (n-- != 0 && *d != '\0')
262{
263d++;
264}
265
266dlen = d - dst;
267n = siz - dlen;
268
269if (n == 0)
270{
271return(dlen + strlen(s));
272}
273
274while (*s != '\0')
275{
276if (n != 1)
277{
278*d++ = *s;
279n--;
280}
281s++;
282}
283*d = '\0';
284
285return(dlen + (s - src)); /* count does not include NUL */
286}
287
288char *strncat(char *s1, const char *s2, size_t n)
289{
290register char *ret = s1;
291while (*s1)
292s1++;
293while (n-- && (*s1++ = *s2++));
294return ret;
295}
296
297char *strcat(char *s1, const char *s2)
298{
299register char *ret = s1;
300while (*s1)
301{
302s1++;
303}
304while ((*s1++ = *s2++));
305return ret;
306}
307
308char *strdup(const char *s1)
309{
310return strcpy(malloc(strlen(s1) + 1), s1);
311}
312
313#if STRNCASECMP
314int strncasecmp(const char *s1, const char *s2, size_t len)
315{
316register int n = len;
317while (--n >= 0 && tolower(*s1) == tolower(*s2++))
318if (*s1++ == '\0')
319return(0);
320return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
321}
322#endif
323
324char *strchr(const char *str, int c)
325{
326 do
327 {
328 if(*str == c)
329 return (char*)str;
330 }
331 while(*(str++));
332
333 return 0;
334}
335
336char *strbreak(const char *str, char **next, long *len)
337{
338 char *start = (char*)str, *end;
339 bool quoted = false;
340
341 if ( !start || !len )
342 return 0;
343
344 *len = 0;
345
346 while ( isspace(*start) )
347 start++;
348
349 if (*start == '"')
350 {
351 start++;
352
353 end = strchr(start, '"');
354 if(end)
355 quoted = true;
356 else
357 end = strchr(start, '\0');
358 }
359 else
360 {
361 for ( end = start; *end && !isspace(*end); end++ )
362 {}
363 }
364
365 *len = end - start;
366
367 if(next)
368 *next = quoted ? end+1 : end;
369
370 return start;
371}
372
373/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
374uint8_t checksum8( void *start, unsigned int length )
375{
376 uint8_t csum = 0;
377 uint8_t * cp = (uint8_t *) start;
378 unsigned int i;
379
380for ( i = 0; i < length; i++)
381{
382csum += *cp++;
383}
384return csum;
385}
386
387

Archive Download this file

Revision: 2615