forked from rhash/RHash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_sums.c
656 lines (561 loc) · 18.1 KB
/
calc_sums.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* calc_sums.c - crc calculating and printing functions */
#include "common_func.h" /* should be included before the C library files */
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* free() */
#include <unistd.h> /* read() */
#include <fcntl.h> /* open() */
#include <time.h> /* localtime(), time() */
#include <sys/stat.h> /* stat() */
#include <errno.h>
#include <assert.h>
#include "librhash/rhash.h"
#include "librhash/timing.h"
#include "parse_cmdline.h"
#include "rhash_main.h"
#include "hash_print.h"
#include "output.h"
#include "win_utils.h"
#include "version.h"
#include "calc_sums.h"
/**
* Initialize BTIH hash function. Unlike other algorithms BTIH
* requires more data for correct computation.
*
* @param info the file data
*/
static void init_btih_data(struct file_info *info)
{
assert((info->rctx->hash_id & RHASH_BTIH) != 0);
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);
rhash_transmit(RMSG_BT_SET_PROGRAM_NAME, info->rctx, RHASH_STR2UPTR(PROGRAM_NAME "/" VERSION), 0);
if(opt.flags & OPT_BT_PRIVATE) {
rhash_transmit(RMSG_BT_SET_OPTIONS, info->rctx, RHASH_BT_OPT_PRIVATE, 0);
}
if(opt.bt_announce) {
rhash_transmit(RMSG_BT_SET_ANNOUNCE, info->rctx, RHASH_STR2UPTR(opt.bt_announce), 0);
}
if(opt.bt_piece_length) {
rhash_transmit(RMSG_BT_SET_PIECE_LENGTH, info->rctx, RHASH_STR2UPTR(opt.bt_piece_length), 0);
}
}
/**
* Calculate hash sums simultaneously, according to the info->sums_flags.
* Calculated hashes are stored in info->rctx.
*
* @param info file data. The info->full_path can be "-" to denote stdin
* @return 0 on success, -1 on fail with error code stored in errno
*/
static int calc_sums(struct file_info *info)
{
FILE* fd = stdin; /* stdin */
int res;
if(IS_DASH_STR(info->full_path)) {
info->print_path = "(stdin)";
#ifdef _WIN32
/* using 0 instead of _fileno(stdin). _fileno() is undefined under 'gcc -ansi' */
if(setmode(0, _O_BINARY) < 0) {
return -1;
}
#endif
} else {
struct rsh_stat_struct stat_buf;
/* skip non-existing files */
if(rsh_stat(info->full_path, &stat_buf) < 0) {
return -1;
}
if((opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED)) && S_ISDIR(stat_buf.st_mode)) {
errno = EISDIR;
return -1;
}
info->size = stat_buf.st_size; /* total size, in bytes */
IF_WINDOWS(win32_set_filesize64(info->full_path, &info->size)); /* set correct filesize for large files under win32 */
if(!info->sums_flags) return 0;
/* skip files opened with exclusive rights without reporting an error */
fd = rsh_fopen_bin(info->full_path, "rb");
if(!fd) {
return -1;
}
}
assert(info->rctx == 0);
info->rctx = rhash_init(info->sums_flags);
/* initialize BitTorrent data */
if(info->sums_flags & RHASH_BTIH) {
init_btih_data(info);
}
if(percents_output->update != 0) {
rhash_set_callback(info->rctx, (rhash_callback_t)percents_output->update, info);
}
/* read and hash file content */
if((res = rhash_file_update(info->rctx, fd)) != -1) {
rhash_final(info->rctx, 0); /* finalize hashing */
info->size = info->rctx->msg_size;
rhash_data.total_size += info->size;
}
if(fd != stdin) fclose(fd);
return res;
}
/**
* Free memory allocated by given file_info structure.
*
* @param info pointer the structure to de-initialize
*/
void file_info_destroy(struct file_info* info)
{
free(info->utf8_print_path);
free(info->allocated_ptr);
rhash_free(info->rctx);
}
/**
* Store print_path in a file_info struct, replacing if needed
* system path separators with specified by user command line option.
*
* @param info pointer to the the file_info structure to change
* @param print_path the print path to store
*/
static void file_info_set_print_path(struct file_info* info, const char* print_path)
{
char *p;
char wrong_sep;
/* check if path separator was specified by command line options */
if(opt.path_separator) {
wrong_sep = (opt.path_separator == '/' ? '\\' : '/');
if((p = (char*)strchr(print_path, wrong_sep)) != NULL) {
info->allocated_ptr = rsh_strdup(print_path);
info->print_path = info->allocated_ptr;
p = info->allocated_ptr + (p - print_path);
/* replace wrong_sep in the print_path with separator defined by options */
for(; *p; p++) {
if(*p == wrong_sep) *p = opt.path_separator;
}
return;
}
}
/* if path was not replaces, than just store the value */
info->print_path = print_path;
}
/**
* Return utf8 version of print_path.
*
* @param info file information
* @return utf8 string on success, NULL if couldn't convert.
*/
const char* file_info_get_utf8_print_path(struct file_info* info)
{
if(info->utf8_print_path == NULL) {
if(is_utf8()) return info->print_path;
info->utf8_print_path = to_utf8(info->print_path);
}
return info->utf8_print_path;
}
/* functions to calculate and print file sums */
/**
* Search for a crc32 hash sum in the given file name.
*
* @param filepath the path to the file.
* @param crc32 pointer to integer to receive parsed hash sum.
* @return non zero if crc32 was found, zero otherwise.
*/
static int find_embedded_crc32(const char* filepath, unsigned* crc32_be)
{
const char* e = filepath + strlen(filepath) - 10;
/* search for the sum enclosed in brackets */
for(; e >= filepath && !IS_PATH_SEPARATOR(*e); e--) {
if((*e == '[' && e[9] == ']') || (*e == '(' && e[9] == ')')) {
const char *p = e + 8;
for(; p > e && IS_HEX(*p); p--);
if(p == e) {
rhash_hex_to_byte(e + 1, (char unsigned*)crc32_be, 8);
return 1;
}
e -= 9;
}
}
return 0;
}
/**
* Rename given file inserting its crc32 sum enclosed in braces just before
* the file extension.
*
* @param info pointer to the data of the file to rename.
* @return 0 on success, -1 on fail with error code in errno
*/
int rename_file_to_embed_crc32(struct file_info *info)
{
size_t len = strlen(info->full_path);
const char* p = info->full_path + len;
const char* c = p - 1;
char* new_path;
char* insertion_point;
unsigned crc32_be;
assert((info->rctx->hash_id & RHASH_CRC32) != 0);
/* check if the filename contains a CRC32 hash sum */
if(find_embedded_crc32(info->print_path, &crc32_be)) {
unsigned char* c =
(unsigned char*)rhash_get_context_ptr(info->rctx, RHASH_CRC32);
unsigned actual_crc32 = ((unsigned)c[0] << 24) |
((unsigned)c[1] << 16) | ((unsigned)c[2] << 8) | (unsigned)c[3];
/* compare with calculated CRC32 */
if(crc32_be != actual_crc32) {
char crc32_str[9];
rhash_print(crc32_str, info->rctx, RHASH_CRC32, RHPR_UPPERCASE);
/* TRANSLATORS: sample filename with embedded CRC32: file_[A1B2C3D4].mkv */
log_warning(_("wrong embedded CRC32, should be %s\n"), crc32_str);
} else return 0;
}
/* find file extension (as the place to insert the hash sum) */
for(; c >= info->full_path && !IS_PATH_SEPARATOR(*c); c--) {
if(*c == '.') {
p = c;
break;
}
}
/* now p is the point to insert delimiter + hash string in brackets */
new_path = (char*)rsh_malloc(len + 12);
insertion_point = new_path + (p - info->full_path);
memcpy(new_path, info->full_path, p - info->full_path);
if(opt.embed_crc_delimiter && *opt.embed_crc_delimiter) *(insertion_point++) = *opt.embed_crc_delimiter;
rhash_print(insertion_point+1, info->rctx, RHASH_CRC32, RHPR_UPPERCASE);
insertion_point[0] = '[';
insertion_point[9] = ']'; /* ']' overrides '\0' inserted by rhash_print_sum() */
strcpy(insertion_point + 10, p); /* append file extension */
/* rename the file */
if(rename(info->full_path, new_path) < 0) {
log_error(_("can't move %s to %s: %s\n"), info->full_path, new_path,
strerror(errno));
free(new_path);
return -1;
}
/* change file name in the file info structure */
if(info->print_path >= info->full_path && info->print_path < p) {
file_info_set_print_path(info, new_path + len - strlen(info->print_path));
} else {
file_info_set_print_path(info, new_path);
}
free(info->full_path);
info->full_path = new_path;
return 0;
}
/**
* Save torrent file.
*
* @param info information about the hashed file
*/
static void save_torrent(struct file_info* info)
{
char *str;
FILE* fd;
struct rsh_stat_struct stat_buf;
size_t path_len = strlen(info->full_path);
size_t text_len;
char* path = (char*)rsh_malloc(path_len + 9);
/* append .torrent extension to the file path */
memcpy(path, info->full_path, path_len);
memcpy(path + path_len, ".torrent", 9);
/* get torrent file content */
text_len = rhash_transmit(RMSG_BT_GET_TEXT, info->rctx, RHASH_STR2UPTR(&str), 0);
assert(text_len != RHASH_ERROR);
if(rsh_stat(path, &stat_buf) >= 0) {
errno = EEXIST;
log_file_error(path);
} else {
fd = rsh_fopen_bin(path, "wb");
if(fd && text_len == fwrite(str, 1, text_len, fd) && !ferror(fd)) {
log_msg(_("%s saved\n"), path);
} else {
log_file_error(path);
}
if(fd) fclose(fd);
}
free(path);
}
/**
* Calculate and print file hash sums using printf format.
*
* @param out a stream to print to
* @param filepath path to the file to calculate sums for
* @param fullpath fullpath to the file relative to the current directory
* @return 0 on success, -1 on fail
*/
int calculate_and_print_sums(FILE* out, const char *print_path, const char *full_path, struct rsh_stat_struct* stat_buf)
{
struct file_info info;
timedelta_t timer;
int res = 0;
memset(&info, 0, sizeof(info));
info.full_path = rsh_strdup(full_path);
file_info_set_print_path(&info, print_path);
info.size = 0;
info.sums_flags = opt.sum_flags;
if(IS_DASH_STR(full_path)) {
print_path = "(stdin)";
memset(&info.stat_buf, 0, sizeof(info.stat_buf));
} else {
if(stat_buf != NULL) {
memcpy(&info.stat_buf, stat_buf, sizeof(info.stat_buf));
} else {
if(rsh_stat(full_path, (stat_buf = &info.stat_buf)) < 0) {
log_file_error(full_path);
free(info.full_path);
file_info_destroy(&info);
return -1;
}
}
if(S_ISDIR(stat_buf->st_mode)) return 0; /* don't handle directories */
info.size = stat_buf->st_size; /* total size, in bytes */
IF_WINDOWS(win32_set_filesize64(info.full_path, &info.size)); /* set correct filesize for large files under win32 */
}
/* initialize percents output */
init_percents(&info);
rhash_timer_start(&timer);
if(info.sums_flags) {
/* calculate sums */
if(calc_sums(&info) < 0) {
/* print error unless sharing access error occurred */
if(errno == EACCES) return 0;
log_file_error(full_path);
res = -1;
}
}
info.time = rhash_timer_stop(&timer);
finish_percents(&info, res);
if(opt.mode & MODE_TORRENT) {
save_torrent(&info);
}
if(opt.flags & OPT_EMBED_CRC) {
/* rename the file */
rename_file_to_embed_crc32(&info);
}
if((opt.mode & MODE_UPDATE) && opt.fmt == FMT_SFV) {
print_sfv_header_line(rhash_data.upd_fd, info.print_path, info.full_path);
if(opt.flags & OPT_VERBOSE) {
print_sfv_header_line(rhash_data.log, info.print_path, info.full_path);
fflush(rhash_data.log);
}
}
if(rhash_data.print_list && res >= 0) {
print_line(out, rhash_data.print_list, &info);
fflush(out);
/* duplicate calculated line to stderr or log file if verbose */
if( (opt.mode & MODE_UPDATE) && (opt.flags & OPT_VERBOSE) ) {
print_line(rhash_data.log, rhash_data.print_list, &info);
fflush(rhash_data.log);
}
if((opt.flags & OPT_SPEED) && info.sums_flags) {
print_file_time_stats(&info);
}
}
free(info.full_path);
file_info_destroy(&info);
return res;
}
/**
* Verify hash sums of the file.
*
* @param info structure file path to process
* @return zero on success, -1 on file error, -2 if hash sums are different
*/
static int verify_sums(struct file_info *info)
{
timedelta_t timer;
int res = 0;
errno = 0;
/* initialize percents output */
init_percents(info);
rhash_timer_start(&timer);
if(calc_sums(info) < 0) {
finish_percents(info, -1);
return -1;
}
info->time = rhash_timer_stop(&timer);
if((opt.flags & OPT_EMBED_CRC) && find_embedded_crc32(
info->print_path, &info->hc.embedded_crc32_be)) {
info->hc.flags |= HC_HAS_EMBCRC32;
assert(info->hc.hash_mask & RHASH_CRC32);
}
if(!hash_check_verify(&info->hc, info->rctx)) {
res = -2;
}
finish_percents(info, res);
if((opt.flags & OPT_SPEED) && info->sums_flags) {
print_file_time_stats(info);
}
return res;
}
/**
* Check hash sums in a hash file.
* Lines beginning with ';' and '#' are ignored.
*
* @param hash_file_path - the path of the file with hash sums to verify.
* @param chdir - true if function should emulate chdir to directory of filepath before checking it.
* @return zero on success, -1 on fail
*/
int check_hash_file(const char* hash_file_path, int chdir)
{
FILE *fd;
char buf[2048];
size_t pos;
const char *ralign;
timedelta_t timer;
struct file_info info;
int res = 0, line_num = 0;
double time;
/* process --check-embedded option */
if(opt.mode & MODE_CHECK_EMBEDDED) {
unsigned crc32_be;
if(find_embedded_crc32(hash_file_path, &crc32_be)) {
/* initialize file_info structure */
memset(&info, 0, sizeof(info));
info.full_path = rsh_strdup(hash_file_path);
file_info_set_print_path(&info, info.full_path);
info.sums_flags = info.hc.hash_mask = RHASH_CRC32;
info.hc.flags = HC_HAS_EMBCRC32;
info.hc.embedded_crc32_be = crc32_be;
res = verify_sums(&info);
fflush(rhash_data.out);
if(res == 0) rhash_data.ok++;
else if(res == -1 && errno == ENOENT) rhash_data.miss++;
rhash_data.processed++;
free(info.full_path);
file_info_destroy(&info);
} else {
log_warning(_("file name doesn't contain a CRC32: %s\n"), hash_file_path);
return -1;
}
return 0;
}
/* initialize statistics */
rhash_data.processed = rhash_data.ok = rhash_data.miss = 0;
rhash_data.total_size = 0;
if( IS_DASH_STR(hash_file_path) ) {
fd = stdin;
hash_file_path = "<stdin>";
} else if( !(fd = rsh_fopen_bin(hash_file_path, "rb") )) {
log_file_error(hash_file_path);
return -1;
}
pos = strlen(hash_file_path)+16;
ralign = str_set(buf, '-', (pos < 80 ? 80 - (int)pos : 2));
fprintf(rhash_data.out, _("\n--( Verifying %s )%s\n"), hash_file_path, ralign);
fflush(rhash_data.out);
rhash_timer_start(&timer);
/* mark the directory part of the path, by setting the pos index */
if(chdir) {
pos = strlen(hash_file_path);
for(; pos > 0 && !IS_PATH_SEPARATOR(hash_file_path[pos]); pos--);
if(IS_PATH_SEPARATOR(hash_file_path[pos])) pos++;
} else pos = 0;
/* read crc file line by line */
for(line_num = 0; fgets(buf, 2048, fd); line_num++)
{
char* line = buf;
char* path_without_ext = NULL;
/* skip unicode BOM */
if(line_num == 0 && buf[0] == (char)0xEF && buf[1] == (char)0xBB && buf[2] == (char)0xBF) line += 3;
if(*line == 0) continue; /* skip empty lines */
if(is_binary_string(line)) {
log_error(_("file is binary: %s\n"), hash_file_path);
if(fd != stdin) fclose(fd);
return -1;
}
/* skip comments and empty lines */
if(IS_COMMENT(*line) || *line == '\r' || *line == '\n') continue;
memset(&info, 0, sizeof(info));
rhash_data.processed++;
if(!hash_check_parse_line(line, &info.hc, !feof(fd))) continue;
if(info.hc.hash_mask == 0) continue;
info.print_path = info.hc.file_path;
info.sums_flags = info.hc.hash_mask;
/* see if crc file contains a hash sum without a filename */
if(info.print_path == NULL) {
char* point;
path_without_ext = rsh_strdup(hash_file_path);
point = strrchr(path_without_ext, '.');
if(point) {
*point = '\0';
file_info_set_print_path(&info, path_without_ext);
}
}
if(info.print_path != NULL) {
int is_absolute = IS_PATH_SEPARATOR(info.print_path[0]);
IF_WINDOWS(is_absolute = is_absolute || (info.print_path[0] && info.print_path[1] == ':'));
/* if filename shall be prepent by a directory path */
if(pos && !is_absolute) {
size_t len = strlen(info.print_path);
info.full_path = (char*)rsh_malloc(pos+len+1);
memcpy(info.full_path, hash_file_path, pos);
strcpy(info.full_path+pos, info.print_path);
} else {
info.full_path = rsh_strdup(info.print_path);
}
/* verify hash sums of the file */
res = verify_sums(&info);
fflush(rhash_data.out);
free(info.full_path);
file_info_destroy(&info);
/* update statistics */
if(res == 0) rhash_data.ok++;
else if(res == -1 && errno == ENOENT) rhash_data.miss++;
}
free(path_without_ext);
}
time = rhash_timer_stop(&timer);
fprintf(rhash_data.out, "%s\n", str_set(buf, '-', 80));
print_check_stats();
if(rhash_data.processed != rhash_data.ok) rhash_data.error_flag = 1;
if(opt.flags & OPT_SPEED && rhash_data.processed > 1) {
print_time_stats(time, rhash_data.total_size, 1);
}
rhash_data.processed = 0;
res = ferror(fd); /* check that crc file has been read without errors */
if(fd != stdin) fclose(fd);
return (res == 0 ? 0 : -1);
}
/**
* Print a file info line in SFV header format.
*
* @param out a stream to print info to
* @param printpath relative file path to print
* @param fullpath a path to the file relative to the current directory.
* @return 0 on success, -1 on fail with error code stored in errno
*/
int print_sfv_header_line(FILE* out, const char* printpath, const char* fullpath)
{
struct rsh_stat_struct stat_buf;
uint64_t filesize;
char buf[24];
if( (rsh_stat(fullpath, &stat_buf)) < 0 ) {
return -1; /* not reporting an error here */
}
if(S_ISDIR(stat_buf.st_mode)) return 0; /* don't handle directories */
filesize = stat_buf.st_size; /* total size, in bytes */
IF_WINDOWS(win32_set_filesize64(fullpath, &filesize)); /* set correct filesize for large files under win32 */
#ifdef _WIN32
/* skip file if it can't be opened with exclusive sharing rights */
if(!can_open_exclusive(fullpath)) {
return 0;
}
#endif
sprintI64(buf, filesize, 12);
fprintf(out, "; %s ", buf);
print_time(out, stat_buf.st_mtime);
fprintf(out, " %s\n", printpath);
return 0;
}
/**
* Print an SFV header banner. The banner consist of 3 comment lines,
* with the program description and current time.
*
* @param out a stream to print to
*/
void print_sfv_banner(FILE* out)
{
time_t cur_time = time(NULL);
struct tm *t = localtime(&cur_time);
if(t) {
fprintf(out, _("; Generated by %s v%s on %4u-%02u-%02u at %02u:%02u.%02u\n"),
PROGRAM_NAME, VERSION, (1900+t->tm_year), t->tm_mon+1,
t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
fprintf(out, _("; Written by Aleksey (Akademgorodok) - http://rhash.sourceforge.net/\n;\n"));
}
}