summaryrefslogtreecommitdiff
path: root/source/slang/slang-fossil.cpp
blob: e3b2e2c9d7edb4def93172e11b76c1f2ab8c8cb0 (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
// slang-fossil.cpp
#include "slang-fossil.h"

namespace Slang
{
namespace Fossil
{

const char Fossil::Header::kMagic[16] = {
    '\xAB', // byte 0
    'f',    // byte 1
    'o',    // byte 2
    's',    // byte 3
    's',    // byte 4
    'i',    // byte 5
    'l',    // byte 6
    ' ',    // byte 7
    '1',    // byte 8
    '0',    // byte 9
    '0',    // byte 10
    '\xBB', // byte 11
    '\r',   // byte 12
    '\n',   // byte 13
    '\x1A', // byte 14
    '\n'    // byte 15
};

FossilizedValRef getRootValue(ISlangBlob* blob)
{
    return getRootValue(blob->getBufferPointer(), blob->getBufferSize());
}

FossilizedValRef getRootValue(void const* data, Size size)
{
    if (!data)
    {
        SLANG_UNEXPECTED("bad format for fossil");
    }

    // There must be enough data to at least hold the header.
    //
    // (In practice there would need to be more data than
    // just the header, but checking this invariant is a start).
    //
    if (size < sizeof(Fossil::Header))
    {
        SLANG_UNEXPECTED("bad format for fossil");
    }

    // Once we've checked that there's enough data, we can read
    // the contents of the header.
    //
    auto header = reinterpret_cast<Fossil::Header const*>(data);

    // The "magic" bytes at the start of the header must be
    // what we expect (which is the contents of `Fossil::Header::kMagic`).
    //
    if (memcmp(header->magic, Fossil::Header::kMagic, sizeof(Fossil::Header::kMagic)) != 0)
    {
        SLANG_UNEXPECTED("bad format for fossil");
    }

    auto reportedSize = header->totalSizeIncludingHeader;
    if (reportedSize > size)
    {
        SLANG_UNEXPECTED("bad format for fossil");
    }

    auto rootValueVariant = header->rootValue.get();
    if (!rootValueVariant)
    {
        SLANG_UNEXPECTED("bad format for fossil");
    }

    return FossilizedValRef(
        rootValueVariant->getContentData(),
        rootValueVariant->getContentLayout());
}

} // namespace Fossil

Size FossilizedStringObj::getSize() const
{
    auto sizePtr = (FossilUInt*)this - 1;
    return Size(*sizePtr);
}

UnownedTerminatedStringSlice FossilizedStringObj::getValue() const
{
    auto size = getSize();
    return UnownedTerminatedStringSlice((char*)this, size);
}

Count FossilizedContainerObj::getElementCount() const
{
    auto countPtr = (FossilUInt*)this - 1;
    return Size(*countPtr);
}

FossilizedValLayout* FossilizedVariantObj::getContentLayout() const
{
    auto layoutPtrPtr = (FossilizedPtr<FossilizedValLayout>*)this - 1;
    return (*layoutPtrPtr).get();
}

FossilizedValRef getPtrTarget(FossilizedPtrValRef ptrRef)
{
    auto ptrLayout = ptrRef.getLayout();
    auto ptrPtr = ptrRef.getData();
    return FossilizedValRef(ptrPtr->getTargetData(), ptrLayout->elementLayout);
}

bool hasValue(FossilizedOptionalObjRef optionalRef)
{
    return optionalRef.getData() != nullptr;
}

FossilizedValRef getValue(FossilizedOptionalObjRef optionalRef)
{
    auto optionalLayout = optionalRef.getLayout();
    auto valuePtr = optionalRef.getData();
    return FossilizedValRef(valuePtr, optionalLayout->elementLayout);
}

Count getElementCount(FossilizedContainerObjRef containerRef)
{
    if (!containerRef)
        return 0;

    auto containerPtr = containerRef.getData();
    return containerPtr->getElementCount();
}

FossilizedValRef getElement(FossilizedContainerObjRef containerRef, Index index)
{
    SLANG_ASSERT(index >= 0);
    SLANG_ASSERT(index < getElementCount(containerRef));

    auto containerLayout = containerRef.getLayout();
    auto elementLayout = containerLayout->elementLayout.get();
    auto elementStride = containerLayout->elementStride;

    auto elementsPtr = (Byte*)containerRef.getData();
    auto elementPtr = (FossilizedVal*)(elementsPtr + elementStride * index);
    return FossilizedValRef(elementPtr, elementLayout);
}

Count getFieldCount(FossilizedRecordValRef recordRef)
{
    auto recordLayout = recordRef.getLayout();
    return recordLayout->fieldCount;
}

FossilizedRecordElementLayout* FossilizedRecordLayout::getField(Index index) const
{
    SLANG_ASSERT(index >= 0);
    SLANG_ASSERT(index < fieldCount);

    auto fieldsPtr = (FossilizedRecordElementLayout*)(this + 1);
    return fieldsPtr + index;
}


FossilizedValRef getField(FossilizedRecordValRef recordRef, Index index)
{
    SLANG_ASSERT(index >= 0);
    SLANG_ASSERT(index < getFieldCount(recordRef));

    auto recordLayout = recordRef.getLayout();
    auto field = recordLayout->getField(index);

    auto fieldsPtr = (Byte*)recordRef.getData();
    auto fieldPtr = (FossilizedVal*)(fieldsPtr + field->offset);
    return FossilizedValRef(fieldPtr, field->layout);
}

FossilizedValRef getVariantContent(FossilizedVariantObjRef variantRef)
{
    return getVariantContent(variantRef.getData());
}

FossilizedValRef getVariantContent(FossilizedVariantObj* variantPtr)
{
    return FossilizedValRef(variantPtr->getContentData(), variantPtr->getContentLayout());
}

} // namespace Slang