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