-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
120 lines (112 loc) · 2.9 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "protobuf.h"
/**
* opens a file to give a list of fields
*/
size_t os_utils_file_size(const char* path) {
size_t file_size = 0;
// open file
FILE* in_file = fopen(path, "r");
if (in_file == NULL)
return 0;
// determine size
fseek(in_file, 0L, SEEK_END);
file_size = ftell(in_file);
fclose(in_file);
return file_size;
}
char* getWireType(enum WireType type) {
switch(type) {
case(WIRETYPE_VARINT):
return "Varint";
case (WIRETYPE_LENGTH_DELIMITED):
return "Length Delimited";
case (WIRETYPE_64BIT):
return "64 Bit";
case (WIRETYPE_START_GROUP):
return "Start Group";
case (WIRETYPE_END_GROUP):
return "End Group";
case (WIRETYPE_32BIT):
return "32 Bit";
}
return "Wire Type Invalid";
}
void stripit(int argc, char** argv) {
char tmp[strlen(argv[argc])];
strcpy(tmp, &argv[argc][1]);
tmp[strlen(tmp)-1] = 0;
strcpy(argv[argc], tmp);
return;
}
void strip_quotes(int argc, char** argv) {
for(int i = 0; i < argc; i++) {
if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') {
stripit(i, argv);
}
}
}
int main(int argc, char** argv) {
strip_quotes(argc, argv);
if (argc <= 1) {
printf("Syntax: %s [filename] [start_pos]\n", argv[0]);
return 0;
}
char* fileName = argv[1];
size_t numBytes = os_utils_file_size(fileName);
printf("Number of bytes in file: %lu\n", numBytes);
if (numBytes == 0)
return 0;
FILE* file = fopen(fileName, "rb");
unsigned char buffer[numBytes];
if (fread(buffer, 1, numBytes, file) != numBytes) {
printf("Unexpected number of bytes.\n");
return 0;
}
//skip ahead
size_t skip = 0;
if (argc > 2) {
skip = atoi(argv[2]);
if (skip > 0) {
printf("Skipping first %lu bytes\n", skip);
}
}
int fieldCounter = 0;
size_t pos = skip;
while(pos < numBytes) {
size_t bytes_read = 0;
int field_no = 0;
enum WireType wire_type = WIRETYPE_64BIT;
if (protobuf_decode_field_and_type(&buffer[pos], numBytes - pos, &field_no, &wire_type, &bytes_read) == 0) {
printf("Unexpected return value from protobuf_decode_field_and_type at pos %lu\n", pos);
return 0;
}
printf("Field counter: %d at position %lu, Field Number: %d, Wire Type: %s", fieldCounter, pos, field_no, getWireType(wire_type));
pos += bytes_read;
// read the value
switch(wire_type) {
case(WIRETYPE_VARINT): {
unsigned long long varint = 0;
protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
pos += bytes_read;
printf(" Value: %llu. Next read position at %lu\n", varint, pos);
break;
}
case (WIRETYPE_LENGTH_DELIMITED): {
unsigned long long varint = 0;
protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
pos += bytes_read;
printf(" Field width: %llu. Next read position at %lu\n", varint, pos);
pos += varint;
break;
}
default:
pos++;
}
fieldCounter++;
}
return 0;
}