LibreOffice Module lotuswordpro (master) 1
lwpfilter.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*************************************************************************
3 *
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
6 *
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
9 *
10 * Sun Microsystems Inc., October, 2000
11 *
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
16 *
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.
20 *
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.
25 *
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
30 *
31 *
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.
38 *
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.
45 *
46 * The Initial Developer of the Original Code is: IBM Corporation
47 *
48 * Copyright: 2008 by IBM Corporation
49 *
50 * All Rights Reserved.
51 *
52 * Contributor(s): _______________________________________
53 *
54 *
55 ************************************************************************/
56/*************************************************************************
57 * @file
58 * Circle object.
59 ************************************************************************/
60#include "lwpfilter.hxx"
61#include <xfilter/xfglobal.hxx>
63#include "lwp9reader.hxx"
64#include <lwpsvstream.hxx>
65
66#include <tools/stream.hxx>
67#include <com/sun/star/io/IOException.hpp>
68#include <com/sun/star/text/XTextDocument.hpp>
69
70#include <memory>
71
72using namespace ::cppu;
73using namespace ::com::sun::star::lang;
74using namespace ::com::sun::star::text;
75using namespace ::com::sun::star::io;
76using namespace ::com::sun::star::beans;
77using namespace ::com::sun::star;
78
85#include "bento.hxx"
86using namespace OpenStormBento;
87#include "explode.hxx"
88static bool Decompress(SvStream* pCompressed, SvStream*& pOutDecompressed)
89{
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);
95
96 LwpSvStream aLwpStream(pCompressed);
97 std::unique_ptr<OpenStormBento::LtcBenContainer> pBentoContainer;
98 {
99 sal_uLong ulRet = BenOpenContainer(&aLwpStream, &pBentoContainer);
100 if (ulRet != BenErr_OK)
101 return false;
102 }
103
104 std::unique_ptr<LtcUtBenValueStream> aWordProData(
105 pBentoContainer->FindValueStreamWithPropertyName("WordProData"));
106
107 if (!aWordProData)
108 return false;
109
110 // decompressing
111 Decompression decompress(aWordProData.get(), aDecompressed.get());
112 if (0 != decompress.explode())
113 return false;
114
115 sal_uInt32 nPos = aWordProData->GetSize();
116 nPos += 0x10;
117
118 pCompressed->Seek(nPos);
119 while (sal_uInt32 iRead = pCompressed->ReadBytes(buffer, 512))
120 aDecompressed->WriteBytes(buffer, iRead);
121
122 // disable stream growing past its current size
123 aDecompressed->SetResizeOffset(0);
124
125 //transfer ownership of aDecompressed's ptr
126 pOutDecompressed = aDecompressed.release();
127 return true;
128}
129
137static bool GetLwpSvStream(SvStream* pStream, LwpSvStream*& pLwpSvStream)
138{
139 SvStream* pDecompressed = nullptr;
140
141 pStream->Seek(0x10);
142 sal_uInt32 nTag(0);
143 pStream->ReadUInt32(nTag);
144 if (nTag != 0x3750574c) // "LWP7"
145 {
146 // small file, needs decompression
147 if (!Decompress(pStream, pDecompressed))
148 {
149 pLwpSvStream = nullptr;
150 return true;
151 }
152 pStream->Seek(0);
153 pDecompressed->Seek(0);
154 }
155
156 pLwpSvStream = nullptr;
157 bool bCompressed = false;
158 if (pDecompressed)
159 {
160 LwpSvStream* pOriginalLwpSvStream = new LwpSvStream(pStream);
161 pLwpSvStream = new LwpSvStream(pDecompressed, pOriginalLwpSvStream);
162 bCompressed = true;
163 }
164 else
165 {
166 pLwpSvStream = new LwpSvStream(pStream);
167 }
168 return bCompressed;
169}
171 uno::Reference<css::xml::sax::XDocumentHandler> const& xHandler)
172{
173 int nRet = 0;
174 try
175 {
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)
181 {
182 SvStream* pDecompressed = pRawLwpSvStream->GetStream();
183 if (pDecompressed)
184 {
185 aDecompressed.reset(pDecompressed);
186 aCompressedLwpSvStream.reset(pRawLwpSvStream->GetCompressedStream());
187 }
188 }
189
190 if (!pRawLwpSvStream)
191 {
192 // nothing returned, fail when uncompressing
193 return 1;
194 }
195
196 aLwpSvStream.reset(pRawLwpSvStream);
197
198 XFSaxStream aStrm(xHandler);
199 Lwp9Reader reader(aLwpSvStream.get(), &aStrm);
200 //Reset all static objects,because this function may be called many times.
202 const bool bOk = reader.Read();
203 if (!bOk)
204 nRet = 1;
205 }
206 catch (...)
207 {
208 return 1;
209 }
210 return nRet;
211}
212
213/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 explode()
decompress from instream to outstream
Definition: explode.cxx:208
Reader framework class for Lotus Word Pro 9 file.
Definition: lwp9reader.hxx:69
bool Read()
@descr The entrance of Word Pro 9 import filter.
Definition: lwp9reader.cxx:77
encapsulate XInputStream to provide SvStream like interfaces
Definition: lwpsvstream.hxx:69
SvStream * GetStream()
Definition: lwpsvstream.hxx:87
LwpSvStream * GetCompressedStream()
Definition: lwpsvstream.hxx:86
SvStream & ReadUInt32(sal_uInt32 &rUInt32)
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
Sax stream object, XDocumentHandler wrapper.
Definition: xfsaxstream.hxx:76
sal_uInt16 nPos
static bool GetLwpSvStream(SvStream *pStream, LwpSvStream *&pLwpSvStream)
@descr Get LwpSvStream, if small file, both compressed/decompressed stream Otherwise,...
Definition: lwpfilter.cxx:137
static bool Decompress(SvStream *pCompressed, SvStream *&pOutDecompressed)
Definition: lwpfilter.cxx:88
int ReadWordproFile(SvStream &rStream, uno::Reference< css::xml::sax::XDocumentHandler > const &xHandler)
Definition: lwpfilter.cxx:170
sal_uLong BenOpenContainer(LwpSvStream *pStream, std::unique_ptr< LtcBenContainer > *ppContainer)
New bento container from file stream.
Definition: bencont.cxx:74
sal_uIntPtr sal_uLong
void XFGlobalReset()
Definition: xfglobal.cxx:123