xref: /AOO41X/main/oox/inc/oox/xls/biffinputstream.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_XLS_BIFFINPUTSTREAM_HXX
25cdf0e10cSrcweir #define OOX_XLS_BIFFINPUTSTREAM_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir #include "oox/helper/binaryinputstream.hxx"
29cdf0e10cSrcweir #include "oox/xls/biffhelper.hxx"
30cdf0e10cSrcweir #include "oox/xls/biffcodec.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace rtl { class OUStringBuffer; }
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace oox {
35cdf0e10cSrcweir namespace xls {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir // ============================================================================
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace prv {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** Buffers the contents of a raw record and encapsulates stream decoding. */
42cdf0e10cSrcweir class BiffInputRecordBuffer
43cdf0e10cSrcweir {
44cdf0e10cSrcweir public:
45cdf0e10cSrcweir     explicit            BiffInputRecordBuffer( BinaryInputStream& rInStrm );
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /** Returns the wrapped binary base stream. */
getBaseStream() const48cdf0e10cSrcweir     inline const BinaryInputStream& getBaseStream() const { return mrInStrm; }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /** Sets a decoder object and decrypts buffered record data. */
51cdf0e10cSrcweir     void                setDecoder( const BiffDecoderRef& rxDecoder );
52cdf0e10cSrcweir     /** Returns the current decoder object. */
getDecoder() const53cdf0e10cSrcweir     inline BiffDecoderRef getDecoder() const { return mxDecoder; }
54cdf0e10cSrcweir     /** Enables/disables usage of current decoder. */
55cdf0e10cSrcweir     void                enableDecoder( bool bEnable );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /** Restarts the stream at the passed position. Buffer is invalid until the
58cdf0e10cSrcweir         next call of startRecord() or startNextRecord(). */
59cdf0e10cSrcweir     void                restartAt( sal_Int64 nPos );
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     /** Reads the record header at the passed position. */
62cdf0e10cSrcweir     bool                startRecord( sal_Int64 nHeaderPos );
63cdf0e10cSrcweir     /** Reads the next record header from the stream. */
64cdf0e10cSrcweir     bool                startNextRecord();
65cdf0e10cSrcweir     /** Returns the start position of the record header in the core stream. */
66cdf0e10cSrcweir     sal_uInt16          getNextRecId();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     /** Returns the start position of the record header in the core stream. */
getRecHeaderPos() const69cdf0e10cSrcweir     inline sal_Int64    getRecHeaderPos() const { return mnHeaderPos; }
70cdf0e10cSrcweir     /** Returns the current record identifier. */
getRecId() const71cdf0e10cSrcweir     inline sal_uInt16   getRecId() const { return mnRecId; }
72cdf0e10cSrcweir     /** Returns the current record size. */
getRecSize() const73cdf0e10cSrcweir     inline sal_uInt16   getRecSize() const { return mnRecSize; }
74cdf0e10cSrcweir     /** Returns the current read position in the current record body. */
getRecPos() const75cdf0e10cSrcweir     inline sal_uInt16   getRecPos() const { return mnRecPos; }
76cdf0e10cSrcweir     /** Returns the number of remaining bytes in the current record body. */
getRecLeft() const77cdf0e10cSrcweir     inline sal_uInt16   getRecLeft() const { return mnRecSize - mnRecPos; }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /** Reads nBytes bytes to the existing buffer opData. Must NOT overread the source buffer. */
80cdf0e10cSrcweir     void                read( void* opData, sal_uInt16 nBytes );
81cdf0e10cSrcweir     /** Ignores nBytes bytes. Must NOT overread the buffer. */
82cdf0e10cSrcweir     void                skip( sal_uInt16 nBytes );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir private:
85cdf0e10cSrcweir     /** Updates data buffer from stream, if needed. */
86cdf0e10cSrcweir     void                updateBuffer();
87cdf0e10cSrcweir     /** Updates decoded data from original data. */
88cdf0e10cSrcweir     void                updateDecoded();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir private:
91cdf0e10cSrcweir     typedef ::std::vector< sal_uInt8 > DataBuffer;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     BinaryInputStream&  mrInStrm;               /// Core input stream.
94cdf0e10cSrcweir     DataBuffer          maOriginalData;         /// Original data read from stream.
95cdf0e10cSrcweir     DataBuffer          maDecodedData;          /// Decoded data.
96cdf0e10cSrcweir     DataBuffer*         mpCurrentData;          /// Points to data buffer currently in use.
97cdf0e10cSrcweir     BiffDecoderRef      mxDecoder;              /// Decoder object.
98cdf0e10cSrcweir     sal_Int64           mnHeaderPos;            /// Stream start position of current record header.
99cdf0e10cSrcweir     sal_Int64           mnBodyPos;              /// Stream start position of current record body.
100cdf0e10cSrcweir     sal_Int64           mnBufferBodyPos;        /// Stream start position of buffered data.
101cdf0e10cSrcweir     sal_Int64           mnNextHeaderPos;        /// Stream start position of next record header.
102cdf0e10cSrcweir     sal_uInt16          mnRecId;                /// Current record identifier.
103cdf0e10cSrcweir     sal_uInt16          mnRecSize;              /// Current record size.
104cdf0e10cSrcweir     sal_uInt16          mnRecPos;               /// Current position in record body.
105cdf0e10cSrcweir     bool                mbValidHeader;          /// True = valid record header.
106cdf0e10cSrcweir };
107cdf0e10cSrcweir 
108cdf0e10cSrcweir } // namespace prv
109cdf0e10cSrcweir 
110cdf0e10cSrcweir // ============================================================================
111cdf0e10cSrcweir 
112cdf0e10cSrcweir /** This class is used to read BIFF record streams.
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     An instance is constructed with a BinaryInputStream object. The passed
115cdf0e10cSrcweir     stream is reset to its start while constructing this stream.
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     To start reading a record call startNextRecord(). Now it is possible to
118cdf0e10cSrcweir     read all contents of the record using operator>>() or any of the read***()
119cdf0e10cSrcweir     functions. If some data exceeds the record size limit, the stream looks for
120cdf0e10cSrcweir     a following CONTINUE record and jumps automatically to it. It is NOT
121cdf0e10cSrcweir     allowed that an atomic data type is split into two records (e.g. 4 bytes of
122cdf0e10cSrcweir     a double in one record and the other 4 bytes in a following CONTINUE).
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     Trying to read over the record limits results in a stream error. The
125cdf0e10cSrcweir     isValid() function indicates that by returning false. From now on the data
126cdf0e10cSrcweir     returned by the read functions is undefined. The error state will be reset,
127cdf0e10cSrcweir     if the record is reset (with the function resetRecord()), or if the next
128cdf0e10cSrcweir     record is started.
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     To switch off the automatic lookup of CONTINUE records, use resetRecord()
131cdf0e10cSrcweir     with false parameter. This is useful e.g. on import of drawing layer data,
132cdf0e10cSrcweir     where sometimes solely CONTINUE records will occur. The automatic lookup
133cdf0e10cSrcweir     keeps switched off until the method resetRecord() is called with parameter
134cdf0e10cSrcweir     true. All other settings done on the stream (e.g. alternative CONTINUE
135cdf0e10cSrcweir     record identifier, enabled decryption, NUL substitution character) will be
136cdf0e10cSrcweir     reset to default values, if a new record is started.
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     The import stream supports decrypting the stream data. The contents of a
139cdf0e10cSrcweir     record (not the record header) will be encrypted by Excel if the file has
140cdf0e10cSrcweir     been stored with password protection. The functions setDecoder() and
141cdf0e10cSrcweir     enableDecoder() control the usage of the decryption algorithms.
142cdf0e10cSrcweir     setDecoder() sets a new decryption algorithm and initially enables it.
143cdf0e10cSrcweir     enableDecoder( false ) may be used to stop the usage of the decryption
144cdf0e10cSrcweir     temporarily (sometimes record contents are never encrypted, e.g. all BOF
145cdf0e10cSrcweir     records or the stream position in SHEET records). Decryption will be
146cdf0e10cSrcweir     reenabled automatically, if a new record is started with the function
147cdf0e10cSrcweir     startNextRecord().
148cdf0e10cSrcweir */
149cdf0e10cSrcweir class BiffInputStream : public BinaryInputStream
150cdf0e10cSrcweir {
151cdf0e10cSrcweir public:
152cdf0e10cSrcweir     /** Constructs the BIFF record stream using the passed binary stream.
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         @param rInStream
155cdf0e10cSrcweir             The base input stream. Must be seekable. Will be seeked to its
156cdf0e10cSrcweir             start position.
157cdf0e10cSrcweir 
158cdf0e10cSrcweir         @param bContLookup  Automatic CONTINUE lookup on/off.
159cdf0e10cSrcweir      */
160cdf0e10cSrcweir     explicit            BiffInputStream(
161cdf0e10cSrcweir                             BinaryInputStream& rInStream,
162cdf0e10cSrcweir                             bool bContLookup = true );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     // record control ---------------------------------------------------------
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     /** Sets stream pointer to the start of the next record content.
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         Ignores all CONTINUE records of the current record, if automatic
169cdf0e10cSrcweir         CONTINUE usage is switched on.
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         @return  False = no record found (end of stream).
172cdf0e10cSrcweir      */
173cdf0e10cSrcweir     bool                startNextRecord();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     /** Sets stream pointer to the start of the content of the specified record.
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         The handle of the current record can be received and stored using the
178cdf0e10cSrcweir         function getRecHandle() for later usage with this function. The record
179cdf0e10cSrcweir         handle is equivalent to the position of the underlying binary stream,
180cdf0e10cSrcweir         thus the function can be used to perform a hard seek to a specific
181cdf0e10cSrcweir         position, if it is sure that a record starts exactly at this position.
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         @return  False = no record found (invalid handle passed).
184cdf0e10cSrcweir      */
185cdf0e10cSrcweir     bool                startRecordByHandle( sal_Int64 nRecHandle );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     /** Sets stream pointer to begin of record content.
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         @param bContLookup
190cdf0e10cSrcweir             Automatic CONTINUE lookup on/off. In difference to other stream
191cdf0e10cSrcweir             settings, this setting is persistent until next call of this
192cdf0e10cSrcweir             function (because it is wanted to receive the next CONTINUE records
193cdf0e10cSrcweir             separately).
194cdf0e10cSrcweir         @param nAltContId
195cdf0e10cSrcweir             Sets an alternative record identifier for content continuation.
196cdf0e10cSrcweir             This value is reset automatically when a new record is started with
197cdf0e10cSrcweir             startNextRecord().
198cdf0e10cSrcweir      */
199cdf0e10cSrcweir     void                resetRecord(
200cdf0e10cSrcweir                             bool bContLookup,
201cdf0e10cSrcweir                             sal_uInt16 nAltContId = BIFF_ID_UNKNOWN );
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     /** Sets stream pointer before current record and invalidates stream.
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         The next call to startNextRecord() will start again the current record.
206cdf0e10cSrcweir         This can be used in situations where a loop or a function leaves on a
207cdf0e10cSrcweir         specific record, but the parent context expects to start this record by
208cdf0e10cSrcweir         itself. The stream is invalid as long as the first record has not been
209cdf0e10cSrcweir         started (it is not allowed to call any other stream operation then).
210cdf0e10cSrcweir      */
211cdf0e10cSrcweir     void                rewindRecord();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     // decoder ----------------------------------------------------------------
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     /** Sets a new decoder object.
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         Enables decryption of record contents for the rest of the stream.
218cdf0e10cSrcweir      */
219cdf0e10cSrcweir     void                setDecoder( const BiffDecoderRef& rxDecoder );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     /** Enables/disables usage of current decoder.
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         Decryption is reenabled automatically, if a new record is started using
224cdf0e10cSrcweir         the function startNextRecord().
225cdf0e10cSrcweir      */
226cdf0e10cSrcweir     void                enableDecoder( bool bEnable = true );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     // stream/record state and info -------------------------------------------
229cdf0e10cSrcweir 
230cdf0e10cSrcweir     /** Returns the current record identifier. */
getRecId() const231cdf0e10cSrcweir     inline sal_uInt16   getRecId() const { return mnRecId; }
232cdf0e10cSrcweir     /** Returns the record identifier of the following record. */
233cdf0e10cSrcweir     sal_uInt16          getNextRecId();
234cdf0e10cSrcweir 
235cdf0e10cSrcweir     /** Returns a unique handle for the current record that can be used with
236cdf0e10cSrcweir         the function startRecordByHandle(). */
getRecHandle() const237cdf0e10cSrcweir     inline sal_Int64    getRecHandle() const { return mnRecHandle; }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     // BinaryStreamBase interface (seeking) -----------------------------------
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     /** Returns the data size of the whole record without record headers. */
242cdf0e10cSrcweir     virtual sal_Int64   size() const;
243cdf0e10cSrcweir     /** Returns the position inside of the whole record content. */
244cdf0e10cSrcweir     virtual sal_Int64   tell() const;
245cdf0e10cSrcweir     /** Seeks in record content to the specified position. */
246cdf0e10cSrcweir     virtual void        seek( sal_Int64 nRecPos );
247cdf0e10cSrcweir     /** Closes the input stream but not the wrapped stream. */
248cdf0e10cSrcweir     virtual void        close();
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     /** Returns the absolute position in the wrapped binary stream. */
251cdf0e10cSrcweir     sal_Int64           tellBase() const;
252cdf0e10cSrcweir     /** Returns the total size of the wrapped binary stream. */
253cdf0e10cSrcweir     sal_Int64           sizeBase() const;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     // BinaryInputStream interface (stream read access) -----------------------
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     /** Reads nBytes bytes to the passed sequence.
258cdf0e10cSrcweir         @return  Number of bytes really read. */
259cdf0e10cSrcweir     virtual sal_Int32   readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 );
260cdf0e10cSrcweir     /** Reads nBytes bytes and copies them to the passed buffer opMem.
261cdf0e10cSrcweir         @return  Number of bytes really read. */
262cdf0e10cSrcweir     virtual sal_Int32   readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
263cdf0e10cSrcweir     /** Seeks forward inside the current record. */
264cdf0e10cSrcweir     virtual void        skip( sal_Int32 nBytes, size_t nAtomSize = 1 );
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     /** Stream operator for integral and floating-point types. */
267cdf0e10cSrcweir     template< typename Type >
operator >>(Type & ornValue)268cdf0e10cSrcweir     inline BiffInputStream& operator>>( Type& ornValue ) { readValue( ornValue ); return *this; }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir     // byte strings -----------------------------------------------------------
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     /** Reads 8/16 bit string length and character array, and returns the string.
273cdf0e10cSrcweir         @param b16BitLen
274cdf0e10cSrcweir             True = Read 16-bit string length field before the character array.
275cdf0e10cSrcweir             False = Read 8-bit string length field before the character array.
276cdf0e10cSrcweir         @param bAllowNulChars
277cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
278cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
279cdf0e10cSrcweir      */
280cdf0e10cSrcweir     ::rtl::OString      readByteString( bool b16BitLen, bool bAllowNulChars = false );
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     /** Reads 8/16 bit string length and character array, and returns a Unicode string.
283cdf0e10cSrcweir         @param b16BitLen
284cdf0e10cSrcweir             True = Read 16-bit string length field before the character array.
285cdf0e10cSrcweir             False = Read 8-bit string length field before the character array.
286cdf0e10cSrcweir         @param eTextEnc  The text encoding used to create the Unicode string.
287cdf0e10cSrcweir         @param bAllowNulChars
288cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
289cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
290cdf0e10cSrcweir      */
291cdf0e10cSrcweir     ::rtl::OUString     readByteStringUC( bool b16BitLen, rtl_TextEncoding eTextEnc, bool bAllowNulChars = false );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir     /** Ignores 8/16 bit string length and character array.
294cdf0e10cSrcweir         @param b16BitLen
295cdf0e10cSrcweir             True = Read 16-bit string length field before the character array.
296cdf0e10cSrcweir             False = Read 8-bit string length field before the character array.
297cdf0e10cSrcweir      */
298cdf0e10cSrcweir     void                skipByteString( bool b16BitLen );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir     // Unicode strings --------------------------------------------------------
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     /** Reads nChars characters of a BIFF8 string, and returns the string.
303cdf0e10cSrcweir         @param nChars  Number of characters to read from the stream.
304cdf0e10cSrcweir         @param b16BitChars
305cdf0e10cSrcweir             True = The character array contains 16-bit characters.
306cdf0e10cSrcweir             False = The character array contains truncated 8-bit characters.
307cdf0e10cSrcweir         @param bAllowNulChars
308cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
309cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
310cdf0e10cSrcweir      */
311cdf0e10cSrcweir     ::rtl::OUString     readUniStringChars( sal_uInt16 nChars, bool b16BitChars, bool bAllowNulChars = false );
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     /** Reads 8-bit flags, extended header, nChar characters, extended data of
314cdf0e10cSrcweir         a BIFF8 string, and returns the string.
315cdf0e10cSrcweir         @param nChars  Number of characters to read from the stream.
316cdf0e10cSrcweir         @param bAllowNulChars
317cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
318cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
319cdf0e10cSrcweir      */
320cdf0e10cSrcweir     ::rtl::OUString     readUniStringBody( sal_uInt16 nChars, bool bAllowNulChars = false );
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     /** Reads 16-bit character count, 8-bit flags, extended header, character
323cdf0e10cSrcweir         array, extended data of a BIFF8 string, and returns the string.
324cdf0e10cSrcweir         @param bAllowNulChars
325cdf0e10cSrcweir             True = NUL characters are inserted into the imported string.
326cdf0e10cSrcweir             False = NUL characters are replaced by question marks (default).
327cdf0e10cSrcweir      */
328cdf0e10cSrcweir     ::rtl::OUString     readUniString( bool bAllowNulChars = false );
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     /** Ignores nChars characters of a BIFF8 string.
331cdf0e10cSrcweir         @param nChars  Number of characters to skip in the stream.
332cdf0e10cSrcweir         @param b16BitChars
333cdf0e10cSrcweir             True = The character array contains 16-bit characters.
334cdf0e10cSrcweir             False = The character array contains truncated 8-bit characters.
335cdf0e10cSrcweir      */
336cdf0e10cSrcweir     void                skipUniStringChars( sal_uInt16 nChars, bool b16BitChars );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     /** Ignores 8-bit flags, extended header, nChar characters, extended data
339cdf0e10cSrcweir         of a BIFF8 string.
340cdf0e10cSrcweir         @param nChars  Number of characters to skip in the stream.
341cdf0e10cSrcweir      */
342cdf0e10cSrcweir     void                skipUniStringBody( sal_uInt16 nChars );
343cdf0e10cSrcweir 
344cdf0e10cSrcweir     /** Ignores 16-bit character count, 8-bit flags, extended header, character
345cdf0e10cSrcweir         array, extended data of a BIFF8 string.
346cdf0e10cSrcweir      */
347cdf0e10cSrcweir     void                skipUniString();
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     // ------------------------------------------------------------------------
350cdf0e10cSrcweir private:
351cdf0e10cSrcweir     /** Initializes all members after base stream has been seeked to new record. */
352cdf0e10cSrcweir     void                setupRecord();
353cdf0e10cSrcweir     /** Restarts the current record from the beginning. */
354cdf0e10cSrcweir     void                restartRecord( bool bInvalidateRecSize );
355cdf0e10cSrcweir     /** Sets stream pointer before specified record and invalidates stream. */
356cdf0e10cSrcweir     void                rewindToRecord( sal_Int64 nRecHandle );
357cdf0e10cSrcweir     /** Returns true, if stream was able to start a valid record. */
isInRecord() const358cdf0e10cSrcweir     inline bool         isInRecord() const { return mnRecHandle >= 0; }
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     /** Returns true, if the passed ID is real or alternative continuation record ID. */
361cdf0e10cSrcweir     bool                isContinueId( sal_uInt16 nRecId ) const;
362cdf0e10cSrcweir     /** Goes to start of the next CONTINUE record.
363cdf0e10cSrcweir         @descr  Stream must be located at the end of a raw record, and handling
364cdf0e10cSrcweir         of CONTINUE records must be enabled.
365cdf0e10cSrcweir         @return  True if next CONTINUE record has been found and initialized. */
366cdf0e10cSrcweir     bool                jumpToNextContinue();
367cdf0e10cSrcweir     /** Goes to start of the next CONTINUE record while reading strings.
368cdf0e10cSrcweir         @descr  Stream must be located at the end of a raw record. If reading
369cdf0e10cSrcweir         has been started in a CONTINUE record, jumps to an existing following
370cdf0e10cSrcweir         CONTINUE record, even if handling of CONTINUE records is disabled (this
371cdf0e10cSrcweir         is a special handling for TXO string data). Reads additional Unicode
372cdf0e10cSrcweir         flag byte at start of the new raw record and sets or resets rb16BitChars.
373cdf0e10cSrcweir         @return  True if next CONTINUE record has been found and initialized. */
374cdf0e10cSrcweir     bool                jumpToNextStringContinue( bool& rb16BitChars );
375cdf0e10cSrcweir     /** Calculates the complete length of the current record including CONTINUE
376cdf0e10cSrcweir         records, stores the length in mnComplRecSize. */
377cdf0e10cSrcweir     void                calcRecordLength();
378cdf0e10cSrcweir 
379cdf0e10cSrcweir     /** Returns the maximum size of raw data possible to read in one block. */
380cdf0e10cSrcweir     sal_uInt16          getMaxRawReadSize( sal_Int32 nBytes, size_t nAtomSize ) const;
381cdf0e10cSrcweir 
382cdf0e10cSrcweir     /** Reads the BIFF8 Unicode string header fields. */
383cdf0e10cSrcweir     void                readUniStringHeader( bool& orb16BitChars, sal_Int32& ornAddSize );
384cdf0e10cSrcweir 
385cdf0e10cSrcweir private:
386cdf0e10cSrcweir     prv::BiffInputRecordBuffer maRecBuffer; /// Raw record data buffer.
387cdf0e10cSrcweir 
388cdf0e10cSrcweir     sal_Int64           mnRecHandle;        /// Handle of current record.
389cdf0e10cSrcweir     sal_uInt16          mnRecId;            /// Identifier of current record (not the CONTINUE ID).
390cdf0e10cSrcweir     sal_uInt16          mnAltContId;        /// Alternative identifier for content continuation records.
391cdf0e10cSrcweir 
392cdf0e10cSrcweir     sal_Int64           mnCurrRecSize;      /// Helper for record size and position.
393cdf0e10cSrcweir     sal_Int64           mnComplRecSize;     /// Size of complete record data (with CONTINUEs).
394cdf0e10cSrcweir     bool                mbHasComplRec;      /// True = mnComplRecSize is valid.
395cdf0e10cSrcweir 
396cdf0e10cSrcweir     bool                mbCont;             /// True = automatic CONTINUE lookup enabled.
397cdf0e10cSrcweir };
398cdf0e10cSrcweir 
399cdf0e10cSrcweir // ============================================================================
400cdf0e10cSrcweir 
401cdf0e10cSrcweir class BiffInputStreamPos
402cdf0e10cSrcweir {
403cdf0e10cSrcweir public:
404cdf0e10cSrcweir     explicit            BiffInputStreamPos( BiffInputStream& rStrm );
405cdf0e10cSrcweir 
406cdf0e10cSrcweir     bool                restorePosition();
407cdf0e10cSrcweir 
getStream()408cdf0e10cSrcweir     inline BiffInputStream& getStream() { return mrStrm; }
409cdf0e10cSrcweir 
410cdf0e10cSrcweir private:
411cdf0e10cSrcweir     BiffInputStream&    mrStrm;
412cdf0e10cSrcweir     sal_Int64           mnRecHandle;
413cdf0e10cSrcweir     sal_Int64           mnRecPos;
414cdf0e10cSrcweir };
415cdf0e10cSrcweir 
416cdf0e10cSrcweir // ============================================================================
417cdf0e10cSrcweir 
418cdf0e10cSrcweir /** Stores the current position of the passed stream on construction and
419cdf0e10cSrcweir     restores it automatically on destruction. */
420cdf0e10cSrcweir class BiffInputStreamPosGuard : private BiffInputStreamPos
421cdf0e10cSrcweir {
422cdf0e10cSrcweir public:
423cdf0e10cSrcweir     explicit            BiffInputStreamPosGuard( BiffInputStream& rStrm );
424cdf0e10cSrcweir                         ~BiffInputStreamPosGuard();
425cdf0e10cSrcweir };
426cdf0e10cSrcweir 
427cdf0e10cSrcweir // ============================================================================
428cdf0e10cSrcweir 
429cdf0e10cSrcweir } // namespace xls
430cdf0e10cSrcweir } // namespace oox
431cdf0e10cSrcweir 
432cdf0e10cSrcweir #endif
433