File: code128_based.c

package info (click to toggle)
zint 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,016 kB
  • sloc: ansic: 133,002; cpp: 8,153; php: 1,846; sh: 1,304; python: 307; makefile: 207; tcl: 74
file content (319 lines) | stat: -rw-r--r-- 13,691 bytes parent folder | download
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
/* code128_based.c - Handles Code 128 derivatives NVE-18, EAN-14, DPD and Universal Postal Union S10 */
/*
    libzint - the open source barcode library
    Copyright (C) 2008-2025 Robin Stuart <rstuart114@gmail.com>
    Bugfixes thanks to Christian Sakowski and BogDan Vatra

    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.
 */
/* SPDX-License-Identifier: BSD-3-Clause */

#include <stdio.h>
#include "common.h"
#include "code128.h"
#include "gs1.h"

/* Was in "code128.c" */

INTERNAL int gs1_128(struct zint_symbol *symbol, unsigned char source[], int length);

/* Helper to do NVE18 or EAN14 */
static int nve18_or_ean14(struct zint_symbol *symbol, unsigned char source[], const int length, const int data_len) {
    static const char prefix[2][2][5] = {
        { "(01)", "[01]" }, /* EAN14 */
        { "(00)", "[00]" }, /* NVE18 */
    };
    unsigned char ean128_equiv[23];
    int error_number, zeroes;
    int i;

    if (length > data_len) {
        return ZEXT errtxtf(ZINT_ERROR_TOO_LONG, symbol, 345, "Input length %1$d too long (maximum %2$d)", length,
                            data_len);
    }

    if ((i = not_sane(NEON_F, source, length))) {
        /* Note: for all "at position" error messages, escape sequences not accounted for */
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 346,
                        "Invalid character at position %d in input (digits only)", i);
    }

    zeroes = data_len - length;
    memcpy(ean128_equiv, prefix[data_len == 17][!(symbol->input_mode & GS1PARENS_MODE)], 4);
    memset(ean128_equiv + 4, '0', zeroes);
    memcpy(ean128_equiv + 4 + zeroes, source, length);

    ean128_equiv[data_len + 4] = gs1_check_digit(ean128_equiv + 4, data_len);
    ean128_equiv[data_len + 5] = '\0'; /* Terminating NUL required by `c128_cost()` */

    error_number = gs1_128(symbol, ean128_equiv, data_len + 5);

    return error_number;
}


/* Add check digit if encoding an NVE18 symbol */
INTERNAL int nve18(struct zint_symbol *symbol, unsigned char source[], int length) {
    return nve18_or_ean14(symbol, source, length, 17 /*data_len*/);
}

/* EAN-14 - A version of EAN-128 */
INTERNAL int ean14(struct zint_symbol *symbol, unsigned char source[], int length) {
    return nve18_or_ean14(symbol, source, length, 13 /*data_len*/);
}

static const char KRSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#define KRSET_F (IS_NUM_F | IS_UPR_F)

/* DPD (Deutscher Paketdienst) Code */
/* Specification at https://esolutions.dpd.com/dokumente/DPD_Parcel_Label_Specification_2.4.1_EN.pdf
 * and identification tag info (Barcode ID) at https://esolutions.dpd.com/dokumente/DPD_Routing_Database_1.3_EN.pdf */
INTERNAL int dpd(struct zint_symbol *symbol, unsigned char source[], int length) {
    int error_number = 0;
    int i, p;
    unsigned char ident_tag;
    unsigned char local_source_buf[29];
    unsigned char *local_source;
    unsigned char hrt[37];
    const int mod = 36;
    const int relabel = symbol->option_2 == 1; /* A "relabel" has no identification tag */
    const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;
    int cd; /* Check digit */

    if ((length != 27 && length != 28) || (length == 28 && relabel)) {
        if (relabel) {
            return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 830,
                            "DPD relabel input length %d wrong (27 characters required)", length);
        }
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 349, "DPD input length %d wrong (27 or 28 characters required)",
                        length);
    }

    if (length == 27 && !relabel) {
        local_source_buf[0] = '%';
        memcpy(local_source_buf + 1, source, ++length); /* Include terminating NUL (required by `c128_cost()`) */
        local_source = local_source_buf;
    } else {
        local_source = source;
    }

    ident_tag = local_source[0];

    to_upper(local_source + !relabel, length - !relabel);
    if ((i = not_sane(KRSET_F, local_source + !relabel, length - !relabel))) {
        if (local_source == local_source_buf || relabel) {
            return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 300,
                        "Invalid character at position %d in input (alphanumerics only)", i);
        }
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 299,
                        "Invalid character at position %d in input (alphanumerics only after first)", i);
    }

    if (z_iscntrl(ident_tag) || !z_isascii(ident_tag)) {
        return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 343,
                        "Invalid DPD identification tag (first character), ASCII values 32 to 126 only");
    }

    (void) code128(symbol, local_source, length); /* Only error returned is for large text which can't happen */

    if (!(symbol->output_options & (BARCODE_BOX | BARCODE_BIND | BARCODE_BIND_TOP))) {
        /* If no option has been selected then uses default bind top option */
        symbol->output_options |= BARCODE_BIND_TOP; /* Note won't extend over quiet zones for DPD */
        if (symbol->border_width == 0) { /* Allow override if non-zero */
            symbol->border_width = 3; /* From examples, not mentioned in spec */
        }
    }

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* DPD Parcel Label Specification Version 2.4.1 (19.01.2021) Section 4.6.1.2
           25mm / 0.4mm (X max) = 62.5 min, 25mm / 0.375 (X) ~ 66.66 default */
        if (relabel) { /* If relabel then half-size */
            const float default_height = 33.3333321f; /* 12.5 / 0.375 */
            error_number = set_height(symbol, 31.25f, default_height, 0.0f, 0 /*no_errtxt*/);
        } else {
            const float default_height = 66.6666641f; /* 25.0 / 0.375 */
            error_number = set_height(symbol, 62.5f, default_height, 0.0f, 0 /*no_errtxt*/);
        }
    } else {
        (void) set_height(symbol, 0.0f, relabel ? 25.0f : 50.0f, 0.0f, 1 /*no_errtxt*/);
    }

    cd = mod;

    for (i = !relabel, p = 0; i < length; i++) {
        hrt[p++] = local_source[i];

        cd += posn(KRSET, local_source[i]);
        if (cd > mod) cd -= mod;
        cd *= 2;
        if (cd >= (mod + 1)) cd -= mod + 1;

        if (!raw_text) {
            switch (i + relabel) {
                case 4:
                case 7:
                case 11:
                case 15:
                case 19:
                case 21:
                case 24:
                case 27:
                    hrt[p++] = ' ';
                    break;
            }
        }
    }

    cd = mod + 1 - cd;
    if (cd == mod) cd = 0;

    hrt[p] = xtoc(cd);

    hrt_cpy_nochk(symbol, hrt, p + 1);

    /* Some compliance checks */
    if (not_sane(NEON_F, local_source + length - 16, 16)) {
        if (not_sane(NEON_F, local_source + length - 3, 3)) { /* 3-digit Country Code (ISO 3166-1) */
            errtxt(0, symbol, 831, "Destination Country Code (last 3 characters) should be numeric");
        } else if (not_sane(NEON_F, local_source + length - 6, 3)) { /* 3-digit Service Code */
            errtxt(0, symbol, 832, "Service Code (characters 6-4 from end) should be numeric");
        } else { /* Last 10 characters of Tracking No. */
            errtxt(0, symbol, 833,
                    "Last 10 characters of Tracking Number (characters 16-7 from end) should be numeric");
        }
        error_number = ZINT_WARN_NONCOMPLIANT;
    }

    return error_number;
}

/* Universal Postal Union S10 */
/* https://www.upu.int/UPU/media/upu/files/postalSolutions/programmesAndServices/standards/S10-12.pdf */
INTERNAL int upu_s10(struct zint_symbol *symbol, unsigned char source[], int length) {
    static const char weights[8] = { 8, 6, 4, 2, 3, 5, 9, 7 };
    int i, j;
    unsigned char local_source[13 + 1];
    unsigned char have_check_digit = '\0';
    int check_digit;
    int error_number = 0;
    unsigned char hrt[18];
    const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;

    if (length != 12 && length != 13) {
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 834, "Input length %d wrong (12 or 13 characters required)",
                        length);
    }
    if (length == 13) { /* Includes check digit - remove for now */
        have_check_digit = source[10];
        memcpy(local_source, source, 10);
        memcpy(local_source + 10, source + 11, length - 11);
        length--;
    } else {
        memcpy(local_source, source, length);
    }
    to_upper(local_source, length);

    if (!z_isupper(local_source[0]) || !z_isupper(local_source[1])) {
        return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 835,
                        "Invalid character in Service Indictor (first 2 characters) (alphabetic only)");
    }
    if (not_sane(NEON_F, local_source + 2, 12 - 4) || (have_check_digit && !z_isdigit(have_check_digit))) {
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 836,
                        "Invalid character in Serial Number (middle %d characters) (digits only)",
                        have_check_digit ? 9 : 8);
    }
    if (!z_isupper(local_source[10]) || !z_isupper(local_source[11])) {
        return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 837,
                        "Invalid character in Country Code (last 2 characters) (alphabetic only)");
    }

    check_digit = 0;
    for (i = 2; i < 10; i++) { /* Serial Number only */
        check_digit += ctoi(local_source[i]) * weights[i - 2];
    }
    check_digit %= 11;
    check_digit = 11 - check_digit;
    if (check_digit == 10) {
        check_digit = 0;
    } else if (check_digit == 11) {
        check_digit = 5;
    }
    if (have_check_digit && ctoi(have_check_digit) != check_digit) {
        return ZEXT errtxtf(ZINT_ERROR_INVALID_CHECK, symbol, 838, "Invalid check digit '%1$c', expecting '%2$c'",
                            have_check_digit, itoc(check_digit));
    }
    /* Add in (back) check digit */
    local_source[12] = local_source[11];
    local_source[11] = local_source[10];
    local_source[10] = itoc(check_digit);
    local_source[13] = '\0'; /* Terminating NUL required by `c128_cost()` */

    /* Do some checks on the Service Indicator (first char only) and Country Code */
    if (strchr("JKSTW", local_source[0]) != NULL) { /* These are reserved & cannot be assigned */
        error_number = errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 839,
                                "Invalid Service Indicator (first character should not be any of \"JKSTW\")");
    } else if (strchr("FHIOXY", local_source[0]) != NULL) { /* These aren't allocated as of spec Oct 2017 */
        error_number = errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 840,
                                "Non-standard Service Indicator (first 2 characters)");
    } else if (!gs1_iso3166_alpha2(local_source + 11)) {
        error_number = errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 841,
                                "Country code (last two characters) is not ISO 3166-1");
    }

    (void) code128(symbol, local_source, 13); /* Only error returned is for large text which can't happen */

    for (i = 0, j = 0; i < 13; i++) {
        if (!raw_text) {
            if (i == 2 || i == 5 || i == 8 || i == 11) {
                hrt[j++] = ' ';
            }
        }
        hrt[j++] = local_source[i];
    }
    hrt_cpy_nochk(symbol, hrt, j);

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* Universal Postal Union S10 Section 8, using max X 0.51mm & minimum height 12.5mm or 15% of width */
        const float min_height_min = 24.5098038f; /* 12.5 / 0.51 */
        float min_height = stripf(symbol->width * 0.15f);
        if (min_height < min_height_min) {
            min_height = min_height_min;
        }
        /* Using 50 as default as none recommended */
        if (error_number == 0) {
            error_number = set_height(symbol, min_height, min_height > 50.0f ? min_height : 50.0f, 0.0f,
                                        0 /*no_errtxt*/);
        } else {
            (void) set_height(symbol, min_height, min_height > 50.0f ? min_height : 50.0f, 0.0f, 1 /*no_errtxt*/);
        }
    } else {
        (void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
    }

    return error_number;
}

/* vim: set ts=4 sw=4 et : */