LCOV - code coverage report
Current view: top level - desktop/source/deployment - dp_persmap.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 94 225 41.8 %
Date: 2014-11-03 Functions: 10 14 71.4 %
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 "dp_misc.h"
      21             : #include "dp_persmap.h"
      22             : #include <rtl/strbuf.hxx>
      23             : 
      24             : #ifndef DISABLE_BDB2PMAP
      25             : #  include <vector>
      26             : #endif
      27             : 
      28             : using namespace ::rtl;
      29             : 
      30             : // the persistent map is used to manage a handful of key-value string pairs
      31             : // this implementation replaces a rather heavy-weight berkeleydb integration
      32             : 
      33             : // the file backing up a persistent map consists of line pairs with
      34             : // - a key string   (encoded with chars 0x00..0x0F being escaped)
      35             : // - a value string (encoded with chars 0x00..0x0F being escaped)
      36             : 
      37             : namespace dp_misc
      38             : {
      39             : 
      40             : static const char PmapMagic[4] = {'P','m','p','1'};
      41             : 
      42         426 : PersistentMap::PersistentMap( OUString const & url_, bool readOnly )
      43             : :    m_MapFile( expandUnoRcUrl(url_) )
      44             : ,    m_bReadOnly( readOnly )
      45             : ,    m_bIsOpen( false )
      46         426 : ,    m_bToBeCreated( !readOnly )
      47         852 : ,    m_bIsDirty( false )
      48             : {
      49             : #ifndef DISABLE_BDB2PMAP
      50         426 :     m_MapFileName = expandUnoRcUrl( url_ );
      51             : #endif
      52         426 :     open();
      53         426 : }
      54             : 
      55           0 : PersistentMap::PersistentMap()
      56             : :    m_MapFile( OUString() )
      57             : ,    m_bReadOnly( false )
      58             : ,    m_bIsOpen( false )
      59             : ,    m_bToBeCreated( false )
      60           0 : ,    m_bIsDirty( false )
      61           0 : {}
      62             : 
      63         852 : PersistentMap::~PersistentMap()
      64             : {
      65         426 :     if( m_bIsDirty )
      66           0 :         flush();
      67         426 :     if( m_bIsOpen )
      68           4 :         m_MapFile.close();
      69         426 : }
      70             : 
      71             : 
      72             : // replace 0x00..0x0F with "%0".."%F"
      73             : // replace "%" with "%%"
      74          12 : static OString encodeString( const OString& rStr)
      75             : {
      76          12 :     const sal_Char* pChar = rStr.getStr();
      77          12 :     const sal_Int32 nLen = rStr.getLength();
      78          12 :     sal_Int32 i = nLen;
      79             :     // short circuit for the simple non-encoded case
      80         708 :     while( --i >= 0)
      81             :     {
      82         684 :         const unsigned char c = (unsigned char) *(pChar++);
      83         684 :         if( c <= 0x0F )
      84           0 :             break;
      85         684 :         if( c == '%')
      86           0 :             break;
      87             :     }
      88          12 :     if( i < 0)
      89          12 :         return rStr;
      90             : 
      91             :     // escape chars 0x00..0x0F with "%0".."%F"
      92           0 :     OStringBuffer aEncStr( nLen + 32);
      93           0 :     aEncStr.append( pChar - (nLen-i), nLen - i);
      94           0 :     while( --i >= 0)
      95             :     {
      96           0 :         unsigned char c = (unsigned char) *(pChar++);
      97           0 :         if( c <= 0x0F )
      98             :         {
      99           0 :             aEncStr.append( '%');
     100           0 :             c += (c <= 0x09) ? '0' : 'A'-10;
     101           0 :         } else if( c == '%')
     102           0 :             aEncStr.append( '%');
     103           0 :         aEncStr.append( c);
     104             :     }
     105             : 
     106           0 :     return aEncStr.makeStringAndClear();
     107             : }
     108             : 
     109             : // replace "%0".."%F" with 0x00..0x0F
     110             : // replace "%%" with "%"
     111           0 : static OString decodeString( const sal_Char* pEncChars, int nLen)
     112             : {
     113           0 :     const char* pChar = pEncChars;
     114           0 :     sal_Int32 i = nLen;
     115             :     // short circuit for the simple non-encoded case
     116           0 :     while( --i >= 0)
     117           0 :         if( *(pChar++) == '%')
     118           0 :             break;
     119           0 :     if( i < 0)
     120           0 :         return OString( pEncChars, nLen);
     121             : 
     122             :     // replace escaped chars with their decoded counterparts
     123           0 :     OStringBuffer aDecStr( nLen);
     124           0 :     pChar = pEncChars;
     125           0 :     for( i = nLen; --i >= 0;)
     126             :     {
     127           0 :         sal_Char c = *(pChar++);
     128             :         // handle escaped character
     129           0 :         if( c == '%')
     130             :         {
     131           0 :             --i;
     132             :             OSL_ASSERT( i >= 0);
     133           0 :             c = *(pChar++);
     134           0 :             if( ('0' <= c) && (c <= '9'))
     135           0 :                 c -= '0';
     136             :             else
     137             :             {
     138             :                 OSL_ASSERT( ('A' <= c) && (c <= 'F'));
     139           0 :                 c -= ('A'-10);
     140             :             }
     141             :         }
     142           0 :         aDecStr.append( c);
     143             :     }
     144             : 
     145           0 :     return aDecStr.makeStringAndClear();
     146             : }
     147             : 
     148         426 : bool PersistentMap::open()
     149             : {
     150             :     // open the existing file
     151         426 :     sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read;
     152         426 :     if( !m_bReadOnly)
     153         426 :         nOpenFlags |= osl_File_OpenFlag_Write;
     154             : 
     155         426 :     const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
     156         426 :     m_bIsOpen = (rcOpen == osl::File::E_None);
     157             : 
     158             :     // or create later if needed
     159         426 :     m_bToBeCreated &= (rcOpen == osl::File::E_NOENT) && !m_bIsOpen;
     160             : 
     161             : #ifndef DISABLE_BDB2PMAP
     162         426 :     if( m_bToBeCreated )
     163         426 :         importFromBDB();
     164             : #endif
     165             : 
     166         426 :     if( !m_bIsOpen)
     167         426 :         return m_bToBeCreated;
     168             : 
     169           0 :     return readAll();
     170             : }
     171             : 
     172             : 
     173           0 : bool PersistentMap::readAll()
     174             : {
     175             :     // prepare for re-reading the map-file
     176           0 :     const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
     177             :     (void)nRes;
     178           0 :     m_entries.clear();
     179             : 
     180             :     // read header and check magic
     181             :     char aHeaderBytes[ sizeof(PmapMagic)];
     182           0 :     sal_uInt64 nBytesRead = 0;
     183           0 :     m_MapFile.read( aHeaderBytes, sizeof(aHeaderBytes), nBytesRead);
     184             :     OSL_ASSERT( nBytesRead == sizeof(aHeaderBytes));
     185           0 :     if( nBytesRead != sizeof(aHeaderBytes))
     186           0 :         return false;
     187             :     // check header magic
     188           0 :     for( int i = 0; i < (int)sizeof(PmapMagic); ++i)
     189           0 :         if( aHeaderBytes[i] != PmapMagic[i])
     190           0 :             return false;
     191             : 
     192             :     // read key value pairs and add them to the map
     193           0 :     ByteSequence aKeyLine;
     194           0 :     ByteSequence aValLine;
     195             :     for(;;)
     196             :     {
     197             :         // read key-value line pair
     198             :         // an empty key name indicates the end of the line pairs
     199           0 :         if( m_MapFile.readLine( aKeyLine) != osl::File::E_None)
     200           0 :             return false;
     201           0 :         if( !aKeyLine.getLength())
     202           0 :             break;
     203           0 :         if( m_MapFile.readLine( aValLine) != osl::File::E_None)
     204           0 :             return false;
     205             :         // decode key and value strings
     206           0 :         const OString aKeyName = decodeString( (sal_Char*)aKeyLine.getConstArray(), aKeyLine.getLength());
     207           0 :         const OString aValName = decodeString( (sal_Char*)aValLine.getConstArray(), aValLine.getLength());
     208             :         // insert key-value pair into map
     209           0 :         add( aKeyName, aValName );
     210             :         // check end-of-file status
     211           0 :         sal_Bool bIsEOF = true;
     212           0 :         if( m_MapFile.isEndOfFile( &bIsEOF) != osl::File::E_None )
     213           0 :             return false;
     214           0 :         if( bIsEOF )
     215           0 :             break;
     216           0 :     }
     217             : 
     218           0 :     m_bIsDirty = false;
     219           0 :     return true;
     220             : }
     221             : 
     222          12 : void PersistentMap::flush()
     223             : {
     224          12 :     if( !m_bIsDirty)
     225           0 :         return;
     226             :     OSL_ASSERT( !m_bReadOnly);
     227          12 :     if( m_bToBeCreated && !m_entries.empty())
     228             :     {
     229           4 :         const sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
     230           4 :         const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
     231           4 :         m_bIsOpen = (rcOpen == osl::File::E_None);
     232           4 :         m_bToBeCreated = !m_bIsOpen;
     233             :     }
     234          12 :     if( !m_bIsOpen)
     235           0 :         return;
     236             : 
     237             :     // write header magic
     238          12 :     const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
     239             :     (void)nRes;
     240          12 :     sal_uInt64 nBytesWritten = 0;
     241          12 :     m_MapFile.write( PmapMagic, sizeof(PmapMagic), nBytesWritten);
     242             : 
     243             :     // write key value pairs
     244          12 :     t_string2string_map::const_iterator it = m_entries.begin();
     245          18 :     for(; it != m_entries.end(); ++it) {
     246             :         // write line for key
     247           6 :         const OString aKeyString = encodeString( (*it).first);
     248           6 :         const sal_Int32 nKeyLen = aKeyString.getLength();
     249           6 :         m_MapFile.write( aKeyString.getStr(), nKeyLen, nBytesWritten);
     250             :         OSL_ASSERT( nKeyLen == (sal_Int32)nBytesWritten);
     251           6 :         m_MapFile.write( "\n", 1, nBytesWritten);
     252             :         // write line for value
     253          12 :         const OString& rValString = encodeString( (*it).second);
     254           6 :         const sal_Int32 nValLen = rValString.getLength();
     255           6 :         m_MapFile.write( rValString.getStr(), nValLen, nBytesWritten);
     256             :         OSL_ASSERT( nValLen == (sal_Int32)nBytesWritten);
     257           6 :         m_MapFile.write( "\n", 1, nBytesWritten);
     258           6 :     }
     259             : 
     260             :     // write a file delimiter (an empty key-string)
     261          12 :     m_MapFile.write( "\n", 1, nBytesWritten);
     262             :     // truncate file here
     263             :     sal_uInt64 nNewFileSize;
     264          12 :     if( m_MapFile.getPos( nNewFileSize) == osl::File::E_None)
     265          12 :         m_MapFile.setSize( nNewFileSize);
     266             :     // flush to disk
     267          12 :     m_MapFile.sync();
     268             :     // the in-memory map now matches to the file on disk
     269          12 :     m_bIsDirty = false;
     270             : }
     271             : 
     272           0 : bool PersistentMap::has( OString const & key ) const
     273             : {
     274           0 :     return get( NULL, key );
     275             : }
     276             : 
     277          70 : bool PersistentMap::get( OString * value, OString const & key ) const
     278             : {
     279          70 :     t_string2string_map::const_iterator it = m_entries.find( key);
     280          70 :     if( it == m_entries.end())
     281          56 :         return false;
     282          14 :     if( value)
     283          14 :         *value = it->second;
     284          14 :     return true;
     285             : }
     286             : 
     287           6 : void PersistentMap::add( OString const & key, OString const & value )
     288             : {
     289           6 :     if( m_bReadOnly)
     290           6 :         return;
     291             :     typedef std::pair<t_string2string_map::iterator,bool> InsertRC;
     292           6 :     InsertRC r = m_entries.insert( t_string2string_map::value_type(key,value));
     293           6 :     m_bIsDirty = r.second;
     294             : }
     295             : 
     296             : 
     297           6 : void PersistentMap::put( OString const & key, OString const & value )
     298             : {
     299           6 :     add( key, value);
     300             :     // HACK: flush now as the extension manager does not seem
     301             :     //       to properly destruct this object in some situations
     302           6 :     if(m_bIsDirty)
     303           6 :         flush();
     304           6 : }
     305             : 
     306           6 : bool PersistentMap::erase( OString const & key, bool flush_immediately )
     307             : {
     308           6 :     if( m_bReadOnly)
     309           0 :         return false;
     310           6 :     size_t nCount = m_entries.erase( key);
     311           6 :     if( !nCount)
     312           0 :         return false;
     313           6 :     m_bIsDirty = true;
     314           6 :     if( flush_immediately)
     315           6 :         flush();
     316           6 :     return true;
     317             : }
     318             : 
     319             : #ifndef DISABLE_BDB2PMAP
     320         426 : bool PersistentMap::importFromBDB()
     321             : {
     322         426 :     if( m_bReadOnly)
     323           0 :         return false;
     324             : 
     325             :     // get the name of its BDB counterpart
     326         426 :     OUString aDBName = m_MapFileName;
     327         426 :     if( !aDBName.endsWith( ".pmap" ))
     328           0 :         return false;
     329         426 :     aDBName = aDBName.replaceAt( aDBName.getLength()-5, 5, ".db");
     330             : 
     331             :     // open the corresponding BDB file for reading
     332         852 :     osl::File aDBFile( aDBName);
     333         426 :     osl::File::RC rc = aDBFile.open( osl_File_OpenFlag_Read);
     334         426 :     if( rc != osl::File::E_None)
     335         426 :         return false;
     336           0 :     sal_uInt64 nFileSize = 0;
     337           0 :     if( aDBFile.getSize( nFileSize) != osl::File::E_None)
     338           0 :         return false;
     339             : 
     340             :     // read the BDB file
     341           0 :     std::vector<sal_uInt8> aRawBDB( nFileSize);
     342           0 :     for( sal_uInt64 nOfs = 0; nOfs < nFileSize;) {
     343           0 :         sal_uInt64 nBytesRead = 0;
     344           0 :         rc = aDBFile.read( (void*)&aRawBDB[nOfs], nFileSize - nOfs, nBytesRead);
     345           0 :         if( (rc != osl::File::E_None) || !nBytesRead)
     346           0 :             return false;
     347           0 :         nOfs += nBytesRead;
     348             :     }
     349             : 
     350             :     // check BDB file header for non_encrypted Hash format v4..9
     351           0 :     if( nFileSize < 0x0100)
     352           0 :         return false;
     353           0 :     if( aRawBDB[24] != 0) // only not-encrypted migration
     354           0 :         return false;
     355           0 :     if( aRawBDB[25] != 8) // we expect a P_HASHMETA page
     356           0 :         return false;
     357           0 :     const bool bLE = (aRawBDB[12]==0x61 && aRawBDB[13]==0x15 && aRawBDB[14]==0x06);
     358           0 :     const bool bBE = (aRawBDB[15]==0x61 && aRawBDB[14]==0x15 && aRawBDB[13]==0x06);
     359           0 :     if( bBE == bLE)
     360           0 :         return false;
     361           0 :     if( (aRawBDB[16] < 4) || (9 < aRawBDB[16])) // version
     362           0 :         return false;
     363             :     const sal_uInt64 nPgSize = bLE
     364           0 :     ?    (aRawBDB[20] + (aRawBDB[21]<<8) + (aRawBDB[22]<<16) + (aRawBDB[23]<<24))
     365           0 :     :    (aRawBDB[23] + (aRawBDB[22]<<8) + (aRawBDB[21]<<16) + (aRawBDB[20]<<24));
     366           0 :     const int nPgCount = nFileSize / nPgSize;
     367           0 :     if( nPgCount * nPgSize != nFileSize)
     368           0 :         return false;
     369             : 
     370             :     // find PackageManager's new_style entries
     371             :     // using a simple heuristic for BDB_Hash pages
     372           0 :     int nEntryCount = 0;
     373           0 :     for( int nPgNo = 1; nPgNo < nPgCount; ++nPgNo) {
     374             :         // parse the next _db_page
     375           0 :         const sal_uInt8* const pPage = &aRawBDB[ nPgNo * nPgSize];
     376           0 :         const sal_uInt8* const pEnd = pPage + nPgSize;
     377           0 :         const int nHfOffset = bLE ? (pPage[22] + (pPage[23]<<8)) : (pPage[23] + (pPage[22]<<8));
     378           0 :         if( nHfOffset <= 0)
     379           0 :             continue;
     380           0 :         const sal_uInt8* pCur = pPage + nHfOffset;
     381             :         // iterate through the entries
     382           0 :         for(; pCur < pEnd; ++pCur) {
     383           0 :             if( pCur[0] != 0x01)
     384           0 :                 continue;
     385             :             // get the value-candidate
     386           0 :             const sal_uInt8* pVal = pCur + 1;
     387           0 :             while( ++pCur < pEnd)
     388           0 :                 if( (*pCur < ' ') || ((*pCur > 0x7F) && (*pCur != 0xFF)))
     389             :                     break;
     390           0 :             if( pCur >= pEnd)
     391           0 :                 break;
     392           0 :             if( (pCur[0] != 0x01) || (pCur[1] != 0xFF))
     393           0 :                 continue;
     394           0 :             const OString aVal( (sal_Char*)pVal, pCur - pVal);
     395             :             // get the key-candidate
     396           0 :             const sal_uInt8* pKey = pCur + 1;
     397           0 :             while( ++pCur < pEnd)
     398           0 :                 if( (*pCur < ' ') || ((*pCur > 0x7F) && (*pCur != 0xFF)))
     399             :                     break;
     400           0 :             if( (pCur < pEnd) && (*pCur > 0x01))
     401           0 :                 continue;
     402           0 :             const OString aKey( (sal_Char*)pKey, pCur - pKey);
     403           0 :             --pCur; // prepare for next round by rewinding to end of key-string
     404             : 
     405             :             // add the key/value pair
     406           0 :             add( aKey, aVal);
     407           0 :             ++nEntryCount;
     408           0 :         }
     409             :     }
     410             : 
     411         426 :     return (nEntryCount > 0);
     412             : }
     413             : #endif // DISABLE_BDB2PMAP
     414             : 
     415             : }
     416             : 
     417             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10