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 #ifndef INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
21 #define INCLUDED_OOX_HELPER_BINARYINPUTSTREAM_HXX
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <oox/helper/binarystreambase.hxx>
27 namespace com
{ namespace sun
{ namespace star
{
28 namespace io
{ class XInputStream
; }
33 class BinaryOutputStream
;
37 /** Interface for binary input stream classes.
39 The binary data in the stream is assumed to be in little-endian format.
41 class OOX_DLLPUBLIC BinaryInputStream
: public virtual BinaryStreamBase
44 /** Derived classes implement reading nBytes bytes to the passed sequence.
45 The sequence will be reallocated internally.
48 The size of the elements in the memory block, if available. Derived
49 classes may be interested in this information.
52 Number of bytes really read.
54 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
56 /** Derived classes implement reading nBytes bytes to the (preallocated!)
60 The size of the elements in the memory block, if available. Derived
61 classes may be interested in this information.
64 Number of bytes really read.
66 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
68 /** Derived classes implement seeking the stream forward by the passed
69 number of bytes. This should work for non-seekable streams too.
72 The size of the elements in the memory block, if available. Derived
73 classes may be interested in this information.
75 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
77 /** Reads a value from the stream and converts it to platform byte order.
78 All data types supported by the ByteOrderConverter class can be used.
80 template< typename Type
>
81 SAL_WARN_UNUSED_RESULT
84 SAL_WARN_UNUSED_RESULT
85 sal_Int8
readInt8() { return readValue
<sal_Int8
>(); }
86 SAL_WARN_UNUSED_RESULT
87 sal_uInt8
readuInt8() { return readValue
<sal_uInt8
>(); }
88 SAL_WARN_UNUSED_RESULT
89 sal_Int16
readInt16() { return readValue
<sal_Int16
>(); }
90 SAL_WARN_UNUSED_RESULT
91 sal_uInt16
readuInt16() { return readValue
<sal_uInt16
>(); }
92 SAL_WARN_UNUSED_RESULT
93 sal_Int32
readInt32() { return readValue
<sal_Int32
>(); }
94 SAL_WARN_UNUSED_RESULT
95 sal_uInt32
readuInt32() { return readValue
<sal_uInt32
>(); }
96 SAL_WARN_UNUSED_RESULT
97 sal_Int64
readInt64() { return readValue
<sal_Int64
>(); }
98 SAL_WARN_UNUSED_RESULT
99 sal_uInt64
readuInt64() { return readValue
<sal_uInt64
>(); }
100 SAL_WARN_UNUSED_RESULT
101 float readFloat() { return readValue
<float>(); }
102 SAL_WARN_UNUSED_RESULT
103 double readDouble() { return readValue
<double>(); }
104 SAL_WARN_UNUSED_RESULT
105 unsigned char readuChar() { return readValue
<unsigned char>(); }
107 /** Reads a (preallocated!) C array of values from the stream.
109 Converts all values in the array to platform byte order. All data types
110 supported by the ByteOrderConverter class can be used.
113 Number of array elements to read (NOT byte count).
116 Number of array elements really read (NOT byte count).
118 template< typename Type
>
119 sal_Int32
readArray( Type
* opnArray
, sal_Int32 nElemCount
);
121 /** Reads a sequence of values from the stream.
123 The sequence will be reallocated internally. Converts all values in the
124 array to platform byte order. All data types supported by the
125 ByteOrderConverter class can be used.
128 Number of elements to put into the sequence (NOT byte count).
131 Number of sequence elements really read (NOT byte count).
133 template< typename Type
>
134 sal_Int32
readArray( ::com::sun::star::uno::Sequence
< Type
>& orSequence
, sal_Int32 nElemCount
);
136 /** Reads a vector of values from the stream.
138 The vector will be resized internally. Converts all values in the
139 vector to platform byte order. All data types supported by the
140 ByteOrderConverter class can be used.
143 Number of elements to put into the vector (NOT byte count).
146 Number of vector elements really read (NOT byte count).
148 template< typename Type
>
149 sal_Int32
readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
);
151 /** Skips an array of values of a certain type in the stream.
153 All data types supported by the ByteOrderConverter class can be used.
156 Number of array elements to skip (NOT byte count).
158 template< typename Type
>
159 void skipArray( sal_Int32 nElemCount
);
161 /** Reads a NUL-terminated Unicode character array and returns the string.
163 OUString
readNulUnicodeArray();
165 /** Reads a byte character array and returns the string.
168 Number of characters (bytes) to read from the stream.
170 @param bAllowNulChars
171 True = NUL characters are inserted into the imported string.
172 False = NUL characters are replaced by question marks (default).
174 OString
readCharArray( sal_Int32 nChars
, bool bAllowNulChars
= false );
176 /** Reads a byte character array and returns a Unicode string.
179 Number of characters (bytes) to read from the stream.
182 The text encoding used to create the Unicode string.
184 @param bAllowNulChars
185 True = NUL characters are inserted into the imported string.
186 False = NUL characters are replaced by question marks (default).
188 OUString
readCharArrayUC( sal_Int32 nChars
, rtl_TextEncoding eTextEnc
, bool bAllowNulChars
= false );
190 /** Reads a Unicode character array and returns the string.
193 Number of 16-bit characters to read from the stream.
195 @param bAllowNulChars
196 True = NUL characters are inserted into the imported string.
197 False = NUL characters are replaced by question marks (default).
199 OUString
readUnicodeArray( sal_Int32 nChars
, bool bAllowNulChars
= false );
201 /** Reads a Unicode character array (may be compressed) and returns the
205 Number of 8-bit or 16-bit characters to read from the stream.
208 True = Character array is compressed (stored as 8-bit characters).
209 False = Character array is not compressed (stored as 16-bit characters).
211 @param bAllowNulChars
212 True = NUL characters are inserted into the imported string.
213 False = NUL characters are replaced by question marks (default).
215 OUString
readCompressedUnicodeArray( sal_Int32 nChars
, bool bCompressed
, bool bAllowNulChars
= false );
217 /** Copies nBytes bytes from the current position to the passed output stream.
219 void copyToStream( BinaryOutputStream
& rOutStrm
, sal_Int64 nBytes
= SAL_MAX_INT64
, sal_Int32 nAtomSize
= 1 );
222 /** This dummy default c'tor will never call the c'tor of the virtual base
223 class BinaryStreamBase as this class cannot be instantiated directly. */
224 BinaryInputStream() : BinaryStreamBase( false ) {}
227 typedef std::shared_ptr
< BinaryInputStream
> BinaryInputStreamRef
;
231 template< typename Type
>
232 Type
BinaryInputStream::readValue()
235 readMemory( &ornValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
236 ByteOrderConverter::convertLittleEndian( ornValue
);
240 template< typename Type
>
241 sal_Int32
BinaryInputStream::readArray( Type
* opnArray
, sal_Int32 nElemCount
)
246 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
247 nRet
= readMemory( opnArray
, nReadSize
, sizeof( Type
) ) / sizeof( Type
);
248 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nRet
) );
253 template< typename Type
>
254 sal_Int32
BinaryInputStream::readArray( ::com::sun::star::uno::Sequence
< Type
>& orSequence
, sal_Int32 nElemCount
)
256 orSequence
.reallocate( nElemCount
);
257 return orSequence
.hasElements() ? readArray( orSequence
.getArray(), nElemCount
) : 0;
260 template< typename Type
>
261 sal_Int32
BinaryInputStream::readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
)
263 orVector
.resize( static_cast< size_t >( nElemCount
) );
264 return orVector
.empty() ? 0 : readArray( &orVector
.front(), nElemCount
);
267 template< typename Type
>
268 void BinaryInputStream::skipArray( sal_Int32 nElemCount
)
270 sal_Int32 nSkipSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
271 skip( nSkipSize
, sizeof( Type
) );
276 /** Wraps a UNO input stream and provides convenient access functions.
278 The binary data in the stream is assumed to be in little-endian format.
280 class OOX_DLLPUBLIC BinaryXInputStream
: public BinaryXSeekableStream
, public BinaryInputStream
283 /** Constructs the wrapper object for the passed input stream.
286 The com.sun.star.io.XInputStream interface of the UNO input stream
290 True = automatically close the wrapped input stream on destruction
291 of this wrapper or when close() is called.
293 explicit BinaryXInputStream(
294 const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& rxInStrm
,
297 virtual ~BinaryXInputStream();
299 /** Closes the input stream. Does also close the wrapped UNO input stream
300 if bAutoClose has been set to true in the constructor. */
301 virtual void close() SAL_OVERRIDE
;
303 /** Reads nBytes bytes to the passed sequence.
304 @return Number of bytes really read. */
305 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
307 /** Reads nBytes bytes to the (existing) buffer opMem.
308 @return Number of bytes really read. */
309 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
311 /** Seeks the stream forward by the passed number of bytes. This works for
312 non-seekable streams too. */
313 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
316 StreamDataSequence maBuffer
; ///< Data buffer used in readMemory() function.
317 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>
318 mxInStrm
; ///< Reference to the input stream.
319 bool mbAutoClose
; ///< True = automatically close stream on destruction.
324 /** Wraps a StreamDataSequence and provides convenient access functions.
326 The binary data in the stream is assumed to be in little-endian format.
328 class OOX_DLLPUBLIC SequenceInputStream
: public SequenceSeekableStream
, public BinaryInputStream
331 /** Constructs the wrapper object for the passed data sequence.
334 The passed data sequence MUST live at least as long as this stream
335 wrapper. The data sequence MUST NOT be changed from outside as long
336 as this stream wrapper is used to read from it.
338 explicit SequenceInputStream( const StreamDataSequence
& rData
);
340 /** Reads nBytes bytes to the passed sequence.
341 @return Number of bytes really read. */
342 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
344 /** Reads nBytes bytes to the (existing) buffer opMem.
345 @return Number of bytes really read. */
346 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
348 /** Seeks the stream forward by the passed number of bytes. This works for
349 non-seekable streams too. */
350 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
353 /** Returns the number of bytes available in the sequence for the passed byte count. */
354 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
355 { return getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, mpData
->getLength() - mnPos
); }
360 /** Wraps a BinaryInputStream and provides access to a specific part of the
363 Provides access to the stream data block starting at the current position
364 of the stream, and with a specific length. If the wrapped stream is
365 seekable, this wrapper will treat the position of the wrapped stream at
366 construction time as position "0" (therefore the class name).
368 The passed input stream MUST live at least as long as this stream wrapper.
369 The stream MUST NOT be changed from outside as long as this stream wrapper
370 is used to read from it.
372 class RelativeInputStream
: public BinaryInputStream
375 /** Constructs the wrapper object for the passed stream.
378 If specified, restricts the amount of data that can be read from
379 the passed input stream.
381 explicit RelativeInputStream(
382 BinaryInputStream
& rInStrm
,
383 sal_Int64 nSize
= SAL_MAX_INT64
);
385 /** Returns the size of the data block in the wrapped stream offered by
387 virtual sal_Int64
size() const SAL_OVERRIDE
;
389 /** Returns the current relative stream position. */
390 virtual sal_Int64
tell() const SAL_OVERRIDE
;
392 /** Seeks the stream to the passed relative position, if the wrapped stream
394 virtual void seek( sal_Int64 nPos
) SAL_OVERRIDE
;
396 /** Closes the input stream but not the wrapped stream. */
397 virtual void close() SAL_OVERRIDE
;
399 /** Reads nBytes bytes to the passed sequence. Does not read out of the
400 data block whose size has been specified on construction.
401 @return Number of bytes really read. */
402 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
404 /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
405 the data block whose size has been specified on construction.
406 @return Number of bytes really read. */
407 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
409 /** Seeks the stream forward by the passed number of bytes. This works for
410 non-seekable streams too. Does not seek out of the data block. */
411 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) SAL_OVERRIDE
;
414 /** Returns the number of bytes available in the sequence for the passed byte count. */
415 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
416 { return getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, mnSize
- mnRelPos
); }
419 BinaryInputStream
* mpInStrm
;
420 sal_Int64 mnStartPos
;
431 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */