summaryrefslogtreecommitdiff
path: root/tools/source/zcodec/zcodec.cxx
blob: 46d3f24b53102be064fb3682fa10540b917b0da5 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include <tools/stream.hxx>
#ifndef _ZLIB_H
#ifdef SYSTEM_ZLIB
#include "zlib.h"
#else
#include "zlib/zlib.h"
#endif
#endif
#include <tools/zcodec.hxx>
#include <rtl/crc.h>
#include <osl/endian.h>

// -----------
// - Defines -
// -----------

#define PZSTREAM ((z_stream*) mpsC_Stream)

/* gzip flag byte */
#define GZ_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define GZ_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define GZ_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define GZ_ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define GZ_COMMENT      0x10 /* bit 4 set: file comment present */
#define GZ_RESERVED     0xE0 /* bits 5..7: reserved */

static int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */


// ----------
// - ZCodec -
// ----------

ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize, sal_uIntPtr nMemUsage )
    : mnCRC(0)
{
    mnMemUsage = nMemUsage;
    mnInBufSize = nInBufSize;
    mnOutBufSize = nOutBufSize;
    mpsC_Stream = new z_stream;
}

ZCodec::ZCodec( void )
    : mnCRC(0)
{
    mnMemUsage = MAX_MEM_USAGE;
    mnInBufSize = DEFAULT_IN_BUFSIZE;
    mnOutBufSize = DEFAULT_OUT_BUFSIZE;
    mpsC_Stream = new z_stream;
}

// ------------------------------------------------------------------------

ZCodec::~ZCodec()
{
    delete (z_stream*) mpsC_Stream;
}

// ------------------------------------------------------------------------

void ZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
{
    mbInit = 0;
    mbStatus = sal_True;
    mbFinish = sal_False;
    mpIStm = mpOStm = NULL;
    mnInToRead = 0xffffffff;
    mpInBuf = mpOutBuf = NULL;
    PZSTREAM->total_out = PZSTREAM->total_in = 0;
    mnCompressMethod = nCompressMethod;
    PZSTREAM->zalloc = ( alloc_func )0;
    PZSTREAM->zfree = ( free_func )0;
    PZSTREAM->opaque = ( voidpf )0;
    PZSTREAM->avail_out = PZSTREAM->avail_in = 0;
}

// ------------------------------------------------------------------------

long ZCodec::EndCompression()
{
    long retvalue = 0;

    if ( mbInit != 0 )
    {
        if ( mbInit & 2 )   // 1->decompress, 3->compress
        {
            do
            {
                ImplWriteBack();
            }
            while ( deflate( PZSTREAM, Z_FINISH ) != Z_STREAM_END );

            ImplWriteBack();

            retvalue = PZSTREAM->total_in;
            deflateEnd( PZSTREAM );
        }
        else
        {
            retvalue = PZSTREAM->total_out;
            inflateEnd( PZSTREAM );
        }
        delete[] mpOutBuf;
        delete[] mpInBuf;
    }
    return ( mbStatus ) ? retvalue : -1;
}


// ------------------------------------------------------------------------

long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
{
    long nOldTotal_In = PZSTREAM->total_in;

    if ( mbInit == 0 )
    {
        mpIStm = &rIStm;
        mpOStm = &rOStm;
        ImplInitBuf( sal_False );
        mpInBuf = new sal_uInt8[ mnInBufSize ];
    }
    while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
    {
        if ( PZSTREAM->avail_out == 0 )
            ImplWriteBack();
        if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
        {
            mbStatus = sal_False;
            break;
        }
    };
    return ( mbStatus ) ? (long)(PZSTREAM->total_in - nOldTotal_In) : -1;
}

// ------------------------------------------------------------------------

