views:

316

answers:

2

I am trying to read the header of an SWF file using NSData. According to SWF format specification I need to access movie's width and height reading bits, not bytes, and I couldn't find a way to do it in Obj-C

Bytes 9 thru ?: Here is stored a RECT (bounds of movie). It must be read in binary form. First of all, we will transform the first byte to binary: "01100000" The first 5 bits will tell us the size in bits of each stored value: "01100" = 12 So, we have 4 fields of 12 bits = 48 bits 48 bits + 5 bits (header of RECT) = 53 bits Fill to complete bytes with zeroes, till we reach a multiple of 8. 53 bits + 3 alignment bits = 56 bits (this RECT is 7 bytes length, 7 * 8 = 56) I use this formula to determine all this stuff:

Where do I start?

+2  A: 

ObjC is a superset of C: You can run C code alongside ObjC with no issues.

Thus, you could use a C-based library like libming to read bytes from your SWF file.

If you need to shuffle bytes into an NSData object, look into the -dataWithBytes:length: method.

Alex Reynolds
I just found another C program to read the SWF header, it seems to work fine and I can compile it with no issues. Now I'm trying to figure out how to mix the pure C with my Obj-C application.
goo
+1  A: 

Start by looking for code with a compatible license that already does what you want. C libraries can be used from Obj-C code simply by linking them in (or arranging for them to be dynamically linked in) and then calling their functions.

Failing that, start by looking at the Binary Data Programming Guide for Cocoa and NSData Class Reference. You'd want to pull out the bytes that contain the bits you're interested in, then use bit masking techniques to extract the bits you care about. You might find the BitTst(), BitSet(), and BitClr() functions and their friends useful, if they're still there in Snow Leopard; I'm not sure whether they ended up in the démodé parts of Carbon or not. There are also the Posix setbit(), clrbit(), isset(), and isclr() macros defined in . Then, finally, there are the C bitwise operators: ^, |, &, ~, <<, and >>.

Jeremy W. Sherman
Just a note that it is not possible to dynamically link to libraries, if this is for an iPhone application.
Alex Reynolds