pngminus: Improve portability and fix style (cont'd)

This commit is contained in:
Cosmin Truta 2018-08-05 21:49:57 -04:00
parent dcefbc7dcd
commit 3f0f1d5579

View File

@ -1,14 +1,15 @@
/*
* pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
* copyright (C) 1999,2015,2017 by Willem van Schaik <willem at schaik.com>
* copyright (C) 1999,2015,2017,2018 by Willem van Schaik <willem at schaik.com>
*
* version 1.0 - 1999.10.15 - First version.
* version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
* version 1.2 - 2017.04.22 - Add buffer-size check
* 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
* 1.2 - 2017.04.22 - Add buffer-size check
* 1.3 - 2017.08.24 - Fix potential overflow in buffer-size check
* (Glenn Randers-Pehrson)
* 1.4 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse)
* 1.5 - 2018.08.05 - Fix buffer overflow in tokenizer (Cosmin Truta)
* 1.6 - 2018.08.05 - Improve portability and fix style (Cosmin Truta)
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
@ -20,11 +21,7 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef __TURBOC__
#include <mem.h>
#include <fcntl.h>
#endif
#include <zlib.h>
#ifndef BOOL
#define BOOL unsigned char
@ -36,37 +33,19 @@
#define FALSE (BOOL) 0
#endif
#define STDIN 0
#define STDOUT 1
#define STDERR 2
/* to make pnm2png verbose so we can find problems (needs to be before png.h) */
/* make pnm2png verbose so we can find problems (needs to be before png.h) */
#ifndef PNG_DEBUG
#define PNG_DEBUG 0
#endif
#include "png.h"
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
#ifndef png_jmpbuf
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
#ifndef PNGMINUS_UNUSED
/* Unused formal parameter warnings are silenced using the following macro
* which is expected to have no bad effects on performance (optimizing
* compilers will probably remove it entirely).
*/
# define PNGMINUS_UNUSED(param) (void)param
#endif
/* function prototypes */
int main (int argc, char *argv[]);
void usage ();
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
BOOL alpha);
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
BOOL interlace, BOOL alpha);
void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
png_uint_32 get_data (FILE *pnm_file, int depth);
png_uint_32 get_value (FILE *pnm_file, int depth);
@ -144,16 +123,14 @@ int main(int argc, char *argv[])
}
} /* end for */
#ifdef __TURBOC__
/* set stdin/stdout to binary, we're reading the PNM always! in binary format */
#if defined(O_BINARY) && (O_BINARY != 0)
/* set stdin/stdout to binary,
* we're reading the PNM always! in binary format
*/
if (fp_rd == stdin)
{
setmode (STDIN, O_BINARY);
}
setmode (fileno (stdin), O_BINARY);
if (fp_wr == stdout)
{
setmode (STDOUT, O_BINARY);
}
setmode (fileno (stdout), O_BINARY);
#endif
/* call the conversion program itself */
@ -183,11 +160,6 @@ void usage()
{
fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, " by Willem van Schaik, 1999\n");
#ifdef __TURBOC__
fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
#else
fprintf (stderr, " for Linux (and Unix) compilers\n");
#endif
fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
fprintf (stderr, " or: ... | pnm2png [options]\n");
fprintf (stderr, "Options:\n");
@ -201,8 +173,8 @@ void usage()
* pnm2png
*/
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
BOOL alpha)
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
BOOL interlace, BOOL alpha)
{
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
@ -257,6 +229,7 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
#else
fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and\n");
fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
return FALSE;
#endif
}
else if ((type_token[1] == '2') || (type_token[1] == '5'))
@ -281,8 +254,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
bit_depth = 4;
else if (maxval <= 255)
bit_depth = 8;
else /* if (maxval <= 65535) */
else if (maxval <= 65535U)
bit_depth = 16;
else /* maxval > 65535U */
return FALSE;
}
else if ((type_token[1] == '3') || (type_token[1] == '6'))
{
@ -305,8 +280,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
bit_depth = 4;
else if (maxval <= 255)
bit_depth = 8;
else /* if (maxval <= 65535) */
else if (maxval <= 65535U)
bit_depth = 16;
else /* maxval > 65535U */
return FALSE;
}
else
{
@ -351,8 +328,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
alpha_depth = 4;
else if (maxval <= 255)
alpha_depth = 8;
else /* if (maxval <= 65535) */
else if (maxval <= 65535U)
alpha_depth = 16;
else /* maxval > 65535U */
return FALSE;
if (alpha_depth != bit_depth)
return FALSE;
}
@ -380,21 +359,29 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
if (packed_bitmap)
{
/* row data is as many bytes as can fit width x channels x bit_depth */
row_bytes = (width * channels * bit_depth + 7) / 8;
}
else
#endif
{
/* row_bytes is the width x number of channels x (bit-depth / 8) */
row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
}
if ((row_bytes == 0 || (size_t)height > ((size_t)(-1))/(size_t)row_bytes))
if ((row_bytes == 0) ||
((size_t) height > (size_t) (-1) / (size_t) row_bytes))
{
/* too big */
return FALSE;
}
if ((png_pixels = (png_byte *)
malloc ((size_t)row_bytes * (size_t)height * sizeof (png_byte))) == NULL)
malloc ((size_t) row_bytes * (size_t) height)) == NULL)
{
/* out of memory */
return FALSE;
}
/* read data from PNM file */
pix_ptr = png_pixels;
@ -405,9 +392,12 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
if (packed_bitmap)
{
for (i = 0; i < (int) row_bytes; i++)
{
/* png supports this format natively so no conversion is needed */
*pix_ptr++ = get_data (pnm_file, 8);
} else
}
}
else
#endif
{
for (col = 0; col < (int) width; col++)
@ -415,10 +405,15 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
for (i = 0; i < (channels - alpha_present); i++)
{
if (raw)
{
*pix_ptr++ = get_data (pnm_file, bit_depth);
}
else
{
if (bit_depth <= 8)
{
*pix_ptr++ = get_value (pnm_file, bit_depth);
}
else
{
tmp16 = get_value (pnm_file, bit_depth);
@ -428,40 +423,45 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
pix_ptr++;
}
}
}
if (alpha) /* read alpha-channel from pgm file */
{
if (alpha_raw)
{
*pix_ptr++ = get_data (alpha_file, alpha_depth);
}
else
{
if (alpha_depth <= 8)
{
*pix_ptr++ = get_value (alpha_file, bit_depth);
}
else
{
tmp16 = get_value (alpha_file, bit_depth);
*pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
*pix_ptr++ = (png_byte) (tmp16 & 0xFF);
}
} /* if alpha */
} /* if packed_bitmap */
}
} /* end if alpha */
} /* end if packed_bitmap */
} /* end for col */
} /* end for row */
/* prepare the standard PNG structures */
png_ptr = png_create_write_struct (png_get_libpng_ver(NULL), NULL, NULL,
NULL);
png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
NULL, NULL, NULL);
if (!png_ptr)
{
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
png_destroy_write_struct (&png_ptr, NULL);
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
@ -473,12 +473,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
}
#endif
/* setjmp() must be called in every function that calls a PNG-reading libpng function */
if (setjmp (png_jmpbuf (png_ptr)))
{
png_destroy_write_struct (&png_ptr, &info_ptr);
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
@ -494,14 +492,13 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
png_write_info (png_ptr, info_ptr);
/* if needed we will allocate memory for an new array of row-pointers */
if (row_pointers == (unsigned char**) NULL)
if (row_pointers == NULL)
{
if ((row_pointers = (png_byte **)
malloc (height * sizeof (png_bytep))) == NULL)
malloc (height * sizeof (png_byte *))) == NULL)
{
png_destroy_write_struct (&png_ptr, &info_ptr);
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
}
@ -519,18 +516,16 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
/* clean up after the write, and free any memory allocated */
png_destroy_write_struct (&png_ptr, &info_ptr);
if (row_pointers != (unsigned char**) NULL)
if (row_pointers != NULL)
free (row_pointers);
if (png_pixels != (unsigned char*) NULL)
if (png_pixels != NULL)
free (png_pixels);
PNGMINUS_UNUSED(raw); /* Quiet a Coverity defect */
return TRUE;
} /* end of pnm2png */
/*
* get_token() - gets the first string after whitespace
* get_token - gets the first string after whitespace
*/
void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
@ -572,7 +567,7 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size)
}
/*
* get_data() - takes first byte and converts into next pixel value,
* get_data - takes first byte and converts into next pixel value,
* taking as much bits as defined by bit-depth and
* using the bit-depth to fill up a byte (0Ah -> AAh)
*/
@ -606,7 +601,7 @@ png_uint_32 get_data (FILE *pnm_file, int depth)
}
/*
* get_value() - takes first (numeric) string and converts into number,
* get_value - takes first (numeric) string and converts into number,
* using the bit-depth to fill up a byte (0Ah -> AAh)
*/
@ -636,4 +631,3 @@ png_uint_32 get_value (FILE *pnm_file, int depth)
}
/* end of source */