wolfengine 3.0.2

Wolf is a set of modules for realtime rendering, realtime streaming and game developing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "lzma.hpp"
#include "wolf_system/src/compression/lzma.rs.h"

#include <LzmaEnc.h>
#include <LzmaDec.h>
#include <Lzma2Enc.h>
#include <Lzma2Dec.h>

#include <limits>
#include <iostream>

#include <mimalloc/mimalloc.h>
#include <gsl/gsl>

#define LZMA_HEADER_SRC_SIZE 8

static void* _lzma_alloc(ISzAllocPtr pPtr, size_t pSize) noexcept
{
    (void)pPtr;
    return mi_malloc(pSize);
}
static void _lzma_free(ISzAllocPtr pPtr, void* pAddr) noexcept
{
    (void)pPtr;
    mi_free(pAddr);
}

static ISzAlloc _alloc_funcs = {_lzma_alloc, _lzma_free};

static void _lzma_prop(CLzmaEncProps* pProp, uint32_t pLevel, int pSrcSize) noexcept
{
    // set up properties
    LzmaEncProps_Init(pProp);
    if (pSrcSize <= (1 << 12))
    {
        pProp->dictSize = 1 << 12; //1mb dictionary
    }
    else if (pSrcSize >= (3 << 29))
    {
        pProp->dictSize = 3 << 29;
    }
    else
    {
        pProp->dictSize = gsl::narrow_cast<uint32_t>(pSrcSize); //smaller dictionary = faster!
    }
    pProp->fb = 40;
    pProp->level = pLevel > 9 ? 9 : pLevel;
}

rust::Vec<uint8_t> compress_1(rust::Slice<const uint8_t> pSrcBuffer,
    uint32_t pLevel,
    rust::String& pTrace)
{
    auto _src_size = gsl::narrow_cast<uint64_t>(pSrcBuffer.size());
    if (_src_size == 0)
    {
        pTrace = "pSrcBuffer.size() == 0. trace info: lzma_cxx::compress_1";
        return rust::Vec<uint8_t>();
    }

    // set up properties
    CLzmaEncProps _props;
    _lzma_prop(&_props, pLevel, _src_size);

    // prepare space for the encoded properties
    size_t _props_size = LZMA_PROPS_SIZE;
    uint8_t _props_encoded[LZMA_PROPS_SIZE] = { 0 };

    // allocate some space for the compression output
    // this is way more than necessary in most cases...
    size_t _output_size_64 = _src_size * 2;
    if (_output_size_64 < 1024)
    {
        _output_size_64 = 1024;
    }

    uint8_t* _compress_mem = gsl::narrow_cast<uint8_t*>(
        mi_malloc(_output_size_64 * sizeof(uint8_t)));
    if (!_compress_mem)
    {
        pTrace = "could not allocate memory for compressed buffer. trace info: lzma_cxx::compress_1";
        return rust::Vec<uint8_t>();
    }

    const auto _lzma_status = LzmaEncode(
        _compress_mem,
        &_output_size_64,
        gsl::narrow_cast<const uint8_t*>(pSrcBuffer.data()),
        _src_size,
        &_props,
        &_props_encoded[0],
        &_props_size,
        0,
        NULL,
        &_alloc_funcs,
        &_alloc_funcs);

    const auto _compressed_size = _output_size_64 + LZMA_HEADER_SRC_SIZE + LZMA_PROPS_SIZE;
    if (_lzma_status == SZ_OK)
    {
        rust::vec<uint8_t> v;
        v.reserve(_compressed_size);

        // tricky: we have to generate the LZMA header
        // 5 bytes properties + 8 byte uncompressed size
        copy(&_props_encoded[0], &_props_encoded[LZMA_PROPS_SIZE], std::back_inserter(v));
        for (int i = 0; i < LZMA_HEADER_SRC_SIZE; i++)
        {
            v.push_back((_src_size >> (i * 8)) & 0xFF);
        }
        //copy the compressed size
        copy(&_compress_mem[0], &_compress_mem[_output_size_64], std::back_inserter(v));

        //free memory
        mi_free(_compress_mem);
        return v;
    }
    else
    {
        pTrace = "LzmaEncode failed on compressing. trace info: lzma_cxx::compress_1";
        return rust::Vec<uint8_t>();
    }
}

