From 8d617a716df0ed5fd9ea1c8e65dd8e59b168573e Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Tue, 21 Mar 2000 20:18:34 +0000 Subject: Update. * iconv/gconv_builtin.c: Include . * iconv/gconv_builtin.h: Add UCS-BE aliases. Add UCS-4LE transformation. Define UNICODEBIG and UNICODELITTLE according to current platform. * iconv/gconv_int.h: Declare __gconv_transform_ucs2reverse_internal, __gconv_transform_internal_ucs2reverse, and __gconv_transform_internal_ucs4le. * iconv/gconv_simple.c: Implement __gconv_transform_internal_ucs4le, __gconv_transform_ucs2reverse_internal and __gconv_transform_internal_ucs2reverse. * iconvdata/Makefile (modules): Add UNICODE. (distribute): Add unicode.c. * iconvdata/gconv-modules: Add definitions for UNICODE module. * iconvdata/unicode.c: New file. * iconvdata/utf-16.c: Rewrite code to emit BOM. Correct code to determine byte order of input and convert accordingly. --- iconvdata/utf-16.c | 51 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'iconvdata/utf-16.c') diff --git a/iconvdata/utf-16.c b/iconvdata/utf-16.c index 55540c9849..c7e677e376 100644 --- a/iconvdata/utf-16.c +++ b/iconvdata/utf-16.c @@ -27,6 +27,8 @@ /* This is the Byte Order Mark character (BOM). */ #define BOM 0xfeff +/* And in the other byte order. */ +#define BOM_OE 0xfffe /* Definitions used in the body of the `gconv' function. */ @@ -41,8 +43,27 @@ #define PREPARE_LOOP \ enum direction dir = ((struct utf16_data *) step->__data)->dir; \ enum variant var = ((struct utf16_data *) step->__data)->var; \ - if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \ - && data->__invocation_counter == 0) \ + int swap = ((struct utf16_data *) step->__data)->swap; \ + if (FROM_DIRECTION || var == UTF_16) \ + { \ + if (data->__invocation_counter == 0) \ + { \ + /* We have to find out which byte order the file is encoded in. */ \ + if (inptr + 2 > inbufend) \ + return __GCONV_EMPTY_INPUT; \ + \ + if (*(uint16_t *) inptr == BOM) \ + /* Simply ignore the BOM character. */ \ + inptr += 2; \ + else if (*(uint16_t *) inptr == BOM_OE) \ + { \ + ((struct utf16_data *) step->__data)->swap = 1; \ + inptr += 2; \ + } \ + } \ + } \ + else if (!FROM_DIRECTION && var == UTF_16 && !data->__internal_use \ + && data->__invocation_counter == 0) \ { \ /* Emit the Byte Order Mark. */ \ if (outbuf + 2 > outend) \ @@ -51,7 +72,7 @@ *(uint16_t *) outbuf = BOM; \ outbuf += 2; \ } -#define EXTRA_LOOP_ARGS , var, data +#define EXTRA_LOOP_ARGS , var, data, swap /* Direction of the transformation. */ @@ -74,6 +95,7 @@ struct utf16_data { enum direction dir; enum variant var; + int swap; }; @@ -127,6 +149,9 @@ gconv_init (struct __gconv_step *step) { new_data->dir = dir; new_data->var = var; + new_data->swap = ((var == UTF_16LE && BYTE_ORDER == BIG_ENDIAN) + || (var == UTF_16BE + && BYTE_ORDER == LITTLE_ENDIAN)); step->__data = new_data; if (dir == from_utf16) @@ -170,8 +195,7 @@ gconv_end (struct __gconv_step *data) { \ uint32_t c = *((uint32_t *) inptr); \ \ - if ((__BYTE_ORDER == __LITTLE_ENDIAN && var == UTF_16BE) \ - || (__BYTE_ORDER == __BIG_ENDIAN && var == UTF_16LE)) \ + if (swap) \ { \ if (c >= 0x10000) \ { \ @@ -225,7 +249,7 @@ gconv_end (struct __gconv_step *data) inptr += 4; \ } #define EXTRA_LOOP_DECLS \ - , enum variant var, struct __gconv_step_data *step_data + , enum variant var, struct __gconv_step_data *step_data, int swap #include @@ -238,8 +262,7 @@ gconv_end (struct __gconv_step *data) { \ uint16_t u1 = *(uint16_t *) inptr; \ \ - if ((__BYTE_ORDER == __LITTLE_ENDIAN && var == UTF_16BE) \ - || (__BYTE_ORDER == __BIG_ENDIAN && var == UTF_16LE)) \ + if (swap) \ { \ u1 = bswap_16 (u1); \ \ @@ -277,16 +300,6 @@ gconv_end (struct __gconv_step *data) } \ else \ { \ - if (u1 == BOM && var == UTF_16 && !step_data->__internal_use \ - && step_data->__invocation_counter == 0 && inptr == *inptrp) \ - { \ - /* This is the first word in the file and it is the BOM and \ - we are converting a file without specified byte order. \ - Simply sack the BOM. */ \ - inptr += 2; \ - continue; \ - } \ - \ if (u1 < 0xd800 || u1 > 0xdfff) \ { \ /* No surrogate. */ \ @@ -322,7 +335,7 @@ gconv_end (struct __gconv_step *data) outptr += 4; \ } #define EXTRA_LOOP_DECLS \ - , enum variant var, struct __gconv_step_data *step_data + , enum variant var, struct __gconv_step_data *step_data, int swap #include -- cgit 1.4.1