1
0
mirror of https://git.code.sf.net/p/zint/code synced 2026-01-08 20:41:59 +00:00

CMakeLists.txt: cmake min required 3.13 -> 3.5 using CMAKE_REQUIRED_LIBRARIES; add ZINT_COVERAGE option

bmp/emf/ps.c: allow for foreground colour in ULTRA
bmp/gif/pcx.c: allow for BARCODE_STDOUT (fflush/fclose)
emf.c: hexagon rotation field not used; dont't use float index in font loop; Windows stdout binary mode
png.c: remove unused 8-bit; simplify libpng error handling and fclose outfile on error
ps.c: fix buffer overflow on colour_to_pscolor() CMYK
tif.c: fix BLACKISZERO indexes
CODE39: simplify check digit setting
reedsol.c: rs_uint_init_gf() log/alog tables must be zeroed
ZBarcode_Encode: debug: fix access out-of-bounds on non-NUL-terminated source if length < 10
manual.txt/zint.h: document NUL-terminated strings
tests: cover further cases for output (bmp/emf/etc), eci/gb18030/gb2312/sjis, reedsol, AZTEC, CODE39
This commit is contained in:
gitlost
2021-07-26 15:29:05 +01:00
parent 059abdf6a1
commit 3f33ed3eb9
90 changed files with 3754 additions and 863 deletions

View File

@@ -48,28 +48,25 @@
#define SSET "0123456789ABCDEF"
struct mainprog_info_type {
long width;
long height;
FILE *outfile;
/* Note if change this need to change "backend/tests/test_png.c" definition also */
struct wpng_error_type {
struct zint_symbol *symbol;
jmp_buf jmpbuf;
};
static void writepng_error_handler(png_structp png_ptr, png_const_charp msg) {
struct mainprog_info_type *graphic;
STATIC_UNLESS_ZINT_TEST void wpng_error_handler(png_structp png_ptr, png_const_charp msg) {
struct wpng_error_type *wpng_error_ptr;
fprintf(stderr, "writepng libpng error: %s (F30)\n", msg);
fflush(stderr);
graphic = (struct mainprog_info_type*) png_get_error_ptr(png_ptr);
if (graphic == NULL) {
wpng_error_ptr = (struct wpng_error_type *) png_get_error_ptr(png_ptr);
if (wpng_error_ptr == NULL) {
/* we are completely hosed now */
fprintf(stderr,
"writepng severe error: jmpbuf not recoverable; terminating. (F31)\n");
fprintf(stderr, "Error 636: libpng error: %s\n", msg ? msg : "<NULL>");
fprintf(stderr, "Error 637: jmpbuf not recoverable, terminating\n");
fflush(stderr);
return;
return; /* libpng will call abort() */
}
longjmp(graphic->jmpbuf, 1);
sprintf(wpng_error_ptr->symbol->errtxt, "635: libpng error: %.60s", msg ? msg : "<NULL>");
longjmp(wpng_error_ptr->jmpbuf, 1);
}
/* Guestimate best compression strategy */
@@ -93,8 +90,8 @@ static int guess_compression_strategy(struct zint_symbol *symbol, unsigned char
}
INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf) {
struct mainprog_info_type wpng_info;
struct mainprog_info_type *graphic;
struct wpng_error_type wpng_error;
FILE *outfile;
png_structp png_ptr;
png_infop info_ptr;
int i;
@@ -109,6 +106,7 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
int bit_depth;
int compression_strategy;
unsigned char *pb;
const int output_to_stdout = symbol->output_options & BARCODE_STDOUT;
#ifndef _MSC_VER
unsigned char outdata[symbol->bitmap_width];
@@ -116,10 +114,7 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
unsigned char* outdata = (unsigned char*) _alloca(symbol->bitmap_width);
#endif
graphic = &wpng_info;
graphic->width = symbol->bitmap_width;
graphic->height = symbol->bitmap_height;
wpng_error.symbol = symbol;
fg.red = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]);
fg.green = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]);
@@ -143,7 +138,7 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
num_trans = 0;
if (symbol->symbology == BARCODE_ULTRA) {
static const int ultra_chars[8] = { 'W', 'C', 'B', 'M', 'R', 'Y', 'G', 'K' };
static png_color ultra_colours[8] = {
static const png_color ultra_colours[8] = {
{ 0xff, 0xff, 0xff, }, /* White */
{ 0, 0xff, 0xff, }, /* Cyan */
{ 0, 0, 0xff, }, /* Blue */
@@ -225,51 +220,57 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
if (num_palette <= 2) {
bit_depth = 1;
} else if (num_palette <= 16) {
bit_depth = 4;
} else {
bit_depth = 8;
bit_depth = 4;
}
/* Open output file in binary mode */
if (symbol->output_options & BARCODE_STDOUT) {
if (output_to_stdout) {
#ifdef _MSC_VER
if (-1 == _setmode(_fileno(stdout), _O_BINARY)) {
sprintf(symbol->errtxt, "631: Can't open output file (%d: %.30s)", errno, strerror(errno));
sprintf(symbol->errtxt, "631: Could not set stdout to binary (%d: %.30s)", errno, strerror(errno));
return ZINT_ERROR_FILE_ACCESS;
}
#endif
graphic->outfile = stdout;
outfile = stdout;
} else {
if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) {
sprintf(symbol->errtxt, "632: Can't open output file (%d: %.30s)", errno, strerror(errno));
if (!(outfile = fopen(symbol->outfile, "wb"))) {
sprintf(symbol->errtxt, "632: Could not open output file (%d: %.30s)", errno, strerror(errno));
return ZINT_ERROR_FILE_ACCESS;
}
}
/* Set up error handling routine as proc() above */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, writepng_error_handler, NULL);
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, &wpng_error, wpng_error_handler, NULL);
if (!png_ptr) {
strcpy(symbol->errtxt, "633: Out of memory");
strcpy(symbol->errtxt, "633: Insufficient memory for PNG write structure buffer");
if (!output_to_stdout) {
fclose(outfile);
}
return ZINT_ERROR_MEMORY;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
strcpy(symbol->errtxt, "634: Out of memory");
strcpy(symbol->errtxt, "634: Insufficient memory for PNG info structure buffer");
if (!output_to_stdout) {
fclose(outfile);
}
return ZINT_ERROR_MEMORY;
}
/* catch jumping here */
if (setjmp(graphic->jmpbuf)) {
if (setjmp(wpng_error.jmpbuf)) {
png_destroy_write_struct(&png_ptr, &info_ptr);
strcpy(symbol->errtxt, "635: libpng error occurred");
if (!output_to_stdout) {
fclose(outfile);
}
return ZINT_ERROR_MEMORY;
}
/* open output file with libpng */
png_init_io(png_ptr, graphic->outfile);
png_init_io(png_ptr, outfile);
/* set compression */
png_set_compression_level(png_ptr, 9);
@@ -281,7 +282,7 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
}
/* set Header block */
png_set_IHDR(png_ptr, info_ptr, graphic->width, graphic->height,
png_set_IHDR(png_ptr, info_ptr, symbol->bitmap_width, symbol->bitmap_height,
bit_depth, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
@@ -308,7 +309,7 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
/* write row contents to file */
png_write_row(png_ptr, outdata);
}
} else if (bit_depth == 4) {
} else { /* Bit depth 4 */
for (row = 0; row < symbol->bitmap_height; row++) {
unsigned char *image_data = outdata;
for (column = 0; column < symbol->bitmap_width; column += 2, image_data++) {
@@ -321,26 +322,18 @@ INTERNAL int png_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
/* write row contents to file */
png_write_row(png_ptr, outdata);
}
} else { /* Bit depth 8 */
for (row = 0; row < symbol->bitmap_height; row++) {
unsigned char *image_data = outdata;
for (column = 0; column < symbol->bitmap_width; column++, pb++, image_data++) {
*image_data = map[*pb];
}
/* write row contents to file */
png_write_row(png_ptr, outdata);
}
}
/* End the file */
png_write_end(png_ptr, NULL);
/* make sure we have disengaged */
if (png_ptr && info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr);
if (symbol->output_options & BARCODE_STDOUT) {
fflush(wpng_info.outfile);
png_destroy_write_struct(&png_ptr, &info_ptr);
if (output_to_stdout) {
fflush(outfile);
} else {
fclose(wpng_info.outfile);
fclose(outfile);
}
return 0;