1
0
mirror of https://git.code.sf.net/p/zint/code synced 2026-01-28 06:16:03 +00:00

general: split up some source files to lessen ZXing-C++ bloat

when `ZXING_USE_BUNDLED_ZINT` set
This commit is contained in:
gitlost
2025-01-19 13:44:43 +00:00
parent 62c54adb56
commit 5c08226700
26 changed files with 2554 additions and 1534 deletions

View File

@@ -1,7 +1,7 @@
/* medical.c - Handles 1 track and 2 track pharmacode and Codabar */
/* medical.c - Handles Pharmacode One-Track, Pharmacode Two-Track, Italian Pharmacode and PZN */
/*
libzint - the open source barcode library
Copyright (C) 2008-2024 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2008-2025 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -35,20 +35,7 @@
INTERNAL int code39(struct zint_symbol *symbol, unsigned char source[], int length);
static const char CALCIUM[] = "0123456789-$:/.+ABCD";
#define CALCIUM_INNER_F (IS_NUM_F | IS_MNS_F | IS_CLI_F | IS_PLS_F) /* CALCIUM_INNER "0123456789-$:/.+" */
/* Codabar table checked against EN 798:1995 */
static const char CodaTable[20][8] = {
{'1','1','1','1','1','2','2','1'}, {'1','1','1','1','2','2','1','1'}, {'1','1','1','2','1','1','2','1'},
{'2','2','1','1','1','1','1','1'}, {'1','1','2','1','1','2','1','1'}, {'2','1','1','1','1','2','1','1'},
{'1','2','1','1','1','1','2','1'}, {'1','2','1','1','2','1','1','1'}, {'1','2','2','1','1','1','1','1'},
{'2','1','1','2','1','1','1','1'}, {'1','1','1','2','2','1','1','1'}, {'1','1','2','2','1','1','1','1'},
{'2','1','1','1','2','1','2','1'}, {'2','1','2','1','1','1','2','1'}, {'2','1','2','1','2','1','1','1'},
{'1','1','2','1','2','1','2','1'}, {'1','1','2','2','1','2','1','1'}, {'1','2','1','2','1','1','2','1'},
{'1','1','1','2','1','2','2','1'}, {'1','1','1','2','2','2','1','1'}
};
/* Pharmacode One-Track */
INTERNAL int pharma(struct zint_symbol *symbol, unsigned char source[], int length) {
/* "Pharmacode can represent only a single integer from 3 to 131070. Unlike other
commonly used one-dimensional barcode schemes, pharmacode does not store the data in a
@@ -149,6 +136,7 @@ static int pharma_two_calc(int tester, char *d) {
return h;
}
/* Pharmacode Two-Track */
INTERNAL int pharma_two(struct zint_symbol *symbol, unsigned char source[], int length) {
/* Draws the patterns for two track pharmacode */
int i;
@@ -197,100 +185,6 @@ INTERNAL int pharma_two(struct zint_symbol *symbol, unsigned char source[], int
return error_number;
}
/* The Codabar system consisting of simple substitution */
INTERNAL int codabar(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number = 0;
int posns[103];
char dest[833]; /* (103 + 1) * 8 + 1 == 833 */
char *d = dest;
int add_checksum, count = 0, checksum = 0;
int d_chars = 0;
if (length > 103) { /* No stack smashing please (103 + 1) * 11 = 1144 */
return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 356, "Input length %d too long (maximum 103)", length);
}
/* BS EN 798:1995 4.2 "'Codabar' symbols shall consist of ... b) start character;
c) one or more symbol characters representing data ... d) stop character ..." */
if (length < 3) {
return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 362, "Input length %d too short (minimum 3)", length);
}
to_upper(source, length);
/* Codabar must begin and end with the characters A, B, C or D */
if ((source[0] != 'A') && (source[0] != 'B') && (source[0] != 'C')
&& (source[0] != 'D')) {
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 358, "Does not begin with \"A\", \"B\", \"C\" or \"D\"");
}
if ((source[length - 1] != 'A') && (source[length - 1] != 'B') &&
(source[length - 1] != 'C') && (source[length - 1] != 'D')) {
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 359, "Does not end with \"A\", \"B\", \"C\" or \"D\"");
}
if ((i = not_sane_lookup(CALCIUM, sizeof(CALCIUM) - 1, source, length, posns))) {
return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 357,
"Invalid character at position %1$d in input (\"%2$s\" only)", i, CALCIUM);
}
/* And must not use A, B, C or D otherwise (BS EN 798:1995 4.3.2) */
if ((i = not_sane(CALCIUM_INNER_F, source + 1, length - 2))) {
return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 363,
"Invalid character at position %d in input (cannot contain \"A\", \"B\", \"C\" or \"D\")", i);
}
/* Add check character: 1 don't show to HRT, 2 do show to HRT
(unfortunately to maintain back-compatibility, this is reverse of C25) */
add_checksum = symbol->option_2 == 1 || symbol->option_2 == 2;
for (i = 0; i < length; i++, d += 8) {
if (add_checksum) {
/* BS EN 798:1995 A.3 suggests using ISO 7064 algorithm but leaves it application defined.
Following BWIPP and TEC-IT, use this simple mod-16 algorithm (not in ISO 7064) */
count += posns[i];
if (i + 1 == length) {
checksum = count % 16;
if (checksum) {
checksum = 16 - checksum;
}
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("Codabar: %s, count %d, checksum %d (%c)\n", source, count, checksum, CALCIUM[checksum]);
}
memcpy(d, CodaTable[checksum], 8);
d += 8;
}
}
memcpy(d, CodaTable[posns[i]], 8);
if (source[i] == '/' || source[i] == ':' || source[i] == '.' || source[i] == '+') { /* Wide data characters */
d_chars++;
}
}
expand(symbol, dest, d - dest);
if (symbol->output_options & COMPLIANT_HEIGHT) {
/* BS EN 798:1995 4.4.1 (d) max of 5mm / 0.43mm (X max) ~ 11.628 or 15% of width where (taking N =
narrow/wide ratio as 2 and I = X) width = ((2 * N + 5) * C + (N 1) * (D + 2)) * X + I * (C 1) + 2Q
= ((4 + 5) * C + (D + 2) + C - 1 + 2 * 10) * X = (10 * C + D + 21) * X
Length (C) includes start/stop chars */
const float min_height_min = 11.6279068f; /* 5.0 / 0.43 */
float min_height = stripf((10.0f * ((add_checksum ? length + 1 : length) + 2.0f) + d_chars + 21.0f) * 0.15f);
if (min_height < min_height_min) {
min_height = min_height_min;
}
/* Using 50 as default as none recommended */
error_number = set_height(symbol, min_height, min_height > 50.0f ? min_height : 50.0f, 0.0f, 0 /*no_errtxt*/);
} else {
(void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
}
ustrcpy(symbol->text, source);
if (symbol->option_2 == 2) {
symbol->text[length - 1] = CALCIUM[checksum]; /* Place before final A/B/C/D character (BS EN 798:1995 A.3) */
symbol->text[length] = source[length - 1];
symbol->text[length + 1] = '\0';
}
return error_number;
}
/* Italian Pharmacode */
INTERNAL int code32(struct zint_symbol *symbol, unsigned char source[], int length) {
static const char TABELLA[] = "0123456789BCDFGHJKLMNPQRSTUVWXYZ";
@@ -374,4 +268,89 @@ INTERNAL int code32(struct zint_symbol *symbol, unsigned char source[], int leng
return error_number;
}
/* Pharmazentralnummer (PZN) */
/* PZN https://www.ifaffm.de/mandanten/1/documents/04_ifa_coding_system/IFA_Info_Code_39_EN.pdf */
/* PZN https://www.ifaffm.de/mandanten/1/documents/04_ifa_coding_system/
IFA-Info_Check_Digit_Calculations_PZN_PPN_UDI_EN.pdf */
INTERNAL int pzn(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number, zeroes;
int count, check_digit;
unsigned char have_check_digit = '\0';
char localstr[1 + 8 + 1]; /* '-' prefix + 8 digits + NUL */
const int pzn7 = symbol->option_2 == 1;
if (length > 8 - pzn7) {
return errtxtf(ZINT_ERROR_TOO_LONG, symbol, 325, "Input length %1$d too long (maximum %2$d)", length,
8 - pzn7);
}
if (length == 8 - pzn7) {
have_check_digit = source[7 - pzn7];
length--;
}
if ((i = not_sane(NEON_F, source, length))) {
return errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 326,
"Invalid character at position %d in input (digits only)", i);
}
localstr[0] = '-';
zeroes = 7 - pzn7 - length + 1;
for (i = 1; i < zeroes; i++)
localstr[i] = '0';
ustrcpy(localstr + zeroes, source);
count = 0;
for (i = 1; i < 8 - pzn7; i++) {
count += (i + pzn7) * ctoi(localstr[i]);
}
check_digit = count % 11;
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("PZN: %s, check digit %d\n", localstr, (int) check_digit);
}
if (check_digit == 10) {
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 327, "Invalid PZN, check digit is '10'");
}
if (have_check_digit && ctoi(have_check_digit) != check_digit) {
return errtxtf(ZINT_ERROR_INVALID_CHECK, symbol, 890, "Invalid check digit '%1$c', expecting '%2$c'",
have_check_digit, itoc(check_digit));
}
localstr[8 - pzn7] = itoc(check_digit);
localstr[9 - pzn7] = '\0';
if (pzn7) {
symbol->option_2 = 0; /* Need to overwrite this so `code39()` doesn't add a check digit itself */
}
error_number = code39(symbol, (unsigned char *) localstr, 9 - pzn7);
if (pzn7) {
symbol->option_2 = 1; /* Restore */
}
ustrcpy(symbol->text, "PZN - "); /* Note changed to put space after hyphen */
ustrcat(symbol->text, localstr + 1);
if (symbol->output_options & COMPLIANT_HEIGHT) {
/* Technical Information regarding PZN Coding V 2.1 (25 Feb 2019) Code size
https://www.ifaffm.de/mandanten/1/documents/04_ifa_coding_system/IFA_Info_Code_39_EN.pdf
"normal" X 0.25mm (0.187mm - 0.45mm), height 8mm - 20mm for 0.25mm X, 10mm mentioned so use that
as default, 10mm / 0.25mm = 40 */
if (error_number < ZINT_ERROR) {
const float min_height = 17.7777786f; /* 8.0 / 0.45 */
const float max_height = 106.951874f; /* 20.0 / 0.187 */
error_number = set_height(symbol, min_height, 40.0f, max_height, 0 /*no_errtxt*/);
}
} else {
if (error_number < ZINT_ERROR) {
(void) set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
}
}
return error_number;
}
/* vim: set ts=4 sw=4 et : */