diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/common/zstd_compression.cpp | 53 | ||||
| -rw-r--r-- | src/common/zstd_compression.h | 42 |
3 files changed, 98 insertions, 1 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5639021d3..1e8e1b215 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -125,6 +125,8 @@ add_library(common STATIC | |||
| 125 | uint128.h | 125 | uint128.h |
| 126 | vector_math.h | 126 | vector_math.h |
| 127 | web_result.h | 127 | web_result.h |
| 128 | zstd_compression.cpp | ||
| 129 | zstd_compression.h | ||
| 128 | ) | 130 | ) |
| 129 | 131 | ||
| 130 | if(ARCHITECTURE_x86_64) | 132 | if(ARCHITECTURE_x86_64) |
| @@ -138,4 +140,4 @@ endif() | |||
| 138 | create_target_directory_groups(common) | 140 | create_target_directory_groups(common) |
| 139 | 141 | ||
| 140 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) | 142 | target_link_libraries(common PUBLIC Boost::boost fmt microprofile) |
| 141 | target_link_libraries(common PRIVATE lz4_static) | 143 | target_link_libraries(common PRIVATE lz4_static libzstd_static) |
diff --git a/src/common/zstd_compression.cpp b/src/common/zstd_compression.cpp new file mode 100644 index 000000000..60a35c67c --- /dev/null +++ b/src/common/zstd_compression.cpp | |||
| @@ -0,0 +1,53 @@ | |||
| 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 <zstd.h> | ||
| 9 | |||
| 10 | #include "common/assert.h" | ||
| 11 | #include "common/zstd_compression.h" | ||
| 12 | |||
| 13 | namespace Common::Compression { | ||
| 14 | |||
| 15 | std::vector<u8> CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level) { | ||
| 16 | compression_level = std::clamp(compression_level, 1, ZSTD_maxCLevel()); | ||
| 17 | |||
| 18 | const std::size_t max_compressed_size = ZSTD_compressBound(source_size); | ||
| 19 | std::vector<u8> compressed(max_compressed_size); | ||
| 20 | |||
| 21 | const std::size_t compressed_size = | ||
| 22 | ZSTD_compress(compressed.data(), compressed.size(), source, source_size, compression_level); | ||
| 23 | |||
| 24 | if (ZSTD_isError(compressed_size)) { | ||
| 25 | // Compression failed | ||
| 26 | return {}; | ||
| 27 | } | ||
| 28 | |||
| 29 | compressed.resize(compressed_size); | ||
| 30 | |||
| 31 | return compressed; | ||
| 32 | } | ||
| 33 | |||
| 34 | std::vector<u8> CompressDataZSTDDefault(const u8* source, std::size_t source_size) { | ||
| 35 | return CompressDataZSTD(source, source_size, ZSTD_CLEVEL_DEFAULT); | ||
| 36 | } | ||
| 37 | |||
| 38 | std::vector<u8> DecompressDataZSTD(const std::vector<u8>& compressed) { | ||
| 39 | const std::size_t decompressed_size = | ||
| 40 | ZSTD_getDecompressedSize(compressed.data(), compressed.size()); | ||
| 41 | std::vector<u8> decompressed(decompressed_size); | ||
| 42 | |||
| 43 | const std::size_t uncompressed_result_size = ZSTD_decompress( | ||
| 44 | decompressed.data(), decompressed.size(), compressed.data(), compressed.size()); | ||
| 45 | |||
| 46 | if (decompressed_size != uncompressed_result_size || ZSTD_isError(uncompressed_result_size)) { | ||
| 47 | // Decompression failed | ||
| 48 | return {}; | ||
| 49 | } | ||
| 50 | return decompressed; | ||
| 51 | } | ||
| 52 | |||
| 53 | } // namespace Common::Compression | ||
diff --git a/src/common/zstd_compression.h b/src/common/zstd_compression.h new file mode 100644 index 000000000..e0a64b035 --- /dev/null +++ b/src/common/zstd_compression.h | |||
| @@ -0,0 +1,42 @@ | |||
| 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 | |||
| 9 | namespace Common::Compression { | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Compresses a source memory region with Zstandard 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 | * @param compression_level the used compression level. Should be between 1 and 22. | ||
| 17 | * | ||
| 18 | * @return the compressed data. | ||
| 19 | */ | ||
| 20 | std::vector<u8> CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level); | ||
| 21 | |||
| 22 | /** | ||
| 23 | * Compresses a source memory region with Zstandard with the default compression level and returns | ||
| 24 | * the compressed data in a vector. | ||
| 25 | * | ||
| 26 | * @param source the uncompressed source memory region. | ||
| 27 | * @param source_size the size in bytes of the uncompressed source memory region. | ||
| 28 | * | ||
| 29 | * @return the compressed data. | ||
| 30 | */ | ||
| 31 | std::vector<u8> CompressDataZSTDDefault(const u8* source, std::size_t source_size); | ||
| 32 | |||
| 33 | /** | ||
| 34 | * Decompresses a source memory region with Zstandard and returns the uncompressed data in a vector. | ||
| 35 | * | ||
| 36 | * @param compressed the compressed source memory region. | ||
| 37 | * | ||
| 38 | * @return the decompressed data. | ||
| 39 | */ | ||
| 40 | std::vector<u8> DecompressDataZSTD(const std::vector<u8>& compressed); | ||
| 41 | |||
| 42 | } // namespace Common::Compression \ No newline at end of file | ||