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