-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.h
More file actions
132 lines (115 loc) · 3.45 KB
/
bencode.h
File metadata and controls
132 lines (115 loc) · 3.45 KB
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
#ifndef BENCODE_H
#define BENCODE_H
#include <stddef.h>
#include <openssl/sha.h>
#define INIT_CAP 32
#define FNV_OFF 2166136261UL // FNV1a 32 bit offset
#define FNV_PRIME 16777619UL // FNVI1a 32 bit prime
typedef enum
{
BE_INT,
BE_STR,
BE_LIST,
BE_DICT,
} be_type;
typedef struct be_node
{
be_type type;
unsigned char *key;
void *val;
} be_node;
typedef struct be_string
{
size_t len;
unsigned char *str;
} be_string;
typedef struct be_list
{
be_node node;
struct be_list *next;
// So that we can add elements easily and preserve order
struct be_list *last;
} be_list;
typedef struct be_dict
{
size_t capacity;
size_t length;
// Please check if this is set to 1 before doing anything with
// the actual hash
int has_info_hash;
// The info_hash (SHA1 hash of the info dictionary)
unsigned char info_hash[SHA_DIGEST_LENGTH];
struct be_node *entries;
} be_dict;
/**
* Decodes a bencoded file
* @param file The path of the bencoded file
* @return The outer dictionary or NULL if the file is not
* properly bencoded. The returned dicitonary should be freed
* with dict_destroy after use
* @see dict_destroy
*/
be_dict *decode_file (const char *file);
/**
* Decodes a bencoded buffer. Buffer should be `unsigned char *`
* @param buffer The pointer to the buffer
* @param len Address of the stored length of the buffer
* @param type Address of a be_type variable so that the type of the
* returned value can be stored in it
* @return The outer dictionary or NULL if the file is not
* properly bencoded. The returned dicitonary should be freed
* with dict_destroy after use
* @see dict_destroy
*/
void *decode (unsigned char **buffer, size_t *len, be_type *type);
/**
* Creates an empty dictionary. The dictionary needs to be freed by
* calling dict_destroy
* @return Created dictionary or NULL if an error occurs
*/
be_dict *dict_create (void);
/**
* Frees all dynamically allocated space in a given dictionary
* @param dict The dictionary to be freed
*/
void dict_destroy (be_dict *dict);
/**
* Returns the value of the node with key and stores the variable type
* into the passed type pointer
* @param dict The dictionary in which the key is to be searched
* @param key The key, of which, value is needed
* @param type The type of the returned value will be stored in this pointer
* @return The value of the given key or NULL if the key doesn't exist in
* the dictionary
*/
void *dict_get (be_dict *dict, unsigned char *key, be_type *type);
/**
* Inserts a key and a value into a given dictionary
* @param dict The dictionary in which key is to be inserted
* @param key The key to be inserted
* @param val The value to be inserted
* @param type The type of the value provided
* @return The key provided or NULL if an error occurs
*/
unsigned char *dict_set (be_dict *dict, unsigned char *key, void *val,
be_type type);
/**
* Prints a value from a bencode dictionary to STDIN
* @param key The key of the value to be printed
* @param val The value to be printed
* @param type The type of the value to be printed
*/
void dict_val_print (unsigned char *key, void *val, be_type type);
/**
* Prints the values in a dictionary to stdout
* @param dict The dictionary to be dumped
*/
void dict_dump (be_dict *dict);
/**
* Dumps a string in hex form. Used generally for printing the 'pieces'
* string and 'info_hash'
* @param str The string to be dumped
* @param len Length of the string
*/
void hex_dump(unsigned char *str, size_t len);
#endif /* BENCODE_H */