-
Notifications
You must be signed in to change notification settings - Fork 1
/
bdecode.h
72 lines (62 loc) · 1.37 KB
/
bdecode.h
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
#ifndef BDECODE_H_
#define BDECODE_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* * * * * * * * * * * * *
* BENCODING DATA TYPES *
* * * * * * * * * * * * */
enum bd_type
{
NUMBER,
STRING,
DICTIONARY,
LIST,
};
struct bd_dict;
struct bd_list;
typedef struct
{
enum bd_type type;
union
{
void* data;
char* str;
struct bd_dict* dict;
struct bd_list* list;
};
} bd_entry;
typedef struct bd_dict
{
char* key;
enum bd_type type;
union
{
void* data;
char* str;
struct bd_dict* dict;
struct bd_list* list;
};
struct bd_dict* next;
} bd_dict;
typedef struct bd_list
{
int used;
int allocated;
bd_entry* entries;
} bd_list;
bd_list* bd_list_create();
bd_dict* bd_dict_create();
void bd_dict_add(bd_dict** dict, char* key, enum bd_type type, void* data);
void bd_list_add(bd_list* list, enum bd_type type, void* data);
void bd_list_destroy(bd_list* list);
void bd_dict_destroy(bd_dict* dict);
/* * * * * * * * * * * * * * * *
* BENCODE DECODING FUNCT *
* * * * * * * * * * * * * * * */
void* decode(unsigned char* buf, size_t size);
bd_dict* decode_dictionary(unsigned char* buf, int* index, size_t size);
bd_list* decode_list(unsigned char* buf, int* index, size_t size);
long long decode_number(unsigned char* buf, int* index, size_t size);
char* decode_string(unsigned char* buf, int* index, size_t size);
#endif