Allow selection of GS1 mode on GS1 symbols

Because some symbols require GS1 data, selecting GS1 mode for them would result in GS1 parsing being done twice, corrupting the data.
Fixes #165 reported by Gregory Van Vooren
This commit is contained in:
Robin Stuart 2019-10-06 11:30:21 +01:00
parent eb3a004ace
commit ff2ceea361
4 changed files with 45 additions and 64 deletions

View File

@ -2,7 +2,7 @@
/*
libzint - the open source barcode library
Copyright (C) 2008-2017 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2008-2019 Robin Stuart <rstuart114@gmail.com>
Bugfixes thanks to Christian Sakowski and BogDan Vatra
Redistribution and use in source and binary forms, with or without
@ -689,11 +689,7 @@ 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;
@ -726,20 +722,12 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len
symbol->rows += 1;
}
if (symbol->input_mode != GS1_MODE) {
/* GS1 data has not been checked yet */
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(reduced[indexchaine]);
if (reduced[indexchaine] == '[') {
mode = parunmodd(source[indexchaine]);
if (source[indexchaine] == '[') {
mode = ABORC;
}
@ -749,16 +737,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) strlen(reduced))) {
while ((list[1][indexliste] == mode) && (indexchaine < (int) ustrlen(source))) {
list[0][indexliste]++;
indexchaine++;
mode = parunmodd(reduced[indexchaine]);
if (reduced[indexchaine] == '[') {
mode = parunmodd(source[indexchaine]);
if (source[indexchaine] == '[') {
mode = ABORC;
}
}
indexliste++;
} while (indexchaine < (int) strlen(reduced));
} while (indexchaine < (int) ustrlen(source));
dxsmooth(&indexliste);
@ -786,7 +774,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 (reduced[i] == '[') {
if (source[i] == '[') {
if (c_count & 1) {
if ((i - c_count) != 0) {
set[i - c_count] = 'B';
@ -826,7 +814,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) strlen(reduced); i++) {
for (i = 0; i < (int) ustrlen(source); i++) {
if ((set[i] == 'a') || (set[i] == 'b')) {
glyph_count = glyph_count + 1.0;
}
@ -837,7 +825,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len
}
}
if ((set[i] == 'C') && (reduced[i] != '[')) {
if ((set[i] == 'C') && (source[i] != '[')) {
glyph_count = glyph_count + 0.5;
} else {
glyph_count = glyph_count + 1.0;
@ -897,20 +885,20 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len
bar_characters++;
}
if (reduced[read] != '[') {
if (source[read] != '[') {
switch (set[read]) { /* Encode data characters */
case 'A':
case 'a':
c128_set_a(reduced[read], dest, values, &bar_characters);
c128_set_a(source[read], dest, values, &bar_characters);
read++;
break;
case 'B':
case 'b':
c128_set_b(reduced[read], dest, values, &bar_characters);
c128_set_b(source[read], dest, values, &bar_characters);
read++;
break;
case 'C':
c128_set_c(reduced[read], reduced[read + 1], dest, values, &bar_characters);
c128_set_c(source[read], source[read + 1], dest, values, &bar_characters);
read += 2;
break;
}
@ -920,7 +908,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len
bar_characters++;
read++;
}
} while (read < (int) strlen(reduced));
} while (read < (int) ustrlen(source));
/* "...note that the linkage flag is an extra code set character between
the last data character and the Symbol Check Character" (GS1 Specification) */
@ -931,7 +919,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[strlen(reduced) - 1]) {
switch (set[ustrlen(source) - 1]) {
case 'A': linkage_flag = 100;
break;
case 'B': linkage_flag = 99;
@ -942,7 +930,7 @@ int ean_128(struct zint_symbol *symbol, unsigned char source[], const size_t len
break;
case 3:
/* CC-C 2D component */
switch (set[strlen(reduced) - 1]) {
switch (set[ustrlen(source) - 1]) {
case 'A': linkage_flag = 99;
break;
case 'B': linkage_flag = 101;

View File

@ -2,7 +2,7 @@
/*
libzint - the open source barcode library
Copyright (C) 2008-2017 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2008-2019 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -1618,10 +1618,8 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) {
unsigned int bs = 20 * rs;
unsigned int pri_len;
#ifndef _MSC_VER
char reduced[rs];
char binary_string[bs];
#else
char* reduced = (char*) _alloca(rs);
char* binary_string = (char*) _alloca(bs);
#endif
struct zint_symbol *linear;
@ -1648,11 +1646,6 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) {
return ZINT_ERROR_INVALID_OPTION;
}
error_number = gs1_verify(symbol, source, length, reduced);
if (error_number != 0) {
return error_number;
}
if (symbol->symbology == BARCODE_EAN128_CC) {
/* Do a test run of encoding the linear component to establish its width */
linear_width = linear_dummy_run((unsigned char *) symbol->primary, pri_len);
@ -1705,7 +1698,7 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) {
}
if (cc_mode == 1) {
i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
i = cc_binary_string(symbol, (char *) source, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
if (i == ZINT_ERROR_TOO_LONG) {
cc_mode = 2;
}
@ -1713,7 +1706,7 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) {
if (cc_mode == 2) {
/* If the data didn't fit into CC-A it is recalculated for CC-B */
i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
i = cc_binary_string(symbol, (char *) source, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
if (i == ZINT_ERROR_TOO_LONG) {
if (symbol->symbology != BARCODE_EAN128_CC) {
return ZINT_ERROR_TOO_LONG;
@ -1725,7 +1718,7 @@ int composite(struct zint_symbol *symbol, unsigned char source[], int length) {
if (cc_mode == 3) {
/* If the data didn't fit in CC-B (and linear part is GS1-128) it is recalculated for CC-C */
i = cc_binary_string(symbol, reduced, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
i = cc_binary_string(symbol, (char *) source, binary_string, cc_mode, &cc_width, &ecc_level, linear_width);
if (i == ZINT_ERROR_TOO_LONG) {
return ZINT_ERROR_TOO_LONG;
}

View File

@ -1,7 +1,7 @@
/* library.c - external functions of libzint
libzint - the open source barcode library
Copyright (C) 2009-2018 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2009-2019 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -184,11 +184,7 @@ extern int ultracode(struct zint_symbol *symbol, const unsigned char source[], c
extern int plot_raster(struct zint_symbol *symbol, int rotate_angle, int file_type); /* Plot to PNG/BMP/PCX */
extern int plot_vector(struct zint_symbol *symbol, int rotate_angle, int file_type); /* Plot to EPS/EMF/SVG */
extern int render_plot(struct zint_symbol *symbol, float width, float height); /* Plot to gLabels */
//extern int ps_plot(struct zint_symbol *symbol); /* Plot to EPS */
//extern int svg_plot(struct zint_symbol *symbol); /* Plot to SVG */
//extern int emf_plot(struct zint_symbol *symbol); /* Plot to Metafile */
extern int render_plot(struct zint_symbol *symbol, float width, float height); /* Plot to gLabels - depreciated */
void error_tag(char error_string[], int error_number) {
@ -386,8 +382,8 @@ static void check_row_heights(struct zint_symbol *symbol) {
}
}
static int gs1_compliant(const int symbology) {
/* Returns 1 if symbology supports GS1 data */
static int check_force_gs1(const int symbology) {
/* Returns 1 if symbology MUST have GS1 data */
int result = 0;
@ -405,6 +401,19 @@ static int gs1_compliant(const int symbology) {
case BARCODE_RSS14STACK_CC:
case BARCODE_RSS14_OMNI_CC:
case BARCODE_RSS_EXPSTACK_CC:
result = 1;
break;
}
return result;
}
static int gs1_compliant(const int symbology) {
/* Returns 1 if symbology supports GS1 data */
int result = check_force_gs1(symbology);
switch (symbology) {
case BARCODE_CODE16K:
case BARCODE_AZTEC:
case BARCODE_DATAMATRIX:
@ -1146,7 +1155,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int
}
/* Start acting on input mode */
if (input_mode == GS1_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");

View File

@ -2,7 +2,7 @@
/*
libzint - the open source barcode library
Copyright (C) 2008-2017 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2008-2019 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -1865,23 +1865,14 @@ 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 reduced[src_len + 1], binary_string[(7 * src_len) + 1];
char 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;
if (symbol->input_mode != GS1_MODE) {
/* GS1 data has not been verified yet */
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;
@ -1897,7 +1888,7 @@ int rssexpanded(struct zint_symbol *symbol, unsigned char source[], int src_len)
strcat(binary_string, "0");
}
i = rss_binary_string(symbol, reduced, binary_string);
i = rss_binary_string(symbol, (char *) source, binary_string);
if (i != 0) {
return i;
}