Inhalt
Topic:.ZBNF_syntaxBin.
Written by Hartmut Schorrig, www.vishia.org. Since 2011-01-11, Latest edition 2014-08-03
Topic:.ZBNF_syntaxBin.motiv.
Binary data can be described in the programming language C with struct{...}
in headerfiles. This is usual. But the struct-description is only simple for fix data struct. If the assignment of data depends
on content in other data (cmd in header etc) then the description with struct
-constructs needs some additional explanation, which are non-formalistic.
The ZBNF-description for binary data should support
exact formalistic description manual used,
Conversion of binary data to a XML-representation,
Read binary data into a Java-program (or another program if the parser would be available for other languages) using the formalistic ZBNF-binary description.
The possibility of generating binary data from XML or inside a Java-program is not considered yet, but it seems able to do too.
Topic:.ZBNF_syntaxBin.syntax.
The syntax prescript should present how binary data are structured.
Topic:.ZBNF_syntaxBin.syntax..
The definition of a binary-component as sequence of bytes should be the same as normal ZBNF:
component::= ''syntax''.
The <@
is the basic expression to refer to binary data:
component::= <@ ...
The size of a part of binary data should be given as first element. If a constant size is given, such components can be translated
to a C-language struct
-construct. For variable size see later.
component::= <@SIZE=16> ...
Topic:.ZBNF_syntaxBin.syntax..
Any value at a given position with given number of bytes can be named. The name is the semanitic-meaning respectively the variable name in a C-language struct.
<@8+2?name1>
2 bytes, big endian at position 8, named name1
.
<@8-9?name1>
2 bytes from position 8 to inclusive position 9, big endian, named name1
.
<@8+2%?name1>
same, but little endian. It is possible to describe a mix of little and big-endian data, though it doesn't might suitable.
<@8+4:float?name2>
after the :
a C-language type can be written which describes the data. It is possible to use specific types, which should be known in
the evaluater of course. The float-format might be the standard-float-format of the most processors.
<@8-23:String0?name>
: It is a String at position 8 to 23. If the String contains less characters then the rest is filled with 0. Note that the
String can contain 16 Characters in this example. A 0 on end is not need (not like C-language).
<@8-head.nrofBytes:String04?name>
: It is a String which's end is 4-byte-aligned. The rest is filled with 0. The number of chars is calculated and store in
the field nrofBytes
in a head
element. It means the length of the String is variable. Note that the end is aligned, not the length of the String. The String
may start at an unaligned position.
<@8+4%float?name2>
: float
in little endian.
<@6+2[10..6]?name3>
: It means the bits 10..6 at the integer representation in the bytes 6 and 7, big-endian.
<@6+2[11]?name4>
: A single bit. It is the same like <@6+1[3]>
.
<@+2?element>
: This element is positioned after the last element. The position is a result of the previous elements.
The positions and length can be numeric expressions which uses the content of defined fields. Such expression are written
in parenthesis (expression)
:
<@0+2?nrofBytes> <@2-(nrofBytes-1)?array>
It defines one field at byte 0,1. That value is associated to the variable nrofBytes
. That variable is used to define any other position, in the case the rest of the data.
<@-(SIZE-1):String04?text>
: This element contains characters from the current position till the last byte. The end position is able to divide by 4 -
4 byte allignment. If the character sequence ends with 0, the 0-chars are removed before storing or evaluation. There are
the fill bytes to the 4-byte-alignment.
<@-SIZE-5_String04?text><@(SIZE-4)+4?numeric>
: It is an example for a String with 4-byte alignment, following by an integer value on end of the structure. Note that the
SIZE
should be determined on reading binary data by any other field. By writing binary data the length of the String may have
any desired length, it dertermines the SIZE
which's value is stored.
<@+strxyzLength:String04?text>
: This String with any desired length is 4-byte-aligned on its end. The length sets the variable strxyzLength
which's value can be used to set a field like <@0+2?len=(strxyzLength/2)>
.
..<@?element>: If the element has a known size, the size should not be given. The missing position means: after the previous element.
Topic:.ZBNF_syntaxBin.syntax..
It is possible to define a value or a range with a constant expression. On parsing of binary data it does only match if the value is proper:
<@8+2?variable=0x2e>
: should contain a value 0x2e
at position 8,9, named variable
.
<@8+2?=0x2e>
: same, a name of the field is not necessary.
<@8+2?cmd=0x7000 - 0x7fff>
: A range is given.
Topic:.ZBNF_syntaxBin.syntax..
It is possible to use the variables which are defined in the component to define the size:
component::=<@SIZE=(2*nrofWords)> <@0+2?nrofWords> ....
defines a binary component which contains an integer value in big endian at position 0, 2 bytes. The value of that integer
is named 'nrofWords'. The byte-size of that component is the value of nrofWords * 2
.
The SIZE
can be used as variable in other expressions, for example:
... <@10-(SIZE-1)?theRestOfData>.
This component is a byte-sequence with a variable size. From position 10 to its end the content is named theRestOfData
.
Topic:.ZBNF_syntaxBin.syntax..
It ends with a dot, like in normal ZBNF:
component::=<@SIZE=nrofChars+6> <@0+2?nrofChars> <@2-5?encoding> <@6-(SIZE-1)?text> .
This is a complete simple component with a variable size starting with 2-byte-integer which contains a number of character,
then a 4-byte-integer, then the rest from 6 to end contains the text. The SIZE
is determined by a calculation with the content of position <@0+2...>
.
Topic:.ZBNF_syntaxBin.syntax..
The following syntax describes a superior component, which contains some binary component:
superiorCompn::= <@component> <@otherComp> <@otherCompText>.
On using a child component it can be defined which value or range has its elements:
superiorCompn::= <@component !cmd=0x14 !nrofBytes=8> <@....
The superiorCompn
matches to given data only if the field cmd
, defined in the child component
has the value 0x14
. The !
is the key symbol after them follows a name of a field of that component.
Topic:.ZBNF_syntaxBin.syntax..
A component may contain text-bytes. It is designated with !textSyntax
. The text-bytes (ASCII-character) are described with the known ZBNF-syntax for textual representation.
component::=<@SIZE=nrofChars+6> <@0+2?nrofChars> <@2-5?encoding> <@6-(SIZE-1)?!textsyntax> . textsyntax::=<$?name>=<*?value>.
Topic:.ZBNF_syntaxBin..
In an XML-representation after parsing any data of this example-syntax may be presented in form of
<semantic @nrofBytes="254" @name1="345" @name2="3.145678" @name3="5"> <otherComp itsElements="value">...</otherComp> <text itsAttrComp="value" > <textCompIntern>... </textCompIntern> </text> </semantic>
Topic:.ZBNF_syntaxBin..
The conversion of a XML-representation back to the binary data should start on the top-level syntax-component. It should detect the proper component with the given semantic. The syntax isn't contained in the XML-tree, only the semantic. Therefore a back transformation is not easy to do. Possibly a Xschema-notation may helpfull. Xschema describes the formal syntax of a XML-tree in form of admissible arrangements. The Xschema-definition should name the component-identifier (syntax name). Then a back-transformation may be able to do.
The conversion of a XML-representation back to the binary data should start on the top-level syntax-component. It should detect the proper component with the given semantic. The syntax isn't contained in the XML-tree, only the semantic. Therefore a back transformation is not easy to do. Possibly a Xschema-notation may helpfull. Xschema describes the formal syntax of a XML-tree in form of admissible arrangements. The Xschema-definition should name the component-identifier (syntax name). Then a back-transformation may be able to do.