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 OOX_HELPER_BINARYINPUTSTREAM_HXX
21 #define 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
;
35 // ============================================================================
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 void readValue( Type
& ornValue
);
83 /** Reads a value from the stream and converts it to platform byte order.
84 All data types supported by the ByteOrderConverter class can be used.
86 template< typename Type
>
87 inline Type
readValue() { Type nValue
; readValue( nValue
); return nValue
; }
89 /** Stream operator for all data types supported by the readValue() function. */
90 template< typename Type
>
91 inline BinaryInputStream
& operator>>( Type
& ornValue
) { readValue( ornValue
); return *this; }
93 inline sal_Int8
readInt8() { return readValue
< sal_Int8
>(); }
94 inline sal_uInt8
readuInt8() { return readValue
< sal_uInt8
>(); }
95 inline sal_Int16
readInt16() { return readValue
< sal_Int16
>(); }
96 inline sal_uInt16
readuInt16() { return readValue
< sal_uInt16
>(); }
97 inline sal_Int32
readInt32() { return readValue
< sal_Int32
>(); }
98 inline sal_uInt32
readuInt32() { return readValue
< sal_uInt32
>(); }
99 inline sal_Int64
readInt64() { return readValue
< sal_Int64
>(); }
100 inline sal_uInt64
readuInt64() { return readValue
< sal_uInt64
>(); }
101 inline float readFloat() { return readValue
< float >(); }
102 inline double readDouble() { return readValue
< double >(); }
104 /** Reads a (preallocated!) C array of values from the stream.
106 Converts all values in the array to platform byte order. All data types
107 supported by the ByteOrderConverter class can be used.
110 Number of array elements to read (NOT byte count).
113 Number of array elements really read (NOT byte count).
115 template< typename Type
>
116 sal_Int32
readArray( Type
* opnArray
, sal_Int32 nElemCount
);
118 /** Reads a sequence of values from the stream.
120 The sequence will be reallocated internally. Converts all values in the
121 array to platform byte order. All data types supported by the
122 ByteOrderConverter class can be used.
125 Number of elements to put into the sequence (NOT byte count).
128 Number of sequence elements really read (NOT byte count).
130 template< typename Type
>
131 sal_Int32
readArray( ::com::sun::star::uno::Sequence
< Type
>& orSequence
, sal_Int32 nElemCount
);
133 /** Reads a vector of values from the stream.
135 The vector will be resized internally. Converts all values in the
136 vector to platform byte order. All data types supported by the
137 ByteOrderConverter class can be used.
140 Number of elements to put into the vector (NOT byte count).
143 Number of vector elements really read (NOT byte count).
145 template< typename Type
>
146 sal_Int32
readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
);
148 /** Skips an array of values of a certain type in the stream.
150 All data types supported by the ByteOrderConverter class can be used.
153 Number of array elements to skip (NOT byte count).
155 template< typename Type
>
156 void skipArray( sal_Int32 nElemCount
);
158 /** Reads a NUL-terminated Unicode character array and returns the string.
160 OUString
readNulUnicodeArray();
162 /** Reads a byte character array and returns the string.
165 Number of characters (bytes) to read from the stream.
167 @param bAllowNulChars
168 True = NUL characters are inserted into the imported string.
169 False = NUL characters are replaced by question marks (default).
171 OString
readCharArray( sal_Int32 nChars
, bool bAllowNulChars
= false );
173 /** Reads a byte character array and returns a Unicode string.
176 Number of characters (bytes) to read from the stream.
179 The text encoding used to create the Unicode string.
181 @param bAllowNulChars
182 True = NUL characters are inserted into the imported string.
183 False = NUL characters are replaced by question marks (default).
185 OUString
readCharArrayUC( sal_Int32 nChars
, rtl_TextEncoding eTextEnc
, bool bAllowNulChars
= false );
187 /** Reads a Unicode character array and returns the string.
190 Number of 16-bit characters to read from the stream.
192 @param bAllowNulChars
193 True = NUL characters are inserted into the imported string.
194 False = NUL characters are replaced by question marks (default).
196 OUString
readUnicodeArray( sal_Int32 nChars
, bool bAllowNulChars
= false );
198 /** Reads a Unicode character array (may be compressed) and returns the
202 Number of 8-bit or 16-bit characters to read from the stream.
205 True = Character array is compressed (stored as 8-bit characters).
206 False = Character array is not compressed (stored as 16-bit characters).
208 @param bAllowNulChars
209 True = NUL characters are inserted into the imported string.
210 False = NUL characters are replaced by question marks (default).
212 OUString
readCompressedUnicodeArray( sal_Int32 nChars
, bool bCompressed
, bool bAllowNulChars
= false );
214 /** Copies nBytes bytes from the current position to the passed output stream.
216 void copyToStream( BinaryOutputStream
& rOutStrm
, sal_Int64 nBytes
= SAL_MAX_INT64
, sal_Int32 nAtomSize
= 1 );
219 /** This dummy default c'tor will never call the c'tor of the virtual base
220 class BinaryStreamBase as this class cannot be instanciated directly. */
221 inline explicit BinaryInputStream() : BinaryStreamBase( false ) {}
224 typedef ::boost::shared_ptr
< BinaryInputStream
> BinaryInputStreamRef
;
226 // ----------------------------------------------------------------------------
228 template< typename Type
>
229 void BinaryInputStream::readValue( Type
& ornValue
)
231 readMemory( &ornValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
232 ByteOrderConverter::convertLittleEndian( ornValue
);
235 template< typename Type
>
236 sal_Int32
BinaryInputStream::readArray( Type
* opnArray
, sal_Int32 nElemCount
)
241 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
242 nRet
= readMemory( opnArray
, nReadSize
, sizeof( Type
) ) / sizeof( Type
);
243 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nRet
) );
248 template< typename Type
>
249 sal_Int32
BinaryInputStream::readArray( ::com::sun::star::uno::Sequence
< Type
>& orSequence
, sal_Int32 nElemCount
)
251 orSequence
.reallocate( nElemCount
);
252 return orSequence
.hasElements() ? readArray( orSequence
.getArray(), nElemCount
) : 0;
255 template< typename Type
>
256 sal_Int32
BinaryInputStream::readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
)
258 orVector
.resize( static_cast< size_t >( nElemCount
) );
259 return orVector
.empty() ? 0 : readArray( &orVector
.front(), nElemCount
);
262 template< typename Type
>
263 void BinaryInputStream::skipArray( sal_Int32 nElemCount
)
265 sal_Int32 nSkipSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
266 skip( nSkipSize
, sizeof( Type
) );
269 // ============================================================================
271 /** Wraps a UNO input stream and provides convenient access functions.
273 The binary data in the stream is assumed to be in little-endian format.
275 class OOX_DLLPUBLIC BinaryXInputStream
: public BinaryXSeekableStream
, public BinaryInputStream
278 /** Constructs the wrapper object for the passed input stream.
281 The com.sun.star.io.XInputStream interface of the UNO input stream
285 True = automatically close the wrapped input stream on destruction
286 of this wrapper or when close() is called.
288 explicit BinaryXInputStream(
289 const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>& rxInStrm
,
292 virtual ~BinaryXInputStream();
294 /** Closes the input stream. Does also close the wrapped UNO input stream
295 if bAutoClose has been set to true in the constructor. */
296 virtual void close();
298 /** Reads nBytes bytes to the passed sequence.
299 @return Number of bytes really read. */
300 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
302 /** Reads nBytes bytes to the (existing) buffer opMem.
303 @return Number of bytes really read. */
304 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
306 /** Seeks the stream forward by the passed number of bytes. This works for
307 non-seekable streams too. */
308 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 );
310 /** Stream operator for all data types supported by the readValue() function. */
311 template< typename Type
>
312 inline BinaryXInputStream
& operator>>( Type
& ornValue
) { readValue( ornValue
); return *this; }
315 StreamDataSequence maBuffer
; ///< Data buffer used in readMemory() function.
316 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
>
317 mxInStrm
; ///< Reference to the input stream.
318 bool mbAutoClose
; ///< True = automatically close stream on destruction.
321 // ============================================================================
323 /** Wraps a StreamDataSequence and provides convenient access functions.
325 The binary data in the stream is assumed to be in little-endian format.
327 class OOX_DLLPUBLIC SequenceInputStream
: public SequenceSeekableStream
, public BinaryInputStream
330 /** Constructs the wrapper object for the passed data sequence.
333 The passed data sequence MUST live at least as long as this stream
334 wrapper. The data sequence MUST NOT be changed from outside as long
335 as this stream wrapper is used to read from it.
337 explicit SequenceInputStream( const StreamDataSequence
& rData
);
339 /** Reads nBytes bytes to the passed sequence.
340 @return Number of bytes really read. */
341 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
343 /** Reads nBytes bytes to the (existing) buffer opMem.
344 @return Number of bytes really read. */
345 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
347 /** Seeks the stream forward by the passed number of bytes. This works for
348 non-seekable streams too. */
349 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 );
351 /** Stream operator for all data types supported by the readValue() function. */
352 template< typename Type
>
353 inline SequenceInputStream
& operator>>( Type
& ornValue
) { readValue( ornValue
); return *this; }
356 /** Returns the number of bytes available in the sequence for the passed byte count. */
357 inline sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
358 { return getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, mpData
->getLength() - mnPos
); }
361 // ============================================================================
363 /** Wraps a BinaryInputStream and provides access to a specific part of the
366 Provides access to the stream data block starting at the current position
367 of the stream, and with a specific length. If the wrapped stream is
368 seekable, this wrapper will treat the position of the wrapped stream at
369 construction time as position "0" (therefore the class name).
371 The passed input stream MUST live at least as long as this stream wrapper.
372 The stream MUST NOT be changed from outside as long as this stream wrapper
373 is used to read from it.
375 class RelativeInputStream
: public BinaryInputStream
378 /** Constructs the wrapper object for the passed stream.
381 If specified, restricts the amount of data that can be read from
382 the passed input stream.
384 explicit RelativeInputStream(
385 BinaryInputStream
& rInStrm
,
386 sal_Int64 nSize
= SAL_MAX_INT64
);
388 /** Returns the size of the data block in the wrapped stream offered by
390 virtual sal_Int64
size() const;
392 /** Returns the current relative stream position. */
393 virtual sal_Int64
tell() const;
395 /** Seeks the stream to the passed relative position, if the wrapped stream
397 virtual void seek( sal_Int64 nPos
);
399 /** Closes the input stream but not the wrapped stream. */
400 virtual void close();
402 /** Reads nBytes bytes to the passed sequence. Does not read out of the
403 data block whose size has been specified on construction.
404 @return Number of bytes really read. */
405 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
407 /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
408 the data block whose size has been specified on construction.
409 @return Number of bytes really read. */
410 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
412 /** Seeks the stream forward by the passed number of bytes. This works for
413 non-seekable streams too. Does not seek out of the data block. */
414 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 );
416 /** Stream operator for all data types supported by the readValue() function. */
417 template< typename Type
>
418 inline RelativeInputStream
& operator>>( Type
& ornValue
) { readValue( ornValue
); return *this; }
421 /** Returns the number of bytes available in the sequence for the passed byte count. */
422 inline sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
423 { return getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, mnSize
- mnRelPos
); }
426 BinaryInputStream
* mpInStrm
;
427 sal_Int64 mnStartPos
;
432 // ============================================================================
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */