Coverage Report

Created: 2025-06-22 07:30

/src/assimp/contrib/Open3DGC/o3dgcBinaryStream.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
3
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
10
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
THE SOFTWARE.
21
*/
22
23
24
#pragma once
25
#ifndef O3DGC_BINARY_STREAM_H
26
#define O3DGC_BINARY_STREAM_H
27
28
#include "o3dgcCommon.h"
29
#include "o3dgcVector.h"
30
31
namespace o3dgc
32
{
33
    const unsigned long O3DGC_BINARY_STREAM_DEFAULT_SIZE       = 4096;
34
    const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0   = 7;
35
    const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL0        = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0) - 1;
36
    const unsigned long O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1   = 6;
37
    const unsigned long O3DGC_BINARY_STREAM_MAX_SYMBOL1        = (1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) - 1;
38
    const unsigned long O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32 = (32+O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0-1) / 
39
                                                                 O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
40
41
    //! 
42
    class BinaryStream
43
    {
44
    public:    
45
        //! Constructor.
46
                                BinaryStream(unsigned long size = O3DGC_BINARY_STREAM_DEFAULT_SIZE)
47
0
                                {
48
0
                                    m_endianness = SystemEndianness();
49
0
                                    m_stream.Allocate(size);
50
0
                                };
51
        //! Destructor.
52
0
                                ~BinaryStream(void){};
53
54
        void                    WriteFloat32(float value, O3DGCStreamType streamType)
55
0
                                {
56
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
57
0
                                    {
58
0
                                        WriteFloat32ASCII(value);
59
0
                                    }
60
0
                                    else
61
0
                                    {
62
0
                                        WriteFloat32Bin(value);
63
0
                                    }
64
0
                                }
65
        void                    WriteUInt32(unsigned long position, unsigned long value, O3DGCStreamType streamType)
66
0
                                {
67
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
68
0
                                    {
69
0
                                        WriteUInt32ASCII(position, value);
70
0
                                    }
71
0
                                    else
72
0
                                    {
73
0
                                        WriteUInt32Bin(position, value);
74
0
                                    }
75
0
                                }
76
        void                    WriteUInt32(unsigned long value, O3DGCStreamType streamType)
77
0
                                {
78
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
79
0
                                    {
80
0
                                        WriteUInt32ASCII(value);
81
0
                                    }
82
0
                                    else
83
0
                                    {
84
0
                                        WriteUInt32Bin(value);
85
0
                                    }
86
0
                                }
87
        void                    WriteUChar(unsigned int position, unsigned char value, O3DGCStreamType streamType)
88
0
                                {
89
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
90
0
                                    {
91
0
                                        WriteUInt32ASCII(position, value);
92
0
                                    }
93
0
                                    else
94
0
                                    {
95
0
                                        WriteUInt32Bin(position, value);
96
0
                                    }
97
0
                                }
98
        void                    WriteUChar(unsigned char value, O3DGCStreamType streamType)
99
0
                                {
100
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
101
0
                                    {
102
0
                                        WriteUCharASCII(value);
103
0
                                    }
104
0
                                    else
105
0
                                    {
106
0
                                        WriteUChar8Bin(value);
107
0
                                    }
108
0
                                }
109
        float                   ReadFloat32(unsigned long & position, O3DGCStreamType streamType) const
110
0
                                {
111
0
                                    float value;
112
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
113
0
                                    {
114
0
                                        value = ReadFloat32ASCII(position);
115
0
                                    }
116
0
                                    else
117
0
                                    {
118
0
                                        value = ReadFloat32Bin(position);
119
0
                                    }
120
0
                                    return value;
121
0
                                }
122
        unsigned long           ReadUInt32(unsigned long & position, O3DGCStreamType streamType) const
123
0
                                {
124
0
                                    unsigned long value;
125
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
126
0
                                    {
127
0
                                        value = ReadUInt32ASCII(position);
128
0
                                    }
129
0
                                    else
130
0
                                    {
131
0
                                        value = ReadUInt32Bin(position);
132
0
                                    }
133
0
                                    return value;
134
0
                                }
135
        unsigned char           ReadUChar(unsigned long & position, O3DGCStreamType streamType) const
136
0
                                {
137
0
                                    unsigned char value;
138
0
                                    if (streamType == O3DGC_STREAM_TYPE_ASCII)
139
0
                                    {
140
0
                                        value = ReadUCharASCII(position);
141
0
                                    }
142
0
                                    else
143
0
                                    {
144
0
                                        value = ReadUChar8Bin(position);
145
0
                                    }
146
0
                                    return value;
147
0
                                }
148
149
        void                    WriteFloat32Bin(unsigned long position, float value) 
150
0
                                {
151
0
                                    assert(position < m_stream.GetSize() - 4);
152
0
                                    unsigned char * ptr = (unsigned char *) (&value);
153
0
                                    if (m_endianness == O3DGC_BIG_ENDIAN)
154
0
                                    {
155
0
                                        m_stream[position++] = ptr[3];
156
0
                                        m_stream[position++] = ptr[2];
157
0
                                        m_stream[position++] = ptr[1];
158
0
                                        m_stream[position  ] = ptr[0];
159
0
                                    }
160
0
                                    else
161
0
                                    {
162
0
                                        m_stream[position++] = ptr[0];
163
0
                                        m_stream[position++] = ptr[1];
164
0
                                        m_stream[position++] = ptr[2];
165
0
                                        m_stream[position  ] = ptr[3];
166
0
                                    }
167
0
                                }
168
        void                    WriteFloat32Bin(float value) 
169
0
                                {
170
0
                                    unsigned char * ptr = (unsigned char *) (&value);
171
0
                                    if (m_endianness == O3DGC_BIG_ENDIAN)
172
0
                                    {
173
0
                                        m_stream.PushBack(ptr[3]);
174
0
                                        m_stream.PushBack(ptr[2]);
175
0
                                        m_stream.PushBack(ptr[1]);
176
0
                                        m_stream.PushBack(ptr[0]);
177
0
                                    }
178
0
                                    else
179
0
                                    {
180
0
                                        m_stream.PushBack(ptr[0]);
181
0
                                        m_stream.PushBack(ptr[1]);
182
0
                                        m_stream.PushBack(ptr[2]);
183
0
                                        m_stream.PushBack(ptr[3]);
184
0
                                    }
185
0
                                }
186
        void                    WriteUInt32Bin(unsigned long position, unsigned long value) 
187
0
                                {
188
0
                                    assert(position < m_stream.GetSize() - 4);
189
0
                                    unsigned char * ptr = (unsigned char *) (&value);
190
0
                                    if (m_endianness == O3DGC_BIG_ENDIAN)
191
0
                                    {
192
0
                                        m_stream[position++] = ptr[3];
193
0
                                        m_stream[position++] = ptr[2];
194
0
                                        m_stream[position++] = ptr[1];
195
0
                                        m_stream[position  ] = ptr[0];
196
0
                                    }
197
0
                                    else
198
0
                                    {
199
0
                                        m_stream[position++] = ptr[0];
200
0
                                        m_stream[position++] = ptr[1];
201
0
                                        m_stream[position++] = ptr[2];
202
0
                                        m_stream[position  ] = ptr[3];
203
0
                                    }
204
0
                                }
205
        void                    WriteUInt32Bin(unsigned long value) 
206
0
                                {
207
0
                                    unsigned char * ptr = (unsigned char *) (&value);
208
0
                                    if (m_endianness == O3DGC_BIG_ENDIAN)
209
0
                                    {
210
0
                                        m_stream.PushBack(ptr[3]);
211
0
                                        m_stream.PushBack(ptr[2]);
212
0
                                        m_stream.PushBack(ptr[1]);
213
0
                                        m_stream.PushBack(ptr[0]);
214
0
                                    }
215
0
                                    else
216
0
                                    {
217
0
                                        m_stream.PushBack(ptr[0]);
218
0
                                        m_stream.PushBack(ptr[1]);
219
0
                                        m_stream.PushBack(ptr[2]);
220
0
                                        m_stream.PushBack(ptr[3]);
221
0
                                    }
222
0
                                }
223
        void                    WriteUChar8Bin(unsigned int position, unsigned char value) 
224
0
                                {
225
0
                                    m_stream[position] = value;
226
0
                                }
227
        void                    WriteUChar8Bin(unsigned char value) 
228
0
                                {
229
0
                                    m_stream.PushBack(value);
230
0
                                }
231
        float                   ReadFloat32Bin(unsigned long & position) const
232
0
                                {
233
0
                                    unsigned long value = ReadUInt32Bin(position);
234
0
                                    float fvalue;
235
0
                                    memcpy(&fvalue, &value, 4);
236
0
                                    return fvalue;
237
0
                                }
238
        unsigned long           ReadUInt32Bin(unsigned long & position)  const
239
0
                                {
240
0
                                    assert(position < m_stream.GetSize() - 4);
241
0
                                    unsigned long value = 0;
242
0
                                    if (m_endianness == O3DGC_BIG_ENDIAN)
243
0
                                    {
244
0
                                        value += (m_stream[position++]<<24);
245
0
                                        value += (m_stream[position++]<<16);
246
0
                                        value += (m_stream[position++]<<8);
247
0
                                        value += (m_stream[position++]);
248
0
                                    }
249
0
                                    else
250
0
                                    {
251
0
                                        value += (m_stream[position++]);
252
0
                                        value += (m_stream[position++]<<8);
253
0
                                        value += (m_stream[position++]<<16);
254
0
                                        value += (m_stream[position++]<<24);
255
0
                                    }
256
0
                                    return value;
257
0
                                }
258
        unsigned char           ReadUChar8Bin(unsigned long & position) const
259
0
                                {
260
0
                                    return m_stream[position++];
261
0
                                }
262
263
        void                    WriteFloat32ASCII(float value) 
264
0
                                {
265
0
                                    unsigned long uiValue;
266
0
                                    memcpy(&uiValue, &value, 4);
267
0
                                    WriteUInt32ASCII(uiValue);
268
0
                                }
269
        void                    WriteUInt32ASCII(unsigned long position, unsigned long value) 
270
0
                                {
271
0
                                    assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
272
0
                                    unsigned long value0 = value;
273
0
                                    for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
274
0
                                    {
275
0
                                        m_stream[position++] = (value0 & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
276
0
                                        value0 >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
277
0
                                    }
278
0
                                }
279
        void                    WriteUInt32ASCII(unsigned long value) 
280
0
                                {
281
0
                                    for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
282
0
                                    {
283
0
                                        m_stream.PushBack(value & O3DGC_BINARY_STREAM_MAX_SYMBOL0);
284
0
                                        value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
285
0
                                    }
286
0
                                }
287
        void                    WriteIntASCII(long value) 
288
0
                                {
289
0
                                    WriteUIntASCII(IntToUInt(value));
290
0
                                }
291
        void                    WriteUIntASCII(unsigned long value) 
292
0
                                {
293
0
                                    if (value >= O3DGC_BINARY_STREAM_MAX_SYMBOL0)
294
0
                                    {
295
0
                                        m_stream.PushBack(O3DGC_BINARY_STREAM_MAX_SYMBOL0);
296
0
                                        value -= O3DGC_BINARY_STREAM_MAX_SYMBOL0;
297
0
                                        unsigned char a, b;
298
0
                                        do
299
0
                                        {
300
0
                                            a  = ((value & O3DGC_BINARY_STREAM_MAX_SYMBOL1) << 1);
301
0
                                            b  = ( (value >>= O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) > 0);
302
0
                                            a += b;
303
0
                                            m_stream.PushBack(a);
304
0
                                        } while (b);
305
0
                                    }
306
0
                                    else
307
0
                                    {
308
0
                                        m_stream.PushBack((unsigned char) value);
309
0
                                    }
310
0
                                }
311
        void                    WriteUCharASCII(unsigned char value) 
312
0
                                {
313
0
                                    assert(value <= O3DGC_BINARY_STREAM_MAX_SYMBOL0);
314
0
                                    m_stream.PushBack(value);
315
0
                                }
316
        float                   ReadFloat32ASCII(unsigned long & position) const
317
0
                                {
318
0
                                    unsigned long value = ReadUInt32ASCII(position);
319
0
                                    float fvalue;
320
0
                                    memcpy(&fvalue, &value, 4);
321
0
                                    return fvalue;
322
0
                                }
323
        unsigned long           ReadUInt32ASCII(unsigned long & position)  const
324
0
                                {
325
0
                                    assert(position < m_stream.GetSize() - O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32);
326
0
                                    unsigned long value = 0;
327
0
                                    unsigned long shift = 0;
328
0
                                    for(unsigned long i = 0; i < O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i)
329
0
                                    {
330
0
                                        value  += (m_stream[position++] << shift);
331
0
                                        shift  += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
332
0
                                    }
333
0
                                    return value;
334
0
                                }
335
        long                    ReadIntASCII(unsigned long & position) const
336
0
                                {
337
0
                                    return UIntToInt(ReadUIntASCII(position));
338
0
                                }
339
        unsigned long           ReadUIntASCII(unsigned long & position) const
340
0
                                {
341
0
                                    unsigned long value = m_stream[position++];
342
0
                                    if (value == O3DGC_BINARY_STREAM_MAX_SYMBOL0)
343
0
                                    {
344
0
                                        long x;
345
0
                                        unsigned long i = 0;
346
0
                                        do
347
0
                                        {
348
0
                                            x = m_stream[position++];
349
0
                                            value += ( (x>>1) << i);
350
0
                                            i += O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1;
351
0
                                        } while (x & 1);
352
0
                                    }
353
0
                                    return value;
354
0
                                }
355
        unsigned char           ReadUCharASCII(unsigned long & position) const
356
0
                                {
357
0
                                    return m_stream[position++];
358
0
                                }
359
        O3DGCErrorCode          Save(const char * const fileName) 
360
0
                                {
361
0
                                    FILE * fout = fopen(fileName, "wb");
362
0
                                    if (!fout)
363
0
                                    {
364
0
                                        return O3DGC_ERROR_CREATE_FILE;
365
0
                                    }
366
0
                                    fwrite(m_stream.GetBuffer(), 1, m_stream.GetSize(), fout);
367
0
                                    fclose(fout);
368
0
                                    return O3DGC_OK;
369
0
                                }
370
        O3DGCErrorCode          Load(const char * const fileName) 
371
0
                                {
372
0
                                    FILE * fin = fopen(fileName, "rb");
373
0
                                    if (!fin)
374
0
                                    {
375
0
                                        return O3DGC_ERROR_OPEN_FILE;
376
0
                                    }
377
0
                                    fseek(fin, 0, SEEK_END);
378
0
                                    unsigned long size = ftell(fin);
379
0
                                    m_stream.Allocate(size);
380
0
                                    rewind(fin);
381
0
                                    unsigned int nread = (unsigned int) fread((void *) m_stream.GetBuffer(), 1, size, fin);
382
0
                                    m_stream.SetSize(size);
383
0
                                    if (nread != size)
384
0
                                    {
385
0
                                        return O3DGC_ERROR_READ_FILE;
386
0
                                    }
387
0
                                    fclose(fin);
388
0
                                    return O3DGC_OK;
389
0
                                }
390
        O3DGCErrorCode          LoadFromBuffer(unsigned char * buffer, unsigned long bufferSize)
391
0
                                {
392
0
                                    m_stream.Allocate(bufferSize);
393
0
                                    memcpy(m_stream.GetBuffer(), buffer, bufferSize);
394
0
                                    m_stream.SetSize(bufferSize);
395
0
                                    return O3DGC_OK;
396
0
                                }
397
        unsigned long           GetSize() const
398
0
                                {
399
0
                                    return m_stream.GetSize();
400
0
                                }
401
    const unsigned char *       GetBuffer(unsigned long position) const
402
0
                                {
403
0
                                    return m_stream.GetBuffer() + position;
404
0
                                }
405
    unsigned char *             GetBuffer(unsigned long position)
406
0
                                {
407
0
                                    return (m_stream.GetBuffer() + position);
408
0
                                }                                
409
    unsigned char *             GetBuffer()
410
0
                                {
411
0
                                    return m_stream.GetBuffer();
412
0
                                }                                
413
    void                        GetBuffer(unsigned long position, unsigned char * & buffer) const
414
0
                                {
415
0
                                    buffer = (unsigned char *) (m_stream.GetBuffer() + position); // fix me: ugly!
416
0
                                }
417
    void                        SetSize(unsigned long size)
418
0
                                { 
419
0
                                    m_stream.SetSize(size);
420
0
                                };
421
    void                        Allocate(unsigned long size)
422
0
                                {
423
0
                                    m_stream.Allocate(size);
424
0
                                }
425
426
    private:
427
        Vector<unsigned char>   m_stream;
428
        O3DGCEndianness         m_endianness;
429
    };
430
431
}
432
#endif // O3DGC_BINARY_STREAM_H
433