mirror of
https://git.code.sf.net/p/zint/code
synced 2025-05-23 19:46:56 -04:00
code cleanup and minor fixes
This commit is contained in:
parent
8c78216dc1
commit
1d241cca05
26 changed files with 1048 additions and 1092 deletions
136
backend/imail.c
136
backend/imail.c
|
@ -3,26 +3,12 @@
|
|||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
/* The function "USPS_MSB_Math_CRC11GenerateFrameCheckSequence"
|
||||
is Copyright (C) 2006 United States Postal Service */
|
||||
|
||||
static short int BCD[40] = {
|
||||
static const short int BCD[40] = {
|
||||
0, 0, 0, 0,
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
|
@ -45,7 +31,7 @@ static short int BCD[40] = {
|
|||
|
||||
/* The following lookup tables were generated using the code in Appendix C */
|
||||
|
||||
static unsigned short AppxD_I[1287] = { /* Appendix D Table 1 - 5 of 13 characters */
|
||||
static const unsigned short AppxD_I[1287] = { /* Appendix D Table 1 - 5 of 13 characters */
|
||||
0x001F, 0x1F00, 0x002F, 0x1E80, 0x0037, 0x1D80, 0x003B, 0x1B80, 0x003D, 0x1780,
|
||||
0x003E, 0x0F80, 0x004F, 0x1E40, 0x0057, 0x1D40, 0x005B, 0x1B40, 0x005D, 0x1740,
|
||||
0x005E, 0x0F40, 0x0067, 0x1CC0, 0x006B, 0x1AC0, 0x006D, 0x16C0, 0x006E, 0x0EC0,
|
||||
|
@ -176,7 +162,7 @@ static unsigned short AppxD_I[1287] = { /* Appendix D Table 1 - 5 of 13 characte
|
|||
0x1823, 0x1883, 0x1843, 0x1445, 0x1249, 0x1151, 0x10E1, 0x0C46, 0x0A4A, 0x0952,
|
||||
0x08E2, 0x064C, 0x0554, 0x04E4, 0x0358, 0x02E8, 0x01F0 };
|
||||
|
||||
static unsigned short AppxD_II[78] = { /* Appendix D Table II - 2 of 13 characters */
|
||||
static const unsigned short AppxD_II[78] = { /* Appendix D Table II - 2 of 13 characters */
|
||||
0x0003, 0x1800, 0x0005, 0x1400, 0x0006, 0x0C00, 0x0009, 0x1200, 0x000A, 0x0A00,
|
||||
0x000C, 0x0600, 0x0011, 0x1100, 0x0012, 0x0900, 0x0014, 0x0500, 0x0018, 0x0300,
|
||||
0x0021, 0x1080, 0x0022, 0x0880, 0x0024, 0x0480, 0x0028, 0x0280, 0x0030, 0x0180,
|
||||
|
@ -186,14 +172,14 @@ static unsigned short AppxD_II[78] = { /* Appendix D Table II - 2 of 13 characte
|
|||
0x0201, 0x1008, 0x0202, 0x0808, 0x0204, 0x0408, 0x0401, 0x1004, 0x0402, 0x0804,
|
||||
0x0801, 0x1002, 0x1001, 0x0802, 0x0404, 0x0208, 0x0110, 0x00A0 };
|
||||
|
||||
static int AppxD_IV[130] = { /* Appendix D Table IV - Bar-to-Character Mapping (reverse lookup) */
|
||||
static const int AppxD_IV[130] = { /* Appendix D Table IV - Bar-to-Character Mapping (reverse lookup) */
|
||||
67, 6, 78, 16, 86, 95, 34, 40, 45, 113, 117, 121, 62, 87, 18, 104, 41, 76, 57, 119, 115, 72, 97,
|
||||
2, 127, 26, 105, 35, 122, 52, 114, 7, 24, 82, 68, 63, 94, 44, 77, 112, 70, 100, 39, 30, 107,
|
||||
15, 125, 85, 10, 65, 54, 88, 20, 106, 46, 66, 8, 116, 29, 61, 99, 80, 90, 37, 123, 51, 25, 84,
|
||||
129, 56, 4, 109, 96, 28, 36, 47, 11, 71, 33, 102, 21, 9, 17, 49, 124, 79, 64, 91, 42, 69, 53,
|
||||
60, 14, 1, 27, 103, 126, 75, 89, 50, 120, 19, 32, 110, 92, 111, 130, 59, 31, 12, 81, 43, 55,
|
||||
5, 74, 22, 101, 128, 58, 118, 48, 108, 38, 98, 93, 23, 83, 13, 73, 3 };
|
||||
|
||||
|
||||
/***************************************************************************
|
||||
** USPS_MSB_Math_CRC11GenerateFrameCheckSequence
|
||||
**
|
||||
|
@ -248,11 +234,11 @@ extern unsigned short
|
|||
void breakup(short int fcs_bit[], unsigned short usps_crc)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
for(i = 0; i < 13; i++) {
|
||||
fcs_bit[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
if(usps_crc >= 4096) {
|
||||
fcs_bit[12] = 1;
|
||||
usps_crc -= 4096;
|
||||
|
@ -330,12 +316,12 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
strcpy(symbol->errtxt, "Invalid characters in data");
|
||||
return error_number;
|
||||
}
|
||||
|
||||
|
||||
strcpy(zip, "");
|
||||
strcpy(tracker, "");
|
||||
|
||||
/* separate the tracking code from the routing code */
|
||||
|
||||
|
||||
read = 0;
|
||||
j = 0;
|
||||
for(i = 0; i < length; i++) {
|
||||
|
@ -360,7 +346,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
} else {
|
||||
zip[read] = '\0';
|
||||
}
|
||||
|
||||
|
||||
if(strlen(tracker) != 20) {
|
||||
strcpy(symbol->errtxt, "Invalid length tracking code");
|
||||
return ERROR_INVALID_DATA;
|
||||
|
@ -369,25 +355,25 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
strcpy(symbol->errtxt, "Invalid ZIP code");
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
|
||||
|
||||
/* *** Step 1 - Conversion of Data Fields into Binary Data *** */
|
||||
|
||||
|
||||
/* Routing code first */
|
||||
|
||||
|
||||
for(i = 0; i < 112; i++) {
|
||||
accum[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
for(read = 0; read < strlen(zip); read++) {
|
||||
|
||||
for(i = 0; i < 112; i++) {
|
||||
x_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
binary_add(accum, x_reg);
|
||||
}
|
||||
|
||||
|
||||
x_reg[0] = BCD[ctoi(zip[read]) * 4];
|
||||
x_reg[1] = BCD[(ctoi(zip[read]) * 4) + 1];
|
||||
x_reg[2] = BCD[(ctoi(zip[read]) * 4) + 2];
|
||||
|
@ -395,16 +381,16 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 4; i < 112; i++) {
|
||||
x_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
binary_add(accum, x_reg);
|
||||
}
|
||||
|
||||
|
||||
/* add weight to routing code */
|
||||
|
||||
|
||||
for(i = 0; i < 112; i++) {
|
||||
x_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
if(strlen(zip) > 9) {
|
||||
strcpy(zip_adder, "1000100001");
|
||||
} else {
|
||||
|
@ -418,21 +404,21 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 112; i++) {
|
||||
accum[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
for(read = 0; read < strlen(zip_adder); read++) {
|
||||
|
||||
for(i = 0; i < 112; i++) {
|
||||
y_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
|
||||
y_reg[0] = BCD[ctoi(zip_adder[read]) * 4];
|
||||
y_reg[1] = BCD[(ctoi(zip_adder[read]) * 4) + 1];
|
||||
y_reg[2] = BCD[(ctoi(zip_adder[read]) * 4) + 2];
|
||||
|
@ -440,23 +426,23 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 4; i < 112; i++) {
|
||||
y_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
binary_add(accum, x_reg);
|
||||
|
||||
|
||||
/* tracking code */
|
||||
|
||||
/* multiply by 10 */
|
||||
for(i = 0; i < 112; i++) {
|
||||
y_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
|
||||
/* add first digit of tracker */
|
||||
y_reg[0] = BCD[ctoi(tracker[0]) * 4];
|
||||
y_reg[1] = BCD[(ctoi(tracker[0]) * 4) + 1];
|
||||
|
@ -465,18 +451,18 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 4; i < 112; i++) {
|
||||
y_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
binary_add(accum, y_reg);
|
||||
|
||||
|
||||
/* multiply by 5 */
|
||||
for(i = 0; i < 112; i++) {
|
||||
y_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 4; i++) {
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
|
||||
/* add second digit */
|
||||
y_reg[0] = BCD[ctoi(tracker[1]) * 4];
|
||||
y_reg[1] = BCD[(ctoi(tracker[1]) * 4) + 1];
|
||||
|
@ -485,9 +471,9 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 4; i < 112; i++) {
|
||||
y_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
binary_add(accum, y_reg);
|
||||
|
||||
|
||||
/* and then the rest */
|
||||
|
||||
for(read = 2; read < strlen(tracker); read++) {
|
||||
|
@ -495,11 +481,11 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 0; i < 112; i++) {
|
||||
y_reg[i] = accum[i];
|
||||
}
|
||||
|
||||
|
||||
for(i = 0; i < 9; i++) {
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
|
||||
y_reg[0] = BCD[ctoi(tracker[read]) * 4];
|
||||
y_reg[1] = BCD[(ctoi(tracker[read]) * 4) + 1];
|
||||
y_reg[2] = BCD[(ctoi(tracker[read]) * 4) + 2];
|
||||
|
@ -507,18 +493,18 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
for(i = 4; i < 112; i++) {
|
||||
y_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
binary_add(accum, y_reg);
|
||||
}
|
||||
|
||||
/* printf("Binary data 1: ");
|
||||
hex_dump(accum); */
|
||||
|
||||
|
||||
/* *** Step 2 - Generation of 11-bit CRC on Binary Data *** */
|
||||
|
||||
accum[103] = 0;
|
||||
accum[102] = 0;
|
||||
|
||||
|
||||
memset(byte_array, 0, 13);
|
||||
for(j = 0; j < 13; j++) {
|
||||
i = 96 - (8 * j);
|
||||
|
@ -532,25 +518,25 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
byte_array[j] += 64 * accum[i + 6];
|
||||
byte_array[j] += 128 * accum[i + 7];
|
||||
}
|
||||
|
||||
|
||||
usps_crc = USPS_MSB_Math_CRC11GenerateFrameCheckSequence(byte_array);
|
||||
/* printf("FCS 2: %4.4X\n", usps_crc); */
|
||||
|
||||
|
||||
/* *** Step 3 - Conversion from Binary Data to Codewords *** */
|
||||
|
||||
|
||||
/* start with codeword J which is base 636 */
|
||||
for(i = 0; i < 112; i++) {
|
||||
x_reg[i] = 0;
|
||||
y_reg[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
x_reg[101] = 1;
|
||||
x_reg[98] = 1;
|
||||
x_reg[97] = 1;
|
||||
x_reg[96] = 1;
|
||||
x_reg[95] = 1;
|
||||
x_reg[94] = 1;
|
||||
|
||||
|
||||
for(i = 92; i >= 0; i--) {
|
||||
y_reg[i] = islarger(accum, x_reg);
|
||||
if(y_reg[i] == 1) {
|
||||
|
@ -558,13 +544,13 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
}
|
||||
shiftdown(x_reg);
|
||||
}
|
||||
|
||||
|
||||
codeword[9] = (accum[9] * 512) + (accum[8] * 256) + (accum[7] * 128) + (accum[6] * 64) +
|
||||
(accum[5] * 32) + (accum[4] * 16) + (accum[3] * 8) + (accum[2] * 4) +
|
||||
(accum[1] * 2) + accum[0];
|
||||
|
||||
|
||||
/* then codewords I to B with base 1365 */
|
||||
|
||||
|
||||
for(j = 8; j > 0; j--) {
|
||||
for(i = 0; i < 112; i++) {
|
||||
accum[i] = y_reg[i];
|
||||
|
@ -584,18 +570,18 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
}
|
||||
shiftdown(x_reg);
|
||||
}
|
||||
|
||||
|
||||
codeword[j] = (accum[10] * 1024) + (accum[9] * 512) + (accum[8] * 256) +
|
||||
(accum[7] * 128) + (accum[6] * 64) + (accum[5] * 32) +
|
||||
(accum[4] * 16) + (accum[3] * 8) + (accum[2] * 4) +
|
||||
(accum[1] * 2) + accum[0];
|
||||
}
|
||||
|
||||
|
||||
codeword[0] = (y_reg[10] * 1024) + (y_reg[9] * 512) + (y_reg[8] * 256) +
|
||||
(y_reg[7] * 128) + (y_reg[6] * 64) + (y_reg[5] * 32) +
|
||||
(y_reg[4] * 16) + (y_reg[3] * 8) + (y_reg[2] * 4) +
|
||||
(y_reg[1] * 2) + y_reg[0];
|
||||
|
||||
|
||||
for(i = 0; i < 8; i++) {
|
||||
if(codeword[i] == 1365) {
|
||||
codeword[i] = 0;
|
||||
|
@ -608,23 +594,23 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
printf("%d ", codeword[i]);
|
||||
}
|
||||
printf("\n"); */
|
||||
|
||||
|
||||
/* *** Step 4 - Inserting Additional Information into Codewords *** */
|
||||
|
||||
|
||||
codeword[9] = codeword[9] * 2;
|
||||
|
||||
|
||||
if(usps_crc >= 1024) {
|
||||
codeword[0] += 659;
|
||||
}
|
||||
|
||||
|
||||
/* printf("Codewords 4b: ");
|
||||
for(i = 0; i < 10; i++) {
|
||||
printf("%d ", codeword[i]);
|
||||
}
|
||||
printf("\n"); */
|
||||
|
||||
|
||||
/* *** Step 5 - Conversion from Codewords to Characters *** */
|
||||
|
||||
|
||||
for(i = 0; i < 10; i++) {
|
||||
if(codeword[i] < 1287) {
|
||||
characters[i] = AppxD_I[codeword[i]];
|
||||
|
@ -632,7 +618,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
characters[i] = AppxD_II[codeword[i] - 1287];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* printf("Characters 5a: ");
|
||||
for(i = 0; i < 10; i++) {
|
||||
printf("%4.4X ", characters[i]);
|
||||
|
@ -646,7 +632,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
characters[i] = 0x1FFF - characters[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* printf("Characters 5b: ");
|
||||
for(i = 0; i < 10; i++) {
|
||||
printf("%4.4X ", characters[i]);
|
||||
|
@ -654,14 +640,14 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
printf("\n"); */
|
||||
|
||||
/* *** Step 6 - Conversion from Characters to the Intelligent Mail Barcode *** */
|
||||
|
||||
|
||||
for(i = 0; i < 10; i++) {
|
||||
breakup(bit_pattern, characters[i]);
|
||||
for(j = 0; j < 13; j++) {
|
||||
bar_map[AppxD_IV[(13 * i) + j] - 1] = bit_pattern[j];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
strcpy(data_pattern, "");
|
||||
temp[1] = '\0';
|
||||
for(i = 0; i < 65; i++) {
|
||||
|
@ -673,7 +659,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length)
|
|||
temp[0] = itoc(j);
|
||||
concat(data_pattern, temp);
|
||||
}
|
||||
|
||||
|
||||
/* Translate 4-state data pattern to symbol */
|
||||
read = 0;
|
||||
for(i = 0; i < strlen(data_pattern); i++)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue