Created
July 18, 2016 00:07
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#ifndef __AFL_LOOP | |
#define __AFL_LOOP(n) ({ static int __i; !(__i++); }) | |
#endif | |
static unsigned char lava_val[4]; | |
#pragma pack(1) | |
#define MAGIC 0x4c415641 | |
enum { | |
TYPEA = 1, | |
TYPEB = 2 | |
}; | |
typedef struct { | |
uint32_t magic; // Magic value | |
uint32_t reserved; // Reserved for future use | |
uint16_t num_recs; // How many entries? | |
uint16_t flags; // None used yet | |
uint32_t timestamp; // Unix Time | |
} file_header; | |
typedef struct { | |
char bar[16]; | |
uint32_t type; | |
union { | |
float fdata; | |
uint32_t intdata; | |
} data; | |
} file_entry; | |
void parse_header(FILE *f, file_header *hdr) { | |
if (1 != fread(hdr, sizeof(file_header), 1, f)) | |
exit(1); | |
if (hdr->magic != MAGIC) | |
exit(1); | |
} | |
file_entry * parse_record(FILE *f) { | |
file_entry *ret = (file_entry *) malloc(sizeof(file_entry)); | |
if (1 != fread(ret, sizeof(file_entry), 1, f)) | |
exit(1); | |
return ret; | |
} | |
void consume_record(file_entry *ent) { | |
printf("Entry: bar = %s, ", ent->bar); | |
if (ent->type == TYPEA) { | |
if (ent) { | |
*(unsigned int *)lava_val = *(unsigned int *)ent; | |
} | |
if (lava_val[0] == 0x6c) { | |
if(lava_val[1] == 0x61) | |
if(lava_val[2] == 0x75) | |
if(lava_val[3] == 0xde) | |
printf("fdata = %f\n"+*(unsigned int *)lava_val, ent->data.fdata); | |
} | |
else if (lava_val[3] == 0x6c) { | |
if(lava_val[2] == 0x61) | |
if(lava_val[1] == 0x75) | |
if(lava_val[0] == 0xde) | |
printf("fdata = %f\n"+*(unsigned int *)lava_val, ent->data.fdata); | |
} | |
else | |
printf("fdata = %f\n", ent->data.fdata); | |
} | |
else if (ent->type == TYPEB) { | |
printf("intdata = %u\n", ent->data.intdata); | |
} | |
else { | |
printf("Unknown type %x\n", ent->type); | |
exit(1); | |
} | |
free(ent); | |
} | |
int main(int argc, char **argv) { | |
while (__AFL_LOOP(1000)) { | |
FILE *f = fdopen(0, "rb"); | |
file_header head; | |
parse_header(f, &head); | |
printf("File timestamp: %u\n", head.timestamp); | |
unsigned i; | |
for (i = 0; i < head.num_recs; i++) { | |
file_entry *ent = parse_record(f); | |
consume_record(ent); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment