kaitai-struct

Question: How to write `010 switch and if` in kaitai


I like the kaitai struct very much that I need to rewrite one of 010 binary template in kaitai. I've found structures similar to the following in my 010 template and I would like to know how to handle this in kaitai.

Ex:1

typedef struct
{
    WORD MCC_length;
    if(MCC_length>3)
    {
        UBYTE MCC[3];
    }
    else if(MCC_length>0)
    {
        UBYTE MCC[MCC_length];
    }
    else if(MCC_length==0)
    {
        UBYTE MCC[3];
    }
    
       WORD MNC_length;
    if(MNC_length>3)
    {
        UBYTE MNC[3];
    }
    else if(MNC_length>0)
    {
        UBYTE MNC[MNC_length];
    }   
    else if(MNC_length==0)
    {
        UBYTE MNC[3];
    }
}PLMN_Format_2;

Ex:2

typedef struct
{
    UBYTE M_ID;
    UBYTE Cell_1;
    UBYTE Cell_2;
    UBYTE Cell_3;
    UBYTE T_Count;
    if(T_Count>0)
    {
        INTER_MR_Cell cell_info[T_Count]<optimize=false>;
    }
    UBYTE Location_ID;
    if(Location_ID == 1)
    {
        UBYTE ShapeID;
        switch(ShapeID)
        {
            case 0:
            {
                UNDEFINED_INFO Undefined_Info;
                break;
            }
            case 1:
            {
                Type_Point_Info1 Point_Info1;
                break;
            }
            case 2:
            {
                Type_Point_Info2 Point_Info2;
                break;
            }
            case 3:
            {
                Type_Point_Info3 Point_Info3;
                break;
            }
            case 4:
            {
                Type_Loc_Info1 Loc_Info1;
                break;
            }
            case 5:
            {
                Type_Loc_Info2 Loc_Info2;
                break;
            }
        }
    }
}PERIOD_MDT;

Solution

  • I've asked the same question in kaitai git, and they provided well detailed explanation on how to do this. Please refer this