build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / filter / igif / decode.cxx
blobbbbbee27d10d6f1205c90709812063866d725148
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 .
20 #include "decode.hxx"
22 #include <rtl/alloc.h>
24 struct GIFLZWTableEntry
26 GIFLZWTableEntry* pPrev;
27 GIFLZWTableEntry* pFirst;
28 sal_uInt8 nData;
31 GIFLZWDecompressor::GIFLZWDecompressor(sal_uInt8 cDataSize)
32 : pBlockBuf(nullptr)
33 , nInputBitsBuf(0)
34 , nOutBufDataLen(0)
35 , nInputBitsBufSize(0)
36 , bEOIFound(false)
37 , nDataSize(cDataSize)
38 , nBlockBufSize(0)
39 , nBlockBufPos(0)
41 nClearCode = 1 << nDataSize;
42 nEOICode = nClearCode + 1;
43 nTableSize = nEOICode + 1;
44 nCodeSize = nDataSize + 1;
45 nOldCode = 0xffff;
46 pOutBufData = pOutBuf.data() + 4096;
48 pTable.reset( new GIFLZWTableEntry[ 4098 ] );
50 for (sal_uInt16 i = 0; i < nTableSize; ++i)
52 pTable[i].pPrev = nullptr;
53 pTable[i].pFirst = pTable.get() + i;
54 pTable[i].nData = (sal_uInt8) i;
57 memset(pTable.get() + nTableSize, 0, sizeof(GIFLZWTableEntry) * (4098 - nTableSize));
60 GIFLZWDecompressor::~GIFLZWDecompressor()
64 Scanline GIFLZWDecompressor::DecompressBlock( sal_uInt8* pSrc, sal_uInt8 cBufSize,
65 sal_uLong& rCount, bool& rEOI )
67 sal_uLong nTargetSize = 4096;
68 sal_uLong nCount = 0;
69 sal_uInt8* pTarget = static_cast<sal_uInt8*>(rtl_allocateMemory( nTargetSize ));
70 sal_uInt8* pTmpTarget = pTarget;
72 nBlockBufSize = cBufSize;
73 nBlockBufPos = 0;
74 pBlockBuf = pSrc;
76 while( ProcessOneCode() )
78 nCount += nOutBufDataLen;
80 if( nCount > nTargetSize )
82 sal_uLong nNewSize = nTargetSize << 1;
83 sal_uLong nOffset = pTmpTarget - pTarget;
84 sal_uInt8* pTmp = static_cast<sal_uInt8*>(rtl_allocateMemory( nNewSize ));
86 memcpy( pTmp, pTarget, nTargetSize );
87 rtl_freeMemory( pTarget );
89 nTargetSize = nNewSize;
90 pTmpTarget = ( pTarget = pTmp ) + nOffset;
93 memcpy( pTmpTarget, pOutBufData, nOutBufDataLen );
94 pTmpTarget += nOutBufDataLen;
95 pOutBufData += nOutBufDataLen;
96 nOutBufDataLen = 0;
98 if ( bEOIFound )
99 break;
102 rCount = nCount;
103 rEOI = bEOIFound;
105 return pTarget;
108 bool GIFLZWDecompressor::AddToTable( sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData )
110 if( nTableSize < 4096 )
112 GIFLZWTableEntry* pE = pTable.get() + nTableSize;
113 pE->pPrev = pTable.get() + nPrevCode;
114 pE->pFirst = pE->pPrev->pFirst;
115 GIFLZWTableEntry *pEntry = pTable[nCodeFirstData].pFirst;
116 if (!pEntry)
117 return false;
118 pE->nData = pEntry->nData;
119 nTableSize++;
121 if ( ( nTableSize == (sal_uInt16) (1 << nCodeSize) ) && ( nTableSize < 4096 ) )
122 nCodeSize++;
124 return true;
127 bool GIFLZWDecompressor::ProcessOneCode()
129 sal_uInt16 nCode;
130 bool bRet = false;
131 bool bEndOfBlock = false;
133 while( nInputBitsBufSize < nCodeSize )
135 if( nBlockBufPos >= nBlockBufSize )
137 bEndOfBlock = true;
138 break;
141 nInputBitsBuf |= ( (sal_uLong) pBlockBuf[ nBlockBufPos++ ] ) << nInputBitsBufSize;
142 nInputBitsBufSize += 8;
145 if ( !bEndOfBlock )
147 // fetch code from input buffer
148 nCode = sal::static_int_cast< sal_uInt16 >(
149 ( (sal_uInt16) nInputBitsBuf ) & ( ~( 0xffff << nCodeSize ) ));
150 nInputBitsBuf >>= nCodeSize;
151 nInputBitsBufSize = nInputBitsBufSize - nCodeSize;
153 if ( nCode < nClearCode )
155 bool bOk = true;
156 if ( nOldCode != 0xffff )
157 bOk = AddToTable(nOldCode, nCode);
158 if (!bOk)
159 return false;
161 else if ( ( nCode > nEOICode ) && ( nCode <= nTableSize ) )
163 if ( nOldCode != 0xffff )
165 bool bOk;
166 if ( nCode == nTableSize )
167 bOk = AddToTable( nOldCode, nOldCode );
168 else
169 bOk = AddToTable( nOldCode, nCode );
170 if (!bOk)
171 return false;
174 else
176 if ( nCode == nClearCode )
178 nTableSize = nEOICode + 1;
179 nCodeSize = nDataSize + 1;
180 nOldCode = 0xffff;
181 nOutBufDataLen = 0;
183 else
184 bEOIFound = true;
186 return true;
189 nOldCode = nCode;
191 if (nCode >= 4096)
192 return false;
194 // write character(/-sequence) of code nCode in the output buffer:
195 GIFLZWTableEntry* pE = pTable.get() + nCode;
198 if (pOutBufData == pOutBuf.data()) //can't go back past start
199 return false;
200 nOutBufDataLen++;
201 *(--pOutBufData) = pE->nData;
202 pE = pE->pPrev;
204 while( pE );
206 bRet = true;
209 return bRet;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */