LCOV - code coverage report
Current view: top level - sc/source/ui/unoobj - scdetect.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 54 104 51.9 %
Date: 2015-06-13 12:38:46 Functions: 7 10 70.0 %
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             :  * 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 "scdetect.hxx"
      21             : 
      22             : #include <sal/macros.h>
      23             : 
      24             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <cppuhelper/supportsservice.hxx>
      27             : #include <com/sun/star/container/XNameAccess.hpp>
      28             : #include <com/sun/star/io/XInputStream.hpp>
      29             : #include <unotools/mediadescriptor.hxx>
      30             : #include <sfx2/docfile.hxx>
      31             : #include <sfx2/fcontnr.hxx>
      32             : 
      33             : using namespace ::com::sun::star;
      34             : using utl::MediaDescriptor;
      35             : 
      36             : namespace {
      37             : 
      38             : const sal_Char pFilterLotus[]        = "Lotus";
      39             : const sal_Char pFilterQPro6[]        = "Quattro Pro 6.0";
      40             : const sal_Char pFilterDBase[]        = "dBase";
      41             : const sal_Char pFilterDif[]      = "DIF";
      42             : const sal_Char pFilterSylk[]     = "SYLK";
      43             : 
      44             : // Tabelle mit Suchmustern
      45             : // Bedeutung der Sequenzen
      46             : // 0x00??: genau Byte 0x?? muss an dieser Stelle stehen
      47             : // 0x0100: ein Byte ueberlesen (don't care)
      48             : // 0x02nn: ein Byte aus 0xnn Alternativen folgt
      49             : // 0x8000: Erkennung abgeschlossen
      50             : 
      51             : #define M_DC        0x0100
      52             : #define M_ALT(ANZ)  (0x0200+(ANZ))
      53             : #define M_ENDE      0x8000
      54             : 
      55             : const sal_uInt16 pLotus[] =      // Lotus 1/1A/2
      56             :     { 0x0000, 0x0000, 0x0002, 0x0000,
      57             :     M_ALT(2), 0x0004, 0x0006,
      58             :     0x0004, M_ENDE };
      59             : 
      60             : const sal_uInt16 pLotusNew[] =   // Lotus >= 9.7
      61             :     { 0x0000, 0x0000, M_DC, 0x0000,     // Rec# + Len (0x1a)
      62             :       M_ALT(3), 0x0003, 0x0004, 0x0005, // File Revision Code 97->ME
      63             :       0x0010, 0x0004, 0x0000, 0x0000,
      64             :       M_ENDE };
      65             : 
      66             : const sal_uInt16 pLotus2[] =     // Lotus >3
      67             :     { 0x0000, 0x0000, 0x001A, 0x0000,   // Rec# + Len (26)
      68             :     M_ALT(2), 0x0000, 0x0002,         // File Revision Code
      69             :     0x0010,
      70             :     0x0004, 0x0000,                   // File Revision Subcode
      71             :     M_ENDE };
      72             : 
      73             : const sal_uInt16 pQPro[] =
      74             :        { 0x0000, 0x0000, 0x0002, 0x0000,
      75             :          M_ALT(4), 0x0001, 0x0002, // WB1, WB2
      76             :          0x0006, 0x0007,           // QPro 6/7 (?)
      77             :          0x0010,
      78             :          M_ENDE };
      79             : 
      80             : const sal_uInt16 pDIF1[] =       // DIF mit CR-LF
      81             :     {
      82             :     'T', 'A', 'B', 'L', 'E',
      83             :     M_DC, M_DC,
      84             :     '0', ',', '1',
      85             :     M_DC, M_DC,
      86             :     '\"',
      87             :     M_ENDE };
      88             : 
      89             : const sal_uInt16 pDIF2[] =       // DIF mit CR oder LF
      90             :     {
      91             :     'T', 'A', 'B', 'L', 'E',
      92             :     M_DC,
      93             :     '0', ',', '1',
      94             :     M_DC,
      95             :     '\"',
      96             :     M_ENDE };
      97             : 
      98             : const sal_uInt16 pSylk[] =       // Sylk
      99             :     {
     100             :     'I', 'D', ';',
     101             :     M_ALT(3), 'P', 'N', 'E',        // 'P' plus undocumented Excel extensions 'N' and 'E'
     102             :     M_ENDE };
     103             : 
     104         595 : bool detectThisFormat(SvStream& rStr, const sal_uInt16* pSearch)
     105             : {
     106             :     sal_uInt8 nByte;
     107         595 :     rStr.Seek( 0 ); // am Anfang war alles Uebel...
     108         595 :     rStr.ReadUChar( nByte );
     109         595 :     bool bSync = true;
     110        1785 :     while( !rStr.IsEof() && bSync )
     111             :     {
     112         595 :         sal_uInt16 nMuster = *pSearch;
     113             : 
     114         595 :         if( nMuster < 0x0100 )
     115             :         { //                                direkter Byte-Vergleich
     116         595 :             if( ( sal_uInt8 ) nMuster != nByte )
     117         595 :                 bSync = false;
     118             :         }
     119           0 :         else if( nMuster & M_DC )
     120             :         { //                                             don't care
     121             :         }
     122           0 :         else if( nMuster & M_ALT(0) )
     123             :         { //                                      alternative Bytes
     124           0 :             sal_uInt8 nAnzAlt = ( sal_uInt8 ) nMuster;
     125           0 :             bSync = false;          // zunaechst unsynchron
     126           0 :             while( nAnzAlt > 0 )
     127             :             {
     128           0 :                 pSearch++;
     129           0 :                 if( ( sal_uInt8 ) *pSearch == nByte )
     130           0 :                     bSync = true;   // jetzt erst Synchronisierung
     131           0 :                 nAnzAlt--;
     132             :             }
     133             :         }
     134           0 :         else if( nMuster & M_ENDE )
     135             :         { //                                        Format detected
     136           0 :             return true;
     137             :         }
     138             : 
     139         595 :         pSearch++;
     140         595 :         rStr.ReadUChar( nByte );
     141             :     }
     142             : 
     143         595 :     return false;
     144             : }
     145             : 
     146             : }
     147             : 
     148         425 : ScFilterDetect::ScFilterDetect( const uno::Reference<uno::XComponentContext>& /*xContext*/ )
     149             : {
     150         425 : }
     151             : 
     152         850 : ScFilterDetect::~ScFilterDetect()
     153             : {
     154         850 : }
     155             : 
     156             : #if 0
     157             : // This method is no longer used, but I do want to keep this for now to see
     158             : // if we could transfer this check to the now centralized ascii detection
     159             : // code in the filter module.
     160             : static sal_Bool lcl_MayBeAscii( SvStream& rStream )
     161             : {
     162             :     // ASCII/CSV is considered possible if there are no null bytes, or a Byte
     163             :     // Order Mark is present, or if, for Unicode UCS2/UTF-16, all null bytes
     164             :     // are on either even or uneven byte positions.
     165             : 
     166             :     rStream.Seek(STREAM_SEEK_TO_BEGIN);
     167             : 
     168             :     const size_t nBufSize = 2048;
     169             :     sal_uInt16 aBuffer[ nBufSize ];
     170             :     sal_uInt8* pByte = reinterpret_cast<sal_uInt8*>(aBuffer);
     171             :     sal_uLong nBytesRead = rStream.Read( pByte, nBufSize*2);
     172             : 
     173             :     if ( nBytesRead >= 2 && (aBuffer[0] == 0xfffe || aBuffer[0] == 0xfeff) )
     174             :     {
     175             :         // Unicode BOM file may contain null bytes.
     176             :         return sal_True;
     177             :     }
     178             : 
     179             :     const sal_uInt16* p = aBuffer;
     180             :     sal_uInt16 nMask = 0xffff;
     181             :     nBytesRead /= 2;
     182             :     while( nBytesRead-- && nMask )
     183             :     {
     184             :         sal_uInt16 nVal = *p++ & nMask;
     185             :         if (!(nVal & 0x00ff))
     186             :             nMask &= 0xff00;
     187             :         if (!(nVal & 0xff00))
     188             :             nMask &= 0x00ff;
     189             :     }
     190             : 
     191             :     return nMask != 0;
     192             : }
     193             : #endif
     194             : 
     195          85 : static bool lcl_MayBeDBase( SvStream& rStream )
     196             : {
     197             :     // Look for dbf marker, see connectivity/source/inc/dbase/DTable.hxx
     198             :     // DBFType for values.
     199             :     const sal_uInt8 nValidMarks[] = {
     200          85 :         0x03, 0x04, 0x05, 0x30, 0x43, 0xB3, 0x83, 0x8b, 0x8e, 0xf5 };
     201             :     sal_uInt8 nMark;
     202          85 :     rStream.Seek(STREAM_SEEK_TO_BEGIN);
     203          85 :     rStream.ReadUChar( nMark );
     204          85 :     bool bValidMark = false;
     205         935 :     for (size_t i=0; i < sizeof(nValidMarks)/sizeof(nValidMarks[0]) && !bValidMark; ++i)
     206             :     {
     207         850 :         if (nValidMarks[i] == nMark)
     208           0 :             bValidMark = true;
     209             :     }
     210          85 :     if ( !bValidMark )
     211          85 :         return false;
     212             : 
     213           0 :     const size_t nHeaderBlockSize = 32;
     214             :     // Empty dbf is >= 32*2+1 bytes in size.
     215           0 :     const size_t nEmptyDbf = nHeaderBlockSize * 2 + 1;
     216             : 
     217           0 :     rStream.Seek(STREAM_SEEK_TO_END);
     218           0 :     sal_uLong nSize = rStream.Tell();
     219           0 :     if ( nSize < nEmptyDbf )
     220           0 :         return false;
     221             : 
     222             :     // length of header starts at 8
     223           0 :     rStream.Seek(8);
     224             :     sal_uInt16 nHeaderLen;
     225           0 :     rStream.ReadUInt16( nHeaderLen );
     226             : 
     227           0 :     if ( nHeaderLen < nEmptyDbf || nSize < nHeaderLen )
     228           0 :         return false;
     229             : 
     230             :     // Last byte of header must be 0x0d, this is how it's specified.
     231             :     // #i9581#,#i26407# but some applications don't follow the specification
     232             :     // and pad the header with one byte 0x00 to reach an
     233             :     // even boundary. Some (#i88577# ) even pad more or pad using a 0x1a ^Z
     234             :     // control character (#i8857#). This results in:
     235             :     // Last byte of header must be 0x0d on 32 bytes boundary.
     236           0 :     sal_uInt16 nBlocks = (nHeaderLen - 1) / nHeaderBlockSize;
     237           0 :     sal_uInt8 nEndFlag = 0;
     238           0 :     while ( nBlocks > 1 && nEndFlag != 0x0d ) {
     239           0 :         rStream.Seek( nBlocks-- * nHeaderBlockSize );
     240           0 :         rStream.ReadUChar( nEndFlag );
     241             :     }
     242             : 
     243           0 :     return ( 0x0d == nEndFlag );
     244             : }
     245             : 
     246         425 : OUString SAL_CALL ScFilterDetect::detect( uno::Sequence<beans::PropertyValue>& lDescriptor )
     247             :     throw( uno::RuntimeException, std::exception )
     248             : {
     249         425 :     MediaDescriptor aMediaDesc( lDescriptor );
     250         850 :     OUString aTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
     251         850 :     uno::Reference< io::XInputStream > xStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
     252         425 :     if ( !xStream.is() )
     253           0 :         return OUString();
     254             : 
     255         850 :     SfxMedium aMedium;
     256         425 :     aMedium.UseInteractionHandler( false );
     257         425 :     aMedium.setStreamToLoadFrom( xStream, true );
     258             : 
     259         425 :     SvStream* pStream = aMedium.GetInStream();
     260         425 :     if ( !pStream || pStream->GetError() )
     261             :         // No stream, no detection.
     262           0 :         return OUString();
     263             : 
     264         425 :     const char* pSearchFilterName = NULL;
     265         425 :     if (aTypeName == "calc_Lotus")
     266             :     {
     267          85 :         if (!detectThisFormat(*pStream, pLotus) && !detectThisFormat(*pStream, pLotusNew) && !detectThisFormat(*pStream, pLotus2))
     268          85 :             return OUString();
     269             : 
     270           0 :         pSearchFilterName = pFilterLotus;
     271             :     }
     272         340 :     else if (aTypeName == "calc_QPro")
     273             :     {
     274          85 :         if (!detectThisFormat(*pStream, pQPro))
     275          85 :             return OUString();
     276             : 
     277           0 :         pSearchFilterName = pFilterQPro6;
     278             :     }
     279         255 :     else if (aTypeName == "calc_SYLK")
     280             :     {
     281          85 :         if (!detectThisFormat(*pStream, pSylk))
     282          85 :             return OUString();
     283             : 
     284           0 :         pSearchFilterName = pFilterSylk;
     285             :     }
     286         170 :     else if (aTypeName == "calc_DIF")
     287             :     {
     288          85 :         if (!detectThisFormat(*pStream, pDIF1) && !detectThisFormat(*pStream, pDIF2))
     289          85 :             return OUString();
     290             : 
     291           0 :         pSearchFilterName = pFilterDif;
     292             :     }
     293          85 :     else if (aTypeName == "calc_dBase")
     294             :     {
     295          85 :         if (!lcl_MayBeDBase(*pStream))
     296          85 :             return OUString();
     297             : 
     298           0 :         pSearchFilterName = pFilterDBase;
     299             :     }
     300             :     else
     301           0 :         return OUString();
     302             : 
     303           0 :     SfxFilterMatcher aMatcher("scalc");
     304           0 :     const SfxFilter* pFilter = aMatcher.GetFilter4FilterName(OUString::createFromAscii(pSearchFilterName));
     305             : 
     306           0 :     if (!pFilter)
     307           0 :         return OUString();
     308             : 
     309           0 :     aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= pFilter->GetName();
     310           0 :     aMediaDesc >> lDescriptor;
     311         425 :     return aTypeName;
     312             : }
     313             : 
     314           0 : OUString SAL_CALL ScFilterDetect::getImplementationName() throw (uno::RuntimeException, std::exception)
     315             : {
     316           0 :     return OUString("com.sun.star.comp.calc.FormatDetector");
     317             : }
     318             : 
     319           0 : sal_Bool ScFilterDetect::supportsService( const OUString& sServiceName )
     320             :     throw (uno::RuntimeException, std::exception)
     321             : {
     322           0 :     return cppu::supportsService(this, sServiceName);
     323             : }
     324             : 
     325           0 : com::sun::star::uno::Sequence<OUString> ScFilterDetect::getSupportedServiceNames()
     326             :     throw (uno::RuntimeException, std::exception)
     327             : {
     328           0 :     uno::Sequence<OUString> seqServiceNames(1);
     329           0 :     seqServiceNames.getArray()[0] = "com.sun.star.frame.ExtendedTypeDetection";
     330           0 :     return seqServiceNames;
     331             : }
     332             : 
     333             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     334         425 : com_sun_star_comp_calc_FormatDetector_get_implementation(::com::sun::star::uno::XComponentContext* context,
     335             :                                                          ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     336             : {
     337         425 :     return cppu::acquire(new ScFilterDetect(context));
     338             : }
     339             : 
     340             : 
     341             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11