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

Archive Download this file

Revision: 2476