Files
jak-project/common/util/compress.cpp
T
water111 f0ceea8b2e [sparticle] 2d hud particles (#849)
* wip, taking a break to work on asm stuff first

* the goal code for sparticle

* mips2c the first sparticle asm function

* temp

* particle processing no longer crashing

* temp

* working texture cache for vi1 and hud textures

* sprites

* cleanup 1

* temp

* temp

* add zstd library

* temp

* working

* tests

* include fix

* uncomment

* better decomp of sparticle stuff, part 1

* update references
2021-09-26 11:41:58 -04:00

49 lines
1.5 KiB
C++

#include <cstring>
#include <cstdio>
#include "compress.h"
#include "third-party/zstd/lib/zstd.h"
#include "common/util/assert.h"
namespace compression {
/*!
* Compress data with zstd. There is an 8-byte header containing the decompressed data's size.
*/
std::vector<u8> compress_zstd(const void* data, size_t size) {
auto max_compressed = ZSTD_compressBound(size);
std::vector<u8> result(sizeof(size_t) + max_compressed);
memcpy(result.data(), &size, sizeof(size_t));
auto compressed_size =
ZSTD_compress(result.data() + sizeof(size_t), max_compressed, data, size, 1);
if (ZSTD_isError(compressed_size)) {
printf("ZSTD error: %s\n", ZSTD_getErrorName(compressed_size));
assert(false);
}
result.resize(sizeof(size_t) + compressed_size);
return result;
}
/*!
* Decompress data with zstd. The first 8-bytes of the data should be a header containing the
* decompressed data's size.
*/
std::vector<u8> decompress_zstd(const void* data, size_t size) {
assert(size >= sizeof(size_t));
size_t decompressed_size;
memcpy(&decompressed_size, data, sizeof(size_t));
size_t compressed_size = size - sizeof(size_t);
std::vector<u8> result(decompressed_size);
auto decomp_size = ZSTD_decompress(result.data(), decompressed_size,
(const u8*)data + sizeof(size_t), compressed_size);
if (ZSTD_isError(decomp_size)) {
printf("ZSTD error: %s\n", ZSTD_getErrorName(compressed_size));
assert(false);
}
assert(decomp_size == decompressed_size);
return result;
}
} // namespace compression