LCOV - code coverage report
Current view: top level - tools/source/zcodec - zcodec.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 210 0.0 %
Date: 2014-04-14 Functions: 0 18 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <tools/stream.hxx>
      21             : 
      22             : #include <zlib.h>
      23             : 
      24             : #include <tools/zcodec.hxx>
      25             : #include <rtl/crc.h>
      26             : #include <osl/endian.h>
      27             : 
      28             : #define PZSTREAM ((z_stream*) mpsC_Stream)
      29             : 
      30             : /* gzip flag byte */
      31             : //      GZ_ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
      32             : #define GZ_HEAD_CRC     0x02 /* bit 1 set: header CRC present */
      33             : #define GZ_EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
      34             : #define GZ_ORIG_NAME    0x08 /* bit 3 set: original file name present */
      35             : #define GZ_COMMENT      0x10 /* bit 4 set: file comment present */
      36             : #define GZ_RESERVED     0xE0 /* bits 5..7: reserved */
      37             : 
      38             : static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
      39             : 
      40           0 : ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize, sal_uIntPtr nMemUsage )
      41             :     : mbInit(0)
      42             :     , mbStatus(false)
      43             :     , mbFinish(false)
      44             :     , mnMemUsage(nMemUsage)
      45             :     , mpIStm(NULL)
      46             :     , mpInBuf(NULL)
      47             :     , mnInBufSize(nInBufSize)
      48             :     , mnInToRead(0)
      49             :     , mpOStm(NULL)
      50             :     , mpOutBuf(NULL)
      51             :     , mnOutBufSize(nOutBufSize)
      52             :     , mnCRC(0)
      53           0 :     , mnCompressMethod(0)
      54             : {
      55           0 :     mpsC_Stream = new z_stream;
      56           0 : }
      57             : 
      58           0 : ZCodec::ZCodec()
      59             :     : mbInit(0)
      60             :     , mbStatus(false)
      61             :     , mbFinish(false)
      62             :     , mnMemUsage(MAX_MEM_USAGE)
      63             :     , mpIStm(NULL)
      64             :     , mpInBuf(NULL)
      65             :     , mnInBufSize(DEFAULT_IN_BUFSIZE)
      66             :     , mnInToRead(0)
      67             :     , mpOStm(NULL)
      68             :     , mpOutBuf(NULL)
      69             :     , mnOutBufSize(DEFAULT_OUT_BUFSIZE)
      70             :     , mnCRC(0)
      71           0 :     , mnCompressMethod(0)
      72             : {
      73           0 :     mpsC_Stream = new z_stream;
      74           0 : }
      75             : 
      76           0 : ZCodec::~ZCodec()
      77             : {
      78           0 :     delete (z_stream*) mpsC_Stream;
      79           0 : }
      80             : 
      81           0 : void ZCodec::BeginCompression( sal_uIntPtr nCompressMethod )
      82             : {
      83           0 :     mbInit = 0;
      84           0 :     mbStatus = true;
      85           0 :     mbFinish = false;
      86           0 :     mpIStm = mpOStm = NULL;
      87           0 :     mnInToRead = 0xffffffff;
      88           0 :     mpInBuf = mpOutBuf = NULL;
      89           0 :     PZSTREAM->total_out = PZSTREAM->total_in = 0;
      90           0 :     mnCompressMethod = nCompressMethod;
      91           0 :     PZSTREAM->zalloc = ( alloc_func )0;
      92           0 :     PZSTREAM->zfree = ( free_func )0;
      93           0 :     PZSTREAM->opaque = ( voidpf )0;
      94           0 :     PZSTREAM->avail_out = PZSTREAM->avail_in = 0;
      95           0 : }
      96             : 
      97           0 : long ZCodec::EndCompression()
      98             : {
      99           0 :     long retvalue = 0;
     100             : 
     101           0 :     if ( mbInit != 0 )
     102             :     {
     103           0 :         if ( mbInit & 2 )   // 1->decompress, 3->compress
     104             :         {
     105           0 :             do
     106             :             {
     107           0 :                 ImplWriteBack();
     108             :             }
     109           0 :             while ( deflate( PZSTREAM, Z_FINISH ) != Z_STREAM_END );
     110             : 
     111           0 :             ImplWriteBack();
     112             : 
     113           0 :             retvalue = PZSTREAM->total_in;
     114           0 :             deflateEnd( PZSTREAM );
     115             :         }
     116             :         else
     117             :         {
     118           0 :             retvalue = PZSTREAM->total_out;
     119           0 :             inflateEnd( PZSTREAM );
     120             :         }
     121           0 :         delete[] mpOutBuf;
     122           0 :         delete[] mpInBuf;
     123             :     }
     124           0 :     return ( mbStatus ) ? retvalue : -1;
     125             : }
     126             : 
     127           0 : long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
     128             : {
     129           0 :     long nOldTotal_In = PZSTREAM->total_in;
     130             : 
     131           0 :     if ( mbInit == 0 )
     132             :     {
     133           0 :         mpIStm = &rIStm;
     134           0 :         mpOStm = &rOStm;
     135           0 :         ImplInitBuf( false );
     136           0 :         mpInBuf = new sal_uInt8[ mnInBufSize ];
     137             :     }
     138           0 :     while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
     139             :     {
     140           0 :         if ( PZSTREAM->avail_out == 0 )
     141           0 :             ImplWriteBack();
     142           0 :         if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
     143             :         {
     144           0 :             mbStatus = false;
     145           0 :             break;
     146             :         }
     147             :     };
     148           0 :     return ( mbStatus ) ? (long)(PZSTREAM->total_in - nOldTotal_In) : -1;
     149             : }
     150             : 
     151           0 : long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
     152             : {
     153             :     int err;
     154             :     sal_uIntPtr nInToRead;
     155           0 :     long    nOldTotal_Out = PZSTREAM->total_out;
     156             : 
     157           0 :     if ( mbFinish )
     158           0 :         return PZSTREAM->total_out - nOldTotal_Out;
     159             : 
     160           0 :     if ( mbInit == 0 )
     161             :     {
     162           0 :         mpIStm = &rIStm;
     163           0 :         mpOStm = &rOStm;
     164           0 :         ImplInitBuf( true );
     165           0 :         PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
     166             :     }
     167           0 :     do
     168             :     {
     169           0 :         if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
     170           0 :         if ( PZSTREAM->avail_in == 0 && mnInToRead )
     171             :         {
     172           0 :             nInToRead = ( mnInBufSize > mnInToRead ) ? mnInToRead : mnInBufSize;
     173           0 :             PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, nInToRead );
     174           0 :             mnInToRead -= nInToRead;
     175             : 
     176           0 :             if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
     177           0 :                 mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
     178             : 
     179             :         }
     180           0 :         err = inflate( PZSTREAM, Z_NO_FLUSH );
     181           0 :         if ( err < 0 )
     182             :         {
     183           0 :             mbStatus = false;
     184           0 :             break;
     185             :         }
     186             : 
     187             :     }
     188           0 :     while ( ( err != Z_STREAM_END)  && ( PZSTREAM->avail_in || mnInToRead ) );
     189           0 :     ImplWriteBack();
     190             : 
     191           0 :     if ( err == Z_STREAM_END )
     192           0 :         mbFinish = true;
     193           0 :     return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
     194             : }
     195             : 
     196           0 : long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
     197             : {
     198           0 :     if ( mbInit == 0 )
     199             :     {
     200           0 :         mpOStm = &rOStm;
     201           0 :         ImplInitBuf( false );
     202             :     }
     203             : 
     204           0 :     PZSTREAM->avail_in = nSize;
     205           0 :     PZSTREAM->next_in = (unsigned char*)pData;
     206             : 
     207           0 :     while ( PZSTREAM->avail_in || ( PZSTREAM->avail_out == 0 ) )
     208             :     {
     209           0 :         if ( PZSTREAM->avail_out == 0 )
     210           0 :             ImplWriteBack();
     211             : 
     212           0 :         if ( deflate( PZSTREAM, Z_NO_FLUSH ) < 0 )
     213             :         {
     214           0 :             mbStatus = false;
     215           0 :             break;
     216             :         }
     217             :     }
     218           0 :     return ( mbStatus ) ? (long)nSize : -1;
     219             : }
     220             : 
     221           0 : long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
     222             : {
     223             :     int err;
     224             :     sal_uIntPtr nInToRead;
     225             : 
     226           0 :     if ( mbFinish )
     227           0 :         return 0;           // PZSTREAM->total_out;
     228             : 
     229           0 :     mpIStm = &rIStm;
     230           0 :     if ( mbInit == 0 )
     231             :     {
     232           0 :         ImplInitBuf( true );
     233             :     }
     234           0 :     PZSTREAM->avail_out = nSize;
     235           0 :     PZSTREAM->next_out = pData;
     236           0 :     do
     237             :     {
     238           0 :         if ( PZSTREAM->avail_in == 0 && mnInToRead )
     239             :         {
     240           0 :             nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
     241             :             PZSTREAM->avail_in = mpIStm->Read (
     242           0 :                 PZSTREAM->next_in = mpInBuf, nInToRead);
     243           0 :             mnInToRead -= nInToRead;
     244             : 
     245           0 :             if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
     246           0 :                 mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
     247             : 
     248             :         }
     249           0 :         err = inflate( PZSTREAM, Z_NO_FLUSH );
     250           0 :         if ( err < 0 )
     251             :         {
     252             :             // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
     253           0 :             mbStatus = (err == Z_BUF_ERROR);
     254           0 :             break;
     255             :         }
     256             :     }
     257           0 :     while ( (err != Z_STREAM_END) &&
     258           0 :             (PZSTREAM->avail_out != 0) &&
     259           0 :             (PZSTREAM->avail_in || mnInToRead) );
     260           0 :     if ( err == Z_STREAM_END )
     261           0 :         mbFinish = true;
     262             : 
     263           0 :     return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
     264             : }
     265             : 
     266           0 : long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
     267             : {
     268           0 :     int err = 0;
     269             :     sal_uIntPtr nInToRead;
     270             : 
     271           0 :     if ( mbFinish )
     272           0 :         return 0;           // PZSTREAM->total_out;
     273             : 
     274           0 :     if ( mbInit == 0 )
     275             :     {
     276           0 :         mpIStm = &rIStm;
     277           0 :         ImplInitBuf( true );
     278             :     }
     279           0 :     PZSTREAM->avail_out = nSize;
     280           0 :     PZSTREAM->next_out = pData;
     281           0 :     do
     282             :     {
     283           0 :         if ( PZSTREAM->avail_in == 0 && mnInToRead )
     284             :         {
     285           0 :             nInToRead = (mnInBufSize > mnInToRead) ? mnInToRead : mnInBufSize;
     286             : 
     287           0 :             sal_uIntPtr nStreamPos = rIStm.Tell();
     288           0 :             rIStm.Seek( STREAM_SEEK_TO_END );
     289           0 :             sal_uIntPtr nMaxPos = rIStm.Tell();
     290           0 :             rIStm.Seek( nStreamPos );
     291           0 :             if ( ( nMaxPos - nStreamPos ) < nInToRead )
     292             :             {
     293           0 :                 rIStm.SetError( ERRCODE_IO_PENDING );
     294           0 :                 err= int(!Z_STREAM_END); // TODO What is appropriate code for this?
     295           0 :                 break;
     296             :             }
     297             : 
     298             :             PZSTREAM->avail_in = mpIStm->Read (
     299           0 :                 PZSTREAM->next_in = mpInBuf, nInToRead);
     300           0 :             mnInToRead -= nInToRead;
     301             : 
     302           0 :             if ( mnCompressMethod & ZCODEC_UPDATE_CRC )
     303           0 :                 mnCRC = UpdateCRC( mnCRC, mpInBuf, nInToRead );
     304             : 
     305             :         }
     306           0 :         err = inflate( PZSTREAM, Z_NO_FLUSH );
     307           0 :         if ( err < 0 )
     308             :         {
     309             :             // Accept Z_BUF_ERROR as EAGAIN or EWOULDBLOCK.
     310           0 :             mbStatus = (err == Z_BUF_ERROR);
     311           0 :             break;
     312             :         }
     313             :     }
     314           0 :     while ( (err != Z_STREAM_END) &&
     315           0 :             (PZSTREAM->avail_out != 0) &&
     316           0 :             (PZSTREAM->avail_in || mnInToRead) );
     317           0 :     if ( err == Z_STREAM_END )
     318           0 :         mbFinish = true;
     319             : 
     320           0 :     return (mbStatus ? (long)(nSize - PZSTREAM->avail_out) : -1);
     321             : }
     322             : 
     323           0 : void ZCodec::ImplWriteBack()
     324             : {
     325           0 :     sal_uIntPtr nAvail = mnOutBufSize - PZSTREAM->avail_out;
     326             : 
     327           0 :     if ( nAvail )
     328             :     {
     329           0 :         if ( mbInit & 2 && ( mnCompressMethod & ZCODEC_UPDATE_CRC ) )
     330           0 :             mnCRC = UpdateCRC( mnCRC, mpOutBuf, nAvail );
     331           0 :         mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
     332           0 :         PZSTREAM->avail_out = mnOutBufSize;
     333             :     }
     334           0 : }
     335             : 
     336           0 : void ZCodec::SetBreak( sal_uIntPtr nInToRead )
     337             : {
     338           0 :     mnInToRead = nInToRead;
     339           0 : }
     340             : 
     341           0 : sal_uIntPtr ZCodec::GetBreak( void )
     342             : {
     343           0 :     return ( mnInToRead + PZSTREAM->avail_in );
     344             : }
     345             : 
     346           0 : void ZCodec::SetCRC( sal_uIntPtr nCRC )
     347             : {
     348           0 :     mnCRC = nCRC;
     349           0 : }
     350             : 
     351           0 : sal_uIntPtr ZCodec::GetCRC()
     352             : {
     353           0 :     return mnCRC;
     354             : }
     355             : 
     356           0 : void ZCodec::ImplInitBuf ( bool nIOFlag )
     357             : {
     358           0 :     if ( mbInit == 0 )
     359             :     {
     360           0 :         if ( nIOFlag )
     361             :         {
     362           0 :             mbInit = 1;
     363           0 :             if ( mbStatus && ( mnCompressMethod & ZCODEC_GZ_LIB ) )
     364             :             {
     365             :                 sal_uInt8 n1, n2, j, nMethod, nFlags;
     366           0 :                 for ( int i = 0; i < 2; i++ )   // gz - magic number
     367             :                 {
     368           0 :                     mpIStm->ReadUChar( j );
     369           0 :                     if ( j != gz_magic[ i ] )
     370           0 :                         mbStatus = false;
     371             :                 }
     372           0 :                 mpIStm->ReadUChar( nMethod );
     373           0 :                 mpIStm->ReadUChar( nFlags );
     374           0 :                 if ( nMethod != Z_DEFLATED )
     375           0 :                     mbStatus = false;
     376           0 :                 if ( ( nFlags & GZ_RESERVED ) != 0 )
     377           0 :                     mbStatus = false;
     378             :                 /* Discard time, xflags and OS code: */
     379           0 :                 mpIStm->SeekRel( 6 );
     380             :                 /* skip the extra field */
     381           0 :                 if ( nFlags & GZ_EXTRA_FIELD )
     382             :                 {
     383           0 :                     mpIStm->ReadUChar( n1 ).ReadUChar( n2 );
     384           0 :                     mpIStm->SeekRel( n1 + ( n2 << 8 ) );
     385             :                 }
     386             :                 /* skip the original file name */
     387           0 :                 if ( nFlags & GZ_ORIG_NAME)
     388             :                 {
     389           0 :                     do
     390             :                     {
     391           0 :                         mpIStm->ReadUChar( j );
     392             :                     }
     393           0 :                     while ( j && !mpIStm->IsEof() );
     394             :                 }
     395             :                 /* skip the .gz file comment */
     396           0 :                 if ( nFlags & GZ_COMMENT )
     397             :                 {
     398           0 :                     do
     399             :                     {
     400           0 :                         mpIStm->ReadUChar( j );
     401             :                     }
     402           0 :                     while ( j && !mpIStm->IsEof() );
     403             :                 }
     404             :                 /* skip the header crc */
     405           0 :                 if ( nFlags & GZ_HEAD_CRC )
     406           0 :                     mpIStm->SeekRel( 2 );
     407           0 :                 if ( mbStatus )
     408           0 :                     mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? false : true;
     409             :             }
     410             :             else
     411             :             {
     412           0 :                 mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
     413             :             }
     414           0 :             mpInBuf = new sal_uInt8[ mnInBufSize ];
     415             :         }
     416             :         else
     417             :         {
     418           0 :             mbInit = 3;
     419             : 
     420             :             mbStatus = ( deflateInit2_( PZSTREAM, mnCompressMethod & 0xff, Z_DEFLATED,
     421           0 :                 MAX_WBITS, mnMemUsage, ( mnCompressMethod >> 8 ) & 0xff,
     422           0 :                     ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );
     423             : 
     424           0 :             PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
     425             :         }
     426             :     }
     427           0 : }
     428             : 
     429           0 : sal_uIntPtr ZCodec::UpdateCRC ( sal_uIntPtr nLatestCRC, sal_uInt8* pSource, long nDatSize)
     430             : {
     431           0 :     return rtl_crc32( nLatestCRC, pSource, nDatSize );
     432             : }
     433             : 
     434             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10