1
0
mirror of https://git.code.sf.net/p/zint/code synced 2025-12-28 23:37:09 +00:00

general: prefix all INTERNAL funcs/tables with zint_, except

those in "backend/common.h", which are prefixed by `z_` - makes
  symbol clashes more unlikely when zint is statically linked
  (ticket #337, props Ulrich Becker)
DOTCODE: fix padding allowance (10 -> 52 - probable max 35) to
  cover cases with large no. of columns requested and little data,
  to prevent `codeword_array` buffer overflow
AZTEC/CODEONE: some code fiddling
general_field: prefix defines with `GF_`, shorten static funcs
  prefix `general_field_` -> `gf_`
This commit is contained in:
gitlost
2025-08-26 23:48:00 +01:00
parent e18b047a45
commit 39380d6767
106 changed files with 4477 additions and 4360 deletions

View File

@@ -482,7 +482,7 @@ static void hx_define_mode(char *mode, const unsigned int ddata[], const int len
/* Get optimal mode for each code point by tracing backwards */
for (i = length - 1; i >= 0; i--) {
j = posn(mode_types, cur_mode);
j = z_posn(mode_types, cur_mode);
cur_mode = char_modes[i][j];
mode[i] = cur_mode;
}
@@ -519,15 +519,15 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
if (eci != 0) {
/* Encoding ECI assignment number, according to Table 5 */
bp = bin_append_posn(8, 4, binary, bp); /* ECI */
bp = z_bin_append_posn(8, 4, binary, bp); /* ECI */
if (eci <= 127) {
bp = bin_append_posn(eci, 8, binary, bp);
bp = z_bin_append_posn(eci, 8, binary, bp);
} else if (eci <= 16383) {
bp = bin_append_posn(2, 2, binary, bp);
bp = bin_append_posn(eci, 14, binary, bp);
bp = z_bin_append_posn(2, 2, binary, bp);
bp = z_bin_append_posn(eci, 14, binary, bp);
} else {
bp = bin_append_posn(6, 3, binary, bp);
bp = bin_append_posn(eci, 21, binary, bp);
bp = z_bin_append_posn(6, 3, binary, bp);
bp = z_bin_append_posn(eci, 21, binary, bp);
}
}
@@ -545,7 +545,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
case 'n':
/* Numeric mode */
/* Mode indicator */
bp = bin_append_posn(1, 4, binary, bp);
bp = z_bin_append_posn(1, 4, binary, bp);
if (debug_print) {
printf("Numeric (N%d): ", block_length);
@@ -555,23 +555,23 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
i = 0;
while (i < block_length) {
const int first = ctoi((const char) ddata[position + i]);
const int first = z_ctoi((const char) ddata[position + i]);
count = 1;
encoding_value = first;
if (i + 1 < block_length && mode[position + i + 1] == 'n') {
const int second = ctoi((const char) ddata[position + i + 1]);
const int second = z_ctoi((const char) ddata[position + i + 1]);
count = 2;
encoding_value = (encoding_value * 10) + second;
if (i + 2 < block_length && mode[position + i + 2] == 'n') {
const int third = ctoi((const char) ddata[position + i + 2]);
const int third = z_ctoi((const char) ddata[position + i + 2]);
count = 3;
encoding_value = (encoding_value * 10) + third;
}
}
bp = bin_append_posn(encoding_value, 10, binary, bp);
bp = z_bin_append_posn(encoding_value, 10, binary, bp);
if (debug_print) {
printf(" 0x%3x(%d)", encoding_value, encoding_value);
@@ -583,13 +583,13 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
/* Mode terminator depends on number of characters in last group (Table 2) */
switch (count) {
case 1:
bp = bin_append_posn(1021, 10, binary, bp);
bp = z_bin_append_posn(1021, 10, binary, bp);
break;
case 2:
bp = bin_append_posn(1022, 10, binary, bp);
bp = z_bin_append_posn(1022, 10, binary, bp);
break;
case 3:
bp = bin_append_posn(1023, 10, binary, bp);
bp = z_bin_append_posn(1023, 10, binary, bp);
break;
}
@@ -601,7 +601,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
case 't':
/* Text mode */
/* Mode indicator */
bp = bin_append_posn(2, 4, binary, bp);
bp = z_bin_append_posn(2, 4, binary, bp);
if (debug_print) {
printf("Text (T%d):", block_length);
@@ -615,7 +615,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
if (hx_getsubmode(ddata[i + position]) != submode) {
/* Change submode */
bp = bin_append_posn(62, 6, binary, bp);
bp = z_bin_append_posn(62, 6, binary, bp);
submode = hx_getsubmode(ddata[i + position]);
if (debug_print) {
fputs(" SWITCH", stdout);
@@ -628,7 +628,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
encoding_value = hx_lookup_text2(ddata[i + position]);
}
bp = bin_append_posn(encoding_value, 6, binary, bp);
bp = z_bin_append_posn(encoding_value, 6, binary, bp);
if (debug_print) {
printf(" %.2x[ASC %.2x]", encoding_value, ddata[i + position]);
@@ -637,7 +637,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
}
/* Terminator */
bp = bin_append_posn(63, 6, binary, bp);
bp = z_bin_append_posn(63, 6, binary, bp);
if (debug_print) {
fputs("\n", stdout);
@@ -646,10 +646,10 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
case 'b':
/* Binary Mode */
/* Mode indicator */
bp = bin_append_posn(3, 4, binary, bp);
bp = z_bin_append_posn(3, 4, binary, bp);
/* Count indicator */
bp = bin_append_posn(block_length + double_byte, 13, binary, bp);
bp = z_bin_append_posn(block_length + double_byte, 13, binary, bp);
if (debug_print) {
printf("Binary Mode (B%d):", block_length + double_byte);
@@ -660,7 +660,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
while (i < block_length) {
/* 8-bit bytes with no conversion */
bp = bin_append_posn(ddata[i + position], ddata[i + position] > 0xFF ? 16 : 8, binary, bp);
bp = z_bin_append_posn(ddata[i + position], ddata[i + position] > 0xFF ? 16 : 8, binary, bp);
if (debug_print) {
printf(" %02x", (int) ddata[i + position]);
@@ -677,7 +677,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
/* Region One encoding */
/* Mode indicator */
if (position == 0 || mode[position - 1] != '2') { /* Unless previous mode Region Two */
bp = bin_append_posn(4, 4, binary, bp);
bp = z_bin_append_posn(4, 4, binary, bp);
}
if (debug_print) {
@@ -710,13 +710,13 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
printf(" %.3x[GB %.4x]", glyph, ddata[i + position]);
}
bp = bin_append_posn(glyph, 12, binary, bp);
bp = z_bin_append_posn(glyph, 12, binary, bp);
i++;
}
/* Terminator */
bp = bin_append_posn(position + block_length == length || mode[position + block_length] != '2'
? 4095 : 4094, 12, binary, bp);
bp = z_bin_append_posn(position + block_length == length || mode[position + block_length] != '2'
? 4095 : 4094, 12, binary, bp);
if (debug_print) {
printf(" (TERM %x)\n", position + block_length == length || mode[position + block_length] != '2'
@@ -728,7 +728,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
/* Region Two encoding */
/* Mode indicator */
if (position == 0 || mode[position - 1] != '1') { /* Unless previous mode Region One */
bp = bin_append_posn(5, 4, binary, bp);
bp = z_bin_append_posn(5, 4, binary, bp);
}
if (debug_print) {
@@ -748,13 +748,13 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
printf(" %.3x[GB %.4x]", glyph, ddata[i + position]);
}
bp = bin_append_posn(glyph, 12, binary, bp);
bp = z_bin_append_posn(glyph, 12, binary, bp);
i++;
}
/* Terminator */
bp = bin_append_posn(position + block_length == length || mode[position + block_length] != '1'
? 4095 : 4094, 12, binary, bp);
bp = z_bin_append_posn(position + block_length == length || mode[position + block_length] != '1'
? 4095 : 4094, 12, binary, bp);
if (debug_print) {
printf(" (TERM %x)\n", position + block_length == length || mode[position + block_length] != '1'
@@ -765,7 +765,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
case 'd':
/* Double byte encoding */
/* Mode indicator */
bp = bin_append_posn(6, 4, binary, bp);
bp = z_bin_append_posn(6, 4, binary, bp);
if (debug_print) {
printf("Double byte (H(d)%d):", block_length);
@@ -787,12 +787,12 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
printf("%.4x ", glyph);
}
bp = bin_append_posn(glyph, 15, binary, bp);
bp = z_bin_append_posn(glyph, 15, binary, bp);
i++;
}
/* Terminator */
bp = bin_append_posn(32767, 15, binary, bp);
bp = z_bin_append_posn(32767, 15, binary, bp);
/* Terminator sequence of length 12 is a mistake
- confirmed by Wang Yi */
@@ -811,7 +811,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
while (i < block_length) {
/* Mode indicator */
bp = bin_append_posn(7, 4, binary, bp);
bp = z_bin_append_posn(7, 4, binary, bp);
first_byte = (ddata[i + position] & 0xff00) >> 8;
second_byte = ddata[i + position] & 0xff;
@@ -825,7 +825,7 @@ static void hx_calculate_binary(char binary[], const char mode[], const unsigned
printf(" %d", glyph);
}
bp = bin_append_posn(glyph, 21, binary, bp);
bp = z_bin_append_posn(glyph, 21, binary, bp);
i += 2;
}
@@ -1123,14 +1123,14 @@ static void hx_add_ecc(unsigned char fullstream[], const unsigned char datastrea
const int table_d1_pos = ((version - 1) * 36) + ((ecc_level - 1) * 9);
rs_t rs;
rs_init_gf(&rs, 0x163); /* x^8 + x^6 + x^5 + x + 1 = 0 */
zint_rs_init_gf(&rs, 0x163); /* x^8 + x^6 + x^5 + x + 1 = 0 */
for (i = 0; i < 3; i++) {
const int batch_size = hx_table_d1[table_d1_pos + (3 * i)];
const int data_length = hx_table_d1[table_d1_pos + (3 * i) + 1];
const int ecc_length = hx_table_d1[table_d1_pos + (3 * i) + 2];
rs_init_code(&rs, ecc_length, 1);
zint_rs_init_code(&rs, ecc_length, 1);
for (block = 0; block < batch_size; block++) {
for (j = 0; j < data_length; j++) {
@@ -1139,7 +1139,7 @@ static void hx_add_ecc(unsigned char fullstream[], const unsigned char datastrea
input_position++;
}
rs_encode(&rs, data_length, data_block, ecc_block);
zint_rs_encode(&rs, data_length, data_block, ecc_block);
for (j = 0; j < ecc_length; j++) {
fullstream[output_position++] = ecc_block[j];
@@ -1159,9 +1159,9 @@ static void hx_set_function_info(unsigned char *grid, const int size, const int
/* Form function information string */
bp = bin_append_posn(version + 20, 8, function_information, bp);
bp = bin_append_posn(ecc_level - 1, 2, function_information, bp);
bp = bin_append_posn(bitmask, 2, function_information, bp);
bp = z_bin_append_posn(version + 20, 8, function_information, bp);
bp = z_bin_append_posn(ecc_level - 1, 2, function_information, bp);
bp = z_bin_append_posn(bitmask, 2, function_information, bp);
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
@@ -1171,12 +1171,12 @@ static void hx_set_function_info(unsigned char *grid, const int size, const int
}
}
rs_init_gf(&rs, 0x13);
rs_init_code(&rs, 4, 1);
rs_encode(&rs, 3, fi_cw, fi_ecc);
zint_rs_init_gf(&rs, 0x13);
zint_rs_init_code(&rs, 4, 1);
zint_rs_encode(&rs, 3, fi_cw, fi_ecc);
for (i = 0; i < 4; i++) {
bp = bin_append_posn(fi_ecc[i], 4, function_information, bp);
bp = z_bin_append_posn(fi_ecc[i], 4, function_information, bp);
}
/* Previously added alternating filler pattern here (as does BWIPP) but not mentioned in ISO/IEC 20830:2021 and
@@ -1477,7 +1477,7 @@ static int hx_apply_bitmask(unsigned char *grid, const int size, const int versi
}
/* Han Xin Code - main */
INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {
INTERNAL int zint_hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {
int warn_number = 0;
int est_binlen;
int ecc_level = symbol->option_1;
@@ -1490,7 +1490,7 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
int bin_len;
const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;
const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;
const int eci_length_segs = get_eci_length_segs(segs, seg_count);
const int eci_length_segs = zint_get_eci_length_segs(segs, seg_count);
struct zint_seg *local_segs = (struct zint_seg *) z_alloca(sizeof(struct zint_seg) * seg_count);
unsigned int *ddata = (unsigned int *) z_alloca(sizeof(unsigned int) * eci_length_segs);
char *mode = (char *) z_alloca(eci_length_segs);
@@ -1500,7 +1500,7 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
unsigned char *picket_fence;
unsigned char *grid;
segs_cpy(symbol, segs, seg_count, local_segs); /* Shallow copy (needed to set default ECI & protect lengths) */
z_segs_cpy(symbol, segs, seg_count, local_segs); /* Shallow copy (needed to set default ECI & protect lengths) */
/* If ZINT_FULL_MULTIBYTE set use Hanzi mode in DATA_MODE or for non-GB 18030 in UNICODE_MODE */
full_multibyte = (symbol->option_3 & 0xFF) == ZINT_FULL_MULTIBYTE;
@@ -1509,13 +1509,13 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
user_mask = 0; /* Ignore */
}
if (raw_text && rt_init_segs(symbol, seg_count)) {
return ZINT_ERROR_MEMORY; /* `rt_init_segs()` only fails with OOM */
if (raw_text && z_rt_init_segs(symbol, seg_count)) {
return ZINT_ERROR_MEMORY; /* `z_rt_init_segs()` only fails with OOM */
}
if ((symbol->input_mode & 0x07) == DATA_MODE) {
if (gb18030_cpy_segs(symbol, local_segs, seg_count, ddata, full_multibyte)) {
return ZINT_ERROR_MEMORY; /* `gb18030_cpy_segs()` only fails with OOM */
if (zint_gb18030_cpy_segs(symbol, local_segs, seg_count, ddata, full_multibyte)) {
return ZINT_ERROR_MEMORY; /* `zint_gb18030_cpy_segs()` only fails with OOM */
}
} else {
unsigned int *dd = ddata;
@@ -1523,29 +1523,29 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
int done = 0, eci = local_segs[i].eci;
if (local_segs[i].eci != 32 || seg_count > 1) { /* Unless ECI 32 (GB 18030) or have multiple segments */
/* Try other conversions (ECI 0 defaults to ISO/IEC 8859-1) */
int error_number = gb18030_utf8_to_eci(local_segs[i].eci, local_segs[i].source, &local_segs[i].length,
dd, full_multibyte);
int error_number = zint_gb18030_utf8_to_eci(local_segs[i].eci, local_segs[i].source,
&local_segs[i].length, dd, full_multibyte);
if (error_number == 0) {
done = 1;
} else if (local_segs[i].eci || seg_count > 1) {
return errtxtf(error_number, symbol, 545, "Invalid character in input for ECI '%d'",
return z_errtxtf(error_number, symbol, 545, "Invalid character in input for ECI '%d'",
local_segs[i].eci);
}
}
if (!done) {
/* Try GB 18030 */
int error_number = gb18030_utf8(symbol, local_segs[i].source, &local_segs[i].length, dd);
int error_number = zint_gb18030_utf8(symbol, local_segs[i].source, &local_segs[i].length, dd);
if (error_number != 0) {
return error_number;
}
if (local_segs[i].eci != 32) {
warn_number = errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 543,
warn_number = z_errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 543,
"Converted to GB 18030 but no ECI specified");
}
eci = 32;
}
if (raw_text && rt_cpy_seg_ddata(symbol, i, &local_segs[i], eci, dd)) {
return ZINT_ERROR_MEMORY; /* `rt_cpy_seg_ddata()` only fails with OOM */
if (raw_text && z_rt_cpy_seg_ddata(symbol, i, &local_segs[i], eci, dd)) {
return ZINT_ERROR_MEMORY; /* `z_rt_cpy_seg_ddata()` only fails with OOM */
}
dd += local_segs[i].length;
}
@@ -1583,7 +1583,7 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
if (version == 85) {
free(binary);
return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 541, "Input too long, requires %d codewords (maximum 3264)",
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 541, "Input too long, requires %d codewords (maximum 3264)",
codewords);
}
@@ -1598,14 +1598,15 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
if ((symbol->option_2 != 0) && (symbol->option_2 < version)) {
free(binary);
if (ecc_level == 1) {
return ZEXT errtxtf(ZINT_ERROR_TOO_LONG, symbol, 542,
"Input too long for Version %1$d, requires %2$d codewords (maximum %3$d)",
symbol->option_2, codewords, hx_data_codewords[ecc_level - 1][symbol->option_2 - 1]);
return ZEXT z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 542,
"Input too long for Version %1$d, requires %2$d codewords (maximum %3$d)",
symbol->option_2, codewords,
hx_data_codewords[ecc_level - 1][symbol->option_2 - 1]);
}
return ZEXT errtxtf(ZINT_ERROR_TOO_LONG, symbol, 542,
"Input too long for Version %1$d, ECC %2$d, requires %3$d codewords (maximum %4$d)",
symbol->option_2, ecc_level, codewords,
hx_data_codewords[ecc_level - 1][symbol->option_2 - 1]);
return ZEXT z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 542,
"Input too long for Version %1$d, ECC %2$d, requires %3$d codewords (maximum %4$d)",
symbol->option_2, ecc_level, codewords,
hx_data_codewords[ecc_level - 1][symbol->option_2 - 1]);
}
/* If there is spare capacity, increase the level of ECC */
@@ -1653,7 +1654,7 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
fputc('\n', stdout);
}
#ifdef ZINT_TEST
if (symbol->debug & ZINT_DEBUG_TEST) debug_test_codeword_dump(symbol, datastream, data_codewords);
if (symbol->debug & ZINT_DEBUG_TEST) z_debug_test_codeword_dump(symbol, datastream, data_codewords);
#endif
hx_setup_grid(grid, size, version);
@@ -1700,7 +1701,7 @@ INTERNAL int hanxin(struct zint_symbol *symbol, struct zint_seg segs[], const in
const int r = i * size;
for (j = 0; j < size; j++) {
if (grid[r + j] & 0x01) {
set_module(symbol, i, j);
z_set_module(symbol, i, j);
}
}
symbol->row_height[i] = 1;