You are here: Elements > Decoder Script Reference > BRANCH

BRANCH

The BRANCH statement allows transfer of control to a given FIELD, GROUP or GROUP FIELD. The format is either:

BRANCH (BranchMethod)

where BranchMethod is a method that resolves to the name of a field or group,

or

BRANCH IF (BooleanMethod) yes_field no_field

where control goes to the yes_field if the condition is true and the no_field if the condition is not true. The yes_field and no_field items may be field names or group names.

The name "branch" may be slightly misleading. The effect is more like that of a GOSUB in BASIC or an "execute" command in certain languages. The statement that is the target of the branch is performed and then control returns to the statement following the BRANCH. If the target is a FIELD then that single statement is performed. If the target is a GROUP or GROUP FIELD then that statement along with any enclosed within its brace block are performed.

BRANCH is used in combination with a Branch Method. As an example, consider a protocol that carries a large set of commands, each of which needs its own set of decoder statements. Then we can put the names of the GROUPs that process the commands in a table and call them as in this example:

BRANCH (FromTable command_table command_code)

In this case, the value in the field named command_code is looked up from the table named command_table and uses the branch item in the matching table entry as the target for the BRANCH. If an entry without a branch item is referenced in this way then the BRANCH becomes a no-operation ‐ no branching is done and processing continues with the next statement in line.

BRANCH IF is used in conjunction with a Boolean Method to determine which of two field/group statements to decode. For example, assume that the contents of a field defined whether the bytes that follow are a command or a data stream. If the field contains a zero, a data stream follows, and if the field contains a number greater than zero, the number is the number of the command. Our branch statement would look like this:

DECODE

FIELD data_or_command (Fixed 1) (Decimal) SUPPRESS_DETAIL

 

BRANCH IF (FieldIs EqualTo 0 data_or_command) data command

 

END_MAIN_PATH

 

FIELD data (ToEndOfLayer 0) (StringOfHex 24) "Data"

 

GROUP command

{

[command fields]

}

 

In this situation, if the data_or_command field contains a zero, the data field will be decoded, otherwise the command group will be decoded.