Repository: mandiant/capa
File: capa/features/extractors/dnfile/helpers.py (Python)

Code before:
            typedef_name.append(name)
            index = nested_class_table[index]

        # Document the root enclosing details
        table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeDef.number, nested_class_table[index])
        if table_row is None:
            return typedef.TypeNamespace, tuple(typedef_name[::-1])

        enclosing_name = table_row.TypeName
        typedef_name.append(enclosing_name)

        return table_row.TypeNamespace, tuple(typedef_name[::-1])

    else:
        return typedef.TypeNamespace, (typedef.TypeName,)


def resolve_nested_typeref_helper(index: int, typeref: dnfile.mdtable.TypeDefRow, pe: dnfile.dnPE):
    """Resolves all nested TypeRef class names. Returns the namespace as a str and the nested TypeRef name as a tuple"""
    # If the ResolutionScope decodes to a typeRef type then it is nested
    if isinstance(typeref.ResolutionScope.table, dnfile.mdtable.TypeRef):
        typeref_name = []
        name = typeref.TypeName
        # Not appending the current typeref name to avoid potential duplicate

        # Validate index
        table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeRef.number, index)
        if table_row is None:
            return typeref.TypeNamespace, (typeref.TypeName,)

        while isinstance(table_row.ResolutionScope.table, dnfile.mdtable.TypeRef):
            # Iterate through the typeref table to resolve the nested name
            typeref_name.append(name)
            name = table_row.TypeName
            table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeRef.number, table_row.ResolutionScope.row_index)
            if table_row is None:
                return typeref.TypeNamespace, tuple(typeref_name[::-1])

        # Document the root enclosing details
        typeref_name.append(table_row.TypeName)

        return table_row.TypeNamespace, tuple(typeref_name[::-1])

    else:
        return typeref.TypeNamespace, (typeref.TypeName,)


def enclosing_and_nested_classes_index_table(pe: dnfile.dnPE):
    """Build index for EnclosingClass based off the NestedClass row index in the nestedclass table"""
    nested_class_table = {}


Review comment (refactor):
```suggestion
        # Not appending the current TypeRef name to avoid potential duplicate
```

Code after:
            typedef_name.append(name)
            index = nested_class_table[index]

        # Document the root enclosing details
        table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeDef.number, nested_class_table[index])
        if table_row is None:
            return typedef.TypeNamespace, tuple(typedef_name[::-1])

        enclosing_name = table_row.TypeName
        typedef_name.append(enclosing_name)

        return table_row.TypeNamespace, tuple(typedef_name[::-1])

    else:
        return typedef.TypeNamespace, (typedef.TypeName,)


def resolve_nested_typeref_name(
    index: int, typeref: dnfile.mdtable.TypeRefRow, pe: dnfile.dnPE
) -> Tuple[str, Tuple[str, ...]]:
    """Resolves all nested TypeRef class names. Returns the namespace as a str and the nested TypeRef name as a tuple"""
    # If the ResolutionScope decodes to a typeRef type then it is nested
    if isinstance(typeref.ResolutionScope.table, dnfile.mdtable.TypeRef):
        typeref_name = []
        name = typeref.TypeName
        # Not appending the current typeref name to avoid potential duplicate

        # Validate index
        table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeRef.number, index)
        if table_row is None:
            return typeref.TypeNamespace, (typeref.TypeName,)

        while isinstance(table_row.ResolutionScope.table, dnfile.mdtable.TypeRef):
            # Iterate through the typeref table to resolve the nested name
            typeref_name.append(name)
            name = table_row.TypeName
            table_row = get_dotnet_table_row(pe, dnfile.mdtable.TypeRef.number, table_row.ResolutionScope.row_index)
            if table_row is None:
                return typeref.TypeNamespace, tuple(typeref_name[::-1])

        # Document the root enclosing details
        typeref_name.append(table_row.TypeName)

        return table_row.TypeNamespace, tuple(typeref_name[::-1])

    else:
        return typeref.TypeNamespace, (typeref.TypeName,)


def get_dotnet_nested_class_table_index(pe: dnfile.dnPE) -> Dict[int, int]:
    """Build index for EnclosingClass based off the NestedClass row index in the nestedclass table"""