long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
{
    int err;
    sal_uIntPtr nInToRead;
    long    nOldTotal_Out = PZSTREAM->total_out;

    if ( mbFinish )
        return PZSTREAM->total_out - nOldTotal_Out;

    if ( mbInit == 0 )
    {
        mpIStm = &rIStm;
        mpOStm = &rOStm;
        ImplInitBuf( sal_True );
        PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
    }
    do
    {
        if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
        if ( PZSTREAM->avail_in == 0 && mnInToRead )
        {
            nInToRead = ( mnInBufSize > mnInToRead ) ? mnInToRead : mnInBufSize;
            PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, nInToRead );
            mnInToRead -= nInToRead;

            if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
                mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );

        }
        err = inflate( PZSTREAM, Z_NO_FLUSH );
        if ( err < 0 )
        {
            mbStatus = sal_False;
            break;
        }

    }
    while ( ( err != Z_STREAM_END)  && ( PZSTREAM->avail_in || mnInToRead ) );
    ImplWriteBack();

    if ( err == Z_STREAM_END )
        mbFinish = sal_True;
    return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
}

// ------------------------------------------------------------------------

long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
{
    if ( mbInit == 0 )
    {
        mpOStm = &rOStm;
        ImplInitBuf( sal_False );
    }

    PZSTREAM->avail_in = nSize;
    PZSTREAM->next_in = (unsigned char*)pData;

    while ( PZSTREAM->avail_in || ( PZSTREAM->avail_out == 0 ) )
    {
        if ( PZSTREAM->avail_out == 0 )
            ImplWriteBack();

        if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
        {
            mbStatus = sal_False;
            break;
        }
    }
    return ( mbStatus ) ? (long)nSize : -1;
}

// ------------------------------------------------------------------------

long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
{
    int err;
    sal_uIntPtr nInToRead;

    if ( mbFinish )
        return 0;           // PZSTREAM->total_out;

    mpIStm = &rIStm;
    if ( mbInit == 0 )
    {
        ImplInitBuf( sal_True );
    }
    PZSTREAM->avail_out = nSize;
    PZSTREAM->next_out = pData;
    do
    {
        if ( PZSTREAM->avail_in == 0 && mnInToRead )
        {
            nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
            PZSTREAM->avail_in = mpIStm->Read (
                PZSTREAM->next_in = mpInBuf, nInToRead);
            mnInToRead -= nInToRead;

            if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
                mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );

        }
        err = inflate( PZSTREAM, Z_NO_FLUSH );
        if ( err < 0 )
        {
            // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
            mbStatus = (err == Z_BUF_ERROR);
            break;
        }
    }
    while ( (err != Z_STREAM_END) &&
            (PZSTREAM->avail_out != 0) &&
            (PZSTREAM->avail_in || mnInToRead) );
    if ( err == Z_STREAM_END )
        mbFinish = sal_True;

    return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
}

// ------------------------------------------------------------------------

long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
{
    int err = 0;
    sal_uIntPtr nInToRead;

    if ( mbFinish )
        return 0;           // PZSTREAM->total_out;

    if ( mbInit == 0 )
    {
        mpIStm = &rIStm;
        ImplInitBuf( sal_True );
    }
    PZSTREAM->avail_out = nSize;
    PZSTREAM->next_out = pData;
    do
    {
        if ( PZSTREAM->avail_in == 0 && mnInToRead )
        {
            nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;

            sal_uIntPtr nStreamPos = rIStm.Tell();
            rIStm.Seek( STREAM_SEEK_TO_END );
            sal_uIntPtr nMaxPos = rIStm.Tell();
            rIStm.Seek( nStreamPos );
            if ( ( nMaxPos - nStreamPos ) < nInToRead )
            {
                rIStm.SetError( ERRCODE_IO_PENDING );
                err= ! Z_STREAM_END; // TODO What is appropriate code for this?
                break;
            }

            PZSTREAM->avail_in = mpIStm->Read (
                PZSTREAM->next_in = mpInBuf, nInToRead);
            mnInToRead -= nInToRead;

            if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
                mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );

        }
        err = inflate( PZSTREAM, Z_NO_FLUSH );
        if ( err < 0 )
        {
            // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
            mbStatus = (err == Z_BUF_ERROR);
            break;
        }
    }
    while ( (err != Z_STREAM_END) &&
            (PZSTREAM->avail_out != 0) &&
            (PZSTREAM->avail_in || mnInToRead) );
    if ( err == Z_STREAM_END )
        mbFinish = sal_True;

    return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
}

