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 230 : 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 230 : , nBlockBufPos(0)
38 : {
39 230 : pOutBuf = new sal_uInt8[ 4096 ];
40 :
41 230 : nClearCode = 1 << nDataSize;
42 230 : nEOICode = nClearCode + 1;
43 230 : nTableSize = nEOICode + 1;
44 230 : nCodeSize = nDataSize + 1;
45 230 : nOldCode = 0xffff;
46 230 : pOutBufData = pOutBuf + 4096;
47 :
48 230 : pTable = new GIFLZWTableEntry[ 4098 ];
49 :
50 29250 : for (sal_uInt16 i = 0; i < nTableSize; ++i)
51 : {
52 29020 : pTable[i].pPrev = NULL;
53 29020 : pTable[i].pFirst = pTable + i;
54 29020 : pTable[i].nData = (sal_uInt8) i;
55 : }
56 :
57 230 : memset(pTable + nTableSize, 0, sizeof(GIFLZWTableEntry) * (4098 - nTableSize));
58 230 : }
59 :
60 230 : GIFLZWDecompressor::~GIFLZWDecompressor()
61 : {
62 230 : delete[] pOutBuf;
63 230 : delete[] pTable;
64 230 : }
65 :
66 14024 : HPBYTE GIFLZWDecompressor::DecompressBlock( HPBYTE pSrc, sal_uInt8 cBufSize,
67 : sal_uLong& rCount, bool& rEOI )
68 : {
69 14024 : sal_uLong nTargetSize = 4096;
70 14024 : sal_uLong nCount = 0;
71 14024 : HPBYTE pTarget = (HPBYTE) rtl_allocateMemory( nTargetSize );
72 14024 : HPBYTE pTmpTarget = pTarget;
73 :
74 14024 : nBlockBufSize = cBufSize;
75 14024 : nBlockBufPos = 0;
76 14024 : pBlockBuf = pSrc;
77 :
78 2560480 : while( ProcessOneCode() )
79 : {
80 2532662 : nCount += nOutBufDataLen;
81 :
82 2532662 : if( nCount > nTargetSize )
83 : {
84 2666 : sal_uLong nNewSize = nTargetSize << 1;
85 2666 : sal_uLong nOffset = pTmpTarget - pTarget;
86 2666 : HPBYTE pTmp = (HPBYTE) rtl_allocateMemory( nNewSize );
87 :
88 2666 : memcpy( pTmp, pTarget, nTargetSize );
89 2666 : rtl_freeMemory( pTarget );
90 :
91 2666 : nTargetSize = nNewSize;
92 2666 : pTmpTarget = ( pTarget = pTmp ) + nOffset;
93 : }
94 :
95 2532662 : memcpy( pTmpTarget, pOutBufData, nOutBufDataLen );
96 2532662 : pTmpTarget += nOutBufDataLen;
97 2532662 : pOutBufData += nOutBufDataLen;
98 2532662 : nOutBufDataLen = 0;
99 :
100 2532662 : if ( bEOIFound )
101 230 : break;
102 : }
103 :
104 14024 : rCount = nCount;
105 14024 : rEOI = bEOIFound;
106 :
107 14024 : return pTarget;
108 : }
109 :
110 2530700 : bool GIFLZWDecompressor::AddToTable( sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData )
111 : {
112 : GIFLZWTableEntry* pE;
113 :
114 2530700 : if( nTableSize < 4096 )
115 : {
116 2530700 : pE = pTable + nTableSize;
117 2530700 : pE->pPrev = pTable + nPrevCode;
118 2530700 : pE->pFirst = pE->pPrev->pFirst;
119 2530700 : GIFLZWTableEntry *pEntry = pTable[nCodeFirstData].pFirst;
120 2530700 : if (!pEntry)
121 0 : return false;
122 2530700 : pE->nData = pEntry->nData;
123 2530700 : nTableSize++;
124 :
125 2530700 : if ( ( nTableSize == (sal_uInt16) (1 << nCodeSize) ) && ( nTableSize < 4096 ) )
126 2460 : nCodeSize++;
127 : }
128 2530700 : return true;
129 : }
130 :
131 2546456 : bool GIFLZWDecompressor::ProcessOneCode()
132 : {
133 : GIFLZWTableEntry* pE;
134 : sal_uInt16 nCode;
135 2546456 : bool bRet = false;
136 2546456 : bool bEndOfBlock = false;
137 :
138 8631958 : while( nInputBitsBufSize < nCodeSize )
139 : {
140 3552840 : if( nBlockBufPos >= nBlockBufSize )
141 : {
142 13794 : bEndOfBlock = true;
143 13794 : break;
144 : }
145 :
146 3539046 : nInputBitsBuf |= ( (sal_uLong) pBlockBuf[ nBlockBufPos++ ] ) << nInputBitsBufSize;
147 3539046 : nInputBitsBufSize += 8;
148 : }
149 :
150 2546456 : if ( !bEndOfBlock )
151 : {
152 : // fetch code from input buffer
153 : nCode = sal::static_int_cast< sal_uInt16 >(
154 2532662 : ( (sal_uInt16) nInputBitsBuf ) & ( ~( 0xffff << nCodeSize ) ));
155 2532662 : nInputBitsBuf >>= nCodeSize;
156 2532662 : nInputBitsBufSize = nInputBitsBufSize - nCodeSize;
157 :
158 2532662 : if ( nCode < nClearCode )
159 : {
160 534876 : bool bOk = true;
161 534876 : if ( nOldCode != 0xffff )
162 534012 : bOk = AddToTable(nOldCode, nCode);
163 534876 : if (!bOk)
164 0 : return false;
165 : }
166 1997786 : else if ( ( nCode > nEOICode ) && ( nCode <= nTableSize ) )
167 : {
168 1996690 : if ( nOldCode != 0xffff )
169 : {
170 : bool bOk;
171 1996688 : if ( nCode == nTableSize )
172 124200 : bOk = AddToTable( nOldCode, nOldCode );
173 : else
174 1872488 : bOk = AddToTable( nOldCode, nCode );
175 1996688 : if (!bOk)
176 0 : return false;
177 1996690 : }
178 : }
179 : else
180 : {
181 1096 : if ( nCode == nClearCode )
182 : {
183 866 : nTableSize = nEOICode + 1;
184 866 : nCodeSize = nDataSize + 1;
185 866 : nOldCode = 0xffff;
186 866 : nOutBufDataLen = 0;
187 : }
188 : else
189 230 : bEOIFound = true;
190 :
191 1096 : return true;
192 : }
193 :
194 2531566 : nOldCode = nCode;
195 :
196 : // write character(/-sequence) of code nCode in the output buffer:
197 2531566 : pE = pTable + nCode;
198 35436686 : do
199 : {
200 35436686 : nOutBufDataLen++;
201 35436686 : *(--pOutBufData) = pE->nData;
202 35436686 : pE = pE->pPrev;
203 : }
204 : while( pE );
205 :
206 2531566 : bRet = true;
207 : }
208 :
209 2545360 : return bRet;
210 1233 : }
211 :
212 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|