Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlsecurity / source / helper / xmlsignaturehelper2.cxx
blob1a7a334595720410958532fe104f8fdcd60da9f3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xmlsignaturehelper2.hxx>
22 #include <tools/solar.h>
23 #include <unotools/streamhelper.hxx>
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>
31 using namespace com::sun::star;
33 // XUriBinding
35 UriBindingHelper::UriBindingHelper()
39 UriBindingHelper::UriBindingHelper( const css::uno::Reference < css::embed::XStorage >& rxStorage )
41 mxStorage = rxStorage;
44 void SAL_CALL UriBindingHelper::setUriBinding( const OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
48 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const OUString& uri )
50 uno::Reference< io::XInputStream > xInputStream;
51 if ( mxStorage.is() )
53 xInputStream = OpenInputStream( mxStorage, uri );
55 else
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 );
62 return xInputStream;
65 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const OUString& rURI )
67 OSL_ASSERT(!rURI.isEmpty());
68 uno::Reference < io::XInputStream > xInStream;
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);
80 sal_Int32 nSepPos = aURI.indexOf( '/' );
81 if ( nSepPos == -1 )
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);
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();
99 else
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);
106 OUString aElement = aURI.copy( nSepPos+1 );
107 uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
108 xInStream = OpenInputStream( xSubStore, aElement );
110 return xInStream;
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */