diff options
Diffstat (limited to 'lib/tar/checksum.c')
-rw-r--r-- | lib/tar/checksum.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/tar/checksum.c b/lib/tar/checksum.c new file mode 100644 index 0000000..925f942 --- /dev/null +++ b/lib/tar/checksum.c @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "internal.h" + +void update_checksum(tar_header_t *hdr) +{ + unsigned int chksum = 0; + size_t i; + + memset(hdr->chksum, ' ', sizeof(hdr->chksum)); + + for (i = 0; i < sizeof(*hdr); ++i) + chksum += ((unsigned char *)hdr)[i]; + + write_octal(hdr->chksum, chksum, 6); + hdr->chksum[6] = '\0'; + hdr->chksum[7] = ' '; +} + +bool is_checksum_valid(const tar_header_t *hdr) +{ + tar_header_t copy; + + memcpy(©, hdr, sizeof(*hdr)); + update_checksum(©); + return memcmp(hdr, ©, sizeof(*hdr)) == 0; +} |