Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / helper / binaryinputstream.cxx
blob61677a1d1725679709db7d8c44c9efc31e730972
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/helper/binaryinputstream.hxx"
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 <osl/diagnose.h>
29 #include "oox/helper/binaryoutputstream.hxx"
31 namespace oox {
33 using namespace ::com::sun::star::io;
34 using namespace ::com::sun::star::uno;
36 namespace {
38 const sal_Int32 INPUTSTREAM_BUFFERSIZE = 0x8000;
40 } // namespace
42 OUString BinaryInputStream::readNulUnicodeArray()
44 OUStringBuffer aBuffer;
45 for (;;)
47 sal_uInt16 nChar = readuInt16();
48 if ( mbEof || (nChar == 0) ) break;
49 aBuffer.append( static_cast< sal_Unicode >( nChar ) );
51 return aBuffer.makeStringAndClear();
54 OString BinaryInputStream::readCharArray( sal_Int32 nChars, bool bAllowNulChars )
56 if( nChars <= 0 )
57 return OString();
59 ::std::vector< sal_uInt8 > aBuffer;
60 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
61 if( nCharsRead <= 0 )
62 return OString();
64 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
65 if( !bAllowNulChars )
66 ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
68 return OString( reinterpret_cast< sal_Char* >( &aBuffer.front() ), nCharsRead );
71 OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
73 return OStringToOUString( readCharArray( nChars, bAllowNulChars ), eTextEnc );
76 OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars, bool bAllowNulChars )
78 if( nChars <= 0 )
79 return OUString();
81 ::std::vector< sal_uInt16 > aBuffer;
82 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
83 if( nCharsRead <= 0 )
84 return OUString();
86 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
87 if( !bAllowNulChars )
88 ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
90 OUStringBuffer aStringBuffer;
91 aStringBuffer.ensureCapacity( nCharsRead );
92 for( ::std::vector< sal_uInt16 >::iterator aIt = aBuffer.begin(), aEnd = aBuffer.end(); aIt != aEnd; ++aIt )
93 aStringBuffer.append( static_cast< sal_Unicode >( *aIt ) );
94 return aStringBuffer.makeStringAndClear();
97 OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed, bool bAllowNulChars )
99 return bCompressed ?
100 // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
101 readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars ) :
102 readUnicodeArray( nChars, bAllowNulChars );
105 void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm, sal_Int64 nBytes, sal_Int32 nAtomSize )
107 if( nBytes > 0 )
109 // make buffer size a multiple of the passed atom size
110 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, (INPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
111 StreamDataSequence aBuffer( nBufferSize );
112 while( nBytes > 0 )
114 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
115 sal_Int32 nBytesRead = readData( aBuffer, nReadSize, nAtomSize );
116 rOutStrm.writeData( aBuffer );
117 if( nReadSize == nBytesRead )
118 nBytes -= nReadSize;
119 else
120 nBytes = 0;
125 BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
126 BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
127 BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
128 maBuffer( INPUTSTREAM_BUFFERSIZE ),
129 mxInStrm( rxInStrm ),
130 mbAutoClose( bAutoClose && rxInStrm.is() )
132 mbEof = !mxInStrm.is();
135 BinaryXInputStream::~BinaryXInputStream()
137 close();
140 void BinaryXInputStream::close()
142 OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
143 if( mxInStrm.is() ) try
145 mxInStrm->closeInput();
147 catch( Exception& )
149 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
151 mxInStrm.clear();
152 mbAutoClose = false;
153 BinaryXSeekableStream::close();
156 sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
158 sal_Int32 nRet = 0;
159 if( !mbEof && (nBytes > 0) ) try
161 nRet = mxInStrm->readBytes( orData, nBytes );
162 mbEof = nRet != nBytes;
164 catch( Exception& )
166 mbEof = true;
168 return nRet;
171 sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
173 sal_Int32 nRet = 0;
174 if( !mbEof && (nBytes > 0) )
176 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
177 sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem );
178 while( !mbEof && (nBytes > 0) )
180 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
181 sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
182 if( nBytesRead > 0 )
183 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
184 opnMem += nBytesRead;
185 nBytes -= nBytesRead;
186 nRet += nBytesRead;
189 return nRet;
192 void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
194 if( !mbEof ) try
196 mxInStrm->skipBytes( nBytes );
198 catch( Exception& )
200 mbEof = true;
204 SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
205 BinaryStreamBase( true ),
206 SequenceSeekableStream( rData )
210 sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
212 sal_Int32 nReadBytes = 0;
213 if( !mbEof )
215 nReadBytes = getMaxBytes( nBytes );
216 orData.realloc( nReadBytes );
217 if( nReadBytes > 0 )
218 memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
219 mnPos += nReadBytes;
220 mbEof = nReadBytes < nBytes;
222 return nReadBytes;
225 sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
227 sal_Int32 nReadBytes = 0;
228 if( !mbEof )
230 nReadBytes = getMaxBytes( nBytes );
231 if( nReadBytes > 0 )
232 memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
233 mnPos += nReadBytes;
234 mbEof = nReadBytes < nBytes;
236 return nReadBytes;
239 void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
241 if( !mbEof )
243 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
244 mnPos += nSkipBytes;
245 mbEof = nSkipBytes < nBytes;
249 RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
250 BinaryStreamBase( rInStrm.isSeekable() ),
251 mpInStrm( &rInStrm ),
252 mnStartPos( rInStrm.tell() ),
253 mnRelPos( 0 )
255 sal_Int64 nRemaining = rInStrm.getRemaining();
256 mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
257 mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
260 sal_Int64 RelativeInputStream::size() const
262 return mpInStrm ? mnSize : -1;
265 sal_Int64 RelativeInputStream::tell() const
267 return mpInStrm ? mnRelPos : -1;
270 void RelativeInputStream::seek( sal_Int64 nPos )
272 if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
274 mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
275 mpInStrm->seek( mnStartPos + mnRelPos );
276 mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
280 void RelativeInputStream::close()
282 mpInStrm = 0;
283 mbEof = true;
286 sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
288 sal_Int32 nReadBytes = 0;
289 if( !mbEof )
291 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
292 nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
293 mnRelPos += nReadBytes;
294 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
296 return nReadBytes;
299 sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
301 sal_Int32 nReadBytes = 0;
302 if( !mbEof )
304 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
305 nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
306 mnRelPos += nReadBytes;
307 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
309 return nReadBytes;
312 void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
314 if( !mbEof )
316 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
317 mpInStrm->skip( nSkipBytes, nAtomSize );
318 mnRelPos += nSkipBytes;
319 mbEof = nSkipBytes < nBytes;
323 } // namespace oox
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */