1 /* rhash_torrent.c - functions to make a torrent file.
2  *
3  * Copyright (c) 2013, Aleksey Kravchenko <rhash.admin@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE  INCLUDING ALL IMPLIED WARRANTIES OF  MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT,  OR CONSEQUENTIAL DAMAGES  OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION OF CONTRACT,  NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION,  ARISING OUT OF  OR IN CONNECTION  WITH THE USE  OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* modifier for Windows DLL */
18 #if (defined(_WIN32) || defined(__CYGWIN__) ) && defined(RHASH_EXPORTS)
19 # define RHASH_API __declspec(dllexport)
20 #endif
21 
22 #include "rhash_torrent.h"
23 #include "algorithms.h"
24 #include "torrent.h"
25 #include <assert.h>
26 
27 /* obtain torrent context from rhash context */
28 #define BT_CTX(rctx) ((torrent_ctx*)(((rhash_context_ext*)rctx)->bt_ctx))
29 
rhash_torrent_add_file(rhash ctx,const char * filepath,unsigned long long filesize)30 RHASH_API int rhash_torrent_add_file(rhash ctx, const char* filepath, unsigned long long filesize)
31 {
32 	if (!BT_CTX(ctx)) return 0;
33 	return bt_add_file(BT_CTX(ctx), filepath, filesize);
34 }
35 
rhash_torrent_set_options(rhash ctx,unsigned options)36 RHASH_API void rhash_torrent_set_options(rhash ctx, unsigned options)
37 {
38 	if (!BT_CTX(ctx)) return;
39 	bt_set_options(BT_CTX(ctx), options);
40 }
41 
rhash_torrent_add_announce(rhash ctx,const char * announce_url)42 RHASH_API int rhash_torrent_add_announce(rhash ctx, const char* announce_url)
43 {
44 	if (!BT_CTX(ctx)) return 0;
45 	return bt_add_announce(BT_CTX(ctx), announce_url);
46 }
47 
rhash_torrent_set_program_name(rhash ctx,const char * name)48 RHASH_API int rhash_torrent_set_program_name(rhash ctx, const char* name)
49 {
50 	if (!BT_CTX(ctx)) return 0;
51 	return bt_set_program_name(BT_CTX(ctx), name);
52 }
53 
rhash_torrent_set_piece_length(rhash ctx,size_t piece_length)54 RHASH_API void rhash_torrent_set_piece_length(rhash ctx, size_t piece_length)
55 {
56 	if (!BT_CTX(ctx)) return;
57 	bt_set_piece_length(BT_CTX(ctx), piece_length);
58 }
59 
rhash_torrent_get_default_piece_length(unsigned long long total_size)60 RHASH_API size_t rhash_torrent_get_default_piece_length(unsigned long long total_size)
61 {
62 	return bt_default_piece_length(total_size);
63 }
64 
rhash_torrent_generate_content(rhash ctx)65 RHASH_API const rhash_str* rhash_torrent_generate_content(rhash ctx)
66 {
67 	torrent_ctx* tc = BT_CTX(ctx);
68 	if (!tc || tc->error || !tc->content.str) return 0;
69 	return (rhash_str*)(&tc->content);
70 }
71