classifyFile function

String classifyFile(
  1. File file
)

Implementation

String classifyFile(File file) {
  final List<int> bytes = file.readAsBytesSync();

  if (bytes.length < 4) {
    return 'Unknown file type';
  }

  final magicBytes = bytes.sublist(0, 4);

  if (_matchesMagicNumber(magicBytes, [0x7f, 0x45, 0x4c, 0x46])) {
    // ELF Header: Executable and Linkable Format for Unix-based systems
    return _classifyElf(bytes);
  } else if (_matchesMagicNumber(magicBytes, [0xca, 0xfe, 0xba, 0xbe]) ||
      _matchesMagicNumber(magicBytes, [0xcf, 0xfa, 0xed, 0xfe])) {
    // Mach-O Header: Used in macOS
    return 'Executable or Dynamic library (Mach-O)';
  } else if (_matchesMagicNumber(magicBytes, [0x4d, 0x5a])) {
    // PE Header: Portable Executable for Windows
    return 'Executable (PE)';
  }

  return 'Unknown file type';
}