14
void benc_log_exception(const char *file, int line, const char *func, const char *msg, ...);
15
void benc_log_syscall(const char *file, int line, const char *func, const char *syscall_name, const char *msg, ...);
17
#define BENC_LOG_EXCEPTION(...) (benc_log_exception(__FILE__, __LINE__, __func__, __VA_ARGS__))
18
#define BENC_LOG_SYSCALL_ERROR(...) (benc_log_exception(__FILE__, __LINE__, __func__, __VA_ARGS__))
20
bobj_t * bobj_new(enum benc_data_type type);
21
size_t bobj_repsize(bobj_t *o);
22
void bobj_encode(bbuf_t *b, bobj_t *o);
24
bool bbuf_inc_ptr(bbuf_t *b);
26
size_t benc_int_repsize(benc_int_t i);
27
void benc_int_encode(bbuf_t *b, benc_int_t i);
28
bool benc_int_decode(bbuf_t *b, benc_int_t *i_p);
30
benc_bstr_t * benc_bstr_new(size_t len, char *bytes);
31
void benc_bstr_free(benc_bstr_t *s);
32
size_t benc_bstr_repsize(benc_bstr_t *s);
33
void benc_bstr_encode(bbuf_t *b, benc_bstr_t *s);
34
bool benc_bstr_decode(bbuf_t *b, benc_bstr_t **s_p);
35
int benc_bstr_compare(benc_bstr_t *a, benc_bstr_t *b);
38
* Print a bobject of unknown type in human readable format.
40
* @param writer the Writer to write to.
41
* @param padSpaceCount the number of spaces to pad if multiple lines are needed.
42
* @param obj the object to print.
43
* @return whatever the Writer returns when writing or -2
44
* if the type of object cannot be determined.
46
int benc_bobj_print(struct Writer* writer,
51
* Print a string in human readable format.
52
* Unprintable characters are escaped.
54
* @param writer the Writer to write to.
55
* @param string the string to write.
56
* @return whatever the Writer returns when writing.
58
int benc_bstr_print(struct Writer* writer,
62
* Serialize a string and write to a writer.
64
* @param writer the Writer to write to.
65
* @param string the string to write.
66
* @return whatever the Writer returns when writing.
68
int benc_bstr_serialize(struct Writer* writer,
72
* Parse a string, reading in with the first callback and writing to the second.
74
* @param reader a Reader which will be asked for the data to parse. This implementation
75
* assumes the reader's pointer is alligned on the first digit of the length
76
* of the string and will leave the read pointer on the first character AFTER
77
* the end of the string.
78
* @param writer a MemAllocator which will be used to store data.
79
* @param stringPointer a pointer which will be set to the benc_bstr_t struct.
80
* @return 0 if everything goes well, -1 if NULL returned by the writer indicating an array
81
* overflow, -2 if -1 returned by the reader indicating an array underflow,
82
* -3 if content unparsable.
84
int benc_bstr_parse(struct Reader* reader,
85
struct MemAllocator* allocator,
86
benc_bstr_t** stringPointer);
89
* Write an integer as decimal in human readable format.
91
* @param writer the Writer to write to.
92
* @param integer the number to write.
93
* @return whatever the Writer returns when writing.
95
int benc_int_print(struct Writer* writer,
99
* Write an integer as decimal in bencoded format.
100
* the integer 10 would be written as "i10e"
102
* @param writer the Writer to write to.
103
* @param integer the number to write.
104
* @return whatever the Writer returns when writing.
106
int benc_int_serialize(struct Writer* writer,
110
* Parse an integer, read in with the reader and set the intPointer to the value of the int.
112
* @param reader a Reader which will be asked for the data to parse. This implementation
113
* assumes the reader's pointer is alligned on the 'i' which begins the integer
114
* and will leave the read pointer on the first character AFTER the 'e' which
116
* @param intPointer a pointer to a memory location which will be set to the valie of the int.
117
* @return 0 if everything goes well, -2 if -1 returned by the reader indicating an
118
* array underflow, -3 if content unparsable.
120
int benc_int_parse(struct Reader* reader,
121
benc_int_t* intPointer);
124
* Write a list in human readable format.
126
* @param writer the Writer to write to.
127
* @param padSpaceCount the number of spaces to put in front of the list.
128
* @param head the top entry of the list.
129
* @return whatever the Writer returns when writing.
131
int benc_list_print(struct Writer* writer,
132
size_t padSpaceCount,
133
benc_list_entry_t* head);
138
* @param writer the Writer to write to.
139
* @param head the top entry of the list.
140
* @return whatever the Writer returns when writing.
142
int benc_list_serialize(struct Writer* writer,
143
benc_list_entry_t* head);
148
* @param reader a Reader which will be asked for the data to parse. This implementation
149
* assumes the reader's pointer is alligned on the 'l' which signifies the
150
* beginning of the list. This will leave the pointer on the first character
151
* AFTER the 'e' which signifies the end of the list.
152
* @param writer a MemAllocator which will be used to store data.
153
* @param listPointer a pointer which will be set to the location of the benc_list_t.
154
* @return 0 if everything goes well, -1 if NULL returned by the writer indicating an array
155
* overflow, -2 if -1 returned by the reader indicating an array underflow,
156
* -3 if content unparsable.
158
int benc_list_parse(struct Reader* reader,
159
struct MemAllocator* writer,
160
benc_list_entry_t** listPointer);
163
* Write a dictionary in human readable format.
165
* @param writer the Writer to write to.
166
* @param padSpaceCount the number of spaces to put in front of the dictionary.
167
* @param head the top entry of the dictionary.
168
* @return whatever the Writer returns when writing.
170
int benc_dict_print(struct Writer* writer,
171
size_t padSpaceCount,
172
benc_dict_entry_t* head);
175
* Serialize a dictionary.
177
* @param writer the Writer to write to.
178
* @param integer the number to write.
179
* @return whatever the Writer returns when writing.
181
int benc_dict_serialize(struct Writer* writer,
182
benc_dict_entry_t* head);
185
* Parse a dictionary, reading in with the first callback and writing to the second.
187
* @param reader a Reader which will be asked for the data to parse. This implementation
188
* assumes the reader's pointer is alligned on the 'd' which indicates
189
* dictionary and will leave the read pointer on the first character AFTER
190
* the 'e' which ends the dictionary.
191
* @param allocator a MemAllocator which will be used to store data.
192
* @param headPointer a pointer which will be set to the head of the dictionary.
193
* @return 0 if everything goes well, -1 if NULL returned by write() indicating an array
194
* overflow, -2 if -1 returned by read indicating an array underflow,
195
* -3 if content unparsable.
197
int benc_dict_parse(struct Reader* reader,
198
struct MemAllocator* allocator,
199
benc_dict_entry_t** headPointer);
201
void benc_list_free(benc_list_entry_t *head);
202
size_t benc_list_repsize(benc_list_entry_t *head);
203
void benc_list_encode(bbuf_t *b, benc_list_entry_t *head);
204
bool benc_list_decode(bbuf_t *b, benc_list_entry_t **head_p);
206
void benc_dict_free(benc_dict_entry_t *head);
207
size_t benc_dict_repsize(benc_dict_entry_t *head);
208
void benc_dict_encode(bbuf_t *b, benc_dict_entry_t *head);
209
bool benc_dict_decode(bbuf_t *b, benc_dict_entry_t **head_p);
211
#endif /* #ifndef BENCODE_H */