rust::Vec<uint8_t> decompress_1(
    rust::Slice<const uint8_t> pSrcBuffer,
    rust::String& pTrace)
{
    const auto _src_size = pSrcBuffer.size();
    if (_src_size < LZMA_HEADER_SRC_SIZE + LZMA_PROPS_SIZE)
    {
        pTrace = "invalid lzma header. trace info: lzma_cxx::decompress_1";
        return rust::Vec<uint8_t>();
    }

    // extract the size from the header
    uint64_t _size_from_header = 0;
    for (uint64_t i = 0; i < LZMA_HEADER_SRC_SIZE; i++)
    {
        auto a = gsl::narrow_cast<uint64_t>(gsl::at(pSrcBuffer, LZMA_PROPS_SIZE + i));
        auto b = gsl::narrow_cast<uint64_t>(i * 8);
        auto c = a << b;
        if (c < std::numeric_limits<uint64_t>::max())
        {
            _size_from_header |= c;
        }
    }
    rust::Vec<uint8_t> v;
    if (_size_from_header <= (256 * 1024 * 1024))
    {
        auto data = gsl::narrow_cast<uint8_t*>(mi_malloc(_size_from_header));
        if (!data)
        {
            pTrace = "could not allocate memory for lzma decompress buffer. trace info: lzma_cxx::decompress_1";
            return rust::Vec<uint8_t>();
        }
        ELzmaStatus _lzma_status;
        size_t _proc_out_size = _size_from_header, _proc_in_size = _src_size - LZMA_HEADER_SRC_SIZE + LZMA_PROPS_SIZE;
        const auto status = LzmaDecode(
            data,
            &_proc_out_size,
            &gsl::at(pSrcBuffer, LZMA_HEADER_SRC_SIZE + LZMA_PROPS_SIZE),
            &_proc_in_size,
            pSrcBuffer.data(),
            5,
            LZMA_FINISH_END,
            &_lzma_status,
            &_alloc_funcs);
        if (status == SZ_OK && _proc_out_size == _size_from_header)
        {
            v.reserve(_proc_out_size);
            //copy data
            copy(&data[0], &data[_proc_out_size], std::back_inserter(v));
            //free memory
            mi_free(data);
        }
    }
    else
    {
        pTrace = "invalid lzma header size. trace info: lzma_cxx::decompress_1";
        return rust::Vec<uint8_t>();
    }
    return v;
}

rust::Vec<uint8_t> compress_2(
    rust::Slice<const uint8_t> pSrcBuffer,
    uint32_t pLevel,
    rust::String& pTrace)
{
    const auto _src_size = pSrcBuffer.size();
    if (_src_size == 0)
    {
        pTrace = "pSrcBuffer.size() == 0. trace info: lzma_cxx::compress_2";
        return rust::Vec<uint8_t>();
    }

    CLzma2EncHandle _enc_handler = Lzma2Enc_Create(&_alloc_funcs, &_alloc_funcs);
    if (!_enc_handler)
    {
        pTrace = "Lzma2Enc_Create(...) == NULL. trace info: lzma_cxx::compress_2";
        return rust::Vec<uint8_t>();
    }

    // set up properties
    CLzmaEncProps _props_1;
    _lzma_prop(&_props_1, pLevel, _src_size);

    CLzma2EncProps _props_2;
    Lzma2EncProps_Init(&_props_2);
    _props_2.lzmaProps = _props_1;

    const auto _props_status = Lzma2Enc_SetProps(_enc_handler, &_props_2);
    if (_props_status != SZ_OK)
    {
        pTrace = "Lzma2Enc_SetProps(...) == SZ_OK. trace info: lzma_cxx::compress_2";
        return rust::Vec<uint8_t>();
    }

    // prepare space for the encoded properties
    const auto properties = Lzma2Enc_WriteProperties(_enc_handler);
    // allocate some space for the compression output
    // this is way more than necessary in most cases.
    size_t _output_size_64 = _src_size * 2;
    if (_output_size_64 < 1024)
    {
        _output_size_64 = 1024;
    }

    std::vector<uint8_t> _out_buffer;
    auto _compress_mem = gsl::narrow_cast<uint8_t*>(mi_malloc(_output_size_64 * sizeof(uint8_t)));
    if (!_compress_mem)
    {
        pTrace = "could not allocate memory for compressed buffer. trace info: lzma_cxx::compress";
        return rust::Vec<uint8_t>();
    }

    const auto _encode_status = Lzma2Enc_Encode2(
        _enc_handler,
        NULL,
        _compress_mem,
        &_output_size_64,
        NULL,
        (const Byte*)pSrcBuffer.data(),
        _src_size,
        NULL);
    Lzma2Enc_Destroy(_enc_handler);

    const auto _compressed_size = _output_size_64 + LZMA_HEADER_SRC_SIZE + sizeof(properties);
    if (_encode_status == SZ_OK)
    {
        rust::Vec<uint8_t> v;
        v.reserve(_compressed_size);

        /*
            tricky: we have to generate the LZMA header
            1 byte properties + 8 bytes uncompressed size
        */
        v.push_back(properties);
        for (int i = 0; i < LZMA_HEADER_SRC_SIZE; i++)
        {
            v.push_back((_src_size >> (i * 8)) & 0xFF);
        }

        //copy the compressed size
        copy(&_compress_mem[0], &_compress_mem[_output_size_64], std::back_inserter(v));

        //free memory
        mi_free(_compress_mem);
        return v;
    }
    else
    {
        pTrace = "LzmaEncode failed on compressing. trace info: lzma_cxx::compress";
        return rust::Vec<uint8_t>();
    }
}

