LibreOffice Module sc (master) 1
exceldetect.cxx
Go to the documentation of this file.
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
10#include "exceldetect.hxx"
11
12#include <com/sun/star/io/XInputStream.hpp>
13#include <com/sun/star/ucb/ContentCreationException.hpp>
14#include <com/sun/star/uno/XComponentContext.hpp>
16
17#include <sfx2/docfile.hxx>
19#include <sot/storage.hxx>
20#include <tools/diagnose_ex.h>
21
22using namespace com::sun::star;
24
27
29{
30 return "com.sun.star.comp.calc.ExcelBiffFormatDetector";
31}
32
34{
35 return cppu::supportsService(this, aName);
36}
37
39{
40 return { "com.sun.star.frame.ExtendedTypeDetection" };
41}
42
43namespace {
44
45bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
46{
47 SfxMedium aMedium;
48 aMedium.UseInteractionHandler(false);
49 aMedium.setStreamToLoadFrom(xInStream, true);
50 SvStream* pStream = aMedium.GetInStream();
51 if (!pStream)
52 return false;
53
54 sal_uInt64 const nSize = pStream->TellEnd();
55 pStream->Seek(0);
56
57 if (!nSize)
58 {
59 // 0-size stream. Failed.
60 return false;
61 }
62
63 try
64 {
65 tools::SvRef<SotStorage> xStorage = new SotStorage(pStream, false);
66 if (!xStorage.is() || xStorage->GetError())
67 return false;
68 return xStorage->IsStream(rName);
69 }
70 catch (const css::ucb::ContentCreationException &)
71 {
72 TOOLS_WARN_EXCEPTION("sc", "hasStream");
73 }
74
75 return false;
76}
77
82bool isExcel40(const uno::Reference<io::XInputStream>& xInStream)
83{
84 SfxMedium aMedium;
85 aMedium.UseInteractionHandler(false);
86 aMedium.setStreamToLoadFrom(xInStream, true);
87 SvStream* pStream = aMedium.GetInStream();
88 if (!pStream)
89 return false;
90
91 sal_uInt64 const nSize = pStream->TellEnd();
92 pStream->Seek(0);
93
94 if (nSize < 4)
95 return false;
96
97 sal_uInt16 nBofId, nBofSize;
98 pStream->ReadUInt16( nBofId ).ReadUInt16( nBofSize );
99
100 switch (nBofId)
101 {
102 case 0x0009: // Excel 2.1 worksheet (BIFF 2)
103 case 0x0209: // Excel 3.0 worksheet (BIFF 3)
104 case 0x0409: // Excel 4.0 worksheet (BIFF 4)
105 case 0x0809: // Excel 5.0 worksheet (BIFF 5), some apps create such files (fdo#70100)
106 break;
107 default:
108 return false;
109 }
110
111 if (nBofSize < 4 || 16 < nBofSize)
112 // BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
113 return false;
114
115 sal_uInt64 const nPos = pStream->Tell();
116 if (nSize - nPos < nBofSize)
117 // BOF record doesn't have required bytes.
118 return false;
119
120 return true;
121}
122
123bool isTemplate(std::u16string_view rType)
124{
125 return rType.find(u"_VorlageTemplate") != std::u16string_view::npos;
126}
127
128}
129
130OUString ScExcelBiffDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
131{
132 MediaDescriptor aMediaDesc(lDescriptor);
133 OUString aType;
134 aMediaDesc[MediaDescriptor::PROP_TYPENAME] >>= aType;
135 if (aType.isEmpty())
136 // Type is not given. We can't proceed.
137 return OUString();
138
139 aMediaDesc.addInputStream();
140 uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM], uno::UNO_QUERY);
141 if (!xInStream.is())
142 // No input stream.
143 return OUString();
144
145 if (aType == "calc_MS_Excel_97" || aType == "calc_MS_Excel_97_VorlageTemplate")
146 {
147 // See if this stream is an Excel 97/XP/2003 (BIFF8) stream.
148 if (!hasStream(xInStream, "Workbook"))
149 // BIFF8 is expected to contain a stream named "Workbook".
150 return OUString();
151
152 aMediaDesc[MediaDescriptor::PROP_FILTERNAME] <<= isTemplate(aType) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
153 }
154
155 else if (aType == "calc_MS_Excel_95" || aType == "calc_MS_Excel_95_VorlageTemplate")
156 {
157 // See if this stream is an Excel 95 (BIFF5) stream.
158 if (!hasStream(xInStream, "Book"))
159 return OUString();
160
161 aMediaDesc[MediaDescriptor::PROP_FILTERNAME] <<= isTemplate(aType) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
162 }
163
164 else if (aType == "calc_MS_Excel_5095" || aType == "calc_MS_Excel_5095_VorlageTemplate")
165 {
166 // See if this stream is an Excel 5.0/95 stream.
167 if (!hasStream(xInStream, "Book"))
168 return OUString();
169
170 aMediaDesc[MediaDescriptor::PROP_FILTERNAME] <<= isTemplate(aType) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
171 }
172
173 else if (aType == "calc_MS_Excel_40" || aType == "calc_MS_Excel_40_VorlageTemplate")
174 {
175 // See if this stream is an Excel 4.0 stream.
176 if (!isExcel40(xInStream))
177 return OUString();
178
179 aMediaDesc[MediaDescriptor::PROP_FILTERNAME] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
180 }
181
182 else
183 // Nothing to detect.
184 return OUString();
185
186 aMediaDesc >> lDescriptor;
187 return aType;
188}
189
190extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
192 css::uno::Sequence<css::uno::Any> const &)
193{
194 return cppu::acquire(new ScExcelBiffDetect);
195}
196
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &lDescriptor) override
virtual sal_Bool SAL_CALL supportsService(const OUString &aName) override
Definition: exceldetect.cxx:33
virtual OUString SAL_CALL getImplementationName() override
Definition: exceldetect.cxx:28
virtual ~ScExcelBiffDetect() override
Definition: exceldetect.cxx:26
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: exceldetect.cxx:38
void setStreamToLoadFrom(const css::uno::Reference< css::io::XInputStream > &xInputStream, bool bIsReadOnly)
void UseInteractionHandler(bool)
SvStream * GetInStream()
sal_uInt64 Tell() const
virtual sal_uInt64 TellEnd()
sal_uInt64 Seek(sal_uInt64 nPos)
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
bool is() const
#define TOOLS_WARN_EXCEPTION(area, stream)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_calc_ExcelBiffFormatDetector_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
OUString aName
sal_uInt16 nPos
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool