/******************************************************************************
 * $Id$
 *
 * Copyright (c) 2005-2008 Transmission authors and contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *****************************************************************************/

#include <assert.h>
#include <ctype.h> /* isspace */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> /* unlink, stat */

#include "transmission.h"
#include "bencode.h"
#include "crypto.h" /* tr_sha1 */
#include "metainfo.h"
#include "platform.h"
#include "trcompat.h" /* strlcpy */
#include "utils.h"

/***********************************************************************
 * Local prototypes
 **********************************************************************/
static int getannounce( tr_info * inf, tr_benc * meta );
static char * announceToScrape( const char * announce );
static int parseFiles( tr_info * inf, tr_benc * name,
                       tr_benc * files, tr_benc * length );

/***
****
***/

#define WANTBYTES( want, got ) \
    if( (want) > (got) ) { return; } else { (got) -= (want); }
static void
strlcat_utf8( void * dest, const void * src, size_t len, char skip )
{
    char       * s      = dest;
    const char * append = src;
    const char * p;

    /* don't overwrite the nul at the end */
    len--;

    /* Go to the end of the destination string */
    while( s[0] )
    {
        s++;
        len--;
    }

    /* Now start appending, converting on the fly if necessary */
    for( p = append; p[0]; )
    {
        /* skip over the requested character */
        if( skip == p[0] )
        {
            p++;
            continue;
        }

        if( !( p[0] & 0x80 ) )
        {
            /* ASCII character */
            WANTBYTES( 1, len );
            *(s++) = *(p++);
            continue;
        }

        if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 )
        {
            /* 2-bytes UTF-8 character */
            WANTBYTES( 2, len );
            *(s++) = *(p++); *(s++) = *(p++);
            continue;
        }

        if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 &&
            ( p[2] & 0xC0 ) == 0x80 )
        {
            /* 3-bytes UTF-8 character */
            WANTBYTES( 3, len );
            *(s++) = *(p++); *(s++) = *(p++);
            *(s++) = *(p++);
            continue;
        }

        if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 &&
            ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 )
        {
            /* 4-bytes UTF-8 character */
            WANTBYTES( 4, len );
            *(s++) = *(p++); *(s++) = *(p++);
            *(s++) = *(p++); *(s++) = *(p++);
            continue;
        }

        /* ISO 8859-1 -> UTF-8 conversion */
        WANTBYTES( 2, len );
        *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 );
        *(s++) = 0x80 | ( *(p++) & 0x3F );
    }
}

static void
savedname( const tr_handle * handle,
           char            * name,
           size_t            len,
           const char      * hash )
{
    const char * torDir = tr_getTorrentDir( handle );

    if( !handle->tag )
    {
        tr_buildPath( name, len, torDir, hash, NULL );
    }
    else
    {
        char base[1024];
        snprintf( base, sizeof(base), "%s-%s", hash, handle->tag );
        tr_buildPath( name, len, torDir, base, NULL );
    }
}


