xref: /aoo42x/main/sc/source/filter/lotus/filter.cxx (revision b77af630)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_scfilt.hxx"
26 //  Das geht:   Versionserkennung WKS, WK1 und WK3
27 //              ...Rest steht in op.cpp
28 
29 //------------------------------------------------------------------------
30 
31 #include <tools/solar.h>
32 #include <string.h>
33 #include <map>
34 
35 #include "filter.hxx"
36 #include "document.hxx"
37 #include "compiler.hxx"
38 #include "scerrors.hxx"
39 
40 #include "root.hxx"
41 #include "lotrange.hxx"
42 #include "optab.h"
43 #include "scmem.h"
44 #include "decl.h"
45 #include "tool.h"
46 
47 #include "fprogressbar.hxx"
48 
49 #include "op.h"
50 
51 // Konstanten ------------------------------------------------------------
52 const sal_uInt16		nBOF = 0x0000;
53 
54 
55 
56 // externe Variablen -----------------------------------------------------
57 extern WKTYP		eTyp;	// Typ der gerade in bearbeitung befindlichen Datei
58 WKTYP				eTyp;
59 
60 extern sal_Bool			bEOF;			// zeigt Ende der Datei
61 sal_Bool				bEOF;
62 
63 extern CharSet		eCharVon;
64 CharSet				eCharVon;
65 
66 extern ScDocument*	pDoc;			// Aufhaenger zum Dokumentzugriff
67 ScDocument*			pDoc;
68 
69 
70 extern OPCODE_FKT	pOpFkt[ FKT_LIMIT ];
71 									// -> optab.cxx, Tabelle moeglicher Opcodes
72 extern OPCODE_FKT	pOpFkt123[ FKT_LIMIT123 ];
73 									// -> optab.cxx, Table of possible Opcodes
74 
75 LOTUS_ROOT*			pLotusRoot = NULL;
76 
77 
78 std::map<sal_uInt16, ScPatternAttr> aLotusPatternPool;
79 
80 static FltError
generate_Opcodes(SvStream & aStream,ScDocument & rDoc,ScfStreamProgressBar & aPrgrsBar,WKTYP eType)81 generate_Opcodes( SvStream& aStream, ScDocument& rDoc,
82                   ScfStreamProgressBar& aPrgrsBar, WKTYP eType )
83 {
84 	OPCODE_FKT *pOps;
85 	int         nOps;
86 
87     switch(eType)
88 	{
89 	    case eWK_1:
90 	    case eWK_2:
91 		pOps = pOpFkt;
92 		nOps = FKT_LIMIT;
93 		break;
94 	    case eWK123:
95 		pOps = pOpFkt123;
96 		nOps = FKT_LIMIT123;
97 		break;
98 	    case eWK3:		return eERR_NI;
99 	    case eWK_Error:	return eERR_FORMAT;
100 	    default:		return eERR_UNKN_WK;
101  	}
102 
103     // #i76299# seems that SvStream::IsEof() does not work correctly
104     aStream.Seek( STREAM_SEEK_TO_END );
105     sal_Size nStrmSize = aStream.Tell();
106     aStream.Seek( STREAM_SEEK_TO_BEGIN );
107     while( !bEOF && !aStream.IsEof() && (aStream.Tell() < nStrmSize) )
108 	{
109 	    sal_uInt16 nOpcode, nLength;
110 
111 	    aStream >> nOpcode >> nLength;
112 	    aPrgrsBar.Progress();
113 	    if( nOpcode == LOTUS_EOF )
114 		bEOF = sal_True;
115 
116 	    else if( nOpcode == LOTUS_FILEPASSWD )
117 		return eERR_FILEPASSWD;
118 
119 	    else if( nOpcode < nOps )
120 		pOps[ nOpcode ] ( aStream, nLength );
121 
122         else if( eType == eWK123 &&
123 		     nOpcode == LOTUS_PATTERN )
124             {
125 		// This is really ugly - needs re-factoring ...
126 		aStream.SeekRel(nLength);
127 		aStream >> nOpcode >> nLength;
128 		if ( nOpcode == 0x29a)
129 		{
130 		    aStream.SeekRel(nLength);
131 		    aStream >> nOpcode >> nLength;
132 		    if ( nOpcode == 0x804 )
133 		    {
134 			aStream.SeekRel(nLength);
135 			OP_ApplyPatternArea123(aStream);
136 		    }
137 		    else
138 			aStream.SeekRel(nLength);
139 		}
140 		else
141 		    aStream.SeekRel(nLength);
142 	    }
143 	    else
144 		aStream.SeekRel( nLength );
145 	}
146 
147 	MemDelete();
148 
149     rDoc.CalcAfterLoad();
150 
151 	return eERR_OK;
152 }
153 
ScanVersion(SvStream & aStream)154 WKTYP ScanVersion( SvStream& aStream )
155 {
156 	// PREC:    pWKDatei:   Zeiger auf offene Datei
157 	// POST:    return:     Typ der Datei
158 	sal_uInt16			nOpcode, nVersNr, nRecLen;
159 
160 	// erstes Byte muss wegen BOF zwingend 0 sein!
161 	aStream >> nOpcode;
162 	if( nOpcode != nBOF )
163 		return eWK_UNKNOWN;
164 
165 	aStream >> nRecLen >> nVersNr;
166 
167 	if( aStream.IsEof() )
168 		return eWK_Error;
169 
170 	switch( nVersNr )
171 	{
172 		case 0x0404:
173 			if( nRecLen == 2 )
174 				return eWK_1;
175 			else
176 				return eWK_UNKNOWN;
177 
178 		case 0x0406:
179 			if( nRecLen == 2 )
180 				return eWK_2;
181 			else
182 				return eWK_UNKNOWN;
183 
184 		case 0x1000:
185 			aStream >> nVersNr;
186 			if( aStream.IsEof() ) return eWK_Error;
187 			if( nVersNr == 0x0004 && nRecLen == 26 )
188 			{	// 4 bytes of 26 read => skip 22 (read instead of seek to make IsEof() work just in case)
189                 sal_Char aDummy[22];
190                 aStream.Read( aDummy, 22 );
191                 return aStream.IsEof() ? eWK_Error : eWK3;
192 			}
193 			break;
194 		case 0x1003:
195 			if( nRecLen == 0x1a )
196 				return eWK123;
197 			else
198 				return eWK_UNKNOWN;
199 		case 0x1005:
200 			if( nRecLen == 0x1a )
201 				return eWK123;
202 			else
203 				return eWK_UNKNOWN;
204 	}
205 
206 	return eWK_UNKNOWN;
207 }
208 
ScImportLotus123old(SvStream & aStream,ScDocument * pDocument,CharSet eSrc)209 FltError ScImportLotus123old( SvStream& aStream, ScDocument* pDocument, CharSet eSrc )
210 {
211 	aStream.Seek( 0UL );
212 
213 	// Zeiger auf Dokument global machen
214 	pDoc = pDocument;
215 
216 	bEOF = sal_False;
217 
218 	eCharVon = eSrc;
219 
220 	// Speicher besorgen
221 	if( !MemNew() )
222 		return eERR_NOMEM;
223 
224 	InitPage(); // Seitenformat initialisieren (nur Tab 0!)
225 
226         // Progressbar starten
227 	ScfStreamProgressBar aPrgrsBar( aStream, pDocument->GetDocumentShell() );
228 
229 	// Datei-Typ ermitteln
230 	eTyp = ScanVersion( aStream );
231 
232 	aLotusPatternPool.clear();
233 
234     return generate_Opcodes( aStream, *pDoc, aPrgrsBar, eTyp );
235 }
236 
237 
238