From a085bca1680407d6483a87926b58b658e8672512 Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Sun, 18 Jun 2017 13:00:22 +0100 Subject: [PATCH] Revion of ECI number encoding --- backend/aztec.c | 61 +++++++++++++++++++++++++++++++++++++++++++--- backend/dmatrix.c | 21 ++++++++++++++-- backend/gridmtx.c | 15 ++++++++++-- backend/hanxin.c | 13 +++++++++- backend/library.c | 5 ++++ backend/maxicode.c | 36 ++++++++++++++++++++++++--- backend/pdf417.c | 50 +++++++++++++++++++++++++++++++------ 7 files changed, 181 insertions(+), 20 deletions(-) diff --git a/backend/aztec.c b/backend/aztec.c index f621a8ff..42fa45b9 100644 --- a/backend/aztec.c +++ b/backend/aztec.c @@ -88,7 +88,7 @@ static int aztec_text_process(const unsigned char source[], const size_t src_len /* Add FNC1 to beginning of GS1 messages */ charmap[maplength] = 0; typemap[maplength++] = PUNC; - charmap[maplength] = 400; + charmap[maplength] = 400; // FLG(0) typemap[maplength++] = PUNC; } else if (eci != 3) { /* Set ECI mode */ @@ -99,7 +99,8 @@ static int aztec_text_process(const unsigned char source[], const size_t src_len typemap[maplength++] = PUNC; charmap[maplength] = 502 + eci; typemap[maplength++] = PUNC; - } else { + } + if ((eci >= 10) && (eci <= 99)) { charmap[maplength] = 402; // FLG(2) typemap[maplength++] = PUNC; charmap[maplength] = 502 + (eci / 10); @@ -107,9 +108,61 @@ static int aztec_text_process(const unsigned char source[], const size_t src_len charmap[maplength] = 502 + (eci % 10); typemap[maplength++] = PUNC; } - + if ((eci >= 100) && (eci <= 999)) { + charmap[maplength] = 403; // FLG(3) + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci / 100); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 100) / 10); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci % 10); + typemap[maplength++] = PUNC; + } + if ((eci >= 1000) && (eci <= 9999)) { + charmap[maplength] = 404; // FLG(4) + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci / 1000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 1000) / 100); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 100) / 10); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci % 10); + typemap[maplength++] = PUNC; + } + if ((eci >= 10000) && (eci <= 99999)) { + charmap[maplength] = 405; // FLG(5) + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci / 10000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 10000) / 1000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 1000) / 100); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 100) / 10); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci % 10); + typemap[maplength++] = PUNC; + } + if (eci >= 100000) { + charmap[maplength] = 406; // FLG(6) + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci / 100000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 100000) / 10000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 10000) / 1000); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 1000) / 100); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + ((eci % 100) / 10); + typemap[maplength++] = PUNC; + charmap[maplength] = 502 + (eci % 10); + typemap[maplength++] = PUNC; + } } - + + /* Copy the rest of the data into charmap */ for (i = 0; i < (int) src_len; i++) { if ((gs1) && (source[i] == '[')) { /* FNC1 represented by FLG(0) */ diff --git a/backend/dmatrix.c b/backend/dmatrix.c index 113824fc..b282e853 100644 --- a/backend/dmatrix.c +++ b/backend/dmatrix.c @@ -578,10 +578,27 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], } if (symbol->eci > 3) { + /* Encode ECI numbers according to Table 6 */ target[tp] = 241; /* ECI Character */ tp++; - target[tp] = (unsigned char) (symbol->eci + 1); - tp++; + if (symbol->eci <= 126) { + target[tp] = (unsigned char) symbol->eci + 1; + tp++; + } + if ((symbol->eci >= 127) && (symbol->eci <= 16382)) { + target[tp] = (unsigned char) ((symbol->eci - 127) / 254) + 128; + tp++; + target[tp] = (unsigned char) ((symbol->eci - 127) % 254) + 1; + tp++; + } + if (symbol->eci >= 16383) { + target[tp] = (unsigned char) ((symbol->eci - 16383) / 64516) + 192; + tp++; + target[tp] = (unsigned char) (((symbol->eci - 16383) / 254) % 254) + 1; + tp++; + target[tp] = (unsigned char) ((symbol->eci - 16383) % 254) + 1; + tp++; + } if (debug) printf("ECI %d ", symbol->eci + 1); } diff --git a/backend/gridmtx.c b/backend/gridmtx.c index 5e59cf16..a33f215f 100644 --- a/backend/gridmtx.c +++ b/backend/gridmtx.c @@ -367,8 +367,19 @@ static int gm_encode(int gbdata[], const size_t length, char binary[], int reade } if (eci != 3) { - bin_append(24, 5, binary); /* ECI */ - bin_append(eci, 10, binary); + /* ECI assignment according to Table 8 */ + bin_append(12, 4, binary); /* ECI */ + if (eci <= 1023) { + bin_append(eci, 11, binary); + } + if ((eci >= 1024) && (eci <= 32767)) { + strcat(binary, "10"); + bin_append(eci, 15, binary); + } + if (eci >= 32768) { + strcat(binary, "11"); + bin_append(eci, 20, binary); + } } do { diff --git a/backend/hanxin.c b/backend/hanxin.c index 2ccf31bc..000f9b35 100644 --- a/backend/hanxin.c +++ b/backend/hanxin.c @@ -339,8 +339,19 @@ static void calculate_binary(char binary[], char mode[], int source[], const siz int submode; if (eci != 3) { + /* Encoding ECI assignment number, according to Table 5 */ bin_append(8, 4, binary); // ECI - bin_append(eci, 8, binary); + if (eci <= 127) { + bin_append(eci, 8, binary); + } + if ((eci >= 128) && (eci <= 16383)) { + strcat(binary, "10"); + bin_append(eci, 14, binary); + } + if (eci >= 16384) { + strcat(binary, "110"); + bin_append(eci, 21, binary); + } } do { diff --git a/backend/library.c b/backend/library.c index b529d18a..13bb009e 100644 --- a/backend/library.c +++ b/backend/library.c @@ -820,6 +820,11 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source,int #endif error_number = 0; + for (i = 0; i < in_length; i++) { + printf("%X ", (int) source[i]); + } + printf("\n"); + if (in_length == 0) { in_length = (int)ustrlen(source); } diff --git a/backend/maxicode.c b/backend/maxicode.c index 50a629ad..861189e6 100644 --- a/backend/maxicode.c +++ b/backend/maxicode.c @@ -452,12 +452,42 @@ int maxi_text_process(int mode, unsigned char source[], int length, int eci) { } while (i <= 143); /* Insert ECI at the beginning of message if needed */ + /* Encode ECI assignment numbers according to table 3 */ if (eci != 3) { maxi_bump(set, character, 0); character[0] = 27; // ECI - maxi_bump(set, character, 1); - character[1] = eci; - length += 2; + if (eci <= 31) { + maxi_bump(set, character, 1); + character[1] = eci; + length += 2; + } + if ((eci >= 32) && (eci <= 1023)) { + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + character[1] = 0x20 + ((eci >> 6) & 0x0F); + character[2] = eci & 0x3F; + length += 3; + } + if ((eci >= 1024) && (eci <= 32767)) { + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + character[1] = 0x30 + ((eci >> 12) & 0x03); + character[2] = (eci >> 6) & 0x3F; + character[3] = eci & 0x3F; + length += 4; + } + if (eci >= 32768) { + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + maxi_bump(set, character, 1); + character[1] = 0x38 + ((eci >> 18) & 0x02); + character[2] = (eci >> 12) & 0x3F; + character[3] = (eci >> 6) & 0x3F; + character[4] = eci & 0x3F; + length += 5; + } } if (((mode == 2) || (mode == 3)) && (length > 84)) { diff --git a/backend/pdf417.c b/backend/pdf417.c index 5cad9878..b13b4418 100644 --- a/backend/pdf417.c +++ b/backend/pdf417.c @@ -616,10 +616,27 @@ static int pdf417(struct zint_symbol *symbol, unsigned char chaine[], const size } if (symbol->eci != 3) { - chainemc[mclength] = 927; /* ECI */ - mclength++; - chainemc[mclength] = symbol->eci; - mclength++; + /* Encoding ECI assignment number, according to Table 8 */ + if (symbol->eci <= 899) { + chainemc[mclength] = 927; /* ECI */ + mclength++; + chainemc[mclength] = symbol->eci; + mclength++; + } + if ((symbol->eci >= 900) && (symbol->eci <= 810899)) { + chainemc[mclength] = 926; /* ECI */ + mclength++; + chainemc[mclength] = (symbol->eci / 900) - 1; + mclength++; + chainemc[mclength] = symbol->eci % 900; + mclength++; + } + if (symbol->eci >= 810900) { + chainemc[mclength] = 925; /* ECI */ + mclength++; + chainemc[mclength] = symbol->eci - 810900; + mclength++; + } } for (i = 0; i < indexliste; i++) { @@ -920,10 +937,27 @@ int micro_pdf417(struct zint_symbol *symbol, unsigned char chaine[], const size_ } if (symbol->eci != 3) { - chainemc[mclength] = 927; /* ECI */ - mclength++; - chainemc[mclength] = symbol->eci; - mclength++; + /* Encoding ECI assignment number, according to Table 8 */ + if (symbol->eci <= 899) { + chainemc[mclength] = 927; /* ECI */ + mclength++; + chainemc[mclength] = symbol->eci; + mclength++; + } + if ((symbol->eci >= 900) && (symbol->eci <= 810899)) { + chainemc[mclength] = 926; /* ECI */ + mclength++; + chainemc[mclength] = (symbol->eci / 900) - 1; + mclength++; + chainemc[mclength] = symbol->eci % 900; + mclength++; + } + if (symbol->eci >= 810900) { + chainemc[mclength] = 925; /* ECI */ + mclength++; + chainemc[mclength] = symbol->eci - 810900; + mclength++; + } } for (i = 0; i < indexliste; i++) {