bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / filter / igif / decode.cxx
blob09981f6e3d284f499f487772d5474753a99ebf61
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "decode.hxx"
23 // ------------------------------------------------------------------------
25 struct GIFLZWTableEntry
27 GIFLZWTableEntry* pPrev;
28 GIFLZWTableEntry* pFirst;
29 sal_uInt8 nData;
32 // ------------------------------------------------------------------------
34 GIFLZWDecompressor::GIFLZWDecompressor( sal_uInt8 cDataSize ) :
35 nInputBitsBuf ( 0 ),
36 nOutBufDataLen ( 0 ),
37 nInputBitsBufSize ( 0 ),
38 bEOIFound ( sal_False ),
39 nDataSize ( cDataSize )
41 pOutBuf = new sal_uInt8[ 4096 ];
43 nClearCode = 1 << nDataSize;
44 nEOICode = nClearCode + 1;
45 nTableSize = nEOICode + 1;
46 nCodeSize = nDataSize + 1;
47 nOldCode = 0xffff;
48 pOutBufData = pOutBuf + 4096;
50 pTable = new GIFLZWTableEntry[ 4098 ];
52 for( sal_uInt16 i = 0; i < nTableSize; i++ )
54 pTable[i].pPrev = NULL;
55 pTable[i].pFirst = pTable + i;
56 pTable[i].nData = (sal_uInt8) i;
60 // ------------------------------------------------------------------------
62 GIFLZWDecompressor::~GIFLZWDecompressor()
64 delete[] pOutBuf;
65 delete[] pTable;
68 // ------------------------------------------------------------------------
70 HPBYTE GIFLZWDecompressor::DecompressBlock( HPBYTE pSrc, sal_uInt8 cBufSize,
71 sal_uLong& rCount, sal_Bool& rEOI )
73 sal_uLong nTargetSize = 4096;
74 sal_uLong nCount = 0;
75 HPBYTE pTarget = (HPBYTE) rtl_allocateMemory( nTargetSize );
76 HPBYTE pTmpTarget = pTarget;
78 nBlockBufSize = cBufSize;
79 nBlockBufPos = 0;
80 pBlockBuf = pSrc;
82 while( ProcessOneCode() )
84 nCount += nOutBufDataLen;
86 if( nCount > nTargetSize )
88 sal_uLong nNewSize = nTargetSize << 1;
89 sal_uLong nOffset = pTmpTarget - pTarget;
90 HPBYTE pTmp = (HPBYTE) rtl_allocateMemory( nNewSize );
92 memcpy( pTmp, pTarget, nTargetSize );
93 rtl_freeMemory( pTarget );
95 nTargetSize = nNewSize;
96 pTmpTarget = ( pTarget = pTmp ) + nOffset;
99 memcpy( pTmpTarget, pOutBufData, nOutBufDataLen );
100 pTmpTarget += nOutBufDataLen;
101 pOutBufData += nOutBufDataLen;
102 nOutBufDataLen = 0;
104 if ( bEOIFound )
105 break;
108 rCount = nCount;
109 rEOI = bEOIFound;
111 return pTarget;
114 // ------------------------------------------------------------------------
116 void GIFLZWDecompressor::AddToTable( sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData )
118 GIFLZWTableEntry* pE;
120 if( nTableSize < 4096 )
122 pE = pTable + nTableSize;
123 pE->pPrev = pTable + nPrevCode;
124 pE->pFirst = pE->pPrev->pFirst;
125 pE->nData = pTable[ nCodeFirstData ].pFirst->nData;
126 nTableSize++;
128 if ( ( nTableSize == (sal_uInt16) (1 << nCodeSize) ) && ( nTableSize < 4096 ) )
129 nCodeSize++;
133 // ------------------------------------------------------------------------
135 sal_Bool GIFLZWDecompressor::ProcessOneCode()
137 GIFLZWTableEntry* pE;
138 sal_uInt16 nCode;
139 sal_Bool bRet = sal_False;
140 sal_Bool bEndOfBlock = sal_False;
142 while( nInputBitsBufSize < nCodeSize )
144 if( nBlockBufPos >= nBlockBufSize )
146 bEndOfBlock = sal_True;
147 break;
150 nInputBitsBuf |= ( (sal_uLong) pBlockBuf[ nBlockBufPos++ ] ) << nInputBitsBufSize;
151 nInputBitsBufSize += 8;
154 if ( !bEndOfBlock )
156 // Einen Code aus dem Eingabe-Buffer holen:
157 nCode = sal::static_int_cast< sal_uInt16 >(
158 ( (sal_uInt16) nInputBitsBuf ) & ( ~( 0xffff << nCodeSize ) ));
159 nInputBitsBuf >>= nCodeSize;
160 nInputBitsBufSize = nInputBitsBufSize - nCodeSize;
162 if ( nCode < nClearCode )
164 if ( nOldCode != 0xffff )
165 AddToTable( nOldCode, nCode );
167 else if ( ( nCode > nEOICode ) && ( nCode <= nTableSize ) )
169 if ( nCode == nTableSize )
170 AddToTable( nOldCode, nOldCode );
171 else
172 AddToTable( nOldCode, nCode );
174 else
176 if ( nCode == nClearCode )
178 nTableSize = nEOICode + 1;
179 nCodeSize = nDataSize + 1;
180 nOldCode = 0xffff;
181 nOutBufDataLen = 0;
183 else
184 bEOIFound = sal_True;
186 return sal_True;
189 nOldCode = nCode;
191 // Zeichen(/-folge) des Codes nCode in den Ausgabe-Buffer schreiben:
192 pE = pTable + nCode;
195 nOutBufDataLen++;
196 *(--pOutBufData) = pE->nData;
197 pE = pE->pPrev;
199 while( pE );
201 bRet = sal_True;
204 return bRet;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */