| 1 |
/* The zlibwrap object. |
| 2 |
* |
| 3 |
* This is an rsyslog object wrapper around zlib. |
| 4 |
* |
| 5 |
* Copyright 2009-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 <zlib.h> |
| 28 |
|
| 29 |
#include "rsyslog.h" |
| 30 |
#include "module-template.h" |
| 31 |
#include "obj.h" |
| 32 |
#include "stream.h" |
| 33 |
#include "zlibw.h" |
| 34 |
|
| 35 |
MODULE_TYPE_LIB |
| 36 |
MODULE_TYPE_NOKEEP; |
| 37 |
|
| 38 |
/* static data */ |
| 39 |
DEFobjStaticHelpers; |
| 40 |
|
| 41 |
|
| 42 |
/* ------------------------------ methods ------------------------------ */ |
| 43 |
|
| 44 |
/* zlib make strong use of macros for its interface functions, so we can not simply |
| 45 |
* pass function pointers to them. Instead, we create very small wrappers which call |
| 46 |
* the relevant entry points. |
| 47 |
*/ |
| 48 |
|
| 49 |
static int myDeflateInit(z_streamp strm, int level) { |
| 50 |
return deflateInit(strm, level); |
| 51 |
} |
| 52 |
|
| 53 |
static int myDeflateInit2(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy) { |
| 54 |
return deflateInit2(strm, level, method, windowBits, memLevel, strategy); |
| 55 |
} |
| 56 |
|
| 57 |
static int myDeflateEnd(z_streamp strm) { |
| 58 |
return deflateEnd(strm); |
| 59 |
} |
| 60 |
|
| 61 |
static int myDeflate(z_streamp strm, int flush) { |
| 62 |
return deflate(strm, flush); |
| 63 |
} |
| 64 |
|
| 65 |
/* finish zlib buffer */ |
| 66 |
static rsRetVal doCompressFinish(strm_t *pThis, rsRetVal (*strmPhysWrite)(strm_t *pThis, uchar *pBuf, size_t lenBuf)) { |
| 67 |
int zRet; /* zlib return state */ |
| 68 |
DEFiRet; |
| 69 |
unsigned outavail; |
| 70 |
assert(pThis != NULL); |
| 71 |
|
| 72 |
assert(pThis->compressionDriver == STRM_COMPRESS_ZIP); |
| 73 |
|
| 74 |
if (!pThis->bzInitDone) goto done; |
| 75 |
|
| 76 |
pThis->zstrm.avail_in = 0; |
| 77 |
/* run deflate() on buffer until everything has been compressed */ |
| 78 |
do { |
| 79 |
DBGPRINTF("in deflate() loop, avail_in %d, total_in %ld\n", pThis->zstrm.avail_in, pThis->zstrm.total_in); |
| 80 |
pThis->zstrm.avail_out = pThis->sIOBufSize; |
| 81 |
pThis->zstrm.next_out = pThis->pZipBuf; |
| 82 |
zRet = deflate(&pThis->zstrm, Z_FINISH); /* no bad return value */ |
| 83 |
DBGPRINTF("after deflate, ret %d, avail_out %d\n", zRet, pThis->zstrm.avail_out); |
| 84 |
outavail = pThis->sIOBufSize - pThis->zstrm.avail_out; |
| 85 |
if (outavail != 0) { |
| 86 |
CHKiRet(strmPhysWrite(pThis, (uchar *)pThis->pZipBuf, outavail)); |
| 87 |
} |
| 88 |
} while (pThis->zstrm.avail_out == 0); |
| 89 |
|
| 90 |
finalize_it: |
| 91 |
zRet = deflateEnd(&pThis->zstrm); |
| 92 |
if (zRet != Z_OK) { |
| 93 |
LogError(0, RS_RET_ZLIB_ERR, "error %d returned from zlib/deflateEnd()", zRet); |
| 94 |
} |
| 95 |
|
| 96 |
pThis->bzInitDone = 0; |
| 97 |
done: |
| 98 |
RETiRet; |
| 99 |
} |
| 100 |
|
| 101 |
/* write the output buffer in zip mode |
| 102 |
* This means we compress it first and then do a physical write. |
| 103 |
* Note that we always do a full deflateInit ... deflate ... deflateEnd |
| 104 |
* sequence. While this is not optimal, we need to do it because we need |
| 105 |
* to ensure that the file is readable even when we are aborted. Doing the |
| 106 |
* full sequence brings us as far towards this goal as possible (and not |
| 107 |
* doing it would be a total failure). It may be worth considering to |
| 108 |
* add a config switch so that the user can decide the risk he is ready |
| 109 |
* to take, but so far this is not yet implemented (not even requested ;)). |
| 110 |
* rgerhards, 2009-06-04 |
| 111 |
*/ |
| 112 |
static rsRetVal doStrmWrite(strm_t *pThis, |
| 113 |
uchar *pBuf, |
| 114 |
size_t lenBuf, |
| 115 |
const int bFlush, |
| 116 |
rsRetVal (*strmPhysWrite)(strm_t *pThis, uchar *pBuf, size_t lenBuf)) { |
| 117 |
int zRet; /* zlib return state */ |
| 118 |
DEFiRet; |
| 119 |
unsigned outavail = 0; |
| 120 |
assert(pThis != NULL); |
| 121 |
assert(pBuf != NULL); |
| 122 |
|
| 123 |
assert(pThis->compressionDriver == STRM_COMPRESS_ZIP); |
| 124 |
|
| 125 |
if (!pThis->bzInitDone) { |
| 126 |
/* allocate deflate state */ |
| 127 |
pThis->zstrm.zalloc = Z_NULL; |
| 128 |
pThis->zstrm.zfree = Z_NULL; |
| 129 |
pThis->zstrm.opaque = Z_NULL; |
| 130 |
/* see note in file header for the params we use with deflateInit2() */ |
| 131 |
zRet = deflateInit2(&pThis->zstrm, pThis->iZipLevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY); |
| 132 |
if (zRet != Z_OK) { |
| 133 |
LogError(0, RS_RET_ZLIB_ERR, "error %d returned from zlib/deflateInit2()", zRet); |
| 134 |
ABORT_FINALIZE(RS_RET_ZLIB_ERR); |
| 135 |
} |
| 136 |
pThis->bzInitDone = RSTRUE; |
| 137 |
} |
| 138 |
|
| 139 |
/* now doing the compression */ |
| 140 |
pThis->zstrm.next_in = (Bytef *)pBuf; |
| 141 |
pThis->zstrm.avail_in = lenBuf; |
| 142 |
/* run deflate() on buffer until everything has been compressed */ |
| 143 |
do { |
| 144 |
DBGPRINTF("in deflate() loop, avail_in %d, total_in %ld, bFlush %d\n", pThis->zstrm.avail_in, |
| 145 |
pThis->zstrm.total_in, bFlush); |
| 146 |
pThis->zstrm.avail_out = pThis->sIOBufSize; |
| 147 |
pThis->zstrm.next_out = pThis->pZipBuf; |
| 148 |
zRet = deflate(&pThis->zstrm, bFlush ? Z_SYNC_FLUSH : Z_NO_FLUSH); /* no bad return value */ |
| 149 |
DBGPRINTF("after deflate, ret %d, avail_out %d, to write %d\n", zRet, pThis->zstrm.avail_out, outavail); |
| 150 |
if (zRet != Z_OK) { |
| 151 |
LogError(0, RS_RET_ZLIB_ERR, "error %d returned from zlib/Deflate()", zRet); |
| 152 |
ABORT_FINALIZE(RS_RET_ZLIB_ERR); |
| 153 |
} |
| 154 |
outavail = pThis->sIOBufSize - pThis->zstrm.avail_out; |
| 155 |
if (outavail != 0) { |
| 156 |
CHKiRet(strmPhysWrite(pThis, (uchar *)pThis->pZipBuf, outavail)); |
| 157 |
} |
| 158 |
} while (pThis->zstrm.avail_out == 0); |
| 159 |
|
| 160 |
finalize_it: |
| 161 |
if (pThis->bzInitDone && pThis->bVeryReliableZip) { |
| 162 |
doCompressFinish(pThis, strmPhysWrite); |
| 163 |
} |
| 164 |
RETiRet; |
| 165 |
} |
| 166 |
/* destruction of caller's zlib resources - a dummy for us */ |
| 167 |
static rsRetVal zlib_Destruct(ATTR_UNUSED strm_t *pThis) { |
| 168 |
return RS_RET_OK; |
| 169 |
} |
| 170 |
|
| 171 |
/* queryInterface function |
| 172 |
* rgerhards, 2008-03-05 |
| 173 |
*/ |
| 174 |
BEGINobjQueryInterface(zlibw) |
| 175 |
CODESTARTobjQueryInterface(zlibw); |
| 176 |
if (pIf->ifVersion != zlibwCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 177 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 178 |
} |
| 179 |
|
| 180 |
/* ok, we have the right interface, so let's fill it |
| 181 |
* Please note that we may also do some backwards-compatibility |
| 182 |
* work here (if we can support an older interface version - that, |
| 183 |
* of course, also affects the "if" above). |
| 184 |
*/ |
| 185 |
pIf->DeflateInit = myDeflateInit; |
| 186 |
pIf->DeflateInit2 = myDeflateInit2; |
| 187 |
pIf->Deflate = myDeflate; |
| 188 |
pIf->DeflateEnd = myDeflateEnd; |
| 189 |
pIf->doStrmWrite = doStrmWrite; |
| 190 |
pIf->doCompressFinish = doCompressFinish; |
| 191 |
pIf->Destruct = zlib_Destruct; |
| 192 |
finalize_it: |
| 193 |
ENDobjQueryInterface(zlibw) |
| 194 |
|
| 195 |
|
| 196 |
/* Initialize the zlibw class. Must be called as the very first method |
| 197 |
* before anything else is called inside this class. |
| 198 |
* rgerhards, 2008-02-19 |
| 199 |
*/ |
| 200 |
BEGINAbstractObjClassInit(zlibw, 1, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 201 |
/* request objects we use */ |
| 202 |
|
| 203 |
/* set our own handlers */ |
| 204 |
ENDObjClassInit(zlibw) |
| 205 |
|
| 206 |
|
| 207 |
/* Exit the class. */ |
| 208 |
BEGINObjClassExit(zlibw, OBJ_IS_LOADABLE_MODULE) /* class, version */ |
| 209 |
CODESTARTObjClassExit(zlibw); |
| 210 |
ENDObjClassExit(zlibw) |
| 211 |
|
| 212 |
|
| 213 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 214 |
|
| 215 |
|
| 216 |
BEGINmodExit |
| 217 |
CODESTARTmodExit; |
| 218 |
zlibwClassExit(); |
| 219 |
ENDmodExit |
| 220 |
|
| 221 |
|
| 222 |
BEGINqueryEtryPt |
| 223 |
CODESTARTqueryEtryPt; |
| 224 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 225 |
ENDqueryEtryPt |
| 226 |
|
| 227 |
|
| 228 |
BEGINmodInit() |
| 229 |
CODESTARTmodInit; |
| 230 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 231 |
|
| 232 |
CHKiRet(zlibwClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 233 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 234 |
ENDmodInit |
| 235 |
/* vi:set ai: |
| 236 |
*/ |