Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sot / source / sdstor / stgole.cxx
blobac23f7a145550248fc59ec114aaa15ebed27c3a4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <algorithm>
23 #include "stgelem.hxx"
24 #include "stgole.hxx"
25 #include <o3tl/safeint.hxx>
26 #include <sot/storinfo.hxx>
28 ///////////////////////// class StgInternalStream
30 StgInternalStream::StgInternalStream( BaseStorage& rStg, const OUString& rName, bool bWr )
32 m_isWritable = true;
33 StreamMode nMode = bWr
34 ? StreamMode::WRITE | StreamMode::SHARE_DENYALL
35 : StreamMode::READ | StreamMode::SHARE_DENYWRITE | StreamMode::NOCREATE;
36 m_pStrm.reset( rStg.OpenStream( rName, nMode ) );
38 // set the error code right here in the stream
39 SetError( rStg.GetError() );
40 SetBufferSize( 1024 );
43 StgInternalStream::~StgInternalStream()
47 std::size_t StgInternalStream::GetData(void* pData, std::size_t nSize)
49 if( m_pStrm )
51 nSize = m_pStrm->Read( pData, nSize );
52 SetError( m_pStrm->GetError() );
53 return nSize;
55 else
56 return 0;
59 std::size_t StgInternalStream::PutData(const void* pData, std::size_t nSize)
61 if( m_pStrm )
63 nSize = m_pStrm->Write( pData, nSize );
64 SetError( m_pStrm->GetError() );
65 return nSize;
67 else
68 return 0;
71 sal_uInt64 StgInternalStream::SeekPos(sal_uInt64 const nPos)
73 return m_pStrm ? m_pStrm->Seek( nPos ) : 0;
76 void StgInternalStream::FlushData()
78 if( m_pStrm )
80 m_pStrm->Flush();
81 SetError( m_pStrm->GetError() );
85 void StgInternalStream::Commit()
87 Flush();
88 m_pStrm->Commit();
91 ///////////////////////// class StgCompObjStream
93 StgCompObjStream::StgCompObjStream( BaseStorage& rStg, bool bWr )
94 : StgInternalStream( rStg, "\1CompObj", bWr )
98 bool StgCompObjStream::Load()
100 memset( &m_aClsId, 0, sizeof( ClsId ) );
101 m_nCbFormat = SotClipboardFormatId::NONE;
102 m_aUserName.clear();
103 if( GetError() != ERRCODE_NONE )
104 return false;
105 Seek( 8 ); // skip the first part
106 sal_Int32 nMarker = 0;
107 ReadInt32( nMarker );
108 if( nMarker == -1 )
110 ReadClsId( *this, m_aClsId );
111 sal_Int32 nLen1 = 0;
112 ReadInt32( nLen1 );
113 if ( nLen1 > 0 )
115 // higher bits are ignored
116 sal_Int32 nStrLen = ::std::min( nLen1, sal_Int32(0xFFFE) );
118 std::unique_ptr<char[]> p(new char[ nStrLen+1 ]);
119 p[nStrLen] = 0;
120 if (ReadBytes( p.get(), nStrLen ) == o3tl::make_unsigned(nStrLen))
122 //The encoding here is "ANSI", which is pretty useless seeing as
123 //the actual codepage used doesn't seem to be specified/stored
124 //anywhere :-(. Might as well pick 1252 and be consistent on
125 //all platforms and envs
126 //https://bz.apache.org/ooo/attachment.cgi?id=68668
127 //for a good edge-case example
128 m_aUserName = OUString(p.get(), nStrLen, RTL_TEXTENCODING_MS_1252);
129 m_nCbFormat = ReadClipboardFormat( *this );
131 else
132 SetError( SVSTREAM_GENERALERROR );
135 return GetError() == ERRCODE_NONE;
138 bool StgCompObjStream::Store()
140 if( GetError() != ERRCODE_NONE )
141 return false;
142 Seek( 0 );
143 OString aAsciiUserName(OUStringToOString(m_aUserName, RTL_TEXTENCODING_MS_1252));
144 WriteInt16( 1 ); // Version?
145 WriteInt16( -2 ); // 0xFFFE = Byte Order Indicator
146 WriteInt32( 0x0A03 ); // Windows 3.10
147 WriteInt32( -1 );
148 WriteClsId( *this, m_aClsId ); // Class ID
149 WriteInt32( aAsciiUserName.getLength() + 1 );
150 WriteOString( aAsciiUserName );
151 WriteUChar( 0 ); // string terminator
152 WriteClipboardFormat( *this, m_nCbFormat );
153 WriteInt32( 0 ); // terminator
154 Commit();
155 return GetError() == ERRCODE_NONE;
158 /////////////////////////// class StgOleStream
160 StgOleStream::StgOleStream( BaseStorage& rStg )
161 : StgInternalStream( rStg, "\1Ole", true )
165 bool StgOleStream::Store()
167 if( GetError() != ERRCODE_NONE )
168 return false;
170 Seek( 0 );
171 WriteInt32( 0x02000001 ); // OLE version, format
172 WriteInt32( 0 ); // Object flags
173 WriteInt32( 0 ); // Update Options
174 WriteInt32( 0 ); // reserved
175 WriteInt32( 0 ); // Moniker 1
176 Commit();
177 return GetError() == ERRCODE_NONE;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */