| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef TNT_FILAMAT_FLATENNER_H |
| 18 |
#define TNT_FILAMAT_FLATENNER_H |
| 19 |
|
| 20 |
#include <utils/Panic.h> |
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
#include <map> |
| 24 |
#include <stdint.h> |
| 25 |
#include <string.h> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "filamat/Package.h" |
| 29 |
|
| 30 |
using namespace utils; |
| 31 |
|
| 32 |
namespace filamat { |
| 33 |
|
| 34 |
class Flattener { |
| 35 |
public: |
| 36 |
Flattener(Package& package) : Flattener(package.getData()) {} |
| 37 |
Flattener(uint8_t* dst) : mCursor(dst), mStart(dst){} |
| 38 |
|
| 39 |
static Flattener& getDryRunner() { |
| 40 |
static Flattener dryRunner = Flattener(nullptr); |
| 41 |
dryRunner.mStart = nullptr; |
| 42 |
dryRunner.mCursor = nullptr; |
| 43 |
dryRunner.mOffsetPlaceholders.clear(); |
| 44 |
dryRunner.mSizePlaceholders.clear(); |
| 45 |
dryRunner.mValuePlaceholders.clear(); |
| 46 |
return dryRunner; |
| 47 |
} |
| 48 |
|
| 49 |
bool isDryRunner() { return mStart == nullptr;} |
| 50 |
|
| 51 |
size_t getBytesWritten() { |
| 52 |
return mCursor - mStart; |
| 53 |
} |
| 54 |
|
| 55 |
void writeBool(bool b) { |
| 56 |
if (mStart != nullptr) { |
| 57 |
mCursor[0] = static_cast<uint8_t>(b); |
| 58 |
} |
| 59 |
mCursor += 1; |
| 60 |
} |
| 61 |
|
| 62 |
void writeUint8(uint8_t i) { |
| 63 |
if (mStart != nullptr) { |
| 64 |
mCursor[0] = i; |
| 65 |
} |
| 66 |
mCursor += 1; |
| 67 |
} |
| 68 |
|
| 69 |
void writeUint16(uint16_t i) { |
| 70 |
if (mStart != nullptr) { |
| 71 |
mCursor[0] = static_cast<uint8_t>( i & 0xff); |
| 72 |
mCursor[1] = static_cast<uint8_t>((i >> 8) & 0xff); |
| 73 |
} |
| 74 |
mCursor += 2; |
| 75 |
} |
| 76 |
|
| 77 |
void writeUint32(uint32_t i) { |
| 78 |
if (mStart != nullptr) { |
| 79 |
mCursor[0] = static_cast<uint8_t>( i & 0xff); |
| 80 |
mCursor[1] = static_cast<uint8_t>((i >> 8) & 0xff); |
| 81 |
mCursor[2] = static_cast<uint8_t>((i >> 16) & 0xff); |
| 82 |
mCursor[3] = static_cast<uint8_t>( i >> 24); |
| 83 |
} |
| 84 |
mCursor += 4; |
| 85 |
} |
| 86 |
|
| 87 |
void writeUint64(uint64_t i) { |
| 88 |
if (mStart != nullptr) { |
| 89 |
mCursor[0] = static_cast<uint8_t>( i & 0xff); |
| 90 |
mCursor[1] = static_cast<uint8_t>((i >> 8) & 0xff); |
| 91 |
mCursor[2] = static_cast<uint8_t>((i >> 16) & 0xff); |
| 92 |
mCursor[3] = static_cast<uint8_t>((i >> 24) & 0xff); |
| 93 |
mCursor[4] = static_cast<uint8_t>((i >> 32) & 0xff); |
| 94 |
mCursor[5] = static_cast<uint8_t>((i >> 40) & 0xff); |
| 95 |
mCursor[6] = static_cast<uint8_t>((i >> 48) & 0xff); |
| 96 |
mCursor[7] = static_cast<uint8_t>((i >> 56) & 0xff); |
| 97 |
} |
| 98 |
mCursor += 8; |
| 99 |
} |
| 100 |
|
| 101 |
void writeString(const char* str) { |
| 102 |
size_t len = strlen(str); |
| 103 |
if (mStart != nullptr) { |
| 104 |
strcpy(reinterpret_cast<char*>(mCursor), str); |
| 105 |
} |
| 106 |
mCursor += len + 1; |
| 107 |
} |
| 108 |
|
| 109 |
void writeBlob(const char* blob, size_t nbytes) { |
| 110 |
writeUint64(nbytes); |
| 111 |
if (mStart != nullptr) { |
| 112 |
memcpy(reinterpret_cast<char*>(mCursor), blob, nbytes); |
| 113 |
} |
| 114 |
mCursor += nbytes; |
| 115 |
} |
| 116 |
|
| 117 |
void writeSizePlaceholder() { |
| 118 |
mSizePlaceholders.push_back(mCursor); |
| 119 |
if (mStart != nullptr) { |
| 120 |
mCursor[0] = 0; |
| 121 |
mCursor[1] = 0; |
| 122 |
mCursor[2] = 0; |
| 123 |
mCursor[3] = 0; |
| 124 |
} |
| 125 |
mCursor += 4; |
| 126 |
} |
| 127 |
|
| 128 |
uint32_t writeSize() { |
| 129 |
assert(mSizePlaceholders.size() > 0); |
| 130 |
|
| 131 |
uint8_t* dst = mSizePlaceholders.back(); |
| 132 |
mSizePlaceholders.pop_back(); |
| 133 |
|
| 134 |
uint32_t size = static_cast<uint32_t>(mCursor - dst - 4); |
| 135 |
if (mStart != nullptr) { |
| 136 |
dst[0] = static_cast<uint8_t>( size & 0xff); |
| 137 |
dst[1] = static_cast<uint8_t>((size >> 8) & 0xff); |
| 138 |
dst[2] = static_cast<uint8_t>((size >> 16) & 0xff); |
| 139 |
dst[3] = static_cast<uint8_t>((size >> 24)); |
| 140 |
} |
| 141 |
return size; |
| 142 |
} |
| 143 |
|
| 144 |
void writeOffsetplaceholder(size_t index) { |
| 145 |
mOffsetPlaceholders.insert(std::pair<size_t, uint8_t*>(index, mCursor)); |
| 146 |
if (mStart != nullptr) { |
| 147 |
mCursor[0] = 0x0; |
| 148 |
mCursor[1] = 0x0; |
| 149 |
mCursor[2] = 0x0; |
| 150 |
mCursor[3] = 0x0; |
| 151 |
} |
| 152 |
mCursor += 4; |
| 153 |
} |
| 154 |
|
| 155 |
void writeOffsets(uint32_t forIndex) { |
| 156 |
if (mStart == nullptr) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
for(auto pair : mOffsetPlaceholders) { |
| 161 |
size_t index = pair.first; |
| 162 |
if (index != forIndex) { |
| 163 |
continue; |
| 164 |
} |
| 165 |
uint8_t* dst = pair.second; |
| 166 |
size_t offset = mCursor - mOffsetsBase; |
| 167 |
if (offset > UINT32_MAX) { |
| 168 |
slog.e << "Unable to write offset greater than UINT32_MAX." << io::endl; |
| 169 |
exit(0); |
| 170 |
} |
| 171 |
dst[0] = static_cast<uint8_t>( offset & 0xff); |
| 172 |
dst[1] = static_cast<uint8_t>((offset >> 8) & 0xff); |
| 173 |
dst[2] = static_cast<uint8_t>((offset >> 16) & 0xff); |
| 174 |
dst[3] = static_cast<uint8_t>((offset >> 24)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void writeValuePlaceholder() { |
| 179 |
mValuePlaceholders.push_back(mCursor); |
| 180 |
if (mStart != nullptr) { |
| 181 |
mCursor[0] = 0; |
| 182 |
mCursor[1] = 0; |
| 183 |
mCursor[2] = 0; |
| 184 |
mCursor[3] = 0; |
| 185 |
} |
| 186 |
mCursor += 4; |
| 187 |
} |
| 188 |
|
| 189 |
void writePlaceHoldValue(size_t v) { |
| 190 |
assert(mValuePlaceholders.size() > 0); |
| 191 |
|
| 192 |
if (v > UINT32_MAX) { |
| 193 |
slog.e << "Unable to write value greater than UINT32_MAX." << io::endl; |
| 194 |
exit(0); |
| 195 |
} |
| 196 |
|
| 197 |
uint8_t* dst = mValuePlaceholders.back(); |
| 198 |
mValuePlaceholders.pop_back(); |
| 199 |
if (mStart != nullptr) { |
| 200 |
dst[0] = static_cast<uint8_t>( v & 0xff); |
| 201 |
dst[1] = static_cast<uint8_t>((v >> 8) & 0xff); |
| 202 |
dst[2] = static_cast<uint8_t>((v >> 16) & 0xff); |
| 203 |
dst[3] = static_cast<uint8_t>((v >> 24)); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
void resetOffsets() { |
| 208 |
mOffsetPlaceholders.clear(); |
| 209 |
} |
| 210 |
|
| 211 |
void markOffsetBase() { |
| 212 |
mOffsetsBase = mCursor; |
| 213 |
} |
| 214 |
|
| 215 |
private: |
| 216 |
uint8_t* mCursor; |
| 217 |
uint8_t* mStart; |
| 218 |
std::vector<uint8_t*> mSizePlaceholders; |
| 219 |
std::multimap<size_t, uint8_t*> mOffsetPlaceholders; |
| 220 |
std::vector<uint8_t*> mValuePlaceholders; |
| 221 |
uint8_t* mOffsetsBase = nullptr; |
| 222 |
}; |
| 223 |
|
| 224 |
} |
| 225 |
#endif |