Line data Source code
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_OOX_OLE_AXBINARYREADER_HXX
21 : #define INCLUDED_OOX_OLE_AXBINARYREADER_HXX
22 :
23 : #include <utility>
24 : #include <oox/helper/binaryinputstream.hxx>
25 : #include <oox/helper/refvector.hxx>
26 : #include <oox/ole/axfontdata.hxx>
27 :
28 : namespace oox {
29 : namespace ole {
30 :
31 :
32 :
33 : /** A wrapper for a binary input stream that supports aligned read operations.
34 :
35 : The implementation does not support seeking back the wrapped stream. All
36 : seeking operations (tell, seekTo, align) are performed relative to the
37 : position of the wrapped stream at construction time of this wrapper. It is
38 : possible to construct this wrapper with an unseekable input stream without
39 : losing any functionality.
40 : */
41 88 : class AxAlignedInputStream : public BinaryInputStream
42 : {
43 : public:
44 : explicit AxAlignedInputStream( BinaryInputStream& rInStrm );
45 :
46 : /** Returns the size of the data this stream represents, if the wrapped
47 : stream supports the size() operation. */
48 : virtual sal_Int64 size() const SAL_OVERRIDE;
49 : /** Return the current relative stream position (relative to position of
50 : the wrapped stream at construction time). */
51 : virtual sal_Int64 tell() const SAL_OVERRIDE;
52 : /** Seeks the stream to the passed relative position, if it is behind the
53 : current position. */
54 : virtual void seek( sal_Int64 nPos ) SAL_OVERRIDE;
55 : /** Closes the input stream but not the wrapped stream. */
56 : virtual void close() SAL_OVERRIDE;
57 :
58 : /** Reads nBytes bytes to the passed sequence.
59 : @return Number of bytes really read. */
60 : virtual sal_Int32 readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
61 : /** Reads nBytes bytes to the (existing) buffer opMem.
62 : @return Number of bytes really read. */
63 : virtual sal_Int32 readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
64 : /** Seeks the stream forward by the passed number of bytes. */
65 : virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
66 :
67 : /** Aligns the stream to a multiple of the passed size (relative to the
68 : position of the wrapped stream at construction time). */
69 : void align( size_t nSize );
70 :
71 : /** Aligns the stream according to the passed type and reads a value. */
72 : template< typename Type >
73 : SAL_WARN_UNUSED_RESULT
74 269 : Type readAligned() { align( sizeof( Type ) ); return readValue< Type >(); }
75 : /** Aligns the stream according to the passed type and skips the size of the type. */
76 : template< typename Type >
77 48 : void skipAligned() { align( sizeof( Type ) ); skip( sizeof( Type ) ); }
78 :
79 : private:
80 : BinaryInputStream* mpInStrm; ///< The wrapped input stream.
81 : sal_Int64 mnStrmPos; ///< Tracks relative position in the stream.
82 : sal_Int64 mnStrmSize; ///< Size of the wrapped stream data.
83 : };
84 :
85 :
86 :
87 : /** A pair of integer values as a property. */
88 : typedef ::std::pair< sal_Int32, sal_Int32 > AxPairData;
89 :
90 : /** An array of string values as a property. */
91 : typedef ::std::vector< OUString > AxArrayString;
92 :
93 :
94 :
95 : /** Import helper to read simple and complex ActiveX form control properties
96 : from a binary input stream. */
97 88 : class AxBinaryPropertyReader
98 : {
99 : public:
100 : explicit AxBinaryPropertyReader( BinaryInputStream& rInStrm, bool b64BitPropFlags = false );
101 :
102 : /** Reads the next integer property value from the stream, if the
103 : respective flag in the property mask is set. */
104 : template< typename StreamType, typename DataType >
105 582 : void readIntProperty( DataType& ornValue )
106 582 : { if( startNextProperty() ) ornValue = maInStrm.readAligned< StreamType >(); }
107 : /** Reads the next boolean property value from the stream, if the
108 : respective flag in the property mask is set. */
109 : void readBoolProperty( bool& orbValue, bool bReverse = false );
110 : /** Reads the next pair property from the stream, if the respective flag in
111 : the property mask is set. */
112 : void readPairProperty( AxPairData& orPairData );
113 : /** Reads the next string property from the stream, if the respective flag
114 : in the property mask is set. */
115 : void readStringProperty( OUString& orValue );
116 : /** Reads ArrayString, an array of fmString ( compressed or uncompressed )
117 : is read from the stream and inserted into rStrings */
118 : void readArrayStringProperty( std::vector< OUString >& rStrings );
119 : /** Reads the next GUID property from the stream, if the respective flag
120 : in the property mask is set. The GUID will be enclosed in braces. */
121 : void readGuidProperty( OUString& orGuid );
122 : /** Reads the next font property from the stream, if the respective flag in
123 : the property mask is set. */
124 : void readFontProperty( AxFontData& orFontData );
125 : /** Reads the next picture property from the stream, if the respective flag
126 : in the property mask is set. */
127 : void readPictureProperty( StreamDataSequence& orPicData );
128 :
129 : /** Skips the next integer property value in the stream, if the respective
130 : flag in the property mask is set. */
131 : template< typename StreamType >
132 339 : void skipIntProperty() { if( startNextProperty() ) maInStrm.skipAligned< StreamType >(); }
133 : /** Skips the next boolean property value in the stream, if the respective
134 : flag in the property mask is set. */
135 18 : void skipBoolProperty() { (void)startNextProperty(); }
136 : /** Skips the next pair property in the stream, if the respective flag in
137 : the property mask is set. */
138 : void skipPairProperty() { readPairProperty( maDummyPairData ); }
139 : /** Skips the next string property in the stream, if the respective flag in
140 : the property mask is set. */
141 6 : void skipStringProperty() { readStringProperty( maDummyString ); }
142 : /** Skips the next ArrayString property in the stream, if the respective flag in
143 : the property mask is set. */
144 0 : void skipArrayStringProperty() { readArrayStringProperty( maDummyArrayString ); }
145 : /** Skips the next GUID property in the stream, if the respective flag in
146 : the property mask is set. */
147 0 : void skipGuidProperty() { readGuidProperty( maDummyString ); }
148 : /** Skips the next font property in the stream, if the respective flag in
149 : the property mask is set. */
150 : void skipFontProperty() { readFontProperty( maDummyFontData ); }
151 : /** Skips the next picture property in the stream, if the respective flag
152 : in the property mask is set. */
153 43 : void skipPictureProperty() { readPictureProperty( maDummyPicData ); }
154 : /** Has to be called for undefined properties. If the respective flag in
155 : the mask is set, the property import cannot be finished successfully. */
156 50 : void skipUndefinedProperty() { ensureValid( !startNextProperty() ); }
157 :
158 : /** Final processing, reads contents of all complex properties. */
159 : bool finalizeImport();
160 :
161 : private:
162 : bool ensureValid( bool bCondition = true );
163 : bool startNextProperty();
164 :
165 : private:
166 : /** Base class for complex properties such as string, point, size, GUID, picture. */
167 139 : struct ComplexProperty
168 : {
169 : virtual ~ComplexProperty();
170 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) = 0;
171 : };
172 :
173 : /** Complex property for a 32-bit value pair, e.g. point or size. */
174 100 : struct PairProperty : public ComplexProperty
175 : {
176 : AxPairData& mrPairData;
177 :
178 50 : explicit PairProperty( AxPairData& rPairData ) :
179 50 : mrPairData( rPairData ) {}
180 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
181 : };
182 :
183 : /** Complex property for a string value. */
184 178 : struct StringProperty : public ComplexProperty
185 : {
186 : OUString& mrValue;
187 : sal_uInt32 mnSize;
188 :
189 89 : explicit StringProperty( OUString& rValue, sal_uInt32 nSize ) :
190 89 : mrValue( rValue ), mnSize( nSize ) {}
191 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
192 : };
193 :
194 : /** Complex property for an array of strings. */
195 0 : struct ArrayStringProperty : public ComplexProperty
196 : {
197 : AxArrayString& mrArray;
198 : sal_uInt32 mnSize;
199 0 : explicit ArrayStringProperty( AxArrayString& rArray, sal_uInt32 nSize ) :
200 0 : mrArray( rArray ), mnSize( nSize ) {}
201 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
202 : };
203 :
204 : /** Complex property for a GUID value. */
205 0 : struct GuidProperty : public ComplexProperty
206 : {
207 : OUString& mrGuid;
208 :
209 0 : explicit GuidProperty( OUString& rGuid ) :
210 0 : mrGuid( rGuid ) {}
211 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
212 : };
213 :
214 : /** Stream property for a font structure. */
215 0 : struct FontProperty : public ComplexProperty
216 : {
217 : AxFontData& mrFontData;
218 :
219 0 : explicit FontProperty( AxFontData& rFontData ) :
220 0 : mrFontData( rFontData ) {}
221 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
222 : };
223 :
224 : /** Stream property for a picture or mouse icon. */
225 0 : struct PictureProperty : public ComplexProperty
226 : {
227 : StreamDataSequence& mrPicData;
228 :
229 0 : explicit PictureProperty( StreamDataSequence& rPicData ) :
230 0 : mrPicData( rPicData ) {}
231 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
232 : };
233 :
234 : typedef RefVector< ComplexProperty > ComplexPropVector;
235 :
236 : private:
237 : AxAlignedInputStream maInStrm; ///< The input stream to read from.
238 : ComplexPropVector maLargeProps; ///< Stores info for all used large properties.
239 : ComplexPropVector maStreamProps; ///< Stores info for all used stream data properties.
240 : AxPairData maDummyPairData; ///< Dummy pair for unsupported properties.
241 : AxFontData maDummyFontData; ///< Dummy font for unsupported properties.
242 : StreamDataSequence maDummyPicData; ///< Dummy picture for unsupported properties.
243 : OUString maDummyString; ///< Dummy string for unsupported properties.
244 : AxArrayString maDummyArrayString; ///< Dummy strings for unsupported ArrayString properties.
245 : sal_Int64 mnPropFlags; ///< Flags specifying existing properties.
246 : sal_Int64 mnNextProp; ///< Next property to read.
247 : sal_Int64 mnPropsEnd; ///< End position of simple/large properties.
248 : bool mbValid; ///< True = stream still valid.
249 : };
250 :
251 :
252 :
253 : } // namespace ole
254 : } // namespace oox
255 :
256 : #endif
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|