Repository: mandiant/capa
File: capa/main.py (Python)

Code before:
    backend: str,
    sigpaths: List[str],
    should_save_workspace=False,
    disable_progress=False,
) -> FeatureExtractor:
    """
    raises:
      UnsupportedFormatError
      UnsupportedArchError
      UnsupportedOSError
    """

    if format_ not in (FORMAT_SC32, FORMAT_SC64, FORMAT_CAPE):
        if not is_supported_format(path):
            raise UnsupportedFormatError()

        if not is_supported_arch(path):
            raise UnsupportedArchError()

        if os_ == OS_AUTO and not is_supported_os(path):
            raise UnsupportedOSError()

    if format_ == FORMAT_CAPE:
        import capa.features.extractors.cape.extractor

        with open(path, "r", encoding="utf-8") as f:
            report = json.load(f)
        return capa.features.extractors.cape.extractor.CapeExtractor.from_report(report)

    elif format_ == FORMAT_DOTNET:
        import capa.features.extractors.dnfile.extractor

        return capa.features.extractors.dnfile.extractor.DnfileFeatureExtractor(path)

    elif backend == BACKEND_BINJA:
        from capa.features.extractors.binja.find_binja_api import find_binja_path

        # When we are running as a standalone executable, we cannot directly import binaryninja
        # We need to fist find the binja API installation path and add it into sys.path
        if is_running_standalone():
            bn_api = find_binja_path()
            if os.path.exists(bn_api):
                sys.path.append(bn_api)

        try:
            from binaryninja import BinaryView, BinaryViewType
        except ImportError:
            raise RuntimeError(
                "Cannot import binaryninja module. Please install the Binary Ninja Python API first: "
                "https://docs.binary.ninja/dev/batch.html#install-the-api)."
            )

Review comment (suggestion):
`encoding` doesn't have any meaning if the text file is not opened in text mode (`rt`). recommend sticking to binary mode (`rb`) and letting json decode from utf-8, as expected.

```suggestion
        with open(path, "rb") as f:
```

Code after:
    backend: str,
    sigpaths: List[str],
    should_save_workspace=False,
    disable_progress=False,
) -> FeatureExtractor:
    """
    raises:
      UnsupportedFormatError
      UnsupportedArchError
      UnsupportedOSError
    """

    if format_ not in (FORMAT_SC32, FORMAT_SC64, FORMAT_CAPE):
        if not is_supported_format(path):
            raise UnsupportedFormatError()

        if not is_supported_arch(path):
            raise UnsupportedArchError()

        if os_ == OS_AUTO and not is_supported_os(path):
            raise UnsupportedOSError()

    if format_ == FORMAT_CAPE:
        import capa.features.extractors.cape.extractor

        with open(path, "rb") as f:
            report = json.load(f)
        return capa.features.extractors.cape.extractor.CapeExtractor.from_report(report)

    elif format_ == FORMAT_DOTNET:
        import capa.features.extractors.dnfile.extractor

        return capa.features.extractors.dnfile.extractor.DnfileFeatureExtractor(path)

    elif backend == BACKEND_BINJA:
        from capa.features.extractors.binja.find_binja_api import find_binja_path

        # When we are running as a standalone executable, we cannot directly import binaryninja
        # We need to fist find the binja API installation path and add it into sys.path
        if is_running_standalone():
            bn_api = find_binja_path()
            if os.path.exists(bn_api):
                sys.path.append(bn_api)

        try:
            from binaryninja import BinaryView, BinaryViewType
        except ImportError:
            raise RuntimeError(
                "Cannot import binaryninja module. Please install the Binary Ninja Python API first: "
                "https://docs.binary.ninja/dev/batch.html#install-the-api)."
            )