This is the main code for a morse code files decoder. From my understanding, it checks each bit in the binary file, maps it against a decoding table to find its alphabet equivalence and then returns the output. I'm not sure if I've got this right though? Can someone help to explain to me the main parts of this code?
Thanks
Code:
char *decode_file(unsigned char *input, int inputsize, int *outputsize)
{
unsigned short masks[7] = {0xe000, 0xf000, 0xf800, 0xfc00, 0xfe00, 0xff00, 0xff80};
int inpos = 0;
int outpos = 0;
short buff = 0;
int bitsleft = 0;
int i;
char *outputbuf;
struct decoding decoding_table[27];
build_decoding_table(decoding_table);
outputbuf = malloc((8*inputsize)/3 + 1);
memset(outputbuf, 0, (8*inputsize)/3 + 1);
while (inpos < inputsize)
{
while (bitsleft < 9)
{
int in = input[inpos++];
buff |= in << (8-bitsleft);
bitsleft += 8;
}
for(i = 0; i < 27; i++)
{
char d_char = decoding_table[i].character;
unsigned short d_bits = decoding_table[i].bits;
int d_len = decoding_table[i].length;
if ((buff & masks[d_len-3]) == d_bits)
{
buff = buff << d_len;
bitsleft -= d_len;
outputbuf[outpos++] = d_char;
i = -1; continue;
}
}
if (inpos < inputsize && bitsleft > 8)
{
/*printf("\nDecoding failed at byte %d of %d\n", inpos, inputsize);*/
break;
}
}
*outputsize = outpos;
return outputbuf;
}