rust::Vec<uint8_t> decompress_2(rust::Slice<const uint8_t> pSrcBuffer,
    rust::String& pTrace)
{
    const auto _src_size = pSrcBuffer.size();
    if (_src_size < LZMA_HEADER_SRC_SIZE + sizeof(Byte))
    {
        pTrace = "invalid lzma2 header. trace info: lzma_cxx::decompress_2";
        return rust::Vec<uint8_t>();
    }

    // extract the size from the header
    uint64_t _size_from_header = 0;
    for (uint64_t i = 0; i < LZMA_HEADER_SRC_SIZE; i++)
    {
        auto a = gsl::narrow_cast<uint64_t>(gsl::at(pSrcBuffer, sizeof(Byte) + i));
        auto b = gsl::narrow_cast<uint64_t>(i * 8);
        auto c = a << b;
        if (c < std::numeric_limits<uint64_t>::max())
        {
            _size_from_header |= c;
        }
    }
    const auto _pre_out_size = _size_from_header * 2;
    if (_size_from_header <= (256 * 1024 * 1024))
    {
        rust::Vec<uint8_t> _output_buffer;
        auto _data = gsl::narrow_cast<uint8_t*>(mi_malloc(_size_from_header));
        if (!_data)
        {
            pTrace = "could not allocate memory for lzma decompress buffer. trace info: lzma_cxx::decompress_2";
            return rust::Vec<uint8_t>();
        }

        CLzma2Dec _dec;
        Lzma2Dec_Construct(&_dec);

        auto _res = Lzma2Dec_Allocate(&_dec, 0, &_alloc_funcs);
        if (_res != SZ_OK)
        {
            pTrace = "Lzma2Dec_Allocate(...) == SZ_OK. trace info: lzma_cxx::decompress_2";
            return rust::Vec<uint8_t>();
        }

        Lzma2Dec_Init(&_dec);
        const auto properties = gsl::at(pSrcBuffer, 0);
        unsigned _out_pos = 0, _in_pos = LZMA_HEADER_SRC_SIZE + sizeof(properties);

        ELzmaStatus _status = LZMA_STATUS_NOT_SPECIFIED;
        constexpr uint32_t BUF_SIZE = 10240;
        while (_out_pos < _pre_out_size)
        {
            size_t _dest_len = std::min((uint64_t)BUF_SIZE, _pre_out_size - _out_pos);
            size_t _src_len = std::min((size_t)BUF_SIZE, (size_t)_src_size - (size_t)_in_pos);
            _res = Lzma2Dec_DecodeToBuf(
                &_dec,
                &_data[_out_pos], 
                &_dest_len,
                &gsl::at(pSrcBuffer, _in_pos), 
                &_src_len,
                (_out_pos + _dest_len == _size_from_header) ? LZMA_FINISH_END : LZMA_FINISH_ANY,
                &_status);
            if (_res != SZ_OK)
            {
                pTrace = "Lzma2Dec_DecodeToBuf(...) == SZ_OK. trace info: lzma_cxx::decompress_2";
                return rust::Vec<uint8_t>();
            }
            _in_pos += _src_len;
            _out_pos += _dest_len;
            if (_status == LZMA_STATUS_FINISHED_WITH_MARK)
            {
                break;
            }
        }
        Lzma2Dec_Free(&_dec, &_alloc_funcs);
        if (_out_pos == _size_from_header)
        {
            _output_buffer.reserve(_out_pos);
            //copy data
            copy(&_data[0], &_data[_out_pos], std::back_inserter(_output_buffer));
            //free memory
            mi_free(_data);
        }
        return _output_buffer;
    }
    else
    {
        pTrace = "invalid lzma2 header size. trace info: lzma_cxx::decompress_2";
        return rust::Vec<uint8_t>();
    }
}

namespace lzma_cxx
{
    rust::Vec<uint8_t> compress(
        rust::Slice<const uint8_t> p_src_buffer,
        LZMAType p_type,
        uint32_t p_level,
        rust::String& p_trace)
    {
        switch (p_type)
        {
        case LZMAType::LZMA1:
        {
            return compress_1(p_src_buffer, p_level, p_trace);
        }
        case LZMAType::LZMA2:
        {
            return compress_2(p_src_buffer, p_level, p_trace);
        }
        }
    }

    rust::Vec<uint8_t> decompress(
        rust::Slice<const uint8_t> p_src_buffer,
        LZMAType p_type,
        rust::String& p_trace)
    {
        switch (p_type)
        {
        case LZMAType::LZMA1:
        {
            return decompress_1(p_src_buffer, p_trace);
        }
        case LZMAType::LZMA2:
        {
            return decompress_2(p_src_buffer, p_trace);
        }
        }
    }
}