From 89b367941e12b1163afa517eeddca1b5ecd2a054 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 4 Feb 2023 16:30:48 +0100 Subject: libtar: some minor cleanups - Use is_memory_zero from libutil - Move checksum update function to tar writer code - Move checksum verify function to tar reader code - Only export the function to compute the checksum Signed-off-by: David Oberhollenzer --- lib/tar/src/checksum.c | 25 +------------------------ lib/tar/src/read_header.c | 11 +++++++---- lib/tar/src/write_header.c | 7 +++++++ 3 files changed, 15 insertions(+), 28 deletions(-) (limited to 'lib/tar/src') diff --git a/lib/tar/src/checksum.c b/lib/tar/src/checksum.c index 6541373..9dc6c0f 100644 --- a/lib/tar/src/checksum.c +++ b/lib/tar/src/checksum.c @@ -4,13 +4,9 @@ * * Copyright (C) 2019 David Oberhollenzer */ -#include "config.h" - #include "tar/format.h" -#include - -static unsigned int get_checksum(const tar_header_t *hdr) +unsigned int tar_compute_checksum(const tar_header_t *hdr) { const unsigned char *header_start = (const unsigned char *)hdr; const unsigned char *chksum_start = (const unsigned char *)hdr->chksum; @@ -27,22 +23,3 @@ static unsigned int get_checksum(const tar_header_t *hdr) chksum += *p; return chksum; } - -void update_checksum(tar_header_t *hdr) -{ - unsigned int chksum = get_checksum(hdr); - - sprintf(hdr->chksum, "%06o", chksum); - hdr->chksum[6] = '\0'; - hdr->chksum[7] = ' '; -} - -bool is_checksum_valid(const tar_header_t *hdr) -{ - unsigned int calculated_chksum = get_checksum(hdr); - sqfs_u64 read_chksum; - - if (read_octal(hdr->chksum, sizeof(hdr->chksum), &read_chksum)) - return 0; - return read_chksum == calculated_chksum; -} diff --git a/lib/tar/src/read_header.c b/lib/tar/src/read_header.c index ea4873b..219c9fb 100644 --- a/lib/tar/src/read_header.c +++ b/lib/tar/src/read_header.c @@ -10,11 +10,14 @@ #include #include -static bool is_zero_block(const tar_header_t *hdr) +static bool is_checksum_valid(const tar_header_t *hdr) { - const unsigned char *ptr = (const unsigned char *)hdr; + sqfs_u64 read_chksum; - return ptr[0] == '\0' && memcmp(ptr, ptr + 1, sizeof(*hdr) - 1) == 0; + if (read_octal(hdr->chksum, sizeof(hdr->chksum), &read_chksum)) + return false; + + return read_chksum == tar_compute_checksum(hdr); } static int check_version(const tar_header_t *hdr) @@ -179,7 +182,7 @@ int read_header(istream_t *fp, tar_header_decoded_t *out) if ((size_t)ret < sizeof(hdr)) goto out_eof; - if (is_zero_block(&hdr)) { + if (is_memory_zero(&hdr, sizeof(hdr))) { if (prev_was_zero) goto out_eof; prev_was_zero = true; diff --git a/lib/tar/src/write_header.c b/lib/tar/src/write_header.c index b0711b3..5d98686 100644 --- a/lib/tar/src/write_header.c +++ b/lib/tar/src/write_header.c @@ -9,6 +9,13 @@ #include "internal.h" #include +static void update_checksum(tar_header_t *hdr) +{ + sprintf(hdr->chksum, "%06o", tar_compute_checksum(hdr)); + hdr->chksum[6] = '\0'; + hdr->chksum[7] = ' '; +} + static void write_binary(char *dst, sqfs_u64 value, int digits) { memset(dst, 0, digits); -- cgit v1.2.3