zint/backend/auspost.c
gitlost fab7435fac Performance improvements for linear encoding and raster output
- use fixed-length string tables (mostly) instead of (char *) pointer ones
   (saves ~40K)
 - re-use C128Table for CODABLOCKF and CODE16K
   (required removal of Stop character and extra CODE16K-only entry)
 - use pointer to destination and copy (memcpy/strcpy(), bin_append_posn())
   instead of concatenating (strcat()) (mostly)
 - replace last remaining bin_append()s with bin_append_posn();
   bin_append() removed
 - add length arg to toupper() and expand() (avoids strlen())
 - change is_sane() to use table-based flags (avoids an iteration)
 - rename lookup() to is_sane_lookup() and change to check and return posns
   and use in pointer to destination loops (avoids strcat()s)
 - remove special case PHARMA in expand() (dealt with in pharma())
 - make #define SILVER/CALCIUM/TECHNETIUM/KRSET etc static strings
 - replace strchr() -> posn()
 - CODE128: populate destination once in checksum loop; re-use and export
   some more routines (c128_set_a/b/c(), c128_put_in_set()) for sharing;
   prefix defines (SHIFTA -> C128_SHIFTA etc) and existing exported routines
 - use factor XOR toggle trick in checksum calcs (avoids branch)
 - raster.c: fill out single 1-pixel row and copy using new draw_bar_line(),
   copy_bar_line() routines; similarly in buffer_plot compare previous line &
   copy if same (same technique as used to improve non-half-integer scaling,
   significant performance increase, (c) codemonkey82);
   also done for PNG (BMP/GIF/PCX/TIFF not done)
 - raster/vector/output.c: shorten "output_" prefix -> "out_";
   sync vector to other raster changes to try to keep source files similar
 - 2of5.c: prefix "c25_"
JAPANPOST: return error if input data truncated (backward incompatible)
DAFT: max chars 50 -> 100
common.c: istwodigit() -> is_twodigit()
common.c/emf.c/output.c: use some further stripf()s (MSVC6 float variations)
library.c: new check_output_args() helper
zint.h: add BARCODE_LAST marker and use in library.c
QRCODE: remove a NOLINT (requires clang-tidy-13), one remaining
CMake: separate no-optimize from ZINT_DEBUG into new ZINT_NOOPT option
2021-10-20 23:05:30 +01:00

283 lines
11 KiB
C

