Subversion Repositories HelenOS

Rev

Rev 4263 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4263 Rev 4327
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <string.h>
36
#include <string.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
-
 
38
#include <assert.h>
38
#include <limits.h>
39
#include <limits.h>
39
#include <ctype.h>
40
#include <ctype.h>
40
#include <malloc.h>
41
#include <malloc.h>
41
#include <errno.h>
42
#include <errno.h>
42
#include <align.h>
43
#include <align.h>
-
 
44
#include <mem.h>
43
#include <string.h>
45
#include <string.h>
44
 
46
 
45
/** Byte mask consisting of lowest @n bits (out of 8) */
47
/** Byte mask consisting of lowest @n bits (out of 8) */
46
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
48
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
47
 
49
 
48
/** Byte mask consisting of lowest @n bits (out of 32) */
50
/** Byte mask consisting of lowest @n bits (out of 32) */
49
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
51
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
50
 
52
 
51
/** Byte mask consisting of highest @n bits (out of 8) */
53
/** Byte mask consisting of highest @n bits (out of 8) */
52
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
54
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
53
 
55
 
54
/** Number of data bits in a UTF-8 continuation byte */
56
/** Number of data bits in a UTF-8 continuation byte */
55
#define CONT_BITS  6
57
#define CONT_BITS  6
56
 
58
 
57
/** Decode a single character from a string.
59
/** Decode a single character from a string.
58
 *
60
 *
59
 * Decode a single character from a string of size @a size. Decoding starts
61
 * Decode a single character from a string of size @a size. Decoding starts
60
 * at @a offset and this offset is moved to the beginning of the next
62
 * at @a offset and this offset is moved to the beginning of the next
61
 * character. In case of decoding error, offset generally advances at least
63
 * character. In case of decoding error, offset generally advances at least
62
 * by one. However, offset is never moved beyond size.
64
 * by one. However, offset is never moved beyond size.
63
 *
65
 *
64
 * @param str    String (not necessarily NULL-terminated).
66
 * @param str    String (not necessarily NULL-terminated).
65
 * @param offset Byte offset in string where to start decoding.
67
 * @param offset Byte offset in string where to start decoding.
66
 * @param size   Size of the string (in bytes).
68
 * @param size   Size of the string (in bytes).
67
 *
69
 *
68
 * @return Value of decoded character, U_SPECIAL on decoding error or
70
 * @return Value of decoded character, U_SPECIAL on decoding error or
69
 *         NULL if attempt to decode beyond @a size.
71
 *         NULL if attempt to decode beyond @a size.
70
 *
72
 *
71
 */
73
 */
72
wchar_t str_decode(const char *str, size_t *offset, size_t size)
74
wchar_t str_decode(const char *str, size_t *offset, size_t size)
73
{
75
{
74
    if (*offset + 1 > size)
76
    if (*offset + 1 > size)
75
        return 0;
77
        return 0;
76
   
78
   
77
    /* First byte read from string */
79
    /* First byte read from string */
78
    uint8_t b0 = (uint8_t) str[(*offset)++];
80
    uint8_t b0 = (uint8_t) str[(*offset)++];
79
   
81
   
80
    /* Determine code length */
82
    /* Determine code length */
81
   
83
   
82
    unsigned int b0_bits;  /* Data bits in first byte */
84
    unsigned int b0_bits;  /* Data bits in first byte */
83
    unsigned int cbytes;   /* Number of continuation bytes */
85
    unsigned int cbytes;   /* Number of continuation bytes */
84
   
86
   
85
    if ((b0 & 0x80) == 0) {
87
    if ((b0 & 0x80) == 0) {
86
        /* 0xxxxxxx (Plain ASCII) */
88
        /* 0xxxxxxx (Plain ASCII) */
87
        b0_bits = 7;
89
        b0_bits = 7;
88
        cbytes = 0;
90
        cbytes = 0;
89
    } else if ((b0 & 0xe0) == 0xc0) {
91
    } else if ((b0 & 0xe0) == 0xc0) {
90
        /* 110xxxxx 10xxxxxx */
92
        /* 110xxxxx 10xxxxxx */
91
        b0_bits = 5;
93
        b0_bits = 5;
92
        cbytes = 1;
94
        cbytes = 1;
93
    } else if ((b0 & 0xf0) == 0xe0) {
95
    } else if ((b0 & 0xf0) == 0xe0) {
94
        /* 1110xxxx 10xxxxxx 10xxxxxx */
96
        /* 1110xxxx 10xxxxxx 10xxxxxx */
95
        b0_bits = 4;
97
        b0_bits = 4;
96
        cbytes = 2;
98
        cbytes = 2;
97
    } else if ((b0 & 0xf8) == 0xf0) {
99
    } else if ((b0 & 0xf8) == 0xf0) {
98
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
100
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
99
        b0_bits = 3;
101
        b0_bits = 3;
100
        cbytes = 3;
102
        cbytes = 3;
101
    } else {
103
    } else {
102
        /* 10xxxxxx -- unexpected continuation byte */
104
        /* 10xxxxxx -- unexpected continuation byte */
103
        return U_SPECIAL;
105
        return U_SPECIAL;
104
    }
106
    }
105
   
107
   
106
    if (*offset + cbytes > size)
108
    if (*offset + cbytes > size)
107
        return U_SPECIAL;
109
        return U_SPECIAL;
108
   
110
   
109
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
111
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
110
   
112
   
111
    /* Decode continuation bytes */
113
    /* Decode continuation bytes */
112
    while (cbytes > 0) {
114
    while (cbytes > 0) {
113
        uint8_t b = (uint8_t) str[(*offset)++];
115
        uint8_t b = (uint8_t) str[(*offset)++];
114
       
116
       
115
        /* Must be 10xxxxxx */
117
        /* Must be 10xxxxxx */
116
        if ((b & 0xc0) != 0x80)
118
        if ((b & 0xc0) != 0x80)
117
            return U_SPECIAL;
119
            return U_SPECIAL;
118
       
120
       
119
        /* Shift data bits to ch */
121
        /* Shift data bits to ch */
120
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
122
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
121
        cbytes--;
123
        cbytes--;
122
    }
124
    }
123
   
125
   
124
    return ch;
126
    return ch;
125
}
127
}
126
 
128
 
