From 1d4cead56ec2ed58694e2450d3f4ebc5f4d54112 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Thu, 17 Nov 2016 22:32:06 +0000 Subject: [PATCH] Handle partial C40 or Text buffer properly when using Shift characters Relates to ticket #38 created by Brunt --- backend/dmatrix.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/backend/dmatrix.c b/backend/dmatrix.c index 283ef947..922f66f6 100644 --- a/backend/dmatrix.c +++ b/backend/dmatrix.c @@ -694,24 +694,26 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], next_mode = DM_ASCII; if (debug) printf("ASC "); } else { + /* Note process_buffer uses special values 41 - 43 to distinguish + Shift 1, Shift 2 and Shift 3 from characters [/0/x01/x02!"#'ab] */ if (source[sp] > 127) { process_buffer[*process_p] = 1; (*process_p)++; process_buffer[*process_p] = 30; (*process_p)++; /* Upper Shift */ - shift_set = c40_shift[source[sp] - 128]; + shift_set = 40 + c40_shift[source[sp] - 128]; value = c40_value[source[sp] - 128]; } else { - shift_set = c40_shift[source[sp]]; + shift_set = 40 + c40_shift[source[sp]]; value = c40_value[source[sp]]; } if (gs1 && (source[sp] == '[')) { - shift_set = 2; + shift_set = 42; value = 27; /* FNC1 */ } - if (shift_set != 0) { + if (shift_set != 40) { process_buffer[*process_p] = shift_set - 1; (*process_p)++; } @@ -721,7 +723,7 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], if (*process_p >= 3) { int iv; - iv = (1600 * process_buffer[0]) + (40 * process_buffer[1]) + (process_buffer[2]) + 1; + iv = (1600 * (process_buffer[0] % 40)) + (40 * (process_buffer[1] % 40)) + (process_buffer[2] % 40) + 1; target[tp] = (unsigned char) (iv / 256); tp++; target[tp] = iv % 256; @@ -757,24 +759,26 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], next_mode = DM_ASCII; if (debug) printf("ASC "); } else { + /* Note process_buffer uses special values 41 - 43 to distinguish + Shift 1, Shift 2 and Shift 3 from characters [/0/x01/x02!"#'AB] */ if (source[sp] > 127) { process_buffer[*process_p] = 1; (*process_p)++; process_buffer[*process_p] = 30; (*process_p)++; /* Upper Shift */ - shift_set = text_shift[source[sp] - 128]; + shift_set = 40 + text_shift[source[sp] - 128]; value = text_value[source[sp] - 128]; } else { - shift_set = text_shift[source[sp]]; + shift_set = 40 + text_shift[source[sp]]; value = text_value[source[sp]]; } if (gs1 && (source[sp] == '[')) { - shift_set = 2; + shift_set = 42; value = 27; /* FNC1 */ } - if (shift_set != 0) { + if (shift_set != 40) { process_buffer[*process_p] = shift_set - 1; (*process_p)++; } @@ -784,7 +788,7 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], if (*process_p >= 3) { int iv; - iv = (1600 * process_buffer[0]) + (40 * process_buffer[1]) + (process_buffer[2]) + 1; + iv = (1600 * (process_buffer[0] % 40)) + (40 * (process_buffer[1] % 40)) + (process_buffer[2] % 40) + 1; target[tp] = (unsigned char) (iv / 256); tp++; target[tp] = iv % 256; @@ -998,7 +1002,7 @@ static int dm200encode_remainder(unsigned char target[], int target_length, cons if (process_p == 2) // 2 data characters left to encode. { // Pad with shift 1 value (0) and encode as double. - int intValue = (1600 * process_buffer[0]) + (40 * process_buffer[1]) + 1; // ie (0 + 1). + int intValue = (1600 * (process_buffer[0] % 40)) + (40 * (process_buffer[1] % 40)) + 1; // ie (0 + 1). target[target_length] = (unsigned char) (intValue / 256); target_length++; target[target_length] = (unsigned char) (intValue % 256); @@ -1009,7 +1013,7 @@ static int dm200encode_remainder(unsigned char target[], int target_length, cons if (symbols_left > process_p) { target[target_length] = (254); target_length++; // Unlatch and encode remaining data in ascii. - if (process_p == 1 || (process_p == 2 && process_buffer[0] < 3)) // Check for a shift value. + if (process_p == 1 || (process_p == 2 && process_buffer[0] > 40)) // Check for a shift value. { target[target_length] = source[inputlen - 1] + 1; target_length++;