I have some code to load a uncompressed BTF image file (look at my web site for specs) and my loader keeps crashing. I ran a debug and it crashes where I call the last fread(). I think I'm reading too far into the file, but I can't see how that's possible. I tried adding an extra byte to the end of the file, but it still crashed.
Code:
// Macro to find the size of the data block.
#define DATABLOCK_SIZE (width*height*bytedepth)
unsigned char BTFImage::Load(char FileName[FILENAME_MAX], int )
{
FILE *FileHandle = fopen(FileName, "rb"); // Load the file
if (!FileHandle) // If it can't load the file, it more than likely doesn't exist
return BTF_NOTEXIST;
unsigned char header[BTF_HEADERSIZE]; // Buffer to hold the header in
fread(header, BTF_HEADERSIZE, 1, FileHandle); // Read the header
if (header[0] != 0x42 || header[1] != 0x54 || header[2] != 0x46) // Check the key
return BTF_BADFILE;
switch (header[3]) // Decode the flag byte
{
case 0x10:
compression = BTF_CLEAN;
bytedepth = 1;
break;
case 0x12:
compression = BTF_RLELOOKUP;
bytedepth = 1;
break;
case 0x13:
compression = BTF_RLELOOKUPINV;
bytedepth = 1;
break;
case 0x30:
compression = BTF_CLEAN;
bytedepth = 3;
break;
case 0x31:
compression = BTF_LOOKUP;
bytedepth = 3;
break;
case 0x32:
compression = BTF_RLELOOKUP;
bytedepth = 3;
break;
case 0x33:
compression = BTF_RLELOOKUPINV;
bytedepth = 3;
break;
case 0x40:
compression = BTF_CLEAN;
bytedepth = 4;
break;
case 0x41:
compression = BTF_LOOKUP;
bytedepth = 4;
break;
case 0x42:
compression = BTF_RLELOOKUP;
bytedepth = 4;
break;
case 0x43:
compression = BTF_RLELOOKUPINV;
bytedepth = 4;
break;
default:
return BTF_BADFLAGBYTE;
}
width = header[4] * 256 + header[5]; // Find width and height
height = header[6] * 256 + header[7];
data = new unsigned char[DATABLOCK_SIZE]; // Allocate space for the image data
if (data = 0) // If the file is GIGANTIC and you are out of RAM, return BTF_NORAM
return BTF_NORAM;
if (compression == BTF_CLEAN) // If no compression
fread(data, DATABLOCK_SIZE, 1, FileHandle); // Read the whole block directly
footer = 0;
fclose(FileHandle); // Close the file
// filename = FileName; // Set the filename to the one passed to us
}
Here is the file I'm loading (in hexadecimal format)
Code:
42 54 46 10 00 01 00 01 05