Debian Codegraph @

runtime/zstdw.c

raw
1 /* The zstdw object.
2 *
3 * This is an rsyslog object wrapper around zstd.
4 *
5 * Copyright 2022 Rainer Gerhards and Adiscon GmbH.
6 *
7 * This file is part of the rsyslog runtime library.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 * -or-
15 * see COPYING.ASL20 in the source distribution
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 #include "config.h"
25 #include <string.h>
26 #include <assert.h>
27 #include <zstd.h>
28
29 #include "rsyslog.h"
30 #include "errmsg.h"
31 #include "stream.h"
32 #include "module-template.h"
33 #include "obj.h"
34 #include "zstdw.h"
35
36 MODULE_TYPE_LIB
37 MODULE_TYPE_NOKEEP;
38
39 /* static data */
40 DEFobjStaticHelpers;
41
42
43 /* finish buffer, to be called before closing the zstd file. */
44 static rsRetVal zstd_doCompressFinish(strm_t *pThis,
45 rsRetVal (*strmPhysWrite)(strm_t *pThis, uchar *pBuf, size_t lenBuf)) {
46 size_t remaining = 0;
47 DEFiRet;
48 assert(pThis != NULL);
49
50 if (!pThis->bzInitDone) goto done;
51
52 char dummybuf; /* not sure if we can pass in NULL as buffer address in this special case */
53 ZSTD_inBuffer input = {&dummybuf, 0, 0};
54
55 do {
56 ZSTD_outBuffer output = {pThis->pZipBuf, pThis->sIOBufSize, 0};
57 remaining = ZSTD_compressStream2(pThis->zstd.cctx, &output, &input, ZSTD_e_end);
58 if (ZSTD_isError(remaining)) {
59 LogError(0, RS_RET_ZLIB_ERR, "error returned from ZSTD_compressStream2(): %s\n",
60 ZSTD_getErrorName(remaining));
61 ABORT_FINALIZE(RS_RET_ZLIB_ERR);
62 }
63
64 CHKiRet(strmPhysWrite(pThis, (uchar *)pThis->pZipBuf, output.pos));
65
66 } while (remaining != 0);
67
68 finalize_it:
69 done:
70 RETiRet;
71 }
72
73
74 static rsRetVal zstd_doStrmWrite(strm_t *pThis,
75 uchar *const pBuf,
76 const size_t lenBuf,
77 const int bFlush,
78 rsRetVal (*strmPhysWrite)(strm_t *pThis, uchar *pBuf, size_t lenBuf)) {
79 DEFiRet;
80 assert(pThis != NULL);
81 assert(pBuf != NULL);
82 if (!pThis->bzInitDone) {
83 pThis->zstd.cctx = (void *)ZSTD_createCCtx();
84 if (pThis->zstd.cctx == NULL) {
85 LogError(0, RS_RET_ZLIB_ERR,
86 "error creating zstd context (ZSTD_createCCtx failed, "
87 "that's all we know");
88 ABORT_FINALIZE(RS_RET_ZLIB_ERR);
89 }
90
91 ZSTD_CCtx_setParameter(pThis->zstd.cctx, ZSTD_c_compressionLevel, pThis->iZipLevel);
92 ZSTD_CCtx_setParameter(pThis->zstd.cctx, ZSTD_c_checksumFlag, 1);
93 if (pThis->zstd.num_wrkrs > 0) {
94 ZSTD_CCtx_setParameter(pThis->zstd.cctx, ZSTD_c_nbWorkers, pThis->zstd.num_wrkrs);
95 }
96 pThis->bzInitDone = RSTRUE;
97 }
98
99 /* now doing the compression */
100 ZSTD_inBuffer input = {pBuf, lenBuf, 0};
101
102 // This following needs to be configurable? It's possibly sufficient to use e_flush
103 // only, as this can also be controlled by veryRobustZip. However, testbench will than
104 // not be able to check when all file lines are complete.
105 ZSTD_EndDirective const mode = bFlush ? ZSTD_e_flush : ZSTD_e_continue;
106 size_t remaining;
107 do {
108 ZSTD_outBuffer output = {pThis->pZipBuf, 128, 0};
109 remaining = ZSTD_compressStream2(pThis->zstd.cctx, &output, &input, mode);
110 if (ZSTD_isError(remaining)) {
111 LogError(0, RS_RET_ZLIB_ERR, "error returned from ZSTD_compressStream2(): %s",
112 ZSTD_getErrorName(remaining));
113 ABORT_FINALIZE(RS_RET_ZLIB_ERR);
114 }
115
116 CHKiRet(strmPhysWrite(pThis, (uchar *)pThis->pZipBuf, output.pos));
117
118 } while (mode == ZSTD_e_end ? (remaining != 0) : (input.pos != input.size));
119
120 finalize_it:
121 if (pThis->bzInitDone && pThis->bVeryReliableZip) {
122 zstd_doCompressFinish(pThis, strmPhysWrite);
123 }
124 RETiRet;
125 }
126
127 /* destruction of caller's zstd ressources */
128 static rsRetVal zstd_Destruct(strm_t *const pThis) {
129 DEFiRet;
130 assert(pThis != NULL);
131
132 if (!pThis->bzInitDone) goto done;
133
134 const int result = ZSTD_freeCCtx(pThis->zstd.cctx);
135 if (ZSTD_isError(result)) {
136 LogError(0, RS_RET_ZLIB_ERR, "error from ZSTD_freeCCtx(): %s", ZSTD_getErrorName(result));
137 }
138
139 pThis->bzInitDone = 0;
140 done:
141 RETiRet;
142 }
143
144
145 /* queryInterface function
146 * rgerhards, 2008-03-05
147 */
148 BEGINobjQueryInterface(zstdw)
149 CODESTARTobjQueryInterface(zstdw);
150 if (pIf->ifVersion != zstdwCURR_IF_VERSION) { /* check for current version, increment on each change */
151 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
152 }
153 pIf->doStrmWrite = zstd_doStrmWrite;
154 pIf->doCompressFinish = zstd_doCompressFinish;
155 pIf->Destruct = zstd_Destruct;
156 finalize_it:
157 ENDobjQueryInterface(zstdw)
158
159
160 /* Initialize the zstdw class. Must be called as the very first method
161 * before anything else is called inside this class.
162 * rgerhards, 2008-02-19
163 */
164 BEGINAbstractObjClassInit(zstdw, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */
165 ENDObjClassInit(zstdw)
166
167
168 /* Exit the class. */
169 BEGINObjClassExit(zstdw, OBJ_IS_LOADABLE_MODULE) /* class, version */
170 CODESTARTObjClassExit(zstdw);
171 ENDObjClassExit(zstdw)
172
173
174 /* --------------- here now comes the plumbing that makes as a library module --------------- */
175
176
177 BEGINmodExit
178 CODESTARTmodExit;
179 zstdwClassExit();
180 ENDmodExit
181
182
183 BEGINqueryEtryPt
184 CODESTARTqueryEtryPt;
185 CODEqueryEtryPt_STD_LIB_QUERIES;
186 ENDqueryEtryPt
187
188
189 BEGINmodInit()
190 CODESTARTmodInit;
191 *ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */
192 CHKiRet(zstdwClassInit(pModInfo));
193 ENDmodInit