Use get16, get32, put16, and put32 instead of direct casting pointer
[kopensolaris-gnu/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 <gconv.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include "ksc5601.h"
25
26 #include <assert.h>
27
28 /* This makes obvious what everybody knows: 0x1b is the Esc character.  */
29 #define ESC     0x1b
30
31 /* The shift sequences for this charset (it does not use ESC).  */
32 #define SI      0x0f
33 #define SO      0x0e
34
35 /* Definitions used in the body of the `gconv' function.  */
36 #define CHARSET_NAME            "ISO-2022-KR//"
37 #define DEFINE_INIT             1
38 #define DEFINE_FINI             1
39 #define FROM_LOOP               from_iso2022kr_loop
40 #define TO_LOOP                 to_iso2022kr_loop
41 #define MIN_NEEDED_FROM         1
42 #define MAX_NEEDED_FROM         3
43 #define MIN_NEEDED_TO           4
44 #define MAX_NEEDED_TO           4
45 #define PREPARE_LOOP \
46   int save_set;                                                               \
47   int *setp = &data->__statep->count;                                         \
48   if (!FROM_DIRECTION && !data->__internal_use                                \
49       && data->__invocation_counter == 0)                                     \
50     {                                                                         \
51       /* Emit the designator sequence.  */                                    \
52       if (outbuf + 4 > outend)                                                \
53         return __GCONV_FULL_OUTPUT;                                           \
54                                                                               \
55       *outbuf++ = ESC;                                                        \
56       *outbuf++ = '$';                                                        \
57       *outbuf++ = ')';                                                        \
58       *outbuf++ = 'C';                                                        \
59     }
60 #define EXTRA_LOOP_ARGS         , setp
61
62
63 /* The COUNT element of the state keeps track of the currently selected
64    character set.  The possible values are:  */
65 enum
66 {
67   ASCII_set = 0,
68   KSC5601_set
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-KR 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         /* We don't really have to handle escape sequences since all the      \
133            switching is done using the SI and SO bytes.  But we have to       \
134            recognize `Esc $ ) C' since this is a kind of flag for this        \
135            encoding.  We simply ignore it.  */                                \
136         if ((NEED_LENGTH_TEST && inptr + 1 > inend)                           \
137             || (inptr[1] == '$'                                               \
138                 && ((NEED_LENGTH_TEST && inptr + 2 > inend)                   \
139                     || (inptr[2] == ')' && inptr + 3 > inend))))              \
140                                                                               \
141           {                                                                   \
142             result = __GCONV_EMPTY_INPUT;                                     \
143             break;                                                            \
144           }                                                                   \
145         if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C')            \
146           {                                                                   \
147             /* Yeah, yeah, we know this is ISO 2022-KR.  */                   \
148             inptr += 4;                                                       \
149             continue;                                                         \
150           }                                                                   \
151       }                                                                       \
152     else if (ch == SO)                                                        \
153       {                                                                       \
154         /* Switch to use KSC.  */                                             \
155         ++inptr;                                                              \
156         set = KSC5601_set;                                                    \
157         continue;                                                             \
158       }                                                                       \
159     else if (ch == SI)                                                        \
160       {                                                                       \
161         /* Switch to use ASCII.  */                                           \
162         ++inptr;                                                              \
163         set = ASCII_set;                                                      \
164         continue;                                                             \
165       }                                                                       \
166                                                                               \
167     if (set == ASCII_set)                                                     \
168       {                                                                       \
169         /* Almost done, just advance the input pointer.  */                   \
170         ++inptr;                                                              \
171       }                                                                       \
172     else                                                                      \
173       {                                                                       \
174         assert (set == KSC5601_set);                                          \
175                                                                               \
176         /* Use the KSC 5601 table.  */                                        \
177         ch = ksc5601_to_ucs4 (&inptr,                                         \
178                               NEED_LENGTH_TEST ? inend - inptr : 2, 0);       \
179                                                                               \
180         if (NEED_LENGTH_TEST && ch == 0)                                      \
181           {                                                                   \
182             result = __GCONV_EMPTY_INPUT;                                     \
183             break;                                                            \
184           }                                                                   \
185         else if (ch == __UNKNOWN_10646_CHAR)                                  \
186           {                                                                   \
187             result = __GCONV_ILLEGAL_INPUT;                                   \
188             break;                                                            \
189           }                                                                   \
190       }                                                                       \
191                                                                               \
192     put32 (outptr, ch);                                                       \
193     outptr += 4;                                                              \
194   }
195 #define EXTRA_LOOP_DECLS        , int *setp
196 #define INIT_PARAMS             int set = *setp
197 #define UPDATE_PARAMS           *setp = set
198 #include <iconv/loop.c>
199
200
201 /* Next, define the other direction.  */
202 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
203 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
204 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
205 #define LOOPFCT                 TO_LOOP
206 #define BODY \
207   {                                                                           \
208     uint32_t ch;                                                              \
209     size_t written = 0;                                                       \
210                                                                               \
211     ch = get32 (inptr);                                                       \
212                                                                               \
213     /* First see whether we can write the character using the currently       \
214        selected character set.  */                                            \
215     if (ch < 0x80)                                                            \
216       {                                                                       \
217         if (set != ASCII_set)                                                 \
218           {                                                                   \
219             *outptr++ = SI;                                                   \
220             set = ASCII_set;                                                  \
221             if (NEED_LENGTH_TEST && outptr == outend)                         \
222               {                                                               \
223                 result = __GCONV_FULL_OUTPUT;                                 \
224                 break;                                                        \
225               }                                                               \
226           }                                                                   \
227                                                                               \
228         *outptr++ = ch;                                                       \
229         written = 1;                                                          \
230       }                                                                       \
231     else                                                                      \
232       {                                                                       \
233         char buf[2];                                                          \
234                                                                               \
235         written = ucs4_to_ksc5601 (ch, buf, 2);                               \
236                                                                               \
237         if (written == __UNKNOWN_10646_CHAR)                                  \
238           {                                                                   \
239             /* Illegal character.  */                                         \
240             result = __GCONV_ILLEGAL_INPUT;                                   \
241             break;                                                            \
242           }                                                                   \
243         assert (written == 2);                                                \
244                                                                               \
245         /* We use KSC 5601.  */                                               \
246         if (set != KSC5601_set)                                               \
247           {                                                                   \
248             *outptr++ = SO;                                                   \
249             set = KSC5601_set;                                                \
250           }                                                                   \
251                                                                               \
252         if (NEED_LENGTH_TEST && outptr + 2 > outend)                          \
253           {                                                                   \
254             result = __GCONV_FULL_OUTPUT;                                     \
255             break;                                                            \
256           }                                                                   \
257                                                                               \
258         *outptr++ = buf[0];                                                   \
259         *outptr++ = buf[1];                                                   \
260       }                                                                       \
261                                                                               \
262     /* Now that we wrote the output increment the input pointer.  */          \
263     inptr += 4;                                                               \
264   }
265 #define EXTRA_LOOP_DECLS        , int *setp
266 #define INIT_PARAMS             int set = *setp
267 #define UPDATE_PARAMS           *setp = set
268 #include <iconv/loop.c>
269
270
271 /* Now define the toplevel functions.  */
272 #include <iconv/skeleton.c>