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

« back to all changes in this revision

Viewing changes to output.c

  • Committer: Bazaar Package Importer
  • Author(s): Aleksey Kravchenko
  • Date: 2011-08-14 20:35:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110814203500-rvnovipwcpqkm91u
Tags: 1.2.7-1
* New upstream release version 1.2.7
 - Fixes security issue (Closes: #632280)
 - changed author/mantainer name to the way I prefer s/Alexey S/Aleksey/
 - simplified desriptions of binary packages
 - wriiten rules in more now standard way, using CFLAGS/LDFLAGS
 - reworded README.Debian a bit
 - aplied patch from upstream to fix possible segfault
 - aplied patch from upstream to fix glibc runtime warning
 - aplied patch from upstream to repair -openssl option handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        fflush(rhash_data.log);
59
59
}
60
60
 
61
 
/* a structure to store how much percents processed */
62
 
struct percents_t {
 
61
/**
 
62
 * Information about printed percents.
 
63
 */
 
64
struct percents_t
 
65
{
63
66
        int points;
64
67
        int use_cursor;
65
68
        int same_output;
72
75
};
73
76
static struct percents_t percents;
74
77
 
 
78
/* the hash functions, which needs to be reported first on mismatch */
 
79
#define REPORT_FIRST_MASK (RHASH_MD5 | RHASH_SHA256 | RHASH_SHA512)
 
80
 
 
81
/**
 
82
 * Print verbose error on hash sums mismatch.
 
83
 * 
 
84
 * @param info file information with path and its hash sums.
 
85
 */
 
86
static void print_verbose_error(struct file_info *info)
 
87
{
 
88
        char actual[130], expected[130];
 
89
        assert(HC_FAILED(info->hc.flags));
 
90
 
 
91
        fprintf(rhash_data.out, "ERROR");
 
92
 
 
93
        if(HC_WRONG_FILESIZE & info->hc.flags) {
 
94
                sprintI64(actual, info->rctx->msg_size, 0);
 
95
                sprintI64(expected, info->hc.file_size, 0);
 
96
                fprintf(rhash_data.out, ", size is %s should be %s", actual, expected);
 
97
        }
 
98
 
 
99
        if(HC_WRONG_EMBCRC32 & info->hc.flags) {
 
100
                rhash_print(expected, info->rctx, RHASH_CRC32, RHPR_UPPERCASE);
 
101
                fprintf(rhash_data.out, ", embedded CRC32 should be %s", expected);
 
102
        }
 
103
 
 
104
        if(HC_WRONG_HASHES & info->hc.flags) {
 
105
                int i;
 
106
                unsigned reported = 0;
 
107
                for(i = 0; i < info->hc.hashes_num; i++) {
 
108
                        hash_value *hv = &info->hc.hashes[i];
 
109
                        char *expected_hash = info->hc.data + hv->offset;
 
110
                        unsigned hid = hv->hash_id;
 
111
                        int pflags;
 
112
                        if((info->hc.wrong_hashes & (1 << i)) == 0) continue;
 
113
 
 
114
                        assert(hid != 0);
 
115
 
 
116
                        /* if can't detect precise hash */
 
117
                        if((hid & (hid - 1)) != 0) {
 
118
                                /* guess the hash id */
 
119
                                if(hid & opt.sum_flags) hid &= opt.sum_flags;
 
120
                                if(hid & ~info->hc.found_hash_ids) hid &= ~info->hc.found_hash_ids;
 
121
                                if(hid & ~reported) hid &= ~reported; /* avoiding repeating */
 
122
                                if(hid & REPORT_FIRST_MASK) hid &= REPORT_FIRST_MASK;
 
123
                                hid &= -(int)hid; /* take the lowest bit */
 
124
                        }
 
125
                        assert(hid != 0 && (hid & (hid - 1)) == 0); /* single bit only */
 
126
                        reported |= hid;
 
127
 
 
128
                        pflags = (hv->length == (rhash_get_digest_size(hid) * 2) ?
 
129
                                (RHPR_HEX | RHPR_UPPERCASE) : (RHPR_BASE32 | RHPR_UPPERCASE));
 
130
                        rhash_print(actual, info->rctx, hid, pflags);
 
131
                        fprintf(rhash_data.out, ", %s is %s should be %s", 
 
132
                                rhash_get_name(hid), actual, expected_hash);
 
133
                }
 
134
        }
 
135
 
 
136
        fprintf(rhash_data.out, "\n");
 
137
}
 
138
 
 
139
 
75
140
/**
76
141
 * Print file path and result of its verification by hash.
77
 
 * Also if error occured, print error message.
 
142
 * Also if error occurred, print error message.
78
143
 *
79
144
 * @param info pointer to the file-info structure
80
145
 * @param print_name set to non-zero to print file path
89
154
                if(info->error == -1) {
90
155
                        /* print error to stdout */
91
156
                        fprintf(rhash_data.out, "%s\n", strerror(errno));
92
 
                } else if(info->wrong_sums == 0 || !(opt.flags & OPT_VERBOSE)) {
93
 
                        /* using 4 characters to overwrite percent */
94
 
                        fprintf(rhash_data.out, (info->wrong_sums == 0 ? "OK \n" : "ERR\n") );
 
157
                } else if(!HC_FAILED(info->hc.flags) || !(opt.flags & OPT_VERBOSE)) {
 
158
                        /* using 3 characters to overwrite percent */
 
159
                        fprintf(rhash_data.out, (!HC_FAILED(info->hc.flags) ? "OK \n" : "ERR\n") );
95
160
                } else {
96
 
                        int id;
97
 
                        char actual[130], expected[130];
98
 
 
99
 
                        /* print verbose info about wrong sums */
100
 
                        fprintf(rhash_data.out, "ERROR");
101
 
                        for(id = 1; id < RHASH_ALL_HASHES; id <<= 1) {
102
 
                                if(id & info->wrong_sums) {
103
 
                                        int pflags = (rhash_is_base32(id) ? RHPR_BASE32 | RHPR_UPPERCASE : RHPR_HEX | RHPR_UPPERCASE);
104
 
                                        rhash_print_bytes(expected, rhash_get_digest_ptr(info->orig_sums, id), rhash_get_digest_size(id), pflags);
105
 
 
106
 
                                        rhash_print(actual, info->rctx, id, RHPR_UPPERCASE);
107
 
                                        fprintf(rhash_data.out, ", %s is %s should be %s", rhash_get_name(id), actual, expected);
108
 
                                }
109
 
                        }
110
 
                        if(RHASH_EMBEDDED_CRC32 & info->wrong_sums) {
111
 
                                rhash_print(expected, info->rctx, RHASH_CRC32, RHPR_UPPERCASE);
112
 
                                fprintf(rhash_data.out, ", embedded sum should be %s", expected);
113
 
                        }
114
 
                        fprintf(rhash_data.out, "\n");
 
161
                        print_verbose_error(info);
115
162
                }
116
163
        }
117
164
        fflush(rhash_data.out);
129
176
        if(opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED)) {
130
177
                int print_name = (opt.flags & (OPT_PERCENTS | OPT_SKIP_OK) ? !init : init);
131
178
 
132
 
                if(!init && (opt.flags & OPT_SKIP_OK) && errno == 0 && info->wrong_sums == 0) {
 
179
                if(!init && (opt.flags & OPT_SKIP_OK) && errno == 0 && !HC_FAILED(info->hc.flags)) {
133
180
                        return; /* skip OK message */
134
181
                }
135
182
 
144
191
 * No information is printed in other modes.
145
192
 *
146
193
 * @param info pointer to the file-info structure
147
 
 * @return non-zero, indicating that the output method succesfully initialized
 
194
 * @return non-zero, indicating that the output method successfully initialized
148
195
 */
149
196
static int dummy_init_percents(struct file_info *info)
150
197
{
157
204
 * Information is printed only in hash verification mode.
158
205
 *
159
206
 * @param info pointer to the file-info structure
160
 
 * @param process_res non-zero if error occured while hashing/checking
 
207
 * @param process_res non-zero if error occurred while hashing/checking
161
208
 */
162
209
static void dummy_finish_percents(struct file_info *info, int process_res)
163
210
{
165
212
        print_results_on_check(info, 0);
166
213
}
167
214
 
168
 
/* functions to output file info with simple multy-line wget-like percents */
 
215
/* functions to output file info with simple multi-line wget-like percents */
169
216
 
170
217
/**
171
218
 * Initialize dots percent mode.
172
219
 *
173
220
 * @param info pointer to the file-info structure
174
 
 * @return non-zero, indicating that the output method succesfully initialized
 
221
 * @return non-zero, indicating that the output method successfully initialized
175
222
 */
176
223
static int dots_init_percents(struct file_info *info)
177
224
{
188
235
 * then print the results of file check.
189
236
 *
190
237
 * @param info pointer to the file-info structure
191
 
 * @param process_res non-zero if error occured while hashing/checking
 
238
 * @param process_res non-zero if error occurred while hashing/checking
192
239
 */
193
240
static void dots_finish_percents(struct file_info *info, int process_res)
194
241
{
213
260
        if( (offset % pt_size) != 0 ) return;
214
261
 
215
262
        if(percents.points == 0) {
216
 
                fprintf(rhash_data.log, "\n%s %s\n", 
217
 
                        (opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED) ? "Checking" : "Processing"), 
 
263
                fprintf(rhash_data.log, "\n%s %s\n",
 
264
                        (opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED) ? "Checking" : "Processing"),
218
265
                        info->print_path);
219
266
                fflush(rhash_data.log);
220
267
        }
237
284
 * Initialize one-line percent mode.
238
285
 *
239
286
 * @param info pointer to the file-info structure
240
 
 * @return non-zero if the output method succesfully initialized
 
287
 * @return non-zero if the output method successfully initialized
241
288
 */
242
289
static int p_init_percents(struct file_info *info)
243
290
{
323
370
                dwCursorPosition.X = percents.cur_x;
324
371
                dwCursorPosition.Y = percents.cur_y;
325
372
                SetConsoleCursorPosition(percents.hOut, dwCursorPosition);
326
 
        } else 
 
373
        } else
327
374
#endif
328
375
        {
329
376
                fprintf(rhash_data.log, "\r%-51s ", info->print_path);
337
384
 * then print the results of file check.
338
385
 *
339
386
 * @param info pointer to the file-info structure
340
 
 * @param process_res non-zero if error occured while hashing/checking
 
387
 * @param process_res non-zero if error occurred while hashing/checking
341
388
 */
342
389
static void p_finish_percents(struct file_info *info, int process_res)
343
390
{
348
395
#endif
349
396
 
350
397
        need_check_result = (opt.mode & (MODE_CHECK | MODE_CHECK_EMBEDDED)) &&
351
 
                !((opt.flags & OPT_SKIP_OK) && errno == 0 && info->wrong_sums == 0);
 
398
                !((opt.flags & OPT_SKIP_OK) && errno == 0 && !HC_FAILED(info->hc.flags));
352
399
        info->error = process_res;
353
400
 
354
401
        if(percents.same_output && need_check_result) {
371
418
        p_init_percents, p_update_percents, p_finish_percents, "digits"
372
419
};
373
420
 
374
 
/** 
 
421
/**
375
422
 * Initialize pointers to output functions.
376
423
 */
377
424
void setup_output(void)
380
427
        rhash_data.log = stderr;
381
428
 
382
429
        if(opt.flags & OPT_PERCENTS) {
383
 
                /* we don't use _fileno() cause it is not in ISO C90, and so undefined
384
 
                when compiling with the GCC -ansi option */
 
430
                /* we don't use _fileno() cause it is not in ISO C90, and so
 
431
                 * is incompatible with the GCC -ansi option */
385
432
                if(rhash_data.log == stderr && isatty(2)) {
386
433
                        percents_output  = &p_perc;
387
 
                } else
388
 
                {
 
434
                } else {
389
435
                        percents_output  = &dots_perc;
390
436
                }
391
437
        } else {
434
480
/**
435
481
 * Print file processing times.
436
482
 */
437
 
void print_file_time_stats(struct file_info* info) 
 
483
void print_file_time_stats(struct file_info* info)
438
484
{
439
485
        print_time_stats(info->time, info->size, 0);
440
486
}
442
488
/**
443
489
 * Print processing time statistics.
444
490
 */
445
 
void print_time_stats(double time, uint64_t size, int total) 
 
491
void print_time_stats(double time, uint64_t size, int total)
446
492
{
447
493
        double speed = (time == 0 ? 0 : (double)(int64_t)size / 1048576.0 / time);
448
494
        fprintf(rhash_data.log, "%s %.3f sec, %4.2f MBps\n",