LibreOffice Module sot (master) 1
stgole.cxx
Go to the documentation of this file.
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#include <memory>
21#include <algorithm>
22
23#include "stgelem.hxx"
24#include "stgole.hxx"
25#include <o3tl/safeint.hxx>
26#include <sot/storinfo.hxx>
27
29
30StgInternalStream::StgInternalStream( BaseStorage& rStg, const OUString& rName, bool bWr )
31{
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 ) );
37
38 // set the error code right here in the stream
39 SetError( rStg.GetError() );
40 SetBufferSize( 1024 );
41}
42
44{
45}
46
47std::size_t StgInternalStream::GetData(void* pData, std::size_t nSize)
48{
49 if( m_pStrm )
50 {
51 nSize = m_pStrm->Read( pData, nSize );
52 SetError( m_pStrm->GetError() );
53 return nSize;
54 }
55 else
56 return 0;
57}
58
59std::size_t StgInternalStream::PutData(const void* pData, std::size_t nSize)
60{
61 if( m_pStrm )
62 {
63 nSize = m_pStrm->Write( pData, nSize );
64 SetError( m_pStrm->GetError() );
65 return nSize;
66 }
67 else
68 return 0;
69}
70
71sal_uInt64 StgInternalStream::SeekPos(sal_uInt64 const nPos)
72{
73 return m_pStrm ? m_pStrm->Seek( nPos ) : 0;
74}
75
77{
78 if( m_pStrm )
79 {
80 m_pStrm->Flush();
81 SetError( m_pStrm->GetError() );
82 }
83}
84
86{
87 Flush();
88 m_pStrm->Commit();
89}
90
92
94 : StgInternalStream( rStg, "\1CompObj", bWr )
95{
96}
97
99{
100 memset( &m_aClsId, 0, sizeof( ClsId ) );
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 )
109 {
110 ReadClsId( *this, m_aClsId );
111 sal_Int32 nLen1 = 0;
112 ReadInt32( nLen1 );
113 if ( nLen1 > 0 )
114 {
115 // higher bits are ignored
116 sal_Int32 nStrLen = ::std::min( nLen1, sal_Int32(0xFFFE) );
117
118 std::unique_ptr<char[]> p(new char[ nStrLen+1 ]);
119 p[nStrLen] = 0;
120 if (ReadBytes( p.get(), nStrLen ) == o3tl::make_unsigned(nStrLen))
121 {
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);
130 }
131 else
133 }
134 }
135 return GetError() == ERRCODE_NONE;
136}
137
139{
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
153 WriteInt32( 0 ); // terminator
154 Commit();
155 return GetError() == ERRCODE_NONE;
156}
157
159
161 : StgInternalStream( rStg, "\1Ole", true )
162{
163}
164
166{
167 if( GetError() != ERRCODE_NONE )
168 return false;
169
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;
178}
179
180/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual BaseStorageStream * OpenStream(const OUString &rEleName, StreamMode=StreamMode::STD_READWRITE, bool bDirect=true)=0
SotClipboardFormatId m_nCbFormat
Definition: stgole.hxx:46
StgCompObjStream(BaseStorage &, bool)
Definition: stgole.cxx:93
ClsId m_aClsId
Definition: stgole.hxx:44
OUString m_aUserName
Definition: stgole.hxx:45
StgInternalStream(BaseStorage &, const OUString &, bool)
Definition: stgole.cxx:30
std::unique_ptr< BaseStorageStream > m_pStrm
Definition: stgole.hxx:29
virtual std::size_t GetData(void *pData, std::size_t nSize) override
Definition: stgole.cxx:47
virtual sal_uInt64 SeekPos(sal_uInt64 nPos) override
Definition: stgole.cxx:71
virtual ~StgInternalStream() override
Definition: stgole.cxx:43
virtual void FlushData() override
Definition: stgole.cxx:76
virtual std::size_t PutData(const void *pData, std::size_t nSize) override
Definition: stgole.cxx:59
bool Store()
Definition: stgole.cxx:165
StgOleStream(BaseStorage &)
Definition: stgole.cxx:160
ErrCode GetError() const
Definition: stg.cxx:60
SvStream & WriteInt32(sal_Int32 nInt32)
bool m_isWritable
void SetBufferSize(sal_uInt16 m_nBufSize)
SvStream & WriteInt16(sal_Int16 nInt16)
SvStream & WriteUChar(unsigned char nChar)
SvStream & WriteOString(std::string_view rStr)
void SetError(ErrCode nErrorCode)
sal_uInt64 Seek(sal_uInt64 nPos)
void Flush()
SvStream & ReadInt32(sal_Int32 &rInt32)
std::size_t ReadBytes(void *pData, std::size_t nSize)
ErrCode GetError() const
#define SVSTREAM_GENERALERROR
#define ERRCODE_NONE
void * p
sal_uInt16 nPos
std::unique_ptr< sal_Int32[]> pData
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
SvStream & ReadClsId(SvStream &r, ClsId &rId)
Definition: stgelem.cxx:37
SvStream & WriteClsId(SvStream &r, const ClsId &rId)
Definition: stgelem.cxx:53
SotClipboardFormatId ReadClipboardFormat(SvStream &rStm)
Definition: storinfo.cxx:29
void WriteClipboardFormat(SvStream &rStm, SotClipboardFormatId nFormat)
Definition: storinfo.cxx:72
StreamMode