2021-05-15 13:38:40 +03:00
|
|
|
/*
|
2021-09-01 05:32:46 +03:00
|
|
|
* Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
|
2021-05-15 13:38:40 +03:00
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Tar.h"
|
|
|
|
|
|
|
|
namespace Archive {
|
|
|
|
|
|
|
|
unsigned TarFileHeader::expected_checksum() const
|
|
|
|
{
|
|
|
|
auto checksum = 0u;
|
2022-04-01 20:58:27 +03:00
|
|
|
u8 const* u8_this = reinterpret_cast<u8 const*>(this);
|
|
|
|
u8 const* u8_m_checksum = reinterpret_cast<u8 const*>(&m_checksum);
|
2021-05-15 13:38:40 +03:00
|
|
|
for (auto i = 0u; i < sizeof(TarFileHeader); ++i) {
|
|
|
|
if (u8_this + i >= u8_m_checksum && u8_this + i < u8_m_checksum + sizeof(m_checksum)) {
|
|
|
|
checksum += ' ';
|
|
|
|
} else {
|
|
|
|
checksum += u8_this[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TarFileHeader::calculate_checksum()
|
|
|
|
{
|
|
|
|
memset(m_checksum, ' ', sizeof(m_checksum));
|
2022-12-04 21:02:33 +03:00
|
|
|
VERIFY(DeprecatedString::formatted("{:06o}", expected_checksum()).copy_characters_to_buffer(m_checksum, sizeof(m_checksum)));
|
2021-05-15 13:38:40 +03:00
|
|
|
}
|
|
|
|
|
2022-11-29 03:01:13 +03:00
|
|
|
bool TarFileHeader::is_zero_block() const
|
|
|
|
{
|
|
|
|
u8 const* buffer = reinterpret_cast<u8 const*>(this);
|
|
|
|
for (size_t i = 0; i < sizeof(TarFileHeader); ++i) {
|
|
|
|
if (buffer[i] != 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-04 21:32:51 +03:00
|
|
|
bool TarFileHeader::content_is_like_extended_header() const
|
|
|
|
{
|
|
|
|
return type_flag() == TarFileType::ExtendedHeader || type_flag() == TarFileType::GlobalExtendedHeader;
|
|
|
|
}
|
|
|
|
|
2022-10-25 05:47:07 +03:00
|
|
|
void TarFileHeader::set_filename_and_prefix(StringView filename)
|
|
|
|
{
|
|
|
|
// FIXME: Add support for extended tar headers for longer filenames.
|
|
|
|
VERIFY(filename.length() <= sizeof(m_filename) + sizeof(m_prefix));
|
|
|
|
|
|
|
|
if (filename.length() <= sizeof(m_filename)) {
|
|
|
|
set_prefix(""sv);
|
|
|
|
set_filename(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<size_t> slash = filename.find('/', filename.length() - sizeof(m_filename));
|
|
|
|
|
|
|
|
VERIFY(slash.has_value());
|
|
|
|
set_prefix(filename.substring_view(0, slash.value() + 1));
|
|
|
|
set_filename(filename.substring_view(slash.value() + 1));
|
|
|
|
}
|
|
|
|
|
2021-05-15 13:38:40 +03:00
|
|
|
}
|