C25STANDARD/C25INTER/C25IATA/C25LOGIC/C25IND: add check digit option (#216)

This commit is contained in:
gitlost 2021-05-17 20:04:00 +01:00
parent 29d761c795
commit f3a94f0c0c
10 changed files with 392 additions and 227 deletions

View file

@ -2,7 +2,7 @@
/*
libzint - the open source barcode library
Copyright (C) 2008 - 2020 Robin Stuart <rstuart114@gmail.com>
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
@ -34,162 +34,121 @@
#include <stdio.h>
#include "common.h"
#ifdef _MSC_VER
#include <malloc.h>
#define inline _inline
#endif
static const char *C25MatrixTable[10] = {
"113311", "311131", "131131", "331111", "113131", "313111",
"133111", "111331", "311311", "131311"
"113311", "311131", "131131", "331111", "113131",
"313111", "133111", "111331", "311311", "131311"
};
static const char *C25MatrixStartStop[2] = { "411111", "41111" };
static const char *C25IndustTable[10] = {
"1111313111", "3111111131", "1131111131", "3131111111", "1111311131",
"3111311111", "1131311111", "1111113131", "3111113111", "1131113111"
};
static const char *C25IndustStartStop[2] = { "313111", "31113" };
static const char *C25IataLogicStartStop[2] = { "1111", "311" };
static const char *C25InterTable[10] = {
"11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133",
"31131", "13131"
"11331", "31113", "13113", "33111", "11313",
"31311", "13311", "11133", "31131", "13131"
};
static inline char check_digit(unsigned int count) {
static inline char check_digit(const unsigned int count) {
return itoc((10 - (count % 10)) % 10);
}
/* Calculate the check digit - the same method used for EAN-13 (GS1 General Specifications 7.9.1) */
static unsigned int gs1_checksum(const unsigned char source[], const int length) {
int i;
unsigned int count = 0;
int factor = length & 1 ? 3 : 1;
for (i = 0; i < length; i++) {
count += factor * ctoi(source[i]);
factor = factor == 1 ? 3 : 1;
}
return count;
}
/* Common to Standard (Matrix), Industrial, IATA, and Data Logic */
static int c25_common(struct zint_symbol *symbol, const unsigned char source[], int length, const int max,
const char *table[10], const char *start_stop[2], const int error_base) {
int i, error_number;
char dest[512]; /* Largest destination 6 + (80 + 1) * 6 + 5 + 1 = 498 */
unsigned char temp[80 + 1 + 1]; /* Largest maximum 80 */
int have_checkdigit = symbol->option_2 == 1 || symbol->option_2 == 2;
if (length > max) {
sprintf(symbol->errtxt, "%d: Input too long (maximum %d)", error_base, max);
return ZINT_ERROR_TOO_LONG;
}
error_number = is_sane(NEON, source, length);
if (error_number == ZINT_ERROR_INVALID_DATA) {
sprintf(symbol->errtxt, "%d: Invalid characters in data", error_base + 1);
return error_number;
}
ustrcpy(temp, source);
if (have_checkdigit) {
/* Add standard GS1 check digit */
temp[length] = check_digit(gs1_checksum(source, length));
temp[++length] = '\0';
}
/* start character */
strcpy(dest, start_stop[0]);
for (i = 0; i < length; i++) {
lookup(NEON, table, temp[i], dest);
}
/* Stop character */
strcat(dest, start_stop[1]);
expand(symbol, dest);
ustrcpy(symbol->text, temp);
if (symbol->option_2 == 2) {
/* Remove check digit from HRT */
symbol->text[length - 1] = '\0';
}
return error_number;
}
/* Code 2 of 5 Standard (Code 2 of 5 Matrix) */
INTERNAL int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number;
char dest[512]; /* 6 + 80 * 6 + 5 + 1 = 492 */
if (length > 80) {
strcpy(symbol->errtxt, "301: Input too long");
return ZINT_ERROR_TOO_LONG;
}
error_number = is_sane(NEON, source, length);
if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "302: Invalid characters in data");
return error_number;
}
/* start character */
strcpy(dest, "411111");
for (i = 0; i < length; i++) {
lookup(NEON, C25MatrixTable, source[i], dest);
}
/* Stop character */
strcat(dest, "41111");
expand(symbol, dest);
ustrcpy(symbol->text, source);
return error_number;
return c25_common(symbol, source, length, 80, C25MatrixTable, C25MatrixStartStop, 301);
}
/* Code 2 of 5 Industrial */
INTERNAL int industrial_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number;
char dest[512]; /* 6 + 45 * 10 + 5 + 1 = 462 */
if (length > 45) {
strcpy(symbol->errtxt, "303: Input too long");
return ZINT_ERROR_TOO_LONG;
}
error_number = is_sane(NEON, source, length);
if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "304: Invalid character in data");
return error_number;
}
/* start character */
strcpy(dest, "313111");
for (i = 0; i < length; i++) {
lookup(NEON, C25IndustTable, source[i], dest);
}
/* Stop character */
strcat(dest, "31113");
expand(symbol, dest);
ustrcpy(symbol->text, source);
return error_number;
return c25_common(symbol, source, length, 45, C25IndustTable, C25IndustStartStop, 303);
}
/* Code 2 of 5 IATA */
INTERNAL int iata_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number;
char dest[512]; /* 4 + 45 * 10 + 3 + 1 = 458 */
if (length > 45) {
strcpy(symbol->errtxt, "305: Input too long");
return ZINT_ERROR_TOO_LONG;
}
error_number = is_sane(NEON, source, length);
if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "306: Invalid characters in data");
return error_number;
}
/* start */
strcpy(dest, "1111");
for (i = 0; i < length; i++) {
lookup(NEON, C25IndustTable, source[i], dest);
}
/* stop */
strcat(dest, "311");
expand(symbol, dest);
ustrcpy(symbol->text, source);
return error_number;
return c25_common(symbol, source, length, 45, C25IndustTable, C25IataLogicStartStop, 305);
}
/* Code 2 of 5 Data Logic */
INTERNAL int logic_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number;
char dest[512]; /* 4 + 80 * 6 + 3 + 1 = 488 */
if (length > 80) {
strcpy(symbol->errtxt, "307: Input too long");
return ZINT_ERROR_TOO_LONG;
}
error_number = is_sane(NEON, source, length);
if (error_number == ZINT_ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "308: Invalid characters in data");
return error_number;
}
/* start character */
strcpy(dest, "1111");
for (i = 0; i < length; i++) {
lookup(NEON, C25MatrixTable, source[i], dest);
}
/* Stop character */
strcat(dest, "311");
expand(symbol, dest);
ustrcpy(symbol->text, source);
return error_number;
return c25_common(symbol, source, length, 80, C25MatrixTable, C25IataLogicStartStop, 307);
}
/* Code 2 of 5 Interleaved */
INTERNAL int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, j, error_number;
char bars[7], spaces[7], mixed[14], dest[512]; /* 4 + 90 * 5 + 3 + 1 = 458 */
#ifndef _MSC_VER
unsigned char temp[length + 2];
#else
unsigned char* temp = (unsigned char *) _alloca((length + 2) * sizeof (unsigned char));
#endif
char bars[7], spaces[7], mixed[14], dest[512]; /* 4 + (90 + 2) * 5 + 3 + 1 = 468 */
unsigned char temp[90 + 2 + 1];
int have_checkdigit = symbol->option_2 == 1 || symbol->option_2 == 2;
if (length > 90) {
strcpy(symbol->errtxt, "309: Input too long");
@ -203,13 +162,20 @@ INTERNAL int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char s
temp[0] = '\0';
/* Input must be an even number of characters for Interlaced 2 of 5 to work:
if an odd number of characters has been entered then add a leading zero */
if (length & 1) {
if an odd number of characters has been entered and no check digit or an even number and have check digit
then add a leading zero */
if (((length & 1) && !have_checkdigit) || (!(length & 1) && have_checkdigit)) {
ustrcpy(temp, "0");
length++;
}
ustrncat(temp, source, length);
if (have_checkdigit) {
/* Add standard GS1 check digit */
temp[length] = check_digit(gs1_checksum(temp, length));
temp[++length] = '\0';
}
/* start character */
strcpy(dest, "1111");
@ -236,17 +202,20 @@ INTERNAL int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char s
strcat(dest, "311");
expand(symbol, dest);
ustrcpy(symbol->text, temp);
if (symbol->option_2 == 2) {
/* Remove check digit from HRT */
symbol->text[length - 1] = '\0';
}
return error_number;
}
/* Interleaved 2-of-5 (ITF) */
INTERNAL int itf14(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number, zeroes;
unsigned int count;
char localstr[16];
count = 0;
unsigned char localstr[16] = {0};
if (length > 13) {
strcpy(symbol->errtxt, "311: Input too long");
@ -267,14 +236,7 @@ INTERNAL int itf14(struct zint_symbol *symbol, unsigned char source[], int lengt
ustrcpy(localstr + zeroes, source);
/* Calculate the check digit - the same method used for EAN-13 */
for (i = 12; i >= 0; i--) {
count += ctoi(localstr[i]);
if (!(i & 1)) {
count += 2 * ctoi(localstr[i]);
}
}
localstr[13] = check_digit(count);
localstr[13] = check_digit(gs1_checksum(localstr, 13));
localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, 14);
ustrcpy(symbol->text, localstr);
@ -295,7 +257,7 @@ INTERNAL int itf14(struct zint_symbol *symbol, unsigned char source[], int lengt
INTERNAL int dpleit(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number;
unsigned int count;
char localstr[16];
unsigned char localstr[16] = {0};
int zeroes;
count = 0;
@ -323,7 +285,7 @@ INTERNAL int dpleit(struct zint_symbol *symbol, unsigned char source[], int leng
}
localstr[13] = check_digit(count);
localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, 14);
error_number = interleaved_two_of_five(symbol, localstr, 14);
ustrcpy(symbol->text, localstr);
return error_number;
}
@ -332,7 +294,7 @@ INTERNAL int dpleit(struct zint_symbol *symbol, unsigned char source[], int leng
INTERNAL int dpident(struct zint_symbol *symbol, unsigned char source[], int length) {
int i, error_number, zeroes;
unsigned int count;
char localstr[16];
unsigned char localstr[16] = {0};
count = 0;
if (length > 11) {
@ -359,7 +321,7 @@ INTERNAL int dpident(struct zint_symbol *symbol, unsigned char source[], int len
}
localstr[11] = check_digit(count);
localstr[12] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *) localstr, 12);
error_number = interleaved_two_of_five(symbol, localstr, 12);
ustrcpy(symbol->text, localstr);
return error_number;
}