Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/util/compress_helpers.h
4223 views
1
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#pragma once
5
6
#include "common/heap_array.h"
7
8
#include <optional>
9
#include <span>
10
11
class Error;
12
13
namespace CompressHelpers {
14
enum class CompressType
15
{
16
Uncompressed,
17
Deflate,
18
Zstandard,
19
XZ,
20
Count
21
};
22
23
using ByteBuffer = DynamicHeapArray<u8>;
24
using OptionalByteBuffer = std::optional<ByteBuffer>;
25
26
std::optional<size_t> GetDecompressedSize(CompressType type, std::span<const u8> data, Error* error = nullptr);
27
std::optional<size_t> DecompressBuffer(std::span<u8> dst, CompressType type, std::span<const u8> data,
28
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
29
OptionalByteBuffer DecompressBuffer(CompressType type, std::span<const u8> data,
30
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
31
OptionalByteBuffer DecompressBuffer(CompressType type, OptionalByteBuffer data,
32
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
33
bool DecompressBuffer(ByteBuffer& dst, CompressType type, std::span<const u8> data,
34
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
35
OptionalByteBuffer DecompressFile(std::string_view path, std::span<const u8> data,
36
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
37
OptionalByteBuffer DecompressFile(std::string_view path, OptionalByteBuffer data,
38
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
39
OptionalByteBuffer DecompressFile(const char* path, std::optional<size_t> decompressed_size = std::nullopt,
40
Error* error = nullptr);
41
OptionalByteBuffer DecompressFile(CompressType type, const char* path,
42
std::optional<size_t> decompressed_size = std::nullopt, Error* error = nullptr);
43
44
OptionalByteBuffer CompressToBuffer(CompressType type, const void* data, size_t data_size, int clevel = -1,
45
Error* error = nullptr);
46
OptionalByteBuffer CompressToBuffer(CompressType type, std::span<const u8> data, int clevel = -1,
47
Error* error = nullptr);
48
OptionalByteBuffer CompressToBuffer(CompressType type, OptionalByteBuffer data, int clevel = -1,
49
Error* error = nullptr);
50
bool CompressToBuffer(ByteBuffer& dst, CompressType type, std::span<const u8> data, int clevel = -1,
51
Error* error = nullptr);
52
bool CompressToBuffer(ByteBuffer& dst, CompressType type, ByteBuffer data, int clevel = -1, Error* error = nullptr);
53
bool CompressToFile(const char* path, std::span<const u8> data, int clevel = -1, bool atomic_write = true,
54
Error* error = nullptr);
55
bool CompressToFile(CompressType type, const char* path, std::span<const u8> data, int clevel = -1,
56
bool atomic_write = true, Error* error = nullptr);
57
58
const char* SZErrorToString(int res);
59
60
} // namespace CompressHelpers
61
62