1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 // ------------------------------------------------------------------------
25 struct GIFLZWTableEntry
27 GIFLZWTableEntry
* pPrev
;
28 GIFLZWTableEntry
* pFirst
;
32 // ------------------------------------------------------------------------
34 GIFLZWDecompressor::GIFLZWDecompressor( sal_uInt8 cDataSize
) :
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;
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()
68 // ------------------------------------------------------------------------
70 HPBYTE
GIFLZWDecompressor::DecompressBlock( HPBYTE pSrc
, sal_uInt8 cBufSize
,
71 sal_uLong
& rCount
, sal_Bool
& rEOI
)
73 sal_uLong nTargetSize
= 4096;
75 HPBYTE pTarget
= (HPBYTE
) rtl_allocateMemory( nTargetSize
);
76 HPBYTE pTmpTarget
= pTarget
;
78 nBlockBufSize
= cBufSize
;
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
;
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
;
128 if ( ( nTableSize
== (sal_uInt16
) (1 << nCodeSize
) ) && ( nTableSize
< 4096 ) )
133 // ------------------------------------------------------------------------
135 sal_Bool
GIFLZWDecompressor::ProcessOneCode()
137 GIFLZWTableEntry
* pE
;
139 sal_Bool bRet
= sal_False
;
140 sal_Bool bEndOfBlock
= sal_False
;
142 while( nInputBitsBufSize
< nCodeSize
)
144 if( nBlockBufPos
>= nBlockBufSize
)
146 bEndOfBlock
= sal_True
;
150 nInputBitsBuf
|= ( (sal_uLong
) pBlockBuf
[ nBlockBufPos
++ ] ) << nInputBitsBufSize
;
151 nInputBitsBufSize
+= 8;
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
);
172 AddToTable( nOldCode
, nCode
);
176 if ( nCode
== nClearCode
)
178 nTableSize
= nEOICode
+ 1;
179 nCodeSize
= nDataSize
+ 1;
184 bEOIFound
= sal_True
;
191 // Zeichen(/-folge) des Codes nCode in den Ausgabe-Buffer schreiben:
196 *(--pOutBufData
) = pE
->nData
;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */