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>
27 #include <rtl/strbuf.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <osl/diagnose.h>
30 #include "oox/helper/binaryoutputstream.hxx"
34 using namespace ::com::sun::star::io
;
35 using namespace ::com::sun::star::uno
;
39 const sal_Int32 INPUTSTREAM_BUFFERSIZE
= 0x8000;
43 OUString
BinaryInputStream::readNulUnicodeArray()
45 OUStringBuffer aBuffer
;
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
)
60 ::std::vector
< sal_uInt8
> aBuffer
;
61 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
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
, bool bAllowNulChars
)
82 ::std::vector
< sal_uInt16
> aBuffer
;
83 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
87 aBuffer
.resize( static_cast< size_t >( nCharsRead
) );
89 ::std::replace( aBuffer
.begin(), aBuffer
.begin() + nCharsRead
, '\0', '?' );
91 OUStringBuffer aStringBuffer
;
92 aStringBuffer
.ensureCapacity( nCharsRead
);
93 for( ::std::vector
< sal_uInt16
>::iterator aIt
= aBuffer
.begin(), aEnd
= aBuffer
.end(); aIt
!= aEnd
; ++aIt
)
94 aStringBuffer
.append( static_cast< sal_Unicode
>( *aIt
) );
95 return aStringBuffer
.makeStringAndClear();
98 OUString
BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars
, bool 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
);
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
)
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()
138 void BinaryXInputStream::close()
140 OSL_ENSURE( !mbAutoClose
|| mxInStrm
.is(), "BinaryXInputStream::close - invalid call" );
141 if( mxInStrm
.is() ) try
143 mxInStrm
->closeInput();
147 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
151 BinaryXSeekableStream::close();
154 sal_Int32
BinaryXInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
157 if( !mbEof
&& (nBytes
> 0) ) try
159 nRet
= mxInStrm
->readBytes( orData
, nBytes
);
160 mbEof
= nRet
!= nBytes
;
169 sal_Int32
BinaryXInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
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
);
181 memcpy( opnMem
, maBuffer
.getConstArray(), static_cast< size_t >( nBytesRead
) );
182 opnMem
+= nBytesRead
;
183 nBytes
-= nBytesRead
;
190 void BinaryXInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
194 mxInStrm
->skipBytes( nBytes
);
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;
213 nReadBytes
= getMaxBytes( nBytes
);
214 orData
.realloc( nReadBytes
);
216 memcpy( orData
.getArray(), mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
218 mbEof
= nReadBytes
< nBytes
;
223 sal_Int32
SequenceInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
225 sal_Int32 nReadBytes
= 0;
228 nReadBytes
= getMaxBytes( nBytes
);
230 memcpy( opMem
, mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
232 mbEof
= nReadBytes
< nBytes
;
237 void SequenceInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
241 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
243 mbEof
= nSkipBytes
< nBytes
;
247 RelativeInputStream::RelativeInputStream( BinaryInputStream
& rInStrm
, sal_Int64 nSize
) :
248 BinaryStreamBase( rInStrm
.isSeekable() ),
249 mpInStrm( &rInStrm
),
250 mnStartPos( rInStrm
.tell() ),
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()
284 sal_Int32
RelativeInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
)
286 sal_Int32 nReadBytes
= 0;
289 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
290 nReadBytes
= mpInStrm
->readData( orData
, nMaxBytes
, nAtomSize
);
291 mnRelPos
+= nReadBytes
;
292 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
297 sal_Int32
RelativeInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
299 sal_Int32 nReadBytes
= 0;
302 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
303 nReadBytes
= mpInStrm
->readMemory( opMem
, nMaxBytes
, nAtomSize
);
304 mnRelPos
+= nReadBytes
;
305 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
310 void RelativeInputStream::skip( sal_Int32 nBytes
, size_t nAtomSize
)
314 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
315 mpInStrm
->skip( nSkipBytes
, nAtomSize
);
316 mnRelPos
+= nSkipBytes
;
317 mbEof
= nSkipBytes
< nBytes
;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */