xref: /aoo41x/main/sc/source/filter/inc/xistream.hxx (revision 38d50f7b)
1*38d50f7bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*38d50f7bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*38d50f7bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*38d50f7bSAndrew Rist  * distributed with this work for additional information
6*38d50f7bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*38d50f7bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*38d50f7bSAndrew Rist  * "License"); you may not use this file except in compliance
9*38d50f7bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*38d50f7bSAndrew Rist  *
11*38d50f7bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*38d50f7bSAndrew Rist  *
13*38d50f7bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*38d50f7bSAndrew Rist  * software distributed under the License is distributed on an
15*38d50f7bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*38d50f7bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*38d50f7bSAndrew Rist  * specific language governing permissions and limitations
18*38d50f7bSAndrew Rist  * under the License.
19*38d50f7bSAndrew Rist  *
20*38d50f7bSAndrew Rist  *************************************************************/
21*38d50f7bSAndrew Rist 
22*38d50f7bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SC_XISTREAM_HXX
25cdf0e10cSrcweir #define SC_XISTREAM_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <comphelper/docpasswordhelper.hxx>
28cdf0e10cSrcweir #include <filter/msfilter/mscodec.hxx>
29cdf0e10cSrcweir #include "xlstream.hxx"
30cdf0e10cSrcweir #include "xlconst.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir class XclImpRoot;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /* ============================================================================
35cdf0e10cSrcweir Input stream class for Excel import
36cdf0e10cSrcweir - CONTINUE record handling
37cdf0e10cSrcweir - ByteString and UniString support
38cdf0e10cSrcweir - Decryption
39cdf0e10cSrcweir ============================================================================ */
40cdf0e10cSrcweir 
41cdf0e10cSrcweir // ============================================================================
42cdf0e10cSrcweir // Decryption
43cdf0e10cSrcweir // ============================================================================
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class XclImpDecrypter;
46cdf0e10cSrcweir typedef ScfRef< XclImpDecrypter > XclImpDecrypterRef;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /** Base class for BIFF stream decryption. */
49cdf0e10cSrcweir class XclImpDecrypter : public ::comphelper::IDocPasswordVerifier
50cdf0e10cSrcweir {
51cdf0e10cSrcweir public:
52cdf0e10cSrcweir     explicit            XclImpDecrypter();
53cdf0e10cSrcweir     virtual             ~XclImpDecrypter();
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /** Returns the current error code of the decrypter. */
GetError() const56cdf0e10cSrcweir     inline ErrCode      GetError() const { return mnError; }
57cdf0e10cSrcweir     /** Returns true, if the decoder has been initialized correctly. */
IsValid() const58cdf0e10cSrcweir     inline bool         IsValid() const { return mnError == ERRCODE_NONE; }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /** Creates a (ref-counted) copy of this decrypter object. */
61cdf0e10cSrcweir     XclImpDecrypterRef  Clone() const;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /** Implementation of the ::comphelper::IDocPasswordVerifier interface */
64cdf0e10cSrcweir     virtual ::comphelper::DocPasswordVerifierResult verifyPassword( const ::rtl::OUString& rPassword, ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& o_rEncryptionData );
65cdf0e10cSrcweir     virtual ::comphelper::DocPasswordVerifierResult verifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /** Updates the decrypter on start of a new record or after seeking stream. */
68cdf0e10cSrcweir     void                Update( SvStream& rStrm, sal_uInt16 nRecSize );
69cdf0e10cSrcweir     /** Reads and decrypts nBytes bytes and stores data into the existing(!) buffer pData.
70cdf0e10cSrcweir         @return  Count of bytes really read. */
71cdf0e10cSrcweir     sal_uInt16          Read( SvStream& rStrm, void* pData, sal_uInt16 nBytes );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir protected:
74cdf0e10cSrcweir     /** Protected copy c'tor for OnClone(). */
75cdf0e10cSrcweir     explicit            XclImpDecrypter( const XclImpDecrypter& rSrc );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir private:
78cdf0e10cSrcweir     /** Implementation of cloning this object. */
79cdf0e10cSrcweir     virtual XclImpDecrypter* OnClone() const = 0;
80cdf0e10cSrcweir     /** Derived classes implement password verification and initialization of
81cdf0e10cSrcweir         the decoder. */
82cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
83cdf0e10cSrcweir         OnVerifyPassword( const ::rtl::OUString& rPassword ) = 0;
84cdf0e10cSrcweir     virtual bool OnVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData ) = 0;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /** Implementation of updating the decrypter. */
87cdf0e10cSrcweir     virtual void        OnUpdate( sal_Size nOldStrmPos, sal_Size nNewStrmPos, sal_uInt16 nRecSize ) = 0;
88cdf0e10cSrcweir     /** Implementation of the decryption. */
89cdf0e10cSrcweir     virtual sal_uInt16  OnRead( SvStream& rStrm, sal_uInt8* pnData, sal_uInt16 nBytes ) = 0;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir private:
92cdf0e10cSrcweir     ErrCode             mnError;        /// Decrypter error code.
93cdf0e10cSrcweir     sal_Size            mnOldPos;       /// Last known stream position.
94cdf0e10cSrcweir     sal_uInt16          mnRecSize;      /// Current record size.
95cdf0e10cSrcweir };
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // ----------------------------------------------------------------------------
98cdf0e10cSrcweir 
99cdf0e10cSrcweir /** Decrypts BIFF5 stream contents. */
100cdf0e10cSrcweir class XclImpBiff5Decrypter : public XclImpDecrypter
101cdf0e10cSrcweir {
102cdf0e10cSrcweir public:
103cdf0e10cSrcweir     explicit            XclImpBiff5Decrypter( sal_uInt16 nKey, sal_uInt16 nHash );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir private:
106cdf0e10cSrcweir     /** Private copy c'tor for OnClone(). */
107cdf0e10cSrcweir     explicit            XclImpBiff5Decrypter( const XclImpBiff5Decrypter& rSrc );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /** Implementation of cloning this object. */
110cdf0e10cSrcweir     virtual XclImpBiff5Decrypter* OnClone() const;
111cdf0e10cSrcweir     /** Implements password verification and initialization of the decoder. */
112cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
113cdf0e10cSrcweir         OnVerifyPassword( const ::rtl::OUString& rPassword );
114cdf0e10cSrcweir     virtual bool OnVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
115cdf0e10cSrcweir     /** Implementation of updating the decrypter. */
116cdf0e10cSrcweir     virtual void        OnUpdate( sal_Size nOldStrmPos, sal_Size nNewStrmPos, sal_uInt16 nRecSize );
117cdf0e10cSrcweir     /** Implementation of the decryption. */
118cdf0e10cSrcweir     virtual sal_uInt16  OnRead( SvStream& rStrm, sal_uInt8* pnData, sal_uInt16 nBytes );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir private:
121cdf0e10cSrcweir     ::msfilter::MSCodec_XorXLS95 maCodec;       /// Crypto algorithm implementation.
122cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
123cdf0e10cSrcweir     sal_uInt16          mnKey;
124cdf0e10cSrcweir     sal_uInt16          mnHash;
125cdf0e10cSrcweir };
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // ----------------------------------------------------------------------------
128cdf0e10cSrcweir 
129cdf0e10cSrcweir /** Decrypts BIFF8 stream contents using the given document identifier. */
130cdf0e10cSrcweir class XclImpBiff8Decrypter : public XclImpDecrypter
131cdf0e10cSrcweir {
132cdf0e10cSrcweir public:
133cdf0e10cSrcweir     explicit            XclImpBiff8Decrypter( sal_uInt8 pnSalt[ 16 ],
134cdf0e10cSrcweir                             sal_uInt8 pnVerifier[ 16 ], sal_uInt8 pnVerifierHash[ 16 ] );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir private:
137cdf0e10cSrcweir     /** Private copy c'tor for OnClone(). */
138cdf0e10cSrcweir     explicit            XclImpBiff8Decrypter( const XclImpBiff8Decrypter& rSrc );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     /** Implementation of cloning this object. */
141cdf0e10cSrcweir     virtual XclImpBiff8Decrypter* OnClone() const;
142cdf0e10cSrcweir     /** Implements password verification and initialization of the decoder. */
143cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >
144cdf0e10cSrcweir         OnVerifyPassword( const ::rtl::OUString& rPassword );
145cdf0e10cSrcweir     virtual bool OnVerifyEncryptionData( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& rEncryptionData );
146cdf0e10cSrcweir     /** Implementation of updating the decrypter. */
147cdf0e10cSrcweir     virtual void        OnUpdate( sal_Size nOldStrmPos, sal_Size nNewStrmPos, sal_uInt16 nRecSize );
148cdf0e10cSrcweir     /** Implementation of the decryption. */
149cdf0e10cSrcweir     virtual sal_uInt16  OnRead( SvStream& rStrm, sal_uInt8* pnData, sal_uInt16 nBytes );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     /** Returns the block number corresponding to the passed stream position. */
152cdf0e10cSrcweir     sal_uInt32          GetBlock( sal_Size nStrmPos ) const;
153cdf0e10cSrcweir     /** Returns the block offset corresponding to the passed stream position. */
154cdf0e10cSrcweir     sal_uInt16          GetOffset( sal_Size nStrmPos ) const;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir private:
157cdf0e10cSrcweir     ::msfilter::MSCodec_Std97 maCodec;       /// Crypto algorithm implementation.
158cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > maEncryptionData;
159cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maSalt;
160cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maVerifier;
161cdf0e10cSrcweir     ::std::vector< sal_uInt8 > maVerifierHash;
162cdf0e10cSrcweir };
163cdf0e10cSrcweir 
164cdf0e10cSrcweir // ============================================================================
165cdf0e10cSrcweir // Stream
166cdf0e10cSrcweir // ============================================================================
167cdf0e10cSrcweir 
168cdf0e10cSrcweir /** This class represents an Excel stream position.
169cdf0e10cSrcweir     @descr  It contains the relevant data for a stream position inside of a record
170cdf0e10cSrcweir     (including CONTINUE records). */
171cdf0e10cSrcweir class XclImpStreamPos
172cdf0e10cSrcweir {
173cdf0e10cSrcweir public:
174cdf0e10cSrcweir     /** Constructs an invalid stream position data object. */
175cdf0e10cSrcweir     explicit            XclImpStreamPos();
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     /** Sets the stream position data to the passed values. */
178cdf0e10cSrcweir     void                Set( const SvStream& rStrm, sal_Size nNextPos, sal_Size nCurrSize,
179cdf0e10cSrcweir                             sal_uInt16 nRawRecId, sal_uInt16 nRawRecSize, sal_uInt16 nRawRecLeft,
180cdf0e10cSrcweir                             bool bValid );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     /** Writes the contained stream position data to the given variables. */
183cdf0e10cSrcweir     void                Get( SvStream& rStrm, sal_Size& rnNextPos, sal_Size& rnCurrSize,
184cdf0e10cSrcweir                             sal_uInt16& rnRawRecId, sal_uInt16& rnRawRecSize, sal_uInt16& rnRawRecLeft,
185cdf0e10cSrcweir                             bool& rbValid ) const;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     /** Returns the stored stream position. */
GetPos() const188cdf0e10cSrcweir     inline sal_Size     GetPos() const { return mnPos; }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir private:
191cdf0e10cSrcweir     sal_Size            mnPos;          /// Absolute position of the stream.
192cdf0e10cSrcweir     sal_Size            mnNextPos;      /// Absolute position of next record.
193cdf0e10cSrcweir     sal_Size            mnCurrSize;     /// Current calculated size of the record.
194cdf0e10cSrcweir     sal_uInt16          mnRawRecId;     /// Current raw record ID (including CONTINUEs).
195cdf0e10cSrcweir     sal_uInt16          mnRawRecSize;   /// Current raw record size (without following CONTINUEs).
196cdf0e10cSrcweir     sal_uInt16          mnRawRecLeft;   /// Bytes left in current raw record (without following CONTINUEs).
197cdf0e10cSrcweir     bool                mbValid;        /// Read state: false = record overread.
198cdf0e10cSrcweir };
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // ============================================================================
201cdf0e10cSrcweir 
202cdf0e10cSrcweir /** This class is used to import record oriented streams.
203cdf0e10cSrcweir     @descr  An instance is constructed with an SvStream. The SvStream stream is
204cdf0e10cSrcweir     reset to its start while constructing this stream.
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     To start reading a record call StartNextRecord(). Now it is possible to
207cdf0e10cSrcweir     read all contents of the record using operator>>() or any of the Read***()
208cdf0e10cSrcweir     functions. If some data exceeds the record size limit, the stream looks for
209cdf0e10cSrcweir     a following CONTINUE record and jumps automatically to it. It is NOT
210cdf0e10cSrcweir     allowed that an atomic data type is split into two records (i.e. 4 bytes of
211cdf0e10cSrcweir     a double in one record and the other 4 bytes in a following CONTINUE).
212cdf0e10cSrcweir 
213cdf0e10cSrcweir     Trying to read over the record limits results in a stream error. The
214cdf0e10cSrcweir     IsValid() function indicates that with returning false. From now on it is
215cdf0e10cSrcweir     undefined what data the read functions will return. The error state will be
216cdf0e10cSrcweir     reset, if the record is reset (with the method ResetRecord()) or if the
217cdf0e10cSrcweir     next record is started.
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     To switch off the automatic lookup of CONTINUE records, use ResetRecord()
220cdf0e10cSrcweir     with false parameter. This is useful i.e. on import of Escher objects,
221cdf0e10cSrcweir     where sometimes solely CONTINUE records will occur. The automatic lookup
222cdf0e10cSrcweir     keeps switched off until the method ResetRecord() is called with parameter
223cdf0e10cSrcweir     true. All other settings done on the stream (i.e. alternative CONTINUE
224cdf0e10cSrcweir     record identifier, enabled decryption, NUL substitution character) will be
225cdf0e10cSrcweir     reset to default values, if a new record is started.
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     The import stream supports decrypting the stream data. The contents of a
228cdf0e10cSrcweir     record (not the record header) will be encrypted by Excel if the file has
229cdf0e10cSrcweir     been stored with password protection. The functions SetDecrypter(),
230cdf0e10cSrcweir     EnableDecryption(), and DisableDecryption() control the usage of the
231cdf0e10cSrcweir     decryption algorithms. SetDecrypter() sets a new decryption algorithm and
232cdf0e10cSrcweir     initially enables it. DisableDecryption() may be used to stop the usage of
233cdf0e10cSrcweir     the decryption temporarily (sometimes record contents are never encrypted,
234cdf0e10cSrcweir     i.e. all BOF records or the stream position in BOUNDSHEET). Decryption will
235cdf0e10cSrcweir     be reenabled automatically, if a new record is started with the function
236cdf0e10cSrcweir     StartNextRecord().
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     It is possible to store several stream positions inside a record (including
239cdf0e10cSrcweir     its CONTINUE records). The positions are stored on a stack, which can be
240cdf0e10cSrcweir     controlled with the functions PushPosition(), PopPosition() and
241cdf0e10cSrcweir     RejectPosition(). The stack will be cleared whenever a new record is
242cdf0e10cSrcweir     started with the function StartNextRecord().
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     Additionally a single global stream position can be stored which keeps
245cdf0e10cSrcweir     valid during the whole import process (methods StoreGlobalPosition(),
246cdf0e10cSrcweir     SeekGlobalPosition() and DeleteGlobalPosition()). This is the only way to
247cdf0e10cSrcweir     jump back to a previous record (that is a real jump without return).
248cdf0e10cSrcweir */
249cdf0e10cSrcweir class XclImpStream
250cdf0e10cSrcweir {
251cdf0e10cSrcweir public:
252cdf0e10cSrcweir     /** Detects the BIFF version of the passed workbook stream. */
253cdf0e10cSrcweir     static XclBiff      DetectBiffVersion( SvStream& rStrm );
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     /** Constructs the Excel record import stream using a TOOLS stream object.
256cdf0e10cSrcweir         @param rInStrm  The system input stream. Will be set to its start position.
257cdf0e10cSrcweir         Must exist as long as this object exists.
258cdf0e10cSrcweir         @param bContLookup  Automatic CONTINUE lookup on/off. */
259cdf0e10cSrcweir     explicit            XclImpStream(
260cdf0e10cSrcweir                             SvStream& rInStrm,
261cdf0e10cSrcweir                             const XclImpRoot& rRoot,
262cdf0e10cSrcweir                             bool bContLookup = true );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir                         ~XclImpStream();
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     /** Returns the filter root data. */
GetRoot() const267cdf0e10cSrcweir     inline const XclImpRoot& GetRoot() const { return mrRoot; }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     /** Sets stream pointer to the start of the next record content.
270cdf0e10cSrcweir         @descr  Ignores all CONTINUE records of the current record, if automatic
271cdf0e10cSrcweir         CONTINUE usage is switched on.
272cdf0e10cSrcweir         @return  false = no record found (end of stream). */
273cdf0e10cSrcweir     bool                StartNextRecord();
274cdf0e10cSrcweir     /** Sets stream pointer to the start of the record content for the record
275cdf0e10cSrcweir         at the passed absolute stream position.
276cdf0e10cSrcweir         @return  false = no record found (end of stream). */
277cdf0e10cSrcweir     bool                StartNextRecord( sal_Size nNextRecPos );
278cdf0e10cSrcweir     /** Sets stream pointer to begin of record content.
279cdf0e10cSrcweir         @param bContLookup  Automatic CONTINUE lookup on/off. In difference
280cdf0e10cSrcweir         to other stream settings, this setting is persistent until next call of
281cdf0e10cSrcweir         this function (because it is wanted to receive the next CONTINUE
282cdf0e10cSrcweir         records separately).
283cdf0e10cSrcweir         @param nAltContId  Sets an alternative record ID for content
284cdf0e10cSrcweir         continuation. This value is reset automatically when a new record is
285cdf0e10cSrcweir         started with StartNextRecord(). */
286cdf0e10cSrcweir     void                ResetRecord( bool bContLookup,
287cdf0e10cSrcweir                             sal_uInt16 nAltContId = EXC_ID_UNKNOWN );
288cdf0e10cSrcweir     /** Sets stream pointer before current record and invalidates stream.
289cdf0e10cSrcweir         @descr  The next call to StartNextRecord() will start again the current
290cdf0e10cSrcweir         record. This can be used in situations where a loop or a function
291cdf0e10cSrcweir         leaves on a specific record, but the parent context expects to start
292cdf0e10cSrcweir         this record by itself. The stream is invalid as long as the first
293cdf0e10cSrcweir         record has not been started (it is not allowed to call any other stream
294cdf0e10cSrcweir         operation then). */
295cdf0e10cSrcweir     void                RewindRecord();
296cdf0e10cSrcweir 
297cdf0e10cSrcweir     /** Enables decryption of record contents for the rest of the stream. */
298cdf0e10cSrcweir     void                SetDecrypter( XclImpDecrypterRef xDecrypter );
299cdf0e10cSrcweir     /** Sets decrypter from another stream. */
300cdf0e10cSrcweir     void                CopyDecrypterFrom( const XclImpStream& rStrm );
301cdf0e10cSrcweir     /** Returns true, if a valid decrypter is set at the stream. */
302cdf0e10cSrcweir     bool                HasValidDecrypter() const;
303cdf0e10cSrcweir     /** Switches usage of current decryption algorithm on/off.
304cdf0e10cSrcweir         @descr  Encryption is re-enabled automatically, if a new record is
305cdf0e10cSrcweir         started using the function StartNextRecord(). */
306cdf0e10cSrcweir     void                EnableDecryption( bool bEnable = true );
307cdf0e10cSrcweir     /** Switches usage of current decryption algorithm off.
308cdf0e10cSrcweir         @descr  This is a record-local setting. The function StartNextRecord()
309cdf0e10cSrcweir         always enables decryption. */
DisableDecryption()310cdf0e10cSrcweir     inline void         DisableDecryption() { EnableDecryption( false ); }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir     /** Pushes current position on user position stack.
313cdf0e10cSrcweir         @descr  This stack is emptied when starting a new record with
314cdf0e10cSrcweir         StartNextRecord(). The decryption state (enabled/disabled) is not
315cdf0e10cSrcweir         pushed onto the stack. */
316cdf0e10cSrcweir     void                PushPosition();
317cdf0e10cSrcweir     /** Seeks to last position from user position stack.
318cdf0e10cSrcweir         @descr  This position will be removed from the stack. */
319cdf0e10cSrcweir     void                PopPosition();
320cdf0e10cSrcweir //UNUSED2008-05  /** Removes last position from user position stack, but does not seek to it. */
321cdf0e10cSrcweir //UNUSED2008-05  void                RejectPosition();
322cdf0e10cSrcweir 
323cdf0e10cSrcweir     /** Stores current position. This position keeps valid in all records. */
324cdf0e10cSrcweir     void                StoreGlobalPosition();
325cdf0e10cSrcweir     /** Seeks to the stored global user position. */
326cdf0e10cSrcweir     void                SeekGlobalPosition();
327cdf0e10cSrcweir     /** Invalidates global user position. */
DeleteGlobalPosition()328cdf0e10cSrcweir     inline void         DeleteGlobalPosition() { mbHasGlobPos = false; }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     /** Returns record reading state: false = record overread. */
IsValid() const331cdf0e10cSrcweir     inline bool         IsValid() const { return mbValid; }
332cdf0e10cSrcweir     /** Returns the current record ID. */
GetRecId() const333cdf0e10cSrcweir     inline sal_uInt16   GetRecId() const { return mnRecId; }
334cdf0e10cSrcweir     /** Returns the position inside of the whole record content. */
335cdf0e10cSrcweir     sal_Size            GetRecPos() const;
336cdf0e10cSrcweir     /** Returns the data size of the whole record without record headers. */
337cdf0e10cSrcweir     sal_Size            GetRecSize();
338cdf0e10cSrcweir     /** Returns remaining data size of the whole record without record headers. */
339cdf0e10cSrcweir     sal_Size            GetRecLeft();
340cdf0e10cSrcweir     /** Returns the record ID of the following record. */
341cdf0e10cSrcweir     sal_uInt16          GetNextRecId();
342cdf0e10cSrcweir 
343cdf0e10cSrcweir     XclImpStream&       operator>>( sal_Int8& rnValue );
344cdf0e10cSrcweir     XclImpStream&       operator>>( sal_uInt8& rnValue );
345cdf0e10cSrcweir     XclImpStream&       operator>>( sal_Int16& rnValue );
346cdf0e10cSrcweir     XclImpStream&       operator>>( sal_uInt16& rnValue );
347cdf0e10cSrcweir     XclImpStream&       operator>>( sal_Int32& rnValue );
348cdf0e10cSrcweir     XclImpStream&       operator>>( sal_uInt32& rnValue );
349cdf0e10cSrcweir     XclImpStream&       operator>>( float& rfValue );
350cdf0e10cSrcweir     XclImpStream&       operator>>( double& rfValue );
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     sal_Int8            ReadInt8();
353cdf0e10cSrcweir     sal_uInt8           ReaduInt8();
354cdf0e10cSrcweir     sal_Int16           ReadInt16();
355cdf0e10cSrcweir     sal_uInt16          ReaduInt16();
356cdf0e10cSrcweir     sal_Int32           ReadInt32();
357cdf0e10cSrcweir     sal_uInt32          ReaduInt32();
358cdf0e10cSrcweir     float               ReadFloat();
359cdf0e10cSrcweir     double              ReadDouble();
360cdf0e10cSrcweir 
361cdf0e10cSrcweir     /** Reads nBytes bytes to the existing(!) buffer pData.
362cdf0e10cSrcweir         @return  Count of bytes really read. */
363cdf0e10cSrcweir     sal_Size            Read( void* pData, sal_Size nBytes );
364cdf0e10cSrcweir     /** Copies nBytes bytes to rOutStrm.
365cdf0e10cSrcweir         @return  Count of bytes really written. */
366cdf0e10cSrcweir     sal_Size            CopyToStream( SvStream& rOutStrm, sal_Size nBytes );
367cdf0e10cSrcweir 
368cdf0e10cSrcweir     /** Copies the entire record to rOutStrm. The current record position keeps unchanged.
369cdf0e10cSrcweir         @return  Count of bytes really written. */
370cdf0e10cSrcweir     sal_Size            CopyRecordToStream( SvStream& rOutStrm );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir     /** Seeks absolute in record content to the specified position.
373cdf0e10cSrcweir         @descr  The value 0 means start of record, independent from physical stream position. */
374cdf0e10cSrcweir     void                Seek( sal_Size nPos );
375cdf0e10cSrcweir     /** Seeks forward inside the current record. */
376cdf0e10cSrcweir     void                Ignore( sal_Size nBytes );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     // *** special string functions *** ---------------------------------------
379cdf0e10cSrcweir 
380cdf0e10cSrcweir     // *** read/ignore unicode strings *** ------------------------------------
381cdf0e10cSrcweir     /*  - look for CONTINUE records even if CONTINUE handling disabled
382cdf0e10cSrcweir           (only if inside of a CONTINUE record - for TXO import)
383cdf0e10cSrcweir         - no overread assertions (for Applix wrong string length export bug)
384cdf0e10cSrcweir 
385cdf0e10cSrcweir         structure of an Excel unicode string:
386cdf0e10cSrcweir         (1) 2 byte character count
387cdf0e10cSrcweir         (2) 1 byte flags (16-bit-characters, rich string, far east string)
388cdf0e10cSrcweir         (3) [2 byte rich string format run count]
389cdf0e10cSrcweir         (4) [4 byte far east data size]
390cdf0e10cSrcweir         (5) character array
391cdf0e10cSrcweir         (6) [4 * (rich string format run count) byte]
392cdf0e10cSrcweir         (7) [(far east data size) byte]
393cdf0e10cSrcweir         header = (1), (2)
394cdf0e10cSrcweir         ext. header = (3), (4)
395cdf0e10cSrcweir         ext. data = (6), (7)
396cdf0e10cSrcweir      */
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     /** Reads ext. header, detects 8/16 bit mode, sets all ext. info.
399cdf0e10cSrcweir         @return  Total size of ext. data. */
400cdf0e10cSrcweir     sal_Size            ReadUniStringExtHeader(
401cdf0e10cSrcweir                             bool& rb16Bit, bool& rbRich, bool& rbFareast,
402cdf0e10cSrcweir                             sal_uInt16& rnFormatRuns, sal_uInt32& rnExtInf, sal_uInt8 nFlags );
403cdf0e10cSrcweir     /** Seeks to begin of character array, detects 8/16 bit mode.
404cdf0e10cSrcweir         @return  Total size of ext. data. */
405cdf0e10cSrcweir     sal_Size            ReadUniStringExtHeader( bool& rb16Bit, sal_uInt8 nFlags );
406cdf0e10cSrcweir 
407cdf0e10cSrcweir     /** Sets a replacement character for NUL characters.
408cdf0e10cSrcweir         @descr  NUL characters must be replaced, because Tools strings cannot
409cdf0e10cSrcweir         handle them. The substitution character is reset to '?' automatically,
410cdf0e10cSrcweir         if a new record is started using the function StartNextRecord().
411cdf0e10cSrcweir         @param cNulSubst  The character to use for NUL replacement. It is
412cdf0e10cSrcweir         possible to specify NUL here. in this case strings are terminated when
413cdf0e10cSrcweir         the first NUL occurs during string import. */
SetNulSubstChar(sal_Unicode cNulSubst='?')414cdf0e10cSrcweir     inline void         SetNulSubstChar( sal_Unicode cNulSubst = '?' ) { mcNulSubst = cNulSubst; }
415cdf0e10cSrcweir 
416cdf0e10cSrcweir     /** Reads nChars characters and returns the string. */
417cdf0e10cSrcweir     String              ReadRawUniString( sal_uInt16 nChars, bool b16Bit );
418cdf0e10cSrcweir     /** Reads ext. header, nChar characters, ext. data and returns the string. */
419cdf0e10cSrcweir     String              ReadUniString( sal_uInt16 nChars, sal_uInt8 nFlags );
420cdf0e10cSrcweir     /** Reads 8 bit flags, ext. header, nChar characters, ext. data and returns the string. */
421cdf0e10cSrcweir     String              ReadUniString( sal_uInt16 nChars );
422cdf0e10cSrcweir     /** Reads 16 bit character count, 8 bit flags, ext. header, character array,
423cdf0e10cSrcweir         ext. data and returns the string. */
424cdf0e10cSrcweir     String              ReadUniString();
425cdf0e10cSrcweir 
426cdf0e10cSrcweir     /** Ignores nChars characters. */
427cdf0e10cSrcweir     void                IgnoreRawUniString( sal_uInt16 nChars, bool b16Bit );
428cdf0e10cSrcweir     /** Ignores ext. header, nChar characters, ext. data. */
429cdf0e10cSrcweir     void                IgnoreUniString( sal_uInt16 nChars, sal_uInt8 nFlags );
430cdf0e10cSrcweir     /** Ignores 8 bit flags, ext. header, nChar characters, ext. data. */
431cdf0e10cSrcweir     void                IgnoreUniString( sal_uInt16 nChars );
432cdf0e10cSrcweir     /** Ignores 16 bit character count, 8 bit flags, ext. header, character array, ext. data. */
433cdf0e10cSrcweir     void                IgnoreUniString();
434cdf0e10cSrcweir 
435cdf0e10cSrcweir     // *** read/ignore 8-bit-strings, store in String *** ---------------------
436cdf0e10cSrcweir 
437cdf0e10cSrcweir     /** Reads nChar byte characters and returns the string. */
438cdf0e10cSrcweir     String              ReadRawByteString( sal_uInt16 nChars );
439cdf0e10cSrcweir     /** Reads 8/16 bit string length, character array and returns the string. */
440cdf0e10cSrcweir     String              ReadByteString( bool b16BitLen );
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     // *** SvStream functions *** ---------------------------------------------
443cdf0e10cSrcweir 
444cdf0e10cSrcweir     /** Returns the absolute stream position. */
GetSvStreamPos() const445cdf0e10cSrcweir     inline sal_Size     GetSvStreamPos() const { return mrStrm.Tell(); }
446cdf0e10cSrcweir     /** Returns the stream size. */
GetSvStreamSize() const447cdf0e10cSrcweir     inline sal_Size     GetSvStreamSize() const { return mnStreamSize; }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir private:
450cdf0e10cSrcweir     /** Stores current stream position into rPos. */
451cdf0e10cSrcweir     void                StorePosition( XclImpStreamPos& rPos );
452cdf0e10cSrcweir     /** Restores stream position contained in rPos. */
453cdf0e10cSrcweir     void                RestorePosition( const XclImpStreamPos& rPos );
454cdf0e10cSrcweir 
455cdf0e10cSrcweir     /** Seeks to next raw record header and reads record ID and size.
456cdf0e10cSrcweir         @descr  This is a "raw" function, means that stream members are
457cdf0e10cSrcweir         inconsistent after return. Does only change mnRawRecId, mnRawRecSize,
458cdf0e10cSrcweir         and the base stream position, but no other members.
459cdf0e10cSrcweir         @return  false = No record header found (end of stream). */
460cdf0e10cSrcweir     bool                ReadNextRawRecHeader();
461cdf0e10cSrcweir 
462cdf0e10cSrcweir     /** Initializes the decrypter to read a new record. */
463cdf0e10cSrcweir     void                SetupDecrypter();
464cdf0e10cSrcweir     /** Initializes all members after base stream has been seeked to new raw record. */
465cdf0e10cSrcweir     void                SetupRawRecord();
466cdf0e10cSrcweir     /** Initializes all members after base stream has been seeked to new record. */
467cdf0e10cSrcweir     void                SetupRecord();
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     /** Returns true, if the passed ID is real or alternative continuation record ID. */
470cdf0e10cSrcweir     bool                IsContinueId( sal_uInt16 nRecId ) const;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir     /** Goes to start of the next CONTINUE record.
473cdf0e10cSrcweir         @descr  Stream must be located at the end of a raw record, and handling
474cdf0e10cSrcweir         of CONTINUE records must be enabled.
475cdf0e10cSrcweir         @return  Copy of mbValid. */
476cdf0e10cSrcweir     bool                JumpToNextContinue();
477cdf0e10cSrcweir     /** Goes to start of the next CONTINUE record while reading strings.
478cdf0e10cSrcweir         @descr  Stream must be located at the end of a raw record. If reading
479cdf0e10cSrcweir         has been started in a CONTINUE record, jumps to an existing following
480cdf0e10cSrcweir         CONTINUE record, even if handling of CONTINUE records is disabled (This
481cdf0e10cSrcweir         is a special handling for TXO string data). Reads additional Unicode
482cdf0e10cSrcweir         flag byte at start of the new raw record and sets or resets rb16Bit.
483cdf0e10cSrcweir         @return  Copy of mbValid. */
484cdf0e10cSrcweir     bool                JumpToNextStringContinue( bool& rb16Bit );
485cdf0e10cSrcweir 
486cdf0e10cSrcweir     /** Ensures that reading nBytes bytes is possible with next stream access.
487cdf0e10cSrcweir         @descr  Stream must be located at the end of a raw record, and handling
488cdf0e10cSrcweir         of CONTINUE records must be enabled.
489cdf0e10cSrcweir         @return  Copy of mbValid. */
490cdf0e10cSrcweir     bool                EnsureRawReadSize( sal_uInt16 nBytes );
491cdf0e10cSrcweir     /** Returns the maximum size of raw data possible to read in one block. */
492cdf0e10cSrcweir     sal_uInt16          GetMaxRawReadSize( sal_Size nBytes ) const;
493cdf0e10cSrcweir 
494cdf0e10cSrcweir     /** Reads and decrypts nBytes bytes to the existing(!) buffer pData.
495cdf0e10cSrcweir         @return  Count of bytes really read. */
496cdf0e10cSrcweir     sal_uInt16          ReadRawData( void* pData, sal_uInt16 nBytes );
497cdf0e10cSrcweir 
498cdf0e10cSrcweir     /** Reads 8 bit/16 bit string length. */
ReadByteStrLen(bool b16BitLen)499cdf0e10cSrcweir     inline sal_uInt16   ReadByteStrLen( bool b16BitLen )
500cdf0e10cSrcweir                             { return b16BitLen ? ReaduInt16() : ReaduInt8(); }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir private:
503cdf0e10cSrcweir     typedef ::std::vector< XclImpStreamPos > XclImpStreamPosStack;
504cdf0e10cSrcweir 
505cdf0e10cSrcweir     SvStream&           mrStrm;         /// Reference to the system input stream.
506cdf0e10cSrcweir     const XclImpRoot&   mrRoot;         /// Filter root data.
507cdf0e10cSrcweir 
508cdf0e10cSrcweir     XclImpDecrypterRef  mxDecrypter;    /// Provides methods to decrypt data.
509cdf0e10cSrcweir 
510cdf0e10cSrcweir     XclImpStreamPos     maFirstRec;     /// Start position of current record.
511cdf0e10cSrcweir     XclImpStreamPosStack maPosStack;    /// Stack for record positions.
512cdf0e10cSrcweir 
513cdf0e10cSrcweir     XclImpStreamPos     maGlobPos;      /// User defined position elsewhere in stream.
514cdf0e10cSrcweir     sal_uInt16          mnGlobRecId;    /// Record ID for user defined position.
515cdf0e10cSrcweir     bool                mbGlobValidRec; /// Was user position a valid record?
516cdf0e10cSrcweir     bool                mbHasGlobPos;   /// Is user position defined?
517cdf0e10cSrcweir 
518cdf0e10cSrcweir     sal_Size            mnStreamSize;   /// Size of system stream.
519cdf0e10cSrcweir     sal_Size            mnNextRecPos;   /// Start of next record header.
520cdf0e10cSrcweir     sal_Size            mnCurrRecSize;  /// Helper for record position.
521cdf0e10cSrcweir     sal_Size            mnComplRecSize; /// Size of complete record data (with CONTINUEs).
522cdf0e10cSrcweir     bool                mbHasComplRec;  /// true = mnComplRecSize is valid.
523cdf0e10cSrcweir 
524cdf0e10cSrcweir     sal_uInt16          mnRecId;        /// Current record ID (not the CONTINUE ID).
525cdf0e10cSrcweir     sal_uInt16          mnAltContId;    /// Alternative record ID for content continuation.
526cdf0e10cSrcweir 
527cdf0e10cSrcweir     sal_uInt16          mnRawRecId;     /// Current raw record ID (including CONTINUEs).
528cdf0e10cSrcweir     sal_uInt16          mnRawRecSize;   /// Current raw record size (without following CONTINUEs).
529cdf0e10cSrcweir     sal_uInt16          mnRawRecLeft;   /// Bytes left in current raw record (without following CONTINUEs).
530cdf0e10cSrcweir 
531cdf0e10cSrcweir     sal_Unicode         mcNulSubst;     /// Replacement for NUL characters.
532cdf0e10cSrcweir 
533cdf0e10cSrcweir     bool                mbCont;         /// Automatic CONTINUE lookup on/off.
534cdf0e10cSrcweir     bool                mbUseDecr;      /// Usage of decryption.
535cdf0e10cSrcweir     bool                mbValidRec;     /// false = No more records to read.
536cdf0e10cSrcweir     bool                mbValid;        /// false = Record overread.
537cdf0e10cSrcweir };
538cdf0e10cSrcweir 
539cdf0e10cSrcweir // ============================================================================
540cdf0e10cSrcweir 
541cdf0e10cSrcweir #endif
542cdf0e10cSrcweir 
543