-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode.h
More file actions
68 lines (55 loc) · 1.69 KB
/
Copy pathbencode.h
File metadata and controls
68 lines (55 loc) · 1.69 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
/*
* C implementation of a bencode decoder.
* This is the format defined by BitTorrent:
* http://wiki.theory.org/BitTorrentSpecification#bencoding
*
* The only external requirements are a few [standard] function calls and
* the long long type. Any sane system should provide all of these things.
*
* This is released into the public domain.
* Written by Mike Frysinger <vapier@gmail.com>.
*
*
* Edited for CS43 @ Swarthmore College by Adam Aviv
*/
/* USAGE:
* - pass the string full of the bencoded data to be_decode()
* - parse the resulting tree however you like
* - call be_free() on the tree to release resources
*/
#ifndef _BENCODE_H
#define _BENCODE_H
/*enumerate for the different types of ben_node*/
typedef enum {
BE_STR,
BE_INT,
BE_LIST,
BE_DICT,
} be_type;
/*predefined for compiler checks*/
struct be_dict;
struct be_node;
/*
* XXX: the "val" field of be_dict and be_node can be confusing ...
*/
typedef struct be_dict {
char *key; //key of a dict
struct be_node *val; //val of a dict
} be_dict;
typedef struct be_node {
be_type type; //type of the node, e.g., a string or a list
union { //node can store all of these types
char *s; // a string
long long i; // a long long integer
struct be_node **l; //a pointer to an array of be_nodes representing a list
struct be_dict *d; //a dictionary
} val; //this union is stored in val
} be_node;
long long be_str_len(be_node *node);
be_node *be_decode(const char *bencode);
be_node *be_decoden(const char *bencode, long long bencode_len);
void be_free(be_node *node);
//dump out the be_node encoding starting from the top
void be_dump(be_node *node);
be_node * load_be_node(char * torrent_file);
#endif