Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/package/source/xstor/xfactory.cxx
Line
Count
Source
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 <sal/config.h>
21
#include <sal/log.hxx>
22
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>
30
31
#include <comphelper/propertyvalue.hxx>
32
#include <comphelper/storagehelper.hxx>
33
#include <cppuhelper/supportsservice.hxx>
34
#include <cppuhelper/weak.hxx>
35
#include <osl/diagnose.h>
36
#include <unotools/tempfile.hxx>
37
38
#include "xfactory.hxx"
39
#include "xstorage.hxx"
40
41
using namespace ::com::sun::star;
42
43
static bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream,
44
                                     const uno::Reference< io::XSeekable >& xSeekable )
45
51.0k
{
46
51.0k
    if ( !xInputStream.is() || !xSeekable.is() )
47
0
        throw uno::RuntimeException();
48
49
51.0k
    if ( xSeekable->getLength() )
50
51.0k
    {
51
51.0k
        uno::Sequence< sal_Int8 > aData( 4 );
52
51.0k
        xSeekable->seek( 0 );
53
51.0k
        sal_Int32 nRead = xInputStream->readBytes( aData, 4 );
54
51.0k
        xSeekable->seek( 0 );
55
56
        // TODO/LATER: should the disk spanned files be supported?
57
        // 0x50, 0x4b, 0x07, 0x08
58
51.0k
        return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 );
59
51.0k
    }
60
20
    else
61
20
        return true; // allow to create a storage based on empty stream
62
51.0k
}
63
64
65
66
uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance()
67
75.9k
{
68
    // TODO: reimplement TempStream service to support XStream interface
69
75.9k
    uno::Reference < io::XStream > xTempStream(new utl::TempFileFastService);
70
71
75.9k
    return cppu::getXWeak(new OStorage(xTempStream, embed::ElementModes::READWRITE,
72
75.9k
                                                  uno::Sequence<beans::PropertyValue>(), m_xContext,
73
75.9k
                                                  embed::StorageFormats::PACKAGE));
74
75.9k
}
75
76
uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments(
77
            const uno::Sequence< uno::Any >& aArguments )
78
51.0k
{
79
    // The request for storage can be done with up to three arguments
80
81
    // The first argument specifies a source for the storage
82
    // it can be URL, XStream, XInputStream.
83
    // The second value is a mode the storage should be open in.
84
    // And the third value is a media descriptor.
85
86
51.0k
    sal_Int32 nArgNum = aArguments.getLength();
87
51.0k
    OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
88
89
51.0k
    if ( !nArgNum )
90
0
        return createInstance();
91
92
    // first try to retrieve storage open mode if any
93
    // by default the storage will be open in readonly mode
94
51.0k
    sal_Int32 nStorageMode = embed::ElementModes::READ;
95
51.0k
    if ( nArgNum >= 2 )
96
51.0k
    {
97
51.0k
        if( !( aArguments[1] >>= nStorageMode ) )
98
0
        {
99
0
            OSL_FAIL( "Wrong second argument!" );
100
0
            throw lang::IllegalArgumentException(); // TODO:
101
0
        }
102
        // it's always possible to read written storage in this implementation
103
51.0k
        nStorageMode |= embed::ElementModes::READ;
104
51.0k
    }
105
106
51.0k
    if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE
107
0
      && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE )
108
0
        throw lang::IllegalArgumentException(); // TODO:
109
110
    // retrieve storage source stream
111
51.0k
    OUString aURL;
112
51.0k
    uno::Reference< io::XStream > xStream;
113
51.0k
    uno::Reference< io::XInputStream > xInputStream;
114
115
51.0k
    if ( aArguments[0] >>= aURL )
116
0
    {
117
0
        if ( aURL.isEmpty() )
118
0
        {
119
0
            OSL_FAIL( "Empty URL is provided!" );
120
0
            throw lang::IllegalArgumentException(); // TODO:
121
0
        }
122
123
0
        if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:") )
124
0
        {
125
0
            OSL_FAIL( "Packages URL's are not valid for storages!" ); // ???
126
0
            throw lang::IllegalArgumentException(); // TODO:
127
0
        }
128
129
0
        uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(
130
0
            ucb::SimpleFileAccess::create(
131
0
                m_xContext ) );
132
133
0
        if ( nStorageMode & embed::ElementModes::WRITE )
134
0
            xStream = xTempAccess->openFileReadWrite( aURL );
135
0
        else
136
0
            xInputStream = xTempAccess->openFileRead( aURL );
137
0
    }
138
51.0k
    else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) )
139
0
    {
140
0
        OSL_FAIL( "Wrong first argument!" );
141
0
        throw uno::Exception(u"wrong first arg"_ustr, nullptr); // TODO: Illegal argument
142
0
    }
143
144
    // retrieve mediadescriptor and set storage properties
145
51.0k
    uno::Sequence< beans::PropertyValue > aDescr;
146
51.0k
    uno::Sequence< beans::PropertyValue > aPropsToSet;
147
148
51.0k
    sal_Int32 nStorageType = embed::StorageFormats::PACKAGE;
149
150
51.0k
    if ( nArgNum >= 3 )
151
51.0k
    {
152
51.0k
        if( aArguments[2] >>= aDescr )
153
51.0k
        {
154
51.0k
            if ( !aURL.isEmpty() )
155
0
            {
156
0
                aPropsToSet = { comphelper::makePropertyValue(u"URL"_ustr, aURL) };
157
0
            }
158
159
51.0k
            sal_Int32 nNumArgs = 1;
160
51.0k
            for (const auto& rProp : aDescr)
161
51.0k
            {
162
51.0k
                if ( rProp.Name == "InteractionHandler"
163
51.0k
                  || rProp.Name == "Password"
164
51.0k
                  || rProp.Name == "RepairPackage"
165
51.0k
                  || rProp.Name == "StatusIndicator" )
166
0
                {
167
0
                    aPropsToSet.realloc( ++nNumArgs );
168
0
                    auto pPropsToSet = aPropsToSet.getArray();
169
0
                    pPropsToSet[nNumArgs-1].Name = rProp.Name;
170
0
                    pPropsToSet[nNumArgs-1].Value = rProp.Value;
171
0
                }
172
51.0k
                else if ( rProp.Name == "StorageFormat" )
173
51.0k
                {
174
51.0k
                    OUString aFormatName;
175
51.0k
                    sal_Int32 nFormatID = 0;
176
51.0k
                    if ( rProp.Value >>= aFormatName )
177
51.0k
                    {
178
51.0k
                        if ( aFormatName == PACKAGE_STORAGE_FORMAT_STRING )
179
0
                            nStorageType = embed::StorageFormats::PACKAGE;
180
51.0k
                        else if ( aFormatName == ZIP_STORAGE_FORMAT_STRING )
181
34.7k
                            nStorageType = embed::StorageFormats::ZIP;
182
16.2k
                        else if ( aFormatName == OFOPXML_STORAGE_FORMAT_STRING )
183
16.2k
                            nStorageType = embed::StorageFormats::OFOPXML;
184
0
                        else
185
0
                            throw lang::IllegalArgumentException( u""_ustr, uno::Reference< uno::XInterface >(), 1 );
186
51.0k
                    }
187
0
                    else if ( rProp.Value >>= nFormatID )
188
0
                    {
189
0
                        if ( nFormatID != embed::StorageFormats::PACKAGE
190
0
                          && nFormatID != embed::StorageFormats::ZIP
191
0
                          && nFormatID != embed::StorageFormats::OFOPXML )
192
0
                            throw lang::IllegalArgumentException( u""_ustr, uno::Reference< uno::XInterface >(), 1 );
193
194
0
                        nStorageType = nFormatID;
195
0
                    }
196
0
                    else
197
0
                        throw lang::IllegalArgumentException( u""_ustr, uno::Reference< uno::XInterface >(), 1 );
198
51.0k
                }
199
0
                else if (rProp.Name == "NoFileSync")
200
0
                {
201
                    // Forward NoFileSync to the storage.
202
0
                    aPropsToSet.realloc(++nNumArgs);
203
0
                    auto pPropsToSet = aPropsToSet.getArray();
204
0
                    pPropsToSet[nNumArgs - 1].Name = rProp.Name;
205
0
                    pPropsToSet[nNumArgs - 1].Value = rProp.Value;
206
0
                }
207
0
                else
208
0
                    OSL_FAIL( "Unacceptable property, will be ignored!" );
209
51.0k
            }
210
51.0k
        }
211
0
        else
212
0
        {
213
0
            OSL_FAIL( "Wrong third argument!" );
214
0
            throw uno::Exception(u"wrong 3rd arg"_ustr, nullptr); // TODO: Illegal argument
215
0
        }
216
217
51.0k
    }
218
219
    // create storage based on source
220
51.0k
    if ( xInputStream.is() )
221
50.9k
    {
222
        // if xInputStream is set the storage should be open from it
223
50.9k
        if ( nStorageMode & embed::ElementModes::WRITE )
224
0
              throw uno::Exception(u"storagemode==write"_ustr, nullptr); // TODO: access denied
225
226
50.9k
        uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY );
227
50.9k
        if ( !xSeekable.is() )
228
0
        {
229
            // TODO: wrap stream to let it be seekable
230
0
            OSL_FAIL( "Nonseekable streams are not supported for now!" );
231
0
        }
232
233
50.9k
        if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) )
234
11.1k
            throw io::IOException(u"package signature check failed, probably not a package file"_ustr, nullptr); // TODO: this is not a package file
235
236
39.8k
        return cppu::getXWeak(
237
39.8k
            new OStorage(xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType));
238
50.9k
    }
239
57
    else if ( xStream.is() )
240
57
    {
241
57
        if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() )
242
57
             || !xStream->getInputStream().is() )
243
0
              throw uno::Exception(u"access denied"_ustr, nullptr); // TODO: access denied
244
245
57
        uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
246
57
        if ( !xSeekable.is() )
247
0
        {
248
            // TODO: wrap stream to let it be seekable
249
0
            OSL_FAIL( "Nonseekable streams are not supported for now!" );
250
0
        }
251
252
57
        if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) )
253
7
            throw io::IOException(); // TODO: this is not a package file
254
255
50
        return cppu::getXWeak(
256
50
            new OStorage(xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType));
257
57
    }
258
259
0
    throw uno::Exception(u"no input stream or regular stream"_ustr, nullptr); // general error during creation
260
51.0k
}
261
262
OUString SAL_CALL OStorageFactory::getImplementationName()
263
0
{
264
0
    return u"com.sun.star.comp.embed.StorageFactory"_ustr;
265
0
}
266
267
sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName )
268
0
{
269
0
    return cppu::supportsService(this, ServiceName);
270
0
}
271
272
uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames()
273
0
{
274
0
    return  { u"com.sun.star.embed.StorageFactory"_ustr,
275
0
                u"com.sun.star.comp.embed.StorageFactory"_ustr };
276
0
}
277
278
279
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
280
package_OStorageFactory_get_implementation(
281
    css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
282
13
{
283
13
    return cppu::acquire(new OStorageFactory(context));
284
13
}
285
286
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */