Update.
[platform/upstream/glibc.git] / iconvdata / iso-2022-kr.c
1 /* Conversion module for ISO-2022-KR.
2    Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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 <dlfcn.h>
22 #include <gconv.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "ksc5601.h"
26
27 #include <assert.h>
28
29 /* This makes obvious what everybody knows: 0x1b is the Esc character.  */
30 #define ESC     0x1b
31
32 /* The shift sequences for this charset (it does not use ESC).  */
33 #define SI      0x0f
34 #define SO      0x0e
35
36 /* Definitions used in the body of the `gconv' function.  */
37 #define CHARSET_NAME            "ISO-2022-KR//"
38 #define DEFINE_INIT             1
39 #define DEFINE_FINI             1
40 #define FROM_LOOP               from_iso2022kr_loop
41 #define TO_LOOP                 to_iso2022kr_loop
42 #define MIN_NEEDED_FROM         1
43 #define MAX_NEEDED_FROM         3
44 #define MIN_NEEDED_TO           4
45 #define MAX_NEEDED_TO           4
46 #define PREPARE_LOOP \
47   int save_set;                                                               \
48   int *setp = &data->__statep->__count;                                       \
49   if (!FROM_DIRECTION && !data->__internal_use                                \
50       && data->__invocation_counter == 0)                                     \
51     {                                                                         \
52       /* Emit the designator sequence.  */                                    \
53       if (outbuf + 4 > outend)                                                \
54         return __GCONV_FULL_OUTPUT;                                           \
55                                                                               \
56       *outbuf++ = ESC;                                                        \
57       *outbuf++ = '$';                                                        \
58       *outbuf++ = ')';                                                        \
59       *outbuf++ = 'C';                                                        \
60     }
61 #define EXTRA_LOOP_ARGS         , setp
62
63
64 /* The COUNT element of the state keeps track of the currently selected
65    character set.  The possible values are:  */
66 enum
67 {
68   ASCII_set = 0,
69   KSC5601_set = 8
70 };
71
72
73 /* Since this is a stateful encoding we have to provide code which resets
74    the output state to the initial state.  This has to be done during the
75    flushing.  */
76 #define EMIT_SHIFT_TO_INIT \
77   if (data->__statep->__count != ASCII_set)                                   \
78     {                                                                         \
79       if (FROM_DIRECTION)                                                     \
80         {                                                                     \
81           /* It's easy, we don't have to emit anything, we just reset the     \
82              state for the input.  */                                         \
83           data->__statep->__count &= 7;                                       \
84           data->__statep->__count |= ASCII_set;                               \
85         }                                                                     \
86       else                                                                    \
87         {                                                                     \
88           unsigned char *outbuf = data->__outbuf;                             \
89                                                                               \
90           /* We are not in the initial state.  To switch back we have         \
91              to emit `SI'.  */                                                \
92           if (__builtin_expect (outbuf == data->__outbufend, 0))              \
93             /* We don't have enough room in the output buffer.  */            \
94             status = __GCONV_FULL_OUTPUT;                                     \
95           else                                                                \
96             {                                                                 \
97               /* Write out the shift sequence.  */                            \
98               *outbuf++ = SI;                                                 \
99               data->__outbuf = outbuf;                                        \
100               data->__statep->__count = ASCII_set;                            \
101             }                                                                 \
102         }                                                                     \
103     }
104
105
106 /* Since we might have to reset input pointer we must be able to save
107    and retore the state.  */
108 #define SAVE_RESET_STATE(Save) \
109   if (Save)                                                                   \
110     save_set = *setp;                                                         \
111   else                                                                        \
112     *setp = save_set
113
114
115 /* First define the conversion function from ISO-2022-KR to UCS4.  */
116 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
117 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
118 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
119 #define LOOPFCT                 FROM_LOOP
120 #define BODY \
121   {                                                                           \
122     uint32_t ch = *inptr;                                                     \
123                                                                               \
124     /* This is a 7bit character set, disallow all 8bit characters.  */        \
125     if (__builtin_expect (ch, 0) > 0x7f)                                      \
126       {                                                                       \
127         if (! ignore_errors_p ())                                             \
128           {                                                                   \
129             result = __GCONV_ILLEGAL_INPUT;                                   \
130             break;                                                            \
131           }                                                                   \
132                                                                               \
133         ++inptr;                                                              \
134         ++*irreversible;                                                      \
135         continue;                                                             \
136       }                                                                       \
137                                                                               \
138     /* Recognize escape sequences.  */                                        \
139     if (__builtin_expect (ch, 0) == ESC)                                      \
140       {                                                                       \
141         /* We don't really have to handle escape sequences since all the      \
142            switching is done using the SI and SO bytes.  But we have to       \
143            recognize `Esc $ ) C' since this is a kind of flag for this        \
144            encoding.  We simply ignore it.  */                                \
145         if (__builtin_expect (inptr + 1 > inend, 0)                           \
146             || (inptr[1] == '$'                                               \
147                 && (__builtin_expect (inptr + 2 > inend, 0)                   \
148                     || (inptr[2] == ')'                                       \
149                         && __builtin_expect (inptr + 3 > inend, 0)))))        \
150                                                                               \
151           {                                                                   \
152             result = __GCONV_EMPTY_INPUT;                                     \
153             break;                                                            \
154           }                                                                   \
155         if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C')            \
156           {                                                                   \
157             /* Yeah, yeah, we know this is ISO 2022-KR.  */                   \
158             inptr += 4;                                                       \
159             continue;                                                         \
160           }                                                                   \
161       }                                                                       \
162     else if (__builtin_expect (ch, 0) == SO)                                  \
163       {                                                                       \
164         /* Switch to use KSC.  */                                             \
165         ++inptr;                                                              \
166         set = KSC5601_set;                                                    \
167         continue;                                                             \
168       }                                                                       \
169     else if (__builtin_expect (ch, 0) == SI)                                  \
170       {                                                                       \
171         /* Switch to use ASCII.  */                                           \
172         ++inptr;                                                              \
173         set = ASCII_set;                                                      \
174         continue;                                                             \
175       }                                                                       \
176                                                                               \
177     if (set == ASCII_set)                                                     \
178       {                                                                       \
179         /* Almost done, just advance the input pointer.  */                   \
180         ++inptr;                                                              \
181       }                                                                       \
182     else                                                                      \
183       {                                                                       \
184         assert (set == KSC5601_set);                                          \
185                                                                               \
186         /* Use the KSC 5601 table.  */                                        \
187         ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0);                      \
188                                                                               \
189         if (__builtin_expect (ch, 1) == 0)                                    \
190           {                                                                   \
191             result = __GCONV_EMPTY_INPUT;                                     \
192             break;                                                            \
193           }                                                                   \
194         else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR)            \
195           {                                                                   \
196             if (! ignore_errors_p ())                                         \
197               {                                                               \
198                 /* This is an illegal character.  */                          \
199                 result = __GCONV_ILLEGAL_INPUT;                               \
200                 break;                                                        \
201               }                                                               \
202                                                                               \
203             ++*irreversible;                                                  \
204             ++inptr;                                                          \
205             continue;                                                         \
206           }                                                                   \
207       }                                                                       \
208                                                                               \
209     put32 (outptr, ch);                                                       \
210     outptr += 4;                                                              \
211   }
212 #define LOOP_NEED_FLAGS
213 #define EXTRA_LOOP_DECLS        , int *setp
214 #define INIT_PARAMS             int set = *setp
215 #define UPDATE_PARAMS           *setp = set
216 #include <iconv/loop.c>
217
218
219 /* Next, define the other direction.  */
220 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
221 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
222 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
223 #define LOOPFCT                 TO_LOOP
224 #define BODY \
225   {                                                                           \
226     uint32_t ch;                                                              \
227     size_t written = 0;                                                       \
228                                                                               \
229     ch = get32 (inptr);                                                       \
230                                                                               \
231     /* First see whether we can write the character using the currently       \
232        selected character set.  */                                            \
233     if (ch < 0x80)                                                            \
234       {                                                                       \
235         if (set != ASCII_set)                                                 \
236           {                                                                   \
237             *outptr++ = SI;                                                   \
238             set = ASCII_set;                                                  \
239             if (__builtin_expect (outptr == outend, 0))                       \
240               {                                                               \
241                 result = __GCONV_FULL_OUTPUT;                                 \
242                 break;                                                        \
243               }                                                               \
244           }                                                                   \
245                                                                               \
246         *outptr++ = ch;                                                       \
247         written = 1;                                                          \
248       }                                                                       \
249     else                                                                      \
250       {                                                                       \
251         char buf[2];                                                          \
252                                                                               \
253         written = ucs4_to_ksc5601 (ch, buf, 2);                               \
254                                                                               \
255         if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR)            \
256           {                                                                   \
257             /* Illegal character.  */                                         \
258             if (step_data->__trans.__trans_fct != NULL)                       \
259               {                                                               \
260                 result = DL_CALL_FCT (step_data->__trans.__trans_fct,         \
261                                       (step, step_data, *inptrp, &inptr,      \
262                                        inend, *outptrp, &outptr, outend,      \
263                                        irreversible));                        \
264                 if (result != __GCONV_OK)                                     \
265                   break;                                                      \
266               }                                                               \
267             else if (! ignore_errors_p ())                                    \
268               {                                                               \
269                 result = __GCONV_ILLEGAL_INPUT;                               \
270                 break;                                                        \
271               }                                                               \
272             else                                                              \
273               {                                                               \
274                 ++*irreversible;                                              \
275                 inptr += 4;                                                   \
276               }                                                               \
277             continue;                                                         \
278           }                                                                   \
279         else                                                                  \
280           {                                                                   \
281             assert (written == 2);                                            \
282                                                                               \
283             /* We use KSC 5601.  */                                           \
284             if (set != KSC5601_set)                                           \
285               {                                                               \
286                 *outptr++ = SO;                                               \
287                 set = KSC5601_set;                                            \
288               }                                                               \
289                                                                               \
290             if (__builtin_expect (outptr + 2 > outend, 0))                    \
291               {                                                               \
292                 result = __GCONV_FULL_OUTPUT;                                 \
293                 break;                                                        \
294               }                                                               \
295                                                                               \
296             *outptr++ = buf[0];                                               \
297             *outptr++ = buf[1];                                               \
298           }                                                                   \
299       }                                                                       \
300                                                                               \
301     /* Now that we wrote the output increment the input pointer.  */          \
302     inptr += 4;                                                               \
303   }
304 #define LOOP_NEED_FLAGS
305 #define EXTRA_LOOP_DECLS        , int *setp
306 #define INIT_PARAMS             int set = *setp
307 #define UPDATE_PARAMS           *setp = set
308 #include <iconv/loop.c>
309
310
311 /* Now define the toplevel functions.  */
312 #include <iconv/skeleton.c>