GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / oox / source / helper / binaryinputstream.cxx
blob1bef500463c63a2ff8610b56362e778a55b9214e
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 "oox/helper/binaryoutputstream.hxx"
30 namespace oox {
32 // ============================================================================
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 // ============================================================================
45 OUString BinaryInputStream::readNulUnicodeArray()
47 OUStringBuffer aBuffer;
48 for( sal_uInt16 nChar = readuInt16(); !mbEof && (nChar > 0); readValue( nChar ) )
49 aBuffer.append( static_cast< sal_Unicode >( nChar ) );
50 return aBuffer.makeStringAndClear();
53 OString BinaryInputStream::readCharArray( sal_Int32 nChars, bool bAllowNulChars )
55 if( nChars <= 0 )
56 return OString();
58 ::std::vector< sal_uInt8 > aBuffer;
59 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
60 if( nCharsRead <= 0 )
61 return OString();
63 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
64 if( !bAllowNulChars )
65 ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
67 return OString( reinterpret_cast< sal_Char* >( &aBuffer.front() ), nCharsRead );
70 OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
72 return OStringToOUString( readCharArray( nChars, bAllowNulChars ), eTextEnc );
75 OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars, bool bAllowNulChars )
77 if( nChars <= 0 )
78 return OUString();
80 ::std::vector< sal_uInt16 > aBuffer;
81 sal_Int32 nCharsRead = readArray( aBuffer, nChars );
82 if( nCharsRead <= 0 )
83 return OUString();
85 aBuffer.resize( static_cast< size_t >( nCharsRead ) );
86 if( !bAllowNulChars )
87 ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
89 OUStringBuffer aStringBuffer;
90 aStringBuffer.ensureCapacity( nCharsRead );
91 for( ::std::vector< sal_uInt16 >::iterator aIt = aBuffer.begin(), aEnd = aBuffer.end(); aIt != aEnd; ++aIt )
92 aStringBuffer.append( static_cast< sal_Unicode >( *aIt ) );
93 return aStringBuffer.makeStringAndClear();
96 OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed, bool bAllowNulChars )
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 readUnicodeArray( nChars, bAllowNulChars );
104 void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm, sal_Int64 nBytes, sal_Int32 nAtomSize )
106 if( nBytes > 0 )
108 // make buffer size a multiple of the passed atom size
109 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, (INPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
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, nAtomSize );
115 rOutStrm.writeData( aBuffer );
116 if( nReadSize == nBytesRead )
117 nBytes -= nReadSize;
118 else
119 nBytes = 0;
124 // ============================================================================
126 BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
127 BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
128 BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
129 maBuffer( INPUTSTREAM_BUFFERSIZE ),
130 mxInStrm( rxInStrm ),
131 mbAutoClose( bAutoClose && rxInStrm.is() )
133 mbEof = !mxInStrm.is();
136 BinaryXInputStream::~BinaryXInputStream()
138 close();
141 void BinaryXInputStream::close()
143 OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
144 if( mxInStrm.is() ) try
146 mxInStrm->closeInput();
148 catch( Exception& )
150 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
152 mxInStrm.clear();
153 mbAutoClose = false;
154 BinaryXSeekableStream::close();
157 sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
159 sal_Int32 nRet = 0;
160 if( !mbEof && (nBytes > 0) ) try
162 nRet = mxInStrm->readBytes( orData, nBytes );
163 mbEof = nRet != nBytes;
165 catch( Exception& )
167 mbEof = true;
169 return nRet;
172 sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
174 sal_Int32 nRet = 0;
175 if( !mbEof && (nBytes > 0) )
177 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
178 sal_uInt8* opnMem = reinterpret_cast< sal_uInt8* >( opMem );
179 while( !mbEof && (nBytes > 0) )
181 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
182 sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
183 if( nBytesRead > 0 )
184 memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
185 opnMem += nBytesRead;
186 nBytes -= nBytesRead;
187 nRet += nBytesRead;
190 return nRet;
193 void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
195 if( !mbEof ) try
197 mxInStrm->skipBytes( nBytes );
199 catch( Exception& )
201 mbEof = true;
205 // ============================================================================
207 SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
208 BinaryStreamBase( true ),
209 SequenceSeekableStream( rData )
213 sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
215 sal_Int32 nReadBytes = 0;
216 if( !mbEof )
218 nReadBytes = getMaxBytes( nBytes );
219 orData.realloc( nReadBytes );
220 if( nReadBytes > 0 )
221 memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
222 mnPos += nReadBytes;
223 mbEof = nReadBytes < nBytes;
225 return nReadBytes;
228 sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
230 sal_Int32 nReadBytes = 0;
231 if( !mbEof )
233 nReadBytes = getMaxBytes( nBytes );
234 if( nReadBytes > 0 )
235 memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
236 mnPos += nReadBytes;
237 mbEof = nReadBytes < nBytes;
239 return nReadBytes;
242 void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
244 if( !mbEof )
246 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
247 mnPos += nSkipBytes;
248 mbEof = nSkipBytes < nBytes;
252 // ============================================================================
254 RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
255 BinaryStreamBase( rInStrm.isSeekable() ),
256 mpInStrm( &rInStrm ),
257 mnStartPos( rInStrm.tell() ),
258 mnRelPos( 0 )
260 sal_Int64 nRemaining = rInStrm.getRemaining();
261 mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
262 mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
265 sal_Int64 RelativeInputStream::size() const
267 return mpInStrm ? mnSize : -1;
270 sal_Int64 RelativeInputStream::tell() const
272 return mpInStrm ? mnRelPos : -1;
275 void RelativeInputStream::seek( sal_Int64 nPos )
277 if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
279 mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
280 mpInStrm->seek( mnStartPos + mnRelPos );
281 mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
285 void RelativeInputStream::close()
287 mpInStrm = 0;
288 mbEof = true;
291 sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
293 sal_Int32 nReadBytes = 0;
294 if( !mbEof )
296 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
297 nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
298 mnRelPos += nReadBytes;
299 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
301 return nReadBytes;
304 sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
306 sal_Int32 nReadBytes = 0;
307 if( !mbEof )
309 sal_Int32 nMaxBytes = getMaxBytes( nBytes );
310 nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
311 mnRelPos += nReadBytes;
312 mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
314 return nReadBytes;
317 void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
319 if( !mbEof )
321 sal_Int32 nSkipBytes = getMaxBytes( nBytes );
322 mpInStrm->skip( nSkipBytes, nAtomSize );
323 mnRelPos += nSkipBytes;
324 mbEof = nSkipBytes < nBytes;
328 // ============================================================================
330 } // namespace oox
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */