• 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: escaping a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: escaping a string


  • Subject: Re: escaping a string
  • From: "Stephen J. Butler" <email@hidden>
  • Date: Sun, 21 Sep 2008 03:24:54 -0500

On Sun, Sep 21, 2008 at 12:09 AM, Sandro Noel <email@hidden> wrote:
> NSData *torrentData = [[NSData alloc]initWithContentsOfURL:[NSURL
> URLWithString:itemLink]];
> if (0 < [torrentData length]){
>        NSString *torrentString = [[[NSString alloc]initWithData:torrentData
> encoding:NSASCIIStringEncoding]autorelease];

As Michael Ash said, a torrent file is not ASCII string data. It is
just a sequence of bytes; raw binary data. You can't decode it as as
ASCII string and expect it to work. To do it properly, you MUST work
NSData 100% of the time until you get a section of the file that is
defined as being a string.

>        // find the range of the info section.
>        NSRange infoRange = [torrentString rangeOfString:@"4:info"
> options:NSCaseInsensitiveSearch];

This strategy may work 99% of the time, but the torrent file format
doesn't support this kind of seeking. That is, to find the "info" key
in a file, you need to *properly* parse the entire file up to that
point. You can't just skip over the beginning data until you find
something that looks like what you're looking for.

Your first step should be writing a class that parses a torrent file
into PropertyList types. The entire file, not just the parts you're
interested in. Once that is working, then move on.

>        torrentString = [torrentString substringFromIndex:(infoRange.location
> + infoRange.length)];
>        torrentString = [torrentString substringToIndex:[torrentString
> length]];
>        NSString *infoHash = [torrentString sha1HexHash];

Assuming you're using the CocoaCryptoHashing category, sha1HexHash
does not do what you want here. You have a hash in binary format and
you want to convert it to an ASCII hex representation. Something like
this:

<http://lists.apple.com/archives/Cocoa-dev/2007/Nov/msg02176.html>
_______________________________________________

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: escaping a string
      • From: Sandro Noel <email@hidden>
References: 
 >escaping a string (From: Sandro Noel <email@hidden>)
 >Re: escaping a string (From: "Michael Ash" <email@hidden>)
 >Re: escaping a string (From: Sandro Noel <email@hidden>)

  • Prev by Date: Re: escaping a string
  • Next by Date: Storing Instances of a structure in a file
  • Previous by thread: Re: escaping a string
  • Next by thread: Re: escaping a string
  • Index(es):
    • Date
    • Thread