diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/common/data_compression.cpp | 46 | ||||
| -rw-r--r-- | src/common/data_compression.h | 17 |
3 files changed, 66 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 850ce8006..43834eb85 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -79,6 +79,8 @@ add_library(common STATIC | |||
| 79 | common_funcs.h | 79 | common_funcs.h |
| 80 | common_paths.h | 80 | common_paths.h |
| 81 | common_types.h | 81 | common_types.h |
| 82 | data_compression.cpp | ||
| 83 | data_compression.h | ||
| 82 | file_util.cpp | 84 | file_util.cpp |
| 83 | file_util.h | 85 | file_util.h |
| 84 | hash.h | 86 | hash.h |
| @@ -136,3 +138,4 @@ endif() | |||
| 136 | create_target_directory_groups(common) | 138 | create_target_directory_groups(common) |
| 137 | 139 | ||
| 138 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) | 140 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) |
| 141 | target_link_libraries(common PRIVATE lz4_static) | ||
diff --git a/src/common/data_compression.cpp b/src/common/data_compression.cpp new file mode 100644 index 000000000..624b8ac47 --- /dev/null +++ b/src/common/data_compression.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 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 <lz4.h> | ||
| 8 | |||
| 9 | #include "data_compression.h" | ||
| 10 | |||
| 11 | namespace Compression { | ||
| 12 | |||
| 13 | |||
| 14 | std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size) { | ||
| 15 | if (source_size > LZ4_MAX_INPUT_SIZE) { | ||
| 16 | // Source size exceeds LZ4 maximum input size | ||
| 17 | return {}; | ||
| 18 | } | ||
| 19 | const auto source_size_int = static_cast<int>(source_size); | ||
| 20 | const int max_compressed_size = LZ4_compressBound(source_size_int); | ||
| 21 | std::vector<u8> compressed(max_compressed_size); | ||
| 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 | if (compressed_size <= 0) { | ||
| 26 | // Compression failed | ||
| 27 | return {}; | ||
| 28 | } | ||
| 29 | compressed.resize(compressed_size); | ||
| 30 | return compressed; | ||
| 31 | } | ||
| 32 | |||
| 33 | std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size) { | ||
| 34 | std::vector<u8> uncompressed(uncompressed_size); | ||
| 35 | const int size_check = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed.data()), | ||
| 36 | reinterpret_cast<char*>(uncompressed.data()), | ||
| 37 | static_cast<int>(compressed.size()), | ||
| 38 | static_cast<int>(uncompressed.size())); | ||
| 39 | if (static_cast<int>(uncompressed_size) != size_check) { | ||
| 40 | // Decompression failed | ||
| 41 | return {}; | ||
| 42 | } | ||
| 43 | return uncompressed; | ||
| 44 | } | ||
| 45 | |||
| 46 | } // namespace Compression | ||
diff --git a/src/common/data_compression.h b/src/common/data_compression.h new file mode 100644 index 000000000..70878b8d9 --- /dev/null +++ b/src/common/data_compression.h | |||
| @@ -0,0 +1,17 @@ | |||
| 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 <vector> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace Compression { | ||
| 12 | |||
| 13 | std::vector<u8> CompressDataLZ4(const u8* source, std::size_t source_size); | ||
| 14 | |||
| 15 | std::vector<u8> DecompressDataLZ4(const std::vector<u8>& compressed, std::size_t uncompressed_size); | ||
| 16 | |||
| 17 | } // namespace Compression \ No newline at end of file | ||