summaryrefslogtreecommitdiff
path: root/backend/2of5.c
blob: c3c795d780d55d62cc1e2e0ead1d3399da67e8d0 (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* 2of5.c - Handles Code 2 of 5 barcodes */

/*
    libzint - the open source barcode library
    Copyright (C) 2008 - 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 <stdio.h>
#include "common.h"
#ifdef _MSC_VER
#include <malloc.h>
#define inline _inline
#endif

static const char *C25MatrixTable[10] = {
    "113311", "311131", "131131", "331111", "113131", "313111",
    "133111", "111331", "311311", "131311"
};

static const char *C25IndustTable[10] = {
    "1111313111", "3111111131", "1131111131", "3131111111", "1111311131",
    "3111311111", "1131311111", "1111113131", "3111113111", "1131113111"
};

static const char *C25InterTable[10] = {
    "11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133",
    "31131", "13131"
};

static inline char check_digit(unsigned int count) {
    return itoc((10 - (count % 10)) % 10);
}

/* Code 2 of 5 Standard (Code 2 of 5 Matrix) */
INTERNAL int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {

    int i, error_number;
    char dest[512]; /* 6 + 80 * 6 + 6 + 1 ~ 512*/

    if (length > 80) {
        strcpy(symbol->errtxt, "301: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "302: Invalid characters in data");
        return error_number;
    }

    /* start character */
    strcpy(dest, "411111");

    for (i = 0; i < length; i++) {
        lookup(NEON, C25MatrixTable, source[i], dest);
    }

    /* Stop character */
    strcat(dest, "41111");

    expand(symbol, dest);
    ustrcpy(symbol->text, source);
    return error_number;
}

/* Code 2 of 5 Industrial */
INTERNAL int industrial_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {

    int i, error_number;
    char dest[512]; /* 6 + 40 * 10 + 6 + 1 */

    if (length > 45) {
        strcpy(symbol->errtxt, "303: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "304: Invalid character in data");
        return error_number;
    }

    /* start character */
    strcpy(dest, "313111");

    for (i = 0; i < length; i++) {
        lookup(NEON, C25IndustTable, source[i], dest);
    }

    /* Stop character */
    strcat(dest, "31113");

    expand(symbol, dest);
    ustrcpy(symbol->text, source);
    return error_number;
}

/* Code 2 of 5 IATA */
INTERNAL int iata_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number;
    char dest[512]; /* 4 + 45 * 10 + 3 + 1 */

    if (length > 45) {
        strcpy(symbol->errtxt, "305: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "306: Invalid characters in data");
        return error_number;
    }

    /* start */
    strcpy(dest, "1111");

    for (i = 0; i < length; i++) {
        lookup(NEON, C25IndustTable, source[i], dest);
    }

    /* stop */
    strcat(dest, "311");

    expand(symbol, dest);
    ustrcpy(symbol->text, source);
    return error_number;
}

/* Code 2 of 5 Data Logic */
INTERNAL int logic_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {

    int i, error_number;
    char dest[512]; /* 4 + 80 * 6 + 3 + 1 */

    if (length > 80) {
        strcpy(symbol->errtxt, "307: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "308: Invalid characters in data");
        return error_number;
    }

    /* start character */
    strcpy(dest, "1111");

    for (i = 0; i < length; i++) {
        lookup(NEON, C25MatrixTable, source[i], dest);
    }

    /* Stop character */
    strcat(dest, "311");

    expand(symbol, dest);
    ustrcpy(symbol->text, source);
    return error_number;
}

/* Code 2 of 5 Interleaved */
INTERNAL int interleaved_two_of_five(struct zint_symbol *symbol, const unsigned char source[], size_t length) {

    int i, j, error_number;
    char bars[7], spaces[7], mixed[14], dest[1000];
#ifndef _MSC_VER
    unsigned char temp[length + 2];
#else
    unsigned char* temp = (unsigned char *) _alloca((length + 2) * sizeof (unsigned char));
#endif

    if (length > 89) {
        strcpy(symbol->errtxt, "309: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "310: Invalid characters in data");
        return error_number;
    }

    ustrcpy(temp, "");
    /* Input must be an even number of characters for Interlaced 2 of 5 to work:
       if an odd number of characters has been entered then add a leading zero */
    if (length & 1) {
        ustrcpy(temp, "0");
        length++;
    }
    ustrcat(temp, source);

    /* start character */
    strcpy(dest, "1111");

    for (i = 0; i < (int) length; i += 2) {
        int k = 0;
        /* look up the bars and the spaces and put them in two strings */
        strcpy(bars, "");
        lookup(NEON, C25InterTable, temp[i], bars);
        strcpy(spaces, "");
        lookup(NEON, C25InterTable, temp[i + 1], spaces);

        /* then merge (interlace) the strings together */
        for (j = 0; j <= 4; j++) {
            mixed[k] = bars[j];
            k++;
            mixed[k] = spaces[j];
            k++;
        }
        mixed[k] = '\0';
        strcat(dest, mixed);
    }

    /* Stop character */
    strcat(dest, "311");

    expand(symbol, dest);
    ustrcpy(symbol->text, temp);
    return error_number;

}

/* Interleaved 2-of-5 (ITF) */
INTERNAL int itf14(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number, zeroes;
    unsigned int count;
    char localstr[16];

    count = 0;

    if (length > 13) {
        strcpy(symbol->errtxt, "311: Input too long");
        return ZINT_ERROR_TOO_LONG;
    }

    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "312: Invalid character in data");
        return error_number;
    }

    /* Add leading zeros as required */
    zeroes = 13 - length;
    for (i = 0; i < zeroes; i++) {
        localstr[i] = '0';
    }
    ustrcpy(localstr + zeroes, source);

    /* Calculate the check digit - the same method used for EAN-13 */
    for (i = 12; i >= 0; i--) {
        count += ctoi(localstr[i]);

        if (!(i & 1)) {
            count += 2 * ctoi(localstr[i]);
        }
    }
    localstr[13] = check_digit(count);
    localstr[14] = '\0';
    error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, strlen(localstr));
    ustrcpy(symbol->text, localstr);
    return error_number;
}

/* Deutshe Post Leitcode */
INTERNAL int dpleit(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number;
    unsigned int count;
    char localstr[16];
    int zeroes;

    count = 0;
    if (length > 13) {
        strcpy(symbol->errtxt, "313: Input wrong length");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "314: Invalid characters in data");
        return error_number;
    }

    zeroes = 13 - length;
    for (i = 0; i < zeroes; i++)
        localstr[i] = '0';
    ustrcpy(localstr + zeroes, source);

    for (i = 12; i >= 0; i--) {
        count += 4 * ctoi(localstr[i]);

        if (i & 1) {
            count += 5 * ctoi(localstr[i]);
        }
    }
    localstr[13] = check_digit(count);
    localstr[14] = '\0';
    error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, strlen(localstr));
    ustrcpy(symbol->text, localstr);
    return error_number;
}

/* Deutsche Post Identcode */
INTERNAL int dpident(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number, zeroes;
    unsigned int count;
    char localstr[16];

    count = 0;
    if (length > 11) {
        strcpy(symbol->errtxt, "315: Input wrong length");
        return ZINT_ERROR_TOO_LONG;
    }
    error_number = is_sane(NEON, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "316: Invalid characters in data");
        return error_number;
    }

    zeroes = 11 - length;
    for (i = 0; i < zeroes; i++)
        localstr[i] = '0';
    ustrcpy(localstr + zeroes, source);

    for (i = 10; i >= 0; i--) {
        count += 4 * ctoi(localstr[i]);

        if (i & 1) {
            count += 5 * ctoi(localstr[i]);
        }
    }
    localstr[11] = check_digit(count);
    localstr[12] = '\0';
    error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, strlen(localstr));
    ustrcpy(symbol->text, localstr);
    return error_number;
}