HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
zlib.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../file/file.hpp"
8#include "../container/module.hpp"
9#include "../parser/parser.hpp"
10#include "../macros.hpp"
11#include "inflate.hpp"
12#include <cstddef>
13#include <filesystem>
14
15hi_export_module(hikogui.codec.zlib);
16
17namespace hi { inline namespace v1 {
18
19[[nodiscard]] inline bstring zlib_decompress(std::span<std::byte const> bytes, std::size_t max_size)
20{
21 struct zlib_header {
22 uint8_t CMF;
23 uint8_t FLG;
24 };
25
26 auto offset = 0_uz;
27
29
30 hilet header_chksum = header->CMF * 256 + header->FLG;
31 hi_check(header_chksum % 31 == 0, "zlib header checksum failed.");
32
33 hi_check((header->CMF & 0xf) == 8, "zlib compression method must be 8");
34 hi_check(((header->CMF >> 4) & 0xf) <= 7, "zlib LZ77 window too large");
35 hi_check((header->FLG & 0x20) == 0, "zlib must not use a preset dictionary");
36
37 if (header->FLG & 0x20) {
39 }
40
41 auto r = inflate(bytes, offset, max_size);
42
44
45 return r;
46}
47
48[[nodiscard]] inline bstring zlib_decompress(std::filesystem::path const& path, std::size_t max_size = 0x01000000)
49{
50 return zlib_decompress(as_span<std::byte const>(file_view(path)), max_size);
51}
52
53}} // namespace hi::v1
Defines the file class.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
hi_export bstring inflate(std::span< std::byte const > bytes, std::size_t &offset, std::size_t max_size=0x0100 '0000)
Inflate compressed data using the deflate algorithm bytes should include at least 32 bit of trailer,...
Definition inflate.hpp:358
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377