1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_tools.hxx" 30*cdf0e10cSrcweir #include <tools/stream.hxx> 31*cdf0e10cSrcweir #ifndef _ZLIB_H 32*cdf0e10cSrcweir #ifdef SYSTEM_ZLIB 33*cdf0e10cSrcweir #include "zlib.h" 34*cdf0e10cSrcweir #else 35*cdf0e10cSrcweir #include "zlib/zlib.h" 36*cdf0e10cSrcweir #endif 37*cdf0e10cSrcweir #endif 38*cdf0e10cSrcweir #include <tools/zcodec.hxx> 39*cdf0e10cSrcweir #include <rtl/crc.h> 40*cdf0e10cSrcweir #include <osl/endian.h> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir // ----------- 43*cdf0e10cSrcweir // - Defines - 44*cdf0e10cSrcweir // ----------- 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir #define PZSTREAM ((z_stream*) mpsC_Stream) 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir /* gzip flag byte */ 49*cdf0e10cSrcweir #define GZ_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 50*cdf0e10cSrcweir #define GZ_HEAD_CRC 0x02 /* bit 1 set: header CRC present */ 51*cdf0e10cSrcweir #define GZ_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 52*cdf0e10cSrcweir #define GZ_ORIG_NAME 0x08 /* bit 3 set: original file name present */ 53*cdf0e10cSrcweir #define GZ_COMMENT 0x10 /* bit 4 set: file comment present */ 54*cdf0e10cSrcweir #define GZ_RESERVED 0xE0 /* bits 5..7: reserved */ 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir static int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */ 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir // ---------- 60*cdf0e10cSrcweir // - ZCodec - 61*cdf0e10cSrcweir // ---------- 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize, sal_uIntPtr nMemUsage ) 64*cdf0e10cSrcweir : mnCRC(0) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir mnMemUsage = nMemUsage; 67*cdf0e10cSrcweir mnInBufSize = nInBufSize; 68*cdf0e10cSrcweir mnOutBufSize = nOutBufSize; 69*cdf0e10cSrcweir mpsC_Stream = new z_stream; 70*cdf0e10cSrcweir } 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir ZCodec::ZCodec( void ) 73*cdf0e10cSrcweir : mnCRC(0) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir mnMemUsage = MAX_MEM_USAGE; 76*cdf0e10cSrcweir mnInBufSize = DEFAULT_IN_BUFSIZE; 77*cdf0e10cSrcweir mnOutBufSize = DEFAULT_OUT_BUFSIZE; 78*cdf0e10cSrcweir mpsC_Stream = new z_stream; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir // ------------------------------------------------------------------------ 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir ZCodec::~ZCodec() 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir delete (z_stream*) mpsC_Stream; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir // ------------------------------------------------------------------------ 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir void ZCodec::BeginCompression( sal_uIntPtr nCompressMethod ) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir mbInit = 0; 93*cdf0e10cSrcweir mbStatus = sal_True; 94*cdf0e10cSrcweir mbFinish = sal_False; 95*cdf0e10cSrcweir mpIStm = mpOStm = NULL; 96*cdf0e10cSrcweir mnInToRead = 0xffffffff; 97*cdf0e10cSrcweir mpInBuf = mpOutBuf = NULL; 98*cdf0e10cSrcweir PZSTREAM->total_out = PZSTREAM->total_in = 0; 99*cdf0e10cSrcweir mnCompressMethod = nCompressMethod; 100*cdf0e10cSrcweir PZSTREAM->zalloc = ( alloc_func )0; 101*cdf0e10cSrcweir PZSTREAM->zfree = ( free_func )0; 102*cdf0e10cSrcweir PZSTREAM->opaque = ( voidpf )0; 103*cdf0e10cSrcweir PZSTREAM->avail_out = PZSTREAM->avail_in = 0; 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir // ------------------------------------------------------------------------ 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir long ZCodec::EndCompression() 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir long retvalue = 0; 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir if ( mbInit != 0 ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir if ( mbInit & 2 ) // 1->decompress, 3->compress 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir do 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir ImplWriteBack(); 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir while ( deflate( PZSTREAM, Z_FINISH ) != Z_STREAM_END ); 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir ImplWriteBack(); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir retvalue = PZSTREAM->total_in; 125*cdf0e10cSrcweir deflateEnd( PZSTREAM ); 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir else 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir retvalue = PZSTREAM->total_out; 130*cdf0e10cSrcweir inflateEnd( PZSTREAM ); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir delete[] mpOutBuf; 133*cdf0e10cSrcweir delete[] mpInBuf; 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir return ( mbStatus ) ? retvalue : -1; 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir // ------------------------------------------------------------------------ 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir long nOldTotal_In = PZSTREAM->total_in; 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir if ( mbInit == 0 ) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir mpIStm = &rIStm; 148*cdf0e10cSrcweir mpOStm = &rOStm; 149*cdf0e10cSrcweir ImplInitBuf( sal_False ); 150*cdf0e10cSrcweir mpInBuf = new sal_uInt8[ mnInBufSize ]; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 ) 153*cdf0e10cSrcweir { 154*cdf0e10cSrcweir if ( PZSTREAM->avail_out == 0 ) 155*cdf0e10cSrcweir ImplWriteBack(); 156*cdf0e10cSrcweir if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 ) 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir mbStatus = sal_False; 159*cdf0e10cSrcweir break; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir }; 162*cdf0e10cSrcweir return ( mbStatus ) ? (long)(PZSTREAM->total_in - nOldTotal_In) : -1; 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir // ------------------------------------------------------------------------ 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir int err; 170*cdf0e10cSrcweir sal_uIntPtr nInToRead; 171*cdf0e10cSrcweir long nOldTotal_Out = PZSTREAM->total_out; 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir if ( mbFinish ) 174*cdf0e10cSrcweir return PZSTREAM->total_out - nOldTotal_Out; 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir if ( mbInit == 0 ) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir mpIStm = &rIStm; 179*cdf0e10cSrcweir mpOStm = &rOStm; 180*cdf0e10cSrcweir ImplInitBuf( sal_True ); 181*cdf0e10cSrcweir PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ]; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir do 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir if ( PZSTREAM->avail_out == 0 ) ImplWriteBack(); 186*cdf0e10cSrcweir if ( PZSTREAM->avail_in == 0 && mnInToRead ) 187*cdf0e10cSrcweir { 188*cdf0e10cSrcweir nInToRead = ( mnInBufSize > mnInToRead ) ? mnInToRead : mnInBufSize; 189*cdf0e10cSrcweir PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, nInToRead ); 190*cdf0e10cSrcweir mnInToRead -= nInToRead; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir if ( mnCompressMethod & ZCODEC_UPDATE_CRC ) 193*cdf0e10cSrcweir mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead ); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir err = inflate( PZSTREAM, Z_NO_FLUSH ); 197*cdf0e10cSrcweir if ( err < 0 ) 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir mbStatus = sal_False; 200*cdf0e10cSrcweir break; 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir while ( ( err != Z_STREAM_END) && ( PZSTREAM->avail_in || mnInToRead ) ); 205*cdf0e10cSrcweir ImplWriteBack(); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir if ( err == Z_STREAM_END ) 208*cdf0e10cSrcweir mbFinish = sal_True; 209*cdf0e10cSrcweir return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir // ------------------------------------------------------------------------ 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir if ( mbInit == 0 ) 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir mpOStm = &rOStm; 219*cdf0e10cSrcweir ImplInitBuf( sal_False ); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir PZSTREAM->avail_in = nSize; 223*cdf0e10cSrcweir PZSTREAM->next_in = (unsigned char*)pData; 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir while ( PZSTREAM->avail_in || ( PZSTREAM->avail_out == 0 ) ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir if ( PZSTREAM->avail_out == 0 ) 228*cdf0e10cSrcweir ImplWriteBack(); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 ) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir mbStatus = sal_False; 233*cdf0e10cSrcweir break; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir return ( mbStatus ) ? (long)nSize : -1; 237*cdf0e10cSrcweir } 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // ------------------------------------------------------------------------ 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize ) 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir int err; 244*cdf0e10cSrcweir sal_uIntPtr nInToRead; 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir if ( mbFinish ) 247*cdf0e10cSrcweir return 0; // PZSTREAM->total_out; 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir mpIStm = &rIStm; 250*cdf0e10cSrcweir if ( mbInit == 0 ) 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir ImplInitBuf( sal_True ); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir PZSTREAM->avail_out = nSize; 255*cdf0e10cSrcweir PZSTREAM->next_out = pData; 256*cdf0e10cSrcweir do 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir if ( PZSTREAM->avail_in == 0 && mnInToRead ) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize; 261*cdf0e10cSrcweir PZSTREAM->avail_in = mpIStm->Read ( 262*cdf0e10cSrcweir PZSTREAM->next_in = mpInBuf, nInToRead); 263*cdf0e10cSrcweir mnInToRead -= nInToRead; 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir if ( mnCompressMethod & ZCODEC_UPDATE_CRC ) 266*cdf0e10cSrcweir mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead ); 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir } 269*cdf0e10cSrcweir err = inflate( PZSTREAM, Z_NO_FLUSH ); 270*cdf0e10cSrcweir if ( err < 0 ) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK. 273*cdf0e10cSrcweir mbStatus = (err == Z_BUF_ERROR); 274*cdf0e10cSrcweir break; 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir } 277*cdf0e10cSrcweir while ( (err != Z_STREAM_END) && 278*cdf0e10cSrcweir (PZSTREAM->avail_out != 0) && 279*cdf0e10cSrcweir (PZSTREAM->avail_in || mnInToRead) ); 280*cdf0e10cSrcweir if ( err == Z_STREAM_END ) 281*cdf0e10cSrcweir mbFinish = sal_True; 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1); 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir // ------------------------------------------------------------------------ 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize ) 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir int err = 0; 291*cdf0e10cSrcweir sal_uIntPtr nInToRead; 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir if ( mbFinish ) 294*cdf0e10cSrcweir return 0; // PZSTREAM->total_out; 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir if ( mbInit == 0 ) 297*cdf0e10cSrcweir { 298*cdf0e10cSrcweir mpIStm = &rIStm; 299*cdf0e10cSrcweir ImplInitBuf( sal_True ); 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir PZSTREAM->avail_out = nSize; 302*cdf0e10cSrcweir PZSTREAM->next_out = pData; 303*cdf0e10cSrcweir do 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir if ( PZSTREAM->avail_in == 0 && mnInToRead ) 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize; 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir sal_uIntPtr nStreamPos = rIStm.Tell(); 310*cdf0e10cSrcweir rIStm.Seek( STREAM_SEEK_TO_END ); 311*cdf0e10cSrcweir sal_uIntPtr nMaxPos = rIStm.Tell(); 312*cdf0e10cSrcweir rIStm.Seek( nStreamPos ); 313*cdf0e10cSrcweir if ( ( nMaxPos - nStreamPos ) < nInToRead ) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir rIStm.SetError( ERRCODE_IO_PENDING ); 316*cdf0e10cSrcweir err= ! Z_STREAM_END; // TODO What is appropriate code for this? 317*cdf0e10cSrcweir break; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir 320*cdf0e10cSrcweir PZSTREAM->avail_in = mpIStm->Read ( 321*cdf0e10cSrcweir PZSTREAM->next_in = mpInBuf, nInToRead); 322*cdf0e10cSrcweir mnInToRead -= nInToRead; 323*cdf0e10cSrcweir 324*cdf0e10cSrcweir if ( mnCompressMethod & ZCODEC_UPDATE_CRC ) 325*cdf0e10cSrcweir mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead ); 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir err = inflate( PZSTREAM, Z_NO_FLUSH ); 329*cdf0e10cSrcweir if ( err < 0 ) 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK. 332*cdf0e10cSrcweir mbStatus = (err == Z_BUF_ERROR); 333*cdf0e10cSrcweir break; 334*cdf0e10cSrcweir } 335*cdf0e10cSrcweir } 336*cdf0e10cSrcweir while ( (err != Z_STREAM_END) && 337*cdf0e10cSrcweir (PZSTREAM->avail_out != 0) && 338*cdf0e10cSrcweir (PZSTREAM->avail_in || mnInToRead) ); 339*cdf0e10cSrcweir if ( err == Z_STREAM_END ) 340*cdf0e10cSrcweir mbFinish = sal_True; 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1); 343*cdf0e10cSrcweir } 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir // ------------------------------------------------------------------------ 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir void ZCodec::ImplWriteBack() 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir sal_uIntPtr nAvail = mnOutBufSize - PZSTREAM->avail_out; 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir if ( nAvail ) 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir if ( mbInit & 2 && ( mnCompressMethod & ZCODEC_UPDATE_CRC ) ) 354*cdf0e10cSrcweir mnCRC = UpdateCRC( mnCRC, mpOutBuf, nAvail ); 355*cdf0e10cSrcweir mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail ); 356*cdf0e10cSrcweir PZSTREAM->avail_out = mnOutBufSize; 357*cdf0e10cSrcweir } 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir // ------------------------------------------------------------------------ 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir void ZCodec::SetBreak( sal_uIntPtr nInToRead ) 363*cdf0e10cSrcweir { 364*cdf0e10cSrcweir mnInToRead = nInToRead; 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir // ------------------------------------------------------------------------ 368*cdf0e10cSrcweir 369*cdf0e10cSrcweir sal_uIntPtr ZCodec::GetBreak( void ) 370*cdf0e10cSrcweir { 371*cdf0e10cSrcweir return ( mnInToRead + PZSTREAM->avail_in ); 372*cdf0e10cSrcweir } 373*cdf0e10cSrcweir 374*cdf0e10cSrcweir // ------------------------------------------------------------------------ 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir void ZCodec::SetCRC( sal_uIntPtr nCRC ) 377*cdf0e10cSrcweir { 378*cdf0e10cSrcweir mnCRC = nCRC; 379*cdf0e10cSrcweir } 380*cdf0e10cSrcweir 381*cdf0e10cSrcweir // ------------------------------------------------------------------------ 382*cdf0e10cSrcweir 383*cdf0e10cSrcweir sal_uIntPtr ZCodec::GetCRC() 384*cdf0e10cSrcweir { 385*cdf0e10cSrcweir return mnCRC; 386*cdf0e10cSrcweir } 387*cdf0e10cSrcweir 388*cdf0e10cSrcweir // ------------------------------------------------------------------------ 389*cdf0e10cSrcweir 390*cdf0e10cSrcweir void ZCodec::ImplInitBuf ( sal_Bool nIOFlag ) 391*cdf0e10cSrcweir { 392*cdf0e10cSrcweir if ( mbInit == 0 ) 393*cdf0e10cSrcweir { 394*cdf0e10cSrcweir if ( nIOFlag ) 395*cdf0e10cSrcweir { 396*cdf0e10cSrcweir mbInit = 1; 397*cdf0e10cSrcweir if ( mbStatus && ( mnCompressMethod & ZCODEC_GZ_LIB ) ) 398*cdf0e10cSrcweir { 399*cdf0e10cSrcweir sal_uInt8 n1, n2, j, nMethod, nFlags; 400*cdf0e10cSrcweir for ( int i = 0; i < 2; i++ ) // gz - magic number 401*cdf0e10cSrcweir { 402*cdf0e10cSrcweir *mpIStm >> j; 403*cdf0e10cSrcweir if ( j != gz_magic[ i ] ) 404*cdf0e10cSrcweir mbStatus = sal_False; 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir *mpIStm >> nMethod; 407*cdf0e10cSrcweir *mpIStm >> nFlags; 408*cdf0e10cSrcweir if ( nMethod != Z_DEFLATED ) 409*cdf0e10cSrcweir mbStatus = sal_False; 410*cdf0e10cSrcweir if ( ( nFlags & GZ_RESERVED ) != 0 ) 411*cdf0e10cSrcweir mbStatus = sal_False; 412*cdf0e10cSrcweir /* Discard time, xflags and OS code: */ 413*cdf0e10cSrcweir mpIStm->SeekRel( 6 ); 414*cdf0e10cSrcweir /* skip the extra field */ 415*cdf0e10cSrcweir if ( nFlags & GZ_EXTRA_FIELD ) 416*cdf0e10cSrcweir { 417*cdf0e10cSrcweir *mpIStm >> n1 >> n2; 418*cdf0e10cSrcweir mpIStm->SeekRel( n1 + ( n2 << 8 ) ); 419*cdf0e10cSrcweir } 420*cdf0e10cSrcweir /* skip the original file name */ 421*cdf0e10cSrcweir if ( nFlags & GZ_ORIG_NAME) 422*cdf0e10cSrcweir { 423*cdf0e10cSrcweir do 424*cdf0e10cSrcweir { 425*cdf0e10cSrcweir *mpIStm >> j; 426*cdf0e10cSrcweir } 427*cdf0e10cSrcweir while ( j && !mpIStm->IsEof() ); 428*cdf0e10cSrcweir } 429*cdf0e10cSrcweir /* skip the .gz file comment */ 430*cdf0e10cSrcweir if ( nFlags & GZ_COMMENT ) 431*cdf0e10cSrcweir { 432*cdf0e10cSrcweir do 433*cdf0e10cSrcweir { 434*cdf0e10cSrcweir *mpIStm >> j; 435*cdf0e10cSrcweir } 436*cdf0e10cSrcweir while ( j && !mpIStm->IsEof() ); 437*cdf0e10cSrcweir } 438*cdf0e10cSrcweir /* skip the header crc */ 439*cdf0e10cSrcweir if ( nFlags & GZ_HEAD_CRC ) 440*cdf0e10cSrcweir mpIStm->SeekRel( 2 ); 441*cdf0e10cSrcweir if ( mbStatus ) 442*cdf0e10cSrcweir mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? sal_False : sal_True; 443*cdf0e10cSrcweir } 444*cdf0e10cSrcweir else 445*cdf0e10cSrcweir { 446*cdf0e10cSrcweir mbStatus = ( inflateInit( PZSTREAM ) >= 0 ); 447*cdf0e10cSrcweir } 448*cdf0e10cSrcweir mpInBuf = new sal_uInt8[ mnInBufSize ]; 449*cdf0e10cSrcweir } 450*cdf0e10cSrcweir else 451*cdf0e10cSrcweir { 452*cdf0e10cSrcweir mbInit = 3; 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir mbStatus = ( deflateInit2_( PZSTREAM, mnCompressMethod & 0xff, Z_DEFLATED, 455*cdf0e10cSrcweir MAX_WBITS, mnMemUsage, ( mnCompressMethod >> 8 ) & 0xff, 456*cdf0e10cSrcweir ZLIB_VERSION, sizeof( z_stream ) ) >= 0 ); 457*cdf0e10cSrcweir 458*cdf0e10cSrcweir PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ]; 459*cdf0e10cSrcweir } 460*cdf0e10cSrcweir } 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir // ------------------------------------------------------------------------ 464*cdf0e10cSrcweir 465*cdf0e10cSrcweir sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uIntPtr nNumber ) 466*cdf0e10cSrcweir { 467*cdf0e10cSrcweir 468*cdf0e10cSrcweir #ifdef OSL_LITENDIAN 469*cdf0e10cSrcweir nNumber = SWAPLONG( nNumber ); 470*cdf0e10cSrcweir #endif 471*cdf0e10cSrcweir return rtl_crc32( nLatestCRC, &nNumber, 4 ); 472*cdf0e10cSrcweir } 473*cdf0e10cSrcweir 474*cdf0e10cSrcweir // ------------------------------------------------------------------------ 475*cdf0e10cSrcweir 476*cdf0e10cSrcweir sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uInt8* pSource, long nDatSize) 477*cdf0e10cSrcweir { 478*cdf0e10cSrcweir return rtl_crc32( nLatestCRC, pSource, nDatSize ); 479*cdf0e10cSrcweir } 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir // ------------------------------------------------------------------------ 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir void GZCodec::BeginCompression( sal_uIntPtr nCompressMethod ) 484*cdf0e10cSrcweir { 485*cdf0e10cSrcweir ZCodec::BeginCompression( nCompressMethod | ZCODEC_GZ_LIB ); 486*cdf0e10cSrcweir }; 487*cdf0e10cSrcweir 488*cdf0e10cSrcweir 489