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