Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / helper / binaryinputstream.cxx
blob84133f6cc6a6cd7d10fd6db5c71a87d21aba95cf
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 <algorithm>
26 #include <vector>
27 #include <rtl/strbuf.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <osl/diagnose.h>
30 #include <oox/helper/binaryoutputstream.hxx>
32 namespace oox {
34 using namespace ::com::sun::star::io;
35 using namespace ::com::sun::star::uno;
37 namespace {
39 const sal_Int32 INPUTSTREAM_BUFFERSIZE = 0x8000;
41 } // namespace
43 OUString BinaryInputStream::readNulUnicodeArray()
45 OUStringBuffer aBuffer;
46 for (;;)
48 sal_uInt16 nChar = readuInt16();
49 if ( mbEof || (nChar == 0) ) break;
50 aBuffer.append( static_cast< sal_Unicode >( nChar ) );
52 return aBuffer.makeStringAndClear();
55 OString BinaryInputStream::readCharArray( sal_Int32 nChars )
57 if( nChars <= 0 )
58 return OString();
60 ::std::vector< sal_uInt8 > aBuffer;
61 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
62 if( nCharsRead <= 0 )
63 return OString();
65 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
66 // NUL characters are replaced by question marks.
67 ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
69 return OString(reinterpret_cast<sal_Char*>(aBuffer.data()), nCharsRead);
72 OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc )
74 return OStringToOUString( readCharArray( nChars ), eTextEnc );
77 OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars )
79 if( nChars <= 0 )
80 return OUString();
82 ::std::vector< sal_uInt16 > aBuffer;
83 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
84 if( nCharsRead <= 0 )
85 return OUString();
87 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
88 // don't allow nul chars
89 ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
91 OUStringBuffer aStringBuffer;
92 aStringBuffer.ensureCapacity( nCharsRead );
93 for (auto const& elem : aBuffer)
94 aStringBuffer.append( static_cast< sal_Unicode >(elem) );
95 return aStringBuffer.makeStringAndClear();
98 OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed )
100 return bCompressed ?
101 // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
102 readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1 ) :
103 readUnicodeArray( nChars );
106 void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm )
108 sal_Int64 nBytes = SAL_MAX_INT64;
109 sal_Int32 nBufferSize = INPUTSTREAM_BUFFERSIZE;
110 StreamDataSequence aBuffer( nBufferSize );
111 while( nBytes > 0 )
113 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
114 sal_Int32 nBytesRead = readData( aBuffer, nReadSize );
115 rOutStrm.writeData( aBuffer );
116 if( nReadSize == nBytesRead )
117 nBytes -= nReadSize;
118 else
119 nBytes = 0;
123 BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
124 BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
125 BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
126 maBuffer( INPUTSTREAM_BUFFERSIZE ),
127 mxInStrm( rxInStrm ),
128 mbAutoClose( bAutoClose && rxInStrm.is() )
130 mbEof = !mxInStrm.is();
133 BinaryXInputStream::~BinaryXInputStream()
135 close();
138 void BinaryXInputStream::close()
140 OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
141 if( mxInStrm.is() ) try
143 mxInStrm->closeInput();
145 catch( Exception& )
147 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
149 mxInStrm.clear();
150 mbAutoClose = false;
151 BinaryXSeekableStream::close();
154 sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
156 sal_Int32 nRet = 0;
157 if( !mbEof && (nBytes > 0) ) try
159 nRet = mxInStrm->readBytes( orData, nBytes );
160 mbEof = nRet != nBytes;
162 catch( Exception& )
164 mbEof = true;
166 return nRet;
169 sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
171 sal_Int32 nRet = 0;
172 if( !mbEof && (nBytes > 0) )
174 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
175 sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem );
176 while( !mbEof && (nBytes > 0) )
178 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
179 sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
180 if( nBytesRead > 0 )
181 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
182 opnMem += nBytesRead;
183 nBytes -= nBytesRead;
184 nRet += nBytesRead;
187 return nRet;
190 void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
192 if( !mbEof ) try
194 mxInStrm->skipBytes( nBytes );
196 catch( Exception& )
198 mbEof = true;
202 SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
203 BinaryStreamBase( true ),
204 SequenceSeekableStream( rData )
208 sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
210 sal_Int32 nReadBytes = 0;
211 if( !mbEof )
213 nReadBytes = getMaxBytes( nBytes );
214 orData.realloc( nReadBytes );
215 if( nReadBytes > 0 )
216 memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
217 mnPos += nReadBytes;
218 mbEof = nReadBytes < nBytes;
220 return nReadBytes;
223 sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
225 sal_Int32 nReadBytes = 0;
226 if( !mbEof )
228 nReadBytes = getMaxBytes( nBytes );
229 if( nReadBytes > 0 )
230 memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
231 mnPos += nReadBytes;
232 mbEof = nReadBytes < nBytes;
234 return nReadBytes;
237 void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
239 if( !mbEof )
241 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
242 mnPos += nSkipBytes;
243 mbEof = nSkipBytes < nBytes;
247 RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
248 BinaryStreamBase( rInStrm.isSeekable() ),
249 mpInStrm( &rInStrm ),
250 mnStartPos( rInStrm.tell() ),
251 mnRelPos( 0 )
253 sal_Int64 nRemaining = rInStrm.getRemaining();
254 mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
255 mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
258 sal_Int64 RelativeInputStream::size() const
260 return mpInStrm ? mnSize : -1;
263 sal_Int64 RelativeInputStream::tell() const
265 return mpInStrm ? mnRelPos : -1;
268 void RelativeInputStream::seek( sal_Int64 nPos )
270 if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
272 mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
273 mpInStrm->seek( mnStartPos + mnRelPos );
274 mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
278 void RelativeInputStream::close()
280 mpInStrm = nullptr;
281 mbEof = true;
284 sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
286 sal_Int32 nReadBytes = 0;
287 if( !mbEof )
289 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
290 nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
291 mnRelPos += nReadBytes;
292 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
294 return nReadBytes;
297 sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
299 sal_Int32 nReadBytes = 0;
300 if( !mbEof )
302 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
303 nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
304 mnRelPos += nReadBytes;
305 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
307 return nReadBytes;
310 void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
312 if( !mbEof )
314 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
315 mpInStrm->skip( nSkipBytes, nAtomSize );
316 mnRelPos += nSkipBytes;
317 mbEof = nSkipBytes < nBytes;
321 } // namespace oox
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */