diff --git a/src/tools/hvif2png/hvif2png.cpp b/src/tools/hvif2png/hvif2png.cpp index 70135bcc46..1dca2ece8b 100644 --- a/src/tools/hvif2png/hvif2png.cpp +++ b/src/tools/hvif2png/hvif2png.cpp @@ -31,6 +31,9 @@ #define SIZE_HVIF_BUFFER_STEP 1024 +static const uint8 kHvifMagic[] = { 'n', 'c', 'i', 'f' }; + + typedef struct h2p_hvif_buffer { uint8* buffer; size_t used; @@ -195,6 +198,8 @@ h2p_read_hvif_input(h2p_hvif_buffer* result, FILE* in) fprintf(stderr,"out of memory\n"); return 0; } + + result->allocated += SIZE_HVIF_BUFFER_STEP; } result->used += fread(&result->buffer[result->used], sizeof(uint8), @@ -208,6 +213,22 @@ h2p_read_hvif_input(h2p_hvif_buffer* result, FILE* in) } } + if (result->used < 4) { + fprintf(stderr, "the hvif data is too small to visably be valid\n"); + return 0; + } + + // hvif files have a magic string of "ncif" so we should check for that as + // well. + + if (memcmp(result->buffer, kHvifMagic, 4) != 0) { + fprintf(stderr, "the input data does not look like hvif because the" + " magic string is not 'ncif'; %d, %d, %d, %d\n", + result->buffer[0], result->buffer[1], result->buffer[2], + result->buffer[3]); + return 0; + } + return result->used; }