2 * code39.c -- encoding for code39
4 * Copyright (c) 1999 Alessandro Rubini (rubini@gnu.org)
5 * Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
6 * Copyright (c) 2001 Nathan D. Holmes / FedEx Services (ndholmes@fedex.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
32 /* this is ordered in decades to simplify encoding */
33 static char alphabet[] =
34 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
36 static char *codeset[] = {
37 "131112", "111213", "111312", "111411", "121113", /* 0 - 4 */
38 "121212", "121311", "111114", "131211", "141111", /* 5 - 9 */
39 "211113", "211212", "211311", "221112", "221211", /* A - E */
40 "231111", "112113", "112212", "112311", "122112", /* F - J */
41 "132111", "111123", "111222", "111321", "121122", /* K - O */
42 "131121", "212112", "212211", "211122", "211221", /* P - T */
43 "221121", "222111", "112122", "112221", "122121", /* U - Y */
44 "123111", "121131", "311112", "311211", "321111", /* Z - $ */
45 "112131", "113121", "211131", "121221", "312111", /* / - (%) */
46 "311121", "122211", "111141" /* (/) - (terminator) */
50 /* 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111 */
51 /* 00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222 */
52 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 */
53 static char shiftset[] =
54 "%$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%% /// ///// / /%%%%%% %%%%%%++++++++++++++++++++++++++%%%%%";
55 static char shiftset2[] =
56 "UABCDEFGHIJKLMNOPQRSTUVWXYZABCDE ABC FGHIJ L ZFGHIJV KLMNOWABCDEFGHIJLKMNOPQRSTUVWXYZPQRST";
59 #define EXTEND_DOLLAR 43
60 #define EXTEND_PERCENT 44
61 #define EXTEND_SLASH 45
62 #define EXTEND_PLUS 46
65 * Check that the text can be encoded. Returns 0 or -1.
66 * All of ASCII-7 is accepted.
68 int Barcode_93_verify(unsigned char *text)
74 for (i=0; text[i]; i++) {
76 /* Test if beyond the range of 7-bit ASCII */
77 if ((int)text[i] > 127)
84 * The encoding functions fills the "partial" and "textinfo" fields.
85 * Lowercase chars are converted to uppercase
87 int Barcode_93_encode(struct Barcode_Item *bc)
89 static unsigned char *text;
90 static char *partial; /* dynamic */
91 static char *textinfo; /* dynamic */
94 int i, code, textpos, checksum_len=0;
102 bc->partial = bc->textinfo = NULL; /* safe */
105 bc->encoding = strdup("code 93");
113 /* the partial code is 6 * (head + 2* text + 2* check + tail) + margin + term. */
114 partial = malloc( (strlen(text) *2 + 4) * 6 +2);
120 checksum_str = (int *)malloc( (strlen(text) + 3) * 2 * sizeof(int));
127 /* the text information is at most "nnn:fff:c " * strlen +term */
128 textinfo = malloc(10*strlen(text) + 2);
138 strcpy(partial, "0");
139 strcat(partial, codeset[START_STOP]);
141 for (i=0; i<strlen(text); i++) {
142 c = strchr(alphabet, text[i]);
144 /* Encode the shift character */
145 switch(shiftset[text[i]])
148 code = EXTEND_DOLLAR;
151 code = EXTEND_PERCENT;
164 strcat(partial, codeset[code]);
165 checksum_str[checksum_len++] = code;
167 /* Encode the second character */
168 code = strchr(alphabet, shiftset2[text[i]]) - alphabet;
169 strcat(partial, codeset[code]);
170 checksum_str[checksum_len++] = code;
172 /* Character is natively supported by the symbology, encode directly */
174 strcat(partial, codeset[c -alphabet]);
175 checksum_str[checksum_len++] = code;
177 sprintf(textptr, "%i:12:%c ", textpos, text[i]);
178 textptr += strlen(textptr);
180 * width of each code is 9; composed codes are 18, but aligning
181 * the text behind the right bars would be ugly, so use 9 anyways
188 /* Add the checksum */
189 if ( (bc->flags & BARCODE_NO_CHECKSUM)==0 )
191 for(i=1; i<=checksum_len; i++)
193 c_checksum += i * (int)checksum_str[checksum_len - i];
194 k_checksum += (i+1) * (int)checksum_str[checksum_len - i];
197 c_checksum = c_checksum % 47;
198 k_checksum += c_checksum;
199 k_checksum = k_checksum % 47;
201 strcat(partial, codeset[c_checksum]);
202 strcat(partial, codeset[k_checksum]);
205 strcat(partial, codeset[START_STOP]); /* end */
207 /* Encodes final 1-unit width bar to turn the start character into a stop char */
208 strcat(partial, "1");
209 bc->partial = partial;
210 bc->textinfo = textinfo;