Fixes for eci, binary div, upce, other

This commit is contained in:
gitlost 2019-09-01 20:23:15 +01:00
parent 89c2ee6f1d
commit d76cdd615b
11 changed files with 80 additions and 100 deletions

View File

@ -17,6 +17,9 @@ add_definitions (-DZINT_VERSION=\"${ZINT_VERSION}\" -Wall)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules" ) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules" )
set(ZINT_DEBUG FALSE CACHE BOOL "Set debug compile flag")
set(ZINT_SANITIZE FALSE CACHE BOOL "Set sanitize compile/link flags")
include (SetPaths.cmake) include (SetPaths.cmake)
INCLUDE (CheckCXXCompilerFlag) INCLUDE (CheckCXXCompilerFlag)
@ -26,6 +29,15 @@ if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC)
if (CXX_COMPILER_FLAG_WALL) if (CXX_COMPILER_FLAG_WALL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
endif (CXX_COMPILER_FLAG_WALL) endif (CXX_COMPILER_FLAG_WALL)
if (ZINT_DEBUG)
add_compile_options("-g")
add_compile_options("-O0")
endif (ZINT_DEBUG)
if (ZINT_SANITIZE)
add_compile_options("-fsanitize=undefined")
add_compile_options("-fsanitize=address")
set (CMAKE_EXE_LINKER_FLAGS "-fsanitize=undefined -fsanitize=address")
endif (ZINT_SANITIZE)
endif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC) endif (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC)

View File

@ -582,6 +582,9 @@ int channel_code(struct zint_symbol *symbol, unsigned char source[], int length)
NextS(channels, 3, channels, channels); NextS(channels, 3, channels, channels);
zeroes = channels - 1 - length; zeroes = channels - 1 - length;
if (zeroes < 0) {
zeroes = 0;
}
memset(hrt, '0', zeroes); memset(hrt, '0', zeroes);
strcpy(hrt + zeroes, (char *) source); strcpy(hrt + zeroes, (char *) source);
ustrcpy(symbol->text, (unsigned char *) hrt); ustrcpy(symbol->text, (unsigned char *) hrt);

View File

@ -46,11 +46,9 @@ int utf_to_eci(const int eci, const unsigned char source[], unsigned char dest[]
int done; int done;
if (eci == 26) { if (eci == 26) {
int in_length = (int) *length;
/* Unicode mode, do not process - just copy data across */ /* Unicode mode, do not process - just copy data across */
for (in_posn = 0; in_posn < in_length; in_posn++) { memcpy(dest, source, *length);
dest[in_posn] = source[in_posn]; dest[*length] = '\0';
}
return 0; return 0;
} }
@ -107,42 +105,8 @@ int utf_to_eci(const int eci, const unsigned char source[], unsigned char dest[]
glyph += (source[in_posn + 2] & 0x3f); glyph += (source[in_posn + 2] & 0x3f);
} }
if ((source[in_posn] >= 0xf0) && (source[in_posn] < 0xf7)) { if (source[in_posn] >= 0xf0 || glyph > 0x2122) {
/* Four-byte character */ /* Not in any ISO 8859 or Windows page */
bytelen = 4;
glyph = (source[in_posn] & 0x07) << 18;
if (*length < (in_posn + 2)) {
return ZINT_ERROR_INVALID_DATA;
}
if (*length < (in_posn + 3)) {
return ZINT_ERROR_INVALID_DATA;
}
if (*length < (in_posn + 4)) {
return ZINT_ERROR_INVALID_DATA;
}
if (source[in_posn + 1] > 0xc0) {
return ZINT_ERROR_INVALID_DATA;
}
if (source[in_posn + 2] > 0xc0) {
return ZINT_ERROR_INVALID_DATA;
}
if (source[in_posn + 3] > 0xc0) {
return ZINT_ERROR_INVALID_DATA;
}
glyph += (source[in_posn + 1] & 0x3f) << 12;
glyph += (source[in_posn + 2] & 0x3f) << 6;
glyph += (source[in_posn + 3] & 0x3f);
}
if (source[in_posn] >= 0xf7) {
/* More than 4 bytes not supported */
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
} }
@ -269,6 +233,9 @@ int utf_to_eci(const int eci, const unsigned char source[], unsigned char dest[]
default: default:
break; break;
} }
if (done) {
break;
}
} }
if (!(done)) { if (!(done)) {

View File

@ -196,6 +196,7 @@ static const unsigned short int iso_8859_16[] = {// Latin alphabet No. 10
0x00a0, 0x0104, 0x0105, 0x0141, 0x20ac, 0x201e, 0x0160, 0x00a7, 0x0161, 0x00a9, 0x0218, 0x00ab, 0x0179, 0x00ad, 0x017a, 0x017b, 0x00a0, 0x0104, 0x0105, 0x0141, 0x20ac, 0x201e, 0x0160, 0x00a7, 0x0161, 0x00a9, 0x0218, 0x00ab, 0x0179, 0x00ad, 0x017a, 0x017b,
0x00b0, 0x00b1, 0x010c, 0x0142, 0x017d, 0x201d, 0x00b6, 0x00b7, 0x017e, 0x010d, 0x0219, 0x00bb, 0x0152, 0x0153, 0x0178, 0x017c, 0x00b0, 0x00b1, 0x010c, 0x0142, 0x017d, 0x201d, 0x00b6, 0x00b7, 0x017e, 0x010d, 0x0219, 0x00bb, 0x0152, 0x0153, 0x0178, 0x017c,
0x00c0, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0106, 0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, 0x00c0, 0x00c1, 0x00c2, 0x0102, 0x00c4, 0x0106, 0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
0x0110, 0x0143, 0x00d2, 0x00d3, 0x00d4, 0x0150, 0x00d6, 0x015a, 0x0170, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x0118, 0x021a, 0x00df,
0x00e0, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x0107, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00e0, 0x00e1, 0x00e2, 0x0103, 0x00e4, 0x0107, 0x00e6, 0x00e7, 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
0x0111, 0x0144, 0x00f2, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x015b, 0x0171, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0119, 0x021b, 0x00ff 0x0111, 0x0144, 0x00f2, 0x00f3, 0x00f4, 0x0151, 0x00f6, 0x015b, 0x0171, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x0119, 0x021b, 0x00ff
}; };

View File

@ -254,6 +254,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
int codeword[10]; int codeword[10];
unsigned short characters[10]; unsigned short characters[10];
short int bar_map[130]; short int bar_map[130];
int zip_len;
error_number = 0; error_number = 0;
@ -301,7 +302,13 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
strcpy(symbol->errtxt, "452: Invalid length tracking code"); strcpy(symbol->errtxt, "452: Invalid length tracking code");
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
} }
if (strlen(zip) > 11) { if (tracker[1] > '4') {
strcpy(symbol->errtxt, "454: Invalid Barcode Identifier");
return ZINT_ERROR_INVALID_DATA;
}
zip_len = strlen(zip);
if (zip_len != 0 && zip_len != 5 && zip_len != 9 && zip_len != 11) {
strcpy(symbol->errtxt, "453: Invalid ZIP code"); strcpy(symbol->errtxt, "453: Invalid ZIP code");
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
} }
@ -314,7 +321,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
accum[i] = 0; accum[i] = 0;
} }
for (read = 0; read < strlen(zip); read++) { for (read = 0; read < zip_len; read++) {
binary_multiply(accum, "10"); binary_multiply(accum, "10");
binary_load(x_reg, "0", 1); binary_load(x_reg, "0", 1);
@ -332,13 +339,13 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
x_reg[i] = accum[i]; x_reg[i] = accum[i];
} }
if (strlen(zip) > 9) { if (zip_len > 9) {
strcpy(zip_adder, "1000100001"); strcpy(zip_adder, "1000100001");
} else { } else {
if (strlen(zip) > 5) { if (zip_len > 5) {
strcpy(zip_adder, "100001"); strcpy(zip_adder, "100001");
} else { } else {
if (strlen(zip) > 0) { if (zip_len > 0) {
strcpy(zip_adder, "1"); strcpy(zip_adder, "1");
} else { } else {
strcpy(zip_adder, "0"); strcpy(zip_adder, "0");
@ -439,7 +446,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
x_reg[94] = 1; x_reg[94] = 1;
for (i = 92; i >= 0; i--) { for (i = 92; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }
@ -465,7 +472,7 @@ int imail(struct zint_symbol *symbol, unsigned char source[], int length) {
x_reg[93] = 1; x_reg[93] = 1;
x_reg[91] = 1; x_reg[91] = 1;
for (i = 91; i >= 0; i--) { for (i = 91; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }

View File

@ -173,7 +173,7 @@ short int islarger(short int accum[], short int reg[]) {
latch = 1; latch = 1;
} }
i--; i--;
} while ((latch == 0) && (i >= -1)); } while ((latch == 0) && (i >= 0));
return larger; return larger;
} }

View File

@ -917,10 +917,10 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int
#endif #endif
error_number = 0; error_number = 0;
if (in_length == 0) { if (in_length <= 0) {
in_length = (int)ustrlen(source); in_length = (int)ustrlen(source);
} }
if (in_length == 0) { if (in_length <= 0) {
strcpy(symbol->errtxt, "205: No input data"); strcpy(symbol->errtxt, "205: No input data");
error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA);
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_DATA;
@ -977,9 +977,6 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int
if (symbol->symbology == 36) { if (symbol->symbology == 36) {
symbol->symbology = BARCODE_UPCA; symbol->symbology = BARCODE_UPCA;
} }
if (symbol->symbology == 38) {
symbol->symbology = BARCODE_UPCE;
}
if ((symbol->symbology >= 41) && (symbol->symbology <= 45)) { if ((symbol->symbology >= 41) && (symbol->symbology <= 45)) {
symbol->symbology = BARCODE_POSTNET; symbol->symbology = BARCODE_POSTNET;
} }
@ -1157,8 +1154,23 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int
case BARCODE_MICROQR: case BARCODE_MICROQR:
case BARCODE_GRIDMATRIX: case BARCODE_GRIDMATRIX:
case BARCODE_HANXIN: case BARCODE_HANXIN:
error_number = utf_to_eci(symbol->eci, source, local_source, (size_t*)&in_length); {
#ifndef _MSC_VER
unsigned char temp[in_length + 1];
#else
unsigned char *temp = (unsigned char*) _alloca(in_length + 1);
#endif
size_t temp_len = in_length;
memcpy(temp, local_source, temp_len);
temp[temp_len] = '\0';
error_number = utf_to_eci(symbol->eci, local_source, temp, &temp_len);
if (error_number == 0) {
in_length = (int) temp_len;
memcpy(local_source, temp, in_length);
local_source[in_length] = '\0';
error_number = extended_charset(symbol, local_source, in_length); error_number = extended_charset(symbol, local_source, in_length);
}
}
break; break;
default: default:
error_number = reduced_charset(symbol, local_source, in_length); error_number = reduced_charset(symbol, local_source, in_length);

View File

@ -464,14 +464,14 @@ int mailmark(struct zint_symbol *symbol, const unsigned char source[], const siz
} }
a[96] = 1; a[96] = 1;
for (i = 91; i >= 0; i--) { for (i = 91; i >= 0; i--) {
b[i] = islarger(cdv, a); b[i] = !islarger(a, cdv);
if (b[i] == 1) { if (b[i] == 1) {
binary_subtract(cdv, a); binary_subtract(cdv, a);
} }
shiftdown(a); shiftdown(a);
} }
data[j] = (cdv[5] * 32) + (cdv[4] * 16) + (cdv[3] * 8) + (cdv[2] * 4) + data[j] = (cdv[4] * 16) + (cdv[3] * 8) + (cdv[2] * 4) +
(cdv[1] * 2) + cdv[0]; (cdv[1] * 2) + cdv[0];
} }
@ -486,14 +486,14 @@ int mailmark(struct zint_symbol *symbol, const unsigned char source[], const siz
a[93] = 1; a[93] = 1;
a[92] = 1; a[92] = 1;
for (i = 91; i >= 0; i--) { for (i = 91; i >= 0; i--) {
b[i] = islarger(cdv, a); b[i] = !islarger(a, cdv);
if (b[i] == 1) { if (b[i] == 1) {
binary_subtract(cdv, a); binary_subtract(cdv, a);
} }
shiftdown(a); shiftdown(a);
} }
data[j] = (cdv[5] * 32) + (cdv[4] * 16) + (cdv[3] * 8) + (cdv[2] * 4) + data[j] = (cdv[4] * 16) + (cdv[3] * 8) + (cdv[2] * 4) +
(cdv[1] * 2) + cdv[0]; (cdv[1] * 2) + cdv[0];
} }

View File

@ -389,7 +389,7 @@ int maxi_text_process(int mode, unsigned char source[], int length, int eci) {
character[i] = 61; character[i] = 61;
maxi_bump(set, character, i); maxi_bump(set, character, i);
character[i] = 61; character[i] = 61;
current_set = 3; current_set = 4;
length++; length++;
i += 3; i += 3;
} else { } else {
@ -405,7 +405,7 @@ int maxi_text_process(int mode, unsigned char source[], int length, int eci) {
character[i] = 62; character[i] = 62;
maxi_bump(set, character, i); maxi_bump(set, character, i);
character[i] = 62; character[i] = 62;
current_set = 3; current_set = 5;
length++; length++;
i += 3; i += 3;
} else { } else {

View File

@ -221,7 +221,7 @@ int rss14(struct zint_symbol *symbol, unsigned char source[], int src_len) {
} }
for (i = 24; i >= 0; i--) { for (i = 24; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }
@ -245,7 +245,7 @@ int rss14(struct zint_symbol *symbol, unsigned char source[], int src_len) {
} }
for (i = 24; i >= 0; i--) { for (i = 24; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }
@ -275,7 +275,7 @@ int rss14(struct zint_symbol *symbol, unsigned char source[], int src_len) {
} }
for (i = 24; i >= 0; i--) { for (i = 24; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }
@ -790,7 +790,7 @@ int rsslimited(struct zint_symbol *symbol, unsigned char source[], int src_len)
} }
for (i = 24; i >= 0; i--) { for (i = 24; i >= 0; i--) {
y_reg[i] = islarger(accum, x_reg); y_reg[i] = !islarger(x_reg, accum);
if (y_reg[i] == 1) { if (y_reg[i] == 1) {
binary_subtract(accum, x_reg); binary_subtract(accum, x_reg);
} }

View File

@ -138,7 +138,7 @@ int upca(struct zint_symbol *symbol, unsigned char source[], char dest[]) {
gtin[length - 1] = '\0'; gtin[length - 1] = '\0';
if (source[length - 1] != upc_check(gtin)) { if (source[length - 1] != upc_check(gtin)) {
strcpy(symbol->errtxt, "270: Invalid check digit"); strcpy(symbol->errtxt, "270: Invalid check digit");
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_CHECK;
} }
gtin[length - 1] = upc_check(gtin); gtin[length - 1] = upc_check(gtin);
} }
@ -150,7 +150,7 @@ int upca(struct zint_symbol *symbol, unsigned char source[], char dest[]) {
/* UPC E is a zero-compressed version of UPC A */ /* UPC E is a zero-compressed version of UPC A */
int upce(struct zint_symbol *symbol, unsigned char source[], char dest[]) { int upce(struct zint_symbol *symbol, unsigned char source[], char dest[]) {
unsigned int i, num_system; unsigned int i, num_system;
char emode, equivalent[12], check_digit, parity[8], temp[8]; char emode, equivalent[12], check_digit, parity[8], temp[9];
char hrt[9]; char hrt[9];
/* Two number systems can be used - system 0 and system 1 */ /* Two number systems can be used - system 0 and system 1 */
@ -191,7 +191,7 @@ int upce(struct zint_symbol *symbol, unsigned char source[], char dest[]) {
} }
strcpy(temp, (char*) source); strcpy(temp, (char*) source);
strcpy(hrt, (char*) source); strcpy(hrt, (char*) source);
for (i = 1; i <= 7; i++) { for (i = 1; i <= 8; i++) {
source[i - 1] = temp[i]; source[i - 1] = temp[i];
} }
} else { } else {
@ -294,7 +294,7 @@ int upce(struct zint_symbol *symbol, unsigned char source[], char dest[]) {
} else { } else {
if (hrt[7] != check_digit) { if (hrt[7] != check_digit) {
strcpy(symbol->errtxt, "274: Invalid check digit"); strcpy(symbol->errtxt, "274: Invalid check digit");
return ZINT_ERROR_INVALID_DATA; return ZINT_ERROR_INVALID_CHECK;
} }
} }
ustrcpy(symbol->text, (unsigned char*) hrt); ustrcpy(symbol->text, (unsigned char*) hrt);
@ -517,7 +517,7 @@ static int isbn(struct zint_symbol *symbol, unsigned char source[], const size_t
} }
/* Input must be 9, 10 or 13 characters */ /* Input must be 9, 10 or 13 characters */
if (((src_len < 9) || (src_len > 13)) || ((src_len > 10) && (src_len < 13))) { if (src_len != 9 && src_len != 10 && src_len != 13) {
strcpy(symbol->errtxt, "278: Input wrong length"); strcpy(symbol->errtxt, "278: Input wrong length");
return ZINT_ERROR_TOO_LONG; return ZINT_ERROR_TOO_LONG;
} }
@ -535,25 +535,6 @@ static int isbn(struct zint_symbol *symbol, unsigned char source[], const size_t
return ZINT_ERROR_INVALID_CHECK; return ZINT_ERROR_INVALID_CHECK;
} }
source[12] = '\0'; source[12] = '\0';
ean13(symbol, source, dest);
}
if (src_len == 10) /* Using 10 digit ISBN */ {
check_digit = isbn_check(source);
if (check_digit != source[src_len - 1]) {
strcpy(symbol->errtxt, "281: Incorrect ISBN check");
return ZINT_ERROR_INVALID_CHECK;
}
for (i = 13; i > 0; i--) {
source[i] = source[i - 3];
}
source[0] = '9';
source[1] = '7';
source[2] = '8';
source[12] = '\0';
ean13(symbol, source, dest);
} }
if (src_len == 9) /* Using 9 digit SBN */ { if (src_len == 9) /* Using 9 digit SBN */ {
@ -562,27 +543,24 @@ static int isbn(struct zint_symbol *symbol, unsigned char source[], const size_t
source[i] = source[i - 1]; source[i] = source[i - 1];
} }
source[0] = '0'; source[0] = '0';
/* Verify check digit */
check_digit = isbn_check(source);
if (check_digit != source[ustrlen(source) - 1]) {
strcpy(symbol->errtxt, "282: Incorrect SBN check");
return ZINT_ERROR_INVALID_CHECK;
} }
/* Convert to EAN-13 number */ if (src_len == 9 || src_len == 10) /* Using 10 digit ISBN or 9 digit SBN padded with leading zero */ {
for (i = 13; i > 0; i--) { check_digit = isbn_check(source);
if (check_digit != source[ustrlen(source) - 1]) {
strcpy(symbol->errtxt, src_len == 9 ? "281: Incorrect SBN check" : "281: Incorrect ISBN check");
return ZINT_ERROR_INVALID_CHECK;
}
for (i = 11; i > 2; i--) {
source[i] = source[i - 3]; source[i] = source[i - 3];
} }
source[0] = '9'; source[0] = '9';
source[1] = '7'; source[1] = '7';
source[2] = '8'; source[2] = '8';
source[12] = '\0'; source[12] = '\0';
ean13(symbol, source, dest);
} }
return 0; return ean13(symbol, source, dest);
} }
/* Add leading zeroes to EAN and UPC strings */ /* Add leading zeroes to EAN and UPC strings */
@ -862,7 +840,7 @@ int eanx(struct zint_symbol *symbol, unsigned char source[], int src_len) {
break; break;
case BARCODE_UPCE: case BARCODE_UPCE:
case BARCODE_UPCE_CHK: case BARCODE_UPCE_CHK:
if ((ustrlen(first_part) >= 6) && (ustrlen(first_part) <= 8)) { if ((ustrlen(first_part) >= 6) && (ustrlen(first_part) <= (symbol->symbology == BARCODE_UPCE ? 7 : 8))) {
error_number = upce(symbol, first_part, (char*) dest); error_number = upce(symbol, first_part, (char*) dest);
} else { } else {
strcpy(symbol->errtxt, "290: Input wrong length"); strcpy(symbol->errtxt, "290: Input wrong length");