LCOV - code coverage report
Current view: top level - sot/source/sdstor - stgelem.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 254 266 95.5 %
Date: 2014-11-03 Functions: 26 28 92.9 %
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             : 
      21             : #include <string.h>
      22             : #include <rtl/ustring.hxx>
      23             : #include <com/sun/star/lang/Locale.hpp>
      24             : #include <unotools/charclass.hxx>
      25             : #include "sot/stg.hxx"
      26             : #include "stgelem.hxx"
      27             : #include "stgcache.hxx"
      28             : #include "stgstrms.hxx"
      29             : #include "stgdir.hxx"
      30             : #include "stgio.hxx"
      31             : 
      32             : static const sal_uInt16 nMaxLegalStr = 31;
      33             : 
      34             : static const sal_uInt8 cStgSignature[ 8 ] = { 0xD0,0xCF,0x11,0xE0,0xA1,0xB1,0x1A,0xE1 };
      35             : 
      36             : ////////////////////////////// struct ClsId
      37             : 
      38       13402 : SvStream& ReadClsId( SvStream& r, ClsId& rId )
      39             : {
      40       13402 :     r.ReadUInt32( rId.Data1 )
      41       26804 :      .ReadUInt16( rId.Data2 )
      42       26804 :      .ReadUInt16( rId.Data3 )
      43       26804 :      .ReadUChar( rId.Data4[0] )
      44       26804 :      .ReadUChar( rId.Data4[1] )
      45       26804 :      .ReadUChar( rId.Data4[2] )
      46       26804 :      .ReadUChar( rId.Data4[3] )
      47       26804 :      .ReadUChar( rId.Data4[4] )
      48       26804 :      .ReadUChar( rId.Data4[5] )
      49       26804 :      .ReadUChar( rId.Data4[6] )
      50       26804 :      .ReadUChar( rId.Data4[7] );
      51       13402 :     return r;
      52             : }
      53             : 
      54        7211 : SvStream& WriteClsId( SvStream& r, const ClsId& rId )
      55             : {
      56             :     return
      57        7211 :        r .WriteUInt32( rId.Data1 )
      58       14422 :          .WriteUInt16( rId.Data2 )
      59       14422 :          .WriteUInt16( rId.Data3 )
      60       14422 :          .WriteUChar( rId.Data4[0] )
      61       14422 :          .WriteUChar( rId.Data4[1] )
      62       14422 :          .WriteUChar( rId.Data4[2] )
      63       14422 :          .WriteUChar( rId.Data4[3] )
      64       14422 :          .WriteUChar( rId.Data4[4] )
      65       14422 :          .WriteUChar( rId.Data4[5] )
      66       14422 :          .WriteUChar( rId.Data4[6] )
      67       14422 :          .WriteUChar( rId.Data4[7] );
      68             : }
      69             : 
      70             : ///////////////////////////// class StgHeader
      71             : 
      72        3568 : StgHeader::StgHeader()
      73             : : nVersion( 0 )
      74             : , nByteOrder( 0 )
      75             : , nPageSize( 0 )
      76             : , nDataPageSize( 0 )
      77             : , bDirty( 0 )
      78             : , nFATSize( 0 )
      79             : , nTOCstrm( 0 )
      80             : , nReserved( 0 )
      81             : , nThreshold( 0 )
      82             : , nDataFAT( 0 )
      83             : , nDataFATSize( 0 )
      84             : , nMasterChain( 0 )
      85        3568 : , nMaster( 0 )
      86             : {
      87        3568 :     memset( cSignature, 0, sizeof( cSignature ) );
      88        3568 :     memset( &aClsId, 0, sizeof( ClsId ) );
      89        3568 :     memset( cReserved, 0, sizeof( cReserved ) );
      90        3568 :     memset( nMasterFAT, 0, sizeof( nMasterFAT ) );
      91        3568 : }
      92             : 
      93         351 : void StgHeader::Init()
      94             : {
      95         351 :     memcpy( cSignature, cStgSignature, 8 );
      96         351 :     memset( &aClsId, 0, sizeof( ClsId ) );
      97         351 :     nVersion      = 0x0003003B;
      98         351 :     nByteOrder    = 0xFFFE;
      99         351 :     nPageSize     = 9;          // 512 bytes
     100         351 :     nDataPageSize = 6;          // 64 bytes
     101         351 :     bDirty = 0;
     102         351 :     memset( cReserved, 0, sizeof( cReserved ) );
     103         351 :     nFATSize = 0;
     104         351 :     nTOCstrm = 0;
     105         351 :     nReserved = 0;
     106         351 :     nThreshold    = 4096;
     107         351 :     nDataFAT = 0;
     108         351 :     nDataFATSize  = 0;
     109         351 :     nMasterChain  = STG_EOF;
     110             : 
     111         351 :     SetTOCStart( STG_EOF );
     112         351 :     SetDataFATStart( STG_EOF );
     113       38610 :     for( short i = 0; i < cFATPagesInHeader; i++ )
     114       38259 :         SetFATPage( i, STG_FREE );
     115         351 : }
     116             : 
     117        1703 : bool StgHeader::Load( StgIo& rIo )
     118             : {
     119        1703 :     bool bResult = false;
     120        1703 :     if ( rIo.GetStrm() )
     121             :     {
     122        1703 :         SvStream& r = *rIo.GetStrm();
     123        1703 :         bResult = Load( r );
     124        1703 :         bResult = ( bResult && rIo.Good() );
     125             :     }
     126             : 
     127        1703 :     return bResult;
     128             : }
     129             : 
     130        3189 : bool StgHeader::Load( SvStream& r )
     131             : {
     132        3189 :     r.Seek( 0L );
     133        3189 :     r.Read( cSignature, 8 );
     134        3189 :     ReadClsId( r, aClsId );         // 08 Class ID
     135        3189 :     r.ReadInt32( nVersion )                   // 1A version number
     136        6378 :      .ReadUInt16( nByteOrder )                 // 1C Unicode byte order indicator
     137        6378 :      .ReadInt16( nPageSize )                  // 1E 1 << nPageSize = block size
     138        6378 :      .ReadInt16( nDataPageSize );             // 20 1 << this size == data block size
     139        3189 :     r.SeekRel( 10 );
     140        3189 :     r.ReadInt32( nFATSize )                   // 2C total number of FAT pages
     141        6378 :      .ReadInt32( nTOCstrm )                   // 30 starting page for the TOC stream
     142        6378 :      .ReadInt32( nReserved )                  // 34
     143        6378 :      .ReadInt32( nThreshold )                 // 38 minimum file size for big data
     144        6378 :      .ReadInt32( nDataFAT )                   // 3C page # of 1st data FAT block
     145        6378 :      .ReadInt32( nDataFATSize )               // 40 # of data FATpages
     146        6378 :      .ReadInt32( nMasterChain )               // 44 chain to the next master block
     147        6378 :      .ReadInt32( nMaster );                   // 48 # of additional master blocks
     148      350790 :     for( short i = 0; i < cFATPagesInHeader; i++ )
     149      347601 :         r.ReadInt32( nMasterFAT[ i ] );
     150             : 
     151        3189 :     return (r.GetErrorCode() == ERRCODE_NONE) && Check();
     152             : }
     153             : 
     154         713 : bool StgHeader::Store( StgIo& rIo )
     155             : {
     156         713 :     if( !bDirty )
     157           0 :         return true;
     158             : 
     159         713 :     SvStream& r = *rIo.GetStrm();
     160         713 :     r.Seek( 0L );
     161         713 :     r.Write( cSignature, 8 );
     162         713 :     WriteClsId( r, aClsId );                   // 08 Class ID
     163         713 :     r.WriteInt32( nVersion )                   // 1A version number
     164        1426 :      .WriteUInt16( nByteOrder )                 // 1C Unicode byte order indicator
     165        1426 :      .WriteInt16( nPageSize )                  // 1E 1 << nPageSize = block size
     166        1426 :      .WriteInt16( nDataPageSize )              // 20 1 << this size == data block size
     167         713 :      .WriteInt32( 0 ).WriteInt32( 0 ).WriteInt16(  0 )
     168        1426 :      .WriteInt32( nFATSize )                   // 2C total number of FAT pages
     169        1426 :      .WriteInt32( nTOCstrm )                   // 30 starting page for the TOC stream
     170        1426 :      .WriteInt32( nReserved )                  // 34
     171        1426 :      .WriteInt32( nThreshold )                 // 38 minimum file size for big data
     172        1426 :      .WriteInt32( nDataFAT )                   // 3C page # of 1st data FAT block
     173        1426 :      .WriteInt32( nDataFATSize )               // 40 # of data FAT pages
     174        1426 :      .WriteInt32( nMasterChain )               // 44 chain to the next master block
     175        1426 :      .WriteInt32( nMaster );                   // 48 # of additional master blocks
     176       78430 :     for( short i = 0; i < cFATPagesInHeader; i++ )
     177       77717 :         r.WriteInt32( nMasterFAT[ i ] );
     178         713 :     bDirty = !rIo.Good();
     179         713 :     return !bDirty;
     180             : }
     181             : 
     182        6948 : static bool lcl_wontoverflow(short shift)
     183             : {
     184        6948 :     return shift >= 0 && shift < (short)sizeof(short) * 8 - 1;
     185             : }
     186             : 
     187        7119 : static bool isKnownSpecial(sal_Int32 nLocation)
     188             : {
     189        7115 :     return (nLocation == STG_FREE ||
     190        3164 :             nLocation == STG_EOF ||
     191       10283 :             nLocation == STG_FAT ||
     192        7119 :             nLocation == STG_MASTER);
     193             : }
     194             : 
     195             : // Perform thorough checks also on unknown variables
     196        4922 : bool StgHeader::Check()
     197             : {
     198        4922 :     return  memcmp( cSignature, cStgSignature, 8 ) == 0
     199        3474 :             && (short) ( nVersion >> 16 ) == 3
     200        3474 :             && nPageSize == 9
     201        3474 :             && lcl_wontoverflow(nPageSize)
     202        3474 :             && lcl_wontoverflow(nDataPageSize)
     203        3474 :             && nFATSize > 0
     204        3474 :             && nTOCstrm >= 0
     205        3474 :             && nThreshold > 0
     206        3474 :             && ( isKnownSpecial(nDataFAT) || ( nDataFAT >= 0 && nDataFATSize > 0 ) )
     207        3474 :             && ( isKnownSpecial(nMasterChain) || nMasterChain >=0 )
     208        8396 :             && nMaster >= 0;
     209             : }
     210             : 
     211      256650 : sal_Int32 StgHeader::GetFATPage( short n ) const
     212             : {
     213      256650 :     if( n >= 0 && n < cFATPagesInHeader )
     214      256650 :         return nMasterFAT[ n ];
     215             :     else
     216           0 :         return STG_EOF;
     217             : }
     218             : 
     219       38634 : void StgHeader::SetFATPage( short n, sal_Int32 nb )
     220             : {
     221       38634 :     if( n >= 0 && n < cFATPagesInHeader )
     222             :     {
     223       38634 :         if( nMasterFAT[ n ] != nb )
     224       38634 :             bDirty = sal_True, nMasterFAT[ n ] = nb;
     225             :     }
     226       38634 : }
     227             : 
     228        1785 : void StgHeader::SetTOCStart( sal_Int32 n )
     229             : {
     230        1785 :     if( n != nTOCstrm ) bDirty = sal_True, nTOCstrm = n;
     231        1785 : }
     232             : 
     233        1064 : void StgHeader::SetDataFATStart( sal_Int32 n )
     234             : {
     235        1064 :     if( n != nDataFAT ) bDirty = sal_True, nDataFAT = n;
     236        1064 : }
     237             : 
     238         713 : void StgHeader::SetDataFATSize( sal_Int32 n )
     239             : {
     240         713 :     if( n != nDataFATSize ) bDirty = sal_True, nDataFATSize = n;
     241         713 : }
     242             : 
     243         375 : void StgHeader::SetFATSize( sal_Int32 n )
     244             : {
     245         375 :     if( n != nFATSize ) bDirty = sal_True, nFATSize = n;
     246         375 : }
     247             : 
     248           0 : void StgHeader::SetFATChain( sal_Int32 n )
     249             : {
     250           0 :     if( n != nMasterChain )
     251           0 :         bDirty = sal_True, nMasterChain = n;
     252           0 : }
     253             : 
     254           0 : void StgHeader::SetMasters( sal_Int32 n )
     255             : {
     256           0 :     if( n != nMaster ) bDirty = sal_True, nMaster = n;
     257           0 : }
     258             : 
     259             : ///////////////////////////// class StgEntry
     260             : 
     261       32534 : bool StgEntry::Init()
     262             : {
     263       32534 :     memset( nName, 0, sizeof( nName ) );
     264       32534 :     nNameLen = 0;
     265       32534 :     cType = 0;
     266       32534 :     cFlags = 0;
     267       32534 :     nLeft = 0;
     268       32534 :     nRight = 0;
     269       32534 :     nChild = 0;
     270       32534 :     memset( &aClsId, 0, sizeof( aClsId ) );
     271       32534 :     nFlags = 0;
     272       32534 :     nMtime[0] = 0; nMtime[1] = 0;
     273       32534 :     nAtime[0] = 0; nAtime[1] = 0;
     274       32534 :     nPage1 = 0;
     275       32534 :     nSize = 0;
     276       32534 :     nUnknown = 0;
     277             : 
     278       32534 :     SetLeaf( STG_LEFT,  STG_FREE );
     279       32534 :     SetLeaf( STG_RIGHT, STG_FREE );
     280       32534 :     SetLeaf( STG_CHILD, STG_FREE );
     281       32534 :     SetLeaf( STG_DATA,  STG_EOF );
     282       32534 :     return true;
     283             : }
     284             : 
     285       41972 : static OUString ToUpperUnicode( const OUString & rStr )
     286             : {
     287             :     // I don't know the locale, so en_US is hopefully fine
     288       41972 :     static CharClass aCC( LanguageTag( com::sun::star::lang::Locale( "en", "US", "" )) );
     289       41972 :     return aCC.uppercase( rStr );
     290             : }
     291             : 
     292       31813 : bool StgEntry::SetName( const OUString& rName )
     293             : {
     294             :     // I don't know the locale, so en_US is hopefully fine
     295       31813 :     aName = ToUpperUnicode( rName );
     296       31813 :     if(aName.getLength() > nMaxLegalStr)
     297             :     {
     298           0 :         aName = aName.copy(0, nMaxLegalStr);
     299             :     }
     300             : 
     301             :     sal_uInt16 i;
     302      295121 :     for( i = 0; i < rName.getLength() && i <= nMaxLegalStr; i++ )
     303             :     {
     304      263308 :         nName[ i ] = rName[ i ];
     305             :     }
     306      818334 :     while (i <= nMaxLegalStr)
     307             :     {
     308      754708 :         nName[ i++ ] = 0;
     309             :     }
     310       31813 :     nNameLen = ( rName.getLength() + 1 ) << 1;
     311       31813 :     return true;
     312             : }
     313             : 
     314       47513 : sal_Int32 StgEntry::GetLeaf( StgEntryRef eRef ) const
     315             : {
     316       47513 :     sal_Int32 n = -1;
     317       47513 :     switch( eRef )
     318             :     {
     319       10159 :     case STG_LEFT:  n = nLeft;  break;
     320       10159 :     case STG_RIGHT: n = nRight; break;
     321       14927 :     case STG_CHILD: n = nChild; break;
     322       12268 :     case STG_DATA:  n = nPage1; break;
     323             :     }
     324       47513 :     return n;
     325             : }
     326             : 
     327      154109 : void StgEntry::SetLeaf( StgEntryRef eRef, sal_Int32 nPage )
     328             : {
     329      154109 :     switch( eRef )
     330             :     {
     331       37355 :     case STG_LEFT:  nLeft  = nPage; break;
     332       37355 :     case STG_RIGHT: nRight = nPage; break;
     333       37355 :     case STG_CHILD: nChild = nPage; break;
     334       42044 :     case STG_DATA:  nPage1 = nPage; break;
     335             :     }
     336      154109 : }
     337             : 
     338         344 : void StgEntry::SetClassId( const ClsId& r )
     339             : {
     340         344 :     memcpy( &aClsId, &r, sizeof( ClsId ) );
     341         344 : }
     342             : 
     343       11030 : void StgEntry::GetName( OUString& rName ) const
     344             : {
     345       11030 :     sal_uInt16 n = nNameLen;
     346       11030 :     if( n )
     347       11028 :         n = ( n >> 1 ) - 1;
     348       11030 :     rName = OUString(nName, n);
     349       11030 : }
     350             : 
     351             : // Compare two entries. Do this case-insensitive.
     352             : 
     353      220840 : short StgEntry::Compare( const StgEntry& r ) const
     354             : {
     355      220840 :     sal_Int32 nRes = r.nNameLen - nNameLen;
     356      220840 :     if( !nRes )
     357       75588 :         nRes = r.aName.compareTo( aName );
     358             : 
     359      220840 :     return (short)nRes;
     360             : }
     361             : 
     362             : // These load/store operations are a bit more complicated,
     363             : // since they have to copy their contents into a packed structure.
     364             : 
     365       10165 : bool StgEntry::Load( const void* pFrom, sal_uInt32 nBufSize )
     366             : {
     367       10165 :     if ( nBufSize < 128 )
     368           0 :         return false;
     369             : 
     370       10165 :     SvMemoryStream r( (sal_Char*) pFrom, nBufSize, STREAM_READ );
     371      335445 :     for( short i = 0; i < 32; i++ )
     372      325280 :         r.ReadUInt16( nName[ i ] );            // 00 name as WCHAR
     373       10165 :     r.ReadUInt16( nNameLen )                   // 40 size of name in bytes including 00H
     374       20330 :      .ReadUChar( cType )                      // 42 entry type
     375       20330 :      .ReadUChar( cFlags )                     // 43 0 or 1 (tree balance?)
     376       20330 :      .ReadInt32( nLeft )                      // 44 left node entry
     377       20330 :      .ReadInt32( nRight )                     // 48 right node entry
     378       20330 :      .ReadInt32( nChild );                    // 4C 1st child entry if storage
     379       10165 :     ReadClsId( r, aClsId );         // 50 class ID (optional)
     380       10165 :     r.ReadInt32( nFlags )                     // 60 state flags(?)
     381       20330 :      .ReadInt32( nMtime[ 0 ] )                // 64 modification time
     382       20330 :      .ReadInt32( nMtime[ 1 ] )                // 64 modification time
     383       20330 :      .ReadInt32( nAtime[ 0 ] )                // 6C creation and access time
     384       20330 :      .ReadInt32( nAtime[ 1 ] )                // 6C creation and access time
     385       20330 :      .ReadInt32( nPage1 )                     // 74 starting block (either direct or translated)
     386       20330 :      .ReadInt32( nSize )                      // 78 file size
     387       20330 :      .ReadInt32( nUnknown );                  // 7C unknown
     388             : 
     389       10165 :     sal_uInt16 n = nNameLen;
     390       10165 :     if( n )
     391       10161 :         n = ( n >> 1 ) - 1;
     392             : 
     393       10165 :     if (n > nMaxLegalStr)
     394           4 :         return false;
     395             : 
     396       10161 :     if ((cType != STG_STORAGE) && ((nSize < 0) || (nPage1 < 0 && !isKnownSpecial(nPage1))))
     397             :     {
     398             :         // the size makes no sense for the substorage
     399             :         // TODO/LATER: actually the size should be an unsigned value, but in this case it would mean a stream of more than 2Gb
     400           2 :         return false;
     401             :     }
     402             : 
     403       10159 :     aName = OUString(nName , n);
     404             :     // I don't know the locale, so en_US is hopefully fine
     405       10159 :     aName = ToUpperUnicode( aName );
     406       10159 :     if(aName.getLength() > nMaxLegalStr)
     407             :     {
     408           0 :         aName = aName.copy(0, nMaxLegalStr);
     409             :     }
     410             : 
     411       10159 :     return true;
     412             : }
     413             : 
     414        6436 : void StgEntry::Store( void* pTo )
     415             : {
     416        6436 :     SvMemoryStream r( (sal_Char *)pTo, 128, STREAM_WRITE );
     417      212388 :     for( short i = 0; i < 32; i++ )
     418      205952 :         r.WriteUInt16( nName[ i ] );            // 00 name as WCHAR
     419        6436 :     r.WriteUInt16( nNameLen )                   // 40 size of name in bytes including 00H
     420       12872 :      .WriteUChar( cType )                      // 42 entry type
     421       12872 :      .WriteUChar( cFlags )                     // 43 0 or 1 (tree balance?)
     422       12872 :      .WriteInt32( nLeft )                      // 44 left node entry
     423       12872 :      .WriteInt32( nRight )                     // 48 right node entry
     424       12872 :      .WriteInt32( nChild );                    // 4C 1st child entry if storage;
     425        6436 :     WriteClsId( r, aClsId );                   // 50 class ID (optional)
     426        6436 :     r.WriteInt32( nFlags )                     // 60 state flags(?)
     427       12872 :      .WriteInt32( nMtime[ 0 ] )                // 64 modification time
     428       12872 :      .WriteInt32( nMtime[ 1 ] )                // 64 modification time
     429       12872 :      .WriteInt32( nAtime[ 0 ] )                // 6C creation and access time
     430       12872 :      .WriteInt32( nAtime[ 1 ] )                // 6C creation and access time
     431       12872 :      .WriteInt32( nPage1 )                     // 74 starting block (either direct or translated)
     432       12872 :      .WriteInt32( nSize )                      // 78 file size
     433       12872 :      .WriteInt32( nUnknown );                  // 7C unknown
     434        6436 : }
     435             : 
     436             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10