LCOV - code coverage report
Current view: top level - sc/source/filter/oox - formulaparser.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 266 1440 18.5 %
Date: 2015-06-13 12:38:46 Functions: 52 208 25.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 "formulaparser.hxx"
      21             : 
      22             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      23             : #include <com/sun/star/sheet/ComplexReference.hpp>
      24             : #include <com/sun/star/sheet/ExternalReference.hpp>
      25             : #include <com/sun/star/sheet/FormulaToken.hpp>
      26             : #include <com/sun/star/sheet/NameToken.hpp>
      27             : #include <com/sun/star/sheet/ReferenceFlags.hpp>
      28             : #include <com/sun/star/sheet/SingleReference.hpp>
      29             : #include <osl/diagnose.h>
      30             : #include <oox/core/filterbase.hxx>
      31             : #include <oox/token/properties.hxx>
      32             : #include "addressconverter.hxx"
      33             : #include "biffinputstream.hxx"
      34             : #include "defnamesbuffer.hxx"
      35             : #include "externallinkbuffer.hxx"
      36             : #include "tablebuffer.hxx"
      37             : #include "worksheethelper.hxx"
      38             : 
      39             : namespace oox {
      40             : namespace xls {
      41             : 
      42             : using namespace ::com::sun::star::sheet;
      43             : using namespace ::com::sun::star::sheet::ReferenceFlags;
      44             : using namespace ::com::sun::star::table;
      45             : using namespace ::com::sun::star::uno;
      46             : 
      47             : namespace {
      48             : 
      49           0 : sal_uInt16 lclReadFmlaSize( BiffInputStream& rStrm, BiffType eBiff, const sal_uInt16* pnFmlaSize )
      50             : {
      51           0 :     return pnFmlaSize ? *pnFmlaSize : ((eBiff == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16());
      52             : }
      53             : 
      54             : } // namespace
      55             : 
      56             : // formula finalizer ==========================================================
      57             : 
      58         414 : FormulaFinalizer::FormulaFinalizer( const OpCodeProvider& rOpCodeProv ) :
      59             :     OpCodeProvider( rOpCodeProv ),
      60         414 :     ApiOpCodes( getOpCodes() )
      61             : {
      62         414 :     maTokens.reserve( 0x2000 );
      63         414 : }
      64             : 
      65         307 : ApiTokenSequence FormulaFinalizer::finalizeTokenArray( const ApiTokenSequence& rTokens )
      66             : {
      67         307 :     maTokens.clear();
      68         307 :     if( rTokens.hasElements() )
      69             :     {
      70         307 :         const ApiToken* pToken = rTokens.getConstArray();
      71         307 :         processTokens( pToken, pToken + rTokens.getLength() );
      72             :     }
      73         307 :     return maTokens.toSequence();
      74             : }
      75             : 
      76           0 : const FunctionInfo* FormulaFinalizer::resolveBadFuncName( const OUString& ) const
      77             : {
      78           0 :     return 0;
      79             : }
      80             : 
      81           0 : OUString FormulaFinalizer::resolveDefinedName( sal_Int32 ) const
      82             : {
      83           0 :     return OUString();
      84             : }
      85             : 
      86         335 : const FunctionInfo* FormulaFinalizer::getFunctionInfo( ApiToken& orFuncToken )
      87             : {
      88             :     // first, try to find a regular function info from token op-code
      89         335 :     if( const FunctionInfo* pRegFuncInfo = getFuncInfoFromApiToken( orFuncToken ) )
      90           3 :         return pRegFuncInfo;
      91             : 
      92             :     // try to recognize a function from an external library
      93         332 :     if( (orFuncToken.OpCode == OPCODE_BAD) && orFuncToken.Data.has< OUString >() )
      94             :     {
      95             :         // virtual call to resolveBadFuncName()
      96           0 :         if( const FunctionInfo* pLibFuncInfo = resolveBadFuncName( orFuncToken.Data.get< OUString >() ) )
      97             :         {
      98             :             // write function op-code to the OPCODE_BAD token
      99           0 :             orFuncToken.OpCode = pLibFuncInfo->mnApiOpCode;
     100             :             // if it is an external function, insert programmatic function name
     101           0 :             if( (orFuncToken.OpCode == OPCODE_EXTERNAL) && (!pLibFuncInfo->maExtProgName.isEmpty()) )
     102           0 :                 orFuncToken.Data <<= pLibFuncInfo->maExtProgName;
     103             :             else
     104           0 :                 orFuncToken.Data.clear();   // clear string from OPCODE_BAD
     105           0 :             return pLibFuncInfo;
     106             :         }
     107             :     }
     108             : 
     109             :     // no success - return null
     110         332 :     return 0;
     111             : }
     112             : 
     113           0 : const FunctionInfo* FormulaFinalizer::getExternCallInfo( ApiToken& orFuncToken, const ApiToken& rECToken )
     114             : {
     115             :     // try to resolve the passed token to a supported sheet function
     116           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromApiToken( rECToken ) )
     117             :     {
     118           0 :         orFuncToken.OpCode = pFuncInfo->mnApiOpCode;
     119             :         // programmatic add-in function name
     120           0 :         if( (pFuncInfo->mnApiOpCode == OPCODE_EXTERNAL) && !pFuncInfo->maExtProgName.isEmpty() )
     121           0 :             orFuncToken.Data <<= pFuncInfo->maExtProgName;
     122             :         // name of unsupported function, convert to OPCODE_BAD to preserve the name
     123           0 :         else if( (pFuncInfo->mnApiOpCode == OPCODE_BAD) && !pFuncInfo->maOoxFuncName.isEmpty() )
     124           0 :             orFuncToken.Data <<= pFuncInfo->maOoxFuncName;
     125           0 :         return pFuncInfo;
     126             :     }
     127             : 
     128             :     // macro call or unknown function name, move data to function token
     129           0 :     if( (rECToken.OpCode == OPCODE_MACRO) || (rECToken.OpCode == OPCODE_BAD) )
     130           0 :         orFuncToken = rECToken;
     131             : 
     132             :     // defined name used as function call, convert to OPCODE_BAD to preserve the name
     133           0 :     if( (rECToken.OpCode == OPCODE_NAME) && rECToken.Data.has< sal_Int32 >() )
     134             :     {
     135           0 :         OUString aDefName = resolveDefinedName( rECToken.Data.get< sal_Int32 >() );
     136           0 :         if( !aDefName.isEmpty() )
     137             :         {
     138           0 :             orFuncToken.OpCode = OPCODE_BAD;
     139           0 :             orFuncToken.Data <<= aDefName;
     140           0 :         }
     141             :     }
     142             : 
     143           0 :     return 0;
     144             : }
     145             : 
     146         311 : void FormulaFinalizer::processTokens( const ApiToken* pToken, const ApiToken* pTokenEnd )
     147             : {
     148         957 :     while( pToken < pTokenEnd )
     149             :     {
     150             :         // push the current token into the vector
     151         335 :         bool bValid = appendFinalToken( *pToken );
     152             :         // try to process a function
     153         335 :         if( const FunctionInfo* pFuncInfo = bValid ? getFunctionInfo( maTokens.back() ) : 0 )
     154           3 :             pToken = processParameters( *pFuncInfo, pToken + 1, pTokenEnd );
     155             :         // otherwise, go to next token
     156             :         else
     157         332 :             ++pToken;
     158             :     }
     159         311 : }
     160             : 
     161           3 : const ApiToken* FormulaFinalizer::processParameters(
     162             :         const FunctionInfo& rFuncInfo, const ApiToken* pToken, const ApiToken* pTokenEnd )
     163             : {
     164             :     // remember position of the token containing the function op-code
     165           3 :     size_t nFuncNameIdx = maTokens.size() - 1;
     166             : 
     167             :     // process a function, if an OPCODE_OPEN token is following
     168             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::processParameters - OPCODE_OPEN expected" );
     169           3 :     if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN) )
     170             :     {
     171             :         // append the OPCODE_OPEN token to the vector
     172           3 :         maTokens.append( OPCODE_OPEN );
     173             : 
     174             :         // store positions of OPCODE_OPEN, parameter separators, and OPCODE_CLOSE
     175           3 :         ParameterPosVector aParams;
     176           3 :         pToken = findParameters( aParams, pToken, pTokenEnd );
     177             :         OSL_ENSURE( aParams.size() >= 2, "FormulaFinalizer::processParameters - missing tokens" );
     178           3 :         size_t nParamCount = aParams.size() - 1;
     179             : 
     180           3 :         if( (nParamCount == 1) && isEmptyParameter( aParams[ 0 ] + 1, aParams[ 1 ] ) )
     181             :         {
     182             :             /*  Empty pair of parentheses -> function call without parameters,
     183             :                 process parameter, there might be spaces between parentheses. */
     184           0 :             processTokens( aParams[ 0 ] + 1, aParams[ 1 ] );
     185             :         }
     186             :         else
     187             :         {
     188           3 :             const FunctionInfo* pRealFuncInfo = &rFuncInfo;
     189           3 :             ParameterPosVector::const_iterator aPosIt = aParams.begin();
     190             : 
     191             :             /*  Preprocess EXTERN.CALL functions. The actual function name is
     192             :                 contained as reference to a defined name in the first (hidden)
     193             :                 parameter. */
     194           3 :             if( rFuncInfo.mnBiffFuncId == BIFF_FUNC_EXTERNCALL )
     195             :             {
     196           0 :                 ApiToken& rFuncToken = maTokens[ nFuncNameIdx ];
     197           0 :                 rFuncToken.OpCode = OPCODE_NONAME;
     198             : 
     199             :                 // try to initialize function token from first parameter
     200           0 :                 if( const ApiToken* pECToken = getSingleToken( *aPosIt + 1, *(aPosIt + 1) ) )
     201           0 :                     if( const FunctionInfo* pECFuncInfo = getExternCallInfo( rFuncToken, *pECToken ) )
     202           0 :                         pRealFuncInfo = pECFuncInfo;
     203             : 
     204             :                 /*  On success (something has been inserted into rFuncToken),
     205             :                     skip the first parameter. */
     206           0 :                 if( rFuncToken.OpCode != OPCODE_NONAME )
     207             :                 {
     208           0 :                     --nParamCount;
     209           0 :                     ++aPosIt;
     210             :                 }
     211             :             }
     212             : 
     213             :             // process all parameters
     214           3 :             FunctionParamInfoIterator aParamInfoIt( *pRealFuncInfo );
     215           3 :             size_t nLastValidSize = maTokens.size();
     216           3 :             size_t nLastValidCount = 0;
     217           7 :             for( size_t nParam = 0; nParam < nParamCount; ++nParam, ++aPosIt, ++aParamInfoIt )
     218             :             {
     219             :                 // add embedded Calc-only parameters
     220           4 :                 if( aParamInfoIt.isCalcOnlyParam() )
     221             :                 {
     222           0 :                     appendCalcOnlyParameter( *pRealFuncInfo, nParam, nParamCount );
     223           0 :                     while( aParamInfoIt.isCalcOnlyParam() ) ++aParamInfoIt;
     224             :                 }
     225             : 
     226           4 :                 const ApiToken* pParamBegin = *aPosIt + 1;
     227           4 :                 const ApiToken* pParamEnd = *(aPosIt + 1);
     228           4 :                 bool bIsEmpty = isEmptyParameter( pParamBegin, pParamEnd );
     229             : 
     230           4 :                 if( !aParamInfoIt.isExcelOnlyParam() )
     231             :                 {
     232             :                     // handle empty parameters
     233           4 :                     if( bIsEmpty )
     234             :                     {
     235             :                         // append leading space tokens from original token array
     236           0 :                         while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
     237           0 :                             maTokens.push_back( *pParamBegin++ );
     238             :                         // add default values for some empty parameters, or the OPCODE_MISSING token
     239           0 :                         appendEmptyParameter( *pRealFuncInfo, nParam );
     240             :                         // reset bIsEmpty flag, if something has been appended in appendEmptyParameter()
     241           0 :                         bIsEmpty = maTokens.back().OpCode == OPCODE_MISSING;
     242             :                         // skip OPCODE_MISSING token in the original token array
     243             :                         OSL_ENSURE( (pParamBegin == pParamEnd) || (pParamBegin->OpCode == OPCODE_MISSING), "FormulaFinalizer::processParameters - OPCODE_MISSING expected" );
     244           0 :                         if( pParamBegin < pParamEnd ) ++pParamBegin;
     245             :                         // append trailing space tokens from original token array
     246           0 :                         while( (pParamBegin < pParamEnd) && (pParamBegin->OpCode == OPCODE_SPACES) )
     247           0 :                             maTokens.push_back( *pParamBegin++ );
     248             :                     }
     249             :                     else
     250             :                     {
     251             :                         // if parameter is not empty, process all tokens of the parameter
     252           4 :                         processTokens( pParamBegin, pParamEnd );
     253             :                     }
     254             : 
     255             :                     // append parameter separator token
     256           4 :                     maTokens.append( OPCODE_SEP );
     257             :                 }
     258             : 
     259             :                 /*  #84453# Update size of new token sequence with valid parameters
     260             :                     to be able to remove trailing optional empty parameters. */
     261           4 :                 if( !bIsEmpty || (nParam < pRealFuncInfo->mnMinParamCount) )
     262             :                 {
     263           4 :                     nLastValidSize = maTokens.size();
     264           4 :                     nLastValidCount = nParam + 1;
     265             :                 }
     266             :             }
     267             : 
     268             :             // #84453# remove trailing optional empty parameters
     269           3 :             maTokens.resize( nLastValidSize );
     270             : 
     271             :             // add trailing Calc-only parameters
     272           3 :             if( aParamInfoIt.isCalcOnlyParam() )
     273           0 :                 appendCalcOnlyParameter( *pRealFuncInfo, nLastValidCount, nParamCount );
     274             : 
     275             :             // add optional parameters that are required in Calc
     276           3 :             appendRequiredParameters( *pRealFuncInfo, nLastValidCount );
     277             : 
     278             :             // remove last parameter separator token
     279           3 :             if( maTokens.back().OpCode == OPCODE_SEP )
     280           3 :                 maTokens.pop_back();
     281             :         }
     282             : 
     283             :         /*  Append the OPCODE_CLOSE token to the vector, but only if there is
     284             :             no OPCODE_BAD token at the end, this token already contains the
     285             :             trailing closing parentheses. */
     286           3 :         if( (pTokenEnd - 1)->OpCode != OPCODE_BAD )
     287           3 :             maTokens.append( OPCODE_CLOSE );
     288             :     }
     289             : 
     290             :     /*  Replace OPCODE_EXTERNAL with OPCODE_NONAME to get #NAME! error in cell,
     291             :         if no matching add-in function was found. */
     292           3 :     ApiToken& rFuncNameToken = maTokens[ nFuncNameIdx ];
     293           3 :     if( (rFuncNameToken.OpCode == OPCODE_EXTERNAL) && !rFuncNameToken.Data.hasValue() )
     294           0 :         rFuncNameToken.OpCode = OPCODE_NONAME;
     295             : 
     296           3 :     return pToken;
     297             : }
     298             : 
     299           6 : bool FormulaFinalizer::isEmptyParameter( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     300             : {
     301           6 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     302           6 :     if( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_MISSING) ) ++pToken;
     303           6 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     304           6 :     return pToken == pTokenEnd;
     305             : }
     306             : 
     307           0 : const ApiToken* FormulaFinalizer::getSingleToken( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     308             : {
     309           0 :     const ApiToken* pSingleToken = 0;
     310             :     // skip leading whitespace tokens
     311           0 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     312             :     // remember first non-whitespace token
     313           0 :     if( pToken < pTokenEnd ) pSingleToken = pToken++;
     314             :     // skip trailing whitespace tokens
     315           0 :     while( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_SPACES) ) ++pToken;
     316             :     // return null, if other non-whitespace tokens follow
     317           0 :     return (pToken == pTokenEnd) ? pSingleToken : 0;
     318             : }
     319             : 
     320           3 : const ApiToken* FormulaFinalizer::skipParentheses( const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     321             : {
     322             :     // skip tokens between OPCODE_OPEN and OPCODE_CLOSE
     323             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "skipParentheses - OPCODE_OPEN expected" );
     324           3 :     ++pToken;
     325          14 :     while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
     326             :     {
     327           8 :         if( pToken->OpCode == OPCODE_OPEN )
     328           1 :             pToken = skipParentheses( pToken, pTokenEnd );
     329             :         else
     330           7 :             ++pToken;
     331             :     }
     332             :     // skip the OPCODE_CLOSE token
     333             :     OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "skipParentheses - OPCODE_CLOSE expected" );
     334           3 :     return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
     335             : }
     336             : 
     337           3 : const ApiToken* FormulaFinalizer::findParameters( ParameterPosVector& rParams,
     338             :         const ApiToken* pToken, const ApiToken* pTokenEnd ) const
     339             : {
     340             :     // push position of OPCODE_OPEN
     341             :     OSL_ENSURE( (pToken < pTokenEnd) && (pToken->OpCode == OPCODE_OPEN), "FormulaFinalizer::findParameters - OPCODE_OPEN expected" );
     342           3 :     rParams.push_back( pToken++ );
     343             : 
     344             :     // find positions of parameter separators
     345          13 :     while( (pToken < pTokenEnd) && (pToken->OpCode != OPCODE_CLOSE) )
     346             :     {
     347           7 :         if( pToken->OpCode == OPCODE_OPEN )
     348           2 :             pToken = skipParentheses( pToken, pTokenEnd );
     349           5 :         else if( pToken->OpCode == OPCODE_SEP )
     350           1 :             rParams.push_back( pToken++ );
     351             :         else
     352           4 :             ++pToken;
     353             :     }
     354             : 
     355             :     // push position of OPCODE_CLOSE
     356             :     OSL_ENSURE( ((pToken < pTokenEnd) && (pToken->OpCode == OPCODE_CLOSE)) || ((pTokenEnd - 1)->OpCode == OPCODE_BAD), "FormulaFinalizer::findParameters - OPCODE_CLOSE expected" );
     357           3 :     rParams.push_back( pToken );
     358           3 :     return (pToken < pTokenEnd) ? (pToken + 1) : pTokenEnd;
     359             : }
     360             : 
     361           0 : void FormulaFinalizer::appendEmptyParameter( const FunctionInfo& rFuncInfo, size_t nParam )
     362             : {
     363             :     // remember old size of the token array
     364           0 :     size_t nTokenArraySize = maTokens.size();
     365             : 
     366           0 :     switch( rFuncInfo.mnBiff12FuncId )
     367             :     {
     368             :         case BIFF_FUNC_IF:
     369           0 :             if( (nParam == 1) || (nParam == 2) )
     370           0 :                 maTokens.append< double >( OPCODE_PUSH, 0.0 );
     371           0 :         break;
     372             :         default:;
     373             :     }
     374             : 
     375             :     // if no token has been added, append a OPCODE_MISSING token
     376           0 :     if( nTokenArraySize == maTokens.size() )
     377           0 :         maTokens.append( OPCODE_MISSING );
     378           0 : }
     379             : 
     380           0 : void FormulaFinalizer::appendCalcOnlyParameter( const FunctionInfo& rFuncInfo, size_t nParam, size_t nParamCount )
     381             : {
     382           0 :     switch( rFuncInfo.mnBiff12FuncId )
     383             :     {
     384             :         case BIFF_FUNC_FLOOR:
     385             :         case BIFF_FUNC_CEILING:
     386           0 :             if (nParam == 2 && nParamCount < 3)
     387             :             {
     388           0 :                 maTokens.append< double >( OPCODE_PUSH, 1.0 );
     389           0 :                 maTokens.append( OPCODE_SEP );
     390             :             }
     391           0 :         break;
     392             :     }
     393           0 : }
     394             : 
     395           3 : void FormulaFinalizer::appendRequiredParameters( const FunctionInfo& rFuncInfo, size_t nParamCount )
     396             : {
     397           3 :     switch( rFuncInfo.mnBiff12FuncId )
     398             :     {
     399             :         case BIFF_FUNC_WEEKNUM:
     400           0 :             if( nParamCount == 1 )
     401             :             {
     402           0 :                 maTokens.append< double >( OPCODE_PUSH, 1.0 );
     403           0 :                 maTokens.append( OPCODE_SEP );
     404             :             }
     405           0 :         break;
     406             :     }
     407           3 : }
     408             : 
     409         335 : bool FormulaFinalizer::appendFinalToken( const ApiToken& rToken )
     410             : {
     411             :     // replace OPCODE_MACRO without macro name with #NAME? error code
     412         335 :     bool bValid = (rToken.OpCode != OPCODE_MACRO) || rToken.Data.hasValue();
     413         335 :     if( bValid )
     414             :     {
     415         335 :         maTokens.push_back( rToken );
     416             :     }
     417             :     else
     418             :     {
     419           0 :         maTokens.append( OPCODE_ARRAY_OPEN );
     420           0 :         maTokens.append( OPCODE_PUSH, BiffHelper::calcDoubleFromError( BIFF_ERR_NAME ) );
     421           0 :         maTokens.append( OPCODE_ARRAY_CLOSE );
     422             :     }
     423         335 :     return bValid;
     424             : }
     425             : 
     426             : // parser implementation base =================================================
     427             : 
     428         414 : class FormulaParserImpl : public FormulaFinalizer, public WorkbookHelper
     429             : {
     430             : public:
     431             :     explicit            FormulaParserImpl( const FormulaParser& rParent );
     432             : 
     433             :     /** Converts an OOXML formula string. */
     434             :     virtual ApiTokenSequence importOoxFormula(
     435             :                             const CellAddress& rBaseAddress,
     436             :                             const OUString& rFormulaString );
     437             : 
     438             :     /** Imports and converts a BIFF12 token array from the passed stream. */
     439             :     virtual ApiTokenSequence importBiff12Formula(
     440             :                             const CellAddress& rBaseAddress,
     441             :                             FormulaType eType,
     442             :                             SequenceInputStream& rStrm );
     443             : 
     444             :     /** Imports and converts a BIFF2-BIFF8 token array from the passed stream. */
     445             :     virtual ApiTokenSequence importBiffFormula(
     446             :                             const CellAddress& rBaseAddress,
     447             :                             FormulaType eType,
     448             :                             BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize );
     449             : 
     450             :     /** Tries to resolve the passed ref-id to an OLE target URL. */
     451             :     OUString            resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const;
     452             : 
     453             : protected:
     454             :     typedef ::std::pair< sal_Int32, bool >  WhiteSpace;
     455             :     typedef ::std::vector< WhiteSpace >     WhiteSpaceVec;
     456             : 
     457             :     /** Initializes the formula parser before importing a formula. */
     458             :     void                initializeImport( const CellAddress& rBaseAddress, FormulaType eType );
     459             :     /** Finalizes the internal token storage after import. */
     460             :     ApiTokenSequence    finalizeImport();
     461             : 
     462             :     // token array ------------------------------------------------------------
     463             : 
     464             :     bool                resetSpaces();
     465             :     static void         appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed );
     466             :     void                appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed );
     467             :     void                appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed );
     468             :     void                appendClosingSpaces( sal_Int32 nCount, bool bLineFeed );
     469             : 
     470             :     size_t              getFormulaSize() const;
     471             :     Any&                appendRawToken( sal_Int32 nOpCode );
     472             :     Any&                insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd );
     473             :     size_t              appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces );
     474             :     size_t              insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd );
     475             : 
     476             :     size_t              getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const;
     477             :     void                pushOperandSize( size_t nSize );
     478             :     size_t              popOperandSize();
     479             : 
     480             :     ApiToken&           getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex );
     481             : 
     482             :     bool                pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     483             :     bool                pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     484             :     template< typename Type >
     485             :     bool                pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     486             :     template< typename Type >
     487           0 :     inline bool         pushValueOperandToken( const Type& rValue, const WhiteSpaceVec* pSpaces = 0 )
     488           0 :                             { return pushValueOperandToken( rValue, OPCODE_PUSH, pSpaces ); }
     489             :     bool                pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     490             :     bool                pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     491             :     bool                pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     492             :     bool                pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces = 0 );
     493             :     bool                pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     494             :     bool                pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     495             :     bool                pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces = 0, const WhiteSpaceVec* pClosingSpaces = 0 );
     496             : 
     497             :     bool                pushOperand( sal_Int32 nOpCode );
     498             :     bool                pushAnyOperand( const Any& rAny, sal_Int32 nOpCode );
     499             :     template< typename Type >
     500             :     bool                pushValueOperand( const Type& rValue, sal_Int32 nOpCode );
     501             :     template< typename Type >
     502          12 :     inline bool         pushValueOperand( const Type& rValue )
     503          12 :                             { return pushValueOperand( rValue, OPCODE_PUSH ); }
     504             :     bool                pushBoolOperand( bool bValue );
     505             :     bool                pushErrorOperand( double fEncodedError );
     506             :     bool                pushBiffBoolOperand( sal_uInt8 nValue );
     507             :     bool                pushBiffErrorOperand( sal_uInt8 nErrorCode );
     508             :     bool                pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     509             :     bool                pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     510             :     template< typename Type >
     511             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef );
     512             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     513             :     bool                pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
     514             :     bool                pushNlrOperand( const BinSingleRef2d& rRef );
     515             :     bool                pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken );
     516             :     bool                pushDefinedNameOperand( const DefinedNameRef& rxDefName );
     517             :     bool                pushExternalFuncOperand( const FunctionInfo& rFuncInfo );
     518             :     bool                pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem );
     519             :     bool                pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink );
     520             :     bool                pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable );
     521             : 
     522             :     bool                pushUnaryPreOperator( sal_Int32 nOpCode );
     523             :     bool                pushUnaryPostOperator( sal_Int32 nOpCode );
     524             :     bool                pushBinaryOperator( sal_Int32 nOpCode );
     525             :     bool                pushParenthesesOperator();
     526             :     bool                pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount );
     527             :     bool                pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount );
     528             : 
     529             : private:
     530             :     // reference conversion ---------------------------------------------------
     531             : 
     532             :     void                initReference2d( SingleReference& orApiRef ) const;
     533             :     static void         initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet );
     534             :     void                convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     535             :     void                convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     536             :     void                convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     537             :     void                convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     538             :     void                convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const;
     539             :     void                convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const;
     540             : 
     541             : private:
     542             :     // finalize token sequence ------------------------------------------------
     543             : 
     544             :     virtual const FunctionInfo* resolveBadFuncName( const OUString& rTokenData ) const SAL_OVERRIDE;
     545             :     virtual OUString resolveDefinedName( sal_Int32 nTokenIndex ) const SAL_OVERRIDE;
     546             : 
     547             : protected:
     548             :     const sal_Int32     mnMaxApiCol;                /// Maximum column index in own document.
     549             :     const sal_Int32     mnMaxApiRow;                /// Maximum row index in own document.
     550             :     const sal_Int32     mnMaxXlsCol;                /// Maximum column index in imported document.
     551             :     const sal_Int32     mnMaxXlsRow;                /// Maximum row index in imported document.
     552             : 
     553             :     CellAddress         maBaseAddr;                 /// Base address for relative references.
     554             :     bool                mbRelativeAsOffset;         /// True = relative row/column index is (signed) offset, false = explicit index.
     555             :     bool                mb2dRefsAs3dRefs;           /// True = convert all 2D references to 3D references in sheet specified by base address.
     556             :     bool                mbSpecialTokens;            /// True = special handling for tExp and tTbl tokens, false = exit with error.
     557             :     bool                mbAllowNulChars;            /// True = keep NUL characters in string tokens.
     558             : 
     559             : private:
     560             :     typedef ::std::vector< size_t > SizeTypeVector;
     561             : 
     562             :     ApiTokenVector      maTokenStorage;             /// Raw unordered token storage.
     563             :     SizeTypeVector      maTokenIndexes;             /// Indexes into maTokenStorage.
     564             :     SizeTypeVector      maOperandSizeStack;         /// Stack with token sizes per operand.
     565             :     WhiteSpaceVec       maLeadingSpaces;            /// List of whitespaces before next token.
     566             :     WhiteSpaceVec       maOpeningSpaces;            /// List of whitespaces before opening parenthesis.
     567             :     WhiteSpaceVec       maClosingSpaces;            /// List of whitespaces before closing parenthesis.
     568             : };
     569             : 
     570         414 : FormulaParserImpl::FormulaParserImpl( const FormulaParser& rParent ) :
     571             :     FormulaFinalizer( rParent ),
     572             :     WorkbookHelper( rParent ),
     573         414 :     mnMaxApiCol( rParent.getAddressConverter().getMaxApiAddress().Column ),
     574         414 :     mnMaxApiRow( rParent.getAddressConverter().getMaxApiAddress().Row ),
     575         414 :     mnMaxXlsCol( rParent.getAddressConverter().getMaxXlsAddress().Column ),
     576         414 :     mnMaxXlsRow( rParent.getAddressConverter().getMaxXlsAddress().Row ),
     577             :     mbRelativeAsOffset( false ),
     578             :     mb2dRefsAs3dRefs( false ),
     579             :     mbSpecialTokens( false ),
     580        2070 :     mbAllowNulChars( false )
     581             : {
     582             :     // reserve enough space to make resize(), push_back() etc. cheap
     583         414 :     maTokenStorage.reserve( 0x2000 );
     584         414 :     maTokenIndexes.reserve( 0x2000 );
     585         414 :     maOperandSizeStack.reserve( 256 );
     586         414 :     maLeadingSpaces.reserve( 256 );
     587         414 :     maOpeningSpaces.reserve( 256 );
     588         414 :     maClosingSpaces.reserve( 256 );
     589         414 : }
     590             : 
     591           0 : ApiTokenSequence FormulaParserImpl::importOoxFormula( const CellAddress&, const OUString& )
     592             : {
     593             :     OSL_FAIL( "FormulaParserImpl::importOoxFormula - not implemented" );
     594           0 :     return ApiTokenSequence();
     595             : }
     596             : 
     597           0 : ApiTokenSequence FormulaParserImpl::importBiff12Formula( const CellAddress&, FormulaType, SequenceInputStream& )
     598             : {
     599             :     SAL_WARN("sc", "FormulaParserImpl::importBiff12Formula - not implemented" );
     600           0 :     return ApiTokenSequence();
     601             : }
     602             : 
     603           0 : ApiTokenSequence FormulaParserImpl::importBiffFormula( const CellAddress&, FormulaType, BiffInputStream&, const sal_uInt16* )
     604             : {
     605             :     OSL_FAIL( "FormulaParserImpl::importBiffFormula - not implemented" );
     606           0 :     return ApiTokenSequence();
     607             : }
     608             : 
     609           0 : OUString FormulaParserImpl::resolveOleTarget( sal_Int32 nRefId, bool bUseRefSheets ) const
     610             : {
     611           0 :     const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, bUseRefSheets ).get();
     612             :     OSL_ENSURE( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE), "FormulaParserImpl::resolveOleTarget - missing or wrong link" );
     613           0 :     if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_OLE) )
     614           0 :          return getBaseFilter().getAbsoluteUrl( pExtLink->getTargetUrl() );
     615           0 :     return OUString();
     616             : }
     617             : 
     618          35 : void FormulaParserImpl::initializeImport( const CellAddress& rBaseAddr, FormulaType eType )
     619             : {
     620          35 :     maBaseAddr = rBaseAddr;
     621          35 :     mbRelativeAsOffset = mb2dRefsAs3dRefs = mbSpecialTokens = mbAllowNulChars = false;
     622          35 :     switch( eType )
     623             :     {
     624             :         case FORMULATYPE_CELL:
     625          34 :             mbSpecialTokens = true;
     626          34 :         break;
     627             :         case FORMULATYPE_ARRAY:
     628           0 :         break;
     629             :         case FORMULATYPE_SHAREDFORMULA:
     630           1 :             mbRelativeAsOffset = true;
     631           1 :         break;
     632             :         case FORMULATYPE_CONDFORMAT:
     633           0 :             mbRelativeAsOffset = true;
     634           0 :         break;
     635             :         case FORMULATYPE_VALIDATION:
     636           0 :             mbRelativeAsOffset = true;
     637             :             // enable NUL characters in BIFF import, string list is single tStr token with NUL separators
     638           0 :             mbAllowNulChars = getFilterType() == FILTER_BIFF;
     639           0 :         break;
     640             :         case FORMULATYPE_DEFINEDNAME:
     641           0 :             mbRelativeAsOffset = true;
     642             :             // BIFF2-BIFF4: convert 2D referebces to absolute 3D references
     643           0 :             mb2dRefsAs3dRefs = (getFilterType() == FILTER_BIFF) && (getBiff() <= BIFF4);
     644           0 :         break;
     645             :     }
     646             : 
     647          35 :     maTokenStorage.clear();
     648          35 :     maTokenIndexes.clear();
     649          35 :     maOperandSizeStack.clear();
     650          35 : }
     651             : 
     652          35 : ApiTokenSequence FormulaParserImpl::finalizeImport()
     653             : {
     654          35 :     ApiTokenSequence aTokens( static_cast< sal_Int32 >( maTokenIndexes.size() ) );
     655          35 :     if( aTokens.hasElements() )
     656             :     {
     657          35 :         ApiToken* pToken = aTokens.getArray();
     658          82 :         for( SizeTypeVector::const_iterator aIt = maTokenIndexes.begin(), aEnd = maTokenIndexes.end(); aIt != aEnd; ++aIt, ++pToken )
     659          47 :             *pToken = maTokenStorage[ *aIt ];
     660             :     }
     661          35 :     return finalizeTokenArray( aTokens );
     662             : }
     663             : 
     664             : // token array ----------------------------------------------------------------
     665             : 
     666          47 : bool FormulaParserImpl::resetSpaces()
     667             : {
     668          47 :     maLeadingSpaces.clear();
     669          47 :     maOpeningSpaces.clear();
     670          47 :     maClosingSpaces.clear();
     671          47 :     return true;
     672             : }
     673             : 
     674           0 : void FormulaParserImpl::appendSpaces( WhiteSpaceVec& orSpaces, sal_Int32 nCount, bool bLineFeed )
     675             : {
     676             :     OSL_ENSURE( nCount >= 0, "FormulaParserImpl::appendSpaces - negative count" );
     677           0 :     if( nCount > 0 )
     678           0 :         orSpaces.push_back( WhiteSpace( nCount, bLineFeed ) );
     679           0 : }
     680             : 
     681           0 : void FormulaParserImpl::appendLeadingSpaces( sal_Int32 nCount, bool bLineFeed )
     682             : {
     683           0 :     appendSpaces( maLeadingSpaces, nCount, bLineFeed );
     684           0 : }
     685             : 
     686           0 : void FormulaParserImpl::appendOpeningSpaces( sal_Int32 nCount, bool bLineFeed )
     687             : {
     688           0 :     appendSpaces( maOpeningSpaces, nCount, bLineFeed );
     689           0 : }
     690             : 
     691           0 : void FormulaParserImpl::appendClosingSpaces( sal_Int32 nCount, bool bLineFeed )
     692             : {
     693           0 :     appendSpaces( maClosingSpaces, nCount, bLineFeed );
     694           0 : }
     695             : 
     696          29 : size_t FormulaParserImpl::getFormulaSize() const
     697             : {
     698          29 :     return maTokenIndexes.size();
     699             : }
     700             : 
     701          41 : Any& FormulaParserImpl::appendRawToken( sal_Int32 nOpCode )
     702             : {
     703          41 :     maTokenIndexes.push_back( maTokenStorage.size() );
     704          41 :     return maTokenStorage.append( nOpCode );
     705             : }
     706             : 
     707           6 : Any& FormulaParserImpl::insertRawToken( sal_Int32 nOpCode, size_t nIndexFromEnd )
     708             : {
     709           6 :     maTokenIndexes.insert( maTokenIndexes.end() - nIndexFromEnd, maTokenStorage.size() );
     710           6 :     return maTokenStorage.append( nOpCode );
     711             : }
     712             : 
     713          41 : size_t FormulaParserImpl::appendWhiteSpaceTokens( const WhiteSpaceVec* pSpaces )
     714             : {
     715          41 :     if( pSpaces && !pSpaces->empty() )
     716           0 :         for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
     717           0 :             appendRawToken( OPCODE_SPACES ) <<= aIt->first;
     718          41 :     return pSpaces ? pSpaces->size() : 0;
     719             : }
     720             : 
     721           6 : size_t FormulaParserImpl::insertWhiteSpaceTokens( const WhiteSpaceVec* pSpaces, size_t nIndexFromEnd )
     722             : {
     723           6 :     if( pSpaces && !pSpaces->empty() )
     724           0 :         for( WhiteSpaceVec::const_iterator aIt = pSpaces->begin(), aEnd = pSpaces->end(); aIt != aEnd; ++aIt )
     725           0 :             insertRawToken( OPCODE_SPACES, nIndexFromEnd ) <<= aIt->first;
     726           6 :     return pSpaces ? pSpaces->size() : 0;
     727             : }
     728             : 
     729           0 : size_t FormulaParserImpl::getOperandSize( size_t nOpCountFromEnd, size_t nOpIndex ) const
     730             : {
     731             :     OSL_ENSURE( (nOpIndex < nOpCountFromEnd) && (nOpCountFromEnd <= maOperandSizeStack.size()),
     732             :         "FormulaParserImpl::getOperandSize - invalid parameters" );
     733           0 :     return maOperandSizeStack[ maOperandSizeStack.size() - nOpCountFromEnd + nOpIndex ];
     734             : }
     735             : 
     736          47 : void FormulaParserImpl::pushOperandSize( size_t nSize )
     737             : {
     738          47 :     maOperandSizeStack.push_back( nSize );
     739          47 : }
     740             : 
     741          12 : size_t FormulaParserImpl::popOperandSize()
     742             : {
     743             :     OSL_ENSURE( !maOperandSizeStack.empty(), "FormulaParserImpl::popOperandSize - invalid call" );
     744          12 :     size_t nOpSize = maOperandSizeStack.back();
     745          12 :     maOperandSizeStack.pop_back();
     746          12 :     return nOpSize;
     747             : }
     748             : 
     749           0 : ApiToken& FormulaParserImpl::getOperandToken( size_t nOpCountFromEnd, size_t nOpIndex, size_t nTokenIndex )
     750             : {
     751             :     SAL_WARN_IF(
     752             :         getOperandSize( nOpCountFromEnd, nOpIndex ) <= nTokenIndex, "sc.filter",
     753             :         "FormulaParserImpl::getOperandToken - invalid parameters" );
     754           0 :     SizeTypeVector::const_iterator aIndexIt = maTokenIndexes.end();
     755           0 :     for( SizeTypeVector::const_iterator aEnd = maOperandSizeStack.end(), aIt = aEnd - nOpCountFromEnd + nOpIndex; aIt != aEnd; ++aIt )
     756           0 :         aIndexIt -= *aIt;
     757           0 :     return maTokenStorage[ *(aIndexIt + nTokenIndex) ];
     758             : }
     759             : 
     760           0 : bool FormulaParserImpl::pushOperandToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     761             : {
     762           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     763           0 :     appendRawToken( nOpCode );
     764           0 :     pushOperandSize( nSpacesSize + 1 );
     765           0 :     return true;
     766             : }
     767             : 
     768           0 : bool FormulaParserImpl::pushAnyOperandToken( const Any& rAny, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     769             : {
     770           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     771           0 :     appendRawToken( nOpCode ) = rAny;
     772           0 :     pushOperandSize( nSpacesSize + 1 );
     773           0 :     return true;
     774             : }
     775             : 
     776             : template< typename Type >
     777          41 : bool FormulaParserImpl::pushValueOperandToken( const Type& rValue, sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     778             : {
     779          41 :     size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     780          41 :     appendRawToken( nOpCode ) <<= rValue;
     781          41 :     pushOperandSize( nSpacesSize + 1 );
     782          41 :     return true;
     783             : }
     784             : 
     785           0 : bool FormulaParserImpl::pushParenthesesOperandToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
     786             : {
     787           0 :     size_t nSpacesSize = appendWhiteSpaceTokens( pOpeningSpaces );
     788           0 :     appendRawToken( OPCODE_OPEN );
     789           0 :     nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
     790           0 :     appendRawToken( OPCODE_CLOSE );
     791           0 :     pushOperandSize( nSpacesSize + 2 );
     792           0 :     return true;
     793             : }
     794             : 
     795           0 : bool FormulaParserImpl::pushUnaryPreOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     796             : {
     797           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     798           0 :     if( bOk )
     799             :     {
     800           0 :         size_t nOpSize = popOperandSize();
     801           0 :         size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOpSize );
     802           0 :         insertRawToken( nOpCode, nOpSize );
     803           0 :         pushOperandSize( nOpSize + nSpacesSize + 1 );
     804             :     }
     805           0 :     return bOk;
     806             : }
     807             : 
     808           0 : bool FormulaParserImpl::pushUnaryPostOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     809             : {
     810           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     811           0 :     if( bOk )
     812             :     {
     813           0 :         size_t nOpSize = popOperandSize();
     814           0 :         size_t nSpacesSize = appendWhiteSpaceTokens( pSpaces );
     815           0 :         appendRawToken( nOpCode );
     816           0 :         pushOperandSize( nOpSize + nSpacesSize + 1 );
     817             :     }
     818           0 :     return bOk;
     819             : }
     820             : 
     821           6 : bool FormulaParserImpl::pushBinaryOperatorToken( sal_Int32 nOpCode, const WhiteSpaceVec* pSpaces )
     822             : {
     823           6 :     bool bOk = maOperandSizeStack.size() >= 2;
     824           6 :     if( bOk )
     825             :     {
     826           6 :         size_t nOp2Size = popOperandSize();
     827           6 :         size_t nOp1Size = popOperandSize();
     828           6 :         size_t nSpacesSize = insertWhiteSpaceTokens( pSpaces, nOp2Size );
     829           6 :         insertRawToken( nOpCode, nOp2Size );
     830           6 :         pushOperandSize( nOp1Size + nSpacesSize + 1 + nOp2Size );
     831             :     }
     832           6 :     return bOk;
     833             : }
     834             : 
     835           0 : bool FormulaParserImpl::pushParenthesesOperatorToken( const WhiteSpaceVec* pOpeningSpaces, const WhiteSpaceVec* pClosingSpaces )
     836             : {
     837           0 :     bool bOk = maOperandSizeStack.size() >= 1;
     838           0 :     if( bOk )
     839             :     {
     840           0 :         size_t nOpSize = popOperandSize();
     841           0 :         size_t nSpacesSize = insertWhiteSpaceTokens( pOpeningSpaces, nOpSize );
     842           0 :         insertRawToken( OPCODE_OPEN, nOpSize );
     843           0 :         nSpacesSize += appendWhiteSpaceTokens( pClosingSpaces );
     844           0 :         appendRawToken( OPCODE_CLOSE );
     845           0 :         pushOperandSize( nOpSize + nSpacesSize + 2 );
     846             :     }
     847           0 :     return bOk;
     848             : }
     849             : 
     850           0 : bool FormulaParserImpl::pushFunctionOperatorToken( sal_Int32 nOpCode, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
     851             : {
     852             :     /*  #i70925# if there are not enough tokens available on token stack, do
     853             :         not exit with error, but reduce parameter count. */
     854           0 :     nParamCount = ::std::min( maOperandSizeStack.size(), nParamCount );
     855             : 
     856             :     // convert all parameters on stack to a single operand separated with OPCODE_SEP
     857           0 :     bool bOk = true;
     858           0 :     for( size_t nParam = 1; bOk && (nParam < nParamCount); ++nParam )
     859           0 :         bOk = pushBinaryOperatorToken( OPCODE_SEP );
     860             : 
     861             :     // add function parentheses and function name
     862           0 :     return bOk &&
     863           0 :         ((nParamCount > 0) ? pushParenthesesOperatorToken( 0, pClosingSpaces ) : pushParenthesesOperandToken( 0, pClosingSpaces )) &&
     864           0 :         pushUnaryPreOperatorToken( nOpCode, pLeadingSpaces );
     865             : }
     866             : 
     867           0 : bool FormulaParserImpl::pushFunctionOperatorToken( const FunctionInfo& rFuncInfo, size_t nParamCount, const WhiteSpaceVec* pLeadingSpaces, const WhiteSpaceVec* pClosingSpaces )
     868             : {
     869           0 :     bool bOk = pushFunctionOperatorToken( rFuncInfo.mnApiOpCode, nParamCount, pLeadingSpaces, pClosingSpaces );
     870           0 :     if( bOk )
     871             :     {
     872             :        // create an external add-in call for the passed built-in function
     873           0 :         if( (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) && !rFuncInfo.maExtProgName.isEmpty() )
     874           0 :             getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maExtProgName;
     875             :         // create a bad token with unsupported function name
     876           0 :         else if( (rFuncInfo.mnApiOpCode == OPCODE_BAD) && !rFuncInfo.maOoxFuncName.isEmpty() )
     877           0 :             getOperandToken( 1, 0, 0 ).Data <<= rFuncInfo.maOoxFuncName;
     878             :     }
     879           0 :     return bOk;
     880             : }
     881             : 
     882           0 : bool FormulaParserImpl::pushOperand( sal_Int32 nOpCode )
     883             : {
     884           0 :     return pushOperandToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
     885             : }
     886             : 
     887           0 : bool FormulaParserImpl::pushAnyOperand( const Any& rAny, sal_Int32 nOpCode )
     888             : {
     889           0 :     return pushAnyOperandToken( rAny, nOpCode, &maLeadingSpaces ) && resetSpaces();
     890             : }
     891             : 
     892             : template< typename Type >
     893          41 : bool FormulaParserImpl::pushValueOperand( const Type& rValue, sal_Int32 nOpCode )
     894             : {
     895          41 :     return pushValueOperandToken( rValue, nOpCode, &maLeadingSpaces ) && resetSpaces();
     896             : }
     897             : 
     898           0 : bool FormulaParserImpl::pushBoolOperand( bool bValue )
     899             : {
     900           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( bValue ? BIFF_FUNC_TRUE : BIFF_FUNC_FALSE ) )
     901           0 :         return pushFunctionOperator( pFuncInfo->mnApiOpCode, 0 );
     902           0 :     return pushValueOperand< double >( bValue ? 1.0 : 0.0 );
     903             : }
     904             : 
     905           0 : bool FormulaParserImpl::pushErrorOperand( double fEncodedError )
     906             : {
     907             :     // HACK: enclose all error codes into an 1x1 matrix
     908             :     // start token array with opening brace and leading spaces
     909           0 :     pushOperand( OPCODE_ARRAY_OPEN );
     910           0 :     size_t nOpSize = popOperandSize();
     911           0 :     size_t nOldArraySize = maTokenIndexes.size();
     912             :     // push a double containing the Calc error code
     913           0 :     appendRawToken( OPCODE_PUSH ) <<= fEncodedError;
     914             :     // close token array and set resulting operand size
     915           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
     916           0 :     pushOperandSize( nOpSize + maTokenIndexes.size() - nOldArraySize );
     917           0 :     return true;
     918             : }
     919             : 
     920           0 : bool FormulaParserImpl::pushBiffBoolOperand( sal_uInt8 nValue )
     921             : {
     922           0 :     return pushBoolOperand( nValue != BIFF_TOK_BOOL_FALSE );
     923             : }
     924             : 
     925           0 : bool FormulaParserImpl::pushBiffErrorOperand( sal_uInt8 nErrorCode )
     926             : {
     927           0 :     return pushErrorOperand( BiffHelper::calcDoubleFromError( nErrorCode ) );
     928             : }
     929             : 
     930           2 : bool FormulaParserImpl::pushReferenceOperand( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     931             : {
     932           2 :     SingleReference aApiRef;
     933           2 :     convertReference2d( aApiRef, rRef, bDeleted, bRelativeAsOffset );
     934           2 :     return pushValueOperand( aApiRef );
     935             : }
     936             : 
     937           0 : bool FormulaParserImpl::pushReferenceOperand( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     938             : {
     939           0 :     ComplexReference aApiRef;
     940           0 :     convertReference2d( aApiRef, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
     941           0 :     return pushValueOperand( aApiRef );
     942             : }
     943             : 
     944             : template< typename Type >
     945           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const Type& rApiRef )
     946             : {
     947           0 :     if( rSheetRange.isExternal() )
     948             :     {
     949           0 :         ExternalReference aApiExtRef;
     950           0 :         aApiExtRef.Index = rSheetRange.getDocLinkIndex();
     951           0 :         aApiExtRef.Reference <<= rApiRef;
     952           0 :         return pushValueOperand( aApiExtRef );
     953             :     }
     954           0 :     return pushValueOperand( rApiRef );
     955             : }
     956             : 
     957           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     958             : {
     959           0 :     if( rSheetRange.is3dRange() )
     960             :     {
     961             :         // single-cell-range over several sheets, needs to create a ComplexReference
     962           0 :         ComplexReference aApiRef;
     963           0 :         convertReference3d( aApiRef, rSheetRange, rRef, rRef, bDeleted, bRelativeAsOffset );
     964           0 :         return pushReferenceOperand( rSheetRange, aApiRef );
     965             :     }
     966           0 :     SingleReference aApiRef;
     967           0 :     convertReference3d( aApiRef, rSheetRange.getFirstSheet(), rSheetRange.isSameSheet(), rRef, bDeleted, bRelativeAsOffset );
     968           0 :     return pushReferenceOperand( rSheetRange, aApiRef );
     969             : }
     970             : 
     971           0 : bool FormulaParserImpl::pushReferenceOperand( const LinkSheetRange& rSheetRange, const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
     972             : {
     973           0 :     ComplexReference aApiRef;
     974           0 :     convertReference3d( aApiRef, rSheetRange, rRef.maRef1, rRef.maRef2, bDeleted, bRelativeAsOffset );
     975           0 :     return pushReferenceOperand( rSheetRange, aApiRef );
     976             : }
     977             : 
     978           0 : bool FormulaParserImpl::pushNlrOperand( const BinSingleRef2d& rRef )
     979             : {
     980           0 :     SingleReference aApiRef;
     981           0 :     convertReference2d( aApiRef, rRef, false, false );
     982           0 :     return pushValueOperand( aApiRef, OPCODE_NLR );
     983             : }
     984             : 
     985           0 : bool FormulaParserImpl::pushEmbeddedRefOperand( const DefinedNameBase& rName, bool bPushBadToken )
     986             : {
     987           0 :     Any aRefAny = rName.getReference( maBaseAddr );
     988           0 :     if( aRefAny.hasValue() )
     989           0 :         return pushAnyOperand( aRefAny, OPCODE_PUSH );
     990           0 :     if( bPushBadToken && !rName.getModelName().isEmpty() && (rName.getModelName()[ 0 ] >= ' ') )
     991           0 :         return pushValueOperand( rName.getModelName(), OPCODE_BAD );
     992           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
     993             : }
     994             : 
     995           0 : bool FormulaParserImpl::pushDefinedNameOperand( const DefinedNameRef& rxDefName )
     996             : {
     997           0 :     if( !rxDefName || rxDefName->getModelName().isEmpty() )
     998           0 :         return pushBiffErrorOperand( BIFF_ERR_NAME );
     999           0 :     if( rxDefName->isMacroFunction() )
    1000           0 :         return pushValueOperand( rxDefName->getModelName(), OPCODE_MACRO );
    1001           0 :     if( rxDefName->getTokenIndex() >= 0 )
    1002           0 :         return pushValueOperand( rxDefName->getTokenIndex(), OPCODE_NAME );
    1003           0 :     return pushEmbeddedRefOperand( *rxDefName, true );
    1004             : }
    1005             : 
    1006           0 : bool FormulaParserImpl::pushExternalFuncOperand( const FunctionInfo& rFuncInfo )
    1007             : {
    1008           0 :     return (rFuncInfo.mnApiOpCode == OPCODE_EXTERNAL) ?
    1009           0 :         pushValueOperand( rFuncInfo.maExtProgName, OPCODE_EXTERNAL ) :
    1010           0 :         pushOperand( rFuncInfo.mnApiOpCode );
    1011             : }
    1012             : 
    1013           0 : bool FormulaParserImpl::pushDdeLinkOperand( const OUString& rDdeServer, const OUString& rDdeTopic, const OUString& rDdeItem )
    1014             : {
    1015             :     // create the function call DDE("server";"topic";"item")
    1016             :     return
    1017           0 :         pushValueOperandToken( rDdeServer ) &&
    1018           0 :         pushValueOperandToken( rDdeTopic ) &&
    1019           0 :         pushValueOperandToken( rDdeItem ) &&
    1020           0 :         pushFunctionOperator( OPCODE_DDE, 3 );
    1021             : }
    1022             : 
    1023           0 : bool FormulaParserImpl::pushExternalNameOperand( const ExternalNameRef& rxExtName, const ExternalLink& rExtLink )
    1024             : {
    1025           0 :     if( rxExtName.get() ) switch( rExtLink.getLinkType() )
    1026             :     {
    1027             :         case LINKTYPE_INTERNAL:
    1028             :         case LINKTYPE_EXTERNAL:
    1029           0 :             return pushEmbeddedRefOperand( *rxExtName, false );
    1030             : 
    1031             :         case LINKTYPE_ANALYSIS:
    1032             :             // TODO: need support for localized addin function names
    1033           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
    1034           0 :                 return pushExternalFuncOperand( *pFuncInfo );
    1035           0 :         break;
    1036             : 
    1037             :         case LINKTYPE_LIBRARY:
    1038           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( rxExtName->getUpcaseModelName() ) )
    1039           0 :                 if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == rExtLink.getFuncLibraryType()) )
    1040           0 :                     return pushExternalFuncOperand( *pFuncInfo );
    1041           0 :         break;
    1042             : 
    1043             :         case LINKTYPE_DDE:
    1044             :         {
    1045           0 :             OUString aDdeServer, aDdeTopic, aDdeItem;
    1046           0 :             if( rxExtName->getDdeLinkData( aDdeServer, aDdeTopic, aDdeItem ) )
    1047           0 :                 return pushDdeLinkOperand( aDdeServer, aDdeTopic, aDdeItem );
    1048             :         }
    1049           0 :         break;
    1050             : 
    1051             :         default:
    1052             :             OSL_ENSURE( rExtLink.getLinkType() != LINKTYPE_SELF, "FormulaParserImpl::pushExternalNameOperand - invalid call" );
    1053             :     }
    1054           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    1055             : }
    1056             : 
    1057          29 : bool FormulaParserImpl::pushSpecialTokenOperand( const BinAddress& rBaseAddr, bool bTable )
    1058             : {
    1059          29 :     CellAddress aBaseAddr( maBaseAddr.Sheet, rBaseAddr.mnCol, rBaseAddr.mnRow );
    1060          29 :     ApiSpecialTokenInfo aTokenInfo( aBaseAddr, bTable );
    1061          29 :     return mbSpecialTokens && (getFormulaSize() == 0) && pushValueOperand( aTokenInfo, OPCODE_BAD );
    1062             : }
    1063             : 
    1064           0 : bool FormulaParserImpl::pushUnaryPreOperator( sal_Int32 nOpCode )
    1065             : {
    1066           0 :     return pushUnaryPreOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1067             : }
    1068             : 
    1069           0 : bool FormulaParserImpl::pushUnaryPostOperator( sal_Int32 nOpCode )
    1070             : {
    1071           0 :     return pushUnaryPostOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1072             : }
    1073             : 
    1074           6 : bool FormulaParserImpl::pushBinaryOperator( sal_Int32 nOpCode )
    1075             : {
    1076           6 :     return pushBinaryOperatorToken( nOpCode, &maLeadingSpaces ) && resetSpaces();
    1077             : }
    1078             : 
    1079           0 : bool FormulaParserImpl::pushParenthesesOperator()
    1080             : {
    1081           0 :     return pushParenthesesOperatorToken( &maOpeningSpaces, &maClosingSpaces ) && resetSpaces();
    1082             : }
    1083             : 
    1084           0 : bool FormulaParserImpl::pushFunctionOperator( sal_Int32 nOpCode, size_t nParamCount )
    1085             : {
    1086           0 :     return pushFunctionOperatorToken( nOpCode, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
    1087             : }
    1088             : 
    1089           0 : bool FormulaParserImpl::pushFunctionOperator( const FunctionInfo& rFuncInfo, size_t nParamCount )
    1090             : {
    1091           0 :     return pushFunctionOperatorToken( rFuncInfo, nParamCount, &maLeadingSpaces, &maClosingSpaces ) && resetSpaces();
    1092             : }
    1093             : 
    1094             : // reference conversion -------------------------------------------------------
    1095             : 
    1096           2 : void FormulaParserImpl::initReference2d( SingleReference& orApiRef ) const
    1097             : {
    1098           2 :     if( mb2dRefsAs3dRefs )
    1099             :     {
    1100           0 :         initReference3d( orApiRef, maBaseAddr.Sheet, false );
    1101             :     }
    1102             :     else
    1103             :     {
    1104           2 :         orApiRef.Flags = SHEET_RELATIVE;
    1105             :         // #i10184# absolute sheet index needed for relative references in shared formulas
    1106           2 :         orApiRef.Sheet = maBaseAddr.Sheet;
    1107           2 :         orApiRef.RelativeSheet = 0;
    1108             :     }
    1109           2 : }
    1110             : 
    1111           0 : void FormulaParserImpl::initReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet )
    1112             : {
    1113           0 :     orApiRef.Flags = SHEET_3D;
    1114           0 :     if( nSheet < 0 )
    1115             :     {
    1116           0 :         orApiRef.Sheet = 0;
    1117           0 :         orApiRef.Flags |= SHEET_DELETED;
    1118             :     }
    1119           0 :     else if( bSameSheet )
    1120             :     {
    1121             :         OSL_ENSURE( nSheet == 0, "FormulaParserImpl::initReference3d - invalid sheet index" );
    1122           0 :         orApiRef.Flags |= SHEET_RELATIVE;
    1123           0 :         orApiRef.RelativeSheet = 0;
    1124             :     }
    1125             :     else
    1126             :     {
    1127           0 :         orApiRef.Sheet = nSheet;
    1128             :     }
    1129           0 : }
    1130             : 
    1131           2 : void FormulaParserImpl::convertReference( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1132             : {
    1133           2 :     if( bDeleted )
    1134             :     {
    1135           0 :         orApiRef.Column = 0;
    1136           0 :         orApiRef.Row = 0;
    1137             :         // no explicit information about whether row or column is deleted
    1138           0 :         orApiRef.Flags |= COLUMN_DELETED | ROW_DELETED;
    1139             :     }
    1140             :     else
    1141             :     {
    1142             :         // column/row indexes and flags
    1143           2 :         setFlag( orApiRef.Flags, COLUMN_RELATIVE, rRef.mbColRel );
    1144           2 :         setFlag( orApiRef.Flags, ROW_RELATIVE, rRef.mbRowRel );
    1145           2 :         (rRef.mbColRel ? orApiRef.RelativeColumn : orApiRef.Column) = rRef.mnCol;
    1146           2 :         (rRef.mbRowRel ? orApiRef.RelativeRow : orApiRef.Row) = rRef.mnRow;
    1147             :         // convert absolute indexes to relative offsets used in API
    1148           2 :         if( !bRelativeAsOffset )
    1149             :         {
    1150           2 :             if( rRef.mbColRel )
    1151           2 :                 orApiRef.RelativeColumn -= maBaseAddr.Column;
    1152           2 :             if( rRef.mbRowRel )
    1153           2 :                 orApiRef.RelativeRow -= maBaseAddr.Row;
    1154             :         }
    1155             :     }
    1156           2 : }
    1157             : 
    1158           0 : void FormulaParserImpl::convertReference( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1159             : {
    1160           0 :     convertReference( orApiRef.Reference1, rRef1, bDeleted, bRelativeAsOffset );
    1161           0 :     convertReference( orApiRef.Reference2, rRef2, bDeleted, bRelativeAsOffset );
    1162             :     /*  Handle references to complete rows or columns (e.g. $1:$2 or C:D),
    1163             :         need to expand or shrink to limits of own document. */
    1164           0 :     if( !bDeleted && !rRef1.mbColRel && !rRef2.mbColRel && (orApiRef.Reference1.Column == 0) && (orApiRef.Reference2.Column == mnMaxXlsCol) )
    1165           0 :         orApiRef.Reference2.Column = mnMaxApiCol;
    1166           0 :     if( !bDeleted && !rRef1.mbRowRel && !rRef2.mbRowRel && (orApiRef.Reference1.Row == 0) && (orApiRef.Reference2.Row == mnMaxXlsRow) )
    1167           0 :         orApiRef.Reference2.Row = mnMaxApiRow;
    1168           0 : }
    1169             : 
    1170           2 : void FormulaParserImpl::convertReference2d( SingleReference& orApiRef, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1171             : {
    1172           2 :     initReference2d( orApiRef );
    1173           2 :     convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
    1174           2 : }
    1175             : 
    1176           0 : void FormulaParserImpl::convertReference2d( ComplexReference& orApiRef, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1177             : {
    1178           0 :     initReference2d( orApiRef.Reference1 );
    1179           0 :     initReference2d( orApiRef.Reference2 );
    1180           0 :     convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
    1181             :     // remove sheet name from second part of reference
    1182           0 :     setFlag( orApiRef.Reference2.Flags, SHEET_3D, false );
    1183           0 : }
    1184             : 
    1185           0 : void FormulaParserImpl::convertReference3d( SingleReference& orApiRef, sal_Int32 nSheet, bool bSameSheet, const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset ) const
    1186             : {
    1187           0 :     initReference3d( orApiRef, nSheet, bSameSheet );
    1188           0 :     convertReference( orApiRef, rRef, bDeleted, bRelativeAsOffset );
    1189           0 : }
    1190             : 
    1191           0 : void FormulaParserImpl::convertReference3d( ComplexReference& orApiRef, const LinkSheetRange& rSheetRange, const BinSingleRef2d& rRef1, const BinSingleRef2d& rRef2, bool bDeleted, bool bRelativeAsOffset ) const
    1192             : {
    1193           0 :     bool bSameSheet = rSheetRange.isSameSheet();
    1194           0 :     initReference3d( orApiRef.Reference1, rSheetRange.getFirstSheet(), bSameSheet );
    1195           0 :     initReference3d( orApiRef.Reference2, rSheetRange.getLastSheet(), bSameSheet );
    1196           0 :     convertReference( orApiRef, rRef1, rRef2, bDeleted, bRelativeAsOffset );
    1197             :     // remove sheet name from second part of reference
    1198           0 :     setFlag( orApiRef.Reference2.Flags, SHEET_3D, rSheetRange.is3dRange() );
    1199           0 : }
    1200             : 
    1201             : // finalize token sequence ----------------------------------------------------
    1202             : 
    1203           0 : const FunctionInfo* FormulaParserImpl::resolveBadFuncName( const OUString& rTokenData ) const
    1204             : {
    1205             :     /*  Try to parse calls to library functions. The format of such a function
    1206             :         call is "[n]!funcname", n>0 being the link identifier of the function
    1207             :         library spreadsheet file. */
    1208           0 :     sal_Int32 nBracketOpen = rTokenData.indexOf( '[' );
    1209           0 :     sal_Int32 nBracketClose = rTokenData.indexOf( ']' );
    1210           0 :     sal_Int32 nExclamation = rTokenData.indexOf( '!' );
    1211           0 :     if( (0 == nBracketOpen) && (nBracketOpen + 1 < nBracketClose) && (nBracketClose + 1 == nExclamation) && (nExclamation + 1 < rTokenData.getLength()) )
    1212             :     {
    1213           0 :         sal_Int32 nRefId = rTokenData.copy( nBracketOpen + 1, nBracketClose - nBracketOpen - 1 ).toInt32();
    1214           0 :         const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get();
    1215           0 :         if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_LIBRARY) )
    1216             :         {
    1217           0 :             OUString aFuncName = rTokenData.copy( nExclamation + 1 ).toAsciiUpperCase();
    1218           0 :             if( const FunctionInfo* pFuncInfo = getFuncInfoFromOoxFuncName( aFuncName ) )
    1219           0 :                 if( (pFuncInfo->meFuncLibType != FUNCLIB_UNKNOWN) && (pFuncInfo->meFuncLibType == pExtLink->getFuncLibraryType()) )
    1220           0 :                     return pFuncInfo;
    1221             :         }
    1222             :     }
    1223           0 :     return 0;
    1224             : }
    1225             : 
    1226           0 : OUString FormulaParserImpl::resolveDefinedName( sal_Int32 nTokenIndex ) const
    1227             : {
    1228           0 :     if( const DefinedName* pDefName = getDefinedNames().getByTokenIndex( nTokenIndex ).get() )
    1229           0 :         return pDefName->getCalcName();
    1230           0 :     return OUString();
    1231             : }
    1232             : 
    1233             : // OOXML/BIFF12 parser implementation =========================================
    1234             : 
    1235         828 : class OoxFormulaParserImpl : public FormulaParserImpl
    1236             : {
    1237             : public:
    1238             :     explicit            OoxFormulaParserImpl( const FormulaParser& rParent );
    1239             : 
    1240             :     virtual ApiTokenSequence importOoxFormula(
    1241             :                             const CellAddress& rBaseAddr,
    1242             :                             const OUString& rFormulaString ) SAL_OVERRIDE;
    1243             : 
    1244             :     virtual ApiTokenSequence importBiff12Formula(
    1245             :                             const CellAddress& rBaseAddr,
    1246             :                             FormulaType eType,
    1247             :                             SequenceInputStream& rStrm ) SAL_OVERRIDE;
    1248             : 
    1249             : private:
    1250             :     // import token contents and create API formula token ---------------------
    1251             : 
    1252             :     bool                importAttrToken( SequenceInputStream& rStrm );
    1253             :     bool                importSpaceToken( SequenceInputStream& rStrm );
    1254             :     bool                importTableToken( SequenceInputStream& rStrm );
    1255             :     bool                importArrayToken( SequenceInputStream& rStrm );
    1256             :     bool                importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1257             :     bool                importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1258             :     bool                importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1259             :     bool                importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1260             :     bool                importMemAreaToken( SequenceInputStream& rStrm, bool bAddData );
    1261             :     bool                importMemFuncToken( SequenceInputStream& rStrm );
    1262             :     bool                importNameToken( SequenceInputStream& rStrm );
    1263             :     bool                importNameXToken( SequenceInputStream& rStrm );
    1264             :     bool                importFuncToken( SequenceInputStream& rStrm );
    1265             :     bool                importFuncVarToken( SequenceInputStream& rStrm );
    1266             :     bool                importExpToken( SequenceInputStream& rStrm );
    1267             : 
    1268             :     LinkSheetRange      readSheetRange( SequenceInputStream& rStrm );
    1269             : 
    1270             :     void                swapStreamPosition( SequenceInputStream& rStrm );
    1271             :     void                skipMemAreaAddData( SequenceInputStream& rStrm );
    1272             : 
    1273             :     // convert BIN token and push API operand or operator ---------------------
    1274             : 
    1275             :     bool                pushBiff12Name( sal_Int32 nNameId );
    1276             :     bool                pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId );
    1277             :     bool                pushBiff12Function( sal_uInt16 nFuncId );
    1278             :     bool                pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
    1279             : 
    1280             : private:
    1281             :     ApiParserWrapper    maApiParser;        /// Wrapper for the API formula parser object.
    1282             :     sal_Int64           mnAddDataPos;       /// Current stream position for additional data (tExp, tArray, tMemArea).
    1283             :     bool                mbNeedExtRefs;      /// True = parser needs initialization of external reference info.
    1284             : };
    1285             : 
    1286         414 : OoxFormulaParserImpl::OoxFormulaParserImpl( const FormulaParser& rParent ) :
    1287             :     FormulaParserImpl( rParent ),
    1288         414 :     maApiParser( rParent.getBaseFilter().getModelFactory(), rParent ),
    1289             :     mnAddDataPos( 0 ),
    1290         828 :     mbNeedExtRefs( true )
    1291             : {
    1292         414 : }
    1293             : 
    1294         272 : ApiTokenSequence OoxFormulaParserImpl::importOoxFormula( const CellAddress& rBaseAddr, const OUString& rFormulaString )
    1295             : {
    1296         272 :     if( mbNeedExtRefs )
    1297             :     {
    1298          72 :         maApiParser.getParserProperties().setProperty( PROP_ExternalLinks, getExternalLinks().getLinkInfos() );
    1299          72 :         mbNeedExtRefs = false;
    1300             :     }
    1301         272 :     return finalizeTokenArray( maApiParser.parseFormula( rFormulaString, rBaseAddr ) );
    1302             : }
    1303             : 
    1304          35 : ApiTokenSequence OoxFormulaParserImpl::importBiff12Formula( const CellAddress& rBaseAddr, FormulaType eType, SequenceInputStream& rStrm )
    1305             : {
    1306          35 :     initializeImport( rBaseAddr, eType );
    1307             : 
    1308          35 :     sal_Int32 nFmlaSize = rStrm.readInt32();
    1309          35 :     sal_Int64 nFmlaPos = rStrm.tell();
    1310          35 :     sal_Int64 nFmlaEndPos = nFmlaPos + nFmlaSize;
    1311             : 
    1312          35 :     rStrm.seek( nFmlaEndPos );
    1313          35 :     sal_Int32 nAddDataSize = rStrm.readInt32();
    1314          35 :     mnAddDataPos = rStrm.tell();
    1315          35 :     sal_Int64 nAddDataEndPos = mnAddDataPos + nAddDataSize;
    1316          35 :     rStrm.seek( nFmlaPos );
    1317             : 
    1318          35 :     bool bOk = (nFmlaSize >= 0) && (nAddDataSize >= 0);
    1319          35 :     bool bRelativeAsOffset = mbRelativeAsOffset;
    1320             : 
    1321         117 :     while( bOk && !rStrm.isEof() && (rStrm.tell() < nFmlaEndPos) )
    1322             :     {
    1323             :         sal_uInt8 nTokenId;
    1324          47 :         nTokenId = rStrm.readuChar();
    1325          47 :         sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
    1326          47 :         sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
    1327             : 
    1328          47 :         if( nTokenClass == BIFF_TOKCLASS_NONE )
    1329             :         {
    1330             :             // base tokens
    1331          45 :             switch( nBaseId )
    1332             :             {
    1333          29 :                 case BIFF_TOKID_EXP:        bOk = importExpToken( rStrm );                                      break;
    1334           4 :                 case BIFF_TOKID_ADD:        bOk = pushBinaryOperator( OPCODE_ADD );                             break;
    1335           1 :                 case BIFF_TOKID_SUB:        bOk = pushBinaryOperator( OPCODE_SUB );                             break;
    1336           1 :                 case BIFF_TOKID_MUL:        bOk = pushBinaryOperator( OPCODE_MULT );                            break;
    1337           0 :                 case BIFF_TOKID_DIV:        bOk = pushBinaryOperator( OPCODE_DIV );                             break;
    1338           0 :                 case BIFF_TOKID_POWER:      bOk = pushBinaryOperator( OPCODE_POWER );                           break;
    1339           0 :                 case BIFF_TOKID_CONCAT:     bOk = pushBinaryOperator( OPCODE_CONCAT );                          break;
    1340           0 :                 case BIFF_TOKID_LT:         bOk = pushBinaryOperator( OPCODE_LESS );                            break;
    1341           0 :                 case BIFF_TOKID_LE:         bOk = pushBinaryOperator( OPCODE_LESS_EQUAL );                      break;
    1342           0 :                 case BIFF_TOKID_EQ:         bOk = pushBinaryOperator( OPCODE_EQUAL );                           break;
    1343           0 :                 case BIFF_TOKID_GE:         bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL );                   break;
    1344           0 :                 case BIFF_TOKID_GT:         bOk = pushBinaryOperator( OPCODE_GREATER );                         break;
    1345           0 :                 case BIFF_TOKID_NE:         bOk = pushBinaryOperator( OPCODE_NOT_EQUAL );                       break;
    1346           0 :                 case BIFF_TOKID_ISECT:      bOk = pushBinaryOperator( OPCODE_INTERSECT );                       break;
    1347           0 :                 case BIFF_TOKID_LIST:       bOk = pushBinaryOperator( OPCODE_LIST );                            break;
    1348           0 :                 case BIFF_TOKID_RANGE:      bOk = pushBinaryOperator( OPCODE_RANGE );                           break;
    1349           0 :                 case BIFF_TOKID_UPLUS:      bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN );                     break;
    1350           0 :                 case BIFF_TOKID_UMINUS:     bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN );                    break;
    1351           0 :                 case BIFF_TOKID_PERCENT:    bOk = pushUnaryPostOperator( OPCODE_PERCENT );                      break;
    1352           0 :                 case BIFF_TOKID_PAREN:      bOk = pushParenthesesOperator();                                    break;
    1353           0 :                 case BIFF_TOKID_MISSARG:    bOk = pushOperand( OPCODE_MISSING );                                break;
    1354           0 :                 case BIFF_TOKID_STR:        bOk = pushValueOperand( BiffHelper::readString( rStrm, false ) );   break;
    1355           0 :                 case BIFF_TOKID_NLR:        bOk = importTableToken( rStrm );                                    break;
    1356           0 :                 case BIFF_TOKID_ATTR:       bOk = importAttrToken( rStrm );                                     break;
    1357           0 :                 case BIFF_TOKID_ERR:        bOk = pushBiffErrorOperand( rStrm.readuInt8() );                    break;
    1358           0 :                 case BIFF_TOKID_BOOL:       bOk = pushBiffBoolOperand( rStrm.readuInt8() );                     break;
    1359          10 :                 case BIFF_TOKID_INT:        bOk = pushValueOperand< double >( rStrm.readuInt16() );             break;
    1360           0 :                 case BIFF_TOKID_NUM:        bOk = pushValueOperand( rStrm.readDouble() );                       break;
    1361           0 :                 default:                    bOk = false;
    1362             :             }
    1363             :         }
    1364             :         else
    1365             :         {
    1366             :             // classified tokens
    1367           2 :             switch( nBaseId )
    1368             :             {
    1369           0 :                 case BIFF_TOKID_ARRAY:      bOk = importArrayToken( rStrm );                            break;
    1370           0 :                 case BIFF_TOKID_FUNC:       bOk = importFuncToken( rStrm );                             break;
    1371           0 :                 case BIFF_TOKID_FUNCVAR:    bOk = importFuncVarToken( rStrm );                          break;
    1372           0 :                 case BIFF_TOKID_NAME:       bOk = importNameToken( rStrm );                             break;
    1373           2 :                 case BIFF_TOKID_REF:        bOk = importRefToken( rStrm, false, false );                break;
    1374           0 :                 case BIFF_TOKID_AREA:       bOk = importAreaToken( rStrm, false, false );               break;
    1375           0 :                 case BIFF_TOKID_MEMAREA:    bOk = importMemAreaToken( rStrm, true );                    break;
    1376           0 :                 case BIFF_TOKID_MEMERR:     bOk = importMemAreaToken( rStrm, false );                   break;
    1377           0 :                 case BIFF_TOKID_MEMNOMEM:   bOk = importMemAreaToken( rStrm, false );                   break;
    1378           0 :                 case BIFF_TOKID_MEMFUNC:    bOk = importMemFuncToken( rStrm );                          break;
    1379           0 :                 case BIFF_TOKID_REFERR:     bOk = importRefToken( rStrm, true, false );                 break;
    1380           0 :                 case BIFF_TOKID_AREAERR:    bOk = importAreaToken( rStrm, true, false );                break;
    1381           0 :                 case BIFF_TOKID_REFN:       bOk = importRefToken( rStrm, false, true );                 break;
    1382           0 :                 case BIFF_TOKID_AREAN:      bOk = importAreaToken( rStrm, false, true );                break;
    1383           0 :                 case BIFF_TOKID_MEMAREAN:   bOk = importMemFuncToken( rStrm );                          break;
    1384           0 :                 case BIFF_TOKID_MEMNOMEMN:  bOk = importMemFuncToken( rStrm );                          break;
    1385           0 :                 case BIFF_TOKID_NAMEX:      bOk = importNameXToken( rStrm );                            break;
    1386           0 :                 case BIFF_TOKID_REF3D:      bOk = importRef3dToken( rStrm, false, bRelativeAsOffset );  break;
    1387           0 :                 case BIFF_TOKID_AREA3D:     bOk = importArea3dToken( rStrm, false, bRelativeAsOffset ); break;
    1388           0 :                 case BIFF_TOKID_REFERR3D:   bOk = importRef3dToken( rStrm, true, bRelativeAsOffset );   break;
    1389           0 :                 case BIFF_TOKID_AREAERR3D:  bOk = importArea3dToken( rStrm, true, bRelativeAsOffset );  break;
    1390           0 :                 default:                    bOk = false;
    1391             :             }
    1392             :         }
    1393             :     }
    1394             : 
    1395             :     // build and finalize the token sequence
    1396          35 :     ApiTokenSequence aFinalTokens;
    1397          35 :     if( bOk && (rStrm.tell() == nFmlaEndPos) && (mnAddDataPos == nAddDataEndPos) )
    1398          35 :         aFinalTokens = finalizeImport();
    1399             : 
    1400             :     // seek behind token array
    1401          35 :     if( (nFmlaSize >= 0) && (nAddDataSize >= 0) )
    1402          35 :         rStrm.seek( nAddDataEndPos );
    1403             : 
    1404             :     // return the final token sequence
    1405          35 :     return aFinalTokens;
    1406             : }
    1407             : 
    1408             : // import token contents and create API formula token -------------------------
    1409             : 
    1410           0 : bool OoxFormulaParserImpl::importAttrToken( SequenceInputStream& rStrm )
    1411             : {
    1412           0 :     bool bOk = true;
    1413             :     sal_uInt8 nType;
    1414           0 :     nType = rStrm.readuChar();
    1415             :     // equal flags in all BIFFs
    1416           0 :     switch( nType )
    1417             :     {
    1418             :         case 0:     // sometimes, tAttrSkip tokens miss the type flag
    1419             :         case BIFF_TOK_ATTR_VOLATILE:
    1420             :         case BIFF_TOK_ATTR_IF:
    1421             :         case BIFF_TOK_ATTR_SKIP:
    1422             :         case BIFF_TOK_ATTR_ASSIGN:
    1423             :         case BIFF_TOK_ATTR_IFERROR:
    1424           0 :             rStrm.skip( 2 );
    1425           0 :         break;
    1426             :         case BIFF_TOK_ATTR_CHOOSE:
    1427           0 :             rStrm.skip( 2 * rStrm.readuInt16() + 2 );
    1428           0 :         break;
    1429             :         case BIFF_TOK_ATTR_SUM:
    1430           0 :             rStrm.skip( 2 );
    1431           0 :             bOk = pushBiff12Function( BIFF_FUNC_SUM, 1 );
    1432           0 :         break;
    1433             :         case BIFF_TOK_ATTR_SPACE:
    1434             :         case BIFF_TOK_ATTR_SPACE_VOLATILE:
    1435           0 :             bOk = importSpaceToken( rStrm );
    1436           0 :         break;
    1437             :         default:
    1438           0 :             bOk = false;
    1439             :     }
    1440           0 :     return bOk;
    1441             : }
    1442             : 
    1443           0 : bool OoxFormulaParserImpl::importSpaceToken( SequenceInputStream& rStrm )
    1444             : {
    1445             :     // equal constants in BIFF and OOX
    1446             :     sal_uInt8 nType, nCount;
    1447           0 :     nType = rStrm.readuChar();
    1448           0 :     nCount = rStrm.readuChar();
    1449           0 :     switch( nType )
    1450             :     {
    1451             :         case BIFF_TOK_ATTR_SPACE_SP:
    1452           0 :             appendLeadingSpaces( nCount, false );
    1453           0 :         break;
    1454             :         case BIFF_TOK_ATTR_SPACE_BR:
    1455           0 :             appendLeadingSpaces( nCount, true );
    1456           0 :         break;
    1457             :         case BIFF_TOK_ATTR_SPACE_SP_OPEN:
    1458           0 :             appendOpeningSpaces( nCount, false );
    1459           0 :         break;
    1460             :         case BIFF_TOK_ATTR_SPACE_BR_OPEN:
    1461           0 :             appendOpeningSpaces( nCount, true );
    1462           0 :         break;
    1463             :         case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
    1464           0 :             appendClosingSpaces( nCount, false );
    1465           0 :         break;
    1466             :         case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
    1467           0 :             appendClosingSpaces( nCount, true );
    1468           0 :         break;
    1469             :     }
    1470           0 :     return true;
    1471             : }
    1472             : 
    1473           0 : bool OoxFormulaParserImpl::importTableToken( SequenceInputStream& rStrm )
    1474             : {
    1475             :     sal_uInt16 nFlags, nTableId, nCol1, nCol2;
    1476           0 :     rStrm.skip( 3 );
    1477           0 :     nFlags = rStrm.readuInt16();
    1478           0 :     nTableId = rStrm.readuInt16();
    1479           0 :     rStrm.skip( 2 );
    1480           0 :     nCol1 = rStrm.readuInt16();
    1481           0 :     nCol2 = rStrm.readuInt16();
    1482           0 :     TableRef xTable = getTables().getTable( nTableId );
    1483           0 :     sal_Int32 nTokenIndex = xTable.get() ? xTable->getTokenIndex() : -1;
    1484           0 :     if( nTokenIndex >= 0 )
    1485             :     {
    1486           0 :         sal_Int32 nWidth = xTable->getWidth();
    1487           0 :         sal_Int32 nHeight = xTable->getHeight();
    1488           0 :         sal_Int32 nStartCol = 0;
    1489           0 :         sal_Int32 nEndCol = nWidth - 1;
    1490           0 :         sal_Int32 nStartRow = 0;
    1491           0 :         sal_Int32 nEndRow = nHeight - 1;
    1492           0 :         bool bFixedStartRow = true;
    1493           0 :         bool bFixedHeight = false;
    1494             : 
    1495           0 :         bool bSingleCol = getFlag( nFlags, BIFF12_TOK_TABLE_COLUMN );
    1496           0 :         bool bColRange = getFlag( nFlags, BIFF12_TOK_TABLE_COLRANGE );
    1497           0 :         bool bValidRef = !bSingleCol || !bColRange;
    1498             :         OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - illegal combination of single column and column range" );
    1499           0 :         if( bValidRef )
    1500             :         {
    1501           0 :             if( bSingleCol )
    1502           0 :                 nStartCol = nEndCol = nCol1;
    1503           0 :             else if( bColRange )
    1504           0 :                 { nStartCol = nCol1; nEndCol = nCol2; }
    1505           0 :             bValidRef = (nStartCol <= nEndCol) && (nEndCol < nWidth);
    1506             :             OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid column range" );
    1507             :         }
    1508             : 
    1509           0 :         if( bValidRef )
    1510             :         {
    1511           0 :             bool bAllRows    = getFlag( nFlags, BIFF12_TOK_TABLE_ALL );
    1512           0 :             bool bHeaderRows = getFlag( nFlags, BIFF12_TOK_TABLE_HEADERS );
    1513           0 :             bool bDataRows   = getFlag( nFlags, BIFF12_TOK_TABLE_DATA );
    1514           0 :             bool bTotalsRows = getFlag( nFlags, BIFF12_TOK_TABLE_TOTALS );
    1515           0 :             bool bThisRow    = getFlag( nFlags, BIFF12_TOK_TABLE_THISROW );
    1516             : 
    1517           0 :             sal_Int32 nStartDataRow = xTable->getHeaderRows();
    1518           0 :             sal_Int32 nEndDataRow = nEndRow - xTable->getTotalsRows();
    1519           0 :             bValidRef = (nStartRow <= nStartDataRow) && (nStartDataRow <= nEndDataRow) && (nEndDataRow <= nEndRow);
    1520             :             OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - invalid data row range" );
    1521           0 :             if( bValidRef )
    1522             :             {
    1523           0 :                 if( bAllRows )
    1524             :                 {
    1525           0 :                     bValidRef = !bHeaderRows && !bDataRows && !bTotalsRows && !bThisRow;
    1526             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#All] table token" );
    1527             :                 }
    1528           0 :                 else if( bHeaderRows )
    1529             :                 {
    1530           0 :                     bValidRef = !bTotalsRows && !bThisRow;
    1531             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Headers] table token" );
    1532           0 :                     nEndRow = bDataRows ? nEndDataRow : (nStartDataRow - 1);
    1533           0 :                     bFixedHeight = !bDataRows;
    1534             :                 }
    1535           0 :                 else if( bDataRows )
    1536             :                 {
    1537           0 :                     bValidRef = !bThisRow;
    1538             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Data] table token" );
    1539           0 :                     nStartRow = nStartDataRow;
    1540           0 :                     if( !bTotalsRows ) nEndRow = nEndDataRow;
    1541             :                 }
    1542           0 :                 else if( bTotalsRows )
    1543             :                 {
    1544           0 :                     bValidRef = !bThisRow;
    1545             :                     OSL_ENSURE( bValidRef, "OoxFormulaParserImpl::importTableToken - unexpected flags in [#Totals] table token" );
    1546           0 :                     nStartRow = nEndDataRow + 1;
    1547           0 :                     bFixedStartRow = false;
    1548           0 :                     bFixedHeight = !bDataRows;
    1549             :                 }
    1550           0 :                 else if( bThisRow )
    1551             :                 {
    1552           0 :                     nStartRow = nEndRow = maBaseAddr.Row - xTable->getRange().StartRow;
    1553           0 :                     bFixedHeight = true;
    1554             :                 }
    1555             :                 else
    1556             :                 {
    1557             :                     // nothing is the same as [#Data]
    1558           0 :                     nStartRow = nStartDataRow;
    1559           0 :                     nEndRow = nEndDataRow;
    1560             :                 }
    1561             :             }
    1562           0 :             if( bValidRef )
    1563           0 :                 bValidRef = (0 <= nStartRow) && (nStartRow <= nEndRow) && (nEndRow < nHeight);
    1564             :         }
    1565           0 :         if( bValidRef )
    1566             :         {
    1567             :             // push single database area token, if table token refers to entire table
    1568           0 :             if( (nStartCol == 0) && (nEndCol + 1 == nWidth) && (nStartRow == 0) && (nEndRow + 1 == nHeight) )
    1569           0 :                 return pushValueOperand( nTokenIndex, OPCODE_DBAREA );
    1570             :             // create an OFFSET function call to refer to a subrange of the table
    1571           0 :             const FunctionInfo* pRowsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_ROWS );
    1572           0 :             const FunctionInfo* pColumnsInfo = getFuncInfoFromBiff12FuncId( BIFF_FUNC_COLUMNS );
    1573             :             return
    1574           0 :                 pRowsInfo && pColumnsInfo &&
    1575           0 :                 pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1576             :                 (bFixedStartRow ?
    1577           0 :                     pushValueOperandToken< double >( nStartRow ) :
    1578           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1579           0 :                      pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
    1580           0 :                      pushValueOperandToken< double >( nHeight - nStartRow ) &&
    1581           0 :                      pushBinaryOperatorToken( OPCODE_SUB ))) &&
    1582           0 :                 pushValueOperandToken< double >( nStartCol ) &&
    1583             :                 (bFixedHeight ?
    1584           0 :                     pushValueOperandToken< double >( nEndRow - nStartRow + 1 ) :
    1585           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1586           0 :                      pushFunctionOperatorToken( *pRowsInfo, 1 ) &&
    1587           0 :                      (((nStartRow == 0) && (nEndRow + 1 == nHeight)) ||
    1588           0 :                       (pushValueOperandToken< double >( nHeight - (nEndRow - nStartRow + 1) ) &&
    1589           0 :                        pushBinaryOperatorToken( OPCODE_SUB ))))) &&
    1590           0 :                 (((nStartCol == 0) && (nEndCol + 1 == nWidth)) ?
    1591           0 :                     (pushValueOperandToken( nTokenIndex, OPCODE_DBAREA ) &&
    1592           0 :                      pushFunctionOperatorToken( *pColumnsInfo, 1 )) :
    1593           0 :                     pushValueOperandToken< double >( nEndCol - nStartCol + 1 )) &&
    1594           0 :                 pushBiff12Function( BIFF_FUNC_OFFSET, 5 );
    1595             :         }
    1596             :     }
    1597           0 :     return pushBiffErrorOperand( BIFF_ERR_REF );
    1598             : }
    1599             : 
    1600           0 : bool OoxFormulaParserImpl::importArrayToken( SequenceInputStream& rStrm )
    1601             : {
    1602           0 :     rStrm.skip( 14 );
    1603             : 
    1604             :     // start token array with opening brace and leading spaces
    1605           0 :     pushOperand( OPCODE_ARRAY_OPEN );
    1606           0 :     size_t nOpSize = popOperandSize();
    1607           0 :     size_t nOldArraySize = getFormulaSize();
    1608             : 
    1609             :     // read array size
    1610           0 :     swapStreamPosition( rStrm );
    1611           0 :     sal_Int32 nRows = rStrm.readInt32();
    1612           0 :     sal_Int32 nCols = rStrm.readInt32();
    1613             :     OSL_ENSURE( (nCols > 0) && (nRows > 0), "OoxFormulaParserImpl::importArrayToken - empty array" );
    1614             : 
    1615             :     // read array values and build token array
    1616           0 :     for( sal_Int32 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
    1617             :     {
    1618           0 :         if( nRow > 0 )
    1619           0 :             appendRawToken( OPCODE_ARRAY_ROWSEP );
    1620           0 :         for( sal_Int32 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
    1621             :         {
    1622           0 :             if( nCol > 0 )
    1623           0 :                 appendRawToken( OPCODE_ARRAY_COLSEP );
    1624           0 :             switch( rStrm.readuInt8() )
    1625             :             {
    1626             :                 case BIFF_TOK_ARRAY_DOUBLE:
    1627           0 :                     appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
    1628           0 :                 break;
    1629             :                 case BIFF_TOK_ARRAY_STRING:
    1630           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::readString( rStrm, false );
    1631           0 :                 break;
    1632             :                 case BIFF_TOK_ARRAY_BOOL:
    1633           0 :                     appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
    1634           0 :                 break;
    1635             :                 case BIFF_TOK_ARRAY_ERROR:
    1636           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
    1637           0 :                     rStrm.skip( 3 );
    1638           0 :                 break;
    1639             :                 default:
    1640             :                     OSL_FAIL( "OoxFormulaParserImpl::importArrayToken - unknown data type" );
    1641           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
    1642             :             }
    1643             :         }
    1644             :     }
    1645           0 :     swapStreamPosition( rStrm );
    1646             : 
    1647             :     // close token array and set resulting operand size
    1648           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
    1649           0 :     pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
    1650           0 :     return true;
    1651             : }
    1652             : 
    1653           2 : bool OoxFormulaParserImpl::importRefToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1654             : {
    1655           2 :     BinSingleRef2d aRef;
    1656           2 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1657           2 :     return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
    1658             : }
    1659             : 
    1660           0 : bool OoxFormulaParserImpl::importAreaToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1661             : {
    1662           0 :     BinComplexRef2d aRef;
    1663           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1664           0 :     return pushReferenceOperand( aRef, bDeleted, bRelativeAsOffset );
    1665             : }
    1666             : 
    1667           0 : bool OoxFormulaParserImpl::importRef3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1668             : {
    1669           0 :     LinkSheetRange aSheetRange = readSheetRange( rStrm );
    1670           0 :     BinSingleRef2d aRef;
    1671           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1672           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    1673             : }
    1674             : 
    1675           0 : bool OoxFormulaParserImpl::importArea3dToken( SequenceInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    1676             : {
    1677           0 :     LinkSheetRange aSheetRange = readSheetRange( rStrm );
    1678           0 :     BinComplexRef2d aRef;
    1679           0 :     aRef.readBiff12Data( rStrm, bRelativeAsOffset );
    1680           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    1681             : }
    1682             : 
    1683           0 : bool OoxFormulaParserImpl::importMemAreaToken( SequenceInputStream& rStrm, bool bAddData )
    1684             : {
    1685           0 :     rStrm.skip( 6 );
    1686           0 :     if( bAddData )
    1687           0 :         skipMemAreaAddData( rStrm );
    1688           0 :     return true;
    1689             : }
    1690             : 
    1691           0 : bool OoxFormulaParserImpl::importMemFuncToken( SequenceInputStream& rStrm )
    1692             : {
    1693           0 :     rStrm.skip( 2 );
    1694           0 :     return true;
    1695             : }
    1696             : 
    1697           0 : bool OoxFormulaParserImpl::importNameToken( SequenceInputStream& rStrm )
    1698             : {
    1699           0 :     return pushBiff12Name( rStrm.readInt32() );
    1700             : }
    1701             : 
    1702           0 : bool OoxFormulaParserImpl::importNameXToken( SequenceInputStream& rStrm )
    1703             : {
    1704           0 :     sal_Int32 nRefId = rStrm.readInt16();
    1705           0 :     sal_Int32 nNameId = rStrm.readInt32();
    1706           0 :     return pushBiff12ExtName( nRefId, nNameId );
    1707             : }
    1708             : 
    1709           0 : bool OoxFormulaParserImpl::importFuncToken( SequenceInputStream& rStrm )
    1710             : {
    1711             :     sal_uInt16 nFuncId;
    1712           0 :     nFuncId = rStrm.readuInt16();
    1713           0 :     return pushBiff12Function( nFuncId );
    1714             : }
    1715             : 
    1716           0 : bool OoxFormulaParserImpl::importFuncVarToken( SequenceInputStream& rStrm )
    1717             : {
    1718             :     sal_uInt8 nParamCount;
    1719             :     sal_uInt16 nFuncId;
    1720           0 :     nParamCount = rStrm.readuChar();
    1721           0 :     nFuncId = rStrm.readuInt16();
    1722           0 :     return pushBiff12Function( nFuncId, nParamCount );
    1723             : }
    1724             : 
    1725          29 : bool OoxFormulaParserImpl::importExpToken( SequenceInputStream& rStrm )
    1726             : {
    1727          29 :     BinAddress aBaseAddr;
    1728          29 :     aBaseAddr.mnRow = rStrm.readInt32();
    1729          29 :     swapStreamPosition( rStrm );
    1730          29 :     aBaseAddr.mnCol = rStrm.readInt32();
    1731          29 :     swapStreamPosition( rStrm );
    1732          29 :     return pushSpecialTokenOperand( aBaseAddr, false );
    1733             : }
    1734             : 
    1735           0 : LinkSheetRange OoxFormulaParserImpl::readSheetRange( SequenceInputStream& rStrm )
    1736             : {
    1737           0 :     return getExternalLinks().getSheetRange( rStrm.readInt16() );
    1738             : }
    1739             : 
    1740          58 : void OoxFormulaParserImpl::swapStreamPosition( SequenceInputStream& rStrm )
    1741             : {
    1742          58 :     sal_Int64 nRecPos = rStrm.tell();
    1743          58 :     rStrm.seek( mnAddDataPos );
    1744          58 :     mnAddDataPos = nRecPos;
    1745          58 : }
    1746             : 
    1747           0 : void OoxFormulaParserImpl::skipMemAreaAddData( SequenceInputStream& rStrm )
    1748             : {
    1749           0 :     swapStreamPosition( rStrm );
    1750           0 :     rStrm.skip( 16 * rStrm.readInt32() );
    1751           0 :     swapStreamPosition( rStrm );
    1752           0 : }
    1753             : 
    1754             : // convert BIN token and push API operand or operator -------------------------
    1755             : 
    1756           0 : bool OoxFormulaParserImpl::pushBiff12Name( sal_Int32 nNameId )
    1757             : {
    1758             :     // one-based in BIFF12 formulas
    1759           0 :     return pushDefinedNameOperand( getDefinedNames().getByIndex( nNameId - 1 ) );
    1760             : }
    1761             : 
    1762           0 : bool OoxFormulaParserImpl::pushBiff12ExtName( sal_Int32 nRefId, sal_Int32 nNameId )
    1763             : {
    1764           0 :     if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
    1765             :     {
    1766           0 :         if( pExtLink->getLinkType() == LINKTYPE_SELF )
    1767           0 :             return pushBiff12Name( nNameId );
    1768             :         // external name indexes are one-based in BIFF12
    1769           0 :         ExternalNameRef xExtName = pExtLink->getNameByIndex( nNameId - 1 );
    1770           0 :         return pushExternalNameOperand( xExtName, *pExtLink );
    1771             :     }
    1772           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    1773             : }
    1774             : 
    1775           0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId )
    1776             : {
    1777           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
    1778           0 :         if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
    1779           0 :             return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
    1780           0 :     return pushFunctionOperator( OPCODE_NONAME, 0 );
    1781             : }
    1782             : 
    1783           0 : bool OoxFormulaParserImpl::pushBiff12Function( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
    1784             : {
    1785           0 :     if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
    1786           0 :         nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
    1787           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiff12FuncId( nFuncId ) )
    1788           0 :         return pushFunctionOperator( *pFuncInfo, nParamCount );
    1789           0 :     return pushFunctionOperator( OPCODE_NONAME, nParamCount );
    1790             : }
    1791             : 
    1792             : // BIFF parser implementation =================================================
    1793             : 
    1794             : namespace {
    1795             : 
    1796             : /** A natural language reference struct with relative flag. */
    1797             : struct BiffNlr
    1798             : {
    1799             :     sal_Int32           mnCol;              /// Column index.
    1800             :     sal_Int32           mnRow;              /// Row index.
    1801             :     bool                mbRel;              /// True = relative column/row reference.
    1802             : 
    1803             :     explicit            BiffNlr();
    1804             : 
    1805             :     void                readBiff8Data( BiffInputStream& rStrm );
    1806             : };
    1807             : 
    1808           0 : BiffNlr::BiffNlr() :
    1809             :     mnCol( 0 ),
    1810             :     mnRow( 0 ),
    1811           0 :     mbRel( false )
    1812             : {
    1813           0 : }
    1814             : 
    1815           0 : void BiffNlr::readBiff8Data( BiffInputStream& rStrm )
    1816             : {
    1817             :     sal_uInt16 nRow, nCol;
    1818           0 :     rStrm >> nRow >> nCol;
    1819           0 :     mnCol = nCol & BIFF_TOK_NLR_MASK;
    1820           0 :     mnRow = nRow;
    1821           0 :     mbRel = getFlag( nCol, BIFF_TOK_NLR_REL );
    1822           0 : }
    1823             : 
    1824           0 : bool lclIsValidNlrStack( const BinAddress& rAddr1, const BinAddress& rAddr2, bool bRow )
    1825             : {
    1826             :     return bRow ?
    1827           0 :         ((rAddr1.mnRow == rAddr2.mnRow) && (rAddr1.mnCol + 1 == rAddr2.mnCol)) :
    1828           0 :         ((rAddr1.mnCol == rAddr2.mnCol) && (rAddr1.mnRow + 1 == rAddr2.mnRow));
    1829             : }
    1830             : 
    1831           0 : bool lclIsValidNlrRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
    1832             : {
    1833             :     return bRow ?
    1834           0 :         ((rNlr.mnRow == rRange.maFirst.mnRow) && (rNlr.mnCol + 1 == rRange.maFirst.mnCol) && (rRange.maFirst.mnRow == rRange.maLast.mnRow)) :
    1835           0 :         ((rNlr.mnCol == rRange.maFirst.mnCol) && (rNlr.mnRow + 1 == rRange.maFirst.mnRow) && (rRange.maFirst.mnCol == rRange.maLast.mnCol));
    1836             : }
    1837             : 
    1838             : } // namespace
    1839             : 
    1840           0 : class BiffFormulaParserImpl : public FormulaParserImpl
    1841             : {
    1842             : public:
    1843             :     explicit            BiffFormulaParserImpl( const FormulaParser& rParent );
    1844             : 
    1845             :     virtual ApiTokenSequence importBiffFormula(
    1846             :                             const CellAddress& rBaseAddr,
    1847             :                             FormulaType eType,
    1848             :                             BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize ) SAL_OVERRIDE;
    1849             : 
    1850             : private:
    1851             :     // import token contents and create API formula token ---------------------
    1852             : 
    1853             :     bool                importTokenNotAvailable( BiffInputStream& rStrm );
    1854             :     bool                importRefTokenNotAvailable( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1855             :     bool                importStrToken2( BiffInputStream& rStrm );
    1856             :     bool                importStrToken8( BiffInputStream& rStrm );
    1857             :     bool                importAttrToken( BiffInputStream& rStrm );
    1858             :     bool                importSpaceToken3( BiffInputStream& rStrm );
    1859             :     bool                importSpaceToken4( BiffInputStream& rStrm );
    1860             :     bool                importSheetToken2( BiffInputStream& rStrm );
    1861             :     bool                importSheetToken3( BiffInputStream& rStrm );
    1862             :     bool                importEndSheetToken2( BiffInputStream& rStrm );
    1863             :     bool                importEndSheetToken3( BiffInputStream& rStrm );
    1864             :     bool                importNlrToken( BiffInputStream& rStrm );
    1865             :     bool                importArrayToken( BiffInputStream& rStrm );
    1866             :     bool                importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1867             :     bool                importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1868             :     bool                importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1869             :     bool                importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1870             :     bool                importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1871             :     bool                importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1872             :     bool                importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1873             :     bool                importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset );
    1874             :     bool                importMemAreaToken( BiffInputStream& rStrm, bool bAddData );
    1875             :     bool                importMemFuncToken( BiffInputStream& rStrm );
    1876             :     bool                importNameToken( BiffInputStream& rStrm );
    1877             :     bool                importNameXToken( BiffInputStream& rStrm );
    1878             :     bool                importFuncToken2( BiffInputStream& rStrm );
    1879             :     bool                importFuncToken4( BiffInputStream& rStrm );
    1880             :     bool                importFuncVarToken2( BiffInputStream& rStrm );
    1881             :     bool                importFuncVarToken4( BiffInputStream& rStrm );
    1882             :     bool                importFuncCEToken( BiffInputStream& rStrm );
    1883             :     bool                importExpToken( BiffInputStream& rStrm );
    1884             :     bool                importTblToken( BiffInputStream& rStrm );
    1885             : 
    1886             :     bool                importNlrAddrToken( BiffInputStream& rStrm, bool bRow );
    1887             :     bool                importNlrRangeToken( BiffInputStream& rStrm );
    1888             :     bool                importNlrSAddrToken( BiffInputStream& rStrm, bool bRow );
    1889             :     bool                importNlrSRangeToken( BiffInputStream& rStrm );
    1890             :     bool                importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nSkip );
    1891             : 
    1892             :     sal_Int32           readRefId( BiffInputStream& rStrm );
    1893             :     sal_uInt16          readNameId( BiffInputStream& rStrm );
    1894             :     LinkSheetRange      readSheetRange5( BiffInputStream& rStrm );
    1895             :     LinkSheetRange      readSheetRange8( BiffInputStream& rStrm );
    1896             : 
    1897             :     void                swapStreamPosition( BiffInputStream& rStrm );
    1898             :     void                skipMemAreaAddData( BiffInputStream& rStrm );
    1899             :     bool                readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow );
    1900             :     bool                readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm );
    1901             : 
    1902             :     // convert BIFF token and push API operand or operator --------------------
    1903             : 
    1904             :     bool                pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
    1905             :     bool                pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset );
    1906             :     bool                pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow );
    1907             :     bool                pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange );
    1908             :     bool                pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow );
    1909             :     bool                pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow );
    1910             :     bool                pushBiffName( sal_uInt16 nNameId );
    1911             :     bool                pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId );
    1912             :     bool                pushBiffFunction( sal_uInt16 nFuncId );
    1913             :     bool                pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount );
    1914             : 
    1915             : private:
    1916             :     typedef bool (BiffFormulaParserImpl::*ImportTokenFunc)( BiffInputStream& );
    1917             :     typedef bool (BiffFormulaParserImpl::*ImportRefTokenFunc)( BiffInputStream&, bool, bool );
    1918             : 
    1919             :     ImportTokenFunc     mpImportStrToken;           /// Pointer to tStr import function (string constant).
    1920             :     ImportTokenFunc     mpImportSpaceToken;         /// Pointer to tAttrSpace import function (spaces/line breaks).
    1921             :     ImportTokenFunc     mpImportSheetToken;         /// Pointer to tSheet import function (external reference).
    1922             :     ImportTokenFunc     mpImportEndSheetToken;      /// Pointer to tEndSheet import function (end of external reference).
    1923             :     ImportTokenFunc     mpImportNlrToken;           /// Pointer to tNlr import function (natural language reference).
    1924             :     ImportRefTokenFunc  mpImportRefToken;           /// Pointer to tRef import function (2d cell reference).
    1925             :     ImportRefTokenFunc  mpImportAreaToken;          /// Pointer to tArea import function (2d area reference).
    1926             :     ImportRefTokenFunc  mpImportRef3dToken;         /// Pointer to tRef3d import function (3d cell reference).
    1927             :     ImportRefTokenFunc  mpImportArea3dToken;        /// Pointer to tArea3d import function (3d area reference).
    1928             :     ImportTokenFunc     mpImportNameXToken;         /// Pointer to tNameX import function (external name).
    1929             :     ImportTokenFunc     mpImportFuncToken;          /// Pointer to tFunc import function (function with fixed parameter count).
    1930             :     ImportTokenFunc     mpImportFuncVarToken;       /// Pointer to tFuncVar import function (function with variable parameter count).
    1931             :     ImportTokenFunc     mpImportFuncCEToken;        /// Pointer to tFuncCE import function (command macro call).
    1932             :     sal_Int64           mnAddDataPos;               /// Current stream position for additional data (tArray, tMemArea, tNlr).
    1933             :     sal_Int32           mnCurrRefId;                /// Current ref-id from tSheet token (BIFF2-BIFF4 only).
    1934             :     sal_uInt16          mnAttrDataSize;             /// Size of one tAttr data element.
    1935             :     sal_uInt16          mnArraySize;                /// Size of tArray data.
    1936             :     sal_uInt16          mnNameSize;                 /// Size of tName data.
    1937             :     sal_uInt16          mnMemAreaSize;              /// Size of tMemArea data.
    1938             :     sal_uInt16          mnMemFuncSize;              /// Size of tMemFunc data.
    1939             :     sal_uInt16          mnRefIdSize;                /// Size of unused data following a reference identifier.
    1940             : };
    1941             : 
    1942           0 : BiffFormulaParserImpl::BiffFormulaParserImpl( const FormulaParser& rParent ) :
    1943             :     FormulaParserImpl( rParent ),
    1944             :     mnAddDataPos( 0 ),
    1945           0 :     mnCurrRefId( 0 )
    1946             : {
    1947           0 :     switch( getBiff() )
    1948             :     {
    1949             :         case BIFF2:
    1950           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    1951           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1952           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken2;
    1953           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken2;
    1954           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1955           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    1956           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    1957           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1958           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1959           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1960           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
    1961           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
    1962           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
    1963           0 :             mnAttrDataSize = 1;
    1964           0 :             mnArraySize = 6;
    1965           0 :             mnNameSize = 5;
    1966           0 :             mnMemAreaSize = 4;
    1967           0 :             mnMemFuncSize = 1;
    1968           0 :             mnRefIdSize = 1;
    1969           0 :         break;
    1970             :         case BIFF3:
    1971           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    1972           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken3;
    1973           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
    1974           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
    1975           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1976           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    1977           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    1978           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1979           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    1980           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1981           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken2;
    1982           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken2;
    1983           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importFuncCEToken;
    1984           0 :             mnAttrDataSize = 2;
    1985           0 :             mnArraySize = 7;
    1986           0 :             mnNameSize = 8;
    1987           0 :             mnMemAreaSize = 6;
    1988           0 :             mnMemFuncSize = 2;
    1989           0 :             mnRefIdSize = 2;
    1990           0 :         break;
    1991             :         case BIFF4:
    1992           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    1993           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    1994           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importSheetToken3;
    1995           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importEndSheetToken3;
    1996           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    1997           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    1998           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    1999           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    2000           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importRefTokenNotAvailable;
    2001           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2002           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2003           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2004           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2005           0 :             mnAttrDataSize = 2;
    2006           0 :             mnArraySize = 7;
    2007           0 :             mnNameSize = 8;
    2008           0 :             mnMemAreaSize = 6;
    2009           0 :             mnMemFuncSize = 2;
    2010           0 :             mnRefIdSize = 2;
    2011           0 :         break;
    2012             :         case BIFF5:
    2013           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken2;
    2014           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    2015           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2016           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2017           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2018           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken2;
    2019           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken2;
    2020           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken5;
    2021           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken5;
    2022           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
    2023           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2024           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2025           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2026           0 :             mnAttrDataSize = 2;
    2027           0 :             mnArraySize = 7;
    2028           0 :             mnNameSize = 12;
    2029           0 :             mnMemAreaSize = 6;
    2030           0 :             mnMemFuncSize = 2;
    2031           0 :             mnRefIdSize = 8;
    2032           0 :         break;
    2033             :         case BIFF8:
    2034           0 :             mpImportStrToken = &BiffFormulaParserImpl::importStrToken8;
    2035           0 :             mpImportSpaceToken = &BiffFormulaParserImpl::importSpaceToken4;
    2036           0 :             mpImportSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2037           0 :             mpImportEndSheetToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2038           0 :             mpImportNlrToken = &BiffFormulaParserImpl::importNlrToken;
    2039           0 :             mpImportRefToken = &BiffFormulaParserImpl::importRefToken8;
    2040           0 :             mpImportAreaToken = &BiffFormulaParserImpl::importAreaToken8;
    2041           0 :             mpImportRef3dToken = &BiffFormulaParserImpl::importRef3dToken8;
    2042           0 :             mpImportArea3dToken = &BiffFormulaParserImpl::importArea3dToken8;
    2043           0 :             mpImportNameXToken = &BiffFormulaParserImpl::importNameXToken;
    2044           0 :             mpImportFuncToken = &BiffFormulaParserImpl::importFuncToken4;
    2045           0 :             mpImportFuncVarToken = &BiffFormulaParserImpl::importFuncVarToken4;
    2046           0 :             mpImportFuncCEToken = &BiffFormulaParserImpl::importTokenNotAvailable;
    2047           0 :             mnAttrDataSize = 2;
    2048           0 :             mnArraySize = 7;
    2049           0 :             mnNameSize = 2;
    2050           0 :             mnMemAreaSize = 6;
    2051           0 :             mnMemFuncSize = 2;
    2052           0 :             mnRefIdSize = 0;
    2053           0 :         break;
    2054             :         case BIFF_UNKNOWN:
    2055           0 :             mpImportStrToken = NULL;
    2056           0 :             mpImportSpaceToken = NULL;
    2057           0 :             mpImportSheetToken = NULL;
    2058           0 :             mpImportEndSheetToken = NULL;
    2059           0 :             mpImportNlrToken = NULL;
    2060           0 :             mpImportRefToken = NULL;
    2061           0 :             mpImportAreaToken = NULL;
    2062           0 :             mpImportRef3dToken = NULL;
    2063           0 :             mpImportArea3dToken = NULL;
    2064           0 :             mpImportNameXToken = NULL;
    2065           0 :             mpImportFuncToken = NULL;
    2066           0 :             mpImportFuncVarToken = NULL;
    2067           0 :             mpImportFuncCEToken = NULL;
    2068           0 :             mnAttrDataSize = 0;
    2069           0 :             mnArraySize = 0;
    2070           0 :             mnNameSize = 0;
    2071           0 :             mnMemAreaSize = 0;
    2072           0 :             mnMemFuncSize = 0;
    2073           0 :             mnRefIdSize = 0;
    2074           0 :         break;
    2075             :     }
    2076           0 : }
    2077             : 
    2078           0 : ApiTokenSequence BiffFormulaParserImpl::importBiffFormula( const CellAddress& rBaseAddr,
    2079             :         FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize )
    2080             : {
    2081           0 :     initializeImport( rBaseAddr, eType );
    2082           0 :     mnCurrRefId = 0;
    2083             : 
    2084           0 :     sal_uInt16 nFmlaSize = lclReadFmlaSize( rStrm, getBiff(), pnFmlaSize );
    2085           0 :     sal_Int64 nEndPos = mnAddDataPos = rStrm.tell() + nFmlaSize;
    2086             : 
    2087           0 :     bool bOk = true;
    2088           0 :     while( bOk && !rStrm.isEof() && (rStrm.tell() < nEndPos) )
    2089             :     {
    2090             :         sal_uInt8 nTokenId;
    2091           0 :         rStrm >> nTokenId;
    2092           0 :         sal_uInt8 nTokenClass = nTokenId & BIFF_TOKCLASS_MASK;
    2093           0 :         sal_uInt8 nBaseId = nTokenId & BIFF_TOKID_MASK;
    2094             : 
    2095           0 :         bOk = !getFlag( nTokenId, BIFF_TOKFLAG_INVALID );
    2096           0 :         if( bOk )
    2097             :         {
    2098           0 :             if( nTokenClass == BIFF_TOKCLASS_NONE )
    2099             :             {
    2100             :                 // base tokens
    2101           0 :                 switch( nBaseId )
    2102             :                 {
    2103           0 :                     case BIFF_TOKID_EXP:        bOk = importExpToken( rStrm );                          break;
    2104           0 :                     case BIFF_TOKID_TBL:        bOk = importTblToken( rStrm );                          break;
    2105           0 :                     case BIFF_TOKID_ADD:        bOk = pushBinaryOperator( OPCODE_ADD );                 break;
    2106           0 :                     case BIFF_TOKID_SUB:        bOk = pushBinaryOperator( OPCODE_SUB );                 break;
    2107           0 :                     case BIFF_TOKID_MUL:        bOk = pushBinaryOperator( OPCODE_MULT );                break;
    2108           0 :                     case BIFF_TOKID_DIV:        bOk = pushBinaryOperator( OPCODE_DIV );                 break;
    2109           0 :                     case BIFF_TOKID_POWER:      bOk = pushBinaryOperator( OPCODE_POWER );               break;
    2110           0 :                     case BIFF_TOKID_CONCAT:     bOk = pushBinaryOperator( OPCODE_CONCAT );              break;
    2111           0 :                     case BIFF_TOKID_LT:         bOk = pushBinaryOperator( OPCODE_LESS );                break;
    2112           0 :                     case BIFF_TOKID_LE:         bOk = pushBinaryOperator( OPCODE_LESS_EQUAL );          break;
    2113           0 :                     case BIFF_TOKID_EQ:         bOk = pushBinaryOperator( OPCODE_EQUAL );               break;
    2114           0 :                     case BIFF_TOKID_GE:         bOk = pushBinaryOperator( OPCODE_GREATER_EQUAL );       break;
    2115           0 :                     case BIFF_TOKID_GT:         bOk = pushBinaryOperator( OPCODE_GREATER );             break;
    2116           0 :                     case BIFF_TOKID_NE:         bOk = pushBinaryOperator( OPCODE_NOT_EQUAL );           break;
    2117           0 :                     case BIFF_TOKID_ISECT:      bOk = pushBinaryOperator( OPCODE_INTERSECT );           break;
    2118           0 :                     case BIFF_TOKID_LIST:       bOk = pushBinaryOperator( OPCODE_LIST );                break;
    2119           0 :                     case BIFF_TOKID_RANGE:      bOk = pushBinaryOperator( OPCODE_RANGE );               break;
    2120           0 :                     case BIFF_TOKID_UPLUS:      bOk = pushUnaryPreOperator( OPCODE_PLUS_SIGN );         break;
    2121           0 :                     case BIFF_TOKID_UMINUS:     bOk = pushUnaryPreOperator( OPCODE_MINUS_SIGN );        break;
    2122           0 :                     case BIFF_TOKID_PERCENT:    bOk = pushUnaryPostOperator( OPCODE_PERCENT );          break;
    2123           0 :                     case BIFF_TOKID_PAREN:      bOk = pushParenthesesOperator();                        break;
    2124           0 :                     case BIFF_TOKID_MISSARG:    bOk = pushOperand( OPCODE_MISSING );                    break;
    2125           0 :                     case BIFF_TOKID_STR:        bOk = (this->*mpImportStrToken)( rStrm );               break;
    2126           0 :                     case BIFF_TOKID_NLR:        bOk = (this->*mpImportNlrToken)( rStrm );               break;
    2127           0 :                     case BIFF_TOKID_ATTR:       bOk = importAttrToken( rStrm );                         break;
    2128           0 :                     case BIFF_TOKID_SHEET:      bOk = (this->*mpImportSheetToken)( rStrm );             break;
    2129           0 :                     case BIFF_TOKID_ENDSHEET:   bOk = (this->*mpImportEndSheetToken)( rStrm );          break;
    2130           0 :                     case BIFF_TOKID_ERR:        bOk = pushBiffErrorOperand( rStrm.readuInt8() );        break;
    2131           0 :                     case BIFF_TOKID_BOOL:       bOk = pushBiffBoolOperand( rStrm.readuInt8() );         break;
    2132           0 :                     case BIFF_TOKID_INT:        bOk = pushValueOperand< double >( rStrm.readuInt16() ); break;
    2133           0 :                     case BIFF_TOKID_NUM:        bOk = pushValueOperand( rStrm.readDouble() );           break;
    2134           0 :                     default:                    bOk = false;
    2135             :                 }
    2136             :             }
    2137             :             else
    2138             :             {
    2139             :                 // classified tokens
    2140           0 :                 switch( nBaseId )
    2141             :                 {
    2142           0 :                     case BIFF_TOKID_ARRAY:      bOk = importArrayToken( rStrm );                                        break;
    2143           0 :                     case BIFF_TOKID_FUNC:       bOk = (this->*mpImportFuncToken)( rStrm );                              break;
    2144           0 :                     case BIFF_TOKID_FUNCVAR:    bOk = (this->*mpImportFuncVarToken)( rStrm );                           break;
    2145           0 :                     case BIFF_TOKID_NAME:       bOk = importNameToken( rStrm );                                         break;
    2146           0 :                     case BIFF_TOKID_REF:        bOk = (this->*mpImportRefToken)( rStrm, false, false );                 break;
    2147           0 :                     case BIFF_TOKID_AREA:       bOk = (this->*mpImportAreaToken)( rStrm, false, false );                break;
    2148           0 :                     case BIFF_TOKID_MEMAREA:    bOk = importMemAreaToken( rStrm, true );                                break;
    2149           0 :                     case BIFF_TOKID_MEMERR:     bOk = importMemAreaToken( rStrm, false );                               break;
    2150           0 :                     case BIFF_TOKID_MEMNOMEM:   bOk = importMemAreaToken( rStrm, false );                               break;
    2151           0 :                     case BIFF_TOKID_MEMFUNC:    bOk = importMemFuncToken( rStrm );                                      break;
    2152           0 :                     case BIFF_TOKID_REFERR:     bOk = (this->*mpImportRefToken)( rStrm, true, false );                  break;
    2153           0 :                     case BIFF_TOKID_AREAERR:    bOk = (this->*mpImportAreaToken)( rStrm, true, false );                 break;
    2154           0 :                     case BIFF_TOKID_REFN:       bOk = (this->*mpImportRefToken)( rStrm, false, true );                  break;
    2155           0 :                     case BIFF_TOKID_AREAN:      bOk = (this->*mpImportAreaToken)( rStrm, false, true );                 break;
    2156           0 :                     case BIFF_TOKID_MEMAREAN:   bOk = importMemFuncToken( rStrm );                                      break;
    2157           0 :                     case BIFF_TOKID_MEMNOMEMN:  bOk = importMemFuncToken( rStrm );                                      break;
    2158           0 :                     case BIFF_TOKID_FUNCCE:     bOk = (this->*mpImportFuncCEToken)( rStrm );                            break;
    2159           0 :                     case BIFF_TOKID_NAMEX:      bOk = (this->*mpImportNameXToken)( rStrm );                             break;
    2160           0 :                     case BIFF_TOKID_REF3D:      bOk = (this->*mpImportRef3dToken)( rStrm, false, mbRelativeAsOffset );  break;
    2161           0 :                     case BIFF_TOKID_AREA3D:     bOk = (this->*mpImportArea3dToken)( rStrm, false, mbRelativeAsOffset ); break;
    2162           0 :                     case BIFF_TOKID_REFERR3D:   bOk = (this->*mpImportRef3dToken)( rStrm, true, mbRelativeAsOffset );   break;
    2163           0 :                     case BIFF_TOKID_AREAERR3D:  bOk = (this->*mpImportArea3dToken)( rStrm, true, mbRelativeAsOffset );  break;
    2164           0 :                     default:                    bOk = false;
    2165             :                 }
    2166             :             }
    2167             :         }
    2168             :     }
    2169             : 
    2170             :     // build and finalize the token sequence
    2171           0 :     ApiTokenSequence aFinalTokens;
    2172           0 :     if( bOk && (rStrm.tell() == nEndPos) )
    2173           0 :         aFinalTokens = finalizeImport();
    2174             : 
    2175             :     // seek behind additional token data of tArray, tMemArea, tNlr tokens
    2176           0 :     rStrm.seek( mnAddDataPos );
    2177             : 
    2178             :     // return the final token sequence
    2179           0 :     return aFinalTokens;
    2180             : }
    2181             : 
    2182             : // import token contents and create API formula token -------------------------
    2183             : 
    2184           0 : bool BiffFormulaParserImpl::importTokenNotAvailable( BiffInputStream& )
    2185             : {
    2186             :     // dummy function for pointer-to-member-function
    2187           0 :     return false;
    2188             : }
    2189             : 
    2190           0 : bool BiffFormulaParserImpl::importRefTokenNotAvailable( BiffInputStream&, bool, bool )
    2191             : {
    2192             :     // dummy function for pointer-to-member-function
    2193           0 :     return false;
    2194             : }
    2195             : 
    2196           0 : bool BiffFormulaParserImpl::importStrToken2( BiffInputStream& rStrm )
    2197             : {
    2198           0 :     return pushValueOperand( rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars ) );
    2199             : }
    2200             : 
    2201           0 : bool BiffFormulaParserImpl::importStrToken8( BiffInputStream& rStrm )
    2202             : {
    2203             :     // read flags field for empty strings also
    2204           0 :     return pushValueOperand( rStrm.readUniStringBody( rStrm.readuInt8(), mbAllowNulChars ) );
    2205             : }
    2206             : 
    2207           0 : bool BiffFormulaParserImpl::importAttrToken( BiffInputStream& rStrm )
    2208             : {
    2209           0 :     bool bOk = true;
    2210             :     sal_uInt8 nType;
    2211           0 :     rStrm >> nType;
    2212           0 :     switch( nType )
    2213             :     {
    2214             :         case 0:     // sometimes, tAttrSkip tokens miss the type flag
    2215             :         case BIFF_TOK_ATTR_VOLATILE:
    2216             :         case BIFF_TOK_ATTR_IF:
    2217             :         case BIFF_TOK_ATTR_SKIP:
    2218             :         case BIFF_TOK_ATTR_ASSIGN:
    2219           0 :             rStrm.skip( mnAttrDataSize );
    2220           0 :         break;
    2221             :         case BIFF_TOK_ATTR_CHOOSE:
    2222           0 :             rStrm.skip( mnAttrDataSize * (1 + ((getBiff() == BIFF2) ? rStrm.readuInt8() : rStrm.readuInt16())) );
    2223           0 :         break;
    2224             :         case BIFF_TOK_ATTR_SUM:
    2225           0 :             rStrm.skip( mnAttrDataSize );
    2226           0 :             bOk = pushBiffFunction( BIFF_FUNC_SUM, 1 );
    2227           0 :         break;
    2228             :         case BIFF_TOK_ATTR_SPACE:
    2229             :         case BIFF_TOK_ATTR_SPACE_VOLATILE:
    2230           0 :             bOk = (this->*mpImportSpaceToken)( rStrm );
    2231           0 :         break;
    2232             :         default:
    2233           0 :             bOk = false;
    2234             :     }
    2235           0 :     return bOk;
    2236             : }
    2237             : 
    2238           0 : bool BiffFormulaParserImpl::importSpaceToken3( BiffInputStream& rStrm )
    2239             : {
    2240           0 :     rStrm.skip( 2 );
    2241           0 :     return true;
    2242             : }
    2243             : 
    2244           0 : bool BiffFormulaParserImpl::importSpaceToken4( BiffInputStream& rStrm )
    2245             : {
    2246             :     sal_uInt8 nType, nCount;
    2247           0 :     rStrm >> nType >> nCount;
    2248           0 :     switch( nType )
    2249             :     {
    2250             :         case BIFF_TOK_ATTR_SPACE_SP:
    2251           0 :             appendLeadingSpaces( nCount, false );
    2252           0 :         break;
    2253             :         case BIFF_TOK_ATTR_SPACE_BR:
    2254           0 :             appendLeadingSpaces( nCount, true );
    2255           0 :         break;
    2256             :         case BIFF_TOK_ATTR_SPACE_SP_OPEN:
    2257           0 :             appendOpeningSpaces( nCount, false );
    2258           0 :         break;
    2259             :         case BIFF_TOK_ATTR_SPACE_BR_OPEN:
    2260           0 :             appendOpeningSpaces( nCount, true );
    2261           0 :         break;
    2262             :         case BIFF_TOK_ATTR_SPACE_SP_CLOSE:
    2263           0 :             appendClosingSpaces( nCount, false );
    2264           0 :         break;
    2265             :         case BIFF_TOK_ATTR_SPACE_BR_CLOSE:
    2266           0 :             appendClosingSpaces( nCount, true );
    2267           0 :         break;
    2268             :     }
    2269           0 :     return true;
    2270             : }
    2271             : 
    2272           0 : bool BiffFormulaParserImpl::importSheetToken2( BiffInputStream& rStrm )
    2273             : {
    2274           0 :     rStrm.skip( 4 );
    2275           0 :     mnCurrRefId = readRefId( rStrm );
    2276           0 :     return true;
    2277             : }
    2278             : 
    2279           0 : bool BiffFormulaParserImpl::importSheetToken3( BiffInputStream& rStrm )
    2280             : {
    2281           0 :     rStrm.skip( 6 );
    2282           0 :     mnCurrRefId = readRefId( rStrm );
    2283           0 :     return true;
    2284             : }
    2285             : 
    2286           0 : bool BiffFormulaParserImpl::importEndSheetToken2( BiffInputStream& rStrm )
    2287             : {
    2288           0 :     rStrm.skip( 3 );
    2289           0 :     mnCurrRefId = 0;
    2290           0 :     return true;
    2291             : }
    2292             : 
    2293           0 : bool BiffFormulaParserImpl::importEndSheetToken3( BiffInputStream& rStrm )
    2294             : {
    2295           0 :     rStrm.skip( 4 );
    2296           0 :     mnCurrRefId = 0;
    2297           0 :     return true;
    2298             : }
    2299             : 
    2300           0 : bool BiffFormulaParserImpl::importNlrToken( BiffInputStream& rStrm )
    2301             : {
    2302           0 :     bool bOk = true;
    2303             :     sal_uInt8 nNlrType;
    2304           0 :     rStrm >> nNlrType;
    2305           0 :     switch( nNlrType )
    2306             :     {
    2307           0 :         case BIFF_TOK_NLR_ERR:      bOk = importNlrErrToken( rStrm, 4 );        break;
    2308           0 :         case BIFF_TOK_NLR_ROWR:     bOk = importNlrAddrToken( rStrm, true );    break;
    2309           0 :         case BIFF_TOK_NLR_COLR:     bOk = importNlrAddrToken( rStrm, false );   break;
    2310           0 :         case BIFF_TOK_NLR_ROWV:     bOk = importNlrAddrToken( rStrm, true );    break;
    2311           0 :         case BIFF_TOK_NLR_COLV:     bOk = importNlrAddrToken( rStrm, false );   break;
    2312           0 :         case BIFF_TOK_NLR_RANGE:    bOk = importNlrRangeToken( rStrm );         break;
    2313           0 :         case BIFF_TOK_NLR_SRANGE:   bOk = importNlrSRangeToken( rStrm );        break;
    2314           0 :         case BIFF_TOK_NLR_SROWR:    bOk = importNlrSAddrToken( rStrm, true );   break;
    2315           0 :         case BIFF_TOK_NLR_SCOLR:    bOk = importNlrSAddrToken( rStrm, false );  break;
    2316           0 :         case BIFF_TOK_NLR_SROWV:    bOk = importNlrSAddrToken( rStrm, true );   break;
    2317           0 :         case BIFF_TOK_NLR_SCOLV:    bOk = importNlrSAddrToken( rStrm, false );  break;
    2318           0 :         case BIFF_TOK_NLR_RANGEERR: bOk = importNlrErrToken( rStrm, 13 );       break;
    2319           0 :         case BIFF_TOK_NLR_SXNAME:   bOk = importNlrErrToken( rStrm, 4 );        break;
    2320           0 :         default:                    bOk = false;
    2321             :     }
    2322           0 :     return bOk;
    2323             : }
    2324             : 
    2325           0 : bool BiffFormulaParserImpl::importArrayToken( BiffInputStream& rStrm )
    2326             : {
    2327           0 :     rStrm.skip( mnArraySize );
    2328             : 
    2329             :     // start token array with opening brace and leading spaces
    2330           0 :     pushOperand( OPCODE_ARRAY_OPEN );
    2331           0 :     size_t nOpSize = popOperandSize();
    2332           0 :     size_t nOldArraySize = getFormulaSize();
    2333           0 :     bool bBiff8 = getBiff() == BIFF8;
    2334             : 
    2335             :     // read array size
    2336           0 :     swapStreamPosition( rStrm );
    2337           0 :     sal_uInt16 nCols = rStrm.readuInt8();
    2338           0 :     sal_uInt16 nRows = rStrm.readuInt16();
    2339           0 :     if( bBiff8 ) { ++nCols; ++nRows; } else if( nCols == 0 ) nCols = 256;
    2340             :     OSL_ENSURE( (nCols > 0) && (nRows > 0), "BiffFormulaParserImpl::importArrayToken - empty array" );
    2341             : 
    2342             :     // read array values and build token array
    2343           0 :     for( sal_uInt16 nRow = 0; !rStrm.isEof() && (nRow < nRows); ++nRow )
    2344             :     {
    2345           0 :         if( nRow > 0 )
    2346           0 :             appendRawToken( OPCODE_ARRAY_ROWSEP );
    2347           0 :         for( sal_uInt16 nCol = 0; !rStrm.isEof() && (nCol < nCols); ++nCol )
    2348             :         {
    2349           0 :             if( nCol > 0 )
    2350           0 :                 appendRawToken( OPCODE_ARRAY_COLSEP );
    2351           0 :             switch( rStrm.readuInt8() )
    2352             :             {
    2353             :                 case BIFF_DATATYPE_EMPTY:
    2354           0 :                     appendRawToken( OPCODE_PUSH ) <<= OUString();
    2355           0 :                     rStrm.skip( 8 );
    2356           0 :                 break;
    2357             :                 case BIFF_DATATYPE_DOUBLE:
    2358           0 :                     appendRawToken( OPCODE_PUSH ) <<= rStrm.readDouble();
    2359           0 :                 break;
    2360             :                 case BIFF_DATATYPE_STRING:
    2361           0 :                     appendRawToken( OPCODE_PUSH ) <<= bBiff8 ?
    2362             :                         rStrm.readUniString( mbAllowNulChars ) :
    2363           0 :                         rStrm.readByteStringUC( false, getTextEncoding(), mbAllowNulChars );
    2364           0 :                 break;
    2365             :                 case BIFF_DATATYPE_BOOL:
    2366           0 :                     appendRawToken( OPCODE_PUSH ) <<= (static_cast< double >( (rStrm.readuInt8() == BIFF_TOK_BOOL_FALSE) ? 0.0 : 1.0 ));
    2367           0 :                     rStrm.skip( 7 );
    2368           0 :                 break;
    2369             :                 case BIFF_DATATYPE_ERROR:
    2370           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( rStrm.readuInt8() );
    2371           0 :                     rStrm.skip( 7 );
    2372           0 :                 break;
    2373             :                 default:
    2374             :                     OSL_FAIL( "BiffFormulaParserImpl::importArrayToken - unknown data type" );
    2375           0 :                     appendRawToken( OPCODE_PUSH ) <<= BiffHelper::calcDoubleFromError( BIFF_ERR_NA );
    2376             :             }
    2377             :         }
    2378             :     }
    2379           0 :     swapStreamPosition( rStrm );
    2380             : 
    2381             :     // close token array and set resulting operand size
    2382           0 :     appendRawToken( OPCODE_ARRAY_CLOSE );
    2383           0 :     pushOperandSize( nOpSize + getFormulaSize() - nOldArraySize );
    2384           0 :     return true;
    2385             : }
    2386             : 
    2387           0 : bool BiffFormulaParserImpl::importRefToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2388             : {
    2389           0 :     BinSingleRef2d aRef;
    2390           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2391           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2392             : }
    2393             : 
    2394           0 : bool BiffFormulaParserImpl::importRefToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2395             : {
    2396           0 :     BinSingleRef2d aRef;
    2397           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2398           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2399             : }
    2400             : 
    2401           0 : bool BiffFormulaParserImpl::importAreaToken2( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2402             : {
    2403           0 :     BinComplexRef2d aRef;
    2404           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2405           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2406             : }
    2407             : 
    2408           0 : bool BiffFormulaParserImpl::importAreaToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2409             : {
    2410           0 :     BinComplexRef2d aRef;
    2411           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2412           0 :     return pushBiffReference( aRef, bDeleted, bRelativeAsOffset );
    2413             : }
    2414             : 
    2415           0 : bool BiffFormulaParserImpl::importRef3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2416             : {
    2417           0 :     LinkSheetRange aSheetRange = readSheetRange5( rStrm );
    2418           0 :     BinSingleRef2d aRef;
    2419           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2420           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2421             : }
    2422             : 
    2423           0 : bool BiffFormulaParserImpl::importRef3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2424             : {
    2425           0 :     LinkSheetRange aSheetRange = readSheetRange8( rStrm );
    2426           0 :     BinSingleRef2d aRef;
    2427           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2428           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2429             : }
    2430             : 
    2431           0 : bool BiffFormulaParserImpl::importArea3dToken5( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2432             : {
    2433           0 :     LinkSheetRange aSheetRange = readSheetRange5( rStrm );
    2434           0 :     BinComplexRef2d aRef;
    2435           0 :     aRef.readBiff2Data( rStrm, bRelativeAsOffset );
    2436           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2437             : }
    2438             : 
    2439           0 : bool BiffFormulaParserImpl::importArea3dToken8( BiffInputStream& rStrm, bool bDeleted, bool bRelativeAsOffset )
    2440             : {
    2441           0 :     LinkSheetRange aSheetRange = readSheetRange8( rStrm );
    2442           0 :     BinComplexRef2d aRef;
    2443           0 :     aRef.readBiff8Data( rStrm, bRelativeAsOffset );
    2444           0 :     return pushReferenceOperand( aSheetRange, aRef, bDeleted, bRelativeAsOffset );
    2445             : }
    2446             : 
    2447           0 : bool BiffFormulaParserImpl::importMemAreaToken( BiffInputStream& rStrm, bool bAddData )
    2448             : {
    2449           0 :     rStrm.skip( mnMemAreaSize );
    2450           0 :     if( bAddData )
    2451           0 :         skipMemAreaAddData( rStrm );
    2452           0 :     return true;
    2453             : }
    2454             : 
    2455           0 : bool BiffFormulaParserImpl::importMemFuncToken( BiffInputStream& rStrm )
    2456             : {
    2457           0 :     rStrm.skip( mnMemFuncSize );
    2458           0 :     return true;
    2459             : }
    2460             : 
    2461           0 : bool BiffFormulaParserImpl::importNameToken( BiffInputStream& rStrm )
    2462             : {
    2463           0 :     sal_uInt16 nNameId = readNameId( rStrm );
    2464           0 :     return (mnCurrRefId > 0) ? pushBiffExtName( mnCurrRefId, nNameId ) : pushBiffName( nNameId );
    2465             : }
    2466             : 
    2467           0 : bool BiffFormulaParserImpl::importNameXToken( BiffInputStream& rStrm )
    2468             : {
    2469           0 :     sal_Int32 nRefId = readRefId( rStrm );
    2470           0 :     sal_uInt16 nNameId = readNameId( rStrm );
    2471           0 :     return pushBiffExtName( nRefId, nNameId );
    2472             : }
    2473             : 
    2474           0 : bool BiffFormulaParserImpl::importFuncToken2( BiffInputStream& rStrm )
    2475             : {
    2476             :     sal_uInt8 nFuncId;
    2477           0 :     rStrm >> nFuncId;
    2478           0 :     return pushBiffFunction( nFuncId );
    2479             : }
    2480             : 
    2481           0 : bool BiffFormulaParserImpl::importFuncToken4( BiffInputStream& rStrm )
    2482             : {
    2483             :     sal_uInt16 nFuncId;
    2484           0 :     rStrm >> nFuncId;
    2485           0 :     return pushBiffFunction( nFuncId );
    2486             : }
    2487             : 
    2488           0 : bool BiffFormulaParserImpl::importFuncVarToken2( BiffInputStream& rStrm )
    2489             : {
    2490             :     sal_uInt8 nParamCount, nFuncId;
    2491           0 :     rStrm >> nParamCount >> nFuncId;
    2492           0 :     return pushBiffFunction( nFuncId, nParamCount );
    2493             : }
    2494             : 
    2495           0 : bool BiffFormulaParserImpl::importFuncVarToken4( BiffInputStream& rStrm )
    2496             : {
    2497             :     sal_uInt8 nParamCount;
    2498             :     sal_uInt16 nFuncId;
    2499           0 :     rStrm >> nParamCount >> nFuncId;
    2500           0 :     return pushBiffFunction( nFuncId, nParamCount & BIFF_TOK_FUNCVAR_COUNTMASK );
    2501             : }
    2502             : 
    2503           0 : bool BiffFormulaParserImpl::importFuncCEToken( BiffInputStream& rStrm )
    2504             : {
    2505             :     sal_uInt8 nParamCount, nFuncId;
    2506           0 :     rStrm >> nParamCount >> nFuncId;
    2507           0 :     sal_uInt16 nCmdId = nFuncId;
    2508           0 :     setFlag( nCmdId, BIFF_TOK_FUNCVAR_CMD );
    2509           0 :     return pushBiffFunction( nCmdId, nParamCount );
    2510             : }
    2511             : 
    2512           0 : bool BiffFormulaParserImpl::importExpToken( BiffInputStream& rStrm )
    2513             : {
    2514           0 :     BinAddress aBaseAddr;
    2515           0 :     aBaseAddr.read( rStrm );
    2516           0 :     return pushSpecialTokenOperand( aBaseAddr, false );
    2517             : }
    2518             : 
    2519           0 : bool BiffFormulaParserImpl::importTblToken( BiffInputStream& rStrm )
    2520             : {
    2521           0 :     BinAddress aBaseAddr;
    2522           0 :     aBaseAddr.read( rStrm );
    2523           0 :     return pushSpecialTokenOperand( aBaseAddr, true );
    2524             : }
    2525             : 
    2526           0 : bool BiffFormulaParserImpl::importNlrAddrToken( BiffInputStream& rStrm, bool bRow )
    2527             : {
    2528           0 :     BiffNlr aNlr;
    2529           0 :     aNlr.readBiff8Data( rStrm );
    2530           0 :     return pushBiffNlrAddr( aNlr, bRow );
    2531             : }
    2532             : 
    2533           0 : bool BiffFormulaParserImpl::importNlrRangeToken( BiffInputStream& rStrm )
    2534             : {
    2535           0 :     BiffNlr aNlr;
    2536           0 :     aNlr.readBiff8Data( rStrm );
    2537           0 :     rStrm.skip( 1 );
    2538           0 :     BinRange aRange;
    2539           0 :     rStrm >> aRange;
    2540           0 :     return pushBiffNlrRange( aNlr, aRange );
    2541             : }
    2542             : 
    2543           0 : bool BiffFormulaParserImpl::importNlrSAddrToken( BiffInputStream& rStrm, bool bRow )
    2544             : {
    2545           0 :     rStrm.skip( 4 );
    2546           0 :     BiffNlr aNlr;
    2547           0 :     return readNlrSAddrAddData( aNlr, rStrm, bRow ) ? pushBiffNlrSAddr( aNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2548             : }
    2549             : 
    2550           0 : bool BiffFormulaParserImpl::importNlrSRangeToken( BiffInputStream& rStrm )
    2551             : {
    2552           0 :     rStrm.skip( 5 );
    2553           0 :     BinRange aRange;
    2554           0 :     rStrm >> aRange;
    2555           0 :     BiffNlr aNlr;
    2556             :     bool bRow;
    2557           0 :     return readNlrSRangeAddData( aNlr, bRow, rStrm ) ? pushBiffNlrSRange( aNlr, aRange, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2558             : }
    2559             : 
    2560           0 : bool BiffFormulaParserImpl::importNlrErrToken( BiffInputStream& rStrm, sal_uInt16 nIgnore )
    2561             : {
    2562           0 :     rStrm.skip( nIgnore );
    2563           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    2564             : }
    2565             : 
    2566           0 : sal_Int32 BiffFormulaParserImpl::readRefId( BiffInputStream& rStrm )
    2567             : {
    2568             :     sal_Int16 nRefId;
    2569           0 :     rStrm >> nRefId;
    2570           0 :     rStrm.skip( mnRefIdSize );
    2571           0 :     return nRefId;
    2572             : }
    2573             : 
    2574           0 : sal_uInt16 BiffFormulaParserImpl::readNameId( BiffInputStream& rStrm )
    2575             : {
    2576             :     sal_uInt16 nNameId;
    2577           0 :     rStrm >> nNameId;
    2578           0 :     rStrm.skip( mnNameSize );
    2579           0 :     return nNameId;
    2580             : }
    2581             : 
    2582           0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange5( BiffInputStream& rStrm )
    2583             : {
    2584           0 :     sal_Int32 nRefId = readRefId( rStrm );
    2585             :     sal_Int16 nTab1, nTab2;
    2586           0 :     rStrm >> nTab1 >> nTab2;
    2587           0 :     return getExternalLinks().getSheetRange( nRefId, nTab1, nTab2 );
    2588             : }
    2589             : 
    2590           0 : LinkSheetRange BiffFormulaParserImpl::readSheetRange8( BiffInputStream& rStrm )
    2591             : {
    2592           0 :     return getExternalLinks().getSheetRange( readRefId( rStrm ) );
    2593             : }
    2594             : 
    2595           0 : void BiffFormulaParserImpl::swapStreamPosition( BiffInputStream& rStrm )
    2596             : {
    2597           0 :     sal_Int64 nRecPos = rStrm.tell();
    2598           0 :     rStrm.seek( mnAddDataPos );
    2599           0 :     mnAddDataPos = nRecPos;
    2600           0 : }
    2601             : 
    2602           0 : void BiffFormulaParserImpl::skipMemAreaAddData( BiffInputStream& rStrm )
    2603             : {
    2604           0 :     swapStreamPosition( rStrm );
    2605           0 :     sal_Int32 nCount = rStrm.readuInt16();
    2606           0 :     rStrm.skip( ((getBiff() == BIFF8) ? 8 : 6) * nCount );
    2607           0 :     swapStreamPosition( rStrm );
    2608           0 : }
    2609             : 
    2610           0 : bool BiffFormulaParserImpl::readNlrSAddrAddData( BiffNlr& orNlr, BiffInputStream& rStrm, bool bRow )
    2611             : {
    2612           0 :     bool bIsRow = false;
    2613           0 :     return readNlrSRangeAddData( orNlr, bIsRow, rStrm ) && (bIsRow == bRow);
    2614             : }
    2615             : 
    2616           0 : bool BiffFormulaParserImpl::readNlrSRangeAddData( BiffNlr& orNlr, bool& orbIsRow, BiffInputStream& rStrm )
    2617             : {
    2618           0 :     orbIsRow = false;
    2619           0 :     swapStreamPosition( rStrm );
    2620             :     // read number of cell addresses and relative flag
    2621             :     sal_uInt32 nCount;
    2622           0 :     rStrm >> nCount;
    2623           0 :     bool bRel = getFlag( nCount, BIFF_TOK_NLR_ADDREL );
    2624           0 :     nCount &= BIFF_TOK_NLR_ADDMASK;
    2625           0 :     sal_Int64 nEndPos = rStrm.tell() + 4 * nCount;
    2626             :     // read list of cell addresses
    2627           0 :     bool bValid = false;
    2628           0 :     if( nCount >= 2 )
    2629             :     {
    2630             :         // detect column/row orientation
    2631           0 :         BinAddress aAddr1, aAddr2;
    2632           0 :         rStrm >> aAddr1 >> aAddr2;
    2633           0 :         orbIsRow = aAddr1.mnRow == aAddr2.mnRow;
    2634           0 :         bValid = lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
    2635             :         // read and verify additional cell positions
    2636           0 :         for( sal_uInt32 nIndex = 2; bValid && (nIndex < nCount); ++nIndex )
    2637             :         {
    2638           0 :             aAddr1 = aAddr2;
    2639           0 :             rStrm >> aAddr2;
    2640           0 :             bValid = !rStrm.isEof() && lclIsValidNlrStack( aAddr1, aAddr2, orbIsRow );
    2641             :         }
    2642             :         // check that last imported position (aAddr2) is not at the end of the sheet
    2643           0 :         bValid = bValid && (orbIsRow ? (aAddr2.mnCol < mnMaxApiCol) : (aAddr2.mnRow < mnMaxApiRow));
    2644             :         // fill the NLR struct with the last imported position
    2645           0 :         if( bValid )
    2646             :         {
    2647           0 :             orNlr.mnCol = aAddr2.mnCol;
    2648           0 :             orNlr.mnRow = aAddr2.mnRow;
    2649           0 :             orNlr.mbRel = bRel;
    2650             :         }
    2651             :     }
    2652             :     // seek to end of additional data for this token
    2653           0 :     rStrm.seek( nEndPos );
    2654           0 :     swapStreamPosition( rStrm );
    2655             : 
    2656           0 :     return bValid;
    2657             : }
    2658             : 
    2659             : // convert BIFF token and push API operand or operator ------------------------
    2660             : 
    2661           0 : bool BiffFormulaParserImpl::pushBiffReference( const BinSingleRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
    2662             : {
    2663           0 :     return (mnCurrRefId > 0) ?
    2664           0 :         pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
    2665           0 :         pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
    2666             : }
    2667             : 
    2668           0 : bool BiffFormulaParserImpl::pushBiffReference( const BinComplexRef2d& rRef, bool bDeleted, bool bRelativeAsOffset )
    2669             : {
    2670           0 :     return (mnCurrRefId > 0) ?
    2671           0 :         pushReferenceOperand( getExternalLinks().getSheetRange( mnCurrRefId, 0, 0 ), rRef, bDeleted, bRelativeAsOffset ) :
    2672           0 :         pushReferenceOperand( rRef, bDeleted, bRelativeAsOffset );
    2673             : }
    2674             : 
    2675           0 : bool BiffFormulaParserImpl::pushBiffNlrAddr( const BiffNlr& rNlr, bool bRow )
    2676             : {
    2677           0 :     BinSingleRef2d aRef;
    2678           0 :     aRef.mnCol = rNlr.mnCol;
    2679           0 :     aRef.mnRow = rNlr.mnRow;
    2680           0 :     aRef.mbColRel = !bRow;
    2681           0 :     aRef.mbRowRel = bRow;
    2682           0 :     return pushNlrOperand( aRef );
    2683             : }
    2684             : 
    2685           0 : bool BiffFormulaParserImpl::pushBiffNlrRange( const BiffNlr& rNlr, const BinRange& rRange )
    2686             : {
    2687           0 :     bool bRow = rNlr.mnRow == rRange.maFirst.mnRow;
    2688           0 :     return lclIsValidNlrRange( rNlr, rRange, bRow ) ?
    2689           0 :         pushBiffNlrAddr( rNlr, bRow ) : pushBiffErrorOperand( BIFF_ERR_REF );
    2690             : }
    2691             : 
    2692           0 : bool BiffFormulaParserImpl::pushBiffNlrSAddr( const BiffNlr& rNlr, bool bRow )
    2693             : {
    2694           0 :     BinRange aRange;
    2695           0 :     aRange.maFirst.mnCol = rNlr.mnCol + (bRow ? 1 : 0);
    2696           0 :     aRange.maFirst.mnRow = rNlr.mnRow + (bRow ? 0 : 1);
    2697           0 :     aRange.maLast.mnCol = bRow ? mnMaxApiCol : rNlr.mnCol;
    2698           0 :     aRange.maLast.mnRow = bRow ? rNlr.mnRow : mnMaxApiRow;
    2699           0 :     return pushBiffNlrSRange( rNlr, aRange, bRow );
    2700             : }
    2701             : 
    2702           0 : bool BiffFormulaParserImpl::pushBiffNlrSRange( const BiffNlr& rNlr, const BinRange& rRange, bool bRow )
    2703             : {
    2704           0 :     if( lclIsValidNlrRange( rNlr, rRange, bRow ) )
    2705             :     {
    2706           0 :         BinComplexRef2d aRef;
    2707           0 :         aRef.maRef1.mnCol = rRange.maFirst.mnCol;
    2708           0 :         aRef.maRef1.mnRow = rRange.maFirst.mnRow;
    2709           0 :         aRef.maRef2.mnCol = rRange.maLast.mnCol;
    2710           0 :         aRef.maRef2.mnRow = rRange.maLast.mnRow;
    2711           0 :         aRef.maRef1.mbColRel = aRef.maRef2.mbColRel = !bRow && rNlr.mbRel;
    2712           0 :         aRef.maRef1.mbRowRel = aRef.maRef2.mbRowRel = bRow && rNlr.mbRel;
    2713           0 :         return pushReferenceOperand( aRef, false, false );
    2714             :     }
    2715           0 :     return pushBiffErrorOperand( BIFF_ERR_REF );
    2716             : }
    2717             : 
    2718           0 : bool BiffFormulaParserImpl::pushBiffName( sal_uInt16 nNameId )
    2719             : {
    2720             :     // one-based in BIFF formulas
    2721           0 :     return pushDefinedNameOperand( getDefinedNames().getByIndex( static_cast< sal_Int32 >( nNameId ) - 1 ) );
    2722             : }
    2723             : 
    2724           0 : bool BiffFormulaParserImpl::pushBiffExtName( sal_Int32 nRefId, sal_uInt16 nNameId )
    2725             : {
    2726           0 :     if( const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId ).get() )
    2727             :     {
    2728           0 :         if( pExtLink->getLinkType() == LINKTYPE_SELF )
    2729           0 :             return pushBiffName( nNameId );
    2730             :         // external name indexes are one-based in BIFF
    2731           0 :         ExternalNameRef xExtName = pExtLink->getNameByIndex( static_cast< sal_Int32 >( nNameId ) - 1 );
    2732           0 :         return pushExternalNameOperand( xExtName, *pExtLink );
    2733             :     }
    2734           0 :     return pushBiffErrorOperand( BIFF_ERR_NAME );
    2735             : }
    2736             : 
    2737           0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId )
    2738             : {
    2739           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
    2740           0 :         if( pFuncInfo->mnMinParamCount == pFuncInfo->mnMaxParamCount )
    2741           0 :             return pushFunctionOperator( *pFuncInfo, pFuncInfo->mnMinParamCount );
    2742           0 :     return pushFunctionOperator( OPCODE_NONAME, 0 );
    2743             : }
    2744             : 
    2745           0 : bool BiffFormulaParserImpl::pushBiffFunction( sal_uInt16 nFuncId, sal_uInt8 nParamCount )
    2746             : {
    2747           0 :     if( getFlag( nFuncId, BIFF_TOK_FUNCVAR_CMD ) )
    2748           0 :         nParamCount &= BIFF_TOK_FUNCVAR_COUNTMASK;
    2749           0 :     if( const FunctionInfo* pFuncInfo = getFuncInfoFromBiffFuncId( nFuncId ) )
    2750           0 :         return pushFunctionOperator( *pFuncInfo, nParamCount );
    2751           0 :     return pushFunctionOperator( OPCODE_NONAME, nParamCount );
    2752             : }
    2753             : 
    2754             : namespace {
    2755             : 
    2756             : /** Extracts the reference identifier and the remaining data from a formula in
    2757             :     the format '[RefID]Remaining'. */
    2758           0 : bool lclExtractRefId( sal_Int32& rnRefId, OUString& rRemainder, const OUString& rFormulaString )
    2759             : {
    2760           0 :     if( (rFormulaString.getLength() >= 4) && (rFormulaString[ 0 ] == '[') )
    2761             :     {
    2762           0 :         sal_Int32 nBracketClose = rFormulaString.indexOf( ']', 1 );
    2763           0 :         if( nBracketClose >= 2 )
    2764             :         {
    2765           0 :             rnRefId = rFormulaString.copy( 1, nBracketClose - 1 ).toInt32();
    2766           0 :             rRemainder = rFormulaString.copy( nBracketClose + 1 );
    2767           0 :             return !rRemainder.isEmpty();
    2768             :         }
    2769             :     }
    2770           0 :     return false;
    2771             : }
    2772             : 
    2773             : }
    2774             : 
    2775         414 : FormulaParser::FormulaParser( const WorkbookHelper& rHelper ) :
    2776         414 :     FormulaProcessorBase( rHelper )
    2777             : {
    2778         414 :     switch( getFilterType() )
    2779             :     {
    2780         414 :         case FILTER_OOXML:  mxImpl.reset( new OoxFormulaParserImpl( *this ) );  break;
    2781           0 :         case FILTER_BIFF:   mxImpl.reset( new BiffFormulaParserImpl( *this ) ); break;
    2782           0 :         case FILTER_UNKNOWN: break;
    2783             :     }
    2784         414 : }
    2785             : 
    2786         828 : FormulaParser::~FormulaParser()
    2787             : {
    2788         828 : }
    2789             : 
    2790         272 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, const OUString& rFormulaString ) const
    2791             : {
    2792         272 :     return mxImpl->importOoxFormula( rBaseAddress, rFormulaString );
    2793             : }
    2794             : 
    2795          35 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, SequenceInputStream& rStrm ) const
    2796             : {
    2797          35 :     return mxImpl->importBiff12Formula( rBaseAddress, eType, rStrm );
    2798             : }
    2799             : 
    2800           0 : ApiTokenSequence FormulaParser::importFormula( const CellAddress& rBaseAddress, FormulaType eType, BiffInputStream& rStrm, const sal_uInt16* pnFmlaSize ) const
    2801             : {
    2802           0 :     return mxImpl->importBiffFormula( rBaseAddress, eType, rStrm, pnFmlaSize );
    2803             : }
    2804             : 
    2805           0 : ApiTokenSequence FormulaParser::convertErrorToFormula( sal_uInt8 nErrorCode ) const
    2806             : {
    2807           0 :     ApiTokenSequence aTokens( 3 );
    2808             :     // HACK: enclose all error codes into an 1x1 matrix
    2809           0 :     aTokens[ 0 ].OpCode = OPCODE_ARRAY_OPEN;
    2810           0 :     aTokens[ 1 ].OpCode = OPCODE_PUSH;
    2811           0 :     aTokens[ 1 ].Data <<= BiffHelper::calcDoubleFromError( nErrorCode );
    2812           0 :     aTokens[ 2 ].OpCode = OPCODE_ARRAY_CLOSE;
    2813           0 :     return aTokens;
    2814             : }
    2815             : 
    2816           0 : OUString FormulaParser::importOleTargetLink( const OUString& rFormulaString )
    2817             : {
    2818           0 :     sal_Int32 nRefId = -1;
    2819           0 :     OUString aRemainder;
    2820           0 :     if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() >= 3) &&
    2821           0 :             (aRemainder[ 0 ] == '!') && (aRemainder[ 1 ] == '\'') && (aRemainder[ aRemainder.getLength() - 1 ] == '\'') )
    2822           0 :         return mxImpl->resolveOleTarget( nRefId, false );
    2823           0 :     return OUString();
    2824             : }
    2825             : 
    2826           0 : OUString FormulaParser::importOleTargetLink( SequenceInputStream& rStrm )
    2827             : {
    2828           0 :     OUString aTargetLink;
    2829           0 :     sal_Int32 nFmlaSize = rStrm.readInt32();
    2830           0 :     sal_Int64 nFmlaEndPos = rStrm.tell() + ::std::max< sal_Int32 >( nFmlaSize, 0 );
    2831           0 :     if( (nFmlaSize == 7) && (rStrm.getRemaining() >= 7) )
    2832             :     {
    2833           0 :         sal_uInt8 nToken = rStrm.readuChar();
    2834           0 :         sal_Int16 nRefId = rStrm.readInt16();
    2835           0 :         rStrm.skip(4); //nNameId
    2836           0 :         if( nToken == (BIFF_TOKCLASS_VAL|BIFF_TOKID_NAMEX) )
    2837           0 :             aTargetLink = mxImpl->resolveOleTarget( nRefId, true );
    2838             :     }
    2839           0 :     rStrm.seek( nFmlaEndPos );
    2840           0 :     return aTargetLink;
    2841             : }
    2842             : 
    2843           0 : OUString FormulaParser::importMacroName( const OUString& rFormulaString )
    2844             : {
    2845             :     /*  Valid macros are either sheet macros or VBA macros. OOXML and all BIFF
    2846             :         documents store defined names for sheet macros, but OOXML documents do
    2847             :         not store any defined name for VBA macros (while BIFF documents do).
    2848             :         Sheet macros may be defined locally to a sheet, or globally to the
    2849             :         document. As a result, all of the following macro specifiers are valid:
    2850             : 
    2851             :         1) Macros located in the own document:
    2852             :             [0]!MySheetMacro    (global sheet macro 'MySheetMacro')
    2853             :             Macro1!MyMacro      (sheet-local sheet macro 'MyMacro')
    2854             :             [0]!MyVBAProc       (VBA macro 'MyVBAProc')
    2855             :             [0]!Mod1.MyVBAProc  (VBA macro 'MyVBAProc' from code module 'Mod1')
    2856             : 
    2857             :         2) Macros from an external document:
    2858             :             [2]!MySheetMacro    (global external sheet macro 'MySheetMacro')
    2859             :             [2]Macro1!MyMacro   (sheet-local external sheet macro 'MyMacro')
    2860             :             [2]!MyVBAProc       (external VBA macro 'MyVBAProc')
    2861             :             [2]!Mod1.MyVBAProc  (external VBA macro from code module 'Mod1')
    2862             : 
    2863             :         This implementation is only interested in VBA macros from the own
    2864             :         document, ignoring the valid syntax 'Macro1!MyMacro' for sheet-local
    2865             :         sheet macros.
    2866             :      */
    2867           0 :     sal_Int32 nRefId = -1;
    2868           0 :     OUString aRemainder;
    2869           0 :     if( lclExtractRefId( nRefId, aRemainder, rFormulaString ) && (aRemainder.getLength() > 1) && (aRemainder[ 0 ] == '!') )
    2870             :     {
    2871             :         /*  In BIFF12 documents, the reference identifier is always the
    2872             :             one-based index of the external link as it is in OOXML documents
    2873             :             (it is not an index into the list of reference sheets as used in
    2874             :             cell formulas). Index 0 is an implicit placeholder for the own
    2875             :             document. In BIFF12 documents, the reference to the own document is
    2876             :             stored explicitly, mostly at the top of the list, so index 1 may
    2877             :             resolve to the own document too.
    2878             :             Passing 'false' to getExternalLink() specifies to ignore the
    2879             :             reference sheets list (if existing) and to access the list of
    2880             :             external links directly. */
    2881           0 :         const ExternalLink* pExtLink = getExternalLinks().getExternalLink( nRefId, false ).get();
    2882             :         OSL_ENSURE( pExtLink, "FormulaParser::importMacroName - missing link" );
    2883             :         // do not accept macros in external documents (not supported)
    2884           0 :         if( pExtLink && (pExtLink->getLinkType() == LINKTYPE_SELF) )
    2885             :         {
    2886             :             // ignore sheet macros (defined name for VBA macros may not exist, see above)
    2887           0 :             OUString aMacroName = aRemainder.copy( 1 );
    2888           0 :             const DefinedName* pDefName = getDefinedNames().getByModelName( aMacroName ).get();
    2889           0 :             if( !pDefName || pDefName->isVBName() )
    2890           0 :                 return aMacroName;
    2891             :         }
    2892             :     }
    2893           0 :     return OUString();
    2894             : }
    2895             : 
    2896             : } // namespace xls
    2897          30 : } // namespace oox
    2898             : 
    2899             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11