2019-10-07 05:39:54 +13:00
|
|
|
/* dmatrix.c Handles Data Matrix ECC 200 symbols */
|
|
|
|
|
|
|
|
/*
|
|
|
|
libzint - the open source barcode library
|
2021-02-23 13:01:15 +13:00
|
|
|
Copyright (C) 2009 - 2021 Robin Stuart <rstuart114@gmail.com>
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
developed from and including some functions from:
|
|
|
|
IEC16022 bar code generation
|
|
|
|
Adrian Kennard, Andrews & Arnold Ltd
|
|
|
|
with help from Cliff Hones on the RS coding
|
|
|
|
|
|
|
|
(c) 2004 Adrian Kennard, Andrews & Arnold Ltd
|
|
|
|
(c) 2006 Stefan Schmidt <stefan@datenfreihafen.org>
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-11-28 05:16:14 +13:00
|
|
|
/* vim: set ts=4 sw=4 et : */
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
#include "common.h"
|
2019-12-19 13:37:55 +13:00
|
|
|
#include "reedsol.h"
|
2019-10-07 05:39:54 +13:00
|
|
|
#include "dmatrix.h"
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
/* Annex F placement algorithm low level */
|
|
|
|
static void dm_placementbit(int *array, const int NR, const int NC, int r, int c, const int p, const char b) {
|
2019-10-07 05:39:54 +13:00
|
|
|
if (r < 0) {
|
|
|
|
r += NR;
|
|
|
|
c += 4 - ((NR + 4) % 8);
|
|
|
|
}
|
|
|
|
if (c < 0) {
|
|
|
|
c += NC;
|
|
|
|
r += 4 - ((NC + 4) % 8);
|
|
|
|
}
|
2021-11-09 02:05:37 +13:00
|
|
|
// Necessary for DMRE (ISO/IEC 21471:2020 Annex E)
|
2019-10-07 05:39:54 +13:00
|
|
|
if (r >= NR) {
|
|
|
|
r -= NR;
|
|
|
|
}
|
|
|
|
// Check index limits
|
|
|
|
assert(r < NR);
|
|
|
|
assert(c < NC);
|
|
|
|
// Check double-assignment
|
|
|
|
assert(0 == array[r * NC + c]);
|
|
|
|
array[r * NC + c] = (p << 3) + b;
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_placementblock(int *array, const int NR, const int NC, const int r,
|
2019-10-07 05:39:54 +13:00
|
|
|
const int c, const int p) {
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementbit(array, NR, NC, r - 2, c - 2, p, 7);
|
|
|
|
dm_placementbit(array, NR, NC, r - 2, c - 1, p, 6);
|
|
|
|
dm_placementbit(array, NR, NC, r - 1, c - 2, p, 5);
|
|
|
|
dm_placementbit(array, NR, NC, r - 1, c - 1, p, 4);
|
|
|
|
dm_placementbit(array, NR, NC, r - 1, c - 0, p, 3);
|
|
|
|
dm_placementbit(array, NR, NC, r - 0, c - 2, p, 2);
|
|
|
|
dm_placementbit(array, NR, NC, r - 0, c - 1, p, 1);
|
|
|
|
dm_placementbit(array, NR, NC, r - 0, c - 0, p, 0);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_placementcornerA(int *array, const int NR, const int NC, const int p) {
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 0, p, 7);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 1, p, 6);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 2, p, 5);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 1, p, 2);
|
|
|
|
dm_placementbit(array, NR, NC, 2, NC - 1, p, 1);
|
|
|
|
dm_placementbit(array, NR, NC, 3, NC - 1, p, 0);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_placementcornerB(int *array, const int NR, const int NC, const int p) {
|
|
|
|
dm_placementbit(array, NR, NC, NR - 3, 0, p, 7);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 2, 0, p, 6);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 0, p, 5);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 4, p, 4);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 3, p, 3);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 2, p, 2);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 1, p, 1);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 1, p, 0);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_placementcornerC(int *array, const int NR, const int NC, const int p) {
|
|
|
|
dm_placementbit(array, NR, NC, NR - 3, 0, p, 7);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 2, 0, p, 6);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 0, p, 5);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 1, p, 2);
|
|
|
|
dm_placementbit(array, NR, NC, 2, NC - 1, p, 1);
|
|
|
|
dm_placementbit(array, NR, NC, 3, NC - 1, p, 0);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_placementcornerD(int *array, const int NR, const int NC, const int p) {
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, 0, p, 7);
|
|
|
|
dm_placementbit(array, NR, NC, NR - 1, NC - 1, p, 6);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 3, p, 5);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
|
|
|
|
dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 3, p, 2);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 2, p, 1);
|
|
|
|
dm_placementbit(array, NR, NC, 1, NC - 1, p, 0);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
/* Annex F placement algorithm main function */
|
|
|
|
static void dm_placement(int *array, const int NR, const int NC) {
|
2019-10-07 05:39:54 +13:00
|
|
|
int r, c, p;
|
|
|
|
// start
|
|
|
|
p = 1;
|
|
|
|
r = 4;
|
|
|
|
c = 0;
|
|
|
|
do {
|
|
|
|
// check corner
|
|
|
|
if (r == NR && !c)
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementcornerA(array, NR, NC, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
if (r == NR - 2 && !c && NC % 4)
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementcornerB(array, NR, NC, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
if (r == NR - 2 && !c && (NC % 8) == 4)
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementcornerC(array, NR, NC, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
if (r == NR + 4 && c == 2 && !(NC % 8))
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementcornerD(array, NR, NC, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
// up/right
|
|
|
|
do {
|
|
|
|
if (r < NR && c >= 0 && !array[r * NC + c])
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementblock(array, NR, NC, r, c, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
r -= 2;
|
|
|
|
c += 2;
|
|
|
|
} while (r >= 0 && c < NC);
|
|
|
|
r++;
|
|
|
|
c += 3;
|
|
|
|
// down/left
|
|
|
|
do {
|
|
|
|
if (r >= 0 && c < NC && !array[r * NC + c])
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_placementblock(array, NR, NC, r, c, p++);
|
2019-10-07 05:39:54 +13:00
|
|
|
r += 2;
|
|
|
|
c -= 2;
|
|
|
|
} while (r < NR && c >= 0);
|
|
|
|
r += 3;
|
|
|
|
c++;
|
|
|
|
} while (r < NR || c < NC);
|
|
|
|
// unfilled corner
|
|
|
|
if (!array[NR * NC - 1])
|
|
|
|
array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate and append ecc code, and if necessary interleave */
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_ecc(unsigned char *binary, const int bytes, const int datablock, const int rsblock, const int skew) {
|
2019-10-07 05:39:54 +13:00
|
|
|
int blocks = (bytes + 2) / datablock, b;
|
2021-02-23 13:01:15 +13:00
|
|
|
int rsblocks = rsblock * blocks;
|
2019-10-07 05:39:54 +13:00
|
|
|
int n;
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_t rs;
|
|
|
|
|
|
|
|
rs_init_gf(&rs, 0x12d);
|
|
|
|
rs_init_code(&rs, rsblock, 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
for (b = 0; b < blocks; b++) {
|
|
|
|
unsigned char buf[256], ecc[256];
|
|
|
|
int p = 0;
|
|
|
|
for (n = b; n < bytes; n += blocks)
|
|
|
|
buf[p++] = binary[n];
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_encode(&rs, p, buf, ecc);
|
2019-10-07 05:39:54 +13:00
|
|
|
p = rsblock - 1; // comes back reversed
|
2021-02-23 13:01:15 +13:00
|
|
|
for (n = b; n < rsblocks; n += blocks) {
|
2019-10-07 05:39:54 +13:00
|
|
|
if (skew) {
|
|
|
|
/* Rotate ecc data to make 144x144 size symbols acceptable */
|
2021-11-09 02:05:37 +13:00
|
|
|
/* See http://groups.google.com/group/postscriptbarcode/msg/5ae8fda7757477da
|
|
|
|
or https://github.com/nu-book/zxing-cpp/issues/259 */
|
2019-10-07 05:39:54 +13:00
|
|
|
if (b < 8) {
|
|
|
|
binary[bytes + n + 2] = ecc[p--];
|
|
|
|
} else {
|
|
|
|
binary[bytes + n - 8] = ecc[p--];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
binary[bytes + n] = ecc[p--];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Is basic (non-shifted) C40? */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_isc40(const unsigned char input) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (input <= '9') {
|
|
|
|
return input >= '0' || input == ' ';
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
return input >= 'A' && input <= 'Z';
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is basic (non-shifted) TEXT? */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_istext(const unsigned char input) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (input <= '9') {
|
|
|
|
return input >= '0' || input == ' ';
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
return input >= 'a' && input <= 'z';
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Is basic (non-shifted) C40/TEXT? */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_isc40text(const int current_mode, const unsigned char input) {
|
|
|
|
return current_mode == DM_C40 ? dm_isc40(input) : dm_istext(input);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Return true (1) if a character is valid in X12 set */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_isX12(const unsigned char input) {
|
2021-11-15 01:09:51 +13:00
|
|
|
return dm_isc40(input) || input == 13 || input == '*' || input == '>';
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-10 02:43:56 +13:00
|
|
|
/* Return true (1) if a character is valid in EDIFACT set */
|
|
|
|
static int dm_isedifact(const unsigned char input, const int gs1) {
|
2021-11-15 01:09:51 +13:00
|
|
|
return (input >= ' ' && input <= '^') && (!gs1 || input != '['); /* Can't encode GS1 FNC1/GS in EDIFACT */
|
2021-11-10 02:43:56 +13:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:37:51 +13:00
|
|
|
static int dm_p_r_6_2_1(const unsigned char source[], const int length, const int sp) {
|
2019-10-07 05:39:54 +13:00
|
|
|
/* Annex P section (r)(6)(ii)(I)
|
|
|
|
"If one of the three X12 terminator/separator characters first
|
|
|
|
occurs in the yet to be processed data before a non-X12 character..."
|
|
|
|
*/
|
2020-12-23 23:57:24 +13:00
|
|
|
int i;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-17 07:37:51 +13:00
|
|
|
for (i = sp; i < length && dm_isX12(source[i]); i++) {
|
|
|
|
if (source[i] == 13 || source[i] == '*' || source[i] == '>') {
|
2021-02-23 13:01:15 +13:00
|
|
|
return 1;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
return 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:37:51 +13:00
|
|
|
/* Count number of TEXT characters around `sp` between `position` and `length`
|
|
|
|
- helper to avoid exiting from Base 256 too early if have series of TEXT characters */
|
|
|
|
static int dm_text_sp_cnt(const unsigned char source[], const int position, const int length, const int sp) {
|
|
|
|
int i;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
/* Count from `sp` forward */
|
|
|
|
for (i = sp; i < length && dm_istext(source[i]); i++, cnt++);
|
|
|
|
/* Count backwards from `sp` */
|
|
|
|
for (i = sp - 1; i >= position && dm_istext(source[i]); i--, cnt++);
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2021-06-30 09:02:24 +12:00
|
|
|
/* Character counts are multiplied by this, so as to be whole integer divisible by 2, 3 and 4 */
|
|
|
|
#define DM_MULT 12
|
|
|
|
|
|
|
|
#define DM_MULT_1_DIV_2 6
|
|
|
|
#define DM_MULT_2_DIV_3 8
|
|
|
|
#define DM_MULT_3_DIV_4 9
|
|
|
|
#define DM_MULT_1 12
|
|
|
|
#define DM_MULT_4_DIV_3 16
|
|
|
|
#define DM_MULT_2 24
|
|
|
|
#define DM_MULT_8_DIV_3 32
|
2021-11-15 01:09:51 +13:00
|
|
|
#define DM_MULT_3 26
|
2021-06-30 09:02:24 +12:00
|
|
|
#define DM_MULT_13_DIV_4 39
|
|
|
|
#define DM_MULT_10_DIV_3 40
|
|
|
|
#define DM_MULT_4 48
|
|
|
|
#define DM_MULT_17_DIV_4 51
|
|
|
|
#define DM_MULT_13_DIV_3 52
|
|
|
|
|
|
|
|
#define DM_MULT_MINUS_1 11
|
|
|
|
#define DM_MULT_CEIL(n) ((((n) + DM_MULT_MINUS_1) / DM_MULT) * DM_MULT)
|
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
/* 'look ahead test' from Annex P */
|
2021-11-17 07:37:51 +13:00
|
|
|
static int dm_look_ahead_test(const unsigned char source[], const int length, const int position,
|
2021-11-15 01:09:51 +13:00
|
|
|
const int current_mode, const int mode_arg, const int gs1, const int debug_print) {
|
2021-06-30 09:02:24 +12:00
|
|
|
int ascii_count, c40_count, text_count, x12_count, edf_count, b256_count;
|
2021-02-23 13:01:15 +13:00
|
|
|
int ascii_rnded, c40_rnded, text_rnded, x12_rnded, edf_rnded, b256_rnded;
|
2021-06-30 09:02:24 +12:00
|
|
|
int cnt_1;
|
2020-12-23 23:57:24 +13:00
|
|
|
int sp;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
/* step (j) */
|
2021-11-15 01:09:51 +13:00
|
|
|
if (current_mode == DM_ASCII || current_mode == DM_BASE256) { /* Adjusted to use for DM_BASE256 also */
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_count = 0;
|
|
|
|
c40_count = DM_MULT_1;
|
|
|
|
text_count = DM_MULT_1;
|
|
|
|
x12_count = DM_MULT_1;
|
|
|
|
edf_count = DM_MULT_1;
|
2021-11-15 01:09:51 +13:00
|
|
|
b256_count = DM_MULT_2; /* Adjusted from DM_MULT_5_DIV_4 (1.25) */
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_count = DM_MULT_1;
|
|
|
|
c40_count = DM_MULT_2;
|
|
|
|
text_count = DM_MULT_2;
|
|
|
|
x12_count = DM_MULT_2;
|
|
|
|
edf_count = DM_MULT_2;
|
2021-11-15 01:09:51 +13:00
|
|
|
b256_count = DM_MULT_3; /* Adjusted from DM_MULT_9_DIV_4 (2.25) */
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (current_mode) {
|
2021-06-30 09:02:24 +12:00
|
|
|
case DM_C40: c40_count = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
break;
|
2021-06-30 09:02:24 +12:00
|
|
|
case DM_TEXT: text_count = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
break;
|
2021-06-30 09:02:24 +12:00
|
|
|
case DM_X12: x12_count = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
break;
|
2021-06-30 09:02:24 +12:00
|
|
|
case DM_EDIFACT: edf_count = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
break;
|
2021-11-15 01:09:51 +13:00
|
|
|
case DM_BASE256:
|
|
|
|
b256_count = mode_arg == 249 ? DM_MULT_1 : 0; /* Adjusted to use no. of bytes written */
|
2019-10-07 05:39:54 +13:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-17 07:37:51 +13:00
|
|
|
for (sp = position; sp < length; sp++) {
|
|
|
|
unsigned char c = source[sp];
|
2021-06-28 02:58:27 +12:00
|
|
|
int is_extended = c & 0x80;
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* ascii ... step (l) */
|
2021-11-15 01:09:51 +13:00
|
|
|
if ((c <= '9') && (c >= '0')) {
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_count += DM_MULT_1_DIV_2; // (l)(1)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (is_extended) {
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_count = DM_MULT_CEIL(ascii_count) + DM_MULT_2; // (l)(2)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_count = DM_MULT_CEIL(ascii_count) + DM_MULT_1; // (l)(3)
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* c40 ... step (m) */
|
2021-11-09 02:05:37 +13:00
|
|
|
if (dm_isc40(c)) {
|
2021-06-30 09:02:24 +12:00
|
|
|
c40_count += DM_MULT_2_DIV_3; // (m)(1)
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (is_extended) {
|
2021-06-30 09:02:24 +12:00
|
|
|
c40_count += DM_MULT_8_DIV_3; // (m)(2)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
c40_count += DM_MULT_4_DIV_3; // (m)(3)
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* text ... step (n) */
|
2021-11-09 02:05:37 +13:00
|
|
|
if (dm_istext(c)) {
|
2021-06-30 09:02:24 +12:00
|
|
|
text_count += DM_MULT_2_DIV_3; // (n)(1)
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (is_extended) {
|
2021-06-30 09:02:24 +12:00
|
|
|
text_count += DM_MULT_8_DIV_3; // (n)(2)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
text_count += DM_MULT_4_DIV_3; // (n)(3)
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* x12 ... step (o) */
|
2021-11-09 02:05:37 +13:00
|
|
|
if (dm_isX12(c)) {
|
2021-06-30 09:02:24 +12:00
|
|
|
x12_count += DM_MULT_2_DIV_3; // (o)(1)
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (is_extended) {
|
2021-06-30 09:02:24 +12:00
|
|
|
x12_count += DM_MULT_13_DIV_3; // (o)(2)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
x12_count += DM_MULT_10_DIV_3; // (o)(3)
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* edifact ... step (p) */
|
2021-11-10 02:43:56 +13:00
|
|
|
if (dm_isedifact(c, gs1)) {
|
2021-06-30 09:02:24 +12:00
|
|
|
edf_count += DM_MULT_3_DIV_4; // (p)(1)
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (is_extended) {
|
2021-06-30 09:02:24 +12:00
|
|
|
edf_count += DM_MULT_17_DIV_4; // (p)(2)
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
edf_count += DM_MULT_13_DIV_4; // (p)(3)
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* base 256 ... step (q) */
|
2021-06-28 02:58:27 +12:00
|
|
|
if ((gs1 == 1) && (c == '[')) {
|
2021-02-23 13:01:15 +13:00
|
|
|
/* FNC1 separator */
|
2021-06-30 09:02:24 +12:00
|
|
|
b256_count += DM_MULT_4; // (q)(1)
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-06-30 09:02:24 +12:00
|
|
|
b256_count += DM_MULT_1; // (q)(2)
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (sp >= position + 3) {
|
|
|
|
/* At least 4 data characters processed ... step (r) */
|
|
|
|
/* NOTE: previous behaviour was at least 5 (same as BWIPP) */
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2021-11-15 01:09:51 +13:00
|
|
|
printf("\n(m:%d, p:%d, sp:%d, a:%d): ascii_count %d, b256_count %d, edf_count %d, text_count %d"
|
2021-06-30 09:02:24 +12:00
|
|
|
", x12_count %d, c40_count %d ",
|
2021-11-15 01:09:51 +13:00
|
|
|
current_mode, position, sp, mode_arg, ascii_count, b256_count, edf_count, text_count,
|
2021-06-27 22:47:55 +12:00
|
|
|
x12_count, c40_count);
|
|
|
|
}
|
2021-06-26 11:27:04 +12:00
|
|
|
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = ascii_count + DM_MULT_1;
|
2021-11-15 01:09:51 +13:00
|
|
|
/* Adjusted from <= b256_count */
|
|
|
|
if (cnt_1 < b256_count && cnt_1 <= edf_count && cnt_1 <= text_count && cnt_1 <= x12_count
|
2021-02-23 13:01:15 +13:00
|
|
|
&& cnt_1 <= c40_count) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("ASC->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_ASCII; /* step (r)(1) */
|
|
|
|
}
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = b256_count + DM_MULT_1;
|
2021-02-23 13:01:15 +13:00
|
|
|
if (cnt_1 <= ascii_count || (cnt_1 < edf_count && cnt_1 < text_count && cnt_1 < x12_count
|
|
|
|
&& cnt_1 < c40_count)) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("BAS->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_BASE256; /* step (r)(2) */
|
|
|
|
}
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = edf_count + DM_MULT_1;
|
2021-02-23 13:01:15 +13:00
|
|
|
if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < text_count && cnt_1 < x12_count
|
|
|
|
&& cnt_1 < c40_count) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("EDI->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_EDIFACT; /* step (r)(3) */
|
|
|
|
}
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = text_count + DM_MULT_1;
|
2021-02-23 13:01:15 +13:00
|
|
|
if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < x12_count
|
|
|
|
&& cnt_1 < c40_count) {
|
2021-11-17 07:37:51 +13:00
|
|
|
/* Adjusted to avoid early exit from Base 256 if have less than break-even sequence of TEXT chars */
|
2021-12-09 07:12:20 +13:00
|
|
|
if (current_mode == DM_BASE256 && position + 6 < length) {
|
|
|
|
if (dm_text_sp_cnt(source, position, length, sp) >= 12) {
|
|
|
|
if (debug_print) printf("TEX->");
|
|
|
|
return DM_TEXT; /* step (r)(4) */
|
|
|
|
}
|
2021-11-17 07:37:51 +13:00
|
|
|
} else {
|
|
|
|
if (debug_print) printf("TEX->");
|
|
|
|
return DM_TEXT; /* step (r)(4) */
|
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = x12_count + DM_MULT_1;
|
2021-02-23 13:01:15 +13:00
|
|
|
if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < text_count
|
|
|
|
&& cnt_1 < c40_count) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("X12->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_X12; /* step (r)(5) */
|
|
|
|
}
|
2021-06-30 09:02:24 +12:00
|
|
|
cnt_1 = c40_count + DM_MULT_1;
|
2021-02-23 13:01:15 +13:00
|
|
|
if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < text_count) {
|
|
|
|
if (c40_count < x12_count) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("C40->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_C40; /* step (r)(6)(i) */
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
if (c40_count == x12_count) {
|
2021-11-17 07:37:51 +13:00
|
|
|
if (dm_p_r_6_2_1(source, length, sp) == 1) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("X12->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_X12; /* step (r)(6)(ii)(I) */
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("C40->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_C40; /* step (r)(6)(ii)(II) */
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* At the end of data ... step (k) */
|
|
|
|
/* step (k)(1) */
|
2021-06-30 09:02:24 +12:00
|
|
|
ascii_rnded = DM_MULT_CEIL(ascii_count);
|
|
|
|
b256_rnded = DM_MULT_CEIL(b256_count);
|
|
|
|
edf_rnded = DM_MULT_CEIL(edf_count);
|
|
|
|
text_rnded = DM_MULT_CEIL(text_count);
|
|
|
|
x12_rnded = DM_MULT_CEIL(x12_count);
|
|
|
|
c40_rnded = DM_MULT_CEIL(c40_count);
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2021-11-15 01:09:51 +13:00
|
|
|
printf("\nEOD(m:%d, p:%d, a:%d): ascii_rnded %d, b256_rnded %d, edf_rnded %d, text_rnded %d"
|
|
|
|
", x12_rnded %d (%d), c40_rnded %d (%d) ",
|
|
|
|
current_mode, position, mode_arg, ascii_rnded, b256_rnded, edf_rnded, text_rnded,
|
|
|
|
x12_rnded, x12_count, c40_rnded, c40_count);
|
2021-06-27 22:47:55 +12:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
|
|
|
|
if (ascii_rnded <= b256_rnded && ascii_rnded <= edf_rnded && ascii_rnded <= text_rnded && ascii_rnded <= x12_rnded
|
|
|
|
&& ascii_rnded <= c40_rnded) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("ASC->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_ASCII; /* step (k)(2) */
|
|
|
|
}
|
|
|
|
if (b256_rnded < ascii_rnded && b256_rnded < edf_rnded && b256_rnded < text_rnded && b256_rnded < x12_rnded
|
|
|
|
&& b256_rnded < c40_rnded) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("BAS->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_BASE256; /* step (k)(3) */
|
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
/* Adjusted from < x12_rnded */
|
|
|
|
if (edf_rnded < ascii_rnded && edf_rnded < b256_rnded && edf_rnded < text_rnded && edf_rnded <= x12_rnded
|
2021-02-23 13:01:15 +13:00
|
|
|
&& edf_rnded < c40_rnded) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("EDI->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_EDIFACT; /* step (k)(4) */
|
|
|
|
}
|
|
|
|
if (text_rnded < ascii_rnded && text_rnded < b256_rnded && text_rnded < edf_rnded && text_rnded < x12_rnded
|
|
|
|
&& text_rnded < c40_rnded) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("TEX->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_TEXT; /* step (k)(5) */
|
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
/* Adjusted from < edf_rnded */
|
|
|
|
if (x12_rnded < ascii_rnded && x12_rnded < b256_rnded && x12_rnded <= edf_rnded && x12_rnded < text_rnded
|
2021-02-23 13:01:15 +13:00
|
|
|
&& x12_rnded < c40_rnded) {
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("X12->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_X12; /* step (k)(6) */
|
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("C40->");
|
2021-02-23 13:01:15 +13:00
|
|
|
return DM_C40; /* step (k)(7) */
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Copy C40/TEXT/X12 triplets from buffer to target. Returns elements left in buffer (< 3) */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_ctx_buffer_xfer(int process_buffer[8], int process_p, unsigned char target[], int *p_tp,
|
2021-10-31 10:00:31 +13:00
|
|
|
const int debug_print) {
|
2021-02-23 13:01:15 +13:00
|
|
|
int i, process_e;
|
|
|
|
int tp = *p_tp;
|
|
|
|
|
|
|
|
process_e = (process_p / 3) * 3;
|
|
|
|
|
|
|
|
for (i = 0; i < process_e; i += 3) {
|
|
|
|
int iv = (1600 * process_buffer[i]) + (40 * process_buffer[i + 1]) + (process_buffer[i + 2]) + 1;
|
|
|
|
target[tp++] = (unsigned char) (iv >> 8);
|
|
|
|
target[tp++] = (unsigned char) (iv & 0xFF);
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2021-02-23 13:01:15 +13:00
|
|
|
printf("[%d %d %d (%d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
|
|
|
|
target[tp - 2], target[tp - 1]);
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
process_p -= process_e;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
if (process_p) {
|
|
|
|
memmove(process_buffer, process_buffer + process_e, sizeof(int) * process_p);
|
|
|
|
}
|
|
|
|
|
|
|
|
*p_tp = tp;
|
|
|
|
|
|
|
|
return process_p;
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Copy EDIFACT quadruplets from buffer to target. Returns elements left in buffer (< 4) */
|
2021-11-09 02:05:37 +13:00
|
|
|
static int dm_edi_buffer_xfer(int process_buffer[8], int process_p, unsigned char target[], int *p_tp,
|
|
|
|
const int empty, const int debug_print) {
|
2021-02-23 13:01:15 +13:00
|
|
|
int i, process_e;
|
|
|
|
int tp = *p_tp;
|
|
|
|
|
|
|
|
process_e = (process_p / 4) * 4;
|
|
|
|
|
|
|
|
for (i = 0; i < process_e; i += 4) {
|
|
|
|
target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
|
|
|
|
target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0f) << 4 | (process_buffer[i + 2] & 0x3c) >> 2);
|
|
|
|
target[tp++] = (unsigned char) ((process_buffer[i + 2] & 0x03) << 6 | process_buffer[i + 3]);
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2021-02-23 13:01:15 +13:00
|
|
|
printf("[%d %d %d %d (%d %d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
|
|
|
|
process_buffer[i + 3], target[tp - 3], target[tp - 2], target[tp - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process_p -= process_e;
|
|
|
|
|
|
|
|
if (process_p) {
|
|
|
|
memmove(process_buffer, process_buffer + process_e, sizeof(int) * process_p);
|
2021-11-09 02:05:37 +13:00
|
|
|
if (empty) {
|
|
|
|
if (process_p == 3) {
|
|
|
|
target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
|
|
|
|
target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0f) << 4
|
|
|
|
| (process_buffer[i + 2] & 0x3c) >> 2);
|
|
|
|
target[tp++] = (unsigned char) ((process_buffer[i + 2] & 0x03) << 6);
|
|
|
|
if (debug_print) {
|
|
|
|
printf("[%d %d %d (%d %d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
|
|
|
|
target[tp - 3], target[tp - 2], target[tp - 1]);
|
|
|
|
}
|
|
|
|
} else if (process_p == 2) {
|
|
|
|
target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
|
|
|
|
target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0f) << 4);
|
|
|
|
if (debug_print) {
|
|
|
|
printf("[%d %d (%d %d)] ", process_buffer[i], process_buffer[i + 1], target[tp - 2],
|
|
|
|
target[tp - 1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target[tp++] = (unsigned char) (process_buffer[i] << 2);
|
2021-11-15 01:09:51 +13:00
|
|
|
if (debug_print) printf("[%d (%d)] ", process_buffer[i], target[tp - 1]);
|
2021-11-09 02:05:37 +13:00
|
|
|
}
|
|
|
|
process_p = 0;
|
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
*p_tp = tp;
|
|
|
|
|
|
|
|
return process_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get symbol size, as specified or else smallest containing `minimum` codewords */
|
2021-11-15 01:09:51 +13:00
|
|
|
STATIC_UNLESS_ZINT_TEST int dm_get_symbolsize(struct zint_symbol *symbol, const int minimum) {
|
2021-02-23 13:01:15 +13:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((symbol->option_2 >= 1) && (symbol->option_2 <= DMSIZESCOUNT)) {
|
2021-11-09 02:05:37 +13:00
|
|
|
return dm_intsymbol[symbol->option_2 - 1];
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
for (i = DMSIZESCOUNT - 2; i >= 0; i--) {
|
2021-11-09 02:05:37 +13:00
|
|
|
if (minimum > dm_matrixbytes[i]) {
|
2021-02-23 13:01:15 +13:00
|
|
|
if (symbol->option_3 == DM_DMRE) {
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
if (symbol->option_3 == DM_SQUARE) {
|
|
|
|
/* Skip rectangular symbols in square only mode */
|
2021-11-09 02:05:37 +13:00
|
|
|
while (i + 1 < DMSIZESCOUNT && dm_matrixH[i + 1] != dm_matrixW[i + 1]) {
|
2021-02-23 13:01:15 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i + 1 < DMSIZESCOUNT ? i + 1 : 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Skip DMRE symbols in no dmre mode */
|
2021-11-09 02:05:37 +13:00
|
|
|
while (i + 1 < DMSIZESCOUNT && dm_isDMRE[i + 1]) {
|
2021-02-23 13:01:15 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i + 1 < DMSIZESCOUNT ? i + 1 : 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of codewords remaining in a particular version (may be negative) */
|
2021-11-15 01:09:51 +13:00
|
|
|
STATIC_UNLESS_ZINT_TEST int dm_codewords_remaining(struct zint_symbol *symbol, const int tp, const int process_p) {
|
2021-11-09 02:05:37 +13:00
|
|
|
int symbolsize = dm_get_symbolsize(symbol, tp + process_p); /* Allow for the remaining data characters */
|
2021-02-23 13:01:15 +13:00
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
return dm_matrixbytes[symbolsize] - tp;
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Number of C40/TEXT elements needed to encode `input` */
|
2021-11-15 01:09:51 +13:00
|
|
|
STATIC_UNLESS_ZINT_TEST int dm_c40text_cnt(const int current_mode, const int gs1, unsigned char input) {
|
2021-02-23 13:01:15 +13:00
|
|
|
int cnt;
|
|
|
|
|
|
|
|
if (gs1 && input == '[') {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
cnt = 1;
|
2021-06-28 02:58:27 +12:00
|
|
|
if (input & 0x80) {
|
2021-02-23 13:01:15 +13:00
|
|
|
cnt += 2;
|
|
|
|
input = input - 128;
|
|
|
|
}
|
2021-11-09 02:05:37 +13:00
|
|
|
if ((current_mode == DM_C40 && dm_c40_shift[input]) || (current_mode == DM_TEXT && dm_text_shift[input])) {
|
2021-02-23 13:01:15 +13:00
|
|
|
cnt += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* Update Base 256 field length */
|
2021-11-15 01:09:51 +13:00
|
|
|
STATIC_UNLESS_ZINT_TEST int dm_update_b256_field_length(unsigned char target[], int tp, int b256_start) {
|
2021-02-23 13:01:15 +13:00
|
|
|
int b256_count = tp - (b256_start + 1);
|
|
|
|
if (b256_count <= 249) {
|
|
|
|
target[b256_start] = b256_count;
|
|
|
|
} else {
|
|
|
|
/* Insert extra codeword */
|
|
|
|
memmove(target + b256_start + 2, target + b256_start + 1, b256_count);
|
|
|
|
target[b256_start] = (unsigned char) (249 + (b256_count / 250));
|
|
|
|
target[b256_start + 1] = (unsigned char) (b256_count % 250);
|
|
|
|
tp++;
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
return tp;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-17 07:37:51 +13:00
|
|
|
/* Switch from ASCII or Base 256 to another mode */
|
|
|
|
STATIC_UNLESS_ZINT_TEST int dm_switch_mode(const int next_mode, unsigned char target[], int tp, int *b256_start,
|
|
|
|
const int debug_print) {
|
|
|
|
switch (next_mode) {
|
|
|
|
case DM_ASCII:
|
|
|
|
if (debug_print) printf("ASC ");
|
|
|
|
break;
|
|
|
|
case DM_C40: target[tp++] = 230;
|
|
|
|
if (debug_print) printf("C40 ");
|
|
|
|
break;
|
|
|
|
case DM_TEXT: target[tp++] = 239;
|
|
|
|
if (debug_print) printf("TEX ");
|
|
|
|
break;
|
|
|
|
case DM_X12: target[tp++] = 238;
|
|
|
|
if (debug_print) printf("X12 ");
|
|
|
|
break;
|
|
|
|
case DM_EDIFACT: target[tp++] = 240;
|
|
|
|
if (debug_print) printf("EDI ");
|
|
|
|
break;
|
|
|
|
case DM_BASE256: target[tp++] = 231;
|
|
|
|
*b256_start = tp;
|
|
|
|
target[tp++] = 0; /* Byte count holder (may be expanded to 2 codewords) */
|
|
|
|
if (debug_print) printf("BAS ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
/* Encodes data using ASCII, C40, Text, X12, EDIFACT or Base 256 modes as appropriate
|
|
|
|
Supports encoding FNC1 in supporting systems */
|
2020-10-27 01:21:43 +13:00
|
|
|
static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], unsigned char target[],
|
2021-11-15 01:09:51 +13:00
|
|
|
int *p_length, int *p_binlen) {
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2020-12-23 23:57:24 +13:00
|
|
|
int sp;
|
2019-10-07 05:39:54 +13:00
|
|
|
int tp, i, gs1;
|
|
|
|
int current_mode, next_mode;
|
2021-11-15 01:09:51 +13:00
|
|
|
int not_first = 0;
|
2021-02-23 13:01:15 +13:00
|
|
|
int inputlen = *p_length;
|
|
|
|
int process_buffer[8]; /* holds remaining data to finalised */
|
|
|
|
int process_p = 0; /* number of characters left to finalise */
|
|
|
|
int b256_start = 0;
|
|
|
|
int symbols_left;
|
2021-10-31 10:00:31 +13:00
|
|
|
const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
sp = 0;
|
|
|
|
tp = 0;
|
|
|
|
|
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-29 09:42:44 +13:00
|
|
|
if (symbol->structapp.count) {
|
|
|
|
int id1, id2;
|
|
|
|
|
|
|
|
if (symbol->structapp.count < 2 || symbol->structapp.count > 16) {
|
|
|
|
strcpy(symbol->errtxt, "720: Structured Append count out of range (2-16)");
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
if (symbol->structapp.index < 1 || symbol->structapp.index > symbol->structapp.count) {
|
|
|
|
sprintf(symbol->errtxt, "721: Structured Append index out of range (1-%d)", symbol->structapp.count);
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
if (symbol->structapp.id[0]) {
|
|
|
|
int id, id_len, id1_err, id2_err;
|
|
|
|
|
|
|
|
for (id_len = 0; id_len < 32 && symbol->structapp.id[id_len]; id_len++);
|
|
|
|
|
|
|
|
if (id_len > 6) { /* ID1 * 1000 + ID2 */
|
|
|
|
strcpy(symbol->errtxt, "722: Structured Append ID too long (6 digit maximum)");
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = to_int((const unsigned char *) symbol->structapp.id, id_len);
|
|
|
|
if (id == -1) {
|
|
|
|
strcpy(symbol->errtxt, "723: Invalid Structured Append ID (digits only)");
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
id1 = id / 1000;
|
|
|
|
id2 = id % 1000;
|
|
|
|
id1_err = id1 < 1 || id1 > 254;
|
|
|
|
id2_err = id2 < 1 || id2 > 254;
|
|
|
|
if (id1_err || id2_err) {
|
|
|
|
if (id1_err && id2_err) {
|
|
|
|
sprintf(symbol->errtxt,
|
|
|
|
"724: Structured Append ID1 '%03d' and ID2 '%03d' out of range (001-254) (ID '%03d%03d')",
|
|
|
|
id1, id2, id1, id2);
|
|
|
|
} else if (id1_err) {
|
|
|
|
sprintf(symbol->errtxt,
|
|
|
|
"725: Structured Append ID1 '%03d' out of range (001-254) (ID '%03d%03d')",
|
|
|
|
id1, id1, id2);
|
|
|
|
} else {
|
|
|
|
sprintf(symbol->errtxt,
|
|
|
|
"726: Structured Append ID2 '%03d' out of range (001-254) (ID '%03d%03d')",
|
|
|
|
id2, id1, id2);
|
|
|
|
}
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
id1 = id2 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
target[tp++] = 233;
|
|
|
|
target[tp++] = (17 - symbol->structapp.count) | ((symbol->structapp.index - 1) << 4);
|
|
|
|
target[tp++] = id1;
|
|
|
|
target[tp++] = id2;
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
/* gs1 flag values: 0: no gs1, 1: gs1 with FNC1 serparator, 2: GS separator */
|
2019-11-28 05:16:14 +13:00
|
|
|
if ((symbol->input_mode & 0x07) == GS1_MODE) {
|
2019-10-07 05:39:54 +13:00
|
|
|
if (symbol->output_options & GS1_GS_SEPARATOR) {
|
|
|
|
gs1 = 2;
|
|
|
|
} else {
|
|
|
|
gs1 = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gs1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gs1) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 232;
|
|
|
|
if (debug_print) printf("FN1 ");
|
2019-10-07 05:39:54 +13:00
|
|
|
} /* FNC1 */
|
|
|
|
|
|
|
|
if (symbol->output_options & READER_INIT) {
|
|
|
|
if (gs1) {
|
2020-06-05 05:45:25 +12:00
|
|
|
strcpy(symbol->errtxt, "521: Cannot encode in GS1 mode and Reader Initialisation at the same time");
|
2019-10-07 05:39:54 +13:00
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-29 09:42:44 +13:00
|
|
|
if (symbol->structapp.count) {
|
|
|
|
strcpy(symbol->errtxt, "727: Cannot have Structured Append and Reader Initialisation at the same time");
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 234; /* Reader Programming */
|
|
|
|
if (debug_print) printf("RP ");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (symbol->eci > 0) {
|
|
|
|
/* Encode ECI numbers according to Table 6 */
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 241; /* ECI Character */
|
2019-10-07 05:39:54 +13:00
|
|
|
if (symbol->eci <= 126) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = (unsigned char) (symbol->eci + 1);
|
2020-12-23 23:57:24 +13:00
|
|
|
} else if (symbol->eci <= 16382) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = (unsigned char) ((symbol->eci - 127) / 254 + 128);
|
|
|
|
target[tp++] = (unsigned char) ((symbol->eci - 127) % 254 + 1);
|
2020-12-23 23:57:24 +13:00
|
|
|
} else {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = (unsigned char) ((symbol->eci - 16383) / 64516 + 192);
|
|
|
|
target[tp++] = (unsigned char) (((symbol->eci - 16383) / 254) % 254 + 1);
|
|
|
|
target[tp++] = (unsigned char) ((symbol->eci - 16383) % 254 + 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ECI %d ", symbol->eci + 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for Macro05/Macro06 */
|
|
|
|
/* "[)>[RS]05[GS]...[RS][EOT]" -> CW 236 */
|
|
|
|
/* "[)>[RS]06[GS]...[RS][EOT]" -> CW 237 */
|
|
|
|
if (tp == 0 && sp == 0 && inputlen >= 9
|
|
|
|
&& source[0] == '[' && source[1] == ')' && source[2] == '>'
|
|
|
|
&& source[3] == '\x1e' && source[4] == '0'
|
|
|
|
&& (source[5] == '5' || source[5] == '6')
|
|
|
|
&& source[6] == '\x1d'
|
|
|
|
&& source[inputlen - 2] == '\x1e' && source[inputlen - 1] == '\x04') {
|
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-29 09:42:44 +13:00
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
/* Output macro Codeword */
|
|
|
|
if (source[5] == '5') {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 236;
|
|
|
|
if (debug_print) printf("Macro05 ");
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 237;
|
|
|
|
if (debug_print) printf("Macro06 ");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
/* Remove macro characters from input string */
|
|
|
|
sp = 7;
|
|
|
|
inputlen -= 2;
|
2021-02-23 13:01:15 +13:00
|
|
|
*p_length -= 2;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-29 09:42:44 +13:00
|
|
|
/* step (a) */
|
|
|
|
current_mode = DM_ASCII;
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
while (sp < inputlen) {
|
|
|
|
|
|
|
|
current_mode = next_mode;
|
|
|
|
|
|
|
|
/* step (b) - ASCII encodation */
|
|
|
|
if (current_mode == DM_ASCII) {
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
|
2021-10-21 11:05:30 +13:00
|
|
|
if (is_twodigits(source, inputlen, sp)) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = (unsigned char) ((10 * ctoi(source[sp])) + ctoi(source[sp + 1]) + 130);
|
|
|
|
if (debug_print) printf("N%02d ", target[tp - 1] - 130);
|
2019-10-07 05:39:54 +13:00
|
|
|
sp += 2;
|
|
|
|
} else {
|
2021-11-15 01:09:51 +13:00
|
|
|
next_mode = dm_look_ahead_test(source, inputlen, sp, current_mode, 0, gs1, debug_print);
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
if (next_mode != DM_ASCII) {
|
2021-11-17 07:37:51 +13:00
|
|
|
tp = dm_switch_mode(next_mode, target, tp, &b256_start, debug_print);
|
2021-11-15 01:09:51 +13:00
|
|
|
not_first = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-06-28 02:58:27 +12:00
|
|
|
if (source[sp] & 0x80) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 235; /* FNC4 */
|
|
|
|
target[tp++] = (source[sp] - 128) + 1;
|
|
|
|
if (debug_print) printf("FN4 A%02X ", target[tp - 1] - 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
|
|
|
if (gs1 && (source[sp] == '[')) {
|
2021-02-23 13:01:15 +13:00
|
|
|
if (gs1 == 2) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 29 + 1; /* GS */
|
|
|
|
if (debug_print) printf("GS ");
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 232; /* FNC1 */
|
|
|
|
if (debug_print) printf("FN1 ");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = source[sp] + 1;
|
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
/* step (c)/(d) C40/TEXT encodation */
|
|
|
|
} else if (current_mode == DM_C40 || current_mode == DM_TEXT) {
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
next_mode = current_mode;
|
2021-11-15 01:09:51 +13:00
|
|
|
if (process_p == 0 && not_first) {
|
|
|
|
next_mode = dm_look_ahead_test(source, inputlen, sp, current_mode, process_p, gs1, debug_print);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
if (next_mode != current_mode) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 254; /* Unlatch */
|
2019-10-07 05:39:54 +13:00
|
|
|
next_mode = DM_ASCII;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ASC ");
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
|
|
|
int shift_set, value;
|
2021-02-23 13:01:15 +13:00
|
|
|
const char *ct_shift, *ct_value;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
if (current_mode == DM_C40) {
|
2021-11-09 02:05:37 +13:00
|
|
|
ct_shift = dm_c40_shift;
|
|
|
|
ct_value = dm_c40_value;
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-11-09 02:05:37 +13:00
|
|
|
ct_shift = dm_text_shift;
|
|
|
|
ct_value = dm_text_value;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-06-28 02:58:27 +12:00
|
|
|
if (source[sp] & 0x80) {
|
2021-02-23 13:01:15 +13:00
|
|
|
process_buffer[process_p++] = 1;
|
|
|
|
process_buffer[process_p++] = 30; /* Upper Shift */
|
|
|
|
shift_set = ct_shift[source[sp] - 128];
|
|
|
|
value = ct_value[source[sp] - 128];
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2020-10-27 01:21:43 +13:00
|
|
|
if (gs1 && (source[sp] == '[')) {
|
|
|
|
if (gs1 == 2) {
|
2021-02-23 13:01:15 +13:00
|
|
|
shift_set = ct_shift[29];
|
|
|
|
value = ct_value[29]; /* GS */
|
2020-10-27 01:21:43 +13:00
|
|
|
} else {
|
|
|
|
shift_set = 2;
|
|
|
|
value = 27; /* FNC1 */
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-02-23 13:01:15 +13:00
|
|
|
shift_set = ct_shift[source[sp]];
|
|
|
|
value = ct_value[source[sp]];
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift_set != 0) {
|
2021-02-23 13:01:15 +13:00
|
|
|
process_buffer[process_p++] = shift_set - 1;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
process_buffer[process_p++] = value;
|
|
|
|
|
|
|
|
if (process_p >= 3) {
|
2021-11-09 02:05:37 +13:00
|
|
|
process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
sp++;
|
2021-11-15 01:09:51 +13:00
|
|
|
not_first = 1;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* step (e) X12 encodation */
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (current_mode == DM_X12) {
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (!dm_isX12(source[sp])) {
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
} else {
|
2021-11-09 02:05:37 +13:00
|
|
|
next_mode = DM_X12;
|
2021-11-15 01:09:51 +13:00
|
|
|
if (process_p == 0 && not_first) {
|
|
|
|
next_mode = dm_look_ahead_test(source, inputlen, sp, current_mode, process_p, gs1, debug_print);
|
2021-11-09 02:05:37 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (next_mode != DM_X12) {
|
|
|
|
process_p = 0; /* Throw away buffer if any */
|
|
|
|
target[tp++] = 254; /* Unlatch */
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
if (debug_print) printf("ASC ");
|
|
|
|
} else {
|
|
|
|
static const char x12_nonalphanum_chars[] = "\015*> ";
|
|
|
|
int value = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if ((source[sp] <= '9') && (source[sp] >= '0')) {
|
|
|
|
value = (source[sp] - '0') + 4;
|
|
|
|
} else if ((source[sp] >= 'A') && (source[sp] <= 'Z')) {
|
|
|
|
value = (source[sp] - 'A') + 14;
|
|
|
|
} else {
|
|
|
|
value = posn(x12_nonalphanum_chars, source[sp]);
|
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
process_buffer[process_p++] = value;
|
2021-11-09 02:05:37 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (process_p >= 3) {
|
|
|
|
process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
sp++;
|
|
|
|
not_first = 1;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* step (f) EDIFACT encodation */
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (current_mode == DM_EDIFACT) {
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (!dm_isedifact(source[sp], gs1)) {
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
} else {
|
2021-11-09 02:05:37 +13:00
|
|
|
next_mode = DM_EDIFACT;
|
|
|
|
if (process_p == 3) {
|
|
|
|
/* Note different then spec Step (f)(1), which suggests checking when 0, but this seems to work
|
|
|
|
better in many cases as the switch to ASCII is "free" */
|
2021-11-15 01:09:51 +13:00
|
|
|
next_mode = dm_look_ahead_test(source, inputlen, sp, current_mode, process_p, gs1, debug_print);
|
2021-11-09 02:05:37 +13:00
|
|
|
}
|
2021-11-15 01:09:51 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (next_mode != DM_EDIFACT) {
|
|
|
|
process_buffer[process_p++] = 31;
|
|
|
|
process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 1 /*empty*/, debug_print);
|
|
|
|
next_mode = DM_ASCII;
|
|
|
|
if (debug_print) printf("ASC ");
|
|
|
|
} else {
|
|
|
|
int value = source[sp];
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (value >= 64) { // '@'
|
|
|
|
value -= 64;
|
2021-11-09 02:05:37 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
process_buffer[process_p++] = value;
|
|
|
|
sp++;
|
|
|
|
not_first = 1;
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
if (process_p >= 4) {
|
|
|
|
process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 0 /*empty*/, debug_print);
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* step (g) Base 256 encodation */
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (current_mode == DM_BASE256) {
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-15 01:09:51 +13:00
|
|
|
if (gs1 == 1 && source[sp] == '[') {
|
|
|
|
next_mode = DM_ASCII;
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-11-15 01:09:51 +13:00
|
|
|
next_mode = DM_BASE256;
|
|
|
|
if (not_first) {
|
|
|
|
next_mode = dm_look_ahead_test(source, inputlen, sp, current_mode, tp - (b256_start + 1), gs1,
|
|
|
|
debug_print);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next_mode != DM_BASE256) {
|
2021-11-09 02:05:37 +13:00
|
|
|
tp = dm_update_b256_field_length(target, tp, b256_start);
|
2021-02-23 13:01:15 +13:00
|
|
|
/* B.2.1 255-state randomising algorithm */
|
|
|
|
for (i = b256_start; i < tp; i++) {
|
2021-11-10 02:43:56 +13:00
|
|
|
const int prn = ((149 * (i + 1)) % 255) + 1;
|
2021-02-23 13:01:15 +13:00
|
|
|
target[i] = (unsigned char) ((target[i] + prn) & 0xFF);
|
|
|
|
}
|
2021-11-17 07:37:51 +13:00
|
|
|
/* We switch directly here to avoid flipping back to Base 256 due to `dm_text_sp_cnt()` */
|
|
|
|
tp = dm_switch_mode(next_mode, target, tp, &b256_start, debug_print);
|
|
|
|
not_first = 0;
|
2021-11-15 01:09:51 +13:00
|
|
|
} else {
|
|
|
|
if (gs1 == 2 && source[sp] == '[') {
|
|
|
|
target[tp++] = 29; /* GS */
|
|
|
|
} else {
|
|
|
|
target[tp++] = source[sp];
|
|
|
|
}
|
|
|
|
sp++;
|
|
|
|
not_first = 1;
|
|
|
|
if (debug_print) printf("B%02X ", target[tp - 1]);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tp > 1558) {
|
2019-11-28 05:16:14 +13:00
|
|
|
strcpy(symbol->errtxt, "520: Data too long to fit in symbol");
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* while */
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
symbols_left = dm_codewords_remaining(symbol, tp, process_p);
|
2020-10-27 01:21:43 +13:00
|
|
|
|
2021-12-09 07:12:20 +13:00
|
|
|
if (debug_print) printf("\nsymbols_left %d, tp %d, process_p %d ", symbols_left, tp, process_p);
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
if (current_mode == DM_C40 || current_mode == DM_TEXT) {
|
|
|
|
/* NOTE: changed to follow spec exactly here, only using Shift 1 padded triplets when 2 symbol chars remain.
|
|
|
|
This matches the behaviour of BWIPP but not tec-it, nor figures 4.15.1-1 and 4.15-1-2 in GS1 General
|
|
|
|
Specifications 21.0.1.
|
|
|
|
*/
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("%s ", current_mode == DM_C40 ? "C40" : "TEX");
|
2021-02-23 13:01:15 +13:00
|
|
|
if (process_p == 0) {
|
|
|
|
if (symbols_left > 0) {
|
|
|
|
target[tp++] = 254; // Unlatch
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ASC ");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
|
|
|
if (process_p == 2 && symbols_left == 2) {
|
|
|
|
/* 5.2.5.2 (b) */
|
|
|
|
process_buffer[process_p++] = 0; // Shift 1
|
2021-11-09 02:05:37 +13:00
|
|
|
(void) dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
} else if (process_p == 1 && symbols_left <= 2 && dm_isc40text(current_mode, source[inputlen - 1])) {
|
2021-02-23 13:01:15 +13:00
|
|
|
/* 5.2.5.2 (c)/(d) */
|
2019-10-07 05:39:54 +13:00
|
|
|
if (symbols_left > 1) {
|
2021-02-23 13:01:15 +13:00
|
|
|
/* 5.2.5.2 (c) */
|
|
|
|
target[tp++] = 254; // Unlatch and encode remaining data in ascii.
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ASC ");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-02-23 13:01:15 +13:00
|
|
|
int cnt, total_cnt = 0;
|
|
|
|
/* Backtrack to last complete triplet (same technique as BWIPP) */
|
|
|
|
while (sp > 0 && process_p % 3) {
|
|
|
|
sp--;
|
2021-11-09 02:05:37 +13:00
|
|
|
cnt = dm_c40text_cnt(current_mode, gs1, source[sp]);
|
2021-02-23 13:01:15 +13:00
|
|
|
total_cnt += cnt;
|
|
|
|
process_p -= cnt;
|
|
|
|
}
|
|
|
|
tp -= (total_cnt / 3) * 2;
|
|
|
|
|
|
|
|
target[tp++] = 254; // Unlatch
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ASC ");
|
2021-02-23 13:01:15 +13:00
|
|
|
for (; sp < inputlen; sp++) {
|
2021-10-21 11:05:30 +13:00
|
|
|
if (is_twodigits(source, inputlen, sp)) {
|
2021-02-23 13:01:15 +13:00
|
|
|
target[tp++] = (unsigned char) ((10 * ctoi(source[sp])) + ctoi(source[sp + 1]) + 130);
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("N%02d ", target[tp - 1] - 130);
|
2021-02-23 13:01:15 +13:00
|
|
|
sp++;
|
2021-06-28 02:58:27 +12:00
|
|
|
} else if (source[sp] & 0x80) {
|
2021-02-23 13:01:15 +13:00
|
|
|
target[tp++] = 235; /* FNC4 */
|
|
|
|
target[tp++] = (source[sp] - 128) + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("FN4 A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (gs1 && source[sp] == '[') {
|
|
|
|
if (gs1 == 2) {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 29 + 1; /* GS */
|
|
|
|
if (debug_print) printf("GS ");
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
2021-10-31 10:00:31 +13:00
|
|
|
target[tp++] = 232; /* FNC1 */
|
|
|
|
if (debug_print) printf("FN1 ");
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target[tp++] = source[sp] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (current_mode == DM_X12) {
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("X12 ");
|
2021-02-23 13:01:15 +13:00
|
|
|
if ((symbols_left == 1) && (process_p == 1)) {
|
|
|
|
// Unlatch not required!
|
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
|
|
|
if (symbols_left > 0) {
|
|
|
|
target[tp++] = (254); // Unlatch.
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("ASC ");
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
if (process_p == 1) {
|
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (process_p == 2) {
|
|
|
|
target[tp++] = source[inputlen - 2] + 1;
|
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X A%02X ", target[tp - 2] - 1, target[tp - 1] - 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (current_mode == DM_EDIFACT) {
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("EDI ");
|
2021-02-23 13:01:15 +13:00
|
|
|
if (symbols_left <= 2 && process_p <= symbols_left) { // Unlatch not required!
|
|
|
|
if (process_p == 1) {
|
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X ", target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
} else if (process_p == 2) {
|
|
|
|
target[tp++] = source[inputlen - 2] + 1;
|
|
|
|
target[tp++] = source[inputlen - 1] + 1;
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) printf("A%02X A%02X ", target[tp - 2] - 1, target[tp - 1] - 1);
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Append edifact unlatch value (31) and empty buffer
|
|
|
|
if (process_p <= 3) {
|
|
|
|
process_buffer[process_p++] = 31;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-09 02:05:37 +13:00
|
|
|
(void) dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 1 /*empty*/, debug_print);
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (current_mode == DM_BASE256) {
|
|
|
|
if (symbols_left > 0) {
|
2021-11-09 02:05:37 +13:00
|
|
|
tp = dm_update_b256_field_length(target, tp, b256_start);
|
2021-02-23 13:01:15 +13:00
|
|
|
}
|
|
|
|
/* B.2.1 255-state randomising algorithm */
|
|
|
|
for (i = b256_start; i < tp; i++) {
|
|
|
|
int prn = ((149 * (i + 1)) % 255) + 1;
|
|
|
|
target[i] = (unsigned char) ((target[i] + prn) & 0xFF);
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2021-02-23 13:01:15 +13:00
|
|
|
printf("\nData (%d): ", tp);
|
|
|
|
for (i = 0; i < tp; i++)
|
2020-10-27 01:21:43 +13:00
|
|
|
printf("%d ", target[i]);
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:01:15 +13:00
|
|
|
*p_binlen = tp;
|
|
|
|
|
|
|
|
return 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add pad bits */
|
2021-11-09 02:05:37 +13:00
|
|
|
static void dm_add_tail(unsigned char target[], int tp, const int tail_length) {
|
2019-10-07 05:39:54 +13:00
|
|
|
int i, prn, temp;
|
|
|
|
|
2021-12-09 07:12:20 +13:00
|
|
|
target[tp++] = 129; /* Pad */
|
|
|
|
for (i = 1; i < tail_length; i++) {
|
|
|
|
/* B.1.1 253-state randomising algorithm */
|
|
|
|
prn = ((149 * (tp + 1)) % 253) + 1;
|
|
|
|
temp = 129 + prn;
|
|
|
|
if (temp <= 254) {
|
|
|
|
target[tp++] = (unsigned char) (temp);
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
2021-12-09 07:12:20 +13:00
|
|
|
target[tp++] = (unsigned char) (temp - 254);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add compliant height, using ZINT_COMPLIANT_HEIGHT flag for back-compatibility
Rename barcode funcs to same as BARCODE_XXX name
library: barcode funcs array for dispatch, used for ZBarcode_ValidID() also
general: change is_sane() comparison to nonzero from ZINT_ERROR_INVALID_OPTION
MAILMARK: fuller error messages
CODABAR: add option to show check character in HRT
zint.h: use 0xNNNN for OR-able defines
GUI: add guard descent height reset button, add Zint version to window title,
static get_zint_version() method, use QStringLiteral (QSL shorthand),
use SIGNAL(toggled()), add errtxt "popup" and status bar, add icons,
add saveAs shortcut, add main menu, context menus and actions, add help,
reset_view() -> reset_colours(), add copy to clipboard as EMF/GIF/PNG/TIF,
lessen triggering of update_preview(), shorten names of getters/setters,
simplify/shorten some update_preview() logic in switch,
CODEONE disable structapp for Version S
qzint.cpp: add on_errored signal, add missing getters, add test
2021-10-09 12:13:39 +13:00
|
|
|
static int datamatrix_200(struct zint_symbol *symbol, const unsigned char source[], int inputlen) {
|
2019-11-28 05:16:14 +13:00
|
|
|
int i, skew = 0;
|
2019-10-07 05:39:54 +13:00
|
|
|
unsigned char binary[2200];
|
|
|
|
int binlen;
|
2021-02-23 13:01:15 +13:00
|
|
|
int symbolsize;
|
Add Structured Append support for AZTEC, CODEONE, DATAMATRIX, DOTCODE,
GRIDMATRIX, MAXICODE, MICROPDF417, PDF417, QRCODE, ULTRA
DOTCODE: use pre-calculated generator poly coeffs in Reed-Solomon for
performance improvement
PDF417/MICROPDF417: use common routine pdf417_initial()
GUI: code lines <= 118, shorthand widget_obj(),
shorten calling upcean_addon_gap(), upcean_guard_descent()
various backend: var name debug -> debug_print
2021-09-29 09:42:44 +13:00
|
|
|
int taillength, error_number;
|
2019-10-07 05:39:54 +13:00
|
|
|
int H, W, FH, FW, datablock, bytes, rsblock;
|
2021-10-31 10:00:31 +13:00
|
|
|
const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
/* inputlen may be decremented by 2 if macro character is used */
|
2021-02-23 13:01:15 +13:00
|
|
|
error_number = dm200encode(symbol, source, binary, &inputlen, &binlen);
|
2019-11-28 05:16:14 +13:00
|
|
|
if (error_number != 0) {
|
|
|
|
return error_number;
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
symbolsize = dm_get_symbolsize(symbol, binlen);
|
2019-10-07 05:39:54 +13:00
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
if (binlen > dm_matrixbytes[symbolsize]) {
|
2021-02-23 13:01:15 +13:00
|
|
|
if ((symbol->option_2 >= 1) && (symbol->option_2 <= DMSIZESCOUNT)) {
|
|
|
|
// The symbol size was given by --ver (option_2)
|
2019-10-07 05:39:54 +13:00
|
|
|
strcpy(symbol->errtxt, "522: Input too long for selected symbol size");
|
2021-02-23 13:01:15 +13:00
|
|
|
} else {
|
|
|
|
strcpy(symbol->errtxt, "523: Data too long to fit in symbol");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:05:37 +13:00
|
|
|
H = dm_matrixH[symbolsize];
|
|
|
|
W = dm_matrixW[symbolsize];
|
|
|
|
FH = dm_matrixFH[symbolsize];
|
|
|
|
FW = dm_matrixFW[symbolsize];
|
|
|
|
bytes = dm_matrixbytes[symbolsize];
|
|
|
|
datablock = dm_matrixdatablock[symbolsize];
|
|
|
|
rsblock = dm_matrixrsblock[symbolsize];
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
taillength = bytes - binlen;
|
|
|
|
|
|
|
|
if (taillength != 0) {
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_add_tail(binary, binlen, taillength);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2020-10-27 01:21:43 +13:00
|
|
|
printf("Pads (%d): ", taillength);
|
|
|
|
for (i = binlen; i < binlen + taillength; i++) printf("%d ", binary[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
|
|
|
|
// ecc code
|
|
|
|
if (symbolsize == INTSYMBOL144) {
|
|
|
|
skew = 1;
|
|
|
|
}
|
2021-11-09 02:05:37 +13:00
|
|
|
dm_ecc(binary, bytes, datablock, rsblock, skew);
|
2021-10-31 10:00:31 +13:00
|
|
|
if (debug_print) {
|
2020-10-27 01:21:43 +13:00
|
|
|
printf("ECC (%d): ", rsblock * (bytes / datablock));
|
|
|
|
for (i = bytes; i < bytes + rsblock * (bytes / datablock); i++) printf("%d ", binary[i]);
|
|
|
|
printf("\n");
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2020-10-27 01:21:43 +13:00
|
|
|
|
2020-09-29 07:46:23 +13:00
|
|
|
#ifdef ZINT_TEST
|
2020-12-23 23:57:24 +13:00
|
|
|
if (symbol->debug & ZINT_DEBUG_TEST) {
|
|
|
|
debug_test_codeword_dump(symbol, binary, skew ? 1558 + 620 : bytes + rsblock * (bytes / datablock));
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
#endif
|
|
|
|
{ // placement
|
2021-11-15 01:09:51 +13:00
|
|
|
const int NC = W - 2 * (W / FW);
|
|
|
|
const int NR = H - 2 * (H / FH);
|
|
|
|
int x, y, *places;
|
2021-11-09 02:05:37 +13:00
|
|
|
if (!(places = (int *) calloc(NC * NR, sizeof(int)))) {
|
|
|
|
strcpy(symbol->errtxt, "718: Insufficient memory for placement array");
|
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
dm_placement(places, NR, NC);
|
2019-10-07 05:39:54 +13:00
|
|
|
for (y = 0; y < H; y += FH) {
|
|
|
|
for (x = 0; x < W; x++)
|
2021-11-10 02:43:56 +13:00
|
|
|
set_module(symbol, (H - y) - 1, x);
|
2019-10-07 05:39:54 +13:00
|
|
|
for (x = 0; x < W; x += 2)
|
2021-11-10 02:43:56 +13:00
|
|
|
set_module(symbol, y, x);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
for (x = 0; x < W; x += FW) {
|
|
|
|
for (y = 0; y < H; y++)
|
2021-11-10 02:43:56 +13:00
|
|
|
set_module(symbol, (H - y) - 1, x);
|
2019-10-07 05:39:54 +13:00
|
|
|
for (y = 0; y < H; y += 2)
|
2021-11-10 02:43:56 +13:00
|
|
|
set_module(symbol, (H - y) - 1, x + FW - 1);
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
2021-11-10 02:43:56 +13:00
|
|
|
#ifdef DM_DEBUG
|
2019-10-07 05:39:54 +13:00
|
|
|
// Print position matrix as in standard
|
|
|
|
for (y = NR - 1; y >= 0; y--) {
|
|
|
|
for (x = 0; x < NC; x++) {
|
2021-11-10 02:43:56 +13:00
|
|
|
const int v = places[(NR - y - 1) * NC + x];
|
|
|
|
if (x != 0) fprintf(stderr, "|");
|
2019-10-07 05:39:54 +13:00
|
|
|
fprintf(stderr, "%3d.%2d", (v >> 3), 8 - (v & 7));
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (y = 0; y < NR; y++) {
|
|
|
|
for (x = 0; x < NC; x++) {
|
2021-11-09 02:05:37 +13:00
|
|
|
const int v = places[(NR - y - 1) * NC + x];
|
2021-11-10 02:43:56 +13:00
|
|
|
if (v == 1 || (v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))))) {
|
|
|
|
set_module(symbol, H - (1 + y + 2 * (y / (FH - 2))) - 1, 1 + x + 2 * (x / (FW - 2)));
|
2019-10-07 05:39:54 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 02:43:56 +13:00
|
|
|
for (y = 0; y < H; y++) {
|
|
|
|
symbol->row_height[y] = 1;
|
|
|
|
}
|
2019-10-07 05:39:54 +13:00
|
|
|
free(places);
|
|
|
|
}
|
|
|
|
|
2021-06-20 00:11:23 +12:00
|
|
|
symbol->height = H;
|
2019-10-07 05:39:54 +13:00
|
|
|
symbol->rows = H;
|
|
|
|
symbol->width = W;
|
|
|
|
|
|
|
|
return error_number;
|
|
|
|
}
|
|
|
|
|
Add compliant height, using ZINT_COMPLIANT_HEIGHT flag for back-compatibility
Rename barcode funcs to same as BARCODE_XXX name
library: barcode funcs array for dispatch, used for ZBarcode_ValidID() also
general: change is_sane() comparison to nonzero from ZINT_ERROR_INVALID_OPTION
MAILMARK: fuller error messages
CODABAR: add option to show check character in HRT
zint.h: use 0xNNNN for OR-able defines
GUI: add guard descent height reset button, add Zint version to window title,
static get_zint_version() method, use QStringLiteral (QSL shorthand),
use SIGNAL(toggled()), add errtxt "popup" and status bar, add icons,
add saveAs shortcut, add main menu, context menus and actions, add help,
reset_view() -> reset_colours(), add copy to clipboard as EMF/GIF/PNG/TIF,
lessen triggering of update_preview(), shorten names of getters/setters,
simplify/shorten some update_preview() logic in switch,
CODEONE disable structapp for Version S
qzint.cpp: add on_errored signal, add missing getters, add test
2021-10-09 12:13:39 +13:00
|
|
|
INTERNAL int datamatrix(struct zint_symbol *symbol, unsigned char source[], int length) {
|
2019-10-07 05:39:54 +13:00
|
|
|
int error_number;
|
|
|
|
|
|
|
|
if (symbol->option_1 <= 1) {
|
|
|
|
/* ECC 200 */
|
Add compliant height, using ZINT_COMPLIANT_HEIGHT flag for back-compatibility
Rename barcode funcs to same as BARCODE_XXX name
library: barcode funcs array for dispatch, used for ZBarcode_ValidID() also
general: change is_sane() comparison to nonzero from ZINT_ERROR_INVALID_OPTION
MAILMARK: fuller error messages
CODABAR: add option to show check character in HRT
zint.h: use 0xNNNN for OR-able defines
GUI: add guard descent height reset button, add Zint version to window title,
static get_zint_version() method, use QStringLiteral (QSL shorthand),
use SIGNAL(toggled()), add errtxt "popup" and status bar, add icons,
add saveAs shortcut, add main menu, context menus and actions, add help,
reset_view() -> reset_colours(), add copy to clipboard as EMF/GIF/PNG/TIF,
lessen triggering of update_preview(), shorten names of getters/setters,
simplify/shorten some update_preview() logic in switch,
CODEONE disable structapp for Version S
qzint.cpp: add on_errored signal, add missing getters, add test
2021-10-09 12:13:39 +13:00
|
|
|
error_number = datamatrix_200(symbol, source, length);
|
2019-10-07 05:39:54 +13:00
|
|
|
} else {
|
|
|
|
/* ECC 000 - 140 */
|
|
|
|
strcpy(symbol->errtxt, "524: Older Data Matrix standards are no longer supported");
|
|
|
|
error_number = ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error_number;
|
|
|
|
}
|