int
tr_metainfoParse( const tr_handle  * handle,
                  tr_info          * inf,
                  const tr_benc    * meta_in )
{
    tr_piece_index_t i;
    tr_benc * beInfo, * val, * val2;
    tr_benc * meta = (tr_benc *) meta_in;
    char buf[4096];

    /* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key
     * from the Metainfo file. Note that the value will be a bencoded 
     * dictionary, given the definition of the info key above. */
    if(( beInfo = tr_bencDictFindType( meta, "info", TYPE_DICT )))
    {
        int len;
        char * str = tr_bencSave( beInfo, &len );
        tr_sha1( inf->hash, str, len, NULL );
        tr_free( str );
    }
    else
    {
        tr_err( _( "Missing metadata entry \"%s\"" ), "info" );
        return TR_EINVALID;
    }

    tr_sha1_to_hex( inf->hashString, inf->hash );
    savedname( handle, buf, sizeof( buf ), inf->hashString );
    tr_free( inf->torrent );
    inf->torrent = tr_strdup( buf );

    /* comment */
    memset( buf, '\0', sizeof( buf ) );
    val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL );
    if( tr_bencIsString( val ) )
        strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
    tr_free( inf->comment );
    inf->comment = tr_strdup( buf );
    
    /* creator */
    memset( buf, '\0', sizeof( buf ) );
    val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL );
    if( tr_bencIsString( val ) )
        strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 );
    tr_free( inf->creator );
    inf->creator = tr_strdup( buf );
    
    /* Date created */
    inf->dateCreated = 0;
    val = tr_bencDictFind( meta, "creation date" );
    if( tr_bencIsInt( val ) )
        inf->dateCreated = val->val.i;
    
    /* Private torrent */
    val  = tr_bencDictFind( beInfo, "private" );
    val2 = tr_bencDictFind( meta,  "private" );
    if( ( tr_bencIsInt(val) && val->val.i ) ||
        ( tr_bencIsInt(val2) && val2->val.i ) )
    {
        inf->isPrivate = 1;
    }
    
    /* Piece length */
    val = tr_bencDictFind( beInfo, "piece length" );
    if( !tr_bencIsInt( val ) )
    {
        if( val )
            tr_err( _( "Invalid metadata entry \"%s\"" ), "piece length" );
        else
            tr_err( _( "Missing metadata entry \"%s\"" ), "piece length" );
        goto fail;
    }
    inf->pieceSize = val->val.i;

    /* Hashes */
    val = tr_bencDictFind( beInfo, "pieces" );
    if( !tr_bencIsString( val ) )
    {
        if( val )
            tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" );
        else
            tr_err( _( "Missing metadata entry \"%s\"" ), "pieces" );
        goto fail;
    }
    if( val->val.s.i % SHA_DIGEST_LENGTH )
    {
        tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" );
        goto fail;
    }
    inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH;

    inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) );

    for ( i=0; i<inf->pieceCount; ++i )
    {
        memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH);
    }

    /* get file or top directory name */
    val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL );
    if( parseFiles( inf, tr_bencDictFindFirst( beInfo,
                                               "name.utf-8", "name", NULL ),
                    tr_bencDictFind( beInfo, "files" ),
                    tr_bencDictFind( beInfo, "length" ) ) )
    {
        goto fail;
    }

    if( !inf->fileCount || !inf->totalSize )
    {
        tr_err( _( "Torrent is corrupt" ) ); /* the content is missing! */
        goto fail;
    }

    /* TODO add more tests so we don't crash on weird files */

    if( (uint64_t) inf->pieceCount !=
        ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize )
    {
        tr_err( _( "Torrent is corrupt" ) ); /* size of hashes and files don't match */
        goto fail;
    }

    /* get announce or announce-list */
    if( getannounce( inf, meta ) )
    {
        goto fail;
    }

    return TR_OK;

  fail:
    tr_metainfoFree( inf );
    return TR_EINVALID;
}

void tr_metainfoFree( tr_info * inf )
{
    tr_file_index_t ff;
    int i, j;

    for( ff=0; ff<inf->fileCount; ++ff )
        tr_free( inf->files[ff].name );

    tr_free( inf->pieces );
    tr_free( inf->files );
    tr_free( inf->comment );
    tr_free( inf->creator );
    tr_free( inf->torrent );
    tr_free( inf->name );
    tr_free( inf->primaryAddress );
    
    for( i=0; i<inf->trackerTiers; ++i ) {
        for( j=0; j<inf->trackerList[i].count; ++j )
            tr_trackerInfoClear( &inf->trackerList[i].list[j] );
        tr_free( inf->trackerList[i].list );
    }
    tr_free( inf->trackerList );

    memset( inf, '\0', sizeof(tr_info) );
}

static int
getfile( char ** setme, const char * prefix, tr_benc * name )
{
    const char ** list;
    int           ii, jj;
    char          buf[4096];

    if( !tr_bencIsList( name ) )
        return TR_EINVALID;

    list = calloc( name->val.l.count, sizeof( list[0] ) );
    if( !list )
        return TR_EINVALID;

    for( ii = jj = 0; name->val.l.count > ii; ii++ )
    {
        tr_benc * dir = &name->val.l.vals[ii];

        if( !tr_bencIsString( dir ) )
            continue;

        if( 0 == strcmp( "..", dir->val.s.s ) )
        {
            if( 0 < jj )
            {
                jj--;
            }
        }
        else if( 0 != strcmp( ".", dir->val.s.s ) )
        {
            list[jj] = dir->val.s.s;
            jj++;
        }
    }

    if( 0 == jj )
    {
        free( list );
        return TR_EINVALID;
    }

    memset( buf, 0, sizeof( buf ) );
    strlcat_utf8( buf, prefix, sizeof(buf), 0 );
    for( ii = 0; jj > ii; ii++ )
    {
        strlcat_utf8( buf, TR_PATH_DELIMITER_STR, sizeof(buf), 0 );
        strlcat_utf8( buf, list[ii], sizeof(buf), TR_PATH_DELIMITER );
    }
    free( list );

    tr_free( *setme );
    *setme = tr_strdup( buf );

    return TR_OK;
}

