2008-09-03 08:01:41 +12:00
|
|
|
/* aztec.c - Handles Aztec 2D Symbols */
|
|
|
|
|
|
|
|
/*
|
|
|
|
libzint - the open source barcode library
|
2021-05-28 05:33:19 +12:00
|
|
|
Copyright (C) 2009-2021 Robin Stuart <rstuart114@gmail.com>
|
2008-09-03 08:01:41 +12:00
|
|
|
|
2013-05-17 05:26:38 +12:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
2008-09-03 08:01:41 +12:00
|
|
|
|
2017-10-24 08:37:52 +13:00
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
2013-05-17 05:26:38 +12:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
2016-02-20 22:38:03 +13:00
|
|
|
notice, this list of conditions and the following disclaimer in the
|
2017-10-24 08:37:52 +13:00
|
|
|
documentation and/or other materials provided with the distribution.
|
2013-05-17 05:26:38 +12:00
|
|
|
3. Neither the name of the project nor the names of its contributors
|
2016-02-20 22:38:03 +13:00
|
|
|
may be used to endorse or promote products derived from this software
|
2017-10-24 08:37:52 +13:00
|
|
|
without specific prior written permission.
|
2008-09-03 08:01:41 +12:00
|
|
|
|
2013-05-17 05:26:38 +12:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
2017-10-24 08:37:52 +13:00
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
2013-05-17 05:26:38 +12:00
|
|
|
SUCH DAMAGE.
|
2016-02-20 22:38:03 +13:00
|
|
|
*/
|
2019-11-28 05:16:14 +13:00
|
|
|
/* vim: set ts=4 sw=4 et : */
|
2008-09-03 08:01:41 +12:00
|
|
|
|
2021-06-18 12:03:33 +12:00
|
|
|
#include <assert.h>
|
2008-09-03 08:01:41 +12:00
|
|
|
#include <stdio.h>
|
2009-06-03 08:23:38 +12:00
|
|
|
#ifdef _MSC_VER
|
2017-10-24 08:37:52 +13:00
|
|
|
#include <malloc.h>
|
2009-06-03 08:23:38 +12:00
|
|
|
#endif
|
2008-09-03 08:01:41 +12:00
|
|
|
#include "common.h"
|
|
|
|
#include "aztec.h"
|
|
|
|
#include "reedsol.h"
|
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
#define AZTEC_MAX_CAPACITY 19968 /* ISO/IEC 24778:2008 5.3 Table 1 Maximum Symbol Bit Capacity */
|
|
|
|
#define AZTEC_BIN_CAPACITY 17940 /* Above less 169 * 12 = 2028 bits (169 = 10% of 1664 + 3) */
|
|
|
|
#define AZTEC_MAP_SIZE 22801 /* AztecMap Version 32 151 x 151 */
|
|
|
|
#define AZTEC_MAP_POSN_MAX 20039 /* Maximum position index in AztecMap */
|
2020-03-29 11:50:55 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
static int az_count_doubles(const unsigned char source[], int i, const int length) {
|
2017-07-22 04:56:36 +12:00
|
|
|
int c = 0;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
while ((i + 1 < length) && ((source[i] == '.') || (source[i] == ',')) && (source[i + 1] == ' ')) {
|
|
|
|
c++;
|
2017-07-22 04:56:36 +12:00
|
|
|
i += 2;
|
2020-10-27 01:21:43 +13:00
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
return c;
|
2008-09-03 08:01:41 +12:00
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
static int az_count_dotcomma(const unsigned char source[], int i, const int length) {
|
2017-07-22 04:56:36 +12:00
|
|
|
int c = 0;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
while (i < length && ((source[i] == '.') || (source[i] == ','))) {
|
|
|
|
c++;
|
2017-07-22 04:56:36 +12:00
|
|
|
i++;
|
2020-10-27 01:21:43 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
return c;
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
static int az_count_chr(const unsigned char source[], int i, const int length, const unsigned char chr) {
|
2017-07-22 04:56:36 +12:00
|
|
|
int c = 0;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
while (i < length && source[i] == chr) {
|
2020-11-28 01:54:44 +13:00
|
|
|
c++;
|
2017-07-22 04:56:36 +12:00
|
|
|
i++;
|
2020-10-27 01:21:43 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
return c;
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
static char az_get_next_mode(const char encode_mode[], const int src_len, int i) {
|
2020-11-28 01:54:44 +13:00
|
|
|
int current_mode = encode_mode[i];
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
do {
|
2016-02-20 22:38:03 +13:00
|
|
|
i++;
|
2020-11-28 01:54:44 +13:00
|
|
|
} while ((i < src_len) && (encode_mode[i] == current_mode));
|
|
|
|
if (i >= src_len) {
|
2017-07-22 04:56:36 +12:00
|
|
|
return 'E';
|
|
|
|
} else {
|
|
|
|
return encode_mode[i];
|
|
|
|
}
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-12-24 00:12:36 +13:00
|
|
|
static int az_bin_append_posn(const int arg, const int length, char *binary, const int bin_posn) {
|
2020-03-29 11:50:55 +13:00
|
|
|
|
2020-12-23 23:57:24 +13:00
|
|
|
if (bin_posn + length > AZTEC_BIN_CAPACITY) {
|
2020-03-29 11:50:55 +13:00
|
|
|
return 0; /* Fail */
|
|
|
|
}
|
2020-12-23 23:57:24 +13:00
|
|
|
return bin_append_posn(arg, length, binary, bin_posn);
|
2020-03-29 11:50:55 +13:00
|
|
|
}
|
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
static int aztec_text_process(const unsigned char source[], int src_len, char binary_string[], const int gs1,
|
|
|
|
const int eci, int *data_length, const int debug) {
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
int i, j;
|
|
|
|
char current_mode;
|
|
|
|
int count;
|
|
|
|
char next_mode;
|
|
|
|
int reduced_length;
|
|
|
|
int byte_mode = 0;
|
2020-11-28 01:54:44 +13:00
|
|
|
int bp;
|
2017-09-11 03:03:09 +12:00
|
|
|
|
2020-03-29 11:50:55 +13:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
char encode_mode[src_len + 1];
|
|
|
|
unsigned char reduced_source[src_len + 1];
|
|
|
|
char reduced_encode_mode[src_len + 1];
|
|
|
|
#else
|
|
|
|
char *encode_mode = (char *) _alloca(src_len + 1);
|
|
|
|
unsigned char *reduced_source = (unsigned char *) _alloca(src_len + 1);
|
|
|
|
char *reduced_encode_mode = (char *) _alloca(src_len + 1);
|
|
|
|
#endif
|
2017-07-24 19:57:29 +12:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
for (i = 0; i < src_len; i++) {
|
2017-10-04 17:05:58 +13:00
|
|
|
if (source[i] >= 128) {
|
2017-07-22 04:56:36 +12:00
|
|
|
encode_mode[i] = 'B';
|
2016-02-20 22:38:03 +13:00
|
|
|
} else {
|
2017-07-22 04:56:36 +12:00
|
|
|
encode_mode[i] = AztecModes[(int) source[i]];
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Deal first with letter combinations which can be combined to one codeword
|
|
|
|
// Combinations are (CR LF) (. SP) (, SP) (: SP) in Punct mode
|
|
|
|
current_mode = 'U';
|
2020-11-28 01:54:44 +13:00
|
|
|
for (i = 0; i + 1 < src_len; i++) {
|
2017-07-22 04:56:36 +12:00
|
|
|
// Combination (CR LF) should always be in Punct mode
|
|
|
|
if ((source[i] == 13) && (source[i + 1] == 10)) {
|
|
|
|
encode_mode[i] = 'P';
|
|
|
|
encode_mode[i + 1] = 'P';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Combination (: SP) should always be in Punct mode
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((source[i] == ':') && (source[i + 1] == ' ')) {
|
2017-07-22 04:56:36 +12:00
|
|
|
encode_mode[i + 1] = 'P';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Combinations (. SP) and (, SP) sometimes use fewer bits in Digit mode
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (((source[i] == '.') || (source[i] == ',')) && (source[i + 1] == ' ') && (encode_mode[i] == 'X')) {
|
2021-05-28 05:33:19 +12:00
|
|
|
count = az_count_doubles(source, i, src_len);
|
|
|
|
next_mode = az_get_next_mode(encode_mode, src_len, i);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (current_mode == 'U') {
|
|
|
|
if ((next_mode == 'D') && (count <= 5)) {
|
|
|
|
for (j = 0; j < (2 * count); j++) {
|
|
|
|
encode_mode[i + j] = 'D';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'L') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode == 'D') && (count <= 4)) {
|
|
|
|
for (j = 0; j < (2 * count); j++) {
|
|
|
|
encode_mode[i + j] = 'D';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'M') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode == 'D') && (count == 1)) {
|
|
|
|
encode_mode[i] = 'D';
|
|
|
|
encode_mode[i + 1] = 'D';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'D') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode != 'D') && (count <= 4)) {
|
|
|
|
for (j = 0; j < (2 * count); j++) {
|
|
|
|
encode_mode[i + j] = 'D';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((next_mode == 'D') && (count <= 7)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < (2 * count); j++) {
|
|
|
|
encode_mode[i + j] = 'D';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Default is Punct mode
|
|
|
|
if (encode_mode[i] == 'X') {
|
|
|
|
encode_mode[i] = 'P';
|
|
|
|
encode_mode[i + 1] = 'P';
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((encode_mode[i] != 'X') && (encode_mode[i] != 'B')) {
|
|
|
|
current_mode = encode_mode[i];
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (debug) {
|
|
|
|
printf("First Pass:\n");
|
2020-11-28 01:54:44 +13:00
|
|
|
for (i = 0; i < src_len; i++) {
|
2017-07-22 04:56:36 +12:00
|
|
|
printf("%c", encode_mode[i]);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-07-22 04:56:36 +12:00
|
|
|
printf("\n");
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Reduce two letter combinations to one codeword marked as [abcd] in Punct mode
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
2020-11-28 01:54:44 +13:00
|
|
|
while (i < src_len) {
|
|
|
|
if (i + 1 < src_len) {
|
|
|
|
if ((source[i] == 13) && (source[i + 1] == 10)) { // CR LF
|
|
|
|
reduced_source[j] = 'a';
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i += 2;
|
|
|
|
} else if ((source[i] == '.') && (source[i + 1] == ' ') && (encode_mode[i] == 'P')) {
|
|
|
|
reduced_source[j] = 'b';
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i += 2;
|
|
|
|
} else if ((source[i] == ',') && (source[i + 1] == ' ') && (encode_mode[i] == 'P')) {
|
|
|
|
reduced_source[j] = 'c';
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i += 2;
|
|
|
|
} else if ((source[i] == ':') && (source[i + 1] == ' ')) {
|
|
|
|
reduced_source[j] = 'd';
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
reduced_source[j] = source[i];
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i++;
|
|
|
|
}
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
|
|
|
reduced_source[j] = source[i];
|
|
|
|
reduced_encode_mode[j] = encode_mode[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
j++;
|
2020-04-29 00:45:36 +12:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_length = j;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
current_mode = 'U';
|
2021-05-28 05:33:19 +12:00
|
|
|
for (i = 0; i < reduced_length; i++) {
|
2017-07-22 04:56:36 +12:00
|
|
|
// Resolve Carriage Return (CR) which can be Punct or Mixed mode
|
|
|
|
if (reduced_source[i] == 13) {
|
2021-05-28 05:33:19 +12:00
|
|
|
count = az_count_chr(reduced_source, i, reduced_length, 13);
|
|
|
|
next_mode = az_get_next_mode(reduced_encode_mode, reduced_length, i);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((current_mode == 'U') && ((next_mode == 'U') || (next_mode == 'B')) && (count == 1)) {
|
|
|
|
reduced_encode_mode[i] = 'P';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'L') && ((next_mode == 'L') || (next_mode == 'B')) && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'P';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'P') || (next_mode == 'P')) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'P';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (current_mode == 'D') {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (((next_mode == 'E') || (next_mode == 'U') || (next_mode == 'D') || (next_mode == 'B'))
|
|
|
|
&& (count <= 2)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'P';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((next_mode == 'L') && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'P';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Default is Mixed mode
|
|
|
|
if (reduced_encode_mode[i] == 'X') {
|
|
|
|
reduced_encode_mode[i] = 'M';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Resolve full stop and comma which can be in Punct or Digit mode
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((reduced_source[i] == '.') || (reduced_source[i] == ',')) {
|
2021-05-28 05:33:19 +12:00
|
|
|
count = az_count_dotcomma(reduced_source, i, reduced_length);
|
|
|
|
next_mode = az_get_next_mode(reduced_encode_mode, reduced_length, i);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (current_mode == 'U') {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (((next_mode == 'U') || (next_mode == 'L') || (next_mode == 'M') || (next_mode == 'B'))
|
|
|
|
&& (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'P';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'L') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode == 'L') && (count <= 2)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'P';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (((next_mode == 'M') || (next_mode == 'B')) && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'P';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'M') {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (((next_mode == 'E') || (next_mode == 'U') || (next_mode == 'L') || (next_mode == 'M'))
|
|
|
|
&& (count <= 4)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'P';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((next_mode == 'B') && (count <= 2)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'P';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'P') && (next_mode != 'D') && (count <= 9)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'P';
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Default is Digit mode
|
|
|
|
if (reduced_encode_mode[i] == 'X') {
|
|
|
|
reduced_encode_mode[i] = 'D';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Resolve Space (SP) which can be any mode except Punct
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_source[i] == ' ') {
|
2021-05-28 05:33:19 +12:00
|
|
|
count = az_count_chr(reduced_source, i, reduced_length, ' ');
|
|
|
|
next_mode = az_get_next_mode(reduced_encode_mode, reduced_length, i);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (current_mode == 'U') {
|
|
|
|
if ((next_mode == 'E') && (count <= 5)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'U';
|
|
|
|
}
|
2021-05-28 05:33:19 +12:00
|
|
|
} else if (((next_mode == 'U') || (next_mode == 'L') || (next_mode == 'M') || (next_mode == 'P')
|
|
|
|
|| (next_mode == 'B')) && (count <= 9)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'U';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'L') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode == 'E') && (count <= 5)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'L';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
|
|
|
|
} else if ((next_mode == 'U') && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'L';
|
2020-11-28 01:54:44 +13:00
|
|
|
|
|
|
|
} else if ((next_mode == 'L') && (count <= 14)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'L';
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
|
|
|
|
} else if (((next_mode == 'M') || (next_mode == 'P') || (next_mode == 'B')) && (count <= 9)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'L';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'M') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if (((next_mode == 'E') || (next_mode == 'U')) && (count <= 9)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'M';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (((next_mode == 'L') || (next_mode == 'B')) && (count <= 14)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'M';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (((next_mode == 'M') || (next_mode == 'P')) && (count <= 19)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'M';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'P') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((next_mode == 'E') && (count <= 5)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'U';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
} else if (((next_mode == 'U') || (next_mode == 'L') || (next_mode == 'M') || (next_mode == 'P')
|
|
|
|
|| (next_mode == 'B')) && (count <= 9)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'U';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Default is Digit mode
|
|
|
|
if (reduced_encode_mode[i] == 'X') {
|
|
|
|
reduced_encode_mode[i] = 'D';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_encode_mode[i] != 'B') {
|
|
|
|
current_mode = reduced_encode_mode[i];
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Decide when to use P/S instead of P/L and U/S instead of U/L
|
|
|
|
current_mode = 'U';
|
2021-05-28 05:33:19 +12:00
|
|
|
for (i = 0; i < reduced_length; i++) {
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_encode_mode[i] != current_mode) {
|
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
for (count = 0; ((i + count) < reduced_length)
|
|
|
|
&& (reduced_encode_mode[i + count] == reduced_encode_mode[i]); count++);
|
|
|
|
next_mode = az_get_next_mode(reduced_encode_mode, reduced_length, i);
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_encode_mode[i] == 'P') {
|
|
|
|
if ((current_mode == 'U') && (count <= 2)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'p';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'L') && (next_mode != 'U') && (count <= 2)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'p';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'L') && (next_mode == 'U') && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'p';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'M') && (next_mode != 'M') && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'p';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'M') && (next_mode == 'M') && (count <= 2)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'p';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'D') && (next_mode != 'D') && (count <= 3)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'p';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'D') && (next_mode == 'D') && (count <= 6)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'p';
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_encode_mode[i] == 'U') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((current_mode == 'L') && ((next_mode == 'L') || (next_mode == 'M')) && (count <= 2)) {
|
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'u';
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
} else if ((current_mode == 'L') && ((next_mode == 'E') || (next_mode == 'D') || (next_mode == 'B')
|
|
|
|
|| (next_mode == 'P')) && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'u';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'D') && (next_mode == 'D') && (count == 1)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
reduced_encode_mode[i] = 'u';
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((current_mode == 'D') && (next_mode == 'P') && (count <= 2)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
for (j = 0; j < count; j++) {
|
|
|
|
reduced_encode_mode[i + j] = 'u';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((reduced_encode_mode[i] != 'p') && (reduced_encode_mode[i] != 'u') && (reduced_encode_mode[i] != 'B')) {
|
|
|
|
current_mode = reduced_encode_mode[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
for (i = 0; i < reduced_length; i++) {
|
|
|
|
printf("%c", reduced_source[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
for (i = 0; i < reduced_length; i++) {
|
|
|
|
printf("%c", reduced_encode_mode[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
bp = 0;
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (gs1) {
|
2020-11-28 01:54:44 +13:00
|
|
|
bp = bin_append_posn(0, 5, binary_string, bp); // P/S
|
|
|
|
bp = bin_append_posn(0, 5, binary_string, bp); // FLG(n)
|
|
|
|
bp = bin_append_posn(0, 3, binary_string, bp); // FLG(0)
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
|
|
|
|
2019-10-07 05:39:54 +13:00
|
|
|
if (eci != 0) {
|
2020-11-28 01:54:44 +13:00
|
|
|
bp = bin_append_posn(0, 5, binary_string, bp); // P/S
|
|
|
|
bp = bin_append_posn(0, 5, binary_string, bp); // FLG(n)
|
2017-07-22 04:56:36 +12:00
|
|
|
if (eci < 10) {
|
2020-11-28 01:54:44 +13:00
|
|
|
bp = bin_append_posn(1, 3, binary_string, bp); // FLG(1)
|
|
|
|
bp = bin_append_posn(2 + eci, 4, binary_string, bp);
|
|
|
|
} else if (eci <= 99) {
|
|
|
|
bp = bin_append_posn(2, 3, binary_string, bp); // FLG(2)
|
|
|
|
bp = bin_append_posn(2 + (eci / 10), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + (eci % 10), 4, binary_string, bp);
|
|
|
|
} else if (eci <= 999) {
|
|
|
|
bp = bin_append_posn(3, 3, binary_string, bp); // FLG(3)
|
|
|
|
bp = bin_append_posn(2 + (eci / 100), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 100) / 10), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + (eci % 10), 4, binary_string, bp);
|
|
|
|
} else if (eci <= 9999) {
|
|
|
|
bp = bin_append_posn(4, 3, binary_string, bp); // FLG(4)
|
|
|
|
bp = bin_append_posn(2 + (eci / 1000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 1000) / 100), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 100) / 10), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + (eci % 10), 4, binary_string, bp);
|
|
|
|
} else if (eci <= 99999) {
|
|
|
|
bp = bin_append_posn(5, 3, binary_string, bp); // FLG(5)
|
|
|
|
bp = bin_append_posn(2 + (eci / 10000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 10000) / 1000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 1000) / 100), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 100) / 10), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + (eci % 10), 4, binary_string, bp);
|
|
|
|
} else {
|
|
|
|
bp = bin_append_posn(6, 3, binary_string, bp); // FLG(6)
|
|
|
|
bp = bin_append_posn(2 + (eci / 100000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 100000) / 10000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 10000) / 1000), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 1000) / 100), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + ((eci % 100) / 10), 4, binary_string, bp);
|
|
|
|
bp = bin_append_posn(2 + (eci % 10), 4, binary_string, bp);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
current_mode = 'U';
|
2020-03-29 11:50:55 +13:00
|
|
|
for (i = 0; i < reduced_length; i++) {
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-10-04 17:05:58 +13:00
|
|
|
if (reduced_encode_mode[i] != 'B') {
|
|
|
|
byte_mode = 0;
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-10-04 17:05:58 +13:00
|
|
|
if ((reduced_encode_mode[i] != current_mode) && (!byte_mode)) {
|
2017-07-22 04:56:36 +12:00
|
|
|
// Change mode
|
|
|
|
if (current_mode == 'U') {
|
|
|
|
switch (reduced_encode_mode[i]) {
|
|
|
|
case 'L':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(28, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // L/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'M':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'P':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'p':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/S
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'D':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // D/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'B':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // B/S
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'L') {
|
2017-07-22 04:56:36 +12:00
|
|
|
switch (reduced_encode_mode[i]) {
|
|
|
|
case 'U':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // D/L
|
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'u':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(28, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'P':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'p':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'D':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // D/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'B':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // B/S
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'M') {
|
2017-07-22 04:56:36 +12:00
|
|
|
switch (reduced_encode_mode[i]) {
|
|
|
|
case 'U':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'L':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(28, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // L/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'P':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'p':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/S
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'D':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // D/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'B':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // B/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'P') {
|
2017-07-22 04:56:36 +12:00
|
|
|
switch (reduced_encode_mode[i]) {
|
|
|
|
case 'U':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
2017-07-22 04:56:36 +12:00
|
|
|
case 'L':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(28, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // L/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'D':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // D/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'B':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2017-07-22 04:56:36 +12:00
|
|
|
current_mode = 'U';
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // B/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (current_mode == 'D') {
|
2017-07-22 04:56:36 +12:00
|
|
|
switch (reduced_encode_mode[i]) {
|
|
|
|
case 'U':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'u':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(15, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'L':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(28, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // L/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'M':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'P':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
|
|
|
if (!(bp = az_bin_append_posn(29, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // M/L
|
|
|
|
if (!(bp = az_bin_append_posn(30, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/L
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'p':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // P/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
case 'B':
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // U/L
|
2017-07-22 04:56:36 +12:00
|
|
|
current_mode = 'U';
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(31, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // B/S
|
2017-07-22 04:56:36 +12:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
// Byte mode length descriptor
|
|
|
|
if ((reduced_encode_mode[i] == 'B') && (!byte_mode)) {
|
2017-10-04 17:05:58 +13:00
|
|
|
for (count = 0; ((i + count) < reduced_length) && (reduced_encode_mode[i + count] == 'B'); count++);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (count > 2079) {
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (count > 31) {
|
|
|
|
/* Put 00000 followed by 11-bit number of bytes less 31 */
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG;
|
|
|
|
if (!(bp = az_bin_append_posn(count - 31, 11, binary_string, bp))) return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
|
|
|
/* Put 5-bit number of bytes */
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(count, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
|
|
|
byte_mode = 1;
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2021-06-20 00:11:23 +12:00
|
|
|
if ((reduced_encode_mode[i] != 'B') && (reduced_encode_mode[i] != 'u')
|
|
|
|
&& (reduced_encode_mode[i] != 'p')) {
|
2017-07-22 04:56:36 +12:00
|
|
|
current_mode = reduced_encode_mode[i];
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if ((reduced_encode_mode[i] == 'U') || (reduced_encode_mode[i] == 'u')) {
|
|
|
|
if (reduced_source[i] == ' ') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(1, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (!(bp = az_bin_append_posn(AztecSymbolChar[(int) reduced_source[i]], 5, binary_string, bp)))
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_encode_mode[i] == 'L') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_source[i] == ' ') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(1, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (!(bp = az_bin_append_posn(AztecSymbolChar[(int) reduced_source[i]], 5, binary_string, bp)))
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_encode_mode[i] == 'M') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_source[i] == ' ') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(1, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 13) {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(14, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // CR
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (!(bp = az_bin_append_posn(AztecSymbolChar[(int) reduced_source[i]], 5, binary_string, bp)))
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if ((reduced_encode_mode[i] == 'P') || (reduced_encode_mode[i] == 'p')) {
|
2017-07-22 04:56:36 +12:00
|
|
|
if (gs1 && (reduced_source[i] == '[')) {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(0, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // FLG(n)
|
|
|
|
if (!(bp = az_bin_append_posn(0, 3, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // FLG(0) = FNC1
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 13) {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(1, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // CR
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 'a') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(2, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // CR LF
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 'b') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(3, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // . SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 'c') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(4, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // , SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == 'd') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(5, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // : SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == ',') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(17, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // Comma
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == '.') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(19, 5, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // Full stop
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (!(bp = az_bin_append_posn(AztecSymbolChar[(int) reduced_source[i]], 5, binary_string, bp)))
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_encode_mode[i] == 'D') {
|
2017-07-22 04:56:36 +12:00
|
|
|
if (reduced_source[i] == ' ') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(1, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // SP
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == ',') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(12, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // Comma
|
2017-07-22 04:56:36 +12:00
|
|
|
} else if (reduced_source[i] == '.') {
|
2020-11-28 01:54:44 +13:00
|
|
|
if (!(bp = az_bin_append_posn(13, 4, binary_string, bp))) return ZINT_ERROR_TOO_LONG; // Full stop
|
2017-07-22 04:56:36 +12:00
|
|
|
} else {
|
2021-05-28 05:33:19 +12:00
|
|
|
if (!(bp = az_bin_append_posn(AztecSymbolChar[(int) reduced_source[i]], 4, binary_string, bp)))
|
|
|
|
return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (reduced_encode_mode[i] == 'B') {
|
|
|
|
if (!(bp = az_bin_append_posn(reduced_source[i], 8, binary_string, bp))) return ZINT_ERROR_TOO_LONG;
|
2017-07-22 04:56:36 +12:00
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2017-07-22 04:56:36 +12:00
|
|
|
if (debug) {
|
|
|
|
printf("Binary String:\n");
|
2021-06-17 04:45:25 +12:00
|
|
|
printf("%.*s\n", bp, binary_string);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2017-09-11 03:03:09 +12:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
*data_length = bp;
|
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
return 0;
|
2008-09-03 08:01:41 +12:00
|
|
|
}
|
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
/* Prevent data from obscuring reference grid */
|
2021-05-28 05:33:19 +12:00
|
|
|
static int az_avoidReferenceGrid(int output) {
|
2016-01-11 06:34:33 +13:00
|
|
|
|
|
|
|
if (output > 10) {
|
2020-11-28 01:54:44 +13:00
|
|
|
output += (output - 11) / 15 + 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
/* Calculate the position of the bits in the grid (non-compact) */
|
|
|
|
static void az_populate_map(short AztecMap[], const int layers) {
|
2017-09-11 03:03:09 +12:00
|
|
|
int layer, n, i;
|
2016-01-11 06:34:33 +13:00
|
|
|
int x, y;
|
2021-05-28 05:33:19 +12:00
|
|
|
const int offset = AztecOffset[layers - 1];
|
|
|
|
const int endoffset = 151 - offset;
|
2016-01-11 06:34:33 +13:00
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
for (layer = 0; layer < layers; layer++) {
|
|
|
|
const int start = (112 * layer) + (16 * layer * layer) + 2;
|
|
|
|
const int length = 28 + (layer * 4) + (layer + 1) * 4;
|
|
|
|
int av0, av1;
|
2016-01-11 06:34:33 +13:00
|
|
|
/* Top */
|
|
|
|
i = 0;
|
2021-05-28 05:33:19 +12:00
|
|
|
x = 64 - (layer * 2);
|
|
|
|
y = 63 - (layer * 2);
|
|
|
|
av0 = az_avoidReferenceGrid(y) * 151;
|
|
|
|
av1 = az_avoidReferenceGrid(y - 1) * 151;
|
2016-01-11 06:34:33 +13:00
|
|
|
for (n = start; n < (start + length); n += 2) {
|
2021-05-28 05:33:19 +12:00
|
|
|
int avxi = az_avoidReferenceGrid(x + i);
|
|
|
|
AztecMap[av0 + avxi] = n;
|
|
|
|
AztecMap[av1 + avxi] = n + 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Right */
|
|
|
|
i = 0;
|
2021-05-28 05:33:19 +12:00
|
|
|
x = 78 + (layer * 2);
|
|
|
|
y = 64 - (layer * 2);
|
|
|
|
av0 = az_avoidReferenceGrid(x);
|
|
|
|
av1 = az_avoidReferenceGrid(x + 1);
|
2016-01-11 06:34:33 +13:00
|
|
|
for (n = start + length; n < (start + (length * 2)); n += 2) {
|
2021-05-28 05:33:19 +12:00
|
|
|
int avyi = az_avoidReferenceGrid(y + i) * 151;
|
|
|
|
AztecMap[avyi + av0] = n;
|
|
|
|
AztecMap[avyi + av1] = n + 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Bottom */
|
|
|
|
i = 0;
|
2021-05-28 05:33:19 +12:00
|
|
|
x = 77 + (layer * 2);
|
|
|
|
y = 78 + (layer * 2);
|
|
|
|
av0 = az_avoidReferenceGrid(y) * 151;
|
|
|
|
av1 = az_avoidReferenceGrid(y + 1) * 151;
|
2016-01-11 06:34:33 +13:00
|
|
|
for (n = start + (length * 2); n < (start + (length * 3)); n += 2) {
|
2021-05-28 05:33:19 +12:00
|
|
|
int avxi = az_avoidReferenceGrid(x - i);
|
|
|
|
AztecMap[av0 + avxi] = n;
|
|
|
|
AztecMap[av1 + avxi] = n + 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Left */
|
|
|
|
i = 0;
|
2021-05-28 05:33:19 +12:00
|
|
|
x = 63 - (layer * 2);
|
|
|
|
y = 77 + (layer * 2);
|
|
|
|
av0 = az_avoidReferenceGrid(x);
|
|
|
|
av1 = az_avoidReferenceGrid(x - 1);
|
2016-01-11 06:34:33 +13:00
|
|
|
for (n = start + (length * 3); n < (start + (length * 4)); n += 2) {
|
2021-05-28 05:33:19 +12:00
|
|
|
int avyi = az_avoidReferenceGrid(y - i) * 151;
|
|
|
|
AztecMap[avyi + av0] = n;
|
|
|
|
AztecMap[avyi + av1] = n + 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
/* Copy "Core Symbol" (finder, descriptor, orientation) */
|
|
|
|
for (y = 0; y < 15; y++) {
|
|
|
|
memcpy(AztecMap + (y + 68) * 151 + 68, AztecMapCore[y], sizeof(short) * 15);
|
2016-01-11 06:34:33 +13:00
|
|
|
}
|
|
|
|
|
2021-05-28 05:33:19 +12:00
|
|
|
/* Reference grid guide bars */
|
|
|
|
for (y = offset <= 11 ? 11 : AztecMapGridYOffsets[(offset - 11) / 16]; y < endoffset; y += 16) {
|
|
|
|
for (x = offset; x < endoffset; x++) {
|
|
|
|
AztecMap[(x * 151) + y] = x & 1;
|
|
|
|
AztecMap[(y * 151) + x] = x & 1;
|
2016-01-11 06:34:33 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
INTERNAL int aztec(struct zint_symbol *symbol, unsigned char source[], int length) {
|
2016-02-24 08:21:48 +13:00
|
|
|
int x, y, i, j, p, data_blocks, ecc_blocks, layers, total_bits;
|
2021-05-28 05:33:19 +12:00
|
|
|
char bit_pattern[AZTEC_MAP_POSN_MAX + 1]; /* Note AZTEC_MAP_POSN_MAX > AZTEC_BIN_CAPACITY */
|
2020-12-18 16:05:08 +13:00
|
|
|
/* To lessen stack usage, share binary_string buffer with bit_pattern, as accessed separately */
|
|
|
|
char *binary_string = bit_pattern;
|
|
|
|
char descriptor[42];
|
2021-05-28 05:33:19 +12:00
|
|
|
char adjusted_string[AZTEC_MAX_CAPACITY];
|
2020-12-18 03:23:23 +13:00
|
|
|
short AztecMap[AZTEC_MAP_SIZE];
|
2016-02-20 22:38:03 +13:00
|
|
|
unsigned char desc_data[4], desc_ecc[6];
|
2020-10-01 00:19:12 +13:00
|
|
|
int error_number, ecc_level, compact, data_length, data_maxsize, codeword_size, adjusted_length;
|
2016-02-20 22:38:03 +13:00
|
|
|
int remainder, padbits, count, gs1, adjustment_size;
|
2020-04-06 10:32:08 +12:00
|
|
|
int debug = (symbol->debug & ZINT_DEBUG_PRINT), reader = 0;
|
2016-02-20 22:38:03 +13:00
|
|
|
int comp_loop = 4;
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_t rs;
|
|
|
|
rs_uint_t rs_uint;
|
2009-08-07 07:44:30 +12:00
|
|
|
|
2016-08-16 23:43:41 +12:00
|
|
|
#ifdef _MSC_VER
|
2016-02-20 22:38:03 +13:00
|
|
|
unsigned int* data_part;
|
|
|
|
unsigned int* ecc_part;
|
2009-09-29 22:45:46 +13:00
|
|
|
#endif
|
|
|
|
|
2019-11-28 05:16:14 +13:00
|
|
|
if ((symbol->input_mode & 0x07) == GS1_MODE) {
|
2016-02-20 22:38:03 +13:00
|
|
|
gs1 = 1;
|
|
|
|
} else {
|
|
|
|
gs1 = 0;
|
|
|
|
}
|
|
|
|
if (symbol->output_options & READER_INIT) {
|
|
|
|
reader = 1;
|
|
|
|
comp_loop = 1;
|
|
|
|
}
|
2016-08-16 23:43:41 +12:00
|
|
|
if (gs1 && reader) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "501: Cannot encode in GS1 and Reader Initialisation mode at the same time");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
error_number = aztec_text_process(source, length, binary_string, gs1, symbol->eci, &data_length, debug);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
if (error_number != 0) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "502: Input too long or too many extended ASCII characters");
|
2020-10-01 00:19:12 +13:00
|
|
|
return error_number;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2021-06-26 11:27:04 +12:00
|
|
|
assert(data_length > 0); /* Suppress clang-tidy warning: clang-analyzer-core.UndefinedBinaryOperatorResult */
|
2016-02-20 22:38:03 +13:00
|
|
|
|
|
|
|
if (!((symbol->option_1 >= -1) && (symbol->option_1 <= 4))) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "503: Invalid error correction level - using default instead");
|
2020-08-22 22:09:57 +12:00
|
|
|
if (symbol->warn_level == WARN_FAIL_ALL) {
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
} else {
|
2020-10-01 00:19:12 +13:00
|
|
|
error_number = ZINT_WARN_INVALID_OPTION;
|
2020-08-22 22:09:57 +12:00
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
symbol->option_1 = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ecc_level = symbol->option_1;
|
|
|
|
|
|
|
|
if ((ecc_level == -1) || (ecc_level == 0)) {
|
|
|
|
ecc_level = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_maxsize = 0; /* Keep compiler happy! */
|
|
|
|
adjustment_size = 0;
|
|
|
|
if (symbol->option_2 == 0) { /* The size of the symbol can be determined by Zint */
|
|
|
|
do {
|
|
|
|
/* Decide what size symbol to use - the smallest that fits the data */
|
|
|
|
compact = 0; /* 1 = Aztec Compact, 0 = Normal Aztec */
|
|
|
|
layers = 0;
|
|
|
|
|
|
|
|
switch (ecc_level) {
|
|
|
|
/* For each level of error correction work out the smallest symbol which
|
|
|
|
the data will fit in */
|
|
|
|
case 1: for (i = 32; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < Aztec10DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 0;
|
|
|
|
data_maxsize = Aztec10DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = comp_loop; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < AztecCompact10DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 1;
|
|
|
|
data_maxsize = AztecCompact10DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: for (i = 32; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < Aztec23DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 0;
|
|
|
|
data_maxsize = Aztec23DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = comp_loop; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < AztecCompact23DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 1;
|
|
|
|
data_maxsize = AztecCompact23DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: for (i = 32; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < Aztec36DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 0;
|
|
|
|
data_maxsize = Aztec36DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = comp_loop; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < AztecCompact36DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 1;
|
|
|
|
data_maxsize = AztecCompact36DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: for (i = 32; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < Aztec50DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 0;
|
|
|
|
data_maxsize = Aztec50DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = comp_loop; i > 0; i--) {
|
|
|
|
if ((data_length + adjustment_size) < AztecCompact50DataSizes[i - 1]) {
|
|
|
|
layers = i;
|
|
|
|
compact = 1;
|
|
|
|
data_maxsize = AztecCompact50DataSizes[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-01 08:42:32 +13:00
|
|
|
if (layers == 0) { /* Couldn't find a symbol which fits the data */
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "504: Input too long (too many bits for selected ECC)");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_TOO_LONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine codeword bitlength - Table 3 */
|
2020-11-28 01:54:44 +13:00
|
|
|
if (layers <= 2) {
|
|
|
|
codeword_size = 6;
|
|
|
|
} else if (layers <= 8) {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 8;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (layers <= 22) {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 10;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0;
|
2020-04-27 01:39:44 +12:00
|
|
|
count = 0;
|
|
|
|
for (i = 0; i < data_length; i++) {
|
2016-02-20 22:38:03 +13:00
|
|
|
if ((j + 1) % codeword_size == 0) {
|
2020-04-27 01:39:44 +12:00
|
|
|
// Last bit of codeword
|
2020-04-29 00:45:36 +12:00
|
|
|
/* 7.3.1.2 "whenever the first B-1 bits ... are all “0”s, then a dummy “1” is inserted..."
|
|
|
|
* "Similarly a message codeword that starts with B-1 “1”s has a dummy “0” inserted..." */
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-10-27 01:21:43 +13:00
|
|
|
if (count == 0 || count == (codeword_size - 1)) {
|
|
|
|
// Codeword of B-1 '0's or B-1 '1's
|
|
|
|
adjusted_string[j] = count == 0 ? '1' : '0';
|
2016-02-20 22:38:03 +13:00
|
|
|
j++;
|
2020-10-27 01:21:43 +13:00
|
|
|
count = binary_string[i] == '1' ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
count = 0;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
|
|
|
|
} else if (binary_string[i] == '1') { /* Skip B so only counting B-1 */
|
|
|
|
count++;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
|
|
|
|
adjusted_string[j] = binary_string[i];
|
|
|
|
j++;
|
2020-04-27 01:39:44 +12:00
|
|
|
}
|
|
|
|
adjusted_length = j;
|
2016-02-20 22:38:03 +13:00
|
|
|
adjustment_size = adjusted_length - data_length;
|
|
|
|
|
|
|
|
/* Add padding */
|
|
|
|
remainder = adjusted_length % codeword_size;
|
|
|
|
|
|
|
|
padbits = codeword_size - remainder;
|
|
|
|
if (padbits == codeword_size) {
|
|
|
|
padbits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < padbits; i++) {
|
2020-11-28 01:54:44 +13:00
|
|
|
adjusted_string[adjusted_length++] = '1';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (i = (adjusted_length - codeword_size); i < adjusted_length; i++) {
|
|
|
|
if (adjusted_string[i] == '1') {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count == codeword_size) {
|
|
|
|
adjusted_string[adjusted_length - 1] = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
printf("Codewords:\n");
|
|
|
|
for (i = 0; i < (adjusted_length / codeword_size); i++) {
|
|
|
|
for (j = 0; j < codeword_size; j++) {
|
|
|
|
printf("%c", adjusted_string[(i * codeword_size) + j]);
|
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
printf(" ");
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
printf("\n");
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (adjusted_length > data_maxsize);
|
|
|
|
/* This loop will only repeat on the rare occasions when the rule about not having all 1s or all 0s
|
|
|
|
means that the binary string has had to be lengthened beyond the maximum number of bits that can
|
|
|
|
be encoded in a symbol of the selected size */
|
|
|
|
|
|
|
|
} else { /* The size of the symbol has been specified by the user */
|
2020-11-28 01:54:44 +13:00
|
|
|
if ((symbol->option_2 < 0) || (symbol->option_2 > 36)) {
|
|
|
|
strcpy(symbol->errtxt, "510: Invalid Aztec Code size");
|
|
|
|
return ZINT_ERROR_INVALID_OPTION;
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
if ((reader == 1) && ((symbol->option_2 >= 2) && (symbol->option_2 <= 4))) {
|
|
|
|
symbol->option_2 = 5;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
if (symbol->option_2 <= 4) {
|
2016-02-20 22:38:03 +13:00
|
|
|
compact = 1;
|
|
|
|
layers = symbol->option_2;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else {
|
2016-02-20 22:38:03 +13:00
|
|
|
compact = 0;
|
|
|
|
layers = symbol->option_2 - 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine codeword bitlength - Table 3 */
|
2020-11-28 01:54:44 +13:00
|
|
|
if (layers <= 2) {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 6;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (layers <= 8) {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 8;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else if (layers <= 22) {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 10;
|
2020-11-28 01:54:44 +13:00
|
|
|
} else {
|
2016-02-20 22:38:03 +13:00
|
|
|
codeword_size = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
j = 0;
|
2020-04-27 01:39:44 +12:00
|
|
|
count = 0;
|
|
|
|
for (i = 0; i < data_length; i++) {
|
2020-04-29 00:45:36 +12:00
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
if ((j + 1) % codeword_size == 0) {
|
2020-04-27 01:39:44 +12:00
|
|
|
// Last bit of codeword
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-10-27 01:21:43 +13:00
|
|
|
if (count == 0 || count == (codeword_size - 1)) {
|
|
|
|
// Codeword of B-1 '0's or B-1 '1's
|
|
|
|
adjusted_string[j] = count == 0 ? '1' : '0';
|
2016-02-20 22:38:03 +13:00
|
|
|
j++;
|
2020-10-27 01:21:43 +13:00
|
|
|
count = binary_string[i] == '1' ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
count = 0;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
|
|
|
|
} else if (binary_string[i] == '1') { /* Skip B so only counting B-1 */
|
|
|
|
count++;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
|
|
|
|
adjusted_string[j] = binary_string[i];
|
|
|
|
j++;
|
2020-04-27 01:39:44 +12:00
|
|
|
}
|
|
|
|
adjusted_length = j;
|
2016-02-20 22:38:03 +13:00
|
|
|
|
|
|
|
remainder = adjusted_length % codeword_size;
|
|
|
|
|
|
|
|
padbits = codeword_size - remainder;
|
|
|
|
if (padbits == codeword_size) {
|
|
|
|
padbits = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < padbits; i++) {
|
2020-11-28 01:54:44 +13:00
|
|
|
adjusted_string[adjusted_length++] = '1';
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for (i = (adjusted_length - codeword_size); i < adjusted_length; i++) {
|
|
|
|
if (adjusted_string[i] == '1') {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count == codeword_size) {
|
|
|
|
adjusted_string[adjusted_length - 1] = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the data actually fits into the selected symbol size */
|
|
|
|
if (compact) {
|
|
|
|
data_maxsize = codeword_size * (AztecCompactSizes[layers - 1] - 3);
|
|
|
|
} else {
|
|
|
|
data_maxsize = codeword_size * (AztecSizes[layers - 1] - 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (adjusted_length > data_maxsize) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "505: Data too long for specified Aztec Code symbol size");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_TOO_LONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug) {
|
|
|
|
printf("Codewords:\n");
|
|
|
|
for (i = 0; i < (adjusted_length / codeword_size); i++) {
|
|
|
|
for (j = 0; j < codeword_size; j++) {
|
|
|
|
printf("%c", adjusted_string[(i * codeword_size) + j]);
|
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
printf(" ");
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2020-04-29 00:45:36 +12:00
|
|
|
printf("\n");
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader && (layers > 22)) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "506: Data too long for reader initialisation symbol");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_TOO_LONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_blocks = adjusted_length / codeword_size;
|
|
|
|
|
|
|
|
if (compact) {
|
|
|
|
ecc_blocks = AztecCompactSizes[layers - 1] - data_blocks;
|
|
|
|
} else {
|
|
|
|
ecc_blocks = AztecSizes[layers - 1] - data_blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug) {
|
2021-05-28 05:33:19 +12:00
|
|
|
printf("Generating a %s symbol with %d layers\n", compact ? "compact" : "full-size", layers);
|
2016-02-20 22:38:03 +13:00
|
|
|
printf("Requires ");
|
|
|
|
if (compact) {
|
|
|
|
printf("%d", AztecCompactSizes[layers - 1]);
|
|
|
|
} else {
|
|
|
|
printf("%d", AztecSizes[layers - 1]);
|
|
|
|
}
|
|
|
|
printf(" codewords of %d-bits\n", codeword_size);
|
|
|
|
printf(" (%d data words, %d ecc words)\n", data_blocks, ecc_blocks);
|
|
|
|
}
|
|
|
|
|
2009-06-20 06:29:49 +12:00
|
|
|
#ifndef _MSC_VER
|
2021-05-28 05:33:19 +12:00
|
|
|
unsigned int data_part[data_blocks], ecc_part[ecc_blocks];
|
2009-06-20 06:29:49 +12:00
|
|
|
#else
|
2021-05-28 05:33:19 +12:00
|
|
|
data_part = (unsigned int *) _alloca(sizeof(unsigned int) * data_blocks);
|
|
|
|
ecc_part = (unsigned int *) _alloca(sizeof(unsigned int) * ecc_blocks);
|
2009-06-20 06:29:49 +12:00
|
|
|
#endif
|
2016-02-20 22:38:03 +13:00
|
|
|
/* Copy across data into separate integers */
|
2021-05-28 05:33:19 +12:00
|
|
|
memset(data_part, 0, sizeof(unsigned int) * data_blocks);
|
|
|
|
memset(ecc_part, 0, sizeof(unsigned int) * ecc_blocks);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2017-08-06 20:10:00 +12:00
|
|
|
/* Split into codewords and calculate reed-solomon error correction codes */
|
|
|
|
for (i = 0; i < data_blocks; i++) {
|
|
|
|
for (p = 0; p < codeword_size; p++) {
|
|
|
|
if (adjusted_string[i * codeword_size + p] == '1') {
|
|
|
|
data_part[i] += 0x01 << (codeword_size - (p + 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
switch (codeword_size) {
|
|
|
|
case 6:
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_gf(&rs, 0x43);
|
|
|
|
rs_init_code(&rs, ecc_blocks, 1);
|
|
|
|
rs_encode_uint(&rs, data_blocks, data_part, ecc_part);
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
|
|
|
case 8:
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_gf(&rs, 0x12d);
|
|
|
|
rs_init_code(&rs, ecc_blocks, 1);
|
|
|
|
rs_encode_uint(&rs, data_blocks, data_part, ecc_part);
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
|
|
|
case 10:
|
2021-06-30 02:43:42 +12:00
|
|
|
if (!rs_uint_init_gf(&rs_uint, 0x409, 1023)) { /* Can fail on malloc() */
|
|
|
|
strcpy(symbol->errtxt, "500: Insufficient memory for Reed-Solomon log tables");
|
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_uint_init_code(&rs_uint, ecc_blocks, 1);
|
|
|
|
rs_uint_encode(&rs_uint, data_blocks, data_part, ecc_part);
|
|
|
|
rs_uint_free(&rs_uint);
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
|
|
|
case 12:
|
2021-06-30 02:43:42 +12:00
|
|
|
if (!rs_uint_init_gf(&rs_uint, 0x1069, 4095)) { /* Can fail on malloc() */
|
|
|
|
/* Note using AUSPOST error nos range as out of 50x ones & 51x taken by CODEONE */
|
|
|
|
strcpy(symbol->errtxt, "400: Insufficient memory for Reed-Solomon log tables");
|
|
|
|
return ZINT_ERROR_MEMORY;
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_uint_init_code(&rs_uint, ecc_blocks, 1);
|
|
|
|
rs_uint_encode(&rs_uint, data_blocks, data_part, ecc_part);
|
|
|
|
rs_uint_free(&rs_uint);
|
2016-02-20 22:38:03 +13:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-06 20:10:00 +12:00
|
|
|
for (i = (ecc_blocks - 1); i >= 0; i--) {
|
2020-11-28 01:54:44 +13:00
|
|
|
adjusted_length = bin_append_posn(ecc_part[i], codeword_size, adjusted_string, adjusted_length);
|
2017-08-06 20:10:00 +12:00
|
|
|
}
|
2017-10-24 08:37:52 +13:00
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
/* Invert the data so that actual data is on the outside and reed-solomon on the inside */
|
2021-05-28 05:33:19 +12:00
|
|
|
memset(bit_pattern, '0', AZTEC_MAP_POSN_MAX + 1);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
|
|
|
total_bits = (data_blocks + ecc_blocks) * codeword_size;
|
|
|
|
for (i = 0; i < total_bits; i++) {
|
|
|
|
bit_pattern[i] = adjusted_string[total_bits - i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now add the symbol descriptor */
|
|
|
|
memset(desc_data, 0, 4);
|
|
|
|
memset(desc_ecc, 0, 6);
|
|
|
|
memset(descriptor, 0, 42);
|
|
|
|
|
|
|
|
if (compact) {
|
|
|
|
/* The first 2 bits represent the number of layers minus 1 */
|
|
|
|
if ((layers - 1) & 0x02) {
|
|
|
|
descriptor[0] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[0] = '0';
|
|
|
|
}
|
|
|
|
if ((layers - 1) & 0x01) {
|
|
|
|
descriptor[1] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[1] = '0';
|
|
|
|
}
|
|
|
|
/* The next 6 bits represent the number of data blocks minus 1 */
|
|
|
|
if (reader) {
|
|
|
|
descriptor[2] = '1';
|
|
|
|
} else {
|
|
|
|
if ((data_blocks - 1) & 0x20) {
|
|
|
|
descriptor[2] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[2] = '0';
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 05:22:26 +12:00
|
|
|
|
|
|
|
for (i = 3; i < 8; i++) {
|
|
|
|
if ((data_blocks - 1) & (0x10 >> (i - 3))) {
|
|
|
|
descriptor[i] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[i] = '0';
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2021-05-28 05:33:19 +12:00
|
|
|
if (debug) printf("Mode Message = %.8s\n", descriptor);
|
2016-02-20 22:38:03 +13:00
|
|
|
} else {
|
|
|
|
/* The first 5 bits represent the number of layers minus 1 */
|
2016-09-18 05:22:26 +12:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if ((layers - 1) & (0x10 >> i)) {
|
|
|
|
descriptor[i] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[i] = '0';
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2016-09-18 05:22:26 +12:00
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
/* The next 11 bits represent the number of data blocks minus 1 */
|
|
|
|
if (reader) {
|
|
|
|
descriptor[5] = '1';
|
|
|
|
} else {
|
|
|
|
if ((data_blocks - 1) & 0x400) {
|
|
|
|
descriptor[5] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[5] = '0';
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 05:22:26 +12:00
|
|
|
for (i = 6; i < 16; i++) {
|
|
|
|
if ((data_blocks - 1) & (0x200 >> (i - 6))) {
|
|
|
|
descriptor[i] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[i] = '0';
|
|
|
|
}
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2021-05-28 05:33:19 +12:00
|
|
|
if (debug) printf("Mode Message = %.16s\n", descriptor);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Split into 4-bit codewords */
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
if (descriptor[i * 4] == '1') {
|
|
|
|
desc_data[i] += 8;
|
|
|
|
}
|
|
|
|
if (descriptor[(i * 4) + 1] == '1') {
|
|
|
|
desc_data[i] += 4;
|
|
|
|
}
|
|
|
|
if (descriptor[(i * 4) + 2] == '1') {
|
|
|
|
desc_data[i] += 2;
|
|
|
|
}
|
|
|
|
if (descriptor[(i * 4) + 3] == '1') {
|
|
|
|
desc_data[i] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add reed-solomon error correction with Galois field GF(16) and prime modulus
|
|
|
|
x^4 + x + 1 (section 7.2.3)*/
|
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_gf(&rs, 0x13);
|
2016-02-20 22:38:03 +13:00
|
|
|
if (compact) {
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_code(&rs, 5, 1);
|
|
|
|
rs_encode(&rs, 2, desc_data, desc_ecc);
|
2016-02-20 22:38:03 +13:00
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
if (desc_ecc[4 - i] & 0x08) {
|
|
|
|
descriptor[(i * 4) + 8] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 8] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[4 - i] & 0x04) {
|
|
|
|
descriptor[(i * 4) + 9] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 9] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[4 - i] & 0x02) {
|
|
|
|
descriptor[(i * 4) + 10] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 10] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[4 - i] & 0x01) {
|
|
|
|
descriptor[(i * 4) + 11] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 11] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_code(&rs, 6, 1);
|
|
|
|
rs_encode(&rs, 4, desc_data, desc_ecc);
|
2016-02-20 22:38:03 +13:00
|
|
|
for (i = 0; i < 6; i++) {
|
|
|
|
if (desc_ecc[5 - i] & 0x08) {
|
|
|
|
descriptor[(i * 4) + 16] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 16] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[5 - i] & 0x04) {
|
|
|
|
descriptor[(i * 4) + 17] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 17] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[5 - i] & 0x02) {
|
|
|
|
descriptor[(i * 4) + 18] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 18] = '0';
|
|
|
|
}
|
|
|
|
if (desc_ecc[5 - i] & 0x01) {
|
|
|
|
descriptor[(i * 4) + 19] = '1';
|
|
|
|
} else {
|
|
|
|
descriptor[(i * 4) + 19] = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge descriptor with the rest of the symbol */
|
2021-05-28 05:33:19 +12:00
|
|
|
if (compact) {
|
|
|
|
memcpy(bit_pattern + 2000 - 2, descriptor, 40);
|
|
|
|
} else {
|
|
|
|
memcpy(bit_pattern + 20000 - 2, descriptor, 40);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Plot all of the data into the symbol in pre-defined spiral pattern */
|
|
|
|
if (compact) {
|
2020-11-28 01:54:44 +13:00
|
|
|
int offset = AztecCompactOffset[layers - 1];
|
|
|
|
int end_offset = 27 - offset;
|
|
|
|
for (y = offset; y < end_offset; y++) {
|
|
|
|
int y_map = y * 27;
|
|
|
|
for (x = offset; x < end_offset; x++) {
|
|
|
|
int map = CompactAztecMap[y_map + x];
|
|
|
|
if (map == 1) {
|
|
|
|
set_module(symbol, y - offset, x - offset);
|
2021-05-28 05:33:19 +12:00
|
|
|
} else if (map >= 2 && bit_pattern[map - 2] == '1') {
|
|
|
|
set_module(symbol, y - offset, x - offset);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
symbol->row_height[y - offset] = 1;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2021-06-20 00:11:23 +12:00
|
|
|
symbol->height = 27 - (2 * offset);
|
2020-11-28 01:54:44 +13:00
|
|
|
symbol->rows = 27 - (2 * offset);
|
|
|
|
symbol->width = 27 - (2 * offset);
|
2016-02-20 22:38:03 +13:00
|
|
|
} else {
|
2020-11-28 01:54:44 +13:00
|
|
|
int offset = AztecOffset[layers - 1];
|
|
|
|
int end_offset = 151 - offset;
|
2021-05-28 05:33:19 +12:00
|
|
|
az_populate_map(AztecMap, layers);
|
2020-11-28 01:54:44 +13:00
|
|
|
for (y = offset; y < end_offset; y++) {
|
|
|
|
int y_map = y * 151;
|
|
|
|
for (x = offset; x < end_offset; x++) {
|
|
|
|
int map = AztecMap[y_map + x];
|
|
|
|
if (map == 1) {
|
|
|
|
set_module(symbol, y - offset, x - offset);
|
2021-05-28 05:33:19 +12:00
|
|
|
} else if (map >= 2 && bit_pattern[map - 2] == '1') {
|
|
|
|
set_module(symbol, y - offset, x - offset);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 01:54:44 +13:00
|
|
|
symbol->row_height[y - offset] = 1;
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
2021-06-20 00:11:23 +12:00
|
|
|
symbol->height = 151 - (2 * offset);
|
2020-11-28 01:54:44 +13:00
|
|
|
symbol->rows = 151 - (2 * offset);
|
|
|
|
symbol->width = 151 - (2 * offset);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
2020-10-01 00:19:12 +13:00
|
|
|
return error_number;
|
2008-09-26 23:39:06 +12:00
|
|
|
}
|
2008-11-09 23:06:05 +13:00
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
/* Encodes Aztec runes as specified in ISO/IEC 24778:2008 Annex A */
|
2019-12-19 13:37:55 +13:00
|
|
|
INTERNAL int aztec_runes(struct zint_symbol *symbol, unsigned char source[], int length) {
|
2020-12-23 23:57:24 +13:00
|
|
|
unsigned int input_value;
|
|
|
|
int error_number, i, y, x, r;
|
2016-02-20 22:38:03 +13:00
|
|
|
char binary_string[28];
|
|
|
|
unsigned char data_codewords[3], ecc_codewords[6];
|
2020-12-23 23:57:24 +13:00
|
|
|
int bp = 0;
|
2020-10-27 01:21:43 +13:00
|
|
|
int debug = symbol->debug & ZINT_DEBUG_PRINT;
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_t rs;
|
2016-02-20 22:38:03 +13:00
|
|
|
|
|
|
|
input_value = 0;
|
|
|
|
if (length > 3) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "507: Input too large");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
error_number = is_sane(NEON, source, length);
|
|
|
|
if (error_number != 0) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "508: Invalid characters in input");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
switch (length) {
|
|
|
|
case 3: input_value = 100 * ctoi(source[0]);
|
|
|
|
input_value += 10 * ctoi(source[1]);
|
|
|
|
input_value += ctoi(source[2]);
|
|
|
|
break;
|
|
|
|
case 2: input_value = 10 * ctoi(source[0]);
|
|
|
|
input_value += ctoi(source[1]);
|
|
|
|
break;
|
|
|
|
case 1: input_value = ctoi(source[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_value > 255) {
|
2017-07-28 03:01:53 +12:00
|
|
|
strcpy(symbol->errtxt, "509: Input too large");
|
2016-02-20 22:38:03 +13:00
|
|
|
return ZINT_ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
2020-12-23 23:57:24 +13:00
|
|
|
bp = bin_append_posn(input_value, 8, binary_string, bp);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-12-24 00:12:36 +13:00
|
|
|
data_codewords[0] = (unsigned char) (input_value >> 4);
|
|
|
|
data_codewords[1] = (unsigned char) (input_value & 0xF);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
2020-11-28 01:54:44 +13:00
|
|
|
rs_init_gf(&rs, 0x13);
|
|
|
|
rs_init_code(&rs, 5, 1);
|
|
|
|
rs_encode(&rs, 2, data_codewords, ecc_codewords);
|
2016-02-20 22:38:03 +13:00
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
2020-12-23 23:57:24 +13:00
|
|
|
bp = bin_append_posn(ecc_codewords[4 - i], 4, binary_string, bp);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 28; i += 2) {
|
|
|
|
if (binary_string[i] == '1') {
|
|
|
|
binary_string[i] = '0';
|
|
|
|
} else {
|
|
|
|
binary_string[i] = '1';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 01:21:43 +13:00
|
|
|
if (debug) {
|
|
|
|
printf("Binary String: %.28s\n", binary_string);
|
|
|
|
}
|
|
|
|
|
2016-02-20 22:38:03 +13:00
|
|
|
for (y = 8; y < 19; y++) {
|
2020-12-23 23:57:24 +13:00
|
|
|
r = y * 27;
|
2016-02-20 22:38:03 +13:00
|
|
|
for (x = 8; x < 19; x++) {
|
2020-12-23 23:57:24 +13:00
|
|
|
if (CompactAztecMap[r + x] == 1) {
|
2016-02-20 22:38:03 +13:00
|
|
|
set_module(symbol, y - 8, x - 8);
|
2021-07-06 23:13:34 +12:00
|
|
|
} else if (CompactAztecMap[r + x] && binary_string[CompactAztecMap[r + x] - 2000] == '1') {
|
2021-05-28 05:33:19 +12:00
|
|
|
set_module(symbol, y - 8, x - 8);
|
2016-02-20 22:38:03 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
symbol->row_height[y - 8] = 1;
|
|
|
|
}
|
2021-06-20 00:11:23 +12:00
|
|
|
symbol->height = 11;
|
2016-02-20 22:38:03 +13:00
|
|
|
symbol->rows = 11;
|
|
|
|
symbol->width = 11;
|
|
|
|
|
|
|
|
return 0;
|
2016-05-23 07:53:22 +12:00
|
|
|
}
|