| 1 |
/* |
| 2 |
* Copyright (c) 2004, 2006, 2007, 2008 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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 |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
#include "windlocl.h" |
| 36 |
|
| 37 |
static int |
| 38 |
utf8toutf32(const unsigned char **pp, uint32_t *out) |
| 39 |
{ |
| 40 |
const unsigned char *p = *pp; |
| 41 |
unsigned c = *p; |
| 42 |
|
| 43 |
if (c & 0x80) { |
| 44 |
if ((c & 0xE0) == 0xC0) { |
| 45 |
const unsigned c2 = *++p; |
| 46 |
if ((c2 & 0xC0) == 0x80) { |
| 47 |
*out = ((c & 0x1F) << 6) |
| 48 |
| (c2 & 0x3F); |
| 49 |
} else { |
| 50 |
return WIND_ERR_INVALID_UTF8; |
| 51 |
} |
| 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) |
| 58 |
| ((c2 & 0x3F) << 6) |
| 59 |
| (c3 & 0x3F); |
| 60 |
} else { |
| 61 |
return WIND_ERR_INVALID_UTF8; |
| 62 |
} |
| 63 |
} else { |
| 64 |
return WIND_ERR_INVALID_UTF8; |
| 65 |
} |
| 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) |
| 74 |
| ((c2 & 0x3F) << 12) |
| 75 |
| ((c3 & 0x3F) << 6) |
| 76 |
| (c4 & 0x3F); |
| 77 |
} else { |
| 78 |
return WIND_ERR_INVALID_UTF8; |
| 79 |
} |
| 80 |
} else { |
| 81 |
return WIND_ERR_INVALID_UTF8; |
| 82 |
} |
| 83 |
} else { |
| 84 |
return WIND_ERR_INVALID_UTF8; |
| 85 |
} |
| 86 |
} else { |
| 87 |
return WIND_ERR_INVALID_UTF8; |
| 88 |
} |
| 89 |
} else { |
| 90 |
*out = c; |
| 91 |
} |
| 92 |
|
| 93 |
*pp = p; |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Convert an UTF-8 string to an UCS4 string. |
| 100 |
* |
| 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 |
| 108 |
* string. |
| 109 |
* |
| 110 |
* @return returns 0 on success, an wind error code otherwise |
| 111 |
* @ingroup wind |
| 112 |
*/ |
| 113 |
|
| 114 |
int |
| 115 |
wind_utf8ucs4(const char *in, uint32_t *out, size_t *out_len) |
| 116 |
{ |
| 117 |
const unsigned char *p; |
| 118 |
size_t o = 0; |
| 119 |
int ret; |
| 120 |
|
| 121 |
for (p = (const unsigned char *)in; *p != '\0'; ++p) { |
| 122 |
uint32_t u; |
| 123 |
|
| 124 |
ret = utf8toutf32(&p, &u); |
| 125 |
if (ret) |
| 126 |
return ret; |
| 127 |
|
| 128 |
if (out) { |
| 129 |
if (o >= *out_len) |
| 130 |
return WIND_ERR_OVERRUN; |
| 131 |
out[o] = u; |
| 132 |
} |
| 133 |
o++; |
| 134 |
} |
| 135 |
*out_len = o; |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Calculate the length of from converting a UTF-8 string to a UCS4 |
| 141 |
* string. |
| 142 |
* |
| 143 |
* @param in an UTF-8 string to convert. |
| 144 |
* @param out_len the length of the resulting UCS4 string. |
| 145 |
* |
| 146 |
* @return returns 0 on success, an wind error code otherwise |
| 147 |
* @ingroup wind |
| 148 |
*/ |
| 149 |
|
| 150 |
int |
| 151 |
wind_utf8ucs4_length(const char *in, size_t *out_len) |
| 152 |
{ |
| 153 |
return wind_utf8ucs4(in, NULL, out_len); |
| 154 |
} |
| 155 |
|
| 156 |
static const char first_char[4] = |
| 157 |
{ 0x00, 0xC0, 0xE0, 0xF0 }; |
| 158 |
|
| 159 |
/** |
| 160 |
* Convert an UCS4 string to a UTF-8 string. |
| 161 |
* |
| 162 |
* @param in an UCS4 string to convert. |
| 163 |
* @param in_len the length input array. |
| 164 |
|
| 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()). |
| 169 |
|
| 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 |
| 172 |
* string. |
| 173 |
* |
| 174 |
* @return returns 0 on success, an wind error code otherwise |
| 175 |
* @ingroup wind |
| 176 |
*/ |
| 177 |
|
| 178 |
int |
| 179 |
wind_ucs4utf8(const uint32_t *in, size_t in_len, char *out, size_t *out_len) |
| 180 |
{ |
| 181 |
uint32_t ch; |
| 182 |
size_t i, len, o; |
| 183 |
|
| 184 |
for (o = 0, i = 0; i < in_len; i++) { |
| 185 |
ch = in[i]; |
| 186 |
|
| 187 |
if (ch < 0x80) { |
| 188 |
len = 1; |
| 189 |
} else if (ch < 0x800) { |
| 190 |
len = 2; |
| 191 |
} else if (ch < 0x10000) { |
| 192 |
len = 3; |
| 193 |
} else if (ch <= 0x10FFFF) { |
| 194 |
len = 4; |
| 195 |
} else |
| 196 |
return WIND_ERR_INVALID_UTF32; |
| 197 |
|
| 198 |
o += len; |
| 199 |
|
| 200 |
if (out) { |
| 201 |
if (o >= *out_len) |
| 202 |
return WIND_ERR_OVERRUN; |
| 203 |
|
| 204 |
switch(len) { |
| 205 |
case 4: |
| 206 |
out[3] = (ch | 0x80) & 0xbf; |
| 207 |
ch = ch >> 6; |
| 208 |
/* FALLTHROUGH */ |
| 209 |
case 3: |
| 210 |
out[2] = (ch | 0x80) & 0xbf; |
| 211 |
ch = ch >> 6; |
| 212 |
/* FALLTHROUGH */ |
| 213 |
case 2: |
| 214 |
out[1] = (ch | 0x80) & 0xbf; |
| 215 |
ch = ch >> 6; |
| 216 |
/* FALLTHROUGH */ |
| 217 |
case 1: |
| 218 |
out[0] = ch | first_char[len - 1]; |
| 219 |
/* FALLTHROUGH */ |
| 220 |
} |
| 221 |
} |
| 222 |
out += len; |
| 223 |
} |
| 224 |
if (out) { |
| 225 |
if (o + 1 >= *out_len) |
| 226 |
return WIND_ERR_OVERRUN; |
| 227 |
*out = '\0'; |
| 228 |
} |
| 229 |
*out_len = o; |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Calculate the length of from converting a UCS4 string to an UTF-8 string. |
| 235 |
* |
| 236 |
* @param in an UCS4 string to convert. |
| 237 |
* @param in_len the length of UCS4 string to convert. |
| 238 |
* @param out_len the length of the resulting UTF-8 string. |
| 239 |
* |
| 240 |
* @return returns 0 on success, an wind error code otherwise |
| 241 |
* @ingroup wind |
| 242 |
*/ |
| 243 |
|
| 244 |
int |
| 245 |
wind_ucs4utf8_length(const uint32_t *in, size_t in_len, size_t *out_len) |
| 246 |
{ |
| 247 |
return wind_ucs4utf8(in, in_len, NULL, out_len); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Read in an UCS2 from a buffer. |
| 252 |
* |
| 253 |
* @param ptr The input buffer to read from. |
| 254 |
* @param len the length of the input buffer. |
| 255 |
* @param flags Flags to control the behavior of the function. |
| 256 |
* @param out the output UCS2, the array must be at least out/2 long. |
| 257 |
* @param out_len the output length |
| 258 |
* |
| 259 |
* @return returns 0 on success, an wind error code otherwise. |
| 260 |
* @ingroup wind |
| 261 |
*/ |
| 262 |
|
| 263 |
int |
| 264 |
wind_ucs2read(const void *ptr, size_t len, unsigned int *flags, |
| 265 |
uint16_t *out, size_t *out_len) |
| 266 |
{ |
| 267 |
const unsigned char *p = ptr; |
| 268 |
int little = ((*flags) & WIND_RW_LE); |
| 269 |
size_t olen = *out_len; |
| 270 |
|
| 271 |
/** if len is zero, flags are unchanged */ |
| 272 |
if (len == 0) { |
| 273 |
*out_len = 0; |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
/** if len is odd, WIND_ERR_LENGTH_NOT_MOD2 is returned */ |
| 278 |
if (len & 1) |
| 279 |
return WIND_ERR_LENGTH_NOT_MOD2; |
| 280 |
|
| 281 |
/** |
| 282 |
* If the flags WIND_RW_BOM is set, check for BOM. If not BOM is |
| 283 |
* found, check is LE/BE flag is already and use that otherwise |
| 284 |
* fail with WIND_ERR_NO_BOM. When done, clear WIND_RW_BOM and |
| 285 |
* the LE/BE flag and set the resulting LE/BE flag. |
| 286 |
*/ |
| 287 |
if ((*flags) & WIND_RW_BOM) { |
| 288 |
uint16_t bom = (p[0] << 8) + p[1]; |
| 289 |
if (bom == 0xfffe || bom == 0xfeff) { |
| 290 |
little = (bom == 0xfffe); |
| 291 |
p += 2; |
| 292 |
len -= 2; |
| 293 |
} else if (((*flags) & (WIND_RW_LE|WIND_RW_BE)) != 0) { |
| 294 |
/* little already set */ |
| 295 |
} else |
| 296 |
return WIND_ERR_NO_BOM; |
| 297 |
*flags = ((*flags) & ~(WIND_RW_BOM|WIND_RW_LE|WIND_RW_BE)); |
| 298 |
*flags |= little ? WIND_RW_LE : WIND_RW_BE; |
| 299 |
} |
| 300 |
|
| 301 |
while (len) { |
| 302 |
if (olen < 1) |
| 303 |
return WIND_ERR_OVERRUN; |
| 304 |
if (little) |
| 305 |
*out = (p[1] << 8) + p[0]; |
| 306 |
else |
| 307 |
*out = (p[0] << 8) + p[1]; |
| 308 |
out++; p += 2; len -= 2; olen--; |
| 309 |
} |
| 310 |
*out_len -= olen; |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Write an UCS2 string to a buffer. |
| 316 |
* |
| 317 |
* @param in The input UCS2 string. |
| 318 |
* @param in_len the length of the input buffer. |
| 319 |
* @param flags Flags to control the behavior of the function. |
| 320 |
* @param ptr The input buffer to write to, the array must be at least |
| 321 |
* (in + 1) * 2 bytes long. |
| 322 |
* @param out_len the output length |
| 323 |
* |
| 324 |
* @return returns 0 on success, an wind error code otherwise. |
| 325 |
* @ingroup wind |
| 326 |
*/ |
| 327 |
|
| 328 |
int |
| 329 |
wind_ucs2write(const uint16_t *in, size_t in_len, unsigned int *flags, |
| 330 |
void *ptr, size_t *out_len) |
| 331 |
{ |
| 332 |
unsigned char *p = ptr; |
| 333 |
size_t len = *out_len; |
| 334 |
|
| 335 |
/** If in buffer is not of length be mod 2, WIND_ERR_LENGTH_NOT_MOD2 is returned*/ |
| 336 |
if (len & 1) |
| 337 |
return WIND_ERR_LENGTH_NOT_MOD2; |
| 338 |
|
| 339 |
/** On zero input length, flags are preserved */ |
| 340 |
if (in_len == 0) { |
| 341 |
*out_len = 0; |
| 342 |
return 0; |
| 343 |
} |
| 344 |
/** If flags have WIND_RW_BOM set, the byte order mark is written |
| 345 |
* first to the output data */ |
| 346 |
if ((*flags) & WIND_RW_BOM) { |
| 347 |
uint16_t bom = 0xfffe; |
| 348 |
|
| 349 |
if (len < 2) |
| 350 |
return WIND_ERR_OVERRUN; |
| 351 |
|
| 352 |
if ((*flags) & WIND_RW_LE) { |
| 353 |
p[0] = (bom ) & 0xff; |
| 354 |
p[1] = (bom >> 8) & 0xff; |
| 355 |
} else { |
| 356 |
p[1] = (bom ) & 0xff; |
| 357 |
p[0] = (bom >> 8) & 0xff; |
| 358 |
} |
| 359 |
len -= 2; |
| 360 |
} |
| 361 |
|
| 362 |
while (in_len) { |
| 363 |
/** If the output wont fit into out_len, WIND_ERR_OVERRUN is returned */ |
| 364 |
if (len < 2) |
| 365 |
return WIND_ERR_OVERRUN; |
| 366 |
if ((*flags) & WIND_RW_LE) { |
| 367 |
p[0] = (in[0] ) & 0xff; |
| 368 |
p[1] = (in[0] >> 8) & 0xff; |
| 369 |
} else { |
| 370 |
p[1] = (in[0] ) & 0xff; |
| 371 |
p[0] = (in[0] >> 8) & 0xff; |
| 372 |
} |
| 373 |
len -= 2; |
| 374 |
in_len--; |
| 375 |
p += 2; |
| 376 |
in++; |
| 377 |
} |
| 378 |
*out_len -= len; |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/** |
| 384 |
* Convert an UTF-8 string to an UCS2 string. |
| 385 |
* |
| 386 |
* @param in an UTF-8 string to convert. |
| 387 |
* @param out the resulting UCS2 strint, must be at least |
| 388 |
* wind_utf8ucs2_length() long. If out is NULL, the function will |
| 389 |
* calculate the needed space for the out variable (just like |
| 390 |
* wind_utf8ucs2_length()). |
| 391 |
* @param out_len before processing out_len should be the length of |
| 392 |
* the out variable, after processing it will be the length of the out |
| 393 |
* string. |
| 394 |
* |
| 395 |
* @return returns 0 on success, an wind error code otherwise |
| 396 |
* @ingroup wind |
| 397 |
*/ |
| 398 |
|
| 399 |
int |
| 400 |
wind_utf8ucs2(const char *in, uint16_t *out, size_t *out_len) |
| 401 |
{ |
| 402 |
const unsigned char *p; |
| 403 |
size_t o = 0; |
| 404 |
int ret; |
| 405 |
|
| 406 |
for (p = (const unsigned char *)in; *p != '\0'; ++p) { |
| 407 |
uint32_t u; |
| 408 |
|
| 409 |
ret = utf8toutf32(&p, &u); |
| 410 |
if (ret) |
| 411 |
return ret; |
| 412 |
|
| 413 |
if (u & 0xffff0000) |
| 414 |
return WIND_ERR_NOT_UTF16; |
| 415 |
|
| 416 |
if (out) { |
| 417 |
if (o >= *out_len) |
| 418 |
return WIND_ERR_OVERRUN; |
| 419 |
out[o] = u; |
| 420 |
} |
| 421 |
o++; |
| 422 |
} |
| 423 |
*out_len = o; |
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Calculate the length of from converting a UTF-8 string to a UCS2 |
| 429 |
* string. |
| 430 |
* |
| 431 |
* @param in an UTF-8 string to convert. |
| 432 |
* @param out_len the length of the resulting UCS4 string. |
| 433 |
* |
| 434 |
* @return returns 0 on success, an wind error code otherwise |
| 435 |
* @ingroup wind |
| 436 |
*/ |
| 437 |
|
| 438 |
int |
| 439 |
wind_utf8ucs2_length(const char *in, size_t *out_len) |
| 440 |
{ |
| 441 |
return wind_utf8ucs2(in, NULL, out_len); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Convert an UCS2 string to a UTF-8 string. |
| 446 |
* |
| 447 |
* @param in an UCS2 string to convert. |
| 448 |
* @param in_len the length of the in UCS2 string. |
| 449 |
* @param out the resulting UTF-8 strint, must be at least |
| 450 |
* wind_ucs2utf8_length() long. If out is NULL, the function will |
| 451 |
* calculate the needed space for the out variable (just like |
| 452 |
* wind_ucs2utf8_length()). |
| 453 |
* @param out_len before processing out_len should be the length of |
| 454 |
* the out variable, after processing it will be the length of the out |
| 455 |
* string. |
| 456 |
* |
| 457 |
* @return returns 0 on success, an wind error code otherwise |
| 458 |
* @ingroup wind |
| 459 |
*/ |
| 460 |
|
| 461 |
int |
| 462 |
wind_ucs2utf8(const uint16_t *in, size_t in_len, char *out, size_t *out_len) |
| 463 |
{ |
| 464 |
uint16_t ch; |
| 465 |
size_t i, len, o; |
| 466 |
|
| 467 |
for (o = 0, i = 0; i < in_len; i++) { |
| 468 |
ch = in[i]; |
| 469 |
|
| 470 |
if (ch < 0x80) { |
| 471 |
len = 1; |
| 472 |
} else if (ch < 0x800) { |
| 473 |
len = 2; |
| 474 |
} else |
| 475 |
len = 3; |
| 476 |
|
| 477 |
o += len; |
| 478 |
|
| 479 |
if (out) { |
| 480 |
if (o >= *out_len) |
| 481 |
return WIND_ERR_OVERRUN; |
| 482 |
|
| 483 |
switch(len) { |
| 484 |
case 3: |
| 485 |
out[2] = (ch | 0x80) & 0xbf; |
| 486 |
ch = ch >> 6; |
| 487 |
/* FALLTHROUGH */ |
| 488 |
case 2: |
| 489 |
out[1] = (ch | 0x80) & 0xbf; |
| 490 |
ch = ch >> 6; |
| 491 |
/* FALLTHROUGH */ |
| 492 |
case 1: |
| 493 |
out[0] = ch | first_char[len - 1]; |
| 494 |
/* FALLTHROUGH */ |
| 495 |
} |
| 496 |
out += len; |
| 497 |
} |
| 498 |
} |
| 499 |
if (out) { |
| 500 |
if (o >= *out_len) |
| 501 |
return WIND_ERR_OVERRUN; |
| 502 |
*out = '\0'; |
| 503 |
} |
| 504 |
*out_len = o; |
| 505 |
return 0; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Calculate the length of from converting a UCS2 string to an UTF-8 string. |
| 510 |
* |
| 511 |
* @param in an UCS2 string to convert. |
| 512 |
* @param in_len an UCS2 string length to convert. |
| 513 |
* @param out_len the length of the resulting UTF-8 string. |
| 514 |
* |
| 515 |
* @return returns 0 on success, an wind error code otherwise |
| 516 |
* @ingroup wind |
| 517 |
*/ |
| 518 |
|
| 519 |
int |
| 520 |
wind_ucs2utf8_length(const uint16_t *in, size_t in_len, size_t *out_len) |
| 521 |
{ |
| 522 |
return wind_ucs2utf8(in, in_len, NULL, out_len); |
| 523 |
} |