127
/** Encode a single character to string representation.
129
/** Encode a single character to string representation.
128
 *
130
 *
129
 * Encode a single character to string representation (i.e. UTF-8) and store
131
 * Encode a single character to string representation (i.e. UTF-8) and store
130
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
132
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
131
 * is moved to the position where the next character can be written to.
133
 * is moved to the position where the next character can be written to.
132
 *
134
 *
133
 * @param ch     Input character.
135
 * @param ch     Input character.
134
 * @param str    Output buffer.
136
 * @param str    Output buffer.
135
 * @param offset Byte offset where to start writing.
137
 * @param offset Byte offset where to start writing.
136
 * @param size   Size of the output buffer (in bytes).
138
 * @param size   Size of the output buffer (in bytes).
137
 *
139
 *
138
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
140
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
139
 *     was not enough space in the output buffer or EINVAL if the character
141
 *     was not enough space in the output buffer or EINVAL if the character
140
 *     code was invalid.
142
 *     code was invalid.
141
 */
143
 */
142
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
144
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
143
{
145
{
144
    if (*offset >= size)
146
    if (*offset >= size)
145
        return EOVERFLOW;
147
        return EOVERFLOW;
146
   
148
   
147
    if (!chr_check(ch))
149
    if (!chr_check(ch))
148
        return EINVAL;
150
        return EINVAL;
149
   
151
   
150
    /* Unsigned version of ch (bit operations should only be done
152
    /* Unsigned version of ch (bit operations should only be done
151
       on unsigned types). */
153
       on unsigned types). */
152
    uint32_t cc = (uint32_t) ch;
154
    uint32_t cc = (uint32_t) ch;
153
   
155
   
154
    /* Determine how many continuation bytes are needed */
156
    /* Determine how many continuation bytes are needed */
155
   
157
   
156
    unsigned int b0_bits;  /* Data bits in first byte */
158
    unsigned int b0_bits;  /* Data bits in first byte */
157
    unsigned int cbytes;   /* Number of continuation bytes */
159
    unsigned int cbytes;   /* Number of continuation bytes */
158
   
160
   
159
    if ((cc & ~LO_MASK_32(7)) == 0) {
161
    if ((cc & ~LO_MASK_32(7)) == 0) {
160
        b0_bits = 7;
162
        b0_bits = 7;
161
        cbytes = 0;
163
        cbytes = 0;
162
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
164
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
163
        b0_bits = 5;
165
        b0_bits = 5;
164
        cbytes = 1;
166
        cbytes = 1;
165
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
167
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
166
        b0_bits = 4;
168
        b0_bits = 4;
167
        cbytes = 2;
169
        cbytes = 2;
168
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
170
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
169
        b0_bits = 3;
171
        b0_bits = 3;
170
        cbytes = 3;
172
        cbytes = 3;
171
    } else {
173
    } else {
172
        /* Codes longer than 21 bits are not supported */
174
        /* Codes longer than 21 bits are not supported */
173
        return EINVAL;
175
        return EINVAL;
174
    }
176
    }
175
   
177
   
176
    /* Check for available space in buffer */
178
    /* Check for available space in buffer */
177
    if (*offset + cbytes >= size)
179
    if (*offset + cbytes >= size)
178
        return EOVERFLOW;
180
        return EOVERFLOW;
179
   
181
   
180
    /* Encode continuation bytes */
182
    /* Encode continuation bytes */
181
    unsigned int i;
183
    unsigned int i;
182
    for (i = cbytes; i > 0; i--) {
184
    for (i = cbytes; i > 0; i--) {
183
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
185
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
184
        cc = cc >> CONT_BITS;
186
        cc = cc >> CONT_BITS;
185
    }
187
    }
186
   
188
   
187
    /* Encode first byte */
189
    /* Encode first byte */
188
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
190
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
189
   
191
   
190
    /* Advance offset */
192
    /* Advance offset */
191
    *offset += cbytes + 1;
193
    *offset += cbytes + 1;
192
   
194
   
193
    return EOK;
195
    return EOK;
194
}
196
}
195
 
197
 
196
/** Get size of string.
198
/** Get size of string.
197
 *
199
 *
198
 * Get the number of bytes which are used by the string @a str (excluding the
200
 * Get the number of bytes which are used by the string @a str (excluding the
199
 * NULL-terminator).
201
 * NULL-terminator).
200
 *
202
 *
201
 * @param str String to consider.
203
 * @param str String to consider.
202
 *
204
 *
203
 * @return Number of bytes used by the string
205
 * @return Number of bytes used by the string
204
 *
206
 *
205
 */
207
 */
206
size_t str_size(const char *str)
208
size_t str_size(const char *str)
207
{
209
{
208
    size_t size = 0;
210
    size_t size = 0;
209
   
211
   
210
    while (*str++ != 0)
212
    while (*str++ != 0)
211
        size++;
213
        size++;
212
   
214
   
213
    return size;
215
    return size;
214
}
216
}
215
 
217
 
216
/** Get size of wide string.
218
/** Get size of wide string.
217
 *
219
 *
218
 * Get the number of bytes which are used by the wide string @a str (excluding the
220
 * Get the number of bytes which are used by the wide string @a str (excluding the
219
 * NULL-terminator).
221
 * NULL-terminator).
220
 *
222
 *
221
 * @param str Wide string to consider.
223
 * @param str Wide string to consider.
222
 *
224
 *
223
 * @return Number of bytes used by the wide string
225
 * @return Number of bytes used by the wide string
224
 *
226
 *
225
 */
227
 */
226
size_t wstr_size(const wchar_t *str)
228
size_t wstr_size(const wchar_t *str)
227
{
229
{
228
    return (wstr_length(str) * sizeof(wchar_t));
230
    return (wstr_length(str) * sizeof(wchar_t));
229
}
231
}
230
 
232
 
231
/** Get size of string with length limit.
233
/** Get size of string with length limit.
232
 *
234
 *
233
 * Get the number of bytes which are used by up to @a max_len first
235
 * Get the number of bytes which are used by up to @a max_len first
234
 * characters in the string @a str. If @a max_len is greater than
236
 * characters in the string @a str. If @a max_len is greater than
235
 * the length of @a str, the entire string is measured (excluding the
237
 * the length of @a str, the entire string is measured (excluding the
236
 * NULL-terminator).
238
 * NULL-terminator).
237
 *
239
 *
238
 * @param str     String to consider.
240
 * @param str     String to consider.
239
 * @param max_len Maximum number of characters to measure.
241
 * @param max_len Maximum number of characters to measure.
240
 *
242
 *
241
 * @return Number of bytes used by the characters.
243
 * @return Number of bytes used by the characters.
242
 *
244
 *
243
 */
