• 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: Reading a binary file format?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading a binary file format?


  • Subject: Re: Reading a binary file format?
  • From: Douglas Davidson <email@hidden>
  • Date: Thu, 11 Oct 2007 16:50:45 -0700


On Oct 11, 2007, at 4:15 PM, Keith Blount wrote:

So, suppose you have a file that contains some text
but also some (presumably binary) integer information.
If you open it up in TextEdit, you can see the text,
but there are some invisible characters that must be
the binary information that is unrecognised by
TextEdit. How would you go about reading it into a
Cocoa program? For instance, say the file has the
integers 'SCLT'  and 0 as part of its header, and some
integers after it, and then 'PLST', and then some
integers and then bytes representing text data. In
TextEdit, you would see "SCLT PLST" followed by the
text. How would I go about reading all of the
information and accessing the integers hidden away in
there etc? Can I do this with NSData or NSString
methods, or do I need to delve deeper into C?

The easiest way to do this is to get the contents of the file into an NSData, for example via dataWithContentsOfFile:. You can then obtain the contents of any particular byte in the file as e.g. *(uint8_t *) ([data bytes] + offset). If the value you are interested in reading is an number that is longer than a single byte, you must take endianness into account, for example using the functions in NSByteOrder.h. If you wish to extract strings from the file, you can use methods like initWithBytes:length:encoding:. Bear in mind that unfortunately a great many binary file formats are quite cavalier about the encoding used for any embedded strings.


There are significant pitfalls involved in reading binary file formats. In particular, any values encountered in the file must be treated with extreme caution; all offsets should be sanity-checked against the length of the file, calculations should be checked for overflow or underflow, values that are supposed to lie in a certain range should be tested, and so forth. Failure to do this is an abundant source of security holes if you are unlucky, or simple crashers if you are lucky. For every value you read from the file, ask yourself what would happen if some malicious person went in and changed those bytes in the file to some nonsensical value--because there are people out there who will do that. I would advise at the very least a thorough understanding of C pointers and pointer arithmetic.

Douglas Davidson

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Reading a binary file format?
      • From: Uli Kusterer <email@hidden>
References: 
 >Reading a binary file format? (From: Keith Blount <email@hidden>)

  • Prev by Date: Re: NSOpenPanel & NSFileTypeForHFSTypeCode
  • Next by Date: Highlighting NSView used in NSStatusItem
  • Previous by thread: Reading a binary file format?
  • Next by thread: Re: Reading a binary file format?
  • Index(es):
    • Date
    • Thread