Пример #1
0
size_t btFiles::FillMetaInfo(FILE* fp)
{
  BTFILE *p;
  if( m_directory ){
    // multi files
    if( bencode_str("files", fp) != 1 ) return 0;

    if( bencode_begin_list(fp) != 1) return 0;
    
    for( p = m_btfhead; p; p = p->bf_next){
      if( bencode_begin_dict(fp) != 1) return 0;
      
      if( bencode_str("length", fp) != 1 ) return 0;
      if( bencode_int(p->bf_length, fp) != 1) return 0;

      if( bencode_str("path", fp) != 1) return 0;
      if( bencode_path2list(p->bf_filename, fp) != 1 ) return 0;
      
      if( bencode_end_dict_list(fp) != 1) return 0;
    }
    
    if(bencode_end_dict_list(fp) != 1 ) return 0;
    
    if(bencode_str("name", fp) != 1) return 0;
    return bencode_str(m_directory, fp);
    
  }else{
    if( bencode_str("length", fp) != 1 ) return 0;
    if( bencode_int(m_btfhead->bf_length, fp) != 1) return 0;
    
    if( bencode_str("name", fp) != 1 ) return 0;
    return bencode_str(m_btfhead->bf_filename, fp);
  }
  return 1;
}
Пример #2
0
int btContent::CreateMetainfoFile(const char *mifn)
{
  FILE *fp;
  fp = fopen(mifn, "r");
  if( fp ){
    fprintf(stderr,"error, file %s already exist.\n",mifn);
    return -1;
  }else if( ENOENT != errno ){
    fprintf(stderr,"error, couldn't create %s.\n",mifn);
    return -1;
  }

  fp = fopen(mifn, "w");

  if( !fp ){
    fprintf(stderr,"error, open %s failed. %s\n",mifn, strerror(errno));
    return -1;
  }
  if( bencode_begin_dict(fp) != 1 ) goto err;

  // announce
  if( bencode_str("announce", fp) != 1 ) goto err;
  if( bencode_str(m_announce, fp) !=1 ) goto err;
  // create date
  if( bencode_str("creation date", fp) != 1) goto err;
  if( bencode_int(m_create_date, fp) != 1 ) goto err;

  // info dict
  if( bencode_str("info", fp) != 1) goto err;
  if( bencode_begin_dict(fp) != 1 ) goto err;

  if( m_btfiles.FillMetaInfo(fp) != 1 ) goto err;

  // piece length
  if( bencode_str("piece length", fp) != 1 ) goto err;
  if( bencode_int(m_piece_length, fp) != 1 ) goto err;
  
  // hash table;
  if( bencode_str("pieces", fp) != 1) goto err;
  if( bencode_buf((const char*) m_hash_table, m_hashtable_length, fp) != 1 ) goto err;

  if( bencode_end_dict_list(fp) != 1 ) goto err; // end info
  if( bencode_end_dict_list(fp) != 1 ) goto err; // end torrent

  fclose(fp);
  return 0;
 err:
  if( fp ) fclose(fp);
  return -1;
}
size_t bencode_path2list(const char *pathname, FILE *fp)
{
  const char *pn;
  const char *p = pathname;

  if( bencode_begin_list(fp) != 1 ) return 0;

//acw
  char *pBuf = new char[strlen(pathname) * 6 + 1];
  if(!(CSC(pBuf, (char*)pathname, strlen(pathname) * 6 + 1, strlen(pathname), '\0'))) p = pBuf;

  for(; *p;){
    pn = strchr(p, PATH_SP);
    if( pn ){
//acw
//      if( bencode_buf(p, pn - p, fp) != 1) return 0;
      if( bencode_buf(p, pn - p, fp) != 1) {delete []pBuf; return 0;}

      p = pn + 1;
    }else{
//acw
//      if( bencode_str(p, fp) != 1) return 0;
      if( bencode_str(p, fp) != 1) {delete []pBuf; return 0;}
      break;
    }
  }

//acw
  delete []pBuf;

  return bencode_end_dict_list(fp);
}
Пример #4
0
size_t bencode_path2list(const char *pathname, FILE *fp)
{
  const char *pn;
  const char *p = pathname;
  
  if( bencode_begin_list(fp) != 1 ) return 0;
  
  for(; *p;){
    pn = strchr(p, PATH_SP);
    if( pn ){
      if( bencode_buf(p, pn - p, fp) != 1) return 0;
      p = pn + 1;
    }else{
      if( bencode_str(p, fp) != 1) return 0;
      break;
    }
  }
  
  return bencode_end_dict_list(fp);
}