Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/tools/source/zcodec/zcodec.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <sal/config.h>
21
22
#include <algorithm>
23
24
#include <tools/stream.hxx>
25
26
#include <zlib.h>
27
28
#include <tools/zcodec.hxx>
29
#include <tools/long.hxx>
30
31
/* gzip flag byte */
32
//                   GZ_ASCII_FLAG     = 0x01;   /* bit 0 set: file probably ascii text */
33
constexpr sal_uInt8  GZ_HEAD_CRC       = 0x02;   /* bit 1 set: header CRC present */
34
constexpr sal_uInt8  GZ_EXTRA_FIELD    = 0x04;   /* bit 2 set: extra field present */
35
constexpr sal_uInt8  GZ_ORIG_NAME      = 0x08;   /* bit 3 set: original file name present */
36
constexpr sal_uInt8  GZ_COMMENT        = 0x10;   /* bit 4 set: file comment present */
37
constexpr sal_uInt8  GZ_RESERVED       = 0xE0;   /* bits 5..7: reserved */
38
constexpr sal_uInt16 GZ_MAGIC_BYTES_LE = 0x8B1F; /* gzip magic bytes, little endian */
39
constexpr sal_uInt8  GZ_DEFLATE        = 0x08;
40
constexpr sal_uInt8  GZ_FS_UNKNOWN     = 0xFF;
41
42
ZCodec::ZCodec( size_t nInBufSize, size_t nOutBufSize )
43
113k
    : meState(STATE_INIT)
44
113k
    , mbStatus(false)
45
113k
    , mbFinish(false)
46
113k
    , mnInBufSize(nInBufSize)
47
113k
    , mnInToRead(0)
48
113k
    , mpOStm(nullptr)
49
113k
    , mnOutBufSize(nOutBufSize)
50
113k
    , mnUncompressedSize(0)
51
113k
    , mnInBufCRC32(0)
52
113k
    , mnLastModifiedTime(0)
53
113k
    , mnCompressLevel(0)
54
113k
    , mbGzLib(false)
55
113k
{
56
113k
    mpsC_Stream = new z_stream;
57
113k
}
58
59
ZCodec::~ZCodec()
60
113k
{
61
113k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
62
113k
    delete pStream;
63
113k
}
64
65
bool ZCodec::IsZCompressed( SvStream& rIStm )
66
190k
{
67
190k
    sal_uInt64 nCurPos = rIStm.Tell();
68
190k
    rIStm.Seek( 0 );
69
190k
    sal_uInt16 nFirstTwoBytes = 0;
70
190k
    rIStm.ReadUInt16( nFirstTwoBytes );
71
190k
    rIStm.Seek( nCurPos );
72
190k
    return nFirstTwoBytes == GZ_MAGIC_BYTES_LE;
73
190k
}
74
75
void ZCodec::BeginCompression( int nCompressLevel, bool gzLib )
76
113k
{
77
113k
    assert(meState == STATE_INIT);
78
113k
    mbStatus = true;
79
113k
    mbFinish = false;
80
113k
    mpOStm = nullptr;
81
113k
    mnInToRead = 0xffffffff;
82
113k
    mpInBuf.reset();
83
113k
    mpOutBuf.reset();
84
113k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
85
113k
    pStream->total_out = pStream->total_in = 0;
86
113k
    mnCompressLevel = nCompressLevel;
87
113k
    mbGzLib = gzLib;
88
113k
    pStream->zalloc = nullptr;
89
113k
    pStream->zfree = nullptr;
90
113k
    pStream->opaque = nullptr;
91
113k
    pStream->avail_out = pStream->avail_in = 0;
92
113k
}
93
94
tools::Long ZCodec::EndCompression()
95
113k
{
96
113k
    tools::Long retvalue = 0;
97
113k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
98
99
113k
    if (meState != STATE_INIT)
100
113k
    {
101
113k
        if (meState == STATE_COMPRESS)
102
82.9k
        {
103
82.9k
            if (mbStatus)
104
82.9k
            {
105
82.9k
                do
106
83.0k
                {
107
83.0k
                    ImplWriteBack();
108
83.0k
                }
109
83.0k
                while ( deflate( pStream, Z_FINISH ) != Z_STREAM_END );
110
111
82.9k
                ImplWriteBack();
112
82.9k
            }
113
114
82.9k
            retvalue = pStream->total_in;
115
82.9k
            deflateEnd( pStream );
116
82.9k
            if ( mbGzLib )
117
0
            {
118
                // metadata must be set to compress as gz format
119
0
                assert(!msFilename.isEmpty());
120
                // overwrite zlib checksum
121
0
                mpOStm->Seek(STREAM_SEEK_TO_END);
122
0
                mpOStm->SeekRel(-4);
123
0
                mpOStm->WriteUInt32( mnInBufCRC32 );       // Uncompressed buffer CRC32
124
0
                mpOStm->WriteUInt32( mnUncompressedSize ); // Uncompressed size mod 2^32
125
0
                mpOStm->Seek( 0 );
126
0
                mpOStm->WriteUInt16( GZ_MAGIC_BYTES_LE )   // Magic bytes
127
0
                        .WriteUInt8( GZ_DEFLATE )          // Compression algorithm
128
0
                        .WriteUInt8( GZ_ORIG_NAME )        // Filename
129
0
                        .WriteUInt32( mnLastModifiedTime ) // Modification time
130
0
                        .WriteUInt8( 0 )                   // Extra flags
131
0
                        .WriteUInt8( GZ_FS_UNKNOWN )       // Operating system
132
0
                        .WriteBytes( msFilename.pData->buffer, msFilename.pData->length );
133
0
                mpOStm->WriteUInt8( 0 ); // null terminate the filename string
134
0
            }
135
82.9k
        }
136
30.8k
        else
137
30.8k
        {
138
30.8k
            retvalue = pStream->total_out;
139
30.8k
            inflateEnd( pStream );
140
30.8k
        }
141
113k
        mpOutBuf.reset();
142
113k
        mpInBuf.reset();
143
113k
        meState = STATE_INIT;
144
113k
    }
145
113k
    return mbStatus ? retvalue : -1;
146
113k
}
147
148
void ZCodec::SetCompressionMetadata( const OString& sFilename, sal_uInt32 nLastModifiedTime, sal_uInt32 nInBufCRC32 )
149
0
{
150
0
    assert( mbGzLib );
151
0
    msFilename = sFilename;
152
0
    mnLastModifiedTime = nLastModifiedTime;
153
0
    mnInBufCRC32 = nInBufCRC32;
154
0
}
155
156
void ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
157
0
{
158
0
    assert(meState == STATE_INIT);
159
0
    mpOStm = &rOStm;
160
0
    rIStm.Seek(0);
161
0
    mnUncompressedSize = rIStm.TellEnd();
162
0
    InitCompress();
163
0
    mpInBuf.reset(new sal_uInt8[ mnInBufSize ]);
164
0
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
165
0
    for (;;)
166
0
    {
167
0
        pStream->next_in = mpInBuf.get();
168
0
        pStream->avail_in = rIStm.ReadBytes( mpInBuf.get(), mnInBufSize );
169
0
        if (pStream->avail_in == 0)
170
0
            break;
171
0
        if ( pStream->avail_out == 0 )
172
0
            ImplWriteBack();
173
0
        if ( deflate( pStream, Z_NO_FLUSH ) < 0 )
174
0
        {
175
0
            mbStatus = false;
176
0
            break;
177
0
        }
178
0
    };
179
0
}
180
181
tools::Long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
182
6.56k
{
183
6.56k
    int err;
184
6.56k
    size_t nInToRead;
185
6.56k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
186
6.56k
    tools::Long    nOldTotal_Out = pStream->total_out;
187
188
6.56k
    assert(meState == STATE_INIT);
189
6.56k
    mpOStm = &rOStm;
190
6.56k
    InitDecompress(rIStm);
191
6.56k
    pStream->avail_out = mnOutBufSize;
192
6.56k
    mpOutBuf.reset(new sal_uInt8[ pStream->avail_out ]);
193
6.56k
    pStream->next_out = mpOutBuf.get();
194
6.56k
    do
195
8.35k
    {
196
8.35k
        if ( pStream->avail_out == 0 ) ImplWriteBack();
197
8.35k
        if ( pStream->avail_in == 0 && mnInToRead )
198
7.34k
        {
199
7.34k
            nInToRead = std::min( mnInBufSize, mnInToRead );
200
7.34k
            pStream->next_in = mpInBuf.get();
201
7.34k
            pStream->avail_in = rIStm.ReadBytes(mpInBuf.get(), nInToRead);
202
7.34k
            mnInToRead -= nInToRead;
203
7.34k
        }
204
8.35k
        err = mbStatus ? inflate(pStream, Z_NO_FLUSH) : Z_ERRNO;
205
8.35k
        if (err < 0 || err == Z_NEED_DICT)
206
4.25k
        {
207
4.25k
            mbStatus = false;
208
4.25k
            break;
209
4.25k
        }
210
211
8.35k
    }
212
6.56k
    while ( ( err != Z_STREAM_END)  && ( pStream->avail_in || mnInToRead ) );
213
6.56k
    ImplWriteBack();
214
215
6.56k
    return mbStatus ? static_cast<tools::Long>(pStream->total_out - nOldTotal_Out) : -1;
216
6.56k
}
217
218
void ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uInt32 nSize )
219
755k
{
220
755k
    if (meState == STATE_INIT)
221
82.9k
    {
222
82.9k
        mpOStm = &rOStm;
223
82.9k
        InitCompress();
224
82.9k
    }
225
755k
    assert(&rOStm == mpOStm);
226
227
755k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
228
755k
    pStream->avail_in = nSize;
229
755k
    pStream->next_in = pData;
230
231
1.51M
    while ( pStream->avail_in || ( pStream->avail_out == 0 ) )
232
755k
    {
233
755k
        if ( pStream->avail_out == 0 )
234
0
            ImplWriteBack();
235
236
755k
        if ( deflate( pStream, Z_NO_FLUSH ) < 0 )
237
0
        {
238
0
            mbStatus = false;
239
0
            break;
240
0
        }
241
755k
    }
242
755k
}
243
244
tools::Long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uInt32 nSize )
245
223k
{
246
223k
    int err;
247
223k
    size_t nInToRead;
248
249
223k
    if ( mbFinish )
250
289
        return 0;           // pStream->total_out;
251
252
223k
    if (meState == STATE_INIT)
253
24.3k
    {
254
24.3k
        InitDecompress(rIStm);
255
24.3k
    }
256
223k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
257
223k
    pStream->avail_out = nSize;
258
223k
    pStream->next_out = pData;
259
223k
    do
260
231k
    {
261
231k
        if ( pStream->avail_in == 0 && mnInToRead )
262
44.4k
        {
263
44.4k
            nInToRead = std::min(mnInBufSize, mnInToRead);
264
44.4k
            pStream->next_in = mpInBuf.get();
265
44.4k
            pStream->avail_in = rIStm.ReadBytes(mpInBuf.get(), nInToRead);
266
44.4k
            mnInToRead -= nInToRead;
267
44.4k
        }
268
231k
        err = mbStatus ? inflate(pStream, Z_NO_FLUSH) : Z_ERRNO;
269
231k
        if (err < 0 || err == Z_NEED_DICT)
270
29.5k
        {
271
            // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
272
29.5k
            mbStatus = (err == Z_BUF_ERROR);
273
29.5k
            break;
274
29.5k
        }
275
231k
    }
276
223k
    while ( (err != Z_STREAM_END) &&
277
201k
            (pStream->avail_out != 0) &&
278
8.19k
            (pStream->avail_in || mnInToRead) );
279
223k
    if ( err == Z_STREAM_END )
280
941
        mbFinish = true;
281
282
223k
    return (mbStatus ? static_cast<tools::Long>(nSize - pStream->avail_out) : -1);
283
223k
}
284
285
void ZCodec::ImplWriteBack()
286
173k
{
287
173k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
288
173k
    size_t nAvail = mnOutBufSize - pStream->avail_out;
289
290
173k
    if ( nAvail > 0 )
291
171k
    {
292
171k
        pStream->next_out = mpOutBuf.get();
293
171k
        mpOStm->WriteBytes( mpOutBuf.get(), nAvail );
294
171k
        pStream->avail_out = mnOutBufSize;
295
171k
    }
296
173k
}
297
298
void ZCodec::InitCompress()
299
82.9k
{
300
82.9k
    assert(meState == STATE_INIT);
301
82.9k
    if (mbGzLib)
302
0
    {
303
        // Seek just enough so that the zlib header is overwritten after compression
304
        // with the gz header
305
        // 10 header bytes + filename length + null terminator - 2 bytes for
306
        // zlib header that gets overwritten
307
0
        mpOStm->Seek(10 + msFilename.getLength() + 1 - 2);
308
0
    }
309
82.9k
    meState = STATE_COMPRESS;
310
82.9k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
311
82.9k
    mbStatus = deflateInit2_(
312
82.9k
        pStream, mnCompressLevel, Z_DEFLATED, MAX_WBITS, MAX_MEM_LEVEL,
313
82.9k
        Z_DEFAULT_STRATEGY, ZLIB_VERSION, sizeof (z_stream)) >= 0;
314
82.9k
    mpOutBuf.reset(new sal_uInt8[mnOutBufSize]);
315
82.9k
    pStream->next_out = mpOutBuf.get();
316
82.9k
    pStream->avail_out = mnOutBufSize;
317
82.9k
}
318
319
void ZCodec::InitDecompress(SvStream & inStream)
320
30.8k
{
321
30.8k
    assert(meState == STATE_INIT);
322
30.8k
    auto pStream = static_cast<z_stream*>(mpsC_Stream);
323
30.8k
    if ( mbStatus &&  mbGzLib )
324
0
    {
325
0
        sal_uInt8 j, nMethod, nFlags;
326
0
        sal_uInt16 nFirstTwoBytes;
327
0
        inStream.Seek( 0 );
328
0
        inStream.ReadUInt16( nFirstTwoBytes );
329
0
        if ( nFirstTwoBytes != GZ_MAGIC_BYTES_LE )
330
0
            mbStatus = false;
331
0
        inStream.ReadUChar( nMethod );
332
0
        inStream.ReadUChar( nFlags );
333
0
        if ( nMethod != Z_DEFLATED )
334
0
            mbStatus = false;
335
0
        if ( ( nFlags & GZ_RESERVED ) != 0 )
336
0
            mbStatus = false;
337
        /* Discard time, xflags and OS code: */
338
0
        inStream.SeekRel( 6 );
339
        /* skip the extra field */
340
0
        if ( nFlags & GZ_EXTRA_FIELD )
341
0
        {
342
0
            sal_uInt8 n1, n2;
343
0
            inStream.ReadUChar( n1 ).ReadUChar( n2 );
344
0
            inStream.SeekRel( n1 + ( n2 << 8 ) );
345
0
        }
346
        /* skip the original file name */
347
0
        if ( nFlags & GZ_ORIG_NAME)
348
0
        {
349
0
            do
350
0
            {
351
0
                inStream.ReadUChar( j );
352
0
            }
353
0
            while ( j && !inStream.eof() );
354
0
        }
355
        /* skip the .gz file comment */
356
0
        if ( nFlags & GZ_COMMENT )
357
0
        {
358
0
            do
359
0
            {
360
0
                inStream.ReadUChar( j );
361
0
            }
362
0
            while ( j && !inStream.eof() );
363
0
        }
364
        /* skip the header crc */
365
0
        if ( nFlags & GZ_HEAD_CRC )
366
0
            inStream.SeekRel( 2 );
367
0
        if ( mbStatus )
368
0
            mbStatus = inflateInit2( pStream, -MAX_WBITS) == Z_OK;
369
0
    }
370
30.8k
    else
371
30.8k
    {
372
30.8k
        mbStatus = ( inflateInit( pStream ) >= 0 );
373
30.8k
    }
374
30.8k
    if ( mbStatus )
375
30.8k
        meState = STATE_DECOMPRESS;
376
30.8k
    mpInBuf.reset(new sal_uInt8[ mnInBufSize ]);
377
30.8k
}
378
379
bool ZCodec::AttemptDecompression(SvStream& rIStm, SvStream& rOStm)
380
0
{
381
0
    assert(meState == STATE_INIT);
382
0
    sal_uInt64 nStreamPos = rIStm.Tell();
383
0
    BeginCompression(ZCODEC_DEFAULT_COMPRESSION, true/*gzLib*/);
384
0
    InitDecompress(rIStm);
385
0
    EndCompression();
386
0
    if ( !mbStatus || rIStm.GetError() )
387
0
    {
388
0
        rIStm.Seek(nStreamPos);
389
0
        return false;
390
0
    }
391
0
    rIStm.Seek(nStreamPos);
392
0
    BeginCompression(ZCODEC_DEFAULT_COMPRESSION, true/*gzLib*/);
393
0
    Decompress(rIStm, rOStm);
394
0
    EndCompression();
395
0
    if( !mbStatus || rIStm.GetError() || rOStm.GetError() )
396
0
    {
397
0
        rIStm.Seek(nStreamPos);
398
0
        return false;
399
0
    }
400
0
    rIStm.Seek(nStreamPos);
401
0
    rOStm.Seek(0);
402
0
    return true;
403
0
}
404
405
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */