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

@ -44,7 +44,7 @@
#define SSET "0123456789ABCDEF"
typedef struct s_statestruct {
unsigned char * pOut;
unsigned char *pOut;
unsigned char *pIn;
unsigned int InLen;
unsigned int OutLength;
@ -81,7 +81,7 @@ static unsigned char NextPaletteIndex(statestruct *pState)
}
static char BufferNextByte(statestruct *pState) {
static int BufferNextByte(statestruct *pState) {
(pState->OutPosCur)++;
/* Check if this position is a byte count position
* fg_f_bytecountbyte_set indicates, if byte count position bytes should be
@ -102,11 +102,10 @@ static char BufferNextByte(statestruct *pState) {
return 0;
}
static char AddCodeToBuffer(statestruct *pState, unsigned short CodeIn, unsigned char CodeBits) {
static int AddCodeToBuffer(statestruct *pState, unsigned short CodeIn, unsigned char CodeBits) {
/* Check, if we may fill up the current byte completely */
if (CodeBits >= pState->OutBitsFree) {
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
(CodeIn << (8 - pState->OutBitsFree));
(pState->pOut)[pState->OutPosCur] |= (unsigned char) (CodeIn << (8 - pState->OutBitsFree));
if (BufferNextByte(pState))
return -1;
CodeIn = (unsigned short) (CodeIn >> pState->OutBitsFree);
@ -123,8 +122,7 @@ static char AddCodeToBuffer(statestruct *pState, unsigned short CodeIn, unsigned
}
/* The remaining bits of CodeIn fit in the current byte. */
if (CodeBits > 0) {
(pState->pOut)[pState->OutPosCur] |= (unsigned char)
(CodeIn << (8 - pState->OutBitsFree));
(pState->pOut)[pState->OutPosCur] |= (unsigned char) (CodeIn << (8 - pState->OutBitsFree));
pState->OutBitsFree -= CodeBits;
}
return 0;
@ -149,7 +147,7 @@ static unsigned short FindPixelOutlet(statestruct *pState, unsigned short HeadNo
return 0;
}
static char NextCode(statestruct *pState, unsigned char * pPixelValueCur, unsigned char CodeBits) {
static int NextCode(statestruct *pState, unsigned char *pPixelValueCur, unsigned char CodeBits) {
unsigned short UpNode;
unsigned short DownNode;
/* start with the root node for last pixel chain */
@ -203,9 +201,9 @@ static int gif_lzw(statestruct *pState, int paletteBitSize) {
paletteBitSize = 2;
/* initial size of compression codes */
CodeBits = paletteBitSize+1;
CodeBits = paletteBitSize + 1;
pState->ClearCode = (1 << paletteBitSize);
pState->FreeCode = pState->ClearCode+2;
pState->FreeCode = pState->ClearCode + 2;
pState->OutBitsFree = 8;
pState->OutPosCur = -1;
pState->fByteCountByteSet = 0;
@ -232,7 +230,7 @@ static int gif_lzw(statestruct *pState, int paletteBitSize) {
return 0;
for (;;) {
char Res;
int Res;
/* generate and save the next code, which may consist of multiple input pixels. */
Res = NextCode(pState, &PixelValueCur, CodeBits);
if (Res < 0)
@ -249,7 +247,8 @@ static int gif_lzw(statestruct *pState, int paletteBitSize) {
}
// > Update last bytecount byte;
if (pState->OutByteCountPos < pState->OutPosCur) {
(pState->pOut)[pState->OutByteCountPos] = (unsigned char) (pState->OutPosCur - pState->OutByteCountPos - 1);
(pState->pOut)[pState->OutByteCountPos]
= (unsigned char) (pState->OutPosCur - pState->OutByteCountPos - 1);
}
pState->OutPosCur++;
return pState->OutPosCur;
@ -281,12 +280,13 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
int colourCount;
unsigned char paletteRGB[10][3];
int paletteCount, paletteCountCur, paletteIndex;
int pixelIndex;
unsigned int pixelIndex;
int paletteBitSize;
int paletteSize;
statestruct State;
int transparent_index;
int bgindex = -1, fgindex = -1;
const int output_to_stdout = symbol->output_options & BARCODE_STDOUT;
unsigned char backgroundColourIndex;
unsigned char RGBCur[3];
@ -297,11 +297,12 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
int fFound;
unsigned char pixelColour;
unsigned int bitmapSize = symbol->bitmap_height * symbol->bitmap_width;
/* Allow for overhead of 4 == code size + byte count + overflow byte + zero terminator */
unsigned int lzoutbufSize = symbol->bitmap_height * symbol->bitmap_width + 4;
unsigned int lzoutbufSize = bitmapSize + 4;
#ifdef _MSC_VER
char * lzwoutbuf;
char *lzwoutbuf;
#endif
#ifndef _MSC_VER
@ -344,8 +345,7 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
colourCount = 0;
paletteCount = 0;
/* loop over all pixels */
for ( pixelIndex = 0; pixelIndex < (symbol->bitmap_height * symbol->bitmap_width); pixelIndex++)
{
for (pixelIndex = 0; pixelIndex < bitmapSize; pixelIndex++) {
fFound = 0;
/* get pixel colour code */
pixelColour = pixelbuf[pixelIndex];
@ -434,7 +434,7 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
/* Set transparency */
/* Note: does not allow both transparent foreground and background -
* background takes prioroty */
* background takes priority */
transparent_index = -1;
if (strlen(symbol->fgcolour) > 6) {
if ((symbol->fgcolour[6] == '0') && (symbol->fgcolour[7] == '0')) {
@ -453,7 +453,7 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
/* 1,2 -> 1, 3,4 ->2, 5,6,7,8->3 */
paletteBitSize = 0;
paletteCountCur = paletteCount-1;
paletteCountCur = paletteCount - 1;
while (paletteCountCur != 0) {
paletteBitSize++;
paletteCountCur >>= 1;
@ -463,20 +463,20 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
paletteBitSize = 1;
/* palette size 2 ^ bit size */
paletteSize = 1<<paletteBitSize;
paletteSize = 1 << paletteBitSize;
/* Open output file in binary mode */
if ((symbol->output_options & BARCODE_STDOUT) != 0) {
if (output_to_stdout) {
#ifdef _MSC_VER
if (-1 == _setmode(_fileno(stdout), _O_BINARY)) {
sprintf(symbol->errtxt, "610: Can't open output file (%d: %.30s)", errno, strerror(errno));
sprintf(symbol->errtxt, "610: Could not set stdout to binary (%d: %.30s)", errno, strerror(errno));
return ZINT_ERROR_FILE_ACCESS;
}
#endif
gif_file = stdout;
} else {
if (!(gif_file = fopen(symbol->outfile, "wb"))) {
sprintf(symbol->errtxt, "611: Can't open output file (%d: %.30s)", errno, strerror(errno));
sprintf(symbol->errtxt, "611: Could not open output file (%d: %.30s)", errno, strerror(errno));
return ZINT_ERROR_FILE_ACCESS;
}
}
@ -581,21 +581,28 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf)
/* prepare state array */
State.pIn = pixelbuf;
State.InLen = symbol->bitmap_height * symbol->bitmap_width;
State.InLen = bitmapSize;
State.pOut = (unsigned char *) lzwoutbuf;
State.OutLength = lzoutbufSize;
/* call lzw encoding */
byte_out = gif_lzw(&State, paletteBitSize);
if (byte_out <= 0) {
fclose(gif_file);
if (!output_to_stdout) {
fclose(gif_file);
}
strcpy(symbol->errtxt, "613: Insufficient memory for LZW buffer");
return ZINT_ERROR_MEMORY;
}
fwrite(lzwoutbuf, byte_out, 1, gif_file);
/* GIF terminator */
fputc('\x3b', gif_file);
fclose(gif_file);
if (output_to_stdout) {
fflush(gif_file);
} else {
fclose(gif_file);
}
return 0;
}