26
25
#include "calc_sums.h"
29
* Calculate hash sums simultaneously, according to info->sums.flags.
28
* Initialize BTIH hash function. Unlike other algorithms BTIH
29
* requires more data for correct computation.
31
* @param fdin file descriptor to read from
32
* @param info the file data to store calculated hashes to
33
* @return 0 on success. On error -1 is returned and errno is set
31
* @param info the file data
35
static int calc_sums_fd(FILE* fd, struct file_info *info)
33
static void init_btih_data(struct file_info *info)
37
assert(info->rctx == 0);
38
info->rctx = rhash_init(info->sums.flags);
40
/* initialize bittorrent data */
41
if(info->sums.flags & RHASH_BTIH) {
42
torrent_ctx *bt_ctx = (torrent_ctx*)rhash_get_context_ptr(info->rctx, RHASH_BTIH);
43
torrent_add_file(bt_ctx, get_basename(file_info_get_utf8_print_path(info)), info->size);
44
torrent_set_program_name(bt_ctx, PROGRAM_NAME "/" VERSION);
46
if(opt.flags & OPT_BT_PRIVATE) {
47
torrent_set_options(bt_ctx, BT_OPT_PRIVATE);
50
torrent_set_announce(bt_ctx, opt.bt_announce);
52
if(opt.bt_piece_length) {
53
torrent_set_piece_length(bt_ctx, opt.bt_piece_length);
57
if(percents_output->update != 0) {
58
rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
61
/* read and hash file content */
62
if(rhash_file_update(info->rctx, fd) == -1) return -1;
64
rhash_final(info->rctx, 0); /* finalize hashing */
66
info->size = info->rctx->msg_size;
67
rhash_data.total_size += info->size;
68
/*if(info->infohash) infohash_add_file(info->infohash, info->full_path, filesize);*/
35
assert((info->rctx->hash_id & RHASH_BTIH) != 0);
36
rhash_transmit(RMSG_BT_ADD_FILE, info->rctx, RHASH_STR2UPTR((char*)get_basename(file_info_get_utf8_print_path(info))), (rhash_uptr_t)&info->size);
37
rhash_transmit(RMSG_BT_SET_PROGRAM_NAME, info->rctx, RHASH_STR2UPTR(PROGRAM_NAME "/" VERSION), 0);
39
if(opt.flags & OPT_BT_PRIVATE) {
40
rhash_transmit(RMSG_BT_SET_OPTIONS, info->rctx, RHASH_BT_OPT_PRIVATE, 0);
44
rhash_transmit(RMSG_BT_SET_ANNOUNCE, info->rctx, RHASH_STR2UPTR(opt.bt_announce), 0);
47
if(opt.bt_piece_length) {
48
rhash_transmit(RMSG_BT_SET_PIECE_LENGTH, info->rctx, RHASH_STR2UPTR(opt.bt_piece_length), 0);
74
53
* Calculate hash sums simultaneously, according to the info->sums.flags.
54
* Calculated hashes are stored in info->rctx.
76
56
* @param info file data. The info->full_path can be "-" to denote stdin
77
* @return 0 on success. On error -1 is returned and errno is set
57
* @return 0 on success, -1 on fail with error code stored in errno
79
59
static int calc_sums(struct file_info *info)
117
res = calc_sums_fd(fd, info);
97
assert(info->rctx == 0);
98
info->rctx = rhash_init(info->sums.flags);
100
/* initialize bittorrent data */
101
if(info->sums.flags & RHASH_BTIH) {
102
init_btih_data(info);
105
if(percents_output->update != 0) {
106
rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
109
/* read and hash file content */
110
if((res = rhash_file_update(info->rctx, fd)) != -1) {
111
rhash_final(info->rctx, 0); /* finalize hashing */
113
info->size = info->rctx->msg_size;
114
rhash_data.total_size += info->size;
118
117
if(fd != stdin) fclose(fd);
282
281
struct rsh_stat_struct stat_buf;
283
size_t len = strlen(info->full_path);
284
char* path = rsh_malloc(len + 9);
286
torrent_ctx* bt_ctx = rhash_get_context_ptr(info->rctx, RHASH_BTIH);
282
size_t path_len = strlen(info->full_path);
284
char* path = (char*)rsh_malloc(path_len + 9);
289
286
/* append .torrent extension to the file path */
290
memcpy(path, info->full_path, len);
291
memcpy(path + len, ".torrent", 9);
287
memcpy(path, info->full_path, path_len);
288
memcpy(path + path_len, ".torrent", 9);
293
290
/* get torrent file content */
294
len = torrent_get_text(bt_ctx, &str);
291
text_len = rhash_transmit(RMSG_BT_GET_TEXT, info->rctx, (unsigned long)&str, 0);
292
assert(text_len != RHASH_ERROR);
296
294
if(rsh_stat(path, &stat_buf) >= 0) {
298
296
log_file_error(path);
300
298
fd = rsh_fopen_bin(path, "wb");
301
if(fd && len == fwrite(str, 1, len, fd) && !ferror(fd)) {
299
if(fd && text_len == fwrite(str, 1, text_len, fd) && !ferror(fd)) {
302
300
log_msg("%s saved\n", path);
304
302
log_file_error(path);
314
312
* @param out a stream to print to
315
313
* @param filepath path to the file to calculate sums for
316
314
* @param fullpath fullpath to the file relative to the current directory
317
* @return 0 on success, -1 on error
315
* @return 0 on success, -1 on fail
319
317
int calculate_and_print_sums(FILE* out, const char *print_path, const char *full_path, struct rsh_stat_struct* stat_buf)
544
542
/* initialize percents output */
545
543
init_percents(info);
544
rhash_timer_start(&timer);
548
546
if(calc_sums(info) < 0) {
549
547
finish_percents(info, -1);
552
550
fill_sums_struct(info);
553
info->time = timer_stop(&timer);
551
info->time = rhash_timer_stop(&timer);
555
553
/* compare the sums and fill info->wrong_sums flags */
556
554
if((orig_sums.flags & RHASH_CRC32) && info->sums.crc32.be != orig_sums.crc32.be) {