Skip to content
Newer
Older
100644 147 lines (117 sloc) 2.89 KB
cd1bf14 @robertseaton Now using syslog, most errors replaced with assert(), some comments a…
authored
1 #include <assert.h>
1d6ffd0 @robertseaton initial commit
authored
2 #include <ctype.h>
3 #include <stdlib.h>
4
5 #include "includes.h"
6
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
7 struct BEncode
8 *initBInt(int64_t i)
1d6ffd0 @robertseaton initial commit
authored
9 {
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
10 struct BEncode *b = malloc(sizeof(struct BEncode));
1d6ffd0 @robertseaton initial commit
authored
11 b->type = BInt;
12 b->cargo.bInt = i;
13
14 return b;
15 }
16
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
17 struct BEncode
18 *initBList(struct BListNode *l)
1d6ffd0 @robertseaton initial commit
authored
19 {
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
20 struct BEncode *b = malloc(sizeof(struct BEncode));
1d6ffd0 @robertseaton initial commit
authored
21 b->type = BList;
22 b->cargo.bList = l;
23
24 return b;
25 }
26
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
27 struct BEncode
28 *initBString(char *s)
1d6ffd0 @robertseaton initial commit
authored
29 {
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
30 struct BEncode *b = malloc(sizeof(struct BEncode));
1d6ffd0 @robertseaton initial commit
authored
31 b->type = BString;
32 b->cargo.bStr = s;
33
34 return b;
35 }
36
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
37 struct BEncode
38 *initBDict(struct BDictNode *d)
1d6ffd0 @robertseaton initial commit
authored
39 {
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
40 struct BEncode *b = malloc(sizeof(struct BEncode));
1d6ffd0 @robertseaton initial commit
authored
41 b->type = BDict;
42 b->cargo.bDict = d;
43
44 return b;
45 }
46
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
47 struct BEncode
48 *parseBInt(char *data, int64_t *position)
1d6ffd0 @robertseaton initial commit
authored
49 {
50 int64_t i = 0;
51
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
52 assert(data[*position] == 'i');
53 (*position)++;
59888a7 @robertseaton Parser now handles integers with leading 0's.
authored
54
55 /* discard leading zeroes */
56 while (data[*position] == '0')
57 (*position)++;
58
1d6ffd0 @robertseaton initial commit
authored
59 while (isdigit(data[*position]))
bd2f418 @robertseaton Initial support for UDP trackers.
authored
60 i = i * 10 + (data[(*position)++] - '0'); /* disgusting */
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
61 assert(data[*position] == 'e');
62 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
63
64 return initBInt(i);
65 }
66
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
67 struct BEncode
68 *parseBList(char *data, int64_t *position)
1d6ffd0 @robertseaton initial commit
authored
69 {
70 struct BListNode l;
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
71 struct BListNode *pt = &l;
1d6ffd0 @robertseaton initial commit
authored
72
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
73 assert(data[*position] == 'l');
74 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
75 while (data[*position] != 'e') {
76 pt->next = malloc(sizeof(struct BListNode));
77 pt = pt->next;
78 pt->cargo = parseBEncode(data, position);
79 pt->next = 0;
80 }
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
81 assert(data[*position] == 'e');
82 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
83
84 return initBList(l.next);
85 }
86
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
87 struct BEncode
88 *parseBString(char *data, int64_t *position)
1d6ffd0 @robertseaton initial commit
authored
89 {
90 int64_t l = 0;
91
92 while (isdigit(data[*position]))
93 l = l * 10 + (data[(*position)++] - '0');
94
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
95 assert(data[*position] == ':');
96 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
97
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
98 char *s = malloc(l + 1);
1d6ffd0 @robertseaton initial commit
authored
99
100 int i;
101 for (i = 0; i < l; i++)
102 s[i] = data[(*position)++];
103
104 s[l] = 0;
105
106 return initBString(s);
107 }
108
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
109 struct BEncode
110 *parseBDict(char *data, int64_t *position)
1d6ffd0 @robertseaton initial commit
authored
111 {
112 struct BDictNode d;
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
113 struct BDictNode *pt = &d;
1d6ffd0 @robertseaton initial commit
authored
114
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
115 assert(data[*position] == 'd');
116 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
117 while (data[*position] != 'e') {
118 pt->next = malloc(sizeof(struct BDictNode));
119 pt = pt->next;
120 struct BEncode* s = parseBString(data, position);
121 pt->key = s->cargo.bStr;
122 pt->value = parseBEncode(data, position);
123 pt->next = 0;
124
125 free(s);
126 }
ff8324b @robertseaton Builds without asserts enabled no longer break the parser.
authored
127 assert(data[*position] == 'e');
128 (*position)++;
1d6ffd0 @robertseaton initial commit
authored
129
130 return initBDict(d.next);
131 }
132
be5e323 @robertseaton Began moving codebase towards compliance with OpenBSD style guidelines.
authored
133 struct BEncode
134 *parseBEncode(char *data, int64_t *position)
1d6ffd0 @robertseaton initial commit
authored
135 {
136 switch (data[*position]) {
137 case 'd':
138 return parseBDict(data, position);
139 case 'l':
140 return parseBList(data, position);
141 case 'i':
142 return parseBInt(data, position);
143 default:
144 return parseBString(data, position);
145 }
146 }
Something went wrong with that request. Please try again.