~ubuntu-branches/ubuntu/quantal/aria2/quantal

« back to all changes in this revision

Viewing changes to src/bittorrent_helper.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-04-02 12:38:55 UTC
  • mfrom: (2.5.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110402123855-znkslovhf5qvkjut
Tags: 1.11.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#include "fmt.h"
47
47
#include "BtConstants.h"
48
48
#include "MessageDigest.h"
49
 
#include "MessageDigestHelper.h"
 
49
#include "message_digest_helper.h"
50
50
#include "a2netcompat.h"
51
51
#include "BtConstants.h"
52
52
#include "bitfield.h"
144
144
 
145
145
    virtual void visit(const String& v)
146
146
    {
147
 
      uris_.push_back(v.s());
148
 
      torrent_->urlList.push_back(v.s());
 
147
      std::string utf8Uri = util::encodeNonUtf8(v.s());
 
148
      uris_.push_back(utf8Uri);
 
149
      torrent_->urlList.push_back(utf8Uri);
149
150
    }
150
151
 
151
152
    virtual void visit(const Integer& v) {}
 
153
    virtual void visit(const Bool& v) {}
 
154
    virtual void visit(const Null& v) {}
152
155
 
153
156
    virtual void visit(const List& v)
154
157
    {
156
159
          itr != eoi; ++itr) {
157
160
        const String* uri = asString(*itr);
158
161
        if(uri) {
159
 
          uris_.push_back(uri->s());
160
 
          torrent_->urlList.push_back(uri->s());
 
162
          std::string utf8Uri = util::encodeNonUtf8(uri->s());
 
163
          uris_.push_back(utf8Uri);
 
164
          torrent_->urlList.push_back(utf8Uri);
161
165
        }
162
166
      }
163
167
    }
198
202
 const std::string& overrideName,
199
203
 const std::vector<std::string>& urlList)
200
204
{
201
 
  std::string name;
202
205
  std::string utf8Name;
203
206
  if(overrideName.empty()) {
204
207
    std::string nameKey;
216
219
               nameData->s().c_str()),
217
220
           error_code::BITTORRENT_PARSE_ERROR);
218
221
      }
219
 
      name = nameData->s();
220
222
    } else {
221
 
      name = utf8Name = strconcat(File(defaultName).getBasename(), ".file");
 
223
      utf8Name = strconcat(File(defaultName).getBasename(), ".file");
222
224
    }
223
225
  } else {
224
 
    name = utf8Name = overrideName;
 
226
    utf8Name = overrideName;
225
227
  }
226
 
  torrent->name = name;
 
228
  torrent->name = utf8Name;
227
229
  std::vector<SharedHandle<FileEntry> > fileEntries;
228
230
  const List* filesList = asList(infoDict->get(C_FILES));
229
231
  if(filesList) {
258
260
      }
259
261
      
260
262
      std::vector<std::string> pathelem(pathList->size()+1);
261
 
      pathelem[0] = name;
 
263
      pathelem[0] = utf8Name;
262
264
      std::vector<std::string>::iterator pathelemOutItr = pathelem.begin();
263
265
      ++pathelemOutItr;
264
266
      for(List::ValueType::const_iterator itr = pathList->begin(),
271
273
                             error_code::BITTORRENT_PARSE_ERROR);
272
274
        }
273
275
      }
274
 
      std::string path = strjoin(pathelem.begin(), pathelem.end(), '/');
275
276
      std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), '/',
276
277
                                     std::ptr_fun(util::encodeNonUtf8));
277
278
      if(util::detectDirTraversal(utf8Path)) {
288
289
        (new FileEntry(util::applyDir(option->get(PREF_DIR),
289
290
                                      util::escapePath(utf8Path)),
290
291
                       fileLengthData->i(), offset, uris));
291
 
      fileEntry->setOriginalName(path);
 
292
      fileEntry->setOriginalName(utf8Path);
292
293
      fileEntries.push_back(fileEntry);
293
294
      offset += fileEntry->getLength();
294
295
    }
308
309
    for(std::vector<std::string>::const_iterator i = urlList.begin(),
309
310
          eoi = urlList.end(); i != eoi; ++i) {
310
311
      if(util::endsWith(*i, A2STR::SLASH_C)) {
311
 
        uris.push_back((*i)+util::percentEncode(name));
 
312
        uris.push_back((*i)+util::percentEncode(utf8Name));
312
313
      } else {
313
314
        uris.push_back(*i);
314
315
      }
317
318
      (new FileEntry(util::applyDir(option->get(PREF_DIR),
318
319
                                    util::escapePath(utf8Name)),
319
320
                     totalLength, 0, uris));
320
 
    fileEntry->setOriginalName(name);
 
321
    fileEntry->setOriginalName(utf8Name);
321
322
    fileEntries.push_back(fileEntry);
322
323
  }
323
324
  ctx->setFileEntries(fileEntries.begin(), fileEntries.end());
324
325
  if(torrent->mode == MULTI) {
325
 
    ctx->setBasePath(util::applyDir(option->get(PREF_DIR), utf8Name));
 
326
    ctx->setBasePath(util::applyDir(option->get(PREF_DIR),
 
327
                                    util::escapePath(utf8Name)));
326
328
  }
327
329
}
328
330
} // namespace
344
346
            eoi2 = tier->end(); uriIter != eoi2; ++uriIter) {
345
347
        const String* uri = asString(*uriIter);
346
348
        if(uri) {
347
 
          ntier.push_back(util::strip(uri->s()));
 
349
          ntier.push_back(util::encodeNonUtf8(util::strip(uri->s())));
348
350
        }
349
351
      }
350
352
      if(!ntier.empty()) {
355
357
    const String* announce = asString(rootDict->get(C_ANNOUNCE));
356
358
    if(announce) {
357
359
      std::vector<std::string> tier;
358
 
      tier.push_back(util::strip(announce->s()));
 
360
      tier.push_back(util::encodeNonUtf8(util::strip(announce->s())));
359
361
      torrent->announceList.push_back(tier);
360
362
    }
361
363
  }
378
380
      if(!hostname) {
379
381
        continue;
380
382
      }
381
 
      if(util::strip(hostname->s()).empty()) {
 
383
      std::string utf8Hostname =
 
384
        util::encodeNonUtf8(util::strip(hostname->s()));
 
385
      if(utf8Hostname.empty()) {
382
386
        continue;
383
387
      }
384
388
      const Integer* port = asInteger(addrPairList->get(1));
385
389
      if(!port || !(0 < port->i() && port->i() < 65536)) {
386
390
        continue;
387
391
      }
388
 
      torrent->nodes.push_back(std::make_pair(hostname->s(), port->i()));
 
392
      torrent->nodes.push_back(std::make_pair(utf8Hostname, port->i()));
389
393
    }
390
394
  }
391
395
}
415
419
  // retrieve infoHash
416
420
  std::string encodedInfoDict = bencode2::encode(infoDict);
417
421
  unsigned char infoHash[INFO_HASH_LENGTH];
418
 
  MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
419
 
                              MessageDigest::sha1(),
420
 
                              encodedInfoDict.data(),
421
 
                              encodedInfoDict.size());
 
422
  message_digest::digest(infoHash, INFO_HASH_LENGTH,
 
423
                         MessageDigest::sha1(),
 
424
                         encodedInfoDict.data(),
 
425
                         encodedInfoDict.size());
422
426
  torrent->infoHash = std::string(&infoHash[0], &infoHash[INFO_HASH_LENGTH]);
423
427
  torrent->metadata = encodedInfoDict;
424
428
  torrent->metadataSize = encodedInfoDict.size();
486
490
  }
487
491
  const String* commentUtf8 = asString(rootDict->get(C_COMMENT_UTF8));
488
492
  if(commentUtf8) {
489
 
    torrent->comment = commentUtf8->s();
 
493
    torrent->comment = util::encodeNonUtf8(commentUtf8->s());
490
494
  } else {
491
495
    const String* comment = asString(rootDict->get(C_COMMENT));
492
496
    if(comment) {
493
 
      torrent->comment = comment->s();
 
497
      torrent->comment = util::encodeNonUtf8(comment->s());
494
498
    }
495
499
  }
496
500
  const String* createdBy = asString(rootDict->get(C_CREATED_BY));
497
501
  if(createdBy) {
498
 
    torrent->createdBy = createdBy->s();
 
502
    torrent->createdBy = util::encodeNonUtf8(createdBy->s());
499
503
  }
500
504
 
501
505
  ctx->setAttribute(BITTORRENT, torrent);
679
683
  memcpy(tx+4, infoHash, 20);
680
684
  unsigned char x[20];
681
685
  SharedHandle<MessageDigest> sha1 = MessageDigest::sha1();
682
 
  MessageDigestHelper::digest(x, sizeof(x), sha1, tx, 24);
 
686
  message_digest::digest(x, sizeof(x), sha1, tx, 24);
683
687
  while(fastSet.size() < fastSetSize) {
684
688
    for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
685
689
      size_t j = i*4;
693
697
    }
694
698
    unsigned char temp[20];
695
699
    sha1->reset();
696
 
    MessageDigestHelper::digest(temp, sizeof(temp), sha1, x, sizeof(x));
 
700
    message_digest::digest(temp, sizeof(temp), sha1, x, sizeof(x));
697
701
    memcpy(x, temp, sizeof(x));
698
702
  }
699
703
}
857
861
  for(struct addrinfo* rp = res; rp; rp = rp->ai_next) {
858
862
    if(rp->ai_family == AF_INET) {
859
863
      struct sockaddr_in* in =
860
 
        reinterpret_cast<struct sockaddr_in*>(res->ai_addr);
 
864
        reinterpret_cast<struct sockaddr_in*>(rp->ai_addr);
861
865
      memcpy(compact, &(in->sin_addr), 4);
862
866
      memcpy(compact+4, &portN, sizeof(portN));
863
867
      return COMPACT_LEN_IPV4;
864
868
    } else if(rp->ai_family == AF_INET6) {
865
869
      struct sockaddr_in6* in6 =
866
 
        reinterpret_cast<struct sockaddr_in6*>(res->ai_addr);
 
870
        reinterpret_cast<struct sockaddr_in6*>(rp->ai_addr);
867
871
      memcpy(compact, &(in6->sin6_addr), 16);
868
872
      memcpy(compact+16, &portN, sizeof(portN));
869
873
      return COMPACT_LEN_IPV6;
975
979
    for(List::ValueType::const_iterator i = trs->begin(), eoi = trs->end();
976
980
        i != eoi; ++i) {
977
981
      std::vector<std::string> tier;
978
 
      tier.push_back(asString(*i)->s());
 
982
      tier.push_back(util::encodeNonUtf8(asString(*i)->s()));
979
983
      attrs->announceList.push_back(tier);
980
984
    }
981
985
  }
983
987
  const List* dns = asList(r->get("dn"));
984
988
  if(dns && !dns->empty()) {
985
989
    const String* dn = asString(dns->get(0));
986
 
    name += dn->s();
 
990
    name += util::encodeNonUtf8(dn->s());
987
991
  } else {
988
992
    name += util::toHex(infoHash);
989
993
  }