xref: /aoo41x/main/oox/source/xls/biffinputstream.cxx (revision ca5ec200)
1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/xls/biffinputstream.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <algorithm>
27cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace oox {
30cdf0e10cSrcweir namespace xls {
31cdf0e10cSrcweir 
32cdf0e10cSrcweir // ============================================================================
33cdf0e10cSrcweir 
34cdf0e10cSrcweir using ::rtl::OString;
35cdf0e10cSrcweir using ::rtl::OStringToOUString;
36cdf0e10cSrcweir using ::rtl::OUString;
37cdf0e10cSrcweir using ::rtl::OUStringBuffer;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace prv {
42cdf0e10cSrcweir 
BiffInputRecordBuffer(BinaryInputStream & rInStrm)43cdf0e10cSrcweir BiffInputRecordBuffer::BiffInputRecordBuffer( BinaryInputStream& rInStrm ) :
44cdf0e10cSrcweir     mrInStrm( rInStrm ),
45cdf0e10cSrcweir     mpCurrentData( 0 ),
46cdf0e10cSrcweir     mnHeaderPos( -1 ),
47cdf0e10cSrcweir     mnBodyPos( 0 ),
48cdf0e10cSrcweir     mnBufferBodyPos( 0 ),
49cdf0e10cSrcweir     mnNextHeaderPos( 0 ),
50cdf0e10cSrcweir     mnRecId( BIFF_ID_UNKNOWN ),
51cdf0e10cSrcweir     mnRecSize( 0 ),
52cdf0e10cSrcweir     mnRecPos( 0 ),
53cdf0e10cSrcweir     mbValidHeader( false )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     OSL_ENSURE( mrInStrm.isSeekable(), "BiffInputRecordBuffer::BiffInputRecordBuffer - stream must be seekable" );
56cdf0e10cSrcweir     mrInStrm.seekToStart();
57cdf0e10cSrcweir     maOriginalData.reserve( SAL_MAX_UINT16 );
58cdf0e10cSrcweir     maDecodedData.reserve( SAL_MAX_UINT16 );
59cdf0e10cSrcweir     enableDecoder( false );     // updates mpCurrentData
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
restartAt(sal_Int64 nPos)62cdf0e10cSrcweir void BiffInputRecordBuffer::restartAt( sal_Int64 nPos )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     mnHeaderPos = -1;
65cdf0e10cSrcweir     mnBodyPos = mnBufferBodyPos = 0;
66cdf0e10cSrcweir     mnNextHeaderPos = nPos;
67cdf0e10cSrcweir     mnRecId = BIFF_ID_UNKNOWN;
68cdf0e10cSrcweir     mnRecSize = mnRecPos = 0;
69cdf0e10cSrcweir     mbValidHeader = false;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
setDecoder(const BiffDecoderRef & rxDecoder)72cdf0e10cSrcweir void BiffInputRecordBuffer::setDecoder( const BiffDecoderRef& rxDecoder )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     mxDecoder = rxDecoder;
75cdf0e10cSrcweir     enableDecoder( true );
76cdf0e10cSrcweir     updateDecoded();
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
enableDecoder(bool bEnable)79cdf0e10cSrcweir void BiffInputRecordBuffer::enableDecoder( bool bEnable )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     mpCurrentData = (bEnable && mxDecoder.get() && mxDecoder->isValid()) ? &maDecodedData : &maOriginalData;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
startRecord(sal_Int64 nHeaderPos)84cdf0e10cSrcweir bool BiffInputRecordBuffer::startRecord( sal_Int64 nHeaderPos )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir     mbValidHeader = (0 <= nHeaderPos) && (nHeaderPos + 4 <= mrInStrm.size());
87cdf0e10cSrcweir     if( mbValidHeader )
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         mnHeaderPos = nHeaderPos;
90cdf0e10cSrcweir         mrInStrm.seek( nHeaderPos );
91cdf0e10cSrcweir         mrInStrm >> mnRecId >> mnRecSize;
92cdf0e10cSrcweir         mnBodyPos = mrInStrm.tell();
93cdf0e10cSrcweir         mnNextHeaderPos = mnBodyPos + mnRecSize;
94cdf0e10cSrcweir         mbValidHeader = !mrInStrm.isEof() && (mnNextHeaderPos <= mrInStrm.size());
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir     if( !mbValidHeader )
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         mnHeaderPos = mnBodyPos = -1;
99cdf0e10cSrcweir         mnNextHeaderPos = 0;
100cdf0e10cSrcweir         mnRecId = BIFF_ID_UNKNOWN;
101cdf0e10cSrcweir         mnRecSize = 0;
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir     mnRecPos = 0;
104cdf0e10cSrcweir     return mbValidHeader;
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
startNextRecord()107cdf0e10cSrcweir bool BiffInputRecordBuffer::startNextRecord()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     return startRecord( mnNextHeaderPos );
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
getNextRecId()112cdf0e10cSrcweir sal_uInt16 BiffInputRecordBuffer::getNextRecId()
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     sal_uInt16 nRecId = BIFF_ID_UNKNOWN;
115cdf0e10cSrcweir     if( mbValidHeader && (mnNextHeaderPos + 4 <= mrInStrm.size()) )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         mrInStrm.seek( mnNextHeaderPos );
118cdf0e10cSrcweir         mrInStrm >> nRecId;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return nRecId;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
read(void * opData,sal_uInt16 nBytes)123cdf0e10cSrcweir void BiffInputRecordBuffer::read( void* opData, sal_uInt16 nBytes )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     updateBuffer();
126cdf0e10cSrcweir     OSL_ENSURE( nBytes > 0, "BiffInputRecordBuffer::read - nothing to read" );
127cdf0e10cSrcweir     OSL_ENSURE( nBytes <= getRecLeft(), "BiffInputRecordBuffer::read - buffer overflow" );
128cdf0e10cSrcweir     memcpy( opData, &(*mpCurrentData)[ mnRecPos ], nBytes );
129cdf0e10cSrcweir     mnRecPos = mnRecPos + nBytes;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
skip(sal_uInt16 nBytes)132cdf0e10cSrcweir void BiffInputRecordBuffer::skip( sal_uInt16 nBytes )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     OSL_ENSURE( nBytes > 0, "BiffInputRecordBuffer::skip - nothing to skip" );
135cdf0e10cSrcweir     OSL_ENSURE( nBytes <= getRecLeft(), "BiffInputRecordBuffer::skip - buffer overflow" );
136cdf0e10cSrcweir     mnRecPos = mnRecPos + nBytes;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
updateBuffer()139cdf0e10cSrcweir void BiffInputRecordBuffer::updateBuffer()
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     OSL_ENSURE( mbValidHeader, "BiffInputRecordBuffer::updateBuffer - invalid access" );
142cdf0e10cSrcweir     if( mnBodyPos != mnBufferBodyPos )
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         mrInStrm.seek( mnBodyPos );
145cdf0e10cSrcweir         maOriginalData.resize( mnRecSize );
146cdf0e10cSrcweir         if( mnRecSize > 0 )
147cdf0e10cSrcweir             mrInStrm.readMemory( &maOriginalData.front(), static_cast< sal_Int32 >( mnRecSize ) );
148cdf0e10cSrcweir         mnBufferBodyPos = mnBodyPos;
149cdf0e10cSrcweir         updateDecoded();
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
updateDecoded()153cdf0e10cSrcweir void BiffInputRecordBuffer::updateDecoded()
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     if( mxDecoder.get() && mxDecoder->isValid() )
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         maDecodedData.resize( mnRecSize );
158cdf0e10cSrcweir         if( mnRecSize > 0 )
159cdf0e10cSrcweir             mxDecoder->decode( &maDecodedData.front(), &maOriginalData.front(), mnBodyPos, mnRecSize );
160cdf0e10cSrcweir     }
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir } // namespace prv
164cdf0e10cSrcweir 
165cdf0e10cSrcweir // ============================================================================
166cdf0e10cSrcweir 
BiffInputStream(BinaryInputStream & rInStream,bool bContLookup)167cdf0e10cSrcweir BiffInputStream::BiffInputStream( BinaryInputStream& rInStream, bool bContLookup ) :
168cdf0e10cSrcweir     BinaryStreamBase( true ),
169cdf0e10cSrcweir     maRecBuffer( rInStream ),
170cdf0e10cSrcweir     mnRecHandle( -1 ),
171cdf0e10cSrcweir     mnRecId( BIFF_ID_UNKNOWN ),
172cdf0e10cSrcweir     mnAltContId( BIFF_ID_UNKNOWN ),
173cdf0e10cSrcweir     mnCurrRecSize( 0 ),
174cdf0e10cSrcweir     mnComplRecSize( 0 ),
175cdf0e10cSrcweir     mbHasComplRec( false ),
176cdf0e10cSrcweir     mbCont( bContLookup )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     mbEof = true;   // EOF will be true if stream is not inside a record
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir // record control -------------------------------------------------------------
182cdf0e10cSrcweir 
startNextRecord()183cdf0e10cSrcweir bool BiffInputStream::startNextRecord()
184cdf0e10cSrcweir {
185cdf0e10cSrcweir     bool bValidRec = false;
186cdf0e10cSrcweir     /*  #i4266# ignore zero records (id==len==0) (e.g. the application
187cdf0e10cSrcweir         "Crystal Report" writes zero records between other records) */
188cdf0e10cSrcweir     bool bIsZeroRec = false;
189cdf0e10cSrcweir     do
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         // record header is never encrypted
192cdf0e10cSrcweir         maRecBuffer.enableDecoder( false );
193cdf0e10cSrcweir         // read header of next raw record, returns false at end of stream
194cdf0e10cSrcweir         bValidRec = maRecBuffer.startNextRecord();
195cdf0e10cSrcweir         // ignore record, if identifier and size are zero
196cdf0e10cSrcweir         bIsZeroRec = (maRecBuffer.getRecId() == 0) && (maRecBuffer.getRecSize() == 0);
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     while( bValidRec && ((mbCont && isContinueId( maRecBuffer.getRecId() )) || bIsZeroRec) );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     // setup other class members
201cdf0e10cSrcweir     setupRecord();
202cdf0e10cSrcweir     return isInRecord();
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
startRecordByHandle(sal_Int64 nRecHandle)205cdf0e10cSrcweir bool BiffInputStream::startRecordByHandle( sal_Int64 nRecHandle )
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     rewindToRecord( nRecHandle );
208cdf0e10cSrcweir     return startNextRecord();
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
resetRecord(bool bContLookup,sal_uInt16 nAltContId)211cdf0e10cSrcweir void BiffInputStream::resetRecord( bool bContLookup, sal_uInt16 nAltContId )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     if( isInRecord() )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         mbCont = bContLookup;
216cdf0e10cSrcweir         mnAltContId = nAltContId;
217cdf0e10cSrcweir         restartRecord( true );
218cdf0e10cSrcweir         maRecBuffer.enableDecoder( true );
219cdf0e10cSrcweir     }
220cdf0e10cSrcweir }
221cdf0e10cSrcweir 
rewindRecord()222cdf0e10cSrcweir void BiffInputStream::rewindRecord()
223cdf0e10cSrcweir {
224cdf0e10cSrcweir     rewindToRecord( mnRecHandle );
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir // decoder --------------------------------------------------------------------
228cdf0e10cSrcweir 
setDecoder(const BiffDecoderRef & rxDecoder)229cdf0e10cSrcweir void BiffInputStream::setDecoder( const BiffDecoderRef& rxDecoder )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir     maRecBuffer.setDecoder( rxDecoder );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
enableDecoder(bool bEnable)234cdf0e10cSrcweir void BiffInputStream::enableDecoder( bool bEnable )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir     maRecBuffer.enableDecoder( bEnable );
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir // stream/record state and info -----------------------------------------------
240cdf0e10cSrcweir 
getNextRecId()241cdf0e10cSrcweir sal_uInt16 BiffInputStream::getNextRecId()
242cdf0e10cSrcweir {
243cdf0e10cSrcweir     sal_uInt16 nRecId = BIFF_ID_UNKNOWN;
244cdf0e10cSrcweir     if( isInRecord() )
245cdf0e10cSrcweir     {
246cdf0e10cSrcweir         sal_Int64 nCurrPos = tell();            // save current position in record
247cdf0e10cSrcweir         while( jumpToNextContinue() ) {}        // skip following CONTINUE records
248cdf0e10cSrcweir         if( maRecBuffer.startNextRecord() )     // read header of next record
249cdf0e10cSrcweir             nRecId = maRecBuffer.getRecId();
250cdf0e10cSrcweir         seek( nCurrPos );                       // restore position, seek() resets old mbValid state
251cdf0e10cSrcweir     }
252cdf0e10cSrcweir     return nRecId;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir // BinaryStreamBase interface (seeking) ---------------------------------------
256cdf0e10cSrcweir 
size() const257cdf0e10cSrcweir sal_Int64 BiffInputStream::size() const
258cdf0e10cSrcweir {
259cdf0e10cSrcweir     if( !mbHasComplRec )
260cdf0e10cSrcweir         const_cast< BiffInputStream* >( this )->calcRecordLength();
261cdf0e10cSrcweir     return mnComplRecSize;
262cdf0e10cSrcweir }
263cdf0e10cSrcweir 
tell() const264cdf0e10cSrcweir sal_Int64 BiffInputStream::tell() const
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     return mbEof ? -1 : (mnCurrRecSize - maRecBuffer.getRecLeft());
267cdf0e10cSrcweir }
268cdf0e10cSrcweir 
seek(sal_Int64 nRecPos)269cdf0e10cSrcweir void BiffInputStream::seek( sal_Int64 nRecPos )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir     if( isInRecord() )
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         if( mbEof || (nRecPos < tell()) )
274cdf0e10cSrcweir             restartRecord( false );
275cdf0e10cSrcweir         if( !mbEof && (nRecPos > tell()) )
276cdf0e10cSrcweir             skip( static_cast< sal_Int32 >( nRecPos - tell() ) );
277cdf0e10cSrcweir     }
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
close()280cdf0e10cSrcweir void BiffInputStream::close()
281cdf0e10cSrcweir {
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
tellBase() const284cdf0e10cSrcweir sal_Int64 BiffInputStream::tellBase() const
285cdf0e10cSrcweir {
286cdf0e10cSrcweir     return maRecBuffer.getBaseStream().tell();
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
sizeBase() const289cdf0e10cSrcweir sal_Int64 BiffInputStream::sizeBase() const
290cdf0e10cSrcweir {
291cdf0e10cSrcweir     return maRecBuffer.getBaseStream().size();
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir // BinaryInputStream interface (stream read access) ---------------------------
295cdf0e10cSrcweir 
readData(StreamDataSequence & orData,sal_Int32 nBytes,size_t nAtomSize)296cdf0e10cSrcweir sal_Int32 BiffInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
297cdf0e10cSrcweir {
298cdf0e10cSrcweir     sal_Int32 nRet = 0;
299cdf0e10cSrcweir     if( !mbEof )
300cdf0e10cSrcweir     {
301cdf0e10cSrcweir         orData.realloc( ::std::max< sal_Int32 >( nBytes, 0 ) );
302cdf0e10cSrcweir         if( nBytes > 0 )
303cdf0e10cSrcweir             nRet = readMemory( orData.getArray(), nBytes, nAtomSize );
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir     return nRet;
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
readMemory(void * opMem,sal_Int32 nBytes,size_t nAtomSize)308cdf0e10cSrcweir sal_Int32 BiffInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
309cdf0e10cSrcweir {
310cdf0e10cSrcweir     sal_Int32 nRet = 0;
311cdf0e10cSrcweir     if( !mbEof && opMem && (nBytes > 0) )
312cdf0e10cSrcweir     {
313cdf0e10cSrcweir         sal_uInt8* pnBuffer = reinterpret_cast< sal_uInt8* >( opMem );
314cdf0e10cSrcweir         sal_Int32 nBytesLeft = nBytes;
315cdf0e10cSrcweir 
316cdf0e10cSrcweir         while( !mbEof && (nBytesLeft > 0) )
317cdf0e10cSrcweir         {
318cdf0e10cSrcweir             sal_uInt16 nReadSize = getMaxRawReadSize( nBytesLeft, nAtomSize );
319cdf0e10cSrcweir             // check nReadSize, stream may already be located at end of a raw record
320cdf0e10cSrcweir             if( nReadSize > 0 )
321cdf0e10cSrcweir             {
322cdf0e10cSrcweir                 maRecBuffer.read( pnBuffer, nReadSize );
323cdf0e10cSrcweir                 nRet += nReadSize;
324cdf0e10cSrcweir                 pnBuffer += nReadSize;
325cdf0e10cSrcweir                 nBytesLeft -= nReadSize;
326cdf0e10cSrcweir             }
327cdf0e10cSrcweir             if( nBytesLeft > 0 )
328cdf0e10cSrcweir                 jumpToNextContinue();
329cdf0e10cSrcweir             OSL_ENSURE( !mbEof, "BiffInputStream::readMemory - record overread" );
330cdf0e10cSrcweir         }
331cdf0e10cSrcweir     }
332cdf0e10cSrcweir     return nRet;
333cdf0e10cSrcweir }
334cdf0e10cSrcweir 
skip(sal_Int32 nBytes,size_t nAtomSize)335cdf0e10cSrcweir void BiffInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir     sal_Int32 nBytesLeft = nBytes;
338cdf0e10cSrcweir     while( !mbEof && (nBytesLeft > 0) )
339cdf0e10cSrcweir     {
340cdf0e10cSrcweir         sal_uInt16 nSkipSize = getMaxRawReadSize( nBytesLeft, nAtomSize );
341cdf0e10cSrcweir         // check nSkipSize, stream may already be located at end of a raw record
342cdf0e10cSrcweir         if( nSkipSize > 0 )
343cdf0e10cSrcweir         {
344cdf0e10cSrcweir             maRecBuffer.skip( nSkipSize );
345cdf0e10cSrcweir             nBytesLeft -= nSkipSize;
346cdf0e10cSrcweir         }
347cdf0e10cSrcweir         if( nBytesLeft > 0 )
348cdf0e10cSrcweir             jumpToNextContinue();
349cdf0e10cSrcweir         OSL_ENSURE( !mbEof, "BiffInputStream::skip - record overread" );
350cdf0e10cSrcweir     }
351cdf0e10cSrcweir }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir // byte strings ---------------------------------------------------------------
354cdf0e10cSrcweir 
readByteString(bool b16BitLen,bool bAllowNulChars)355cdf0e10cSrcweir OString BiffInputStream::readByteString( bool b16BitLen, bool bAllowNulChars )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir     sal_Int32 nStrLen = b16BitLen ? readuInt16() : readuInt8();
358cdf0e10cSrcweir     return readCharArray( nStrLen, bAllowNulChars );
359cdf0e10cSrcweir }
360cdf0e10cSrcweir 
readByteStringUC(bool b16BitLen,rtl_TextEncoding eTextEnc,bool bAllowNulChars)361cdf0e10cSrcweir OUString BiffInputStream::readByteStringUC( bool b16BitLen, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
362cdf0e10cSrcweir {
363cdf0e10cSrcweir     return OStringToOUString( readByteString( b16BitLen, bAllowNulChars ), eTextEnc );
364cdf0e10cSrcweir }
365cdf0e10cSrcweir 
skipByteString(bool b16BitLen)366cdf0e10cSrcweir void BiffInputStream::skipByteString( bool b16BitLen )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     skip( b16BitLen ? readuInt16() : readuInt8() );
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
371cdf0e10cSrcweir // Unicode strings ------------------------------------------------------------
372cdf0e10cSrcweir 
readUniStringChars(sal_uInt16 nChars,bool b16BitChars,bool bAllowNulChars)373cdf0e10cSrcweir OUString BiffInputStream::readUniStringChars( sal_uInt16 nChars, bool b16BitChars, bool bAllowNulChars )
374cdf0e10cSrcweir {
375cdf0e10cSrcweir     OUStringBuffer aBuffer;
376cdf0e10cSrcweir     aBuffer.ensureCapacity( nChars );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir     /*  This function has to react on CONTINUE records which repeat the flags
379cdf0e10cSrcweir         field in their first byte and may change the 8bit/16bit character mode,
380cdf0e10cSrcweir         thus a plain call to readCompressedUnicodeArray() cannot be used here. */
381cdf0e10cSrcweir     sal_Int32 nCharsLeft = nChars;
382cdf0e10cSrcweir     while( !mbEof && (nCharsLeft > 0) )
383cdf0e10cSrcweir     {
384cdf0e10cSrcweir         /*  Read the character array from the remaining part of the current raw
385cdf0e10cSrcweir             record. First, calculate the maximum number of characters that can
386cdf0e10cSrcweir             be read without triggering to start a following CONTINUE record. */
387cdf0e10cSrcweir         sal_Int32 nRawChars = b16BitChars ? (getMaxRawReadSize( nCharsLeft * 2, 2 ) / 2) : getMaxRawReadSize( nCharsLeft, 1 );
388cdf0e10cSrcweir         aBuffer.append( readCompressedUnicodeArray( nRawChars, !b16BitChars, bAllowNulChars ) );
389cdf0e10cSrcweir 
390cdf0e10cSrcweir         /*  Prepare for next CONTINUE record. Calling jumpToNextStringContinue()
391cdf0e10cSrcweir             reads the leading byte in the following CONTINUE record and updates
392cdf0e10cSrcweir             the b16BitChars flag. */
393cdf0e10cSrcweir         nCharsLeft -= nRawChars;
394cdf0e10cSrcweir         if( nCharsLeft > 0 )
395cdf0e10cSrcweir             jumpToNextStringContinue( b16BitChars );
396cdf0e10cSrcweir     }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir     return aBuffer.makeStringAndClear();
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
readUniStringBody(sal_uInt16 nChars,bool bAllowNulChars)401cdf0e10cSrcweir OUString BiffInputStream::readUniStringBody( sal_uInt16 nChars, bool bAllowNulChars )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir     bool b16BitChars;
404cdf0e10cSrcweir     sal_Int32 nAddSize;
405cdf0e10cSrcweir     readUniStringHeader( b16BitChars, nAddSize );
406cdf0e10cSrcweir     OUString aString = readUniStringChars( nChars, b16BitChars, bAllowNulChars );
407cdf0e10cSrcweir     skip( nAddSize );
408cdf0e10cSrcweir     return aString;
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
readUniString(bool bAllowNulChars)411cdf0e10cSrcweir OUString BiffInputStream::readUniString( bool bAllowNulChars )
412cdf0e10cSrcweir {
413cdf0e10cSrcweir     return readUniStringBody( readuInt16(), bAllowNulChars );
414cdf0e10cSrcweir }
415cdf0e10cSrcweir 
skipUniStringChars(sal_uInt16 nChars,bool b16BitChars)416cdf0e10cSrcweir void BiffInputStream::skipUniStringChars( sal_uInt16 nChars, bool b16BitChars )
417cdf0e10cSrcweir {
418cdf0e10cSrcweir     sal_Int32 nCharsLeft = nChars;
419cdf0e10cSrcweir     while( !mbEof && (nCharsLeft > 0) )
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir         // skip the character array
422cdf0e10cSrcweir         sal_Int32 nSkipSize = b16BitChars ? getMaxRawReadSize( 2 * nCharsLeft, 2 ) : getMaxRawReadSize( nCharsLeft, 1 );
423cdf0e10cSrcweir         skip( nSkipSize );
424cdf0e10cSrcweir 
425cdf0e10cSrcweir         // prepare for next CONTINUE record
426cdf0e10cSrcweir         nCharsLeft -= (b16BitChars ? (nSkipSize / 2) : nSkipSize);
427cdf0e10cSrcweir         if( nCharsLeft > 0 )
428cdf0e10cSrcweir             jumpToNextStringContinue( b16BitChars );
429cdf0e10cSrcweir     }
430cdf0e10cSrcweir }
431cdf0e10cSrcweir 
skipUniStringBody(sal_uInt16 nChars)432cdf0e10cSrcweir void BiffInputStream::skipUniStringBody( sal_uInt16 nChars )
433cdf0e10cSrcweir {
434cdf0e10cSrcweir     bool b16BitChars;
435cdf0e10cSrcweir     sal_Int32 nAddSize;
436cdf0e10cSrcweir     readUniStringHeader( b16BitChars, nAddSize );
437cdf0e10cSrcweir     skipUniStringChars( nChars, b16BitChars );
438cdf0e10cSrcweir     skip( nAddSize );
439cdf0e10cSrcweir }
440cdf0e10cSrcweir 
skipUniString()441cdf0e10cSrcweir void BiffInputStream::skipUniString()
442cdf0e10cSrcweir {
443cdf0e10cSrcweir     skipUniStringBody( readuInt16() );
444cdf0e10cSrcweir }
445cdf0e10cSrcweir 
446cdf0e10cSrcweir // private --------------------------------------------------------------------
447cdf0e10cSrcweir 
setupRecord()448cdf0e10cSrcweir void BiffInputStream::setupRecord()
449cdf0e10cSrcweir {
450cdf0e10cSrcweir     // initialize class members
451cdf0e10cSrcweir     mnRecHandle = maRecBuffer.getRecHeaderPos();
452cdf0e10cSrcweir     mnRecId = maRecBuffer.getRecId();
453cdf0e10cSrcweir     mnAltContId = BIFF_ID_UNKNOWN;
454cdf0e10cSrcweir     mnCurrRecSize = mnComplRecSize = maRecBuffer.getRecSize();
455cdf0e10cSrcweir     mbHasComplRec = !mbCont;
456cdf0e10cSrcweir     mbEof = !isInRecord();
457cdf0e10cSrcweir     // enable decoder in new record
458cdf0e10cSrcweir     enableDecoder( true );
459cdf0e10cSrcweir }
460cdf0e10cSrcweir 
restartRecord(bool bInvalidateRecSize)461cdf0e10cSrcweir void BiffInputStream::restartRecord( bool bInvalidateRecSize )
462cdf0e10cSrcweir {
463cdf0e10cSrcweir     if( isInRecord() )
464cdf0e10cSrcweir     {
465cdf0e10cSrcweir         maRecBuffer.startRecord( getRecHandle() );
466cdf0e10cSrcweir         mnCurrRecSize = maRecBuffer.getRecSize();
467cdf0e10cSrcweir         if( bInvalidateRecSize )
468cdf0e10cSrcweir         {
469cdf0e10cSrcweir             mnComplRecSize = mnCurrRecSize;
470cdf0e10cSrcweir             mbHasComplRec = !mbCont;
471cdf0e10cSrcweir         }
472cdf0e10cSrcweir         mbEof = false;
473cdf0e10cSrcweir     }
474cdf0e10cSrcweir }
475cdf0e10cSrcweir 
rewindToRecord(sal_Int64 nRecHandle)476cdf0e10cSrcweir void BiffInputStream::rewindToRecord( sal_Int64 nRecHandle )
477cdf0e10cSrcweir {
478cdf0e10cSrcweir     if( nRecHandle >= 0 )
479cdf0e10cSrcweir     {
480cdf0e10cSrcweir         maRecBuffer.restartAt( nRecHandle );
481cdf0e10cSrcweir         mnRecHandle = -1;
482cdf0e10cSrcweir         mbEof = true;   // as long as the record is not started
483cdf0e10cSrcweir     }
484cdf0e10cSrcweir }
485cdf0e10cSrcweir 
isContinueId(sal_uInt16 nRecId) const486cdf0e10cSrcweir bool BiffInputStream::isContinueId( sal_uInt16 nRecId ) const
487cdf0e10cSrcweir {
488cdf0e10cSrcweir     return (nRecId == BIFF_ID_CONT) || (nRecId == mnAltContId);
489cdf0e10cSrcweir }
490cdf0e10cSrcweir 
jumpToNextContinue()491cdf0e10cSrcweir bool BiffInputStream::jumpToNextContinue()
492cdf0e10cSrcweir {
493cdf0e10cSrcweir     mbEof = mbEof || !mbCont || !isContinueId( maRecBuffer.getNextRecId() ) || !maRecBuffer.startNextRecord();
494cdf0e10cSrcweir     if( !mbEof )
495cdf0e10cSrcweir         mnCurrRecSize += maRecBuffer.getRecSize();
496cdf0e10cSrcweir     return !mbEof;
497cdf0e10cSrcweir }
498cdf0e10cSrcweir 
jumpToNextStringContinue(bool & rb16BitChars)499cdf0e10cSrcweir bool BiffInputStream::jumpToNextStringContinue( bool& rb16BitChars )
500cdf0e10cSrcweir {
501cdf0e10cSrcweir     OSL_ENSURE( maRecBuffer.getRecLeft() == 0, "BiffInputStream::jumpToNextStringContinue - alignment error" );
502cdf0e10cSrcweir 
503cdf0e10cSrcweir     if( mbCont && (getRemaining() > 0) )
504cdf0e10cSrcweir     {
505cdf0e10cSrcweir         jumpToNextContinue();
506cdf0e10cSrcweir     }
507cdf0e10cSrcweir     else if( mnRecId == BIFF_ID_CONT )
508cdf0e10cSrcweir     {
509cdf0e10cSrcweir         /*  CONTINUE handling is off, but we have started reading in a CONTINUE
510cdf0e10cSrcweir             record -> start next CONTINUE for TXO import. We really start a new
511cdf0e10cSrcweir             record here - no chance to return to string origin. */
512cdf0e10cSrcweir         mbEof = mbEof || (maRecBuffer.getNextRecId() != BIFF_ID_CONT) || !maRecBuffer.startNextRecord();
513cdf0e10cSrcweir         if( !mbEof )
514cdf0e10cSrcweir             setupRecord();
515cdf0e10cSrcweir     }
516cdf0e10cSrcweir 
517cdf0e10cSrcweir     // trying to read the flags invalidates stream, if no CONTINUE record has been found
518cdf0e10cSrcweir     sal_uInt8 nFlags;
519cdf0e10cSrcweir     readValue( nFlags );
520cdf0e10cSrcweir     rb16BitChars = getFlag( nFlags, BIFF_STRF_16BIT );
521cdf0e10cSrcweir     return !mbEof;
522cdf0e10cSrcweir }
523cdf0e10cSrcweir 
calcRecordLength()524cdf0e10cSrcweir void BiffInputStream::calcRecordLength()
525cdf0e10cSrcweir {
526cdf0e10cSrcweir     sal_Int64 nCurrPos = tell();            // save current position in record
527cdf0e10cSrcweir     while( jumpToNextContinue() ) {}        // jumpToNextContinue() adds up mnCurrRecSize
528cdf0e10cSrcweir     mnComplRecSize = mnCurrRecSize;
529cdf0e10cSrcweir     mbHasComplRec = true;
530cdf0e10cSrcweir     seek( nCurrPos );                       // restore position, seek() resets old mbValid state
531cdf0e10cSrcweir }
532cdf0e10cSrcweir 
getMaxRawReadSize(sal_Int32 nBytes,size_t nAtomSize) const533cdf0e10cSrcweir sal_uInt16 BiffInputStream::getMaxRawReadSize( sal_Int32 nBytes, size_t nAtomSize ) const
534cdf0e10cSrcweir {
535cdf0e10cSrcweir     sal_uInt16 nMaxSize = getLimitedValue< sal_uInt16, sal_Int32 >( nBytes, 0, maRecBuffer.getRecLeft() );
536cdf0e10cSrcweir     if( (0 < nMaxSize) && (nMaxSize < nBytes) && (nAtomSize > 1) )
537cdf0e10cSrcweir     {
538cdf0e10cSrcweir         // check that remaining data in record buffer is a multiple of the passed atom size
539cdf0e10cSrcweir         sal_uInt16 nPadding = static_cast< sal_uInt16 >( nMaxSize % nAtomSize );
540cdf0e10cSrcweir         OSL_ENSURE( nPadding == 0, "BiffInputStream::getMaxRawReadSize - alignment error" );
541cdf0e10cSrcweir         nMaxSize = nMaxSize - nPadding;
542cdf0e10cSrcweir     }
543cdf0e10cSrcweir     return nMaxSize;
544cdf0e10cSrcweir }
545cdf0e10cSrcweir 
readUniStringHeader(bool & orb16BitChars,sal_Int32 & ornAddSize)546cdf0e10cSrcweir void BiffInputStream::readUniStringHeader( bool& orb16BitChars, sal_Int32& ornAddSize )
547cdf0e10cSrcweir {
548cdf0e10cSrcweir     sal_uInt8 nFlags = readuInt8();
549cdf0e10cSrcweir     OSL_ENSURE( !getFlag( nFlags, BIFF_STRF_UNKNOWN ), "BiffInputStream::readUniStringHeader - unknown flags" );
550cdf0e10cSrcweir     orb16BitChars = getFlag( nFlags, BIFF_STRF_16BIT );
551cdf0e10cSrcweir     sal_uInt16 nFontCount = getFlag( nFlags, BIFF_STRF_RICH ) ? readuInt16() : 0;
552cdf0e10cSrcweir     sal_Int32 nPhoneticSize = getFlag( nFlags, BIFF_STRF_PHONETIC ) ? readInt32() : 0;
553cdf0e10cSrcweir     ornAddSize = 4 * nFontCount + ::std::max< sal_Int32 >( 0, nPhoneticSize );
554cdf0e10cSrcweir }
555cdf0e10cSrcweir 
556cdf0e10cSrcweir // ============================================================================
557cdf0e10cSrcweir 
BiffInputStreamPos(BiffInputStream & rStrm)558cdf0e10cSrcweir BiffInputStreamPos::BiffInputStreamPos( BiffInputStream& rStrm ) :
559cdf0e10cSrcweir     mrStrm( rStrm ),
560cdf0e10cSrcweir     mnRecHandle( rStrm.getRecHandle() ),
561cdf0e10cSrcweir     mnRecPos( rStrm.tell() )
562cdf0e10cSrcweir {
563cdf0e10cSrcweir }
564cdf0e10cSrcweir 
restorePosition()565cdf0e10cSrcweir bool BiffInputStreamPos::restorePosition()
566cdf0e10cSrcweir {
567cdf0e10cSrcweir     bool bValidRec = mrStrm.startRecordByHandle( mnRecHandle );
568cdf0e10cSrcweir     if( bValidRec )
569cdf0e10cSrcweir         mrStrm.seek( mnRecPos );
570cdf0e10cSrcweir     return bValidRec && !mrStrm.isEof();
571cdf0e10cSrcweir }
572cdf0e10cSrcweir 
573cdf0e10cSrcweir // ============================================================================
574cdf0e10cSrcweir 
BiffInputStreamPosGuard(BiffInputStream & rStrm)575cdf0e10cSrcweir BiffInputStreamPosGuard::BiffInputStreamPosGuard( BiffInputStream& rStrm ) :
576cdf0e10cSrcweir     BiffInputStreamPos( rStrm )
577cdf0e10cSrcweir {
578cdf0e10cSrcweir }
579cdf0e10cSrcweir 
~BiffInputStreamPosGuard()580cdf0e10cSrcweir BiffInputStreamPosGuard::~BiffInputStreamPosGuard()
581cdf0e10cSrcweir {
582cdf0e10cSrcweir     restorePosition();
583cdf0e10cSrcweir }
584cdf0e10cSrcweir 
585cdf0e10cSrcweir // ============================================================================
586cdf0e10cSrcweir 
587cdf0e10cSrcweir } // namespace xls
588cdf0e10cSrcweir } // namespace oox
589