Esempio n. 1
0
static void
bitfieldToBenc( const tr_bitfield * b, tr_benc * benc )
{
    if( tr_bitfieldHasAll( b ) )
        tr_bencInitStr( benc, "all", 3 );
    else if( tr_bitfieldHasNone( b ) )
        tr_bencInitStr( benc, "none", 4 );
    else {
        size_t byte_count = 0;
        uint8_t * raw = tr_bitfieldGetRaw( b, &byte_count );
        tr_bencInitRaw( benc, raw, byte_count );
        tr_free( raw );
    }
}
Esempio n. 2
0
static bool
replaceURL (tr_benc * metainfo, const char * in, const char * out)
{
  const char * str;
  tr_benc * announce_list;
  bool changed = false;

  if (tr_bencDictFindStr (metainfo, "announce", &str) && strstr (str, in))
    {
      char * newstr = replaceSubstr (str, in, out);
      printf ("\tReplaced in \"announce\": \"%s\" --> \"%s\"\n", str, newstr);
      tr_bencDictAddStr (metainfo, "announce", newstr);
      tr_free (newstr);
      changed = true;
    }

  if (tr_bencDictFindList (metainfo, "announce-list", &announce_list))
    {
      tr_benc * tier;
      int tierCount = 0;
      while ((tier = tr_bencListChild (announce_list, tierCount++)))
        {
          tr_benc * node;
          int nodeCount = 0;
          while ((node = tr_bencListChild (tier, nodeCount++)))
            {
              if (tr_bencGetStr (node, &str) && strstr (str, in))
                {
                  char * newstr = replaceSubstr (str, in, out);
                  printf ("\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount, str, newstr);
                  tr_bencFree (node);
                  tr_bencInitStr (node, newstr, -1);
                  tr_free (newstr);
                  changed = true;
                }
            }
        }
    }

  return changed;
}
Esempio n. 3
0
static int
callback( void *             vdata,
          int                type,
          const JSON_value * value )
{
    struct json_benc_data * data = vdata;
    tr_benc *               node;

    switch( type )
    {
        case JSON_T_ARRAY_BEGIN:
            node = getNode( data );
            tr_bencInitList( node, 0 );
            tr_ptrArrayAppend( data->stack, node );
            break;

        case JSON_T_ARRAY_END:
            tr_ptrArrayPop( data->stack );
            break;

        case JSON_T_OBJECT_BEGIN:
            node = getNode( data );
            tr_bencInitDict( node, 0 );
            tr_ptrArrayAppend( data->stack, node );
            break;

        case JSON_T_OBJECT_END:
            tr_ptrArrayPop( data->stack );
            break;

        case JSON_T_FLOAT:
        {
            char buf[128];
            tr_snprintf( buf, sizeof( buf ), "%f",
                         (double)value->vu.float_value );
            tr_bencInitStr( getNode( data ), buf, -1 );
            break;
        }

        case JSON_T_NULL:
            tr_bencInitStr( getNode( data ), "", 0 );
            break;

        case JSON_T_INTEGER:
            tr_bencInitInt( getNode( data ), value->vu.integer_value );
            break;

        case JSON_T_TRUE:
            tr_bencInitInt( getNode( data ), 1 );
            break;

        case JSON_T_FALSE:
            tr_bencInitInt( getNode( data ), 0 );
            break;

        case JSON_T_STRING:
            tr_bencInitStr( getNode( data ),
                            value->vu.str.value,
                            value->vu.str.length );
            break;

        case JSON_T_KEY:
            assert( !data->key );
            data->key = tr_strdup( value->vu.str.value );
            break;
    }

    return 1;
}