xref: /trunk/main/oox/source/ole/olehelper.cxx (revision 31bbceb0f9d64c0c2c3b22a794a1666c1f33396e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "oox/ole/olehelper.hxx"
23 
24 #include <rtl/ustrbuf.hxx>
25 #include "oox/helper/binaryinputstream.hxx"
26 #include "oox/helper/graphichelper.hxx"
27 #include "oox/token/tokens.hxx"
28 
29 namespace oox {
30 namespace ole {
31 
32 using ::rtl::OUString;
33 using ::rtl::OUStringBuffer;
34 
35 namespace {
36 
37 const sal_uInt32 OLE_COLORTYPE_MASK         = 0xFF000000;
38 const sal_uInt32 OLE_COLORTYPE_CLIENT       = 0x00000000;
39 const sal_uInt32 OLE_COLORTYPE_PALETTE      = 0x01000000;
40 const sal_uInt32 OLE_COLORTYPE_BGR          = 0x02000000;
41 const sal_uInt32 OLE_COLORTYPE_SYSCOLOR     = 0x80000000;
42 
43 const sal_uInt32 OLE_PALETTECOLOR_MASK      = 0x0000FFFF;
44 const sal_uInt32 OLE_BGRCOLOR_MASK          = 0x00FFFFFF;
45 const sal_uInt32 OLE_SYSTEMCOLOR_MASK       = 0x0000FFFF;
46 
47 /** Swaps the red and blue component of the passed color. */
48 inline sal_uInt32 lclSwapRedBlue( sal_uInt32 nColor )
49 {
50     return static_cast< sal_uInt32 >( (nColor & 0xFF00FF00) | ((nColor & 0x0000FF) << 16) | ((nColor & 0xFF0000) >> 16) );
51 }
52 
53 /** Returns the UNO RGB color from the passed encoded OLE BGR color. */
54 inline sal_Int32 lclDecodeBgrColor( sal_uInt32 nOleColor )
55 {
56     return static_cast< sal_Int32 >( lclSwapRedBlue( nOleColor ) & 0xFFFFFF );
57 }
58 
59 // ----------------------------------------------------------------------------
60 
61 const sal_Char* const OLE_GUID_URLMONIKER   = "{79EAC9E0-BAF9-11CE-8C82-00AA004BA90B}";
62 const sal_Char* const OLE_GUID_FILEMONIKER  = "{00000303-0000-0000-C000-000000000046}";
63 
64 const sal_uInt32 OLE_STDPIC_ID              = 0x0000746C;
65 
66 const sal_uInt32 OLE_STDHLINK_VERSION       = 2;
67 const sal_uInt32 OLE_STDHLINK_HASTARGET     = 0x00000001; // Has hyperlink moniker.
68 const sal_uInt32 OLE_STDHLINK_ABSOLUTE      = 0x00000002; // Absolute path.
69 const sal_uInt32 OLE_STDHLINK_HASLOCATION   = 0x00000008; // Has target location.
70 const sal_uInt32 OLE_STDHLINK_HASDISPLAY    = 0x00000010; // Has display string.
71 const sal_uInt32 OLE_STDHLINK_HASGUID       = 0x00000020; // Has identification GUID.
72 const sal_uInt32 OLE_STDHLINK_HASTIME       = 0x00000040; // Has creation time.
73 const sal_uInt32 OLE_STDHLINK_HASFRAME      = 0x00000080; // Has frame.
74 const sal_uInt32 OLE_STDHLINK_ASSTRING      = 0x00000100; // Hyperlink as simple string.
75 
76 // ----------------------------------------------------------------------------
77 
78 template< typename Type >
79 void lclAppendHex( OUStringBuffer& orBuffer, Type nValue )
80 {
81     const sal_Int32 nWidth = 2 * sizeof( Type );
82     static const sal_Unicode spcHexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
83     orBuffer.setLength( orBuffer.getLength() + nWidth );
84     for( sal_Int32 nCharIdx = orBuffer.getLength() - 1, nCharEnd = nCharIdx - nWidth; nCharIdx > nCharEnd; --nCharIdx, nValue >>= 4 )
85         orBuffer.setCharAt( nCharIdx, spcHexChars[ nValue & 0xF ] );
86 }
87 
88 OUString lclReadStdHlinkString( BinaryInputStream& rInStrm, bool bUnicode )
89 {
90     OUString aRet;
91     sal_Int32 nChars = rInStrm.readInt32();
92     if( nChars > 0 )
93     {
94         sal_Int32 nReadChars = getLimitedValue< sal_Int32, sal_Int32 >( nChars, 0, SAL_MAX_UINT16 );
95         // byte strings are always in ANSI (Windows 1252) encoding
96         aRet = bUnicode ? rInStrm.readUnicodeArray( nReadChars, true ) : rInStrm.readCharArrayUC( nReadChars, RTL_TEXTENCODING_MS_1252, true );
97         // strings are NUL terminated, remove trailing NUL and possible other garbage
98         sal_Int32 nNulPos = aRet.indexOf( '\0' );
99         if( nNulPos >= 0 )
100             aRet = aRet.copy( 0, nNulPos );
101         // skip remaining chars
102         rInStrm.skip( (bUnicode ? 2 : 1) * (nChars - nReadChars) );
103     }
104     return aRet;
105 }
106 
107 } // namespace
108 
109 // ============================================================================
110 
111 StdFontInfo::StdFontInfo() :
112     mnHeight( 0 ),
113     mnWeight( OLE_STDFONT_NORMAL ),
114     mnCharSet( WINDOWS_CHARSET_ANSI ),
115     mnFlags( 0 )
116 {
117 }
118 
119 StdFontInfo::StdFontInfo( const ::rtl::OUString& rName, sal_uInt32 nHeight,
120         sal_uInt16 nWeight, sal_uInt16 nCharSet, sal_uInt8 nFlags ) :
121     maName( rName ),
122     mnHeight( nHeight ),
123     mnWeight( nWeight ),
124     mnCharSet( nCharSet ),
125     mnFlags( nFlags )
126 {
127 }
128 
129 // ============================================================================
130 
131 /*static*/ sal_Int32 OleHelper::decodeOleColor(
132         const GraphicHelper& rGraphicHelper, sal_uInt32 nOleColor, bool bDefaultColorBgr )
133 {
134     static const sal_Int32 spnSystemColors[] =
135     {
136         XML_scrollBar,      XML_background,     XML_activeCaption,  XML_inactiveCaption,
137         XML_menu,           XML_window,         XML_windowFrame,    XML_menuText,
138         XML_windowText,     XML_captionText,    XML_activeBorder,   XML_inactiveBorder,
139         XML_appWorkspace,   XML_highlight,      XML_highlightText,  XML_btnFace,
140         XML_btnShadow,      XML_grayText,       XML_btnText,        XML_inactiveCaptionText,
141         XML_btnHighlight,   XML_3dDkShadow,     XML_3dLight,        XML_infoText,
142         XML_infoBk
143     };
144 
145     switch( nOleColor & OLE_COLORTYPE_MASK )
146     {
147         case OLE_COLORTYPE_CLIENT:
148             return bDefaultColorBgr ? lclDecodeBgrColor( nOleColor ) : rGraphicHelper.getPaletteColor( nOleColor & OLE_PALETTECOLOR_MASK );
149 
150         case OLE_COLORTYPE_PALETTE:
151             return rGraphicHelper.getPaletteColor( nOleColor & OLE_PALETTECOLOR_MASK );
152 
153         case OLE_COLORTYPE_BGR:
154             return lclDecodeBgrColor( nOleColor );
155 
156         case OLE_COLORTYPE_SYSCOLOR:
157             return rGraphicHelper.getSystemColor( STATIC_ARRAY_SELECT( spnSystemColors, nOleColor & OLE_SYSTEMCOLOR_MASK, XML_TOKEN_INVALID ), API_RGB_WHITE );
158     }
159     OSL_ENSURE( false, "OleHelper::decodeOleColor - unknown color type" );
160     return API_RGB_BLACK;
161 }
162 
163 /*static*/ sal_uInt32 OleHelper::encodeOleColor( sal_Int32 nRgbColor )
164 {
165     return OLE_COLORTYPE_BGR | lclSwapRedBlue( static_cast< sal_uInt32 >( nRgbColor & 0xFFFFFF ) );
166 }
167 
168 /*static*/ OUString OleHelper::importGuid( BinaryInputStream& rInStrm )
169 {
170     OUStringBuffer aBuffer;
171     aBuffer.append( sal_Unicode( '{' ) );
172     lclAppendHex( aBuffer, rInStrm.readuInt32() );
173     aBuffer.append( sal_Unicode( '-' ) );
174     lclAppendHex( aBuffer, rInStrm.readuInt16() );
175     aBuffer.append( sal_Unicode( '-' ) );
176     lclAppendHex( aBuffer, rInStrm.readuInt16() );
177     aBuffer.append( sal_Unicode( '-' ) );
178     lclAppendHex( aBuffer, rInStrm.readuInt8() );
179     lclAppendHex( aBuffer, rInStrm.readuInt8() );
180     aBuffer.append( sal_Unicode( '-' ) );
181     for( int nIndex = 0; nIndex < 6; ++nIndex )
182         lclAppendHex( aBuffer, rInStrm.readuInt8() );
183     aBuffer.append( sal_Unicode( '}' ) );
184     return aBuffer.makeStringAndClear();
185 }
186 
187 /*static*/ bool OleHelper::importStdFont( StdFontInfo& orFontInfo, BinaryInputStream& rInStrm, bool bWithGuid )
188 {
189     if( bWithGuid )
190     {
191         bool bIsStdFont = importGuid( rInStrm ).equalsAscii( OLE_GUID_STDFONT );
192         OSL_ENSURE( bIsStdFont, "OleHelper::importStdFont - unexpected header GUID, expected StdFont" );
193         if( !bIsStdFont )
194             return false;
195     }
196 
197     sal_uInt8 nVersion, nNameLen;
198     rInStrm >> nVersion >> orFontInfo.mnCharSet >> orFontInfo.mnFlags >> orFontInfo.mnWeight >> orFontInfo.mnHeight >> nNameLen;
199     // according to spec the name is ASCII
200     orFontInfo.maName = rInStrm.readCharArrayUC( nNameLen, RTL_TEXTENCODING_ASCII_US );
201     OSL_ENSURE( nVersion <= 1, "OleHelper::importStdFont - wrong version" );
202     return !rInStrm.isEof() && (nVersion <= 1);
203 }
204 
205 /*static*/ bool OleHelper::importStdPic( StreamDataSequence& orGraphicData, BinaryInputStream& rInStrm, bool bWithGuid )
206 {
207     if( bWithGuid )
208     {
209         bool bIsStdPic = importGuid( rInStrm ).equalsAscii( OLE_GUID_STDPIC );
210         OSL_ENSURE( bIsStdPic, "OleHelper::importStdPic - unexpected header GUID, expected StdPic" );
211         if( !bIsStdPic )
212             return false;
213     }
214 
215     sal_uInt32 nStdPicId;
216     sal_Int32 nBytes;
217     rInStrm >> nStdPicId >> nBytes;
218     OSL_ENSURE( nStdPicId == OLE_STDPIC_ID, "OleHelper::importStdPic - unexpected header version" );
219     return !rInStrm.isEof() && (nStdPicId == OLE_STDPIC_ID) && (nBytes > 0) && (rInStrm.readData( orGraphicData, nBytes ) == nBytes);
220 }
221 
222 /*static*/ bool OleHelper::importStdHlink( StdHlinkInfo& orHlinkInfo, BinaryInputStream& rInStrm, bool bWithGuid )
223 {
224     if( bWithGuid )
225     {
226         bool bIsStdHlink = importGuid( rInStrm ).equalsAscii( OLE_GUID_STDHLINK );
227         OSL_ENSURE( bIsStdHlink, "OleHelper::importStdHlink - unexpected header GUID, expected StdHlink" );
228         if( !bIsStdHlink )
229             return false;
230     }
231 
232     sal_uInt32 nVersion, nFlags;
233     rInStrm >> nVersion >> nFlags;
234     OSL_ENSURE( nVersion == OLE_STDHLINK_VERSION, "OleHelper::importStdHlink - unexpected header version" );
235     if( rInStrm.isEof() || (nVersion != OLE_STDHLINK_VERSION) )
236         return false;
237 
238     // display string
239     if( getFlag( nFlags, OLE_STDHLINK_HASDISPLAY ) )
240         orHlinkInfo.maDisplay = lclReadStdHlinkString( rInStrm, true );
241     // frame string
242     if( getFlag( nFlags, OLE_STDHLINK_HASFRAME ) )
243         orHlinkInfo.maFrame = lclReadStdHlinkString( rInStrm, true );
244 
245     // target
246     if( getFlag( nFlags, OLE_STDHLINK_HASTARGET ) )
247     {
248         if( getFlag( nFlags, OLE_STDHLINK_ASSTRING ) )
249         {
250             OSL_ENSURE( getFlag( nFlags, OLE_STDHLINK_ABSOLUTE ), "OleHelper::importStdHlink - link not absolute" );
251             orHlinkInfo.maTarget = lclReadStdHlinkString( rInStrm, true );
252         }
253         else // hyperlink moniker
254         {
255             OUString aGuid = importGuid( rInStrm );
256             if( aGuid.equalsAscii( OLE_GUID_FILEMONIKER ) )
257             {
258                 // file name, maybe relative and with directory up-count
259                 sal_Int16 nUpLevels;
260                 rInStrm >> nUpLevels;
261                 OSL_ENSURE( (nUpLevels == 0) || !getFlag( nFlags, OLE_STDHLINK_ABSOLUTE ), "OleHelper::importStdHlink - absolute filename with upcount" );
262                 orHlinkInfo.maTarget = lclReadStdHlinkString( rInStrm, false );
263                 rInStrm.skip( 24 );
264                 sal_Int32 nBytes = rInStrm.readInt32();
265                 if( nBytes > 0 )
266                 {
267                     sal_Int64 nEndPos = rInStrm.tell() + ::std::max< sal_Int32 >( nBytes, 0 );
268                     sal_uInt16 nChars = getLimitedValue< sal_uInt16, sal_Int32 >( rInStrm.readInt32() / 2, 0, SAL_MAX_UINT16 );
269                     rInStrm.skip( 2 ); // key value
270                     orHlinkInfo.maTarget = rInStrm.readUnicodeArray( nChars ); // NOT null terminated
271                     rInStrm.seek( nEndPos );
272                 }
273                 if( !getFlag( nFlags, OLE_STDHLINK_ABSOLUTE ) )
274                     for( sal_Int16 nLevel = 0; nLevel < nUpLevels; ++nLevel )
275                         orHlinkInfo.maTarget = CREATE_OUSTRING( "../" ) + orHlinkInfo.maTarget;
276             }
277             else if( aGuid.equalsAscii( OLE_GUID_URLMONIKER ) )
278             {
279                 // URL, maybe relative and with leading '../'
280                 sal_Int32 nBytes = rInStrm.readInt32();
281                 sal_Int64 nEndPos = rInStrm.tell() + ::std::max< sal_Int32 >( nBytes, 0 );
282                 orHlinkInfo.maTarget = rInStrm.readNulUnicodeArray();
283                 rInStrm.seek( nEndPos );
284             }
285             else
286             {
287                 OSL_ENSURE( false, "OleHelper::importStdHlink - unsupported hyperlink moniker" );
288                 return false;
289             }
290         }
291     }
292 
293     // target location
294     if( getFlag( nFlags, OLE_STDHLINK_HASLOCATION ) )
295         orHlinkInfo.maLocation = lclReadStdHlinkString( rInStrm, true );
296 
297     return !rInStrm.isEof();
298 }
299 
300 } // namespace ole
301 } // namespace oox
302 
303 /* vim: set noet sw=4 ts=4: */
304