-
Notifications
You must be signed in to change notification settings - Fork 0
Element and bitbased Approach
Simple approach. Concept can be extended and evolved.
<?xml version="1.0" encoding="UTF-8"?>
<elements>
<element type="integer">
<length>
<bit>12</bit>
</length>
</element>
<element type="string">
<length>
<bit>16</bit>
</length>
</element>
</elements>An element defines the type and the length of the bits to be parsed. XML has to be validated after creation. Length has to be greater than zero.
The sequence of the data follows from the sequence of the elements. So, adhering to the provided example, the first 12 bits, or 1,5 bytes, are to be interpreted as signed integer with big endianess, the follwing 16 bits, or 2 bytes, illustrate a utf8 string.
<?xml version="1.0" encoding="UTF-8"?>
<element type="string">
<length padding-left="4" padding-right="4">
<bit>32</bit>
</length>
</element>Padding is used to tweak alignment of the data. ASCII for example encodes characters with 7 bits, using left or right padding, this can be stretched to a full byte, in order to achieve a concise design.
<?xml version="1.0" encoding="UTF-8"?>
<elements>
<element id="size" type="integer">
<length>
<bit>8</bit>
</length>
</element>
<element id="term" type="string">
<length>#size</length>
</element>
<element id="term-length" type="bytes">
<length padding-left="4" padding-right="4" />
<value>#term/length</value>
</element>
<element id="copy" type="bytes">
<value>#term/value</value>
</element>
</elements>Writing references only to determine the length of the follwing field can be a cumbersome, verbose and errorprone
task. It's easier to specify that the leading n bits should be interpreted as length field.
<?xml version="1.0" encoding="UTF-8"?>
<elements>
<element type="string">
<length preceding-length-field="8" padding-left="4" padding-right="4" />
</element>
<element type="string">
<length preceding-length-field="8" padding-left="4" padding-right="4">
<bit>32</bit>
</length>
</element>
</elements>The above elements are equal. If the preceding length field is enabled the body of the length element is ignored.
It is possible to create types that can be referenced and used in the element tags
<?xml version="1.0" encoding="UTF-8"?>
<types>
<type name="first-name">
<elements>
<element id="size" type="integer">
<length>
<bit>8</bit>
</length>
</element>
<element id="term" type="string">
<length>#size</length>
</element>
</elements>
</type>
</types>Mappings are useful to decrease the packet since, instead of sending the whole error message for example, a mapping could be created that links error messages to byte codes.
<element id="error" type="byte">
<length>
<bit>8</bit>
</length>
<mapping type="string">
<entry key="0xA1" value="Error #1: Whatch out, we have an error here!" />
<entry key="0xB1" value="Error #2: Whatch out, we have an error here!" />
<entry key="0xC1" value="Error #3: Whatch out, we have an error here!" />
<entry key="0xD1" value="Error #4: Whatch out, we have an error here!" />
</mapping>
</element><?xml version="1.0" encoding="UTF-8"?>
<unit name="get-group">
<complex-types>
<type name="person">
<elements>
<element id="first-name" type="string">
<length preceding-length-field="4" />
</element>
<element id="last-name" type="string">
<length preceding-length-field="4" />
</element>
</elements>
</type>
</complex-types>
<header>
<elements>
<element type="byte">
<value hex="DEADBEEF" />
</element>
</elements>
</header>
<body>
<elements>
<element classification="person">
<length preceding-length-field="8" />
</element>
</elements>
</body>
</unit>XML formatted with http://www.freeformatter.com/xml-formatter.html