Use get16, get32, put16, and put32 instead of direct casting pointer
[kopensolaris-gnu/glibc.git] / iconvdata / iso-2022-cn.c
1 /* Conversion module for ISO-2022-CN.
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <gconv.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include "gb2312.h"
25 #include "cns11643l1.h"
26 #include "cns11643l2.h"
27
28 #include <assert.h>
29
30 /* This makes obvious what everybody knows: 0x1b is the Esc character.  */
31 #define ESC     0x1b
32
33 /* We have single-byte shift-in and shift-out sequences, and the single
34    shift sequence SS2 which replaces the SS2 designation for the next
35    two bytes.  */
36 #define SI      0x0f
37 #define SO      0x0e
38 #define SS2_0   ESC
39 #define SS2_1   0x4e
40
41 /* Definitions used in the body of the `gconv' function.  */
42 #define CHARSET_NAME            "ISO-2022-CN//"
43 #define DEFINE_INIT             1
44 #define DEFINE_FINI             1
45 #define FROM_LOOP               from_iso2022cn_loop
46 #define TO_LOOP                 to_iso2022cn_loop
47 #define MIN_NEEDED_FROM         1
48 #define MAX_NEEDED_FROM         4
49 #define MIN_NEEDED_TO           4
50 #define MAX_NEEDED_TO           4
51 #define PREPARE_LOOP \
52   int save_set;                                                               \
53   int *setp = &data->__statep->count;
54 #define EXTRA_LOOP_ARGS         , setp
55
56
57 /* The COUNT element of the state keeps track of the currently selected
58    character set.  The possible values are:  */
59 enum
60 {
61   ASCII_set = 0,
62   GB2312_set,
63   CNS11643_1_set,
64   CNS11643_2_set,
65   CURRENT_MASK = 3,
66   GB2312_ann = 4,
67   CNS11643_1_ann = 8,
68   CNS11643_2_ann = 16
69 };
70
71
72 /* Since this is a stateful encoding we have to provide code which resets
73    the output state to the initial state.  This has to be done during the
74    flushing.  */
75 #define EMIT_SHIFT_TO_INIT \
76   if (data->__statep->count != ASCII_set)                                     \
77     {                                                                         \
78       if (FROM_DIRECTION)                                                     \
79         /* It's easy, we don't have to emit anything, we just reset the       \
80            state for the input.  */                                           \
81         data->__statep->count = ASCII_set;                                    \
82       else                                                                    \
83         {                                                                     \
84           unsigned char *outbuf = data->__outbuf;                             \
85                                                                               \
86           /* We are not in the initial state.  To switch back we have         \
87              to emit `SI'.  */                                                \
88           if (outbuf == data->__outbufend)                                    \
89             /* We don't have enough room in the output buffer.  */            \
90             status = __GCONV_FULL_OUTPUT;                                     \
91           else                                                                \
92             {                                                                 \
93               /* Write out the shift sequence.  */                            \
94               *outbuf++ = SI;                                                 \
95               if (data->__is_last)                                            \
96                 *written += 1;                                                \
97               data->__outbuf = outbuf;                                        \
98               data->__statep->count = ASCII_set;                              \
99             }                                                                 \
100         }                                                                     \
101     }
102
103
104 /* Since we might have to reset input pointer we must be able to save
105    and retore the state.  */
106 #define SAVE_RESET_STATE(Save) \
107   if (Save)                                                                   \
108     save_set = *setp;                                                         \
109   else                                                                        \
110     *setp = save_set
111
112
113 /* First define the conversion function from ISO-2022-CN to UCS4.  */
114 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
115 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
116 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
117 #define LOOPFCT                 FROM_LOOP
118 #define BODY \
119   {                                                                           \
120     uint32_t ch = *inptr;                                                     \
121                                                                               \
122     /* This is a 7bit character set, disallow all 8bit characters.  */        \
123     if (ch > 0x7f)                                                            \
124       {                                                                       \
125         result = __GCONV_ILLEGAL_INPUT;                                       \
126         break;                                                                \
127       }                                                                       \
128                                                                               \
129     /* Recognize escape sequences.  */                                        \
130     if (ch == ESC)                                                            \
131       {                                                                       \
132         /* There are two kinds of escape sequences we have to handle:         \
133            - those announcing the use of GB and CNS characters on the         \
134              line; we can simply ignore them                                  \
135            - the initial byte of the SS2 sequence.                            \
136         */                                                                    \
137         if (NEED_LENGTH_TEST                                                  \
138             && (inptr + 1 > inend                                             \
139                 || (inptr[1] == '$'                                           \
140                     && (inptr + 2 > inend                                     \
141                         || (inptr[2] == ')' && inptr + 3 > inend)             \
142                         || (inptr[2] == '*' && inptr + 3 > inend)))           \
143                 || (inptr[1] == SS2_1 && inptr + 3 > inend)))                 \
144           {                                                                   \
145             result = __GCONV_EMPTY_INPUT;                                     \
146             break;                                                            \
147           }                                                                   \
148         if (inptr[1] == '$'                                                   \
149             && ((inptr[2] == ')' && (inptr[3] == 'A' || inptr[3] == 'G'))     \
150                 || (inptr[2] == '*' && inptr[3] == 'H')))                     \
151           {                                                                   \
152             /* OK, we accept those character sets.  */                        \
153             if (inptr[3] == 'A')                                              \
154               ann = GB2312_ann;                                               \
155             else if (inptr[3] == 'G')                                         \
156               ann = CNS11643_1_ann;                                           \
157             inptr += 4;                                                       \
158             continue;                                                         \
159           }                                                                   \
160       }                                                                       \
161     else if (ch == SO)                                                        \
162       {                                                                       \
163         /* Switch to use GB2312 or CNS 11643 plane 1, depending on which      \
164            S0 designation came last.  The only problem is what to do with     \
165            faulty input files where no designator came.                       \
166            XXX For now I'll default to use GB2312.  If this is not the        \
167            best behaviour (e.g., we should flag an error) let me know.  */    \
168         ++inptr;                                                              \
169         set = ann == CNS11643_1_ann ? CNS11643_1_set : GB2312_set;            \
170         continue;                                                             \
171       }                                                                       \
172     else if (ch == SI)                                                        \
173       {                                                                       \
174         /* Switch to use ASCII.  */                                           \
175         ++inptr;                                                              \
176         set = ASCII_set;                                                      \
177         continue;                                                             \
178       }                                                                       \
179                                                                               \
180     if (ch == ESC && inptr[1] == SS2_1)                                       \
181       {                                                                       \
182         /* This is a character from CNS 11643 plane 2.                        \
183            XXX We could test here whether the use of this character           \
184            set was announced.  */                                             \
185         inptr += 2;                                                           \
186         ch = cns11643l2_to_ucs4 (&inptr, 2, 0);                               \
187         if (ch == __UNKNOWN_10646_CHAR)                                       \
188           {                                                                   \
189             inptr -= 2;                                                       \
190             result = __GCONV_ILLEGAL_INPUT;                                   \
191             break;                                                            \
192           }                                                                   \
193       }                                                                       \
194     else if (set == ASCII_set)                                                \
195       {                                                                       \
196         /* Almost done, just advance the input pointer.  */                   \
197         ++inptr;                                                              \
198       }                                                                       \
199     else                                                                      \
200       {                                                                       \
201         /* That's pretty easy, we have a dedicated functions for this.  */    \
202         if (set == GB2312_set)                                                \
203           ch = gb2312_to_ucs4 (&inptr,                                        \
204                                NEED_LENGTH_TEST ? inend - inptr : 2, 0);      \
205         else                                                                  \
206           {                                                                   \
207             assert (set == CNS11643_1_set);                                   \
208             ch = cns11643l1_to_ucs4 (&inptr,                                  \
209                                      NEED_LENGTH_TEST ? inend - inptr : 2, 0);\
210           }                                                                   \
211                                                                               \
212         if (NEED_LENGTH_TEST && ch == 0)                                      \
213           {                                                                   \
214             result = __GCONV_EMPTY_INPUT;                                     \
215             break;                                                            \
216           }                                                                   \
217         else if (ch == __UNKNOWN_10646_CHAR)                                  \
218           {                                                                   \
219             result = __GCONV_ILLEGAL_INPUT;                                   \
220             break;                                                            \
221           }                                                                   \
222       }                                                                       \
223                                                                               \
224     put32 (outptr, ch);                                                       \
225     outptr += 4;                                                              \
226   }
227 #define EXTRA_LOOP_DECLS        , int *setp
228 #define INIT_PARAMS             int set = *setp & CURRENT_MASK; \
229                                 int ann = *setp & ~CURRENT_MASK
230 #define UPDATE_PARAMS           *setp = set | ann
231 #include <iconv/loop.c>
232
233
234 /* Next, define the other direction.  */
235 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
236 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
237 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
238 #define LOOPFCT                 TO_LOOP
239 #define BODY \
240   {                                                                           \
241     uint32_t ch;                                                              \
242     size_t written = 0;                                                       \
243                                                                               \
244     ch = get32 (inptr);                                                       \
245                                                                               \
246     /* First see whether we can write the character using the currently       \
247        selected character set.  */                                            \
248     if (ch < 0x80)                                                            \
249       {                                                                       \
250         if (set != ASCII_set)                                                 \
251           {                                                                   \
252             *outptr++ = SI;                                                   \
253             set = ASCII_set;                                                  \
254             if (NEED_LENGTH_TEST && outptr == outend)                         \
255               {                                                               \
256                 result = __GCONV_FULL_OUTPUT;                                 \
257                 break;                                                        \
258               }                                                               \
259           }                                                                   \
260                                                                               \
261         *outptr++ = ch;                                                       \
262         written = 1;                                                          \
263                                                                               \
264         /* At the end of the line we have to clear the `ann' flags since      \
265            every line must contain this information again.  */                \
266         if (ch == L'\n')                                                      \
267           ann = 0;                                                            \
268       }                                                                       \
269     else                                                                      \
270       {                                                                       \
271         char buf[2];                                                          \
272         int used;                                                             \
273                                                                               \
274         if (set == GB2312_set || (ann & CNS11643_1_ann) == 0)                 \
275           {                                                                   \
276             written = ucs4_to_gb2312 (ch, buf, 2);                            \
277             used = GB2312_set;                                                \
278           }                                                                   \
279         else                                                                  \
280           {                                                                   \
281             written = ucs4_to_cns11643l1 (ch, buf, 2);                        \
282             used = CNS11643_1_set;                                            \
283           }                                                                   \
284                                                                               \
285         if (written == __UNKNOWN_10646_CHAR)                                  \
286           {                                                                   \
287             /* Cannot convert it using the currently selected SO set.         \
288                Next try the SS2 set.  */                                      \
289             written = ucs4_to_cns11643l2 (ch, buf, 2);                        \
290             if (written != __UNKNOWN_10646_CHAR)                              \
291               /* Yep, that worked.  */                                        \
292               used = CNS11643_2_set;                                          \
293             else                                                              \
294               {                                                               \
295                 /* Well, see whether we have to change the SO set.  */        \
296                 if (set == GB2312_set)                                        \
297                   written = ucs4_to_cns11643l1 (ch, buf, 2);                  \
298                 else                                                          \
299                   written = ucs4_to_gb2312 (ch, buf, 2);                      \
300                                                                               \
301                 if (written != __UNKNOWN_10646_CHAR)                          \
302                   /* Oh well, then switch SO.  */                             \
303                   used = GB2312_set + CNS11643_1_set - set;                   \
304                 else                                                          \
305                   {                                                           \
306                     /* Even this does not work.  Error.  */                   \
307                     result = __GCONV_ILLEGAL_INPUT;                           \
308                     break;                                                    \
309                   }                                                           \
310               }                                                               \
311           }                                                                   \
312         assert (written == 2);                                                \
313                                                                               \
314         /* See whether we have to emit an escape sequence.  */                \
315         if (set != used)                                                      \
316           {                                                                   \
317             /* First see whether we announced that we use this                \
318                character set.  */                                             \
319             if ((ann & (2 << used)) == 0)                                     \
320               {                                                               \
321                 const char *escseq;                                           \
322                                                                               \
323                 if (NEED_LENGTH_TEST && outptr + 4 > outend)                  \
324                   {                                                           \
325                     result = __GCONV_FULL_OUTPUT;                             \
326                     break;                                                    \
327                   }                                                           \
328                                                                               \
329                 assert (used >= 1 && used <= 3);                              \
330                 escseq = "\e$)A\e$)G\e$*H" + (used - 1) * 4;                  \
331                 *outptr++ = *escseq++;                                        \
332                 *outptr++ = *escseq++;                                        \
333                 *outptr++ = *escseq++;                                        \
334                 *outptr++ = *escseq++;                                        \
335                                                                               \
336                 if (used == GB2312_set)                                       \
337                   ann = (ann & CNS11643_2_ann) | GB2312_ann;                  \
338                 else if (used == CNS11643_1_set)                              \
339                   ann = (ann & CNS11643_2_ann) | CNS11643_1_ann;              \
340                 else                                                          \
341                   ann |= CNS11643_2_ann;                                      \
342               }                                                               \
343                                                                               \
344             if (used == CNS11643_2_set)                                       \
345               {                                                               \
346                 if (outptr + 2 > outend)                                      \
347                   {                                                           \
348                     result = __GCONV_FULL_OUTPUT;                             \
349                     break;                                                    \
350                   }                                                           \
351                 *outptr++ = SS2_0;                                            \
352                 *outptr++ = SS2_1;                                            \
353               }                                                               \
354             else                                                              \
355               {                                                               \
356                 /* We only have to emit something is currently ASCII is       \
357                    selected.  Otherwise we are switching within the           \
358                    SO charset.  */                                            \
359                 if (set == ASCII_set)                                         \
360                   {                                                           \
361                     if (outptr + 1 > outend)                                  \
362                       {                                                       \
363                         result = __GCONV_FULL_OUTPUT;                         \
364                         break;                                                \
365                       }                                                       \
366                     *outptr++ = SO;                                           \
367                   }                                                           \
368               }                                                               \
369                                                                               \
370             /* Always test the length here since we have used up all the      \
371                guaranteed output buffer slots.  */                            \
372             if (outptr + 2 > outend)                                          \
373               {                                                               \
374                 result = __GCONV_FULL_OUTPUT;                                 \
375                 break;                                                        \
376               }                                                               \
377           }                                                                   \
378         else if (NEED_LENGTH_TEST && outptr + 2 > outend)                     \
379           {                                                                   \
380             result = __GCONV_FULL_OUTPUT;                                     \
381             break;                                                            \
382           }                                                                   \
383                                                                               \
384         *outptr++ = buf[0];                                                   \
385         *outptr++ = buf[1];                                                   \
386       }                                                                       \
387                                                                               \
388     /* Now that we wrote the output increment the input pointer.  */          \
389     inptr += 4;                                                               \
390   }
391 #define EXTRA_LOOP_DECLS        , int *setp
392 #define INIT_PARAMS             int set = *setp & CURRENT_MASK; \
393                                 int ann = *setp & ~CURRENT_MASK
394 #define UPDATE_PARAMS           *setp = set | ann
395 #include <iconv/loop.c>
396
397
398 /* Now define the toplevel functions.  */
399 #include <iconv/skeleton.c>