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 4 : LZWDecompressor::LZWDecompressor()
24 4 : : pOutBufData(NULL)
25 : {
26 : sal_uInt16 i;
27 :
28 4 : pTable=new LZWTableEntry[4096];
29 4 : pOutBuf=new sal_uInt8[4096];
30 16388 : for (i=0; i<4096; i++)
31 : {
32 16384 : pTable[i].nPrevCode=0;
33 16384 : pTable[i].nDataCount=1;
34 16384 : pTable[i].nData=(sal_uInt8)i;
35 : }
36 4 : pIStream=NULL;
37 4 : bFirst = sal_True;
38 4 : nOldCode = 0;
39 4 : }
40 :
41 :
42 4 : LZWDecompressor::~LZWDecompressor()
43 : {
44 4 : delete[] pOutBuf;
45 4 : delete[] pTable;
46 4 : }
47 :
48 :
49 174 : void LZWDecompressor::StartDecompression(SvStream & rIStream)
50 : {
51 174 : pIStream=&rIStream;
52 :
53 174 : nTableSize=258;
54 :
55 174 : bEOIFound=sal_False;
56 :
57 174 : nOutBufDataLen=0;
58 :
59 174 : *pIStream >> nInputBitsBuf;
60 :
61 174 : nInputBitsBufSize=8;
62 :
63 174 : if ( bFirst )
64 : {
65 4 : bInvert = nInputBitsBuf == 1;
66 4 : bFirst = sal_False;
67 : }
68 :
69 174 : 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 174 : }
72 :
73 :
74 1026 : sal_uLong LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uLong nMaxCount)
75 : {
76 : sal_uLong nCount;
77 :
78 1026 : if (pIStream==NULL) return 0;
79 :
80 1026 : nCount=0;
81 26754 : for (;;) {
82 :
83 27780 : if (pIStream->GetError()) break;
84 :
85 27780 : if (((sal_uLong)nOutBufDataLen)>=nMaxCount) {
86 1024 : nOutBufDataLen = nOutBufDataLen - (sal_uInt16)nMaxCount;
87 1024 : nCount+=nMaxCount;
88 45414 : while (nMaxCount>0) {
89 43366 : *(pTarget++)=*(pOutBufData++);
90 43366 : nMaxCount--;
91 : }
92 1024 : break;
93 : }
94 :
95 26756 : nMaxCount-=(sal_uLong)nOutBufDataLen;
96 26756 : nCount+=nOutBufDataLen;
97 1976232 : while (nOutBufDataLen>0) {
98 1922720 : *(pTarget++)=*(pOutBufData++);
99 1922720 : nOutBufDataLen--;
100 : }
101 :
102 26756 : if (bEOIFound==sal_True) break;
103 :
104 26754 : DecompressSome();
105 :
106 : }
107 :
108 1026 : return nCount;
109 : }
110 :
111 :
112 26926 : sal_uInt16 LZWDecompressor::GetNextCode()
113 : {
114 : sal_uInt16 nBits,nCode;
115 :
116 26926 : 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 26926 : nCode=0;
122 53852 : do {
123 53852 : if (nInputBitsBufSize<=nBits)
124 : {
125 30260 : nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf;
126 30260 : nBits = nBits - nInputBitsBufSize;
127 30260 : *pIStream >> nInputBitsBuf;
128 30260 : 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 30260 : nInputBitsBufSize=8;
131 : }
132 : else
133 : {
134 23592 : nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits));
135 23592 : nInputBitsBufSize = nInputBitsBufSize - nBits;
136 23592 : nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize);
137 23592 : nBits=0;
138 : }
139 : } while (nBits>0);
140 :
141 26926 : return nCode;
142 : }
143 :
144 :
145 26580 : void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData)
146 : {
147 1967034 : while (pTable[nCodeFirstData].nDataCount>1)
148 1913874 : nCodeFirstData=pTable[nCodeFirstData].nPrevCode;
149 :
150 26580 : pTable[nTableSize].nPrevCode=nPrevCode;
151 26580 : pTable[nTableSize].nDataCount=pTable[nPrevCode].nDataCount+1;
152 26580 : pTable[nTableSize].nData=pTable[nCodeFirstData].nData;
153 :
154 26580 : nTableSize++;
155 26580 : }
156 :
157 :
158 26754 : void LZWDecompressor::DecompressSome()
159 : {
160 : sal_uInt16 i,nCode;
161 :
162 26754 : nCode=GetNextCode();
163 26754 : if (nCode==256) {
164 172 : nTableSize=258;
165 172 : nCode=GetNextCode();
166 172 : if (nCode==257) { bEOIFound=sal_True; return; }
167 : }
168 26582 : else if (nCode<nTableSize) AddToTable(nOldCode,nCode);
169 25462 : else if (nCode==nTableSize) AddToTable(nOldCode,nOldCode);
170 2 : else { bEOIFound=sal_True; return; }
171 :
172 26752 : nOldCode=nCode;
173 :
174 26752 : nOutBufDataLen=pTable[nCode].nDataCount;
175 26752 : pOutBufData=pOutBuf+nOutBufDataLen;
176 1992838 : for (i=0; i<nOutBufDataLen; i++) {
177 1966086 : *(--pOutBufData)=pTable[nCode].nData;
178 1966086 : nCode=pTable[nCode].nPrevCode;
179 : }
180 : }
181 :
182 :
183 :
184 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|