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 :
21 : #include "lzwdecom.hxx"
22 :
23 2 : LZWDecompressor::LZWDecompressor()
24 2 : : pOutBufData(NULL)
25 : {
26 : sal_uInt16 i;
27 :
28 2 : pTable=new LZWTableEntry[4096];
29 2 : pOutBuf=new sal_uInt8[4096];
30 8194 : for (i=0; i<4096; i++)
31 : {
32 8192 : pTable[i].nPrevCode=0;
33 8192 : pTable[i].nDataCount=1;
34 8192 : pTable[i].nData=(sal_uInt8)i;
35 : }
36 2 : pIStream=NULL;
37 2 : bFirst = sal_True;
38 2 : nOldCode = 0;
39 2 : }
40 :
41 :
42 2 : LZWDecompressor::~LZWDecompressor()
43 : {
44 2 : delete[] pOutBuf;
45 2 : delete[] pTable;
46 2 : }
47 :
48 :
49 87 : void LZWDecompressor::StartDecompression(SvStream & rIStream)
50 : {
51 87 : pIStream=&rIStream;
52 :
53 87 : nTableSize=258;
54 :
55 87 : bEOIFound=sal_False;
56 :
57 87 : nOutBufDataLen=0;
58 :
59 87 : *pIStream >> nInputBitsBuf;
60 :
61 87 : nInputBitsBufSize=8;
62 :
63 87 : if ( bFirst )
64 : {
65 2 : bInvert = nInputBitsBuf == 1;
66 2 : bFirst = sal_False;
67 : }
68 :
69 87 : if ( bInvert )
70 0 : nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
71 87 : }
72 :
73 :
74 513 : sal_uLong LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uLong nMaxCount)
75 : {
76 : sal_uLong nCount;
77 :
78 513 : if (pIStream==NULL) return 0;
79 :
80 513 : nCount=0;
81 13377 : for (;;) {
82 :
83 13890 : if (pIStream->GetError()) break;
84 :
85 13890 : if (((sal_uLong)nOutBufDataLen)>=nMaxCount) {
86 512 : nOutBufDataLen = nOutBufDataLen - (sal_uInt16)nMaxCount;
87 512 : nCount+=nMaxCount;
88 22707 : while (nMaxCount>0) {
89 21683 : *(pTarget++)=*(pOutBufData++);
90 21683 : nMaxCount--;
91 : }
92 512 : break;
93 : }
94 :
95 13378 : nMaxCount-=(sal_uLong)nOutBufDataLen;
96 13378 : nCount+=nOutBufDataLen;
97 988116 : while (nOutBufDataLen>0) {
98 961360 : *(pTarget++)=*(pOutBufData++);
99 961360 : nOutBufDataLen--;
100 : }
101 :
102 13378 : if (bEOIFound==sal_True) break;
103 :
104 13377 : DecompressSome();
105 :
106 : }
107 :
108 513 : return nCount;
109 : }
110 :
111 :
112 13463 : sal_uInt16 LZWDecompressor::GetNextCode()
113 : {
114 : sal_uInt16 nBits,nCode;
115 :
116 13463 : if (nTableSize<511) nBits=9;
117 0 : else if (nTableSize<1023) nBits=10;
118 0 : else if (nTableSize<2047) nBits=11;
119 0 : else nBits=12;
120 :
121 13463 : nCode=0;
122 26926 : do {
123 26926 : if (nInputBitsBufSize<=nBits)
124 : {
125 15130 : nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf;
126 15130 : nBits = nBits - nInputBitsBufSize;
127 15130 : *pIStream >> nInputBitsBuf;
128 15130 : if ( bInvert )
129 0 : nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
130 15130 : nInputBitsBufSize=8;
131 : }
132 : else
133 : {
134 11796 : nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits));
135 11796 : nInputBitsBufSize = nInputBitsBufSize - nBits;
136 11796 : nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize);
137 11796 : nBits=0;
138 : }
139 : } while (nBits>0);
140 :
141 13463 : return nCode;
142 : }
143 :
144 :
145 13290 : void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData)
146 : {
147 983517 : while (pTable[nCodeFirstData].nDataCount>1)
148 956937 : nCodeFirstData=pTable[nCodeFirstData].nPrevCode;
149 :
150 13290 : pTable[nTableSize].nPrevCode=nPrevCode;
151 13290 : pTable[nTableSize].nDataCount=pTable[nPrevCode].nDataCount+1;
152 13290 : pTable[nTableSize].nData=pTable[nCodeFirstData].nData;
153 :
154 13290 : nTableSize++;
155 13290 : }
156 :
157 :
158 13377 : void LZWDecompressor::DecompressSome()
159 : {
160 : sal_uInt16 i,nCode;
161 :
162 13377 : nCode=GetNextCode();
163 13377 : if (nCode==256) {
164 86 : nTableSize=258;
165 86 : nCode=GetNextCode();
166 86 : if (nCode==257) { bEOIFound=sal_True; return; }
167 : }
168 13291 : else if (nCode<nTableSize) AddToTable(nOldCode,nCode);
169 12731 : else if (nCode==nTableSize) AddToTable(nOldCode,nOldCode);
170 1 : else { bEOIFound=sal_True; return; }
171 :
172 13376 : nOldCode=nCode;
173 :
174 13376 : nOutBufDataLen=pTable[nCode].nDataCount;
175 13376 : pOutBufData=pOutBuf+nOutBufDataLen;
176 996419 : for (i=0; i<nOutBufDataLen; i++) {
177 983043 : *(--pOutBufData)=pTable[nCode].nData;
178 983043 : nCode=pTable[nCode].nPrevCode;
179 : }
180 : }
181 :
182 :
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|