H
H
h0x0d92014-05-29 12:06:24
C++ / C#
h0x0d9, 2014-05-29 12:06:24

Wrapper for raw data?

It is required to assemble / disassemble binary packages according to formal descriptions of formats.
Formal descriptions are stored by the Wrapper class.
Raw data from u_int8 *buffer is wrapped by the Wrapper class, which divides the buffer into fields, assigns a tag and field size to each field. The data structure for the Wrapper is given by a list of objects of the FieldDescription class that contain information about the fields (tag, size), and the order in the list determines the order in the Wrapper.

class Wrapper
{
    Wrapper(char *);
    void setStructure(vector<const FieldDescription *> description);
    vector<string> getTagOrder() const;
    void *getValue(string tag) const;
    size_t getSize(string tag) const;
    void setValue(string tag, void * dataPtr);
    map<string, void *> getFieldTagToValue() const;
    map<string, size_t> getFieldTagToSizes() const;     
};

To complete the picture, I will give an example of a user class. To interpret Wrapper fields, there is the Interpreter class, which, depending on the value of the interpreted field and other conditional fields, matches a text string.
class Interpreter
{
public:
    // интерпретация данных
    enum InterpretationType
    {
        PARAMLIST,
        NUMBER,
        STRING,
        IPv4,
        IPv6
    };
    void add(Tag_t tag, InterpretationType type);
    void add(Tag_t tag, map<ParamValue, ParamName_t> parmCode2Name);
    void add_if(Tag_t tag, map<ParamValue_t, ParamName_t> parmValue2Name, 
Tag_t conditionTag, ParamValue_t conditionValue);
    void add_if(Tag_t tag, map<ParamValue, ParamName_t> parmValue2Name, 
multiMap<Tag_t, ParamValue_t> conditionTags2Values);
    void add_if(Tag_t tag, InterpretationType type, multiMap<Tag_t, ParamValue_t> conditionTags2Values);
    map<Tag_t, string> convert(const Wrapper &prim) const;
};

In the future, I would like to write data from xml to buffer and read from the buffer to the same xml.
And so far there is the following problem: what if some data fields become a container type - they contain a list of values ​​of the same size for one tag?
Please comment on the solution.
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xandox, 2014-05-29
@h0x0d9

Your solution is very reminiscent of google protocol buffers, and as a result, if possible, I would advise not to reinvent the wheel, but to take a ready-made solution
to your question - see how it is done in protocol buffers ,
if briefly
there are three types of fields required, optional, repeated (required - we are not interested now), if optional or required is encountered several times in the incoming message, then the last of these values ​​for the field is taken, if the field is repeated, then all values ​​are added to the collection.
PS protocol buffers is a binary protocol, but it can also have a textual representation. I don’t really understand why in the 21st century someone might need xml? It has three original (in the sense of Google) implementations + a bunch of compatible ones for different languages.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question