245
 */
244
size_t str_lsize(const char *str, count_t max_len)
246
size_t str_lsize(const char *str, count_t max_len)
245
{
247
{
246
    count_t len = 0;
248
    count_t len = 0;
247
    size_t offset = 0;
249
    size_t offset = 0;
248
   
250
   
249
    while (len < max_len) {
251
    while (len < max_len) {
250
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
252
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
251
            break;
253
            break;
252
       
254
       
253
        len++;
255
        len++;
254
    }
256
    }
255
   
257
   
256
    return offset;
258
    return offset;
257
}
259
}
258
 
260
 
259
/** Get size of wide string with length limit.
261
/** Get size of wide string with length limit.
260
 *
262
 *
261
 * Get the number of bytes which are used by up to @a max_len first
263
 * Get the number of bytes which are used by up to @a max_len first
262
 * wide characters in the wide string @a str. If @a max_len is greater than
264
 * wide characters in the wide string @a str. If @a max_len is greater than
263
 * the length of @a str, the entire wide string is measured (excluding the
265
 * the length of @a str, the entire wide string is measured (excluding the
264
 * NULL-terminator).
266
 * NULL-terminator).
265
 *
267
 *
266
 * @param str     Wide string to consider.
268
 * @param str     Wide string to consider.
267
 * @param max_len Maximum number of wide characters to measure.
269
 * @param max_len Maximum number of wide characters to measure.
268
 *
270
 *
269
 * @return Number of bytes used by the wide characters.
271
 * @return Number of bytes used by the wide characters.
270
 *
272
 *
271
 */
273
 */
272
size_t wstr_lsize(const wchar_t *str, count_t max_len)
274
size_t wstr_lsize(const wchar_t *str, count_t max_len)
273
{
275
{
274
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
276
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
275
}
277
}
276
 
278
 
277
/** Get number of characters in a string.
279
/** Get number of characters in a string.
278
 *
280
 *
279
 * @param str NULL-terminated string.
281
 * @param str NULL-terminated string.
280
 *
282
 *
281
 * @return Number of characters in string.
283
 * @return Number of characters in string.
282
 *
284
 *
283
 */
285
 */
284
count_t str_length(const char *str)
286
count_t str_length(const char *str)
285
{
287
{
286
    count_t len = 0;
288
    count_t len = 0;
287
    size_t offset = 0;
289
    size_t offset = 0;
288
   
290
   
289
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
291
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
290
        len++;
292
        len++;
291
   
293
   
292
    return len;
294
    return len;
293
}
295
}
294
 
296
 
295
/** Get number of characters in a wide string.
297
/** Get number of characters in a wide string.
296
 *
298
 *
297
 * @param str NULL-terminated wide string.
299
 * @param str NULL-terminated wide string.
298
 *
300
 *
299
 * @return Number of characters in @a str.
301
 * @return Number of characters in @a str.
300
 *
302
 *
301
 */
303
 */
302
count_t wstr_length(const wchar_t *wstr)
304
count_t wstr_length(const wchar_t *wstr)
303
{
305
{
304
    count_t len = 0;
306
    count_t len = 0;
305
   
307
   
306
    while (*wstr++ != 0)
308
    while (*wstr++ != 0)
307
        len++;
309
        len++;
308
   
310
   
309
    return len;
311
    return len;
310
}
312
}
311
 
313
 
312
/** Get number of characters in a string with size limit.
314
/** Get number of characters in a string with size limit.
313
 *
315
 *
314
 * @param str  NULL-terminated string.
316
 * @param str  NULL-terminated string.
315
 * @param size Maximum number of bytes to consider.
317
 * @param size Maximum number of bytes to consider.
316
 *
318
 *
317
 * @return Number of characters in string.
319
 * @return Number of characters in string.
318
 *
320
 *
319
 */
321
 */
320
count_t str_nlength(const char *str, size_t size)
322
count_t str_nlength(const char *str, size_t size)
321
{
323
{
322
    count_t len = 0;
324
    count_t len = 0;
323
    size_t offset = 0;
325
    size_t offset = 0;
324
   
326
   
325
    while (str_decode(str, &offset, size) != 0)
327
    while (str_decode(str, &offset, size) != 0)
326
        len++;
328
        len++;
327
   
329
   
328
    return len;
330
    return len;
329
}
331
}
330
 
332
 
331
/** Get number of characters in a string with size limit.
333
/** Get number of characters in a string with size limit.
332
 *
334
 *
333
 * @param str  NULL-terminated string.
335
 * @param str  NULL-terminated string.
334
 * @param size Maximum number of bytes to consider.
336
 * @param size Maximum number of bytes to consider.
335
 *
337
 *
336
 * @return Number of characters in string.
338
 * @return Number of characters in string.
337
 *
339
 *
338
 */
340
 */
339
count_t wstr_nlength(const wchar_t *str, size_t size)
341
count_t wstr_nlength(const wchar_t *str, size_t size)
340
{
342
{
341
    count_t len = 0;
343
    count_t len = 0;
342
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
344
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
343
    count_t offset = 0;
345
    count_t offset = 0;
344
   
346
   
345
    while ((offset < limit) && (*str++ != 0)) {
347
    while ((offset < limit) && (*str++ != 0)) {
346
        len++;
348
        len++;
347
        offset += sizeof(wchar_t);
349
        offset += sizeof(wchar_t);
348
    }
350
    }
349
   
351
   
350
    return len;
352
    return len;
351
}
353
}
352
 
354
 
353
/** Check whether character is plain ASCII.
355
/** Check whether character is plain ASCII.
354
 *
356
 *
355
 * @return True if character is plain ASCII.
357
 * @return True if character is plain ASCII.
356
 *
358
 *
357
 */
359
 */
358
bool ascii_check(wchar_t ch)
360
bool ascii_check(wchar_t ch)
359
{
361
{
360
    if ((ch >= 0) && (ch <= 127))
362
    if ((ch >= 0) && (ch <= 127))
361
        return true;
363
        return true;
362
   
364
   
363
    return false;
365
    return false;
364
}
366
}
365
 
367
 
