1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:07:46 +00:00

LibCore+LibHTTP+LibGfx: Switch to LibCompress

This commit removes the only 3rd party library (and its usages)
in serenity: puff, which is used for deflate decompression. and
replaces it with the existing original serenity implementation
in LibCompress. :^)
This commit is contained in:
Idan Horowitz 2021-03-03 23:54:07 +02:00 committed by Andreas Kling
parent 373a595c56
commit c12781a6a2
16 changed files with 31 additions and 1121 deletions

View file

@ -28,7 +28,7 @@
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <LibCore/puff.h>
#include <LibCompress/Gzip.h>
#include <LibGfx/PNGLoader.h>
#include <fcntl.h>
#include <math.h>
@ -39,6 +39,7 @@
#include <unistd.h>
#ifdef __serenity__
# include <LibCompress/Deflate.h>
# include <serenity.h>
#endif
@ -120,8 +121,7 @@ struct PNGLoadingContext {
bool has_alpha() const { return color_type & 4 || palette_transparency_data.size() > 0; }
Vector<Scanline> scanlines;
RefPtr<Gfx::Bitmap> bitmap;
u8* decompression_buffer { nullptr };
size_t decompression_buffer_size { 0 };
ByteBuffer decompression_buffer;
Vector<u8> compressed_data;
Vector<PaletteEntry> palette_data;
Vector<u8> palette_transparency_data;
@ -603,7 +603,7 @@ static bool decode_png_chunks(PNGLoadingContext& context)
static bool decode_png_bitmap_simple(PNGLoadingContext& context)
{
Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size());
for (int y = 0; y < context.height; ++y) {
u8 filter;
@ -749,7 +749,7 @@ static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, in
static bool decode_png_adam7(PNGLoadingContext& context)
{
Streamer streamer(context.decompression_buffer, context.decompression_buffer_size);
Streamer streamer(context.decompression_buffer.data(), context.decompression_buffer.size());
context.bitmap = Bitmap::create_purgeable(context.has_alpha() ? BitmapFormat::RGBA32 : BitmapFormat::RGB32, { context.width, context.height });
if (!context.bitmap)
return false;
@ -777,25 +777,12 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
if (context.color_type == 3 && context.palette_data.is_empty())
return false; // Didn't see a PLTE chunk for a palettized image, or it was empty.
unsigned long srclen = context.compressed_data.size() - 6;
unsigned long destlen = 0;
int ret = puff(nullptr, &destlen, context.compressed_data.data() + 2, &srclen);
if (ret != 0) {
context.state = PNGLoadingContext::State::Error;
return false;
}
context.decompression_buffer_size = destlen;
#ifdef __serenity__
context.decompression_buffer = (u8*)mmap_with_name(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "PNG decompression buffer");
#else
context.decompression_buffer = (u8*)mmap(nullptr, context.decompression_buffer_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
#endif
ret = puff(context.decompression_buffer, &destlen, context.compressed_data.data() + 2, &srclen);
if (ret != 0) {
auto result = Compress::DeflateDecompressor::decompress_all(context.compressed_data.span().slice(2));
if (!result.has_value()) {
context.state = PNGLoadingContext::State::Error;
return false;
}
context.decompression_buffer = result.value();
context.compressed_data.clear();
context.scanlines.ensure_capacity(context.height);
@ -812,9 +799,7 @@ static bool decode_png_bitmap(PNGLoadingContext& context)
VERIFY_NOT_REACHED();
}
munmap(context.decompression_buffer, context.decompression_buffer_size);
context.decompression_buffer = nullptr;
context.decompression_buffer_size = 0;
context.decompression_buffer.clear();
context.state = PNGLoadingContext::State::BitmapDecoded;
return true;