-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBencode.c
More file actions
42 lines (36 loc) · 891 Bytes
/
Bencode.c
File metadata and controls
42 lines (36 loc) · 891 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "Bencode.h"
char* get_substring(const char* str, int beg, int n) {
assert(beg - 1+n < strlen(str));
char* ret = malloc(n + 1);
strncpy(ret, (str + beg), n);
*(ret+n) = 0;
return ret;
}
char* parse_bencode_string(char* be_str) {
int i;
int len_str;
sscanf(be_str, "%i", &len_str);
for (i = 0; i < strlen(be_str); i++) {
if (be_str[i - 1] == ':') {
break;
}
}
char* ret_str = get_substring(be_str, i, len_str);
strncpy(ret_str, &be_str[i], len_str);
return ret_str;
}
char* parse_bencode_integer(char* be_str) {
return 0;
}
int main() {
char* test = "4:spam";
/*const char* res = parse_bencode_string(test);
puts(res);*/
printf("%s\n", parse_bencode_string(test));
return 0;
}