366
/** Check whether character is valid
368
/** Check whether character is valid
367
 *
369
 *
368
 * @return True if character is a valid Unicode code point.
370
 * @return True if character is a valid Unicode code point.
369
 *
371
 *
370
 */
372
 */
371
bool chr_check(wchar_t ch)
373
bool chr_check(wchar_t ch)
372
{
374
{
373
    if ((ch >= 0) && (ch <= 1114111))
375
    if ((ch >= 0) && (ch <= 1114111))
374
        return true;
376
        return true;
375
   
377
   
376
    return false;
378
    return false;
377
}
379
}
378
 
380
 
379
/** Compare two NULL terminated strings.
381
/** Compare two NULL terminated strings.
380
 *
382
 *
381
 * Do a char-by-char comparison of two NULL-terminated strings.
383
 * Do a char-by-char comparison of two NULL-terminated strings.
382
 * The strings are considered equal iff they consist of the same
384
 * The strings are considered equal iff they consist of the same
383
 * characters on the minimum of their lengths.
385
 * characters on the minimum of their lengths.
384
 *
386
 *
385
 * @param s1 First string to compare.
387
 * @param s1 First string to compare.
386
 * @param s2 Second string to compare.
388
 * @param s2 Second string to compare.
387
 *
389
 *
388
 * @return 0 if the strings are equal, -1 if first is smaller,
390
 * @return 0 if the strings are equal, -1 if first is smaller,
389
 *         1 if second smaller.
391
 *         1 if second smaller.
390
 *
392
 *
391
 */
393
 */
392
int str_cmp(const char *s1, const char *s2)
394
int str_cmp(const char *s1, const char *s2)
393
{
395
{
394
    wchar_t c1 = 0;
396
    wchar_t c1 = 0;
395
    wchar_t c2 = 0;
397
    wchar_t c2 = 0;
396
   
398
   
397
    size_t off1 = 0;
399
    size_t off1 = 0;
398
    size_t off2 = 0;
400
    size_t off2 = 0;
399
 
401
 
400
    while (true) {
402
    while (true) {
401
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
403
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
402
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
404
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
403
 
405
 
404
        if (c1 < c2)
406
        if (c1 < c2)
405
            return -1;
407
            return -1;
406
       
408
       
407
        if (c1 > c2)
409
        if (c1 > c2)
408
            return 1;
410
            return 1;
409
 
411
 
410
        if (c1 == 0 || c2 == 0)
412
        if (c1 == 0 || c2 == 0)
411
            break;     
413
            break;     
412
    }
414
    }
413
 
415
 
414
    return 0;
416
    return 0;
415
}
417
}
416
 
418
 
417
/** Compare two NULL terminated strings with length limit.
419
/** Compare two NULL terminated strings with length limit.
418
 *
420
 *
419
 * Do a char-by-char comparison of two NULL-terminated strings.
421
 * Do a char-by-char comparison of two NULL-terminated strings.
420
 * The strings are considered equal iff they consist of the same
422
 * The strings are considered equal iff they consist of the same
421
 * characters on the minimum of their lengths and the length limit.
423
 * characters on the minimum of their lengths and the length limit.
422
 *
424
 *
423
 * @param s1      First string to compare.
425
 * @param s1      First string to compare.
424
 * @param s2      Second string to compare.
426
 * @param s2      Second string to compare.
425
 * @param max_len Maximum number of characters to consider.
427
 * @param max_len Maximum number of characters to consider.
426
 *
428
 *
427
 * @return 0 if the strings are equal, -1 if first is smaller,
429
 * @return 0 if the strings are equal, -1 if first is smaller,
428
 *         1 if second smaller.
430
 *         1 if second smaller.
429
 *
431
 *
430
 */
432
 */
431
int str_lcmp(const char *s1, const char *s2, count_t max_len)
433
int str_lcmp(const char *s1, const char *s2, count_t max_len)
432
{
434
{
433
    wchar_t c1 = 0;
435
    wchar_t c1 = 0;
434
    wchar_t c2 = 0;
436
    wchar_t c2 = 0;
435
   
437
   
436
    size_t off1 = 0;
438
    size_t off1 = 0;
437
    size_t off2 = 0;
439
    size_t off2 = 0;
438
   
440
   
439
    count_t len = 0;
441
    count_t len = 0;
440
 
442
 
441
    while (true) {
443
    while (true) {
442
        if (len >= max_len)
444
        if (len >= max_len)
443
            break;
445
            break;
444
 
446
 
445
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
447
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
446
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
448
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
447
 
449
 
448
        if (c1 < c2)
450
        if (c1 < c2)
449
            return -1;
451
            return -1;
450
 
452
 
451
        if (c1 > c2)
453
        if (c1 > c2)
452
            return 1;
454
            return 1;
453
 
455
 
454
        if (c1 == 0 || c2 == 0)
456
        if (c1 == 0 || c2 == 0)
455
            break;
457
            break;
456
 
458
 
457
        ++len; 
459
        ++len; 
458
    }
460
    }
459
 
461
 
460
    return 0;
462
    return 0;
461
 
463
 
462
}
464
}
463
 
465
 
464
/** Copy NULL-terminated string.
466
/** Copy string.
465
 *
467
 *
466
 * Copy source string @a src to destination buffer @a dst.
468
 * Copy source string @a src to destination buffer @a dest.
467
 * No more than @a size bytes are written. NULL-terminator is always
469
 * No more than @a size bytes are written. If the size of the output buffer
468
 * written after the last succesfully copied character (i.e. if the
470
 * is at least one byte, the output string will always be well-formed, i.e.
469
 * destination buffer is has at least 1 byte, it will be always
471
 * null-terminated and containing only complete characters.
470
 * NULL-terminated).
-
 
471
 *
472
 *
472
 * @param src   Source string.
-
 
473
 * @param dst   Destination buffer.
473
 * @param dst   Destination buffer.
474
 * @param count Size of the destination buffer.
474
 * @param count Size of the destination buffer (must be > 0).
475
 *
475
 * @param src   Source string.
476
 */
476
 */
477
void str_ncpy(char *dst, const char *src, size_t size)
477
void str_cpy(char *dest, size_t size, const char *src)
478
{
478
{
479
    /* No space for the NULL-terminator in the buffer */
-
 
480
    if (size == 0)
-
 
481
        return;
-
 
482
   
-
 
483
    wchar_t ch;
479
    wchar_t ch;
484
    size_t str_off = 0;
480
    size_t src_off;
485
    size_t dst_off = 0;
481
    size_t dest_off;
-
 
482
 
-
 
483
    /* There must be space for a null terminator in the buffer. */
-
 
484
    assert(size > 0);
486
   
485
   
-
 
486
    src_off = 0;
-
 
487
    dest_off = 0;
-
 
488
 
487
    while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
489
    while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
488
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
490
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
489
            break;
491
            break;
490
    }
492
    }
