tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / vcl / source / filter / igif / decode.cxx
blobb062593a9e4888b37468759e7e7bdee2ffca04f4
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 <cstdlib>
23 #include <cstring>
25 struct GIFLZWTableEntry
27 GIFLZWTableEntry* pPrev;
28 GIFLZWTableEntry* pFirst;
29 sal_uInt8 nData;
32 GIFLZWDecompressor::GIFLZWDecompressor(sal_uInt8 cDataSize)
33 : pTable(new GIFLZWTableEntry[4098])
34 , pOutBufData(pOutBuf.data() + 4096)
35 , pBlockBuf(nullptr)
36 , nInputBitsBuf(0)
37 , bEOIFound(false)
38 , nDataSize(cDataSize)
39 , nBlockBufSize(0)
40 , nBlockBufPos(0)
41 , nClearCode(1 << nDataSize)
42 , nEOICode(nClearCode + 1)
43 , nTableSize(nEOICode + 1)
44 , nCodeSize(nDataSize + 1)
45 , nOldCode(0xffff)
46 , nOutBufDataLen(0)
47 , nInputBitsBufSize(0)
49 for (sal_uInt16 i = 0; i < nTableSize; ++i)
51 pTable[i].pPrev = nullptr;
52 pTable[i].pFirst = &pTable[i];
53 pTable[i].nData = static_cast<sal_uInt8>(i);
56 memset(pTable.get() + nTableSize, 0, sizeof(GIFLZWTableEntry) * (4098 - nTableSize));
59 GIFLZWDecompressor::~GIFLZWDecompressor()
63 Scanline GIFLZWDecompressor::DecompressBlock( sal_uInt8* pSrc, sal_uInt8 cBufSize,
64 sal_uLong& rCount, bool& rEOI )
66 sal_uLong nTargetSize = 4096;
67 sal_uLong nCount = 0;
68 sal_uInt8* pTarget = static_cast<sal_uInt8*>(std::malloc( nTargetSize ));
69 sal_uInt8* pTmpTarget = pTarget;
71 nBlockBufSize = cBufSize;
72 nBlockBufPos = 0;
73 pBlockBuf = pSrc;
75 while (pTarget && ProcessOneCode())
77 nCount += nOutBufDataLen;
79 if( nCount > nTargetSize )
81 sal_uLong nNewSize = nTargetSize << 1;
82 sal_uLong nOffset = pTmpTarget - pTarget;
83 if (auto p = static_cast<sal_uInt8*>(std::realloc(pTarget, nNewSize)))
84 pTarget = p;
85 else
87 free(pTarget);
88 pTarget = nullptr;
89 break;
92 nTargetSize = nNewSize;
93 pTmpTarget = pTarget + nOffset;
96 memcpy( pTmpTarget, pOutBufData, nOutBufDataLen );
97 pTmpTarget += nOutBufDataLen;
98 pOutBufData += nOutBufDataLen;
99 nOutBufDataLen = 0;
101 if ( bEOIFound )
102 break;
105 rCount = nCount;
106 rEOI = bEOIFound;
108 return pTarget;
111 bool GIFLZWDecompressor::AddToTable( sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData )
113 if( nTableSize < 4096 )
115 GIFLZWTableEntry* pE = pTable.get() + nTableSize;
116 pE->pPrev = pTable.get() + nPrevCode;
117 pE->pFirst = pE->pPrev->pFirst;
118 GIFLZWTableEntry *pEntry = pTable[nCodeFirstData].pFirst;
119 if (!pEntry)
120 return false;
121 pE->nData = pEntry->nData;
122 nTableSize++;
124 if ( ( nTableSize == static_cast<sal_uInt16>(1 << nCodeSize) ) && ( nTableSize < 4096 ) )
125 nCodeSize++;
127 return true;
130 bool GIFLZWDecompressor::ProcessOneCode()
132 bool bRet = false;
133 bool bEndOfBlock = false;
135 while( nInputBitsBufSize < nCodeSize )
137 if( nBlockBufPos >= nBlockBufSize )
139 bEndOfBlock = true;
140 break;
143 nInputBitsBuf |= static_cast<sal_uLong>(pBlockBuf[ nBlockBufPos++ ]) << nInputBitsBufSize;
144 nInputBitsBufSize += 8;
147 if ( !bEndOfBlock )
149 // fetch code from input buffer
150 sal_uInt16 nCode = sal::static_int_cast< sal_uInt16 >(
151 static_cast<sal_uInt16>(nInputBitsBuf) & ( ~( 0xffff << nCodeSize ) ));
152 nInputBitsBuf >>= nCodeSize;
153 nInputBitsBufSize = nInputBitsBufSize - nCodeSize;
155 if ( nCode < nClearCode )
157 bool bOk = true;
158 if ( nOldCode != 0xffff )
159 bOk = AddToTable(nOldCode, nCode);
160 if (!bOk)
161 return false;
163 else if ( ( nCode > nEOICode ) && ( nCode <= nTableSize ) )
165 if ( nOldCode != 0xffff )
167 bool bOk;
168 if ( nCode == nTableSize )
169 bOk = AddToTable( nOldCode, nOldCode );
170 else
171 bOk = AddToTable( nOldCode, nCode );
172 if (!bOk)
173 return false;
176 else
178 if ( nCode == nClearCode )
180 nTableSize = nEOICode + 1;
181 nCodeSize = nDataSize + 1;
182 nOldCode = 0xffff;
183 nOutBufDataLen = 0;
185 else
186 bEOIFound = true;
188 return true;
191 nOldCode = nCode;
193 if (nCode >= 4096)
194 return false;
196 // write character(/-sequence) of code nCode in the output buffer:
197 GIFLZWTableEntry* pE = pTable.get() + nCode;
200 if (pOutBufData == pOutBuf.data()) //can't go back past start
201 return false;
202 nOutBufDataLen++;
203 *(--pOutBufData) = pE->nData;
204 pE = pE->pPrev;
206 while( pE );
208 bRet = true;
211 return bRet;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */