Avoid casts in tar_checksum

* src/list.c (tar_checksum, from_header):
Recode to avoid casts.
This commit is contained in:
Paul Eggert
2024-08-12 17:52:09 -07:00
parent 5ab90d6c96
commit 1521d3dae0

View File

@@ -337,18 +337,13 @@ list_archive (void)
enum read_header enum read_header
tar_checksum (union block *header, bool silent) tar_checksum (union block *header, bool silent)
{ {
size_t i;
int unsigned_sum = 0; /* the POSIX one :-) */ int unsigned_sum = 0; /* the POSIX one :-) */
int signed_sum = 0; /* the Sun one :-( */ int signed_sum = 0; /* the Sun one :-( */
int recorded_sum;
int parsed_sum;
char *p;
p = header->buffer; for (int i = 0; i < sizeof *header; i++)
for (i = sizeof *header; i-- != 0;)
{ {
unsigned_sum += (unsigned char) *p; unsigned char uc = header->buffer[i]; unsigned_sum += uc;
signed_sum += (signed char) (*p++); signed char sc = header->buffer[i]; signed_sum += sc;
} }
if (unsigned_sum == 0) if (unsigned_sum == 0)
@@ -356,22 +351,20 @@ tar_checksum (union block *header, bool silent)
/* Adjust checksum to count the "chksum" field as blanks. */ /* Adjust checksum to count the "chksum" field as blanks. */
for (i = sizeof header->header.chksum; i-- != 0;) for (int i = 0; i < sizeof header->header.chksum; i++)
{ {
unsigned_sum -= (unsigned char) header->header.chksum[i]; unsigned char uc = header->header.chksum[i]; unsigned_sum -= uc;
signed_sum -= (signed char) (header->header.chksum[i]); signed char sc = header->header.chksum[i]; signed_sum -= sc;
} }
unsigned_sum += ' ' * sizeof header->header.chksum; unsigned_sum += ' ' * sizeof header->header.chksum;
signed_sum += ' ' * sizeof header->header.chksum; signed_sum += ' ' * sizeof header->header.chksum;
parsed_sum = from_header (header->header.chksum, int recorded_sum = from_header (header->header.chksum,
sizeof header->header.chksum, 0, sizeof header->header.chksum, 0,
0, INT_MAX, true, silent); 0, INT_MAX, true, silent);
if (parsed_sum < 0) if (recorded_sum < 0)
return HEADER_FAILURE; return HEADER_FAILURE;
recorded_sum = parsed_sum;
if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
return HEADER_FAILURE; return HEADER_FAILURE;
@@ -879,7 +872,8 @@ from_header (char const *where0, size_t digs, char const *type,
value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit; value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
for (;;) for (;;)
{ {
value = (value << LG_256) + (unsigned char) *where++; unsigned char uc = *where++;
value = (value << LG_256) + uc;
if (where == lim) if (where == lim)
break; break;
if (((value << LG_256 >> LG_256) | topbits) != value) if (((value << LG_256 >> LG_256) | topbits) != value)