Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "lzwdecom.hxx"
31 : :
32 : 10 : LZWDecompressor::LZWDecompressor()
33 : 10 : : pOutBufData(NULL)
34 : : {
35 : : sal_uInt16 i;
36 : :
37 : 10 : pTable=new LZWTableEntry[4096];
38 : 10 : pOutBuf=new sal_uInt8[4096];
39 [ + + ]: 40970 : for (i=0; i<4096; i++)
40 : : {
41 : 40960 : pTable[i].nPrevCode=0;
42 : 40960 : pTable[i].nDataCount=1;
43 : 40960 : pTable[i].nData=(sal_uInt8)i;
44 : : }
45 : 10 : pIStream=NULL;
46 : 10 : bFirst = sal_True;
47 : 10 : nOldCode = 0;
48 : 10 : }
49 : :
50 : :
51 : 10 : LZWDecompressor::~LZWDecompressor()
52 : : {
53 [ + - ]: 10 : delete[] pOutBuf;
54 [ + - ]: 10 : delete[] pTable;
55 : 10 : }
56 : :
57 : :
58 : 435 : void LZWDecompressor::StartDecompression(SvStream & rIStream)
59 : : {
60 : 435 : pIStream=&rIStream;
61 : :
62 : 435 : nTableSize=258;
63 : :
64 : 435 : bEOIFound=sal_False;
65 : :
66 : 435 : nOutBufDataLen=0;
67 : :
68 : 435 : *pIStream >> nInputBitsBuf;
69 : :
70 : 435 : nInputBitsBufSize=8;
71 : :
72 [ + + ]: 435 : if ( bFirst )
73 : : {
74 : 10 : bInvert = nInputBitsBuf == 1;
75 : 10 : bFirst = sal_False;
76 : : }
77 : :
78 [ - + ]: 435 : if ( bInvert )
79 : 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 );
80 : 435 : }
81 : :
82 : :
83 : 2565 : sal_uLong LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uLong nMaxCount)
84 : : {
85 : : sal_uLong nCount;
86 : :
87 [ - + ]: 2565 : if (pIStream==NULL) return 0;
88 : :
89 : 2565 : nCount=0;
90 : 66885 : for (;;) {
91 : :
92 [ - + ]: 69450 : if (pIStream->GetError()) break;
93 : :
94 [ + + ]: 69450 : if (((sal_uLong)nOutBufDataLen)>=nMaxCount) {
95 : 2560 : nOutBufDataLen = nOutBufDataLen - (sal_uInt16)nMaxCount;
96 : 2560 : nCount+=nMaxCount;
97 [ + + ]: 110975 : while (nMaxCount>0) {
98 : 108415 : *(pTarget++)=*(pOutBufData++);
99 : 108415 : nMaxCount--;
100 : : }
101 : 2560 : break;
102 : : }
103 : :
104 : 66890 : nMaxCount-=(sal_uLong)nOutBufDataLen;
105 : 66890 : nCount+=nOutBufDataLen;
106 [ + + ]: 4873690 : while (nOutBufDataLen>0) {
107 : 4806800 : *(pTarget++)=*(pOutBufData++);
108 : 4806800 : nOutBufDataLen--;
109 : : }
110 : :
111 [ + + ]: 66890 : if (bEOIFound==sal_True) break;
112 : :
113 : 66885 : DecompressSome();
114 : :
115 : : }
116 : :
117 : 2565 : return nCount;
118 : : }
119 : :
120 : :
121 : 67315 : sal_uInt16 LZWDecompressor::GetNextCode()
122 : : {
123 : : sal_uInt16 nBits,nCode;
124 : :
125 [ + - ]: 67315 : if (nTableSize<511) nBits=9;
126 [ # # ]: 0 : else if (nTableSize<1023) nBits=10;
127 [ # # ]: 0 : else if (nTableSize<2047) nBits=11;
128 : 0 : else nBits=12;
129 : :
130 : 67315 : nCode=0;
131 [ + + ]: 134630 : do {
132 [ + + ]: 134630 : if (nInputBitsBufSize<=nBits)
133 : : {
134 : 75650 : nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf;
135 : 75650 : nBits = nBits - nInputBitsBufSize;
136 : 75650 : *pIStream >> nInputBitsBuf;
137 [ - + ]: 75650 : if ( bInvert )
138 : 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 );
139 : 75650 : nInputBitsBufSize=8;
140 : : }
141 : : else
142 : : {
143 : 58980 : nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits));
144 : 58980 : nInputBitsBufSize = nInputBitsBufSize - nBits;
145 : 58980 : nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize);
146 : 58980 : nBits=0;
147 : : }
148 : : } while (nBits>0);
149 : :
150 : 67315 : return nCode;
151 : : }
152 : :
153 : :
154 : 66450 : void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData)
155 : : {
156 [ + + ]: 4851135 : while (pTable[nCodeFirstData].nDataCount>1)
157 : 4784685 : nCodeFirstData=pTable[nCodeFirstData].nPrevCode;
158 : :
159 : 66450 : pTable[nTableSize].nPrevCode=nPrevCode;
160 : 66450 : pTable[nTableSize].nDataCount=pTable[nPrevCode].nDataCount+1;
161 : 66450 : pTable[nTableSize].nData=pTable[nCodeFirstData].nData;
162 : :
163 : 66450 : nTableSize++;
164 : 66450 : }
165 : :
166 : :
167 : 66885 : void LZWDecompressor::DecompressSome()
168 : : {
169 : : sal_uInt16 i,nCode;
170 : :
171 : 66885 : nCode=GetNextCode();
172 [ + + ]: 66885 : if (nCode==256) {
173 : 430 : nTableSize=258;
174 : 430 : nCode=GetNextCode();
175 [ - + ]: 430 : if (nCode==257) { bEOIFound=sal_True; return; }
176 : : }
177 [ + + ]: 66455 : else if (nCode<nTableSize) AddToTable(nOldCode,nCode);
178 [ + + ]: 63655 : else if (nCode==nTableSize) AddToTable(nOldCode,nOldCode);
179 : 5 : else { bEOIFound=sal_True; return; }
180 : :
181 : 66880 : nOldCode=nCode;
182 : :
183 : 66880 : nOutBufDataLen=pTable[nCode].nDataCount;
184 : 66880 : pOutBufData=pOutBuf+nOutBufDataLen;
185 [ + + ]: 4982100 : for (i=0; i<nOutBufDataLen; i++) {
186 : 4915215 : *(--pOutBufData)=pTable[nCode].nData;
187 : 4915215 : nCode=pTable[nCode].nPrevCode;
188 : : }
189 : : }
190 : :
191 : :
192 : :
193 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|