1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include "stgelem.hxx"
25 #include <sot/storinfo.hxx>
27 ///////////////////////// class StgInternalStream
29 StgInternalStream::StgInternalStream( BaseStorage
& rStg
, const OUString
& rName
, bool bWr
)
32 StreamMode nMode
= bWr
33 ? StreamMode::WRITE
| StreamMode::SHARE_DENYALL
34 : StreamMode::READ
| StreamMode::SHARE_DENYWRITE
| StreamMode::NOCREATE
;
35 m_pStrm
.reset( rStg
.OpenStream( rName
, nMode
) );
37 // set the error code right here in the stream
38 SetError( rStg
.GetError() );
39 SetBufferSize( 1024 );
42 StgInternalStream::~StgInternalStream()
46 std::size_t StgInternalStream::GetData(void* pData
, std::size_t nSize
)
50 nSize
= m_pStrm
->Read( pData
, nSize
);
51 SetError( m_pStrm
->GetError() );
58 std::size_t StgInternalStream::PutData(const void* pData
, std::size_t nSize
)
62 nSize
= m_pStrm
->Write( pData
, nSize
);
63 SetError( m_pStrm
->GetError() );
70 sal_uInt64
StgInternalStream::SeekPos(sal_uInt64
const nPos
)
72 return m_pStrm
? m_pStrm
->Seek( nPos
) : 0;
75 void StgInternalStream::FlushData()
80 SetError( m_pStrm
->GetError() );
84 void StgInternalStream::Commit()
90 ///////////////////////// class StgCompObjStream
92 StgCompObjStream::StgCompObjStream( BaseStorage
& rStg
, bool bWr
)
93 : StgInternalStream( rStg
, "\1CompObj", bWr
)
97 bool StgCompObjStream::Load()
99 memset( &m_aClsId
, 0, sizeof( ClsId
) );
100 m_nCbFormat
= SotClipboardFormatId::NONE
;
102 if( GetError() != ERRCODE_NONE
)
104 Seek( 8 ); // skip the first part
105 sal_Int32 nMarker
= 0;
106 ReadInt32( nMarker
);
109 ReadClsId( *this, m_aClsId
);
114 // higher bits are ignored
115 sal_uLong nStrLen
= ::std::min( nLen1
, sal_Int32(0xFFFE) );
117 std::unique_ptr
<sal_Char
[]> p(new sal_Char
[ nStrLen
+1 ]);
119 if (ReadBytes( p
.get(), nStrLen
) == nStrLen
)
121 //The encoding here is "ANSI", which is pretty useless seeing as
122 //the actual codepage used doesn't seem to be specified/stored
123 //anywhere :-(. Might as well pick 1252 and be consistent on
124 //all platforms and envs
125 //https://bz.apache.org/ooo/attachment.cgi?id=68668
126 //for a good edge-case example
127 m_aUserName
= OUString(p
.get(), nStrLen
, RTL_TEXTENCODING_MS_1252
);
128 m_nCbFormat
= ReadClipboardFormat( *this );
131 SetError( SVSTREAM_GENERALERROR
);
134 return GetError() == ERRCODE_NONE
;
137 bool StgCompObjStream::Store()
139 if( GetError() != ERRCODE_NONE
)
142 OString
aAsciiUserName(OUStringToOString(m_aUserName
, RTL_TEXTENCODING_MS_1252
));
143 WriteInt16( 1 ); // Version?
144 WriteInt16( -2 ); // 0xFFFE = Byte Order Indicator
145 WriteInt32( 0x0A03 ); // Windows 3.10
147 WriteClsId( *this, m_aClsId
); // Class ID
148 WriteInt32( aAsciiUserName
.getLength() + 1 );
149 WriteOString( aAsciiUserName
);
150 WriteUChar( 0 ); // string terminator
151 WriteClipboardFormat( *this, m_nCbFormat
);
152 WriteInt32( 0 ); // terminator
154 return GetError() == ERRCODE_NONE
;
157 /////////////////////////// class StgOleStream
159 StgOleStream::StgOleStream( BaseStorage
& rStg
)
160 : StgInternalStream( rStg
, "\1Ole", true )
164 bool StgOleStream::Store()
166 if( GetError() != ERRCODE_NONE
)
170 WriteInt32( 0x02000001 ); // OLE version, format
171 WriteInt32( 0 ); // Object flags
172 WriteInt32( 0 ); // Update Options
173 WriteInt32( 0 ); // reserved
174 WriteInt32( 0 ); // Moniker 1
176 return GetError() == ERRCODE_NONE
;
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */