• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: determining binary files from text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: determining binary files from text


  • Subject: Re: determining binary files from text
  • From: Kurt Revis <email@hidden>
  • Date: Thu, 28 Feb 2002 13:00:34 -0800

This is off-topic for Cocoa-dev, but what the heck.

Ed Silva <email@hidden> says:

The 'file' command says 'Mach-O executable ppc' for a binary, but how does it determine that? I know that 'file' uses /etc/magic, but I don't know how, can anyone clue me in? Or is there a Foundation (or lower API) method for doing this?

'file' basically looks for "magic" numbers in the file (usually at the front of the file, but not always). In your case, you can do the same thing. Read the first four bytes of the file; if they are 4a 6f 79 21 ("Joy!") you are looking at a CFM binary, and if they are fe ed fa ce you are looking at a Mach-O binary (not sure if this means it's PPC-only or just non-fat). There is a different magic number, ca fe ba be, for fat Mach-O binaries; this magic number is also used for Java executables.

Probably just checking against these magic numbers will be good enough for your purposes. (You should take care to do this in an endian-correct way, of course, just in case you ever want to port to a little-endian processor.)

If you really want to be future-proof, you could scan the file looking for non-ASCII characters (nulls, characters > 127, and so on) and assume a file is binary if you see enough non-ASCII characters. But this will fail if people ever start writing shell scripts using Unicode characters.

These are all heuristic methods--in other words, just guessing (although the guess is an educated one). I don't know of a way to do this with absolute certainty, right now. Anyone?

--
Kurt Revis
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: determining binary files from text
      • From: David Rehring <email@hidden>
    • Re: determining binary files from text
      • From: Rob Petrovec <email@hidden>
References: 
 >determining binary files from text (From: Ed Silva <email@hidden>)

  • Prev by Date: Problems converting NSString path to FSSpec
  • Next by Date: Re: Multiple Line Help Tags
  • Previous by thread: Re: determining binary files from text
  • Next by thread: Re: determining binary files from text
  • Index(es):
    • Date
    • Thread