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/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
)
59 ::std::vector
< sal_uInt8
> aBuffer
;
60 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
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
)
81 ::std::vector
< sal_uInt16
> aBuffer
;
82 sal_Int32 nCharsRead
= readArray( aBuffer
, nChars
);
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
)
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
);
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
)
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()
137 void BinaryXInputStream::close()
139 OSL_ENSURE( !mbAutoClose
|| mxInStrm
.is(), "BinaryXInputStream::close - invalid call" );
140 if( mxInStrm
.is() ) try
142 mxInStrm
->closeInput();
146 OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
150 BinaryXSeekableStream::close();
153 sal_Int32
BinaryXInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
156 if( !mbEof
&& (nBytes
> 0) ) try
158 nRet
= mxInStrm
->readBytes( orData
, nBytes
);
159 mbEof
= nRet
!= nBytes
;
168 sal_Int32
BinaryXInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
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
);
180 memcpy( opnMem
, maBuffer
.getConstArray(), static_cast< size_t >( nBytesRead
) );
181 opnMem
+= nBytesRead
;
182 nBytes
-= nBytesRead
;
189 void BinaryXInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
193 mxInStrm
->skipBytes( nBytes
);
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;
212 nReadBytes
= getMaxBytes( nBytes
);
213 orData
.realloc( nReadBytes
);
215 memcpy( orData
.getArray(), mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
217 mbEof
= nReadBytes
< nBytes
;
222 sal_Int32
SequenceInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
224 sal_Int32 nReadBytes
= 0;
227 nReadBytes
= getMaxBytes( nBytes
);
229 memcpy( opMem
, mpData
->getConstArray() + mnPos
, static_cast< size_t >( nReadBytes
) );
231 mbEof
= nReadBytes
< nBytes
;
236 void SequenceInputStream::skip( sal_Int32 nBytes
, size_t /*nAtomSize*/ )
240 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
242 mbEof
= nSkipBytes
< nBytes
;
246 RelativeInputStream::RelativeInputStream( BinaryInputStream
& rInStrm
, sal_Int64 nSize
) :
247 BinaryStreamBase( rInStrm
.isSeekable() ),
248 mpInStrm( &rInStrm
),
249 mnStartPos( rInStrm
.tell() ),
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()
283 sal_Int32
RelativeInputStream::readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
)
285 sal_Int32 nReadBytes
= 0;
288 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
289 nReadBytes
= mpInStrm
->readData( orData
, nMaxBytes
, nAtomSize
);
290 mnRelPos
+= nReadBytes
;
291 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
296 sal_Int32
RelativeInputStream::readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
)
298 sal_Int32 nReadBytes
= 0;
301 sal_Int32 nMaxBytes
= getMaxBytes( nBytes
);
302 nReadBytes
= mpInStrm
->readMemory( opMem
, nMaxBytes
, nAtomSize
);
303 mnRelPos
+= nReadBytes
;
304 mbEof
= (nMaxBytes
< nBytes
) || mpInStrm
->isEof();
309 void RelativeInputStream::skip( sal_Int32 nBytes
, size_t nAtomSize
)
313 sal_Int32 nSkipBytes
= getMaxBytes( nBytes
);
314 mpInStrm
->skip( nSkipBytes
, nAtomSize
);
315 mnRelPos
+= nSkipBytes
;
316 mbEof
= nSkipBytes
< nBytes
;
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */