]> git.sur5r.net Git - glabels/blob - barcode-0.98/code93.c
Imported Upstream version 2.2.8
[glabels] / barcode-0.98 / code93.c
1 /*
2  * code39.c -- encoding for code39
3  *
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) 
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <errno.h>
28
29 #include "barcode.h"
30
31
32 /* this is ordered in decades to simplify encoding */
33 static char alphabet[] = 
34    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
35
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) */
47 };
48
49
50 /*      00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111 */
51 /*      00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990000000000111111111122222222 */
52 /*      01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567 */
53 static char shiftset[] =
54        "%$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%% ///  ///// /             /%%%%%%                          %%%%%%++++++++++++++++++++++++++%%%%%";
55 static char shiftset2[] =
56        "UABCDEFGHIJKLMNOPQRSTUVWXYZABCDE ABC  FGHIJ L             ZFGHIJV                          KLMNOWABCDEFGHIJLKMNOPQRSTUVWXYZPQRST";
57
58 #define START_STOP 47
59 #define EXTEND_DOLLAR   43
60 #define EXTEND_PERCENT   44
61 #define EXTEND_SLASH   45
62 #define EXTEND_PLUS   46
63
64 /*
65  * Check that the text can be encoded. Returns 0 or -1.
66  * All of ASCII-7 is accepted.
67  */
68 int Barcode_93_verify(unsigned char *text)
69 {
70     int i;
71
72     if (!strlen(text))
73         return -1;
74     for (i=0; text[i]; i++) {
75
76         /* Test if beyond the range of 7-bit ASCII */
77         if ((int)text[i] > 127)
78             return -1;
79     }
80     return 0;
81 }
82
83 /*
84  * The encoding functions fills the "partial" and "textinfo" fields.
85  * Lowercase chars are converted to uppercase
86  */
87 int Barcode_93_encode(struct Barcode_Item *bc)
88 {
89     static unsigned char *text;
90     static char *partial;  /* dynamic */
91     static char *textinfo; /* dynamic */
92     char *c, *textptr;
93     int *checksum_str;
94     int i, code, textpos, checksum_len=0;
95     int c_checksum = 0;
96     int k_checksum = 0;
97
98     if (bc->partial)
99         free(bc->partial);
100     if (bc->textinfo)
101         free(bc->textinfo);
102     bc->partial = bc->textinfo = NULL; /* safe */
103
104     if (!bc->encoding)
105         bc->encoding = strdup("code 93");
106
107     text = bc->ascii;
108     if (!text) {
109         bc->error = EINVAL;
110         return -1;
111     }
112
113     /* the partial code is 6 * (head + 2* text + 2* check + tail) + margin + term. */
114     partial = malloc( (strlen(text) *2 + 4) * 6 +2);
115     if (!partial) {
116         bc->error = errno;
117         return -1;
118     }
119
120     checksum_str = (int *)malloc( (strlen(text) + 3) * 2 * sizeof(int));
121     if (!checksum_str) {
122         free(partial);
123         bc->error = errno;
124         return -1;
125     }
126
127     /* the text information is at most "nnn:fff:c " * strlen +term */
128     textinfo = malloc(10*strlen(text) + 2);
129     if (!textinfo) {
130         bc->error = errno;
131         free(partial);
132         free(checksum_str);
133         return -1;
134     }
135
136     textptr = textinfo;
137     textpos = 22;
138     strcpy(partial, "0");
139     strcat(partial, codeset[START_STOP]);
140
141     for (i=0; i<strlen(text); i++) {
142         c = strchr(alphabet, text[i]);
143         if (!c) {
144            /* Encode the shift character */
145            switch(shiftset[text[i]])
146            {
147               case '$':
148                  code = EXTEND_DOLLAR;
149                  break;
150               case '%':
151                  code = EXTEND_PERCENT;
152                  break;
153               case '/':
154                  code = EXTEND_SLASH;
155                  break;
156               case '+':
157                  code = EXTEND_PLUS;
158                  break;
159               default:
160                  code = 0;
161                  break;
162            }
163
164            strcat(partial, codeset[code]);
165            checksum_str[checksum_len++] = code;
166
167            /* Encode the second character */
168            code = strchr(alphabet, shiftset2[text[i]]) - alphabet;
169            strcat(partial, codeset[code]);
170            checksum_str[checksum_len++] = code;
171         } else {
172             /* Character is natively supported by the symbology, encode directly */
173             code = c - alphabet;
174             strcat(partial, codeset[c -alphabet]);
175             checksum_str[checksum_len++] = code;
176         }
177         sprintf(textptr, "%i:12:%c ", textpos, text[i]);
178         textptr += strlen(textptr);
179         /*
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
182          */
183         textpos += 9;
184     }
185     c_checksum=0;
186     k_checksum=0;
187
188     /* Add the checksum */
189     if ( (bc->flags & BARCODE_NO_CHECKSUM)==0 ) 
190     {
191         for(i=1; i<=checksum_len; i++)
192         {
193            c_checksum += i * (int)checksum_str[checksum_len - i];
194            k_checksum += (i+1) * (int)checksum_str[checksum_len - i];
195         }
196
197         c_checksum = c_checksum % 47;
198         k_checksum += c_checksum;                           
199         k_checksum = k_checksum % 47;
200
201         strcat(partial, codeset[c_checksum]);
202         strcat(partial, codeset[k_checksum]);
203
204     }
205     strcat(partial, codeset[START_STOP]); /* end */
206
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;
211
212     return 0;
213 }