string.c 7.24 KB
Newer Older
longpanda's avatar
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * @file
 *
 * String functions
 *
 */

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "ctype.h"
#include "wctype.h"

/**
 * Copy memory area
 *
 * @v dest		Destination address
 * @v src		Source address
 * @v len		Length
 * @ret dest		Destination address
 */
void * memcpy ( void *dest, const void *src, size_t len ) {
	void *edi = dest;
	const void *esi = src;
	int discard_ecx;

	/* Perform dword-based copy for bulk, then byte-based for remainder */
	__asm__ __volatile__ ( "rep movsl"
			       : "=&D" ( edi ), "=&S" ( esi ),
				 "=&c" ( discard_ecx )
			       : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
			       : "memory" );
	__asm__ __volatile__ ( "rep movsb"
			       : "=&D" ( edi ), "=&S" ( esi ),
				 "=&c" ( discard_ecx )
			       : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
			       : "memory" );
	return dest;
}

/**
 * Copy memory area backwards
 *
 * @v dest		Destination address
 * @v src		Source address
 * @v len		Length
 * @ret dest		Destination address
 */
static void * memcpy_reverse ( void *dest, const void *src, size_t len ) {
	void *edi = ( dest + len - 1 );
	const void *esi = ( src + len - 1 );
	int discard_ecx;

	/* Assume memmove() is not performance-critical, and perform a
	 * bytewise copy for simplicity.
	 *
	 * Disable interrupts to avoid known problems on platforms
	 * that assume the direction flag always remains cleared.
	 */
	__asm__ __volatile__ ( "pushf\n\t"
			       "cli\n\t"
			       "std\n\t"
			       "rep movsb\n\t"
			       "popf\n\t"
			       : "=&D" ( edi ), "=&S" ( esi ),
				 "=&c" ( discard_ecx )
			       : "0" ( edi ), "1" ( esi ),
				 "2" ( len )
			       : "memory" );
	return dest;
}

/**
 * Copy (possibly overlapping) memory area
 *
 * @v dest		Destination address
 * @v src		Source address
 * @v len		Length
 * @ret dest		Destination address
 */
void * memmove ( void *dest, const void *src, size_t len ) {

	if ( dest <= src ) {
		return memcpy ( dest, src, len );
	} else {
		return memcpy_reverse ( dest, src, len );
	}
}

/**
 * Set memory area
 *
 * @v dest		Destination address
 * @v src		Source address
 * @v len		Length
 * @ret dest		Destination address
 */
void * memset ( void *dest, int c, size_t len ) {
	void *edi = dest;
	int eax = c;
	int discard_ecx;

	/* Expand byte to whole dword */
	eax |= ( eax << 8 );
	eax |= ( eax << 16 );

	/* Perform dword-based set for bulk, then byte-based for remainder */
	__asm__ __volatile__ ( "rep stosl"
			       : "=&D" ( edi ), "=&a" ( eax ),
				 "=&c" ( discard_ecx )
			       : "0" ( edi ), "1" ( eax ), "2" ( len >> 2 )
			       : "memory" );
	__asm__ __volatile__ ( "rep stosb"
			       : "=&D" ( edi ), "=&a" ( eax ),
				 "=&c" ( discard_ecx )
			       : "0" ( edi ), "1" ( eax ), "2" ( len & 3 )
			       : "memory" );
	return dest;
}

/**
 * Compare memory areas
 *
 * @v src1		First source area
 * @v src2		Second source area
 * @v len		Length
 * @ret diff		Difference
 */
int memcmp ( const void *src1, const void *src2, size_t len ) {
	const uint8_t *bytes1 = src1;
	const uint8_t *bytes2 = src2;
	int diff;

	while ( len-- ) {
		if ( ( diff = ( *(bytes1++) - *(bytes2++) ) ) )
			return diff;
	}
	return 0;
}

/**
 * Compare two strings
 *
 * @v str1		First string
 * @v str2		Second string
 * @ret diff		Difference
 */
