tdf#131098 docx export: write fill property of graphic
[LibreOffice.git] / oox / source / helper / binaryinputstream.cxx
blob27cbc6ad6e1f3a14f40a792379e3bcea7c3cb849
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/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 )
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 // NUL characters are replaced by question marks.
66 ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
68 return OString(reinterpret_cast<char*>(aBuffer.data()), nCharsRead);
71 OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc )
73 return OStringToOUString( readCharArray( nChars ), eTextEnc );
76 OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars )
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 // don't allow nul chars
88 ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
90 OUStringBuffer aStringBuffer;
91 aStringBuffer.ensureCapacity( nCharsRead );
92 for (auto const& elem : aBuffer)
93 aStringBuffer.append( static_cast< sal_Unicode >(elem) );
94 return aStringBuffer.makeStringAndClear();
97 OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed )
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 ) :
102 readUnicodeArray( nChars );
105 void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm )
107 sal_Int64 nBytes = SAL_MAX_INT64;
108 sal_Int32 nBufferSize = INPUTSTREAM_BUFFERSIZE;
109 StreamDataSequence aBuffer( nBufferSize );
110 while( nBytes > 0 )
112 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
113 sal_Int32 nBytesRead = readData( aBuffer, nReadSize );
114 rOutStrm.writeData( aBuffer );
115 if( nReadSize == nBytesRead )
116 nBytes -= nReadSize;
117 else
118 nBytes = 0;
122 BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
123 BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
124 BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
125 maBuffer( INPUTSTREAM_BUFFERSIZE ),
126 mxInStrm( rxInStrm ),
127 mbAutoClose( bAutoClose && rxInStrm.is() )
129 mbEof = !mxInStrm.is();
132 BinaryXInputStream::~BinaryXInputStream()
134 close();
137 void BinaryXInputStream::close()
139 OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
140 if( mxInStrm.is() ) try
142 mxInStrm->closeInput();
144 catch( Exception& )
146 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
148 mxInStrm.clear();
149 mbAutoClose = false;
150 BinaryXSeekableStream::close();
153 sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
155 sal_Int32 nRet = 0;
156 if( !mbEof && (nBytes > 0) ) try
158 nRet = mxInStrm->readBytes( orData, nBytes );
159 mbEof = nRet != nBytes;
161 catch( Exception& )
163 mbEof = true;
165 return nRet;
168 sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
170 sal_Int32 nRet = 0;
171 if( !mbEof && (nBytes > 0) )
173 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
174 sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem );
175 while( !mbEof && (nBytes > 0) )
177 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
178 sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
179 if( nBytesRead > 0 )
180 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
181 opnMem += nBytesRead;
182 nBytes -= nBytesRead;
183 nRet += nBytesRead;
186 return nRet;
189 void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
191 if( !mbEof ) try
193 mxInStrm->skipBytes( nBytes );
195 catch( Exception& )
197 mbEof = true;
201 SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
202 BinaryStreamBase( true ),
203 SequenceSeekableStream( rData )
207 sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
209 sal_Int32 nReadBytes = 0;
210 if( !mbEof )
212 nReadBytes = getMaxBytes( nBytes );
213 orData.realloc( nReadBytes );
214 if( nReadBytes > 0 )
215 memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
216 mnPos += nReadBytes;
217 mbEof = nReadBytes < nBytes;
219 return nReadBytes;
222 sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
224 sal_Int32 nReadBytes = 0;
225 if( !mbEof )
227 nReadBytes = getMaxBytes( nBytes );
228 if( nReadBytes > 0 )
229 memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
230 mnPos += nReadBytes;
231 mbEof = nReadBytes < nBytes;
233 return nReadBytes;
236 void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
238 if( !mbEof )
240 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
241 mnPos += nSkipBytes;
242 mbEof = nSkipBytes < nBytes;
246 RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
247 BinaryStreamBase( rInStrm.isSeekable() ),
248 mpInStrm( &rInStrm ),
249 mnStartPos( rInStrm.tell() ),
250 mnRelPos( 0 )
252 sal_Int64 nRemaining = rInStrm.getRemaining();
253 mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
254 mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
257 sal_Int64 RelativeInputStream::size() const
259 return mpInStrm ? mnSize : -1;
262 sal_Int64 RelativeInputStream::tell() const
264 return mpInStrm ? mnRelPos : -1;
267 void RelativeInputStream::seek( sal_Int64 nPos )
269 if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
271 mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
272 mpInStrm->seek( mnStartPos + mnRelPos );
273 mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
277 void RelativeInputStream::close()
279 mpInStrm = nullptr;
280 mbEof = true;
283 sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
285 sal_Int32 nReadBytes = 0;
286 if( !mbEof )
288 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
289 nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
290 mnRelPos += nReadBytes;
291 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
293 return nReadBytes;
296 sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
298 sal_Int32 nReadBytes = 0;
299 if( !mbEof )
301 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
302 nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
303 mnRelPos += nReadBytes;
304 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
306 return nReadBytes;
309 void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
311 if( !mbEof )
313 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
314 mpInStrm->skip( nSkipBytes, nAtomSize );
315 mnRelPos += nSkipBytes;
316 mbEof = nSkipBytes < nBytes;
320 } // namespace oox
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */