LibreOffice Module oox (master)  1
axbinarywriter.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
10 
11 namespace oox::ole {
12 
13 namespace {
14 
15 const sal_uInt32 AX_STRING_COMPRESSED = 0x80000000;
16 
17 } // namespace
18 
20  BinaryStreamBase( false ),
21  mpOutStrm( &rOutStrm ),
22  mnStrmPos( 0 ),
23  mnStrmSize( rOutStrm.getRemaining() ),
24  mnWrappedBeginPos( rOutStrm.tell() )
25 {
26  mbEof = mbEof || rOutStrm.isEof();
27 }
28 
29 sal_Int64 AxAlignedOutputStream::size() const
30 {
31  return mpOutStrm ? mnStrmSize : -1;
32 }
33 
34 sal_Int64 AxAlignedOutputStream::tell() const
35 {
36  return mpOutStrm ? mnStrmPos : -1;
37 }
38 
39 void AxAlignedOutputStream::seek( sal_Int64 nPos )
40 {
41  mbEof = (nPos < 0);
42  if( !mbEof )
43  {
44  mpOutStrm->seek( static_cast< sal_Int32 >( mnWrappedBeginPos + nPos ) );
46  }
47 }
48 
50 {
51  mpOutStrm = nullptr;
52  mbEof = true;
53 }
54 
55 void AxAlignedOutputStream::writeData( const StreamDataSequence& orData, size_t nAtomSize )
56 {
57  mpOutStrm->writeData( orData, nAtomSize );
59 }
60 
61 void AxAlignedOutputStream::writeMemory( const void* opMem, sal_Int32 nBytes, size_t nAtomSize )
62 {
63  mpOutStrm->writeMemory( opMem, nBytes, nAtomSize );
65 }
66 
67 void AxAlignedOutputStream::pad( sal_Int32 nBytes )
68 {
69  //PRESUMABLY we need to pad with 0's here as appropriate
70  css::uno::Sequence< sal_Int8 > aData( nBytes );
71  // ok we could be padding with rubbish here, but really that shouldn't matter
72  // set to 0(s), easier to not get fooled by 0's when looking at
73  // binary content...
74  memset( static_cast<void*>( aData.getArray() ), 0, nBytes );
75  mpOutStrm->writeData( aData );
77 }
78 
79 void AxAlignedOutputStream::align( size_t nSize )
80 {
81  pad( static_cast< sal_Int32 >( (nSize - (mnStrmPos % nSize)) % nSize ) );
82 }
83 
84 namespace {
85 
86 void lclWriteString( AxAlignedOutputStream& rOutStrm, OUString const & rValue, sal_uInt32 nSize )
87 {
88  bool bCompressed = getFlag( nSize, AX_STRING_COMPRESSED );
89  rOutStrm.writeCompressedUnicodeArray( rValue, bCompressed );
90 }
91 
92 } // namespace
93 
95 {
96 }
97 
99 {
100  rOutStrm.WriteInt32(mrPairData.first).WriteInt32(mrPairData.second);
101  return true;
102 }
103 
105 {
106  lclWriteString( rOutStrm, mrValue, mnSize );
107  return true;
108 }
109 
111  maOutStrm( rOutStrm ),
112  mnPropFlags( 0x0 ),
113  mbValid( true ),
114  mb64BitPropFlags( b64BitPropFlags )
115 {
116  sal_uInt16 nId( 0x0200 );
117  maOutStrm.WriteUInt16(nId);
118  mnBlockSize = 0; // will be filled in the finalize method
119 
120  maOutStrm.WriteUInt16(nId);
122 
123  if( mb64BitPropFlags )
125  else
127  mnNextProp = 1;
128 }
129 
131 {
132  // orbValue == bReverse false then we want to set the bit, e.g. don't skip
133  startNextProperty( !orbValue );
134 }
135 
137 {
139  maLargeProps.push_back( ComplexPropVector::value_type( std::make_shared<PairProperty>( orPairData ) ) );
140 }
141 
143 {
144  sal_uInt32 nSize = orValue.getLength() * 2;
145  setFlag( nSize, AX_STRING_COMPRESSED, false );
146  maOutStrm.writeAligned< sal_uInt32 >( nSize );
147  maLargeProps.push_back( ComplexPropVector::value_type( std::make_shared<StringProperty>( orValue, nSize ) ) );
149 }
150 
152 {
153  // write large properties
154  maOutStrm.align( 4 );
155  for (auto const& largeProp : maLargeProps)
156  {
157  if (!ensureValid())
158  break;
159  largeProp->writeProperty( maOutStrm );
160  maOutStrm.align( 4 );
161  }
162 
164 
165  // write stream properties (no stream alignment between properties!)
166  for (auto const& streamProp : maStreamProps)
167  {
168  if (!ensureValid())
169  break;
170  streamProp->writeProperty( maOutStrm );
171  }
172 
173  sal_Int64 nPos = maOutStrm.tell();
175 
177 
178  if( mb64BitPropFlags )
180  else
182 
183  maOutStrm.seek( nPos );
184 }
185 
187 {
188  mbValid = mbValid && !maOutStrm.isEof();
189  return mbValid;
190 }
191 
193 {
194  // if we are skipping then we clear the flag
195  setFlag( mnPropFlags, mnNextProp, !bSkip );
196  mnNextProp <<= 1;
197 }
198 
199 } // namespace oox::ole
200 
201 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int64 mnNextProp
Next property to read.
Base class for binary stream classes.
bool mbValid
True = stream still valid.
AxAlignedOutputStream maOutStrm
The input stream to read from.
virtual void writeData(const StreamDataSequence &orData, size_t nAtomSize=1) override
Reads nBytes bytes to the passed sequence.
virtual bool writeProperty(AxAlignedOutputStream &rOutStrm) override
sal_Int16 nId
Definition: olehelper.cxx:97
::std::pair< sal_Int32, sal_Int32 > AxPairData
A pair of integer values as a property.
virtual void writeData(const StreamDataSequence &rData, size_t nAtomSize=1)=0
Derived classes implement writing the contents of the passed data sequence.
sal_Int64 mnPropFlags
Flags specifying existing properties.
virtual void writeMemory(const void *pMem, sal_Int32 nBytes, size_t nAtomSize=1) override
Reads nBytes bytes to the (existing) buffer opMem.
Interface for binary output stream classes.
void writeBoolProperty(bool orbValue)
Write a boolean property value to the stream, the respective flag in the property mask is set...
sal_Int64 mnPropFlagsStart
pos of Prop flags
BinaryOutputStream & WriteUInt32(sal_uInt32 x)
void finalizeExport()
Final processing, write contents of all complex properties, writes record size.
void writeAligned(Type nVal)
Aligns the stream according to the passed type and reads a value.
void pad(sal_Int32 nBytes)
void writeCompressedUnicodeArray(const OUString &rString, bool bCompressed)
bool getFlag(Type nBitField, Type nMask)
Returns true, if at least one of the bits set in nMask is set in nBitField.
Definition: helper.hxx:135
virtual sal_Int64 tell() const override
Return the current relative stream position (relative to position of the wrapped stream at constructi...
A wrapper for a binary output stream that supports aligned write operations.
void writePairProperty(AxPairData &orPairData)
Write a pair property the stream, the respective flag in the property mask is set.
void setFlag(Type &ornBitField, Type nMask, bool bSet=true)
Sets or clears (according to bSet) all set bits of nMask in ornBitField.
Definition: helper.hxx:165
virtual sal_Int64 size() const override
Returns the size of the data this stream represents, if the wrapped stream supports the size() operat...
css::uno::Sequence< sal_Int8 > StreamDataSequence
AxAlignedOutputStream(BinaryOutputStream &rOutStrm)
BinaryOutputStream & WriteInt16(sal_Int16 x)
BinaryOutputStream & WriteInt32(sal_Int32 x)
container_type::value_type value_type
Definition: refvector.hxx:45
sal_Int64 mnStrmSize
Size of the wrapped stream data.
virtual void writeMemory(const void *pMem, sal_Int32 nBytes, size_t nAtomSize=1)=0
Derived classes implement writing the contents of the (preallocated!) memory buffer pMem...
sal_Int64 mnWrappedBeginPos
starting pos or wrapped stream
virtual sal_Int64 tell() const =0
Implementations return the current stream position, if possible.
void startNextProperty(bool bSkip=false)
bool mbEof
End of stream flag.
BinaryOutputStream & WriteUInt16(sal_uInt16 x)
virtual void close() override
Closes the input stream but not the wrapped stream.
void align(size_t nSize)
Aligns the stream to a multiple of the passed size (relative to the position of the wrapped stream at...
virtual bool writeProperty(AxAlignedOutputStream &rOutStrm) override
virtual void seek(sal_Int64 nPos)=0
Implementations seek the stream to the passed position, if the stream is seekable.
void writeStringProperty(OUString &orValue)
Write a string property to the stream, the respective flag in the property mask is set...
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed relative position, if it is behind the current position.
ComplexPropVector maLargeProps
Stores info for all used large properties.
BinaryOutputStream * mpOutStrm
The wrapped input stream.
sal_Int64 mnStrmPos
Tracks relative position in the stream.
ComplexPropVector maStreamProps
Stores info for all used stream data properties.
bool isEof() const
Returns true, if the stream position is invalid (EOF).
AxBinaryPropertyWriter(BinaryOutputStream &rOutStrm, bool b64BitPropFlags=false)
BinaryOutputStream & WriteInt64(sal_Int64 x)
GUIDCNamePair aData
Definition: olehelper.cxx:98
sal_uInt16 nPos
sal_uInt32 mnSize
Definition: dffdumper.cxx:163