Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/helper/binaryinputstream.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 <oox/helper/binaryinputstream.hxx>
21
22
#include <com/sun/star/io/XInputStream.hpp>
23
#include <com/sun/star/io/XSeekable.hpp>
24
#include <string.h>
25
#include <algorithm>
26
#include <vector>
27
#include <rtl/ustrbuf.hxx>
28
#include <osl/diagnose.h>
29
#include <oox/helper/binaryoutputstream.hxx>
30
31
namespace oox {
32
33
using namespace ::com::sun::star::io;
34
using namespace ::com::sun::star::uno;
35
36
namespace {
37
38
const sal_Int32 INPUTSTREAM_BUFFERSIZE      = 0x8000;
39
40
} // namespace
41
42
OUString BinaryInputStream::readNulUnicodeArray()
43
0
{
44
0
    OUStringBuffer aBuffer;
45
0
    for (;;)
46
0
    {
47
0
      sal_uInt16 nChar = readuInt16();
48
0
      if ( mbEof || (nChar == 0) ) break;
49
0
      aBuffer.append( static_cast< sal_Unicode >( nChar ) );
50
0
    }
51
0
    return aBuffer.makeStringAndClear();
52
0
}
53
54
OString BinaryInputStream::readCharArray( sal_Int32 nChars )
55
99
{
56
99
    if( nChars <= 0 )
57
21
        return OString();
58
59
78
    ::std::vector< sal_uInt8 > aBuffer;
60
78
    sal_Int32 nCharsRead = readArray( aBuffer, nChars );
61
78
    if( nCharsRead <= 0 )
62
0
        return OString();
63
64
78
    aBuffer.resize( static_cast< size_t >( nCharsRead ) );
65
    // NUL characters are replaced by question marks.
66
78
    ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
67
68
78
    return OString(reinterpret_cast<char*>(aBuffer.data()), nCharsRead);
69
78
}
70
71
OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc )
72
99
{
73
99
    return OStringToOUString( readCharArray( nChars ), eTextEnc );
74
99
}
75
76
OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars )
77
8.31k
{
78
8.31k
    if( nChars <= 0 )
79
7.85k
        return OUString();
80
81
464
    ::std::vector< sal_uInt16 > aBuffer;
82
464
    sal_Int32 nCharsRead = readArray( aBuffer, nChars );
83
464
    if( nCharsRead <= 0 )
84
0
        return OUString();
85
86
464
    aBuffer.resize( static_cast< size_t >( nCharsRead ) );
87
    // don't allow nul chars
88
464
    ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
89
90
464
    OUStringBuffer aStringBuffer;
91
464
    aStringBuffer.ensureCapacity( nCharsRead );
92
464
    for (auto const& elem : aBuffer)
93
49.6k
        aStringBuffer.append( static_cast< sal_Unicode >(elem) );
94
464
    return aStringBuffer.makeStringAndClear();
95
464
}
96
97
OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed )
98
30
{
99
30
    return bCompressed ?
100
         // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
101
30
        readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1 ) :
102
30
        readUnicodeArray( nChars );
103
30
}
104
105
void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm )
106
1.51k
{
107
1.51k
    sal_Int64 nBytes = SAL_MAX_INT64;
108
1.51k
    sal_Int32 nBufferSize = INPUTSTREAM_BUFFERSIZE;
109
1.51k
    StreamDataSequence aBuffer( nBufferSize );
110
3.02k
    while( nBytes > 0 )
111
1.51k
    {
112
1.51k
        sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
113
1.51k
        sal_Int32 nBytesRead = readData( aBuffer, nReadSize );
114
1.51k
        rOutStrm.writeData( aBuffer );
115
1.51k
        if( nReadSize == nBytesRead )
116
0
            nBytes -= nReadSize;
117
1.51k
        else
118
1.51k
            nBytes = 0;
119
1.51k
    }
120
1.51k
}
121
122
BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
123
175k
    BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
124
175k
    BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
125
175k
    maBuffer( INPUTSTREAM_BUFFERSIZE ),
126
175k
    mxInStrm( rxInStrm ),
127
175k
    mbAutoClose( bAutoClose && rxInStrm.is() )
128
175k
{
129
175k
    mbEof = !mxInStrm.is();
130
175k
}
Unexecuted instantiation: oox::BinaryXInputStream::BinaryXInputStream(com::sun::star::uno::Reference<com::sun::star::io::XInputStream> const&, bool)
oox::BinaryXInputStream::BinaryXInputStream(com::sun::star::uno::Reference<com::sun::star::io::XInputStream> const&, bool)
Line
Count
Source
123
175k
    BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
124
175k
    BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
125
175k
    maBuffer( INPUTSTREAM_BUFFERSIZE ),
126
175k
    mxInStrm( rxInStrm ),
127
175k
    mbAutoClose( bAutoClose && rxInStrm.is() )
128
175k
{
129
175k
    mbEof = !mxInStrm.is();
130
175k
}
131
132
BinaryXInputStream::~BinaryXInputStream()
133
175k
{
134
175k
    close();
135
175k
}
136
137
void BinaryXInputStream::close()
138
175k
{
139
175k
    OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
140
175k
    if( mxInStrm.is() ) try
141
173k
    {
142
173k
        mxInStrm->closeInput();
143
173k
    }
144
173k
    catch( Exception& )
145
173k
    {
146
81
        OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
147
81
    }
148
175k
    mxInStrm.clear();
149
175k
    mbAutoClose = false;
150
175k
    BinaryXSeekableStream::close();
151
175k
}
152
153
sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
154
159k
{
155
159k
    sal_Int32 nRet = 0;
156
159k
    if( !mbEof && (nBytes > 0) ) try
157
115k
    {
158
115k
        nRet = mxInStrm->readBytes( orData, nBytes );
159
115k
        mbEof = nRet != nBytes;
160
115k
    }
161
115k
    catch( Exception& )
162
115k
    {
163
13
        mbEof = true;
164
13
    }
165
159k
    return nRet;
166
159k
}
167
168
sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
169
216k
{
170
216k
    sal_Int32 nRet = 0;
171
216k
    if( !mbEof && (nBytes > 0) )
172
42.5k
    {
173
42.5k
        sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
174
42.5k
        sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem );
175
85.0k
        while( !mbEof && (nBytes > 0) )
176
42.5k
        {
177
42.5k
            sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
178
42.5k
            sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
179
42.5k
            if( nBytesRead > 0 )
180
25.1k
                memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
181
42.5k
            opnMem += nBytesRead;
182
42.5k
            nBytes -= nBytesRead;
183
42.5k
            nRet += nBytesRead;
184
42.5k
        }
185
42.5k
    }
186
216k
    return nRet;
187
216k
}
188
189
void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
190
34.5k
{
191
34.5k
    if( !mbEof ) try
192
34.5k
    {
193
34.5k
        mxInStrm->skipBytes( nBytes );
194
34.5k
    }
195
34.5k
    catch( Exception& )
196
34.5k
    {
197
2
        mbEof = true;
198
2
    }
199
34.5k
}
200
201
SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
202
357
    BinaryStreamBase( true ),
203
357
    SequenceSeekableStream( rData )
204
357
{
205
357
}
Unexecuted instantiation: oox::SequenceInputStream::SequenceInputStream(com::sun::star::uno::Sequence<signed char> const&)
oox::SequenceInputStream::SequenceInputStream(com::sun::star::uno::Sequence<signed char> const&)
Line
Count
Source
202
357
    BinaryStreamBase( true ),
203
357
    SequenceSeekableStream( rData )
204
357
{
205
357
}
206
207
sal_Int32 SequenceInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
208
0
{
209
0
    sal_Int32 nReadBytes = 0;
210
0
    if( !mbEof )
211
0
    {
212
0
        nReadBytes = getMaxBytes( nBytes );
213
0
        orData.realloc( nReadBytes );
214
0
        if( nReadBytes > 0 )
215
0
            memcpy( orData.getArray(), mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
216
0
        mnPos += nReadBytes;
217
0
        mbEof = nReadBytes < nBytes;
218
0
    }
219
0
    return nReadBytes;
220
0
}
221
222
sal_Int32 SequenceInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
223
79
{
224
79
    sal_Int32 nReadBytes = 0;
225
79
    if( !mbEof )
226
79
    {
227
79
        nReadBytes = getMaxBytes( nBytes );
228
79
        if( nReadBytes > 0 )
229
79
            memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
230
79
        mnPos += nReadBytes;
231
79
        mbEof = nReadBytes < nBytes;
232
79
    }
233
79
    return nReadBytes;
234
79
}
235
236
void SequenceInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
237
0
{
238
0
    if( !mbEof )
239
0
    {
240
0
        sal_Int32 nSkipBytes = getMaxBytes( nBytes );
241
0
        mnPos += nSkipBytes;
242
0
        mbEof = nSkipBytes < nBytes;
243
0
    }
244
0
}
245
246
RelativeInputStream::RelativeInputStream( BinaryInputStream& rInStrm, sal_Int64 nSize ) :
247
0
    BinaryStreamBase( rInStrm.isSeekable() ),
248
0
    mpInStrm( &rInStrm ),
249
0
    mnStartPos( rInStrm.tell() ),
250
0
    mnRelPos( 0 )
251
0
{
252
0
    sal_Int64 nRemaining = rInStrm.getRemaining();
253
0
    mnSize = (nRemaining >= 0) ? ::std::min( nSize, nRemaining ) : nSize;
254
0
    mbEof = mbEof || rInStrm.isEof() || (mnSize < 0);
255
0
}
Unexecuted instantiation: oox::RelativeInputStream::RelativeInputStream(oox::BinaryInputStream&, long)
Unexecuted instantiation: oox::RelativeInputStream::RelativeInputStream(oox::BinaryInputStream&, long)
256
257
sal_Int64 RelativeInputStream::size() const
258
0
{
259
0
    return mpInStrm ? mnSize : -1;
260
0
}
261
262
sal_Int64 RelativeInputStream::tell() const
263
0
{
264
0
    return mpInStrm ? mnRelPos : -1;
265
0
}
266
267
void RelativeInputStream::seek( sal_Int64 nPos )
268
0
{
269
0
    if( mpInStrm && isSeekable() && (mnStartPos >= 0) )
270
0
    {
271
0
        mnRelPos = getLimitedValue< sal_Int64, sal_Int64 >( nPos, 0, mnSize );
272
0
        mpInStrm->seek( mnStartPos + mnRelPos );
273
0
        mbEof = (mnRelPos != nPos) || mpInStrm->isEof();
274
0
    }
275
0
}
276
277
void RelativeInputStream::close()
278
0
{
279
0
    mpInStrm = nullptr;
280
0
    mbEof = true;
281
0
}
282
283
sal_Int32 RelativeInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t nAtomSize )
284
0
{
285
0
    sal_Int32 nReadBytes = 0;
286
0
    if( !mbEof )
287
0
    {
288
0
        sal_Int32 nMaxBytes = getMaxBytes( nBytes );
289
0
        nReadBytes = mpInStrm->readData( orData, nMaxBytes, nAtomSize );
290
0
        mnRelPos += nReadBytes;
291
0
        mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
292
0
    }
293
0
    return nReadBytes;
294
0
}
295
296
sal_Int32 RelativeInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
297
0
{
298
0
    sal_Int32 nReadBytes = 0;
299
0
    if( !mbEof )
300
0
    {
301
0
        sal_Int32 nMaxBytes = getMaxBytes( nBytes );
302
0
        nReadBytes = mpInStrm->readMemory( opMem, nMaxBytes, nAtomSize );
303
0
        mnRelPos += nReadBytes;
304
0
        mbEof = (nMaxBytes < nBytes) || mpInStrm->isEof();
305
0
    }
306
0
    return nReadBytes;
307
0
}
308
309
void RelativeInputStream::skip( sal_Int32 nBytes, size_t nAtomSize )
310
0
{
311
0
    if( !mbEof )
312
0
    {
313
0
        sal_Int32 nSkipBytes = getMaxBytes( nBytes );
314
0
        mpInStrm->skip( nSkipBytes, nAtomSize );
315
0
        mnRelPos += nSkipBytes;
316
0
        mbEof = nSkipBytes < nBytes;
317
0
    }
318
0
}
319
320
} // namespace oox
321
322
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */