LibreOffice Module package (master) 1
zipfileaccess.cxx
Go to the documentation of this file.
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#include <com/sun/star/lang/DisposedException.hpp>
21#include <com/sun/star/lang/IllegalArgumentException.hpp>
22#include <com/sun/star/ucb/XCommandEnvironment.hpp>
23#include <com/sun/star/io/NotConnectedException.hpp>
24#include <com/sun/star/io/XActiveDataSink.hpp>
25#include <com/sun/star/io/XStream.hpp>
26#include <com/sun/star/io/XSeekable.hpp>
27#include <com/sun/star/beans/NamedValue.hpp>
31#include <zipfileaccess.hxx>
32#include "ZipPackageSink.hxx"
33#include <EncryptionData.hxx>
34
35#include <ucbhelper/content.hxx>
36#include <rtl/ref.hxx>
37#include <sal/log.hxx>
38#include <osl/diagnose.h>
39
40using namespace ::com::sun::star;
41
42#if OSL_DEBUG_LEVEL > 0
43#define THROW_WHERE SAL_WHERE
44#else
45#define THROW_WHERE ""
46#endif
47
48OZipFileAccess::OZipFileAccess( const uno::Reference< uno::XComponentContext >& rxContext )
49: m_aMutexHolder( new comphelper::RefCountedMutex )
50, m_xContext( rxContext )
51, m_bDisposed( false )
52, m_bOwnContent( false )
53{
54 if ( !rxContext.is() )
55 throw uno::RuntimeException(THROW_WHERE );
56}
57
59{
60 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
61 if ( !m_bDisposed )
62 {
63 try {
64 // dispose will use refcounting so the further destruction must be avoided
65 osl_atomic_increment(&m_refCount);
66 dispose();
67 } catch( uno::Exception& )
68 {}
69 }
70}
71
72uno::Sequence< OUString > OZipFileAccess::GetPatternsFromString_Impl( const OUString& aString )
73{
74 if ( aString.isEmpty() )
75 return uno::Sequence< OUString >();
76
77 uno::Sequence< OUString > aPattern( 1 );
78 auto pPattern = aPattern.getArray();
79 sal_Int32 nInd = 0;
80
81 const sal_Unicode* pString = aString.getStr();
82 while( *pString )
83 {
84 if ( *pString == '\\' )
85 {
86 pString++;
87
88 if ( *pString == '\\' )
89 {
90 pPattern[nInd] += "\\";
91 pString++;
92 }
93 else if ( *pString == '*' )
94 {
95 pPattern[nInd] += "*";
96 pString++;
97 }
98 else
99 {
100 OSL_FAIL( "The backslash is not guarded!" );
101 pPattern[nInd] += "\\";
102 }
103 }
104 else if ( *pString == '*' )
105 {
106 aPattern.realloc( ( ++nInd ) + 1 );
107 pPattern = aPattern.getArray();
108 pString++;
109 }
110 else
111 {
112 pPattern[nInd] += OUStringChar( *pString );
113 pString++;
114 }
115 }
116
117 return aPattern;
118}
119
120bool OZipFileAccess::StringGoodForPattern_Impl( std::u16string_view aString,
121 const uno::Sequence< OUString >& aPattern )
122{
123 sal_Int32 nInd = aPattern.getLength() - 1;
124 if ( nInd < 0 )
125 return false;
126
127 if ( nInd == 0 )
128 {
129 if ( aPattern[0].isEmpty() )
130 return true;
131
132 return aString == aPattern[0];
133 }
134
135 sal_Int32 nBeginInd = aPattern[0].getLength();
136 sal_Int32 nEndInd = aString.size() - aPattern[nInd].getLength();
137 if ( nEndInd < nBeginInd
138 || ( nEndInd != sal_Int32(aString.size()) && aString.substr( nEndInd ) != aPattern[nInd] )
139 || ( nBeginInd != 0 && aString.substr( 0, nBeginInd ) != aPattern[0] ) )
140 return false;
141
142 for ( sal_Int32 nCurInd = aPattern.getLength() - 2; nCurInd > 0; nCurInd-- )
143 {
144 if ( aPattern[nCurInd].isEmpty() )
145 continue;
146
147 if ( nEndInd == nBeginInd )
148 return false;
149
150 // check that search does not use nEndInd position
151 size_t nLastInd = aString.substr(0, nEndInd - 1).rfind( aPattern[nCurInd] );
152
153 if ( nLastInd == std::u16string_view::npos )
154 return false;
155
156 if ( sal_Int32(nLastInd) < nBeginInd )
157 return false;
158
159 nEndInd = nLastInd;
160 }
161
162 return true;
163}
164
165// XInitialization
166void SAL_CALL OZipFileAccess::initialize( const uno::Sequence< uno::Any >& aArguments )
167{
168 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
169
170 if ( m_bDisposed )
171 throw lang::DisposedException(THROW_WHERE );
172
173 if ( m_pZipFile )
174 throw uno::RuntimeException(THROW_WHERE ); // initialization is allowed only one time
175
176 if ( !aArguments.hasElements() )
177 throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
178
179 OSL_ENSURE( aArguments.getLength() == 1, "Too many arguments are provided, only the first one will be used!" );
180
181 OUString aParamURL;
182 uno::Reference< io::XStream > xStream;
183 uno::Reference< io::XSeekable > xSeekable;
184 uno::Sequence<beans::NamedValue> aArgs;
185
186 auto openInputStream = [&]()
187 {
188 ::ucbhelper::Content aContent(
189 aParamURL,
190 uno::Reference< css::ucb::XCommandEnvironment >(),
191 m_xContext );
192 uno::Reference < io::XActiveDataSink > xSink = new ZipPackageSink;
193 if ( aContent.openStream ( xSink ) )
194 {
195 m_xContentStream = xSink->getInputStream();
196 m_bOwnContent = true;
197 xSeekable.set( m_xContentStream, uno::UNO_QUERY );
198 }
199 };
200
201 if ( aArguments[0] >>= aParamURL )
202 {
203 openInputStream();
204 }
205 else if ( aArguments[0] >>= xStream )
206 {
207 // a writable stream can implement both XStream & XInputStream
208 m_xContentStream = xStream->getInputStream();
209 xSeekable.set( xStream, uno::UNO_QUERY );
210 }
211 else if ( aArguments[0] >>= m_xContentStream )
212 {
213 xSeekable.set( m_xContentStream, uno::UNO_QUERY );
214 }
215 else if (aArguments[0] >>= aArgs)
216 {
217 for (const beans::NamedValue& rArg : std::as_const(aArgs))
218 {
219 if (rArg.Name == "URL")
220 rArg.Value >>= aParamURL;
221 }
222
223 if (aParamURL.isEmpty())
224 throw lang::IllegalArgumentException(
225 THROW_WHERE"required argument 'URL' is not given or invalid.",
226 uno::Reference<uno::XInterface>(), 1);
227
228 openInputStream();
229 }
230 else
231 throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
232
233 if ( !m_xContentStream.is() )
234 throw io::IOException(THROW_WHERE );
235
236 if ( !xSeekable.is() )
237 {
238 // TODO: after fwkbugfix02 is integrated a helper class can be used to make the stream seekable
239 throw io::IOException(THROW_WHERE );
240 }
241
242 // TODO: in case xSeekable is implemented on separated XStream implementation a wrapper is required
243 m_pZipFile.emplace(
247 true );
248}
249
250// XNameAccess
251uno::Any SAL_CALL OZipFileAccess::getByName( const OUString& aName )
252{
253 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
254
255 if ( m_bDisposed )
256 throw lang::DisposedException(THROW_WHERE );
257
258 if ( !m_pZipFile )
259 throw uno::RuntimeException(THROW_WHERE);
260
261 EntryHash::iterator aIter = m_pZipFile->GetEntryHash().find( aName );
262 if ( aIter == m_pZipFile->GetEntryHash().end() )
263 throw container::NoSuchElementException(THROW_WHERE );
264
265 uno::Reference< io::XInputStream > xEntryStream;
266 try
267 {
268 xEntryStream = m_pZipFile->getDataStream((*aIter).second,
270 false,
272 }
273 catch (const container::NoSuchElementException&)
274 {
275 throw;
276 }
277 catch (const lang::WrappedTargetException&)
278 {
279 throw;
280 }
281 catch (const uno::RuntimeException&)
282 {
283 throw;
284 }
285 catch (const uno::Exception&)
286 {
287 css::uno::Any anyEx = cppu::getCaughtException();
288 throw lang::WrappedTargetException( "This package is unusable!",
289 getXWeak(), anyEx);
290 }
291
292 if ( !xEntryStream.is() )
293 throw uno::RuntimeException(THROW_WHERE );
294
295 return uno::Any ( xEntryStream );
296}
297
298uno::Sequence< OUString > SAL_CALL OZipFileAccess::getElementNames()
299{
300 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
301
302 if ( m_bDisposed )
303 throw lang::DisposedException(THROW_WHERE );
304
305 if ( !m_pZipFile )
306 throw uno::RuntimeException(THROW_WHERE);
307
308 uno::Sequence< OUString > aNames( m_pZipFile->GetEntryHash().size() );
309 auto pNames = aNames.getArray();
310 sal_Int32 nLen = 0;
311
312 for ( const auto& rEntry : m_pZipFile->GetEntryHash() )
313 {
314 if ( aNames.getLength() < ++nLen )
315 {
316 OSL_FAIL( "The size must be the same!" );
317 aNames.realloc( nLen );
318 pNames = aNames.getArray();
319 }
320
321 pNames[nLen-1] = rEntry.second.sPath;
322 }
323
324 if ( aNames.getLength() != nLen )
325 {
326 OSL_FAIL( "The size must be the same!" );
327 aNames.realloc( nLen );
328 }
329
330 return aNames;
331}
332
333sal_Bool SAL_CALL OZipFileAccess::hasByName( const OUString& aName )
334{
335 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
336
337 if ( m_bDisposed )
338 throw lang::DisposedException(THROW_WHERE );
339
340 if ( !m_pZipFile )
341 throw uno::RuntimeException(THROW_WHERE);
342
343 EntryHash::iterator aIter = m_pZipFile->GetEntryHash().find( aName );
344
345 return ( aIter != m_pZipFile->GetEntryHash().end() );
346}
347
349{
350 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
351
352 if ( m_bDisposed )
353 throw lang::DisposedException(THROW_WHERE );
354
355 if ( !m_pZipFile )
356 throw uno::RuntimeException(THROW_WHERE);
357
359}
360
362{
363 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
364
365 if ( m_bDisposed )
366 throw lang::DisposedException(THROW_WHERE );
367
368 if ( !m_pZipFile )
369 throw uno::RuntimeException(THROW_WHERE);
370
371 return ( !m_pZipFile->GetEntryHash().empty() );
372}
373
374// XZipFileAccess
375uno::Reference< io::XInputStream > SAL_CALL OZipFileAccess::getStreamByPattern( const OUString& aPatternString )
376{
377 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
378
379 if ( m_bDisposed )
380 throw lang::DisposedException(THROW_WHERE );
381
382 if ( !m_pZipFile )
383 throw io::NotConnectedException(THROW_WHERE );
384
385 // Code to compare strings by patterns
386 uno::Sequence< OUString > aPattern = GetPatternsFromString_Impl( aPatternString );
387
388 auto aIter = std::find_if(m_pZipFile->GetEntryHash().begin(), m_pZipFile->GetEntryHash().end(),
389 [&aPattern](const EntryHash::value_type& rEntry) { return StringGoodForPattern_Impl(rEntry.second.sPath, aPattern); });
390 if (aIter != m_pZipFile->GetEntryHash().end())
391 {
392 uno::Reference< io::XInputStream > xEntryStream( m_pZipFile->getDataStream( (*aIter).second,
394 false,
395 m_aMutexHolder ) );
396
397 if ( !xEntryStream.is() )
398 throw uno::RuntimeException(THROW_WHERE );
399 return xEntryStream;
400 }
401
402 throw container::NoSuchElementException(THROW_WHERE );
403}
404
405// XComponent
407{
408 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
409
410 if ( m_bDisposed )
411 throw lang::DisposedException(THROW_WHERE );
412
414 {
415 lang::EventObject aSource( getXWeak() );
416 m_pListenersContainer->disposeAndClear( aSource );
417 m_pListenersContainer.reset();
418 }
419
420 m_pZipFile.reset();
421
422 if ( m_xContentStream.is() && m_bOwnContent )
423 try {
424 m_xContentStream->closeInput();
425 } catch( uno::Exception& )
426 {}
427
428 m_bDisposed = true;
429}
430
431void SAL_CALL OZipFileAccess::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
432{
433 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
434
435 if ( m_bDisposed )
436 throw lang::DisposedException(THROW_WHERE );
437
439 m_pListenersContainer.reset( new ::comphelper::OInterfaceContainerHelper3<css::lang::XEventListener>( m_aMutexHolder->GetMutex() ) );
440 m_pListenersContainer->addInterface( xListener );
441}
442
443void SAL_CALL OZipFileAccess::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
444{
445 ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
446
447 if ( m_bDisposed )
448 throw lang::DisposedException(THROW_WHERE );
449
451 m_pListenersContainer->removeInterface( xListener );
452}
453
455{
456 return "com.sun.star.comp.package.zip.ZipFileAccess";
457}
458
459sal_Bool SAL_CALL OZipFileAccess::supportsService( const OUString& ServiceName )
460{
462}
463
464uno::Sequence< OUString > SAL_CALL OZipFileAccess::getSupportedServiceNames()
465{
466 return { "com.sun.star.packages.zip.ZipFileAccess",
467 "com.sun.star.comp.packages.zip.ZipFileAccess" };
468}
469
470
471extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
473 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
474{
475 return cppu::acquire(new OZipFileAccess(context));
476}
477
478
479/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
Reference< XInputStream > xStream
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getStreamByPattern(const OUString &aPattern) override
OZipFileAccess(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual ~OZipFileAccess() override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Type SAL_CALL getElementType() override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual OUString SAL_CALL getImplementationName() override
static bool StringGoodForPattern_Impl(std::u16string_view, const css::uno::Sequence< OUString > &aPattern)
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
std::unique_ptr<::comphelper::OInterfaceContainerHelper3< css::lang::XEventListener > > m_pListenersContainer
static css::uno::Sequence< OUString > GetPatternsFromString_Impl(const OUString &aString)
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
std::optional< ZipFile > m_pZipFile
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::io::XInputStream > m_xContentStream
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual sal_Bool SAL_CALL hasElements() override
rtl::Reference< comphelper::RefCountedMutex > m_aMutexHolder
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
virtual void SAL_CALL dispose() override
css::uno::Type const & get()
css::uno::Reference< css::io::XInputStream > openStream()
ULONG m_refCount
bool m_bDisposed
Sequence< PropertyValue > aArguments
OUString aName
tools::SvRef< SvBaseLink > xSink
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
unsigned char sal_Bool
sal_uInt16 sal_Unicode
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * package_OZipFileAccess_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
#define THROW_WHERE