static int getannounce( tr_info * inf, tr_benc * meta )
{
    tr_benc           * val, * urlval;
    char              * address, * announce;
    int                 ii, jj, port, random;
    tr_tracker_info   * sublist;
    void * swapping;

    /* Announce-list */
    val = tr_bencDictFind( meta, "announce-list" );
    if( tr_bencIsList(val) && 0 < val->val.l.count )
    {
        inf->trackerTiers = 0;
        inf->trackerList = calloc( val->val.l.count,
                                   sizeof( inf->trackerList[0] ) );

        /* iterate through the announce-list's tiers */
        for( ii = 0; ii < val->val.l.count; ii++ )
        {
            int subcount = 0;
            tr_benc * subval = &val->val.l.vals[ii];

            if( !tr_bencIsList(subval) || 0 >= subval->val.l.count )
                continue;

            sublist = calloc( subval->val.l.count, sizeof( sublist[0] ) );

            /* iterate through the tier's items */
            for( jj = 0; jj < subval->val.l.count; jj++ )
            {
                tr_tracker_info tmp;

                urlval = &subval->val.l.vals[jj];
                if( TYPE_STR != urlval->type ||
                    tr_trackerInfoInit( &tmp, urlval->val.s.s, urlval->val.s.i ) )
                {
                    continue;
                }

                if( !inf->primaryAddress ) {
                     char buf[1024];
                     snprintf( buf, sizeof(buf), "%s:%d", tmp.address, tmp.port );
                     inf->primaryAddress = tr_strdup( buf );
                }

                /* place the item info in a random location in the sublist */
                random = tr_rand( subcount + 1 );
                if( random != subcount )
                    sublist[subcount] = sublist[random];
                sublist[random] = tmp;
                subcount++;
            }

            /* just use sublist as-is if it's full */
            if( subcount == subval->val.l.count )
            {
                inf->trackerList[inf->trackerTiers].list = sublist;
                inf->trackerList[inf->trackerTiers].count = subcount;
                inf->trackerTiers++;
            }
            /* if we skipped some of the tier's items then trim the sublist */
            else if( 0 < subcount )
            {
                inf->trackerList[inf->trackerTiers].list = calloc( subcount, sizeof( sublist[0] ) );
                memcpy( inf->trackerList[inf->trackerTiers].list, sublist,
                        sizeof( sublist[0] ) * subcount );
                inf->trackerList[inf->trackerTiers].count = subcount;
                inf->trackerTiers++;
                free( sublist );
            }
            /* drop the whole sublist if we didn't use any items at all */
            else
            {
                free( sublist );
            }
        }

        /* did we use any of the tiers? */
        if( 0 == inf->trackerTiers )
        {
            tr_inf( _( "Invalid metadata entry \"%s\"" ), "announce-list" );
            free( inf->trackerList );
            inf->trackerList = NULL;
        }
        /* trim unused sublist pointers */
        else if( inf->trackerTiers < val->val.l.count )
        {
            swapping = inf->trackerList;
            inf->trackerList = calloc( inf->trackerTiers,
                                       sizeof( inf->trackerList[0] ) );
            memcpy( inf->trackerList, swapping,
                    sizeof( inf->trackerList[0] ) * inf->trackerTiers );
            free( swapping );
        }
    }

    /* Regular announce value */
    val = tr_bencDictFind( meta, "announce" );
    if( !tr_bencIsString( val ) )
    {
        tr_err( _( "Missing metadata entry \"%s\"" ), "announce" );
        return TR_EINVALID;
    }

    if( !inf->trackerTiers )
    {
        char buf[4096], *pch;
        strlcpy( buf, val->val.s.s, sizeof( buf ) );
        pch = buf;
        while( isspace( *pch ) )
            ++pch;

        if( tr_httpParseURL( pch, -1, &address, &port, &announce ) )
        {
            tr_err( _( "Invalid announce URL \"%s\"" ), val->val.s.s );
            return TR_EINVALID;
        }
        sublist                   = calloc( 1, sizeof( sublist[0] ) );
        sublist[0].address        = address;
        sublist[0].port           = port;
        sublist[0].announce       = announce;
        sublist[0].scrape         = announceToScrape( announce );
        inf->trackerList          = calloc( 1, sizeof( inf->trackerList[0] ) );
        inf->trackerList[0].list  = sublist;
        inf->trackerList[0].count = 1;
        inf->trackerTiers         = 1;

        if( !inf->primaryAddress ) {
            char buf[1024];
            snprintf( buf, sizeof(buf), "%s:%d", sublist[0].address, sublist[0].port );
            inf->primaryAddress = tr_strdup( buf );
        }

    }

    return TR_OK;
}

