| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <errno.h> |
| 4 |
#include <iconv.h> |
| 5 |
|
| 6 |
static iconv_t s_cd; |
| 7 |
|
| 8 |
/* Call iconv_open only once so the benchmark will be faster? */ |
| 9 |
static void __attribute__ ((constructor)) init_iconv(void) |
| 10 |
{ |
| 11 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 12 |
s_cd = iconv_open("UTF-16LE", "UTF-8"); |
| 13 |
#else |
| 14 |
s_cd = iconv_open("UTF-16BE", "UTF-8"); |
| 15 |
#endif |
| 16 |
if (s_cd == (iconv_t)-1) { |
| 17 |
perror("iconv_open"); |
| 18 |
exit(1); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
/* |
| 23 |
* Parameters: |
| 24 |
* - buf8, len8: input utf-8 string |
| 25 |
* - buf16: buffer to store decoded utf-16 string |
| 26 |
* - *len16: on entry - utf-16 buffer length in bytes |
| 27 |
* on exit - length in bytes of valid decoded utf-16 string |
| 28 |
* Returns: |
| 29 |
* - 0: success |
| 30 |
* - >0: error position of input utf-8 string |
| 31 |
* - -1: utf-16 buffer overflow |
| 32 |
* LE/BE depends on host |
| 33 |
*/ |
| 34 |
int utf8_to16_iconv(const unsigned char *buf8, size_t len8, |
| 35 |
unsigned short *buf16, size_t *len16) |
| 36 |
{ |
| 37 |
size_t ret, len16_save = *len16; |
| 38 |
const unsigned char *buf8_0 = buf8; |
| 39 |
|
| 40 |
ret = iconv(s_cd, (char **)&buf8, &len8, (char **)&buf16, len16); |
| 41 |
|
| 42 |
*len16 = len16_save - *len16; |
| 43 |
|
| 44 |
if (ret != (size_t)-1) |
| 45 |
return 0; |
| 46 |
|
| 47 |
if (errno == E2BIG) |
| 48 |
return -1; /* Output buffer full */ |
| 49 |
|
| 50 |
return buf8 - buf8_0 + 1; /* EILSEQ, EINVAL, error position */ |
| 51 |
} |