diff --git a/backend/code128.c b/backend/code128.c index 425f1004..e9279cb6 100644 --- a/backend/code128.c +++ b/backend/code128.c @@ -30,6 +30,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ #include #include @@ -689,7 +690,11 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len float glyph_count; char dest[1000]; int separator_row, linkage_flag, c_count; - +#ifndef _MSC_VER + char reduced[length + 1]; +#else + char* reduced = (char*) _alloca(length + 1); +#endif error_number = 0; strcpy(dest, ""); linkage_flag = 0; @@ -707,13 +712,6 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len strcpy(symbol->errtxt, "342: Input too long"); return ZINT_ERROR_TOO_LONG; } - for (i = 0; i < length; i++) { - if (source[i] == '\0') { - /* Null characters not allowed! */ - strcpy(symbol->errtxt, "343: NULL character in input data"); - return ZINT_ERROR_INVALID_DATA; - } - } /* if part of a composite symbol make room for the separator pattern */ if (symbol->symbology == BARCODE_EAN128_CC) { @@ -722,12 +720,17 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len symbol->rows += 1; } + error_number = gs1_verify(symbol, source, length, reduced); + if (error_number != 0) { + return error_number; + } + /* Decide on mode using same system as PDF417 and rules of ISO 15417 Annex E */ indexliste = 0; indexchaine = 0; - mode = parunmodd(source[indexchaine]); - if (source[indexchaine] == '[') { + mode = parunmodd(reduced[indexchaine]); + if (reduced[indexchaine] == '[') { mode = ABORC; } @@ -737,16 +740,16 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len do { list[1][indexliste] = mode; - while ((list[1][indexliste] == mode) && (indexchaine < (int) ustrlen(source))) { + while ((list[1][indexliste] == mode) && (indexchaine < (int) strlen(reduced))) { list[0][indexliste]++; indexchaine++; - mode = parunmodd(source[indexchaine]); - if (source[indexchaine] == '[') { + mode = parunmodd(reduced[indexchaine]); + if (reduced[indexchaine] == '[') { mode = ABORC; } } indexliste++; - } while (indexchaine < (int) ustrlen(source)); + } while (indexchaine < (int) strlen(reduced)); dxsmooth(&indexliste); @@ -774,7 +777,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len c_count = 0; for (i = 0; i < read; i++) { if (set[i] == 'C') { - if (source[i] == '[') { + if (reduced[i] == '[') { if (c_count & 1) { if ((i - c_count) != 0) { set[i - c_count] = 'B'; @@ -814,7 +817,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len being too long */ last_set = ' '; glyph_count = 0.0; - for (i = 0; i < (int) ustrlen(source); i++) { + for (i = 0; i < (int) strlen(reduced); i++) { if ((set[i] == 'a') || (set[i] == 'b')) { glyph_count = glyph_count + 1.0; } @@ -825,7 +828,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len } } - if ((set[i] == 'C') && (source[i] != '[')) { + if ((set[i] == 'C') && (reduced[i] != '[')) { glyph_count = glyph_count + 0.5; } else { glyph_count = glyph_count + 1.0; @@ -885,20 +888,20 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len bar_characters++; } - if (source[read] != '[') { + if (reduced[read] != '[') { switch (set[read]) { /* Encode data characters */ case 'A': case 'a': - c128_set_a(source[read], dest, values, &bar_characters); + c128_set_a(reduced[read], dest, values, &bar_characters); read++; break; case 'B': case 'b': - c128_set_b(source[read], dest, values, &bar_characters); + c128_set_b(reduced[read], dest, values, &bar_characters); read++; break; case 'C': - c128_set_c(source[read], source[read + 1], dest, values, &bar_characters); + c128_set_c(reduced[read], reduced[read + 1], dest, values, &bar_characters); read += 2; break; } @@ -908,7 +911,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len bar_characters++; read++; } - } while (read < (int) ustrlen(source)); + } while (read < (int) strlen(reduced)); /* "...note that the linkage flag is an extra code set character between the last data character and the Symbol Check Character" (GS1 Specification) */ @@ -919,7 +922,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len case 1: case 2: /* CC-A or CC-B 2D component */ - switch (set[ustrlen(source) - 1]) { + switch (set[strlen(reduced) - 1]) { case 'A': linkage_flag = 100; break; case 'B': linkage_flag = 99; @@ -930,7 +933,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len break; case 3: /* CC-C 2D component */ - switch (set[ustrlen(source) - 1]) { + switch (set[strlen(reduced) - 1]) { case 'A': linkage_flag = 99; break; case 'B': linkage_flag = 101; diff --git a/backend/common.c b/backend/common.c index 020a07cc..9ba3b326 100644 --- a/backend/common.c +++ b/backend/common.c @@ -29,6 +29,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ #include #include #include @@ -232,6 +233,11 @@ int is_extendable(const int symbology) { return 0; } +/* Indicates which symbols can have composite 2D component data */ +int is_composite(int symbology) { + return symbology >= BARCODE_EANX_CC && symbology <= BARCODE_RSS_EXPSTACK_CC; +} + int istwodigits(const unsigned char source[], const size_t position) { if ((source[position] >= '0') && (source[position] <= '9')) { if ((source[position + 1] >= '0') && (source[position + 1] <= '9')) { diff --git a/backend/common.h b/backend/common.h index 2607d6a3..7e1cfab3 100644 --- a/backend/common.h +++ b/backend/common.h @@ -29,6 +29,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ /* Used in some logic */ #ifndef __COMMON_H @@ -70,6 +71,7 @@ extern "C" { extern void unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord); extern int is_stackable(const int symbology); extern int is_extendable(const int symbology); + extern int is_composite(const int symbology); extern int utf8toutf16(struct zint_symbol *symbol, const unsigned char source[], int vals[], size_t *length); extern void set_minimum_height(struct zint_symbol *symbol, const int min_height); #ifdef __cplusplus diff --git a/backend/composite.c b/backend/composite.c index 2751195d..8552aa45 100644 --- a/backend/composite.c +++ b/backend/composite.c @@ -1883,10 +1883,9 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) { } } } - if ((linear->width + bottom_shift) > symbol->width) { + if ((linear->width + bottom_shift) > symbol->width + top_shift) { symbol->width = linear->width + bottom_shift; - } - if ((symbol->width + top_shift) > symbol->width) { + } else if ((symbol->width + top_shift) > linear->width + bottom_shift) { symbol->width += top_shift; } symbol->rows += linear->rows; diff --git a/backend/gs1.c b/backend/gs1.c index 74e3ed41..bb6969db 100644 --- a/backend/gs1.c +++ b/backend/gs1.c @@ -82,8 +82,12 @@ int gs1_verify(struct zint_symbol *symbol, const unsigned char source[], const s strcpy(symbol->errtxt, "250: Extended ASCII characters are not supported by GS1"); return ZINT_ERROR_INVALID_DATA; } + if (source[i] == '\0') { + strcpy(symbol->errtxt, "262: NUL characters not permitted in GS1 mode"); + return ZINT_ERROR_INVALID_DATA; + } if (source[i] < 32) { - strcpy(symbol->errtxt, "251: Control characters are not supported by GS1 "); + strcpy(symbol->errtxt, "251: Control characters are not supported by GS1"); return ZINT_ERROR_INVALID_DATA; } } @@ -662,26 +666,3 @@ int gs1_verify(struct zint_symbol *symbol, const unsigned char source[], const s /* the character '[' in the reduced string refers to the FNC1 character */ return 0; } - -int ugs1_verify(struct zint_symbol *symbol, const unsigned char source[], const unsigned int src_len, unsigned char reduced[]) { - /* Only to keep the compiler happy */ -#ifndef _MSC_VER - char temp[src_len + 5]; -#else - char* temp = (char*) _alloca(src_len + 5); -#endif - int error_number; - - error_number = gs1_verify(symbol, source, src_len, temp); - if (error_number != 0) { - return error_number; - } - - if (strlen(temp) < src_len + 5) { - ustrcpy(reduced, (unsigned char*) temp); - return 0; - } - strcpy(symbol->errtxt, "261: ugs1_verify overflow"); - return ZINT_ERROR_INVALID_DATA; -} - diff --git a/backend/gs1.h b/backend/gs1.h index 0dba0841..228af50d 100644 --- a/backend/gs1.h +++ b/backend/gs1.h @@ -29,6 +29,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ #ifndef __GS1_H #define __GS1_H @@ -37,10 +38,9 @@ extern "C" { #endif /* __cplusplus */ extern int gs1_verify(struct zint_symbol *symbol, const unsigned char source[], const size_t src_len, char reduced[]); - extern int ugs1_verify(struct zint_symbol *symbol, const unsigned char source[], const unsigned int src_len, unsigned char reduced[]); #ifdef __cplusplus } #endif /* __cplusplus */ -#endif /* __GS1_H */ \ No newline at end of file +#endif /* __GS1_H */ diff --git a/backend/library.c b/backend/library.c index f61c05c4..48a9c62d 100644 --- a/backend/library.c +++ b/backend/library.c @@ -28,6 +28,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ #include #include @@ -385,22 +386,14 @@ static void check_row_heights(struct zint_symbol *symbol) { static int check_force_gs1(const int symbology) { /* Returns 1 if symbology MUST have GS1 data */ - int result = 0; - + int result = is_composite(symbology); + switch (symbology) { case BARCODE_EAN128: + case BARCODE_EAN14: + case BARCODE_NVE18: case BARCODE_RSS_EXP: case BARCODE_RSS_EXPSTACK: - case BARCODE_EANX_CC: - case BARCODE_EAN128_CC: - case BARCODE_RSS14_CC: - case BARCODE_RSS_LTD_CC: - case BARCODE_RSS_EXP_CC: - case BARCODE_UPCA_CC: - case BARCODE_UPCE_CC: - case BARCODE_RSS14STACK_CC: - case BARCODE_RSS14_OMNI_CC: - case BARCODE_RSS_EXPSTACK_CC: result = 1; break; } @@ -1138,13 +1131,6 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int error_number = ZINT_WARN_INVALID_OPTION; } - if (error_number > 4) { - error_tag(symbol->errtxt, error_number); - return error_number; - } else { - error_buffer = error_number; - } - if ((!(supports_eci(symbol->symbology))) && (symbol->eci != 0)) { strcpy(symbol->errtxt, "217: Symbology does not support ECI switching"); error_number = ZINT_ERROR_INVALID_OPTION; @@ -1155,31 +1141,22 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int error_number = ZINT_ERROR_INVALID_OPTION; } - /* Start acting on input mode */ - if ((input_mode == GS1_MODE) || (check_force_gs1(symbol->symbology))) { - for (i = 0; i < in_length; i++) { - if (source[i] == '\0') { - strcpy(symbol->errtxt, "219: NULL characters not permitted in GS1 mode"); - error_tag(symbol->errtxt, ZINT_ERROR_INVALID_DATA); - return ZINT_ERROR_INVALID_DATA; - } - } - if (gs1_compliant(symbol->symbology) == 1) { - error_number = ugs1_verify(symbol, source, in_length, local_source); - if (error_number != 0) { - return error_number; - } - in_length =(int)ustrlen(local_source); - } else { - strcpy(symbol->errtxt, "220: Selected symbology does not support GS1 mode"); - error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); - return ZINT_ERROR_INVALID_OPTION; - } - } else { - memcpy(local_source, source, in_length); - local_source[in_length] = '\0'; + if ((symbol->dot_size < 0.01) || (symbol->dot_size > 20.0)) { + strcpy(symbol->errtxt, "221: Invalid dot size"); + error_number = ZINT_ERROR_INVALID_OPTION; } + if (error_number > 4) { + error_tag(symbol->errtxt, error_number); + return error_number; + } else { + error_buffer = error_number; + } + + memcpy(local_source, source, in_length); + local_source[in_length] = '\0'; + + /* Start acting on input mode */ if (input_mode & ESCAPE_MODE) { error_number = escape_char_process(symbol, local_source, &in_length); if (error_number != 0) { @@ -1189,6 +1166,30 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int input_mode -= ESCAPE_MODE; } + if ((input_mode == GS1_MODE) || (check_force_gs1(symbol->symbology))) { + if (gs1_compliant(symbol->symbology) == 1) { + // Reduce input for composite and non-forced symbologies, others (EAN128 and RSS_EXP based) will handle it themselves + if (is_composite(symbol->symbology) || !check_force_gs1(symbol->symbology)) { +#ifndef _MSC_VER + char reduced[in_length + 1]; +#else + char* reduced = (char*) _alloca(in_length + 1); +#endif + error_number = gs1_verify(symbol, local_source, in_length, reduced); + if (error_number != 0) { + error_tag(symbol->errtxt, error_number); + return error_number; + } + ustrcpy(local_source, reduced); // Cannot contain nul char + in_length = (int) ustrlen(local_source); + } + } else { + strcpy(symbol->errtxt, "220: Selected symbology does not support GS1 mode"); + error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); + return ZINT_ERROR_INVALID_OPTION; + } + } + if ((input_mode < 0) || (input_mode > 2)) { input_mode = DATA_MODE; } @@ -1200,12 +1201,6 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int if (input_mode == UNICODE_MODE) { strip_bom(local_source, &in_length); } - - if ((symbol->dot_size < 0.01) || (symbol->dot_size > 20.0)) { - strcpy(symbol->errtxt, "221: Invalid dot size"); - error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION); - return ZINT_ERROR_INVALID_OPTION; - } switch (symbol->symbology) { case BARCODE_QRCODE: @@ -1238,7 +1233,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int #endif size_t temp_len = in_length; memcpy(temp, local_source, temp_len); - temp[temp_len] = '\0'; + 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; diff --git a/backend/rss.c b/backend/rss.c index 0dae4bf0..c9d8644c 100644 --- a/backend/rss.c +++ b/backend/rss.c @@ -29,6 +29,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ /* The functions "combins" and "getRSSwidths" are copyright BSI and are released with permission under the following terms: @@ -1865,14 +1866,20 @@ int rssexpanded(struct zint_symbol *symbol, unsigned char source[], int src_len) int check_char, c_odd, c_even, elements[235], pattern_width, reader, writer; int separator_row; #ifndef _MSC_VER - char binary_string[(7 * src_len) + 1]; + char reduced[src_len + 1], binary_string[(7 * src_len) + 1]; #else + char* reduced = (char*) _alloca(src_len + 1); char* binary_string = (char*) _alloca((7 * src_len) + 1); #endif separator_row = 0; reader = 0; + i = gs1_verify(symbol, source, src_len, reduced); + if (i != 0) { + return i; + } + if ((symbol->symbology == BARCODE_RSS_EXP_CC) || (symbol->symbology == BARCODE_RSS_EXPSTACK_CC)) { /* make space for a composite separator pattern */ separator_row = symbol->rows; @@ -1888,7 +1895,7 @@ int rssexpanded(struct zint_symbol *symbol, unsigned char source[], int src_len) strcat(binary_string, "0"); } - i = rss_binary_string(symbol, (char *) source, binary_string); + i = rss_binary_string(symbol, reduced, binary_string); if (i != 0) { return i; } diff --git a/backend/tests/CMakeLists.txt b/backend/tests/CMakeLists.txt index 9c4a620b..4fef7e19 100644 --- a/backend/tests/CMakeLists.txt +++ b/backend/tests/CMakeLists.txt @@ -49,7 +49,9 @@ endmacro(zint_add_test) zint_add_test(channel, test_channel) zint_add_test(composite, test_composite) zint_add_test(eci, test_eci) +zint_add_test(gs1, test_gs1) zint_add_test(imail, test_imail) +zint_add_test(library, test_library) zint_add_test(mailmark, test_mailmark) zint_add_test(maxicode, test_maxicode) zint_add_test(postal, test_postal) diff --git a/backend/tests/test_gs1.c b/backend/tests/test_gs1.c new file mode 100644 index 00000000..0a9e1580 --- /dev/null +++ b/backend/tests/test_gs1.c @@ -0,0 +1,277 @@ +/* + libzint - the open source barcode library + Copyright (C) 2008-2019 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + */ +/* vim: set ts=4 sw=4 et : */ + +#include "testcommon.h" + +//#define TEST_GS1_REDUCE_GENERATE_EXPECTED + +/* + * Check that EAN128 and RSS_EXP based symbologies reduce GS1 data + */ +static void test_gs1_reduce(void) +{ + testStart(""); + + int ret; + struct item { + int symbology; + int input_mode; + unsigned char* data; + unsigned char* primary; + int ret; + + char* comment; + unsigned char* expected; + }; + struct item data[] = { + /* 0*/ { BARCODE_EAN128, -1, "12345678901234", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" }, + /* 1*/ { BARCODE_EAN128, -1, "[01]12345678901234", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011" + }, + /* 2*/ { BARCODE_EAN128, GS1_MODE, "[01]12345678901234", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011" + }, + /* 3*/ { BARCODE_EAN128, UNICODE_MODE, "[01]12345678901234", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110010001011000111010111101100011101011" + }, + /* 4*/ { BARCODE_EAN128_CC, -1, "[01]12345678901234", "[21]1234", 0, "Input mode ignored", + "1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000" + "1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000" + "1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000" + "0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100" + "1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011" + }, + /* 5*/ { BARCODE_EAN128_CC, GS1_MODE, "[01]12345678901234", "[21]1234", 0, "Input mode ignored", + "1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000" + "1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000" + "1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000" + "0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100" + "1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011" + }, + /* 6*/ { BARCODE_EAN128_CC, UNICODE_MODE, "[01]12345678901234", "[21]1234", 0, "Input mode ignored", + "1101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010000000000000000000000000000000000000000000000" + "1101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010000000000000000000000000000000000000000000000" + "1101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010000000000000000000000000000000000000000000000" + "0010110001100001010001001100100110100110001101110100111000111010010011110101100100001001010011000110111010011100010100001011010000110011100010100" + "1101001110011110101110110011011001011001110010001011000111000101101100001010011011110110101100111001000101100011101011110100101111001100011101011" + }, + /* 7*/ { BARCODE_EAN14, -1, "1234567890123", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011" + }, + /* 8*/ { BARCODE_EAN14, GS1_MODE, "1234567890123", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011" + }, + /* 9*/ { BARCODE_EAN14, UNICODE_MODE, "1234567890123", "", 0, "Input mode ignored", + "11010011100111101011101100110110010110011100100010110001110001011011000010100110111101101011001110011011000110100001100101100011101011" + }, + /*10*/ { BARCODE_NVE18, -1, "12345678901234567", "", 0, "Input mode ignored", + "110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011" + }, + /*11*/ { BARCODE_NVE18, GS1_MODE, "12345678901234567", "", 0, "Input mode ignored", + "110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011" + }, + /*12*/ { BARCODE_NVE18, UNICODE_MODE, "12345678901234567", "", 0, "Input mode ignored", + "110100111001111010111011011001100101100111001000101100011100010110110000101001101111011010110011100100010110001110001011011000010010101101110001100011101011" + }, + /*13*/ { BARCODE_RSS_EXP, -1, "2012", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" }, + /*14*/ { BARCODE_RSS_EXP, -1, "[20]12", "", 0, "Input mode ignored", + "0101010100000000011011111111000010101011000000010001011111001011100010111100000000101" + }, + /*15*/ { BARCODE_RSS_EXP, GS1_MODE, "[20]12", "", 0, "Input mode ignored", + "0101010100000000011011111111000010101011000000010001011111001011100010111100000000101" + }, + /*16*/ { BARCODE_RSS_EXP, UNICODE_MODE, "[20]12", "", 0, "Input mode ignored", + "0101010100000000011011111111000010101011000000010001011111001011100010111100000000101" + }, + /*17*/ { BARCODE_RSS_EXP_CC, -1, "[20]12", "[21]1234", 0, "Input mode ignored", + "00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101" + "00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101" + "00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101" + "00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000" + "01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000" + }, + /*18*/ { BARCODE_RSS_EXP_CC, GS1_MODE, "[20]12", "[21]1234", 0, "Input mode ignored", + "00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101" + "00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101" + "00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101" + "00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000" + "01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000" + }, + /*19*/ { BARCODE_RSS_EXP_CC, UNICODE_MODE, "[20]12", "[21]1234", 0, "Input mode ignored", + "00110110111011010000100000110100110011101100001001110100110000101000110001001001101100000011011000101" + "00110110110011111010001001100110101110010000001001100100100111100101111001110101100100000011001000101" + "00110110100010111110011000010100111101000000101001100110101110111001111001001111011000011011101000101" + "00001010111111111001000000001010010001100100011101101000001101000111010000111111100000000000000000000" + "01010101000000000110111111110000101110011011100010010111110010111000101111000000001010000000000000000" + }, + /*20*/ { BARCODE_RSS_EXPSTACK, -1, "12", "", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" }, + /*21*/ { BARCODE_RSS_EXPSTACK, -1, "[20]12", "", 0, "Input mode ignored", + "010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101" + }, + /*22*/ { BARCODE_RSS_EXPSTACK, GS1_MODE, "[20]12", "", 0, "Input mode ignored", + "010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101" + }, + /*23*/ { BARCODE_RSS_EXPSTACK, UNICODE_MODE, "[20]12", "", 0, "Input mode ignored", + "010010000010000101101111111100001010000010000110010101111100101110001011110000000010101111100001011101" + }, + /*24*/ { BARCODE_RSS_EXPSTACK_CC, -1, "12", "[21]1234", ZINT_ERROR_INVALID_DATA, "GS1 data required", "" }, + /*25*/ { BARCODE_RSS_EXPSTACK_CC, -1, "[20]12", "[21]1234", 0, "Input mode ignored", + "001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010" + "001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010" + "001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010" + "000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000" + "010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101" + }, + /*26*/ { BARCODE_RSS_EXPSTACK_CC, GS1_MODE, "[20]12", "[21]1234", 0, "Input mode ignored", + "001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010" + "001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010" + "001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010" + "000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000" + "010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101" + }, + /*27*/ { BARCODE_RSS_EXPSTACK_CC, UNICODE_MODE, "[20]12", "[21]1234", 0, "Input mode ignored", + "001101101110110100001000001101001100111011000010011101001100001010001100010010011011000000110110001010" + "001101101100111110100010011001101011100100000010011001001001111001011110011101011001000000110010001010" + "001101101000101111100110000101001111010000001010011001101011101110011110010011110110000110111010001010" + "000001111111010110010000000010100100111001100001011010000011010001100100001010101001010000011110100000" + "010010000000101001101111111100001011000110011110100101111100101110001011110000000010101111100001011101" + }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + char* text; + + for (int i = 0; i < data_size; i++) { + + struct zint_symbol* symbol = ZBarcode_Create(); + assert_nonnull(symbol, "Symbol not created\n"); + + symbol->symbology = data[i].symbology; + if (data[i].input_mode != -1) { + symbol->input_mode = data[i].input_mode; + } + + if (strlen(data[i].primary)) { + text = data[i].primary; + strcpy(symbol->primary, data[i].data); + } else { + text = data[i].data; + } + int length = strlen(text); + + ret = ZBarcode_Encode(symbol, text, length); + + #ifdef TEST_GS1_REDUCE_GENERATE_EXPECTED + if (data[i].ret == 0) { + printf(" /*%2d*/ { %s, %s, \"%s\", \"%s\", %d, \"%s\",\n", + i, testUtilBarcodeName(data[i].symbology), testUtilInputModeName(data[i].input_mode), data[i].data, data[i].primary, data[i].ret, data[i].comment); + testUtilModulesDump(symbol, " ", "\n"); + printf(" },\n"); + } else { + printf(" /*%2d*/ { %s, %s, \"%s\", \"%s\", %s, \"%s\", \"\" },\n", + i, testUtilBarcodeName(data[i].symbology), testUtilInputModeName(data[i].input_mode), data[i].data, data[i].primary, testUtilErrorName(data[i].ret), data[i].comment); + } + #else + assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d %s\n", i, ret, data[i].ret, symbol->errtxt); + + if (ret == 0) { + int width, row; + ret = testUtilModulesCmp(symbol, data[i].expected, &width, &row); + assert_zero(ret, "i:%d %s testUtilModulesCmp ret %d != 0 width %d row %d (%s)\n", i, testUtilBarcodeName(data[i].symbology), ret, width, row, data[i].data); + } + #endif + + ZBarcode_Delete(symbol); + } + + testFinish(); +} + +static void test_hrt(void) +{ + testStart(""); + + int ret; + struct item { + int symbology; + unsigned char* data; + unsigned char* primary; + + unsigned char* expected; + }; + // s/\/\*[ 0-9]*\*\//\=printf("\/*%2d*\/", line(".") - line("'<")) + struct item data[] = { + /* 0*/ { BARCODE_EAN128, "[01]12345678901234[20]12", "", "(01)12345678901234(20)12" }, + /* 1*/ { BARCODE_EAN128_CC, "[01]12345678901234[20]12", "[21]12345", "(01)12345678901234(20)12" }, + /* 2*/ { BARCODE_RSS_EXP, "[01]12345678901234[20]12", "", "(01)12345678901234(20)12" }, + /* 3*/ { BARCODE_RSS_EXP_CC, "[01]12345678901234", "[21]12345", "(01)12345678901234" }, + /* 4*/ { BARCODE_RSS_EXPSTACK, "[20]12", "", "(20)12" }, + /* 5*/ { BARCODE_RSS_EXPSTACK_CC, "[20]12", "[21]12345", "(20)12" }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + char* text; + + for (int i = 0; i < data_size; i++) { + + struct zint_symbol* symbol = ZBarcode_Create(); + assert_nonnull(symbol, "Symbol not created\n"); + + symbol->symbology = data[i].symbology; + + if (strlen(data[i].primary)) { + text = data[i].primary; + strcpy(symbol->primary, data[i].data); + } else { + text = data[i].data; + } + int length = strlen(text); + + ret = ZBarcode_Encode(symbol, text, length); + assert_zero(ret, "i:%d ZBarcode_Encode ret %d != 0 %s\n", i, ret, symbol->errtxt); + + assert_zero(strcmp(symbol->text, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->text, data[i].expected); + + ZBarcode_Delete(symbol); + } + + testFinish(); +} + +int main() +{ + test_gs1_reduce(); + test_hrt(); + + testReport(); + + return 0; +} diff --git a/backend/tests/test_library.c b/backend/tests/test_library.c new file mode 100644 index 00000000..4f8b593f --- /dev/null +++ b/backend/tests/test_library.c @@ -0,0 +1,105 @@ +/* + libzint - the open source barcode library + Copyright (C) 2008-2019 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + */ +/* vim: set ts=4 sw=4 et : */ + +#include "testcommon.h" + +static void test_checks(void) +{ + testStart(""); + + int ret; + struct item { + int symbology; + unsigned char* data; + int length; + int input_mode; + int eci; + float dot_size; + int ret; + + char* expected; + }; + // s/\/\*[ 0-9]*\*\//\=printf("\/*%2d*\/", line(".") - line("'<")) + struct item data[] = { + /* 0*/ { BARCODE_CODE128, "1234", -1, -1, 3, -1, ZINT_ERROR_INVALID_OPTION, "Error 217: Symbology does not support ECI switching" }, + /* 1*/ { BARCODE_CODE128, "1234", -1, -1, 0, -1, 0, "" }, + /* 2*/ { BARCODE_QRCODE, "1234", -1, -1, 3, -1, 0, "" }, + /* 3*/ { BARCODE_QRCODE, "1234", -1, -1, 999999 + 1, -1, ZINT_ERROR_INVALID_OPTION, "Error 218: Invalid ECI mode" }, + /* 4*/ { BARCODE_CODE128, "1234", -1, -1, -1, 20.1, ZINT_ERROR_INVALID_OPTION, "Error 221: Invalid dot size" }, + /* 5*/ { BARCODE_CODE128, "1234", -1, GS1_MODE, -1, -1, ZINT_ERROR_INVALID_OPTION, "Error 220: Selected symbology does not support GS1 mode" }, + /* 6*/ { BARCODE_EAN128, "[21]12\0004", 8, GS1_MODE, -1, -1, ZINT_ERROR_INVALID_DATA, "Error 262: NUL characters not permitted in GS1 mode" }, + /* 7*/ { BARCODE_EAN128, "[21]12é4", -1, GS1_MODE, -1, -1, ZINT_ERROR_INVALID_DATA, "Error 250: Extended ASCII characters are not supported by GS1" }, + /* 8*/ { BARCODE_EAN128, "[21]12\0074", -1, GS1_MODE, -1, -1, ZINT_ERROR_INVALID_DATA, "Error 251: Control characters are not supported by GS1" }, + /* 9*/ { BARCODE_EAN128, "[21]1234", -1, GS1_MODE, -1, -1, 0, "" }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + char* text; + char* primary; + char escaped_primary[1024]; + + for (int i = 0; i < data_size; i++) { + + struct zint_symbol* symbol = ZBarcode_Create(); + assert_nonnull(symbol, "Symbol not created\n"); + + symbol->symbology = data[i].symbology; + if (data[i].input_mode != -1) { + symbol->input_mode = data[i].input_mode; + } + if (data[i].eci != -1) { + symbol->eci = data[i].eci; + } + if (data[i].dot_size != -1) { + symbol->dot_size = data[i].dot_size; + } + int length = data[i].length == -1 ? strlen(data[i].data) : data[i].length; + + ret = ZBarcode_Encode(symbol, data[i].data, length); + assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode(%d) ret %d != %d (%s)\n", i, data[i].symbology, ret, data[i].ret, symbol->errtxt); + + ret = strcmp(symbol->errtxt, data[i].expected); + assert_zero(ret, "i:%d (%d) strcmp(%s, %s) %d != 0\n", i, data[i].symbology, symbol->errtxt, data[i].expected, ret); + + ZBarcode_Delete(symbol); + } + + testFinish(); +} + +int main() +{ + test_checks(); + + testReport(); + + return 0; +} diff --git a/backend/tests/testcommon.c b/backend/tests/testcommon.c index 58da0834..b53bb151 100644 --- a/backend/tests/testcommon.c +++ b/backend/tests/testcommon.c @@ -27,6 +27,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ /* * Adapted from qrencode/tests/common.c * Copyright (C) 2006-2017 Kentaro Fukuchi @@ -252,11 +253,10 @@ char* testUtilBarcodeName(int symbology) { { BARCODE_GRIDMATRIX, "BARCODE_GRIDMATRIX", 142 }, { BARCODE_UPNQR, "BARCODE_UPNQR", 143 }, { BARCODE_ULTRA, "BARCODE_ULTRA", 144 }, - { -1, "", 145 }, }; int data_size = sizeof(data) / sizeof(struct item); - if (symbology < 0 || symbology > data_size) { + if (symbology < 0 || symbology >= data_size) { return ""; } if (data[symbology].val != symbology || (data[symbology].define != -1 && data[symbology].define != symbology)) { // Self-check @@ -266,6 +266,69 @@ char* testUtilBarcodeName(int symbology) { return data[symbology].name; } +char* testUtilErrorName(int error_number) { + struct item { + int define; + char* name; + int val; + }; + struct item data[] = { + { -1, "", 0 }, + { -1, "", 1 }, + { ZINT_WARN_INVALID_OPTION, "ZINT_WARN_INVALID_OPTION", 2 }, + { ZINT_WARN_USES_ECI, "ZINT_WARN_USES_ECI", 3 }, + { -1, "", 4 }, + { ZINT_ERROR_TOO_LONG, "ZINT_ERROR_TOO_LONG", 5 }, + { ZINT_ERROR_INVALID_DATA, "ZINT_ERROR_INVALID_DATA", 6 }, + { ZINT_ERROR_INVALID_CHECK, "ZINT_ERROR_INVALID_CHECK", 7 }, + { ZINT_ERROR_INVALID_OPTION, "ZINT_ERROR_INVALID_OPTION", 8 }, + { ZINT_ERROR_ENCODING_PROBLEM, "ZINT_ERROR_ENCODING_PROBLEM", 9 }, + { ZINT_ERROR_FILE_ACCESS, "ZINT_ERROR_FILE_ACCESS", 10 }, + { ZINT_ERROR_MEMORY, "ZINT_ERROR_MEMORY", 11 }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + if (error_number < 0 || error_number >= data_size) { + return ""; + } + if (data[error_number].val != error_number || (data[error_number].define != -1 && data[error_number].define != error_number)) { // Self-check + fprintf(stderr, "testUtilErrorName data table out of sync (%d)\n", error_number); + abort(); + } + return data[error_number].name; +} + +char* testUtilInputModeName(int input_mode) { + struct item { + int define; + char* name; + int val; + }; + struct item data[] = { + { DATA_MODE, "DATA_MODE", 0 }, + { UNICODE_MODE, "UNICODE_MODE", 1 }, + { GS1_MODE, "GS1_MODE", 2 }, + { -1, "", 3 }, + { -1, "", 4 }, + { -1, "", 5 }, + { -1, "", 6 }, + { -1, "", 7 }, + { DATA_MODE | ESCAPE_MODE, "DATA_MODE | ESCAPE_MODE", 8 }, + { UNICODE_MODE | ESCAPE_MODE, "UNICODE_MODE | ESCAPE_MODE", 9 }, + { GS1_MODE | ESCAPE_MODE, "GS1_MODE | ESCAPE_MODE", 10 }, + }; + int data_size = sizeof(data) / sizeof(struct item); + + if (input_mode < 0 || input_mode >= data_size) { + return input_mode == -1 ? "-1" : ""; + } + if (data[input_mode].val != input_mode || (data[input_mode].define != -1 && data[input_mode].define != input_mode)) { // Self-check + fprintf(stderr, "testUtilInputModeName data table out of sync (%d)\n", input_mode); + abort(); + } + return data[input_mode].name; +} + int testUtilDAFTConvert(const struct zint_symbol* symbol, char* buffer, int buffer_size) { buffer[0] = '\0'; diff --git a/backend/tests/testcommon.h b/backend/tests/testcommon.h index 90a9b644..cd0f19c5 100644 --- a/backend/tests/testcommon.h +++ b/backend/tests/testcommon.h @@ -27,6 +27,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/* vim: set ts=4 sw=4 et : */ /* * Adapted from qrencode/tests/common.h * Copyright (C) 2006-2017 Kentaro Fukuchi @@ -66,6 +67,8 @@ void testReport(); extern void vector_free(struct zint_symbol *symbol); /* Free vector structures */ char* testUtilBarcodeName(int symbology); +char* testUtilErrorName(int error_number); +char* testUtilInputModeName(int input_mode); int testUtilDAFTConvert(const struct zint_symbol* symbol, char* buffer, int buffer_size); char* testUtilEscape(char* buffer, char* escaped, int escaped_size); char* testUtilReadCSVField(char* buffer, char* field, int field_size);