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