code cleanup and minor fixes

This commit is contained in:
oxygenic 2012-12-31 14:41:59 +01:00
parent 8c78216dc1
commit 1d241cca05
26 changed files with 1048 additions and 1092 deletions

View file

@ -201,14 +201,14 @@ int isx12(unsigned char source)
if(source == 32) { return 1; }
if((source >= '0') && (source <= '9')) { return 1; }
if((source >= 'A') && (source <= 'Z')) { return 1; }
return 0;
}
void dminsert(char binary_string[], int posn, char newbit)
{ /* Insert a character into the middle of a string at position posn */
int i, end;
end = strlen(binary_string);
for(i = end; i > posn; i--) {
binary_string[i] = binary_string[i - 1];
@ -219,7 +219,7 @@ void dminsert(char binary_string[], int posn, char newbit)
void insert_value(unsigned char binary_stream[], int posn, int streamlen, char newbit)
{
int i;
for(i = streamlen; i > posn; i--) {
binary_stream[i] = binary_stream[i - 1];
}
@ -230,11 +230,11 @@ int look_ahead_test(unsigned char source[], int sourcelen, int position, int cur
{
/* A custom version of the 'look ahead test' from Annex P */
/* This version is deliberately very reluctant to end a data stream with EDIFACT encoding */
float ascii_count, c40_count, text_count, x12_count, edf_count, b256_count, best_count;
int sp, done, best_scheme;
char reduced_char;
/* step (j) */
if(current_mode == DM_ASCII) {
ascii_count = 0.0;
@ -251,7 +251,7 @@ int look_ahead_test(unsigned char source[], int sourcelen, int position, int cur
edf_count = 2.0;
b256_count = 2.25;
}
switch(current_mode) {
case DM_C40: c40_count = 0.0; break;
case DM_TEXT: text_count = 0.0; break;
@ -259,71 +259,71 @@ int look_ahead_test(unsigned char source[], int sourcelen, int position, int cur
case DM_EDIFACT: edf_count = 0.0; break;
case DM_BASE256: b256_count = 0.0; break;
}
for(sp = position; (sp < sourcelen) && (sp <= (position + 8)); sp++) {
if(source[sp] <= 127) { reduced_char = source[sp]; } else { reduced_char = source[sp] - 127; }
if((source[sp] >= '0') && (source[sp] <= '9')) { ascii_count += 0.5; } else { ascii_count += 1.0; }
if(source[sp] > 127) { ascii_count += 1.0; }
done = 0;
if(reduced_char == ' ') { c40_count += (2.0 / 3.0); done = 1; }
if((reduced_char >= '0') && (reduced_char <= '9')) { c40_count += (2.0 / 3.0); done = 1; }
if((reduced_char >= 'A') && (reduced_char <= 'Z')) { c40_count += (2.0 / 3.0); done = 1; }
if(source[sp] > 127) { c40_count += (4.0 / 3.0); }
if(done == 0) { c40_count += (4.0 / 3.0); }
done = 0;
if(reduced_char == ' ') { text_count += (2.0 / 3.0); done = 1; }
if((reduced_char >= '0') && (reduced_char <= '9')) { text_count += (2.0 / 3.0); done = 1; }
if((reduced_char >= 'a') && (reduced_char <= 'z')) { text_count += (2.0 / 3.0); done = 1; }
if(source[sp] > 127) { text_count += (4.0 / 3.0); }
if(done == 0) { text_count += (4.0 / 3.0); }
if(isx12(source[sp])) { x12_count += (2.0 / 3.0); } else { x12_count += 4.0; }
/* step (p) */
done = 0;
if((source[sp] >= ' ') && (source[sp] <= '^')) { edf_count += (3.0 / 4.0); } else { edf_count += 6.0; }
if(gs1 && (source[sp] == '[')) { edf_count += 6.0; }
if(sp >= (sourcelen - 5)) { edf_count += 6.0; } /* MMmmm fudge! */
/* step (q) */
if(gs1 && (source[sp] == '[')) { b256_count += 4.0; } else { b256_count += 1.0; }
/* printf("%c lat a%.2f c%.2f t%.2f x%.2f e%.2f b%.2f\n", source[sp], ascii_count, c40_count, text_count, x12_count, edf_count, b256_count); */
}
best_count = ascii_count;
best_scheme = DM_ASCII;
if(b256_count <= best_count) {
best_count = b256_count;
best_scheme = DM_BASE256;
}
if(edf_count <= best_count) {
best_count = edf_count;
best_scheme = DM_EDIFACT;
}
if(text_count <= best_count) {
best_count = text_count;
best_scheme = DM_TEXT;
}
if(x12_count <= best_count) {
best_count = x12_count;
best_scheme = DM_X12;
}
if(c40_count <= best_count) {
best_count = c40_count;
best_scheme = DM_C40;
}
return best_scheme;
}
@ -331,7 +331,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
{
/* Encodes data using ASCII, C40, Text, X12, EDIFACT or Base 256 modes as appropriate */
/* Supports encoding FNC1 in supporting systems */
int sp, tp, i, gs1;
int current_mode, next_mode;
int inputlen = length;
@ -357,19 +357,19 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
memset(edifact_buffer, 0, 8);
edifact_p = 0;
strcpy(binary, "");
/* step (a) */
current_mode = DM_ASCII;
next_mode = DM_ASCII;
if(symbol->input_mode == GS1_MODE) { gs1 = 1; } else { gs1 = 0; }
if(gs1) {
target[tp] = 232; tp++;
concat(binary, " ");
if(debug) printf("FN1 ");
} /* FNC1 */
if(symbol->output_options & READER_INIT) {
if(gs1) {
strcpy(symbol->errtxt, "Cannot encode in GS1 mode and Reader Initialisation at the same time");
@ -380,15 +380,15 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if(debug) printf("RP ");
}
}
while (sp < inputlen) {
current_mode = next_mode;
/* step (b) - ASCII encodation */
if(current_mode == DM_ASCII) {
next_mode = DM_ASCII;
if(istwodigits(source, sp) && ((sp + 1) != inputlen)) {
target[tp] = (10 * ctoi(source[sp])) + ctoi(source[sp + 1]) + 130;
if(debug) printf("N%d ", target[tp] - 130);
@ -396,7 +396,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
sp += 2;
} else {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
if(next_mode != DM_ASCII) {
switch(next_mode) {
case DM_C40: target[tp] = 230; tp++; concat(binary, " ");
@ -432,18 +432,18 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
sp++;
}
}
}
/* step (c) C40 encodation */
if(current_mode == DM_C40) {
int shift_set, value;
next_mode = DM_C40;
if(c40_p == 0) {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
}
if(next_mode != DM_C40) {
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
next_mode = DM_ASCII;
@ -458,26 +458,26 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
shift_set = c40_shift[source[sp]];
value = c40_value[source[sp]];
}
if(gs1 && (source[sp] == '[')) {
shift_set = 2;
value = 27; /* FNC1 */
}
if(shift_set != 0) {
c40_buffer[c40_p] = shift_set - 1; c40_p++;
}
c40_buffer[c40_p] = value; c40_p++;
if(c40_p >= 3) {
int iv;
iv = (1600 * c40_buffer[0]) + (40 * c40_buffer[1]) + (c40_buffer[2]) + 1;
target[tp] = iv / 256; tp++;
target[tp] = iv % 256; tp++;
concat(binary, " ");
if (debug) printf("[%d %d %d] ", c40_buffer[0], c40_buffer[1], c40_buffer[2]);
c40_buffer[0] = c40_buffer[3];
c40_buffer[1] = c40_buffer[4];
c40_buffer[2] = c40_buffer[5];
@ -489,16 +489,16 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
sp++;
}
}
/* step (d) Text encodation */
if(current_mode == DM_TEXT) {
int shift_set, value;
next_mode = DM_TEXT;
if(text_p == 0) {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
}
if(next_mode != DM_TEXT) {
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
next_mode = DM_ASCII;
@ -513,26 +513,26 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
shift_set = text_shift[source[sp]];
value = text_value[source[sp]];
}
if(gs1 && (source[sp] == '[')) {
shift_set = 2;
value = 27; /* FNC1 */
}
if(shift_set != 0) {
text_buffer[text_p] = shift_set - 1; text_p++;
}
text_buffer[text_p] = value; text_p++;
if(text_p >= 3) {
int iv;
iv = (1600 * text_buffer[0]) + (40 * text_buffer[1]) + (text_buffer[2]) + 1;
target[tp] = iv / 256; tp++;
target[tp] = iv % 256; tp++;
concat(binary, " ");
if (debug) printf("[%d %d %d] ", text_buffer[0], text_buffer[1], text_buffer[2]);
text_buffer[0] = text_buffer[3];
text_buffer[1] = text_buffer[4];
text_buffer[2] = text_buffer[5];
@ -544,16 +544,16 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
sp++;
}
}
/* step (e) X12 encodation */
if(current_mode == DM_X12) {
int value = 0;
next_mode = DM_X12;
if(text_p == 0) {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
}
if(next_mode != DM_X12) {
target[tp] = 254; tp++; concat(binary, " ");/* Unlatch */
next_mode = DM_ASCII;
@ -565,18 +565,18 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if(source[sp] == ' ') { value = 3; }
if((source[sp] >= '0') && (source[sp] <= '9')) { value = (source[sp] - '0') + 4; }
if((source[sp] >= 'A') && (source[sp] <= 'Z')) { value = (source[sp] - 'A') + 14; }
x12_buffer[x12_p] = value; x12_p++;
if(x12_p >= 3) {
int iv;
iv = (1600 * x12_buffer[0]) + (40 * x12_buffer[1]) + (x12_buffer[2]) + 1;
target[tp] = iv / 256; tp++;
target[tp] = iv % 256; tp++;
concat(binary, " ");
if (debug) printf("[%d %d %d] ", x12_buffer[0], x12_buffer[1], x12_buffer[2]);
x12_buffer[0] = x12_buffer[3];
x12_buffer[1] = x12_buffer[4];
x12_buffer[2] = x12_buffer[5];
@ -588,34 +588,34 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
sp++;
}
}
/* step (f) EDIFACT encodation */
if(current_mode == DM_EDIFACT) {
int value = 0;
next_mode = DM_EDIFACT;
if(edifact_p == 3) {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
}
if(next_mode != DM_EDIFACT) {
edifact_buffer[edifact_p] = 31; edifact_p++;
next_mode = DM_ASCII;
} else {
if((source[sp] >= '@') && (source[sp] <= '^')) { value = source[sp] - '@'; }
if((source[sp] >= ' ') && (source[sp] <= '?')) { value = source[sp]; }
edifact_buffer[edifact_p] = value; edifact_p++;
sp++;
}
if(edifact_p >= 4) {
target[tp] = (edifact_buffer[0] << 2) + ((edifact_buffer[1] & 0x30) >> 4); tp++;
target[tp] = ((edifact_buffer[1] & 0x0f) << 4) + ((edifact_buffer[2] & 0x3c) >> 2); tp++;
target[tp] = ((edifact_buffer[2] & 0x03) << 6) + edifact_buffer[3]; tp++;
concat(binary, " ");
if (debug) printf("[%d %d %d %d] ", edifact_buffer[0], edifact_buffer[1], edifact_buffer[2], edifact_buffer[3]);
edifact_buffer[0] = edifact_buffer[4];
edifact_buffer[1] = edifact_buffer[5];
edifact_buffer[2] = edifact_buffer[6];
@ -627,11 +627,11 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
edifact_p -= 4;
}
}
/* step (g) Base 256 encodation */
if(current_mode == DM_BASE256) {
next_mode = look_ahead_test(source, inputlen, sp, current_mode, gs1);
if(next_mode == DM_BASE256) {
target[tp] = source[sp];
if(debug) printf("B%02X ", target[tp]);
@ -643,13 +643,13 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if(debug) printf("ASC ");
}
}
if(tp > 1558) {
return 0;
}
} /* while */
/* Empty buffers */
if(c40_p == 2) {
target[tp] = 254; tp++; /* unlatch */
@ -666,7 +666,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if(debug) printf("ASC A%02X ", target[tp - 1] - 1);
current_mode = DM_ASCII;
}
if(text_p == 2) {
target[tp] = 254; tp++; /* unlatch */
target[tp] = source[inputlen - 2] + 1; tp++;
@ -682,7 +682,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if(debug) printf("ASC A%02X ", target[tp - 1] - 1);
current_mode = DM_ASCII;
}
if(x12_p == 2) {
target[tp] = 254; tp++; /* unlatch */
target[tp] = source[inputlen - 2] + 1; tp++;
@ -706,9 +706,9 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
if((i == 0) || ((i != 0) && (binary[i - 1] != 'b'))) {
/* start of binary data */
int binary_count; /* length of b256 data */
for(binary_count = 0; binary[binary_count + i] == 'b'; binary_count++);
if(binary_count <= 249) {
dminsert(binary, i, 'b');
insert_value(target, i, tp, binary_count); tp++;
@ -722,17 +722,17 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
}
i++;
}
for(i = 0; i < tp; i++) {
if(binary[i] == 'b') {
int prn, temp;
prn = ((149 * (i + 1)) % 255) + 1;
temp = target[i] + prn;
if (temp <= 255) { target[i] = temp; } else { target[i] = temp - 256; }
}
}
if(debug) {
printf("\n\n");
for(i = 0; i < tp; i++){
@ -740,7 +740,7 @@ int dm200encode(struct zint_symbol *symbol, unsigned char source[], unsigned cha
}
printf("\n");
}
*(last_mode) = current_mode;
return tp;
}
@ -749,7 +749,7 @@ void add_tail(unsigned char target[], int tp, int tail_length, int last_mode)
{
/* adds unlatch and pad bits */
int i, prn, temp;
switch(last_mode) {
case DM_C40:
case DM_TEXT:
@ -757,7 +757,7 @@ void add_tail(unsigned char target[], int tp, int tail_length, int last_mode)
target[tp] = 254; tp++; /* Unlatch */
tail_length--;
}
for(i = tail_length; i > 0; i--) {
if(i == tail_length) {
target[tp] = 129; tp++; /* Pad */
@ -783,20 +783,20 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng
int H, W, FH, FW, datablock, bytes, rsblock;
int last_mode;
unsigned char *grid = 0;
binlen = dm200encode(symbol, source, binary, &last_mode, length);
if(binlen == 0) {
strcpy(symbol->errtxt, "Data too long to fit in symbol");
return ERROR_TOO_LONG;
}
if((symbol->option_2 >= 1) && (symbol->option_2 <= 30)) {
optionsize = intsymbol[symbol->option_2 - 1];
} else {
optionsize = -1;
}
calcsize = 29;
for(i = 29; i > -1; i--) {
if(matrixbytes[i] >= binlen) {
@ -816,7 +816,7 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng
calcsize++;
}
}
symbolsize = optionsize;
if(calcsize > optionsize) {
symbolsize = calcsize;
@ -826,7 +826,7 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng
strcpy(symbol->errtxt, "Data does not fit in selected symbol size");
}
}
H = matrixH[symbolsize];
W = matrixW[symbolsize];
FH = matrixFH[symbolsize];
@ -834,13 +834,13 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng
bytes = matrixbytes[symbolsize];
datablock = matrixdatablock[symbolsize];
rsblock = matrixrsblock[symbolsize];
taillength = bytes - binlen;
if(taillength != 0) {
add_tail(binary, binlen, taillength, last_mode);
}
// ecc code
if(symbolsize == 29) { skew = 1; }
ecc200(binary, bytes, datablock, rsblock, skew);
@ -885,17 +885,17 @@ int data_matrix_200(struct zint_symbol *symbol, unsigned char source[], int leng
free(grid);
free(places);
}
symbol->rows = H;
symbol->width = W;
return error_number;
}
int dmatrix(struct zint_symbol *symbol, unsigned char source[], int length)
{
int error_number;
if(symbol->option_1 <= 1) {
/* ECC 200 */
error_number = data_matrix_200(symbol, source, length);