Skip to content

Commit be120d8

Browse files
committed
Refactoring: Fix compilation warnings
1 parent f0432d0 commit be120d8

File tree

9 files changed

+54
-29
lines changed

9 files changed

+54
-29
lines changed

hash_check.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ enum HashNameMatchModes {
298298
* allowed.
299299
* @return id of hash function if found, zero otherwise
300300
*/
301-
static unsigned bsd_hash_name_to_id(const char* name, unsigned length, enum HashNameMatchModes match_mode)
301+
static unsigned bsd_hash_name_to_id(const char* name, size_t length, enum HashNameMatchModes match_mode)
302302
{
303303
#define code2mask_size (19 * 2)
304304
static unsigned code2mask[code2mask_size] = {
@@ -522,7 +522,7 @@ static int match_hash_tokens(struct hash_token* token, const char* format, unsig
522522
buf[len] = toupper(begin[len]);
523523
}
524524
buf[len] = '\0';
525-
token->expected_hash_id = bsd_hash_name_to_id(buf, len, ExactMatch);
525+
token->expected_hash_id = bsd_hash_name_to_id(buf, (size_t)len, ExactMatch);
526526
if (!token->expected_hash_id)
527527
return ResFailed;
528528
token->hash_type = FmtAll;
@@ -1048,7 +1048,7 @@ unsigned get_crc32(struct rhash_context* ctx)
10481048
static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context* ctx)
10491049
{
10501050
uint64_t hash_mask = parser->hash_mask;
1051-
unsigned unverified_mask;
1051+
uint64_t unverified_mask;
10521052
unsigned printed;
10531053
char hex[132], base32[104], base64[88];
10541054
int j;
@@ -1067,7 +1067,7 @@ static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context*
10671067
if (parser->hashes_num == 0)
10681068
return !HP_FAILED(parser->bit_flags);
10691069

1070-
unverified_mask = (1 << parser->hashes_num) - 1;
1070+
unverified_mask = ((uint64_t)1 << parser->hashes_num) - 1;
10711071

10721072
while(hash_mask && unverified_mask) {
10731073
uint64_t bit64 = hash_mask & -hash_mask;
@@ -1084,7 +1084,7 @@ static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context*
10841084
int comparision_mode;
10851085

10861086
/* skip already verified message digests and message digests of different size */
1087-
if (!(unverified_mask & (1 << j)) || !(hv->hash_mask & bit64))
1087+
if (!(unverified_mask & ((uint64_t)1 << j)) || !(hv->hash_mask & bit64))
10881088
continue;
10891089
comparision_mode = 0;
10901090
bit_length = rhash_get_digest_size(hash_id) * 8;
@@ -1127,7 +1127,7 @@ static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context*
11271127
if (!is_hash_string_equal(calculated_hash, expected_hash, hv->length, comparision_mode))
11281128
continue;
11291129

1130-
unverified_mask &= ~(1 << j); /* mark the j-th message digest as verified */
1130+
unverified_mask &= ~((uint64_t)1 << j); /* mark the j-th message digest as verified */
11311131
parser->found_hash_ids |= bit64;
11321132

11331133
/* end the loop if all message digests were successfully verified */

hash_print.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,11 @@ static int print_time64(FILE* out, uint64_t time64, int sfv_format)
497497

498498
static int is_gost94(unsigned hash_id)
499499
{
500-
static unsigned gost94_id = hash_id_to_extended(RHASH_GOST94);
501-
static unsigned gost94_cryptopro_id = hash_id_to_extended(RHASH_GOST94_CRYPTOPRO);
500+
static unsigned gost94_id = 0, gost94_cryptopro_id = 0;
501+
if (!gost94_id) {
502+
gost94_id = hash_id_to_extended(RHASH_GOST94);
503+
gost94_cryptopro_id = hash_id_to_extended(RHASH_GOST94_CRYPTOPRO);
504+
}
502505
return (hash_id == gost94_id || hash_id == gost94_cryptopro_id);
503506
}
504507

@@ -687,6 +690,7 @@ void init_hash_info_table(void)
687690
*(short_opt++) : 0);
688691

689692
info->name = rhash_get_name(hash_id);
693+
RSH_REQUIRE(info->name != NULL, "rhash_get_name(0x%08x) failed\n", hash_id);
690694
assert(strlen(info->name) < 19);
691695
p = info->name;
692696
d = info->short_name;
@@ -736,7 +740,7 @@ void init_hash_info_table(void)
736740
}
737741
} else
738742
info->bsd_name = info->name;
739-
assert(info->bsd_name);
743+
RSH_REQUIRE(info->bsd_name, "No BSD name for hash id 0x%08x\n", hash_id);
740744
++info;
741745
}
742746
assert((info - hash_info_table) == RHASH_HASH_COUNT);

librhash/algorithms.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,12 @@ const unsigned* rhash_get_all_hash_ids(unsigned all_id, size_t* count)
223223
EXTENDED_HASH_ID(24), EXTENDED_HASH_ID(25), EXTENDED_HASH_ID(26), EXTENDED_HASH_ID(27),
224224
EXTENDED_HASH_ID(28), EXTENDED_HASH_ID(29), EXTENDED_HASH_ID(30), EXTENDED_HASH_ID(31)
225225
};
226+
#if defined(rhash_popcount)
226227
static const unsigned count_low = rhash_popcount(RHASH_LOW_HASHES_MASK);
228+
#else
229+
RHASH_ASSERT(RHASH_LOW_HASHES_MASK == 0x7fffffff);
230+
static const unsigned count_low = 31; /* popcount(RHASH_LOW_HASHES_MASK) */
231+
#endif
227232
RHASH_ASSERT(RHASH_COUNTOF(all_ids) == RHASH_HASH_COUNT);
228233
*count = (all_id == RHASH_ALL_HASHES ? RHASH_HASH_COUNT : count_low);
229234
return all_ids;

librhash/blake3.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static void process_block(struct blake3_ctx *ctx, const uint32_t msg[static 16])
178178
cur_hash += words_per_stack_entry;
179179
memcpy(cur_hash, blake3_IV, sizeof(blake3_IV));
180180
}
181-
ctx->stack_depth = (cur_hash - ctx->stack) / words_per_stack_entry;
181+
ctx->stack_depth = (uint32_t)((cur_hash - ctx->stack) / words_per_stack_entry);
182182
}
183183

184184
/**

librhash/byte_order.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static uint64_t get_cpuid_features(void)
278278
int has_cpu_feature(unsigned feature_bit)
279279
{
280280
static uint64_t features;
281-
const uint64_t feature = ((uint64_t)1) << feature_bit;
281+
const uint64_t feature = I64(1) << feature_bit;
282282
if (!features)
283283
features = (get_cpuid_features() | 1);
284284
return !!(features & feature);

librhash/gost12.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static const uint64_t zero_512[8] = { 0,0,0,0,0,0,0,0 };
619619
* @param ctx context to initialize
620620
* @param hash_size the size of the digest in bytes
621621
*/
622-
static RHASH_INLINE void rhash_gost12_init(gost12_ctx* ctx, size_t hash_size)
622+
static RHASH_INLINE void rhash_gost12_init(gost12_ctx* ctx, unsigned hash_size)
623623
{
624624
memset(ctx, 0, sizeof(gost12_ctx));
625625
if (hash_size != gost12_512_hash_size)
@@ -808,7 +808,7 @@ void rhash_gost12_update(gost12_ctx* ctx, const unsigned char* msg, size_t size)
808808
size_t rest = gost12_block_size - ctx->index;
809809

810810
le64_copy(ctx->message, ctx->index, msg, (size < rest ? size : rest));
811-
ctx->index += size;
811+
ctx->index += (unsigned)size;
812812
if (size < rest)
813813
return;
814814

@@ -837,7 +837,7 @@ void rhash_gost12_update(gost12_ctx* ctx, const unsigned char* msg, size_t size)
837837
}
838838
if (size)
839839
{
840-
ctx->index = size;
840+
ctx->index = (unsigned)size;
841841
le64_copy(ctx->message, 0, msg, size); /* save leftovers */
842842
}
843843
}

librhash/rhash.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include <stddef.h>
3838
#include <string.h>
3939

40+
#if defined(_WIN32)
41+
# include <io.h>
42+
#endif
43+
4044
#define STATE_ACTIVE 0xb01dbabe
4145
#define STATE_STOPPED 0xdeadbeef
4246
#define STATE_DELETED 0xdecea5ed
@@ -106,7 +110,7 @@ static rhash_context_ext* rhash_alloc_multi(size_t count, const unsigned hash_id
106110
}
107111
assert(IS_EXTENDED_HASH_ID(info->info->hash_id));
108112
assert(IS_VALID_EXTENDED_HASH_ID(info->info->hash_id));
109-
hash_bitmask |= 1 << GET_EXTENDED_HASH_ID_INDEX(info->info->hash_id);
113+
hash_bitmask |= I64(1) << GET_EXTENDED_HASH_ID_INDEX(info->info->hash_id);
110114

111115
/* align context sizes and sum up */
112116
ctx_size_sum += GET_CTX_ALIGNED(info->context_size);
@@ -122,7 +126,7 @@ static rhash_context_ext* rhash_alloc_multi(size_t count, const unsigned hash_id
122126
rctx->rc.hash_mask = hash_bitmask;
123127
rctx->flags = RCTX_AUTO_FINAL; /* turn on auto-final by default */
124128
rctx->state = STATE_ACTIVE;
125-
rctx->hash_vector_size = count;
129+
rctx->hash_vector_size = (unsigned)count;
126130

127131
/* calculate aligned pointer >= (&rctx->vector[count]) */
128132
phash_ctx = (char*)rctx + header_size;
@@ -402,7 +406,7 @@ RHASH_API rhash rhash_import(const void* in, size_t size)
402406
item_size = rhash_import_alg(hash_ids[i], dst_context, src_item, left_size);
403407
imported_size += item_size;
404408
if (!item_size || size < imported_size) {
405-
ectx->hash_vector_size = i + 1; /* clean only initialized contextes */
409+
ectx->hash_vector_size = (unsigned)i + 1; /* clean only initialized contextes */
406410
rhash_free(&ectx->rc);
407411
return import_error_einval();
408412
}
@@ -411,7 +415,7 @@ RHASH_API rhash rhash_import(const void* in, size_t size)
411415
item_size = hash_info->context_size;
412416
imported_size += item_size;
413417
if (size < imported_size) {
414-
ectx->hash_vector_size = i + 1;
418+
ectx->hash_vector_size = (unsigned)i + 1;
415419
rhash_free(&ectx->rc);
416420
return import_error_einval();
417421
}
@@ -528,6 +532,14 @@ struct file_update_context {
528532
size_t buffer_size; /* Size of the data buffer */
529533
};
530534

535+
#if defined(_WIN32)
536+
/* For Windows define ssize_t, which is Posix, but not in standard C */
537+
# define ssize_t intptr_t
538+
# define READ_SIZE_TYPE unsigned
539+
#else
540+
# define READ_SIZE_TYPE size_t
541+
#endif
542+
531543
/**
532544
* Read data from a C file stream into the context buffer using fread().
533545
*
@@ -555,7 +567,7 @@ static ssize_t read_file_fd_impl(struct file_update_context *fctx, size_t data_s
555567
static ssize_t read_int_fd_impl(struct file_update_context *fctx, size_t data_size)
556568
{
557569
assert(data_size <= fctx->buffer_size);
558-
return read(fctx->int_fd, fctx->buffer, data_size);
570+
return read(fctx->int_fd, fctx->buffer, (READ_SIZE_TYPE)data_size);
559571
}
560572

561573
/**
@@ -723,13 +735,13 @@ RHASH_API int rhash_get_hash_length(unsigned hash_id)
723735
RHASH_API const char* rhash_get_name(unsigned hash_id)
724736
{
725737
const rhash_info* info = rhash_info_by_id(hash_id);
726-
return (info ? info->name : 0);
738+
return (info ? info->name : NULL);
727739
}
728740

729741
RHASH_API const char* rhash_get_magnet_name(unsigned hash_id)
730742
{
731743
const rhash_info* info = rhash_info_by_id(hash_id);
732-
return (info ? info->magnet_name : 0);
744+
return (info ? info->magnet_name : NULL);
733745
}
734746

735747
static size_t rhash_get_magnet_url_size(const char* filepath,

librhash/sha_ni.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static void rhash_sha256_ni_process_block(unsigned* state, const uint8_t* block,
397397
MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
398398

399399
/* Rounds 28-31 */
400-
MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
400+
MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
401401
STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
402402
TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
403403
MSG0 = _mm_add_epi32(MSG0, TMP);

output.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ struct percents_output_info_t* percents_output = NULL;
3838
*/
3939
static int count_printed_size(const char* format, const char* path, unsigned output_flags)
4040
{
41-
size_t format_length = 0;
41+
size_t format_length = 0, total_length;
4242
if (format) {
4343
assert(strstr(format, "%s") != NULL);
4444
format_length = (IS_UTF8() ? count_utf8_symbols(format) : strlen(format)) - 2;
4545
}
4646
assert(path != NULL);
47-
return format_length + (IS_UTF8() || (output_flags & OutForceUtf8) ? count_utf8_symbols(path) : strlen(path));
47+
total_length = format_length + (IS_UTF8() || (output_flags & OutForceUtf8) ?
48+
count_utf8_symbols(path) : strlen(path));
49+
return (int)total_length;
4850
}
4951

5052
/**
@@ -123,7 +125,7 @@ static int fprint_escaped(FILE* out, const char* str, unsigned output_flags)
123125
pos = 0;
124126
}
125127
if ((output_flags & OutCountSymbols) != 0)
126-
return escaped + count_printed_size(NULL, str, output_flags);
128+
return (int)escaped + count_printed_size(NULL, str, output_flags);
127129
return 0;
128130
}
129131

@@ -397,8 +399,9 @@ static int print_verbose_hash_check_error(struct file_info* info)
397399
struct hash_value* hv = &info->hp->hashes[i];
398400
char* expected_hash = info->hp->line_begin + hv->offset;
399401
uint64_t hid = hv->hash_mask;
402+
uint32_t hash_id;
400403
int pflags;
401-
if ((info->hp->wrong_hashes & (1 << i)) == 0)
404+
if ((info->hp->wrong_hashes & ((uint64_t)1 << i)) == 0)
402405
continue;
403406

404407
assert(hid != 0);
@@ -414,13 +417,14 @@ static int print_verbose_hash_check_error(struct file_info* info)
414417
}
415418
assert(hid != 0 && (hid & (hid - 1)) == 0); /* single bit only */
416419
reported |= hid;
420+
hash_id = bit64_to_hash_id(hid);
417421

418-
pflags = (hv->length == (rhash_get_digest_size(hid) * 2) ?
422+
pflags = (hv->length == (rhash_get_digest_size(hash_id) * 2) ?
419423
(RHPR_HEX | RHPR_UPPERCASE) : (RHPR_BASE32 | RHPR_UPPERCASE));
420-
rhash_print(actual, info->rctx, hid, pflags);
424+
rhash_print(actual, info->rctx, hash_id, pflags);
421425
/* TRANSLATORS: print a message like "CRC32 is ABC12345 should be BCA54321" */
422426
if (rsh_fprintf(rhash_data.out, _(", %s is %s should be %s"),
423-
rhash_get_name(hid), actual, expected_hash) < 0)
427+
rhash_get_name(hash_id), actual, expected_hash) < 0)
424428
return -1;
425429
}
426430
}

0 commit comments

Comments
 (0)