#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include "common.h"
INTERNAL int z_ctoi(const char source) {
if (z_isdigit(source))
return (source - '0');
if (source >= 'A' && source <= 'F')
return (source - 'A' + 10);
if (source >= 'a' && source <= 'f')
return (source - 'a' + 10);
return -1;
}
INTERNAL int z_to_int(const unsigned char source[], const int length) {
int val = 0;
int non_digit = 0;
int i;
for (i = 0; i < length; i++) {
val *= 10;
val += source[i] - '0';
non_digit |= !z_isdigit(source[i]);
}
return non_digit ? -1 : val;
}
INTERNAL void z_to_upper(unsigned char source[], const int length) {
int i;
for (i = 0; i < length; i++) {
source[i] &= z_islower(source[i]) ? 0x5F : 0xFF;
}
}
INTERNAL int z_chr_cnt(const unsigned char source[], const int length, const unsigned char c) {
int count = 0;
int i;
for (i = 0; i < length; i++) {
count += source[i] == c;
}
return count;
}
#define IS_CLS_F (IS_CLI_F | IS_SIL_F)
static const unsigned short flgs[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
IS_SPC_F, IS_C82_F, IS_C82_F, IS_HSH_F,
IS_CLS_F, IS_SIL_F | IS_C82_F, IS_C82_F, IS_C82_F,
IS_C82_F, IS_C82_F, IS_AST_F, IS_PLS_F,
IS_C82_F, IS_MNS_F, IS_CLS_F | IS_C82_F, IS_CLS_F | IS_C82_F,
IS_NUM_F, IS_NUM_F, IS_NUM_F, IS_NUM_F,
IS_NUM_F, IS_NUM_F, IS_NUM_F, IS_NUM_F,
IS_NUM_F, IS_NUM_F, IS_CLI_F | IS_C82_F, IS_C82_F,
IS_C82_F, IS_C82_F, IS_C82_F, IS_C82_F,
0, IS_UHX_F | IS_ARS_F, IS_UHX_F | IS_ARS_F, IS_UHX_F | IS_ARS_F,
IS_UHX_F | IS_ARS_F, IS_UHX_F | IS_ARS_F, IS_UHX_F | IS_ARS_F, IS_UPO_F | IS_ARS_F,
IS_UPO_F | IS_ARS_F, IS_UPO_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F,
IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F, IS_UPO_F,
IS_UPO_F | IS_ARS_F, IS_UPO_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F,
IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F,
IS_UX__F | IS_ARS_F, IS_UPO_F | IS_ARS_F, IS_UPO_F | IS_ARS_F, 0,
0, 0, 0, IS_C82_F,
0, IS_LHX_F, IS_LHX_F, IS_LHX_F,
IS_LHX_F, IS_LHX_F, IS_LHX_F, IS_LWO_F,
IS_LWO_F, IS_LWO_F, IS_LWO_F, IS_LWO_F,
IS_LWO_F, IS_LWO_F, IS_LWO_F, IS_LWO_F,
IS_LWO_F, IS_LWO_F, IS_LWO_F, IS_LWO_F,
IS_LWO_F, IS_LWO_F, IS_LWO_F, IS_LWO_F,
IS_LX__F, IS_LWO_F, IS_LWO_F, 0,
0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
INTERNAL int z_is_chr(const unsigned int flg, const unsigned int c) {
return z_isascii(c) && (flgs[c] & flg);
}
INTERNAL int z_not_sane(const unsigned int flg, const unsigned char source[], const int length) {
int i;
for (i = 0; i < length; i++) {
if (!(flgs[source[i]] & flg)) {
return i + 1;
}
}
return 0;
}
INTERNAL int z_not_sane_lookup(const char test_string[], const int test_length, const unsigned char source[],
const int length, int *posns) {
int i, j;
for (i = 0; i < length; i++) {
posns[i] = -1;
for (j = 0; j < test_length; j++) {
if (source[i] == test_string[j]) {
posns[i] = j;
break;
}
}
if (posns[i] == -1) {
return i + 1;
}
}
return 0;
}
INTERNAL int z_posn(const char set_string[], const char data) {
const char *s;
for (s = set_string; *s; s++) {
if (data == *s) {
return s - set_string;
}
}
return -1;
}
INTERNAL int z_bin_append_posn(const int arg, const int length, char *binary, const int bin_posn) {
int i;
const int end = length - 1;
for (i = 0; i < length; i++) {
binary[bin_posn + i] = '0' + ((arg >> (end - i)) & 1);
}
return bin_posn + length;
}
#ifndef Z_COMMON_INLINE
INTERNAL int z_module_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord) {
return (symbol->encoded_data[y_coord][x_coord >> 3] >> (x_coord & 0x07)) & 1;
}
INTERNAL void z_set_module(struct zint_symbol *symbol, const int y_coord, const int x_coord) {
symbol->encoded_data[y_coord][x_coord >> 3] |= 1 << (x_coord & 0x07);
}
INTERNAL int z_module_colour_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord) {
return symbol->encoded_data[y_coord][x_coord];
}
INTERNAL void z_set_module_colour(struct zint_symbol *symbol, const int y_coord, const int x_coord,
const int colour) {
symbol->encoded_data[y_coord][x_coord] = colour;
}
INTERNAL void z_unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord) {
symbol->encoded_data[y_coord][x_coord >> 3] &= ~(1 << (x_coord & 0x07));
}
#endif
INTERNAL void z_expand(struct zint_symbol *symbol, const char data[], const int length) {
int reader;
int writer = 0;
int latch = 1;
const int row = symbol->rows;
symbol->rows++;
for (reader = 0; reader < length; reader++) {
int i;
const int num = data[reader] - '0';
assert(num >= 0);
for (i = 0; i < num; i++) {
if (latch) {
z_set_module(symbol, row, writer);
}
writer++;
}
latch = !latch;
}
if (writer > symbol->width) {
symbol->width = writer;
}
}
static int errtxt_id_str(char *errtxt, int num) {
int len = 0;
if (num == -1) {
errtxt[0] = '\0';
return 0;
}
if (num < 0 || num > 9999) {
num = 9999;
}
if (num >= 1000) {
errtxt[len++] = '0' + (num / 1000);
num %= 1000;
}
errtxt[len++] = '0' + (num / 100);
num %= 100;
errtxt[len++] = '0' + (num / 10);
num %= 10;
errtxt[len++] = '0' + num;
errtxt[len++] = ':';
errtxt[len++] = ' ';
return len;
}
INTERNAL int z_errtxt(const int error_number, struct zint_symbol *symbol, const int err_id, const char *msg) {
const int max_len = ARRAY_SIZE(symbol->errtxt) - 1;
const int id_len = errtxt_id_str(symbol->errtxt, err_id);
int msg_len = (int) strlen(msg);
if (id_len + msg_len > max_len) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
msg_len = max_len - id_len;
}
memcpy(symbol->errtxt + id_len, msg, msg_len);
symbol->errtxt[id_len + msg_len] = '\0';
return error_number;
}
static int errtxtf_dpad(const char *fmt);
static int errtxtf_num_arg(const char *fmt, int *p_arg) {
int ret = 0;
int arg = -2;
if (!errtxtf_dpad(fmt) && z_isdigit(fmt[0])) {
arg = fmt[1] == '$' ? fmt[0] - '0' - 1 : -1;
ret = 2;
}
if (p_arg) {
*p_arg = arg;
}
return ret;
}
static int errtxtf_slen(const char *fmt, const int arg, int *p_arg_cnt, int *p_len) {
int ret = 0;
int len = -1;
if (fmt[0] == '.') {
if (z_isdigit(fmt[1]) && fmt[1] != '0') {
len = fmt[1] - '0';
for (ret = 2; z_isdigit(fmt[ret]); ret++) {
len = len * 10 + fmt[ret] - '0';
}
if (fmt[ret] != 's') {
len = -1;
}
} else if (fmt[1] == '*' && fmt[2] == 's' && arg < 0) {
len = 0;
ret = 2;
} else if (fmt[1] == '*' && z_isdigit(fmt[2]) && fmt[3] == '$' && fmt[4] == 's') {
if (arg == -1 || arg == fmt[2] - '0') {
len = 0;
if (p_arg_cnt) {
(*p_arg_cnt)++;
}
}
ret = 4;
} else {
ret = 1;
}
}
if (p_len) {
*p_len = len;
}
return ret;
}
static int errtxtf_dpad(const char *fmt) {
if (fmt[0] == '0' && z_isdigit(fmt[1])) {
if (fmt[1] != '0' && fmt[2] == 'd') {
return 2;
}
if (z_isdigit(fmt[1]) && fmt[1] != '0' && z_isdigit(fmt[2]) && fmt[3] == 'd') {
return 3;
}
}
return 0;
}
static int errtxtf_fprec(const char *fmt) {
if (fmt[0] == '.' && z_isdigit(fmt[1]) && (fmt[2] == 'f' || fmt[2] == 'g')) {
return 2;
}
return 0;
}
INTERNAL int z_errtxtf(const int error_number, struct zint_symbol *symbol, const int err_id, const char *fmt, ...) {
const int max_len = ARRAY_SIZE(symbol->errtxt) - 1;
int p = errtxt_id_str(symbol->errtxt, err_id);
const char *f;
int i;
int arg_cnt = 0;
int have_num_arg = 0, have_unnum_arg = 0;
va_list ap;
int idxs[9] = {0};
char specs[9] = {0};
const char *ss[9] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
int slens[9] = {0};
int have_slens[9] = {0};
char dpads[9][3] = {{0}};
char fprecs[9] = {0};
char dfgs[9][100] = {{0}};
int cs[9] = {0};
for (f = fmt, i = 0; *f; f++) {
if (*f == '%') {
int inc, arg, len;
if (*++f == '%') {
continue;
}
if ((inc = errtxtf_num_arg(f, &arg))) {
if (arg == -1) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: invalid numbered format specifier");
}
if (i >= 9) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: too many format specifiers (9 maximum)");
}
f += inc;
have_num_arg = 1;
idxs[i] = arg;
} else {
if (i >= 9) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: too many format specifiers (9 maximum)");
}
have_unnum_arg = 1;
idxs[i] = i;
}
if ((inc = errtxtf_fprec(f))) {
assert(inc == 2);
fprecs[idxs[i]] = f[1];
f += inc;
}
if ((inc = errtxtf_slen(f, arg, &arg_cnt, &len))) {
if (len == -1) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: invalid length precision");
}
slens[idxs[i]] = len == 0 ? -1 : len;
have_slens[idxs[i]] = 1;
f += inc;
}
if ((inc = errtxtf_dpad(f))) {
memcpy(dpads[idxs[i]], f + 1, inc - 1);
dpads[idxs[i]][inc - 1] = '\0';
f += inc;
}
if (*f != 'c' && *f != 'd' && *f != 'f' && *f != 'g' && *f != 's') {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: unknown format specifier ('%c','%d','%f','%g','%s' only)");
}
specs[idxs[i++]] = *f;
arg_cnt++;
}
}
if (have_num_arg && have_unnum_arg) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
return z_errtxt(ZINT_ERROR_ENCODING_PROBLEM, symbol, 0,
"Internal error: mixed numbered and unnumbered format specifiers");
}
va_start(ap, fmt);
for (i = 0; i < arg_cnt; i++) {
if (specs[i] == 'c') {
cs[i] = va_arg(ap, int);
} else if (specs[i] == 'd') {
if (dpads[i][0]) {
char dpad_fmt[30];
sprintf(dpad_fmt, "%%0%sd", dpads[i]);
sprintf(dfgs[i], dpad_fmt, va_arg(ap, int));
} else {
sprintf(dfgs[i], "%d", va_arg(ap, int));
}
} else if (specs[i] == 'f' || specs[i] == 'g') {
if (fprecs[i]) {
char fprec_fmt[5];
sprintf(fprec_fmt, "%%.%c%c", fprecs[i], specs[i]);
sprintf(dfgs[i], fprec_fmt, va_arg(ap, double));
} else {
sprintf(dfgs[i], specs[i] == 'f' ? "%f" : "%g", va_arg(ap, double));
}
} else if (specs[i] == 's') {
if (have_slens[i] && slens[i] == -1) {
slens[i] = va_arg(ap, int);
}
ss[i] = va_arg(ap, char *);
}
}
va_end(ap);
for (f = fmt, i = 0; *f && p < max_len; f++) {
if (*f == '%') {
int idx;
if (*++f == '%') {
symbol->errtxt[p++] = '%';
continue;
}
f += errtxtf_num_arg(f, NULL );
f += errtxtf_slen(f, -1 , NULL , NULL );
f += errtxtf_dpad(f);
idx = idxs[i];
if (specs[idx] == 'c') {
symbol->errtxt[p++] = cs[idx];
} else {
int len;
if (specs[idx] == 's') {
if (have_slens[idx]) {
const char *si = ss[idx];
for (len = 0; len < slens[idx] && si[len]; len++);
} else {
len = (int) strlen(ss[idx]);
}
} else {
len = (int) strlen(dfgs[idx]);
}
if (len) {
if (p + len > max_len) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
len = max_len - p;
}
memcpy(symbol->errtxt + p, specs[idx] == 's' ? ss[idx] : dfgs[idx], len);
p += len;
}
}
i++;
} else {
symbol->errtxt[p++] = *f;
}
}
if (*f) {
if (!(symbol->debug & ZINT_DEBUG_TEST)) assert(0);
}
symbol->errtxt[p] = '\0';
return error_number;
}
INTERNAL int z_errtxt_adj(const int error_number, struct zint_symbol *symbol, const char *fmt, const char *msg) {
char err_buf[ARRAY_SIZE(symbol->errtxt)];
memcpy(err_buf, symbol->errtxt, strlen(symbol->errtxt) + 1);
if (msg) {
z_errtxtf(0, symbol, -1, fmt, err_buf, msg);
} else {
z_errtxtf(0, symbol, -1, fmt, err_buf);
}
return error_number;
}
INTERNAL int z_is_bindable(const int symbology) {
if (symbology < BARCODE_PHARMA_TWO && symbology != BARCODE_POSTNET) {
return 1;
}
switch (symbology) {
case BARCODE_CODE128AB:
case BARCODE_ISBNX:
case BARCODE_EAN14:
case BARCODE_VIN:
case BARCODE_NVE18:
case BARCODE_KOREAPOST:
case BARCODE_PLESSEY:
case BARCODE_TELEPEN_NUM:
case BARCODE_ITF14:
case BARCODE_CODE32:
case BARCODE_CODABLOCKF:
case BARCODE_DPD:
case BARCODE_HIBC_128:
case BARCODE_HIBC_39:
case BARCODE_HIBC_BLOCKF:
case BARCODE_UPU_S10:
case BARCODE_CHANNEL:
case BARCODE_BC412:
return 1;
break;
}
return 0;
}
INTERNAL int z_is_ean(const int symbology) {
switch (symbology) {
case BARCODE_EAN8:
case BARCODE_EAN_2ADDON:
case BARCODE_EAN_5ADDON:
case BARCODE_EANX:
case BARCODE_EANX_CHK:
case BARCODE_EAN13:
case BARCODE_ISBNX:
case BARCODE_EANX_CC:
case BARCODE_EAN8_CC:
case BARCODE_EAN13_CC:
return 1;
break;
}
return 0;
}
INTERNAL int z_is_upcean(const int symbology) {
if (z_is_ean(symbology)) {
return 1;
}
switch (symbology) {
case BARCODE_UPCA:
case BARCODE_UPCA_CHK:
case BARCODE_UPCE:
case BARCODE_UPCE_CHK:
case BARCODE_UPCA_CC:
case BARCODE_UPCE_CC:
return 1;
break;
}
return 0;
}
INTERNAL int z_is_composite(const int symbology) {
return (symbology >= BARCODE_EANX_CC && symbology <= BARCODE_DBAR_EXPSTK_CC)
|| symbology == BARCODE_EAN8_CC || symbology == BARCODE_EAN13_CC;
}
INTERNAL int z_is_dotty(const int symbology) {
switch (symbology) {
case BARCODE_QRCODE:
case BARCODE_DATAMATRIX:
case BARCODE_MICROQR:
case BARCODE_HIBC_DM:
case BARCODE_AZTEC:
case BARCODE_HIBC_QR:
case BARCODE_HIBC_AZTEC:
case BARCODE_AZRUNE:
case BARCODE_CODEONE:
case BARCODE_GRIDMATRIX:
case BARCODE_HANXIN:
case BARCODE_MAILMARK_2D:
case BARCODE_DOTCODE:
case BARCODE_UPNQR:
case BARCODE_RMQR:
return 1;
break;
}
return 0;
}
INTERNAL int z_is_fixed_ratio(const int symbology) {
if (z_is_dotty(symbology)) {
return 1;
}
switch (symbology) {
case BARCODE_MAXICODE:
case BARCODE_ULTRA:
return 1;
break;
}
return 0;
}
INTERNAL int z_is_twodigits(const unsigned char source[], const int length, const int position) {
return position + 1 < length && z_isdigit(source[position]) && z_isdigit(source[position + 1]);
}
INTERNAL int z_cnt_digits(const unsigned char source[], const int length, const int position, const int max) {
int i;
const int max_length = max == -1 || position + max > length ? length : position + max;
for (i = position; i < max_length && z_isdigit(source[i]); i++);
return i - position;
}
INTERNAL unsigned int z_decode_utf8(unsigned int *state, unsigned int *codep, const unsigned char byte) {
static const unsigned char utf8d[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12,
};
const unsigned int type = utf8d[byte];
*codep = *state != 0 ? (byte & 0x3Fu) | (*codep << 6) : (0xFF >> type) & byte;
*state = utf8d[256 + *state + type];
return *state;
}
INTERNAL int z_is_valid_utf8(const unsigned char source[], const int length) {
int i;
unsigned int codepoint, state = 0;
for (i = 0; i < length; i++) {
if (z_decode_utf8(&state, &codepoint, source[i]) == 12) {
return 0;
}
}
return state == 0;
}
INTERNAL int z_utf8_to_unicode(struct zint_symbol *symbol, const unsigned char source[], unsigned int vals[],
int *length, const int disallow_4byte) {
int bpos;
int jpos;
unsigned int codepoint, state = 0;
bpos = 0;
jpos = 0;
while (bpos < *length) {
do {
z_decode_utf8(&state, &codepoint, source[bpos++]);
} while (bpos < *length && state != 0 && state != 12);
if (state != 0) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 240, "Corrupt Unicode data");
}
if (disallow_4byte && codepoint > 0xFFFF) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 242,
"Unicode sequences of more than 3 bytes not supported");
}
vals[jpos] = codepoint;
jpos++;
}
*length = jpos;
return 0;
}
INTERNAL int z_hrt_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length) {
int i, j;
int warn_number = 0;
for (i = 0, j = 0; i < length && j < ARRAY_SIZE(symbol->text); i++) {
if (z_isascii(source[i])) {
symbol->text[j++] = z_iscntrl(source[i]) ? ' ' : source[i];
} else if (source[i] < 0xC0) {
if (source[i] < 0xA0) {
symbol->text[j++] = ' ';
} else {
if (j + 2 >= ARRAY_SIZE(symbol->text)) {
warn_number = ZINT_WARN_HRT_TRUNCATED;
break;
}
symbol->text[j++] = 0xC2;
symbol->text[j++] = source[i];
}
} else {
if (j + 2 >= ARRAY_SIZE(symbol->text)) {
warn_number = ZINT_WARN_HRT_TRUNCATED;
break;
}
symbol->text[j++] = 0xC3;
symbol->text[j++] = source[i] - 0x40;
}
}
if (j == ARRAY_SIZE(symbol->text)) {
warn_number = ZINT_WARN_HRT_TRUNCATED;
j--;
}
symbol->text_length = j;
symbol->text[j] = '\0';
if (warn_number) {
z_errtxt(0, symbol, 249, "Human Readable Text truncated");
}
return warn_number;
}
INTERNAL void z_hrt_cpy_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length) {
assert(length < ARRAY_SIZE(symbol->text));
memcpy(symbol->text, source, (size_t) length);
symbol->text_length = length;
symbol->text[length] = '\0';
}
INTERNAL void z_hrt_cpy_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length,
const char separator, const unsigned char cat[], const int cat_length) {
unsigned char *t = symbol->text;
const int total_length = (length > 0 ? length : 0) + z_isascii(separator) + (cat_length > 0 ? cat_length : 0);
assert(total_length < ARRAY_SIZE(symbol->text));
if (length > 0) {
memcpy(t, source, (size_t) length);
t += length;
}
if (z_isascii(separator)) {
*t++ = (unsigned char) separator;
}
if (cat_length > 0) {
memcpy(t, cat, (size_t) cat_length);
}
symbol->text_length = total_length;
symbol->text[total_length] = '\0';
}
INTERNAL void z_hrt_cpy_chr(struct zint_symbol *symbol, const char ch) {
symbol->text[0] = ch;
symbol->text_length = 1;
symbol->text[1] = '\0';
}
INTERNAL void z_hrt_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length) {
assert(symbol->text_length + length < ARRAY_SIZE(symbol->text));
memcpy(symbol->text + symbol->text_length, source, (size_t) length);
symbol->text_length += length;
symbol->text[symbol->text_length] = '\0';
}
INTERNAL void z_hrt_cat_chr_nochk(struct zint_symbol *symbol, const char ch) {
assert(symbol->text_length + 1 < ARRAY_SIZE(symbol->text));
symbol->text[symbol->text_length++] = (const unsigned char) ch;
symbol->text[symbol->text_length] = '\0';
}
INTERNAL void z_hrt_printf_nochk(struct zint_symbol *symbol, const char *fmt, ...) {
va_list ap;
int size;
va_start(ap, fmt);
size = vsprintf((char *) symbol->text, fmt, ap);
assert(size >= 0);
assert(size < ARRAY_SIZE(symbol->text));
symbol->text_length = size;
va_end(ap);
}
INTERNAL void z_hrt_conv_gs1_brackets_nochk(struct zint_symbol *symbol, const unsigned char source[],
const int length) {
int i;
int bracket_level = 0;
assert(length < ARRAY_SIZE(symbol->text));
for (i = 0; i < length; i++) {
if (source[i] == '[') {
symbol->text[i] = '(';
bracket_level++;
} else if (source[i] == ']' && bracket_level) {
symbol->text[i] = ')';
bracket_level--;
} else {
symbol->text[i] = source[i];
}
}
symbol->text_length = length;
symbol->text[length] = '\0';
}
INTERNAL int z_ct_init_segs(struct zint_symbol *symbol, const int seg_count) {
int i;
if (symbol->content_segs) {
z_ct_free_segs(symbol);
}
if (!(symbol->content_segs = (struct zint_seg *) calloc((size_t) seg_count, sizeof(struct zint_seg)))) {
return z_errtxt(ZINT_ERROR_MEMORY, symbol, 243, "Insufficient memory for content segs buffer");
}
for (i = 0; i < seg_count; i++) {
symbol->content_segs[i].source = NULL;
}
symbol->content_seg_count = seg_count;
return 0;
}
INTERNAL void z_ct_free_segs(struct zint_symbol *symbol) {
if (symbol->content_segs) {
int i;
assert(symbol->content_seg_count);
for (i = 0; i < symbol->content_seg_count; i++) {
if (symbol->content_segs[i].source) {
free(symbol->content_segs[i].source);
}
}
free(symbol->content_segs);
symbol->content_segs = NULL;
}
symbol->content_seg_count = 0;
}
static int ct_init_seg_source(struct zint_symbol *symbol, const int seg_idx, const int length) {
assert(symbol->content_segs);
assert(seg_idx >= 0 && seg_idx < symbol->content_seg_count);
assert(!symbol->content_segs[seg_idx].source);
assert(length > 0);
if (!(symbol->content_segs[seg_idx].source = (unsigned char *) malloc((size_t) length))) {
return z_errtxt(ZINT_ERROR_MEMORY, symbol, 245, "Insufficient memory for content text source buffer");
}
return 0;
}
INTERNAL int z_ct_cpy_segs(struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count) {
int seg_idx;
assert(!symbol->content_segs);
if (z_ct_init_segs(symbol, seg_count)) {
return ZINT_ERROR_MEMORY;
}
for (seg_idx = 0; seg_idx < seg_count; seg_idx++) {
const struct zint_seg *const seg = segs + seg_idx;
const int length = seg->length > 0 ? seg->length : (int) z_ustrlen(seg->source);
if (ct_init_seg_source(symbol, seg_idx, length)) {
return ZINT_ERROR_MEMORY;
}
memcpy(symbol->content_segs[seg_idx].source, seg->source, (size_t) length);
symbol->content_segs[seg_idx].length = length;
symbol->content_segs[seg_idx].eci = seg->eci ? seg->eci : 3;
}
return 0;
}
INTERNAL void z_ct_set_seg_eci(struct zint_symbol *symbol, const int seg_idx, const int eci) {
assert(symbol->content_segs);
assert(seg_idx >= 0 && seg_idx < symbol->content_seg_count);
symbol->content_segs[seg_idx].eci = eci;
}
INTERNAL int z_ct_cpy(struct zint_symbol *symbol, const unsigned char source[], const int length) {
assert(!symbol->content_segs);
if (z_ct_init_segs(symbol, 1 ) || ct_init_seg_source(symbol, 0 , length)) {
return ZINT_ERROR_MEMORY;
}
memcpy(symbol->content_segs[0].source, source, (size_t) length);
symbol->content_segs[0].length = length;
symbol->content_segs[0].eci = 3;
return 0;
}
INTERNAL int z_ct_cpy_cat(struct zint_symbol *symbol, const unsigned char source[], const int length,
const char separator, const unsigned char cat[], const int cat_length) {
unsigned char *s;
const int total_length = (length > 0 ? length : 0) + z_isascii(separator) + (cat_length > 0 ? cat_length : 0);
assert(!symbol->content_segs);
if (z_ct_init_segs(symbol, 1 ) || ct_init_seg_source(symbol, 0 , total_length)) {
return ZINT_ERROR_MEMORY;
}
s = symbol->content_segs[0].source;
if (length > 0) {
memcpy(s, source, (size_t) length);
s += length;
}
if (z_isascii(separator)) {
*s++ = (unsigned char) separator;
}
if (cat_length > 0) {
memcpy(s, cat, (size_t) cat_length);
}
symbol->content_segs[0].length = total_length;
symbol->content_segs[0].eci = 3;
return 0;
}
INTERNAL int z_ct_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length) {
int i;
int iso_cnt = 0;
unsigned char *s;
assert(!symbol->content_segs);
for (i = 0; i < length; i++) {
iso_cnt += !z_isascii(source[i]);
}
if (z_ct_init_segs(symbol, 1 ) || ct_init_seg_source(symbol, 0 , length + iso_cnt)) {
return ZINT_ERROR_MEMORY;
}
s = symbol->content_segs[0].source;
for (i = 0; i < length; i++) {
if (z_isascii(source[i])) {
*s++ = source[i];
} else if (source[i] < 0xC0) {
*s++ = 0xC2;
*s++ = source[i];
} else {
*s++ = 0xC3;
*s++ = source[i] - 0x40;
}
}
assert((int) (s - symbol->content_segs[0].source) == length + iso_cnt);
symbol->content_segs[0].length = length + iso_cnt;
symbol->content_segs[0].eci = 3;
return 0;
}
INTERNAL int z_ct_printf_256(struct zint_symbol *symbol, const char *fmt, ...) {
va_list ap;
int size;
assert(!symbol->content_segs);
if (z_ct_init_segs(symbol, 1 ) || ct_init_seg_source(symbol, 0 , 256)) {
return ZINT_ERROR_MEMORY;
}
va_start(ap, fmt);
size = vsprintf((char *) symbol->content_segs[0].source, fmt, ap);
assert(size >= 0);
assert(size < 256);
symbol->content_segs[0].length = size;
symbol->content_segs[0].eci = 3;
va_end(ap);
return 0;
}
INTERNAL int z_set_height(struct zint_symbol *symbol, const float min_row_height, const float default_height,
const float max_height, const int no_errtxt) {
int error_number = 0;
float fixed_height = 0.0f;
int zero_count = 0;
float row_height;
int i;
const int rows = symbol->rows ? symbol->rows : 1;
const float epsilon = 0.00000095367431640625f;
for (i = 0; i < rows; i++) {
if (symbol->row_height[i]) {
fixed_height += symbol->row_height[i];
} else {
zero_count++;
}
}
if (zero_count) {
if (symbol->height) {
if (symbol->input_mode & HEIGHTPERROW_MODE) {
row_height = z_stripf(symbol->height);
} else {
row_height = z_stripf((symbol->height - fixed_height) / zero_count);
}
} else if (default_height) {
row_height = z_stripf(default_height / zero_count);
} else {
row_height = z_stripf(min_row_height);
}
if (row_height < 0.5f) {
row_height = 0.5f;
}
if (min_row_height) {
if (z_stripf(row_height + epsilon) < z_stripf(min_row_height)) {
error_number = ZINT_WARN_NONCOMPLIANT;
if (!no_errtxt) {
z_errtxt(0, symbol, 247, "Height not compliant with standards (too small)");
}
}
}
symbol->height = z_stripf(row_height * zero_count + fixed_height);
} else {
symbol->height = z_stripf(fixed_height);
}
if (max_height) {
if (z_stripf(symbol->height) > z_stripf(max_height + epsilon)) {
error_number = ZINT_WARN_NONCOMPLIANT;
if (!no_errtxt) {
ZEXT z_errtxtf(0, symbol, 248, "Height not compliant with standards (maximum %.4g)", max_height);
}
}
}
return error_number;
}
#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
#define ZINT_NOINLINE __attribute__((__noinline__))
#elif defined(_MSC_VER) && _MSC_VER >= 1310
#define ZINT_NOINLINE __declspec(noinline)
#else
#define ZINT_NOINLINE
#endif
INTERNAL ZINT_NOINLINE float z_stripf(const float arg) {
return *((volatile const float *) &arg);
}
INTERNAL int z_segs_length(const struct zint_seg segs[], const int seg_count) {
int total_len = 0;
int i;
for (i = 0; i < seg_count; i++) {
total_len += segs[i].length == -1 ? (int) z_ustrlen(segs[i].source) : segs[i].length;
}
return total_len;
}
INTERNAL void z_segs_cpy(const struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count,
struct zint_seg local_segs[]) {
const int default_eci = symbol->symbology == BARCODE_GRIDMATRIX ? 29 : symbol->symbology == BARCODE_UPNQR ? 4 : 3;
int i;
local_segs[0] = segs[0];
for (i = 1; i < seg_count; i++) {
local_segs[i] = segs[i];
if (local_segs[i].eci == 0 && local_segs[i - 1].eci != 0 && local_segs[i - 1].eci != default_eci) {
local_segs[i].eci = default_eci;
}
}
}
INTERNAL char *z_debug_print_escape(const unsigned char *source, const int first_len, char *buf) {
int i;
if (buf) {
int j = 0;
for (i = 0; i < first_len; i++) {
const unsigned char ch = source[i];
if (z_iscntrl(ch) || !z_isascii(ch)) {
j += sprintf(buf + j, "\\x%02X", ch & 0xFF);
} else {
buf[j++] = ch;
}
}
buf[j] = '\0';
} else {
for (i = 0; i < first_len; i++) {
const unsigned char ch = source[i];
if (z_iscntrl(ch) || !z_isascii(ch)) {
printf("\\x%02X", ch & 0xFF);
} else {
fputc(ch, stdout);
}
}
}
return buf;
}
#ifdef ZINT_TEST
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 7
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-overflow="
#endif
INTERNAL void z_debug_test_codeword_dump(struct zint_symbol *symbol, const unsigned char *codewords,
const int length) {
int i, max = length, cnt_len = 0;
assert(ARRAY_SIZE(symbol->errtxt) >= 100);
if (length > 30) {
sprintf(symbol->errtxt, "(%d) ", length);
cnt_len = (int) strlen(symbol->errtxt);
max = 30 - (cnt_len + 2) / 3;
}
for (i = 0; i < max; i++) {
sprintf(symbol->errtxt + cnt_len + i * 3, "%02X ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0';
}
INTERNAL void z_debug_test_codeword_dump_short(struct zint_symbol *symbol, const short *codewords, const int length) {
int i, max = 0, cnt_len, errtxt_len;
char temp[20];
assert(ARRAY_SIZE(symbol->errtxt) >= 100);
errtxt_len = sprintf(symbol->errtxt, "(%d) ", length);
for (i = 0, cnt_len = errtxt_len; i < length; i++) {
cnt_len += sprintf(temp, "%d ", codewords[i]);
if (cnt_len > 92) {
break;
}
max++;
}
for (i = 0; i < max; i++) {
errtxt_len += sprintf(symbol->errtxt + errtxt_len, "%d ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0';
}
INTERNAL void z_debug_test_codeword_dump_int(struct zint_symbol *symbol, const int *codewords, const int length) {
int i, max = 0, cnt_len, errtxt_len;
char temp[20];
assert(ARRAY_SIZE(symbol->errtxt) >= 100);
errtxt_len = sprintf(symbol->errtxt, "(%d) ", length);
for (i = 0, cnt_len = errtxt_len; i < length; i++) {
cnt_len += sprintf(temp, "%d ", codewords[i]);
if (cnt_len > 92) {
break;
}
max++;
}
for (i = 0; i < max; i++) {
errtxt_len += sprintf(symbol->errtxt + errtxt_len, "%d ", codewords[i]);
}
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0';
}
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif
#endif