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