1 /* torrent.h */
2 #ifndef TORRENT_H
3 #define TORRENT_H
4 #include "ustd.h"
5 #include "sha1.h"
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #define btih_hash_size  20
12 
13 /* vector structure */
14 typedef struct torrent_vect
15 {
16 	void** array;     /* array of elements of the vector */
17 	size_t size;      /* vector size */
18 	size_t allocated; /* number of allocated elements */
19 } torrent_vect;
20 
21 /* a binary string */
22 typedef struct torrent_str
23 {
24 	char* str;
25 	size_t length;
26 	size_t allocated;
27 } torrent_str;
28 
29 /* BitTorrent algorithm context */
30 typedef struct torrent_ctx
31 {
32 	unsigned char btih[20]; /* resulting BTIH hash sum */
33 	unsigned options;       /* algorithm options */
34 	sha1_ctx sha1_context;  /* context for hashing current file piece */
35 #if defined(USE_OPENSSL) || defined(OPENSSL_RUNTIME)
36 	unsigned long reserved; /* need more space for OpenSSL SHA1 context */
37 	void (*sha_init)(void*);
38 	void (*sha_update)(void*, const void*, size_t size);
39 	void (*sha_final)(void*, unsigned char*);
40 #endif
41 	size_t index;             /* byte index in the current piece */
42 	size_t piece_length;      /* length of a torrent file piece */
43 	size_t piece_count;       /* the number of pieces processed */
44 	torrent_vect hash_blocks; /* array of blocks storing SHA1 hashes */
45 	torrent_vect files;       /* names of files in a torrent batch */
46 	torrent_vect announce;    /* announce URLs */
47 	char* program_name;       /* the name of the program */
48 
49 	torrent_str content;      /* the content of generated torrent file */
50 	int error; /* non-zero if error occurred, zero otherwise */
51 } torrent_ctx;
52 
53 void bt_init(torrent_ctx* ctx);
54 void bt_update(torrent_ctx* ctx, const void* msg, size_t size);
55 void bt_final(torrent_ctx* ctx, unsigned char result[20]);
56 void bt_cleanup(torrent_ctx* ctx);
57 
58 unsigned char* bt_get_btih(torrent_ctx* ctx);
59 size_t bt_get_text(torrent_ctx* ctx, char** pstr);
60 
61 /* possible options */
62 #define BT_OPT_PRIVATE 1
63 #define BT_OPT_INFOHASH_ONLY 2
64 
65 void bt_set_options(torrent_ctx* ctx, unsigned options);
66 int  bt_add_file(torrent_ctx* ctx, const char* path, uint64_t filesize);
67 int  bt_add_announce(torrent_ctx* ctx, const char* announce_url);
68 int  bt_set_program_name(torrent_ctx* ctx, const char* name);
69 void bt_set_piece_length(torrent_ctx* ctx, size_t piece_length);
70 size_t bt_default_piece_length(uint64_t total_size);
71 
72 #ifdef __cplusplus
73 } /* extern "C" */
74 #endif /* __cplusplus */
75 
76 #endif /* TORRENT_H */
77