File: iconv_decode_iso5426.c

package info (click to toggle)
yaz 4.2.30-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 15,120 kB
  • ctags: 13,367
  • sloc: xml: 119,746; ansic: 66,073; sh: 11,795; tcl: 2,125; makefile: 1,308; yacc: 371
file content (294 lines) | stat: -rw-r--r-- 8,759 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2012 Index Data
 * See the file LICENSE for details.
 */
/**
 * \file
 * \brief ISO 5426 decoding
 *
 * MARC-8 reference:
 *  http://www.loc.gov/marc/specifications/specchariso8.html
 *
 * ISO 5426 reference (in German)
 * Zeichenkonkordanz MAB2-Zeichensatz - ISO/IEC 10646 / Unicode
 * http://www.d-nb.de/standardisierung/pdf/mab_unic.pdf
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <errno.h>
#include <string.h>

#include <yaz/xmalloc.h>
#include "iconv-p.h"

struct decoder_data {
    int g0_mode;
    int g1_mode;

    int comb_offset;
    int comb_size;
    unsigned long comb_x[8];
    size_t comb_no_read[8];
};

yaz_conv_func_t yaz_iso5426_42_conv;
yaz_conv_func_t yaz_iso5426_45_conv;
yaz_conv_func_t yaz_iso5426_67_conv;
yaz_conv_func_t yaz_iso5426_62_conv;
yaz_conv_func_t yaz_iso5426_70_conv;
yaz_conv_func_t yaz_iso5426_32_conv;
yaz_conv_func_t yaz_iso5426_4E_conv;
yaz_conv_func_t yaz_iso5426_51_conv;
yaz_conv_func_t yaz_iso5426_33_conv;
yaz_conv_func_t yaz_iso5426_34_conv;
yaz_conv_func_t yaz_iso5426_53_conv;
yaz_conv_func_t yaz_iso5426_31_conv;


static unsigned long yaz_read_iso5426_comb(yaz_iconv_t cd,
                                         struct decoder_data *data,
                                         unsigned char *inp,
                                         size_t inbytesleft, size_t *no_read,
                                         int *comb);

static unsigned long read_iso5426(yaz_iconv_t cd, yaz_iconv_decoder_t d,
                               unsigned char *inp,
                               size_t inbytesleft, size_t *no_read)
{
    struct decoder_data *data = (struct decoder_data *) d->data;
    unsigned long x;
    if (data->comb_offset < data->comb_size)
    {
        *no_read = data->comb_no_read[data->comb_offset];
        x = data->comb_x[data->comb_offset];

        /* special case for double-diacritic combining characters, 
           INVERTED BREVE and DOUBLE TILDE.
           We'll increment the no_read counter by 1, since we want to skip over
           the processing of the closing ligature character
        */
        /* this code is no longer necessary.. our handlers code in
           yaz_iso5426_?_conv (generated by charconv.tcl) now returns
           0 and no_read=1 when a sequence does not match the input.
           The SECOND HALFs in codetables.xml produces a non-existant
           entry in the conversion trie.. Hence when met, the input byte is
           skipped as it should (in yaz_iconv)
        */
#if 0
        if (x == 0x0361 || x == 0x0360)
            *no_read += 1;
#endif
        data->comb_offset++;
        return x;
    }

    data->comb_offset = 0;
    for (data->comb_size = 0; data->comb_size < 8; data->comb_size++)
    {
        int comb = 0;

        if (inbytesleft == 0 && data->comb_size)
        {
            yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL);
            x = 0;
            *no_read = 0;
            break;
        }
        x = yaz_read_iso5426_comb(cd, data, inp, inbytesleft, no_read, &comb);
        if (!comb || !x)
            break;
        data->comb_x[data->comb_size] = x;
        data->comb_no_read[data->comb_size] = *no_read;
        inp += *no_read;
        inbytesleft = inbytesleft - *no_read;
    }
    return x;
}

#if 0
/* not used */
static unsigned long read_iso5426s(yaz_iconv_t cd, yaz_iconv_decoder_t d,
                                 unsigned char *inp,
                                 size_t inbytesleft, size_t *no_read)
{
    struct decoder_data *data = (struct decoder_data *) d->data;
    unsigned long x = read_iso5426(cd, d, inp, inbytesleft, no_read);
    if (x && data->comb_size == 1)
    {
        if (yaz_iso_8859_1_lookup_x12(x, data->comb_x[0], &x))
        {
            *no_read += data->comb_no_read[0];
            data->comb_size = 0;
        }
    }
    return x;
}
#endif

static unsigned long yaz_read_iso5426_comb(yaz_iconv_t cd,
                                         struct decoder_data *data,
                                         unsigned char *inp,
                                         size_t inbytesleft, size_t *no_read,
                                         int *comb)
{
    *no_read = 0;
    while (inbytesleft > 0 && *inp == 27)
    {
        int *modep = &data->g0_mode;
        size_t inbytesleft0 = inbytesleft;

        inbytesleft--;
        inp++;
        if (inbytesleft == 0)
            goto incomplete;
        if (*inp == '$') /* set with multiple bytes */
        {
            inbytesleft--;
            inp++;
        }
        if (inbytesleft == 0)
            goto incomplete;
        if (*inp == '(' || *inp == ',')  /* G0 */
        {
            inbytesleft--;
            inp++;
        }
        else if (*inp == ')' || *inp == '-') /* G1 */
        {
            inbytesleft--;
            inp++;
            modep = &data->g1_mode;
        }
        if (inbytesleft == 0)
            goto incomplete;
        if (*inp == '!') /* ANSEL is a special case */
        {
            inbytesleft--;
            inp++;
        }
        if (inbytesleft == 0)
            goto incomplete;
        *modep = *inp++; /* Final character */
        inbytesleft--;

        (*no_read) += inbytesleft0 - inbytesleft;
    }
    if (inbytesleft == 0)
        return 0;
    else if (*inp == ' ')
    {
        *no_read += 1;
        return ' ';
    }
    else
    {
        unsigned long x;
        size_t no_read_sub = 0;
        int mode = *inp < 128 ? data->g0_mode : data->g1_mode;
        *comb = 0;

        switch(mode)
        {
        case 'B':  /* Basic ASCII */
        case 's':  /* ASCII */
            x = yaz_iso5426_42_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'E':  /* ANSEL */
            x = yaz_iso5426_45_conv(inp, inbytesleft, &no_read_sub, comb, 127, 128);
            break;

#if 0
        case 'g':  /* Greek */
            x = yaz_iso5426_67_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'b':  /* Subscripts */
            x = yaz_iso5426_62_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'p':  /* Superscripts */
            x = yaz_iso5426_70_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case '2':  /* Basic Hebrew */
            x = yaz_iso5426_32_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'N':  /* Basic Cyrillic */
            x = yaz_iso5426_4E_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'Q':  /* Extended Cyrillic */
            x = yaz_iso5426_51_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case '3':  /* Basic Arabic */
            x = yaz_iso5426_33_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case '4':  /* Extended Arabic */
            x = yaz_iso5426_34_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case 'S':  /* Greek */
            x = yaz_iso5426_53_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
        case '1':  /* Chinese, Japanese, Korean (EACC) */
            x = yaz_iso5426_31_conv(inp, inbytesleft, &no_read_sub, comb, 127, 0);
            break;
#endif
        default:
            *no_read = 0;
            yaz_iconv_set_errno(cd, YAZ_ICONV_EILSEQ);
            return 0;
        }
        *no_read += no_read_sub;
        return x;
    }
incomplete:
    *no_read = 0;
    yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL);
    return 0;
}


static size_t init_iso5426(yaz_iconv_t cd, yaz_iconv_decoder_t d,
                         unsigned char *inp,
                         size_t inbytesleft, size_t *no_read)
{
    struct decoder_data *data = (struct decoder_data *) d->data;
    data->g0_mode = 'B';
    data->g1_mode = 'E';
    data->comb_offset = data->comb_size = 0;
    return 0;
}

void destroy_iso5426(yaz_iconv_decoder_t d)
{
    struct decoder_data *data = (struct decoder_data *) d->data;
    xfree(data);
}

yaz_iconv_decoder_t yaz_iso5426_decoder(const char *fromcode,
                                      yaz_iconv_decoder_t d)
{
    if (!yaz_matchstr(fromcode, "ISO5426"))
        d->read_handle = read_iso5426;
    else
        return 0;
    {
        struct decoder_data *data = (struct decoder_data *)
            xmalloc(sizeof(*data));
        d->data = data;
        d->init_handle = init_iso5426;
        d->destroy_handle = destroy_iso5426;
    }
    return d;
}


/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */