Code:
#include <stdio.h>
#include <stdlib.h>
#define err(x) {printf("\nError: %s... Exiting...\n", x); exit(1);}
static unsigned char start = 0x32;
static unsigned char pat1[] = {0x99, 0x11, 0x45};
static unsigned char pat2[] = {0x73, 0x49};
static unsigned char pat3[] = {0xff, 0x34};
static unsigned char intrim_pat1[][2] = { {0x03, 0x80}, {0x03, 0x81}, {0x03, 0x83}, {0x03, 0x86}, {0x03, 0x87} };
static unsigned char end[] = {0xff, 0x33};
typedef enum {
MAIN_BLOCK,
SUB_BLOCK
} block;
void print_bytes(const unsigned char *ptr, int len)
{
int i;
for(i=0;i<len;i++)
printf("%02x", ptr[i]);
printf("|");
return;
}
void print_data(const unsigned char *ptr, int len, block bl)
{
int i;
if(MAIN_BLOCK == bl){
print_bytes(ptr, 1);
print_bytes(ptr+1, 3);
print_bytes(ptr+4, 8);
print_bytes(ptr+12, 8);
} else {
print_bytes(ptr, 1);
print_bytes(ptr+1, 1);
print_bytes(ptr+2, 1);
print_bytes(ptr+3, 1);
print_bytes(ptr+4, 4);
print_bytes(ptr+8, 8);
print_bytes(ptr+16, 1);
if(*ptr == intrim_pat1[2][1]){
print_bytes(ptr+17, 1);
}
}
return;
}
void get_len_and_print(FILE *fp, unsigned char *ptr)
{
int len = 0;
//only buf[0] is populated at this stage
if(1 != fread(ptr+1, sizeof(char), 1, fp))
err("Insufficient data");
len = *(ptr+1);
if(len != fread(ptr+2, sizeof(char), len, fp))
err("Insufficient data");
print_data(ptr, len+1, SUB_BLOCK);
return;
}
int main(int argc, char **argv)
{
if(argc < 2)
err("File name missing");
char found = 0, more = 0, again = 0;
unsigned char buf[256];
unsigned char *ptr = buf;
int pos = 0, i, len;
int arr_size = (sizeof(intrim_pat1)/2);
FILE *fp = fopen(argv[1], "rb");
if(!fp) err("Unable to open the file");
while(2 == fread(ptr, sizeof(char), 2, fp)){
pos = ftell(fp);
//check for end of file pattern
if(found && !memcmp(buf, end, 2)){
found=0; //start over or stop??
continue;
}
//check for 0xff 0x34
if(found && !(memcmp(buf, pat3, 2))){
more = 1;
continue;
}
if(found && more){
for(i=0; i < arr_size; i++){
// We got the intrim pattern.
if(!memcmp(buf, intrim_pat1[i], 2)){
again=1;
more=0;
break;
}
}
if(again) {
// Now read the next 1 byte which is actually the size
if(1 != fread(ptr+2, sizeof(char), 1, fp))
err("Insufficient data");
len = buf[2];
if(len > 255) {
fprintf(stderr, "Len overflow: %d\n", len);
return 1;
}
if(len != fread(ptr+3, sizeof(char), len, fp))
err("Insufficient data");
print_data(ptr+1, len+2, SUB_BLOCK);
// Now check for the remaining pattern
// Assuming it will be in ascending order
// Break even if we dont find the very next pattern
// Also, reset the fp. We will flush all the data in buffer
// till now, since we have printed them already
pos = ftell(fp);
if(1 != fread(ptr, sizeof(char), 1, fp))
err("Insufficient data");
while(i < arr_size){
if( buf[0] == intrim_pat1[i][1]){
get_len_and_print(fp, ptr);
if(1 != fread(ptr, sizeof(char), 1, fp))
err("Insufficient data");
pos = ftell(fp);
}
i++;
}
again=0;
}
}
if(buf[0] == start){
if(18 != fread(ptr+2, sizeof(char), 18, fp))
err("Insufficient data");
if(memcmp(buf+4, pat1, 3) && memcmp(buf+12, pat2, 2)){
fseek(fp, pos, SEEK_SET);
}else{
found = 1; //found the starting of the block with data
printf("\n");
print_data(ptr, 19, MAIN_BLOCK);
}
continue;
}
pos--;
if(fseek(fp, pos, SEEK_SET))
err("Error in seeking");
}
return 0;
}