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);
}
Example #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;
}
Example #3
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);
}
size_t bencode_str(const char *str, FILE *fp)
{
  return bencode_buf(str, strlen(str), fp);
}