tdf#163948: fix crash when NotesPane is enabled on Tabbed UI
[LibreOffice.git] / lotuswordpro / source / filter / lwpfilter.cxx
blob9a8f8ef4d0261c8b8ece4fbf09c3e08a0193816f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
12 * GNU Lesser General Public License Version 2.1
13 * =============================================
14 * Copyright 2000 by Sun Microsystems, Inc.
15 * 901 San Antonio Road, Palo Alto, CA 94303, USA
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License version 2.1, as published by the Free Software Foundation.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
32 * Sun Industry Standards Source License Version 1.1
33 * =================================================
34 * The contents of this file are subject to the Sun Industry Standards
35 * Source License Version 1.1 (the "License"); You may not use this file
36 * except in compliance with the License. You may obtain a copy of the
37 * License at http://www.openoffice.org/license.html.
39 * Software provided under this License is provided on an "AS IS" basis,
40 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 * See the License for the specific provisions governing your rights and
44 * obligations concerning the Software.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /*************************************************************************
57 * @file
58 * Circle object.
59 ************************************************************************/
60 #include "lwpfilter.hxx"
61 #include <xfilter/xfglobal.hxx>
62 #include <xfilter/xfsaxstream.hxx>
63 #include "lwp9reader.hxx"
64 #include <lwpsvstream.hxx>
66 #include <tools/stream.hxx>
67 #include <com/sun/star/io/IOException.hpp>
68 #include <com/sun/star/text/XTextDocument.hpp>
70 #include <memory>
72 using namespace ::cppu;
73 using namespace ::com::sun::star::lang;
74 using namespace ::com::sun::star::text;
75 using namespace ::com::sun::star::io;
76 using namespace ::com::sun::star::beans;
77 using namespace ::com::sun::star;
79 /**
80 * @descr decompressed small file
81 * @param pCompressed - real file stream
82 * @param pDecompressed - file decompressed, create inside, caller should delete it
83 * @return success - sal_True, fail - sal_False
85 #include "bento.hxx"
86 using namespace OpenStormBento;
87 #include "explode.hxx"
88 static bool Decompress(SvStream* pCompressed, SvStream*& pOutDecompressed)
90 pCompressed->Seek(0);
91 std::unique_ptr<SvMemoryStream> aDecompressed(new SvMemoryStream(4096, 4096));
92 unsigned char buffer[512];
93 pCompressed->ReadBytes(buffer, 16);
94 aDecompressed->WriteBytes(buffer, 16);
96 LwpSvStream aLwpStream(pCompressed);
97 std::unique_ptr<OpenStormBento::LtcBenContainer> pBentoContainer;
99 BenError ulRet = BenOpenContainer(&aLwpStream, &pBentoContainer);
100 if (ulRet != BenErr_OK)
101 return false;
104 std::unique_ptr<LtcUtBenValueStream> aWordProData(
105 pBentoContainer->FindValueStreamWithPropertyName("WordProData"));
107 if (!aWordProData)
108 return false;
110 // decompressing
111 Decompression decompress(aWordProData.get(), aDecompressed.get());
112 if (0 != decompress.explode())
113 return false;
115 sal_uInt64 nPos = aWordProData->GetSize();
116 nPos += 0x10;
118 pCompressed->Seek(nPos);
119 while (sal_uInt32 iRead = pCompressed->ReadBytes(buffer, 512))
120 aDecompressed->WriteBytes(buffer, iRead);
122 // disable stream growing past its current size
123 aDecompressed->SetResizeOffset(0);
125 //transfer ownership of aDecompressed's ptr
126 pOutDecompressed = aDecompressed.release();
127 return true;
131 * @descr Get LwpSvStream, if small file, both compressed/decompressed stream
132 * Otherwise, only normal stream
133 * @param pStream - real file stream
134 * @param LwpSvStream * , created inside, deleted outside
135 * @param sal_Bool, sal_True -
137 static bool GetLwpSvStream(SvStream* pStream, LwpSvStream*& pLwpSvStream)
139 SvStream* pDecompressed = nullptr;
141 pStream->Seek(0x10);
142 sal_uInt32 nTag(0);
143 pStream->ReadUInt32(nTag);
144 if (nTag != 0x3750574c) // "LWP7"
146 // small file, needs decompression
147 if (!Decompress(pStream, pDecompressed))
149 pLwpSvStream = nullptr;
150 return true;
152 pStream->Seek(0);
153 pDecompressed->Seek(0);
156 pLwpSvStream = nullptr;
157 bool bCompressed = false;
158 if (pDecompressed)
160 LwpSvStream* pOriginalLwpSvStream = new LwpSvStream(pStream);
161 pLwpSvStream = new LwpSvStream(pDecompressed, pOriginalLwpSvStream);
162 bCompressed = true;
164 else
166 pLwpSvStream = new LwpSvStream(pStream);
168 return bCompressed;
170 int ReadWordproFile(SvStream& rStream,
171 uno::Reference<css::xml::sax::XDocumentHandler> const& xHandler)
173 int nRet = 0;
176 LwpSvStream* pRawLwpSvStream = nullptr;
177 std::unique_ptr<LwpSvStream> aLwpSvStream;
178 std::unique_ptr<LwpSvStream> aCompressedLwpSvStream;
179 std::unique_ptr<SvStream> aDecompressed;
180 if (GetLwpSvStream(&rStream, pRawLwpSvStream) && pRawLwpSvStream)
182 SvStream* pDecompressed = pRawLwpSvStream->GetStream();
183 if (pDecompressed)
185 aDecompressed.reset(pDecompressed);
186 aCompressedLwpSvStream.reset(pRawLwpSvStream->GetCompressedStream());
190 if (!pRawLwpSvStream)
192 // nothing returned, fail when uncompressing
193 return 1;
196 aLwpSvStream.reset(pRawLwpSvStream);
198 XFSaxStream aStrm(xHandler);
199 Lwp9Reader reader(aLwpSvStream.get(), &aStrm);
200 //Reset all static objects,because this function may be called many times.
201 XFGlobalReset();
202 const bool bOk = reader.Read();
203 if (!bOk)
204 nRet = 1;
206 catch (...)
208 return 1;
210 return nRet;
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */