summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibArchive
diff options
context:
space:
mode:
authorKarol Kosek <krkk@serenityos.org>2022-12-25 15:33:30 +0100
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2022-12-27 07:24:07 +0330
commit8238f926fda6dbdaa7131a55965e76179d82f8ba (patch)
tree6002640b78d6e6a6a6363a11109552d113cab6da /Userland/Libraries/LibArchive
parent012645bdf954229fbc4fc9c0b2c6f290cc1330fc (diff)
downloadserenity-8238f926fda6dbdaa7131a55965e76179d82f8ba.zip
LibArchive+Utilities: Port ZipOutputStream to Core::Stream
Diffstat (limited to 'Userland/Libraries/LibArchive')
-rw-r--r--Userland/Libraries/LibArchive/Zip.cpp21
-rw-r--r--Userland/Libraries/LibArchive/Zip.h120
2 files changed, 75 insertions, 66 deletions
diff --git a/Userland/Libraries/LibArchive/Zip.cpp b/Userland/Libraries/LibArchive/Zip.cpp
index bf842d9a0d..45af7cd9ef 100644
--- a/Userland/Libraries/LibArchive/Zip.cpp
+++ b/Userland/Libraries/LibArchive/Zip.cpp
@@ -9,11 +9,6 @@
namespace Archive {
-OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method)
-{
- return stream << to_underlying(method);
-}
-
bool Zip::find_end_of_central_directory_offset(ReadonlyBytes buffer, size_t& offset)
{
for (size_t backwards_offset = 0; backwards_offset <= UINT16_MAX; backwards_offset++) // the file may have a trailing comment of an arbitrary 16 bit length
@@ -108,8 +103,8 @@ bool Zip::for_each_member(Function<IterationDecision(ZipMember const&)> callback
return true;
}
-ZipOutputStream::ZipOutputStream(OutputStream& stream)
- : m_stream(stream)
+ZipOutputStream::ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream> stream)
+ : m_stream(move(stream))
{
}
@@ -119,12 +114,12 @@ static u16 minimum_version_needed(ZipCompressionMethod method)
return method == ZipCompressionMethod::Deflate ? 20 : 10;
}
-void ZipOutputStream::add_member(ZipMember const& member)
+ErrorOr<void> ZipOutputStream::add_member(ZipMember const& member)
{
VERIFY(!m_finished);
VERIFY(member.name.length() <= UINT16_MAX);
VERIFY(member.compressed_data.size() <= UINT32_MAX);
- m_members.append(member);
+ TRY(m_members.try_append(member));
LocalFileHeader local_file_header {
.minimum_version = minimum_version_needed(member.compression_method),
@@ -141,10 +136,10 @@ void ZipOutputStream::add_member(ZipMember const& member)
.extra_data = nullptr,
.compressed_data = member.compressed_data.data(),
};
- local_file_header.write(m_stream);
+ return local_file_header.write(*m_stream);
}
-void ZipOutputStream::finish()
+ErrorOr<void> ZipOutputStream::finish()
{
VERIFY(!m_finished);
m_finished = true;
@@ -175,7 +170,7 @@ void ZipOutputStream::finish()
.comment = nullptr,
};
file_header_offset += sizeof(LocalFileHeader::signature) + (sizeof(LocalFileHeader) - (sizeof(u8*) * 3)) + member.name.length() + member.compressed_data.size();
- central_directory_record.write(m_stream);
+ TRY(central_directory_record.write(*m_stream));
central_directory_size += central_directory_record.size();
}
@@ -189,7 +184,7 @@ void ZipOutputStream::finish()
.comment_length = 0,
.comment = nullptr,
};
- end_of_central_directory.write(m_stream);
+ return end_of_central_directory.write(*m_stream);
}
}
diff --git a/Userland/Libraries/LibArchive/Zip.h b/Userland/Libraries/LibArchive/Zip.h
index 704b46e2cb..4ce3572c53 100644
--- a/Userland/Libraries/LibArchive/Zip.h
+++ b/Userland/Libraries/LibArchive/Zip.h
@@ -11,8 +11,8 @@
#include <AK/DeprecatedString.h>
#include <AK/Function.h>
#include <AK/IterationDecision.h>
-#include <AK/Stream.h>
#include <AK/Vector.h>
+#include <LibCore/Stream.h>
#include <string.h>
namespace Archive {
@@ -55,18 +55,23 @@ struct [[gnu::packed]] EndOfCentralDirectory {
return true;
}
- void write(OutputStream& stream) const
+ ErrorOr<void> write(Core::Stream::Stream& stream) const
{
- stream.write_or_error(signature);
- stream << disk_number;
- stream << central_directory_start_disk;
- stream << disk_records_count;
- stream << total_records_count;
- stream << central_directory_size;
- stream << central_directory_offset;
- stream << comment_length;
+ auto write_value = [&stream](auto value) {
+ return stream.write_entire_buffer({ &value, sizeof(value) });
+ };
+
+ TRY(stream.write_entire_buffer(signature));
+ TRY(write_value(disk_number));
+ TRY(write_value(central_directory_start_disk));
+ TRY(write_value(disk_records_count));
+ TRY(write_value(total_records_count));
+ TRY(write_value(central_directory_size));
+ TRY(write_value(central_directory_offset));
+ TRY(write_value(comment_length));
if (comment_length > 0)
- stream.write_or_error({ comment, comment_length });
+ TRY(stream.write_entire_buffer({ comment, comment_length }));
+ return {};
}
};
@@ -100,8 +105,6 @@ union ZipGeneralPurposeFlags {
};
static_assert(sizeof(ZipGeneralPurposeFlags) == sizeof(u16));
-OutputStream& operator<<(OutputStream& stream, ZipCompressionMethod method);
-
struct [[gnu::packed]] CentralDirectoryRecord {
static constexpr Array<u8, signature_length> signature = { 0x50, 0x4b, 0x01, 0x02 }; // 'PK\x01\x02'
@@ -138,31 +141,36 @@ struct [[gnu::packed]] CentralDirectoryRecord {
return true;
}
- void write(OutputStream& stream) const
+ ErrorOr<void> write(Core::Stream::Stream& stream) const
{
- stream.write_or_error(signature);
- stream << made_by_version;
- stream << minimum_version;
- stream << general_purpose_flags.flags;
- stream << compression_method;
- stream << modification_time;
- stream << modification_date;
- stream << crc32;
- stream << compressed_size;
- stream << uncompressed_size;
- stream << name_length;
- stream << extra_data_length;
- stream << comment_length;
- stream << start_disk;
- stream << internal_attributes;
- stream << external_attributes;
- stream << local_file_header_offset;
+ auto write_value = [&stream](auto value) {
+ return stream.write_entire_buffer({ &value, sizeof(value) });
+ };
+
+ TRY(stream.write_entire_buffer(signature));
+ TRY(write_value(made_by_version));
+ TRY(write_value(minimum_version));
+ TRY(write_value(general_purpose_flags.flags));
+ TRY(write_value(compression_method));
+ TRY(write_value(modification_time));
+ TRY(write_value(modification_date));
+ TRY(write_value(crc32));
+ TRY(write_value(compressed_size));
+ TRY(write_value(uncompressed_size));
+ TRY(write_value(name_length));
+ TRY(write_value(extra_data_length));
+ TRY(write_value(comment_length));
+ TRY(write_value(start_disk));
+ TRY(write_value(internal_attributes));
+ TRY(write_value(external_attributes));
+ TRY(write_value(local_file_header_offset));
if (name_length > 0)
- stream.write_or_error({ name, name_length });
+ TRY(stream.write_entire_buffer({ name, name_length }));
if (extra_data_length > 0)
- stream.write_or_error({ extra_data, extra_data_length });
+ TRY(stream.write_entire_buffer({ extra_data, extra_data_length }));
if (comment_length > 0)
- stream.write_or_error({ comment, comment_length });
+ TRY(stream.write_entire_buffer({ comment, comment_length }));
+ return {};
}
[[nodiscard]] size_t size() const
@@ -202,25 +210,30 @@ struct [[gnu::packed]] LocalFileHeader {
return true;
}
- void write(OutputStream& stream) const
+ ErrorOr<void> write(Core::Stream::Stream& stream) const
{
- stream.write_or_error(signature);
- stream << minimum_version;
- stream << general_purpose_flags.flags;
- stream << compression_method;
- stream << modification_time;
- stream << modification_date;
- stream << crc32;
- stream << compressed_size;
- stream << uncompressed_size;
- stream << name_length;
- stream << extra_data_length;
+ auto write_value = [&stream](auto value) {
+ return stream.write_entire_buffer({ &value, sizeof(value) });
+ };
+
+ TRY(stream.write_entire_buffer(signature));
+ TRY(write_value(minimum_version));
+ TRY(write_value(general_purpose_flags.flags));
+ TRY(write_value(compression_method));
+ TRY(write_value(modification_time));
+ TRY(write_value(modification_date));
+ TRY(write_value(crc32));
+ TRY(write_value(compressed_size));
+ TRY(write_value(uncompressed_size));
+ TRY(write_value(name_length));
+ TRY(write_value(extra_data_length));
if (name_length > 0)
- stream.write_or_error({ name, name_length });
+ TRY(stream.write_entire_buffer({ name, name_length }));
if (extra_data_length > 0)
- stream.write_or_error({ extra_data, extra_data_length });
+ TRY(stream.write_entire_buffer({ extra_data, extra_data_length }));
if (compressed_size > 0)
- stream.write_or_error({ compressed_data, compressed_size });
+ TRY(stream.write_entire_buffer({ compressed_data, compressed_size }));
+ return {};
}
};
@@ -254,12 +267,13 @@ private:
class ZipOutputStream {
public:
- ZipOutputStream(OutputStream&);
- void add_member(ZipMember const&);
- void finish();
+ ZipOutputStream(NonnullOwnPtr<Core::Stream::Stream>);
+
+ ErrorOr<void> add_member(ZipMember const&);
+ ErrorOr<void> finish();
private:
- OutputStream& m_stream;
+ NonnullOwnPtr<Core::Stream::Stream> m_stream;
Vector<ZipMember> m_members;
bool m_finished { false };