/
decompress.c
187 lines (152 loc) · 7.1 KB
/
decompress.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***********************************************************************************************************************************
ZST Decompress
***********************************************************************************************************************************/
#include "build.auto.h"
#ifdef HAVE_LIBZST
#include <zstd.h>
#include "common/compress/zst/common.h"
#include "common/compress/zst/decompress.h"
#include "common/debug.h"
#include "common/io/filter/filter.h"
#include "common/log.h"
#include "common/type/object.h"
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
typedef struct ZstDecompress
{
ZSTD_DStream *context; // Decompression context
IoFilter *filter; // Filter interface
bool inputSame; // Is the same input required on the next process call?
size_t inputOffset; // Current offset in input buffer
bool frameDone; // Has the current frame completed?
bool done; // Is decompression done?
} ZstDecompress;
/***********************************************************************************************************************************
Render as string for logging
***********************************************************************************************************************************/
static void
zstDecompressToLog(const ZstDecompress *const this, StringStatic *const debugLog)
{
strStcFmt(
debugLog, "{inputSame: %s, inputOffset: %zu, frameDone %s, done: %s}", cvtBoolToConstZ(this->inputSame), this->inputOffset,
cvtBoolToConstZ(this->frameDone), cvtBoolToConstZ(this->done));
}
#define FUNCTION_LOG_ZST_DECOMPRESS_TYPE \
ZstDecompress *
#define FUNCTION_LOG_ZST_DECOMPRESS_FORMAT(value, buffer, bufferSize) \
FUNCTION_LOG_OBJECT_FORMAT(value, zstDecompressToLog, buffer, bufferSize)
/***********************************************************************************************************************************
Free decompression context
***********************************************************************************************************************************/
static void
zstDecompressFreeResource(THIS_VOID)
{
THIS(ZstDecompress);
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(ZST_DECOMPRESS, this);
FUNCTION_LOG_END();
ASSERT(this != NULL);
ZSTD_freeDStream(this->context);
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Decompress data
***********************************************************************************************************************************/
static void
zstDecompressProcess(THIS_VOID, const Buffer *compressed, Buffer *decompressed)
{
THIS(ZstDecompress);
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(ZST_DECOMPRESS, this);
FUNCTION_LOG_PARAM(BUFFER, compressed);
FUNCTION_LOG_PARAM(BUFFER, decompressed);
FUNCTION_LOG_END();
ASSERT(this != NULL);
ASSERT(this->context != NULL);
ASSERT(decompressed != NULL);
// When there is no more input then decompression is done
if (compressed == NULL)
{
// If the current frame being decompressed was not completed then error
if (!this->frameDone)
THROW(FormatError, "unexpected eof in compressed data");
this->done = true;
}
else
{
// Initialize input/output buffer
ZSTD_inBuffer in = {.src = bufPtrConst(compressed) + this->inputOffset, .size = bufUsed(compressed) - this->inputOffset};
ZSTD_outBuffer out = {.dst = bufRemainsPtr(decompressed), .size = bufRemains(decompressed)};
// Perform decompression. Track frame done so we can detect unexpected EOF.
this->frameDone = zstError(ZSTD_decompressStream(this->context, &out, &in)) == 0;
bufUsedInc(decompressed, out.pos);
// If the input buffer was not entirely consumed then set inputSame and store the offset where processing will restart
if (in.pos < in.size)
{
// Output buffer should be completely full
ASSERT(out.pos == out.size);
this->inputSame = true;
this->inputOffset += in.pos;
}
// Else ready for more input
else
{
this->inputOffset = 0;
this->inputSame = false;
}
}
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Is decompress done?
***********************************************************************************************************************************/
static bool
zstDecompressDone(const THIS_VOID)
{
THIS(const ZstDecompress);
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(ZST_DECOMPRESS, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
FUNCTION_TEST_RETURN(BOOL, this->done);
}
/***********************************************************************************************************************************
Is the same input required on the next process call?
***********************************************************************************************************************************/
static bool
zstDecompressInputSame(const THIS_VOID)
{
THIS(const ZstDecompress);
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(ZST_DECOMPRESS, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
FUNCTION_TEST_RETURN(BOOL, this->inputSame);
}
/**********************************************************************************************************************************/
FN_EXTERN IoFilter *
zstDecompressNew(const bool raw)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
(void)raw; // Raw unsupported
FUNCTION_LOG_END();
OBJ_NEW_BEGIN(ZstDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
*this = (ZstDecompress)
{
.context = ZSTD_createDStream(),
};
// Set callback to ensure zst context is freed
memContextCallbackSet(objMemContext(this), zstDecompressFreeResource, this);
// Initialize context
zstError(ZSTD_initDStream(this->context));
}
OBJ_NEW_END();
FUNCTION_LOG_RETURN(
IO_FILTER,
ioFilterNewP(
ZST_DECOMPRESS_FILTER_TYPE, this, NULL, .done = zstDecompressDone, .inOut = zstDecompressProcess,
.inputSame = zstDecompressInputSame));
}
#endif // HAVE_LIBZST