1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <rtl/strbuf.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include "oox/helper/binaryoutputstream.hxx"
32 // ============================================================================
34 using namespace ::com::sun::star::io
;
35 using namespace ::com::sun::star::uno
;
39 const sal_Int32 INPUTSTREAM_BUFFERSIZE
= 0x8000;
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
)
58 ::std::vector
< sal_uInt8
> aBuffer
;
59 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
63 aBuffer
.resize( static_cast< size_t >( nCharsRead
) );
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
)
80 ::std::vector
< sal_uInt16
> aBuffer
;
81 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
85 aBuffer
.resize( static_cast< size_t >( nCharsRead
) );
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
)
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
)
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
);
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
)
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()
141 void BinaryXInputStream::close()
143 OSL_ENSURE( !mbAutoClose
|| mxInStrm
.is(), "BinaryXInputStream::close - invalid call" );
144 if( mxInStrm
.is() ) try
146 mxInStrm
->closeInput();
150 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
154 BinaryXSeekableStream::close();
157 sal_Int32
BinaryXInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
160 if( !mbEof
&& (nBytes
> 0) ) try
162 nRet
= mxInStrm
->readBytes( orData
, nBytes
);
163 mbEof
= nRet
!= nBytes
;
172 sal_Int32
BinaryXInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
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
);
184 memcpy( opnMem
, maBuffer
.getConstArray(), static_cast< size_t >( nBytesRead
) );
185 opnMem
+= nBytesRead
;
186 nBytes
-= nBytesRead
;
193 void BinaryXInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
197 mxInStrm
->skipBytes( nBytes
);
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;
218 nReadBytes
= getMaxBytes( nBytes
);
219 orData
.realloc( nReadBytes
);
221 memcpy( orData
.getArray(), mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
223 mbEof
= nReadBytes
< nBytes
;
228 sal_Int32
SequenceInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
230 sal_Int32 nReadBytes
= 0;
233 nReadBytes
= getMaxBytes( nBytes
);
235 memcpy( opMem
, mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
237 mbEof
= nReadBytes
< nBytes
;
242 void SequenceInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
246 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
248 mbEof
= nSkipBytes
< nBytes
;
252 // ============================================================================
254 RelativeInputStream::RelativeInputStream( BinaryInputStream
& rInStrm
, sal_Int64 nSize
) :
255 BinaryStreamBase( rInStrm
.isSeekable() ),
256 mpInStrm( &rInStrm
),
257 mnStartPos( rInStrm
.tell() ),
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()
291 sal_Int32
RelativeInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
)
293 sal_Int32 nReadBytes
= 0;
296 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
297 nReadBytes
= mpInStrm
->readData( orData
, nMaxBytes
, nAtomSize
);
298 mnRelPos
+= nReadBytes
;
299 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
304 sal_Int32
RelativeInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
306 sal_Int32 nReadBytes
= 0;
309 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
310 nReadBytes
= mpInStrm
->readMemory( opMem
, nMaxBytes
, nAtomSize
);
311 mnRelPos
+= nReadBytes
;
312 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
317 void RelativeInputStream::skip( sal_Int32 nBytes
, size_t nAtomSize
)
321 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
322 mpInStrm
->skip( nSkipBytes
, nAtomSize
);
323 mnRelPos
+= nSkipBytes
;
324 mbEof
= nSkipBytes
< nBytes
;
328 // ============================================================================
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */