2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "magneturi.h"
31 #include <libtorrent/bencode.hpp>
32 #include <libtorrent/error_code.hpp>
33 #include <libtorrent/magnet_uri.hpp>
34 #include <libtorrent/sha1_hash.hpp>
36 #include <QRegularExpression>
38 #include "base/global.h"
43 // BEP9 Extension for Peers to Send Metadata Files
45 bool isV1Hash(const QString
&string
)
47 // There are 2 representations for BitTorrent v1 info hash:
48 // 1. 40 chars hex-encoded string
49 // == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
50 // 2. 32 chars Base32 encoded string
51 // == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
52 const int V1_HEX_SIZE
= SHA1Hash::length() * 2;
53 const int V1_BASE32_SIZE
= SHA1Hash::length() * 1.6;
55 return ((((string
.size() == V1_HEX_SIZE
))
56 && !string
.contains(QRegularExpression(u
"[^0-9A-Fa-f]"_s
)))
57 || ((string
.size() == V1_BASE32_SIZE
)
58 && !string
.contains(QRegularExpression(u
"[^2-7A-Za-z]"_s
))));
61 bool isV2Hash(const QString
&string
)
63 // There are 1 representation for BitTorrent v2 info hash:
64 // 1. 64 chars hex-encoded string
65 // == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
66 const int V2_HEX_SIZE
= SHA256Hash::length() * 2;
68 return (string
.size() == V2_HEX_SIZE
)
69 && !string
.contains(QRegularExpression(u
"[^0-9A-Fa-f]"_s
));
73 using namespace BitTorrent
;
75 const int magnetUriId
= qRegisterMetaType
<MagnetUri
>();
77 MagnetUri::MagnetUri(const QString
&source
)
80 if (source
.isEmpty()) return;
83 m_url
= u
"magnet:?xt=urn:btmh:1220" + source
; // 0x12 0x20 is the "multihash format" tag for the SHA-256 hashing scheme.
84 else if (isV1Hash(source
))
85 m_url
= u
"magnet:?xt=urn:btih:" + source
;
88 lt::parse_magnet_uri(m_url
.toStdString(), m_addTorrentParams
, ec
);
93 #ifdef QBT_USES_LIBTORRENT2
94 m_infoHash
= m_addTorrentParams
.info_hashes
;
96 m_infoHash
= m_addTorrentParams
.info_hash
;
99 m_name
= QString::fromStdString(m_addTorrentParams
.name
);
101 m_trackers
.reserve(static_cast<decltype(m_trackers
)::size_type
>(m_addTorrentParams
.trackers
.size()));
103 auto tierIter
= m_addTorrentParams
.tracker_tiers
.cbegin();
104 for (const std::string
&url
: m_addTorrentParams
.trackers
)
106 if (tierIter
!= m_addTorrentParams
.tracker_tiers
.cend())
109 m_trackers
.append({QString::fromStdString(url
), tier
});
112 m_urlSeeds
.reserve(static_cast<decltype(m_urlSeeds
)::size_type
>(m_addTorrentParams
.url_seeds
.size()));
113 for (const std::string
&urlSeed
: m_addTorrentParams
.url_seeds
)
114 m_urlSeeds
.append(QString::fromStdString(urlSeed
));
117 bool MagnetUri::isValid() const
122 InfoHash
MagnetUri::infoHash() const
127 QString
MagnetUri::name() const
132 QVector
<TrackerEntry
> MagnetUri::trackers() const
137 QVector
<QUrl
> MagnetUri::urlSeeds() const
142 QString
MagnetUri::url() const
147 lt::add_torrent_params
MagnetUri::addTorrentParams() const
149 return m_addTorrentParams
;