Re: determining binary files from text
Re: determining binary files from text
- Subject: Re: determining binary files from text
- From: Rob Petrovec <email@hidden>
- Date: Thu, 28 Feb 2002 13:21:03 -0800
I would simply scan through the file looking for any non-printable
characters... Not just non-ASCII... If its a binary there are going to be
TONS of non-printable characters in it. If its Text all the characters
will/should be printable... Good luck...
--Rob
On 2/28/02 1:00 PM, "Kurt Revis" <email@hidden> wrote:
>
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.
_______________________________________________
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.