1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "exceldetect.hxx"
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/ucb/XContent.hpp>
#include "svl/itemset.hxx"
#include "svl/eitem.hxx"
#include "sfx2/app.hxx"
#include "sfx2/docfile.hxx"
#include "sfx2/sfxsids.hrc"
#include "unotools/mediadescriptor.hxx"
#include "sot/storage.hxx"
using namespace com::sun::star;
using utl::MediaDescriptor;
ScExcelBiffDetect::ScExcelBiffDetect( const uno::Reference<uno::XComponentContext>& /*xContext*/ ) {}
ScExcelBiffDetect::~ScExcelBiffDetect() {}
OUString ScExcelBiffDetect::getImplementationName() throw (uno::RuntimeException)
{
return impl_getStaticImplementationName();
}
sal_Bool ScExcelBiffDetect::supportsService( const OUString& aName ) throw (uno::RuntimeException)
{
uno::Sequence<OUString> aSrvNames = getSupportedServiceNames();
const OUString* pArray = aSrvNames.getConstArray();
for (sal_Int32 i = 0; i < aSrvNames.getLength(); ++i, ++pArray)
{
if (*pArray == aName)
return true;
}
return false;
}
uno::Sequence<OUString> ScExcelBiffDetect::getSupportedServiceNames() throw (uno::RuntimeException)
{
return impl_getStaticSupportedServiceNames();
}
namespace {
bool hasStream(const uno::Reference<io::XInputStream>& xInStream, const OUString& rName)
{
SfxMedium aMedium;
aMedium.UseInteractionHandler(false);
aMedium.setStreamToLoadFrom(xInStream, true);
SvStream* pStream = aMedium.GetInStream();
if (!pStream)
return false;
pStream->Seek(STREAM_SEEK_TO_END);
sal_Size nSize = pStream->Tell();
pStream->Seek(0);
if (!nSize)
// 0-size stream. Failed.
return false;
SotStorageRef xStorage = new SotStorage(pStream, false);
if (!xStorage.Is() || xStorage->GetError())
return false;
return xStorage->IsStream(rName);
}
/**
* We detect BIFF 2, 3 and 4 file types together since the only thing that
* set them apart is the BOF ID.
*/
bool isExcel40(const uno::Reference<io::XInputStream>& xInStream)
{
SfxMedium aMedium;
aMedium.UseInteractionHandler(false);
aMedium.setStreamToLoadFrom(xInStream, true);
SvStream* pStream = aMedium.GetInStream();
if (!pStream)
return false;
pStream->Seek(STREAM_SEEK_TO_END);
sal_Size nSize = pStream->Tell();
pStream->Seek(0);
if (nSize < 4)
return false;
sal_uInt16 nBofId, nBofSize;
*pStream >> nBofId >> nBofSize;
switch (nBofId)
{
case 0x0009: // Excel 2.1 worksheet (BIFF 2)
case 0x0209: // Excel 3.0 worksheet (BIFF 3)
case 0x0409: // Excel 4.0 worksheet (BIFF 4)
case 0x0809: // Excel 5.0 worksheet (BIFF 5), some apps create such files (fdo#70100)
break;
default:
return false;
}
if (nBofSize < 4 || 16 < nBofSize)
// BOF record must be sized between 4 and 16 for BIFF 2, 3 and 4.
return false;
sal_Size nPos = pStream->Tell();
if (nSize - nPos < nBofSize)
// BOF record doesn't have required bytes.
return false;
return true;
}
bool isTemplate(const OUString& rType)
{
return rType.indexOf("_VorlageTemplate") != -1;
}
}
OUString ScExcelBiffDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
throw (uno::RuntimeException)
{
MediaDescriptor aMediaDesc(lDescriptor);
OUString aType;
aMediaDesc[MediaDescriptor::PROP_TYPENAME()] >>= aType;
if (aType.isEmpty())
// Type is not given. We can't proceed.
return OUString();
aMediaDesc.addInputStream();
uno::Reference<io::XInputStream> xInStream(aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY);
if (!xInStream.is())
// No input stream.
return OUString();
if (aType == "calc_MS_Excel_97" || aType == "calc_MS_Excel_97_VorlageTemplate")
{
// See if this stream is a Excel 97/XP/2003 (BIFF8) stream.
if (!hasStream(xInStream, "Workbook"))
// BIFF8 is expected to contain a stream named "Workbook".
return OUString();
aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 97 Vorlage/Template") : OUString("MS Excel 97");
return aType;
}
if (aType == "calc_MS_Excel_95" || aType == "calc_MS_Excel_95_VorlageTemplate")
{
// See if this stream is a Excel 95 (BIFF5) stream.
if (!hasStream(xInStream, "Book"))
return OUString();
aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 95 Vorlage/Template") : OUString("MS Excel 95");
return aType;
}
if (aType == "calc_MS_Excel_5095" || aType == "calc_MS_Excel_5095_VorlageTemplate")
{
// See if this stream is a Excel 5.0/95 stream.
if (!hasStream(xInStream, "Book"))
return OUString();
aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 5.0/95 Vorlage/Template") : OUString("MS Excel 5.0/95");
return aType;
}
if (aType == "calc_MS_Excel_40" || aType == "calc_MS_Excel_40_VorlageTemplate")
{
// See if this stream is a Excel 4.0 stream.
if (!isExcel40(xInStream))
return OUString();
aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= isTemplate(aType) ? OUString("MS Excel 4.0 Vorlage/Template") : OUString("MS Excel 4.0");
return aType;
}
// failed!
return OUString();
}
uno::Sequence<OUString> ScExcelBiffDetect::impl_getStaticSupportedServiceNames()
{
uno::Sequence<OUString> aNames(1);
aNames[0] = "com.sun.star.frame.ExtendedTypeDetection";
return aNames;
}
OUString ScExcelBiffDetect::impl_getStaticImplementationName()
{
return OUString("com.sun.star.comp.calc.ExcelBiffFormatDetector");
}
uno::Reference<uno::XInterface> ScExcelBiffDetect::impl_createInstance(
const uno::Reference<uno::XComponentContext>& xContext )
throw (com::sun::star::uno::Exception)
{
return static_cast<cppu::OWeakObject*>(new ScExcelBiffDetect(xContext));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|