⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 magnet.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Magnet.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

namespace KCeasyEngine {

TMagnet::TMagnet()
:   Streamable(false)
{
}

TMagnet::~TMagnet()
{
}

void TMagnet::Clear()
{
    XTList.clear();
    XSList.clear();
    ASList.clear();
    DN = "";
    KT = "";
    MT = "";
    Streamable = false;
    MediaType = "";
    FileSize = 0;
}

bool TMagnet::Parse(const string& Uri)
{
    int pos;

    Clear();

    if((pos = Uri.find(':')) == -1)
        return false;

    string Proto = Uri.substr(0,pos);

    if(Proto == "magnet")
        return ParseMagnet(Uri);
    else if(Proto == "sig2dat")
        return ParseSig2Dat(Uri);

    return false;
}

string TMagnet::Assemble()
{
    list<string>::iterator itr;
    string Uri = "magnet:?";

    for(itr = XTList.begin(); itr != XTList.end(); ++itr)
//        Uri += string("xt=") + UrlEncode(*itr) + "&";
        Uri += string("xt=") + *itr + "&";
    for(itr = XSList.begin(); itr != XSList.end(); ++itr)
//        Uri += string("xs=") + UrlEncode(*itr) + "&";
        Uri += string("xs=") + *itr + "&";
    for(itr = ASList.begin(); itr != ASList.end(); ++itr)
//        Uri += string("as=") + UrlEncode(*itr) + "&";
        Uri += string("as=") + *itr + "&";

    if(DN != "")
        Uri += string("dn=") + UrlEncode(DN) + "&";
    if(KT != "")
        Uri += string("kt=") + UrlEncode(KT) + "&";
    if(MT != "")
        Uri += string("mt=") + UrlEncode(MT) + "&";

    if(Streamable)
        Uri += string("x.streamable=1&");
    if(MediaType != "")
        Uri += string("x.mediatype=") + UrlEncode(MediaType) + "&";

    if(Uri == "magnet:?")
        return "";

    if(Uri[Uri.length()-1] == '&')
        Uri.resize(Uri.length()-1);

    return Uri;
}

// returns true if Uri starts with "magnet:" or "sig2dat"
bool TMagnet::IsMagnetURI(const string& Uri)
{
    string Proto = Uri.substr(0,7);
    return (Proto == "magnet:" || Proto == "sig2dat");
}

// protected

THashSet* TMagnet::HashSetFromList(const list<string>& List)
{
    THashSet* HashSet = new THashSet();

    list<string>::const_iterator itr = List.begin();
    for(;itr != List.end(); ++itr) {
        THash* Hash = new THash();
        if(!Hash->SetUrn(*itr)) {
            delete Hash;
            continue;
        }
        HashSet->AddHash(Hash);
    }

    return HashSet;
}

list<string> TMagnet::ListFromHashSet(const THashSet* HashSet)
{
    list<string> List;
    // HACKHACK: add FTH only if no kzhash
    if(!HashSet->GetHash("kzhash") && HashSet->GetHash("fth"))
        List.push_back(HashSet->GetHash("fth")->GetUrn());

    for(THashSet::Iterator itr = HashSet->Begin(); itr != HashSet->End(); ++itr)
        if((*itr)->GetName() != "fth")
            List.push_back((*itr)->GetUrn());

    return List;
}

bool TMagnet::ParseMagnet(const string& Uri)
{
    int pos, processed = 0;

    Clear();

    if((pos = Uri.find(':')) == -1)
        return false;

    if(Uri.substr(0,pos+2) != "magnet:?")
        return false;

    list<string> params = string_split(Uri.substr(pos+2),"&");
    if(params.empty())
        return false;

    list<string>::iterator pitr = params.begin();
    for(;pitr != params.end(); ++pitr) {
        list<string> tokens = string_split(*pitr,"=");
        if(tokens.size() != 2)
            continue;

        string key = tokens.front();
        string value = UrlDecode (tokens.back());

        if(key.substr(0,3) == "xt.") {
            if(string_to_int(key.substr(3)) > 0)
                key.resize(2);
        }

        if(key == "xt")
            XTList.push_back(value);
        else if (key == "xs")
            XSList.push_back(value);
        else if (key == "as")
            ASList.push_back(value);
        else if (key == "dn")
            DN = value;
        else if (key == "kt")
            KT = value;
        else if (key == "mt")
            MT = value;
        else if (key == "x.streamable")
            Streamable = (value == "1");
        else if (key == "x.mediatype")
            MediaType = value;
        else
            continue;

        processed++;
    }

    if(processed == 0)
        return false;

    return true;
}

bool TMagnet::ParseSig2Dat(const string& Uri)
{
    int pos;

    Clear();

    if((pos = Uri.find(':')) == -1)
        return false;

    if(Uri.substr(0,pos) != "sig2dat")
        return false;

    list<string> params = string_split(Uri,"|");
    if(params.empty())
        return false;

    // I think we all agree that the guy who came up with this format was a retard.

    list<string>::iterator pitr = params.begin();
    for(;pitr != params.end(); ++pitr) {
        if((pos = (*pitr).find(':')) == -1)
            continue;
        string Key = (*pitr).substr(0,pos);
        string Value = UrlDecode((*pitr).substr(pos+1));

        if(Key == "file") {
            DN = string_trim(Value);
        } else if(Key == "length") {
            // FIXME: overflow
            FileSize = string_to_int(Value);
        } else if(Key == "uuhash") {
            XTList.push_back(string("urn:fth:") + string_trim(Value));
        }
    }

    if(XTList.empty() || DN == "")
        return false;

    return true;
}

} // namespace KCeasyEngine

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -