2 * i25.c -- "interleaved 2 of 5"
4 * Copyright (c) 1999,2000 Alessandro Rubini (rubini@gnu.org)
5 * Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
30 static char *codes[] = {
31 "11331", "31113", "13113", "33111", "11313",
32 "31311", "13311", "11133", "31131", "13131"
35 static char *guard[] = {"a1a1", "c1a"}; /* begin end */
37 int Barcode_i25_verify(unsigned char *text)
41 while (*text && isdigit(*text))
44 return -1; /* a non-digit char */
48 int Barcode_i25_encode(struct Barcode_Item *bc)
51 unsigned char *partial; /* dynamic */
52 unsigned char *textinfo; /* dynamic */
53 unsigned char *textptr, *p1, *p2, *pd;
54 int i, len, sum[2], textpos, usesum = 0;
60 bc->partial = bc->textinfo = NULL; /* safe */
63 bc->encoding = strdup("interleaved 2 of 5");
71 if ((bc->flags & BARCODE_NO_CHECKSUM)) usesum = 0; else usesum = 1;
73 /* create the real text string, padded to an even number of digits */
74 text = malloc(strlen(bc->ascii) + 3); /* leading 0, checksum, term. */
79 /* add the leading 0 if needed */
80 i = strlen(bc->ascii) + usesum;
84 strcpy(text+1, bc->ascii);
86 strcpy(text, bc->ascii);
88 /* add the trailing checksum if needed, the leading 0 is ignored */
91 for (i=0; text[i]; i++)
92 sum[i%2] += text[i]-'0';
94 * The "even" sum must be multiplied by three, and the *
95 * rightmost digit is defined as "even". The digits' position
96 * is already correct, whether or not we added a leading zero.
97 * (e.g., they are in pos. 0..4 or 1..4 of the string)
99 i = sum[0] * 3 + sum[1];
101 text[strlen(text)-1] += (10 - (i%10)) % 10;
104 /* the partial code is 5 * (text + check) + 4(head) + 3(tail) + term. */
105 partial = malloc( (strlen(text) + 3) * 5 +2); /* be large... */
112 /* the text information is at most "nnn:fff:c " * (strlen+1) +term */
113 textinfo = malloc(10*(strlen(text)+1) + 2);
122 strcpy(partial, "0"); /* the first space */
123 strcat(partial, guard[0]); /* start */
124 textpos = 4; /* width of initial guard */
128 for (i=0; i<len; i+=2) {
129 if (!isdigit(text[i]) || !isdigit(text[i+1])) {
130 bc->error = EINVAL; /* impossible if text is verified */
136 /* interleave two digits */
137 p1 = codes[text[i]-'0'];
138 p2 = codes[text[i+1]-'0'];
139 pd = partial + strlen(partial); /* destination */
145 /* and print the ascii text (but don't print the checksum, if any */
146 if (usesum && strlen(text+i)==2) {
147 /* print only one digit, discard the checksum */
148 sprintf(textptr, "%i:12:%c ", textpos, text[i]);
150 sprintf(textptr, "%i:12:%c %i:12:%c ", textpos, text[i],
151 textpos+9, text[i+1]);
153 textpos += 18; /* width of two codes */
154 textptr += strlen(textptr);
156 strcat(partial, guard[1]);
158 bc->partial = partial;
159 bc->textinfo = textinfo;