LCOV - code coverage report
Current view: top level - package/source/zipapi - Deflater.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 58 81 71.6 %
Date: 2014-04-11 Functions: 14 14 100.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 <package/Deflater.hxx>
      21             : #include <zlib.h>
      22             : #include <com/sun/star/packages/zip/ZipConstants.hpp>
      23             : #include <string.h>
      24             : 
      25             : using namespace com::sun::star::packages::zip::ZipConstants;
      26             : using namespace com::sun::star;
      27             : using namespace ZipUtils;
      28             : 
      29             : /** Provides general purpose compression using the ZLIB compression
      30             :  * library.
      31             :  */
      32             : 
      33         964 : Deflater::~Deflater(void)
      34             : {
      35         482 :     end();
      36         482 : }
      37         482 : void Deflater::init (sal_Int32 nLevelArg, sal_Int32 nStrategyArg, bool bNowrap)
      38             : {
      39         482 :     pStream = new z_stream;
      40             :     /* Memset it to 0...sets zalloc/zfree/opaque to NULL */
      41         482 :     memset (pStream, 0, sizeof(*pStream));
      42             : 
      43         482 :     switch (deflateInit2(pStream, nLevelArg, Z_DEFLATED, bNowrap? -MAX_WBITS : MAX_WBITS,
      44             :                 DEF_MEM_LEVEL, nStrategyArg))
      45             :     {
      46             :         case Z_OK:
      47         482 :             break;
      48             :         case Z_MEM_ERROR:
      49           0 :             delete pStream;
      50           0 :             break;
      51             :         case Z_STREAM_ERROR:
      52           0 :             delete pStream;
      53           0 :             break;
      54             :         default:
      55           0 :              break;
      56             :     }
      57         482 : }
      58             : 
      59         482 : Deflater::Deflater(sal_Int32 nSetLevel, bool bNowrap)
      60             : : bFinish(false)
      61             : , bFinished(false)
      62             : , bSetParams(false)
      63             : , nLevel(nSetLevel)
      64             : , nStrategy(DEFAULT_STRATEGY)
      65             : , nOffset(0)
      66         482 : , nLength(0)
      67             : {
      68         482 :     init(nSetLevel, DEFAULT_STRATEGY, bNowrap);
      69         482 : }
      70             : 
      71       11754 : sal_Int32 Deflater::doDeflateBytes (uno::Sequence < sal_Int8 > &rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength)
      72             : {
      73             :     sal_Int32 nResult;
      74       11754 :     if (bSetParams)
      75             :     {
      76           0 :         pStream->next_in   = (unsigned char*) sInBuffer.getConstArray() + nOffset;
      77           0 :         pStream->next_out  = (unsigned char*) rBuffer.getArray()+nNewOffset;
      78           0 :         pStream->avail_in  = nLength;
      79           0 :         pStream->avail_out = nNewLength;
      80             : 
      81             : #if !defined Z_PREFIX
      82           0 :         nResult = deflateParams(pStream, nLevel, nStrategy);
      83             : #else
      84             :         nResult = z_deflateParams(pStream, nLevel, nStrategy);
      85             : #endif
      86           0 :         switch (nResult)
      87             :         {
      88             :             case Z_OK:
      89           0 :                 bSetParams = false;
      90           0 :                 nOffset += nLength - pStream->avail_in;
      91           0 :                 nLength = pStream->avail_in;
      92           0 :                 return nNewLength - pStream->avail_out;
      93             :             case Z_BUF_ERROR:
      94           0 :                 bSetParams = false;
      95           0 :                 return 0;
      96             :             default:
      97           0 :                 return 0;
      98             :         }
      99             :     }
     100             :     else
     101             :     {
     102       11754 :         pStream->next_in   = (unsigned char*) sInBuffer.getConstArray() + nOffset;
     103       11754 :         pStream->next_out  = (unsigned char*) rBuffer.getArray()+nNewOffset;
     104       11754 :         pStream->avail_in  = nLength;
     105       11754 :         pStream->avail_out = nNewLength;
     106             : 
     107             : #if !defined Z_PREFIX
     108       11754 :         nResult = deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
     109             : #else
     110             :         nResult = z_deflate(pStream, bFinish ? Z_FINISH : Z_NO_FLUSH);
     111             : #endif
     112       11754 :         switch (nResult)
     113             :         {
     114             :             case Z_STREAM_END:
     115        5756 :                 bFinished = true;
     116             :             case Z_OK:
     117       11754 :                 nOffset += nLength - pStream->avail_in;
     118       11754 :                 nLength = pStream->avail_in;
     119       11754 :                 return nNewLength - pStream->avail_out;
     120             :             case Z_BUF_ERROR:
     121           0 :                 bSetParams = false;
     122           0 :                 return 0;
     123             :             default:
     124           0 :                 return 0;
     125             :         }
     126             :     }
     127             : }
     128             : 
     129        6038 : void SAL_CALL Deflater::setInputSegment( const uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
     130             : {
     131             :     OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
     132             : 
     133        6038 :     sInBuffer = rBuffer;
     134        6038 :     nOffset = nNewOffset;
     135        6038 :     nLength = nNewLength;
     136        6038 : }
     137         482 : void SAL_CALL Deflater::setLevel( sal_Int32 nNewLevel )
     138             : {
     139             :     if ((nNewLevel < 0 || nNewLevel > 9) && nNewLevel != DEFAULT_COMPRESSION)
     140             :     {
     141             :         // do error handling
     142             :     }
     143         482 :     if (nNewLevel != nLevel)
     144             :     {
     145           0 :         nLevel = nNewLevel;
     146           0 :         bSetParams = true;
     147             :     }
     148         482 : }
     149       12036 : bool SAL_CALL Deflater::needsInput(  )
     150             : {
     151       12036 :     return nLength <=0;
     152             : }
     153        5756 : void SAL_CALL Deflater::finish(  )
     154             : {
     155        5756 :     bFinish = true;
     156        5756 : }
     157       29304 : bool SAL_CALL Deflater::finished(  )
     158             : {
     159       29304 :     return bFinished;
     160             : }
     161       11754 : sal_Int32 SAL_CALL Deflater::doDeflateSegment( uno::Sequence< sal_Int8 >& rBuffer, sal_Int32 nNewOffset, sal_Int32 nNewLength )
     162             : {
     163             :     OSL_ASSERT( !(nNewOffset < 0 || nNewLength < 0 || nNewOffset + nNewLength > rBuffer.getLength()));
     164       11754 :     return doDeflateBytes(rBuffer, nNewOffset, nNewLength);
     165             : }
     166        5751 : sal_Int64 SAL_CALL Deflater::getTotalIn(  )
     167             : {
     168        5751 :     return pStream->total_in; // FIXME64: zlib doesn't look 64bit clean here
     169             : }
     170        5751 : sal_Int64 SAL_CALL Deflater::getTotalOut(  )
     171             : {
     172        5751 :     return pStream->total_out; // FIXME64: zlib doesn't look 64bit clean here
     173             : }
     174        5756 : void SAL_CALL Deflater::reset(  )
     175             : {
     176             : #if !defined Z_PREFIX
     177        5756 :     deflateReset(pStream);
     178             : #else
     179             :     z_deflateReset(pStream);
     180             : #endif
     181        5756 :     bFinish = false;
     182        5756 :     bFinished = false;
     183        5756 :     nOffset = nLength = 0;
     184        5756 : }
     185         482 : void SAL_CALL Deflater::end(  )
     186             : {
     187         482 :     if (pStream != NULL)
     188             :     {
     189             : #if !defined Z_PREFIX
     190         482 :         deflateEnd(pStream);
     191             : #else
     192             :         z_deflateEnd(pStream);
     193             : #endif
     194         482 :         delete pStream;
     195             :     }
     196         482 :     pStream = NULL;
     197         482 : }
     198             : 
     199             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10