struct packet *packet_deserialize(unsigned char *buf, int len) { if (len < 11) return NULL; struct packet *ret = malloc(sizeof(struct packet)); ret->family = *buf; ret->unk1 = *(buf+1); ret->dir = *(buf+2); ret->pltype = *(buf+3); ret->connid = ntohs(*(unsigned short *)(buf+4)); ret->subseq = *(buf+6); ret->unk2 = *(buf+7); ret->sessid = ntohs(*(unsigned short *)(buf+8)); ret->tail = *(buf+10); ret->data = NULL; ret->rawdata = NULL; ret->rawdatalen = 0; if (len > 11) { // try to deserialize inner block ret->data = block_deserialize(buf+11, len-11, NULL); if (!ret->data) { // failed, save as raw data ret->rawdata = malloc(len-11); memcpy(ret->rawdata, buf+11, len-11); ret->rawdatalen = len-11; } } return ret; }
/* Read a block from a file pointer. Returns 1 for success; 0 for failure. */ int block_read(struct block *b, FILE *fp) { unsigned char buf[SERIALIZED_BLOCK_LEN]; int n; n = fread(buf, sizeof(buf), 1, fp); if (n != 1) return 0; block_deserialize(b, buf); return 1; }