Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/comphelper/source/streaming/memorystream.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 <algorithm>
21
#include <cassert>
22
23
24
#include <com/sun/star/lang/IllegalArgumentException.hpp>
25
#include <com/sun/star/io/IOException.hpp>
26
#include <comphelper/memorystream.hxx>
27
#include <cppuhelper/supportsservice.hxx>
28
#include <o3tl/safeint.hxx>
29
#include <osl/diagnose.h>
30
31
#include <string.h>
32
33
namespace com::sun::star::uno { class XComponentContext; }
34
35
using ::cppu::OWeakObject;
36
using ::cppu::WeakImplHelper;
37
using namespace ::com::sun::star::io;
38
using namespace ::com::sun::star::uno;
39
using namespace ::com::sun::star::lang;
40
41
namespace comphelper
42
{
43
44
45
UNOMemoryStream::UNOMemoryStream()
46
12.2k
: mnCursor(0)
47
12.2k
{
48
12.2k
    maData.reserve(1 * 1024 * 1024);
49
12.2k
}
50
51
// XServiceInfo
52
OUString SAL_CALL UNOMemoryStream::getImplementationName()
53
0
{
54
0
    return u"com.sun.star.comp.MemoryStream"_ustr;
55
0
}
56
57
sal_Bool SAL_CALL UNOMemoryStream::supportsService(const OUString& ServiceName)
58
0
{
59
0
    return cppu::supportsService(this, ServiceName);
60
0
}
61
62
css::uno::Sequence<OUString> SAL_CALL UNOMemoryStream::getSupportedServiceNames()
63
0
{
64
0
    return { u"com.sun.star.comp.MemoryStream"_ustr };
65
0
}
66
67
// XStream
68
Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream(  )
69
27.4k
{
70
27.4k
    return this;
71
27.4k
}
72
73
Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream(  )
74
24.1k
{
75
24.1k
    return this;
76
24.1k
}
77
78
// XInputStream
79
sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
80
28.1k
{
81
28.1k
    if( nBytesToRead < 0 )
82
0
        throw IOException(u"nBytesToRead < 0"_ustr);
83
84
28.1k
    nBytesToRead = std::min( nBytesToRead, available() );
85
28.1k
    aData.realloc( nBytesToRead );
86
87
28.1k
    if( nBytesToRead )
88
15.1k
    {
89
15.1k
        sal_Int8* pData = &(*maData.begin());
90
15.1k
        sal_Int8* pCursor = &(pData[mnCursor]);
91
15.1k
        memcpy( aData.getArray(), pCursor, nBytesToRead );
92
93
15.1k
        mnCursor += nBytesToRead;
94
15.1k
    }
95
96
28.1k
    return nBytesToRead;
97
28.1k
}
98
99
// ByteReader
100
sal_Int32 UNOMemoryStream::readSomeBytes( sal_Int8* aData, sal_Int32 nBytesToRead )
101
469
{
102
469
    if( nBytesToRead < 0 )
103
0
        throw IOException(u"nBytesToRead < 0"_ustr);
104
105
469
    nBytesToRead = std::min( nBytesToRead, available() );
106
107
469
    if( nBytesToRead )
108
464
    {
109
464
        sal_Int8* pData = &(*maData.begin());
110
464
        sal_Int8* pCursor = &(pData[mnCursor]);
111
464
        memcpy( aData, pCursor, nBytesToRead );
112
113
464
        mnCursor += nBytesToRead;
114
464
    }
115
116
469
    return nBytesToRead;
117
469
}
118
119
sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
120
22.9k
{
121
22.9k
    return readBytes( aData, nMaxBytesToRead );
122
22.9k
}
123
124
void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip )
125
0
{
126
0
    if( nBytesToSkip < 0 )
127
0
        throw IOException(u"nBytesToSkip < 0"_ustr);
128
129
0
    mnCursor += std::min( nBytesToSkip, available() );
130
0
}
131
132
sal_Int32 SAL_CALL UNOMemoryStream::available()
133
28.6k
{
134
28.6k
    return std::min<sal_Int64>( SAL_MAX_INT32, maData.size() - mnCursor);
135
28.6k
}
136
137
void SAL_CALL UNOMemoryStream::closeInput()
138
26.4k
{
139
26.4k
    mnCursor = 0;
140
26.4k
}
141
142
// XSeekable
143
void SAL_CALL UNOMemoryStream::seek( sal_Int64 location )
144
13.0k
{
145
13.0k
    if( (location < 0) || (location > SAL_MAX_INT32) )
146
1
        throw IllegalArgumentException(u"this implementation does not support more than 2GB!"_ustr, static_cast<OWeakObject*>(this), 0 );
147
148
    // seek operation should be able to resize the stream
149
13.0k
    if ( o3tl::make_unsigned(location) > maData.size() )
150
0
        maData.resize( static_cast< sal_Int32 >( location ) );
151
152
13.0k
    mnCursor = static_cast< sal_Int32 >( location );
153
13.0k
}
154
155
sal_Int64 SAL_CALL UNOMemoryStream::getPosition()
156
671
{
157
671
    return static_cast< sal_Int64 >( mnCursor );
158
671
}
159
160
sal_Int64 SAL_CALL UNOMemoryStream::getLength()
161
544
{
162
544
    return static_cast< sal_Int64 >( maData.size() );
163
544
}
164
165
// XOutputStream
166
void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData )
167
12.2k
{
168
12.2k
    writeBytes(aData.getConstArray(), aData.getLength());
169
12.2k
}
170
171
void UNOMemoryStream::writeBytes( const sal_Int8* pInData, sal_Int32 nBytesToWrite )
172
12.2k
{
173
12.2k
    assert(nBytesToWrite >= 0);
174
12.2k
    if( !nBytesToWrite )
175
0
        return;
176
177
12.2k
    sal_Int64 nNewSize = static_cast<sal_Int64>(mnCursor) + nBytesToWrite;
178
12.2k
    if( nNewSize > SAL_MAX_INT32 )
179
0
    {
180
0
        OSL_ASSERT(false);
181
0
        throw IOException(u"this implementation does not support more than 2GB!"_ustr, static_cast<OWeakObject*>(this) );
182
0
    }
183
184
12.2k
    if( o3tl::make_unsigned( nNewSize ) > maData.size() )
185
12.2k
        maData.resize( nNewSize );
186
187
12.2k
    sal_Int8* pData = &(*maData.begin());
188
12.2k
    sal_Int8* pCursor = &(pData[mnCursor]);
189
12.2k
    memcpy(pCursor, pInData, nBytesToWrite);
190
191
12.2k
    mnCursor += nBytesToWrite;
192
12.2k
}
193
194
void SAL_CALL UNOMemoryStream::flush()
195
284
{
196
284
}
197
198
void SAL_CALL UNOMemoryStream::closeOutput()
199
11.9k
{
200
11.9k
    mnCursor = 0;
201
11.9k
}
202
203
//XTruncate
204
void SAL_CALL UNOMemoryStream::truncate()
205
0
{
206
0
    maData.clear();
207
0
    mnCursor = 0;
208
0
}
209
210
} // namespace comphelper
211
212
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
213
com_sun_star_comp_MemoryStream(
214
    css::uno::XComponentContext *,
215
    css::uno::Sequence<css::uno::Any> const &)
216
0
{
217
0
    return cppu::acquire(new ::comphelper::UNOMemoryStream());
218
0
}
219
220
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */