2 * Copyright (c) 2004, 2006, 2007, 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 utf8toutf32(const unsigned char **pp
, uint32_t *out
)
40 const unsigned char *p
= *pp
;
44 if ((c
& 0xE0) == 0xC0) {
45 const unsigned c2
= *++p
;
46 if ((c2
& 0xC0) == 0x80) {
47 *out
= ((c
& 0x1F) << 6)
50 return WIND_ERR_INVALID_UTF8
;
52 } else if ((c
& 0xF0) == 0xE0) {
53 const unsigned c2
= *++p
;
54 if ((c2
& 0xC0) == 0x80) {
55 const unsigned c3
= *++p
;
56 if ((c3
& 0xC0) == 0x80) {
57 *out
= ((c
& 0x0F) << 12)
61 return WIND_ERR_INVALID_UTF8
;
64 return WIND_ERR_INVALID_UTF8
;
66 } else if ((c
& 0xF8) == 0xF0) {
67 const unsigned c2
= *++p
;
68 if ((c2
& 0xC0) == 0x80) {
69 const unsigned c3
= *++p
;
70 if ((c3
& 0xC0) == 0x80) {
71 const unsigned c4
= *++p
;
72 if ((c4
& 0xC0) == 0x80) {
73 *out
= ((c
& 0x07) << 18)
78 return WIND_ERR_INVALID_UTF8
;
81 return WIND_ERR_INVALID_UTF8
;
84 return WIND_ERR_INVALID_UTF8
;
87 return WIND_ERR_INVALID_UTF8
;
99 * Convert an UTF-8 string to an UCS4 string.
101 * @param in an UTF-8 string to convert.
102 * @param out the resulting UCS4 strint, must be at least
103 * wind_utf8ucs4_length() long. If out is NULL, the function will
104 * calculate the needed space for the out variable (just like
105 * wind_utf8ucs4_length()).
106 * @param out_len before processing out_len should be the length of
107 * the out variable, after processing it will be the length of the out
110 * @return returns 0 on success, an wind error code otherwise
115 wind_utf8ucs4(const char *in
, uint32_t *out
, size_t *out_len
)
117 const unsigned char *p
;
121 for (p
= (const unsigned char *)in
; *p
!= '\0'; ++p
) {
124 ret
= utf8toutf32(&p
, &u
);
130 return WIND_ERR_OVERRUN
;
140 * Calculate the length of from converting a UTF-8 string to a UCS4
143 * @param in an UTF-8 string to convert.
144 * @param out_len the length of the resulting UCS4 string.
146 * @return returns 0 on success, an wind error code otherwise
151 wind_utf8ucs4_length(const char *in
, size_t *out_len
)
153 return wind_utf8ucs4(in
, NULL
, out_len
);
156 static const char first_char
[4] =
157 { 0x00, 0xC0, 0xE0, 0xF0 };
160 * Convert an UCS4 string to a UTF-8 string.
162 * @param in an UCS4 string to convert.
163 * @param in_len the length input array.
165 * @param out the resulting UTF-8 strint, must be at least
166 * wind_ucs4utf8_length() + 1 long (the extra char for the NUL). If
167 * out is NULL, the function will calculate the needed space for the
168 * out variable (just like wind_ucs4utf8_length()).
170 * @param out_len before processing out_len should be the length of
171 * the out variable, after processing it will be the length of the out
174 * @return returns 0 on success, an wind error code otherwise
179 wind_ucs4utf8(const uint32_t *in
, size_t in_len
, char *out
, size_t *out_len
)
184 for (o
= 0, i
= 0; i
< in_len
; i
++) {
189 } else if (ch
< 0x800) {
191 } else if (ch
< 0x10000) {
193 } else if (ch
<= 0x10FFFF) {
196 return WIND_ERR_INVALID_UTF32
;
202 return WIND_ERR_OVERRUN
;
206 out
[3] = (ch
| 0x80) & 0xbf;
210 out
[2] = (ch
| 0x80) & 0xbf;
214 out
[1] = (ch
| 0x80) & 0xbf;
218 out
[0] = ch
| first_char
[len
- 1];
227 if (o
+ 1 >= *out_len
)
228 return WIND_ERR_OVERRUN
;
236 * Calculate the length of from converting a UCS4 string to an UTF-8 string.
238 * @param in an UCS4 string to convert.
239 * @param in_len the length of UCS4 string to convert.
240 * @param out_len the length of the resulting UTF-8 string.
242 * @return returns 0 on success, an wind error code otherwise
247 wind_ucs4utf8_length(const uint32_t *in
, size_t in_len
, size_t *out_len
)
249 return wind_ucs4utf8(in
, in_len
, NULL
, out_len
);
253 * Read in an UCS2 from a buffer.
255 * @param ptr The input buffer to read from.
256 * @param len the length of the input buffer.
257 * @param flags Flags to control the behavior of the function.
258 * @param out the output UCS2, the array must be at least out/2 long.
259 * @param out_len the output length
261 * @return returns 0 on success, an wind error code otherwise.
266 wind_ucs2read(const void *ptr
, size_t len
, unsigned int *flags
,
267 uint16_t *out
, size_t *out_len
)
269 const unsigned char *p
= ptr
;
270 int little
= ((*flags
) & WIND_RW_LE
);
271 size_t olen
= *out_len
;
273 /** if len is zero, flags are unchanged */
279 /** if len is odd, WIND_ERR_LENGTH_NOT_MOD2 is returned */
281 return WIND_ERR_LENGTH_NOT_MOD2
;
284 * If the flags WIND_RW_BOM is set, check for BOM. If not BOM is
285 * found, check is LE/BE flag is already and use that otherwise
286 * fail with WIND_ERR_NO_BOM. When done, clear WIND_RW_BOM and
287 * the LE/BE flag and set the resulting LE/BE flag.
289 if ((*flags
) & WIND_RW_BOM
) {
290 uint16_t bom
= (p
[0] << 8) + p
[1];
291 if (bom
== 0xfffe || bom
== 0xfeff) {
292 little
= (bom
== 0xfffe);
295 } else if (((*flags
) & (WIND_RW_LE
|WIND_RW_BE
)) != 0) {
296 /* little already set */
298 return WIND_ERR_NO_BOM
;
299 *flags
= ((*flags
) & ~(WIND_RW_BOM
|WIND_RW_LE
|WIND_RW_BE
));
300 *flags
|= little
? WIND_RW_LE
: WIND_RW_BE
;
305 return WIND_ERR_OVERRUN
;
307 *out
= (p
[1] << 8) + p
[0];
309 *out
= (p
[0] << 8) + p
[1];
310 out
++; p
+= 2; len
-= 2; olen
--;
317 * Write an UCS2 string to a buffer.
319 * @param in The input UCS2 string.
320 * @param in_len the length of the input buffer.
321 * @param flags Flags to control the behavior of the function.
322 * @param ptr The input buffer to write to, the array must be at least
323 * (in + 1) * 2 bytes long.
324 * @param out_len the output length
326 * @return returns 0 on success, an wind error code otherwise.
331 wind_ucs2write(const uint16_t *in
, size_t in_len
, unsigned int *flags
,
332 void *ptr
, size_t *out_len
)
334 unsigned char *p
= ptr
;
335 size_t len
= *out_len
;
337 /** If in buffer is not of length be mod 2, WIND_ERR_LENGTH_NOT_MOD2 is returned*/
339 return WIND_ERR_LENGTH_NOT_MOD2
;
341 /** On zero input length, flags are preserved */
346 /** If flags have WIND_RW_BOM set, the byte order mark is written
347 * first to the output data */
348 if ((*flags
) & WIND_RW_BOM
) {
349 uint16_t bom
= 0xfffe;
352 return WIND_ERR_OVERRUN
;
354 if ((*flags
) & WIND_RW_LE
) {
355 p
[0] = (bom
) & 0xff;
356 p
[1] = (bom
>> 8) & 0xff;
358 p
[1] = (bom
) & 0xff;
359 p
[0] = (bom
>> 8) & 0xff;
365 /** If the output wont fit into out_len, WIND_ERR_OVERRUN is returned */
367 return WIND_ERR_OVERRUN
;
368 if ((*flags
) & WIND_RW_LE
) {
369 p
[0] = (in
[0] ) & 0xff;
370 p
[1] = (in
[0] >> 8) & 0xff;
372 p
[1] = (in
[0] ) & 0xff;
373 p
[0] = (in
[0] >> 8) & 0xff;
386 * Convert an UTF-8 string to an UCS2 string.
388 * @param in an UTF-8 string to convert.
389 * @param out the resulting UCS2 strint, must be at least
390 * wind_utf8ucs2_length() long. If out is NULL, the function will
391 * calculate the needed space for the out variable (just like
392 * wind_utf8ucs2_length()).
393 * @param out_len before processing out_len should be the length of
394 * the out variable, after processing it will be the length of the out
397 * @return returns 0 on success, an wind error code otherwise
402 wind_utf8ucs2(const char *in
, uint16_t *out
, size_t *out_len
)
404 const unsigned char *p
;
408 for (p
= (const unsigned char *)in
; *p
!= '\0'; ++p
) {
411 ret
= utf8toutf32(&p
, &u
);
416 return WIND_ERR_NOT_UTF16
;
420 return WIND_ERR_OVERRUN
;
430 * Calculate the length of from converting a UTF-8 string to a UCS2
433 * @param in an UTF-8 string to convert.
434 * @param out_len the length of the resulting UCS4 string.
436 * @return returns 0 on success, an wind error code otherwise
441 wind_utf8ucs2_length(const char *in
, size_t *out_len
)
443 return wind_utf8ucs2(in
, NULL
, out_len
);
447 * Convert an UCS2 string to a UTF-8 string.
449 * @param in an UCS2 string to convert.
450 * @param in_len the length of the in UCS2 string.
451 * @param out the resulting UTF-8 strint, must be at least
452 * wind_ucs2utf8_length() long. If out is NULL, the function will
453 * calculate the needed space for the out variable (just like
454 * wind_ucs2utf8_length()).
455 * @param out_len before processing out_len should be the length of
456 * the out variable, after processing it will be the length of the out
459 * @return returns 0 on success, an wind error code otherwise
464 wind_ucs2utf8(const uint16_t *in
, size_t in_len
, char *out
, size_t *out_len
)
469 for (o
= 0, i
= 0; i
< in_len
; i
++) {
474 } else if (ch
< 0x800) {
483 return WIND_ERR_OVERRUN
;
487 out
[2] = (ch
| 0x80) & 0xbf;
491 out
[1] = (ch
| 0x80) & 0xbf;
495 out
[0] = ch
| first_char
[len
- 1];
505 return WIND_ERR_OVERRUN
;
513 * Calculate the length of from converting a UCS2 string to an UTF-8 string.
515 * @param in an UCS2 string to convert.
516 * @param in_len an UCS2 string length to convert.
517 * @param out_len the length of the resulting UTF-8 string.
519 * @return returns 0 on success, an wind error code otherwise
524 wind_ucs2utf8_length(const uint16_t *in
, size_t in_len
, size_t *out_len
)
526 return wind_ucs2utf8(in
, in_len
, NULL
, out_len
);