Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/package/source/zipapi/ByteGrabber.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 <ByteGrabber.hxx>
21
#include <sal/log.hxx>
22
#include <com/sun/star/io/IOException.hpp>
23
#include <com/sun/star/io/XSeekable.hpp>
24
#include <com/sun/star/io/XInputStream.hpp>
25
#include <com/sun/star/lang/IllegalArgumentException.hpp>
26
27
using namespace ::com::sun::star;
28
29
/** ByteGrabber implements the >> operators on an XOutputStream. This is
30
 *  potentially quite slow and may need to be optimised
31
 */
32
33
ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > const & xIstream)
34
21.9k
: xStream(xIstream)
35
21.9k
, xSeek (xIstream, uno::UNO_QUERY )
36
21.9k
, aSequence ( 4 )
37
21.9k
{
38
    // The input stream here could be from a remote UNO connection, in which
39
    // case it will not implement comphelper::ByteReader.
40
21.9k
    mpByteReader = dynamic_cast<comphelper::ByteReader*>(xStream.get());
41
21.9k
}
42
43
ByteGrabber::~ByteGrabber()
44
21.9k
{
45
21.9k
}
46
47
void ByteGrabber::setInputStream (const uno::Reference < io::XInputStream >& xNewStream)
48
0
{
49
0
    xStream = xNewStream;
50
0
    xSeek.set(xNewStream, uno::UNO_QUERY);
51
0
    mpByteReader = dynamic_cast<comphelper::ByteReader*>(xStream.get());
52
0
}
53
54
sal_Int32 ByteGrabber::readBytes( sal_Int8* aData,
55
                                        sal_Int32 nBytesToRead )
56
867k
{
57
867k
    if (mpByteReader)
58
867k
        return mpByteReader->readSomeBytes(aData, nBytesToRead );
59
0
    else
60
0
    {
61
0
        sal_Int32 nBytesActuallyRead = xStream->readSomeBytes(aSequence, nBytesToRead);
62
0
        memcpy(aData, aSequence.getConstArray(), nBytesActuallyRead);
63
0
        return nBytesActuallyRead;
64
0
    }
65
867k
}
66
67
// XSeekable chained...
68
void ByteGrabber::seek( sal_Int64 location )
69
620k
{
70
620k
    if (!xSeek.is() )
71
0
        throw io::IOException();
72
73
620k
    xSeek->seek( location );
74
620k
}
75
76
sal_Int64 ByteGrabber::getPosition(  )
77
857k
{
78
857k
    if (!xSeek.is() )
79
0
        throw io::IOException();
80
81
857k
    return xSeek->getPosition();
82
857k
}
83
84
sal_Int64 ByteGrabber::getLength(  )
85
21.9k
{
86
21.9k
    if (!xSeek.is() )
87
0
        throw io::IOException();
88
89
21.9k
    return xSeek->getLength();
90
21.9k
}
91
92
sal_uInt16 ByteGrabber::ReadUInt16()
93
86.4k
{
94
86.4k
    if (mpByteReader)
95
86.4k
    {
96
86.4k
        if (mpByteReader->readSomeBytes(maBuffer.data(), 2) != 2)
97
0
            return 0;
98
99
86.4k
        return static_cast <sal_uInt16>
100
86.4k
               ( (maBuffer[0] & 0xFF)
101
86.4k
                 | (maBuffer[1] & 0xFF) << 8);
102
86.4k
    }
103
0
    else
104
0
    {
105
0
        if (xStream->readBytes(aSequence, 2) != 2)
106
0
            return 0;
107
108
0
        return static_cast<sal_uInt16>((aSequence[0] & 0xFF) | (aSequence[1] & 0xFF) << 8);
109
0
    }
110
86.4k
}
111
112
sal_uInt32 ByteGrabber::ReadUInt32()
113
688k
{
114
688k
    if (mpByteReader)
115
688k
    {
116
688k
        if (mpByteReader->readSomeBytes(maBuffer.data(), 4) != 4)
117
491
            return 0;
118
119
688k
        return static_cast < sal_uInt32 >
120
688k
                ( (maBuffer[0] & 0xFF)
121
688k
              | ( maBuffer[1] & 0xFF ) << 8
122
688k
              | ( maBuffer[2] & 0xFF ) << 16
123
688k
              | ( maBuffer[3] & 0xFF ) << 24 );
124
688k
    }
125
0
    else
126
0
    {
127
0
        if (xStream->readBytes(aSequence, 4) != 4)
128
0
            return 0;
129
130
0
        return static_cast < sal_uInt32 >
131
0
                ( (aSequence[0] & 0xFF)
132
0
              | ( aSequence[1] & 0xFF ) << 8
133
0
              | ( aSequence[2] & 0xFF ) << 16
134
0
              | ( aSequence[3] & 0xFF ) << 24 );
135
0
    }
136
688k
}
137
138
sal_uInt64 ByteGrabber::ReadUInt64()
139
98
{
140
98
    if (mpByteReader)
141
98
    {
142
98
        if (mpByteReader->readSomeBytes(maBuffer.data(), 8) != 8)
143
46
            return 0;
144
145
52
        return  static_cast<sal_uInt64>(maBuffer[0] & 0xFF)
146
52
              | static_cast<sal_uInt64>(maBuffer[1] & 0xFF) << 8
147
52
              | static_cast<sal_uInt64>(maBuffer[2] & 0xFF) << 16
148
52
              | static_cast<sal_uInt64>(maBuffer[3] & 0xFF) << 24
149
52
              | static_cast<sal_uInt64>(maBuffer[4] & 0xFF) << 32
150
52
              | static_cast<sal_uInt64>(maBuffer[5] & 0xFF) << 40
151
52
              | static_cast<sal_uInt64>(maBuffer[6] & 0xFF) << 48
152
52
              | static_cast<sal_uInt64>(maBuffer[7] & 0xFF) << 56;
153
98
    }
154
0
    else
155
0
    {
156
0
        if (xStream->readBytes(aSequence, 8) != 8)
157
0
            return 0;
158
159
0
        return  static_cast<sal_uInt64>(aSequence[0] & 0xFF)
160
0
              | static_cast<sal_uInt64>(aSequence[1] & 0xFF) << 8
161
0
              | static_cast<sal_uInt64>(aSequence[2] & 0xFF) << 16
162
0
              | static_cast<sal_uInt64>(aSequence[3] & 0xFF) << 24
163
0
              | static_cast<sal_uInt64>(aSequence[4] & 0xFF) << 32
164
0
              | static_cast<sal_uInt64>(aSequence[5] & 0xFF) << 40
165
0
              | static_cast<sal_uInt64>(aSequence[6] & 0xFF) << 48
166
0
              | static_cast<sal_uInt64>(aSequence[7] & 0xFF) << 56;
167
0
    }
168
98
}
169
170
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */