mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
LibArchive: Implement a basic parser for tar extended headers
This commit is contained in:
parent
ff19c1cffe
commit
f6e86915f5
1 changed files with 50 additions and 0 deletions
|
@ -40,6 +40,9 @@ public:
|
||||||
const TarFileHeader& header() const { return m_header; }
|
const TarFileHeader& header() const { return m_header; }
|
||||||
TarFileStream file_contents();
|
TarFileStream file_contents();
|
||||||
|
|
||||||
|
template<VoidFunction<StringView, StringView> F>
|
||||||
|
ErrorOr<void> for_each_extended_header(F func);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TarFileHeader m_header;
|
TarFileHeader m_header;
|
||||||
InputStream& m_stream;
|
InputStream& m_stream;
|
||||||
|
@ -64,4 +67,51 @@ private:
|
||||||
friend class TarFileStream;
|
friend class TarFileStream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<VoidFunction<StringView, StringView> F>
|
||||||
|
inline ErrorOr<void> TarInputStream::for_each_extended_header(F func)
|
||||||
|
{
|
||||||
|
VERIFY(header().content_is_like_extended_header());
|
||||||
|
|
||||||
|
Archive::TarFileStream file_stream = file_contents();
|
||||||
|
|
||||||
|
ByteBuffer file_contents_buffer = TRY(ByteBuffer::create_zeroed(header().size()));
|
||||||
|
VERIFY(file_stream.read(file_contents_buffer) == header().size());
|
||||||
|
|
||||||
|
StringView file_contents { file_contents_buffer };
|
||||||
|
|
||||||
|
while (!file_contents.is_empty()) {
|
||||||
|
// Split off the length (until the first space).
|
||||||
|
Optional<size_t> length_end_index = file_contents.find(' ');
|
||||||
|
if (!length_end_index.has_value())
|
||||||
|
return Error::from_string_literal("Malformed extended header: No length found.");
|
||||||
|
Optional<unsigned int> length = file_contents.substring_view(0, length_end_index.value()).to_uint();
|
||||||
|
if (!length.has_value())
|
||||||
|
return Error::from_string_literal("Malformed extended header: Could not parse length.");
|
||||||
|
unsigned int remaining_length = length.value();
|
||||||
|
|
||||||
|
remaining_length -= length_end_index.value() + 1;
|
||||||
|
file_contents = file_contents.substring_view(length_end_index.value() + 1);
|
||||||
|
|
||||||
|
// Extract the header.
|
||||||
|
StringView header = file_contents.substring_view(0, remaining_length - 1);
|
||||||
|
file_contents = file_contents.substring_view(remaining_length - 1);
|
||||||
|
|
||||||
|
// Ensure that the header ends at the expected location.
|
||||||
|
if (file_contents.length() < 1 || !file_contents.starts_with('\n'))
|
||||||
|
return Error::from_string_literal("Malformed extended header: Header does not end at expected location.");
|
||||||
|
file_contents = file_contents.substring_view(1);
|
||||||
|
|
||||||
|
// Find the delimiting '='.
|
||||||
|
Optional<size_t> header_delimiter_index = header.find('=');
|
||||||
|
if (!header_delimiter_index.has_value())
|
||||||
|
return Error::from_string_literal("Malformed extended header: Header does not have a delimiter.");
|
||||||
|
StringView key = header.substring_view(0, header_delimiter_index.value());
|
||||||
|
StringView value = header.substring_view(header_delimiter_index.value() + 1);
|
||||||
|
|
||||||
|
func(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue