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,
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 /*************************************************************************
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>
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
;
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
86 using namespace OpenStormBento
;
87 #include "explode.hxx"
88 static bool Decompress(SvStream
* pCompressed
, SvStream
*& pOutDecompressed
)
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
)
104 std::unique_ptr
<LtcUtBenValueStream
> aWordProData(
105 pBentoContainer
->FindValueStreamWithPropertyName("WordProData"));
111 Decompression
decompress(aWordProData
.get(), aDecompressed
.get());
112 if (0 != decompress
.explode())
115 sal_uInt64 nPos
= aWordProData
->GetSize();
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();
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;
143 pStream
->ReadUInt32(nTag
);
144 if (nTag
!= 0x3750574c) // "LWP7"
146 // small file, needs decompression
147 if (!Decompress(pStream
, pDecompressed
))
149 pLwpSvStream
= nullptr;
153 pDecompressed
->Seek(0);
156 pLwpSvStream
= nullptr;
157 bool bCompressed
= false;
160 LwpSvStream
* pOriginalLwpSvStream
= new LwpSvStream(pStream
);
161 pLwpSvStream
= new LwpSvStream(pDecompressed
, pOriginalLwpSvStream
);
166 pLwpSvStream
= new LwpSvStream(pStream
);
170 int ReadWordproFile(SvStream
& rStream
,
171 uno::Reference
<css::xml::sax::XDocumentHandler
> const& xHandler
)
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();
185 aDecompressed
.reset(pDecompressed
);
186 aCompressedLwpSvStream
.reset(pRawLwpSvStream
->GetCompressedStream());
190 if (!pRawLwpSvStream
)
192 // nothing returned, fail when uncompressing
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.
202 const bool bOk
= reader
.Read();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */