1046d9d1fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3046d9d1fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4046d9d1fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5046d9d1fSAndrew Rist * distributed with this work for additional information
6046d9d1fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7046d9d1fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8046d9d1fSAndrew Rist * "License"); you may not use this file except in compliance
9046d9d1fSAndrew Rist * with the License. You may obtain a copy of the License at
10046d9d1fSAndrew Rist *
11046d9d1fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12046d9d1fSAndrew Rist *
13046d9d1fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14046d9d1fSAndrew Rist * software distributed under the License is distributed on an
15046d9d1fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16046d9d1fSAndrew Rist * KIND, either express or implied. See the License for the
17046d9d1fSAndrew Rist * specific language governing permissions and limitations
18046d9d1fSAndrew Rist * under the License.
19046d9d1fSAndrew Rist *
20046d9d1fSAndrew Rist *************************************************************/
21046d9d1fSAndrew Rist
22046d9d1fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sot.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "rtl/string.h"
28cdf0e10cSrcweir #include "rtl/string.h"
29cdf0e10cSrcweir #include "stgole.hxx"
30cdf0e10cSrcweir #include "sot/storinfo.hxx" // Read/WriteClipboardFormat()
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <tools/debug.hxx>
33cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER>=1400)
34cdf0e10cSrcweir #pragma warning(disable: 4342)
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir ///////////////////////// class StgInternalStream ////////////////////////
37cdf0e10cSrcweir
StgInternalStream(BaseStorage & rStg,const String & rName,sal_Bool bWr)38cdf0e10cSrcweir StgInternalStream::StgInternalStream
39cdf0e10cSrcweir ( BaseStorage& rStg, const String& rName, sal_Bool bWr )
40cdf0e10cSrcweir {
41cdf0e10cSrcweir bIsWritable = sal_True;
42cdf0e10cSrcweir sal_uInt16 nMode = bWr
43cdf0e10cSrcweir ? STREAM_WRITE | STREAM_SHARE_DENYALL
44cdf0e10cSrcweir : STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE;
45cdf0e10cSrcweir pStrm = rStg.OpenStream( rName, nMode );
46cdf0e10cSrcweir
47cdf0e10cSrcweir // set the error code right here in the stream
48cdf0e10cSrcweir SetError( rStg.GetError() );
49cdf0e10cSrcweir SetBufferSize( 1024 );
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
~StgInternalStream()52cdf0e10cSrcweir StgInternalStream::~StgInternalStream()
53cdf0e10cSrcweir {
54cdf0e10cSrcweir delete pStrm;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
GetData(void * pData,sal_uLong nSize)57cdf0e10cSrcweir sal_uLong StgInternalStream::GetData( void* pData, sal_uLong nSize )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir if( pStrm )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir nSize = pStrm->Read( pData, nSize );
62cdf0e10cSrcweir SetError( pStrm->GetError() );
63cdf0e10cSrcweir return nSize;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir else
66cdf0e10cSrcweir return 0;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
PutData(const void * pData,sal_uLong nSize)69cdf0e10cSrcweir sal_uLong StgInternalStream::PutData( const void* pData, sal_uLong nSize )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir if( pStrm )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir nSize = pStrm->Write( pData, nSize );
74cdf0e10cSrcweir SetError( pStrm->GetError() );
75cdf0e10cSrcweir return nSize;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir else
78cdf0e10cSrcweir return 0;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
SeekPos(sal_uLong nPos)81cdf0e10cSrcweir sal_uLong StgInternalStream::SeekPos( sal_uLong nPos )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir return pStrm ? pStrm->Seek( nPos ) : 0;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
FlushData()86cdf0e10cSrcweir void StgInternalStream::FlushData()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir if( pStrm )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir pStrm->Flush();
91cdf0e10cSrcweir SetError( pStrm->GetError() );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir
Commit()95cdf0e10cSrcweir void StgInternalStream::Commit()
96cdf0e10cSrcweir {
97cdf0e10cSrcweir Flush();
98cdf0e10cSrcweir pStrm->Commit();
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
101cdf0e10cSrcweir ///////////////////////// class StgCompObjStream /////////////////////////
102cdf0e10cSrcweir
StgCompObjStream(BaseStorage & rStg,sal_Bool bWr)103cdf0e10cSrcweir StgCompObjStream::StgCompObjStream( BaseStorage& rStg, sal_Bool bWr )
104cdf0e10cSrcweir : StgInternalStream( rStg, String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "\1CompObj" ) ), bWr )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir memset( &aClsId, 0, sizeof( ClsId ) );
107cdf0e10cSrcweir nCbFormat = 0;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
Load()110cdf0e10cSrcweir sal_Bool StgCompObjStream::Load()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir memset( &aClsId, 0, sizeof( ClsId ) );
113cdf0e10cSrcweir nCbFormat = 0;
114cdf0e10cSrcweir aUserName.Erase();
115cdf0e10cSrcweir if( GetError() != SVSTREAM_OK )
116cdf0e10cSrcweir return sal_False;
117cdf0e10cSrcweir Seek( 8L ); // skip the first part
118cdf0e10cSrcweir sal_Int32 nMarker = 0;
119cdf0e10cSrcweir *this >> nMarker;
120cdf0e10cSrcweir if( nMarker == -1L )
121cdf0e10cSrcweir {
122cdf0e10cSrcweir *this >> aClsId;
123cdf0e10cSrcweir sal_Int32 nLen1 = 0;
124cdf0e10cSrcweir *this >> nLen1;
125*297a844aSArmin Le Grand if ( nLen1 > 0 )
126*297a844aSArmin Le Grand {
127*297a844aSArmin Le Grand // higher bits are ignored
128*297a844aSArmin Le Grand sal_uLong nStrLen = ::std::min( nLen1, (sal_Int32)0xFFFE );
129*297a844aSArmin Le Grand
130*297a844aSArmin Le Grand sal_Char* p = new sal_Char[ nStrLen+1 ];
131*297a844aSArmin Le Grand p[nStrLen] = 0;
132*297a844aSArmin Le Grand if( Read( p, nStrLen ) == nStrLen )
133*297a844aSArmin Le Grand {
134*297a844aSArmin Le Grand aUserName = nStrLen ? String( p, gsl_getSystemTextEncoding() ) : String();
135*297a844aSArmin Le Grand nCbFormat = ReadClipboardFormat( *this );
136*297a844aSArmin Le Grand }
137*297a844aSArmin Le Grand else
138*297a844aSArmin Le Grand SetError( SVSTREAM_GENERALERROR );
139*297a844aSArmin Le Grand delete [] p;
140*297a844aSArmin Le Grand }
141cdf0e10cSrcweir }
142cdf0e10cSrcweir return sal_Bool( GetError() == SVSTREAM_OK );
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
Store()145cdf0e10cSrcweir sal_Bool StgCompObjStream::Store()
146cdf0e10cSrcweir {
147cdf0e10cSrcweir if( GetError() != SVSTREAM_OK )
148cdf0e10cSrcweir return sal_False;
149cdf0e10cSrcweir Seek( 0L );
150cdf0e10cSrcweir ByteString aAsciiUserName( aUserName, RTL_TEXTENCODING_ASCII_US );
151cdf0e10cSrcweir *this << (sal_Int16) 1 // Version?
152cdf0e10cSrcweir << (sal_Int16) -2 // 0xFFFE = Byte Order Indicator
153cdf0e10cSrcweir << (sal_Int32) 0x0A03 // Windows 3.10
154cdf0e10cSrcweir << (sal_Int32) -1L
155cdf0e10cSrcweir << aClsId // Class ID
156cdf0e10cSrcweir << (sal_Int32) (aAsciiUserName.Len() + 1)
157cdf0e10cSrcweir << (const char *)aAsciiUserName.GetBuffer()
158cdf0e10cSrcweir << (sal_uInt8) 0; // string terminator
159cdf0e10cSrcweir /* // determine the clipboard format string
160cdf0e10cSrcweir String aCbFmt;
161cdf0e10cSrcweir if( nCbFormat > FORMAT_GDIMETAFILE )
162cdf0e10cSrcweir aCbFmt = Exchange::GetFormatName( nCbFormat );
163cdf0e10cSrcweir if( aCbFmt.Len() )
164cdf0e10cSrcweir *this << (sal_Int32) aCbFmt.Len() + 1
165cdf0e10cSrcweir << (const char*) aCbFmt
166cdf0e10cSrcweir << (sal_uInt8) 0;
167cdf0e10cSrcweir else if( nCbFormat )
168cdf0e10cSrcweir *this << (sal_Int32) -1 // for Windows
169cdf0e10cSrcweir << (sal_Int32) nCbFormat;
170cdf0e10cSrcweir else
171cdf0e10cSrcweir *this << (sal_Int32) 0; // no clipboard format
172cdf0e10cSrcweir */
173cdf0e10cSrcweir WriteClipboardFormat( *this, nCbFormat );
174cdf0e10cSrcweir *this << (sal_Int32) 0; // terminator
175cdf0e10cSrcweir Commit();
176cdf0e10cSrcweir return sal_Bool( GetError() == SVSTREAM_OK );
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir /////////////////////////// class StgOleStream ///////////////////////////
180cdf0e10cSrcweir
StgOleStream(BaseStorage & rStg,sal_Bool bWr)181cdf0e10cSrcweir StgOleStream::StgOleStream( BaseStorage& rStg, sal_Bool bWr )
182cdf0e10cSrcweir : StgInternalStream( rStg, String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "\1Ole" ) ), bWr )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir nFlags = 0;
185cdf0e10cSrcweir }
186cdf0e10cSrcweir
Load()187cdf0e10cSrcweir sal_Bool StgOleStream::Load()
188cdf0e10cSrcweir {
189cdf0e10cSrcweir nFlags = 0;
190cdf0e10cSrcweir if( GetError() != SVSTREAM_OK )
191cdf0e10cSrcweir return sal_False;
192cdf0e10cSrcweir sal_Int32 version = 0;
193cdf0e10cSrcweir Seek( 0L );
194cdf0e10cSrcweir *this >> version >> nFlags;
195cdf0e10cSrcweir return sal_Bool( GetError() == SVSTREAM_OK );
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
Store()198cdf0e10cSrcweir sal_Bool StgOleStream::Store()
199cdf0e10cSrcweir {
200cdf0e10cSrcweir if( GetError() != SVSTREAM_OK )
201cdf0e10cSrcweir return sal_False;
202cdf0e10cSrcweir Seek( 0L );
203cdf0e10cSrcweir *this << (sal_Int32) 0x02000001 // OLE version, format
204cdf0e10cSrcweir << (sal_Int32) nFlags // Object flags
205cdf0e10cSrcweir << (sal_Int32) 0 // Update Options
206cdf0e10cSrcweir << (sal_Int32) 0 // reserved
207cdf0e10cSrcweir << (sal_Int32) 0; // Moniker 1
208cdf0e10cSrcweir Commit();
209cdf0e10cSrcweir return sal_Bool( GetError() == SVSTREAM_OK );
210cdf0e10cSrcweir }
211cdf0e10cSrcweir
212