1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _COMPHELPER_STORAGEHELPER_HXX
24 #define _COMPHELPER_STORAGEHELPER_HXX
25 
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 #include <com/sun/star/embed/XStorage.hpp>
31 #include <com/sun/star/embed/ElementModes.hpp>
32 #include <com/sun/star/beans/NamedValue.hpp>
33 #include <com/sun/star/io/XInputStream.hpp>
34 #include <com/sun/star/io/XOutputStream.hpp>
35 #include <com/sun/star/io/XStream.hpp>
36 #include "comphelper/comphelperdllapi.h"
37 
38 
39 #define PACKAGE_STORAGE_FORMAT_STRING	::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageFormat" ) )
40 #define ZIP_STORAGE_FORMAT_STRING		::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ZipFormat" ) )
41 #define OFOPXML_STORAGE_FORMAT_STRING	::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OFOPXMLFormat" ) )
42 
43 #define PACKAGE_ENCRYPTIONDATA_SHA256UTF8 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA256UTF8EncryptionKey" ) )
44 #define PACKAGE_ENCRYPTIONDATA_SHA1UTF8   ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA1UTF8EncryptionKey" ) )
45 #define PACKAGE_ENCRYPTIONDATA_SHA1MS1252 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "PackageSHA1MS1252EncryptionKey" ) )
46 
47 namespace comphelper {
48 
49 class COMPHELPER_DLLPUBLIC OStorageHelper
50 {
51 public:
52 	static ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory >
53 		GetStorageFactory(
54 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xSF
55 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
56 		throw ( ::com::sun::star::uno::Exception );
57 
58 	static ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory >
59 		GetFileSystemStorageFactory(
60 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xSF
61 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
62 		throw ( ::com::sun::star::uno::Exception );
63 
64 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
65 		GetTemporaryStorage(
66 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
67 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
68 		throw ( ::com::sun::star::uno::Exception );
69 
70     /// this one will only return Storage
71 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
72 		GetStorageFromURL(
73 			const ::rtl::OUString& aURL,
74 			sal_Int32 nStorageMode,
75 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
76 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
77 		throw ( ::com::sun::star::uno::Exception );
78 
79     /// this one will return either Storage or FileSystemStorage
80 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
81 		GetStorageFromURL2(
82 			const ::rtl::OUString& aURL,
83 			sal_Int32 nStorageMode,
84 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
85 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
86 
87 		throw ( ::com::sun::star::uno::Exception );
88 
89 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
90         GetStorageFromInputStream(
91             const ::com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream >& xStream,
92 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
93 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
94 		throw ( ::com::sun::star::uno::Exception );
95 
96 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
97         GetStorageFromStream(
98             const ::com::sun::star::uno::Reference < ::com::sun::star::io::XStream >& xStream,
99 			sal_Int32 nStorageMode = ::com::sun::star::embed::ElementModes::READWRITE,
100 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
101 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
102 		throw ( ::com::sun::star::uno::Exception );
103 
104 	static void CopyInputToOutput(
105 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xInput,
106 			const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& xOutput )
107 		throw ( ::com::sun::star::uno::Exception );
108 
109 	static ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
110 		GetInputStreamFromURL(
111 			const ::rtl::OUString& aURL,
112 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
113 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
114 		throw ( ::com::sun::star::uno::Exception );
115 
116 	static void SetCommonStorageEncryptionData(
117 			const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage,
118 			const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aEncryptionData )
119 		throw ( ::com::sun::star::uno::Exception );
120 
121 	// the following method supports only storages of OOo formats
122 	static sal_Int32 GetXStorageFormat(
123 			const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage )
124 		throw ( ::com::sun::star::uno::Exception );
125 
126 	// The followin methods are related to creation of a storage of specified format
127 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
128 		GetTemporaryStorageOfFormat(
129 			const ::rtl::OUString& aFormat,
130 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
131 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() )
132 		throw ( ::com::sun::star::uno::Exception );
133 
134 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
135 		GetStorageOfFormatFromURL(
136 			const ::rtl::OUString& aFormat,
137 			const ::rtl::OUString& aURL,
138 			sal_Int32 nStorageMode,
139 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
140 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >(),
141             sal_Bool bRepairStorage = sal_False )
142 		throw ( ::com::sun::star::uno::Exception );
143 
144 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
145         GetStorageOfFormatFromInputStream(
146 			const ::rtl::OUString& aFormat,
147             const ::com::sun::star::uno::Reference < ::com::sun::star::io::XInputStream >& xStream,
148 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
149 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >(),
150             sal_Bool bRepairStorage = sal_False )
151 		throw ( ::com::sun::star::uno::Exception );
152 
153 	static ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
154         GetStorageOfFormatFromStream(
155 			const ::rtl::OUString& aFormat,
156             const ::com::sun::star::uno::Reference < ::com::sun::star::io::XStream >& xStream,
157 			sal_Int32 nStorageMode = ::com::sun::star::embed::ElementModes::READWRITE,
158 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xFactory
159 							= ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >(),
160             sal_Bool bRepairStorage = sal_False )
161 		throw ( ::com::sun::star::uno::Exception );
162 
163     static ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
164         CreatePackageEncryptionData(
165             const ::rtl::OUString& aPassword,
166             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xSF
167                 = ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >() );
168 
169     static sal_Bool IsValidZipEntryFileName( const ::rtl::OUString& aName, sal_Bool bSlashAllowed );
170     static sal_Bool IsValidZipEntryFileName( const sal_Unicode *pChar, sal_Int32 nLength, sal_Bool bSlashAllowed );
171 
172     static sal_Bool PathHasSegment( const ::rtl::OUString& aPath, const ::rtl::OUString& aSegment );
173 };
174 
175 }
176 
177 #endif
178 
179