89 lines
2.9 KiB
Plaintext
89 lines
2.9 KiB
Plaintext
|
-: 0:Source:crashy.c
|
||
|
-: 0:Graph:crashy.gcno
|
||
|
-: 0:Data:crashy.gcda
|
||
|
-: 0:Runs:9
|
||
|
-: 0:Programs:1
|
||
|
-: 1:#include <stdio.h>
|
||
|
-: 2:#include <stdlib.h>
|
||
|
-: 3:#include <string.h>
|
||
|
-: 4:
|
||
|
9: 5:int main(int argc, char *argv[]) {
|
||
|
-: 6:
|
||
|
-: 7: FILE *fp;
|
||
|
-: 8: unsigned int sz, len, ptr;
|
||
|
-: 9: char *buffer;
|
||
|
-: 10: char type;
|
||
|
-: 11: char tmp_char;
|
||
|
-: 12: int tmp_int;
|
||
|
-: 13: char *tmp_string;
|
||
|
-: 14:
|
||
|
9: 15: if(argc != 2) {
|
||
|
1: 16: fprintf(stderr, "Usage: %s <inputfile>\n", argv[0]);
|
||
|
1: 17: return 1;
|
||
|
-: 18: }
|
||
|
-: 19:
|
||
|
8: 20: fp = fopen(argv[1], "r");
|
||
|
8: 21: if(!fp) {
|
||
|
1: 22: fprintf(stderr, "Failed to open '%s'\n", argv[1]);
|
||
|
1: 23: return 1;
|
||
|
-: 24: }
|
||
|
-: 25:
|
||
|
7: 26: fseek(fp, 0, SEEK_END);
|
||
|
7: 27: sz = ftell(fp);
|
||
|
7: 28: fseek(fp, 0, SEEK_SET);
|
||
|
-: 29:
|
||
|
7: 30: buffer = malloc(sz);
|
||
|
7: 31: if(!buffer) {
|
||
|
1: 32: fprintf(stderr, "Failed to allocate %d bytes of memory\n", sz);
|
||
|
1: 33: return 1;
|
||
|
-: 34: }
|
||
|
-: 35:
|
||
|
6: 36: fread(buffer, sizeof(char), sz, fp);
|
||
|
-: 37:
|
||
|
6: 38: ptr = 0;
|
||
|
31: 39: while(ptr < sz) {
|
||
|
-: 40:
|
||
|
25: 41: type = buffer[ptr++];
|
||
|
25: 42: switch(type) {
|
||
|
-: 43:
|
||
|
-: 44: case '\x01': // integer
|
||
|
8: 45: tmp_int = *(int *)(buffer + ptr);
|
||
|
8: 46: ptr += sizeof(int);
|
||
|
8: 47: printf("i: 0x%08x\n", tmp_int);
|
||
|
8: 48: break;
|
||
|
-: 49:
|
||
|
-: 50: case '\x02': // char
|
||
|
5: 51: tmp_char = buffer[ptr++];
|
||
|
5: 52: printf("c: 0x%02x\n", (unsigned char)tmp_char);
|
||
|
5: 53: break;
|
||
|
-: 54:
|
||
|
-: 55: case '\x03': // string
|
||
|
6: 56: len = *(int *)(buffer + ptr);
|
||
|
6: 57: ptr += sizeof(int);
|
||
|
6: 58: tmp_string = malloc(len);
|
||
|
6: 59: if(!tmp_string) {
|
||
|
1: 60: fprintf(stderr, "Failed to allocate %d bytes of memory\n", len);
|
||
|
1: 61: return 1;
|
||
|
-: 62: }
|
||
|
5: 63: strcpy(tmp_string, (buffer + ptr));
|
||
|
5: 64: printf("s: %s\n", tmp_string);
|
||
|
5: 65: ptr += len;
|
||
|
5: 66: break;
|
||
|
-: 67:
|
||
|
-: 68: case '\x04': // comment
|
||
|
1: 69: printf("#: %s\n", (buffer + ptr));
|
||
|
1: 70: while(*(buffer + ptr++) != '\x00');
|
||
|
1: 71: break;
|
||
|
-: 72:
|
||
|
-: 73: case '\xff': // END
|
||
|
5: 74: return 0;
|
||
|
-: 75:
|
||
|
-: 76: default:
|
||
|
#####: 77: fprintf(stderr, "Unknown data type '%c'\n", type);
|
||
|
-: 78:
|
||
|
-: 79: }
|
||
|
-: 80:
|
||
|
-: 81: }
|
||
|
-: 82:
|
||
|
#####: 83:}
|