summaryrefslogtreecommitdiff
path: root/backend/eci.c
blob: 422779042fa28aff8bd3f494d6034eaad783035c (plain)
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
/*  eci.c - Extended Channel Interpretations

    libzint - the open source barcode library
    Copyright (C) 2009 - 2020 Robin Stuart <rstuart114@gmail.com>

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. Neither the name of the project nor the names of its contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
 */
/* vim: set ts=4 sw=4 et : */

#include <string.h>
#include <stdio.h>
#include "eci.h"
#include "common.h"
#ifdef _MSC_VER
#include <malloc.h>
#endif

/* Convert Unicode to other character encodings */
INTERNAL int utf_to_eci(const int eci, const unsigned char source[], unsigned char dest[], size_t *length) {
    int in_posn;
    int out_posn;
    int ext;
    int done;
    
    if (eci == 26) {
        /* Unicode mode, do not process - just copy data across */
        memcpy(dest, source, *length);
        dest[*length] = '\0';
        return 0;
    }

    in_posn = 0;
    out_posn = 0;
    do {
        /* Single byte (ASCII) character */
        int bytelen = 1;
        int glyph = (int) source[in_posn];

        if ((source[in_posn] >= 0x80) && (source[in_posn] < 0xc0)) {
            /* Something has gone wrong, abort */
            return ZINT_ERROR_INVALID_DATA;
        }

        if ((source[in_posn] >= 0xc0) && (source[in_posn] < 0xe0)) {
            /* Two-byte character */
            bytelen = 2;
            glyph = (source[in_posn] & 0x1f) << 6;

            if ((int) *length < (in_posn + 2)) {
                return ZINT_ERROR_INVALID_DATA;
            }

            if (source[in_posn + 1] > 0xc0) {
                return ZINT_ERROR_INVALID_DATA;
            }

            glyph += (source[in_posn + 1] & 0x3f);
        }

        if ((source[in_posn] >= 0xe0) && (source[in_posn] < 0xf0)) {
            /* Three-byte character */
            bytelen = 3;
            glyph = (source[in_posn] & 0x0f) << 12;

            if ((int) *length < (in_posn + 2)) {
                return ZINT_ERROR_INVALID_DATA;
            }

            if ((int) *length < (in_posn + 3)) {
                return ZINT_ERROR_INVALID_DATA;
            }

            if (source[in_posn + 1] > 0xc0) {
                return ZINT_ERROR_INVALID_DATA;
            }

            if (source[in_posn + 2] > 0xc0) {
                return ZINT_ERROR_INVALID_DATA;
            }

            glyph += (source[in_posn + 1] & 0x3f) << 6;
            glyph += (source[in_posn + 2] & 0x3f);
        }

        if (source[in_posn] >= 0xf0 || glyph > 0x2122) {
            /* Not in any ISO 8859 or Windows page */
            return ZINT_ERROR_INVALID_DATA;
        }

        if (glyph < 128) {
            dest[out_posn] = glyph;
        } else {
            done = 0;
            for (ext = 0; ext < 128; ext++) {
                switch (eci) {
                    case 3: // Latin-1
                        if (glyph == iso_8859_1[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 4: // Latin-2
                        if (glyph == iso_8859_2[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 5: // Latin-3
                        if (glyph == iso_8859_3[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 6: // Latin-4
                        if (glyph == iso_8859_4[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 7: // Latin/Cyrillic
                        if (glyph == iso_8859_5[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 8: // Latin/Arabic
                        if (glyph == iso_8859_6[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 9: // Latin/Greek
                        if (glyph == iso_8859_7[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 10: // Latin/Hebrew
                        if (glyph == iso_8859_8[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 11: // Latin-5
                        if (glyph == iso_8859_9[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 12: // Latin-6
                        if (glyph == iso_8859_10[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 13: // Latin/Thai
                        if (glyph == iso_8859_11[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 15: // Latin-7
                        if (glyph == iso_8859_13[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 16: // Latin-8
                        if (glyph == iso_8859_14[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 17: // Latin-9
                        if (glyph == iso_8859_15[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 18: // Latin-10
                        if (glyph == iso_8859_16[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 21: // Windows-1250
                        if (glyph == windows_1250[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 22: // Windows-1251
                        if (glyph == windows_1251[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 23: // Windows-1252
                        if (glyph == windows_1252[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    case 24: // Windows-1256
                        if (glyph == windows_1256[ext]) {
                            dest[out_posn] = ext + 128;
                            done = 1;
                        }
                        break;
                    default:
                        break;
                }
                if (done) {
                    break;
                }
            }

            if (!(done)) {
                return ZINT_ERROR_INVALID_DATA;
            }
        }

        in_posn += bytelen;
        out_posn++;
    } while (in_posn < (int) *length);
    dest[out_posn] = '\0';
    *length = out_posn;

    return 0;
}

/* Find the lowest ECI mode which will encode a given set of Unicode text */
INTERNAL int get_best_eci(unsigned char source[], size_t length) {
    int eci = 3;

#ifndef _MSC_VER
    unsigned char local_source[length + 1];
#else
    unsigned char *local_source = (unsigned char*) _alloca(length + 1);
#endif

    do {
        if (utf_to_eci(eci, source, local_source, &length) == 0) {
            return eci;
        }
        eci++;
    } while (eci < 25);

    return 26; // If all of these fail, use Unicode!
}