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
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <oox/dllapi.h>
29 #include <oox/helper/binarystreambase.hxx>
30 #include <oox/helper/helper.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/textenc.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/types.h>
36 namespace com::sun::star
{
37 namespace io
{ class XInputStream
; }
42 class BinaryOutputStream
;
45 /** Interface for binary input stream classes.
47 The binary data in the stream is assumed to be in little-endian format.
49 class OOX_DLLPUBLIC BinaryInputStream
: public virtual BinaryStreamBase
52 /** Derived classes implement reading nBytes bytes to the passed sequence.
53 The sequence will be reallocated internally.
56 The size of the elements in the memory block, if available. Derived
57 classes may be interested in this information.
60 Number of bytes really read.
62 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
64 /** Derived classes implement reading nBytes bytes to the (preallocated!)
68 The size of the elements in the memory block, if available. Derived
69 classes may be interested in this information.
72 Number of bytes really read.
74 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
76 /** Derived classes implement seeking the stream forward by the passed
77 number of bytes. This should work for non-seekable streams too.
80 The size of the elements in the memory block, if available. Derived
81 classes may be interested in this information.
83 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
85 /** Reads a value from the stream and converts it to platform byte order.
86 All data types supported by the ByteOrderConverter class can be used.
88 template< typename Type
>
93 sal_Int8
readInt8() { return readValue
<sal_Int8
>(); }
95 sal_uInt8
readuInt8() { return readValue
<sal_uInt8
>(); }
97 sal_Int16
readInt16() { return readValue
<sal_Int16
>(); }
99 sal_uInt16
readuInt16() { return readValue
<sal_uInt16
>(); }
101 sal_Int32
readInt32() { return readValue
<sal_Int32
>(); }
103 sal_uInt32
readuInt32() { return readValue
<sal_uInt32
>(); }
105 sal_Int64
readInt64() { return readValue
<sal_Int64
>(); }
107 float readFloat() { return readValue
<float>(); }
109 double readDouble() { return readValue
<double>(); }
111 unsigned char readuChar() { return readValue
<unsigned char>(); }
113 /** Reads a (preallocated!) C array of values from the stream.
115 Converts all values in the array to platform byte order. All data types
116 supported by the ByteOrderConverter class can be used.
119 Number of array elements to read (NOT byte count).
122 Number of array elements really read (NOT byte count).
124 template< typename Type
>
125 sal_Int32
readArray( Type
* opnArray
, sal_Int32 nElemCount
);
127 /** Reads a vector of values from the stream.
129 The vector will be resized internally. Converts all values in the
130 vector to platform byte order. All data types supported by the
131 ByteOrderConverter class can be used.
134 Number of elements to put into the vector (NOT byte count).
137 Number of vector elements really read (NOT byte count).
139 template< typename Type
>
140 sal_Int32
readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
);
142 /** Reads a NUL-terminated Unicode character array and returns the string.
144 OUString
readNulUnicodeArray();
146 /** Reads a byte character array and returns the string.
147 NUL characters are replaced by question marks.
150 Number of characters (bytes) to read from the stream.
152 OString
readCharArray( sal_Int32 nChars
);
154 /** Reads a byte character array and returns a Unicode string.
155 NUL characters are replaced by question marks.
158 Number of characters (bytes) to read from the stream.
161 The text encoding used to create the Unicode string.
163 OUString
readCharArrayUC( sal_Int32 nChars
, rtl_TextEncoding eTextEnc
);
165 /** Reads a Unicode character array and returns the string.
166 NUL characters are replaced by question marks (default).
169 Number of 16-bit characters to read from the stream.
171 OUString
readUnicodeArray( sal_Int32 nChars
);
173 /** Reads a Unicode character array (may be compressed) and returns the
175 NUL characters are replaced by question marks (default).
178 Number of 8-bit or 16-bit characters to read from the stream.
181 True = Character array is compressed (stored as 8-bit characters).
182 False = Character array is not compressed (stored as 16-bit characters).
184 OUString
readCompressedUnicodeArray( sal_Int32 nChars
, bool bCompressed
);
186 /** Copies bytes from the current position to the passed output stream.
188 void copyToStream( BinaryOutputStream
& rOutStrm
);
191 /** This dummy default c'tor will never call the c'tor of the virtual base
192 class BinaryStreamBase as this class cannot be instantiated directly. */
193 BinaryInputStream() : BinaryStreamBase( false ) {}
196 BinaryInputStream( BinaryInputStream
const& ) = delete;
197 BinaryInputStream
& operator=( BinaryInputStream
const& ) = delete;
200 typedef std::shared_ptr
< BinaryInputStream
> BinaryInputStreamRef
;
203 template< typename Type
>
204 Type
BinaryInputStream::readValue()
206 Type ornValue
= Type();
207 readMemory( &ornValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
208 ByteOrderConverter::convertLittleEndian( ornValue
);
212 template< typename Type
>
213 sal_Int32
BinaryInputStream::readArray( Type
* opnArray
, sal_Int32 nElemCount
)
218 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
219 nRet
= readMemory( opnArray
, nReadSize
, sizeof( Type
) ) / sizeof( Type
);
220 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nRet
) );
225 template< typename Type
>
226 sal_Int32
BinaryInputStream::readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
)
228 orVector
.resize( static_cast< size_t >( nElemCount
) );
229 return orVector
.empty() ? 0 : readArray(orVector
.data(), nElemCount
);
233 /** Wraps a UNO input stream and provides convenient access functions.
235 The binary data in the stream is assumed to be in little-endian format.
237 class OOX_DLLPUBLIC BinaryXInputStream final
: public BinaryXSeekableStream
, public BinaryInputStream
240 /** Constructs the wrapper object for the passed input stream.
243 The com.sun.star.io.XInputStream interface of the UNO input stream
247 True = automatically close the wrapped input stream on destruction
248 of this wrapper or when close() is called.
250 explicit BinaryXInputStream(
251 const css::uno::Reference
< css::io::XInputStream
>& rxInStrm
,
254 virtual ~BinaryXInputStream() override
;
256 /** Closes the input stream. Does also close the wrapped UNO input stream
257 if bAutoClose has been set to true in the constructor. */
258 virtual void close() override
;
260 /** Reads nBytes bytes to the passed sequence.
261 @return Number of bytes really read. */
262 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
264 /** Reads nBytes bytes to the (existing) buffer opMem.
265 @return Number of bytes really read. */
266 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
268 /** Seeks the stream forward by the passed number of bytes. This works for
269 non-seekable streams too. */
270 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
273 StreamDataSequence maBuffer
; ///< Data buffer used in readMemory() function.
274 css::uno::Reference
< css::io::XInputStream
>
275 mxInStrm
; ///< Reference to the input stream.
276 bool mbAutoClose
; ///< True = automatically close stream on destruction.
280 /** Wraps a StreamDataSequence and provides convenient access functions.
282 The binary data in the stream is assumed to be in little-endian format.
284 class OOX_DLLPUBLIC SequenceInputStream final
: public SequenceSeekableStream
, public BinaryInputStream
287 /** Constructs the wrapper object for the passed data sequence.
290 The passed data sequence MUST live at least as long as this stream
291 wrapper. The data sequence MUST NOT be changed from outside as long
292 as this stream wrapper is used to read from it.
294 explicit SequenceInputStream( const StreamDataSequence
& rData
);
296 /** Reads nBytes bytes to the passed sequence.
297 @return Number of bytes really read. */
298 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
300 /** Reads nBytes bytes to the (existing) buffer opMem.
301 @return Number of bytes really read. */
302 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
304 /** Seeks the stream forward by the passed number of bytes. This works for
305 non-seekable streams too. */
306 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
309 /** Returns the number of bytes available in the sequence for the passed byte count. */
310 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
311 { return getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, mpData
->getLength() - mnPos
); }
315 /** Wraps a BinaryInputStream and provides access to a specific part of the
318 Provides access to the stream data block starting at the current position
319 of the stream, and with a specific length. If the wrapped stream is
320 seekable, this wrapper will treat the position of the wrapped stream at
321 construction time as position "0" (therefore the class name).
323 The passed input stream MUST live at least as long as this stream wrapper.
324 The stream MUST NOT be changed from outside as long as this stream wrapper
325 is used to read from it.
327 class RelativeInputStream final
: public BinaryInputStream
330 /** Constructs the wrapper object for the passed stream.
333 If specified, restricts the amount of data that can be read from
334 the passed input stream.
336 explicit RelativeInputStream(
337 BinaryInputStream
& rInStrm
,
340 /** Returns the size of the data block in the wrapped stream offered by
342 virtual sal_Int64
size() const override
;
344 /** Returns the current relative stream position. */
345 virtual sal_Int64
tell() const override
;
347 /** Seeks the stream to the passed relative position, if the wrapped stream
349 virtual void seek( sal_Int64 nPos
) override
;
351 /** Closes the input stream but not the wrapped stream. */
352 virtual void close() override
;
354 /** Reads nBytes bytes to the passed sequence. Does not read out of the
355 data block whose size has been specified on construction.
356 @return Number of bytes really read. */
357 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
359 /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
360 the data block whose size has been specified on construction.
361 @return Number of bytes really read. */
362 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
364 /** Seeks the stream forward by the passed number of bytes. This works for
365 non-seekable streams too. Does not seek out of the data block. */
366 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
369 /** Returns the number of bytes available in the sequence for the passed byte count. */
370 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
371 { return getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, mnSize
- mnRelPos
); }
374 BinaryInputStream
* mpInStrm
;
375 sal_Int64 mnStartPos
;
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */