diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-04-04 18:29:47 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-12 14:06:24 +0200 |
commit | dcee024fee44595d7708461651f39bcc649bb131 (patch) | |
tree | 4af5944f52584513e8e9156e82a645d302e68669 /Userland/Libraries/LibArchive | |
parent | a09421f1be9a4e80baa212c43db168a8e8152536 (diff) | |
download | serenity-dcee024fee44595d7708461651f39bcc649bb131.zip |
LibArchive: Support POSIX.1-1988 tar files
These old tar files didn't have magic numbers, so I've also added a checksum
check to TarInputStream::valid()
Diffstat (limited to 'Userland/Libraries/LibArchive')
-rw-r--r-- | Userland/Libraries/LibArchive/Tar.h | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibArchive/TarStream.cpp | 9 |
2 files changed, 31 insertions, 9 deletions
diff --git a/Userland/Libraries/LibArchive/Tar.h b/Userland/Libraries/LibArchive/Tar.h index cdd001c23f..0c92d20e74 100644 --- a/Userland/Libraries/LibArchive/Tar.h +++ b/Userland/Libraries/LibArchive/Tar.h @@ -49,10 +49,12 @@ enum class TarFileType : char { }; constexpr size_t block_size = 512; -constexpr const char* gnu_magic = "ustar "; // gnu format magic -constexpr const char* gnu_version = " "; // gnu format version -constexpr const char* ustar_magic = "ustar"; // ustar format magic -constexpr const char* ustar_version = "00"; // ustar format version +constexpr const char* gnu_magic = "ustar "; // gnu format magic +constexpr const char* gnu_version = " "; // gnu format version +constexpr const char* ustar_magic = "ustar"; // ustar format magic +constexpr const char* ustar_version = "00"; // ustar format version +constexpr const char* posix1_tar_magic = ""; // POSIX.1-1988 format magic +constexpr const char* posix1_tar_version = ""; // POSIX.1-1988 format version class [[gnu::packed]] TarFileHeader { public: @@ -63,6 +65,7 @@ public: // FIXME: support 2001-star size encoding size_t size() const { return get_tar_field(m_size); } time_t timestamp() const { return get_tar_field(m_timestamp); } + unsigned checksum() const { return get_tar_field(m_checksum); } TarFileType type_flag() const { return TarFileType(m_type_flag); } const StringView link_name() const { return m_link_name; } const StringView magic() const { return StringView(m_magic, min(__builtin_strlen(m_magic), sizeof(m_magic))); } // in some cases this is a null terminated string, in others its not @@ -90,6 +93,7 @@ public: void set_minor(int minor) { VERIFY(String::formatted("{:o}", minor).copy_characters_to_buffer(m_minor, sizeof(m_minor))); } void set_prefix(const String& prefix) { VERIFY(prefix.copy_characters_to_buffer(m_prefix, sizeof(m_prefix))); } + unsigned expected_checksum() const; void calculate_checksum(); private: @@ -128,14 +132,25 @@ size_t TarFileHeader::get_tar_field(const char (&field)[N]) } return value; } -void TarFileHeader::calculate_checksum() + +unsigned TarFileHeader::expected_checksum() const { - memset(m_checksum, ' ', sizeof(m_checksum)); auto checksum = 0u; + const u8* u8_this = reinterpret_cast<const u8*>(this); + const u8* u8_m_checksum = reinterpret_cast<const u8*>(&m_checksum); for (auto i = 0u; i < sizeof(TarFileHeader); ++i) { - checksum += ((unsigned char*)this)[i]; + if (u8_this + i >= u8_m_checksum && u8_this + i < u8_m_checksum + sizeof(m_checksum)) { + checksum += ' '; + } else { + checksum += u8_this[i]; + } } - VERIFY(String::formatted("{:o}", checksum).copy_characters_to_buffer(m_checksum, sizeof(m_checksum))); + return checksum; } +void TarFileHeader::calculate_checksum() +{ + memset(m_checksum, ' ', sizeof(m_checksum)); + VERIFY(String::formatted("{:06o}", expected_checksum()).copy_characters_to_buffer(m_checksum, sizeof(m_checksum))); +} } diff --git a/Userland/Libraries/LibArchive/TarStream.cpp b/Userland/Libraries/LibArchive/TarStream.cpp index b3763bef17..de40ebe406 100644 --- a/Userland/Libraries/LibArchive/TarStream.cpp +++ b/Userland/Libraries/LibArchive/TarStream.cpp @@ -125,7 +125,14 @@ bool TarInputStream::valid() const { auto& header_magic = header().magic(); auto& header_version = header().version(); - return (header_magic == gnu_magic && header_version == gnu_version) || (header_magic == ustar_magic && header_version == ustar_version); + + if (!((header_magic == gnu_magic && header_version == gnu_version) + || (header_magic == ustar_magic && header_version == ustar_version) + || (header_magic == posix1_tar_magic && header_version == posix1_tar_version))) + return false; + + // POSIX.1-1988 tar does not have magic numbers, so we also neet to verify the header checksum. + return header().checksum() == header().expected_checksum(); } TarFileStream TarInputStream::file_contents() |