xref: /AOO41X/main/oox/inc/oox/helper/binaryinputstream.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef OOX_HELPER_BINARYINPUTSTREAM_HXX
29*cdf0e10cSrcweir #define OOX_HELPER_BINARYINPUTSTREAM_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <vector>
32*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
33*cdf0e10cSrcweir #include "oox/helper/binarystreambase.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace com { namespace sun { namespace star {
36*cdf0e10cSrcweir     namespace io { class XInputStream; }
37*cdf0e10cSrcweir } } }
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir namespace oox {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir class BinaryOutputStream;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir // ============================================================================
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /** Interface for binary input stream classes.
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     The binary data in the stream is assumed to be in little-endian format.
48*cdf0e10cSrcweir  */
49*cdf0e10cSrcweir class BinaryInputStream : public virtual BinaryStreamBase
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir public:
52*cdf0e10cSrcweir     /** Derived classes implement reading nBytes bytes to the passed sequence.
53*cdf0e10cSrcweir         The sequence will be reallocated internally.
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir         @param nAtomSize
56*cdf0e10cSrcweir             The size of the elements in the memory block, if available. Derived
57*cdf0e10cSrcweir             classes may be interested in this information.
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         @return
60*cdf0e10cSrcweir             Number of bytes really read.
61*cdf0e10cSrcweir      */
62*cdf0e10cSrcweir     virtual sal_Int32   readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     /** Derived classes implement reading nBytes bytes to the (preallocated!)
65*cdf0e10cSrcweir         memory buffer opMem.
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         @param nAtomSize
68*cdf0e10cSrcweir             The size of the elements in the memory block, if available. Derived
69*cdf0e10cSrcweir             classes may be interested in this information.
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir         @return
72*cdf0e10cSrcweir             Number of bytes really read.
73*cdf0e10cSrcweir      */
74*cdf0e10cSrcweir     virtual sal_Int32   readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     /** Derived classes implement seeking the stream forward by the passed
77*cdf0e10cSrcweir         number of bytes. This should work for non-seekable streams too.
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         @param nAtomSize
80*cdf0e10cSrcweir             The size of the elements in the memory block, if available. Derived
81*cdf0e10cSrcweir             classes may be interested in this information.
82*cdf0e10cSrcweir      */
83*cdf0e10cSrcweir     virtual void        skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     /** Reads a value from the stream and converts it to platform byte order.
86*cdf0e10cSrcweir         All data types supported by the ByteOrderConverter class can be used.
87*cdf0e10cSrcweir      */
88*cdf0e10cSrcweir     template< typename Type >
89*cdf0e10cSrcweir     void                readValue( Type& ornValue );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     /** Reads a value from the stream and converts it to platform byte order.
92*cdf0e10cSrcweir         All data types supported by the ByteOrderConverter class can be used.
93*cdf0e10cSrcweir      */
94*cdf0e10cSrcweir     template< typename Type >
95*cdf0e10cSrcweir     inline Type         readValue() { Type nValue; readValue( nValue ); return nValue; }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     /** Stream operator for all data types supported by the readValue() function. */
98*cdf0e10cSrcweir     template< typename Type >
99*cdf0e10cSrcweir     inline BinaryInputStream& operator>>( Type& ornValue ) { readValue( ornValue ); return *this; }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     inline sal_Int8     readInt8() { return readValue< sal_Int8 >(); }
102*cdf0e10cSrcweir     inline sal_uInt8    readuInt8() { return readValue< sal_uInt8 >(); }
103*cdf0e10cSrcweir     inline sal_Int16    readInt16() { return readValue< sal_Int16 >(); }
104*cdf0e10cSrcweir     inline sal_uInt16   readuInt16() { return readValue< sal_uInt16 >(); }
105*cdf0e10cSrcweir     inline sal_Int32    readInt32() { return readValue< sal_Int32 >(); }
106*cdf0e10cSrcweir     inline sal_uInt32   readuInt32() { return readValue< sal_uInt32 >(); }
107*cdf0e10cSrcweir     inline sal_Int64    readInt64() { return readValue< sal_Int64 >(); }
108*cdf0e10cSrcweir     inline sal_uInt64   readuInt64() { return readValue< sal_uInt64 >(); }
109*cdf0e10cSrcweir     inline float        readFloat() { return readValue< float >(); }
110*cdf0e10cSrcweir     inline double       readDouble() { return readValue< double >(); }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     /** Reads a (preallocated!) C array of values from the stream.
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         Converts all values in the array to platform byte order. All data types
115*cdf0e10cSrcweir         supported by the ByteOrderConverter class can be used.
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         @param nElemCount
118*cdf0e10cSrcweir             Number of array elements to read (NOT byte count).
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         @return
121*cdf0e10cSrcweir             Number of array elements really read (NOT byte count).
122*cdf0e10cSrcweir      */
123*cdf0e10cSrcweir     template< typename Type >
124*cdf0e10cSrcweir     sal_Int32           readArray( Type* opnArray, sal_Int32 nElemCount );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /** Reads a sequence of values from the stream.
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         The sequence will be reallocated internally. Converts all values in the
129*cdf0e10cSrcweir         array to platform byte order. All data types supported by the
130*cdf0e10cSrcweir         ByteOrderConverter class can be used.
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         @param nElemCount
133*cdf0e10cSrcweir             Number of elements to put into the sequence (NOT byte count).
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir         @return
136*cdf0e10cSrcweir             Number of sequence elements really read (NOT byte count).
137*cdf0e10cSrcweir      */
138*cdf0e10cSrcweir     template< typename Type >
139*cdf0e10cSrcweir     sal_Int32           readArray( ::com::sun::star::uno::Sequence< Type >& orSequence, sal_Int32 nElemCount );
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     /** Reads a vector of values from the stream.
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         The vector will be resized internally. Converts all values in the
144*cdf0e10cSrcweir         vector to platform byte order. All data types supported by the
145*cdf0e10cSrcweir         ByteOrderConverter class can be used.
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         @param nElemCount
148*cdf0e10cSrcweir             Number of elements to put into the vector (NOT byte count).
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         @return
151*cdf0e10cSrcweir             Number of vector elements really read (NOT byte count).
152*cdf0e10cSrcweir      */
153*cdf0e10cSrcweir     template< typename Type >
154*cdf0e10cSrcweir     sal_Int32           readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     /** Skips an array of values of a certain type in the stream.
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         All data types supported by the ByteOrderConverter class can be used.
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         @param nElemCount
161*cdf0e10cSrcweir             Number of array elements to skip (NOT byte count).
162*cdf0e10cSrcweir      */
163*cdf0e10cSrcweir     template< typename Type >
164*cdf0e10cSrcweir     void                skipArray( sal_Int32 nElemCount );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     /** Reads a NUL-terminated byte character array and returns the string.
167*cdf0e10cSrcweir      */
168*cdf0e10cSrcweir     ::rtl::OString      readNulCharArray();
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     /** Reads a NUL-terminated byte character array and returns a Unicode string.
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         @param eTextEnc
173*cdf0e10cSrcweir             The text encoding used to create the Unicode string.
174*cdf0e10cSrcweir      */
175*cdf0e10cSrcweir     ::rtl::OUString     readNulCharArrayUC( rtl_TextEncoding eTextEnc );
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     /** Reads a NUL-terminated Unicode character array and returns the string.
178*cdf0e10cSrcweir      */
179*cdf0e10cSrcweir     ::rtl::OUString     readNulUnicodeArray();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     /** Reads a byte character array and returns the string.
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         @param nChars
184*cdf0e10cSrcweir             Number of characters (bytes) to read from the stream.
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         @param bAllowNulChars
187*cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
188*cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
189*cdf0e10cSrcweir      */
190*cdf0e10cSrcweir     ::rtl::OString      readCharArray( sal_Int32 nChars, bool bAllowNulChars = false );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     /** Reads a byte character array and returns a Unicode string.
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         @param nChars
195*cdf0e10cSrcweir             Number of characters (bytes) to read from the stream.
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         @param eTextEnc
198*cdf0e10cSrcweir             The text encoding used to create the Unicode string.
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         @param bAllowNulChars
201*cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
202*cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
203*cdf0e10cSrcweir      */
204*cdf0e10cSrcweir     ::rtl::OUString     readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc, bool bAllowNulChars = false );
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     /** Reads a Unicode character array and returns the string.
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         @param nChars
209*cdf0e10cSrcweir             Number of 16-bit characters to read from the stream.
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         @param bAllowNulChars
212*cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
213*cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
214*cdf0e10cSrcweir      */
215*cdf0e10cSrcweir     ::rtl::OUString     readUnicodeArray( sal_Int32 nChars, bool bAllowNulChars = false );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     /** Reads a Unicode character array (may be compressed) and returns the
218*cdf0e10cSrcweir         string.
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         @param nChars
221*cdf0e10cSrcweir             Number of 8-bit or 16-bit characters to read from the stream.
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir         @param bCompressed
224*cdf0e10cSrcweir             True = Character array is compressed (stored as 8-bit characters).
225*cdf0e10cSrcweir             False = Character array is not compressed (stored as 16-bit characters).
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         @param bAllowNulChars
228*cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
229*cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
230*cdf0e10cSrcweir      */
231*cdf0e10cSrcweir     ::rtl::OUString     readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed, bool bAllowNulChars = false );
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     /** Copies nBytes bytes from the current position to the passed output stream.
234*cdf0e10cSrcweir      */
235*cdf0e10cSrcweir     void                copyToStream( BinaryOutputStream& rOutStrm, sal_Int64 nBytes = SAL_MAX_INT64, sal_Int32 nAtomSize = 1 );
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir protected:
238*cdf0e10cSrcweir     /** This dummy default c'tor will never call the c'tor of the virtual base
239*cdf0e10cSrcweir         class BinaryStreamBase as this class cannot be instanciated directly. */
240*cdf0e10cSrcweir     inline explicit     BinaryInputStream() : BinaryStreamBase( false ) {}
241*cdf0e10cSrcweir };
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir typedef ::boost::shared_ptr< BinaryInputStream > BinaryInputStreamRef;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir // ----------------------------------------------------------------------------
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir template< typename Type >
248*cdf0e10cSrcweir void BinaryInputStream::readValue( Type& ornValue )
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     readMemory( &ornValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
251*cdf0e10cSrcweir     ByteOrderConverter::convertLittleEndian( ornValue );
252*cdf0e10cSrcweir }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir template< typename Type >
255*cdf0e10cSrcweir sal_Int32 BinaryInputStream::readArray( Type* opnArray, sal_Int32 nElemCount )
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir     sal_Int32 nRet = 0;
258*cdf0e10cSrcweir     if( !mbEof )
259*cdf0e10cSrcweir     {
260*cdf0e10cSrcweir         sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
261*cdf0e10cSrcweir         nRet = readMemory( opnArray, nReadSize, sizeof( Type ) ) / sizeof( Type );
262*cdf0e10cSrcweir         ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nRet ) );
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir     return nRet;
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir template< typename Type >
268*cdf0e10cSrcweir sal_Int32 BinaryInputStream::readArray( ::com::sun::star::uno::Sequence< Type >& orSequence, sal_Int32 nElemCount )
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     orSequence.reallocate( nElemCount );
271*cdf0e10cSrcweir     return orSequence.hasElements() ? readArray( orSequence.getArray(), nElemCount ) : 0;
272*cdf0e10cSrcweir }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir template< typename Type >
275*cdf0e10cSrcweir sal_Int32 BinaryInputStream::readArray( ::std::vector< Type >& orVector, sal_Int32 nElemCount )
276*cdf0e10cSrcweir {
277*cdf0e10cSrcweir     orVector.resize( static_cast< size_t >( nElemCount ) );
278*cdf0e10cSrcweir     return orVector.empty() ? 0 : readArray( &orVector.front(), nElemCount );
279*cdf0e10cSrcweir }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir template< typename Type >
282*cdf0e10cSrcweir void BinaryInputStream::skipArray( sal_Int32 nElemCount )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir     sal_Int32 nSkipSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
285*cdf0e10cSrcweir     skip( nSkipSize, sizeof( Type ) );
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir // ============================================================================
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir /** Wraps a UNO input stream and provides convenient access functions.
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir     The binary data in the stream is assumed to be in little-endian format.
293*cdf0e10cSrcweir  */
294*cdf0e10cSrcweir class BinaryXInputStream : public BinaryXSeekableStream, public BinaryInputStream
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir public:
297*cdf0e10cSrcweir     /** Constructs the wrapper object for the passed input stream.
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir         @param rxInStream
300*cdf0e10cSrcweir             The com.sun.star.io.XInputStream interface of the UNO input stream
301*cdf0e10cSrcweir             to be wrapped.
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir         @param bAutoClose
304*cdf0e10cSrcweir             True = automatically close the wrapped input stream on destruction
305*cdf0e10cSrcweir             of this wrapper or when close() is called.
306*cdf0e10cSrcweir      */
307*cdf0e10cSrcweir     explicit            BinaryXInputStream(
308*cdf0e10cSrcweir                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm,
309*cdf0e10cSrcweir                             bool bAutoClose );
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir     virtual             ~BinaryXInputStream();
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir     /** Closes the input stream. Does also close the wrapped UNO input stream
314*cdf0e10cSrcweir         if bAutoClose has been set to true in the constructor. */
315*cdf0e10cSrcweir     virtual void        close();
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     /** Reads nBytes bytes to the passed sequence.
318*cdf0e10cSrcweir         @return  Number of bytes really read. */
319*cdf0e10cSrcweir     virtual sal_Int32   readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 );
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir     /** Reads nBytes bytes to the (existing) buffer opMem.
322*cdf0e10cSrcweir         @return  Number of bytes really read. */
323*cdf0e10cSrcweir     virtual sal_Int32   readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir     /** Seeks the stream forward by the passed number of bytes. This works for
326*cdf0e10cSrcweir         non-seekable streams too. */
327*cdf0e10cSrcweir     virtual void        skip( sal_Int32 nBytes, size_t nAtomSize = 1 );
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     /** Stream operator for all data types supported by the readValue() function. */
330*cdf0e10cSrcweir     template< typename Type >
331*cdf0e10cSrcweir     inline BinaryXInputStream& operator>>( Type& ornValue ) { readValue( ornValue ); return *this; }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir private:
334*cdf0e10cSrcweir     StreamDataSequence  maBuffer;       /// Data buffer used in readMemory() function.
335*cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
336*cdf0e10cSrcweir                         mxInStrm;       /// Reference to the input stream.
337*cdf0e10cSrcweir     bool                mbAutoClose;    /// True = automatically close stream on destruction.
338*cdf0e10cSrcweir };
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir // ============================================================================
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir /** Wraps a StreamDataSequence and provides convenient access functions.
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir     The binary data in the stream is assumed to be in little-endian format.
345*cdf0e10cSrcweir  */
346*cdf0e10cSrcweir class SequenceInputStream : public SequenceSeekableStream, public BinaryInputStream
347*cdf0e10cSrcweir {
348*cdf0e10cSrcweir public:
349*cdf0e10cSrcweir     /** Constructs the wrapper object for the passed data sequence.
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir         @attention
352*cdf0e10cSrcweir             The passed data sequence MUST live at least as long as this stream
353*cdf0e10cSrcweir             wrapper. The data sequence MUST NOT be changed from outside as long
354*cdf0e10cSrcweir             as this stream wrapper is used to read from it.
355*cdf0e10cSrcweir      */
356*cdf0e10cSrcweir     explicit            SequenceInputStream( const StreamDataSequence& rData );
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir     /** Reads nBytes bytes to the passed sequence.
359*cdf0e10cSrcweir         @return  Number of bytes really read. */
360*cdf0e10cSrcweir     virtual sal_Int32   readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 );
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir     /** Reads nBytes bytes to the (existing) buffer opMem.
363*cdf0e10cSrcweir         @return  Number of bytes really read. */
364*cdf0e10cSrcweir     virtual sal_Int32   readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     /** Seeks the stream forward by the passed number of bytes. This works for
367*cdf0e10cSrcweir         non-seekable streams too. */
368*cdf0e10cSrcweir     virtual void        skip( sal_Int32 nBytes, size_t nAtomSize = 1 );
369*cdf0e10cSrcweir 
370*cdf0e10cSrcweir     /** Stream operator for all data types supported by the readValue() function. */
371*cdf0e10cSrcweir     template< typename Type >
372*cdf0e10cSrcweir     inline SequenceInputStream& operator>>( Type& ornValue ) { readValue( ornValue ); return *this; }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir private:
375*cdf0e10cSrcweir     /** Returns the number of bytes available in the sequence for the passed byte count. */
376*cdf0e10cSrcweir     inline sal_Int32    getMaxBytes( sal_Int32 nBytes ) const
377*cdf0e10cSrcweir                             { return getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, mpData->getLength() - mnPos ); }
378*cdf0e10cSrcweir };
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir // ============================================================================
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir /** Wraps a BinaryInputStream and provides access to a specific part of the
383*cdf0e10cSrcweir     stream data.
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir     Provides access to the stream data block starting at the current position
386*cdf0e10cSrcweir     of the stream, and with a specific length. If the wrapped stream is
387*cdf0e10cSrcweir     seekable, this wrapper will treat the position of the wrapped stream at
388*cdf0e10cSrcweir     construction time as position "0" (therefore the class name).
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir     The passed input stream MUST live at least as long as this stream wrapper.
391*cdf0e10cSrcweir     The stream MUST NOT be changed from outside as long as this stream wrapper
392*cdf0e10cSrcweir     is used to read from it.
393*cdf0e10cSrcweir  */
394*cdf0e10cSrcweir class RelativeInputStream : public BinaryInputStream
395*cdf0e10cSrcweir {
396*cdf0e10cSrcweir public:
397*cdf0e10cSrcweir     /** Constructs the wrapper object for the passed stream.
398*cdf0e10cSrcweir 
399*cdf0e10cSrcweir         @param nSize
400*cdf0e10cSrcweir             If specified, restricts the amount of data that can be read from
401*cdf0e10cSrcweir             the passed input stream.
402*cdf0e10cSrcweir      */
403*cdf0e10cSrcweir     explicit            RelativeInputStream(
404*cdf0e10cSrcweir                             BinaryInputStream& rInStrm,
405*cdf0e10cSrcweir                             sal_Int64 nSize = SAL_MAX_INT64 );
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir     /** Returns the size of the data block in the wrapped stream offered by
408*cdf0e10cSrcweir         this wrapper. */
409*cdf0e10cSrcweir     virtual sal_Int64   size() const;
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir     /** Returns the current relative stream position. */
412*cdf0e10cSrcweir     virtual sal_Int64   tell() const;
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir     /** Seeks the stream to the passed relative position, if the wrapped stream
415*cdf0e10cSrcweir         is seekable. */
416*cdf0e10cSrcweir     virtual void        seek( sal_Int64 nPos );
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir     /** Closes the input stream but not the wrapped stream. */
419*cdf0e10cSrcweir     virtual void        close();
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir     /** Reads nBytes bytes to the passed sequence. Does not read out of the
422*cdf0e10cSrcweir         data block whose size has been specified on construction.
423*cdf0e10cSrcweir         @return  Number of bytes really read. */
424*cdf0e10cSrcweir     virtual sal_Int32   readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 );
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir     /** Reads nBytes bytes to the (existing) buffer opMem. Does not read out of
427*cdf0e10cSrcweir         the data block whose size has been specified on construction.
428*cdf0e10cSrcweir         @return  Number of bytes really read. */
429*cdf0e10cSrcweir     virtual sal_Int32   readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir     /** Seeks the stream forward by the passed number of bytes. This works for
432*cdf0e10cSrcweir         non-seekable streams too. Does not seek out of the data block. */
433*cdf0e10cSrcweir     virtual void        skip( sal_Int32 nBytes, size_t nAtomSize = 1 );
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir     /** Stream operator for all data types supported by the readValue() function. */
436*cdf0e10cSrcweir     template< typename Type >
437*cdf0e10cSrcweir     inline RelativeInputStream& operator>>( Type& ornValue ) { readValue( ornValue ); return *this; }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir private:
440*cdf0e10cSrcweir     /** Returns the number of bytes available in the sequence for the passed byte count. */
441*cdf0e10cSrcweir     inline sal_Int32    getMaxBytes( sal_Int32 nBytes ) const
442*cdf0e10cSrcweir                             { return getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, mnSize - mnRelPos ); }
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir private:
445*cdf0e10cSrcweir     BinaryInputStream*  mpInStrm;
446*cdf0e10cSrcweir     sal_Int64           mnStartPos;
447*cdf0e10cSrcweir     sal_Int64           mnRelPos;
448*cdf0e10cSrcweir     sal_Int64           mnSize;
449*cdf0e10cSrcweir };
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir // ============================================================================
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir } // namespace oox
454*cdf0e10cSrcweir 
455*cdf0e10cSrcweir #endif
456