1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_OOX_OLE_AXBINARYREADER_HXX
21 #define INCLUDED_OOX_OLE_AXBINARYREADER_HXX
27 #include <oox/helper/binaryinputstream.hxx>
28 #include <oox/helper/binarystreambase.hxx>
29 #include <oox/helper/refvector.hxx>
30 #include <rtl/ustring.hxx>
31 #include <sal/types.h>
33 namespace oox
{ namespace ole
{ struct AxFontData
; } }
39 /** A wrapper for a binary input stream that supports aligned read operations.
41 The implementation does not support seeking back the wrapped stream. All
42 seeking operations (tell, seekTo, align) are performed relative to the
43 position of the wrapped stream at construction time of this wrapper. It is
44 possible to construct this wrapper with an unseekable input stream without
45 losing any functionality.
47 class AxAlignedInputStream final
: public BinaryInputStream
50 explicit AxAlignedInputStream( BinaryInputStream
& rInStrm
);
52 /** Returns the size of the data this stream represents, if the wrapped
53 stream supports the size() operation. */
54 virtual sal_Int64
size() const override
;
55 /** Return the current relative stream position (relative to position of
56 the wrapped stream at construction time). */
57 virtual sal_Int64
tell() const override
;
58 /** Seeks the stream to the passed relative position, if it is behind the
60 virtual void seek( sal_Int64 nPos
) override
;
61 /** Closes the input stream but not the wrapped stream. */
62 virtual void close() override
;
64 /** Reads nBytes bytes to the passed sequence.
65 @return Number of bytes really read. */
66 virtual sal_Int32
readData( StreamDataSequence
& orData
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
67 /** Reads nBytes bytes to the (existing) buffer opMem.
68 @return Number of bytes really read. */
69 virtual sal_Int32
readMemory( void* opMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
70 /** Seeks the stream forward by the passed number of bytes. */
71 virtual void skip( sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
73 /** Aligns the stream to a multiple of the passed size (relative to the
74 position of the wrapped stream at construction time). */
75 void align( size_t nSize
);
77 /** Aligns the stream according to the passed type and reads a value. */
78 template< typename Type
>
80 Type
readAligned() { align( sizeof( Type
) ); return readValue
< Type
>(); }
81 /** Aligns the stream according to the passed type and skips the size of the type. */
82 template< typename Type
>
83 void skipAligned() { align( sizeof( Type
) ); skip( sizeof( Type
) ); }
86 BinaryInputStream
* mpInStrm
; ///< The wrapped input stream.
87 sal_Int64 mnStrmPos
; ///< Tracks relative position in the stream.
88 sal_Int64
const mnStrmSize
; ///< Size of the wrapped stream data.
92 /** A pair of integer values as a property. */
93 typedef ::std::pair
< sal_Int32
, sal_Int32
> AxPairData
;
95 /** An array of string values as a property. */
96 typedef ::std::vector
< OUString
> AxArrayString
;
99 /** Import helper to read simple and complex ActiveX form control properties
100 from a binary input stream. */
101 class AxBinaryPropertyReader
104 explicit AxBinaryPropertyReader( BinaryInputStream
& rInStrm
, bool b64BitPropFlags
= false );
106 /** Reads the next integer property value from the stream, if the
107 respective flag in the property mask is set. */
108 template< typename StreamType
, typename DataType
>
109 void readIntProperty( DataType
& ornValue
)
110 { if( startNextProperty() ) ornValue
= maInStrm
.readAligned
< StreamType
>(); }
111 /** Reads the next boolean property value from the stream, if the
112 respective flag in the property mask is set. */
113 void readBoolProperty( bool& orbValue
, bool bReverse
= false );
114 /** Reads the next pair property from the stream, if the respective flag in
115 the property mask is set. */
116 void readPairProperty( AxPairData
& orPairData
);
117 /** Reads the next string property from the stream, if the respective flag
118 in the property mask is set. */
119 void readStringProperty( OUString
& orValue
);
120 /** Reads ArrayString, an array of fmString ( compressed or uncompressed )
121 is read from the stream and inserted into rStrings */
122 void readArrayStringProperty( std::vector
< OUString
>& rStrings
);
123 /** Reads the next GUID property from the stream, if the respective flag
124 in the property mask is set. The GUID will be enclosed in braces. */
125 void readGuidProperty( OUString
& orGuid
);
126 /** Reads the next font property from the stream, if the respective flag in
127 the property mask is set. */
128 void readFontProperty( AxFontData
& orFontData
);
129 /** Reads the next picture property from the stream, if the respective flag
130 in the property mask is set. */
131 void readPictureProperty( StreamDataSequence
& orPicData
);
133 /** Skips the next integer property value in the stream, if the respective
134 flag in the property mask is set. */
135 template< typename StreamType
>
136 void skipIntProperty() { if( startNextProperty() ) maInStrm
.skipAligned
< StreamType
>(); }
137 /** Skips the next boolean property value in the stream, if the respective
138 flag in the property mask is set. */
139 void skipBoolProperty() { (void)startNextProperty(); }
140 /** Skips the next string property in the stream, if the respective flag in
141 the property mask is set. */
142 void skipStringProperty() { readStringProperty( maDummyString
); }
143 /** Skips the next ArrayString property in the stream, if the respective flag in
144 the property mask is set. */
145 void skipArrayStringProperty() { readArrayStringProperty( maDummyArrayString
); }
146 /** Skips the next GUID property in the stream, if the respective flag in
147 the property mask is set. */
148 void skipGuidProperty() { readGuidProperty( maDummyString
); }
149 /** Skips the next picture property in the stream, if the respective flag
150 in the property mask is set. */
151 void skipPictureProperty() { readPictureProperty( maDummyPicData
); }
152 /** Has to be called for undefined properties. If the respective flag in
153 the mask is set, the property import cannot be finished successfully. */
154 void skipUndefinedProperty() { ensureValid( !startNextProperty() ); }
156 /** Final processing, reads contents of all complex properties. */
157 bool finalizeImport();
160 bool ensureValid( bool bCondition
= true );
161 bool startNextProperty();
164 /** Base class for complex properties such as string, point, size, GUID, picture. */
165 struct ComplexProperty
167 virtual ~ComplexProperty();
168 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) = 0;
171 /** Complex property for a 32-bit value pair, e.g. point or size. */
172 struct PairProperty
: public ComplexProperty
174 AxPairData
& mrPairData
;
176 explicit PairProperty( AxPairData
& rPairData
) :
177 mrPairData( rPairData
) {}
178 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
181 /** Complex property for a string value. */
182 struct StringProperty
: public ComplexProperty
185 sal_uInt32
const mnSize
;
187 explicit StringProperty( OUString
& rValue
, sal_uInt32 nSize
) :
188 mrValue( rValue
), mnSize( nSize
) {}
189 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
192 /** Complex property for an array of strings. */
193 struct ArrayStringProperty
: public ComplexProperty
195 AxArrayString
& mrArray
;
196 sal_uInt32
const mnSize
;
197 explicit ArrayStringProperty( AxArrayString
& rArray
, sal_uInt32 nSize
) :
198 mrArray( rArray
), mnSize( nSize
) {}
199 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
202 /** Complex property for a GUID value. */
203 struct GuidProperty
: public ComplexProperty
207 explicit GuidProperty( OUString
& rGuid
) :
209 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
212 /** Stream property for a font structure. */
213 struct FontProperty
: public ComplexProperty
215 AxFontData
& mrFontData
;
217 explicit FontProperty( AxFontData
& rFontData
) :
218 mrFontData( rFontData
) {}
219 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
222 /** Stream property for a picture or mouse icon. */
223 struct PictureProperty
: public ComplexProperty
225 StreamDataSequence
& mrPicData
;
227 explicit PictureProperty( StreamDataSequence
& rPicData
) :
228 mrPicData( rPicData
) {}
229 virtual bool readProperty( AxAlignedInputStream
& rInStrm
) override
;
232 typedef RefVector
< ComplexProperty
> ComplexPropVector
;
235 AxAlignedInputStream maInStrm
; ///< The input stream to read from.
236 ComplexPropVector maLargeProps
; ///< Stores info for all used large properties.
237 ComplexPropVector maStreamProps
; ///< Stores info for all used stream data properties.
238 StreamDataSequence maDummyPicData
; ///< Dummy picture for unsupported properties.
239 OUString maDummyString
; ///< Dummy string for unsupported properties.
240 AxArrayString maDummyArrayString
; ///< Dummy strings for unsupported ArrayString properties.
241 sal_Int64 mnPropFlags
; ///< Flags specifying existing properties.
242 sal_Int64 mnNextProp
; ///< Next property to read.
243 sal_Int64 mnPropsEnd
; ///< End position of simple/large properties.
244 bool mbValid
; ///< True = stream still valid.
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */