#include <assert.h>
#include <stdio.h>
#include "common.h"
#include "large.h"
#include "reedsol.h"
#define RUBIDIUM_F (IS_NUM_F | IS_UPR_F | IS_SPC_F)
#define SET_A "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define SET_L "ABDEFGHJLNPQRSTUWXYZ"
#define SET_N "0123456789"
#define SET_S " "
static const char mailmark_postcode_format[6][9] = {
{'A','N','A','N','L','L','N','L','S'}, {'A','A','N','N','L','L','N','L','S'},
{'A','A','N','N','N','L','L','N','L'}, {'A','A','N','A','N','L','L','N','L'},
{'A','N','N','L','L','N','L','S','S'}, {'A','N','N','N','L','L','N','L','S'}
};
static const unsigned char mailmark_data_symbol_odd[32] = {
0x01, 0x02, 0x04, 0x07, 0x08, 0x0B, 0x0D, 0x0E, 0x10, 0x13, 0x15, 0x16,
0x19, 0x1A, 0x1C, 0x1F, 0x20, 0x23, 0x25, 0x26, 0x29, 0x2A, 0x2C, 0x2F,
0x31, 0x32, 0x34, 0x37, 0x38, 0x3B, 0x3D, 0x3E
};
static const unsigned char mailmark_data_symbol_even[30] = {
0x03, 0x05, 0x06, 0x09, 0x0A, 0x0C, 0x0F, 0x11, 0x12, 0x14, 0x17, 0x18,
0x1B, 0x1D, 0x1E, 0x21, 0x22, 0x24, 0x27, 0x28, 0x2B, 0x2D, 0x2E, 0x30,
0x33, 0x35, 0x36, 0x39, 0x3A, 0x3C
};
static const unsigned char mailmark_extender_group_c[22] = {
3, 5, 7, 11, 13, 14, 16, 17, 19, 0, 1, 2, 4, 6, 8, 9, 10, 12, 15, 18, 20, 21
};
static const unsigned char mailmark_extender_group_l[26] = {
2, 5, 7, 8, 13, 14, 15, 16, 21, 22, 23, 0, 1, 3, 4, 6, 9, 10, 11, 12, 17, 18, 19, 20, 24, 25
};
static int mailmark_verify_character(const char input, const char type, const int is_2d) {
int val = 0;
switch (type) {
case 'A':
val = z_posn(SET_A, input);
break;
case 'L':
val = z_posn(is_2d ? SET_A : SET_L, input);
break;
case 'N':
val = z_posn(SET_N, input);
break;
case 'S':
val = z_posn(SET_S, input);
break;
}
return val != -1;
}
static int mailmark_verify_postcode(const char postcode[10], const int length, int *p_postcode_type) {
const int is_2d = !p_postcode_type;
int postcode_type;
assert(length == 9 || (length >= 2 && length <= 4));
if (length >= 4 && memcmp(postcode, "XY11 ", length) == 0) {
postcode_type = 7;
} else {
if (length == 2 || (length == 9 && postcode[7] == ' ')) {
postcode_type = 5;
} else {
if (length == 3 || (length == 9 && postcode[8] == ' ')) {
if (z_isdigit(postcode[1])) {
if (z_isdigit(postcode[2])) {
postcode_type = 6;
} else {
postcode_type = 1;
}
} else {
postcode_type = 2;
}
} else {
assert(length >= 4);
if (z_isdigit(postcode[3])) {
postcode_type = 3;
} else {
postcode_type = 4;
}
}
}
}
if (p_postcode_type) {
*p_postcode_type = postcode_type;
}
if (postcode_type != 7) {
int i;
const char *const pattern = mailmark_postcode_format[postcode_type - 1];
for (i = 0; i < length; i++) {
if (!mailmark_verify_character(postcode[i], pattern[i], is_2d)) {
return 1;
}
}
}
return 0;
}
INTERNAL int zint_daft_set_height(struct zint_symbol *symbol, const float min_height, const float max_height);
INTERNAL int zint_mailmark_4s(struct zint_symbol *symbol, unsigned char source[], int length) {
unsigned char local_source[28];
int format;
int version_id;
int mail_class;
int supply_chain_id;
unsigned int item_id;
char postcode[10];
int postcode_type;
large_uint destination_postcode;
large_uint b;
large_uint cdv;
unsigned char data[26];
int data_top, data_step;
unsigned char check[7];
unsigned int extender[27];
char bar[80];
char *d = bar;
int check_count;
int i, j, len;
rs_t rs;
int error_number = 0;
const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;
if (length > 26) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 580, "Input length %d too long (maximum 26)", length);
}
memcpy(local_source, source, length);
if (length < 22) {
if (length < 14) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 588, "Input length %d too short (minimum 14)", length);
}
memset(local_source + length, ' ', 22 - length);
length = 22;
} else if (length > 22 && length < 26) {
memset(local_source + length, ' ', 26 - length);
length = 26;
}
z_to_upper(local_source, length);
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("Producing 4-state Mailmark (%d): %.*s<end>\n", length, length, local_source);
}
if ((i = z_not_sane(RUBIDIUM_F, local_source, length))) {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 581,
"Invalid character at position %d in input (alphanumerics and space only)", i);
}
format = z_ctoi(local_source[0]);
if (format < 0 || format > 4) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 582, "Format (1st character) out of range (0 to 4)");
}
version_id = z_ctoi(local_source[1]) - 1;
if (version_id < 0 || version_id > 3) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 583, "Version ID (2nd character) out of range (1 to 4)");
}
mail_class = z_ctoi(local_source[2]);
if (mail_class < 0 || mail_class > 14) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 584,
"Class (3rd character) out of range (0 to 9 and A to E)");
}
supply_chain_id = 0;
for (i = 3; i < (length - 17); i++) {
if (z_isdigit(local_source[i])) {
supply_chain_id *= 10;
supply_chain_id += z_ctoi(local_source[i]);
} else {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 585,
"Invalid Supply Chain ID at character %d (digits only)", i);
}
}
item_id = 0;
for (i = length - 17; i < (length - 9); i++) {
if (z_isdigit(local_source[i])) {
item_id *= 10;
item_id += z_ctoi(local_source[i]);
} else {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 586,
"Invalid Item ID at character %d (digits only)", i);
}
}
for (i = 0; i < 9; i++) {
postcode[i] = local_source[(length - 9) + i];
}
if (mailmark_verify_postcode(postcode, 9, &postcode_type) != 0) {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 587, "Invalid postcode \"%.9s\"", postcode);
}
zint_large_load_u64(&destination_postcode, 0);
if (postcode_type != 7) {
const char *const pattern = mailmark_postcode_format[postcode_type - 1];
zint_large_load_u64(&b, 0);
for (i = 0; i < 9; i++) {
switch (pattern[i]) {
case 'A':
zint_large_mul_u64(&b, 26);
zint_large_add_u64(&b, z_posn(SET_A, postcode[i]));
break;
case 'L':
zint_large_mul_u64(&b, 20);
zint_large_add_u64(&b, z_posn(SET_L, postcode[i]));
break;
case 'N':
zint_large_mul_u64(&b, 10);
zint_large_add_u64(&b, z_posn(SET_N, postcode[i]));
break;
}
}
zint_large_load(&destination_postcode, &b);
zint_large_load_u64(&b, 1);
if (postcode_type == 1) {
zint_large_add(&destination_postcode, &b);
}
zint_large_add_u64(&b, 5408000000);
if (postcode_type == 2) {
zint_large_add(&destination_postcode, &b);
}
zint_large_add_u64(&b, 5408000000);
if (postcode_type == 3) {
zint_large_add(&destination_postcode, &b);
}
zint_large_add_u64(&b, 54080000000);
if (postcode_type == 4) {
zint_large_add(&destination_postcode, &b);
}
zint_large_add_u64(&b, 140608000000);
if (postcode_type == 5) {
zint_large_add(&destination_postcode, &b);
}
zint_large_add_u64(&b, 208000000);
if (postcode_type == 6) {
zint_large_add(&destination_postcode, &b);
}
}
zint_large_load_u64(&cdv, 0);
zint_large_add(&cdv, &destination_postcode);
zint_large_mul_u64(&cdv, 100000000);
zint_large_add_u64(&cdv, item_id);
if (length == 22) {
zint_large_mul_u64(&cdv, 100);
} else {
zint_large_mul_u64(&cdv, 1000000);
}
zint_large_add_u64(&cdv, supply_chain_id);
zint_large_mul_u64(&cdv, 15);
zint_large_add_u64(&cdv, mail_class);
zint_large_mul_u64(&cdv, 5);
zint_large_add_u64(&cdv, format);
zint_large_mul_u64(&cdv, 4);
zint_large_add_u64(&cdv, version_id);
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("DPC type %d\n", postcode_type);
fputs("CDV: ", stdout);
zint_large_print(&cdv);
}
if (length == 22) {
data_top = 15;
data_step = 8;
check_count = 6;
} else {
data_top = 18;
data_step = 10;
check_count = 7;
}
for (j = data_top; j >= (data_step + 1); j--) {
data[j] = (unsigned char) zint_large_div_u64(&cdv, 32);
}
for (j = data_step; j >= 0; j--) {
data[j] = (unsigned char) zint_large_div_u64(&cdv, 30);
}
zint_rs_init_gf(&rs, 0x25);
zint_rs_init_code(&rs, check_count, 1);
data_top++;
zint_rs_encode(&rs, data_top, data, check);
memcpy(data + data_top, check, check_count);
if (symbol->debug & ZINT_DEBUG_PRINT) {
fputs("Codewords:", stdout);
for (i = 0; i < data_top + check_count; i++) {
printf(" %d", (int) data[i]);
}
fputc('\n', stdout);
}
for (i = 0; i <= data_step; i++) {
data[i] = mailmark_data_symbol_even[data[i]];
}
for (i = data_step + 1; i < (data_top + check_count); i++) {
data[i] = mailmark_data_symbol_odd[data[i]];
}
for (i = 0; i < length; i++) {
if (length == 22) {
extender[mailmark_extender_group_c[i]] = data[i];
} else {
extender[mailmark_extender_group_l[i]] = data[i];
}
}
for (i = 0; i < length; i++) {
for (j = 0; j < 3; j++) {
switch (extender[i] & 0x24) {
case 0x24:
*d++ = 'F';
break;
case 0x20:
if (i % 2) {
*d++ = 'D';
} else {
*d++ = 'A';
}
break;
case 0x04:
if (i % 2) {
*d++ = 'A';
} else {
*d++ = 'D';
}
break;
default:
*d++ = 'T';
break;
}
extender[i] = extender[i] << 1;
}
}
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("Bar pattern: %.*s\n", (int) (d - bar), bar);
}
j = 0;
for (i = 0, len = d - bar; i < len; i++) {
if (bar[i] == 'F' || bar[i] == 'A') {
z_set_module(symbol, 0, j);
}
z_set_module(symbol, 1, j);
if (bar[i] == 'F' || bar[i] == 'D') {
z_set_module(symbol, 2, j);
}
j += 2;
}
if (symbol->output_options & COMPLIANT_HEIGHT) {
const float min_height = 6.47952747f;
const float max_height = 10.8062992f;
symbol->row_height[0] = 3.16417313f;
symbol->row_height[1] = 2.16496062f;
error_number = zint_daft_set_height(symbol, min_height, max_height);
} else {
symbol->row_height[0] = 4.0f;
symbol->row_height[1] = 2.0f;
(void) zint_daft_set_height(symbol, 0.0f, 0.0f);
}
symbol->rows = 3;
symbol->width = j - 1;
if (content_segs && z_ct_cpy(symbol, local_source, length)) {
return ZINT_ERROR_MEMORY;
}
return error_number;
}
INTERNAL int zint_datamatrix(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count);
INTERNAL int zint_mailmark_2d(struct zint_symbol *symbol, unsigned char source[], int length) {
static const char spaces[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
unsigned char local_source[90 + 1];
char postcode[10];
int i;
struct zint_seg segs[1];
const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;
if (length > 90) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 589, "Input length %d too long (maximum 90)", length);
}
if (length < 28) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 860, "Input length %d too short (minimum 28)", length);
}
memcpy(local_source, source, 4);
z_to_upper(local_source, 3);
if (memcmp(local_source, "JGB ", 4) != 0) {
if (length > 86) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 861, "Input length %d too long (maximum 86)", length);
}
memcpy(local_source, "JGB ", 4);
memcpy(local_source + 4, source, length);
length += 4;
} else {
memcpy(local_source, source, length);
}
if (length < 32) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 862, "Input length %d too short (minimum 32)", length);
}
if (length < 39) {
memset(local_source + length, ' ', 39 - length);
length = 39;
}
z_to_upper(local_source, 39);
if (length < 45) {
memset(local_source + length, ' ', 45 - length);
length = 45;
}
local_source[length] = '\0';
if (symbol->option_2) {
if (symbol->option_2 != 8 && symbol->option_2 != 10 && symbol->option_2 != 30) {
return z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 863, "Invalid Version '%d' (8, 10 or 30 only)",
symbol->option_2);
}
if (symbol->option_2 == 8) {
if (length > 51) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 864,
"Input length %d too long for Version 8 (maximum 51)", length);
}
} else if (symbol->option_2 == 30) {
if (length > 70) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 865,
"Input length %d too long for Version 30 (maximum 70)", length);
}
}
} else {
if (length <= 51) {
symbol->option_2 = 8;
} else if (length <= 70 && (symbol->option_3 & 0x7F) != DM_SQUARE) {
symbol->option_2 = 30;
} else {
symbol->option_2 = 10;
}
}
if (symbol->debug & ZINT_DEBUG_PRINT) {
printf("Producing 2D Mailmark %d (%d): %s<end>\n", symbol->option_2, length, local_source);
}
if ((i = z_not_sane(RUBIDIUM_F, local_source, 45))) {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 866,
"Invalid character at position %d in input (alphanumerics and space only in first 45)", i);
}
if (local_source[4] == ' ') {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 867, "Invalid Information Type ID (cannot be space)");
}
if (local_source[5] != '1') {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 868, "Invalid Version ID (\"1\" only)");
}
if (local_source[6] == ' ') {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 869, "Invalid Class (cannot be space)");
}
if (z_cnt_digits(local_source, length, 7, 7) != 7) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 870, "Invalid Supply Chain ID (7 digits only)");
}
if (z_cnt_digits(local_source, length, 14, 8) != 8) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 871, "Invalid Item ID (8 digits only)");
}
if (memcmp(local_source + 22, spaces, 9) != 0) {
for (i = 0; i < 9; i++) {
postcode[i] = local_source[22 + i];
}
for (i = 8; i >= 0 && postcode[i] == ' '; i--);
i++;
if (mailmark_verify_postcode(postcode, i >= 2 && i <= 4 ? i : 9, NULL ) != 0) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 872, "Invalid Destination Post Code plus DPS");
}
}
if (local_source[31] < '0' || local_source[31] > '6') {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 873, "Invalid Service Type (\"0\" to \"6\" only)");
}
if (memcmp(local_source + 32, spaces, 7) != 0) {
for (i = 0; i < 7; i++) {
postcode[i] = local_source[32 + i];
}
for (i = 6; postcode[i] == ' '; i--);
i++;
postcode[i++] = '1';
postcode[i++] = 'A';
while (i != 9) {
postcode[i++] = ' ';
}
if (mailmark_verify_postcode(postcode, 9, NULL ) != 0) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 874, "Invalid Return to Sender Post Code");
}
}
if (memcmp(local_source + 39, spaces, 6) != 0) {
return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 875, "Invalid Reserved field (must be spaces only)");
}
segs[0].eci = 0;
segs[0].source = local_source;
segs[0].length = length;
if (content_segs) {
if ((symbol->input_mode & 0x07) == DATA_MODE) {
if (z_ct_cpy(symbol, local_source, length)) {
return ZINT_ERROR_MEMORY;
}
} else if (z_ct_cpy_iso8859_1(symbol, local_source, length)) {
return ZINT_ERROR_MEMORY;
}
}
return zint_datamatrix(symbol, segs, 1);
}