File: i25.c

package info (click to toggle)
barcode 0.99-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 4,648 kB
  • sloc: ansic: 21,507; sh: 11,815; makefile: 24; sed: 16
file content (165 lines) | stat: -rw-r--r-- 4,621 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
/*
 * i25.c -- "interleaved 2 of 5"
 *
 * Copyright (c) 1999,2000 Alessandro Rubini (rubini@gnu.org)
 * Copyright (c) 1999      Prosa Srl. (prosa@prosa.it)
 * Copyright (c) 2010, 2011 Giuseppe Scrivano (gscrivano@gnu.org)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "barcode.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

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

static char *guard[] = {"a1a1", "c1a"}; /* begin end */

int Barcode_i25_verify(unsigned char *text)
{
    if (!text[0])
	return -1;
    while (*text && isdigit(*text))
	text++;
    if (*text)
	return -1; /* a non-digit char */
    return 0; /* ok */
}

int Barcode_i25_encode(struct Barcode_Item *bc)
{
    char *text;
    char *partial;  /* dynamic */
    char *textinfo; /* dynamic */
    char *textptr, *p1, *p2, *pd;
    int i, len, sum[2], textpos, usesum = 0;

    if (bc->partial)
	free(bc->partial);
    if (bc->textinfo)
	free(bc->textinfo);
    bc->partial = bc->textinfo = NULL; /* safe */

    if (!bc->encoding)
	bc->encoding = strdup("interleaved 2 of 5");

    text = bc->ascii;
    if (!bc->ascii) {
        bc->error = EINVAL;
        return -1;
    }

    if ((bc->flags & BARCODE_NO_CHECKSUM)) usesum = 0; else usesum = 1;

    /* create the real text string, padded to an even number of digits */
    text = malloc(strlen(bc->ascii) + 3); /* leading 0, checksum, term. */
    if (!text) {
	bc->error = errno;
	return -1;
    }
    /* add the leading 0 if needed */
    i = strlen(bc->ascii) + usesum;
    if (i % 2) {
	/* add a leading 0 */
	text[0] = '0';
	strcpy(text+1, bc->ascii);
    } else {
	strcpy(text, bc->ascii);
    }
    /* add the trailing checksum if needed, the leading 0 is ignored */
    if (usesum) {
	sum[0] = sum[1] = 0;
	for (i=0; text[i]; i++) 
	    sum[i%2] += text[i]-'0';
	/*
	 * The "even" sum must be multiplied by three, and the *
	 * rightmost digit is defined as "even". The digits' position
	 * is already correct, whether or not we added a leading zero.
	 * (e.g., they are in pos. 0..4 or 1..4 of the string)
	 */
	i = sum[0] * 3 + sum[1];
	strcat(text, "0");
	text[strlen(text)-1] += (10 - (i%10)) % 10;
    }

    /* the partial code is 5 * (text + check) + 4(head) + 3(tail) + term. */
    partial = malloc( (strlen(text) + 3) * 5 +2); /* be large... */
    if (!partial) {
        bc->error = errno;
	free(text);
        return -1;
    }

    /* the text information is at most "nnn:fff:c " * (strlen+1) +term */
    textinfo = malloc(10*(strlen(text)+1) + 2);
    if (!textinfo) {
        bc->error = errno;
        free(partial);
	free(text);
        return -1;
    }


    strcpy(partial, "0"); /* the first space */
    strcat(partial, guard[0]); /* start */
    textpos = 4; /* width of initial guard */
    textptr = textinfo;

    len = strlen(text);
    for (i=0; i<len; i+=2) {
        if (!isdigit(text[i]) || !isdigit(text[i+1])) {
            bc->error = EINVAL; /* impossible if text is verified */
            free(partial);
            free(textinfo);
            free(text);
            return -1;
        }
	/* interleave two digits */
	p1 = codes[text[i]-'0'];
	p2 = codes[text[i+1]-'0'];
	pd = partial + strlen(partial); /* destination */
	while (*p1) {
	    *(pd++) = *(p1++);
	    *(pd++) = *(p2++);
	}
	*pd = '\0';
	/* and print the ascii text (but don't print the checksum, if any */
	if (usesum && strlen(text+i)==2) {
	    /* print only one digit, discard the checksum */
	    sprintf(textptr, "%i:12:%c ", textpos, text[i]);
	} else {
	    sprintf(textptr, "%i:12:%c %i:12:%c ", textpos, text[i],
		    textpos+9, text[i+1]);
	}
        textpos += 18; /* width of two codes */
        textptr += strlen(textptr);
    }
    textptr[-1] = '\0'; /* overwrite last space */
    strcat(partial, guard[1]);

    bc->partial = partial;
    bc->textinfo = textinfo;
    free(text);

    return 0;
}