summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt3
-rw-r--r--src/common/lz4_compression.cpp78
-rw-r--r--src/common/lz4_compression.h55
3 files changed, 136 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 850ce8006..5639021d3 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -91,6 +91,8 @@ add_library(common STATIC
91 logging/log.h 91 logging/log.h
92 logging/text_formatter.cpp 92 logging/text_formatter.cpp
93 logging/text_formatter.h 93 logging/text_formatter.h
94 lz4_compression.cpp
95 lz4_compression.h
94 math_util.h 96 math_util.h
95 memory_hook.cpp 97 memory_hook.cpp
96 memory_hook.h 98 memory_hook.h
@@ -136,3 +138,4 @@ endif()
136create_target_directory_groups(common) 138create_target_directory_groups(common)
137 139
138target_link_libraries(common PUBLIC Boost::boost fmt microprofile) 140target_link_libraries(common PUBLIC Boost::boost fmt microprofile)
141target_link_libraries(common PRIVATE lz4_static)
diff --git a/src/common/lz4_compression.cpp b/src/common/lz4_compression.cpp
new file mode 100644
index 000000000..dc9b4a916
--- /dev/null
+++ b/src/common/lz4_compression.cpp
@@ -0,0 +1,78 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <lz4hc.h>
9
10#include "common/assert.h"
11#include "common/lz4_compression.h"
12
13namespace Common::Compression {
14
15std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size) {
16 ASSERT_MSG(source_size <= LZ4_MAX_INPUT_SIZE, "Source size exceeds LZ4 maximum input size");
17
18 const auto source_size_int = static_cast<int>(source_size);
19 const int max_compressed_size = LZ4_compressBound(source_size_int);
20 std::vector<u8> compressed(max_compressed_size);
21
22 const int compressed_size = LZ4_compress_default(reinterpret_cast<const char*>(source),
23 reinterpret_cast<char*>(compressed.data()),
24 source_size_int, max_compressed_size);
25
26 if (compressed_size <= 0) {
27 // Compression failed
28 return {};
29 }
30
31 compressed.resize(compressed_size);
32
33 return compressed;
34}
35
36std::vector<u8> CompressDataLZ4HC(const u8* source, std::size_t source_size,
37 s32 compression_level) {
38 ASSERT_MSG(source_size <= LZ4_MAX_INPUT_SIZE, "Source size exceeds LZ4 maximum input size");
39
40 compression_level = std::clamp(compression_level, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
41
42 const auto source_size_int = static_cast<int>(source_size);
43 const int max_compressed_size = LZ4_compressBound(source_size_int);
44 std::vector<u8> compressed(max_compressed_size);
45
46 const int compressed_size = LZ4_compress_HC(
47 reinterpret_cast<const char*>(source), reinterpret_cast<char*>(compressed.data()),
48 source_size_int, max_compressed_size, compression_level);
49
50 if (compressed_size <= 0) {
51 // Compression failed
52 return {};
53 }
54
55 compressed.resize(compressed_size);
56
57 return compressed;
58}
59
60std::vector<u8> CompressDataLZ4HCMax(const u8* source, std::size_t source_size) {
61 return CompressDataLZ4HC(source, source_size, LZ4HC_CLEVEL_MAX);
62}
63
64std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed,
65 std::size_t uncompressed_size) {
66 std::vector<u8> uncompressed(uncompressed_size);
67 const int size_check = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed.data()),
68 reinterpret_cast<char*>(uncompressed.data()),
69 static_cast<int>(compressed.size()),
70 static_cast<int>(uncompressed.size()));
71 if (static_cast<int>(uncompressed_size) != size_check) {
72 // Decompression failed
73 return {};
74 }
75 return uncompressed;
76}
77
78} // namespace Common::Compression
diff --git a/src/common/lz4_compression.h b/src/common/lz4_compression.h
new file mode 100644
index 000000000..fe2231a6c
--- /dev/null
+++ b/src/common/lz4_compression.h
@@ -0,0 +1,55 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <vector>
6
7#include "common/common_types.h"
8
9namespace Common::Compression {
10
11/**
12 * Compresses a source memory region with LZ4 and returns the compressed data in a vector.
13 *
14 * @param source the uncompressed source memory region.
15 * @param source_size the size in bytes of the uncompressed source memory region.
16 *
17 * @return the compressed data.
18 */
19std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size);
20
21/**
22 * Utilizes the LZ4 subalgorithm LZ4HC with the specified compression level. Higher compression
23 * levels result in a smaller compressed size, but require more CPU time for compression. The
24 * compression level has almost no impact on decompression speed. Data compressed with LZ4HC can
25 * also be decompressed with the default LZ4 decompression.
26 *
27 * @param source the uncompressed source memory region.
28 * @param source_size the size in bytes of the uncompressed source memory region.
29 * @param compression_level the used compression level. Should be between 3 and 12.
30 *
31 * @return the compressed data.
32 */
33std::vector<u8> CompressDataLZ4HC(const u8* source, std::size_t source_size, s32 compression_level);
34
35/**
36 * Utilizes the LZ4 subalgorithm LZ4HC with the highest possible compression level.
37 *
38 * @param source the uncompressed source memory region.
39 * @param source_size the size in bytes of the uncompressed source memory region.
40 *
41 * @return the compressed data.
42 */
43std::vector<u8> CompressDataLZ4HCMax(const u8* source, std::size_t source_size);
44
45/**
46 * Decompresses a source memory region with LZ4 and returns the uncompressed data in a vector.
47 *
48 * @param compressed the compressed source memory region.
49 * @param uncompressed_size the size in bytes of the uncompressed data.
50 *
51 * @return the decompressed data.
52 */
53std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size);
54
55} // namespace Common::Compression \ No newline at end of file