summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/unoobj/exceldetect.cxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /sc/source/ui/unoobj/exceldetect.cxx
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sc/source/ui/unoobj/exceldetect.cxx')
-rw-r--r--sc/source/ui/unoobj/exceldetect.cxx198
1 files changed, 198 insertions, 0 deletions
diff --git a/sc/source/ui/unoobj/exceldetect.cxx b/sc/source/ui/unoobj/exceldetect.cxx
new file mode 100644
index 000000000..75694bbc9
--- /dev/null
+++ b/sc/source/ui/unoobj/exceldetect.cxx
@@ -0,0 +1,198 @@
+/* -*- 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/ContentCreationException.hpp>
+#include <com/sun/star/uno/XComponentContext.hpp>
+#include <cppuhelper/supportsservice.hxx>
+
+#include <sfx2/docfile.hxx>
+#include <unotools/mediadescriptor.hxx>
+#include <sot/storage.hxx>
+#include <tools/diagnose_ex.h>
+
+using namespace com::sun::star;
+using utl::MediaDescriptor;
+
+ScExcelBiffDetect::ScExcelBiffDetect() {}
+ScExcelBiffDetect::~ScExcelBiffDetect() {}
+
+OUString ScExcelBiffDetect::getImplementationName()
+{
+ return "com.sun.star.comp.calc.ExcelBiffFormatDetector";
+}
+
+sal_Bool ScExcelBiffDetect::supportsService( const OUString& aName )
+{
+ return cppu::supportsService(this, aName);
+}
+
+uno::Sequence<OUString> ScExcelBiffDetect::getSupportedServiceNames()
+{
+ return { "com.sun.star.frame.ExtendedTypeDetection" };
+}
+
+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;
+
+ sal_uInt64 const nSize = pStream->TellEnd();
+ pStream->Seek(0);
+
+ if (!nSize)
+ {
+ // 0-size stream. Failed.
+ return false;
+ }
+
+ try
+ {
+ tools::SvRef<SotStorage> xStorage = new SotStorage(pStream, false);
+ if (!xStorage.is() || xStorage->GetError())
+ return false;
+ return xStorage->IsStream(rName);
+ }
+ catch (const css::ucb::ContentCreationException &)
+ {
+ TOOLS_WARN_EXCEPTION("sc", "hasStream");
+ }
+
+ return false;
+}
+
+/**
+ * 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;
+
+ sal_uInt64 const nSize = pStream->TellEnd();
+ pStream->Seek(0);
+
+ if (nSize < 4)
+ return false;
+
+ sal_uInt16 nBofId, nBofSize;
+ pStream->ReadUInt16( nBofId ).ReadUInt16( 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_uInt64 const nPos = pStream->Tell();
+ if (nSize - nPos < nBofSize)
+ // BOF record doesn't have required bytes.
+ return false;
+
+ return true;
+}
+
+bool isTemplate(std::u16string_view rType)
+{
+ return rType.find(u"_VorlageTemplate") != std::u16string_view::npos;
+}
+
+}
+
+OUString ScExcelBiffDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
+{
+ 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 an 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");
+ }
+
+ else if (aType == "calc_MS_Excel_95" || aType == "calc_MS_Excel_95_VorlageTemplate")
+ {
+ // See if this stream is an 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");
+ }
+
+ else if (aType == "calc_MS_Excel_5095" || aType == "calc_MS_Excel_5095_VorlageTemplate")
+ {
+ // See if this stream is an 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");
+ }
+
+ else if (aType == "calc_MS_Excel_40" || aType == "calc_MS_Excel_40_VorlageTemplate")
+ {
+ // See if this stream is an 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");
+ }
+
+ else
+ // Nothing to detect.
+ return OUString();
+
+ aMediaDesc >> lDescriptor;
+ return aType;
+}
+
+extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
+com_sun_star_comp_calc_ExcelBiffFormatDetector_get_implementation(css::uno::XComponentContext* /*context*/,
+ css::uno::Sequence<css::uno::Any> const &)
+{
+ return cppu::acquire(new ScExcelBiffDetect);
+}
+
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */