Include sys/socket.h, arpa/inet.h
[yaz-moved-to-github.git] / src / iconv_decode_iso5426.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief ISO 5426 decoding
8  *
9  * MARC-8 reference:
10  *  http://www.loc.gov/marc/specifications/specchariso8.html
11  *
12  * ISO 5426 reference (in German)
13  * Zeichenkonkordanz MAB2-Zeichensatz - ISO/IEC 10646 / Unicode
14  * http://www.d-nb.de/standardisierung/pdf/mab_unic.pdf
15  */
16
17 #if HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include <yaz/xmalloc.h>
27 #include "iconv-p.h"
28
29 struct decoder_data {
30     int g0_mode;
31     int g1_mode;
32
33     int comb_offset;
34     int comb_size;
35     unsigned long comb_x[8];
36     size_t comb_no_read[8];
37 };
38
39 yaz_conv_func_t yaz_iso5426_42_conv;
40 yaz_conv_func_t yaz_iso5426_45_conv;
41 yaz_conv_func_t yaz_iso5426_67_conv;
42 yaz_conv_func_t yaz_iso5426_62_conv;
43 yaz_conv_func_t yaz_iso5426_70_conv;
44 yaz_conv_func_t yaz_iso5426_32_conv;
45 yaz_conv_func_t yaz_iso5426_4E_conv;
46 yaz_conv_func_t yaz_iso5426_51_conv;
47 yaz_conv_func_t yaz_iso5426_33_conv;
48 yaz_conv_func_t yaz_iso5426_34_conv;
49 yaz_conv_func_t yaz_iso5426_53_conv;
50 yaz_conv_func_t yaz_iso5426_31_conv;
51
52
53 static unsigned long yaz_read_iso5426_comb(yaz_iconv_t cd,
54                                          struct decoder_data *data,
55                                          unsigned char *inp,
56                                          size_t inbytesleft, size_t *no_read,
57                                          int *comb);
58
59 static unsigned long read_iso5426(yaz_iconv_t cd, yaz_iconv_decoder_t d,
60                                unsigned char *inp,
61                                size_t inbytesleft, size_t *no_read)
62 {
63     struct decoder_data *data = (struct decoder_data *) d->data;
64     unsigned long x;
65     if (data->comb_offset < data->comb_size)
66     {
67         *no_read = data->comb_no_read[data->comb_offset];
68         x = data->comb_x[data->comb_offset];
69
70         /* special case for double-diacritic combining characters, 
71            INVERTED BREVE and DOUBLE TILDE.
72            We'll increment the no_read counter by 1, since we want to skip over
73            the processing of the closing ligature character
74         */
75         /* this code is no longer necessary.. our handlers code in
76            yaz_iso5426_?_conv (generated by charconv.tcl) now returns
77            0 and no_read=1 when a sequence does not match the input.
78            The SECOND HALFs in codetables.xml produces a non-existant
79            entry in the conversion trie.. Hence when met, the input byte is
80            skipped as it should (in yaz_iconv)
81         */
82 #if 0
83         if (x == 0x0361 || x == 0x0360)
84             *no_read += 1;
85 #endif
86         data->comb_offset++;
87         return x;
88     }
89
90     data->comb_offset = 0;
91     for (data->comb_size = 0; data->comb_size < 8; data->comb_size++)
92     {
93         int comb = 0;
94
95         if (inbytesleft == 0 && data->comb_size)
96         {
97             yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL);
98             x = 0;
99             *no_read = 0;
100             break;
101         }
102         x = yaz_read_iso5426_comb(cd, data, inp, inbytesleft, no_read, &comb);
103         if (!comb || !x)
104             break;
105         data->comb_x[data->comb_size] = x;
106         data->comb_no_read[data->comb_size] = *no_read;
107         inp += *no_read;
108         inbytesleft = inbytesleft - *no_read;
109     }
110     return x;
111 }
112
113 static unsigned long read_iso5426s(yaz_iconv_t cd, yaz_iconv_decoder_t d,
114                                  unsigned char *inp,
115                                  size_t inbytesleft, size_t *no_read)
116 {
117     struct decoder_data *data = (struct decoder_data *) d->data;
118     unsigned long x = read_iso5426(cd, d, inp, inbytesleft, no_read);
119     if (x && data->comb_size == 1)
120     {
121         if (yaz_iso_8859_1_lookup_x12(x, data->comb_x[0], &x))
122         {
123             *no_read += data->comb_no_read[0];
124             data->comb_size = 0;
125         }
126     }
127     return x;
128 }
129
130 static unsigned long yaz_read_iso5426_comb(yaz_iconv_t cd,
131                                          struct decoder_data *data,
132                                          unsigned char *inp,
133                                          size_t inbytesleft, size_t *no_read,
134                                          int *comb)
135 {
136     *no_read = 0;
137     while (inbytesleft > 0 && *inp == 27)
138     {
139         int *modep = &data->g0_mode;
140         size_t inbytesleft0 = inbytesleft;
141
142         inbytesleft--;
143         inp++;
144         if (inbytesleft == 0)
145             goto incomplete;
146         if (*inp == '$') /* set with multiple bytes */
147         {
148             inbytesleft--;
149             inp++;
150         }
151         if (inbytesleft == 0)
152             goto incomplete;
153         if (*inp == '(' || *inp == ',')  /* G0 */
154         {
155             inbytesleft--;
156             inp++;
157         }
158         else if (*inp == ')' || *inp == '-') /* G1 */
159         {
160             inbytesleft--;
161             inp++;
162             modep = &data->g1_mode;
163         }
164         if (inbytesleft == 0)
165             goto incomplete;
166         if (*inp == '!') /* ANSEL is a special case */
167         {
168             inbytesleft--;
169             inp++;
170         }
171         if (inbytesleft == 0)
172             goto incomplete;
173         *modep = *inp++; /* Final character */
174         inbytesleft--;
175
176         (*no_read) += inbytesleft0 - inbytesleft;
177     }
178     if (inbytesleft == 0)
179         return 0;
180     else if (*inp == ' ')
181     {
182         *no_read += 1;
183         return ' ';
184     }
185     else
186     {
187         unsigned long x;
188         size_t no_read_sub = 0;
189         int mode = *inp < 128 ? data->g0_mode : data->g1_mode;
190         *comb = 0;
191
192         switch(mode)
193         {
194         case 'B':  /* Basic ASCII */
195         case 's':  /* ASCII */
196             x = yaz_iso5426_42_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
197             break;
198         case 'E':  /* ANSEL */
199             x = yaz_iso5426_45_conv(inp, inbytesleft, &no_read_sub, comb, 127, 128);
200             break;
201
202 #if 0
203         case 'g':  /* Greek */
204             x = yaz_iso5426_67_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
205             break;
206         case 'b':  /* Subscripts */
207             x = yaz_iso5426_62_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
208             break;
209         case 'p':  /* Superscripts */
210             x = yaz_iso5426_70_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
211             break;
212         case '2':  /* Basic Hebrew */
213             x = yaz_iso5426_32_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
214             break;
215         case 'N':  /* Basic Cyrillic */
216             x = yaz_iso5426_4E_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
217             break;
218         case 'Q':  /* Extended Cyrillic */
219             x = yaz_iso5426_51_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
220             break;
221         case '3':  /* Basic Arabic */
222             x = yaz_iso5426_33_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
223             break;
224         case '4':  /* Extended Arabic */
225             x = yaz_iso5426_34_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
226             break;
227         case 'S':  /* Greek */
228             x = yaz_iso5426_53_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
229             break;
230         case '1':  /* Chinese, Japanese, Korean (EACC) */
231             x = yaz_iso5426_31_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
232             break;
233 #endif
234         default:
235             *no_read = 0;
236             yaz_iconv_set_errno(cd, YAZ_ICONV_EILSEQ);
237             return 0;
238         }
239         *no_read += no_read_sub;
240         return x;
241     }
242 incomplete:
243     *no_read = 0;
244     yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL);
245     return 0;
246 }
247
248
249 static size_t init_iso5426(yaz_iconv_t cd, yaz_iconv_decoder_t d,
250                          unsigned char *inp,
251                          size_t inbytesleft, size_t *no_read)
252 {
253     struct decoder_data *data = (struct decoder_data *) d->data;
254     data->g0_mode = 'B';
255     data->g1_mode = 'E';
256     data->comb_offset = data->comb_size = 0;
257     return 0;
258 }
259
260 void destroy_iso5426(yaz_iconv_decoder_t d)
261 {
262     struct decoder_data *data = (struct decoder_data *) d->data;
263     xfree(data);
264 }
265
266 yaz_iconv_decoder_t yaz_iso5426_decoder(const char *fromcode,
267                                       yaz_iconv_decoder_t d)
268 {
269     if (!yaz_matchstr(fromcode, "ISO5426"))
270         d->read_handle = read_iso5426;
271     else
272         return 0;
273     {
274         struct decoder_data *data = (struct decoder_data *)
275             xmalloc(sizeof(*data));
276         d->data = data;
277         d->init_handle = init_iso5426;
278         d->destroy_handle = destroy_iso5426;
279     }
280     return d;
281 }
282
283
284 /*
285  * Local variables:
286  * c-basic-offset: 4
287  * indent-tabs-mode: nil
288  * End:
289  * vim: shiftwidth=4 tabstop=8 expandtab
290  */