LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - exceldetect.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 57 83 68.7 %
Date: 2015-06-13 12:38:46 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11