LCOV - code coverage report
Current view: top level - libreoffice/sot/source/sdstor - stgelem.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 245 255 96.1 %
Date: 2012-12-27 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> // memset(), memcpy()
      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 sal_uInt8 cStgSignature[ 8 ] = { 0xD0,0xCF,0x11,0xE0,0xA1,0xB1,0x1A,0xE1 };
      35             : 
      36             : ////////////////////////////// struct ClsId  /////////////////////////////
      37             : 
      38        1410 : SvStream& operator >>( SvStream& r, ClsId& rId )
      39             : {
      40        1410 :     r >> rId.n1
      41        2820 :       >> rId.n2
      42        2820 :       >> rId.n3
      43        2820 :       >> rId.n4
      44        2820 :       >> rId.n5
      45        2820 :       >> rId.n6
      46        2820 :       >> rId.n7
      47        2820 :       >> rId.n8
      48        2820 :       >> rId.n9
      49        2820 :       >> rId.n10
      50        2820 :       >> rId.n11;
      51        1410 :     return r;
      52             : }
      53             : 
      54         360 : SvStream& operator <<( SvStream& r, const ClsId& rId )
      55             : {
      56             :     return
      57         360 :        r  << (sal_Int32) rId.n1
      58         720 :           << (sal_Int16) rId.n2
      59         720 :           << (sal_Int16) rId.n3
      60         720 :           << (sal_uInt8) rId.n4
      61         720 :           << (sal_uInt8) rId.n5
      62         720 :           << (sal_uInt8) rId.n6
      63         720 :           << (sal_uInt8) rId.n7
      64         720 :           << (sal_uInt8) rId.n8
      65         720 :           << (sal_uInt8) rId.n9
      66         720 :           << (sal_uInt8) rId.n10
      67         720 :           << (sal_uInt8) rId.n11;
      68             : }
      69             : 
      70             : ///////////////////////////// class StgHeader ////////////////////////////
      71             : 
      72         512 : 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         512 : , nMaster( 0 )
      86             : {
      87         512 :     memset( cSignature, 0, sizeof( cSignature ) );
      88         512 :     memset( &aClsId, 0, sizeof( ClsId ) );
      89         512 :     memset( cReserved, 0, sizeof( cReserved ) );
      90         512 :     memset( nMasterFAT, 0, sizeof( nMasterFAT ) );
      91         512 : }
      92             : 
      93          16 : void StgHeader::Init()
      94             : {
      95          16 :     memcpy( cSignature, cStgSignature, 8 );
      96          16 :     memset( &aClsId, 0, sizeof( ClsId ) );
      97          16 :     nVersion      = 0x0003003B;
      98          16 :     nByteOrder    = 0xFFFE;
      99          16 :     nPageSize     = 9;          // 512 bytes
     100          16 :     nDataPageSize = 6;          // 64 bytes
     101          16 :     bDirty = 0;
     102          16 :     memset( cReserved, 0, sizeof( cReserved ) );
     103          16 :     nFATSize = 0;
     104          16 :     nTOCstrm = 0;
     105          16 :     nReserved = 0;
     106          16 :     nThreshold    = 4096;
     107          16 :     nDataFAT = 0;
     108          16 :     nDataFATSize  = 0;
     109          16 :     nMasterChain  = STG_EOF;
     110             : 
     111          16 :     SetTOCStart( STG_EOF );
     112          16 :     SetDataFATStart( STG_EOF );
     113        1760 :     for( short i = 0; i < 109; i++ )
     114        1744 :         SetFATPage( i, STG_FREE );
     115          16 : }
     116             : 
     117          86 : sal_Bool StgHeader::Load( StgIo& rIo )
     118             : {
     119          86 :     sal_Bool bResult = sal_False;
     120          86 :     if ( rIo.GetStrm() )
     121             :     {
     122          86 :         SvStream& r = *rIo.GetStrm();
     123          86 :         bResult = Load( r );
     124          86 :         bResult = ( bResult && rIo.Good() );
     125             :     }
     126             : 
     127          86 :     return bResult;
     128             : }
     129             : 
     130         496 : sal_Bool StgHeader::Load( SvStream& r )
     131             : {
     132         496 :     r.Seek( 0L );
     133         496 :     r.Read( cSignature, 8 );
     134         496 :     r >> aClsId                     // 08 Class ID
     135         992 :       >> nVersion                   // 1A version number
     136         992 :       >> nByteOrder                 // 1C Unicode byte order indicator
     137         992 :       >> nPageSize                  // 1E 1 << nPageSize = block size
     138         992 :       >> nDataPageSize;             // 20 1 << this size == data block size
     139         496 :     r.SeekRel( 10 );
     140         496 :     r >> nFATSize                   // 2C total number of FAT pages
     141         992 :       >> nTOCstrm                   // 30 starting page for the TOC stream
     142         992 :       >> nReserved                  // 34
     143         992 :       >> nThreshold                 // 38 minimum file size for big data
     144         992 :       >> nDataFAT                   // 3C page # of 1st data FAT block
     145         992 :       >> nDataFATSize               // 40 # of data FATpages
     146         992 :       >> nMasterChain               // 44 chain to the next master block
     147         992 :       >> nMaster;                   // 48 # of additional master blocks
     148       54560 :     for( short i = 0; i < 109; i++ )
     149       54064 :         r >> nMasterFAT[ i ];
     150             : 
     151         496 :     return ( r.GetErrorCode() == ERRCODE_NONE && Check() );
     152             : }
     153             : 
     154          32 : sal_Bool StgHeader::Store( StgIo& rIo )
     155             : {
     156          32 :     if( !bDirty )
     157           0 :         return sal_True;
     158          32 :     SvStream& r = *rIo.GetStrm();
     159          32 :     r.Seek( 0L );
     160          32 :     r.Write( cSignature, 8 + 16 );
     161          32 :     r << nVersion                   // 1A version number
     162          64 :       << nByteOrder                 // 1C Unicode byte order indicator
     163          64 :       << nPageSize                  // 1E 1 << nPageSize = block size
     164          64 :       << nDataPageSize              // 20 1 << this size == data block size
     165          32 :       << (sal_Int32) 0 << (sal_Int32) 0 << (sal_Int16) 0
     166          64 :       << nFATSize                   // 2C total number of FAT pages
     167          64 :       << nTOCstrm                   // 30 starting page for the TOC stream
     168          64 :       << nReserved                  // 34
     169          64 :       << nThreshold                 // 38 minimum file size for big data
     170          64 :       << nDataFAT                   // 3C page # of 1st data FAT block
     171          64 :       << nDataFATSize               // 40 # of data FAT pages
     172          64 :       << nMasterChain               // 44 chain to the next master block
     173          64 :       << nMaster;                   // 48 # of additional master blocks
     174        3520 :     for( short i = 0; i < 109; i++ )
     175        3488 :         r << nMasterFAT[ i ];
     176          32 :     bDirty = !rIo.Good();
     177          32 :     return sal_Bool( !bDirty );
     178             : }
     179             : 
     180         924 : static bool lcl_wontoverflow(short shift)
     181             : {
     182         924 :     return shift >= 0 && shift < (short)sizeof(short) * 8 - 1;
     183             : }
     184             : 
     185         929 : static bool isKnownSpecial(sal_Int32 nLocation)
     186             : {
     187             :     return (nLocation == STG_FREE ||
     188             :             nLocation == STG_EOF ||
     189             :             nLocation == STG_FAT ||
     190         929 :             nLocation == STG_MASTER);
     191             : }
     192             : 
     193             : // Perform thorough checks also on unknown variables
     194         726 : sal_Bool StgHeader::Check()
     195             : {
     196         726 :     return sal_Bool( memcmp( cSignature, cStgSignature, 8 ) == 0
     197             :             && (short) ( nVersion >> 16 ) == 3 )
     198             :             && nPageSize == 9
     199         462 :             && lcl_wontoverflow(nPageSize)
     200         462 :             && lcl_wontoverflow(nDataPageSize)
     201             :             && nFATSize > 0
     202             :             && nTOCstrm >= 0
     203             :             && nThreshold > 0
     204         462 :             && ( isKnownSpecial(nDataFAT) || ( nDataFAT >= 0 && nDataFATSize > 0 ) )
     205         462 :             && ( isKnownSpecial(nMasterChain) || nMasterChain >=0 )
     206        2574 :             && nMaster >= 0;
     207             : }
     208             : 
     209       28027 : sal_Int32 StgHeader::GetFATPage( short n ) const
     210             : {
     211       28027 :     if( n >= 0 && n < 109 )
     212       28027 :         return nMasterFAT[ n ];
     213             :     else
     214           0 :         return STG_EOF;
     215             : }
     216             : 
     217        1764 : void StgHeader::SetFATPage( short n, sal_Int32 nb )
     218             : {
     219        1764 :     if( n >= 0 && n < 109 )
     220             :     {
     221        1764 :         if( nMasterFAT[ n ] != nb )
     222        1764 :             bDirty = sal_True, nMasterFAT[ n ] = nb;
     223             :     }
     224        1764 : }
     225             : 
     226          83 : void StgHeader::SetTOCStart( sal_Int32 n )
     227             : {
     228          83 :     if( n != nTOCstrm ) bDirty = sal_True, nTOCstrm = n;
     229          83 : }
     230             : 
     231          48 : void StgHeader::SetDataFATStart( sal_Int32 n )
     232             : {
     233          48 :     if( n != nDataFAT ) bDirty = sal_True, nDataFAT = n;
     234          48 : }
     235             : 
     236          32 : void StgHeader::SetDataFATSize( sal_Int32 n )
     237             : {
     238          32 :     if( n != nDataFATSize ) bDirty = sal_True, nDataFATSize = n;
     239          32 : }
     240             : 
     241          20 : void StgHeader::SetFATSize( sal_Int32 n )
     242             : {
     243          20 :     if( n != nFATSize ) bDirty = sal_True, nFATSize = n;
     244          20 : }
     245             : 
     246           0 : void StgHeader::SetFATChain( sal_Int32 n )
     247             : {
     248           0 :     if( n != nMasterChain )
     249           0 :         bDirty = sal_True, nMasterChain = n;
     250           0 : }
     251             : 
     252           0 : void StgHeader::SetMasters( sal_Int32 n )
     253             : {
     254           0 :     if( n != nMaster ) bDirty = sal_True, nMaster = n;
     255           0 : }
     256             : 
     257             : ///////////////////////////// class StgEntry /////////////////////////////
     258             : 
     259        2282 : sal_Bool StgEntry::Init()
     260             : {
     261        2282 :     memset( nName, 0, sizeof( nName ) );
     262        2282 :     nNameLen = 0;
     263        2282 :     cType = 0;
     264        2282 :     cFlags = 0;
     265        2282 :     nLeft = 0;
     266        2282 :     nRight = 0;
     267        2282 :     nChild = 0;
     268        2282 :     memset( &aClsId, 0, sizeof( aClsId ) );
     269        2282 :     nFlags = 0;
     270        2282 :     nMtime[0] = 0; nMtime[1] = 0;
     271        2282 :     nAtime[0] = 0; nAtime[1] = 0;
     272        2282 :     nPage1 = 0;
     273        2282 :     nSize = 0;
     274        2282 :     nUnknown = 0;
     275             : 
     276        2282 :     SetLeaf( STG_LEFT,  STG_FREE );
     277        2282 :     SetLeaf( STG_RIGHT, STG_FREE );
     278        2282 :     SetLeaf( STG_CHILD, STG_FREE );
     279        2282 :     SetLeaf( STG_DATA,  STG_EOF );
     280        2282 :     return sal_True;
     281             : }
     282             : 
     283        3144 : static String ToUpperUnicode( const String & rStr )
     284             : {
     285             :     // I don't know the locale, so en_US is hopefully fine
     286        3144 :     static CharClass aCC( LanguageTag( com::sun::star::lang::Locale( "en", "US", "" )) );
     287        3144 :     return aCC.uppercase( rStr );
     288             : }
     289             : 
     290        2247 : sal_Bool StgEntry::SetName( const String& rName )
     291             : {
     292             :     // I don't know the locale, so en_US is hopefully fine
     293        2247 :     aName = ToUpperUnicode( rName );
     294        2247 :     aName.Erase( nMaxLegalStr );
     295             : 
     296             :     int i;
     297       22368 :     for( i = 0; i < aName.Len() && i < 32; i++ )
     298       20121 :         nName[ i ] = rName.GetChar( sal_uInt16( i ));
     299       56277 :     while( i < 32 )
     300       51783 :         nName[ i++ ] = 0;
     301        2247 :     nNameLen = ( aName.Len() + 1 ) << 1;
     302        2247 :     return sal_True;
     303             : }
     304             : 
     305        4122 : sal_Int32 StgEntry::GetLeaf( StgEntryRef eRef ) const
     306             : {
     307        4122 :     sal_Int32 n = -1;
     308        4122 :     switch( eRef )
     309             :     {
     310         897 :         case STG_LEFT:  n = nLeft;  break;
     311         897 :         case STG_RIGHT: n = nRight; break;
     312        1309 :         case STG_CHILD: n = nChild; break;
     313        1019 :         case STG_DATA:  n = nPage1; break;
     314             :     }
     315        4122 :     return n;
     316             : }
     317             : 
     318       10559 : void StgEntry::SetLeaf( StgEntryRef eRef, sal_Int32 nPage )
     319             : {
     320       10559 :     switch( eRef )
     321             :     {
     322        2568 :         case STG_LEFT:  nLeft  = nPage; break;
     323        2568 :         case STG_RIGHT: nRight = nPage; break;
     324        2568 :         case STG_CHILD: nChild = nPage; break;
     325        2855 :         case STG_DATA:  nPage1 = nPage; break;
     326             :     }
     327       10559 : }
     328             : 
     329          18 : void StgEntry::SetClassId( const ClsId& r )
     330             : {
     331          18 :     memcpy( &aClsId, &r, sizeof( ClsId ) );
     332          18 : }
     333             : 
     334        1007 : void StgEntry::GetName( String& rName ) const
     335             : {
     336        1007 :     sal_uInt16 n = nNameLen;
     337        1007 :     if( n )
     338        1006 :         n = ( n >> 1 ) - 1;
     339        1007 :     rName = rtl::OUString(nName, n);
     340        1007 : }
     341             : 
     342             : // Compare two entries. Do this case-insensitive.
     343             : 
     344       23203 : short StgEntry::Compare( const StgEntry& r ) const
     345             : {
     346             :     /*
     347             :     short nRes = r.nNameLen - nNameLen;
     348             :     if( !nRes ) return strcmp( r.aName, aName );
     349             :     else return nRes;
     350             :     */
     351       23203 :     sal_Int32 nRes = r.nNameLen - nNameLen;
     352       23203 :     if( !nRes )
     353        9738 :         nRes = r.aName.CompareTo( aName );
     354       23203 :     return (short)nRes;
     355             :     //return aName.CompareTo( r.aName );
     356             : }
     357             : 
     358             : // These load/store operations are a bit more complicated,
     359             : // since they have to copy their contents into a packed structure.
     360             : 
     361         900 : sal_Bool StgEntry::Load( const void* pFrom, sal_uInt32 nBufSize )
     362             : {
     363         900 :     if ( nBufSize < 128 )
     364           0 :         return sal_False;
     365             : 
     366         900 :     SvMemoryStream r( (sal_Char*) pFrom, nBufSize, STREAM_READ );
     367       29700 :     for( short i = 0; i < 32; i++ )
     368       28800 :         r >> nName[ i ];            // 00 name as WCHAR
     369         900 :     r >> nNameLen                   // 40 size of name in bytes including 00H
     370        1800 :       >> cType                      // 42 entry type
     371        1800 :       >> cFlags                     // 43 0 or 1 (tree balance?)
     372        1800 :       >> nLeft                      // 44 left node entry
     373        1800 :       >> nRight                     // 48 right node entry
     374        1800 :       >> nChild                     // 4C 1st child entry if storage
     375        1800 :       >> aClsId                     // 50 class ID (optional)
     376        1800 :       >> nFlags                     // 60 state flags(?)
     377        1800 :       >> nMtime[ 0 ]                // 64 modification time
     378        1800 :       >> nMtime[ 1 ]                // 64 modification time
     379        1800 :       >> nAtime[ 0 ]                // 6C creation and access time
     380        1800 :       >> nAtime[ 1 ]                // 6C creation and access time
     381        1800 :       >> nPage1                     // 74 starting block (either direct or translated)
     382        1800 :       >> nSize                      // 78 file size
     383        1800 :       >> nUnknown;                  // 7C unknown
     384             : 
     385         900 :     sal_uInt16 n = nNameLen;
     386         900 :     if( n )
     387         898 :         n = ( n >> 1 ) - 1;
     388             : 
     389         900 :     if (n > nMaxLegalStr)
     390           2 :         return sal_False;
     391             : 
     392         898 :     if ((cType != STG_STORAGE) && ((nSize < 0) || (nPage1 < 0 && !isKnownSpecial(nPage1))))
     393             :     {
     394             :         // the size makes no sense for the substorage
     395             :         // TODO/LATER: actually the size should be an unsigned value, but in this case it would mean a stream of more than 2Gb
     396           1 :         return sal_False;
     397             :     }
     398             : 
     399         897 :     aName = rtl::OUString( nName, n );
     400             :     // I don't know the locale, so en_US is hopefully fine
     401         897 :     aName = ToUpperUnicode( aName );
     402         897 :     aName.Erase( nMaxLegalStr );
     403             : 
     404         897 :     return sal_True;
     405             : }
     406             : 
     407         360 : void StgEntry::Store( void* pTo )
     408             : {
     409         360 :     SvMemoryStream r( (sal_Char *)pTo, 128, STREAM_WRITE );
     410       11880 :     for( short i = 0; i < 32; i++ )
     411       11520 :         r << nName[ i ];            // 00 name as WCHAR
     412         360 :     r << nNameLen                   // 40 size of name in bytes including 00H
     413         720 :       << cType                      // 42 entry type
     414         720 :       << cFlags                     // 43 0 or 1 (tree balance?)
     415         720 :       << nLeft                      // 44 left node entry
     416         720 :       << nRight                     // 48 right node entry
     417         720 :       << nChild                     // 4C 1st child entry if storage;
     418         720 :       << aClsId                     // 50 class ID (optional)
     419         720 :       << nFlags                     // 60 state flags(?)
     420         720 :       << nMtime[ 0 ]                // 64 modification time
     421         720 :       << nMtime[ 1 ]                // 64 modification time
     422         720 :       << nAtime[ 0 ]                // 6C creation and access time
     423         720 :       << nAtime[ 1 ]                // 6C creation and access time
     424         720 :       << nPage1                     // 74 starting block (either direct or translated)
     425         720 :       << nSize                      // 78 file size
     426         720 :       << nUnknown;                  // 7C unknown
     427         360 : }
     428             : 
     429             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10