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 0 : OZipFileAccess::OZipFileAccess( const uno::Reference< uno::XComponentContext >& rxContext )
45 0 : : m_aMutexHolder( new SotMutexHolder )
46 : , m_xContext( rxContext )
47 : , m_pZipFile( NULL )
48 : , m_pListenersContainer( NULL )
49 : , m_bDisposed( sal_False )
50 0 : , m_bOwnContent( false )
51 : {
52 0 : if ( !rxContext.is() )
53 0 : throw uno::RuntimeException(THROW_WHERE, uno::Reference< uno::XInterface >() );
54 0 : }
55 :
56 0 : OZipFileAccess::~OZipFileAccess()
57 : {
58 : {
59 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
60 0 : if ( !m_bDisposed )
61 : {
62 : try {
63 0 : m_refCount++; // dispose will use refcounting so the further distruction must be avoided
64 0 : dispose();
65 0 : } catch( uno::Exception& )
66 : {}
67 0 : }
68 : }
69 0 : }
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 : sal_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 sal_False;
123 :
124 0 : if ( nInd == 0 )
125 : {
126 0 : if ( aPattern[0].isEmpty() )
127 0 : return sal_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 sal_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 sal_False;
151 :
152 0 : if ( nLastInd < nBeginInd )
153 0 : return sal_False;
154 :
155 0 : nEndInd = nLastInd;
156 : }
157 :
158 0 : return sal_True;
159 : }
160 :
161 0 : return sal_False;
162 : }
163 :
164 : // XInitialization
165 0 : void SAL_CALL OZipFileAccess::initialize( const uno::Sequence< uno::Any >& aArguments )
166 : throw ( uno::Exception,
167 : uno::RuntimeException, std::exception )
168 : {
169 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
170 :
171 0 : if ( m_bDisposed )
172 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
173 :
174 0 : if ( m_pZipFile )
175 0 : throw uno::RuntimeException(THROW_WHERE, uno::Reference< uno::XInterface >() ); // initialization is allowed only one time
176 :
177 0 : 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 0 : OUString aParamURL;
183 0 : uno::Reference< io::XStream > xStream;
184 0 : uno::Reference< io::XSeekable > xSeekable;
185 :
186 0 : if ( ( aArguments[0] >>= aParamURL ) )
187 : {
188 : ::ucbhelper::Content aContent(
189 : aParamURL,
190 : uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
191 0 : m_xContext );
192 0 : uno::Reference < io::XActiveDataSink > xSink = new ZipPackageSink;
193 0 : if ( aContent.openStream ( xSink ) )
194 : {
195 0 : m_xContentStream = xSink->getInputStream();
196 0 : m_bOwnContent = true;
197 0 : xSeekable = uno::Reference< io::XSeekable >( m_xContentStream, uno::UNO_QUERY );
198 0 : }
199 : }
200 0 : else if ( (aArguments[0] >>= xStream ) )
201 : {
202 : // a writable stream can implement both XStream & XInputStream
203 0 : m_xContentStream = xStream->getInputStream();
204 0 : xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
205 : }
206 0 : else if ( aArguments[0] >>= m_xContentStream )
207 : {
208 0 : 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 0 : if ( !m_xContentStream.is() )
214 0 : throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
215 :
216 0 : 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, uno::Reference< uno::XInterface >() );
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 0 : sal_True );
227 0 : }
228 :
229 : // XNameAccess
230 0 : uno::Any SAL_CALL OZipFileAccess::getByName( const OUString& aName )
231 : throw ( container::NoSuchElementException,
232 : lang::WrappedTargetException,
233 : uno::RuntimeException, std::exception )
234 : {
235 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
236 :
237 0 : if ( m_bDisposed )
238 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
239 :
240 0 : if ( !m_pZipFile )
241 0 : throw io::NotConnectedException(THROW_WHERE, 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(THROW_WHERE, 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(THROW_WHERE, uno::Reference< uno::XInterface >() );
254 :
255 0 : return uno::makeAny ( xEntryStream );
256 : }
257 :
258 0 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::getElementNames()
259 : throw ( uno::RuntimeException, std::exception )
260 : {
261 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
262 :
263 0 : if ( m_bDisposed )
264 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
265 :
266 0 : if ( !m_pZipFile )
267 0 : throw io::NotConnectedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
268 :
269 0 : uno::Sequence< OUString > aNames( m_pZipFile->GetEntryHash().size() );
270 0 : sal_Int32 nLen = 0;
271 :
272 0 : for ( EntryHash::iterator aIter = m_pZipFile->GetEntryHash().begin(); aIter != m_pZipFile->GetEntryHash().end(); ++aIter )
273 : {
274 0 : if ( aNames.getLength() < ++nLen )
275 : {
276 : OSL_FAIL( "The size must be the same!\n" );
277 0 : aNames.realloc( nLen );
278 : }
279 :
280 0 : aNames[nLen-1] = (*aIter).second.sPath;
281 : }
282 :
283 0 : if ( aNames.getLength() != nLen )
284 : {
285 : OSL_FAIL( "The size must be the same!\n" );
286 0 : aNames.realloc( nLen );
287 : }
288 :
289 0 : return aNames;
290 : }
291 :
292 0 : sal_Bool SAL_CALL OZipFileAccess::hasByName( const OUString& aName )
293 : throw (uno::RuntimeException, std::exception)
294 : {
295 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
296 :
297 0 : if ( m_bDisposed )
298 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
299 :
300 0 : if ( !m_pZipFile )
301 0 : throw io::NotConnectedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
302 :
303 0 : EntryHash::iterator aIter = m_pZipFile->GetEntryHash().find( aName );
304 :
305 0 : return ( aIter != m_pZipFile->GetEntryHash().end() );
306 : }
307 :
308 0 : uno::Type SAL_CALL OZipFileAccess::getElementType()
309 : throw ( uno::RuntimeException, std::exception )
310 : {
311 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
312 :
313 0 : if ( m_bDisposed )
314 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
315 :
316 0 : if ( !m_pZipFile )
317 0 : throw io::NotConnectedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
318 :
319 0 : return getCppuType( ( const uno::Reference< io::XInputStream >* )NULL );
320 : }
321 :
322 0 : sal_Bool SAL_CALL OZipFileAccess::hasElements()
323 : throw ( uno::RuntimeException, std::exception )
324 : {
325 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
326 :
327 0 : if ( m_bDisposed )
328 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
329 :
330 0 : if ( !m_pZipFile )
331 0 : throw io::NotConnectedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
332 :
333 0 : return ( m_pZipFile->GetEntryHash().size() != 0 );
334 : }
335 :
336 : // XZipFileAccess
337 0 : uno::Reference< io::XInputStream > SAL_CALL OZipFileAccess::getStreamByPattern( const OUString& aPatternString )
338 : throw ( container::NoSuchElementException,
339 : io::IOException,
340 : uno::RuntimeException, std::exception )
341 : {
342 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
343 :
344 0 : if ( m_bDisposed )
345 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
346 :
347 0 : if ( !m_pZipFile )
348 0 : throw io::NotConnectedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
349 :
350 : // Code to compare strings by patterns
351 0 : uno::Sequence< OUString > aPattern = GetPatternsFromString_Impl( aPatternString );
352 :
353 0 : for ( EntryHash::iterator aIter = m_pZipFile->GetEntryHash().begin(); aIter != m_pZipFile->GetEntryHash().end(); ++aIter )
354 : {
355 0 : if ( StringGoodForPattern_Impl( (*aIter).second.sPath, aPattern ) )
356 : {
357 0 : uno::Reference< io::XInputStream > xEntryStream( m_pZipFile->getDataStream( (*aIter).second,
358 : ::rtl::Reference< EncryptionData >(),
359 : sal_False,
360 0 : m_aMutexHolder ) );
361 :
362 0 : if ( !xEntryStream.is() )
363 0 : throw uno::RuntimeException(THROW_WHERE, uno::Reference< uno::XInterface >() );
364 0 : return xEntryStream;
365 : }
366 : }
367 :
368 0 : throw container::NoSuchElementException(THROW_WHERE, uno::Reference< uno::XInterface >() );
369 : }
370 :
371 : // XComponent
372 0 : void SAL_CALL OZipFileAccess::dispose()
373 : throw ( uno::RuntimeException, std::exception )
374 : {
375 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
376 :
377 0 : if ( m_bDisposed )
378 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
379 :
380 0 : if ( m_pListenersContainer )
381 : {
382 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
383 0 : m_pListenersContainer->disposeAndClear( aSource );
384 0 : delete m_pListenersContainer;
385 0 : m_pListenersContainer = NULL;
386 : }
387 :
388 0 : if ( m_pZipFile )
389 : {
390 0 : delete m_pZipFile;
391 0 : m_pZipFile = NULL;
392 : }
393 :
394 0 : if ( m_xContentStream.is() && m_bOwnContent )
395 : try {
396 0 : m_xContentStream->closeInput();
397 0 : } catch( uno::Exception& )
398 : {}
399 :
400 0 : m_bDisposed = sal_True;
401 0 : }
402 :
403 0 : void SAL_CALL OZipFileAccess::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
404 : throw ( uno::RuntimeException, std::exception )
405 : {
406 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
407 :
408 0 : if ( m_bDisposed )
409 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
410 :
411 0 : if ( !m_pListenersContainer )
412 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutexHolder->GetMutex() );
413 0 : m_pListenersContainer->addInterface( xListener );
414 0 : }
415 :
416 0 : void SAL_CALL OZipFileAccess::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
417 : throw ( uno::RuntimeException, std::exception )
418 : {
419 0 : ::osl::MutexGuard aGuard( m_aMutexHolder->GetMutex() );
420 :
421 0 : if ( m_bDisposed )
422 0 : throw lang::DisposedException(THROW_WHERE, uno::Reference< uno::XInterface >() );
423 :
424 0 : if ( m_pListenersContainer )
425 0 : m_pListenersContainer->removeInterface( xListener );
426 0 : }
427 :
428 0 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::impl_staticGetSupportedServiceNames()
429 : {
430 0 : uno::Sequence< OUString > aRet(2);
431 0 : aRet[0] = "com.sun.star.packages.zip.ZipFileAccess";
432 0 : aRet[1] = "com.sun.star.comp.packages.zip.ZipFileAccess";
433 0 : return aRet;
434 : }
435 :
436 0 : OUString SAL_CALL OZipFileAccess::impl_staticGetImplementationName()
437 : {
438 0 : return OUString("com.sun.star.comp.package.zip.ZipFileAccess");
439 : }
440 :
441 0 : uno::Reference< uno::XInterface > SAL_CALL OZipFileAccess::impl_staticCreateSelfInstance(
442 : const uno::Reference< lang::XMultiServiceFactory >& rxMSF )
443 : {
444 0 : return uno::Reference< uno::XInterface >( *new OZipFileAccess( comphelper::getComponentContext(rxMSF) ) );
445 : }
446 :
447 0 : OUString SAL_CALL OZipFileAccess::getImplementationName()
448 : throw ( uno::RuntimeException, std::exception )
449 : {
450 0 : return impl_staticGetImplementationName();
451 : }
452 :
453 0 : sal_Bool SAL_CALL OZipFileAccess::supportsService( const OUString& ServiceName )
454 : throw ( uno::RuntimeException, std::exception )
455 : {
456 0 : return cppu::supportsService(this, ServiceName);
457 : }
458 :
459 0 : uno::Sequence< OUString > SAL_CALL OZipFileAccess::getSupportedServiceNames()
460 : throw ( uno::RuntimeException, std::exception )
461 : {
462 0 : return impl_staticGetSupportedServiceNames();
463 : }
464 :
465 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|