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