File: 2of5inter_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 (195 lines) | stat: -rw-r--r-- 8,213 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
/* 2of5_based.c - Handles Code 2 of 5 Interleaved derivatives ITF-14, DP Leitcode and DP Identcode */
/*
    libzint - the open source barcode library
    Copyright (C) 2008-2025 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.
 */
/* SPDX-License-Identifier: BSD-3-Clause */

/* Was in "2of5.c" */

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

INTERNAL int c25_inter_common(struct zint_symbol *symbol, unsigned char source[], int length,
                const int checkdigit_option, const int dont_set_height);

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

    if (length > 13) {
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 311, "Input length %d too long (maximum 13)", length);
    }

    if ((i = not_sane(NEON_F, source, length))) {
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 312,
                        "Invalid character at position %d in input (digits only)", i);
    }

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

    /* Calculate the check digit - the same method used for EAN-13 */
    local_source[13] = gs1_check_digit(local_source, 13);
    error_number = c25_inter_common(symbol, local_source, 14, 0 /*checkdigit_option*/, 1 /*dont_set_height*/);

    if (error_number < ZINT_ERROR) {
        if (!(symbol->output_options & (BARCODE_BOX | BARCODE_BIND | BARCODE_BIND_TOP))) {
            /* If no option has been selected then uses default box option */
            symbol->output_options |= BARCODE_BOX;
            if (symbol->border_width == 0) { /* Allow override if non-zero */
                /* GS1 General Specifications 21.0.1 Sections 5.3.2.4 & 5.3.6 (4.83 / 1.016 ~ 4.75) */
                symbol->border_width = 5; /* Note change from previous value 8 */
            }
        }

        if (symbol->output_options & COMPLIANT_HEIGHT) {
            /* GS1 General Specifications 21.0.1 5.12.3.2 table 2, including footnote (**): (note bind/box additional
               to symbol->height), same as GS1-128: "in case of further space constraints"
               height 5.8mm / 1.016mm (X max) ~ 5.7; default 31.75mm / 0.495mm ~ 64.14 */
            const float min_height = 5.70866156f; /* 5.8 / 1.016 */
            const float default_height = 64.1414108f; /* 31.75 / 0.495 */
            error_number = set_height(symbol, min_height, default_height, 0.0f, 0 /*no_errtxt*/);
        } else {
            (void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
        }
    }

    hrt_cpy_nochk(symbol, local_source, 14);

    return error_number;
}

/* Deutsche Post check digit */
static char c25_dp_check_digit(const unsigned int count) {
    return itoc((10 - (count % 10)) % 10);
}

/* Deutsche Post Leitcode */
/* Documentation (of a very incomplete and non-technical type):
https://www.deutschepost.de/content/dam/dpag/images/D_d/dialogpost-schwer/dp-dialogpost-schwer-broschuere-072021.pdf
*/
INTERNAL int dpleit(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number;
    unsigned int count;
    int factor;
    unsigned char local_source[14];
    int zeroes;
    const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;

    count = 0;
    if (length > 13) {
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 313, "Input length %d too long (maximum 13)", length);
    }
    if ((i = not_sane(NEON_F, source, length))) {
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 314,
                        "Invalid character at position %d in input (digits only)", i);
    }

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

    factor = 4;
    for (i = 12; i >= 0; i--) {
        count += factor * ctoi(local_source[i]);
        factor ^= 0x0D; /* Toggles 4 and 9 */
    }
    local_source[13] = c25_dp_check_digit(count);
    error_number = c25_inter_common(symbol, local_source, 14, 0 /*checkdigit_option*/, 1 /*dont_set_height*/);

    /* TODO: Find documentation on BARCODE_DPLEIT dimensions/height */
    /* Based on eyeballing DIALOGPOST SCHWER, using 72X as default */
    (void) set_height(symbol, 0.0f, 72.0f, 0.0f, 1 /*no_errtxt*/);

    if (raw_text) {
        hrt_cpy_nochk(symbol, local_source, 14);
    } else {
        /* HRT formatting as per DIALOGPOST SCHWER brochure but TEC-IT differs as do examples at
           https://www.philaseiten.de/cgi-bin/index.pl?ST=8615&CP=0&F=1#M147 */
        hrt_printf_nochk(symbol, "%.5s.%.3s.%.3s.%.3s", local_source, local_source + 5, local_source + 8,
                    local_source + 11);
    }

    return error_number;
}

/* Deutsche Post Identcode */
/* See dpleit() for (sort of) documentation reference */
INTERNAL int dpident(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number, zeroes;
    unsigned int count;
    int factor;
    unsigned char local_source[12];
    const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;

    count = 0;
    if (length > 11) {
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 315, "Input length %d too long (maximum 11)", length);
    }
    if ((i = not_sane(NEON_F, source, length))) {
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 316,
                        "Invalid character at position %d in input (digits only)", i);
    }

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

    factor = 4;
    for (i = 10; i >= 0; i--) {
        count += factor * ctoi(local_source[i]);
        factor ^= 0x0D; /* Toggles 4 and 9 */
    }
    local_source[11] = c25_dp_check_digit(count);
    error_number = c25_inter_common(symbol, local_source, 12, 0 /*checkdigit_option*/, 1 /*dont_set_height*/);

    /* TODO: Find documentation on BARCODE_DPIDENT dimensions/height */
    /* Based on eyeballing DIALOGPOST SCHWER, using 72X as default */
    (void) set_height(symbol, 0.0f, 72.0f, 0.0f, 1 /*no_errtxt*/);

    if (raw_text) {
        hrt_cpy_nochk(symbol, local_source, 12);
    } else {
        /* HRT formatting as per DIALOGPOST SCHWER brochure but TEC-IT differs as do other examples (see above) */
        hrt_printf_nochk(symbol, "%.2s.%.2s %c.%.3s.%.3s %c", local_source, local_source + 2, local_source[4],
                local_source + 5, local_source + 8, local_source[11]);
    }

    return error_number;
}

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