I'm trying to make a simple RLE encoder that executes as fast as is possible. I've made this code:-
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.
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)
putc(0xC0 | count, dest);
if (0xC0 == (data & 0xC0))
printf("Done (%lu)\n", inptr - buffer);
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?