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 OOX_OLE_AXBINARYREADER_HXX
21 : #define 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 : loosing any functionality.
40 : */
41 8 : 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;
49 : /** Return the current relative stream position (relative to position of
50 : the wrapped stream at construction time). */
51 : virtual sal_Int64 tell() const;
52 : /** Seeks the stream to the passed relative position, if it is behind the
53 : current position. */
54 : virtual void seek( sal_Int64 nPos );
55 : /** Closes the input stream but not the wrapped stream. */
56 : virtual void close();
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 );
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 );
64 : /** Seeks the stream forward by the passed number of bytes. */
65 : virtual void skip( sal_Int32 nBytes, size_t nAtomSize = 1 );
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 an atomar value. */
72 : template< typename Type >
73 31 : inline 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 6 : inline 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< ::rtl::OUString > AxStringArray;
91 :
92 : // ============================================================================
93 :
94 : /** Import helper to read simple and complex ActiveX form control properties
95 : from a binary input stream. */
96 8 : 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 43 : inline void readIntProperty( DataType& ornValue )
105 43 : { 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( ::rtl::OUString& orValue );
115 : /** Reads the next GUID property from the stream, if the respective flag
116 : in the property mask is set. The GUID will be enclosed in braces. */
117 : void readGuidProperty( ::rtl::OUString& orGuid );
118 : /** Reads the next font property from the stream, if the respective flag in
119 : the property mask is set. */
120 : void readFontProperty( AxFontData& orFontData );
121 : /** Reads the next picture property from the stream, if the respective flag
122 : in the property mask is set. */
123 : void readPictureProperty( StreamDataSequence& orPicData );
124 :
125 : /** Skips the next integer property value in the stream, if the respective
126 : flag in the property mask is set. */
127 : template< typename StreamType >
128 27 : inline void skipIntProperty() { if( startNextProperty() ) maInStrm.skipAligned< StreamType >(); }
129 : /** Skips the next boolean property value in the stream, if the respective
130 : flag in the property mask is set. */
131 1 : inline void skipBoolProperty() { startNextProperty(); }
132 : /** Skips the next pair property in the stream, if the respective flag in
133 : the property mask is set. */
134 : void skipPairProperty() { readPairProperty( maDummyPairData ); }
135 : /** Skips the next string property in the stream, if the respective flag in
136 : the property mask is set. */
137 0 : inline void skipStringProperty() { readStringProperty( maDummyString ); }
138 : /** Skips the next GUID property in the stream, if the respective flag in
139 : the property mask is set. */
140 0 : inline void skipGuidProperty() { readGuidProperty( maDummyString ); }
141 : /** Skips the next font property in the stream, if the respective flag in
142 : the property mask is set. */
143 : inline void skipFontProperty() { readFontProperty( maDummyFontData ); }
144 : /** Skips the next picture property in the stream, if the respective flag
145 : in the property mask is set. */
146 4 : inline void skipPictureProperty() { readPictureProperty( maDummyPicData ); }
147 : /** Has to be called for undefined properties. If the respective flag in
148 : the mask is set, the property import cannot be finished successfully. */
149 2 : inline void skipUndefinedProperty() { ensureValid( !startNextProperty() ); }
150 :
151 : /** Final processing, reads contents of all complex properties. */
152 : bool finalizeImport();
153 :
154 : private:
155 : bool ensureValid( bool bCondition = true );
156 : bool startNextProperty();
157 :
158 : private:
159 : /** Base class for complex properties such as string, point, size, GUID, picture. */
160 11 : struct ComplexProperty
161 : {
162 : virtual ~ComplexProperty();
163 : virtual bool readProperty( AxAlignedInputStream& rInStrm ) = 0;
164 : };
165 :
166 : /** Complex property for a 32-bit value pair, e.g. point or size. */
167 8 : struct PairProperty : public ComplexProperty
168 : {
169 : AxPairData& mrPairData;
170 :
171 4 : inline explicit PairProperty( AxPairData& rPairData ) :
172 4 : mrPairData( rPairData ) {}
173 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
174 : };
175 :
176 : /** Complex property for a string value. */
177 14 : struct StringProperty : public ComplexProperty
178 : {
179 : ::rtl::OUString& mrValue;
180 : sal_uInt32 mnSize;
181 :
182 7 : inline explicit StringProperty( ::rtl::OUString& rValue, sal_uInt32 nSize ) :
183 7 : mrValue( rValue ), mnSize( nSize ) {}
184 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
185 : };
186 :
187 : /** Complex property for an array of strings. */
188 0 : struct StringArrayProperty : public ComplexProperty
189 : {
190 : AxStringArray& mrArray;
191 : sal_uInt32 mnSize;
192 : inline explicit StringArrayProperty( AxStringArray& rArray, sal_uInt32 nSize ) :
193 : mrArray( rArray ), mnSize( nSize ) {}
194 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
195 : };
196 :
197 : /** Complex property for a GUID value. */
198 0 : struct GuidProperty : public ComplexProperty
199 : {
200 : ::rtl::OUString& mrGuid;
201 :
202 0 : inline explicit GuidProperty( ::rtl::OUString& rGuid ) :
203 0 : mrGuid( rGuid ) {}
204 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
205 : };
206 :
207 : /** Stream property for a font structure. */
208 0 : struct FontProperty : public ComplexProperty
209 : {
210 : AxFontData& mrFontData;
211 :
212 0 : inline explicit FontProperty( AxFontData& rFontData ) :
213 0 : mrFontData( rFontData ) {}
214 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
215 : };
216 :
217 : /** Stream property for a picture or mouse icon. */
218 0 : struct PictureProperty : public ComplexProperty
219 : {
220 : StreamDataSequence& mrPicData;
221 :
222 0 : inline explicit PictureProperty( StreamDataSequence& rPicData ) :
223 0 : mrPicData( rPicData ) {}
224 : virtual bool readProperty( AxAlignedInputStream& rInStrm );
225 : };
226 :
227 : typedef RefVector< ComplexProperty > ComplexPropVector;
228 :
229 : private:
230 : AxAlignedInputStream maInStrm; ///< The input stream to read from.
231 : ComplexPropVector maLargeProps; ///< Stores info for all used large properties.
232 : ComplexPropVector maStreamProps; ///< Stores info for all used stream data properties.
233 : AxPairData maDummyPairData; ///< Dummy pair for unsupported properties.
234 : AxFontData maDummyFontData; ///< Dummy font for unsupported properties.
235 : StreamDataSequence maDummyPicData; ///< Dummy picture for unsupported properties.
236 : ::rtl::OUString maDummyString; ///< Dummy string for unsupported properties.
237 : AxStringArray maDummyStringArray; ///< Dummy string array for unsupported properties.
238 : sal_Int64 mnPropFlags; ///< Flags specifying existing properties.
239 : sal_Int64 mnNextProp; ///< Next property to read.
240 : sal_Int64 mnPropsEnd; ///< End position of simple/large properties.
241 : bool mbValid; ///< True = stream still valid.
242 : };
243 :
244 : // ============================================================================
245 :
246 : } // namespace ole
247 : } // namespace oox
248 :
249 : #endif
250 :
251 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|