-
 
493
 
-
 
494
    dest[dest_off] = '\0';
-
 
495
}
-
 
496
 
-
 
497
/** Copy size-limited substring.
-
 
498
 *
-
 
499
 * Copy prefix of string @a src of max. size @a size to destination buffer
-
 
500
 * @a dest. No more than @a size bytes are written. The output string will
-
 
501
 * always be well-formed, i.e. null-terminated and containing only complete
-
 
502
 * characters.
-
 
503
 *
-
 
504
 * No more than @a n bytes are read from the input string, so it does not
-
 
505
 * have to be null-terminated.
-
 
506
 *
-
 
507
 * @param dst   Destination buffer.
-
 
508
 * @param count Size of the destination buffer (must be > 0).
-
 
509
 * @param src   Source string.
-
 
510
 * @param n Maximum number of bytes to read from @a src.
-
 
511
 */
-
 
512
void str_ncpy(char *dest, size_t size, const char *src, size_t n)
-
 
513
{
-
 
514
    wchar_t ch;
-
 
515
    size_t src_off;
-
 
516
    size_t dest_off;
-
 
517
 
-
 
518
    /* There must be space for a null terminator in the buffer. */
-
 
519
    assert(size > 0);
491
   
520
   
492
    if (dst_off >= size)
521
    src_off = 0;
493
        dst[size - 1] = 0;
522
    dest_off = 0;
-
 
523
 
-
 
524
    while ((ch = str_decode(src, &src_off, n)) != 0) {
-
 
525
        if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
-
 
526
            break;
494
    else
527
    }
-
 
528
 
495
        dst[dst_off] = 0;
529
    dest[dest_off] = '\0';
-
 
530
}
-
 
531
 
-
 
532
/** Append one string to another.
-
 
533
 *
-
 
534
 * Append source string @a src to string in destination buffer @a dest.
-
 
535
 * Size of the destination buffer is @a dest. If the size of the output buffer
-
 
536
 * is at least one byte, the output string will always be well-formed, i.e.
-
 
537
 * null-terminated and containing only complete characters.
-
 
538
 *
-
 
539
 * @param dst   Destination buffer.
-
 
540
 * @param count Size of the destination buffer.
-
 
541
 * @param src   Source string.
-
 
542
 */
-
 
543
void str_append(char *dest, size_t size, const char *src)
-
 
544
{
-
 
545
    size_t dstr_size;
-
 
546
 
-
 
547
    dstr_size = str_size(dest);
-
 
548
    str_cpy(dest + dstr_size, size - dstr_size, src);
496
}
549
}
497
 
550
 
498
/** Copy NULL-terminated wide string to string
551
/** Copy NULL-terminated wide string to string
499
 *
552
 *
500
 * Copy source wide string @a src to destination buffer @a dst.
553
 * Copy source wide string @a src to destination buffer @a dst.
501
 * No more than @a size bytes are written. NULL-terminator is always
554
 * No more than @a size bytes are written. NULL-terminator is always
502
 * written after the last succesfully copied character (i.e. if the
555
 * written after the last succesfully copied character (i.e. if the
503
 * destination buffer is has at least 1 byte, it will be always
556
 * destination buffer is has at least 1 byte, it will be always
504
 * NULL-terminated).
557
 * NULL-terminated).
505
 *
558
 *
506
 * @param src   Source wide string.
559
 * @param src   Source wide string.
507
 * @param dst   Destination buffer.
560
 * @param dst   Destination buffer.
508
 * @param count Size of the destination buffer.
561
 * @param count Size of the destination buffer.
509
 *
562
 *
510
 */
563
 */
511
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
564
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
512
{
565
{
513
    /* No space for the NULL-terminator in the buffer */
566
    /* No space for the NULL-terminator in the buffer */
514
    if (size == 0)
567
    if (size == 0)
515
        return;
568
        return;
516
   
569
   
517
    wchar_t ch;
570
    wchar_t ch;
518
    count_t src_idx = 0;
571
    count_t src_idx = 0;
519
    size_t dst_off = 0;
572
    size_t dst_off = 0;
520
   
573
   
521
    while ((ch = src[src_idx++]) != 0) {
574
    while ((ch = src[src_idx++]) != 0) {
522
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
575
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
523
            break;
576
            break;
524
    }
577
    }
525
   
578
   
526
    if (dst_off >= size)
579
    if (dst_off >= size)
527
        dst[size - 1] = 0;
580
        dst[size - 1] = 0;
528
    else
581
    else
529
        dst[dst_off] = 0;
582
        dst[dst_off] = 0;
530
}
583
}
531
 
584
 
532
/** Find first occurence of character in string.
585
/** Find first occurence of character in string.
533
 *
586
 *
534
 * @param str String to search.
587
 * @param str String to search.
535
 * @param ch  Character to look for.
588
 * @param ch  Character to look for.
536
 *
589
 *
537
 * @return Pointer to character in @a str or NULL if not found.
590
 * @return Pointer to character in @a str or NULL if not found.
538
 *
-
 
539
 */
591
 */
540
const char *str_chr(const char *str, wchar_t ch)
592
const char *str_chr(const char *str, wchar_t ch)
541
{
593
{
542
    wchar_t acc;
594
    wchar_t acc;
543
    size_t off = 0;
595
    size_t off = 0;
-
 
596
    size_t last = 0;
544
   
597
   
545
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
598
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
546
        if (acc == ch)
599
        if (acc == ch)
547
            return (str + off);
600
            return (str + last);
-
 
601
        last = off;
548
    }
602
    }
549
   
603
   
550
    return NULL;
604
    return NULL;
551
}
605
}
552
 
606
 
-
 
607
/** Find last occurence of character in string.
-
 
608
 *
-
 
609
 * @param str String to search.
-
 
610
 * @param ch  Character to look for.
-
 
611
 *
-
 
612
 * @return Pointer to character in @a str or NULL if not found.
-
 
613
 */
-
 
614
const char *str_rchr(const char *str, wchar_t ch)
-
 
615
{
-
 
616
    wchar_t acc;
-
 
617
    size_t off = 0;
-
 
618
    size_t last = 0;
-
 
619
    char *res = NULL;
-
 
620
   
-
 
621
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
-
 
622
        if (acc == ch)
-
 
623
            res = (str + last);
-
 
624
        last = off;
-
 
625
    }
-
 
626
   
-
 
627
    return res;
-
 
628
}
-
 
629
 
553
/** Insert a wide character into a wide string.
630
/** Insert a wide character into a wide string.
554
 *
631
 *
555
 * Insert a wide character into a wide string at position
632
 * Insert a wide character into a wide string at position
556
 * @a pos. The characters after the position are shifted.
633
 * @a pos. The characters after the position are shifted.
557
 *
634
 *
558
 * @param str     String to insert to.
635
 * @param str     String to insert to.
559
 * @param ch      Character to insert to.
636
 * @param ch      Character to insert to.
560
 * @param pos     Character index where to insert.
637
 * @param pos     Character index where to insert.
561
 @ @param max_pos Characters in the buffer.
638
 @ @param max_pos Characters in the buffer.
562
 *
639
 *
563
 * @return True if the insertion was sucessful, false if the position
640
 * @return True if the insertion was sucessful, false if the position
564
 *         is out of bounds.
641
 *         is out of bounds.
565
 *
642
 *
566
 */
643
 */
567
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
644
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
568
{
645
{
569
    count_t len = wstr_length(str);
646
    count_t len = wstr_length(str);
570
   
647
   
571
    if ((pos > len) || (pos + 1 > max_pos))
648
    if ((pos > len) || (pos + 1 > max_pos))
572
        return false;
649
        return false;
573
   
650
   
574
    count_t i;
651
    count_t i;
575
    for (i = len; i + 1 > pos; i--)
652
    for (i = len; i + 1 > pos; i--)
576
        str[i + 1] = str[i];
653
        str[i + 1] = str[i];
577
   
654
   
578
    str[pos] = ch;
655
    str[pos] = ch;
579
   
656
   
580
    return true;
657
    return true;
581
}
658
}
582
 
659
 
583
/** Remove a wide character from a wide string.
660
/** Remove a wide character from a wide string.
584
 *
661
 *
585
 * Remove a wide character from a wide string at position
662
 * Remove a wide character from a wide string at position
586
 * @a pos. The characters after the position are shifted.
663
 * @a pos. The characters after the position are shifted.
587
 *
664
 *
588
 * @param str String to remove from.
665
 * @param str String to remove from.
589
 * @param pos Character index to remove.
666
 * @param pos Character index to remove.
590
 *
667
 *
591
 * @return True if the removal was sucessful, false if the position
668
 * @return True if the removal was sucessful, false if the position
592
 *         is out of bounds.
669
 *         is out of bounds.
593
 *
670
 *
594
 */
671
 */
595
bool wstr_remove(wchar_t *str, count_t pos)
672
bool wstr_remove(wchar_t *str, count_t pos)
596
{
673
{
597
    count_t len = wstr_length(str);
674
    count_t len = wstr_length(str);
598
   
675
   
599
    if (pos >= len)
676
    if (pos >= len)
600
        return false;
677
        return false;
601
   
678
   
602
    count_t i;
679
    count_t i;
603
    for (i = pos + 1; i <= len; i++)
680
    for (i = pos + 1; i <= len; i++)
604
        str[i - 1] = str[i];
681
        str[i - 1] = str[i];
605
   
682
   
606
    return true;
683
    return true;
607
}
684
}
608
 
685
 
609
/** Count the number of characters in the string, not including terminating 0.
-
 
610
 *
-
 
611
 * @param str       String.
-
 
612
 * @return      Number of characters in string.
-
 
613
 */
-
 
614
size_t strlen(const char *str)
-
 
615
{
-
 
616
    size_t counter = 0;
-
 
617
 
-
 
618
    while (str[counter] != 0)
-
 
619
        counter++;
-
 
620
 
-
 
621
    return counter;
-
 
622
}
-
 
623
 
-
 
624
int strcmp(const char *a, const char *b)
-
 
625
{
-
 
626
    int c = 0;
-
 
627
   
-
 
628
    while (a[c] && b[c] && (!(a[c] - b[c])))
-
 
629
        c++;
-
 
630
   
-
 
631
    return (a[c] - b[c]);
-
 
632
}
-
 
633
 
-
 
634
int strncmp(const char *a, const char *b, size_t n)
-
 
635
{
-
 
636
    size_t c = 0;
-
 
637
 
-
 
638
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
-
 
639
        c++;
-
 
640
   
-
 
641
    return ( c < n ? a[c] - b[c] : 0);
-
 
642
   
-
 
643
}
-
 
644
 
-
 
645
int stricmp(const char *a, const char *b)
686
int stricmp(const char *a, const char *b)
646
{
687
{
647
    int c = 0;
688
    int c = 0;
648
   
689
   
649
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
690
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
650
        c++;
691
        c++;
651
   
692
   
652
    return (tolower(a[c]) - tolower(b[c]));
693
    return (tolower(a[c]) - tolower(b[c]));
653
}
694
}
654
 
695
 
655
/** Return pointer to the first occurence of character c in string.
-
 
656
 *
-
 
657
 * @param str       Scanned string.
-
 
658
 * @param c     Searched character (taken as one byte).
-
 
659
 * @return      Pointer to the matched character or NULL if it is not
-
 
660
 *          found in given string.
-
 
661
 */
-
 
662
char *strchr(const char *str, int c)
-
 
663
{
-
 
664
    while (*str != '\0') {
-
 
665
        if (*str == (char) c)
-
 
666
            return (char *) str;
-
 
667
        str++;
-
 
668
    }
-
 
669
 
-
 
670
    return NULL;
-
 
671
}
-
 
672
 
-
 
673
/** Return pointer to the last occurence of character c in string.
-
 
674
 *
-
 
675
 * @param str       Scanned string.
-
 
676
 * @param c     Searched character (taken as one byte).
-
 
677
 * @return      Pointer to the matched character or NULL if it is not
-
 
678
 *          found in given string.
-
 
679
 */
-
 
680
char *strrchr(const char *str, int c)
-
 
681
{
-
 
682
    char *retval = NULL;
-
 
683
 
-
 
684
    while (*str != '\0') {
-
 
685
        if (*str == (char) c)
-
 
686
            retval = (char *) str;
-
 
687
        str++;
-
 
688
    }
-
 
689
 
-
 
690
    return (char *) retval;
-
 
691
}
-
 
692
 
-
 
693
/** Convert string to a number.
696
/** Convert string to a number.
694
 * Core of strtol and strtoul functions.
697
 * Core of strtol and strtoul functions.
695
 *
698
 *
696
 * @param nptr      Pointer to string.
699
 * @param nptr      Pointer to string.
697
 * @param endptr    If not NULL, function stores here pointer to the first
700
 * @param endptr    If not NULL, function stores here pointer to the first
698
 *          invalid character.
701
 *          invalid character.
699
 * @param base      Zero or number between 2 and 36 inclusive.
702
 * @param base      Zero or number between 2 and 36 inclusive.
700
 * @param sgn       It's set to 1 if minus found.
703
 * @param sgn       It's set to 1 if minus found.
701
 * @return      Result of conversion.
704
 * @return      Result of conversion.
702
 */
705
 */
703
static unsigned long
706
static unsigned long
704
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
707
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
705
{
708
{
706
    unsigned char c;
709
    unsigned char c;
707
    unsigned long result = 0;
710
    unsigned long result = 0;
708
    unsigned long a, b;
711
    unsigned long a, b;
709
    const char *str = nptr;
712
    const char *str = nptr;
710
    const char *tmpptr;
713
    const char *tmpptr;
711
   
714
   
712
    while (isspace(*str))
715
    while (isspace(*str))
713
        str++;
716
        str++;
714
   
717
   
715
    if (*str == '-') {
718
    if (*str == '-') {
716
        *sgn = 1;
719
        *sgn = 1;
717
        ++str;
720
        ++str;
718
    } else if (*str == '+')
721
    } else if (*str == '+')
719
        ++str;
722
        ++str;
720
   
723
   
721
    if (base) {
724
    if (base) {
722
        if ((base == 1) || (base > 36)) {
725
        if ((base == 1) || (base > 36)) {
723
            /* FIXME: set errno to EINVAL */
726
            /* FIXME: set errno to EINVAL */
724
            return 0;
727
            return 0;
725
        }
728
        }
726
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
729
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
727
            (str[1] == 'X'))) {
730
            (str[1] == 'X'))) {
728
            str += 2;
731
            str += 2;
729
        }
732
        }
730
    } else {
733
    } else {
731
        base = 10;
734
        base = 10;
732
       
735
       
733
        if (*str == '0') {
736
        if (*str == '0') {
734
            base = 8;
737
            base = 8;
735
            if ((str[1] == 'X') || (str[1] == 'x'))  {
738
            if ((str[1] == 'X') || (str[1] == 'x'))  {
736
                base = 16;
739
                base = 16;
737
                str += 2;
740
                str += 2;
738
            }
741
            }
739
        }
742
        }
740
    }
743
    }
741
   
744
   
742
    tmpptr = str;
745
    tmpptr = str;
743
 
746
 
744
    while (*str) {
747
    while (*str) {
745
        c = *str;
748
        c = *str;
746
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
749
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
747
            (c <= '9' ? c - '0' : 0xff)));
750
            (c <= '9' ? c - '0' : 0xff)));
748
        if (c > base) {
751
        if (c > base) {
749
            break;
752
            break;
750
        }
753
        }
751
       
754
       
752
        a = (result & 0xff) * base + c;
755
        a = (result & 0xff) * base + c;
753
        b = (result >> 8) * base + (a >> 8);
756
        b = (result >> 8) * base + (a >> 8);
754
       
757
       
755
        if (b > (ULONG_MAX >> 8)) {
758
        if (b > (ULONG_MAX >> 8)) {
756
            /* overflow */
759
            /* overflow */
757
            /* FIXME: errno = ERANGE*/
760
            /* FIXME: errno = ERANGE*/
758
            return ULONG_MAX;
761
            return ULONG_MAX;
759
        }
762
        }
760
   
763
   
761
        result = (b << 8) + (a & 0xff);
764
        result = (b << 8) + (a & 0xff);
762
        ++str;
765
        ++str;
763
    }
766
    }
764
   
767
   
765
    if (str == tmpptr) {
768
    if (str == tmpptr) {
766
        /*
769
        /*
767
         * No number was found => first invalid character is the first
770
         * No number was found => first invalid character is the first
768
         * character of the string.
771
         * character of the string.
769
         */
772
         */
770
        /* FIXME: set errno to EINVAL */
773
        /* FIXME: set errno to EINVAL */
771
        str = nptr;
774
        str = nptr;
772
        result = 0;
775
        result = 0;
773
    }
776
    }
774
   
777
   
775
    if (endptr)
778
    if (endptr)
776
        *endptr = (char *) str;
779
        *endptr = (char *) str;
777
 
780
 
778
    if (nptr == str) {
781
    if (nptr == str) {
779
        /*FIXME: errno = EINVAL*/
782
        /*FIXME: errno = EINVAL*/
780
        return 0;
783
        return 0;
781
    }
784
    }
782
 
785
 
783
    return result;
786
    return result;
784
}
787
}
785
 
788
 
786
/** Convert initial part of string to long int according to given base.
789
/** Convert initial part of string to long int according to given base.
787
 * The number may begin with an arbitrary number of whitespaces followed by
790
 * The number may begin with an arbitrary number of whitespaces followed by
788
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
791
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
789
 * inserted and the number will be taken as hexadecimal one. If the base is 0
792
 * inserted and the number will be taken as hexadecimal one. If the base is 0
790
 * and the number begin with a zero, number will be taken as octal one (as with
793
 * and the number begin with a zero, number will be taken as octal one (as with
791
 * base 8). Otherwise the base 0 is taken as decimal.
794
 * base 8). Otherwise the base 0 is taken as decimal.
792
 *
795
 *
793
 * @param nptr      Pointer to string.
796
 * @param nptr      Pointer to string.
794
 * @param endptr    If not NULL, function stores here pointer to the first
797
 * @param endptr    If not NULL, function stores here pointer to the first
795
 *          invalid character.
798
 *          invalid character.
796
 * @param base      Zero or number between 2 and 36 inclusive.
799
 * @param base      Zero or number between 2 and 36 inclusive.
797
 * @return      Result of conversion.
800
 * @return      Result of conversion.
798
 */
801
 */
799
long int strtol(const char *nptr, char **endptr, int base)
802
long int strtol(const char *nptr, char **endptr, int base)
800
{
803
{
801
    char sgn = 0;
804
    char sgn = 0;
802
    unsigned long number = 0;
805
    unsigned long number = 0;
803
   
806
   
804
    number = _strtoul(nptr, endptr, base, &sgn);
807
    number = _strtoul(nptr, endptr, base, &sgn);
805
 
808
 
806
    if (number > LONG_MAX) {
809
    if (number > LONG_MAX) {
807
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
810
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
808
            /* FIXME: set 0 to errno */
811
            /* FIXME: set 0 to errno */
809
            return number;     
812
            return number;     
810
        }
813
        }
811
        /* FIXME: set ERANGE to errno */
814
        /* FIXME: set ERANGE to errno */
812
        return (sgn ? LONG_MIN : LONG_MAX);
815
        return (sgn ? LONG_MIN : LONG_MAX);
813
    }
816
    }
814
   
817
   
815
    return (sgn ? -number : number);
818
    return (sgn ? -number : number);
816
}
819
}
817
 
820
 
818
 
821
 
819
/** Convert initial part of string to unsigned long according to given base.
822
/** Convert initial part of string to unsigned long according to given base.
820
 * The number may begin with an arbitrary number of whitespaces followed by
823
 * The number may begin with an arbitrary number of whitespaces followed by
821
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
824
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
822
 * inserted and the number will be taken as hexadecimal one. If the base is 0
825
 * inserted and the number will be taken as hexadecimal one. If the base is 0
823
 * and the number begin with a zero, number will be taken as octal one (as with
826
 * and the number begin with a zero, number will be taken as octal one (as with
824
 * base 8). Otherwise the base 0 is taken as decimal.
827
 * base 8). Otherwise the base 0 is taken as decimal.
825
 *
828
 *
826
 * @param nptr      Pointer to string.
829
 * @param nptr      Pointer to string.
827
 * @param endptr    If not NULL, function stores here pointer to the first
830
 * @param endptr    If not NULL, function stores here pointer to the first
828
 *          invalid character
831
 *          invalid character
829
 * @param base      Zero or number between 2 and 36 inclusive.
832
 * @param base      Zero or number between 2 and 36 inclusive.
830
 * @return      Result of conversion.
833
 * @return      Result of conversion.
831
 */
834
 */
832
unsigned long strtoul(const char *nptr, char **endptr, int base)
835
unsigned long strtoul(const char *nptr, char **endptr, int base)
833
{
836
{
834
    char sgn = 0;
837
    char sgn = 0;
835
    unsigned long number = 0;
838
    unsigned long number = 0;
836
   
839
   
837
    number = _strtoul(nptr, endptr, base, &sgn);
840
    number = _strtoul(nptr, endptr, base, &sgn);
838
 
841
 
839
    return (sgn ? -number : number);
842
    return (sgn ? -number : number);
840
}
843
}
841
 
844
 
842
char *strcpy(char *dest, const char *src)
-
 
843
{
-
 
844
    char *orig = dest;
-
 
845
   
-
 
846
    while ((*(dest++) = *(src++)))
-
 
847
        ;
-
 
848
    return orig;
-
 
849
}
-
 
850
 
-
 
851
char *strncpy(char *dest, const char *src, size_t n)
-
 
852
{
-
 
853
    char *orig = dest;
-
 
854
   
-
 
855
    while ((*(dest++) = *(src++)) && --n)
-
 
856
        ;
-
 
857
    return orig;
-
 
858
}
-
 
859
 
-
 
860
char *strcat(char *dest, const char *src)
-
 
861
{
-
 
862
    char *orig = dest;
-
 
863
    while (*dest++)
-
 
864
        ;
-
 
865
    --dest;
-
 
866
    while ((*dest++ = *src++))
-
 
867
        ;
-
 
868
    return orig;
-
 
869
}
-
 
870
 
-
 
871
char * strdup(const char *s1)
845
char *str_dup(const char *src)
872
{
846
{
873
    size_t len = strlen(s1) + 1;
847
    size_t size = str_size(src);
874
    void *ret = malloc(len);
848
    void *dest = malloc(size + 1);
875
 
849
 
876
    if (ret == NULL)
850
    if (dest == NULL)
877
        return (char *) NULL;
851
        return (char *) NULL;
878
 
852
 
879
    return (char *) memcpy(ret, s1, len);
853
    return (char *) memcpy(dest, src, size + 1);
880
}
854
}
881
 
855
 
882
char *strtok(char *s, const char *delim)
856
char *strtok(char *s, const char *delim)
883
{
857
{
884
    static char *next;
858
    static char *next;
885
 
859
 
886
    return strtok_r(s, delim, &next);
860
    return strtok_r(s, delim, &next);
887
}
861
}
888
 
862
 
889
char *strtok_r(char *s, const char *delim, char **next)
863
char *strtok_r(char *s, const char *delim, char **next)
890
{
864
{
891
    char *start, *end;
865
    char *start, *end;
892
 
866
 
893
    if (s == NULL)
867
    if (s == NULL)
894
        s = *next;
868
        s = *next;
895
 
869
 
896
    /* Skip over leading delimiters. */
870
    /* Skip over leading delimiters. */
897
    while (*s && (strchr(delim, *s) != NULL)) ++s;
871
    while (*s && (str_chr(delim, *s) != NULL)) ++s;
898
    start = s;
872
    start = s;
899
 
873
 
900
    /* Skip over token characters. */
874
    /* Skip over token characters. */
901
    while (*s && (strchr(delim, *s) == NULL)) ++s;
875
    while (*s && (str_chr(delim, *s) == NULL)) ++s;
902
    end = s;
876
    end = s;
903
    *next = (*s ? s + 1 : s);
877
    *next = (*s ? s + 1 : s);
904
 
878
 
905
    if (start == end) {
879
    if (start == end) {
906
        return NULL;    /* No more tokens. */
880
        return NULL;    /* No more tokens. */
907
    }
881
    }
908
 
882
 
909
    /* Overwrite delimiter with NULL terminator. */
883
    /* Overwrite delimiter with NULL terminator. */
910
    *end = '\0';
884
    *end = '\0';
911
    return start;
885
    return start;
912
}
886
}
913
 
887
 
914
/** @}
888
/** @}
915
 */
889
 */
916
 
890