Code:
typedef enum {
MAIN_BLOCK,
SUB_BLOCK,
SUB_BLOCK1
} block;
void print_bytes(const unsigned char *ptr, int len, block bl)
{
int i;
unsigned char op;
for(i=0;i<len;i++){
if((MAIN_BLOCK == bl) || (SUB_BLOCK == bl)){
op = ptr[i] & MASK;
if(ptr[i] == DONT_PRINT1)
continue;
if( ((i == (len-1)) && !(op ^ DONT_PRINT2))
|| (!(op ^ DONT_PRINT2) && (ptr[i+1] && (ptr[i+1] == DONT_PRINT1))) ){
op = ptr[i] & INV_MASK;
if(op){
printf("%x", op);
}
continue;
}
printf("%02x", ptr[i]);
}
else if(SUB_BLOCK1 == bl) {
printf("%02x", ptr[i]);
}
}
printf("|");
return;
}
void print_bytes_each(const unsigned char *ptr, int len)
{
int i;
for(i=0;i<len;i++)
printf("%02x|", ptr[i]);
return;
}
void print_data(const unsigned char *ptr, int len, block bl)
{
if(MAIN_BLOCK == bl){
print_bytes(ptr, 1, bl);
print_bytes(ptr+1, 3, bl);
print_bytes(ptr+4, 8, bl);
print_bytes(ptr+12, 8, bl);
} else if(SUB_BLOCK == bl){
print_bytes(ptr, 1, bl);
print_bytes(ptr+1, 1, bl);
print_bytes(ptr+2, 1, bl);
print_bytes(ptr+3, 1, bl);
print_bytes(ptr+4, 4, bl);
print_bytes(ptr+8, 8, bl);
print_bytes(ptr+16, 1, bl);
if(*ptr == intrim_pat1[2][1]){
print_bytes(ptr+17, 1, bl);
}
} else if(SUB_BLOCK1 == bl) {
print_bytes(ptr, 1, bl);
print_bytes_each(ptr+2, 14);
}
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 !=14){
if(len != fread(ptr+2, sizeof(char), len, fp))
err("Insufficient data");
print_data(ptr, len+1, SUB_BLOCK);
}
else if(len == 14){
if(len != fread(ptr+2, sizeof(char), len, fp))
err("Insufficient data");
print_data(ptr, len, SUB_BLOCK1);
}
return;
}