1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
19 #ifndef INCLUDED_COMPHELPER_STORAGEHELPER_HXX
20 #define INCLUDED_COMPHELPER_STORAGEHELPER_HXX
22 #include <com/sun/star/uno/Sequence.h>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <comphelper/comphelperdllapi.h>
27 #include <string_view>
29 inline constexpr OUStringLiteral PACKAGE_STORAGE_FORMAT_STRING
= u
"PackageFormat";
30 inline constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING
= u
"ZipFormat";
31 inline constexpr OUStringLiteral OFOPXML_STORAGE_FORMAT_STRING
= u
"OFOPXMLFormat";
33 inline constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA256UTF8
= u
"PackageSHA256UTF8EncryptionKey";
34 inline constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA1UTF8
= u
"PackageSHA1UTF8EncryptionKey";
35 inline constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA1MS1252
= u
"PackageSHA1MS1252EncryptionKey";
36 inline constexpr OUStringLiteral PACKAGE_ENCRYPTIONDATA_SHA1CORRECT
= u
"PackageSHA1CorrectEncryptionKey";
38 namespace com::sun::star
{
39 namespace beans
{ struct NamedValue
; }
40 namespace embed
{ class XStorage
; }
46 namespace lang
{ class XSingleServiceFactory
; }
47 namespace uno
{ class XComponentContext
; }
50 namespace comphelper
{
52 // Unfortunately - the impl.s of XStorage like to invalidate all
53 // their sub streams and storages when you release references, so
54 // it is necessary to keep references to all storages down the
55 // path - this is 'beautiful' (TM). So we need this ugly hack:
56 class COMPHELPER_DLLPUBLIC LifecycleProxy
61 std::unique_ptr
<Impl
> m_xBadness
;
64 // commit the storages: necessary for writes to streams to take effect!
65 void commitStorages();
68 class COMPHELPER_DLLPUBLIC OStorageHelper
71 /// @throws css::uno::Exception
72 static css::uno::Reference
< css::lang::XSingleServiceFactory
>
74 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
75 = css::uno::Reference
< css::uno::XComponentContext
>() );
77 /// @throws css::uno::Exception
78 static css::uno::Reference
< css::lang::XSingleServiceFactory
>
79 GetFileSystemStorageFactory(
80 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
81 = css::uno::Reference
< css::uno::XComponentContext
>() );
83 /// @throws css::uno::Exception
84 static css::uno::Reference
< css::embed::XStorage
>
86 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
87 = css::uno::Reference
< css::uno::XComponentContext
>() );
89 /// this one will only return Storage
91 /// @throws css::uno::Exception
92 static css::uno::Reference
< css::embed::XStorage
>
95 sal_Int32 nStorageMode
,
96 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
97 = css::uno::Reference
< css::uno::XComponentContext
>() );
99 /// this one will return either Storage or FileSystemStorage
101 /// @throws css::uno::Exception
102 static css::uno::Reference
< css::embed::XStorage
>
104 const OUString
& aURL
,
105 sal_Int32 nStorageMode
,
106 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
107 = css::uno::Reference
< css::uno::XComponentContext
>() );
109 /// @throws css::uno::Exception
110 static css::uno::Reference
< css::embed::XStorage
>
111 GetStorageFromInputStream(
112 const css::uno::Reference
< css::io::XInputStream
>& xStream
,
113 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
114 = css::uno::Reference
< css::uno::XComponentContext
>() );
116 /// @throws css::uno::Exception
117 static css::uno::Reference
< css::embed::XStorage
>
118 GetStorageFromStream(
119 const css::uno::Reference
< css::io::XStream
>& xStream
,
120 sal_Int32 nStorageMode
= css::embed::ElementModes::READWRITE
,
121 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
122 = css::uno::Reference
< css::uno::XComponentContext
>() );
124 /// @throws css::uno::Exception
125 static void CopyInputToOutput(
126 const css::uno::Reference
< css::io::XInputStream
>& xInput
,
127 const css::uno::Reference
< css::io::XOutputStream
>& xOutput
);
129 /// @throws css::uno::Exception
130 static css::uno::Reference
< css::io::XInputStream
>
131 GetInputStreamFromURL(
132 const OUString
& aURL
,
133 const css::uno::Reference
< css::uno::XComponentContext
>& context
);
135 /// @throws css::uno::Exception
136 static void SetCommonStorageEncryptionData(
137 const css::uno::Reference
< css::embed::XStorage
>& xStorage
,
138 const css::uno::Sequence
< css::beans::NamedValue
>& aEncryptionData
);
140 // the following method supports only storages of OOo formats
141 /// @throws css::uno::Exception
142 static sal_Int32
GetXStorageFormat(
143 const css::uno::Reference
< css::embed::XStorage
>& xStorage
);
145 /// @throws css::uno::Exception
146 static css::uno::Reference
< css::embed::XStorage
>
147 GetStorageOfFormatFromURL(
148 const OUString
& aFormat
,
149 const OUString
& aURL
,
150 sal_Int32 nStorageMode
,
151 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
152 = css::uno::Reference
< css::uno::XComponentContext
>() );
154 /// @throws css::uno::Exception
155 static css::uno::Reference
< css::embed::XStorage
>
156 GetStorageOfFormatFromInputStream(
157 const OUString
& aFormat
,
158 const css::uno::Reference
< css::io::XInputStream
>& xStream
,
159 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
160 = css::uno::Reference
< css::uno::XComponentContext
>(),
161 bool bRepairStorage
= false );
163 /// @throws css::uno::Exception
164 static css::uno::Reference
< css::embed::XStorage
>
165 GetStorageOfFormatFromStream(
166 const OUString
& aFormat
,
167 const css::uno::Reference
< css::io::XStream
>& xStream
,
168 sal_Int32 nStorageMode
= css::embed::ElementModes::READWRITE
,
169 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
170 = css::uno::Reference
< css::uno::XComponentContext
>(),
171 bool bRepairStorage
= false );
173 static css::uno::Sequence
< css::beans::NamedValue
>
174 CreatePackageEncryptionData(
175 std::u16string_view aPassword
);
177 static css::uno::Sequence
< css::beans::NamedValue
>
178 CreateGpgPackageEncryptionData();
180 static bool IsValidZipEntryFileName( std::u16string_view aName
, bool bSlashAllowed
);
182 static bool PathHasSegment( std::u16string_view aPath
, std::u16string_view aSegment
);
184 // Methods to allow easy use of hierarchical names inside storages
186 static css::uno::Reference
< css::embed::XStorage
> GetStorageAtPath(
187 const css::uno::Reference
< css::embed::XStorage
> &xStorage
,
188 std::u16string_view aPath
, sal_uInt32 nOpenMode
, LifecycleProxy
const &rNastiness
);
189 static css::uno::Reference
< css::io::XStream
> GetStreamAtPath(
190 const css::uno::Reference
< css::embed::XStorage
> &xStorage
,
191 std::u16string_view aPath
, sal_uInt32 nOpenMode
, LifecycleProxy
const &rNastiness
);
192 static css::uno::Reference
< css::io::XStream
> GetStreamAtPackageURL(
193 const css::uno::Reference
< css::embed::XStorage
> &xStorage
,
194 const OUString
& rURL
, sal_uInt32
const nOpenMode
,
195 LifecycleProxy
const & rNastiness
);
198 GetODFVersionFromStorage(const css::uno::Reference
<css::embed::XStorage
>& xStorage
);
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */