Allow escape character processing

Fixes #101 reported by Martin Zizka
Converts \xNN characters > 0x7c to UTF-8 if appropriate
Removes some redundant legacy code
This commit is contained in:
Robin Stuart 2018-04-22 14:25:39 +01:00
parent 909e307636
commit dddf2934fc
3 changed files with 22 additions and 21 deletions

View File

@ -568,7 +568,7 @@ int ZBarcode_ValidID(int symbol_id) {
static int extended_charset(struct zint_symbol *symbol, const unsigned char *source, const int length) {
int error_number = 0;
/* These are the "elite" standards which can support multiple character sets */
switch (symbol->symbology) {
case BARCODE_QRCODE: error_number = qr_code(symbol, source, length);
@ -595,7 +595,7 @@ static int reduced_charset(struct zint_symbol *symbol, const unsigned char *sour
#else
unsigned char* preprocessed = (unsigned char*) _alloca(in_length + 1);
#endif
if (symbol->symbology == BARCODE_CODE16K) {
symbol->whitespace_width = 16;
symbol->border_width = 2;
@ -611,8 +611,8 @@ static int reduced_charset(struct zint_symbol *symbol, const unsigned char *sour
symbol->output_options += BARCODE_BOX;
}
}
switch (symbol->input_mode) {
switch (symbol->input_mode & 0x07) {
case DATA_MODE:
case GS1_MODE:
memcpy(preprocessed, source, in_length);
@ -626,7 +626,7 @@ static int reduced_charset(struct zint_symbol *symbol, const unsigned char *sour
}
break;
}
switch (symbol->symbology) {
case BARCODE_C25MATRIX: error_number = matrix_two_of_five(symbol, preprocessed, in_length);
break;
@ -814,7 +814,7 @@ int escape_char_process(struct zint_symbol *symbol, unsigned char *input_string,
in_posn = 0;
out_posn = 0;
do {
if (input_string[in_posn] == '\\') {
switch (input_string[in_posn + 1]) {
@ -861,7 +861,14 @@ int escape_char_process(struct zint_symbol *symbol, unsigned char *input_string,
hex1 = ctoi(input_string[in_posn + 2]);
hex2 = ctoi(input_string[in_posn + 3]);
if ((hex1 >= 0) && (hex2 >= 0)) {
escaped_string[out_posn] = (hex1 << 4) + hex2;
if (hex1 > 7 && (symbol->input_mode & UNICODE_MODE) != 0) {
// Convert to UTF-8
escaped_string[out_posn] = 0xc0 + (hex1 >> 2);
out_posn++;
escaped_string[out_posn] = 0x80 + ((hex1 & 0x03) << 4) + hex2;
} else {
escaped_string[out_posn] = (hex1 << 4) + hex2;
}
in_posn += 4;
} else {
strcpy(symbol->errtxt, "233: Corrupt escape character in input data");
@ -881,11 +888,11 @@ int escape_char_process(struct zint_symbol *symbol, unsigned char *input_string,
}
out_posn++;
} while (in_posn < *length);
memcpy(input_string, escaped_string, out_posn);
input_string[out_posn] = '\0';
*length = out_posn;
error_number = 0;
return error_number;
@ -1115,7 +1122,7 @@ int ZBarcode_Encode(struct zint_symbol *symbol, const unsigned char *source, int
error_tag(symbol->errtxt, ZINT_ERROR_INVALID_OPTION);
return ZINT_ERROR_INVALID_OPTION;
}
switch (symbol->symbology) {
case BARCODE_QRCODE:
case BARCODE_MICROQR:

View File

@ -223,8 +223,6 @@ extern "C" {
#define DATA_MODE 0
#define UNICODE_MODE 1
#define GS1_MODE 2
#define KANJI_MODE 3
#define SJIS_MODE 4
#define ESCAPE_MODE 8
// Data Matrix specific options

View File

@ -432,8 +432,6 @@ int main(int argc, char **argv) {
{"primary", 1, 0, 0},
{"scale", 1, 0, 0},
{"gs1", 0, 0, 0},
{"kanji", 0, 0, 0},
{"sjis", 0, 0, 0},
{"binary", 0, 0, 0},
{"notext", 0, 0, 0},
{"square", 0, 0, 0},
@ -489,14 +487,12 @@ int main(int argc, char **argv) {
if (!strcmp(long_options[option_index].name, "gs1")) {
my_symbol->input_mode = GS1_MODE;
}
if (!strcmp(long_options[option_index].name, "kanji")) {
my_symbol->input_mode = KANJI_MODE;
}
if (!strcmp(long_options[option_index].name, "sjis")) {
my_symbol->input_mode = SJIS_MODE;
}
if (!strcmp(long_options[option_index].name, "binary")) {
my_symbol->input_mode = DATA_MODE;
if (my_symbol->input_mode & ESCAPE_MODE) {
my_symbol->input_mode = DATA_MODE + ESCAPE_MODE;
} else {
my_symbol->input_mode = DATA_MODE;
}
}
if (!strcmp(long_options[option_index].name, "fg")) {
strncpy(my_symbol->fgcolour, optarg, 7);