xref: /aoo41x/main/oox/source/ole/vbainputstream.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/ole/vbainputstream.hxx"
25cdf0e10cSrcweir #include <osl/diagnose.h>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir namespace oox {
28cdf0e10cSrcweir namespace ole {
29cdf0e10cSrcweir 
30cdf0e10cSrcweir // ============================================================================
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir const sal_uInt8 VBASTREAM_SIGNATURE         = 1;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir const sal_uInt16 VBACHUNK_SIGMASK           = 0x7000;
37cdf0e10cSrcweir const sal_uInt16 VBACHUNK_SIG               = 0x3000;
38cdf0e10cSrcweir const sal_uInt16 VBACHUNK_COMPRESSED        = 0x8000;
39cdf0e10cSrcweir const sal_uInt16 VBACHUNK_LENMASK           = 0x0FFF;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir } // namespace
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // ============================================================================
44cdf0e10cSrcweir 
VbaInputStream(BinaryInputStream & rInStrm)45cdf0e10cSrcweir VbaInputStream::VbaInputStream( BinaryInputStream& rInStrm ) :
46cdf0e10cSrcweir     BinaryStreamBase( false ),
47cdf0e10cSrcweir     mpInStrm( &rInStrm ),
48cdf0e10cSrcweir     mnChunkPos( 0 )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     maChunk.reserve( 4096 );
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     sal_uInt8 nSig = rInStrm.readuInt8();
53cdf0e10cSrcweir     OSL_ENSURE( nSig == VBASTREAM_SIGNATURE, "VbaInputStream::VbaInputStream - wrong signature" );
54cdf0e10cSrcweir     mbEof = mbEof || rInStrm.isEof() || (nSig != VBASTREAM_SIGNATURE);
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
size() const57cdf0e10cSrcweir sal_Int64 VbaInputStream::size() const
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     return -1;
60cdf0e10cSrcweir }
61cdf0e10cSrcweir 
tell() const62cdf0e10cSrcweir sal_Int64 VbaInputStream::tell() const
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     return -1;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
seek(sal_Int64)67cdf0e10cSrcweir void VbaInputStream::seek( sal_Int64 )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
close()71cdf0e10cSrcweir void VbaInputStream::close()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     mpInStrm = 0;
74cdf0e10cSrcweir     mbEof = true;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
readData(StreamDataSequence & orData,sal_Int32 nBytes,size_t nAtomSize)77cdf0e10cSrcweir sal_Int32 VbaInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     sal_Int32 nRet = 0;
80cdf0e10cSrcweir     if( !mbEof )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         orData.realloc( ::std::max< sal_Int32 >( nBytes, 0 ) );
83cdf0e10cSrcweir         if( nBytes > 0 )
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             nRet = readMemory( orData.getArray(), nBytes, nAtomSize );
86cdf0e10cSrcweir             if( nRet < nBytes )
87cdf0e10cSrcweir                 orData.realloc( nRet );
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir     return nRet;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
readMemory(void * opMem,sal_Int32 nBytes,size_t)93cdf0e10cSrcweir sal_Int32 VbaInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     sal_Int32 nRet = 0;
96cdf0e10cSrcweir     sal_uInt8* opnMem = reinterpret_cast< sal_uInt8* >( opMem );
97cdf0e10cSrcweir     while( (nBytes > 0) && updateChunk() )
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         sal_Int32 nChunkLeft = static_cast< sal_Int32 >( maChunk.size() - mnChunkPos );
100cdf0e10cSrcweir         sal_Int32 nReadBytes = ::std::min( nBytes, nChunkLeft );
101cdf0e10cSrcweir         memcpy( opnMem, &*(maChunk.begin() + mnChunkPos), nReadBytes );
102cdf0e10cSrcweir         opnMem += nReadBytes;
103cdf0e10cSrcweir         mnChunkPos += static_cast< size_t >( nReadBytes );
104cdf0e10cSrcweir         nBytes -= nReadBytes;
105cdf0e10cSrcweir         nRet += nReadBytes;
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir     return nRet;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
skip(sal_Int32 nBytes,size_t)110cdf0e10cSrcweir void VbaInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     while( (nBytes > 0) && updateChunk() )
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         sal_Int32 nChunkLeft = static_cast< sal_Int32 >( maChunk.size() - mnChunkPos );
115cdf0e10cSrcweir         sal_Int32 nSkipBytes = ::std::min( nBytes, nChunkLeft );
116cdf0e10cSrcweir         mnChunkPos += static_cast< size_t >( nSkipBytes );
117cdf0e10cSrcweir         nBytes -= nSkipBytes;
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir // private --------------------------------------------------------------------
122cdf0e10cSrcweir 
updateChunk()123cdf0e10cSrcweir bool VbaInputStream::updateChunk()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     if( mbEof || (mnChunkPos < maChunk.size()) ) return !mbEof;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     // try to read next chunk header, this may trigger EOF
128cdf0e10cSrcweir     sal_uInt16 nHeader = mpInStrm->readuInt16();
129cdf0e10cSrcweir     mbEof = mpInStrm->isEof();
130cdf0e10cSrcweir     if( mbEof ) return false;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     // check header signature
133cdf0e10cSrcweir     OSL_ENSURE( (nHeader & VBACHUNK_SIGMASK) == VBACHUNK_SIG, "VbaInputStream::updateChunk - invalid chunk signature" );
134cdf0e10cSrcweir     mbEof = (nHeader & VBACHUNK_SIGMASK) != VBACHUNK_SIG;
135cdf0e10cSrcweir     if( mbEof ) return false;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     // decode length of chunk data and compression flag
138cdf0e10cSrcweir     bool bCompressed = getFlag( nHeader, VBACHUNK_COMPRESSED );
139cdf0e10cSrcweir     sal_uInt16 nChunkLen = (nHeader & VBACHUNK_LENMASK) + 1;
140cdf0e10cSrcweir     OSL_ENSURE( bCompressed || (nChunkLen == 4096), "VbaInputStream::updateChunk - invalid uncompressed chunk size" );
141cdf0e10cSrcweir     if( bCompressed )
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         maChunk.clear();
144cdf0e10cSrcweir         sal_uInt8 nBitCount = 4;
145cdf0e10cSrcweir         sal_uInt16 nChunkPos = 0;
146cdf0e10cSrcweir         while( !mbEof && !mpInStrm->isEof() && (nChunkPos < nChunkLen) )
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             sal_uInt8 nTokenFlags = mpInStrm->readuInt8();
149cdf0e10cSrcweir             ++nChunkPos;
150cdf0e10cSrcweir             for( int nBit = 0; !mbEof && !mpInStrm->isEof() && (nBit < 8) && (nChunkPos < nChunkLen); ++nBit, nTokenFlags >>= 1 )
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 if( nTokenFlags & 1 )
153cdf0e10cSrcweir                 {
154cdf0e10cSrcweir                     sal_uInt16 nCopyToken = mpInStrm->readuInt16();
155cdf0e10cSrcweir                     nChunkPos = nChunkPos + 2;
156cdf0e10cSrcweir                     // update bit count used for offset/length in the token
157cdf0e10cSrcweir                     while( static_cast< size_t >( 1 << nBitCount ) < maChunk.size() ) ++nBitCount;
158cdf0e10cSrcweir                     // extract length from lower (16-nBitCount) bits, plus 3
159cdf0e10cSrcweir                     sal_uInt16 nLength = extractValue< sal_uInt16 >( nCopyToken, 0, 16 - nBitCount ) + 3;
160cdf0e10cSrcweir                     // extract offset from high nBitCount bits, plus 1
161cdf0e10cSrcweir                     sal_uInt16 nOffset = extractValue< sal_uInt16 >( nCopyToken, 16 - nBitCount, nBitCount ) + 1;
162cdf0e10cSrcweir                     mbEof = (nOffset > maChunk.size()) || (maChunk.size() + nLength > 4096);
163cdf0e10cSrcweir                     OSL_ENSURE( !mbEof, "VbaInputStream::updateChunk - invalid offset or size in copy token" );
164cdf0e10cSrcweir                     if( !mbEof )
165cdf0e10cSrcweir                     {
166cdf0e10cSrcweir                         // append data to buffer
167cdf0e10cSrcweir                         maChunk.resize( maChunk.size() + nLength );
168cdf0e10cSrcweir                         sal_uInt8* pnTo = &*(maChunk.end() - nLength);
169cdf0e10cSrcweir                         const sal_uInt8* pnEnd = pnTo + nLength;
170cdf0e10cSrcweir                         const sal_uInt8* pnFrom = pnTo - nOffset;
171cdf0e10cSrcweir                         // offset may be less than length, effectively duplicating source data several times
172cdf0e10cSrcweir                         size_t nRunLen = ::std::min< size_t >( nLength, nOffset );
173cdf0e10cSrcweir                         while( pnTo < pnEnd )
174cdf0e10cSrcweir                         {
175cdf0e10cSrcweir                             size_t nStepLen = ::std::min< size_t >( nRunLen, pnEnd - pnTo );
176cdf0e10cSrcweir                             memcpy( pnTo, pnFrom, nStepLen );
177cdf0e10cSrcweir                             pnTo += nStepLen;
178cdf0e10cSrcweir                         }
179cdf0e10cSrcweir                     }
180cdf0e10cSrcweir                 }
181cdf0e10cSrcweir                 else
182cdf0e10cSrcweir                 {
183cdf0e10cSrcweir                     maChunk.resize( maChunk.size() + 1 );
184cdf0e10cSrcweir                     *mpInStrm >> maChunk.back();
185cdf0e10cSrcweir                     ++nChunkPos;
186cdf0e10cSrcweir                 }
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir     else
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         maChunk.resize( nChunkLen );
193cdf0e10cSrcweir         mpInStrm->readMemory( &maChunk.front(), nChunkLen );
194cdf0e10cSrcweir     }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir     mnChunkPos = 0;
197cdf0e10cSrcweir     return !mbEof;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // ============================================================================
201cdf0e10cSrcweir 
202cdf0e10cSrcweir } // namespace ole
203cdf0e10cSrcweir } // namespace oox
204cdf0e10cSrcweir 
205