Coverage Report

Created: 2026-06-30 11:14

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
106
{
56
106
    if( nChars <= 0 )
57
22
        return OString();
58
59
84
    ::std::vector< sal_uInt8 > aBuffer;
60
84
    sal_Int32 nCharsRead = readArray( aBuffer, nChars );
61
84
    if( nCharsRead <= 0 )
62
0
        return OString();
63
64
84
    aBuffer.resize( static_cast< size_t >( nCharsRead ) );
65
    // NUL characters are replaced by question marks.
66
84
    ::std::replace( aBuffer.begin(), aBuffer.end(), '\0', '?' );
67
68
84
    return OString(reinterpret_cast<char*>(aBuffer.data()), nCharsRead);
69
84
}
70
71
OUString BinaryInputStream::readCharArrayUC( sal_Int32 nChars, rtl_TextEncoding eTextEnc )
72
106
{
73
106
    return OStringToOUString( readCharArray( nChars ), eTextEnc );
74
106
}
75
76
OUString BinaryInputStream::readUnicodeArray( sal_Int32 nChars )
77
9.70k
{
78
9.70k
    if( nChars <= 0 )
79
9.26k
        return OUString();
80
81
440
    ::std::vector< sal_uInt16 > aBuffer;
82
440
    sal_Int32 nCharsRead = readArray( aBuffer, nChars );
83
440
    if( nCharsRead <= 0 )
84
0
        return OUString();
85
86
440
    aBuffer.resize( static_cast< size_t >( nCharsRead ) );
87
    // don't allow nul chars
88
440
    ::std::replace( aBuffer.begin(), aBuffer.begin() + nCharsRead, '\0', '?' );
89
90
440
    OUStringBuffer aStringBuffer;
91
440
    aStringBuffer.ensureCapacity( nCharsRead );
92
440
    for (auto const& elem : aBuffer)
93
46.6k
        aStringBuffer.append( static_cast< sal_Unicode >(elem) );
94
440
    return aStringBuffer.makeStringAndClear();
95
440
}
96
97
OUString BinaryInputStream::readCompressedUnicodeArray( sal_Int32 nChars, bool bCompressed )
98
32
{
99
32
    return bCompressed ?
100
         // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
101
32
        readCharArrayUC( nChars, RTL_TEXTENCODING_ISO_8859_1 ) :
102
32
        readUnicodeArray( nChars );
103
32
}
104
105
void BinaryInputStream::copyToStream( BinaryOutputStream& rOutStrm )
106
1.43k
{
107
1.43k
    sal_Int64 nBytes = SAL_MAX_INT64;
108
1.43k
    sal_Int32 nBufferSize = INPUTSTREAM_BUFFERSIZE;
109
1.43k
    StreamDataSequence aBuffer( nBufferSize );
110
2.87k
    while( nBytes > 0 )
111
1.43k
    {
112
1.43k
        sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
113
1.43k
        sal_Int32 nBytesRead = readData( aBuffer, nReadSize );
114
1.43k
        rOutStrm.writeData( aBuffer );
115
1.43k
        if( nReadSize == nBytesRead )
116
0
            nBytes -= nReadSize;
117
1.43k
        else
118
1.43k
            nBytes = 0;
119
1.43k
    }
120
1.43k
}
121
122
BinaryXInputStream::BinaryXInputStream( const Reference< XInputStream >& rxInStrm, bool bAutoClose ) :
123
150k
    BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
124
150k
    BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
125
150k
    maBuffer( INPUTSTREAM_BUFFERSIZE ),
126
150k
    mxInStrm( rxInStrm ),
127
150k
    mbAutoClose( bAutoClose && rxInStrm.is() )
128
150k
{
129
150k
    mbEof = !mxInStrm.is();
130
150k
}
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
150k
    BinaryStreamBase( Reference< XSeekable >( rxInStrm, UNO_QUERY ).is() ),
124
150k
    BinaryXSeekableStream( Reference< XSeekable >( rxInStrm, UNO_QUERY ) ),
125
150k
    maBuffer( INPUTSTREAM_BUFFERSIZE ),
126
150k
    mxInStrm( rxInStrm ),
127
150k
    mbAutoClose( bAutoClose && rxInStrm.is() )
128
150k
{
129
150k
    mbEof = !mxInStrm.is();
130
150k
}
131
132
BinaryXInputStream::~BinaryXInputStream()
133
150k
{
134
150k
    close();
135
150k
}
136
137
void BinaryXInputStream::close()
138
150k
{
139
150k
    OSL_ENSURE( !mbAutoClose || mxInStrm.is(), "BinaryXInputStream::close - invalid call" );
140
150k
    if( mxInStrm.is() ) try
141
148k
    {
142
148k
        mxInStrm->closeInput();
143
148k
    }
144
148k
    catch( Exception& )
145
148k
    {
146
53
        OSL_FAIL( "BinaryXInputStream::close - closing input stream failed" );
147
53
    }
148
150k
    mxInStrm.clear();
149
150k
    mbAutoClose = false;
150
150k
    BinaryXSeekableStream::close();
151
150k
}
152
153
sal_Int32 BinaryXInputStream::readData( StreamDataSequence& orData, sal_Int32 nBytes, size_t /*nAtomSize*/ )
154
141k
{
155
141k
    sal_Int32 nRet = 0;
156
141k
    if( !mbEof && (nBytes > 0) ) try
157
95.4k
    {
158
95.4k
        nRet = mxInStrm->readBytes( orData, nBytes );
159
95.4k
        mbEof = nRet != nBytes;
160
95.4k
    }
161
95.4k
    catch( Exception& )
162
95.4k
    {
163
9
        mbEof = true;
164
9
    }
165
141k
    return nRet;
166
141k
}
167
168
sal_Int32 BinaryXInputStream::readMemory( void* opMem, sal_Int32 nBytes, size_t nAtomSize )
169
178k
{
170
178k
    sal_Int32 nRet = 0;
171
178k
    if( !mbEof && (nBytes > 0) )
172
38.1k
    {
173
38.1k
        sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, INPUTSTREAM_BUFFERSIZE );
174
38.1k
        sal_uInt8* opnMem = static_cast< sal_uInt8* >( opMem );
175
76.3k
        while( !mbEof && (nBytes > 0) )
176
38.1k
        {
177
38.1k
            sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
178
38.1k
            sal_Int32 nBytesRead = readData( maBuffer, nReadSize, nAtomSize );
179
38.1k
            if( nBytesRead > 0 )
180
24.1k
                memcpy( opnMem, maBuffer.getConstArray(), static_cast< size_t >( nBytesRead ) );
181
38.1k
            opnMem += nBytesRead;
182
38.1k
            nBytes -= nBytesRead;
183
38.1k
            nRet += nBytesRead;
184
38.1k
        }
185
38.1k
    }
186
178k
    return nRet;
187
178k
}
188
189
void BinaryXInputStream::skip( sal_Int32 nBytes, size_t /*nAtomSize*/ )
190
34.0k
{
191
34.0k
    if( !mbEof ) try
192
33.9k
    {
193
33.9k
        mxInStrm->skipBytes( nBytes );
194
33.9k
    }
195
33.9k
    catch( Exception& )
196
33.9k
    {
197
0
        mbEof = true;
198
0
    }
199
34.0k
}
200
201
SequenceInputStream::SequenceInputStream( const StreamDataSequence& rData ) :
202
398
    BinaryStreamBase( true ),
203
398
    SequenceSeekableStream( rData )
204
398
{
205
398
}
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
398
    BinaryStreamBase( true ),
203
398
    SequenceSeekableStream( rData )
204
398
{
205
398
}
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
86
{
224
86
    sal_Int32 nReadBytes = 0;
225
86
    if( !mbEof )
226
86
    {
227
86
        nReadBytes = getMaxBytes( nBytes );
228
86
        if( nReadBytes > 0 )
229
86
            memcpy( opMem, mpData->getConstArray() + mnPos, static_cast< size_t >( nReadBytes ) );
230
86
        mnPos += nReadBytes;
231
86
        mbEof = nReadBytes < nBytes;
232
86
    }
233
86
    return nReadBytes;
234
86
}
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: */