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 176 : 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 538 : Type readAligned() { align( sizeof( Type ) ); return readValue< Type >(); }
74 : /** Aligns the stream according to the passed type and skips the size of the type. */
75 : template< typename Type >
76 96 : void skipAligned() { align( sizeof( Type ) ); skip( sizeof( Type ) ); }
77 :
78 : private:
79 : BinaryInputStream* mpInStrm; ///< The wrapped input stream.
80 : sal_Int64 mnStrmPos; ///< Tracks relative position in the stream.
81 : sal_Int64 mnStrmSize; ///< Size of the wrapped stream data.
82 : };
83 :
84 :
85 :
86 : /** A pair of integer values as a property. */
87 : typedef ::std::pair< sal_Int32, sal_Int32 > AxPairData;
88 :
89 : /** An array of string values as a property. */
90 : typedef ::std::vector< OUString > AxArrayString;
91 :
92 :
93 :
94 : /** Import helper to read simple and complex ActiveX form control properties
95 : from a binary input stream. */
96 176 : class AxBinaryPropertyReader
97 : {
98 : public:
99 : explicit AxBinaryPropertyReader( BinaryInputStream& rInStrm, bool b64BitPropFlags = false );
100 :
101 : /** Reads the next integer property value from the stream, if the
102 : respective flag in the property mask is set. */
103 : template< typename StreamType, typename DataType >
104 1164 : void readIntProperty( DataType& ornValue )
105 1164 : { if( startNextProperty() ) ornValue = maInStrm.readAligned< StreamType >(); }
106 : /** Reads the next boolean property value from the stream, if the
107 : respective flag in the property mask is set. */
108 : void readBoolProperty( bool& orbValue, bool bReverse = false );
109 : /** Reads the next pair property from the stream, if the respective flag in
110 : the property mask is set. */
111 : void readPairProperty( AxPairData& orPairData );
112 : /** Reads the next string property from the stream, if the respective flag
113 : in the property mask is set. */
114 : void readStringProperty( OUString& orValue );
115 : /** Reads ArrayString, an array of fmString ( compressed or uncompressed )
116 : is read from the stream and inserted into rStrings */
117 : void readArrayStringProperty( std::vector< OUString >& rStrings );
118 : /** Reads the next GUID property from the stream, if the respective flag
119 : in the property mask is set. The GUID will be enclosed in braces. */
120 : void readGuidProperty( OUString& orGuid );
121 : /** Reads the next font property from the stream, if the respective flag in
122 : the property mask is set. */
123 : void readFontProperty( AxFontData& orFontData );
124 : /** Reads the next picture property from the stream, if the respective flag
125 : in the property mask is set. */
126 : void readPictureProperty( StreamDataSequence& orPicData );
127 :
128 : /** Skips the next integer property value in the stream, if the respective
129 : flag in the property mask is set. */
130 : template< typename StreamType >
131 678 : void skipIntProperty() { if( startNextProperty() ) maInStrm.skipAligned< StreamType >(); }
132 : /** Skips the next boolean property value in the stream, if the respective
133 : flag in the property mask is set. */
134 36 : void skipBoolProperty() { (void)startNextProperty(); }
135 : /** Skips the next pair property in the stream, if the respective flag in
136 : the property mask is set. */
137 : void skipPairProperty() { readPairProperty( maDummyPairData ); }
138 : /** Skips the next string property in the stream, if the respective flag in
139 : the property mask is set. */
140 12 : void skipStringProperty() { readStringProperty( maDummyString ); }
141 : /** Skips the next ArrayString property in the stream, if the respective flag in
142 : the property mask is set. */
143 0 : void skipArrayStringProperty() { readArrayStringProperty( maDummyArrayString ); }
144 : /** Skips the next GUID property in the stream, if the respective flag in
145 : the property mask is set. */
146 0 : void skipGuidProperty() { readGuidProperty( maDummyString ); }
147 : /** Skips the next font property in the stream, if the respective flag in
148 : the property mask is set. */
149 : void skipFontProperty() { readFontProperty( maDummyFontData ); }
150 : /** Skips the next picture property in the stream, if the respective flag
151 : in the property mask is set. */
152 86 : void skipPictureProperty() { readPictureProperty( maDummyPicData ); }
153 : /** Has to be called for undefined properties. If the respective flag in
154 : the mask is set, the property import cannot be finished successfully. */
155 100 : void skipUndefinedProperty() { ensureValid( !startNextProperty() ); }
156 :
157 : /** Final processing, reads contents of all complex properties. */
158 : bool finalizeImport();
159 :
160 : private:
161 : bool ensureValid( bool bCondition = true );
162 : bool startNextProperty();
163 :
164 : private:
165 : /** Base class for complex properties such as string, point, size, GUID, picture. */
166 278 : struct ComplexProperty
167 : {
168 : virtual ~ComplexProperty();
169 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) = 0;
170 : };
171 :
172 : /** Complex property for a 32-bit value pair, e.g. point or size. */
173 200 : struct PairProperty : public ComplexProperty
174 : {
175 : AxPairData& mrPairData;
176 :
177 100 : explicit PairProperty( AxPairData& rPairData ) :
178 100 : mrPairData( rPairData ) {}
179 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
180 : };
181 :
182 : /** Complex property for a string value. */
183 356 : struct StringProperty : public ComplexProperty
184 : {
185 : OUString& mrValue;
186 : sal_uInt32 mnSize;
187 :
188 178 : explicit StringProperty( OUString& rValue, sal_uInt32 nSize ) :
189 178 : mrValue( rValue ), mnSize( nSize ) {}
190 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
191 : };
192 :
193 : /** Complex property for an array of strings. */
194 0 : struct ArrayStringProperty : public ComplexProperty
195 : {
196 : AxArrayString& mrArray;
197 : sal_uInt32 mnSize;
198 0 : explicit ArrayStringProperty( AxArrayString& rArray, sal_uInt32 nSize ) :
199 0 : mrArray( rArray ), mnSize( nSize ) {}
200 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
201 : };
202 :
203 : /** Complex property for a GUID value. */
204 0 : struct GuidProperty : public ComplexProperty
205 : {
206 : OUString& mrGuid;
207 :
208 0 : explicit GuidProperty( OUString& rGuid ) :
209 0 : mrGuid( rGuid ) {}
210 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
211 : };
212 :
213 : /** Stream property for a font structure. */
214 0 : struct FontProperty : public ComplexProperty
215 : {
216 : AxFontData& mrFontData;
217 :
218 0 : explicit FontProperty( AxFontData& rFontData ) :
219 0 : mrFontData( rFontData ) {}
220 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
221 : };
222 :
223 : /** Stream property for a picture or mouse icon. */
224 0 : struct PictureProperty : public ComplexProperty
225 : {
226 : StreamDataSequence& mrPicData;
227 :
228 0 : explicit PictureProperty( StreamDataSequence& rPicData ) :
229 0 : mrPicData( rPicData ) {}
230 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) SAL_OVERRIDE;
231 : };
232 :
233 : typedef RefVector< ComplexProperty > ComplexPropVector;
234 :
235 : private:
236 : AxAlignedInputStream maInStrm; ///< The input stream to read from.
237 : ComplexPropVector maLargeProps; ///< Stores info for all used large properties.
238 : ComplexPropVector maStreamProps; ///< Stores info for all used stream data properties.
239 : AxPairData maDummyPairData; ///< Dummy pair for unsupported properties.
240 : AxFontData maDummyFontData; ///< Dummy font for unsupported properties.
241 : StreamDataSequence maDummyPicData; ///< Dummy picture for unsupported properties.
242 : OUString maDummyString; ///< Dummy string for unsupported properties.
243 : AxArrayString maDummyArrayString; ///< Dummy strings for unsupported ArrayString properties.
244 : sal_Int64 mnPropFlags; ///< Flags specifying existing properties.
245 : sal_Int64 mnNextProp; ///< Next property to read.
246 : sal_Int64 mnPropsEnd; ///< End position of simple/large properties.
247 : bool mbValid; ///< True = stream still valid.
248 : };
249 :
250 :
251 :
252 : } // namespace ole
253 : } // namespace oox
254 :
255 : #endif
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|