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 <osl/diagnose.h>
29 #include "oox/helper/binaryoutputstream.hxx"
33 using namespace ::com::sun::star::io
;
34 using namespace ::com::sun::star::uno
;
38 const sal_Int32 INPUTSTREAM_BUFFERSIZE
= 0x8000;
42 OUString
BinaryInputStream::readNulUnicodeArray()
44 OUStringBuffer aBuffer
;
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
)
59 ::std::vector
< sal_uInt8
> aBuffer
;
60 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
64 aBuffer
.resize( static_cast< size_t >( nCharsRead
) );
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
)
81 ::std::vector
< sal_uInt16
> aBuffer
;
82 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
86 aBuffer
.resize( static_cast< size_t >( nCharsRead
) );
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
)
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
)
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
);
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
)
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()
140 void BinaryXInputStream::close()
142 OSL_ENSURE( !mbAutoClose
|| mxInStrm
.is(), "BinaryXInputStream::close - invalid call" );
143 if( mxInStrm
.is() ) try
145 mxInStrm
->closeInput();
149 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
153 BinaryXSeekableStream::close();
156 sal_Int32
BinaryXInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
159 if( !mbEof
&& (nBytes
> 0) ) try
161 nRet
= mxInStrm
->readBytes( orData
, nBytes
);
162 mbEof
= nRet
!= nBytes
;
171 sal_Int32
BinaryXInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
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
);
183 memcpy( opnMem
, maBuffer
.getConstArray(), static_cast< size_t >( nBytesRead
) );
184 opnMem
+= nBytesRead
;
185 nBytes
-= nBytesRead
;
192 void BinaryXInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
196 mxInStrm
->skipBytes( nBytes
);
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;
215 nReadBytes
= getMaxBytes( nBytes
);
216 orData
.realloc( nReadBytes
);
218 memcpy( orData
.getArray(), mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
220 mbEof
= nReadBytes
< nBytes
;
225 sal_Int32
SequenceInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
227 sal_Int32 nReadBytes
= 0;
230 nReadBytes
= getMaxBytes( nBytes
);
232 memcpy( opMem
, mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
234 mbEof
= nReadBytes
< nBytes
;
239 void SequenceInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
243 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
245 mbEof
= nSkipBytes
< nBytes
;
249 RelativeInputStream::RelativeInputStream( BinaryInputStream
& rInStrm
, sal_Int64 nSize
) :
250 BinaryStreamBase( rInStrm
.isSeekable() ),
251 mpInStrm( &rInStrm
),
252 mnStartPos( rInStrm
.tell() ),
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()
286 sal_Int32
RelativeInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
)
288 sal_Int32 nReadBytes
= 0;
291 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
292 nReadBytes
= mpInStrm
->readData( orData
, nMaxBytes
, nAtomSize
);
293 mnRelPos
+= nReadBytes
;
294 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
299 sal_Int32
RelativeInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
301 sal_Int32 nReadBytes
= 0;
304 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
305 nReadBytes
= mpInStrm
->readMemory( opMem
, nMaxBytes
, nAtomSize
);
306 mnRelPos
+= nReadBytes
;
307 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
312 void RelativeInputStream::skip( sal_Int32 nBytes
, size_t nAtomSize
)
316 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
317 mpInStrm
->skip( nSkipBytes
, nAtomSize
);
318 mnRelPos
+= nSkipBytes
;
319 mbEof
= nSkipBytes
< nBytes
;
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */