Coverage Report

Created: 2026-11-12 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/MDL/HalfLife/HL1DataBuffer.h
Line
Count
Source
1
/*
2
---------------------------------------------------------------------------
3
Open Asset Import Library (assimp)
4
---------------------------------------------------------------------------
5
6
Copyright (c) 2006-2026, assimp team
7
8
All rights reserved.
9
10
Redistribution and use of this software in source and binary forms,
11
with or without modification, are permitted provided that the following
12
conditions are met:
13
14
* Redistributions of source code must retain the above
15
copyright notice, this list of conditions and the
16
following disclaimer.
17
18
* Redistributions in binary form must reproduce the above
19
copyright notice, this list of conditions and the
20
following disclaimer in the documentation and/or other
21
materials provided with the distribution.
22
23
* Neither the name of the assimp team, nor the names of its
24
contributors may be used to endorse or promote products
25
derived from this software without specific prior
26
written permission of the assimp team.
27
28
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
---------------------------------------------------------------------------
40
*/
41
42
/** @file HL1DataBuffer.h
43
 *  @brief Declaration of the Half-Life 1 data buffer.
44
 */
45
46
#ifndef AI_HL1DATABUFFER_INCLUDED
47
#define AI_HL1DATABUFFER_INCLUDED
48
49
#include <assimp/Exceptional.h>
50
#include <assimp/defs.h>
51
#include <cstddef>
52
#include <cstdint>
53
#include <memory>
54
55
namespace Assimp {
56
namespace MDL {
57
namespace HalfLife {
58
59
/**
60
 * \brief Lightweight byte buffer wrapper for HL1 binary parsing.
61
 *
62
 * Acts as either:
63
 *  - a non-owning view into external memory, or
64
 *  - an owning buffer backed by a unique_ptr.
65
 *
66
 * Copy is disabled to avoid accidental double-ownership; move is supported.
67
 */
68
class HL1DataBuffer {
69
public:
70
    /** \brief Construct an empty buffer (null view). */
71
9.90k
    HL1DataBuffer() AI_NO_EXCEPT : data_(nullptr),
72
9.90k
                                   length_(0),
73
9.90k
                                   owner_(nullptr) {}
74
75
    /** \brief Non-copyable (buffer may own memory). */
76
    HL1DataBuffer(const HL1DataBuffer &) = delete;
77
78
    /** \brief Non-copyable (buffer may own memory). */
79
    HL1DataBuffer &operator=(const HL1DataBuffer &) = delete;
80
81
    /** \brief Move-construct, transferring ownership/view state. */
82
0
    HL1DataBuffer(HL1DataBuffer &&other) AI_NO_EXCEPT : data_(other.data_),
83
0
                                                        length_(other.length_),
84
0
                                                        owner_(std::move(other.owner_)) {
85
0
        other.data_ = nullptr;
86
0
        other.length_ = 0;
87
0
        other.owner_ = nullptr;
88
0
    }
89
90
    /** \brief Move-assign, transferring ownership/view state. */
91
4.27k
    HL1DataBuffer &operator=(HL1DataBuffer &&other) AI_NO_EXCEPT {
92
4.27k
        if (this != &other) {
93
4.27k
            data_ = other.data_;
94
4.27k
            length_ = other.length_;
95
4.27k
            owner_ = std::move(other.owner_);
96
97
4.27k
            other.data_ = nullptr;
98
4.27k
            other.length_ = 0;
99
4.27k
            other.owner_ = nullptr;
100
4.27k
        }
101
102
4.27k
        return *this;
103
4.27k
    }
104
105
    /**
106
     * \brief Create a non-owning view into external bytes.
107
     *
108
     * \param[in] data Pointer to raw bytes (must outlive the view).
109
     * \param[in] length Size in bytes.
110
     */
111
1.12k
    static HL1DataBuffer view(const unsigned char *data, size_t length) {
112
1.12k
        HL1DataBuffer b;
113
1.12k
        b.data_ = data;
114
1.12k
        b.length_ = length;
115
1.12k
        b.owner_ = nullptr;
116
1.12k
        return b;
117
1.12k
    }
118
119
    /**
120
     * \brief Create a non-owning view of another buffer.
121
     *
122
     * \param[in] other Source buffer.
123
     */
124
1.04k
    static HL1DataBuffer view(const HL1DataBuffer &other) {
125
1.04k
        HL1DataBuffer b;
126
1.04k
        b.data_ = other.data_;
127
1.04k
        b.length_ = other.length_;
128
1.04k
        b.owner_ = nullptr;
129
1.04k
        return b;
130
1.04k
    }
131
132
    /**
133
     * \brief Create an owning buffer by taking ownership of allocated storage.
134
     *
135
     * \param[in] buffer Unique buffer storage.
136
     * \param[in] length Size in bytes.
137
     */
138
3.23k
    static HL1DataBuffer owning(std::unique_ptr<unsigned char[]> buffer, size_t length) {
139
3.23k
        HL1DataBuffer b;
140
3.23k
        b.data_ = buffer.get();
141
3.23k
        b.length_ = length;
142
3.23k
        b.owner_ = std::move(buffer);
143
3.23k
        return b;
144
3.23k
    }
145
146
    /**
147
     * \brief Return a typed pointer into the buffer with bounds checks.
148
     *
149
     * \param[in] offset Byte offset from the start of the buffer.
150
     * \param[in] elements Number of elements to access.
151
     * \return Pointer to the requested typed data inside the buffer.
152
     * \throws DeadlyImportError if the request is out of range.
153
     */
154
    template <typename DataType>
155
101k
    const DataType *get_data(int offset, int elements) const {
156
101k
        if (offset < 0 || elements < 0) {
157
34
            throw DeadlyImportError("MDL file contains invalid data");
158
34
        }
159
160
101k
        const size_t uoffset = static_cast<size_t>(offset);
161
101k
        const size_t uelements = static_cast<size_t>(elements);
162
163
101k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
105
            throw DeadlyImportError("MDL file contains invalid data");
165
105
        }
166
167
101k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
101k
    }
Assimp::MDL::HalfLife::Header_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Header_HL1>(int, int) const
Line
Count
Source
155
2.17k
    const DataType *get_data(int offset, int elements) const {
156
2.17k
        if (offset < 0 || elements < 0) {
157
0
            throw DeadlyImportError("MDL file contains invalid data");
158
0
        }
159
160
2.17k
        const size_t uoffset = static_cast<size_t>(offset);
161
2.17k
        const size_t uelements = static_cast<size_t>(elements);
162
163
2.17k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
0
            throw DeadlyImportError("MDL file contains invalid data");
165
0
        }
166
167
2.17k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
2.17k
    }
Assimp::MDL::HalfLife::SequenceHeader_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::SequenceHeader_HL1>(int, int) const
Line
Count
Source
155
3.22k
    const DataType *get_data(int offset, int elements) const {
156
3.22k
        if (offset < 0 || elements < 0) {
157
0
            throw DeadlyImportError("MDL file contains invalid data");
158
0
        }
159
160
3.22k
        const size_t uoffset = static_cast<size_t>(offset);
161
3.22k
        const size_t uelements = static_cast<size_t>(elements);
162
163
3.22k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
0
            throw DeadlyImportError("MDL file contains invalid data");
165
0
        }
166
167
3.22k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
3.22k
    }
Assimp::MDL::HalfLife::Texture_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Texture_HL1>(int, int) const
Line
Count
Source
155
1.85k
    const DataType *get_data(int offset, int elements) const {
156
1.85k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
1.85k
        const size_t uoffset = static_cast<size_t>(offset);
161
1.85k
        const size_t uelements = static_cast<size_t>(elements);
162
163
1.85k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
11
            throw DeadlyImportError("MDL file contains invalid data");
165
11
        }
166
167
1.83k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
1.85k
    }
unsigned char const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<unsigned char>(int, int) const
Line
Count
Source
155
9.11k
    const DataType *get_data(int offset, int elements) const {
156
9.11k
        if (offset < 0 || elements < 0) {
157
4
            throw DeadlyImportError("MDL file contains invalid data");
158
4
        }
159
160
9.11k
        const size_t uoffset = static_cast<size_t>(offset);
161
9.11k
        const size_t uelements = static_cast<size_t>(elements);
162
163
9.11k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
33
            throw DeadlyImportError("MDL file contains invalid data");
165
33
        }
166
167
9.08k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
9.11k
    }
short const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<short>(int, int) const
Line
Count
Source
155
12.2k
    const DataType *get_data(int offset, int elements) const {
156
12.2k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
12.2k
        const size_t uoffset = static_cast<size_t>(offset);
161
12.2k
        const size_t uelements = static_cast<size_t>(elements);
162
163
12.2k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
12.2k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
12.2k
    }
Assimp::MDL::HalfLife::Bone_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Bone_HL1>(int, int) const
Line
Count
Source
155
2.08k
    const DataType *get_data(int offset, int elements) const {
156
2.08k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
2.08k
        const size_t uoffset = static_cast<size_t>(offset);
161
2.08k
        const size_t uelements = static_cast<size_t>(elements);
162
163
2.08k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
5
            throw DeadlyImportError("MDL file contains invalid data");
165
5
        }
166
167
2.07k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
2.08k
    }
Assimp::MDL::HalfLife::Bodypart_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Bodypart_HL1>(int, int) const
Line
Count
Source
155
2.37k
    const DataType *get_data(int offset, int elements) const {
156
2.37k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
2.37k
        const size_t uoffset = static_cast<size_t>(offset);
161
2.37k
        const size_t uelements = static_cast<size_t>(elements);
162
163
2.37k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
2.37k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
2.37k
    }
Assimp::MDL::HalfLife::Model_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Model_HL1>(int, int) const
Line
Count
Source
155
4.00k
    const DataType *get_data(int offset, int elements) const {
156
4.00k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
4.00k
        const size_t uoffset = static_cast<size_t>(offset);
161
4.00k
        const size_t uelements = static_cast<size_t>(elements);
162
163
4.00k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
5
            throw DeadlyImportError("MDL file contains invalid data");
165
5
        }
166
167
4.00k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
4.00k
    }
Assimp::MDL::HalfLife::Mesh_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Mesh_HL1>(int, int) const
Line
Count
Source
155
1.53k
    const DataType *get_data(int offset, int elements) const {
156
1.53k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
1.53k
        const size_t uoffset = static_cast<size_t>(offset);
161
1.53k
        const size_t uelements = static_cast<size_t>(elements);
162
163
1.53k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
8
            throw DeadlyImportError("MDL file contains invalid data");
165
8
        }
166
167
1.52k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
1.53k
    }
Assimp::MDL::HalfLife::Trivert const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Trivert>(int, int) const
Line
Count
Source
155
58.0k
    const DataType *get_data(int offset, int elements) const {
156
58.0k
        if (offset < 0 || elements < 0) {
157
0
            throw DeadlyImportError("MDL file contains invalid data");
158
0
        }
159
160
58.0k
        const size_t uoffset = static_cast<size_t>(offset);
161
58.0k
        const size_t uelements = static_cast<size_t>(elements);
162
163
58.0k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
1
            throw DeadlyImportError("MDL file contains invalid data");
165
1
        }
166
167
58.0k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
58.0k
    }
Assimp::MDL::HalfLife::SequenceDesc_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::SequenceDesc_HL1>(int, int) const
Line
Count
Source
155
1.72k
    const DataType *get_data(int offset, int elements) const {
156
1.72k
        if (offset < 0 || elements < 0) {
157
4
            throw DeadlyImportError("MDL file contains invalid data");
158
4
        }
159
160
1.71k
        const size_t uoffset = static_cast<size_t>(offset);
161
1.71k
        const size_t uelements = static_cast<size_t>(elements);
162
163
1.71k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
1.71k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
1.71k
    }
Assimp::MDL::HalfLife::SequenceGroup_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::SequenceGroup_HL1>(int, int) const
Line
Count
Source
155
1.79k
    const DataType *get_data(int offset, int elements) const {
156
1.79k
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
1.79k
        const size_t uoffset = static_cast<size_t>(offset);
161
1.79k
        const size_t uelements = static_cast<size_t>(elements);
162
163
1.79k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
6
            throw DeadlyImportError("MDL file contains invalid data");
165
6
        }
166
167
1.79k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
1.79k
    }
Assimp::MDL::HalfLife::AnimValueOffset_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::AnimValueOffset_HL1>(int, int) const
Line
Count
Source
155
1.14k
    const DataType *get_data(int offset, int elements) const {
156
1.14k
        if (offset < 0 || elements < 0) {
157
3
            throw DeadlyImportError("MDL file contains invalid data");
158
3
        }
159
160
1.14k
        const size_t uoffset = static_cast<size_t>(offset);
161
1.14k
        const size_t uelements = static_cast<size_t>(elements);
162
163
1.14k
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
1.14k
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
1.14k
    }
Assimp::MDL::HalfLife::AnimEvent_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::AnimEvent_HL1>(int, int) const
Line
Count
Source
155
59
    const DataType *get_data(int offset, int elements) const {
156
59
        if (offset < 0 || elements < 0) {
157
3
            throw DeadlyImportError("MDL file contains invalid data");
158
3
        }
159
160
56
        const size_t uoffset = static_cast<size_t>(offset);
161
56
        const size_t uelements = static_cast<size_t>(elements);
162
163
56
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
52
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
56
    }
Assimp::MDL::HalfLife::Attachment_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Attachment_HL1>(int, int) const
Line
Count
Source
155
35
    const DataType *get_data(int offset, int elements) const {
156
35
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
33
        const size_t uoffset = static_cast<size_t>(offset);
161
33
        const size_t uelements = static_cast<size_t>(elements);
162
163
33
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
4
            throw DeadlyImportError("MDL file contains invalid data");
165
4
        }
166
167
29
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
33
    }
Assimp::MDL::HalfLife::Hitbox_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::Hitbox_HL1>(int, int) const
Line
Count
Source
155
83
    const DataType *get_data(int offset, int elements) const {
156
83
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
81
        const size_t uoffset = static_cast<size_t>(offset);
161
81
        const size_t uelements = static_cast<size_t>(elements);
162
163
81
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
9
            throw DeadlyImportError("MDL file contains invalid data");
165
9
        }
166
167
72
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
81
    }
Assimp::MDL::HalfLife::BoneController_HL1 const* Assimp::MDL::HalfLife::HL1DataBuffer::get_data<Assimp::MDL::HalfLife::BoneController_HL1>(int, int) const
Line
Count
Source
155
26
    const DataType *get_data(int offset, int elements) const {
156
26
        if (offset < 0 || elements < 0) {
157
2
            throw DeadlyImportError("MDL file contains invalid data");
158
2
        }
159
160
24
        const size_t uoffset = static_cast<size_t>(offset);
161
24
        const size_t uelements = static_cast<size_t>(elements);
162
163
24
        if (uoffset > length_ || uelements > (length_ - uoffset) / sizeof(DataType)) {
164
3
            throw DeadlyImportError("MDL file contains invalid data");
165
3
        }
166
167
21
        return reinterpret_cast<const DataType *>(data_ + uoffset);
168
24
    }
169
170
    /** \brief Return whether a byte range is inside the buffer. */
171
192k
    bool ContainsBytes(const uint8_t *ptr, size_t bytes) const {
172
192k
        if (ptr == nullptr || data_ == nullptr) {
173
0
            return false;
174
0
        }
175
176
192k
        const auto start = reinterpret_cast<uintptr_t>(data_);
177
192k
        const auto address = reinterpret_cast<uintptr_t>(ptr);
178
192k
        if (address < start) {
179
0
            return false;
180
0
        }
181
182
192k
        const auto offset = static_cast<size_t>(address - start);
183
192k
        if (offset > length_) {
184
0
            return false;
185
0
        }
186
187
192k
        return bytes <= length_ - offset;
188
192k
    }
189
190
    /** \brief Return whether a typed range is inside the buffer. */
191
    template <typename DataType>
192
150k
    bool Contains(const DataType *ptr, size_t elements) const {
193
150k
        if (elements > length_ / sizeof(DataType)) {
194
0
            return false;
195
0
        }
196
197
150k
        return ContainsBytes(static_cast<const uint8_t *>(static_cast<const void *>(ptr)), elements * sizeof(DataType));
198
150k
    }
199
200
    /** \brief Size of the buffer in bytes. */
201
0
    size_t size() const {
202
0
        return length_;
203
0
    }
204
205
private:
206
    /** Raw byte pointer (points to owner_.get() when owning, otherwise external). */
207
    const unsigned char *data_;
208
    /** Buffer length in bytes. */
209
    size_t length_;
210
    /** Owning storage (null for views). */
211
    std::unique_ptr<unsigned char[]> owner_;
212
};
213
214
} // namespace HalfLife
215
} // namespace MDL
216
} // namespace Assimp
217
218
#endif // AI_HL1DATABUFFER_INCLUDED