-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommand.c
93 lines (75 loc) · 2.04 KB
/
command.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <windows.h>
#include <malloc.h>
#include "kaitai.h"
#include "parsers.h"
//
// This file is part of Kiewtai, a port of Kaitai to the Hiew hex editor.
//
// This is just a test wrapper command to verify parsing is working.
//
// Tavis Ormandy <[email protected]>
//
VOID CALLBACK FieldNotify(PVOID UserPtr, LPCSTR Name, ULONG Start, ULONG End)
{
printf("Field: %s Start: %lu End: %lu\n", Name, Start, End);
return;
}
VOID CALLBACK ErrorNotify(PVOID UserPtr, LPCSTR Message)
{
printf("Error: %s\n", Message);
return;
}
int main(int argc, char *argv[]) {
char *buffer = NULL;
FILE *input = NULL;
size_t len;
if (argc != 3) {
fprintf(stderr, "usage: %s [ParserName] [file]\n", argv[0]);
goto error;
}
input = fopen(argv[2], "rb");
if (input == NULL) {
fprintf(stderr, "could not open file %s\n", argv[2]);
goto error;
}
fseek(input, 0, SEEK_END);
len = ftell(input);
rewind(input);
buffer = malloc(len);
if (buffer == NULL) {
fprintf(stderr, "memory allocation failed\n");
goto error;
}
if (fread(buffer, 1, len, input) != len) {
fprintf(stderr, "failed to read file\n");
goto error;
}
for (PKAITAI_PARSER Parser = &KaitaiParsers[0]; Parser->Name; Parser++) {
if (strcmp(Parser->Name, argv[1]) == 0) {
if (KaitaiQueryFormat(Parser,
KAITAI_FLAG_EXPANDARRAYS,
buffer,
len,
FieldNotify,
ErrorNotify,
NULL) == FALSE) {
fprintf(stderr, "Parse Failure\n");
}
goto success;
}
}
fprintf(stderr, "could not find matching parser\n");
error:
if (buffer)
free(buffer);
if (input)
fclose(input);
return 1;
success:
free(buffer);
fclose(input);
return 0;
}