File: code11.c

package info (click to toggle)
zint 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (156 lines) | stat: -rw-r--r-- 5,569 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
/* code11.c - Handles Code 11 */
/*
    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 "code.c" */

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

#define SODIUM_MNS_F    (IS_NUM_F | IS_MNS_F) /* SODIUM "0123456789-" */

static const char C11Table[11 + 1][6] = {
    {'1','1','1','1','2','1'}, {'2','1','1','1','2','1'}, {'1','2','1','1','2','1'}, {'2','2','1','1','1','1'},
    {'1','1','2','1','2','1'}, {'2','1','2','1','1','1'}, {'1','2','2','1','1','1'}, {'1','1','1','2','2','1'},
    {'2','1','1','2','1','1'}, {'2','1','1','1','1','1'}, {'1','1','2','1','1','1'},
    {'1','1','2','2','1','1'} /* Start character (full 6), Stop character (first 5) */
};

/* Code 11 */
INTERNAL int code11(struct zint_symbol *symbol, unsigned char source[], int length) {

    int i;
    int h;
    int weight[141]; /* 140 + 1 extra for 1st check */
    char dest[864]; /* 6 + 140 * 6 + 2 * 6 + 5 + 1 = 864 */
    int error_number = 0;
    char *d = dest;
    int num_check_digits;
    unsigned char checkstr[2];
    static const unsigned char checkchrs[11] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' };

    /* Suppresses clang-tidy clang-analyzer-core.UndefinedBinaryOperatorResult warning */
    assert(length > 0);

    if (length > 140) { /* 8 (Start) + 140 * 8 + 2 * 8 (Check) + 7 (Stop) = 1151 */
        return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 320, "Input length %d too long (maximum 140)", length);
    }
    if ((i = not_sane(SODIUM_MNS_F, source, length))) {
        return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 321,
                        "Invalid character at position %d in input (digits and \"-\" only)", i);
    }

    if (symbol->option_2 < 0 || symbol->option_2 > 2) {
        return errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 339, "Invalid check digit version '%d' (1 or 2 only)",
                        symbol->option_2);
    }
    if (symbol->option_2 == 2) {
        num_check_digits = 0;
    } else if (symbol->option_2 == 1) {
        num_check_digits = 1;
    } else {
        num_check_digits = 2;
    }


    /* start character */
    memcpy(d, C11Table[11], 6);
    d +=  6;

    /* Draw main body of barcode */
    for (i = 0; i < length; i++, d += 6) {
        if (source[i] == '-')
            weight[i] = 10;
        else
            weight[i] = ctoi(source[i]);
        memcpy(d, C11Table[weight[i]], 6);
    }

    if (num_check_digits) {
		int c_weight = 1, c_count = 0, c_digit;
        /* Calculate C checksum */
        for (h = length - 1; h >= 0; h--) {
            c_count += (c_weight * weight[h]);
            c_weight++;

            if (c_weight > 10) {
                c_weight = 1;
            }
        }
        c_digit = c_count % 11;

        checkstr[0] = checkchrs[c_digit];
        memcpy(d, C11Table[c_digit], 6);
        d += 6;

        if (num_check_digits == 2) {
			int k_weight = 1, k_count = 0, k_digit;
            weight[length] = c_digit;

            /* Calculate K checksum */
            for (h = length; h >= 0; h--) {
                k_count += (k_weight * weight[h]);
                k_weight++;

                if (k_weight > 9) {
                    k_weight = 1;
                }
            }
            k_digit = k_count % 11;

            checkstr[1] = checkchrs[k_digit];
            memcpy(d, C11Table[k_digit], 6);
            d += 6;
        }
    }

    if (symbol->debug & ZINT_DEBUG_PRINT) {
        printf("Check digits (%d): %.*s%s\n", num_check_digits, num_check_digits, checkstr,
                num_check_digits ? "" : "<none>");
    }

    /* Stop character */
    memcpy(d, C11Table[11], 5);
    d += 5;

    expand(symbol, dest, d - dest);

    /* TODO: Find documentation on BARCODE_CODE11 dimensions/height */

    hrt_cpy_nochk(symbol, source, length);
    if (num_check_digits) {
        hrt_cat_nochk(symbol, checkstr, num_check_digits);
    }
    return error_number;
}

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