-
Notifications
You must be signed in to change notification settings - Fork 12
/
bencode.h
140 lines (124 loc) · 2.7 KB
/
bencode.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
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
#ifndef BENCODE_H_
#define BENCODE_H_
typedef struct
{
const char *str;
const char *start;
void *parent;
int val;
int len;
} bencode_t;
/**
* Initialise a bencode object.
* @param be The bencode object
* @param str Buffer we expect input from
* @param len Length of buffer
*/
void bencode_init(
bencode_t * be,
const char *str,
int len
);
/**
* @return 1 if the bencode object is an int; otherwise 0.
*/
int bencode_is_int(
const bencode_t * be
);
/**
* @return 1 if the bencode object is a string; otherwise 0.
*/
int bencode_is_string(
const bencode_t * be
);
/**
* @return 1 if the bencode object is a list; otherwise 0.
*/
int bencode_is_list(
const bencode_t * be
);
/**
* @return 1 if the bencode object is a dict; otherwise 0.
*/
int bencode_is_dict(
const bencode_t * be
);
/**
* Obtain value from integer bencode object.
* @param val Long int we are writing the result to
* @return 1 on success, otherwise 0
*/
int bencode_int_value(
bencode_t * be,
long int *val
);
/**
* @return 1 if there is another item on this dict; otherwise 0.
*/
int bencode_dict_has_next(
bencode_t * be
);
/**
* Get the next item within this dictionary.
* @param be_item Next item.
* @param key Const pointer to key string of next item.
* @param klen Length of the key of next item.
* @return 1 on success; otherwise 0.
*/
int bencode_dict_get_next(
bencode_t * be,
bencode_t * be_item,
const char **key,
int *klen
);
/**
* Get the string value from this bencode object.
* The buffer returned is stored on the stack.
* @param be The bencode object.
* @param str Const pointer to the buffer.
* @param slen Length of the buffer we are outputting.
* @return 1 on success; otherwise 0
*/
int bencode_string_value(
bencode_t * be,
const char **str,
int *len
);
/**
* Tell if there is another item within this list.
* @param be The bencode object
* @return 1 if another item exists on the list; 0 otherwise; -1 on invalid processing
*/
int bencode_list_has_next(
bencode_t * be
);
/**
* Get the next item within this list.
* @param be The bencode object
* @param be_item The next bencode object that we are going to initiate.
* @return return 0 on end; 1 on have next; -1 on error
*/
int bencode_list_get_next(
bencode_t * be,
bencode_t * be_item
);
/**
* Copy bencode object into other bencode object
*/
void bencode_clone(
bencode_t * be,
bencode_t * output
);
/**
* Get the start and end position of this dictionary
* @param be Bencode object
* @param start Starting string
* @param len Length of the dictionary
* @return 0 on success
*/
int bencode_dict_get_start_and_len(
bencode_t * be,
const char **start,
int *len
);
#endif /* BENCODE_H_ */