zint/backend/2of5.c

344 lines
8.0 KiB
C
Raw Normal View History

2008-07-14 09:15:55 +12:00
/* 2of5.c - Handles Code 2 of 5 barcodes */
/*
libzint - the open source barcode library
2008-11-17 21:47:42 +13:00
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
2008-07-14 09:15:55 +12:00
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#ifdef _MSC_VER
#include <malloc.h>
#endif
2008-07-14 09:15:55 +12:00
2012-12-30 07:37:03 +13:00
static const char *C25MatrixTable[10] = {"113311", "311131", "131131", "331111", "113131", "313111",
2008-07-14 09:15:55 +12:00
"133111", "111331", "311311", "131311"};
2012-12-30 07:37:03 +13:00
static const char *C25IndustTable[10] = {"1111313111", "3111111131", "1131111131", "3131111111", "1111311131",
2008-07-14 09:15:55 +12:00
"3111311111", "1131311111", "1111113131", "3111113111", "1131113111"};
2012-12-30 07:37:03 +13:00
static const char *C25InterTable[10] = {"11331", "31113", "13113", "33111", "11313", "31311", "13311", "11133",
2008-07-14 09:15:55 +12:00
"31131", "13131"};
static inline char check_digit(unsigned int count)
{
return itoc((10 - (count % 10)) % 10);
}
int matrix_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Code 2 of 5 Standard (Code 2 of 5 Matrix) */
2008-09-19 20:47:28 +12:00
int i, error_number;
2009-10-07 08:03:00 +13:00
char dest[512]; /* 6 + 80 * 6 + 6 + 1 ~ 512*/
2008-07-14 09:15:55 +12:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2012-12-30 07:37:03 +13:00
if(length > 80) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
/* start character */
2009-10-07 08:03:00 +13:00
strcpy(dest, "411111");
2008-07-14 09:15:55 +12:00
2009-10-07 08:03:00 +13:00
for(i = 0; i < length; i++) {
lookup(NEON, C25MatrixTable, source[i], dest);
2008-07-14 09:15:55 +12:00
}
/* Stop character */
concat (dest, "41111");
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
expand(symbol, dest);
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, source);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int industrial_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Code 2 of 5 Industrial */
2008-09-19 20:47:28 +12:00
int i, error_number;
2009-10-07 08:03:00 +13:00
char dest[512]; /* 6 + 40 * 10 + 6 + 1 */
2008-07-14 09:15:55 +12:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2012-12-30 07:37:03 +13:00
if(length > 45) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid character in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
/* start character */
2009-10-07 08:03:00 +13:00
strcpy(dest, "313111");
2008-07-14 09:15:55 +12:00
2009-10-07 08:03:00 +13:00
for(i = 0; i < length; i++) {
lookup(NEON, C25IndustTable, source[i], dest);
2008-07-14 09:15:55 +12:00
}
/* Stop character */
concat (dest, "31113");
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
expand(symbol, dest);
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, source);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int iata_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Code 2 of 5 IATA */
2008-09-19 20:47:28 +12:00
int i, error_number;
2009-10-07 08:03:00 +13:00
char dest[512]; /* 4 + 45 * 10 + 3 + 1 */
2012-12-30 07:37:03 +13:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2012-12-30 07:37:03 +13:00
if(length > 45) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
/* start */
2009-10-07 08:03:00 +13:00
strcpy(dest, "1111");
2008-07-14 09:15:55 +12:00
for(i = 0; i < length; i++) {
2009-10-07 08:03:00 +13:00
lookup(NEON, C25IndustTable, source[i], dest);
2008-07-14 09:15:55 +12:00
}
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
/* stop */
concat (dest, "311");
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
expand(symbol, dest);
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, source);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int logic_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Code 2 of 5 Data Logic */
2008-09-19 20:47:28 +12:00
int i, error_number;
2009-10-07 08:03:00 +13:00
char dest[512]; /* 4 + 80 * 6 + 3 + 1 */
2012-12-30 07:37:03 +13:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
if(length > 80) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
/* start character */
2009-10-07 08:03:00 +13:00
strcpy(dest, "1111");
2008-07-14 09:15:55 +12:00
2009-10-07 08:03:00 +13:00
for(i = 0; i < length; i++) {
lookup(NEON, C25MatrixTable, source[i], dest);
2008-07-14 09:15:55 +12:00
}
/* Stop character */
concat (dest, "311");
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
expand(symbol, dest);
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, source);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int interleaved_two_of_five(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Code 2 of 5 Interleaved */
2008-09-19 20:47:28 +12:00
int i, j, k, error_number;
2008-07-14 09:15:55 +12:00
char bars[7], spaces[7], mixed[14], dest[1000];
2009-11-03 04:47:08 +13:00
#ifndef _MSC_VER
unsigned char temp[length + 2];
#else
unsigned char* temp = (unsigned char *)_alloca((length + 2) * sizeof(unsigned char));
#endif
2008-07-14 09:15:55 +12:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2012-12-30 07:37:03 +13:00
2009-11-03 04:47:08 +13:00
if(length > 89) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if (error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2012-12-30 07:37:03 +13:00
2009-11-03 04:47:08 +13:00
ustrcpy(temp, (unsigned char *) "");
2008-07-14 09:15:55 +12:00
/* 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)
2008-07-14 09:15:55 +12:00
{
2009-11-03 04:47:08 +13:00
ustrcpy(temp, (unsigned char *) "0");
length++;
2008-07-14 09:15:55 +12:00
}
2009-11-03 04:47:08 +13:00
uconcat(temp, source);
2008-07-14 09:15:55 +12:00
/* start character */
2009-10-07 08:03:00 +13:00
strcpy(dest, "1111");
2008-07-14 09:15:55 +12:00
2009-11-03 04:47:08 +13:00
for(i = 0; i < length; i+=2 )
2008-07-14 09:15:55 +12:00
{
/* look up the bars and the spaces and put them in two strings */
strcpy(bars, "");
2009-10-07 08:03:00 +13:00
lookup(NEON, C25InterTable, temp[i], bars);
2008-07-14 09:15:55 +12:00
strcpy(spaces, "");
2009-10-07 08:03:00 +13:00
lookup(NEON, C25InterTable, temp[i + 1], spaces);
2008-07-14 09:15:55 +12:00
/* then merge (interlace) the strings together */
k = 0;
for(j = 0; j <= 4; j++)
{
mixed[k] = bars[j]; k++;
mixed[k] = spaces[j]; k++;
}
mixed[k] = '\0';
concat (dest, mixed);
}
/* Stop character */
concat (dest, "311");
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
expand(symbol, dest);
2009-10-07 08:03:00 +13:00
ustrcpy(symbol->text, temp);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int itf14(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{
int i, error_number, zeroes;
unsigned int count;
2009-10-07 08:03:00 +13:00
char localstr[16];
2012-12-30 07:37:03 +13:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
count = 0;
2012-12-30 07:37:03 +13:00
if(length > 13) {
strcpy(symbol->errtxt, "Input too long");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2012-12-30 07:37:03 +13:00
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid character in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2008-10-13 10:05:53 +13:00
/* Add leading zeros as required */
zeroes = 13 - length;
2008-10-13 10:05:53 +13:00
for(i = 0; i < zeroes; i++) {
2009-10-07 08:03:00 +13:00
localstr[i] = '0';
2008-10-13 10:05:53 +13:00
}
2009-10-07 08:03:00 +13:00
strcpy(localstr + zeroes, (char *)source);
2012-12-30 07:37:03 +13:00
2008-07-14 09:15:55 +12:00
/* Calculate the check digit - the same method used for EAN-13 */
for (i = 12; i >= 0; i--) {
2008-10-13 10:05:53 +13:00
count += ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
if (!(i & 1)) {
2008-10-13 10:05:53 +13:00
count += 2 * ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
}
}
localstr[13] = check_digit(count);
2009-10-07 08:03:00 +13:00
localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, (unsigned char*)localstr);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int dpleit(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Deutshe Post Leitcode */
2008-09-19 20:47:28 +12:00
int i, error_number;
unsigned int count;
2009-10-07 08:03:00 +13:00
char localstr[16];
2008-10-13 10:05:53 +13:00
int zeroes;
2008-07-14 09:15:55 +12:00
2008-09-19 20:47:28 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
count = 0;
if(length > 13) {
strcpy(symbol->errtxt, "Input wrong length");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
zeroes = 13 - length;
2008-10-13 10:05:53 +13:00
for(i = 0; i < zeroes; i++)
2009-10-07 08:03:00 +13:00
localstr[i] = '0';
strcpy(localstr + zeroes, (char *)source);
2008-10-13 10:05:53 +13:00
for (i = 12; i >= 0; i--)
2008-07-14 09:15:55 +12:00
{
2008-10-13 10:05:53 +13:00
count += 4 * ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
if (i & 1) {
2008-10-13 10:05:53 +13:00
count += 5 * ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
}
}
localstr[13] = check_digit(count);
2009-10-07 08:03:00 +13:00
localstr[14] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, (unsigned char*)localstr);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int dpident(struct zint_symbol *symbol, unsigned char source[], int length)
2008-07-14 09:15:55 +12:00
{ /* Deutsche Post Identcode */
2008-10-13 10:05:53 +13:00
int i, error_number, zeroes;
unsigned int count;
2009-10-07 08:03:00 +13:00
char localstr[16];
2008-07-14 09:15:55 +12:00
count = 0;
if(length > 11) {
strcpy(symbol->errtxt, "Input wrong length");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2009-10-07 08:03:00 +13:00
error_number = is_sane(NEON, source, length);
2008-09-19 20:47:28 +12:00
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data");
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
zeroes = 11 - length;
2008-10-13 10:05:53 +13:00
for(i = 0; i < zeroes; i++)
2009-10-07 08:03:00 +13:00
localstr[i] = '0';
strcpy(localstr + zeroes, (char *)source);
2008-10-13 10:05:53 +13:00
for (i = 10; i >= 0; i--)
2008-07-14 09:15:55 +12:00
{
2008-10-13 10:05:53 +13:00
count += 4 * ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
if (i & 1) {
2008-10-13 10:05:53 +13:00
count += 5 * ctoi(localstr[i]);
2008-07-14 09:15:55 +12:00
}
}
localstr[11] = check_digit(count);
2009-10-07 08:03:00 +13:00
localstr[12] = '\0';
error_number = interleaved_two_of_five(symbol, (unsigned char *)localstr, strlen(localstr));
2009-02-20 08:09:57 +13:00
ustrcpy(symbol->text, (unsigned char*)localstr);
2008-09-19 20:47:28 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}