~timkross/cjdns/cjdns

« back to all changes in this revision

Viewing changes to libbenc/bencode.h

  • Committer: cjdelisle
  • Date: 2011-02-16 23:03:00 UTC
  • Revision ID: git-v1:d475c9c10adc25590bea5e7dc5383b32f66d5eb8
First commit for cjdns.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef BENCODE_H
 
2
#define BENCODE_H
 
3
 
 
4
#include <stdbool.h>
 
5
#include <stdint.h>
 
6
#include <stdlib.h>
 
7
#include <stdio.h>
 
8
#include <string.h>
 
9
#include <errno.h>
 
10
#include <stdarg.h>
 
11
 
 
12
#include "benc.h"
 
13
 
 
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, ...);
 
16
 
 
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__))
 
19
 
 
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);
 
23
 
 
24
bool bbuf_inc_ptr(bbuf_t *b);
 
25
 
 
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);
 
29
 
 
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);
 
36
 
 
37
/**
 
38
 * Print a bobject of unknown type in human readable format.
 
39
 *
 
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.
 
45
 */
 
46
int benc_bobj_print(struct Writer* writer,
 
47
                    size_t padSpaceCount,
 
48
                    bobj_t* obj);
 
49
 
 
50
/**
 
51
 * Print a string in human readable format.
 
52
 * Unprintable characters are escaped.
 
53
 *
 
54
 * @param writer the Writer to write to.
 
55
 * @param string the string to write.
 
56
 * @return whatever the Writer returns when writing.
 
57
 */
 
58
int benc_bstr_print(struct Writer* writer,
 
59
                    benc_bstr_t* string);
 
60
 
 
61
/**
 
62
 * Serialize a string and write to a writer.
 
63
 *
 
64
 * @param writer the Writer to write to.
 
65
 * @param string the string to write.
 
66
 * @return whatever the Writer returns when writing.
 
67
 */
 
68
int benc_bstr_serialize(struct Writer* writer,
 
69
                        benc_bstr_t* string);
 
70
 
 
71
/**
 
72
 * Parse a string, reading in with the first callback and writing to the second.
 
73
 *
 
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.
 
83
 */
 
84
int benc_bstr_parse(struct Reader* reader,
 
85
                    struct MemAllocator* allocator,
 
86
                    benc_bstr_t** stringPointer);
 
87
 
 
88
/**
 
89
 * Write an integer as decimal in human readable format.
 
90
 *
 
91
 * @param writer the Writer to write to.
 
92
 * @param integer the number to write.
 
93
 * @return whatever the Writer returns when writing.
 
94
 */
 
95
int benc_int_print(struct Writer* writer,
 
96
                           benc_int_t integer);
 
97
 
 
98
/**
 
99
 * Write an integer as decimal in bencoded format.
 
100
 * the integer 10 would be written as "i10e"
 
101
 *
 
102
 * @param writer the Writer to write to.
 
103
 * @param integer the number to write.
 
104
 * @return whatever the Writer returns when writing.
 
105
 */
 
106
int benc_int_serialize(struct Writer* writer,
 
107
                       benc_int_t integer);
 
108
 
 
109
/**
 
110
 * Parse an integer, read in with the reader and set the intPointer to the value of the int.
 
111
 *
 
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
 
115
 *               ends the integer.
 
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.
 
119
 */
 
120
int benc_int_parse(struct Reader* reader,
 
121
                   benc_int_t* intPointer);
 
122
 
 
123
/**
 
124
 * Write a list in human readable format.
 
125
 *
 
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.
 
130
 */
 
131
int benc_list_print(struct Writer* writer,
 
132
                    size_t padSpaceCount,
 
133
                    benc_list_entry_t* head);
 
134
 
 
135
/**
 
136
 * Serialize a list.
 
137
 *
 
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.
 
141
 */
 
142
int benc_list_serialize(struct Writer* writer,
 
143
                        benc_list_entry_t* head);
 
144
 
 
145
/**
 
146
 * Parse a list.
 
147
 *
 
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.
 
157
 */
 
158
int benc_list_parse(struct Reader* reader,
 
159
                    struct MemAllocator* writer,
 
160
                    benc_list_entry_t** listPointer);
 
161
 
 
162
/**
 
163
 * Write a dictionary in human readable format.
 
164
 *
 
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.
 
169
 */
 
170
int benc_dict_print(struct Writer* writer,
 
171
                    size_t padSpaceCount,
 
172
                    benc_dict_entry_t* head);
 
173
 
 
174
/**
 
175
 * Serialize a dictionary.
 
176
 *
 
177
 * @param writer the Writer to write to.
 
178
 * @param integer the number to write.
 
179
 * @return whatever the Writer returns when writing.
 
180
 */
 
181
int benc_dict_serialize(struct Writer* writer,
 
182
                        benc_dict_entry_t* head);
 
183
 
 
184
/**
 
185
 * Parse a dictionary, reading in with the first callback and writing to the second.
 
186
 *
 
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.
 
196
 */
 
197
int benc_dict_parse(struct Reader* reader,
 
198
                    struct MemAllocator* allocator,
 
199
                    benc_dict_entry_t** headPointer);
 
200
 
 
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);
 
205
 
 
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);
 
210
 
 
211
#endif        /* #ifndef BENCODE_H */