static char * announceToScrape( const char * announce )
{
    char old[]  = "announce";
    int  oldlen = 8;
    char new[]  = "scrape";
    int  newlen = 6;
    char * slash, * scrape;
    size_t scrapelen, used;

    slash = strrchr( announce, '/' );
    if( NULL == slash )
    {
        return NULL;
    }
    slash++;
    
    if( 0 != strncmp( slash, old, oldlen ) )
    {
        return NULL;
    }

    scrapelen = strlen( announce ) - oldlen + newlen;
    scrape = calloc( scrapelen + 1, 1 );
    if( NULL == scrape )
    {
        return NULL;
    }
    assert( ( size_t )( slash - announce ) < scrapelen );
    memcpy( scrape, announce, slash - announce );
    used = slash - announce;
    strncat( scrape, new, scrapelen - used );
    used += newlen;
    assert( strlen( scrape ) == used );
    if( used < scrapelen )
    {
        assert( strlen( slash + oldlen ) == scrapelen - used );
        strncat( scrape, slash + oldlen, scrapelen - used );
    }

    return scrape;
}

int
tr_trackerInfoInit( tr_tracker_info  * info,
                    const char       * address,
                    int                address_len )
{
    int ret = tr_httpParseURL( address, address_len,
                               &info->address,
                               &info->port,
                               &info->announce );
    if( !ret )
        info->scrape = announceToScrape( info->announce );

    return ret;
}

void
tr_trackerInfoClear( tr_tracker_info * info )
{
    tr_free( info->address );
    tr_free( info->announce );
    tr_free( info->scrape );
    memset( info, '\0', sizeof(tr_tracker_info) );
}

void
tr_metainfoRemoveSaved( const tr_handle * handle,
                        const char      * hashString )
{
    char file[MAX_PATH_LENGTH];
    savedname( handle, file, sizeof file, hashString );
    unlink( file );
}

/* Save a copy of the torrent file in the saved torrent directory */
int
tr_metainfoSave( const tr_handle  * handle,
                 const char       * hash,
                 const uint8_t    * buf,
                 size_t             buflen )
{
    char   path[MAX_PATH_LENGTH];
    FILE * file;

    savedname( handle, path, sizeof path, hash );
    file = fopen( path, "wb+" );
    if( !file )
    {
        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), path, tr_strerror( errno ) );
        return TR_EINVALID;
    }
    fseek( file, 0, SEEK_SET );
    if( fwrite( buf, 1, buflen, file ) != buflen )
    {
        tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), path, tr_strerror( errno ) );
        fclose( file );
        return TR_EINVALID;
    }
    fclose( file );

    return TR_OK;
}

static int
parseFiles( tr_info * inf, tr_benc * name,
            tr_benc * files, tr_benc * length )
{
    tr_benc * item, * path;
    int ii;

    if( !tr_bencIsString( name ) )
    {
        if( name )
            tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
        else
            tr_err( _( "Missing metadata entry \"%s\"" ), "name" );
        return TR_EINVALID;
    }

    tr_free( inf->name );
    inf->name = tr_strdup( name->val.s.s );
    if( !inf->name || !*inf->name )
    {
        tr_err( _( "Invalid metadata entry \"%s\"" ), "name" );
        return TR_EINVALID;
    }
    inf->totalSize = 0;

    if( tr_bencIsList( files ) )
    {
        /* Multi-file mode */
        inf->isMultifile = 1;
        inf->fileCount = files->val.l.count;
        inf->files     = calloc( inf->fileCount, sizeof( inf->files[0] ) );

        if( !inf->files )
            return TR_EINVALID;

        for( ii = 0; files->val.l.count > ii; ii++ )
        {
            item = &files->val.l.vals[ii];
            path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL );
            if( getfile( &inf->files[ii].name, inf->name, path ) )
            {
                if( path )
                    tr_err( _( "Invalid metadata entry \"%s\"" ), "path" );
                else
                    tr_err( _( "Missing metadata entry \"%s\"" ), "path" );
                return TR_EINVALID;
            }
            length = tr_bencDictFind( item, "length" );
            if( !tr_bencIsInt( length ) )
            {
                if( length )
                    tr_err( _( "Invalid metadata entry \"%s\"" ), "length" );
                else
                    tr_err( _( "Missing metadata entry \"%s\"" ), "length" );
                return TR_EINVALID;
            }
            inf->files[ii].length = length->val.i;
            inf->totalSize         += length->val.i;
        }
    }
    else if( tr_bencIsInt( length ) )
    {
        char buf[4096];

        /* Single-file mode */
        inf->isMultifile = 0;
        inf->fileCount = 1;
        inf->files     = calloc( 1, sizeof( inf->files[0] ) );

        if( !inf->files )
            return TR_EINVALID;

        memset( buf, 0, sizeof( buf ) );
        strlcat_utf8( buf, name->val.s.s, sizeof(buf), TR_PATH_DELIMITER );
        tr_free( inf->files[0].name );
        inf->files[0].name = tr_strdup( buf );

        inf->files[0].length = length->val.i;
        inf->totalSize      += length->val.i;
    }
    else
    {
        tr_err( _( "Invalid or missing metadata entries \"length\" and \"files\"" ) );
    }

    return TR_OK;
}
