REPEAT COUNT | SIZE | TRUNCATED_FIELD_OK | UNTIL Method

The optional REPEAT clause is used with a qualifying keyword, COUNT, SIZE or UNTIL plus an appropriate method.

REPEAT COUNT is used when the group is to be repeated a known number of times. A Size Method is used to indicate the number as in:

GROUP seven_things REPEAT COUNT (Fixed 7 Times) "Seven things"

Be careful here because the value returned by a sizing method is a number of bits. If you wrote instead:

GROUP seven_things REPEAT COUNT (Fixed 7) "Seven things"

then the GROUP would be repeated 56 times! (That's because the default unit for the Fixed method is bytes.) The best thing to do is always use the "Times" for the units in Repeat Count clauses.

If the repeat count is the value of a field then use the FromField method as in:

GROUP n_things REPEAT COUNT (FromField Times thing_count 0) "Things"

REPEAT SIZE is used when a group is repeated an unknown number of times to process a known amount of data. A Size Method is used to define the span of the repeated group. Suppose, for example, we have a protocol in which a layer can contain an arbitrary number of variable-length commands terminated by a one-byte checksum. Then you would use something like:

GROUP central_command REPEAT SIZE (ToEndOfLayer 1)

ToEndOfLayer returns the number of bits left up to the end of the layer minus the number of bytes given as the parameter. So in this case it will return the number of bits left excluding a one-byte checksum. The GROUP is then repeated until this number of bits has been processed.

When processing REPEAT SIZE, you can think of the number of bytes inside the repeat loop as a self-contained sub-layer. If you try to access bits past the end of that layer, then an error will be reported in the decode. This is usually a sign that either your decoder is faulty or that the frame that was passed in was damaged. In either case, the error is a useful indication. It is possible to imagine a case, however, where it is not desirable to display an error if the group is truncated. The optional keyword, TRUNCATED_FIELD_OK, suppresses that error.

REPEAT UNTIL is used in combination with a Boolean method. Here is an example from the HTTP decoder:

GROUP headers REPEAT UNTIL (CurrentDataMatches 16 0x0d0a)

The CurrentDataMatches method checks for a given value at the pointer. This particular group repeats until a 16- bit quantity matches hexadecimal 0D0A (that is a CR+LF in ASCII).