Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/oox/helper/binarystreambase.hxx
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
#ifndef INCLUDED_OOX_HELPER_BINARYSTREAMBASE_HXX
21
#define INCLUDED_OOX_HELPER_BINARYSTREAMBASE_HXX
22
23
#include <com/sun/star/uno/Reference.hxx>
24
#include <com/sun/star/uno/Sequence.hxx>
25
#include <oox/dllapi.h>
26
#include <sal/types.h>
27
28
namespace com::sun::star {
29
    namespace io { class XSeekable; }
30
}
31
32
namespace oox {
33
34
typedef css::uno::Sequence< sal_Int8 > StreamDataSequence;
35
36
37
/** Base class for binary stream classes.
38
 */
39
class OOX_DLLPUBLIC BinaryStreamBase
40
{
41
public:
42
    virtual             ~BinaryStreamBase();
43
44
    /** Implementations return the size of the stream, if possible.
45
46
        This function may be implemented for some types of unseekable streams,
47
        and MUST be implemented for all seekable streams.
48
49
        @return
50
            The size of the stream in bytes, or -1, if not implemented.
51
     */
52
    virtual sal_Int64   size() const = 0;
53
54
    /** Implementations return the current stream position, if possible.
55
56
        This function may be implemented for some types of unseekable streams,
57
        and MUST be implemented for all seekable streams.
58
59
        @return
60
            The current position in the stream, or -1, if not implemented.
61
     */
62
    virtual sal_Int64   tell() const = 0;
63
64
    /** Implementations seek the stream to the passed position, if
65
        the stream is seekable.
66
     */
67
    virtual void        seek( sal_Int64 nPos ) = 0;
68
69
    /** Implementations close the stream.
70
     */
71
    virtual void        close() = 0;
72
73
    /** Returns true, if the implementation supports the seek() operation.
74
75
        Implementations may still implement size() and tell() even if the
76
        stream is not seekable.
77
     */
78
0
    bool         isSeekable() const { return mbSeekable; }
79
80
    /** Returns true, if the stream position is invalid (EOF). This flag turns
81
        true *after* the first attempt to seek/read beyond the stream end.
82
     */
83
32.6k
    bool         isEof() const { return mbEof; }
84
85
    /** Returns the size of the remaining data available in the stream, if
86
        stream supports size() and tell(), otherwise -1.
87
     */
88
    sal_Int64           getRemaining() const;
89
90
    /** Seeks the stream to the beginning, if stream is seekable.
91
     */
92
0
    void         seekToStart() { seek( 0 ); }
93
94
    /** Seeks the stream forward to a position that is a multiple of the passed
95
        block size, if stream is seekable.
96
97
        @param nBlockSize
98
            The size of the data blocks the streams needs to be aligned to.
99
100
        @param nAnchorPos
101
            Position in the stream the data blocks are aligned to.
102
     */
103
    void                alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos );
104
105
protected:
106
248k
    explicit            BinaryStreamBase( bool bSeekable ) : mbEof( false ), mbSeekable( bSeekable ) {}
107
108
private:
109
                        BinaryStreamBase( const BinaryStreamBase& ) = delete;
110
    BinaryStreamBase&   operator=( const BinaryStreamBase& ) = delete;
111
112
protected:
113
    bool                mbEof;          ///< End of stream flag.
114
115
private:
116
    const bool          mbSeekable;     ///< True = implementation supports seeking.
117
};
118
119
120
/** Base class for binary input and output streams wrapping a UNO stream,
121
    seekable via the com.sun.star.io.XSeekable interface.
122
 */
123
class BinaryXSeekableStream : public virtual BinaryStreamBase
124
{
125
public:
126
    virtual             ~BinaryXSeekableStream() override;
127
128
    /** Returns the size of the stream, if wrapped stream is seekable, otherwise -1. */
129
    virtual sal_Int64   size() const override;
130
    /** Returns the current stream position, if wrapped stream is seekable, otherwise -1. */
131
    virtual sal_Int64   tell() const override;
132
    /** Seeks the stream to the passed position, if wrapped stream is seekable. */
133
    virtual void        seek( sal_Int64 nPos ) override;
134
    /** Releases the reference to the UNO XSeekable interface. */
135
    virtual void        close() override;
136
137
protected:
138
    explicit            BinaryXSeekableStream(
139
                            const css::uno::Reference< css::io::XSeekable >& rxSeekable );
140
141
private:
142
    css::uno::Reference< css::io::XSeekable >
143
                        mxSeekable;     ///< Stream seeking interface.
144
};
145
146
147
/** Base class for binary input and output streams wrapping a
148
    StreamDataSequence, which is always seekable.
149
150
    The wrapped data sequence MUST live at least as long as this stream
151
    wrapper. The data sequence MUST NOT be changed from outside as long as this
152
    stream wrapper is used to modify it.
153
 */
154
class OOX_DLLPUBLIC SequenceSeekableStream : public virtual BinaryStreamBase
155
{
156
public:
157
    /** Returns the size of the wrapped data sequence. */
158
    virtual sal_Int64   size() const override;
159
    /** Returns the current stream position. */
160
    virtual sal_Int64   tell() const override;
161
    /** Seeks the stream to the passed position. */
162
    virtual void        seek( sal_Int64 nPos ) override;
163
    /** Releases the reference to the data sequence. */
164
    virtual void        close() override;
165
166
protected:
167
    explicit            SequenceSeekableStream( const StreamDataSequence& rData );
168
169
protected:
170
    const StreamDataSequence* mpData;   ///< Wrapped data sequence.
171
    sal_Int32           mnPos;          ///< Current position in the sequence.
172
};
173
174
175
} // namespace oox
176
177
#endif
178
179
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */