From 1b1b9d0177e051400b5722cfc9a5c42cb27a7b9e Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 20 Nov 2019 17:03:55 -0500 Subject: s/TAR_HEADER_SIZE/PAX_BLOCK_SIZE/ --- pax.c | 33 ++++++++++++++++----------------- pax.h | 6 +++--- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/pax.c b/pax.c index 4cbd0f2..98b4b4a 100644 --- a/pax.c +++ b/pax.c @@ -37,41 +37,40 @@ #include "pax.h" -enum pax_format pax_identify(size_t n, void *header) +static enum pax_format pax_identify(void *header) { - if (n < sizeof(MAGIC)) { - return PAX_FORMAT_UNKNOWN; + if (memcmp(header, MAGIC, sizeof(MAGIC) - 1) == 0) { + return PAX_FORMAT_CPIO; } - if (n >= cpio_header_size) { - if (memcmp(header, MAGIC, sizeof(MAGIC) - 1) == 0) { - return PAX_FORMAT_CPIO; - } - } - - /* TODO: differentiate between tar and pax format */ - if (n >= TAR_HEADER_SIZE) { - struct tar_header *th = header; - if (memcmp(th->magic, TMAGIC, TMAGLEN) == 0) { - return PAX_FORMAT_TAR; + struct tar_header *th = header; + if (memcmp(th->magic, TMAGIC, TMAGLEN) == 0) { + if (th->typeflag[0] == 'x' || th->typeflag[0] == 'g') { + return PAX_FORMAT_PAX; } + return PAX_FORMAT_TAR; } fprintf(stderr, "pax: unknown archive format\n"); return PAX_FORMAT_UNKNOWN; } -int pax_list(FILE *input) +static int pax_list(FILE *input) { - char header[TAR_HEADER_SIZE]; + char header[PAX_BLOCK_SIZE]; int nread = fread(header, 1, sizeof(header), input); + if (nread != sizeof(header)) { + fprintf(stderr, "pax: input truncated\n"); + return 1; + } - switch (pax_identify(nread, header)) { + switch (pax_identify(header)) { case PAX_FORMAT_CPIO: return cpio_list(input, nread, header); break; case PAX_FORMAT_TAR: + case PAX_FORMAT_PAX: return tar_list(input, nread, header); break; diff --git a/pax.h b/pax.h index 3856ee4..66c194f 100644 --- a/pax.h +++ b/pax.h @@ -25,7 +25,7 @@ #include #include -#define TAR_HEADER_SIZE 512 +#define PAX_BLOCK_SIZE 512 struct tar_header { char name[100]; @@ -65,8 +65,8 @@ enum pax_format { PAX_FORMAT_TAR, }; -int cpio_list(FILE *input, size_t firstlen, void *firstblock); -int tar_list(FILE *input, size_t firstlen, void *firstblock); +int cpio_list(FILE *input, size_t nblocks, void *firstblock); +int tar_list(FILE *input, size_t nblocks, void *firstblock); void pax_list_file(struct stat *st, const char *filename); uintmax_t pax_atoi(size_t n, const char s[static n], int base); -- cgit v1.2.1