diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-02-04 16:30:48 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-02-04 16:43:40 +0100 |
commit | 89b367941e12b1163afa517eeddca1b5ecd2a054 (patch) | |
tree | 932f6566a0194d0dfe978b378a053e5725ef9ca7 /lib | |
parent | 72c8155d9fc0eaeac72c053f46ebb7b231d4596a (diff) |
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tar/src/checksum.c | 25 | ||||
-rw-r--r-- | lib/tar/src/read_header.c | 11 | ||||
-rw-r--r-- | lib/tar/src/write_header.c | 7 |
3 files changed, 15 insertions, 28 deletions
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 <goliath@infraroot.at> */ -#include "config.h" - #include "tar/format.h" -#include <stdio.h> - -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 <string.h> #include <stdlib.h> -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 <string.h> +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); |