From d550f18d90c18f26630ccc33ad1fb6c08663cd64 Mon Sep 17 00:00:00 2001 From: tgotic Date: Mon, 16 May 2011 20:03:21 +0200 Subject: [PATCH] latin1_process function changed --- backend/aztec.c | 9 ++++-- backend/common.c | 51 ++++++++++++++++----------------- backend/common.h | 2 +- backend/library.c | 7 +++-- backend/maxicode.c | 7 +++-- backend/png.c | 70 ++++++++++++++++------------------------------ zint.nsi | 2 +- 7 files changed, 68 insertions(+), 80 deletions(-) diff --git a/backend/aztec.c b/backend/aztec.c index bd2d051f..4425f9fc 100644 --- a/backend/aztec.c +++ b/backend/aztec.c @@ -680,8 +680,11 @@ int aztec(struct zint_symbol *symbol, unsigned char source[], int length) local_source[length] = '\0'; break; case UNICODE_MODE: - err_code = latin1_process(symbol, source, local_source, &length); - if(err_code != 0) { return err_code; } + err_code = latin1_process(source, local_source, &length); + if(err_code != 0) { + strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)"); + return err_code; + } break; } @@ -1317,7 +1320,7 @@ int aztec_runes(struct zint_symbol *symbol, unsigned char source[], int length) rs_encode(2, data_codewords, ecc_codewords); rs_free(); - strcpy(binary_string, ""); + memset(binary_string, 0, 28); for(i = 0; i < 5; i++) { if(ecc_codewords[4 - i] & 0x08) { binary_string[(i * 4) + 8] = '1'; } else { binary_string[(i * 4) + 8] = '0'; } diff --git a/backend/common.c b/backend/common.c index 9c5277cf..5156bc3e 100644 --- a/backend/common.c +++ b/backend/common.c @@ -283,40 +283,41 @@ float froundup(float input) return output; } -int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[], int *length) +int latin1_process(unsigned char source[], unsigned char preprocessed[], int *length) { int j, i, next; /* Convert Unicode to Latin-1 for those symbologies which only support Latin-1 */ j = 0; i = 0; - do { - next = -1; - if(source[i] < 128) { - preprocessed[j] = source[i]; - j++; - next = i + 1; - } else { - if(source[i] == 0xC2) { - preprocessed[j] = source[i + 1]; + if (length && *length) { + do { + next = -1; + if(source[i] < 128) { + preprocessed[j] = source[i]; j++; - next = i + 2; + next = i + 1; + } else { + if(source[i] == 0xC2) { + preprocessed[j] = source[i + 1]; + j++; + next = i + 2; + } + if(source[i] == 0xC3) { + preprocessed[j] = source[i + 1] + 64; + j++; + next = i + 2; + } } - if(source[i] == 0xC3) { - preprocessed[j] = source[i + 1] + 64; - j++; - next = i + 2; + if(next == -1) { + return ZERROR_INVALID_DATA; } - } - if(next == -1) { - strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)"); - return ZERROR_INVALID_DATA; - } - i = next; - } while(i < *length); - preprocessed[j] = '\0'; - *length = j; - + i = next; + } while(i < *length); + preprocessed[j] = '\0'; + *length = j; + } + return 0; } diff --git a/backend/common.h b/backend/common.h index 52bc9117..6761b3c3 100644 --- a/backend/common.h +++ b/backend/common.h @@ -61,7 +61,7 @@ extern void unset_module(struct zint_symbol *symbol, int y_coord, int x_coord); extern int istwodigits(unsigned char source[], int position); extern float froundup(float input); extern int parunmodd(unsigned char llyth); -extern int latin1_process(struct zint_symbol *symbol, unsigned char source[], unsigned char preprocessed[], int *length); +extern int latin1_process(unsigned char source[], unsigned char preprocessed[], int *length); extern int utf8toutf16(struct zint_symbol *symbol, unsigned char source[], int vals[], int *length); #ifdef __cplusplus } diff --git a/backend/library.c b/backend/library.c index eba41374..00e98d97 100644 --- a/backend/library.c +++ b/backend/library.c @@ -469,8 +469,11 @@ int reduced_charset(struct zint_symbol *symbol, unsigned char *source, int lengt preprocessed[length] = '\0'; break; case UNICODE_MODE: - error_number = latin1_process(symbol, source, preprocessed, &length); - if(error_number != 0) { return error_number; } + error_number = latin1_process(source, preprocessed, &length); + if(error_number != 0) { + strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)"); + return error_number; + } break; } diff --git a/backend/maxicode.c b/backend/maxicode.c index c39b137b..55bdc376 100644 --- a/backend/maxicode.c +++ b/backend/maxicode.c @@ -575,8 +575,11 @@ int maxicode(struct zint_symbol *symbol, unsigned char source[], int length) local_source[length] = '\0'; break; case UNICODE_MODE: - error_number = latin1_process(symbol, source, local_source, &length); - if(error_number != 0) { return error_number; } + error_number = latin1_process(source, local_source, &length); + if(error_number != 0) { + strcpy(symbol->errtxt, "error: Invalid character in input string (only Latin-1 characters supported)"); + return error_number; + } break; } memset(maxi_codeword, 0, sizeof(maxi_codeword)); diff --git a/backend/png.c b/backend/png.c index 07c84fef..e45b1a5a 100644 --- a/backend/png.c +++ b/backend/png.c @@ -680,62 +680,33 @@ int maxi_png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) return error_number; } -void to_latin1(unsigned char source[], unsigned char preprocessed[]) -{ - int j, i, input_length; - - input_length = ustrlen(source); - - j = 0; - i = 0; - do { - if(source[i] < 128) { - preprocessed[j] = source[i]; - j++; - i++; - } else { - if(source[i] == 0xC2) { - preprocessed[j] = source[i + 1]; - j++; - i += 2; - } - if(source[i] == 0xC3) { - preprocessed[j] = source[i + 1] + 64; - j++; - i += 2; - } - } - } while (i < input_length); - preprocessed[j] = '\0'; - - return; -} - int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) { int textdone, main_width, comp_offset, large_bar_count; char textpart[10], addon[6]; float addon_text_posn, preset_height, large_bar_height; int i, r, textoffset, yoffset, xoffset, latch, image_width, image_height; - char *pixelbuf; + char *pixelbuf = NULL; int addon_latch = 0, smalltext = 0; int this_row, block_width, plot_height, plot_yposn, textpos; float row_height, row_posn; int error_number; int default_text_posn; int next_yposn; -#ifndef _MSC_VER - unsigned char local_text[ustrlen(symbol->text) + 1]; -#else - unsigned char* local_text = (unsigned char*)_alloca(ustrlen(symbol->text) + 1); -#endif + unsigned char* local_text = NULL; + int tlen = ustrlen(symbol->text); - if(symbol->show_hrt != 0) { - to_latin1(symbol->text, local_text); - } else { - local_text[0] = '\0'; + if (tlen) { + local_text = (unsigned char*)calloc(tlen + 1, sizeof(char)); + if (NULL == local_text) { + strcpy(symbol->errtxt, "Out of memory"); + return ZERROR_MEMORY; + } } + if(symbol->show_hrt != 0 && tlen) + latin1_process(symbol->text, local_text, &tlen); + textdone = 0; main_width = symbol->width; strcpy(addon, ""); @@ -773,7 +744,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) /* Certain symbols need whitespace otherwise characters get chopped off the sides */ if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { - switch(ustrlen(local_text)) { + switch(tlen) { case 13: /* EAN 13 */ case 16: case 19: @@ -805,7 +776,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) r = 0; /* Isolate add-on text */ if(is_extendable(symbol->symbology)) { - for(i = 0; i < ustrlen(local_text); i++) { + for(i = 0; i < tlen; i++) { if (latch == 1) { addon[r] = local_text[i]; r++; @@ -817,7 +788,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) } addon[r] = '\0'; - if(ustrlen(local_text) != 0) { + if(tlen) { textoffset = 9; } else { textoffset = 0; @@ -829,6 +800,9 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) if (!(pixelbuf = (char *) malloc(image_width * image_height))) { printf("Insufficient memory for pixel buffer"); + if(local_text) + free(local_text); + return ZERROR_ENCODING_PROBLEM; } else { for(i = 0; i < (image_width * image_height); i++) { @@ -894,7 +868,7 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { /* guard bar extensions and text formatting for EAN8 and EAN13 */ - switch(ustrlen(local_text)) { + switch(tlen) { case 8: /* EAN-8 */ case 11: case 14: @@ -1105,13 +1079,17 @@ int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) } /* Put the human readable text at the bottom */ - if((textdone == 0) && (ustrlen(local_text) != 0)) { + if((textdone == 0) && tlen) { textpos = (image_width / 2); draw_string(pixelbuf, (char*)local_text, textpos, default_text_posn, smalltext, image_width, image_height); } error_number=png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); free(pixelbuf); + + if (local_text) + free(local_text); + return error_number; } diff --git a/zint.nsi b/zint.nsi index 8b7feafe..57b1d8c2 100644 --- a/zint.nsi +++ b/zint.nsi @@ -5,7 +5,7 @@ ;****************************************************************************** !define PRODUCT_NAME "Zint" !define PRODUCT_EXE "qtZint.exe" -!define PRODUCT_VERSION "2.4.2.0" +!define PRODUCT_VERSION "2.4.2.2" !define PRODUCT_WEB_SITE "http://www.zint.org.uk" !define PRODUCT_PUBLISHER "Robin Stuart & BogDan Vatra" !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_EXE}"