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