code reworked

This commit is contained in:
openapc 2012-12-29 19:37:03 +01:00
parent f48d7ab6a6
commit bf2dbe7494
26 changed files with 284 additions and 499 deletions

View file

@ -3,20 +3,6 @@
/*
libzint - the open source barcode library
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* In version 0.5 this file was 1,553 lines long! */
@ -29,15 +15,15 @@
#define SODIUM "0123456789-"
#define SILVER "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd"
static char *C11Table[11] = {"111121", "211121", "121121", "221111", "112121", "212111", "122111",
static const char *C11Table[11] = {"111121", "211121", "121121", "221111", "112121", "212111", "122111",
"111221", "211211", "211111", "112111"};
/* Code 39 tables checked against ISO/IEC 16388:2007 */
/* Incorporates Table A1 */
static char *C39Table[43] = { "1112212111", "2112111121", "1122111121", "2122111111", "1112211121",
static const char *C39Table[43] = { "1112212111", "2112111121", "1122111121", "2122111111", "1112211121",
"2112211111", "1122211111", "1112112121", "2112112111", "1122112111", "2111121121",
"1121121121", "2121121111", "1111221121", "2111221111", "1121221111", "1111122121",
"2111122111", "1121122111", "1111222111", "2111111221", "1121111221", "2121111211",
@ -47,7 +33,7 @@ static char *C39Table[43] = { "1112212111", "2112111121", "1122111121", "2122111
"1211121211", "1112121211"};
/* Code 39 character assignments (Table 1) */
static char *EC39Ctrl[128] = {"%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G", "$H", "$I", "$J", "$K",
static const char *EC39Ctrl[128] = {"%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G", "$H", "$I", "$J", "$K",
"$L", "$M", "$N", "$O", "$P", "$Q", "$R", "$S", "$T", "$U", "$V", "$W", "$X", "$Y", "$Z",
"%A", "%B", "%C", "%D", "%E", " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G", "/H", "/I", "/J",
"/K", "/L", "-", ".", "/O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "/Z", "%F",
@ -57,7 +43,7 @@ static char *EC39Ctrl[128] = {"%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G", "$
"+P", "+Q", "+R", "+S", "+T", "+U", "+V", "+W", "+X", "+Y", "+Z", "%P", "%Q", "%R", "%S", "%T"};
/* Encoding the full ASCII character set in Code 39 (Table A2) */
static char *C93Ctrl[128] = {"bU", "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH", "aI", "aJ", "aK",
static const char *C93Ctrl[128] = {"bU", "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH", "aI", "aJ", "aK",
"aL", "aM", "aN", "aO", "aP", "aQ", "aR", "aS", "aT", "aU", "aV", "aW", "aX", "aY", "aZ",
"bA", "bB", "bC", "bD", "bE", " ", "cA", "cB", "cC", "cD", "cE", "cF", "cG", "cH", "cI", "cJ",
"cK", "cL", "cM", "cN", "cO", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "cZ", "bF",
@ -66,7 +52,7 @@ static char *C93Ctrl[128] = {"bU", "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH
"bW", "dA", "dB", "dC", "dD", "dE", "dF", "dG", "dH", "dI", "dJ", "dK", "dL", "dM", "dN", "dO",
"dP", "dQ", "dR", "dS", "dT", "dU", "dV", "dW", "dX", "dY", "dZ", "bP", "bQ", "bR", "bS", "bT"};
static char *C93Table[47] = {"131112", "111213", "111312", "111411", "121113", "121212", "121311",
static const char *C93Table[47] = {"131112", "111213", "111312", "111411", "121113", "121212", "121311",
"111114", "131211", "141111", "211113", "211212", "211311", "221112", "221211", "231111",
"112113", "112212", "112311", "122112", "132111", "111123", "111222", "111321", "121122",
"131121", "212112", "212211", "211122", "211221", "221121", "222111", "112122", "112221",
@ -82,7 +68,7 @@ char pattern[30];
/* Function Prototypes */
void NextS(int Chan, int i, int MaxS, int MaxB);
void NextB(int Chan, int i, int MaxB, int MaxS);
/* *********************** CODE 11 ******************** */
int code_11(struct zint_symbol *symbol, unsigned char source[], int length)
@ -93,7 +79,7 @@ int code_11(struct zint_symbol *symbol, unsigned char source[], int length)
int weight[128], error_number;
char dest[1024]; /* 6 + 121 * 6 + 2 * 6 + 5 + 1 ~ 1024*/
char checkstr[3];
error_number = 0;
if(length > 121) {
@ -153,7 +139,7 @@ int code_11(struct zint_symbol *symbol, unsigned char source[], int length)
checkstr[2] = '\0';
lookup(SODIUM, C11Table, checkstr[0], dest);
lookup(SODIUM, C11Table, checkstr[1], dest);
/* Stop character */
concat (dest, "11221");
@ -172,14 +158,14 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length)
int error_number;
char dest[775];
char localstr[2] = { 0 };
error_number = 0;
counter = 0;
if((symbol->option_2 < 0) || (symbol->option_2 > 1)) {
symbol->option_2 = 0;
}
if((symbol->symbology == BARCODE_LOGMARS) && (length > 59)) {
strcpy(symbol->errtxt, "Input too long");
return ERROR_TOO_LONG;
@ -201,9 +187,9 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length)
lookup(SILVER, C39Table, source[i], dest);
counter += posn(SILVER, source[i]);
}
if((symbol->symbology == BARCODE_LOGMARS) || (symbol->option_2 == 1)) {
counter = counter % 43;
if(counter < 10) {
check_digit = itoc(counter);
@ -224,19 +210,19 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length)
}
}
lookup(SILVER, C39Table, check_digit, dest);
/* Display a space check digit as _, otherwise it looks like an error */
if(check_digit == ' ') {
check_digit = '_';
}
localstr[0] = check_digit;
localstr[1] = '\0';
}
/* Stop character */
concat (dest, "121121211");
if((symbol->symbology == BARCODE_LOGMARS) || (symbol->symbology == BARCODE_HIBC_39)) {
/* LOGMARS uses wider 'wide' bars than normal Code 39 */
counter = strlen(dest);
@ -246,9 +232,9 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length)
}
}
}
expand(symbol, dest);
if(symbol->symbology == BARCODE_CODE39) {
ustrcpy(symbol->text, (unsigned char*)"*");
uconcat(symbol->text, source);
@ -263,11 +249,11 @@ int c39(struct zint_symbol *symbol, unsigned char source[], int length)
int pharmazentral(struct zint_symbol *symbol, unsigned char source[], int length)
{ /* Pharmazentral Nummer (PZN) */
int i, error_number, zeroes;
unsigned int count, check_digit;
char localstr[10];
error_number = 0;
count = 0;
@ -280,17 +266,17 @@ int pharmazentral(struct zint_symbol *symbol, unsigned char source[], int length
strcpy(symbol->errtxt, "Invalid characters in data");
return error_number;
}
localstr[0] = '-';
zeroes = 6 - length + 1;
for(i = 1; i < zeroes; i++)
localstr[i] = '0';
strcpy(localstr + zeroes, (char *)source);
for (i = 1; i < 7; i++) {
count += (i + 1) * ctoi(localstr[i]);
}
check_digit = count%11;
if (check_digit == 11) { check_digit = 0; }
localstr[7] = itoc(check_digit);
@ -321,7 +307,7 @@ int ec39(struct zint_symbol *symbol, unsigned char source[], int length)
strcpy(symbol->errtxt, "Input too long");
return ERROR_TOO_LONG;
}
/* Creates a buffer string and places control characters into it */
for(i = 0; i < length; i++) {
if(source[i] > 127) {
@ -334,7 +320,7 @@ int ec39(struct zint_symbol *symbol, unsigned char source[], int length)
/* Then sends the buffer to the C39 function */
error_number = c39(symbol, buffer, ustrlen(buffer));
for(i = 0; i < length; i++)
symbol->text[i] = source[i] ? source[i] : ' ';
symbol->text[length] = '\0';
@ -356,15 +342,15 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length)
char buffer[220];
char dest[670];
char set_copy[] = SILVER;
error_number = 0;
strcpy(buffer, "");
if(length > 107) {
strcpy(symbol->errtxt, "Input too long");
return ERROR_TOO_LONG;
}
/* Message Content */
for (i = 0; i < length; i++) {
if (source[i] > 127) {
@ -375,14 +361,14 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length)
concat(buffer, C93Ctrl[source[i]]);
symbol->text[i] = source[i] ? source[i] : ' ';
}
/* Now we can check the true length of the barcode */
h = strlen(buffer);
if (h > 107) {
strcpy(symbol->errtxt, "Input too long");
return ERROR_TOO_LONG;
}
for (i = 0; i < h; i++) {
values[i] = posn(SILVER, buffer[i]);
}
@ -437,7 +423,7 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length)
/* Their are used here on the understanding that they form part of the specification
for Channel Code and therefore their use is permitted under the following terms
set out in that document:
"It is the intent and understanding of AIM [t]hat the symbology presented in this
specification is entirely in the public domain and free of all use restrictions,
licenses and fees. AIM USA, its memer companies, or individual officers
@ -446,7 +432,7 @@ int c93(struct zint_symbol *symbol, unsigned char source[], int length)
void CheckCharacter() {
int i;
char part[3];
if(value == target_value) {
/* Target reached - save the generated pattern */
strcpy(pattern, "11110");
@ -461,7 +447,7 @@ void CheckCharacter() {
void NextB(int Chan, int i, int MaxB, int MaxS) {
int b;
b = (S[i]+B[i-1]+S[i-1]+B[i-2] > 4)? 1:2;
if (i < Chan+2) {
for (; b <= MaxB; b++) {
@ -477,7 +463,7 @@ void NextB(int Chan, int i, int MaxB, int MaxS) {
void NextS(int Chan, int i, int MaxS, int MaxB) {
int s;
for (s = (i<Chan+2)? 1: MaxS; s <= MaxS; s++) {
S[i] = s;
NextB(Chan,i,MaxB,MaxS+1-s);
@ -486,13 +472,13 @@ void NextS(int Chan, int i, int MaxS, int MaxB) {
int channel_code(struct zint_symbol *symbol, unsigned char source[], int length) {
/* Channel Code - According to ANSI/AIM BC12-1998 */
int channels, i;
int error_number = 0, range = 0, zeroes;
char hrt[9];
target_value = 0;
if(length > 7) {
strcpy(symbol->errtxt, "Input too long");
return ERROR_TOO_LONG;
@ -502,16 +488,16 @@ int channel_code(struct zint_symbol *symbol, unsigned char source[], int length)
strcpy(symbol->errtxt, "Invalid characters in data");
return error_number;
}
if((symbol->option_2 < 3) || (symbol->option_2 > 8)) { channels = 0; } else { channels = symbol->option_2; }
if(channels == 0) { channels = length + 1; }
if(channels == 2) { channels = 3; }
for(i = 0; i < length; i++) {
target_value *= 10;
target_value += ctoi((char) source[i]);
}
switch(channels) {
case 3: if(target_value > 26) { range = 1; } break;
case 4: if(target_value > 292) { range = 1; } break;
@ -524,19 +510,19 @@ int channel_code(struct zint_symbol *symbol, unsigned char source[], int length)
strcpy(symbol->errtxt, "Value out of range");
return ERROR_INVALID_DATA;
}
for(i = 0; i < 11; i++) { B[i] = 0; S[i] = 0; }
B[0] = S[1] = B[1] = S[2] = B[2] = 1;
value = 0;
NextS(channels,3,channels,channels);
zeroes = channels - 1 - length;
memset(hrt, '0', zeroes);
strcpy(hrt + zeroes, (char *)source);
ustrcpy(symbol->text, (unsigned char *)hrt);
expand(symbol, pattern);
return error_number;
}