int strcmp ( const char *str1, const char *str2 ) {
	int c1;
	int c2;

	do {
		c1 = *(str1++);
		c2 = *(str2++);
	} while ( ( c1 != '\0' ) && ( c1 == c2 ) );

	return ( c1 - c2 );
}

/**
 * Compare two strings, case-insensitively
 *
 * @v str1		First string
 * @v str2		Second string
 * @ret diff		Difference
 */
int strcasecmp ( const char *str1, const char *str2 ) {
	int c1;
	int c2;

	do {
		c1 = toupper ( *(str1++) );
		c2 = toupper ( *(str2++) );
	} while ( ( c1 != '\0' ) && ( c1 == c2 ) );

	return ( c1 - c2 );
}

/**
 * Compare two wide-character strings, case-insensitively
 *
 * @v str1		First string
 * @v str2		Second string
 * @ret diff		Difference
 */
int wcscasecmp ( const wchar_t *str1, const wchar_t *str2 ) {
	int c1;
	int c2;

	do {
		c1 = towupper ( *(str1++) );
		c2 = towupper ( *(str2++) );
	} while ( ( c1 != L'\0' ) && ( c1 == c2 ) );

	return ( c1 - c2 );
}

/**
 * Get length of string
 *
 * @v str		String
 * @ret len		Length
 */
size_t strlen ( const char *str ) {
	size_t len = 0;

	while ( *(str++) )
		len++;
	return len;
}

/**
 * Get length of wide-character string
 *
 * @v str		String
 * @ret len		Length (in characters)
 */
size_t wcslen ( const wchar_t *str ) {
	size_t len = 0;

	while ( *(str++) )
		len++;
	return len;
}

/**
 * Find character in wide-character string
 *
 * @v str		String
 * @v c			Wide character
 * @ret first		First occurrence of wide character in string, or NULL
 */
wchar_t * wcschr ( const wchar_t *str, wchar_t c ) {

	for ( ; *str ; str++ ) {
		if ( *str == c )
			return ( ( wchar_t * )str );
	}
	return NULL;
}

char *strchr(const char *str, char c) {
    for ( ; *str ; str++ ) {
		if ( *str == c )
			return ( ( char * )str );
	}
	return NULL;
}

/**
 * Check to see if character is a space
 *
 * @v c                 Character
 * @ret isspace         Character is a space
 */
int isspace ( int c ) {
        switch ( c ) {
        case ' ' :
        case '\f' :
        case '\n' :
        case '\r' :
        case '\t' :
        case '\v' :
                return 1;
        default:
                return 0;
        }
}

/**
 * Convert a string to an unsigned integer
 *
 * @v nptr		String
 * @v endptr		End pointer to fill in (or NULL)
 * @v base		Numeric base
 * @ret val		Value
 */
unsigned long strtoul ( const char *nptr, char **endptr, int base ) {
	unsigned long val = 0;
	int negate = 0;
	unsigned int digit;

	/* Skip any leading whitespace */
	while ( isspace ( *nptr ) )
		nptr++;

	/* Parse sign, if present */
	if ( *nptr == '+' ) {
		nptr++;
	} else if ( *nptr == '-' ) {
		nptr++;
		negate = 1;
	}

	/* Parse base */
	if ( base == 0 ) {

		/* Default to decimal */
		base = 10;

		/* Check for octal or hexadecimal markers */
		if ( *nptr == '0' ) {
			nptr++;
			base = 8;
			if ( ( *nptr | 0x20 ) == 'x' ) {
				nptr++;
				base = 16;
			}
		}
	}

	/* Parse digits */
	for ( ; ; nptr++ ) {
		digit = *nptr;
		if ( digit >= 'a' ) {
			digit = ( digit - 'a' + 10 );
		} else if ( digit >= 'A' ) {
			digit = ( digit - 'A' + 10 );
		} else if ( digit <= '9' ) {
			digit = ( digit - '0' );
		}
		if ( digit >= ( unsigned int ) base )
			break;
		val = ( ( val * base ) + digit );
	}

	/* Record end marker, if applicable */
	if ( endptr )
		*endptr = ( ( char * ) nptr );

	/* Return value */
	return ( negate ? -val : val );
}