// ------------------------------------------------------------------------

void ZCodec::ImplWriteBack()
{
    sal_uIntPtr nAvail = mnOutBufSize - PZSTREAM->avail_out;

    if ( nAvail )
    {
        if ( mbInit & 2 && ( mnCompressMethod & ZCODEC_UPDATE_CRC ) )
            mnCRC = UpdateCRC( mnCRC, mpOutBuf, nAvail );
        mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
        PZSTREAM->avail_out = mnOutBufSize;
    }
}

// ------------------------------------------------------------------------

void ZCodec::SetBreak( sal_uIntPtr nInToRead )
{
    mnInToRead = nInToRead;
}

// ------------------------------------------------------------------------

sal_uIntPtr ZCodec::GetBreak( void )
{
    return ( mnInToRead + PZSTREAM->avail_in );
}

// ------------------------------------------------------------------------

void ZCodec::SetCRC( sal_uIntPtr nCRC )
{
    mnCRC = nCRC;
}

// ------------------------------------------------------------------------

sal_uIntPtr ZCodec::GetCRC()
{
    return mnCRC;
}

// ------------------------------------------------------------------------

void ZCodec::ImplInitBuf ( sal_Bool nIOFlag )
{
    if ( mbInit == 0 )
    {
        if ( nIOFlag )
        {
            mbInit = 1;
            if ( mbStatus && ( mnCompressMethod & ZCODEC_GZ_LIB ) )
            {
                sal_uInt8 n1, n2, j, nMethod, nFlags;
                for ( int i = 0; i < 2; i++ )   // gz - magic number
                {
                    *mpIStm >> j;
                    if ( j != gz_magic[ i ] )
                        mbStatus = sal_False;
                }
                *mpIStm >> nMethod;
                *mpIStm >> nFlags;
                if ( nMethod != Z_DEFLATED )
                    mbStatus = sal_False;
                if ( ( nFlags & GZ_RESERVED ) != 0 )
                    mbStatus = sal_False;
                /* Discard time, xflags and OS code: */
                mpIStm->SeekRel( 6 );
                /* skip the extra field */
                if ( nFlags & GZ_EXTRA_FIELD )
                {
                    *mpIStm >> n1 >> n2;
                    mpIStm->SeekRel( n1 + ( n2 << 8 ) );
                }
                /* skip the original file name */
                if ( nFlags & GZ_ORIG_NAME)
                {
                    do
                    {
                        *mpIStm >> j;
                    }
                    while ( j && !mpIStm->IsEof() );
                }
                /* skip the .gz file comment */
                if ( nFlags & GZ_COMMENT )
                {
                    do
                    {
                        *mpIStm >> j;
                    }
                    while ( j && !mpIStm->IsEof() );
                }
                /* skip the header crc */
                if ( nFlags & GZ_HEAD_CRC )
                    mpIStm->SeekRel( 2 );
                if ( mbStatus )
                    mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? sal_False : sal_True;
            }
            else
            {
                mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
            }
            mpInBuf = new sal_uInt8[ mnInBufSize ];
        }
        else
        {
            mbInit = 3;

            mbStatus = ( deflateInit2_( PZSTREAM, mnCompressMethod & 0xff, Z_DEFLATED,
                MAX_WBITS, mnMemUsage, ( mnCompressMethod >> 8 ) & 0xff,
                    ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );

            PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
        }
    }
}

// ------------------------------------------------------------------------

sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uInt8* pSource, long nDatSize)
{
    return rtl_crc32( nLatestCRC, pSource, nDatSize );
}

// ------------------------------------------------------------------------

void GZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
{
    ZCodec::BeginCompression( nCompressMethod | ZCODEC_GZ_LIB );
};


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */