qBittorrent
magneturi.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #include "magneturi.h"
30 
31 #include <libtorrent/bencode.hpp>
32 #include <libtorrent/error_code.hpp>
33 #include <libtorrent/magnet_uri.hpp>
34 #include <libtorrent/sha1_hash.hpp>
35 
36 #include <QRegularExpression>
37 
38 #include "infohash.h"
39 
40 namespace
41 {
42  // BEP9 Extension for Peers to Send Metadata Files
43 
44  bool isV1Hash(const QString &string)
45  {
46  // There are 2 representations for BitTorrent v1 info hash:
47  // 1. 40 chars hex-encoded string
48  // == 20 (SHA-1 length in bytes) * 2 (each byte maps to 2 hex characters)
49  // 2. 32 chars Base32 encoded string
50  // == 20 (SHA-1 length in bytes) * 1.6 (the efficiency of Base32 encoding)
51  const int V1_HEX_SIZE = SHA1Hash::length() * 2;
52  const int V1_BASE32_SIZE = SHA1Hash::length() * 1.6;
53 
54  return ((((string.size() == V1_HEX_SIZE))
55  && !string.contains(QRegularExpression(QLatin1String("[^0-9A-Fa-f]"))))
56  || ((string.size() == V1_BASE32_SIZE)
57  && !string.contains(QRegularExpression(QLatin1String("[^2-7A-Za-z]")))));
58  }
59 
60  bool isV2Hash(const QString &string)
61  {
62  // There are 1 representation for BitTorrent v2 info hash:
63  // 1. 64 chars hex-encoded string
64  // == 32 (SHA-2 256 length in bytes) * 2 (each byte maps to 2 hex characters)
65  const int V2_HEX_SIZE = SHA256Hash::length() * 2;
66 
67  return (string.size() == V2_HEX_SIZE)
68  && !string.contains(QRegularExpression(QLatin1String("[^0-9A-Fa-f]")));
69  }
70 }
71 
72 using namespace BitTorrent;
73 
74 const int magnetUriId = qRegisterMetaType<MagnetUri>();
75 
77  : m_valid(false)
78  , m_url(source)
79 {
80  if (source.isEmpty()) return;
81 
82  if (isV2Hash(source))
83  m_url = QString::fromLatin1("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 = QString::fromLatin1("magnet:?xt=urn:btih:") + source;
86 
87  lt::error_code ec;
88  lt::parse_magnet_uri(m_url.toStdString(), m_addTorrentParams, ec);
89  if (ec) return;
90 
91  m_valid = true;
92 
93 #ifdef QBT_USES_LIBTORRENT2
94  m_infoHash = m_addTorrentParams.info_hashes;
95 #else
96  m_infoHash = m_addTorrentParams.info_hash;
97 #endif
98 
99  m_name = QString::fromStdString(m_addTorrentParams.name);
100 
101  m_trackers.reserve(static_cast<decltype(m_trackers)::size_type>(m_addTorrentParams.trackers.size()));
102  for (const std::string &tracker : m_addTorrentParams.trackers)
103  m_trackers.append({QString::fromStdString(tracker)});
104 
105  m_urlSeeds.reserve(static_cast<decltype(m_urlSeeds)::size_type>(m_addTorrentParams.url_seeds.size()));
106  for (const std::string &urlSeed : m_addTorrentParams.url_seeds)
107  m_urlSeeds.append(QString::fromStdString(urlSeed));
108 }
109 
110 bool MagnetUri::isValid() const
111 {
112  return m_valid;
113 }
114 
116 {
117  return m_infoHash;
118 }
119 
120 QString MagnetUri::name() const
121 {
122  return m_name;
123 }
124 
125 QVector<TrackerEntry> MagnetUri::trackers() const
126 {
127  return m_trackers;
128 }
129 
130 QVector<QUrl> MagnetUri::urlSeeds() const
131 {
132  return m_urlSeeds;
133 }
134 
135 QString MagnetUri::url() const
136 {
137  return m_url;
138 }
139 
140 lt::add_torrent_params MagnetUri::addTorrentParams() const
141 {
142  return m_addTorrentParams;
143 }
QVector< QUrl > m_urlSeeds
Definition: magneturi.h:62
QVector< TrackerEntry > trackers() const
Definition: magneturi.cpp:125
lt::add_torrent_params addTorrentParams() const
Definition: magneturi.cpp:140
MagnetUri(const QString &source={})
Definition: magneturi.cpp:76
bool isValid() const
Definition: magneturi.cpp:110
lt::add_torrent_params m_addTorrentParams
Definition: magneturi.h:63
QVector< TrackerEntry > m_trackers
Definition: magneturi.h:61
QString url() const
Definition: magneturi.cpp:135
InfoHash infoHash() const
Definition: magneturi.cpp:115
QString name() const
Definition: magneturi.cpp:120
QVector< QUrl > urlSeeds() const
Definition: magneturi.cpp:130
static constexpr int length()
Definition: digest32.h:53
const int magnetUriId
Definition: magneturi.cpp:74
bool isV1Hash(const QString &string)
Definition: magneturi.cpp:44
bool isV2Hash(const QString &string)
Definition: magneturi.cpp:60