summaryrefslogtreecommitdiffstats
path: root/src/djinterop/enginelibrary/encode_decode_utils.hpp
blob: 8ab72edf26f77b25444a453a54e6d986b3c55e88 (plain)
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
/*
    This file is part of libdjinterop.

    libdjinterop is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libdjinterop is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with libdjinterop.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <cstdint>
#include <cstring>
#include <tuple>
#include <vector>

namespace djinterop
{
namespace enginelibrary
{
// Uncompress a zlib'ed BLOB
std::vector<char> zlib_uncompress(
    const std::vector<char>& compressed, std::vector<char> uncompressed = {});

// Compress a byte array using zlib
std::vector<char> zlib_compress(
    const std::vector<char>& uncompressed, std::vector<char> compressed = {});

// Extract an int8_t from a raw value at ptr address
inline std::pair<uint8_t, const char*> decode_uint8(const char* ptr)
{
    return {static_cast<uint8_t>(*ptr), ptr + 1};
}

// Encode an uint8_t as a raw byte to an output pointer
inline char* encode_uint8(uint8_t value, char* ptr)
{
    *ptr = static_cast<char>(value);
    return ptr + 1;
}

// Decode an int32_t from a little-endian encoded raw value at ptr address
inline std::pair<int32_t, const char*> decode_int32_le(const char* ptr)
{
    int32_t value = static_cast<int32_t>(static_cast<uint8_t>(ptr[0])) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[1]) << 8) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[2]) << 16) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[3]) << 24);
    return {value, ptr + 4};
}

// Encode an int32_t as 4 raw bytes to an output pointer with little-endianness
inline char* encode_int32_le(int32_t value, char* ptr)
{
    ptr[0] = static_cast<char>(value);
    ptr[1] = static_cast<char>(value >> 8);
    ptr[2] = static_cast<char>(value >> 16);
    ptr[3] = static_cast<char>(value >> 24);
    return ptr + 4;
}

// Decode an int32_t from a big-endian encoded raw value at ptr address
inline std::pair<int32_t, const char*> decode_int32_be(const char* ptr)
{
    int32_t value = static_cast<int32_t>(static_cast<uint8_t>(ptr[0]) << 24) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[1]) << 16) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[2]) << 8) |
                    static_cast<int32_t>(static_cast<uint8_t>(ptr[3]));
    return {value, ptr + 4};
}

// Encode an int32_t as 4 raw bytes to an output pointer with big-endianness
inline char* encode_int32_be(int32_t value, char* ptr)
{
    ptr[0] = static_cast<char>(value >> 24);
    ptr[1] = static_cast<char>(value >> 16);
    ptr[2] = static_cast<char>(value >> 8);
    ptr[3] = static_cast<char>(value);
    return ptr + 4;
}

// Decode an int64_t from a little-endian encoded raw value at ptr address
inline std::pair<int64_t, const char*> decode_int64_le(const char* ptr)
{
    int32_t e1, e2;
    std::tie(e1, ptr) = decode_int32_le(ptr);
    std::tie(e2, ptr) = decode_int32_le(ptr);
    int64_t value = static_cast<int64_t>(static_cast<uint32_t>(e1)) |
                    static_cast<int64_t>(static_cast<uint32_t>(e2)) << 32;
    return {value, ptr};
}

// Encode an int64_t as 4 raw bytes to an output pointer with little-endianness
inline char* encode_int64_le(int64_t value, char* ptr)
{
    ptr = encode_int32_le(static_cast<int32_t>(value), ptr);
    ptr = encode_int32_le(static_cast<int32_t>(value >> 32), ptr);
    return ptr;
}

// Decode an int64_t from a big-endian encoded raw value at ptr address
inline std::pair<int64_t, const char*> decode_int64_be(const char* ptr)
{
    int32_t e1, e2;
    std::tie(e1, ptr) = decode_int32_be(ptr);
    std::tie(e2, ptr) = decode_int32_be(ptr);
    int64_t value = static_cast<int64_t>(static_cast<uint32_t>(e1)) << 32 |
                    static_cast<int64_t>(static_cast<uint32_t>(e2));
    return {value, ptr};
}

// Encode an int64_t as 4 raw bytes to an output pointer with big-endianness
inline char* encode_int64_be(int64_t value, char* ptr)
{
    ptr = encode_int32_be(static_cast<int32_t>(value >> 32), ptr);
    ptr = encode_int32_be(static_cast<int32_t>(value), ptr);
    return ptr;
}

// Decode a double from a little-endian encoded raw value at ptr address
inline std::pair<double, const char*> decode_double_le(const char* ptr)
{
    int64_t value;
    std::tie(value, ptr) = decode_int64_le(ptr);
    double result;
    std::memcpy(&result, &value, 8);
    return {result, ptr};
}

// Encode a double as 8 raw bytes to an output pointer with little-endianness
inline char* encode_double_le(double value, char* ptr)
{
    int64_t temp;
    std::memcpy(&temp, &value, 8);
    return encode_int64_le(temp, ptr);
}

// Decode a double from a big-endian encoded raw value at ptr address
inline std::pair<double, const char*> decode_double_be(const char* ptr)
{
    int64_t value;
    std::tie(value, ptr) = decode_int64_be(ptr);
    double result;
    std::memcpy(&result, &value, 8);
    return {result, ptr};
}

// Encode a double as 8 raw bytes to an output pointer with big-endianness
inline char* encode_double_be(double value, char* ptr)
{
    int64_t temp;
    std::memcpy(&temp, &value, 8);
    return encode_int64_be(temp, ptr);
}

}  // namespace enginelibrary
}  // namespace djinterop