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