Hi,
I'm trying to make a simple RLE encoder that executes as fast as is possible. I've made this code:-
Code:
int EncodeFrame(FILE *src, FILE *dest)
{
unsigned char *inptr, data;
fread(buffer, 65280, 1, src);
inptr = buffer;
while (inptr - buffer < 65280)
{
data = *(inptr++);
if (data == *inptr)
{
unsigned char count = 1;
while (data == *inptr && count < 63)
{
count++;
inptr++;
}
putc(0xC0 | count, dest);
putc(data, dest);
}
else
{
if (0xC0 == (data & 0xC0))
putc(0xC1, dest);
putc(data, dest);
}
}
printf("Done (%lu)\n", inptr - buffer);
return 0;
}
As you can see, it reads 65,280 bytes of data from an input file into a buffer of the same size, and uses inptr as a read pointer from which comparisons are made.
The thing is, the printf message always manages to output "Done (65340)" instead of "Done (65280)", meaning that extraneous data is being stored, a problem highlighted by the decoder, which always ends up trying to write beyond the allocated memory area and crashing.
Can anyone see what isn't right here?