Re: NSData, chars, and torrent files
Re: NSData, chars, and torrent files
- Subject: Re: NSData, chars, and torrent files
- From: Bill Bumgarner <email@hidden>
- Date: Wed, 24 May 2006 20:29:14 -0700
On May 24, 2006, at 7:55 PM, Duncan Anker wrote:
Jon Shier wrote:
I'm about at the end of my rope with this. As an attempt to
learn Cocoa (and OO programming in general) I'm writing my own
BitTorrent client. But I can't seem to get past a basic problem: I
can't read the contents of a .torrent file into an easily
accessible data structure with any success. I initially read it
into an NSString, but that corrupted the SHA1 hashes that are in
the file as raw byte strings. Then, on advice from Allan Odgaard
(yay IRC!), I switched to using an NSData object to initially read
the file, then converting it to a const char * so I can iterate
through it and parse the data (which, by the way, worked file when
I was using an NSString). However, this doesn't seem to work. The
char I get is only a fraction of the NSData object (I can tell the
NSData object has all of the file in it because of its length),
always terminating at some point in the SHA1 hash data.
Not knowing much about the structure of a .torrent file, but
assuming certain pieces of it are fixed, can you not just use
something like (usual off the top of one's head disclaimers apply):
NSData *myData = [[NSData dataWithContentsOfMappedFile: myFile]
retain];
NSData *theDataThatIWant = [myData
subdataWithRange:theRangeOfWhatIWant];
Wow. Talking about diving into a raging river to learn to swim... ;-)
The .torrent file is composed of "bencoded dictionaries". Looking at
the protocol, it is effectively a series of key/value pairs with each
bit of info prefixed by information indicating how it should be
interpreted.
This is a good reference:
http://www.bittorrent.org/protocol.html
For example, a .torrent might open with:
d8:announce30:http://tracker.prq.to/announce
Which means:
'd' - a dictionary of key/value pairs
'8:' - 8 bytes follow for the first key ('announce')
'30:' - 30 bytes follow for the value ('http://tracker.prq.to/announce')
... etc ...
The part that is tripping up Jon is the SHA1 hashes which as they are
encoded as binary.
So... with that:
(1) Don't use NSString. It is intended for actual Strings, not
Data. (Other languages, like Python, use Strings to encapsulate any
random data. Cocoa does not.) When the BitTorrent protocol refers to
a string, they might mean either a string or data. You'll have to
deal with figuring out which they mean by context.
(2) You will need to interpret the protocol as a line protocol. That
is, read each byte and figure out what it means in the context it was
given.
(3) The SHA1 hashes are stored in the 'pieces' key. In the
sample .torrent I grabbed, I found a pieces key/value pair like this:
6:pieces27760:<binary data deleted>
It means that the key is 6 characters in length and the value is 27760
characters in length. So, when encountered you would need to read
the next 27,760 bytes from the underlying data stream.
... etc ...
If the "char I get is only a fraction of the NSData object", it is
likely because you are trying to treat the .torrent stream as a (char
*) of the "a C string buffer" kind. It isn't. It is raw data as
discussed above. You will need to treat it as a stream of bytes using
API that doesn't stop processing every time it hits a NULL character.
b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden