SerializeTxt.h 1.56 KB
Newer Older
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
/* Copyright 2015 the SumatraPDF project authors (see AUTHORS file).
   License: Simplified BSD (see COPYING.BSD) */

struct TxtNode;

namespace sertxt {

struct FieldMetadata;

typedef struct {
    uint16_t                size;
    uint16_t                nFields;
    const char *            fieldNames;
    const FieldMetadata *   fields;
} StructMetadata;

typedef enum {
    TYPE_BOOL,
    TYPE_I16,
    TYPE_U16,
    TYPE_I32,
    TYPE_U32,
    TYPE_U64,
    TYPE_FLOAT,
    TYPE_COLOR,
    TYPE_STR,
    TYPE_WSTR,
    TYPE_STRUCT_PTR,
    TYPE_ARRAY,
    // do && with TYPE_MASK to get just the type, no flags
    TYPE_MASK = 0xFF,
    // a flag, if set the value is not to be serialized
    TYPE_NO_STORE_MASK = 0x4000,
    // a flag, if set the value is serialized in a compact form
    TYPE_STORE_COMPACT_MASK = 0x8000,
} Type;

// TODO: re-arrange fields for max compactness
// information about a single field
struct FieldMetadata {
    // offset of the value from the beginning of the struct
    uint16_t         offset;
    Type             type;
    // StructMetadata * for TYP_ARRAY and TYPE_STRUCT_PT
    // otherwise default value for this field
    uintptr_t        defValOrDefinition;
};

uint8_t *   Serialize(const uint8_t *data,  const StructMetadata *def, size_t *sizeOut);
uint8_t*    Deserialize(struct TxtNode *root, const StructMetadata *def);
uint8_t*    Deserialize(char *data, size_t dataSize, const StructMetadata *def);
void        FreeStruct(uint8_t *data, const StructMetadata *def);

} // namespace sertxt