From 9f5ae4cbb2163509f793ddb07c783fa99ec46027 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Sat, 22 Aug 2020 11:09:57 +0100 Subject: [PATCH] First stage at creating ZPL compatability mode Partly resolves #142 and relates to discussion in #197 --- backend/aztec.c | 6 +++- backend/gs1.c | 22 ++++++++++--- backend/library.c | 81 +++++++++++++++++++++++++++++++++-------------- backend/pdf417.c | 48 +++++++++++++++++++++------- backend/zint.h | 7 ++++ frontend/main.c | 8 +++++ 6 files changed, 131 insertions(+), 41 deletions(-) diff --git a/backend/aztec.c b/backend/aztec.c index 246bc480..c9c72236 100644 --- a/backend/aztec.c +++ b/backend/aztec.c @@ -1018,7 +1018,11 @@ INTERNAL int aztec(struct zint_symbol *symbol, unsigned char source[], const siz if (!((symbol->option_1 >= -1) && (symbol->option_1 <= 4))) { strcpy(symbol->errtxt, "503: Invalid error correction level - using default instead"); - err_code = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + err_code = ZINT_WARN_INVALID_OPTION; + } symbol->option_1 = -1; } diff --git a/backend/gs1.c b/backend/gs1.c index 5824d8fe..99ff3c32 100644 --- a/backend/gs1.c +++ b/backend/gs1.c @@ -86,6 +86,7 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[] int bracket_level, max_bracket_level, ai_length, max_ai_length, min_ai_length; int ai_count; int error_latch; + int error_value; #ifdef _MSC_VER int *ai_value; int *ai_location; @@ -124,7 +125,11 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[] if (source[0] != '[') { strcpy(symbol->errtxt, "252: Data does not start with an AI"); - return ZINT_ERROR_INVALID_DATA; + if (symbol->warn_level != WARN_ZPL_COMPAT) { + return ZINT_ERROR_INVALID_DATA; + } else { + error_value = ZINT_WARN_NONCOMPLIANT; + } } /* Check the position of the brackets */ @@ -135,6 +140,7 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[] min_ai_length = 5; j = 0; ai_latch = 0; + error_value = 0; for (i = 0; i < (int) src_len; i++) { ai_length += j; if (((j == 1) && (source[i] != ']')) && ((source[i] < '0') || (source[i] > '9'))) { @@ -661,14 +667,22 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[] itostr(ai_string, ai_value[i]); strcpy(symbol->errtxt, "259: Invalid data length for AI "); strcat(symbol->errtxt, ai_string); - return ZINT_ERROR_INVALID_DATA; + if (symbol->warn_level != WARN_ZPL_COMPAT) { + return ZINT_ERROR_INVALID_DATA; + } else { + error_value = ZINT_WARN_NONCOMPLIANT; + } } if (error_latch == 2) { itostr(ai_string, ai_value[i]); strcpy(symbol->errtxt, "260: Invalid AI value "); strcat(symbol->errtxt, ai_string); - return ZINT_ERROR_INVALID_DATA; + if (symbol->warn_level != WARN_ZPL_COMPAT) { + return ZINT_ERROR_INVALID_DATA; + } else { + error_value = ZINT_WARN_NONCOMPLIANT; + } } } @@ -706,5 +720,5 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[] reduced[j] = '\0'; /* the character '[' in the reduced string refers to the FNC1 character */ - return 0; + return error_value; } diff --git a/backend/library.c b/backend/library.c index 1d0bc961..84e81428 100644 --- a/backend/library.c +++ b/backend/library.c @@ -74,6 +74,7 @@ struct zint_symbol *ZBarcode_Create() { symbol->dot_size = 4.0 / 5.0; symbol->vector = NULL; symbol->debug = 0; + symbol->warn_level = WARN_DEFAULT; return symbol; } @@ -1044,9 +1045,13 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int /* First check the symbology field */ if (symbol->symbology < 1) { - strcpy(symbol->errtxt, "206: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "206: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } /* symbol->symbologys 1 to 86 are defined by tbarcode */ @@ -1063,9 +1068,13 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int symbol->symbology = BARCODE_UPCA; } if (symbol->symbology == 19) { - strcpy(symbol->errtxt, "207: Codabar 18 not supported, using Codabar"); - symbol->symbology = BARCODE_CODABAR; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "207: Codabar 18 not supported"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return WARN_FAIL_ALL; + } else { + symbol->symbology = BARCODE_CODABAR; + error_number = ZINT_WARN_INVALID_OPTION; + } } if (symbol->symbology == 26) { symbol->symbology = BARCODE_UPCA; @@ -1090,9 +1099,13 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int symbol->symbology = BARCODE_NVE18; } if (symbol->symbology == 54) { - strcpy(symbol->errtxt, "210: General Parcel Code not supported, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "210: General Parcel Code not supported"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } if ((symbol->symbology == 59) || (symbol->symbology == 61)) { symbol->symbology = BARCODE_CODE128; @@ -1113,14 +1126,22 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int symbol->symbology = BARCODE_GS1_128; } if (symbol->symbology == 91) { - strcpy(symbol->errtxt, "212: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "212: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } if ((symbol->symbology >= 94) && (symbol->symbology <= 95)) { - strcpy(symbol->errtxt, "213: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "213: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } if (symbol->symbology == 100) { symbol->symbology = BARCODE_HIBC_128; @@ -1144,25 +1165,37 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int symbol->symbology = BARCODE_HIBC_BLOCKF; } if ((symbol->symbology == 113) || (symbol->symbology == 114)) { - strcpy(symbol->errtxt, "214: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "214: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } if (symbol->symbology == 115) { symbol->symbology = BARCODE_DOTCODE; } if ((symbol->symbology >= 117) && (symbol->symbology <= 127)) { if (symbol->symbology != 121) { - strcpy(symbol->errtxt, "215: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "215: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } } /* Everything from 128 up is Zint-specific */ if (symbol->symbology > 145) { - strcpy(symbol->errtxt, "216: Symbology out of range, using Code 128"); - symbol->symbology = BARCODE_CODE128; - error_number = ZINT_WARN_INVALID_OPTION; + strcpy(symbol->errtxt, "216: Symbology out of range"); + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->symbology = BARCODE_CODE128; + error_number = ZINT_WARN_INVALID_OPTION; + } } if ((!(supports_eci(symbol->symbology))) && (symbol->eci != 0)) { diff --git a/backend/pdf417.c b/backend/pdf417.c index 3ae87692..713ec056 100644 --- a/backend/pdf417.c +++ b/backend/pdf417.c @@ -836,13 +836,21 @@ INTERNAL int pdf417enc(struct zint_symbol *symbol, unsigned char source[], const if ((symbol->option_1 < -1) || (symbol->option_1 > 8)) { strcpy(symbol->errtxt, "460: Security value out of range"); - symbol->option_1 = -1; - error_number = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_1 = -1; + error_number = ZINT_WARN_INVALID_OPTION; + } } if ((symbol->option_2 < 0) || (symbol->option_2 > 30)) { strcpy(symbol->errtxt, "461: Number of columns out of range"); - symbol->option_2 = 0; - error_number = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_2 = 0; + error_number = ZINT_WARN_INVALID_OPTION; + } } /* 349 */ @@ -973,8 +981,12 @@ INTERNAL int micro_pdf417(struct zint_symbol *symbol, unsigned char chaine[], co } if (symbol->option_2 > 4) { strcpy(symbol->errtxt, "468: Specified width out of range"); - symbol->option_2 = 0; - codeerr = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_2 = 0; + codeerr = ZINT_WARN_INVALID_OPTION; + } } if (debug) { @@ -991,23 +1003,35 @@ INTERNAL int micro_pdf417(struct zint_symbol *symbol, unsigned char chaine[], co if ((symbol->option_2 == 1) && (mclength > 20)) { /* the user specified 1 column but the data doesn't fit - go to automatic */ - symbol->option_2 = 0; strcpy(symbol->errtxt, "469: Specified symbol size too small for data"); - codeerr = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_2 = 0; + codeerr = ZINT_WARN_INVALID_OPTION; + } } if ((symbol->option_2 == 2) && (mclength > 37)) { /* the user specified 2 columns but the data doesn't fit - go to automatic */ - symbol->option_2 = 0; strcpy(symbol->errtxt, "470: Specified symbol size too small for data"); - codeerr = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_2 = 0; + codeerr = ZINT_WARN_INVALID_OPTION; + } } if ((symbol->option_2 == 3) && (mclength > 82)) { /* the user specified 3 columns but the data doesn't fit - go to automatic */ - symbol->option_2 = 0; strcpy(symbol->errtxt, "471: Specified symbol size too small for data"); - codeerr = ZINT_WARN_INVALID_OPTION; + if (symbol->warn_level == WARN_FAIL_ALL) { + return ZINT_ERROR_INVALID_OPTION; + } else { + symbol->option_2 = 0; + codeerr = ZINT_WARN_INVALID_OPTION; + } } if (symbol->option_2 == 1) { diff --git a/backend/zint.h b/backend/zint.h index 7bb88ea1..65579998 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -106,6 +106,7 @@ extern "C" { float dot_size; struct zint_vector *vector; int debug; + int warn_level; }; #define ZINT_VERSION_MAJOR 2 @@ -268,6 +269,7 @@ extern "C" { // Warning and error conditions #define ZINT_WARN_INVALID_OPTION 2 #define ZINT_WARN_USES_ECI 3 +#define ZINT_WARN_NONCOMPLIANT 4 #define ZINT_ERROR_TOO_LONG 5 #define ZINT_ERROR_INVALID_DATA 6 #define ZINT_ERROR_INVALID_CHECK 7 @@ -292,6 +294,11 @@ extern "C" { #define ZINT_DEBUG_PRINT 1 #define ZINT_DEBUG_TEST 2 +// Warning warn +#define WARN_DEFAULT 0 +#define WARN_ZPL_COMPAT 1 +#define WARN_FAIL_ALL 2 + #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(_MSC_VER) #if defined (DLL_EXPORT) || defined(PIC) || defined(_USRDLL) #define ZINT_EXTERN __declspec(dllexport) diff --git a/frontend/main.c b/frontend/main.c index 1e29f0f0..3fd5c5fe 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -125,6 +125,8 @@ static void usage(void) { " -t, --types Display table of barcode types\n" " --vers=NUMBER Set symbol version (size, check digits, other options)\n" " -w, --whitesp=NUMBER Set width of whitespace in multiples of X-dimension\n" + " --werror Convert all warnings into errors\n" + " --wzpl ZPL compatibility mode (allows non-standard symbols)\n" , ZINT_VERSION_MAJOR, ZINT_VERSION_MINOR, ZINT_VERSION_RELEASE); } @@ -885,6 +887,12 @@ int main(int argc, char **argv) { if (!strcmp(long_options[option_index].name, "nobackground")) { strcpy(my_symbol->bgcolour, "ffffff00"); } + if (!strcmp(long_options[option_index].name, "werror")) { + my_symbol->warn_level = WARN_FAIL_ALL; + } + if (!strcmp(long_options[option_index].name, "wzpl")) { + my_symbol->warn_level = WARN_ZPL_COMPAT; + } break; case 'h':