~ubuntu-branches/ubuntu/trusty/rhash/trusty

« back to all changes in this revision

Viewing changes to calc_sums.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexey S Kravchenko
  • Date: 2011-06-15 01:03:34 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110615010334-ochayini7ut2djvs
Tags: 1.2.6-1
* New upstream release version 1.2.6
 - ABI changed - now librhash0.0 is librhash0
 - OpenSSL support for faster hash calculation if libssl is installed
   (fully optional) for both rhash and librhash0

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
#include "librhash/hex.h"
15
15
#include "librhash/rhash.h"
16
 
#include "librhash/torrent.h"
17
16
#include "librhash/timing.h"
18
17
#include "parse_cmdline.h"
19
18
#include "rhash_main.h"
26
25
#include "calc_sums.h"
27
26
 
28
27
/**
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.
30
30
 *
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
34
32
 */
35
 
static int calc_sums_fd(FILE* fd, struct file_info *info)
 
33
static void init_btih_data(struct file_info *info)
36
34
{
37
 
  assert(info->rctx == 0);
38
 
  info->rctx = rhash_init(info->sums.flags);
39
 
 
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);
45
 
 
46
 
    if(opt.flags & OPT_BT_PRIVATE) {
47
 
      torrent_set_options(bt_ctx, BT_OPT_PRIVATE);
48
 
    }
49
 
    if(opt.bt_announce) {
50
 
      torrent_set_announce(bt_ctx, opt.bt_announce);
51
 
    }
52
 
    if(opt.bt_piece_length) {
53
 
      torrent_set_piece_length(bt_ctx, opt.bt_piece_length);
54
 
    }
55
 
  }
56
 
 
57
 
  if(percents_output->update != 0) {
58
 
    rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
59
 
  }
60
 
 
61
 
  /* read and hash file content */
62
 
  if(rhash_file_update(info->rctx, fd) == -1) return -1;
63
 
 
64
 
  rhash_final(info->rctx, 0); /* finalize hashing */
65
 
 
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);*/
69
 
 
70
 
  return 0;
 
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);
 
38
 
 
39
        if(opt.flags & OPT_BT_PRIVATE) {
 
40
                rhash_transmit(RMSG_BT_SET_OPTIONS, info->rctx, RHASH_BT_OPT_PRIVATE, 0);
 
41
        }
 
42
 
 
43
        if(opt.bt_announce) {
 
44
                rhash_transmit(RMSG_BT_SET_ANNOUNCE, info->rctx, RHASH_STR2UPTR(opt.bt_announce), 0);
 
45
        }
 
46
 
 
47
        if(opt.bt_piece_length) {
 
48
                rhash_transmit(RMSG_BT_SET_PIECE_LENGTH, info->rctx, RHASH_STR2UPTR(opt.bt_piece_length), 0);
 
49
        }
71
50
}
72
51
 
73
52
/**
74
53
 * Calculate hash sums simultaneously, according to the info->sums.flags.
 
54
 * Calculated hashes are stored in info->rctx.
75
55
 *
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
78
58
 */
79
59
static int calc_sums(struct file_info *info)
80
60
{
85
65
    info->print_path = "(stdin)";
86
66
 
87
67
#ifdef _WIN32
88
 
    /* using 0, wich is _fileno(stdin). _fileno() is undefined under 'gcc -ansi' */
 
68
    /* using 0 instead of _fileno(stdin). _fileno() is undefined under 'gcc -ansi' */
89
69
    if(setmode(0, _O_BINARY) < 0) {
90
70
      return -1;
91
71
    }
113
93
      return -1;
114
94
    }
115
95
  }
116
 
 
117
 
  res = calc_sums_fd(fd, info);
 
96
  
 
97
  assert(info->rctx == 0);
 
98
  info->rctx = rhash_init(info->sums.flags);
 
99
 
 
100
  /* initialize bittorrent data */
 
101
  if(info->sums.flags & RHASH_BTIH) {
 
102
    init_btih_data(info);
 
103
  }
 
104
 
 
105
  if(percents_output->update != 0) {
 
106
    rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
 
107
  }
 
108
 
 
109
  /* read and hash file content */
 
110
  if((res = rhash_file_update(info->rctx, fd)) != -1) {
 
111
    rhash_final(info->rctx, 0); /* finalize hashing */
 
112
 
 
113
    info->size = info->rctx->msg_size;
 
114
    rhash_data.total_size += info->size;
 
115
  }
 
116
 
118
117
  if(fd != stdin) fclose(fd);
119
118
  return res;
120
119
}
197
196
      const char *p = e + 8;
198
197
      for(; p > e && IS_HEX(*p); p--);
199
198
      if(p == e) {
200
 
        hex_to_byte(e + 1, (char unsigned*)crc32_be, 8);
 
199
        rhash_hex_to_byte(e + 1, (char unsigned*)crc32_be, 8);
201
200
        return 1;
202
201
      }
203
202
      e -= 9;
280
279
  char *str;
281
280
  FILE* fd;
282
281
  struct rsh_stat_struct stat_buf;
283
 
  size_t len = strlen(info->full_path);
284
 
  char* path = rsh_malloc(len + 9);
285
 
 
286
 
  torrent_ctx* bt_ctx = rhash_get_context_ptr(info->rctx, RHASH_BTIH);
287
 
  assert(bt_ctx);
288
 
 
 
282
  size_t path_len = strlen(info->full_path);
 
283
  size_t text_len;
 
284
  char* path = (char*)rsh_malloc(path_len + 9);
 
285
  
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);
292
289
 
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);
295
293
  
296
294
  if(rsh_stat(path, &stat_buf) >= 0) {
297
295
    errno = EEXIST;
298
296
    log_file_error(path);
299
297
  } else {
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);
303
301
    } else {
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
318
316
 */
319
317
int calculate_and_print_sums(FILE* out, const char *print_path, const char *full_path, struct rsh_stat_struct* stat_buf)
320
318
{
351
349
 
352
350
  /* initialize percents output */
353
351
  init_percents(&info);
354
 
  timer_start(&timer);
 
352
  rhash_timer_start(&timer);
355
353
 
356
354
  if(info.sums.flags) {
357
355
    /* calculate sums */
363
361
    }
364
362
  }
365
363
 
366
 
  info.time = timer_stop(&timer);
 
364
  info.time = rhash_timer_stop(&timer);
367
365
  finish_percents(&info, res);
368
366
 
369
367
  if(opt.mode & MODE_TORRENT) {
543
541
 
544
542
  /* initialize percents output */
545
543
  init_percents(info);
546
 
  timer_start(&timer);
 
544
  rhash_timer_start(&timer);
547
545
 
548
546
  if(calc_sums(info) < 0) {
549
547
    finish_percents(info, -1);
550
548
    return -1;
551
549
  }
552
550
  fill_sums_struct(info);
553
 
  info->time = timer_stop(&timer);
 
551
  info->time = rhash_timer_stop(&timer);
554
552
  
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) {
655
653
 *
656
654
 * @param crc_file_path - the path of the file with hash sums to verify.
657
655
 * @param chdir - true if function should emulate chdir to directory of filepath before checking it.
658
 
 * @return zero on success, -1 on error
 
656
 * @return zero on success, -1 on fail
659
657
 */
660
658
int check_crc_file(const char* crc_file_path, int chdir)
661
659
{
710
708
  ralign = str_set(buf, '-', (pos < 80 ? 80 - (int)pos : 2));
711
709
  fprintf(rhash_data.out, "\n--( Verifying %s )%s\n", crc_file_path, ralign);
712
710
  fflush(rhash_data.out);
713
 
  timer_start(&timer);
 
711
  rhash_timer_start(&timer);
714
712
  
715
713
  /* mark dirname part of the path, by setting pos */
716
714
  if(chdir) {
783
781
    rhash_data.processed++;
784
782
    free(path_without_ext);
785
783
  }
786
 
  time = timer_stop(&timer);
 
784
  time = rhash_timer_stop(&timer);
787
785
  
788
786
  fprintf(rhash_data.out, "%s\n", str_set(buf, '-', 80));
789
787
  print_check_stats();
806
804
 * @param out a stream to print info to
807
805
 * @param printpath relative file path to print
808
806
 * @param fullpath a path to the file relative to the current directory.
809
 
 * @return 0 on success, -1 on error with error stored in errno
 
807
 * @return 0 on success, -1 on fail with error code stored in errno
810
808
 */
811
809
int print_sfv_header_line(FILE* out, const char* printpath, const char* fullpath)
812
810
{