summaryrefslogtreecommitdiff
path: root/sot/source/sdstor/stgole.cxx
blob: c91d86120867ef10fd8d3e11e034ee52464111bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <memory>
#include <algorithm>

#include <rtl/string.h>
#include "stgole.hxx"
#include <sot/storinfo.hxx>
#include <sot/exchange.hxx>

#ifdef _MSC_VER
#pragma warning(disable: 4342)
#endif
///////////////////////// class StgInternalStream

StgInternalStream::StgInternalStream( BaseStorage& rStg, const OUString& rName, bool bWr )
{
    m_isWritable = true;
    StreamMode nMode = bWr
                 ? StreamMode::WRITE | StreamMode::SHARE_DENYALL
                 : StreamMode::READ | StreamMode::SHARE_DENYWRITE | StreamMode::NOCREATE;
    m_pStrm.reset( rStg.OpenStream( rName, nMode ) );

    // set the error code right here in the stream
    SetError( rStg.GetError() );
    SetBufferSize( 1024 );
}

StgInternalStream::~StgInternalStream()
{
}

std::size_t StgInternalStream::GetData(void* pData, std::size_t nSize)
{
    if( m_pStrm )
    {
        nSize = m_pStrm->Read( pData, nSize );
        SetError( m_pStrm->GetError() );
        return nSize;
    }
    else
        return 0;
}

std::size_t StgInternalStream::PutData(const void* pData, std::size_t nSize)
{
    if( m_pStrm )
    {
        nSize = m_pStrm->Write( pData, nSize );
        SetError( m_pStrm->GetError() );
        return nSize;
    }
    else
        return 0;
}

sal_uInt64 StgInternalStream::SeekPos(sal_uInt64 const nPos)
{
    return m_pStrm ? m_pStrm->Seek( nPos ) : 0;
}

void StgInternalStream::FlushData()
{
    if( m_pStrm )
    {
        m_pStrm->Flush();
        SetError( m_pStrm->GetError() );
    }
}

void StgInternalStream::Commit()
{
    Flush();
    m_pStrm->Commit();
}

///////////////////////// class StgCompObjStream

StgCompObjStream::StgCompObjStream( BaseStorage& rStg, bool bWr )
    : StgInternalStream( rStg, "\1CompObj", bWr )
{
    memset( &m_aClsId, 0, sizeof( ClsId ) );
    m_nCbFormat = SotClipboardFormatId::NONE;
}

bool StgCompObjStream::Load()
{
    memset( &m_aClsId, 0, sizeof( ClsId ) );
    m_nCbFormat = SotClipboardFormatId::NONE;
    m_aUserName.clear();
    if( GetError() != ERRCODE_NONE )
        return false;
    Seek( 8 );     // skip the first part
    sal_Int32 nMarker = 0;
    ReadInt32( nMarker );
    if( nMarker == -1 )
    {
        ReadClsId( *this, m_aClsId );
        sal_Int32 nLen1 = 0;
        ReadInt32( nLen1 );
        if ( nLen1 > 0 )
        {
            // higher bits are ignored
            sal_uLong nStrLen = ::std::min( nLen1, (sal_Int32)0xFFFE );

            std::unique_ptr<sal_Char[]> p(new sal_Char[ nStrLen+1 ]);
            p[nStrLen] = 0;
            if (ReadBytes( p.get(), nStrLen ) == nStrLen)
            {
                //The encoding here is "ANSI", which is pretty useless seeing as
                //the actual codepage used doesn't seem to be specified/stored
                //anywhere :-(. Might as well pick 1252 and be consistent on
                //all platforms and envs
                //https://bz.apache.org/ooo/attachment.cgi?id=68668
                //for a good edge-case example
                m_aUserName = OUString(p.get(), nStrLen, RTL_TEXTENCODING_MS_1252);
                m_nCbFormat = ReadClipboardFormat( *this );
            }
            else
                SetError( SVSTREAM_GENERALERROR );
        }
    }
    return GetError() == ERRCODE_NONE;
}

bool StgCompObjStream::Store()
{
    if( GetError() != ERRCODE_NONE )
        return false;
    Seek( 0 );
    OString aAsciiUserName(OUStringToOString(m_aUserName, RTL_TEXTENCODING_MS_1252));
    WriteInt16( 1 );          // Version?
    WriteInt16( -2 );                     // 0xFFFE = Byte Order Indicator
    WriteInt32( 0x0A03 );         // Windows 3.10
    WriteInt32( -1 );
    WriteClsId( *this, m_aClsId );             // Class ID
    WriteInt32( aAsciiUserName.getLength() + 1 );
    WriteCharPtr( aAsciiUserName.getStr() );
    WriteUChar( 0 );             // string terminator
    WriteClipboardFormat( *this, m_nCbFormat );
    WriteInt32( 0 );             // terminator
    Commit();
    return GetError() == ERRCODE_NONE;
}

/////////////////////////// class StgOleStream

StgOleStream::StgOleStream( BaseStorage& rStg )
    : StgInternalStream( rStg, "\1Ole", true )
{
}

bool StgOleStream::Store()
{
    if( GetError() != ERRCODE_NONE )
        return false;

    Seek( 0 );
    WriteInt32( 0x02000001 );         // OLE version, format
    WriteInt32( 0 );             // Object flags
    WriteInt32( 0 );                  // Update Options
    WriteInt32( 0 );                  // reserved
    WriteInt32( 0 );                 // Moniker 1
    Commit();
    return GetError() == ERRCODE_NONE;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */