2 * plessey.c -- encoding for Plessey
4 * Copyright (c) 2000 Leonid A. Broukhis (leob@mailcom.com)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
29 static char * patterns[] = { "13", "31" };
31 /* this is ordered in decades to simplify encoding */
32 static char alphabet[] =
33 "0123456789" "ABCDEF";
35 /* stop sequence may be 231311313 (barcodemill.com) */
36 static char *fillers[] = { "031311331", "331311313" };
38 static int width = 16, startpos = 16;
41 * Check that the text can be encoded. Returns 0 or -1.
42 * If it's all lowecase convert to uppercase and accept it
44 int Barcode_pls_verify(unsigned char *text)
46 int i, upper = 0, lower = 0;
50 for (i=0; text[i]; i++) {
51 if (!strchr(alphabet,toupper(text[i])))
53 if (isupper(text[i])) upper++;
54 if (islower(text[i])) lower++;
61 static int add_one(char *ptr, int code)
63 sprintf(ptr, "%s%s%s%s",
65 patterns[(code >> 1) & 1],
66 patterns[(code >> 2) & 1],
67 patterns[(code >> 3) & 1]
73 * The encoding functions fills the "partial" and "textinfo" fields.
74 * Lowercase chars are converted to uppercase
76 int Barcode_pls_encode(struct Barcode_Item *bc)
79 static char *partial; /* dynamic */
80 static char *textinfo; /* dynamic */
81 char *c, *ptr, *textptr;
82 unsigned char *checkptr;
84 static char check[9] = {1,1,1,1,0,1,0,0,1};
89 bc->partial = bc->textinfo = NULL; /* safe */
92 bc->encoding = strdup("plessey");
99 /* the partial code is 8 * (head + text + check + tail) + margin + term. */
100 partial = malloc( (strlen(text) + 4) * 8 + 3);
101 checkptr = calloc (1, strlen(text) * 4 + 8);
103 if (!partial || !checkptr) {
104 if (partial) free(partial);
105 if (checkptr) free(checkptr);
110 /* the text information is at most "nnn:fff:c " * strlen +term */
111 textinfo = malloc(10*strlen(text) + 2);
118 strcpy(partial, fillers[0]);
119 ptr = partial + strlen(partial);
123 for (i=0; i<strlen(text); i++) {
124 c = strchr(alphabet, toupper(text[i]));
126 bc->error = EINVAL; /* impossible if text is verified */
133 sprintf(textptr, "%i:12:%c ", textpos, toupper(text[i]));
135 textpos += width; /* width of each code */
136 textptr += strlen(textptr);
138 checkptr[4*i] = code & 1;
139 checkptr[4*i+1] = (code >> 1) & 1;
140 checkptr[4*i+2] = (code >> 2) & 1;
141 checkptr[4*i+3] = (code >> 3) & 1;
143 /* The CRC checksum is required */
144 for (i=0; i < 4*strlen(text); i++) {
147 for (j = 0; j < 9; j++)
148 checkptr[i+j] ^= check[j];
150 for (i = 0; i < 8; i++) {
151 sprintf(ptr, patterns[checkptr[strlen(text) * 4 + i]]);
154 fprintf(stderr, "CRC: ");
155 for (i = 0; i < 8; i++) {
156 fputc('0' + checkptr[strlen(text) * 4 + i], stderr);
159 strcpy(ptr, fillers[1]);
160 bc->partial = partial;
161 bc->textinfo = textinfo;