1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "oox/xls/biffdetector.hxx" 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <algorithm> 31*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp> 33*cdf0e10cSrcweir #include <comphelper/mediadescriptor.hxx> 34*cdf0e10cSrcweir #include <rtl/strbuf.hxx> 35*cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx" 36*cdf0e10cSrcweir #include "oox/ole/olestorage.hxx" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir namespace oox { 39*cdf0e10cSrcweir namespace xls { 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir // ============================================================================ 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 44*cdf0e10cSrcweir using namespace ::com::sun::star::io; 45*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 46*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using ::comphelper::MediaDescriptor; 49*cdf0e10cSrcweir using ::rtl::OStringBuffer; 50*cdf0e10cSrcweir using ::rtl::OUString; 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir // ============================================================================ 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir Sequence< OUString > BiffDetector_getSupportedServiceNames() 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir Sequence< OUString > aServiceNames( 1 ); 57*cdf0e10cSrcweir aServiceNames[ 0 ] = CREATE_OUSTRING( "com.sun.star.frame.ExtendedTypeDetection" ); 58*cdf0e10cSrcweir return aServiceNames; 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir OUString BiffDetector_getImplementationName() 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir return CREATE_OUSTRING( "com.sun.star.comp.oox.xls.BiffDetector" ); 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir Reference< XInterface > SAL_CALL BiffDetector_createInstance( const Reference< XComponentContext >& rxContext ) throw( Exception ) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir return static_cast< ::cppu::OWeakObject* >( new BiffDetector( rxContext ) ); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir // ============================================================================ 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir BiffDetector::BiffDetector( const Reference< XComponentContext >& rxContext ) throw( RuntimeException ) : 74*cdf0e10cSrcweir mxContext( rxContext, UNO_SET_THROW ) 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir BiffDetector::~BiffDetector() 79*cdf0e10cSrcweir { 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir /*static*/ BiffType BiffDetector::detectStreamBiffVersion( BinaryInputStream& rInStream ) 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir BiffType eBiff = BIFF_UNKNOWN; 85*cdf0e10cSrcweir if( !rInStream.isEof() && rInStream.isSeekable() && (rInStream.size() > 4) ) 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir sal_Int64 nOldPos = rInStream.tell(); 88*cdf0e10cSrcweir rInStream.seekToStart(); 89*cdf0e10cSrcweir sal_uInt16 nBofId, nBofSize; 90*cdf0e10cSrcweir rInStream >> nBofId >> nBofSize; 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir if( (4 <= nBofSize) && (nBofSize <= 16) && (rInStream.tell() + nBofSize <= rInStream.size()) ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir switch( nBofId ) 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir case BIFF2_ID_BOF: 97*cdf0e10cSrcweir eBiff = BIFF2; 98*cdf0e10cSrcweir break; 99*cdf0e10cSrcweir case BIFF3_ID_BOF: 100*cdf0e10cSrcweir eBiff = BIFF3; 101*cdf0e10cSrcweir break; 102*cdf0e10cSrcweir case BIFF4_ID_BOF: 103*cdf0e10cSrcweir eBiff = BIFF4; 104*cdf0e10cSrcweir break; 105*cdf0e10cSrcweir case BIFF5_ID_BOF: 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir if( 6 <= nBofSize ) 108*cdf0e10cSrcweir { 109*cdf0e10cSrcweir sal_uInt16 nVersion; 110*cdf0e10cSrcweir rInStream >> nVersion; 111*cdf0e10cSrcweir // #i23425# #i44031# #i62752# there are some *really* broken documents out there... 112*cdf0e10cSrcweir switch( nVersion & 0xFF00 ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir case 0: eBiff = BIFF5; break; // #i44031# #i62752# 115*cdf0e10cSrcweir case BIFF_BOF_BIFF2: eBiff = BIFF2; break; 116*cdf0e10cSrcweir case BIFF_BOF_BIFF3: eBiff = BIFF3; break; 117*cdf0e10cSrcweir case BIFF_BOF_BIFF4: eBiff = BIFF4; break; 118*cdf0e10cSrcweir case BIFF_BOF_BIFF5: eBiff = BIFF5; break; 119*cdf0e10cSrcweir case BIFF_BOF_BIFF8: eBiff = BIFF8; break; 120*cdf0e10cSrcweir default: OSL_ENSURE( false, 121*cdf0e10cSrcweir OStringBuffer( "lclDetectStreamBiffVersion - unknown BIFF version: 0x" ). 122*cdf0e10cSrcweir append( static_cast< sal_Int32 >( nVersion ), 16 ).getStr() ); 123*cdf0e10cSrcweir } 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir break; 127*cdf0e10cSrcweir // else do nothing, no BIFF stream 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir rInStream.seek( nOldPos ); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir return eBiff; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /*static*/ BiffType BiffDetector::detectStorageBiffVersion( OUString& orWorkbookStreamName, const StorageRef& rxStorage ) 136*cdf0e10cSrcweir { 137*cdf0e10cSrcweir static const OUString saBookName = CREATE_OUSTRING( "Book" ); 138*cdf0e10cSrcweir static const OUString saWorkbookName = CREATE_OUSTRING( "Workbook" ); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir BiffType eBiff = BIFF_UNKNOWN; 141*cdf0e10cSrcweir if( rxStorage.get() ) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir if( rxStorage->isStorage() ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir // try to open the "Book" stream 146*cdf0e10cSrcweir BinaryXInputStream aBookStrm5( rxStorage->openInputStream( saBookName ), true ); 147*cdf0e10cSrcweir BiffType eBookStrm5Biff = detectStreamBiffVersion( aBookStrm5 ); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // try to open the "Workbook" stream 150*cdf0e10cSrcweir BinaryXInputStream aBookStrm8( rxStorage->openInputStream( saWorkbookName ), true ); 151*cdf0e10cSrcweir BiffType eBookStrm8Biff = detectStreamBiffVersion( aBookStrm8 ); 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir // decide which stream to use 154*cdf0e10cSrcweir if( (eBookStrm8Biff != BIFF_UNKNOWN) && ((eBookStrm5Biff == BIFF_UNKNOWN) || (eBookStrm8Biff > eBookStrm5Biff)) ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir /* Only "Workbook" stream exists; or both streams exist, and 157*cdf0e10cSrcweir "Workbook" has higher BIFF version than "Book" stream. */ 158*cdf0e10cSrcweir eBiff = eBookStrm8Biff; 159*cdf0e10cSrcweir orWorkbookStreamName = saWorkbookName; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir else if( eBookStrm5Biff != BIFF_UNKNOWN ) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir /* Only "Book" stream exists; or both streams exist, and 164*cdf0e10cSrcweir "Book" has higher BIFF version than "Workbook" stream. */ 165*cdf0e10cSrcweir eBiff = eBookStrm5Biff; 166*cdf0e10cSrcweir orWorkbookStreamName = saBookName; 167*cdf0e10cSrcweir } 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir else 170*cdf0e10cSrcweir { 171*cdf0e10cSrcweir // no storage, try plain input stream from medium (even for BIFF5+) 172*cdf0e10cSrcweir BinaryXInputStream aStrm( rxStorage->openInputStream( OUString() ), false ); 173*cdf0e10cSrcweir eBiff = detectStreamBiffVersion( aStrm ); 174*cdf0e10cSrcweir orWorkbookStreamName = OUString(); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir return eBiff; 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir // com.sun.star.lang.XServiceInfo interface ----------------------------------- 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir OUString SAL_CALL BiffDetector::getImplementationName() throw( RuntimeException ) 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir return BiffDetector_getImplementationName(); 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir sal_Bool SAL_CALL BiffDetector::supportsService( const OUString& rService ) throw( RuntimeException ) 189*cdf0e10cSrcweir { 190*cdf0e10cSrcweir const Sequence< OUString > aServices = BiffDetector_getSupportedServiceNames(); 191*cdf0e10cSrcweir const OUString* pArray = aServices.getConstArray(); 192*cdf0e10cSrcweir const OUString* pArrayEnd = pArray + aServices.getLength(); 193*cdf0e10cSrcweir return ::std::find( pArray, pArrayEnd, rService ) != pArrayEnd; 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir Sequence< OUString > SAL_CALL BiffDetector::getSupportedServiceNames() throw( RuntimeException ) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir return BiffDetector_getSupportedServiceNames(); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir // com.sun.star.document.XExtendedFilterDetect interface ---------------------- 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir OUString SAL_CALL BiffDetector::detect( Sequence< PropertyValue >& rDescriptor ) throw( RuntimeException ) 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir OUString aTypeName; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir MediaDescriptor aDescriptor( rDescriptor ); 208*cdf0e10cSrcweir aDescriptor.addInputStream(); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir Reference< XInputStream > xInStrm( aDescriptor[ MediaDescriptor::PROP_INPUTSTREAM() ], UNO_QUERY_THROW ); 211*cdf0e10cSrcweir StorageRef xStorage( new ::oox::ole::OleStorage( mxContext, xInStrm, true ) ); 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir OUString aWorkbookName; 214*cdf0e10cSrcweir switch( detectStorageBiffVersion( aWorkbookName, xStorage ) ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir case BIFF2: 217*cdf0e10cSrcweir case BIFF3: 218*cdf0e10cSrcweir case BIFF4: aTypeName = CREATE_OUSTRING( "calc_MS_Excel_40" ); break; 219*cdf0e10cSrcweir case BIFF5: aTypeName = CREATE_OUSTRING( "calc_MS_Excel_95" ); break; 220*cdf0e10cSrcweir case BIFF8: aTypeName = CREATE_OUSTRING( "calc_MS_Excel_97" ); break; 221*cdf0e10cSrcweir default:; 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir return aTypeName; 225*cdf0e10cSrcweir } 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir // ============================================================================ 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir } // namespace xls 230*cdf0e10cSrcweir } // namespace oox 231