private static bool TryParseMachO()

in net/JetBrains.FormatRipper/src/FileExplorer/FileTypeExplorer.cs [102:151]


    private static bool TryParseMachO(Stream stream, out FileProperties properties)
    {
      try
      {
        if (MachOFile.Is(stream))
        {
          static MH_FileType? GetAggregatedFileType(IEnumerable<MachOFile.Section> sections)
          {
            MH_FileType? fileType = null;
            foreach (var section in sections)
              if (fileType == null)
                fileType = section.MhFileType;
              else if (fileType != section.MhFileType)
                return null;

            return fileType;
          }

          static bool IsAllHasCodeSignature(IEnumerable<MachOFile.Section> sections)
          {
            foreach (var section in sections)
              if (!section.HasSignature)
                return false;
            return true;
          }

          var file = MachOFile.Parse(stream);
          var fileSections = file.Sections;

          properties = GetAggregatedFileType(fileSections) switch
            {
              MH_FileType.MH_EXECUTE => FileProperties.ExecutableType,
              MH_FileType.MH_DYLIB => FileProperties.SharedLibraryType,
              MH_FileType.MH_BUNDLE => FileProperties.BundleType,
              _ => FileProperties.UnknownType
            };
          if (IsAllHasCodeSignature(fileSections))
            properties |= FileProperties.Signed;
          if (file.IsFatLittleEndian != null)
            properties |= FileProperties.MultiArch;
          return true;
        }
      }
      catch (IOException)
      {
      }

      properties = default;
      return false;
    }