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 .
25 struct GIFLZWTableEntry
27 GIFLZWTableEntry
* pPrev
;
28 GIFLZWTableEntry
* pFirst
;
32 GIFLZWDecompressor::GIFLZWDecompressor(sal_uInt8 cDataSize
)
33 : pTable(new GIFLZWTableEntry
[4098])
34 , pOutBufData(pOutBuf
.data() + 4096)
38 , nDataSize(cDataSize
)
41 , nClearCode(1 << nDataSize
)
42 , nEOICode(nClearCode
+ 1)
43 , nTableSize(nEOICode
+ 1)
44 , nCodeSize(nDataSize
+ 1)
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;
68 sal_uInt8
* pTarget
= static_cast<sal_uInt8
*>(std::malloc( nTargetSize
));
69 sal_uInt8
* pTmpTarget
= pTarget
;
71 nBlockBufSize
= cBufSize
;
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
)))
92 nTargetSize
= nNewSize
;
93 pTmpTarget
= pTarget
+ nOffset
;
96 memcpy( pTmpTarget
, pOutBufData
, nOutBufDataLen
);
97 pTmpTarget
+= nOutBufDataLen
;
98 pOutBufData
+= nOutBufDataLen
;
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
;
121 pE
->nData
= pEntry
->nData
;
124 if ( ( nTableSize
== static_cast<sal_uInt16
>(1 << nCodeSize
) ) && ( nTableSize
< 4096 ) )
130 bool GIFLZWDecompressor::ProcessOneCode()
133 bool bEndOfBlock
= false;
135 while( nInputBitsBufSize
< nCodeSize
)
137 if( nBlockBufPos
>= nBlockBufSize
)
143 nInputBitsBuf
|= static_cast<sal_uLong
>(pBlockBuf
[ nBlockBufPos
++ ]) << nInputBitsBufSize
;
144 nInputBitsBufSize
+= 8;
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
)
158 if ( nOldCode
!= 0xffff )
159 bOk
= AddToTable(nOldCode
, nCode
);
163 else if ( ( nCode
> nEOICode
) && ( nCode
<= nTableSize
) )
165 if ( nOldCode
!= 0xffff )
168 if ( nCode
== nTableSize
)
169 bOk
= AddToTable( nOldCode
, nOldCode
);
171 bOk
= AddToTable( nOldCode
, nCode
);
178 if ( nCode
== nClearCode
)
180 nTableSize
= nEOICode
+ 1;
181 nCodeSize
= nDataSize
+ 1;
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
203 *(--pOutBufData
) = pE
->nData
;
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */