srctree

Robin Linden parent 05ff1c6a b961d792
archive: Move the zlib wrapper from //engine to a new library

inlinesplit
.clang-tidy added: 99, removed: 60, total 39
@@ -69,7 +69,7 @@ Checks: >
 
WarningsAsErrors: "*"
 
HeaderFilterRegex: "\\./(browser|css|css2|dom|dom2|engine|etest|geom|gfx|html|html2|img|js|layout|net|os|protocol|render|style|tui|uri|url|util|wasm)/"
HeaderFilterRegex: "\\./(archive|browser|css|css2|dom|dom2|engine|etest|geom|gfx|html|html2|img|js|layout|net|os|protocol|render|style|tui|uri|url|util|wasm)/"
 
CheckOptions:
# readability-identifier-naming
 
.gitlint added: 99, removed: 60, total 39
@@ -4,4 +4,4 @@ ignore=body-is-missing
# TODO(robinlinden): Better way of documenting and setting this up.
# Each commit must start with the main area it affects.
[title-match-regex]
regex=^(browser|bzl|css|css2|dom|dom2|engine|etest|geom|gfx|html|html2|img|js|layout|net|os|protocol|render|style|tui|uri|url|util|wasm|all|build|ci|deps|doc|meta)(/.*|\+.*)?:
regex=^(archive|browser|bzl|css|css2|dom|dom2|engine|etest|geom|gfx|html|html2|img|js|layout|net|os|protocol|render|style|tui|uri|url|util|wasm|all|build|ci|deps|doc|meta)(/.*|\+.*)?:
 
filename was Deleted added: 99, removed: 60, total 39
@@ -0,0 +1,14 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//bzl:copts.bzl", "HASTUR_COPTS")
 
cc_library(
name = "archive",
srcs = glob(["*.cpp"]),
hdrs = glob(["*.h"]),
copts = HASTUR_COPTS,
visibility = ["//visibility:public"],
deps = [
"@spdlog",
"@zlib",
],
)
 
filename was Deleted added: 99, removed: 60, total 39
@@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "archive/zlib.h"
 
#include <spdlog/spdlog.h>
#include <zlib.h>
 
namespace archive {
 
std::optional<std::string> zlib_decode(std::string_view data) {
z_stream s{
.next_in = reinterpret_cast<Bytef const *>(data.data()),
.avail_in = static_cast<uInt>(data.size()),
};
 
// https://github.com/madler/zlib/blob/v1.2.13/zlib.h#L832
// The windowBits parameter is the base two logarithm of the
// maximum window size (the size of the history buffer). It
// should be in the range 8..15 for this version of the library.
// <...>
// windowBits can also be greater than 15 for optional gzip
// decoding. Add 32 to windowBits to enable zlib and gzip
// decoding with automatic header detection, or add 16 to decode
// only the gzip format <...>.
constexpr int kWindowBits = 15;
constexpr int kEnableGzip = 32;
if (inflateInit2(&s, kWindowBits + kEnableGzip) != Z_OK) {
return std::nullopt;
}
 
std::string out{};
std::string buf{};
constexpr auto kZlibInflateChunkSize = std::size_t{64} * 1024; // Chosen by a fair dice roll.
buf.resize(kZlibInflateChunkSize);
do {
s.next_out = reinterpret_cast<Bytef *>(buf.data());
s.avail_out = static_cast<uInt>(buf.size());
int ret = inflate(&s, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
spdlog::error("Error '{}: {}' during zlib inflation", ret, s.msg);
inflateEnd(&s);
return std::nullopt;
}
 
uInt inflated_bytes = static_cast<uInt>(buf.size()) - s.avail_out;
out += buf.substr(0, inflated_bytes);
} while (s.avail_out == 0);
 
inflateEnd(&s);
return out;
}
 
} // namespace archive
 
filename was Deleted added: 99, removed: 60, total 39
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#ifndef ARCHIVE_ZLIB_H_
#define ARCHIVE_ZLIB_H_
 
#include <optional>
#include <string>
#include <string_view>
 
namespace archive {
 
std::optional<std::string> zlib_decode(std::string_view);
 
} // namespace archive
 
#endif
 
engine/BUILD added: 99, removed: 60, total 39
@@ -8,6 +8,7 @@ cc_library(
copts = HASTUR_COPTS,
visibility = ["//visibility:public"],
deps = [
"//archive",
"//css",
"//dom",
"//html",
@@ -16,7 +17,6 @@ cc_library(
"//style",
"//uri",
"@spdlog",
"@zlib",
],
)
 
 
engine/engine.cpp added: 99, removed: 60, total 39
@@ -4,69 +4,21 @@
 
#include "engine/engine.h"
 
#include "archive/zlib.h"
#include "css/default.h"
#include "css/parser.h"
#include "html/parser.h"
#include "style/style.h"
 
#include <spdlog/spdlog.h>
#include <zlib.h>
 
#include <future>
#include <iterator>
#include <optional>
#include <string_view>
#include <utility>
 
using namespace std::literals;
 
namespace engine {
namespace {
 
std::optional<std::string> zlib_decode(std::string_view data) {
z_stream s{
.next_in = reinterpret_cast<Bytef const *>(data.data()),
.avail_in = static_cast<uInt>(data.size()),
};
 
// https://github.com/madler/zlib/blob/v1.2.13/zlib.h#L832
// The windowBits parameter is the base two logarithm of the
// maximum window size (the size of the history buffer). It
// should be in the range 8..15 for this version of the library.
// <...>
// windowBits can also be greater than 15 for optional gzip
// decoding. Add 32 to windowBits to enable zlib and gzip
// decoding with automatic header detection, or add 16 to decode
// only the gzip format <...>.
constexpr int kWindowBits = 15;
constexpr int kEnableGzip = 32;
if (inflateInit2(&s, kWindowBits + kEnableGzip) != Z_OK) {
return std::nullopt;
}
 
std::string out{};
std::string buf{};
constexpr auto kZlibInflateChunkSize = std::size_t{64} * 1024; // Chosen by a fair dice roll.
buf.resize(kZlibInflateChunkSize);
do {
s.next_out = reinterpret_cast<Bytef *>(buf.data());
s.avail_out = static_cast<uInt>(buf.size());
int ret = inflate(&s, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
spdlog::error("Error '{}: {}' during zlib inflation", ret, s.msg);
inflateEnd(&s);
return std::nullopt;
}
 
uInt inflated_bytes = static_cast<uInt>(buf.size()) - s.avail_out;
out += buf.substr(0, inflated_bytes);
} while (s.avail_out == 0);
 
inflateEnd(&s);
return out;
}
 
} // namespace
 
protocol::Error Engine::navigate(uri::Uri uri) {
auto is_redirect = [](int status_code) {
@@ -160,7 +112,7 @@ void Engine::on_navigation_success() {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives
auto encoding = style_data.headers.get("Content-Encoding");
if (encoding == "gzip" || encoding == "x-gzip") {
auto decoded = zlib_decode(style_data.body);
auto decoded = archive::zlib_decode(style_data.body);
if (!decoded) {
spdlog::error("Failed {}-decoding of '{}'", *encoding, stylesheet_url.uri);
return {};