LCOV - code coverage report
Current view: top level - vcl/source/filter/igif - decode.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 91 91 100.0 %
Date: 2014-04-11 Functions: 5 5 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 "decode.hxx"
      21             : 
      22             : struct GIFLZWTableEntry
      23             : {
      24             :     GIFLZWTableEntry*   pPrev;
      25             :     GIFLZWTableEntry*   pFirst;
      26             :     sal_uInt8               nData;
      27             : };
      28             : 
      29          99 : GIFLZWDecompressor::GIFLZWDecompressor(sal_uInt8 cDataSize)
      30             :     : pBlockBuf(NULL)
      31             :     , nInputBitsBuf(0)
      32             :     , nOutBufDataLen(0)
      33             :     , nInputBitsBufSize(0)
      34             :     , bEOIFound(false)
      35             :     , nDataSize(cDataSize)
      36             :     , nBlockBufSize(0)
      37          99 :     , nBlockBufPos(0)
      38             : {
      39          99 :     pOutBuf = new sal_uInt8[ 4096 ];
      40             : 
      41          99 :     nClearCode = 1 << nDataSize;
      42          99 :     nEOICode = nClearCode + 1;
      43          99 :     nTableSize = nEOICode + 1;
      44          99 :     nCodeSize = nDataSize + 1;
      45          99 :     nOldCode = 0xffff;
      46          99 :     pOutBufData = pOutBuf + 4096;
      47             : 
      48          99 :     pTable = new GIFLZWTableEntry[ 4098 ];
      49             : 
      50       13753 :     for( sal_uInt16 i = 0; i < nTableSize; i++ )
      51             :     {
      52       13654 :         pTable[i].pPrev = NULL;
      53       13654 :         pTable[i].pFirst = pTable + i;
      54       13654 :         pTable[i].nData = (sal_uInt8) i;
      55             :     }
      56          99 : }
      57             : 
      58          99 : GIFLZWDecompressor::~GIFLZWDecompressor()
      59             : {
      60          99 :     delete[] pOutBuf;
      61          99 :     delete[] pTable;
      62          99 : }
      63             : 
      64        1017 : HPBYTE GIFLZWDecompressor::DecompressBlock( HPBYTE pSrc, sal_uInt8 cBufSize,
      65             :                                             sal_uLong& rCount, bool& rEOI )
      66             : {
      67        1017 :     sal_uLong   nTargetSize = 4096;
      68        1017 :     sal_uLong   nCount = 0;
      69        1017 :     HPBYTE  pTarget = (HPBYTE) rtl_allocateMemory( nTargetSize );
      70        1017 :     HPBYTE  pTmpTarget = pTarget;
      71             : 
      72        1017 :     nBlockBufSize = cBufSize;
      73        1017 :     nBlockBufPos = 0;
      74        1017 :     pBlockBuf = pSrc;
      75             : 
      76      181981 :     while( ProcessOneCode() )
      77             :     {
      78      180046 :         nCount += nOutBufDataLen;
      79             : 
      80      180046 :         if( nCount > nTargetSize )
      81             :         {
      82         632 :             sal_uLong   nNewSize = nTargetSize << 1;
      83         632 :             sal_uLong   nOffset = pTmpTarget - pTarget;
      84         632 :             HPBYTE  pTmp = (HPBYTE) rtl_allocateMemory( nNewSize );
      85             : 
      86         632 :             memcpy( pTmp, pTarget, nTargetSize );
      87         632 :             rtl_freeMemory( pTarget );
      88             : 
      89         632 :             nTargetSize = nNewSize;
      90         632 :             pTmpTarget = ( pTarget = pTmp ) + nOffset;
      91             :         }
      92             : 
      93      180046 :         memcpy( pTmpTarget, pOutBufData, nOutBufDataLen );
      94      180046 :         pTmpTarget += nOutBufDataLen;
      95      180046 :         pOutBufData += nOutBufDataLen;
      96      180046 :         nOutBufDataLen = 0;
      97             : 
      98      180046 :         if ( bEOIFound )
      99          99 :             break;
     100             :     }
     101             : 
     102        1017 :     rCount = nCount;
     103        1017 :     rEOI = bEOIFound;
     104             : 
     105        1017 :     return pTarget;
     106             : }
     107             : 
     108      179685 : void GIFLZWDecompressor::AddToTable( sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData )
     109             : {
     110             :     GIFLZWTableEntry* pE;
     111             : 
     112      179685 :     if( nTableSize < 4096 )
     113             :     {
     114      179685 :         pE = pTable + nTableSize;
     115      179685 :         pE->pPrev = pTable + nPrevCode;
     116      179685 :         pE->pFirst = pE->pPrev->pFirst;
     117      179685 :         pE->nData = pTable[ nCodeFirstData ].pFirst->nData;
     118      179685 :         nTableSize++;
     119             : 
     120      179685 :         if ( ( nTableSize == (sal_uInt16) (1 << nCodeSize) ) && ( nTableSize < 4096 ) )
     121         309 :             nCodeSize++;
     122             :     }
     123      179685 : }
     124             : 
     125      180964 : bool GIFLZWDecompressor::ProcessOneCode()
     126             : {
     127             :     GIFLZWTableEntry*   pE;
     128             :     sal_uInt16              nCode;
     129      180964 :     bool                bRet = false;
     130      180964 :     bool                bEndOfBlock = false;
     131             : 
     132      605294 :     while( nInputBitsBufSize < nCodeSize )
     133             :     {
     134      244284 :         if( nBlockBufPos >= nBlockBufSize )
     135             :         {
     136         918 :             bEndOfBlock = true;
     137         918 :             break;
     138             :         }
     139             : 
     140      243366 :         nInputBitsBuf |= ( (sal_uLong) pBlockBuf[ nBlockBufPos++ ] ) << nInputBitsBufSize;
     141      243366 :         nInputBitsBufSize += 8;
     142             :     }
     143             : 
     144      180964 :     if ( !bEndOfBlock )
     145             :     {
     146             :         // fetch code from input buffer
     147             :         nCode = sal::static_int_cast< sal_uInt16 >(
     148      180046 :             ( (sal_uInt16) nInputBitsBuf ) & ( ~( 0xffff << nCodeSize ) ));
     149      180046 :         nInputBitsBuf >>= nCodeSize;
     150      180046 :         nInputBitsBufSize = nInputBitsBufSize - nCodeSize;
     151             : 
     152      180046 :         if ( nCode < nClearCode )
     153             :         {
     154       28809 :             if ( nOldCode != 0xffff )
     155       28678 :                 AddToTable( nOldCode, nCode );
     156             :         }
     157      151237 :         else if ( ( nCode > nEOICode ) && ( nCode <= nTableSize ) )
     158             :         {
     159      302014 :             if ( nCode == nTableSize )
     160        9556 :                 AddToTable( nOldCode, nOldCode );
     161             :             else
     162      141451 :                 AddToTable( nOldCode, nCode );
     163             :         }
     164             :         else
     165             :         {
     166         230 :             if ( nCode == nClearCode )
     167             :             {
     168         131 :                 nTableSize = nEOICode + 1;
     169         131 :                 nCodeSize = nDataSize + 1;
     170         131 :                 nOldCode = 0xffff;
     171         131 :                 nOutBufDataLen = 0;
     172             :             }
     173             :             else
     174          99 :                 bEOIFound = true;
     175             : 
     176         230 :             return true;
     177             :         }
     178             : 
     179      179816 :         nOldCode = nCode;
     180             : 
     181             :         // write character(/-sequence) of code nCode in the output buffer:
     182      179816 :         pE = pTable + nCode;
     183     4475814 :         do
     184             :         {
     185     4475814 :             nOutBufDataLen++;
     186     4475814 :             *(--pOutBufData) = pE->nData;
     187     4475814 :             pE = pE->pPrev;
     188             :         }
     189             :         while( pE );
     190             : 
     191      179816 :         bRet = true;
     192             :     }
     193             : 
     194      180734 :     return bRet;
     195             : }
     196             : 
     197             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10