summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-serialize-types.cpp
blob: ea6cd9c344aeab6b2901d9c21e46f989812a7d9f (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
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
// slang-serialize-types.cpp
#include "slang-serialize-types.h"

#include "../core/slang-byte-encode-util.h"
#include "../core/slang-math.h"
#include "../core/slang-text-io.h"

namespace Slang
{

// Needed for linkage with some compilers
/* static */ const SerialStringData::StringIndex SerialStringData::kNullStringIndex;
/* static */ const SerialStringData::StringIndex SerialStringData::kEmptyStringIndex;

namespace
{ // anonymous

struct ByteReader
{
    Byte operator()() const { return Byte(*m_pos++); }
    ByteReader(const char* pos)
        : m_pos(pos)
    {
    }
    mutable const char* m_pos;
};

} // namespace


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialStringTableUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/* static */ void SerialStringTableUtil::encodeStringTable(
    const StringSlicePool& pool,
    List<char>& stringTable)
{
    // Skip the default handles -> nothing is encoded via them
    return encodeStringTable(pool.getAdded(), stringTable);
}

/* static */ void SerialStringTableUtil::encodeStringTable(
    const ConstArrayView<UnownedStringSlice>& slices,
    List<char>& stringTable)
{
    stringTable.clear();
    for (const auto& slice : slices)
    {
        // TODO(JS):
        // This is a bit of a hack. We need to store the string length, along with the string
        // contents. We don't want to write the size as (say) uint32, because most strings are
        // short. So we just save off the length as a utf8 encoding. As it stands this *does* have
        // an arguable problem because encoding isn't of the full 32 bits.
        const int len = int(slice.getLength());

        // We need to write into the the string array
        char prefixBytes[6];
        const int numPrefixBytes = encodeUnicodePointToUTF8(len, prefixBytes);
        const Index baseIndex = stringTable.getCount();

        auto newCount = baseIndex + numPrefixBytes + len;
        stringTable.growToCount(newCount);

        char* dst = stringTable.begin() + baseIndex;

        memcpy(dst, prefixBytes, numPrefixBytes);
        memcpy(dst + numPrefixBytes, slice.begin(), len);
    }
}

/* static */ void SerialStringTableUtil::appendDecodedStringTable(
    const char* table,
    size_t tableSize,
    List<UnownedStringSlice>& slicesOut)
{
    const char* start = table;
    const char* cur = start;
    const char* end = table + tableSize;

    while (cur < end)
    {
        ByteReader reader(cur);
        const int len = getUnicodePointFromUTF8(reader);
        slicesOut.add(UnownedStringSlice(reader.m_pos, len));
        cur = reader.m_pos + len;
    }
}

/* static */ void SerialStringTableUtil::decodeStringTable(
    const char* table,
    size_t tableSize,
    List<UnownedStringSlice>& slicesOut)
{
    slicesOut.setCount(2);
    slicesOut[0] = UnownedStringSlice(nullptr, size_t(0));
    slicesOut[1] = UnownedStringSlice("", size_t(0));

    appendDecodedStringTable(table, tableSize, slicesOut);
}

/* static */ void SerialStringTableUtil::decodeStringTable(
    const char* table,
    size_t tableSize,
    StringSlicePool& outPool)
{
    outPool.clear();

    const char* start = table;
    const char* cur = start;
    const char* end = table + tableSize;

    while (cur < end)
    {
        ByteReader reader(cur);
        const int len = getUnicodePointFromUTF8(reader);
        outPool.add(UnownedStringSlice(reader.m_pos, len));
        cur = reader.m_pos + len;
    }
}

/* static */ void SerialStringTableUtil::calcStringSlicePoolMap(
    const List<UnownedStringSlice>& slices,
    StringSlicePool& pool,
    List<StringSlicePool::Handle>& indexMapOut)
{
    SLANG_ASSERT(slices.getCount() >= StringSlicePool::kDefaultHandlesCount);
    SLANG_ASSERT(
        slices[int(StringSlicePool::kNullHandle)] == "" &&
        slices[int(StringSlicePool::kNullHandle)].begin() == nullptr);
    SLANG_ASSERT(slices[int(StringSlicePool::kEmptyHandle)] == "");

    indexMapOut.setCount(slices.getCount());
    // Set up all of the defaults
    for (int i = 0; i < StringSlicePool::kDefaultHandlesCount; ++i)
    {
        indexMapOut[i] = StringSlicePool::Handle(i);
    }

    const Index numSlices = slices.getCount();
    for (Index i = StringSlicePool::kDefaultHandlesCount; i < numSlices; ++i)
    {
        indexMapOut[i] = pool.add(slices[i]);
    }
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialRiffUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/* static */ Result SerialRiffUtil::writeArrayChunk(
    SerialCompressionType compressionType,
    FourCC chunkId,
    const void* data,
    size_t numEntries,
    size_t typeSize,
    RiffContainer* container)
{
    typedef RiffContainer::Chunk Chunk;
    typedef RiffContainer::ScopeChunk ScopeChunk;

    if (numEntries == 0)
    {
        return SLANG_OK;
    }

    // Make compressed fourCC
    chunkId = (compressionType != SerialCompressionType::None)
                  ? SLANG_MAKE_COMPRESSED_FOUR_CC(chunkId)
                  : chunkId;

    ScopeChunk scope(container, Chunk::Kind::Data, chunkId);

    switch (compressionType)
    {
    case SerialCompressionType::None:
        {
            SerialBinary::ArrayHeader header;
            header.numEntries = uint32_t(numEntries);

            container->write(&header, sizeof(header));
            container->write(data, typeSize * numEntries);
            break;
        }
    case SerialCompressionType::VariableByteLite:
        {
            List<uint8_t> compressedPayload;

            size_t numCompressedEntries = (numEntries * typeSize) / sizeof(uint32_t);
            ByteEncodeUtil::encodeLiteUInt32(
                (const uint32_t*)data,
                numCompressedEntries,
                compressedPayload);

            SerialBinary::CompressedArrayHeader header;
            header.numEntries = uint32_t(numEntries);
            header.numCompressedEntries = uint32_t(numCompressedEntries);

            container->write(&header, sizeof(header));
            container->write(compressedPayload.getBuffer(), compressedPayload.getCount());
            break;
        }
    default:
        {
            return SLANG_FAIL;
        }
    }
    return SLANG_OK;
}

/* static */ Result SerialRiffUtil::readArrayChunk(
    SerialCompressionType compressionType,
    RiffContainer::DataChunk* dataChunk,
    ListResizer& listOut)
{
    typedef SerialBinary Bin;

    RiffReadHelper read = dataChunk->asReadHelper();
    const size_t typeSize = listOut.getTypeSize();

    switch (compressionType)
    {
    case SerialCompressionType::VariableByteLite:
        {
            Bin::CompressedArrayHeader header;
            SLANG_RETURN_ON_FAIL(read.read(header));

            void* dst = listOut.setSize(header.numEntries);
            SLANG_ASSERT(
                header.numCompressedEntries ==
                uint32_t((header.numEntries * typeSize) / sizeof(uint32_t)));

            // Decode..
            ByteEncodeUtil::decodeLiteUInt32(
                read.getData(),
                header.numCompressedEntries,
                (uint32_t*)dst);
            break;
        }
    case SerialCompressionType::None:
        {
            // Read uncompressed
            Bin::ArrayHeader header;
            SLANG_RETURN_ON_FAIL(read.read(header));
            const size_t payloadSize = header.numEntries * typeSize;
            SLANG_ASSERT(payloadSize == read.getRemainingSize());
            void* dst = listOut.setSize(header.numEntries);
            ::memcpy(dst, read.getData(), payloadSize);
            break;
        }
    }
    return SLANG_OK;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialParseUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

// clang-format off
#define SLANG_SERIAL_BINARY_COMPRESSION_TYPE(x) \
    x(None, none) \
    x(VariableByteLite, lite)
// clang-format on

/* static */ SlangResult SerialParseUtil::parseCompressionType(
    const UnownedStringSlice& text,
    SerialCompressionType& outType)
{
    struct Pair
    {
        UnownedStringSlice name;
        SerialCompressionType type;
    };

    // clang-format off
#define SLANG_SERIAL_BINARY_PAIR(type, name) \
    {UnownedStringSlice::fromLiteral(#name), SerialCompressionType::type},
    // clang-format on

    static const Pair s_pairs[] = {SLANG_SERIAL_BINARY_COMPRESSION_TYPE(SLANG_SERIAL_BINARY_PAIR)};

    for (const auto& pair : s_pairs)
    {
        if (pair.name == text)
        {
            outType = pair.type;
            return SLANG_OK;
        }
    }
    return SLANG_FAIL;
}

/* static */ UnownedStringSlice SerialParseUtil::getText(SerialCompressionType type)
{
#define SLANG_SERIAL_BINARY_CASE(type, name) \
    case SerialCompressionType::type:        \
        return UnownedStringSlice::fromLiteral(#name);
    switch (type)
    {
        SLANG_SERIAL_BINARY_COMPRESSION_TYPE(SLANG_SERIAL_BINARY_CASE)
    default:
        break;
    }
    SLANG_ASSERT(!"Unknown compression type");
    return UnownedStringSlice::fromLiteral("unknown");
}


} // namespace Slang