Written by Hartmut Schorrig, www.vishia.org. Since 2011-01-11, Latest edition 2014-08-03

1 Motivation


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

The possibility of generating binary data from XML or inside a Java-program is not considered yet, but it seems able to do too.

2 Notation as syntax prescript


The syntax prescript should present how binary data are structured.

2.1 Basic notation


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> ...

2.2 fields and position


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.

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.

2.3 Definition of values or ranges for variables


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:

2.4 The SIZE may variable often


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.

2.5 A simple component


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...>.

2.6 Build a component with other components


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.

2.7 Presentation of text content with normal ZBNF


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> .

3 Transformation of binary data to XML


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" >

4 Transformation from XML to binary data ?


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.