{{
import malcat


def pval(key, val=None, fmt="{}", va=False, fa=False, rva=False, kalign=30, valign=0, formated=False):
    r = ""
    if key:
        r = "[normal]{}[/normal]:".format(key)
        lr = len(r) - 17
        if lr < kalign:
            r += (" "*(kalign-lr))
    if isinstance(val, malcat.StructAccess):
        if val.has_enum and val.enum:
            d = "[color6]{}[/color6]([dim]{}[/dim])".format(val.enum, fmt.format(val.value))
            formated = True
            ld = len(d) - len("[color3][/color3][dim][/dim]")
        else:
            d = fmt.format(val.value)
            ld = len(d)
        val = val.value
    else:
        d = fmt.format(val)
        ld = len(d)
    if not formated:
        if type(val) == int: 
            if val == 0:
                grp = "dim"
            elif fa:
                grp = "fa"
            elif rva:
                grp = "rva"
            elif va:
                grp = "va"
            else:
                grp = "normal"
        else:
            grp = "normal"
        d = "[{}]{}[/{}]".format(grp,d,grp)
    if ld < valign:
        d += (" "*(valign-ld))
    write(r+d)  

def bitfieldpp(fa):
    s = []
    for i in range(fa.count - 1, -1, -1):
        bit = fa.at(i)
        if bit.value:
            s.append("[color5]{}[/color5]([dim]{:X}[/dim])".format(bit.name, 1 << i))
    if s:
        return  " + ".join(s)
    else:
        return "[dim]0[/dim]"

def pbitfield(key, fa, kalign=30):    
    r = "[normal]{}[/normal]:".format(key)
    lr = len(r) - 17
    if lr < kalign:
        r += (" "*(kalign-lr))
    write(r + bitfieldpp(fa))

def sections_rights(rights):
    s = ""
    for e in [
        ("MemRead", "R"),
        ("MemWrite", "W"),
        ("MemExecute", "X"),
        ("ScnAlign1", "1"),
        ("ScnAlign2", "2"),
        ("ScnAlign8", "8"),
        ("MemShared", "S"),
        ("MemNotPaged", "P"),
        ("MemNotCached", "H"),
        ("MemDiscardable", "D"),
        ("SctCntCode", "C"),
        ("SctCntInitializedData", "I"),
        ("SctCntUnititializedData", "U"),
        ]:
        if rights[e[0]]:
            s += "[color5]{}[/color5]".format(e[1])
        else:
            s += "[dim]∙[/dim]"
    return s
}}
[color1]Header[/color1] 
  {{ pval("Version", analysis.struct.FIB.Version, "{:04x}", valign=24) }} {{ pval("Product", analysis.struct.FIB.Product, "{:04x}") }}
  {{ pval("Lid", analysis.struct.FIB.Lid, valign=24) }} {{ pval("LidFE", analysis.struct.FIB.LidFE) }}
  {{ pval("Environment", analysis.struct.FIB.Environment, valign=24) }} {{ pval("User text offset", analysis.struct.FIB.Min, "{:08x}", fa=True) }}
  {{ pval("MagicCreated", analysis.struct.FIB.MagicCreated, "{:08x}", valign=24) }} {{ pval("MagicRevised", analysis.struct.FIB.MagicRevised, "{:08x}") }}
  {{ pbitfield("Flags", analysis.struct.FIB.Flags) }}
  {{ pbitfield("Save Flags", analysis.struct.FIB.SaveFlags) }}

[color1]FC/LCB pairs[/color1] 
{{ for i in range(53, 53 +  analysis.struct["FIB"]["NumberOfFcbPairs"] // 2, 2): }}
  {{ pval(analysis.struct.FIB.at(i).name, analysis.struct.FIB[i], "{:x}", valign=24) }} {{ pval(analysis.struct.FIB.at(i+1).name, analysis.struct.FIB[i+1], "{:x}") }}
{{:endfor}}
