zint/backend/postal.c

503 lines
13 KiB
C
Raw Normal View History

2008-07-14 09:15:55 +12:00
/* postal.c - Handles PostNet, PLANET, FIM. RM4SCC and Flattermarken */
/* Zint - A barcode generating program using libpng
2008-11-17 21:47:42 +13:00
Copyright (C) 2008 Robin Stuart <robin@zint.org.uk>
2008-07-19 02:35:32 +12:00
Including bug fixes by Bryan Hatton
2008-07-14 09:15:55 +12:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#define BESET "ABCD"
#define KRSET "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
/* PostNet number encoding table - In this table L is long as S is short */
static char *PNTable[10] = {"LLSSS", "SSSLL", "SSLSL", "SSLLS", "SLSSL", "SLSLS", "SLLSS", "LSSSL",
"LSSLS", "LSLSS"};
static char *PLTable[10] = {"SSLLL", "LLLSS", "LLSLS", "LLSSL", "LSLLS", "LSLSL", "LSSLL", "SLLLS",
"SLLSL", "SLSLL"};
static char *FIMTable[4] = {"12121112121", "111112111211111", "121111111111121", "13111111131"};
static char *RoyalValues[36] = {"11", "12", "13", "14", "15", "10", "21", "22", "23", "24", "25",
"20", "31", "32", "33", "34", "35", "30", "41", "42", "43", "44", "45", "40", "51", "52",
"53", "54", "55", "50", "01", "02", "03", "04", "05", "00"};
2008-09-16 19:46:22 +12:00
/* 0 = Full, 1 = Ascender, 2 = Descender, 3 = Tracker */
2008-07-14 09:15:55 +12:00
static char *RoyalTable[36] = {"3300", "3210", "3201", "2310", "2301", "2211", "3120", "3030", "3021",
"2130", "2121", "2031", "3102", "3012", "3003", "2112", "2103", "2013", "1320", "1230",
"1221", "0330", "0321", "0231", "1302", "1212", "1203", "0312", "0303", "0213", "1122",
"1032", "1023", "0132", "0123", "0033"};
2008-09-03 07:47:26 +12:00
2008-07-14 09:15:55 +12:00
static char *FlatTable[10] = {"0504", "18", "0117", "0216", "0315", "0414", "0513", "0612", "0711",
"0810"};
2008-12-08 09:11:50 +13:00
static char *KoreaTable[10] = {"1313150613", "0713131313", "0417131313", "1506131313",
"0413171313", "17171313", "1315061313", "0413131713", "17131713", "13171713"};
2008-07-14 09:15:55 +12:00
int postnet(struct zint_symbol *symbol, unsigned char source[], char dest[])
{
/* Handles the PostNet system used for Zip codes in the US */
unsigned int i, sum, check_digit;
2008-09-16 19:46:22 +12:00
int error_number, h;
2008-07-14 09:15:55 +12:00
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
2008-10-17 08:23:54 +13:00
if(ustrlen(source) > 38) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [401]");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(NESET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [402]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
sum = 0;
/* start character */
concat (dest, "L");
2008-10-01 04:05:53 +13:00
for (i=0; i < ustrlen(source); i++)
2008-07-14 09:15:55 +12:00
{
lookup(NESET, PNTable, source[i], dest);
sum += ctoi(source[i]);
}
2008-07-19 02:35:32 +12:00
check_digit = (10 - (sum%10))%10;
2008-07-14 09:15:55 +12:00
concat(dest, PNTable[check_digit]);
/* stop character */
concat (dest, "L");
2008-10-01 04:05:53 +13:00
h = ustrlen(source);
2008-07-14 09:15:55 +12:00
source[h] = itoc(check_digit);
source[h + 1] = '\0';
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int post_plot(struct zint_symbol *symbol, unsigned char source[])
{
/* Puts PostNet barcodes into the pattern matrix */
char height_pattern[200];
unsigned int loopey;
int writer;
strcpy(height_pattern, "");
2008-09-16 19:46:22 +12:00
int error_number;
2008-07-14 09:15:55 +12:00
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
2008-09-16 19:46:22 +12:00
error_number = postnet(symbol, source, height_pattern);
if(error_number != 0) {
return error_number;
2008-07-14 09:15:55 +12:00
}
writer = 0;
for(loopey = 0; loopey < strlen(height_pattern); loopey++)
{
if(height_pattern[loopey] == 'L')
{
symbol->encoded_data[0][writer] = '1';
}
symbol->encoded_data[1][writer] = '1';
2008-07-19 02:35:32 +12:00
writer += 3;
2008-07-14 09:15:55 +12:00
}
symbol->row_height[0] = 6;
symbol->row_height[1] = 6;
symbol->rows = 2;
symbol->width = writer - 1;
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int planet(struct zint_symbol *symbol, unsigned char source[], char dest[])
{
/* Handles the PLANET system used for item tracking in the US */
unsigned int i, sum, check_digit;
2008-09-16 19:46:22 +12:00
int error_number, h;
2008-07-14 09:15:55 +12:00
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
2008-10-17 08:23:54 +13:00
if(ustrlen(source) > 38) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [821]");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(NESET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [822]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
sum = 0;
/* start character */
concat (dest, "L");
2008-10-01 04:05:53 +13:00
for (i=0; i < ustrlen(source); i++)
2008-07-14 09:15:55 +12:00
{
lookup(NESET, PLTable, source[i], dest);
sum += ctoi(source[i]);
}
2008-07-19 02:35:32 +12:00
check_digit = (10 - (sum%10))%10;
2008-07-14 09:15:55 +12:00
concat(dest, PLTable[check_digit]);
/* stop character */
concat (dest, "L");
2008-10-01 04:05:53 +13:00
h = ustrlen(source);
2008-07-14 09:15:55 +12:00
source[h] = itoc(check_digit);
source[h + 1] = '\0';
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
int planet_plot(struct zint_symbol *symbol, unsigned char source[])
{
/* Puts PLANET barcodes into the pattern matrix */
char height_pattern[200];
unsigned int loopey;
int writer;
2008-09-16 19:46:22 +12:00
int error_number;
2008-07-14 09:15:55 +12:00
strcpy(height_pattern, "");
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
2008-09-16 19:46:22 +12:00
error_number = planet(symbol, source, height_pattern);
if(error_number != 0) {
return error_number;
2008-07-14 09:15:55 +12:00
}
writer = 0;
for(loopey = 0; loopey < strlen(height_pattern); loopey++)
{
if(height_pattern[loopey] == 'L')
{
symbol->encoded_data[0][writer] = '1';
}
symbol->encoded_data[1][writer] = '1';
2008-07-19 02:35:32 +12:00
writer += 3;
2008-07-14 09:15:55 +12:00
}
symbol->row_height[0] = 6;
symbol->row_height[1] = 6;
symbol->rows = 2;
symbol->width = writer - 1;
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2008-12-08 09:11:50 +13:00
int korea_post(struct zint_symbol *symbol, unsigned char source[])
{ /* Korean Postal Authority */
int total, h, loop, check, zeroes, error_number;
char localstr[7], checkstr[3], dest[80];
error_number = 0;
h = ustrlen(source);
if(h > 6) {
strcpy(symbol->errtxt, "Input too long [771]");
return ERROR_TOO_LONG;
}
error_number = is_sane(NESET, source);
if(error_number == ERROR_INVALID_DATA) {
strcpy(symbol->errtxt, "Invalid characters in data [772]");
return error_number;
}
strcpy(localstr, "");
zeroes = 6 - h;
for(loop = 0; loop < zeroes; loop++)
concat(localstr, "0");
concat(localstr, (char *)source);
total = 0;
for(loop = 0; loop < 6; loop++) {
total += ctoi(localstr[loop]);
}
check = 10 - (total % 10);
2008-12-16 20:41:35 +13:00
if(check == 10) { check = 0; }
2008-12-08 09:11:50 +13:00
checkstr[0] = itoc(check);
checkstr[1] = '\0';
concat(localstr, checkstr);
strcpy(dest, "");
for(loop = 5; loop >= 0; loop--) {
lookup(NESET, KoreaTable, localstr[loop], dest);
}
lookup(NESET, KoreaTable, localstr[6], dest);
expand(symbol, dest);
strcpy(symbol->text, localstr);
return error_number;
}
2008-07-14 09:15:55 +12:00
int fim(struct zint_symbol *symbol, unsigned char source[])
{
/* The simplest barcode symbology ever! Supported by MS Word, so here it is! */
/* glyphs from http://en.wikipedia.org/wiki/Facing_Identification_Mark */
2008-09-16 19:46:22 +12:00
int error_number;
2008-07-14 09:15:55 +12:00
char dest[17];
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
strcpy(dest, "");
to_upper(source);
2008-10-01 04:05:53 +13:00
if(ustrlen(source) > 1) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [491]");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(BESET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [492]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
lookup(BESET, FIMTable, source[0], dest);
expand(symbol, dest);
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
char rm4scc(char source[], unsigned char dest[])
{
/* Handles the 4 State barcodes used in the UK by Royal Mail */
unsigned int i;
int top, bottom, row, column, check_digit;
char values[3], set_copy[38];
strcpy(set_copy, KRSET);
top = 0;
bottom = 0;
/* start character */
2008-10-03 21:31:53 +13:00
concat ((char*)dest, "1");
2008-07-14 09:15:55 +12:00
for (i=0; i < strlen(source); i++) {
2008-10-03 21:31:53 +13:00
lookup(KRSET, RoyalTable, source[i], (char*)dest);
2008-07-14 09:15:55 +12:00
strcpy(values, RoyalValues[posn(KRSET, source[i])]);
top += ctoi(values[0]);
bottom += ctoi(values[1]);
}
/* Calculate the check digit */
row = (top % 6) - 1;
column = (bottom % 6) - 1;
if(row == -1) { row = 5; }
if(column == -1) { column = 5; }
check_digit = (6 * row) + column;
2008-10-03 21:31:53 +13:00
concat((char*)dest, RoyalTable[check_digit]);
2008-07-14 09:15:55 +12:00
/* stop character */
2008-10-03 21:31:53 +13:00
concat ((char*)dest, "0");
2008-07-14 09:15:55 +12:00
return set_copy[check_digit];
}
int royal_plot(struct zint_symbol *symbol, unsigned char source[])
{
/* Puts RM4SCC into the data matrix */
char height_pattern[200], check;
unsigned int loopey;
int writer;
2008-09-16 19:46:22 +12:00
int error_number, h;
2008-07-14 09:15:55 +12:00
strcpy(height_pattern, "");
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
to_upper(source);
2008-10-01 04:05:53 +13:00
if(ustrlen(source) > 120) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [701]");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(KRSET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [702]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2008-10-03 21:31:53 +13:00
check = rm4scc((char*)source, (unsigned char*)height_pattern);
2008-07-14 09:15:55 +12:00
writer = 0;
for(loopey = 0; loopey < strlen(height_pattern); loopey++)
{
if((height_pattern[loopey] == '1') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[0][writer] = '1';
}
symbol->encoded_data[1][writer] = '1';
if((height_pattern[loopey] == '2') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[2][writer] = '1';
}
writer += 2;
}
symbol->row_height[0] = 4;
symbol->row_height[1] = 2;
symbol->row_height[2] = 4;
symbol->rows = 3;
symbol->width = writer - 1;
2008-10-01 04:05:53 +13:00
h = ustrlen(source);
2008-07-14 09:15:55 +12:00
source[h] = check;
source[h + 1] = '\0';
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2008-09-03 07:47:26 +12:00
int kix_code(struct zint_symbol *symbol, unsigned char source[])
{
/* Handles Dutch Post TNT KIX symbols */
/* The same as RM4SCC but without check digit */
/* Specification at http://www.tntpost.nl/zakelijk/klantenservice/downloads/kIX_code/download.aspx */
2008-10-13 10:05:53 +13:00
char height_pattern[50], localstr[13];
2008-09-03 07:47:26 +12:00
unsigned int loopey;
int writer, i;
2008-10-13 10:05:53 +13:00
int error_number, zeroes;
2008-09-03 07:47:26 +12:00
strcpy(height_pattern, "");
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-09-03 07:47:26 +12:00
to_upper(source);
2008-10-13 10:05:53 +13:00
if(ustrlen(source) > 11) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [901]");
2008-09-03 07:47:26 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(KRSET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [902]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-09-03 07:47:26 +12:00
}
2008-10-13 10:05:53 +13:00
/* Add leading zeroes */
strcpy(localstr, "");
zeroes = 11 - ustrlen(source);
for(i = 0; i < zeroes; i++)
concat(localstr, "0");
concat(localstr, (char *)source);
/* Encode data */
for (i = 0; i < 11; i++) {
lookup(KRSET, RoyalTable, localstr[i], height_pattern);
2008-09-03 07:47:26 +12:00
}
writer = 0;
for(loopey = 0; loopey < strlen(height_pattern); loopey++)
{
if((height_pattern[loopey] == '1') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[0][writer] = '1';
}
symbol->encoded_data[1][writer] = '1';
if((height_pattern[loopey] == '2') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[2][writer] = '1';
}
writer += 2;
}
symbol->row_height[0] = 4;
symbol->row_height[1] = 2;
symbol->row_height[2] = 4;
symbol->rows = 3;
symbol->width = writer - 1;
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
}
int daft_code(struct zint_symbol *symbol, unsigned char source[])
{
/* Handles DAFT Code symbols */
/* Presumably 'daft' doesn't mean the same thing in Germany as it does in the UK! */
int input_length;
2008-09-20 17:47:37 +12:00
char height_pattern[100], local_source[55];
2008-09-16 19:46:22 +12:00
unsigned int loopey;
int writer, i;
strcpy(height_pattern, "");
2008-10-01 04:05:53 +13:00
input_length = ustrlen(source);
2008-10-03 21:31:53 +13:00
strcpy(local_source, (char*)source);
2008-09-20 17:47:37 +12:00
if(input_length > 50) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [931]");
2008-09-16 19:46:22 +12:00
return ERROR_TOO_LONG;
}
2008-10-03 21:31:53 +13:00
to_upper((unsigned char*)local_source);
2008-09-16 19:46:22 +12:00
for (i = 0; i < input_length; i++) {
2008-09-20 17:47:37 +12:00
if(local_source[i] == 'D') { concat(height_pattern, "2"); }
if(local_source[i] == 'A') { concat(height_pattern, "1"); }
if(local_source[i] == 'F') { concat(height_pattern, "0"); }
if(local_source[i] == 'T') { concat(height_pattern, "3"); }
2008-09-16 19:46:22 +12:00
}
writer = 0;
for(loopey = 0; loopey < strlen(height_pattern); loopey++)
{
if((height_pattern[loopey] == '1') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[0][writer] = '1';
}
symbol->encoded_data[1][writer] = '1';
if((height_pattern[loopey] == '2') || (height_pattern[loopey] == '0'))
{
symbol->encoded_data[2][writer] = '1';
}
writer += 2;
}
2008-09-20 17:47:37 +12:00
2008-09-16 19:46:22 +12:00
symbol->row_height[0] = 4;
symbol->row_height[1] = 2;
symbol->row_height[2] = 4;
symbol->rows = 3;
symbol->width = writer - 1;
strcpy(symbol->text, "");
return 0;
2008-09-03 07:47:26 +12:00
}
2008-07-14 09:15:55 +12:00
int flattermarken(struct zint_symbol *symbol, unsigned char source[])
{ /* Flattermarken - Not really a barcode symbology and (in my opinion) probably not much use
but it's supported by TBarCode so it's supported by Zint! */
2008-09-16 19:46:22 +12:00
int loop, error_number;
2008-07-14 09:15:55 +12:00
char dest[1000];
2008-09-16 19:46:22 +12:00
error_number = 0;
2008-07-14 09:15:55 +12:00
strcpy(dest, "");
2008-10-01 04:05:53 +13:00
if(ustrlen(source) > 90) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Input too long [281]");
2008-07-14 09:15:55 +12:00
return ERROR_TOO_LONG;
}
2008-09-16 19:46:22 +12:00
error_number = is_sane(NESET, source);
if(error_number == ERROR_INVALID_DATA) {
2008-10-05 18:51:58 +13:00
strcpy(symbol->errtxt, "Invalid characters in data [282]");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}
2008-10-01 04:05:53 +13:00
for(loop = 0; loop < ustrlen(source); loop++) {
2008-07-14 09:15:55 +12:00
lookup(NESET, FlatTable, source[loop], dest);
}
expand(symbol, dest);
strcpy(symbol->text, "");
2008-09-16 19:46:22 +12:00
return error_number;
2008-07-14 09:15:55 +12:00
}