23#include <com/sun/star/ucb/SimpleFileAccess.hpp>
24#include <com/sun/star/embed/ElementModes.hpp>
25#include <com/sun/star/embed/StorageFormats.hpp>
26#include <com/sun/star/beans/PropertyValue.hpp>
27#include <com/sun/star/io/IOException.hpp>
28#include <com/sun/star/io/TempFile.hpp>
29#include <com/sun/star/io/XSeekable.hpp>
35#include <osl/diagnose.h>
43#if OSL_DEBUG_LEVEL > 0
44#define THROW_WHERE SAL_WHERE
50 const uno::Reference< io::XSeekable >& xSeekable )
52 if ( !xInputStream.is() || !xSeekable.is() )
53 throw uno::RuntimeException();
55 if ( xSeekable->getLength() )
57 uno::Sequence< sal_Int8 >
aData( 4 );
59 sal_Int32 nRead = xInputStream->readBytes(
aData, 4 );
64 return ( nRead == 4 &&
aData[0] == 0x50 &&
aData[1] == 0x4b &&
aData[2] == 0x03 &&
aData[3] == 0x04 );
77 return cppu::getXWeak(
new OStorage(xTempStream, embed::ElementModes::READWRITE,
78 uno::Sequence<beans::PropertyValue>(),
m_xContext,
79 embed::StorageFormats::PACKAGE));
83 const uno::Sequence< uno::Any >& aArguments )
93 OSL_ENSURE( nArgNum < 4,
"Wrong parameter number" );
100 sal_Int32 nStorageMode = embed::ElementModes::READ;
105 OSL_FAIL(
"Wrong second argument!" );
106 throw lang::IllegalArgumentException();
109 nStorageMode |= embed::ElementModes::READ;
112 if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
113 && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
114 throw lang::IllegalArgumentException();
118 uno::Reference< io::XStream >
xStream;
119 uno::Reference< io::XInputStream > xInputStream;
123 if (
aURL.isEmpty() )
125 OSL_FAIL(
"Empty URL is provided!" );
126 throw lang::IllegalArgumentException();
129 if (
aURL.startsWithIgnoreAsciiCase(
"vnd.sun.star.pkg:") )
131 OSL_FAIL(
"Packages URL's are not valid for storages!" );
132 throw lang::IllegalArgumentException();
135 uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
136 ucb::SimpleFileAccess::create(
139 if ( nStorageMode & embed::ElementModes::WRITE )
142 xInputStream = xTempAccess->openFileRead(
aURL );
146 OSL_FAIL(
"Wrong first argument!" );
147 throw uno::Exception(
"wrong first arg",
nullptr);
151 uno::Sequence< beans::PropertyValue > aDescr;
152 uno::Sequence< beans::PropertyValue > aPropsToSet;
154 sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
160 if ( !
aURL.isEmpty() )
165 sal_Int32 nNumArgs = 1;
166 for (
const auto& rProp : std::as_const(aDescr) )
168 if ( rProp.Name ==
"InteractionHandler"
169 || rProp.Name ==
"Password"
170 || rProp.Name ==
"RepairPackage"
171 || rProp.Name ==
"StatusIndicator" )
173 aPropsToSet.realloc( ++nNumArgs );
174 auto pPropsToSet = aPropsToSet.getArray();
175 pPropsToSet[nNumArgs-1].Name = rProp.Name;
176 pPropsToSet[nNumArgs-1].Value = rProp.Value;
178 else if ( rProp.Name ==
"StorageFormat" )
180 OUString aFormatName;
181 sal_Int32 nFormatID = 0;
182 if ( rProp.Value >>= aFormatName )
185 nStorageType = embed::StorageFormats::PACKAGE;
187 nStorageType = embed::StorageFormats::ZIP;
189 nStorageType = embed::StorageFormats::OFOPXML;
191 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
193 else if ( rProp.Value >>= nFormatID )
195 if ( nFormatID != embed::StorageFormats::PACKAGE
196 && nFormatID != embed::StorageFormats::ZIP
197 && nFormatID != embed::StorageFormats::OFOPXML )
198 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
200 nStorageType = nFormatID;
203 throw lang::IllegalArgumentException(
THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
205 else if (rProp.Name ==
"NoFileSync")
208 aPropsToSet.realloc(++nNumArgs);
209 auto pPropsToSet = aPropsToSet.getArray();
210 pPropsToSet[nNumArgs - 1].Name = rProp.Name;
211 pPropsToSet[nNumArgs - 1].Value = rProp.Value;
214 OSL_FAIL(
"Unacceptable property, will be ignored!" );
219 OSL_FAIL(
"Wrong third argument!" );
220 throw uno::Exception(
"wrong 3rd arg",
nullptr);
226 if ( xInputStream.is() )
229 if ( nStorageMode & embed::ElementModes::WRITE )
230 throw uno::Exception(
"storagemode==write",
nullptr);
232 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
233 if ( !xSeekable.is() )
236 OSL_FAIL(
"Nonseekable streams are not supported for now!" );
240 throw io::IOException(
"package signature check failed, probably not a package file",
nullptr);
242 return cppu::getXWeak(
247 if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !
xStream->getOutputStream().is() )
248 || !
xStream->getInputStream().is() )
249 throw uno::Exception(
"access denied",
nullptr);
251 uno::Reference< io::XSeekable > xSeekable(
xStream, uno::UNO_QUERY );
252 if ( !xSeekable.is() )
255 OSL_FAIL(
"Nonseekable streams are not supported for now!" );
259 throw io::IOException();
261 return cppu::getXWeak(
265 throw uno::Exception(
"no input stream or regular stream",
nullptr);
270 return "com.sun.star.comp.embed.StorageFactory";
280 return {
"com.sun.star.embed.StorageFactory",
281 "com.sun.star.comp.embed.StorageFactory" };
285extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
287 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const&)
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstanceWithArguments(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Sequence< PropertyValue > aArguments
constexpr OUStringLiteral aData
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral ZIP_STORAGE_FORMAT_STRING
constexpr OUStringLiteral OFOPXML_STORAGE_FORMAT_STRING
constexpr OUStringLiteral PACKAGE_STORAGE_FORMAT_STRING
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * package_OStorageFactory_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
static bool CheckPackageSignature_Impl(const uno::Reference< io::XInputStream > &xInputStream, const uno::Reference< io::XSeekable > &xSeekable)