bump product version to 4.2.0.1
[LibreOffice.git] / oox / source / ole / axbinarywriter.cxx
blob3f71dc2633878dba75cdf0471e03118904da61b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #include "oox/ole/axbinarywriter.hxx"
11 #include "oox/ole/olehelper.hxx"
13 namespace oox {
14 namespace ole {
16 // ============================================================================
18 namespace {
20 const sal_uInt32 AX_STRING_COMPRESSED = 0x80000000;
22 } // namespace
24 // ============================================================================
26 AxAlignedOutputStream::AxAlignedOutputStream( BinaryOutputStream& rOutStrm ) :
27 BinaryStreamBase( false ),
28 mpOutStrm( &rOutStrm ),
29 mnStrmPos( 0 ),
30 mnStrmSize( rOutStrm.getRemaining() ),
31 mnWrappedBeginPos( rOutStrm.tell() )
33 mbEof = mbEof || rOutStrm.isEof();
36 sal_Int64 AxAlignedOutputStream::size() const
38 return mpOutStrm ? mnStrmSize : -1;
41 sal_Int64 AxAlignedOutputStream::tell() const
43 return mpOutStrm ? mnStrmPos : -1;
46 void AxAlignedOutputStream::seek( sal_Int64 nPos )
48 mbEof = (nPos < 0);
49 if( !mbEof )
51 mpOutStrm->seek( static_cast< sal_Int32 >( mnWrappedBeginPos + nPos ) );
52 mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
56 void AxAlignedOutputStream::close()
58 mpOutStrm = 0;
59 mbEof = true;
62 void AxAlignedOutputStream::writeData( const StreamDataSequence& orData, size_t nAtomSize )
64 mpOutStrm->writeData( orData, nAtomSize );
65 mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
68 void AxAlignedOutputStream::writeMemory( const void* opMem, sal_Int32 nBytes, size_t nAtomSize )
70 mpOutStrm->writeMemory( opMem, nBytes, nAtomSize );
71 mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
74 void AxAlignedOutputStream::pad( sal_Int32 nBytes, size_t nAtomSize )
76 //PRESUMABELY we need to pad with 0's here as appropriate
77 com::sun::star::uno::Sequence< sal_Int8 > aData( nBytes );
78 // ok we could be padding with rubbish here, but really that shouldn't matter
79 // set to 0(s), easier to not get fooled by 0's when looking at
80 // binary content......
81 memset( static_cast<void*>( aData.getArray() ), 0, nBytes );
82 mpOutStrm->writeData( aData, nAtomSize );
83 mnStrmPos = mpOutStrm->tell() - mnWrappedBeginPos;
86 void AxAlignedOutputStream::align( size_t nSize )
88 pad( static_cast< sal_Int32 >( (nSize - (mnStrmPos % nSize)) % nSize ) );
91 // ============================================================================
93 namespace {
95 void lclWriteString( AxAlignedOutputStream& rOutStrm, OUString& rValue, sal_uInt32 nSize, bool bArrayString )
97 bool bCompressed = getFlag( nSize, AX_STRING_COMPRESSED );
98 rOutStrm.writeCompressedUnicodeArray( rValue, bCompressed || bArrayString );
101 } // namespace
103 // ----------------------------------------------------------------------------
105 AxBinaryPropertyWriter::ComplexProperty::~ComplexProperty()
109 bool AxBinaryPropertyWriter::PairProperty::writeProperty( AxAlignedOutputStream& rOutStrm )
111 rOutStrm << mrPairData.first << mrPairData.second;
112 return true;
115 bool AxBinaryPropertyWriter::StringProperty::writeProperty( AxAlignedOutputStream& rOutStrm )
117 lclWriteString( rOutStrm, mrValue, mnSize, false );
118 return true;
121 // ----------------------------------------------------------------------------
123 AxBinaryPropertyWriter::AxBinaryPropertyWriter( BinaryOutputStream& rOutStrm, bool b64BitPropFlags ) :
124 maOutStrm( rOutStrm ),
125 mnPropFlags( 0x0 ),
126 mbValid( true ),
127 mb64BitPropFlags( b64BitPropFlags )
129 sal_uInt16 nId( 0x0200 );
130 maOutStrm << nId;
131 mnBlockSize = 0; // will be filled in the finalize method
133 maOutStrm << nId;
134 mnPropFlagsStart = maOutStrm.tell();
136 if( mb64BitPropFlags )
137 maOutStrm << mnPropFlags;
138 else
139 maOutStrm << sal_uInt32( mnPropFlags );
140 mnNextProp = 1;
143 void AxBinaryPropertyWriter::writeBoolProperty( bool orbValue, bool bReverse )
145 // orbValue ^ bReverse true then we want to set the bit, e.g. don't skip
146 startNextProperty( !( ( orbValue ^ bReverse ) >= 1 ) );
149 void AxBinaryPropertyWriter::writePairProperty( AxPairData& orPairData )
151 if( startNextProperty() )
152 maLargeProps.push_back( ComplexPropVector::value_type( new PairProperty( orPairData ) ) );
155 void AxBinaryPropertyWriter::writeStringProperty( OUString& orValue, bool bCompressed )
157 sal_uInt32 nSize = orValue.getLength();
158 if ( bCompressed )
159 setFlag( nSize, AX_STRING_COMPRESSED );
160 else
161 nSize *= 2;
162 maOutStrm.writeAligned< sal_uInt32 >( nSize );
163 maLargeProps.push_back( ComplexPropVector::value_type( new StringProperty( orValue, nSize ) ) );
164 startNextProperty();
167 bool AxBinaryPropertyWriter::finalizeExport()
169 // write large properties
170 maOutStrm.align( 4 );
171 if( !maLargeProps.empty() )
173 for( ComplexPropVector::iterator aIt = maLargeProps.begin(), aEnd = maLargeProps.end(); ensureValid() && (aIt != aEnd); ++aIt )
175 (*aIt)->writeProperty( maOutStrm );
176 maOutStrm.align( 4 );
180 mnBlockSize = maOutStrm.tell() - mnPropFlagsStart;
182 // write stream properties (no stream alignment between properties!)
183 if( !maStreamProps.empty() )
184 for( ComplexPropVector::iterator aIt = maStreamProps.begin(), aEnd = maStreamProps.end(); ensureValid() && (aIt != aEnd); ++aIt )
185 (*aIt)->writeProperty( maOutStrm );
187 sal_Int64 nPos = maOutStrm.tell();
188 maOutStrm.seek( mnPropFlagsStart - sizeof( mnBlockSize ) );
190 maOutStrm << mnBlockSize;
192 if( mb64BitPropFlags )
193 maOutStrm << mnPropFlags;
194 else
195 maOutStrm << sal_uInt32( mnPropFlags );
197 maOutStrm.seek( nPos );
198 return true;
201 bool AxBinaryPropertyWriter::ensureValid( bool bCondition )
203 mbValid = mbValid && bCondition && !maOutStrm.isEof();
204 return mbValid;
207 bool AxBinaryPropertyWriter::startNextProperty( bool bSkip )
209 // if we are skipping then we clear the flag
210 setFlag( mnPropFlags, mnNextProp, !bSkip );
211 mnNextProp <<= 1;
212 return true;
215 // ============================================================================
217 } // namespace exp
218 } // namespace ole
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */