LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/oox/source/helper - binaryinputstream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 83 165 50.3 %
Date: 2013-07-09 Functions: 13 29 44.8 %
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 "oox/helper/binaryinputstream.hxx"
      21             : 
      22             : #include <com/sun/star/io/XInputStream.hpp>
      23             : #include <com/sun/star/io/XSeekable.hpp>
      24             : #include <string.h>
      25             : #include <vector>
      26             : #include <rtl/strbuf.hxx>
      27             : #include <rtl/ustrbuf.hxx>
      28             : #include "oox/helper/binaryoutputstream.hxx"
      29             : 
      30             : namespace oox {
      31             : 
      32             : // ============================================================================
      33             : 
      34             : using namespace ::com::sun::star::io;
      35             : using namespace ::com::sun::star::uno;
      36             : 
      37             : namespace {
      38             : 
      39             : const sal_Int32 INPUTSTREAM_BUFFERSIZE      = 0x8000;
      40             : 
      41             : } // namespace
      42             : 
      43             : // ============================================================================
      44             : 
      45           1 : OUString BinaryInputStream::readNulUnicodeArray()
      46             : {
      47           1 :     OUStringBuffer aBuffer;
      48          11 :     for( sal_uInt16 nChar = readuInt16(); !mbEof && (nChar > 0); readValue( nChar ) )
      49          10 :         aBuffer.append( static_cast< sal_Unicode >( nChar ) );
      50           1 :     return aBuffer.makeStringAndClear();
      51             : }
      52             : 
      53         537 : OString BinaryInputStream::readCharArray( sal_Int32 nChars, bool bAllowNulChars )
      54             : {
      55         537 :     if( nChars <= 0 )
      56         144 :         return OString();
      57             : 
      58         393 :     ::std::vector< sal_uInt8 > aBuffer;
      59         393 :     sal_Int32 nCharsRead = readArray( aBuffer, nChars );
      60         393 :     if( nCharsRead <= 0 )
      61           0 :         return OString();
      62             : 
      63         393 :     aBuffer.resize( static_cast< size_t >( nCharsRead ) );
      64         393 :     if( !bAllowNulChars )
      65         393 :         ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
      66             : 
      67         393 :     return OString( reinterpret_cast< sal_Char* >( &aBuffer.front() ), nCharsRead );
      68             : }
      69             : 
      70         537 : OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
      71             : {
      72         537 :     return OStringToOUString( readCharArray( nChars, bAllowNulChars ), eTextEnc );
      73             : }
      74             : 
      75           0 : OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars, bool bAllowNulChars )
      76             : {
      77           0 :     if( nChars <= 0 )
      78           0 :         return OUString();
      79             : 
      80           0 :     ::std::vector< sal_uInt16 > aBuffer;
      81           0 :     sal_Int32 nCharsRead = readArray( aBuffer, nChars );
      82           0 :     if( nCharsRead <= 0 )
      83           0 :         return OUString();
      84             : 
      85           0 :     aBuffer.resize( static_cast< size_t >( nCharsRead ) );
      86           0 :     if( !bAllowNulChars )
      87           0 :         ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
      88             : 
      89           0 :     OUStringBuffer aStringBuffer;
      90           0 :     aStringBuffer.ensureCapacity( nCharsRead );
      91           0 :     for( ::std::vector< sal_uInt16 >::iterator aIt = aBuffer.begin(), aEnd = aBuffer.end(); aIt != aEnd; ++aIt )
      92           0 :         aStringBuffer.append( static_cast< sal_Unicode >( *aIt ) );
      93           0 :     return aStringBuffer.makeStringAndClear();
      94             : }
      95             : 
      96          77 : OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed, bool bAllowNulChars )
      97             : {
      98             :     return bCompressed ?
      99             :          // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
     100             :         readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars ) :
     101          77 :         readUnicodeArray( nChars, bAllowNulChars );
     102             : }
     103             : 
     104         597 : void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm, sal_Int64 nBytes, sal_Int32 nAtomSize )
     105             : {
     106         597 :     if( nBytes > 0 )
     107             :     {
     108             :         // make buffer size a multiple of the passed atom size
     109         597 :         sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, (INPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
     110         597 :         StreamDataSequence aBuffer( nBufferSize );
     111        1793 :         while( nBytes > 0 )
     112             :         {
     113         599 :             sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
     114         599 :             sal_Int32 nBytesRead = readData( aBuffer, nReadSize, nAtomSize );
     115         599 :             rOutStrm.writeData( aBuffer );
     116         599 :             if( nReadSize == nBytesRead )
     117           2 :                 nBytes -= nReadSize;
     118             :             else
     119         597 :                 nBytes = 0;
     120         597 :         }
     121             :     }
     122         597 : }
     123             : 
     124             : // ============================================================================
     125             : 
     126         877 : BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
     127        1754 :     BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
     128             :     BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
     129             :     maBuffer( INPUTSTREAM_BUFFERSIZE ),
     130             :     mxInStrm( rxInStrm ),
     131        1754 :     mbAutoClose( bAutoClose && rxInStrm.is() )
     132             : {
     133         877 :     mbEof = !mxInStrm.is();
     134         877 : }
     135             : 
     136        2631 : BinaryXInputStream::~BinaryXInputStream()
     137             : {
     138         877 :     close();
     139        1754 : }
     140             : 
     141         877 : void BinaryXInputStream::close()
     142             : {
     143             :     OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
     144         877 :     if( mxInStrm.is() ) try
     145             :     {
     146         714 :         mxInStrm->closeInput();
     147             :     }
     148           0 :     catch( Exception& )
     149             :     {
     150             :         OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
     151             :     }
     152         877 :     mxInStrm.clear();
     153         877 :     mbAutoClose = false;
     154         877 :     BinaryXSeekableStream::close();
     155         877 : }
     156             : 
     157       94955 : sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
     158             : {
     159       94955 :     sal_Int32 nRet = 0;
     160       94955 :     if( !mbEof && (nBytes > 0) ) try
     161             :     {
     162       94792 :         nRet = mxInStrm->readBytes( orData, nBytes );
     163       94792 :         mbEof = nRet != nBytes;
     164             :     }
     165           0 :     catch( Exception& )
     166             :     {
     167           0 :         mbEof = true;
     168             :     }
     169       94955 :     return nRet;
     170             : }
     171             : 
     172       94296 : sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
     173             : {
     174       94296 :     sal_Int32 nRet = 0;
     175       94296 :     if( !mbEof && (nBytes > 0) )
     176             :     {
     177       94296 :         sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
     178       94296 :         sal_uInt8* opnMem = reinterpret_cast< sal_uInt8* >( opMem );
     179      282888 :         while( !mbEof && (nBytes > 0) )
     180             :         {
     181       94296 :             sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
     182       94296 :             sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
     183       94296 :             if( nBytesRead > 0 )
     184       94152 :                 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
     185       94296 :             opnMem += nBytesRead;
     186       94296 :             nBytes -= nBytesRead;
     187       94296 :             nRet += nBytesRead;
     188             :         }
     189             :     }
     190       94296 :     return nRet;
     191             : }
     192             : 
     193         784 : void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
     194             : {
     195         784 :     if( !mbEof ) try
     196             :     {
     197         784 :         mxInStrm->skipBytes( nBytes );
     198             :     }
     199           0 :     catch( Exception& )
     200             :     {
     201           0 :         mbEof = true;
     202             :     }
     203         784 : }
     204             : 
     205             : // ============================================================================
     206             : 
     207        2218 : SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
     208             :     BinaryStreamBase( true ),
     209        2218 :     SequenceSeekableStream( rData )
     210             : {
     211        2218 : }
     212             : 
     213           0 : sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
     214             : {
     215           0 :     sal_Int32 nReadBytes = 0;
     216           0 :     if( !mbEof )
     217             :     {
     218           0 :         nReadBytes = getMaxBytes( nBytes );
     219           0 :         orData.realloc( nReadBytes );
     220           0 :         if( nReadBytes > 0 )
     221           0 :             memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
     222           0 :         mnPos += nReadBytes;
     223           0 :         mbEof = nReadBytes < nBytes;
     224             :     }
     225           0 :     return nReadBytes;
     226             : }
     227             : 
     228         516 : sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
     229             : {
     230         516 :     sal_Int32 nReadBytes = 0;
     231         516 :     if( !mbEof )
     232             :     {
     233         516 :         nReadBytes = getMaxBytes( nBytes );
     234         516 :         if( nReadBytes > 0 )
     235         516 :             memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
     236         516 :         mnPos += nReadBytes;
     237         516 :         mbEof = nReadBytes < nBytes;
     238             :     }
     239         516 :     return nReadBytes;
     240             : }
     241             : 
     242           0 : void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
     243             : {
     244           0 :     if( !mbEof )
     245             :     {
     246           0 :         sal_Int32 nSkipBytes = getMaxBytes( nBytes );
     247           0 :         mnPos += nSkipBytes;
     248           0 :         mbEof = nSkipBytes < nBytes;
     249             :     }
     250           0 : }
     251             : 
     252             : // ============================================================================
     253             : 
     254           0 : RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
     255           0 :     BinaryStreamBase( rInStrm.isSeekable() ),
     256             :     mpInStrm( &rInStrm ),
     257           0 :     mnStartPos( rInStrm.tell() ),
     258           0 :     mnRelPos( 0 )
     259             : {
     260           0 :     sal_Int64 nRemaining = rInStrm.getRemaining();
     261           0 :     mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
     262           0 :     mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
     263           0 : }
     264             : 
     265           0 : sal_Int64 RelativeInputStream::size() const
     266             : {
     267           0 :     return mpInStrm ? mnSize : -1;
     268             : }
     269             : 
     270           0 : sal_Int64 RelativeInputStream::tell() const
     271             : {
     272           0 :     return mpInStrm ? mnRelPos : -1;
     273             : }
     274             : 
     275           0 : void RelativeInputStream::seek( sal_Int64 nPos )
     276             : {
     277           0 :     if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
     278             :     {
     279           0 :         mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
     280           0 :         mpInStrm->seek( mnStartPos + mnRelPos );
     281           0 :         mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
     282             :     }
     283           0 : }
     284             : 
     285           0 : void RelativeInputStream::close()
     286             : {
     287           0 :     mpInStrm = 0;
     288           0 :     mbEof = true;
     289           0 : }
     290             : 
     291           0 : sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
     292             : {
     293           0 :     sal_Int32 nReadBytes = 0;
     294           0 :     if( !mbEof )
     295             :     {
     296           0 :         sal_Int32 nMaxBytes = getMaxBytes( nBytes );
     297           0 :         nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
     298           0 :         mnRelPos += nReadBytes;
     299           0 :         mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
     300             :     }
     301           0 :     return nReadBytes;
     302             : }
     303             : 
     304           0 : sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
     305             : {
     306           0 :     sal_Int32 nReadBytes = 0;
     307           0 :     if( !mbEof )
     308             :     {
     309           0 :         sal_Int32 nMaxBytes = getMaxBytes( nBytes );
     310           0 :         nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
     311           0 :         mnRelPos += nReadBytes;
     312           0 :         mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
     313             :     }
     314           0 :     return nReadBytes;
     315             : }
     316             : 
     317           0 : void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
     318             : {
     319           0 :     if( !mbEof )
     320             :     {
     321           0 :         sal_Int32 nSkipBytes = getMaxBytes( nBytes );
     322           0 :         mpInStrm->skip( nSkipBytes, nAtomSize );
     323           0 :         mnRelPos += nSkipBytes;
     324           0 :         mbEof = nSkipBytes < nBytes;
     325             :     }
     326           0 : }
     327             : 
     328             : // ============================================================================
     329             : 
     330             : } // namespace oox
     331             : 
     332             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10