#include <assert.h>
#include <stdio.h>
#include "common.h"
#define PLESS_SSET_F (IS_NUM_F | IS_UHX_F)
static const char PlessTable[16][8] = {
{'1','3','1','3','1','3','1','3'}, {'3','1','1','3','1','3','1','3'}, {'1','3','3','1','1','3','1','3'},
{'3','1','3','1','1','3','1','3'}, {'1','3','1','3','3','1','1','3'}, {'3','1','1','3','3','1','1','3'},
{'1','3','3','1','3','1','1','3'}, {'3','1','3','1','3','1','1','3'}, {'1','3','1','3','1','3','3','1'},
{'3','1','1','3','1','3','3','1'}, {'1','3','3','1','1','3','3','1'}, {'3','1','3','1','1','3','3','1'},
{'1','3','1','3','3','1','3','1'}, {'3','1','1','3','3','1','3','1'}, {'1','3','3','1','3','1','3','1'},
{'3','1','3','1','3','1','3','1'}
};
static const char MSITable[10][8] = {
{'1','2','1','2','1','2','1','2'}, {'1','2','1','2','1','2','2','1'}, {'1','2','1','2','2','1','1','2'},
{'1','2','1','2','2','1','2','1'}, {'1','2','2','1','1','2','1','2'}, {'1','2','2','1','1','2','2','1'},
{'1','2','2','1','2','1','1','2'}, {'1','2','2','1','2','1','2','1'}, {'2','1','1','2','1','2','1','2'},
{'2','1','1','2','1','2','2','1'}
};
INTERNAL int zint_plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
int i;
unsigned char checkptr[67 * 4 + 8] = {0};
static const char grid[9] = {1, 1, 1, 1, 0, 1, 0, 0, 1};
char dest[570];
char *d = dest;
unsigned int check_digits = 0;
char c1, c2;
int error_number = 0;
const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;
if (length > 67) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 370, "Input length %d too long (maximum 67)", length);
}
if ((i = z_not_sane(PLESS_SSET_F, source, length))) {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 371,
"Invalid character at position %d in input (digits and \"ABCDEF\" only)", i);
}
memcpy(d, "31311331", 8);
d += 8;
for (i = 0; i < length; i++, d += 8) {
const unsigned int check = source[i] - '0' - (source[i] >> 6) * 7;
memcpy(d, PlessTable[check], 8);
checkptr[4 * i] = check & 1;
checkptr[4 * i + 1] = (check >> 1) & 1;
checkptr[4 * i + 2] = (check >> 2) & 1;
checkptr[4 * i + 3] = (check >> 3) & 1;
}
for (i = 0; i < (4 * length); i++) {
if (checkptr[i]) {
int j;
for (j = 0; j < 9; j++)
checkptr[i + j] ^= grid[j];
}
}
for (i = 0; i < 8; i++) {
switch (checkptr[length * 4 + i]) {
case 0:
memcpy(d, "13", 2);
d += 2;
break;
case 1:
memcpy(d, "31", 2);
d += 2;
check_digits |= (1 << i);
break;
}
}
memcpy(d, "331311313", 9);
d += 9;
z_expand(symbol, dest, d - dest);
c1 = (char) z_xtoc(check_digits & 0xF);
c2 = (char) z_xtoc(check_digits >> 4);
z_hrt_cpy_nochk(symbol, source, length);
if (symbol->option_2 == 1) {
z_hrt_cat_chr_nochk(symbol, c1);
z_hrt_cat_chr_nochk(symbol, c2);
}
if (content_segs && z_ct_printf_256(symbol, "%.*s%c%c", length, source, c1, c2)) {
return ZINT_ERROR_MEMORY;
}
return error_number;
}
static char msi_check_digit_mod10(const unsigned char source[], const int length) {
static const char vals[2][10] = {
{ 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
};
int i, x = 0, undoubled = 0;
for (i = length - 1; i >= 0; i--) {
x += vals[undoubled][z_ctoi(source[i])];
undoubled = !undoubled;
}
return z_itoc((10 - x % 10) % 10);
}
static char msi_check_digit_mod11(const unsigned char source[], const int length, const int wrap) {
int i, x = 0, weight = 2;
for (i = length - 1; i >= 0; i--) {
x += weight * z_ctoi(source[i]);
weight++;
if (weight > wrap) {
weight = 2;
}
}
return z_itoc((11 - x % 11) % 11);
}
static char *msi_plessey_nomod(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int content_segs, char *d) {
int i;
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
z_hrt_cpy_nochk(symbol, source, length);
if (content_segs && z_ct_cpy(symbol, source, length)) {
return NULL;
}
return d;
}
static char *msi_plessey_mod10(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int content_segs, char *d) {
int i;
char check_digit;
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
check_digit = msi_check_digit_mod10(source, length);
memcpy(d, MSITable[check_digit - '0'], 8);
d += 8;
z_hrt_cpy_nochk(symbol, source, length);
if (!no_checktext) {
z_hrt_cat_chr_nochk(symbol, check_digit);
}
if (content_segs && z_ct_cpy_cat(symbol, source, length, check_digit, NULL , 0)) {
return NULL;
}
return d;
}
static char *msi_plessey_mod1010(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int content_segs, char *d) {
int i;
unsigned char local_source[92 + 2];
memcpy(local_source, source, length);
local_source[length] = msi_check_digit_mod10(source, length);
local_source[length + 1] = msi_check_digit_mod10(local_source, length + 1);
for (i = 0; i < length + 2; i++, d += 8) {
memcpy(d, MSITable[local_source[i] - '0'], 8);
}
if (no_checktext) {
z_hrt_cpy_nochk(symbol, source, length);
} else {
z_hrt_cpy_nochk(symbol, local_source, length + 2);
}
if (content_segs && z_ct_cpy(symbol, local_source, length + 2)) {
return NULL;
}
return d;
}
static char *msi_plessey_mod11(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, const int content_segs, char *d) {
int i;
unsigned char check_digits[2];
int check_digits_len = 1;
for (i = 0; i < length; i++, d += 8) {
memcpy(d, MSITable[source[i] - '0'], 8);
}
check_digits[0] = msi_check_digit_mod11(source, length, wrap);
if (check_digits[0] == ':') {
check_digits[0] = '1';
check_digits[1] = '0';
check_digits_len++;
}
for (i = 0; i < check_digits_len; i++, d += 8) {
memcpy(d, MSITable[check_digits[i] - '0'], 8);
}
z_hrt_cpy_nochk(symbol, source, length);
if (!no_checktext) {
z_hrt_cat_nochk(symbol, check_digits, check_digits_len);
}
if (content_segs && z_ct_cpy_cat(symbol, source, length, '\xFF' , check_digits,
check_digits_len)) {
return NULL;
}
return d;
}
static char *msi_plessey_mod1110(struct zint_symbol *symbol, const unsigned char source[], const int length,
const int no_checktext, const int wrap, const int content_segs, char *d) {
int i;
char check_digit;
unsigned char local_source[92 + 3 + 1];
int local_length = length;
memcpy(local_source, source, length);
check_digit = msi_check_digit_mod11(source, length, wrap);
if (check_digit == ':') {
local_source[local_length++] = '1';
local_source[local_length++] = '0';
} else {
local_source[local_length++] = check_digit;
}
local_source[local_length] = msi_check_digit_mod10(local_source, local_length);
local_length++;
for (i = 0; i < local_length; i++, d += 8) {
memcpy(d, MSITable[local_source[i] - '0'], 8);
}
if (no_checktext) {
z_hrt_cpy_nochk(symbol, source, length);
} else {
z_hrt_cpy_nochk(symbol, local_source, local_length);
}
if (content_segs && z_ct_cpy(symbol, local_source, local_length)) {
return NULL;
}
return d;
}
INTERNAL int zint_msi_plessey(struct zint_symbol *symbol, unsigned char source[], int length) {
int error_number = 0;
int i;
char dest[766];
char *d = dest;
int check_option = symbol->option_2;
int no_checktext = 0;
const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;
assert(length > 0);
if (length > 92) {
return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 372, "Input length %d too long (maximum 92)", length);
}
if ((i = z_not_sane(NEON_F, source, length))) {
return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 377,
"Invalid character at position %d in input (digits only)", i);
}
if (check_option >= 11 && check_option <= 16) {
check_option -= 10;
no_checktext = 1;
}
if (check_option < 0 || check_option > 6) {
check_option = 0;
}
memcpy(d, "21", 2);
d += 2;
switch (check_option) {
case 0: d = msi_plessey_nomod(symbol, source, length, content_segs, d); break;
case 1: d = msi_plessey_mod10(symbol, source, length, no_checktext, content_segs, d); break;
case 2: d = msi_plessey_mod1010(symbol, source, length, no_checktext, content_segs, d); break;
case 3: d = msi_plessey_mod11(symbol, source, length, no_checktext, 7 , content_segs, d); break;
case 4: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 7 , content_segs, d); break;
case 5: d = msi_plessey_mod11(symbol, source, length, no_checktext, 9 , content_segs, d); break;
case 6: d = msi_plessey_mod1110(symbol, source, length, no_checktext, 9 , content_segs, d); break;
}
if (!d) {
return ZINT_ERROR_MEMORY;
}
memcpy(d, "121", 3);
d += 3;
z_expand(symbol, dest, d - dest);
return error_number;
}