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 BinaryInputStream() = default;
194 BinaryInputStream( BinaryInputStream
const& ) = delete;
195 BinaryInputStream
& operator=( BinaryInputStream
const& ) = delete;
198 typedef std::shared_ptr
< BinaryInputStream
> BinaryInputStreamRef
;
201 template< typename Type
>
202 Type
BinaryInputStream::readValue()
204 Type ornValue
= Type();
205 readMemory( &ornValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
206 ByteOrderConverter::convertLittleEndian( ornValue
);
210 template< typename Type
>
211 sal_Int32
BinaryInputStream::readArray( Type
* opnArray
, sal_Int32 nElemCount
)
216 sal_Int32 nReadSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
217 nRet
= readMemory( opnArray
, nReadSize
, sizeof( Type
) ) / sizeof( Type
);
218 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nRet
) );
223 template< typename Type
>
224 sal_Int32
BinaryInputStream::readArray( ::std::vector
< Type
>& orVector
, sal_Int32 nElemCount
)
226 orVector
.resize( static_cast< size_t >( nElemCount
) );
227 return orVector
.empty() ? 0 : readArray(orVector
.data(), nElemCount
);
231 /** Wraps a UNO input stream and provides convenient access functions.
233 The binary data in the stream is assumed to be in little-endian format.
235 class OOX_DLLPUBLIC BinaryXInputStream final
: public BinaryXSeekableStream
, public BinaryInputStream
238 /** Constructs the wrapper object for the passed input stream.
241 The com.sun.star.io.XInputStream interface of the UNO input stream
245 True = automatically close the wrapped input stream on destruction
246 of this wrapper or when close() is called.
248 explicit BinaryXInputStream(
249 const css::uno::Reference
< css::io::XInputStream
>& rxInStrm
,
252 virtual ~BinaryXInputStream() override
;
254 /** Closes the input stream. Does also close the wrapped UNO input stream
255 if bAutoClose has been set to true in the constructor. */
256 virtual void close() override
;
258 /** Reads nBytes bytes to the passed sequence.
259 @return Number of bytes really read. */
260 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
262 /** Reads nBytes bytes to the (existing) buffer opMem.
263 @return Number of bytes really read. */
264 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
266 /** Seeks the stream forward by the passed number of bytes. This works for
267 non-seekable streams too. */
268 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
271 StreamDataSequence maBuffer
; ///< Data buffer used in readMemory() function.
272 css::uno::Reference
< css::io::XInputStream
>
273 mxInStrm
; ///< Reference to the input stream.
274 bool mbAutoClose
; ///< True = automatically close stream on destruction.
278 /** Wraps a StreamDataSequence and provides convenient access functions.
280 The binary data in the stream is assumed to be in little-endian format.
282 class OOX_DLLPUBLIC SequenceInputStream final
: public SequenceSeekableStream
, public BinaryInputStream
285 /** Constructs the wrapper object for the passed data sequence.
288 The passed data sequence MUST live at least as long as this stream
289 wrapper. The data sequence MUST NOT be changed from outside as long
290 as this stream wrapper is used to read from it.
292 explicit SequenceInputStream( const StreamDataSequence
& rData
);
294 /** Reads nBytes bytes to the passed sequence.
295 @return Number of bytes really read. */
296 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
298 /** Reads nBytes bytes to the (existing) buffer opMem.
299 @return Number of bytes really read. */
300 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
302 /** Seeks the stream forward by the passed number of bytes. This works for
303 non-seekable streams too. */
304 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
307 /** Returns the number of bytes available in the sequence for the passed byte count. */
308 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
309 { return getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, mpData
->getLength() - mnPos
); }
313 /** Wraps a BinaryInputStream and provides access to a specific part of the
316 Provides access to the stream data block starting at the current position
317 of the stream, and with a specific length. If the wrapped stream is
318 seekable, this wrapper will treat the position of the wrapped stream at
319 construction time as position "0" (therefore the class name).
321 The passed input stream MUST live at least as long as this stream wrapper.
322 The stream MUST NOT be changed from outside as long as this stream wrapper
323 is used to read from it.
325 class RelativeInputStream final
: public BinaryInputStream
328 /** Constructs the wrapper object for the passed stream.
331 If specified, restricts the amount of data that can be read from
332 the passed input stream.
334 explicit RelativeInputStream(
335 BinaryInputStream
& rInStrm
,
338 /** Returns the size of the data block in the wrapped stream offered by
340 virtual sal_Int64
size() const override
;
342 /** Returns the current relative stream position. */
343 virtual sal_Int64
tell() const override
;
345 /** Seeks the stream to the passed relative position, if the wrapped stream
347 virtual void seek( sal_Int64 nPos
) override
;
349 /** Closes the input stream but not the wrapped stream. */
350 virtual void close() override
;
352 /** Reads nBytes bytes to the passed sequence. Does not read out of the
353 data block whose size has been specified on construction.
354 @return Number of bytes really read. */
355 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
357 /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
358 the data block whose size has been specified on construction.
359 @return Number of bytes really read. */
360 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
362 /** Seeks the stream forward by the passed number of bytes. This works for
363 non-seekable streams too. Does not seek out of the data block. */
364 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
367 /** Returns the number of bytes available in the sequence for the passed byte count. */
368 sal_Int32
getMaxBytes( sal_Int32 nBytes
) const
369 { return getLimitedValue
< sal_Int32
, sal_Int64
>( nBytes
, 0, mnSize
- mnRelPos
); }
372 BinaryInputStream
* mpInStrm
;
373 sal_Int64 mnStartPos
;
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */