27 void DpZLibCompressor::cleanupContext(ZLibCtx& ctx) {
28 if (ctx.compression_buffer.getSize() != 0) {
29 bufferCompressionReturn_out(0, ctx.compression_buffer);
32 if (ctx.zlib_alloc_buffer.getSize() != 0) {
33 bufferZLibReturn_out(0, ctx.zlib_alloc_buffer);
37 Svc::CompressionAlgorithm DpZLibCompressor ::compressChunk_handler(
FwIndexType portNum,
43 ctx.compression_buffer = bufferCompressionGet_out(0, min_compression);
44 if (ctx.compression_buffer.getSize() == 0) {
45 log_WARNING_LO_ZLibCompressionBadBuffer(min_compression);
48 return CompressionAlgorithm::UNCOMPRESSED;
52 const FwSizeType zlib_alloc_size = paramGet_ZLibBufferSize(param_valid);
55 ctx.zlib_alloc_buffer = bufferZLibGet_out(0, zlib_alloc_size);
56 if (ctx.zlib_alloc_buffer.getSize() == 0) {
57 log_WARNING_LO_ZLibAllocBadBuffer(zlib_alloc_size);
60 return CompressionAlgorithm::UNCOMPRESSED;
63 ctx.zlib_stream.zalloc = DpZLibCompressor::zlib_alloc_fn;
64 ctx.zlib_stream.zfree = DpZLibCompressor::zlib_free_fn;
65 ctx.zlib_stream.opaque = &ctx;
67 const CompressionAlgorithm alg = zlibCompressionHelper(ctx, buffer);
69 if (alg != CompressionAlgorithm::UNCOMPRESSED) {
76 FW_ASSERT(buffer.
getSize() - write_offset >= ctx.compression_buffer.getSize(),
80 (void)std::memcpy(buffer.
getData() + write_offset, ctx.compression_buffer.getData(),
81 ctx.compression_buffer.getSize());
82 buffer.
setSize(ctx.compression_buffer.getSize() + write_offset);
89 CompressionAlgorithm DpZLibCompressor::zlibCompressionHelper(ZLibCtx& ctx,
const Fw::Buffer& in_buffer) {
91 const I8 compression_level = paramGet_CompressionLevel(param_valid);
95 zlib_ok = deflateInit(&ctx.zlib_stream, compression_level);
96 if (zlib_ok != Z_OK) {
97 ctx.comp.log_WARNING_LO_ZLibInitError(
99 return CompressionAlgorithm::UNCOMPRESSED;
102 if (in_buffer.
getSize() > std::numeric_limits<uInt>::max() ||
103 ctx.compression_buffer.getSize() > std::numeric_limits<uInt>::max()) {
104 ctx.comp.log_WARNING_LO_BufferTooBigForZLib(in_buffer.
getSize(), ctx.compression_buffer.getSize(),
105 std::numeric_limits<uInt>::max());
106 return CompressionAlgorithm::UNCOMPRESSED;
109 ctx.zlib_stream.next_in = in_buffer.
getData();
110 ctx.zlib_stream.avail_in =
static_cast<uInt
>(in_buffer.
getSize());
112 ctx.zlib_stream.next_out = ctx.compression_buffer.getData();
113 ctx.zlib_stream.avail_out =
static_cast<uInt
>(ctx.compression_buffer.getSize());
115 zlib_ok = deflate(&ctx.zlib_stream, Z_FINISH);
117 if (zlib_ok == Z_STREAM_END && ctx.zlib_stream.total_out <= ctx.compression_buffer.getSize()) {
118 ctx.compression_buffer.setSize(ctx.zlib_stream.total_out);
120 ctx.comp.log_DIAGNOSTIC_ZLibCompression(in_buffer.
getSize(), ctx.compression_buffer.getSize());
122 ctx.comp.log_DIAGNOSTIC_ZLibMemoryUsage(ctx.bump_allocator, ctx.zlib_alloc_buffer.getSize());
124 return CompressionAlgorithm::ZLIB_DEFLATE;
126 if (zlib_ok != Z_OK) {
127 ctx.comp.log_WARNING_LO_ZLibDeflateError(
131 ctx.comp.log_DIAGNOSTIC_ZLibNoCompression(in_buffer.
getSize(), ctx.compression_buffer.getSize());
132 return CompressionAlgorithm::UNCOMPRESSED;
141 voidpf DpZLibCompressor::zlib_alloc_fn(voidpf opaque, uInt items, uInt size) {
143 ZLibCtx* ctx =
reinterpret_cast<ZLibCtx*
>(opaque);
145 FW_ASSERT(ctx->bump_allocator <= ctx->zlib_alloc_buffer.getSize(),
147 static_cast<FwAssertArgType>(ctx->zlib_alloc_buffer.getSize()));
148 const FwSizeType free_space = ctx->zlib_alloc_buffer.getSize() - ctx->bump_allocator;
152 if (free_space < alloc_size) {
156 U8* alloc_ptr = ctx->zlib_alloc_buffer.getData() + ctx->bump_allocator;
157 ctx->bump_allocator += alloc_size;
162 void DpZLibCompressor::zlib_free_fn(voidpf opaque, voidpf address) {
~DpZLibCompressor()
Destroy DpZLibCompressor object.
DpZLibCompressor(const char *const compName)
Construct DpZLibCompressor object.
#define FW_PARAM_OK(paramValid)
PlatformSizeType FwSizeType
void setSize(FwSizeType size)
int8_t I8
8-bit signed integer
uint8_t U8
8-bit unsigned integer
FwSizeType getSize() const
PlatformIndexType FwIndexType
RateGroupDivider component implementation.
Enum representing parameter validity.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.