Cleanup: pass header size to 'is_a' callbacks

No functional changes, prepare for fixing out-of-bounds access
when reading headers.
This commit is contained in:
Campbell Barton 2020-11-11 16:14:06 +11:00
parent 99f56b4c16
commit 2d60845786
17 changed files with 57 additions and 55 deletions

View File

@ -32,7 +32,7 @@ typedef struct ImFileType {
void (*init)(void);
void (*exit)(void);
int (*is_a)(const unsigned char *buf);
bool (*is_a)(const unsigned char *buf, const size_t size);
int (*ftype)(const struct ImFileType *type, const struct ImBuf *ibuf);
struct ImBuf *(*load)(const unsigned char *mem,
size_t size,
@ -67,7 +67,7 @@ void imb_tile_cache_tile_free(struct ImBuf *ibuf, int tx, int ty);
/* Type Specific Functions */
/* png */
int imb_is_a_png(const unsigned char *mem);
bool imb_is_a_png(const unsigned char *mem, const size_t size);
struct ImBuf *imb_loadpng(const unsigned char *mem,
size_t size,
int flags,
@ -75,7 +75,7 @@ struct ImBuf *imb_loadpng(const unsigned char *mem,
bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags);
/* targa */
int imb_is_a_targa(const unsigned char *buf);
bool imb_is_a_targa(const unsigned char *buf, const size_t size);
struct ImBuf *imb_loadtarga(const unsigned char *mem,
size_t size,
int flags,
@ -83,7 +83,7 @@ struct ImBuf *imb_loadtarga(const unsigned char *mem,
bool imb_savetarga(struct ImBuf *ibuf, const char *filepath, int flags);
/* iris */
int imb_is_a_iris(const unsigned char *mem);
bool imb_is_a_iris(const unsigned char *mem, const size_t size);
struct ImBuf *imb_loadiris(const unsigned char *mem,
size_t size,
int flags,
@ -91,7 +91,7 @@ struct ImBuf *imb_loadiris(const unsigned char *mem,
bool imb_saveiris(struct ImBuf *ibuf, const char *filepath, int flags);
/* jp2 */
int imb_is_a_jp2(const unsigned char *buf);
bool imb_is_a_jp2(const unsigned char *buf, const size_t size);
struct ImBuf *imb_load_jp2(const unsigned char *mem,
size_t size,
int flags,
@ -102,7 +102,7 @@ struct ImBuf *imb_load_jp2_filepath(const char *filepath,
bool imb_save_jp2(struct ImBuf *ibuf, const char *filepath, int flags);
/* jpeg */
int imb_is_a_jpeg(const unsigned char *mem);
bool imb_is_a_jpeg(const unsigned char *mem, const size_t size);
bool imb_savejpeg(struct ImBuf *ibuf, const char *filepath, int flags);
struct ImBuf *imb_load_jpeg(const unsigned char *buffer,
size_t size,
@ -110,7 +110,7 @@ struct ImBuf *imb_load_jpeg(const unsigned char *buffer,
char colorspace[IM_MAX_SPACE]);
/* bmp */
int imb_is_a_bmp(const unsigned char *buf);
bool imb_is_a_bmp(const unsigned char *buf, const size_t size);
struct ImBuf *imb_bmp_decode(const unsigned char *mem,
size_t size,
int flags,
@ -118,7 +118,7 @@ struct ImBuf *imb_bmp_decode(const unsigned char *mem,
bool imb_savebmp(struct ImBuf *ibuf, const char *filepath, int flags);
/* cineon */
int imb_is_a_cineon(const unsigned char *buf);
bool imb_is_a_cineon(const unsigned char *buf, const size_t size);
bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags);
struct ImBuf *imb_load_cineon(const unsigned char *mem,
size_t size,
@ -126,7 +126,7 @@ struct ImBuf *imb_load_cineon(const unsigned char *mem,
char colorspace[IM_MAX_SPACE]);
/* dpx */
int imb_is_a_dpx(const unsigned char *buf);
bool imb_is_a_dpx(const unsigned char *buf, const size_t size);
bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags);
struct ImBuf *imb_load_dpx(const unsigned char *mem,
size_t size,
@ -134,7 +134,7 @@ struct ImBuf *imb_load_dpx(const unsigned char *mem,
char colorspace[IM_MAX_SPACE]);
/* hdr */
int imb_is_a_hdr(const unsigned char *buf);
bool imb_is_a_hdr(const unsigned char *buf, const size_t size);
struct ImBuf *imb_loadhdr(const unsigned char *mem,
size_t size,
int flags,
@ -143,7 +143,7 @@ bool imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags);
/* tiff */
void imb_inittiff(void);
int imb_is_a_tiff(const unsigned char *buf);
bool imb_is_a_tiff(const unsigned char *buf, const size_t size);
struct ImBuf *imb_loadtiff(const unsigned char *mem,
size_t size,
int flags,

View File

@ -72,13 +72,13 @@ typedef struct BMPHEADER {
CHECK_HEADER_FIELD(_mem, "CI") || CHECK_HEADER_FIELD(_mem, "CP") || \
CHECK_HEADER_FIELD(_mem, "IC") || CHECK_HEADER_FIELD(_mem, "PT"))
static int checkbmp(const uchar *mem)
static bool checkbmp(const uchar *mem)
{
if (!CHECK_HEADER_FIELD_BMP(mem)) {
return 0;
}
int ret_val = 0;
bool ok = false;
BMPINFOHEADER bmi;
uint u;
@ -94,15 +94,15 @@ static int checkbmp(const uchar *mem)
if (bmi.biCompression == 0) {
u = LITTLE_SHORT(bmi.biBitCount);
if (u > 0 && u <= 32) {
ret_val = 1;
ok = true;
}
}
}
return ret_val;
return ok;
}
int imb_is_a_bmp(const uchar *buf)
bool imb_is_a_bmp(const uchar *buf, size_t UNUSED(size))
{
return checkbmp(buf);
}

View File

@ -188,7 +188,7 @@ bool imb_save_cineon(struct ImBuf *buf, const char *filepath, int flags)
return imb_save_dpx_cineon(buf, filepath, 1, flags);
}
int imb_is_a_cineon(const unsigned char *buf)
bool imb_is_a_cineon(const unsigned char *buf, size_t UNUSED(size))
{
return logImageIsCineon(buf);
}
@ -198,7 +198,7 @@ ImBuf *imb_load_cineon(const unsigned char *mem,
int flags,
char colorspace[IM_MAX_SPACE])
{
if (imb_is_a_cineon(mem)) {
if (imb_is_a_cineon(mem, size)) {
return imb_load_dpx_cineon(mem, size, 1, flags, colorspace);
}
return NULL;
@ -209,7 +209,7 @@ bool imb_save_dpx(struct ImBuf *buf, const char *filepath, int flags)
return imb_save_dpx_cineon(buf, filepath, 0, flags);
}
int imb_is_a_dpx(const unsigned char *buf)
bool imb_is_a_dpx(const unsigned char *buf, size_t UNUSED(size))
{
return logImageIsDpx(buf);
}
@ -219,7 +219,7 @@ ImBuf *imb_load_dpx(const unsigned char *mem,
int flags,
char colorspace[IM_MAX_SPACE])
{
if (imb_is_a_dpx(mem)) {
if (imb_is_a_dpx(mem, size)) {
return imb_load_dpx_cineon(mem, size, 0, flags, colorspace);
}
return NULL;

View File

@ -72,18 +72,19 @@ bool imb_save_dds(struct ImBuf *ibuf, const char *name, int /*flags*/)
return true;
}
int imb_is_a_dds(const unsigned char *mem) /* note: use at most first 32 bytes */
/* note: use at most first 32 bytes */
bool imb_is_a_dds(const unsigned char *mem, size_t UNUSED(size))
{
/* heuristic check to see if mem contains a DDS file */
/* header.fourcc == FOURCC_DDS */
if ((mem[0] != 'D') || (mem[1] != 'D') || (mem[2] != 'S') || (mem[3] != ' ')) {
return 0;
return false;
}
/* header.size == 124 */
if ((mem[4] != 124) || mem[5] || mem[6] || mem[7]) {
return 0;
return false;
}
return 1;
return true;
}
struct ImBuf *imb_load_dds(const unsigned char *mem,
@ -108,7 +109,7 @@ struct ImBuf *imb_load_dds(const unsigned char *mem,
*/
colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
if (!imb_is_a_dds(mem)) {
if (!imb_is_a_dds(mem, size)) {
return nullptr;
}

View File

@ -26,7 +26,8 @@
extern "C" {
#endif
int imb_is_a_dds(const unsigned char *mem); /* use only first 32 bytes of mem */
/* use only first 32 bytes of mem */
bool imb_is_a_dds(const unsigned char *mem, const size_t size);
bool imb_save_dds(struct ImBuf *ibuf, const char *name, int flags);
struct ImBuf *imb_load_dds(const unsigned char *mem,
size_t size,

View File

@ -243,7 +243,7 @@ static void test_endian_zbuf(struct ImBuf *ibuf)
/* this one is only def-ed once, strangely... */
#define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
int imb_is_a_iris(const uchar *mem)
bool imb_is_a_iris(const uchar *mem, size_t UNUSED(size))
{
return ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC));
}
@ -271,7 +271,7 @@ struct ImBuf *imb_loadiris(const uchar *mem, size_t size, int flags, char colors
return NULL;
}
if (!imb_is_a_iris(mem)) {
if (!imb_is_a_iris(mem, size)) {
return NULL;
}

View File

@ -80,7 +80,7 @@ static OPJ_CODEC_FORMAT format_from_header(const unsigned char mem[JP2_FILEHEADE
return OPJ_CODEC_UNKNOWN;
}
int imb_is_a_jp2(const unsigned char *buf)
bool imb_is_a_jp2(const unsigned char *buf, size_t UNUSED(size))
{
return (check_jp2(buf) || check_j2k(buf));
}

View File

@ -57,7 +57,7 @@ static ImBuf *ibJpegImageFromCinfo(struct jpeg_decompress_struct *cinfo, int fla
static const uchar jpeg_default_quality = 75;
static uchar ibuf_quality;
int imb_is_a_jpeg(const unsigned char *mem)
bool imb_is_a_jpeg(const unsigned char *mem, const size_t UNUSED(size))
{
if ((mem[0] == 0xFF) && (mem[1] == 0xD8)) {
return 1;
@ -429,7 +429,7 @@ ImBuf *imb_load_jpeg(const unsigned char *buffer,
struct my_error_mgr jerr;
ImBuf *ibuf;
if (!imb_is_a_jpeg(buffer)) {
if (!imb_is_a_jpeg(buffer, size)) {
return NULL;
}

View File

@ -163,7 +163,7 @@ static ImBuf *imb_oiio_load_image_float(
extern "C" {
int imb_is_a_photoshop(const unsigned char *mem)
bool imb_is_a_photoshop(const unsigned char *mem, size_t UNUSED(size))
{
const unsigned char magic[4] = {'8', 'B', 'P', 'S'};
return memcmp(magic, mem, sizeof(magic)) == 0;

View File

@ -31,7 +31,7 @@ extern "C" {
struct ImBuf;
int imb_is_a_photoshop(const unsigned char *mem);
bool imb_is_a_photoshop(const unsigned char *mem, const size_t size);
int imb_save_photoshop(struct ImBuf *ibuf, const char *name, int flags);

View File

@ -330,7 +330,7 @@ extern "C" {
* Test presence of OpenEXR file.
* \param mem: pointer to loaded OpenEXR bitstream
*/
int imb_is_a_openexr(const unsigned char *mem)
bool imb_is_a_openexr(const unsigned char *mem, const size_t UNUSED(size))
{
return Imf::isImfMagic((const char *)mem);
}
@ -1905,7 +1905,7 @@ struct ImBuf *imb_load_openexr(const unsigned char *mem,
IMemStream *membuf = nullptr;
MultiPartInputFile *file = nullptr;
if (imb_is_a_openexr(mem) == 0) {
if (imb_is_a_openexr(mem, size) == 0) {
return nullptr;
}

View File

@ -32,7 +32,7 @@ extern "C" {
void imb_initopenexr(void);
void imb_exitopenexr(void);
int imb_is_a_openexr(const unsigned char *mem);
bool imb_is_a_openexr(const unsigned char *mem, const size_t size);
bool imb_save_openexr(struct ImBuf *ibuf, const char *name, int flags);

View File

@ -59,9 +59,9 @@ BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val)
return (_val << 8) + _val;
}
int imb_is_a_png(const unsigned char *mem)
bool imb_is_a_png(const unsigned char *mem, size_t UNUSED(size))
{
int ret_val = 0;
bool ret_val = 0;
#if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2)
/* Older version of libpng doesn't use const pointer to memory. */
@ -548,7 +548,7 @@ ImBuf *imb_loadpng(const unsigned char *mem, size_t size, int flags, char colors
float *to_float;
unsigned int channels;
if (imb_is_a_png(mem) == 0) {
if (imb_is_a_png(mem, size) == 0) {
return NULL;
}

View File

@ -197,7 +197,7 @@ static void FLOAT2RGBE(const fCOLOR fcol, RGBE rgbe)
/* ImBuf read */
int imb_is_a_hdr(const unsigned char *buf)
bool imb_is_a_hdr(const unsigned char *buf, size_t UNUSED(size))
{
/* NOTE: `#?RADIANCE` is used by other programs such as `ImageMagik`,
* Although there are some files in the wild that only use `#?` (from looking online).
@ -226,7 +226,7 @@ struct ImBuf *imb_loadhdr(const unsigned char *mem,
const unsigned char *ptr, *mem_eof = mem + size;
char oriY[80], oriX[80];
if (imb_is_a_hdr(mem)) {
if (imb_is_a_hdr(mem, size)) {
colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT);
/* find empty line, next line is resolution info */

View File

@ -361,7 +361,7 @@ bool imb_savetarga(struct ImBuf *ibuf, const char *filepath, int UNUSED(flags))
return ok;
}
static int checktarga(TARGA *tga, const unsigned char *mem)
static bool checktarga(TARGA *tga, const unsigned char *mem)
{
tga->numid = mem[0];
tga->maptyp = mem[1];
@ -378,7 +378,7 @@ static int checktarga(TARGA *tga, const unsigned char *mem)
tga->imgdes = mem[17];
if (tga->maptyp > 1) {
return 0;
return false;
}
switch (tga->imgtyp) {
case 1: /* raw cmap */
@ -389,27 +389,27 @@ static int checktarga(TARGA *tga, const unsigned char *mem)
case 11: /* b&w */
break;
default:
return 0;
return false;
}
if (tga->mapsize && tga->mapbits > 32) {
return 0;
return false;
}
if (tga->xsize <= 0) {
return 0;
return false;
}
if (tga->ysize <= 0) {
return 0;
return false;
}
if (tga->pixsize > 32) {
return 0;
return false;
}
if (tga->pixsize == 0) {
return 0;
return false;
}
return 1;
return true;
}
int imb_is_a_targa(const unsigned char *buf)
bool imb_is_a_targa(const unsigned char *buf, size_t UNUSED(size))
{
TARGA tga;

View File

@ -316,7 +316,7 @@ static TIFF *imb_tiff_client_open(ImbTIFFMemFile *memFile, const unsigned char *
* hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005.
*/
#define IMB_TIFF_NCB 4 /* number of comparison bytes used */
int imb_is_a_tiff(const unsigned char *buf)
bool imb_is_a_tiff(const unsigned char *buf, size_t UNUSED(size))
{
const char big_endian[IMB_TIFF_NCB] = {0x4d, 0x4d, 0x00, 0x2a};
const char lil_endian[IMB_TIFF_NCB] = {0x49, 0x49, 0x2a, 0x00};
@ -578,7 +578,7 @@ ImBuf *imb_loadtiff(const unsigned char *mem,
fprintf(stderr, "imb_loadtiff: size < IMB_TIFF_NCB\n");
return NULL;
}
if (imb_is_a_tiff(mem) == 0) {
if (imb_is_a_tiff(mem, size) == 0) {
return NULL;
}

View File

@ -169,7 +169,7 @@ int IMB_ispic_type_from_memory(const unsigned char *mem, const size_t mem_size)
for (const ImFileType *type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
if (type->is_a != NULL) {
if (type->is_a(buf)) {
if (type->is_a(buf, HEADER_SIZE)) {
return type->filetype;
}
}
@ -200,7 +200,7 @@ bool IMB_ispic_type_matches(const char *filepath, int filetype)
* Keep the check for developers. */
BLI_assert(type->is_a != NULL);
if (type->is_a != NULL) {
return type->is_a(buf);
return type->is_a(buf, HEADER_SIZE);
}
}
}