-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
170 lines (156 loc) · 3.53 KB
/
parser.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* parser.c
*
* Created on: Jul 1, 2012
* Author: pranav
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "parser.h"
unsigned char* read_from_file(char* file) {
char *bytes;
long int pos;
FILE *f = fopen(file, "r");
fseek(f, 0, SEEK_END);
pos = ftell(f);
fseek(f, 0, SEEK_SET);
bytes = malloc(pos);
fread(bytes, pos*sizeof(unsigned char), sizeof(unsigned char), f);
fclose(f);
return bytes;
}
long bencode_parse_len(unsigned char* bytes) {
long i = 0;
while(bytes[i] != 'e')
i++;
return i-1;
}
long bencode_parse_number(unsigned char* bytes) {
int i = 0;
long n;
if(isdigit(bytes[i])) {
n = atoll(&bytes[i]);
}
return n;
}
bencode_dict* bencode_parse_dict(unsigned char* bytes) {
int i = 0;
long len, keylen, dictlen = 0;
bencode_dict *start, *it;
start = NULL;
while(*bytes!='e') {
len = 0;
it = (bencode_dict *)malloc(sizeof(bencode_dict));
memset(it, 0, sizeof(it));
keylen = bencode_parse_number(bytes);
while(*(bytes++)!=':') len++; //Counts number of digits also
it->key = (unsigned char *)malloc(keylen);
memcpy(it->key, bytes, keylen);
bytes+=keylen;
it->value = (bencode_node *)bencode_parse(bytes);
dictlen += it->value->len + keylen + 1 + len;
bytes += it->value->len;
it->next = start;
start = it;
}
start->len = dictlen;
return start;
}
bencode_node* bencode_parse_list(unsigned char* bytes) {
int i = 0;
long len, listlen = 0;
bencode_node *start, *it;
start = NULL;
while(*bytes!='e') {
it = (bencode_node*)bencode_parse(bytes);
listlen += it->len;
bytes += it->len;
it->next = start;
start = it;
}
start->len = listlen;
return start;
}
long long bencode_parse_integer(unsigned char* bytes) {
int i = 0;
long long n = 0;
if(isdigit(bytes[i])) {
n = atoll(&bytes[i]);
}
return n;
}
bencode_string* bencode_parse_string(unsigned char* bytes) {
unsigned char *string;
bencode_string *tmp = malloc(sizeof(bencode_string));
long long len = bencode_parse_number(bytes);
while(*(bytes++) != ':');
string = (unsigned char *)malloc(len+1);
bzero(string, len+1);
memcpy(string, bytes, len);
tmp->len = len;
tmp->s = string;
return tmp;
}
struct bencode_node* bencode_parse(unsigned char* bytes) {
bencode_node *n;
long pos, i, j, len;
switch(*bytes) {
case 'd': {
n = (bencode_node *)malloc(sizeof(bencode_node));
memset(n, 0, sizeof(n));
n->type = DICTIONARY;
n->content.d = bencode_parse_dict(++bytes);
len = n->content.d->len;
n->len = len+2;
bytes += n->len;
return n;
}
case 'l': {
n = (bencode_node *)malloc(sizeof(bencode_node));
memset(n, 0, sizeof(n));
n->type = LIST;
n->content.l = bencode_parse_list(++bytes);
len = n->content.l->len;
n->len = len+2;
bytes += n->len;
return n;
}
case 'i': {
n = (bencode_node *)malloc(sizeof(bencode_node));
memset(n, 0, sizeof(n));
n->type = INTEGER;
n->content.i = bencode_parse_integer(++bytes);
n->len = 2 + (n->content.i == 0 ? 1 : (int)(log10(n->content.i)+1));
bytes += n->len;
return n;
}
case '0'...'9': {
n = (bencode_node *)malloc(sizeof(bencode_node));
bencode_string *tmp;
memset(n, 0, sizeof(n));
n->type = STRING;
tmp = bencode_parse_string(bytes);
n->content.s = tmp->s;
len = tmp->len;
n->len = len + 1 + (len == 0 ? 1 : (int)(log10(len)+1));
bytes += n->len;
return n;
}
case 'e': {
return NULL;
}
}
}
struct bencode_dict* get_dict_node_by_key(bencode_dict* d, char* key) {
int cmp;
while(d!=NULL) {
if(!strcmp(d->key, key))
return d;
else
d = d->next;
}
return d;
}