/* auspost.c - Handles Australia Post 4-State Barcode */
/*
libzint - the open source barcode library
Copyright (C) 2008 - 2021 Robin Stuart <rstuart114@gmail.com>
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.
*/
/* vim: set ts=4 sw=4 et : */
static const char GDSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz #";
#define GDSET_F (IS_NUM_F | IS_UPR_F | IS_LWR_F | IS_SPC_F | IS_HSH_F)
static const char AusNTable[10][2] = {
{'0','0'}, {'0','1'}, {'0','2'}, {'1','0'}, {'1','1'}, {'1','2'}, {'2','0'}, {'2','1'}, {'2','2'}, {'3','0'}
};
static const char AusCTable[64][3] = {
{'2','2','2'}, {'3','0','0'}, {'3','0','1'}, {'3','0','2'}, {'3','1','0'}, {'3','1','1'},
{'3','1','2'}, {'3','2','0'}, {'3','2','1'}, {'3','2','2'}, {'0','0','0'}, {'0','0','1'},
{'0','0','2'}, {'0','1','0'}, {'0','1','1'}, {'0','1','2'}, {'0','2','0'}, {'0','2','1'},
{'0','2','2'}, {'1','0','0'}, {'1','0','1'}, {'1','0','2'}, {'1','1','0'}, {'1','1','1'},
{'1','1','2'}, {'1','2','0'}, {'1','2','1'}, {'1','2','2'}, {'2','0','0'}, {'2','0','1'},
{'2','0','2'}, {'2','1','0'}, {'2','1','1'}, {'2','1','2'}, {'2','2','0'}, {'2','2','1'},
{'0','2','3'}, {'0','3','0'}, {'0','3','1'}, {'0','3','2'}, {'0','3','3'}, {'1','0','3'},
{'1','1','3'}, {'1','2','3'}, {'1','3','0'}, {'1','3','1'}, {'1','3','2'}, {'1','3','3'},
{'2','0','3'}, {'2','1','3'}, {'2','2','3'}, {'2','3','0'}, {'2','3','1'}, {'2','3','2'},
{'2','3','3'}, {'3','0','3'}, {'3','1','3'}, {'3','2','3'}, {'3','3','0'}, {'3','3','1'},
{'3','3','2'}, {'3','3','3'}, {'0','0','3'}, {'0','1','3'}
};
static const char AusBarTable[64][3] = {
{'0','0','0'}, {'0','0','1'}, {'0','0','2'}, {'0','0','3'}, {'0','1','0'}, {'0','1','1'},
{'0','1','2'}, {'0','1','3'}, {'0','2','0'}, {'0','2','1'}, {'0','2','2'}, {'0','2','3'},
{'0','3','0'}, {'0','3','1'}, {'0','3','2'}, {'0','3','3'}, {'1','0','0'}, {'1','0','1'},
{'1','0','2'}, {'1','0','3'}, {'1','1','0'}, {'1','1','1'}, {'1','1','2'}, {'1','1','3'},
{'1','2','0'}, {'1','2','1'}, {'1','2','2'}, {'1','2','3'}, {'1','3','0'}, {'1','3','1'},
{'1','3','2'}, {'1','3','3'}, {'2','0','0'}, {'2','0','1'}, {'2','0','2'}, {'2','0','3'},
{'2','1','0'}, {'2','1','1'}, {'2','1','2'}, {'2','1','3'}, {'2','2','0'}, {'2','2','1'},
{'2','2','2'}, {'2','2','3'}, {'2','3','0'}, {'2','3','1'}, {'2','3','2'}, {'2','3','3'},
{'3','0','0'}, {'3','0','1'}, {'3','0','2'}, {'3','0','3'}, {'3','1','0'}, {'3','1','1'},
{'3','1','2'}, {'3','1','3'}, {'3','2','0'}, {'3','2','1'}, {'3','2','2'}, {'3','2','3'},
{'3','3','0'}, {'3','3','1'}, {'3','3','2'}, {'3','3','3'}
};
#include <stdio.h>
#include "common.h"
#include "reedsol.h"
static char aus_convert_pattern(char data, int shift) {
return (data - '0') << shift;
}
/* Adds Reed-Solomon error correction to auspost */
static char *aus_rs_error(char data_pattern[], char *d) {
int reader, length, triple_writer = 0;
unsigned char triple[31];
unsigned char result[5];
rs_t rs;
for (reader = 2, length = d - data_pattern; reader < length; reader += 3, triple_writer++) {
triple[triple_writer] = aus_convert_pattern(data_pattern[reader], 4)
+ aus_convert_pattern(data_pattern[reader + 1], 2)
+ aus_convert_pattern(data_pattern[reader + 2], 0);
}
rs_init_gf(&rs, 0x43);
rs_init_code(&rs, 4, 1);
rs_encode(&rs, triple_writer, triple, result);
for (reader = 4; reader > 0; reader--, d += 3) {
memcpy(d, AusBarTable[(int) result[reader - 1]], 3);
}
return d;
}
INTERNAL int daft_set_height(struct zint_symbol *symbol, const float min_height, const float max_height);
/* Handles Australia Posts's 4 State Codes */
INTERNAL int auspost(struct zint_symbol *symbol, unsigned char source[], int length) {
/* Customer Standard Barcode, Barcode 2 or Barcode 3 system determined automatically
(i.e. the FCC doesn't need to be specified by the user) dependent
on the length of the input string */
/* The contents of data_pattern conform to the following standard:
0 = Tracker, Ascender and Descender
1 = Tracker and Ascender
2 = Tracker and Descender
3 = Tracker only */
int error_number;
int writer;
int loopey, reader;
int h;
char data_pattern[200];
char *d = data_pattern;
char fcc[3] = {0}, dpid[10];
char localstr[30];
/* Do all of the length checking first to avoid stack smashing */
if (symbol->symbology == BARCODE_AUSPOST) {
if (length != 8 && length != 13 && length != 16 && length != 18 && length != 23) {
strcpy(symbol->errtxt, "401: Auspost input is wrong length (8, 13, 16, 18 or 23 characters only)");
return ZINT_ERROR_TOO_LONG;
}
} else if (length > 8) {
strcpy(symbol->errtxt, "403: Auspost input is too long (8 character maximum)");
return ZINT_ERROR_TOO_LONG;
}
/* Check input immediately to catch nuls */
if (!is_sane(GDSET_F, source, length)) {
strcpy(symbol->errtxt, "404: Invalid character in data (alphanumerics, space and \"#\" only)");
return ZINT_ERROR_INVALID_DATA;
}
localstr[0] = '\0';
if (symbol->symbology == BARCODE_AUSPOST) {
/* Format control code (FCC) */
switch (length) {
case 8:
strcpy(fcc, "11");
break;
case 13:
strcpy(fcc, "59");
break;
case 16:
strcpy(fcc, "59");
if (!is_sane(NEON_F, source, length)) {
strcpy(symbol->errtxt, "402: Invalid character in data (digits only for length 16)");
return ZINT_ERROR_INVALID_DATA;
}
break;
case 18:
strcpy(fcc, "62");
break;
case 23:
strcpy(fcc, "62");
if (!is_sane(NEON_F, source, length)) {
strcpy(symbol->errtxt, "406: Invalid character in data (digits only for length 23)");
return ZINT_ERROR_INVALID_DATA;
}
break;
}
} else {
int zeroes;
switch (symbol->symbology) {
case BARCODE_AUSREPLY: strcpy(fcc, "45");
break;
case BARCODE_AUSROUTE: strcpy(fcc, "87");
break;
case BARCODE_AUSREDIRECT: strcpy(fcc, "92");
break;
}
/* Add leading zeros as required */
zeroes = 8 - length;
memset(localstr, '0', zeroes);
localstr[zeroes] = '\0';
}
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("AUSPOST FCC: %s\n", fcc);
}
ustrncat(localstr, source, length);
h = (int) strlen(localstr);
/* Verify that the first 8 characters are numbers */
memcpy(dpid, localstr, 8);
dpid[8] = '\0';
if (!is_sane(NEON_F, (unsigned char *) dpid, 8)) {
strcpy(symbol->errtxt, "405: Invalid character in DPID (first 8 characters) (digits only)");
return ZINT_ERROR_INVALID_DATA;
}
/* Start character */
memcpy(d, "13", 2);
d += 2;
/* Encode the FCC */
for (reader = 0; reader < 2; reader++, d += 2) {
memcpy(d, AusNTable[fcc[reader] - '0'], 2);
}
/* Delivery Point Identifier (DPID) */
for (reader = 0; reader < 8; reader++, d += 2) {
memcpy(d, AusNTable[dpid[reader] - '0'], 2);
}
/* Customer Information */
if (h > 8) {
if ((h == 13) || (h == 18)) {
for (reader = 8; reader < h; reader++, d += 3) {
memcpy(d, AusCTable[posn(GDSET, localstr[reader])], 3);
}
} else if ((h == 16) || (h == 23)) {
for (reader = 8; reader < h; reader++, d += 2) {
memcpy(d, AusNTable[localstr[reader] - '0'], 2);
}
}
}
/* Filler bar */
h = d - data_pattern;
switch (h) {
case 22:
case 37:
case 52:
*d++ = '3';
break;
default:
break;
}
/* Reed Solomon error correction */
d = aus_rs_error(data_pattern, d);
/* Stop character */
memcpy(d, "13", 2);
d += 2;
/* Turn the symbol into a bar pattern ready for plotting */
writer = 0;
h = d - data_pattern;
for (loopey = 0; loopey < h; loopey++) {
if ((data_pattern[loopey] == '1') || (data_pattern[loopey] == '0')) {
set_module(symbol, 0, writer);
}
set_module(symbol, 1, writer);
if ((data_pattern[loopey] == '2') || (data_pattern[loopey] == '0')) {
set_module(symbol, 2, writer);
}
writer += 2;
}
if (symbol->output_options & COMPLIANT_HEIGHT) {
/* Australia Post Customer Barcoding Technical Specifications (Revised Aug 2012) Dimensions, placement and
printing p.12
(https://auspost.com.au/content/dam/auspost_corp/media/documents/
customer-barcode-technical-specifications-aug2012.pdf)
X 0.5mm (average of 0.4mm - 0.6mm), min height 4.2mm / 0.6mm (X max) = 7, max 5.6mm / 0.4mm (X min) = 14
Tracker 1.3mm (average of 1mm - 1.6mm)
Ascender/Descender 3.15mm (average of 2.6mm - 3.7mm) less T = 1.85mm
*/
symbol->row_height[0] = 3.7f; /* 1.85f / 0.5f */
symbol->row_height[1] = 2.6f; /* 1.3f / 0.5f */
error_number = daft_set_height(symbol, 7.0f, 14.0f); /* Note using max X for minimum and min X for maximum */
} else {
symbol->row_height[0] = 3.0f;
symbol->row_height[1] = 2.0f;
error_number = daft_set_height(symbol, 0.0f, 0.0f);
}
symbol->rows = 3;
symbol->width = writer - 1;
return error_number;
}