Slide 9c: Bitmap Parsing State Machine
// ParseBitmaps reads only what the control bits demand.
func ParseBitmaps(input []byte, isHex bool, supportTertiary bool) (primary, secondary, tertiary []byte, consumed int, err error) {
const (
BIN_LEN = 8
HEX_LEN = 16
)
baseLen := BIN_LEN
if isHex { baseLen = HEX_LEN }
if len(input) < baseLen {
return nil, nil, nil, 0, ErrBitmapTooShort
}
// Decode primary bitmap from input[:baseLen]
primary = append([]byte(nil), input[:baseLen]...)
consumed = baseLen
// If primary bit 1 is set, decode secondary
if IsFieldPresent(primary, 1) {
if len(input) < consumed+baseLen {
return nil, nil, nil, 0, ErrBitmapTooShort
}
secondary = append([]byte(nil), input[consumed:consumed+baseLen]...)
consumed += baseLen
// If secondary bit 1 is set, decode tertiary (tertiary support)
if supportTertiary && IsFieldPresent(secondary, 1) {
if len(input) < consumed+baseLen {
return nil, nil, nil, 0, ErrBitmapTooShort
}
tertiary = append([]byte(nil), input[consumed:consumed+baseLen]...)
consumed += baseLen
}
}
return primary, secondary, tertiary, consumed, nil
}
The rule is simple:
- control bits decide how many bitmap bytes exist;
- field bytes are never decoded until all required bitmap bytes are validated.