summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-05-07 23:40:02 +0300
committerAndreas Kling <kling@serenityos.org>2021-05-07 23:22:07 +0200
commit2119e0fb3f8ace00f395e61eeb10222b7e78f899 (patch)
treef1848cbced75d75f277e8a6c9cb64230e8a45b00 /Userland
parent1b6824d2962b534b34b2d3ec6ba3ab5f77bc68b5 (diff)
downloadserenity-2119e0fb3f8ace00f395e61eeb10222b7e78f899.zip
LibCompress: Add a method that describes a Gzip Header
This can be used to display information about a gzip file that is stored in it's header.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCompress/Gzip.cpp14
-rw-r--r--Userland/Libraries/LibCompress/Gzip.h1
2 files changed, 15 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCompress/Gzip.cpp b/Userland/Libraries/LibCompress/Gzip.cpp
index 70ad0bcff3..9d62505c2a 100644
--- a/Userland/Libraries/LibCompress/Gzip.cpp
+++ b/Userland/Libraries/LibCompress/Gzip.cpp
@@ -9,6 +9,7 @@
#include <AK/MemoryStream.h>
#include <AK/String.h>
+#include <LibCore/DateTime.h>
namespace Compress {
@@ -140,6 +141,19 @@ size_t GzipDecompressor::read(Bytes bytes)
return total_read;
}
+Optional<String> GzipDecompressor::describe_header(ReadonlyBytes bytes)
+{
+ if (bytes.size() < sizeof(BlockHeader))
+ return {};
+
+ auto& header = *(reinterpret_cast<const BlockHeader*>(bytes.data()));
+ if (!header.valid_magic_number() || !header.supported_by_implementation())
+ return {};
+
+ LittleEndian<u32> original_size = *reinterpret_cast<const u32*>(bytes.offset(bytes.size() - sizeof(u32)));
+ return String::formatted("last modified: {}, original size {}", Core::DateTime::from_timestamp(header.modification_time).to_string(), (u32)original_size);
+}
+
bool GzipDecompressor::read_or_error(Bytes bytes)
{
if (read(bytes) < bytes.size()) {
diff --git a/Userland/Libraries/LibCompress/Gzip.h b/Userland/Libraries/LibCompress/Gzip.h
index 8d0f9973ae..635ba99f9e 100644
--- a/Userland/Libraries/LibCompress/Gzip.h
+++ b/Userland/Libraries/LibCompress/Gzip.h
@@ -50,6 +50,7 @@ public:
bool handle_any_error() override;
static Optional<ByteBuffer> decompress_all(ReadonlyBytes);
+ static Optional<String> describe_header(ReadonlyBytes);
static bool is_likely_compressed(ReadonlyBytes bytes);
private: