DOTCODE: score_array Rev 4; initial HT/FS/GS/RS, macro fixes; CR/LF; #191 clang-tidy warnings; gs1 DEL

This commit is contained in:
gitlost 2020-05-02 00:38:35 +01:00
parent bdd8a7923f
commit 3bda3b6213
4 changed files with 1029 additions and 667 deletions

View File

@ -32,7 +32,7 @@
/* vim: set ts=4 sw=4 et : */ /* vim: set ts=4 sw=4 et : */
/* /*
* Attempts to encode DotCode according to AIMD013 Rev 1.34a, dated Feb 19, 2009 * Attempts to encode DotCode according to (AIMD013) ISS DotCode Rev. 4.0, DRAFT 0.15, TSC Pre-PR #5, dated May 28, 2019
* Incorporating suggestions from Terry Burton at BWIPP * Incorporating suggestions from Terry Burton at BWIPP
*/ */
@ -68,6 +68,7 @@ static const unsigned short int dot_patterns[113] = {
0x1b8, 0x1c6, 0x1cc 0x1b8, 0x1c6, 0x1cc
}; };
// Printed() routine from Annex A adapted to char array of ASCII 1's and 0's
static int get_dot(char Dots[], const int Hgt, const int Wid, const int x, const int y) { static int get_dot(char Dots[], const int Hgt, const int Wid, const int x, const int y) {
int retval = 0; int retval = 0;
@ -102,61 +103,67 @@ static int clr_row(char *Dots, const int Hgt, const int Wid, const int y) {
return 1; return 1;
} }
/* Dot pattern scoring routine from Annex A */ // calc penalty for empty interior columns
static int score_array(char Dots[], int Hgt, int Wid) { static int col_penalty(char *Dots, int Hgt, int Wid) {
int x, y, worstedge, first, last, sum; int x, penalty = 0, penalty_local = 0;
int penalty_local = 0;
int penalty = 0;
// first, guard against "pathelogical" gaps in the array
if (Hgt & 1) {
if (Hgt < 12) {
sum = 0;
for (x = 1; x < Wid - 1; x++) { for (x = 1; x < Wid - 1; x++) {
if (!(clr_col(Dots, Hgt, Wid, x))) { if (clr_col(Dots, Hgt, Wid, x)) {
sum = 0; if (penalty_local == 0) {
if (penalty_local) {
penalty += penalty_local;
penalty_local = 0;
}
} else {
sum++;
if (sum == 1) {
penalty_local = Hgt; penalty_local = Hgt;
} else { } else {
penalty_local *= Hgt; penalty_local *= Hgt;
} }
}
}
}
} else { } else {
if (Wid < 12) {
sum = 0;
for (y = 1; y < Hgt - 1; y++) {
if (!(clr_row(Dots, Hgt, Wid, y))) {
sum = 0;
if (penalty_local) { if (penalty_local) {
penalty += penalty_local; penalty += penalty_local;
penalty_local = 0; penalty_local = 0;
} }
} else { }
sum++; }
if (sum == 1) {
return penalty + penalty_local;
}
// calc penalty for empty interior rows
static int row_penalty(char *Dots, int Hgt, int Wid) {
int y, penalty = 0, penalty_local = 0;
for (y = 1; y < Hgt - 1; y++) {
if (clr_row(Dots, Hgt, Wid, y)) {
if (penalty_local == 0) {
penalty_local = Wid; penalty_local = Wid;
} else { } else {
penalty_local *= Wid; penalty_local *= Wid;
} }
} else {
if (penalty_local) {
penalty += penalty_local;
penalty_local = 0;
} }
} }
} }
return penalty + penalty_local;
} }
/* Dot pattern scoring routine from Annex A */
static int score_array(char Dots[], int Hgt, int Wid) {
int x, y, worstedge, first, last, sum;
int penalty = 0;
// first, guard against "pathelogical" gaps in the array
// subtract a penalty score for empty rows/columns from total code score for each mask,
// where the penalty is Sum(N ^ n), where N is the number of positions in a column/row,
// and n is the number of consecutive empty rows/columns
penalty = row_penalty(Dots, Hgt, Wid) + col_penalty(Dots, Hgt, Wid);
sum = 0; sum = 0;
first = -1; first = -1;
last = -1; last = -1;
// across the top edge, count printed dots and measure their extent // across the top edge, count printed dots and measure their extent
for (x = 0; x < Wid; x += 2) for (x = 0; x < Wid; x += 2) {
if (get_dot(Dots, Hgt, Wid, x, 0)) { if (get_dot(Dots, Hgt, Wid, x, 0)) {
if (first < 0) { if (first < 0) {
first = x; first = x;
@ -164,19 +171,20 @@ static int score_array(char Dots[], int Hgt, int Wid) {
last = x; last = x;
sum++; sum++;
} }
worstedge = sum + last - first; }
worstedge *= Hgt;
if (sum == 0) { if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty top edge return SCORE_UNLIT_EDGE; // guard against empty top edge
} }
worstedge = sum + last - first;
worstedge *= Hgt;
sum = 0; sum = 0;
first = -1; first = -1;
last = -1; last = -1;
// across the bottom edge, ditto // across the bottom edge, ditto
for (x = Wid & 1; x < Wid; x += 2) for (x = Wid & 1; x < Wid; x += 2) {
if (get_dot(Dots, Hgt, Wid, x, Hgt - 1)) { if (get_dot(Dots, Hgt, Wid, x, Hgt - 1)) {
if (first < 0) { if (first < 0) {
first = x; first = x;
@ -184,22 +192,23 @@ static int score_array(char Dots[], int Hgt, int Wid) {
last = x; last = x;
sum++; sum++;
} }
}
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty bottom edge
}
sum += last - first; sum += last - first;
sum *= Hgt; sum *= Hgt;
if (sum < worstedge) { if (sum < worstedge) {
worstedge = sum; worstedge = sum;
} }
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty bottom edge
}
sum = 0; sum = 0;
first = -1; first = -1;
last = -1; last = -1;
// down the left edge, ditto // down the left edge, ditto
for (y = 0; y < Hgt; y += 2) for (y = 0; y < Hgt; y += 2) {
if (get_dot(Dots, Hgt, Wid, 0, y)) { if (get_dot(Dots, Hgt, Wid, 0, y)) {
if (first < 0) { if (first < 0) {
first = y; first = y;
@ -207,22 +216,23 @@ static int score_array(char Dots[], int Hgt, int Wid) {
last = y; last = y;
sum++; sum++;
} }
}
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty left edge
}
sum += last - first; sum += last - first;
sum *= Wid; sum *= Wid;
if (sum < worstedge) { if (sum < worstedge) {
worstedge = sum; worstedge = sum;
} }
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty left edge
}
sum = 0; sum = 0;
first = -1; first = -1;
last = -1; last = -1;
// down the right edge, ditto // down the right edge, ditto
for (y = Hgt & 1; y < Hgt; y += 2) for (y = Hgt & 1; y < Hgt; y += 2) {
if (get_dot(Dots, Hgt, Wid, Wid - 1, y)) { if (get_dot(Dots, Hgt, Wid, Wid - 1, y)) {
if (first < 0) { if (first < 0) {
first = y; first = y;
@ -230,16 +240,17 @@ static int score_array(char Dots[], int Hgt, int Wid) {
last = y; last = y;
sum++; sum++;
} }
}
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty right edge
}
sum += last - first; sum += last - first;
sum *= Wid; sum *= Wid;
if (sum < worstedge) { if (sum < worstedge) {
worstedge = sum; worstedge = sum;
} }
if (sum == 0) {
return SCORE_UNLIT_EDGE; // guard against empty right edge
}
// throughout the array, count the # of unprinted 5-somes (cross patterns) // throughout the array, count the # of unprinted 5-somes (cross patterns)
// plus the # of printed dots surrounded by 8 unprinted neighbors // plus the # of printed dots surrounded by 8 unprinted neighbors
sum = 0; sum = 0;
@ -268,12 +279,22 @@ static int score_array(char Dots[], int Hgt, int Wid) {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
static void rsencode(int nd, int nc, unsigned char *wd) { static void rsencode(int nd, int nc, unsigned char *wd) {
int i, j, k, nw, start, step, root[GF], c[GF]; // roots (antilogs): root[0] = 1; for (i = 1; i < GF - 1; i++) root[i] = (PM * root[i - 1]) % GF;
static int root[GF - 1] = {
// Start by generating "nc" roots (antilogs): 1, 3, 9, 27, 81, 17, 51, 40, 7, 21,
root[0] = 1; 63, 76, 2, 6, 18, 54, 49, 34, 102, 80,
for (i = 1; (i <= nc) && (i < GF); i++) 14, 42, 13, 39, 4, 12, 36, 108, 98, 68,
root[i] = (PM * root[i - 1]) % GF; 91, 47, 28, 84, 26, 78, 8, 24, 72, 103,
83, 23, 69, 94, 56, 55, 52, 43, 16, 48,
31, 93, 53, 46, 25, 75, 112, 110, 104, 86,
32, 96, 62, 73, 106, 92, 50, 37, 111, 107,
95, 59, 64, 79, 11, 33, 99, 71, 100, 74,
109, 101, 77, 5, 15, 45, 22, 66, 85, 29,
87, 35, 105, 89, 41, 10, 30, 90, 44, 19,
57, 58, 61, 70, 97, 65, 82, 20, 60, 67,
88, 38
};
int i, j, k, nw, start, step, c[GF];
// Here we compute how many interleaved R-S blocks will be needed // Here we compute how many interleaved R-S blocks will be needed
nw = nd + nc; nw = nd + nc;
@ -281,13 +302,14 @@ static void rsencode(int nd, int nc, unsigned char *wd) {
// ...& then for each such block: // ...& then for each such block:
for (start = 0; start < step; start++) { for (start = 0; start < step; start++) {
int ND = (nd - start + step - 1) / step, NW = (nw - start + step - 1) / step, NC = NW - ND; int ND = (nd - start + step - 1) / step;
int NW = (nw - start + step - 1) / step;
int NC = NW - ND;
// first compute the generator polynomial "c" of order "NC": // first compute the generator polynomial "c" of order "NC":
for (i = 1; i <= NC; i++) memset(c, 0, GF * sizeof(int)); // Keep clang-tidy happy (as far as UndefinedBinaryOperatorResult warning below at least)
c[i] = 0;
c[0] = 1;
c[0] = 1;
for (i = 1; i <= NC; i++) { for (i = 1; i <= NC; i++) {
for (j = NC; j >= 1; j--) { for (j = NC; j >= 1; j--) {
c[j] = (GF + c[j] - (root[i] * c[j - 1]) % GF) % GF; c[j] = (GF + c[j] - (root[i] * c[j - 1]) % GF) % GF;
@ -296,19 +318,21 @@ static void rsencode(int nd, int nc, unsigned char *wd) {
// & then compute the corresponding checkword values into wd[] // & then compute the corresponding checkword values into wd[]
// ... (a) starting at wd[start] & (b) stepping by step // ... (a) starting at wd[start] & (b) stepping by step
for (i = ND; i < NW; i++) for (i = ND; i < NW; i++) {
wd[start + i * step] = 0; wd[start + i * step] = 0;
}
for (i = 0; i < ND; i++) { for (i = 0; i < ND; i++) {
k = (wd[start + i * step] + wd[start + ND * step]) % GF; k = (wd[start + i * step] + wd[start + ND * step]) % GF; // NOLINT wd set 0..(nd - 1) and start + i * step <= nd - 1
for (j = 0; j < NC - 1; j++) { for (j = 0; j < NC - 1; j++) {
wd[start + (ND + j) * step] = (GF - ((c[j + 1] * k) % GF) + wd[start + (ND + j + 1) * step]) % GF; wd[start + (ND + j) * step] = (GF - ((c[j + 1] * k) % GF) + wd[start + (ND + j + 1) * step]) % GF;
} }
wd[start + (ND + NC - 1) * step] = (GF - ((c[NC] * k) % GF)) % GF; wd[start + (ND + NC - 1) * step] = (GF - ((c[NC] * k) % GF)) % GF;
} }
for (i = ND; i < NW; i++) for (i = ND; i < NW; i++) {
wd[start + i * step] = (GF - wd[start + i * step]) % GF; wd[start + i * step] = (GF - wd[start + i * step]) % GF;
} }
} }
}
/* Check if the next character is directly encodable in code set A (Annex F.II.D) */ /* Check if the next character is directly encodable in code set A (Annex F.II.D) */
static int datum_a(const unsigned char source[], int position, int length) { static int datum_a(const unsigned char source[], int position, int length) {
@ -323,7 +347,7 @@ static int datum_a(const unsigned char source[], int position, int length) {
return retval; return retval;
} }
/* Check if the next character is directly encodable in code set B (Annex F.II.D) */ /* Check if the next character is directly encodable in code set B (Annex F.II.D). Note changed to return 2 if CR/LF */
static int datum_b(const unsigned char source[], int position, int length) { static int datum_b(const unsigned char source[], int position, int length) {
int retval = 0; int retval = 0;
@ -340,9 +364,9 @@ static int datum_b(const unsigned char source[], int position, int length) {
retval = 1; retval = 1;
} }
if (position != length - 2) { if (position + 1 < length) {
if ((source[position] == 13) && (source[position + 1] == 10)) { // CRLF if ((source[position] == 13) && (source[position + 1] == 10)) { // CRLF
retval = 1; retval = 2;
} }
} }
} }
@ -426,17 +450,21 @@ static int ahead_a(const unsigned char source[], int position, int length) {
return count; return count;
} }
/* Annex F.II.H */ /* Annex F.II.H Note: changed to return number of chars encodable. Number of codewords returned in *p_nx. */
static int ahead_b(const unsigned char source[], int position, int length) { static int ahead_b(const unsigned char source[], int position, int length, int *p_nx) {
int count = 0; int count = 0;
int i; int i, incr;
for (i = position; ((i < length) && datum_b(source, i, length)) for (i = position; (i < length) && (incr = datum_b(source, i, length))
&& (try_c(source, i, length) < 2); i++) { && (try_c(source, i, length) < 2); i += incr) {
count++; count++;
} }
return count; if (p_nx != NULL) {
*p_nx = count;
}
return i - position;
} }
/* checks if the next character is in the range 128 to 255 (Annex F.II.I) */ /* checks if the next character is in the range 128 to 255 (Annex F.II.I) */
@ -452,12 +480,15 @@ static int binary(const unsigned char source[], int length, int position) {
/* Analyse input data stream and encode using algorithm from Annex F */ /* Analyse input data stream and encode using algorithm from Annex F */
static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned char source[], int length, unsigned char *codeword_array, int *binary_finish) { static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned char source[], int length, unsigned char *codeword_array, int *binary_finish) {
static char lead_specials[] = "\x09\x1C\x1D\x1E"; // HT, FS, GS, RS
int input_position, array_length, i; int input_position, array_length, i;
char encoding_mode; char encoding_mode;
int inside_macro; int inside_macro;
int debug = (symbol->debug & ZINT_DEBUG_PRINT); int debug = (symbol->debug & ZINT_DEBUG_PRINT);
int binary_buffer_size = 0; int binary_buffer_size = 0;
int lawrencium[6]; // Reversed radix 103 values int lawrencium[6]; // Reversed radix 103 values
int nx;
#if defined(_MSC_VER) && _MSC_VER == 1200 #if defined(_MSC_VER) && _MSC_VER == 1200
uint64_t binary_buffer = 0; uint64_t binary_buffer = 0;
@ -509,39 +540,16 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
} }
// Prevent encodation as a macro if a special character is in first position // Prevent encodation as a macro if a special character is in first position
if (source[input_position] == 9) { if (strchr(lead_specials, source[input_position]) != NULL) {
codeword_array[array_length] = 101; // Latch A codeword_array[array_length] = 101; // Latch A
array_length++; array_length++;
codeword_array[array_length] = 73; // HT codeword_array[array_length] = source[input_position] + 64;
array_length++; array_length++;
encoding_mode = 'A'; encoding_mode = 'A';
input_position++;
} }
if (source[input_position] == 28) { while (input_position < length) {
codeword_array[array_length] = 101; // Latch A
array_length++;
codeword_array[array_length] = 92; // FS
array_length++;
encoding_mode = 'A';
}
if (source[input_position] == 29) {
codeword_array[array_length] = 101; // Latch A
array_length++;
codeword_array[array_length] = 93; // GS
array_length++;
encoding_mode = 'A';
}
if (source[input_position] == 30) {
codeword_array[array_length] = 101; // Latch A
array_length++;
codeword_array[array_length] = 94; // RS
array_length++;
encoding_mode = 'A';
}
do {
int done = 0; int done = 0;
/* Step A */ /* Step A */
if ((input_position == length - 2) && (inside_macro != 0) && (inside_macro != 100)) { if ((input_position == length - 2) && (inside_macro != 0) && (inside_macro != 100)) {
@ -565,18 +573,19 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
/* Step C1 */ /* Step C1 */
if ((!done) && (encoding_mode == 'C')) { if ((!done) && (encoding_mode == 'C')) {
if ((array_length == 0) && (length > 9)) { if ((array_length == 0) && (length > 6)) {
if ((source[input_position] == '[') if ((source[input_position] == '[')
&& (source[input_position + 1] == ')') && (source[input_position + 1] == ')')
&& (source[input_position + 2] == '>') && (source[input_position + 2] == '>')
&& (source[input_position + 3] == 30) // RS && (source[input_position + 3] == 30) // RS
&& (source[length - 1] == 04)) { // EOT && (source[length - 1] == 4)) { // EOT
if ((source[input_position + 6] == 29) && (source[length - 2] == 30)) { // GS/RS if ((source[input_position + 6] == 29) && (source[length - 2] == 30)) { // GS/RS
if ((source[input_position + 4] == '0') && (source[input_position + 5] == '5')) { if ((source[input_position + 4] == '0') && (source[input_position + 5] == '5')) {
codeword_array[array_length] = 102; // Shift B codeword_array[array_length] = 106; // Latch B
array_length++; array_length++;
encoding_mode = 'B';
codeword_array[array_length] = 97; // Macro codeword_array[array_length] = 97; // Macro
array_length++; array_length++;
input_position += 7; input_position += 7;
@ -587,9 +596,10 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
} }
} }
if ((source[input_position + 4] == '0') && (source[input_position + 5] == '6')) { if ((!done) && (source[input_position + 4] == '0') && (source[input_position + 5] == '6')) {
codeword_array[array_length] = 102; // Shift B codeword_array[array_length] = 106; // Latch B
array_length++; array_length++;
encoding_mode = 'B';
codeword_array[array_length] = 98; // Macro codeword_array[array_length] = 98; // Macro
array_length++; array_length++;
input_position += 7; input_position += 7;
@ -600,9 +610,10 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
} }
} }
if ((source[input_position + 4] == '1') && (source[input_position + 5] == '2')) { if ((!done) && (source[input_position + 4] == '1') && (source[input_position + 5] == '2')) {
codeword_array[array_length] = 102; // Shift B codeword_array[array_length] = 106; // Latch B
array_length++; array_length++;
encoding_mode = 'B';
codeword_array[array_length] = 99; // Macro codeword_array[array_length] = 99; // Macro
array_length++; array_length++;
input_position += 7; input_position += 7;
@ -614,10 +625,11 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
} }
} }
if ((!done) && (source[input_position] >= '0') && (source[input_position] <= '9') && if ((!done) && (source[input_position + 4] >= '0') && (source[input_position + 4] <= '9') &&
(source[input_position + 1] >= '0') && (source[input_position + 1] <= '9')) { (source[input_position + 5] >= '0') && (source[input_position + 5] <= '9')) {
codeword_array[array_length] = 102; // Shift B codeword_array[array_length] = 106; // Latch B
array_length++; array_length++;
encoding_mode = 'B';
codeword_array[array_length] = 100; // Macro codeword_array[array_length] = 100; // Macro
array_length++; array_length++;
input_position += 4; input_position += 4;
@ -672,12 +684,12 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
if (binary(source, length, input_position)) { if (binary(source, length, input_position)) {
if (n_digits(source, input_position + 1, length) > 0) { if (n_digits(source, input_position + 1, length) > 0) {
if ((source[input_position] - 128) < 32) { if ((source[input_position] - 128) < 32) {
codeword_array[array_length] = 110; // Bin Shift A codeword_array[array_length] = 110; // Upper Shift A
array_length++; array_length++;
codeword_array[array_length] = source[input_position] - 128 + 64; codeword_array[array_length] = source[input_position] - 128 + 64;
array_length++; array_length++;
} else { } else {
codeword_array[array_length] = 111; // Bin Shift B codeword_array[array_length] = 111; // Upper Shift B
array_length++; array_length++;
codeword_array[array_length] = source[input_position] - 128 - 32; codeword_array[array_length] = source[input_position] - 128 - 32;
array_length++; array_length++;
@ -698,19 +710,22 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
/* Step C4 */ /* Step C4 */
if ((!done) && (encoding_mode == 'C')) { if ((!done) && (encoding_mode == 'C')) {
int m = ahead_a(source, input_position, length); int m = ahead_a(source, input_position, length);
int n = ahead_b(source, input_position, length); int n = ahead_b(source, input_position, length, &nx);
if (m > n) { if (m > n) {
codeword_array[array_length] = 101; // Latch A codeword_array[array_length] = 101; // Latch A
array_length++; array_length++;
encoding_mode = 'A'; encoding_mode = 'A';
} else { } else {
if (n <= 4) { if (nx >= 1 && nx <= 4) {
codeword_array[array_length] = 101 + n; // nx Shift B codeword_array[array_length] = 101 + nx; // nx Shift B
array_length++; array_length++;
for (i = 0; i < n; i++) { for (i = 0; i < nx; i++) {
if (source[input_position] >= 32) { if (source[input_position] >= 32) {
codeword_array[array_length] = source[input_position] - 32; codeword_array[array_length] = source[input_position] - 32;
} else if (source[input_position] == 13) { // CR/LF
codeword_array[array_length] = 96;
input_position++;
} else { } else {
switch(source[input_position]) { switch(source[input_position]) {
case 9: codeword_array[array_length] = 97; break; // HT case 9: codeword_array[array_length] = 97; break; // HT
@ -890,6 +905,7 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
/* Step E2 */ /* Step E2 */
if ((!done) && (encoding_mode == 'A')) { if ((!done) && (encoding_mode == 'A')) {
if ((source[input_position] == '[') && ((symbol->input_mode & 0x07) == GS1_MODE)) { if ((source[input_position] == '[') && ((symbol->input_mode & 0x07) == GS1_MODE)) {
// Note: this branch probably never reached as no reason to be in Code Set A for GS1 data
codeword_array[array_length] = 107; // FNC1 codeword_array[array_length] = 107; // FNC1
array_length++; array_length++;
input_position++; input_position++;
@ -944,14 +960,17 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
/* Step E4 */ /* Step E4 */
if ((!done) && (encoding_mode == 'A')) { if ((!done) && (encoding_mode == 'A')) {
int n = ahead_b(source, input_position, length); ahead_b(source, input_position, length, &nx);
if (n <= 6) { if (nx >= 1 && nx <= 6) {
codeword_array[array_length] = 95 + n; // nx Shift B codeword_array[array_length] = 95 + nx; // nx Shift B
array_length++; array_length++;
for (i = 0; i < n; i++) { for (i = 0; i < nx; i++) {
if (source[input_position] >= 32) { if (source[input_position] >= 32) {
codeword_array[array_length] = source[input_position] - 32; codeword_array[array_length] = source[input_position] - 32;
} else if (source[input_position] == 13) { // CR/LF
codeword_array[array_length] = 96;
input_position++;
} else { } else {
switch(source[input_position]) { switch(source[input_position]) {
case 9: codeword_array[array_length] = 97; break; // HT case 9: codeword_array[array_length] = 97; break; // HT
@ -1062,7 +1081,7 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
binary_buffer = 0; binary_buffer = 0;
binary_buffer_size = 0; binary_buffer_size = 0;
if (ahead_a(source, input_position, length) > ahead_b(source, input_position, length)) { if (ahead_a(source, input_position, length) > ahead_b(source, input_position, length, NULL)) {
codeword_array[array_length] = 109; // Terminate with Latch to A codeword_array[array_length] = 109; // Terminate with Latch to A
encoding_mode = 'A'; encoding_mode = 'A';
} else { } else {
@ -1070,12 +1089,12 @@ static int dotcode_encode_message(struct zint_symbol *symbol, const unsigned cha
encoding_mode = 'B'; encoding_mode = 'B';
} }
array_length++; array_length++;
done = 1; // done = 1 // As long as last branch not needed
if (debug) { if (debug) {
printf("F3 "); printf("F3 ");
} }
} }
} while (input_position < length); }
if (encoding_mode == 'X') { if (encoding_mode == 'X') {
if (binary_buffer_size != 0) { if (binary_buffer_size != 0) {
@ -1111,7 +1130,7 @@ static size_t make_dotstream(unsigned char masked_array[], int array_length, cha
/* The rest of the data uses 9-bit dot patterns from Annex C */ /* The rest of the data uses 9-bit dot patterns from Annex C */
for (i = 1; i < array_length; i++) { for (i = 1; i < array_length; i++) {
bin_append(dot_patterns[masked_array[i]], 9, dot_stream); bin_append(dot_patterns[masked_array[i]], 9, dot_stream); // NOLINT masked_array values modulo 113 and fully set
} }
return strlen(dot_stream); return strlen(dot_stream);
@ -1318,7 +1337,7 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
ecc_length = 3 + (data_length / 2); ecc_length = 3 + (data_length / 2);
if (debug) { if (debug & ZINT_DEBUG_PRINT) {
printf("Codeword length = %d, ECC length = %d\n", data_length, ecc_length); printf("Codeword length = %d, ECC length = %d\n", data_length, ecc_length);
printf("Codewords: "); printf("Codewords: ");
for (i = 0; i < data_length; i++) { for (i = 0; i < data_length; i++) {
@ -1326,6 +1345,11 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
} }
printf("\n"); printf("\n");
} }
#ifdef ZINT_TEST
if (debug & ZINT_DEBUG_TEST) {
debug_test_codeword_dump(symbol, codeword_array, data_length);
}
#endif
min_dots = 9 * (data_length + 3 + (data_length / 2)) + 2; min_dots = 9 * (data_length + 3 + (data_length / 2)) + 2;
min_area = min_dots * 2; min_area = min_dots * 2;
@ -1380,6 +1404,10 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
} }
} }
if (debug & ZINT_DEBUG_PRINT) {
printf("Width = %d, Height = %d\n", width, height);
}
if ((height > 200) || (width > 200)) { if ((height > 200) || (width > 200)) {
strcpy(symbol->errtxt, "526: Specified symbol size is too large"); strcpy(symbol->errtxt, "526: Specified symbol size is too large");
return ZINT_ERROR_INVALID_OPTION; return ZINT_ERROR_INVALID_OPTION;
@ -1453,7 +1481,7 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
mask_score[i] = score_array(dot_array, height, width); mask_score[i] = score_array(dot_array, height, width);
if (debug) { if (debug & ZINT_DEBUG_PRINT) {
printf("Mask %d score is %d\n", i, mask_score[i]); printf("Mask %d score is %d\n", i, mask_score[i]);
} }
} }
@ -1487,7 +1515,7 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
mask_score[i + 4] = score_array(dot_array, height, width); mask_score[i + 4] = score_array(dot_array, height, width);
if (debug) { if (debug & ZINT_DEBUG_PRINT) {
printf("Mask %d score is %d\n", i + 4, mask_score[i + 4]); printf("Mask %d score is %d\n", i + 4, mask_score[i + 4]);
} }
} }
@ -1500,8 +1528,8 @@ INTERNAL int dotcode(struct zint_symbol *symbol, const unsigned char source[], i
} }
} }
if (debug) { if (debug & ZINT_DEBUG_PRINT) {
printf("Applying mask %d\n", best_mask); printf("Applying mask %d, high_score %d\n", best_mask, high_score);
} }
/* Apply best mask */ /* Apply best mask */

View File

@ -105,6 +105,10 @@ INTERNAL int gs1_verify(struct zint_symbol *symbol, const unsigned char source[]
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; return ZINT_ERROR_INVALID_DATA;
} }
if (source[i] == 127) {
strcpy(symbol->errtxt, "263: DEL characters are not supported by GS1");
return ZINT_ERROR_INVALID_DATA;
}
} }
if (source[0] != '[') { if (source[0] != '[') {

View File

@ -31,11 +31,99 @@
#include "testcommon.h" #include "testcommon.h"
#define TEST_ENCODE_GENERATE_EXPECTED 1 //#define TEST_INPUT_GENERATE_EXPECTED 1
//#define TEST_ENCODE_GENERATE_EXPECTED 1
// TODO: Verify against AIM standard static void test_input(void) {
static void test_encode(void) testStart("");
{
int ret;
struct item {
int input_mode;
int eci;
unsigned char *data;
int length;
int ret;
char *expected;
char *comment;
};
struct item data[] = {
/* 0*/ { UNICODE_MODE, -1, "A", -1, 0, "66 21", "" },
/* 1*/ { UNICODE_MODE, 3, "A", -1, 0, "6C 03 66 21", "" },
/* 2*/ { UNICODE_MODE, 40, "A", -1, 0, "6C 28 00 00 66 21", "" },
/* 3*/ { UNICODE_MODE, 113, "A", -1, 0, "6C 28 00 49 66 21", "" },
/* 4*/ { UNICODE_MODE, 899, "A", -1, 0, "6C 28 07 44 66 21", "" },
/* 5*/ { UNICODE_MODE, 12769, "A", -1, 0, "6C 28 70 49 66 21", "" },
/* 6*/ { UNICODE_MODE, 811799, "A", -1, 0, "6C 67 40 50 66 21", "" },
/* 7*/ { UNICODE_MODE, -1, "\000", 1, 0, "65 40", "LatchA (0x65) NUL" },
/* 8*/ { UNICODE_MODE, -1, "\010", -1, 0, "65 48", "LatchA (0x65) BS" },
/* 9*/ { UNICODE_MODE, -1, "\011", -1, 0, "65 49", "Lead special; LatchA (0x65) HT" },
/* 10*/ { UNICODE_MODE, -1, "\034", -1, 0, "65 5C", "Lead special; LatchA (0x65) FS" },
/* 11*/ { UNICODE_MODE, -1, "\035", -1, 0, "65 5D", "Lead special; LatchA (0x65) GS" },
/* 12*/ { UNICODE_MODE, -1, "\036", -1, 0, "65 5E", "Lead special; LatchA (0x65) RS" },
/* 13*/ { UNICODE_MODE, -1, "\037", -1, 0, "65 5F", "LatchA (0x65) US" },
/* 14*/ { UNICODE_MODE, -1, "\177", -1, 0, "66 5F", "ShiftB (0x66) DEL" },
/* 15*/ { UNICODE_MODE, -1, "[)>\03605\035A\036\004", -1, 0, "6A 61 21", "[)>RS 05 GS A RS EOT; LatchB (0x6A) Macro97 (0x61) A" },
/* 16*/ { UNICODE_MODE, -1, "[)>\03606\035\011\034\035\036\036\004", -1, 0, "6A 62 61 62 63 64", "[)>RS 06 GS HT FS GS RS RS EOT; LatchB (0x6A) Macro98 (0x62) HT FS GS RS" },
/* 17*/ { UNICODE_MODE, -1, "[)>\03612\03512345\036\004", -1, 0, "6A 63 11 67 17 2D", "[)>RS 12 GS A RS EOT; LatchB (0x6A) Macro99 (0x63) 1 2xShiftC (0x67) 23 45" },
/* 18*/ { UNICODE_MODE, -1, "[)>\03601Blah\004", -1, 0, "6A 64 10 11 22 4C 41 48", "[)>RS 01 Blah EOT; LatchB (0x6A) Macro100 (0x64) 0 1 B l a h" },
/* 19*/ { UNICODE_MODE, -1, "[)>\03605\035A\004", -1, 0, "6A 64 10 15 63 21", "[)>RS 05 GS A EOT; LatchB (0x6A) Macro100 (0x64) 0 5 HT A" },
/* 20*/ { UNICODE_MODE, -1, "[)>\03606A\004", -1, 0, "6A 64 10 16 21", "[)>RS 06 A EOT; LatchB (0x6A) Macro100 (0x64) 0 6 A" },
/* 21*/ { UNICODE_MODE, -1, "[)>\036991\036\004", -1, 0, "6A 64 19 19 11 64", "[)>RS 99 1 RS EOT; LatchB (0x6A) Macro100 (0x64) 9 9 1 RS" },
/* 22*/ { UNICODE_MODE, -1, "1712345610", -1, 0, "6B 64 0C 22 38", "FNC1 (0x6B) 17..10 12 34 56" },
/* 23*/ { GS1_MODE, -1, "[17]123456[10]123", -1, 0, "64 0C 22 38 0C 66 13", "17..10 12 34 56 12 ShiftB (0x66) 3" },
/* 24*/ { GS1_MODE, -1, "[90]ABC[90]abc[90]123", -1, 0, "5A 6A 21 22 23 6B 19 10 41 42 43 6B 19 67 01 17", "90 LatchB (0x6A) A B C FNC1 (0x6B) 9 0 a b c FNC1 (0x6B) 9 2xShitfC (0x67) 01 23" },
/* 25*/ { UNICODE_MODE, -1, "99aA[{00\000", 9, 0, "6B 63 6A 41 21 3B 5B 10 10 65 40", "FNC1 (0x6B) 99 LatchB (0x6A) a A [ { 0 0 ShiftA (0x65) NUL" },
/* 26*/ { UNICODE_MODE, -1, "\015\012", -1, 0, "66 60", "ShiftB (0x66) CR/LF" },
/* 27*/ { UNICODE_MODE, -1, "A\015\012", -1, 0, "67 21 60", "2xShiftB (0x67) A CR/LF" },
/* 28*/ { UNICODE_MODE, -1, "\015\015\012", -1, 0, "65 4D 4D 4A", "LatchA (0x65) CR CR LF" },
/* 29*/ { UNICODE_MODE, -1, "ABCDE12345678", -1, 0, "6A 21 22 23 24 25 69 0C 22 38 4E", "LatchB (0x6A) A B C D 4xShiftC 12 34 56 78" },
/* 30*/ { UNICODE_MODE, -1, "\000ABCD1234567890", 15, 0, "65 40 21 22 23 24 6A 0C 22 38 4E 5A", "LatchA (0x65) NULL A B C D LatchC (0x6A) 12 34 56 78 90" },
/* 31*/ { DATA_MODE, -1, "\141\142\143\144\145\200\201\202\203\204\377", -1, 0, "6A 41 42 43 44 45 70 31 5A 35 21 5A 5F 02 31", "LatchB (0x6A) a b c d e BinaryLatch (0x70) 0x80 0x81 0x82 0x83 0x84 0xFF" },
/* 32*/ { DATA_MODE, -1, "\200\061\062\240\063\064\201\202\065\066", -1, 0, "6E 40 0C 6F 00 22 70 03 10 42 6E 15 16", "UpperShiftA (0x6E) NUL 12 UpperShiftB (0x6F) SP 34 BinaryLatch (0x70) 0x81 0x82 TermB (0x6E) 5 6" },
/* 33*/ { DATA_MODE, -1, "\200\201\202\203\061\062\063\064", -1, 0, "70 13 56 0A 59 2C 67 0C 22", "BinaryLatch (0x70) 0x80 0x81 0x82 0x83 Intr2xShiftC (0x67) 12 3" },
/* 34*/ { DATA_MODE, -1, "\001\200\201\202\203\204\200\201\202\203\204", -1, 0, "65 41 70 31 5A 35 21 5A 5F 31 5A 35 21 5A 5F", "LatchA (0x65) SOH BinaryLatch (0x70) 0x80 0x81 0x82 0x83 0x80 0x81 0x82 0x83" },
/* 35*/ { UNICODE_MODE, -1, "\001abc\011\015\012\036", -1, 0, "65 41 65 41 42 43 61 60 64", "LatchA (0x65) SOH 6xShiftB (0x65) a b c HT CR/LF RS" },
};
int data_size = sizeof(data) / sizeof(struct item);
char escaped[1024];
for (int i = 0; i < data_size; i++) {
struct zint_symbol *symbol = ZBarcode_Create();
assert_nonnull(symbol, "Symbol not created\n");
symbol->symbology = BARCODE_DOTCODE;
symbol->input_mode = data[i].input_mode;
if (data[i].eci != -1) {
symbol->eci = data[i].eci;
}
symbol->debug = ZINT_DEBUG_TEST; // Needed to get codeword dump in errtxt
//symbol->debug |= ZINT_DEBUG_PRINT;
int length = data[i].length != -1 ? data[i].length : strlen(data[i].data);
ret = ZBarcode_Encode(symbol, data[i].data, length);
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
#ifdef TEST_INPUT_GENERATE_EXPECTED
printf(" /*%3d*/ { %s, %d, \"%s\", %d, %s, \"%s\", \"%s\" },\n",
i, testUtilInputModeName(data[i].input_mode), data[i].eci, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)),
data[i].length, testUtilErrorName(data[i].ret), symbol->errtxt, data[i].comment);
#else
if (ret < 5) {
assert_zero(strcmp(symbol->errtxt, data[i].expected), "i:%d strcmp(%s, %s) != 0\n", i, symbol->errtxt, data[i].expected);
}
#endif
ZBarcode_Delete(symbol);
}
testFinish();
}
static void test_encode(void) {
testStart(""); testStart("");
int ret; int ret;
@ -43,6 +131,7 @@ static void test_encode(void)
int input_mode; int input_mode;
int option_2; int option_2;
unsigned char *data; unsigned char *data;
int length;
int ret; int ret;
int expected_rows; int expected_rows;
@ -51,7 +140,7 @@ static void test_encode(void)
char *expected; char *expected;
}; };
struct item data[] = { struct item data[] = {
/* 0*/ { UNICODE_MODE, -1, "2741", 0, 10, 13, "Verified manually againt bwipp (tec-it differs)", /* 0*/ { UNICODE_MODE, -1, "2741", -1, 0, 10, 13, "ISS DotCode Rev 4.0 Figure 7B Mask = 2 prime (5)",
"1010001010101" "1010001010101"
"0001000000000" "0001000000000"
"1000100010101" "1000100010101"
@ -63,6 +152,207 @@ static void test_encode(void)
"1000100010101" "1000100010101"
"0101000100010" "0101000100010"
}, },
/* 1*/ { GS1_MODE, 64, "[01]00012345678905[17]201231[10]ABC123456", -1, 0, 9, 64, "ISS DotCode Rev 4.0 Figure 1 (left)",
"1010000000101000101010000010000010001010100010101000101000001010"
"0100010001010001010001000001010100010100010001000100010101000001"
"1010001010000000101010100010001010000010101000000010100010100000"
"0000000101000101010001010100000001000100010001000100010100000001"
"1000001000000010100010101000001010100000101010100000100010001010"
"0101000101000001000001010001010100000001000100010101000101010001"
"1000000010100010001000001000100010101000100010000010101000100000"
"0001010100010001010100010001010000010001010000000101010001010101"
"1000100010001000100010100010001010001000101000101000100010000010"
},
/* 2*/ { GS1_MODE, -1, "[01]00012345678905[17]201231[10]ABC123456", -1, 0, 20, 29, "ISS DotCode Rev 4.0 Figure 1 (right)",
"10101000101010100010101000101"
"00010100010100010100000001010"
"00001010100010000000101010000"
"01000001010101010101010101000"
"10101010100000000010100010101"
"01000100000000000101010101010"
"00100010001010101000100000101"
"00000101010000000001010000010"
"10001010100010101010001010100"
"01010000000000010000000100010"
"00100000000010100000100000000"
"00010000000101000001000001000"
"10101000101000001010001010001"
"01010001010001010101000000010"
"00001000001010001010100000101"
"01000100010100000000010100010"
"10100010000010101000101010001"
"00010101000001010100010100010"
"10000010101000100000001000001"
"01000100010101010000000101010"
},
/* 3*/ { GS1_MODE, -1, "[17]070620[10]ABC123456", -1, 0, 16, 23, "ISS DotCode Rev 4.0 Figure 6, Mask = 1",
"10000000001010001000101"
"01010101000100000101000"
"00100010000000100000001"
"01010001000001000001000"
"10101010100000001010101"
"00000100010100000100010"
"00000000001010101010001"
"00010001010001000001000"
"00101010101000001010001"
"01000100000001010000000"
"10101000101000101000001"
"00010101000100010101010"
"10001000001010100000101"
"01010001010001000001010"
"10000010101010100010101"
"01000101000101010101010"
},
/* 4*/ { GS1_MODE, 40, "[01]00012345678905", -1, 0, 7, 40, "ISS DotCode Rev 4.0 Figure 8, 7x40 **NOT SAME** but same if force mask 1 instead of mask 6",
"1010000010001010000010100000001010100010"
"0101010001000100010100000101010000010101"
"0000001010100010001010001000100000100010"
"0001000001000101010100000100010100010001"
"1000101010100000100000101000000010101010"
"0100010000010001000101000101010001000001"
"1010000010001000100010101000101000100010"
},
/* 5*/ { GS1_MODE, 18, "[01]00012345678905", -1, 0, 17, 18, "ISS DotCode Rev 4.0 Figure 8, 17x18 **NOT SAME** no matter what mask; but verified manually against bwipp and tec-it",
"101000001000101010"
"010100000101010001"
"000000101000001010"
"000100010101000101"
"001010000000100010"
"010100000100010101"
"100010001000001010"
"010001000100010100"
"001000001010000010"
"010100000001010001"
"000000101010001010"
"000101000001000101"
"100010001010100010"
"000100010000000101"
"100010001010001010"
"010001010001000101"
"100010001000100010"
},
/* 6*/ { GS1_MODE, 35, "[01]00012345678905", -1, 0, 8, 35, "ISS DotCode Rev 4.0 Figure 8, 8x35; **NOT SAME** using mask 3 prime (7) not 3 so extra dot in bottom right corner compared to figure",
"10100010000000000010100000100010101"
"00010101010001000000010100010100000"
"10001000101010101010001010000010101"
"01010001000100000101000100010101010"
"10101000100000101000100010001000001"
"00010100010000010001010001010000000"
"10000010101010101010000010000010001"
"01000001000101000100010100010001010"
},
/* 7*/ { GS1_MODE, 17, "[01]00012345678905", -1, 0, 18, 17, "ISS DotCode Rev 4.0 Figure 8, 18x17 **NOT SAME** no matter what mask; verified manually against bwipp and tec-it",
"10101000001000001"
"01000001010100010"
"00000000100010001"
"00010101000101010"
"10101000001010000"
"01000100010000000"
"00000010000000100"
"01010000000001000"
"10101010101000101"
"00000000010101010"
"00101010100000000"
"01000101000001010"
"10001000000010001"
"00000001010100010"
"00100010001000101"
"01010100010101000"
"10101010101010101"
"01010101000101010"
},
/* 8*/ { UNICODE_MODE, 35, "Dots can be Square!", -1, 0, 18, 35, "ISS DotCode Rev 4.0 Figure 11 **NOT SAME**; verified manually against bwipp and tec-it",
"10000010101000000000000000101010101"
"01010101000101000100010100000001000"
"00001000000010101000101010101010000"
"01000001000100000001010001000000000"
"00100010101010101000000010100000101"
"00000001010001010101010100010101000"
"10101000100010000010101010001010001"
"00010001010100010101000101000101010"
"00101010001000001010101000100000101"
"00010100010001010000000001010001010"
"00101000100010100000100000000000101"
"01010100010001010100010100000100000"
"10000010000000000010001000101010100"
"00010000000100010001000000010001010"
"10001000001010101010001010101000001"
"01000101010101000100000100010101000"
"10101000101000001000100010101000101"
"01000001000001000101010001000000010"
},
/* 9*/ { GS1_MODE, -1, "[99]8766", -1, 0, 10, 13, "ISS DotCode Rev 4.0 Table G.1 Mask 0 prime (4); all mask scores match Table G.1",
"1000001010001"
"0001010000010"
"0000000010001"
"0100010101000"
"0010101000101"
"0100010101010"
"0010000010000"
"0101010000010"
"1010000000101"
"0101000101010"
},
/* 10*/ { UNICODE_MODE, 6, "A", -1, 0, 19, 6, "ISS DotCode Rev 4.0 5.2.1.4 2) Table 4, 1 padding dot available; same as bwipp and tec-it except corners lit",
"101010"
"000101"
"101010"
"000001"
"100010"
"000100"
"001010"
"000101"
"101000"
"010000"
"100010"
"010000"
"000010"
"000101"
"101010"
"000001"
"101000"
"010001"
"101010"
},
/* 11*/ { UNICODE_MODE, 94, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS", -1, 0, 37, 94, "Interleaved R-S; same as bwipp and tec-it except corners lit",
"1000001010000000100010000010101010101000001000100000001010101000001000001000101010001000101010"
"0101010000000101000001010001010001010100010001000001000000010101010000000101010100010001010101"
"0010101000100010000010101010000000101010000010101000001000100010100000100010100010001000101000"
"0000000100010101000001010000010000010101010100010100000100000101000100010001000001010001010001"
"0010100000100010101000001000101010000010001000001010100000101000101010000010001000101010100010"
"0100000101010001010001000101000001000100000101010001000101000100010100000100010100010001010100"
"1010000000100010100000101010000010101000001000001010001010000010001010100000101010100000100000"
"0001010100000101000001000000010001010100000101000100010101000001000101000100010000010101010001"
"1010001010001010000000100000101010100000100010101000000010100010001010100010000010100010001010"
"0001000100010100010001010000000101010100010000010100000100000101010100010000010100010001000001"
"1000001010001010100010000010001000100010100010100010000010001000101010100000101000001000101010"
"0101000001000101000100000001010001010101010001000001000000010001010100010101010000010100000001"
"0000100000001010100010101000000010001010001010000000101010001000101010001000101000100000100010"
"0000000100000100000101010101000000010101010001000100010001000101010001000100010000010100010101"
"0010101000001010001000101000000000101010001010000010101010000010001000100000001010101010001010"
"0100010001010100010000010100010001010100010000010001000100000100010101000101000100000101010001"
"1000001000100010101010101000000010001000001010000010101010000010101010000000100010001000101000"
"0000010000010101010000010101000000010101000001000101000100010001000101010001000100010000010100"
"1000001000101010000010001000001000101010100000100000001010001010101000000010001010101010001000"
"0100010001010001000100010000010100000100010100010100010001000001010100010101000100000101000101"
"1010000000101000100000001000101010100010000000101010101010000010101000000010100010001000100010"
"0100000101010000010100010001010100010000010101000100000100000101000001010100010000000101010001"
"1000001000001010001010001000101000001000101010000010101000100010100000101000100000001000101010"
"0101010100010001010101000000010101010000010100000100010100010001000100000100010001010100010100"
"1000100000101000000010100010101000001010100010100000100000101010000010101000100000001010000000"
"0001000001000101000101010000010001000100000101010001000100010100000101000101000100000101010001"
"0000000010001000101000100000100000101010101000000010101010001000100010001000101010001000100010"
"0101010100010000000100010101010001010100000001010101000001000001000001010100000101010001010101"
"1000100010101000100000101010001000101010000010001010001010001010000010100010100010000000100000"
"0000010101010000010101000000010101000001000101000100010001000101010001000100010000010100010000"
"1000101010000010001000001000101010100000100000001010001010101000000010001010101010001000000010"
"0100000101000001010001010000000100010100010100010001000001010100010101000100000101000101010000"
"0000001010001000100010100010000010101000100010001000101000001000100000101000100010100010001010"
"0101010000010100010000010100000101010100000101000001000000010101000101000101010000000101010101"
"1000001010001010001000101000001000101010000010101000100010100000101000100000001000101010100000"
"0001000101010100000001010101000001010000010001010001000100010000010001000101010001010001000001"
"1010001000001010101000000010101000101000001000001010100000101010001000000010100000001010101010"
},
}; };
int data_size = sizeof(data) / sizeof(struct item); int data_size = sizeof(data) / sizeof(struct item);
@ -78,16 +368,16 @@ static void test_encode(void)
if (data[i].option_2 != -1) { if (data[i].option_2 != -1) {
symbol->option_2 = data[i].option_2; symbol->option_2 = data[i].option_2;
} }
symbol->debug = ZINT_DEBUG_PRINT; //symbol->debug = ZINT_DEBUG_PRINT;
int length = strlen(data[i].data); int length = data[i].length != -1 ? data[i].length : strlen(data[i].data);
ret = ZBarcode_Encode(symbol, data[i].data, length); ret = ZBarcode_Encode(symbol, data[i].data, length);
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt); assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
#ifdef TEST_ENCODE_GENERATE_EXPECTED #ifdef TEST_ENCODE_GENERATE_EXPECTED
printf(" /*%3d*/ { %s, %d, \"%s\", %s, %d, %d, \"%s\",\n", printf(" /*%3d*/ { %s, %d, \"%s\", %d, %s, %d, %d, \"%s\",\n",
i, testUtilInputModeName(data[i].input_mode), data[i].option_2, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), i, testUtilInputModeName(data[i].input_mode), data[i].option_2, testUtilEscape(data[i].data, length, escaped, sizeof(escaped)), data[i].length,
testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment); testUtilErrorName(data[i].ret), symbol->rows, symbol->width, data[i].comment);
testUtilModulesDump(symbol, " ", "\n"); testUtilModulesDump(symbol, " ", "\n");
printf(" },\n"); printf(" },\n");
@ -111,8 +401,7 @@ static void test_encode(void)
} }
// #181 Christian Hartlage / Nico Gunkel OSS-Fuzz // #181 Christian Hartlage / Nico Gunkel OSS-Fuzz
static void test_fuzz(void) static void test_fuzz(void) {
{
testStart(""); testStart("");
int ret; int ret;
@ -172,10 +461,55 @@ static void test_fuzz(void)
testFinish(); testFinish();
} }
int main() static void test_large(void) {
{ testStart("");
int ret;
struct item {
int option_2;
char datum;
int length;
int ret;
};
// s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<"))
struct item data[] = {
/* 0*/ { 200, '0', 2940, 0 }, // 2940 largest Code Set C data that fits in 200x199 HxW
/* 1*/ { 200, '0', 2941, ZINT_ERROR_INVALID_OPTION },
/* 2*/ { 200, '9', 200, 0 }, // Changes a number of mask scores re pre-Rev. 4 version, but best score still the same (7)
/* 3*/ { 30, '\001', 71, 0 }, // Codeword length 72, ECC length 39, for ND + 1 == 112
};
int data_size = sizeof(data) / sizeof(struct item);
char data_buf[4096];
for (int i = 0; i < data_size; i++) {
struct zint_symbol *symbol = ZBarcode_Create();
assert_nonnull(symbol, "Symbol not created\n");
symbol->symbology = BARCODE_DOTCODE;
symbol->input_mode = DATA_MODE;
symbol->option_2 = data[i].option_2;
//symbol->debug = ZINT_DEBUG_PRINT;
int length = data[i].length;
memset(data_buf, data[i].datum, length);
ret = ZBarcode_Encode(symbol, data_buf, length);
assert_equal(ret, data[i].ret, "i:%d ZBarcode_Encode ret %d != %d (%s)\n", i, ret, data[i].ret, symbol->errtxt);
ZBarcode_Delete(symbol);
}
testFinish();
}
int main() {
test_input();
test_encode(); test_encode();
test_fuzz(); test_fuzz();
test_large();
testReport(); testReport();

File diff suppressed because it is too large Load Diff