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 .
22 #include <rtl/alloc.h>
24 struct GIFLZWTableEntry
26 GIFLZWTableEntry
* pPrev
;
27 GIFLZWTableEntry
* pFirst
;
31 GIFLZWDecompressor::GIFLZWDecompressor(sal_uInt8 cDataSize
)
35 , nInputBitsBufSize(0)
37 , nDataSize(cDataSize
)
41 nClearCode
= 1 << nDataSize
;
42 nEOICode
= nClearCode
+ 1;
43 nTableSize
= nEOICode
+ 1;
44 nCodeSize
= nDataSize
+ 1;
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;
69 sal_uInt8
* pTarget
= static_cast<sal_uInt8
*>(rtl_allocateMemory( nTargetSize
));
70 sal_uInt8
* pTmpTarget
= pTarget
;
72 nBlockBufSize
= cBufSize
;
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
;
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
;
118 pE
->nData
= pEntry
->nData
;
121 if ( ( nTableSize
== (sal_uInt16
) (1 << nCodeSize
) ) && ( nTableSize
< 4096 ) )
127 bool GIFLZWDecompressor::ProcessOneCode()
131 bool bEndOfBlock
= false;
133 while( nInputBitsBufSize
< nCodeSize
)
135 if( nBlockBufPos
>= nBlockBufSize
)
141 nInputBitsBuf
|= ( (sal_uLong
) pBlockBuf
[ nBlockBufPos
++ ] ) << nInputBitsBufSize
;
142 nInputBitsBufSize
+= 8;
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
)
156 if ( nOldCode
!= 0xffff )
157 bOk
= AddToTable(nOldCode
, nCode
);
161 else if ( ( nCode
> nEOICode
) && ( nCode
<= nTableSize
) )
163 if ( nOldCode
!= 0xffff )
166 if ( nCode
== nTableSize
)
167 bOk
= AddToTable( nOldCode
, nOldCode
);
169 bOk
= AddToTable( nOldCode
, nCode
);
176 if ( nCode
== nClearCode
)
178 nTableSize
= nEOICode
+ 1;
179 nCodeSize
= nDataSize
+ 1;
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
201 *(--pOutBufData
) = pE
->nData
;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */