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 | |
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>
-rw-r--r-- | include/tar/format.h | 4 | ||||
-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 |
4 files changed, 16 insertions, 31 deletions
diff --git a/include/tar/format.h b/include/tar/format.h index 53a4665..c3e372d 100644 --- a/include/tar/format.h +++ b/include/tar/format.h @@ -94,9 +94,7 @@ int read_octal(const char *str, int digits, sqfs_u64 *out); int read_number(const char *str, int digits, sqfs_u64 *out); -void update_checksum(tar_header_t *hdr); - -bool is_checksum_valid(const tar_header_t *hdr); +unsigned int tar_compute_checksum(const tar_header_t *hdr); #ifdef __cplusplus } 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); |