Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sot/source/sdstor/stgole.cxx
Line
Count
Source
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
28
///////////////////////// class StgInternalStream
29
30
StgInternalStream::StgInternalStream( BaseStorage& rStg, const OUString& rName, bool bWr )
31
4.75k
{
32
4.75k
    m_isWritable = true;
33
4.75k
    StreamMode nMode = bWr
34
4.75k
                 ? StreamMode::WRITE | StreamMode::SHARE_DENYALL
35
4.75k
                 : StreamMode::READ | StreamMode::SHARE_DENYWRITE | StreamMode::NOCREATE;
36
4.75k
    m_pStrm.reset( rStg.OpenStream( rName, nMode ) );
37
38
    // set the error code right here in the stream
39
4.75k
    SetError( rStg.GetError() );
40
4.75k
    SetBufferSize( 1024 );
41
4.75k
}
42
43
StgInternalStream::~StgInternalStream()
44
4.75k
{
45
4.75k
}
46
47
std::size_t StgInternalStream::GetData(void* pData, std::size_t nSize)
48
0
{
49
0
    if( m_pStrm )
50
0
    {
51
0
        nSize = m_pStrm->Read( pData, nSize );
52
0
        SetError( m_pStrm->GetError() );
53
0
        return nSize;
54
0
    }
55
0
    else
56
0
        return 0;
57
0
}
58
59
std::size_t StgInternalStream::PutData(const void* pData, std::size_t nSize)
60
378
{
61
378
    if( m_pStrm )
62
378
    {
63
378
        nSize = m_pStrm->Write( pData, nSize );
64
378
        SetError( m_pStrm->GetError() );
65
378
        return nSize;
66
378
    }
67
0
    else
68
0
        return 0;
69
378
}
70
71
sal_uInt64 StgInternalStream::SeekPos(sal_uInt64 const nPos)
72
378
{
73
378
    return m_pStrm ? m_pStrm->Seek( nPos ) : 0;
74
378
}
75
76
void StgInternalStream::FlushData()
77
378
{
78
378
    if( m_pStrm )
79
378
    {
80
378
        m_pStrm->Flush();
81
378
        SetError( m_pStrm->GetError() );
82
378
    }
83
378
}
84
85
void StgInternalStream::Commit()
86
378
{
87
378
    Flush();
88
378
    m_pStrm->Commit();
89
378
}
90
91
///////////////////////// class StgCompObjStream
92
93
StgCompObjStream::StgCompObjStream( BaseStorage& rStg, bool bWr )
94
4.56k
    : StgInternalStream( rStg, u"\1CompObj"_ustr, bWr )
95
4.56k
{
96
4.56k
}
97
98
bool StgCompObjStream::Load()
99
4.37k
{
100
4.37k
    memset( &m_aClsId, 0, sizeof( ClsId ) );
101
4.37k
    m_nCbFormat = SotClipboardFormatId::NONE;
102
4.37k
    m_aUserName.clear();
103
4.37k
    if( GetError() != ERRCODE_NONE )
104
4.37k
        return false;
105
0
    Seek( 8 );     // skip the first part
106
0
    sal_Int32 nMarker = 0;
107
0
    ReadInt32( nMarker );
108
0
    if( nMarker == -1 )
109
0
    {
110
0
        ReadClsId( *this, m_aClsId );
111
0
        sal_Int32 nLen1 = 0;
112
0
        ReadInt32( nLen1 );
113
0
        if ( nLen1 > 0 )
114
0
        {
115
            // higher bits are ignored
116
0
            sal_Int32 nStrLen = ::std::min( nLen1, sal_Int32(0xFFFE) );
117
118
0
            std::unique_ptr<char[]> p(new char[ nStrLen+1 ]);
119
0
            p[nStrLen] = 0;
120
0
            if (ReadBytes( p.get(), nStrLen ) == o3tl::make_unsigned(nStrLen))
121
0
            {
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
0
                m_aUserName = OUString(p.get(), nStrLen, RTL_TEXTENCODING_MS_1252);
129
0
                m_nCbFormat = ReadClipboardFormat( *this );
130
0
            }
131
0
            else
132
0
                SetError( SVSTREAM_GENERALERROR );
133
0
        }
134
0
    }
135
0
    return GetError() == ERRCODE_NONE;
136
4.37k
}
137
138
bool StgCompObjStream::Store()
139
189
{
140
189
    if( GetError() != ERRCODE_NONE )
141
0
        return false;
142
189
    Seek( 0 );
143
189
    OString aAsciiUserName(OUStringToOString(m_aUserName, RTL_TEXTENCODING_MS_1252));
144
189
    WriteInt16( 1 );          // Version?
145
189
    WriteInt16( -2 );                     // 0xFFFE = Byte Order Indicator
146
189
    WriteInt32( 0x0A03 );         // Windows 3.10
147
189
    WriteInt32( -1 );
148
189
    WriteClsId( *this, m_aClsId );             // Class ID
149
189
    WriteInt32( aAsciiUserName.getLength() + 1 );
150
189
    WriteOString( aAsciiUserName );
151
189
    WriteUChar( 0 );             // string terminator
152
189
    WriteClipboardFormat( *this, m_nCbFormat );
153
189
    WriteInt32( 0 );             // terminator
154
189
    Commit();
155
189
    return GetError() == ERRCODE_NONE;
156
189
}
157
158
/////////////////////////// class StgOleStream
159
160
StgOleStream::StgOleStream( BaseStorage& rStg )
161
189
    : StgInternalStream( rStg, u"\1Ole"_ustr, true )
162
189
{
163
189
}
164
165
bool StgOleStream::Store()
166
189
{
167
189
    if( GetError() != ERRCODE_NONE )
168
0
        return false;
169
170
189
    Seek( 0 );
171
189
    WriteInt32( 0x02000001 );         // OLE version, format
172
189
    WriteInt32( 0 );             // Object flags
173
189
    WriteInt32( 0 );                  // Update Options
174
189
    WriteInt32( 0 );                  // reserved
175
189
    WriteInt32( 0 );                 // Moniker 1
176
189
    Commit();
177
189
    return GetError() == ERRCODE_NONE;
178
189
}
179
180
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */