Chameleon

Chameleon Svn Source Tree

Root/tags/2.3/i386/libsa/string.c

Source at commit HEAD created 4 years 9 months ago.
By ifabio, Few update to kernelPatcher (Credits to CrazyBirdy)
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
121//==========================================================================
122/* NOTE: Moved from ntfs.c */
123int memcmp(const void *p1, const void *p2, size_t len)
124{
125while (len--)
126{
127if (*(const char*)(p1++) != *(const char*)(p2++))
128{
129return -1;
130}
131}
132 return 0;
133}
134
135
136//==========================================================================
137
138int strcmp(const char * s1, const char * s2)
139{
140while (*s1 && (*s1 == *s2))
141{
142s1++;
143s2++;
144}
145
146return (*s1 - *s2);
147}
148
149/* Derived from FreeBSD source */
150int strncmp(const char * s1, const char * s2, size_t n)
151{
152if (!n)
153return 0;
154do {
155if (*s1 != *s2++)
156{
157return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
158}
159
160if (!*s1++)
161{
162break;
163}
164} while (--n);
165return 0;
166}
167
168char *strcpy(char *s1, const char *s2)
169{
170register char *ret = s1;
171while ((*s1++ = *s2++))
172continue;
173return ret;
174}
175
176char *stpcpy(char *s1, const char *s2)
177{
178while ((*s1++ = *s2++))
179{
180continue;
181}
182return --s1;
183}
184
185char *strncpy(char *s1, const char *s2, size_t n)
186{
187register char *ret = s1;
188while (n && (*s1++ = *s2++))
189--n;
190
191if (n > 0) {
192bzero(s1, n);
193}
194return ret;
195}
196
197char *stpncpy(char *s1, const char *s2, size_t n)
198{
199while (n && (*s1++ = *s2++))
200--n;
201if (n > 0)
202{
203bzero(s1, n);
204}
205return s1;
206}
207
208char *strstr(const char *in, const char *str)
209{
210 char c;
211 size_t len;
212
213 c = *str++;
214 if (!c)
215 return (char *) in;// Trivial empty string case
216
217 len = strlen(str);
218 do {
219 char sc;
220
221 do {
222 sc = *in++;
223 if (!sc)
224 return (char *) 0;
225 } while (sc != c);
226 } while (strncmp(in, str, len) != 0);
227
228 return (char *) (in - 1);
229}
230
231
232//==========================================================================
233
234int ptol(const char *str)
235{
236register int c = *str;
237
238if (c <= '7' && c >= '0')
239{
240c -= '0';
241}
242else if (c <= 'h' && c >= 'a')
243{
244c -= 'a';
245}
246else
247{
248c = 0;
249}
250
251return c;
252}
253
254
255//==========================================================================
256
257int atoi(const char *str)
258{
259register int sum = 0;
260
261while (*str == ' ' || *str == '\t')
262{
263str++;
264}
265
266while (*str >= '0' && *str <= '9')
267{
268sum *= 10;
269sum += *str++ - '0';
270}
271return sum;
272}
273/*
274 * Appends src to string dst of size siz (unlike strncat, siz is the
275 * full size of dst, not space left). At most siz-1 characters
276 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
277 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
278 * If retval >= siz, truncation occurred.
279 */
280size_t strlcat(char *dst, const char *src, size_t siz)
281{
282char *d = dst;
283const char *s = src;
284size_t n = siz;
285size_t dlen;
286
287/* Find the end of dst and adjust bytes left but don't go past end */
288while (n-- != 0 && *d != '\0')
289{
290d++;
291}
292
293dlen = d - dst;
294n = siz - dlen;
295
296if (n == 0)
297{
298return(dlen + strlen(s));
299}
300
301while (*s != '\0')
302{
303if (n != 1)
304{
305*d++ = *s;
306n--;
307}
308s++;
309}
310*d = '\0';
311
312return(dlen + (s - src)); /* count does not include NUL */
313}
314
315char *strncat(char *s1, const char *s2, size_t n)
316{
317register char *ret = s1;
318while (*s1)
319s1++;
320while (n-- && (*s1++ = *s2++));
321return ret;
322}
323
324char *strcat(char *s1, const char *s2)
325{
326register char *ret = s1;
327while (*s1)
328{
329s1++;
330}
331while ((*s1++ = *s2++));
332return ret;
333}
334
335char *strdup(const char *s1)
336{
337return strcpy(malloc(strlen(s1) + 1), s1);
338}
339
340#if STRNCASECMP
341int strncasecmp(const char *s1, const char *s2, size_t len)
342{
343register int n = len;
344while (--n >= 0 && tolower(*s1) == tolower(*s2++))
345if (*s1++ == '\0')
346return(0);
347return(n<0 ? 0 : tolower(*s1) - tolower(*--s2));
348}
349#endif
350
351char *strchr(const char *str, int c)
352{
353 do
354 {
355 if(*str == c)
356 return (char*)str;
357 }
358 while(*(str++));
359
360 return 0;
361}
362
363char *strbreak(const char *str, char **next, long *len)
364{
365 char *start = (char*)str, *end;
366 bool quoted = false;
367
368 if ( !start || !len )
369 return 0;
370
371 *len = 0;
372
373 while ( isspace(*start) )
374 start++;
375
376 if (*start == '"')
377 {
378 start++;
379
380 end = strchr(start, '"');
381 if(end)
382 quoted = true;
383 else
384 end = strchr(start, '\0');
385 }
386 else
387 {
388 for ( end = start; *end && !isspace(*end); end++ )
389 {}
390 }
391
392 *len = end - start;
393
394 if(next)
395 *next = quoted ? end+1 : end;
396
397 return start;
398}
399
400/* COPYRIGHT NOTICE: checksum8 from AppleSMBIOS */
401uint8_t checksum8( void *start, unsigned int length )
402{
403uint8_tcsum = 0;
404uint8_t*cp = (uint8_t *) start;
405unsigned int i;
406
407for ( i = 0; i < length; i++)
408{
409csum += *cp++;
410}
411return csum;
412}
413
414/*
415 bsd functions variants (already available in klibc)
416 strsep, strpbrk and __strxspn
417 */
418#ifndef UCHAR_MAX
419#define UCHAR_MAX255u
420#endif
421//==========================================================================
422char *strsep_c(char **stringp, const char *delim)
423{
424char *s = *stringp;
425char *e;
426
427if (!s)
428return NULL;
429
430e = strpbrk_c(s, delim);
431if (e)
432*e++ = '\0';
433
434*stringp = e;
435return s;
436}
437//==========================================================================
438char *strpbrk_c(const char *s, const char *accept)
439{
440const char *ss = s + __strxspn_c(s, accept, 1);
441
442return *ss ? (char *)ss : NULL;
443}
444//==========================================================================
445size_t __strxspn_c(const char *s, const char *map, int parity)
446{
447char matchmap[UCHAR_MAX + 1];
448size_t n = 0;
449
450/* Create bitmap */
451memset(matchmap, 0, sizeof matchmap);
452while (*map)
453matchmap[(unsigned char)*map++] = 1;
454
455/* Make sure the null character never matches */
456matchmap[0] = parity;
457
458/* Calculate span length */
459while (matchmap[(unsigned char)*s++] ^ parity)
460n++;
461
462return n;
463}
464//==========================================================================
465

Archive Download this file

Revision: HEAD