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 <xmlsignaturehelper2.hxx>
21 
22 #include <tools/solar.h>
23 #include <unotools/streamhelper.hxx>
24 
25 #include <com/sun/star/embed/XStorage.hpp>
26 #include <com/sun/star/embed/ElementModes.hpp>
27 #include <osl/diagnose.h>
28 #include <rtl/uri.hxx>
29 #include <sal/log.hxx>
30 
31 using namespace com::sun::star;
32 
33 // XUriBinding
34 
UriBindingHelper()35 UriBindingHelper::UriBindingHelper()
36 {
37 }
38 
UriBindingHelper(const css::uno::Reference<css::embed::XStorage> & rxStorage)39 UriBindingHelper::UriBindingHelper( const css::uno::Reference < css::embed::XStorage >& rxStorage )
40 {
41     mxStorage = rxStorage;
42 }
43 
setUriBinding(const OUString &,const uno::Reference<io::XInputStream> &)44 void SAL_CALL UriBindingHelper::setUriBinding( const OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
45 {
46 }
47 
getUriBinding(const OUString & uri)48 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const OUString& uri )
49 {
50     uno::Reference< io::XInputStream > xInputStream;
51     if ( mxStorage.is() )
52     {
53         xInputStream = OpenInputStream( mxStorage, uri );
54     }
55     else
56     {
57         SvFileStream* pStream = new SvFileStream( uri, StreamMode::READ );
58         sal_uInt64 nBytes = pStream->TellEnd();
59         SvLockBytesRef xLockBytes = new SvLockBytes( pStream, true );
60         xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
61     }
62     return xInputStream;
63 }
64 
OpenInputStream(const uno::Reference<embed::XStorage> & rxStore,const OUString & rURI)65 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const OUString& rURI )
66 {
67     OSL_ASSERT(!rURI.isEmpty());
68     uno::Reference < io::XInputStream > xInStream;
69 
70     OUString aURI(rURI);
71     // Ignore leading slash, don't attempt to open a storage with name "".
72     if (aURI.startsWith("/"))
73         aURI = aURI.copy(1);
74     // Ignore query part of the URI.
75     sal_Int32 nQueryPos = aURI.indexOf('?');
76     if (nQueryPos != -1)
77         aURI = aURI.copy(0, nQueryPos);
78 
79 
80     sal_Int32 nSepPos = aURI.indexOf( '/' );
81     if ( nSepPos == -1 )
82     {
83         // Cloning because of I can't keep all storage references open
84         // MBA with think about a better API...
85         const OUString sName = ::rtl::Uri::decode(
86             aURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
87         if (sName.isEmpty() && !aURI.isEmpty())
88             throw uno::Exception("Could not decode URI for stream element.", nullptr);
89 
90         uno::Reference< io::XStream > xStream;
91         if (!rxStore->hasByName(sName))
92             SAL_WARN("xmlsecurity.helper", "expected stream, but not found: " << sName);
93         else
94             xStream = rxStore->cloneStreamElement( sName );
95         if ( !xStream.is() )
96             throw uno::RuntimeException();
97         xInStream = xStream->getInputStream();
98     }
99     else
100     {
101         const OUString aStoreName = ::rtl::Uri::decode(
102             aURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
103         if (aStoreName.isEmpty() && !aURI.isEmpty())
104             throw uno::Exception("Could not decode URI for stream element.", nullptr);
105 
106         OUString aElement = aURI.copy( nSepPos+1 );
107         uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
108         xInStream = OpenInputStream( xSubStore, aElement );
109     }
110     return xInStream;
111 }
112 
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
114