-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompression.cpp
48 lines (36 loc) · 1.26 KB
/
compression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "prism/compression.h"
#include <zstd.h>
#include "prism/log.h"
#include "prism/system.h"
#include "prism/memoryhandler.h"
namespace prism {
static const int COMPRESSION_BUFFER = 400;
void compressBufferZSTD(Buffer* tBuffer)
{
if (!tBuffer->mIsOwned) {
logError("Unable to compress unowned Buffer");
recoverFromError();
}
char* src = (char*)tBuffer->mData;
auto dstBufferSize = size_t(tBuffer->mLength + COMPRESSION_BUFFER);
char* dst = (char*)allocMemory(int(dstBufferSize));
auto dstLength = ZSTD_compress(dst, dstBufferSize, src, tBuffer->mLength, 1);
dst = (char*)reallocMemory(dst, int(dstLength));
freeBuffer(*tBuffer);
*tBuffer = makeBufferOwned(dst, int(dstLength));
}
void decompressBufferZSTD(Buffer* tBuffer)
{
if (!tBuffer->mIsOwned) {
logError("Unable to decompress unowned Buffer");
recoverFromError();
}
char* src = (char*)tBuffer->mData;
size_t uncompressedLength = (size_t)ZSTD_getFrameContentSize(src, tBuffer->mLength);
char* dst = (char*)allocMemory(int(uncompressedLength));
auto dstLength = ZSTD_decompress(dst, uncompressedLength, src, size_t(tBuffer->mLength));
dst = (char*)reallocMemory(dst, int(dstLength));
freeBuffer(*tBuffer);
*tBuffer = makeBufferOwned(dst, uint32_t(dstLength));
}
}