Skip to content

Commit a23f45c

Browse files
authored
Split platform specific code to its own file
PR #21368.
1 parent 8a6207d commit a23f45c

File tree

5 files changed

+240
-137
lines changed

5 files changed

+240
-137
lines changed

src/base/utils/random.cpp

Lines changed: 6 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3-
* Copyright (C) 2017 Mike Tzou
3+
* Copyright (C) 2017-2024 Mike Tzou (Chocobo1)
44
*
55
* This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -30,146 +30,16 @@
3030

3131
#include <random>
3232

33-
#include <QtLogging>
3433
#include <QtSystemDetection>
3534

36-
#if defined(Q_OS_WIN)
37-
#include <windows.h>
38-
#include "base/global.h"
39-
#include "base/utils/os.h"
40-
#elif defined(Q_OS_LINUX)
41-
#include <cerrno>
42-
#include <cstring>
43-
#include <sys/random.h>
35+
#if defined(Q_OS_LINUX)
36+
#include "randomlayer_linux.cpp"
37+
#elif defined(Q_OS_WIN)
38+
#include "randomlayer_win.cpp"
4439
#else
45-
#include <cerrno>
46-
#include <cstdio>
47-
#include <cstring>
40+
#include "randomlayer_other.cpp"
4841
#endif
4942

50-
namespace
51-
{
52-
#if defined(Q_OS_WIN)
53-
class RandomLayer
54-
{
55-
// need to satisfy UniformRandomBitGenerator requirements
56-
public:
57-
using result_type = uint32_t;
58-
59-
RandomLayer()
60-
: m_processPrng {Utils::OS::loadWinAPI<PPROCESSPRNG>(u"BCryptPrimitives.dll"_s, "ProcessPrng")}
61-
{
62-
if (!m_processPrng)
63-
qFatal("Failed to load ProcessPrng().");
64-
}
65-
66-
static constexpr result_type min()
67-
{
68-
return std::numeric_limits<result_type>::min();
69-
}
70-
71-
static constexpr result_type max()
72-
{
73-
return std::numeric_limits<result_type>::max();
74-
}
75-
76-
result_type operator()()
77-
{
78-
result_type buf = 0;
79-
const bool result = m_processPrng(reinterpret_cast<PBYTE>(&buf), sizeof(buf));
80-
if (!result)
81-
qFatal("ProcessPrng() failed.");
82-
83-
return buf;
84-
}
85-
86-
private:
87-
using PPROCESSPRNG = BOOL (WINAPI *)(PBYTE, SIZE_T);
88-
const PPROCESSPRNG m_processPrng;
89-
};
90-
#elif defined(Q_OS_LINUX)
91-
class RandomLayer
92-
{
93-
// need to satisfy UniformRandomBitGenerator requirements
94-
public:
95-
using result_type = uint32_t;
96-
97-
RandomLayer()
98-
{
99-
}
100-
101-
static constexpr result_type min()
102-
{
103-
return std::numeric_limits<result_type>::min();
104-
}
105-
106-
static constexpr result_type max()
107-
{
108-
return std::numeric_limits<result_type>::max();
109-
}
110-
111-
result_type operator()()
112-
{
113-
const int RETRY_MAX = 3;
114-
115-
for (int i = 0; i < RETRY_MAX; ++i)
116-
{
117-
result_type buf = 0;
118-
const ssize_t result = ::getrandom(&buf, sizeof(buf), 0);
119-
if (result == sizeof(buf)) // success
120-
return buf;
121-
122-
if (result < 0)
123-
qFatal("getrandom() error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
124-
}
125-
126-
qFatal("getrandom() failed. Reason: too many retries.");
127-
}
128-
};
129-
#else
130-
class RandomLayer
131-
{
132-
// need to satisfy UniformRandomBitGenerator requirements
133-
public:
134-
using result_type = uint32_t;
135-
136-
RandomLayer()
137-
: m_randDev {fopen("/dev/urandom", "rb")}
138-
{
139-
if (!m_randDev)
140-
qFatal("Failed to open /dev/urandom. Reason: %s. Error code: %d.", std::strerror(errno), errno);
141-
}
142-
143-
~RandomLayer()
144-
{
145-
fclose(m_randDev);
146-
}
147-
148-
static constexpr result_type min()
149-
{
150-
return std::numeric_limits<result_type>::min();
151-
}
152-
153-
static constexpr result_type max()
154-
{
155-
return std::numeric_limits<result_type>::max();
156-
}
157-
158-
result_type operator()() const
159-
{
160-
result_type buf = 0;
161-
if (fread(&buf, sizeof(buf), 1, m_randDev) != 1)
162-
qFatal("Read /dev/urandom error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
163-
164-
return buf;
165-
}
166-
167-
private:
168-
FILE *m_randDev = nullptr;
169-
};
170-
#endif
171-
}
172-
17343
uint32_t Utils::Random::rand(const uint32_t min, const uint32_t max)
17444
{
17545
static RandomLayer layer;

src/base/utils/random.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Bittorrent Client using Qt and libtorrent.
3-
* Copyright (C) 2017 Mike Tzou
3+
* Copyright (C) 2017-2024 Mike Tzou (Chocobo1)
44
*
55
* This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License

src/base/utils/randomlayer_linux.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2024 Mike Tzou (Chocobo1)
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 <cerrno>
30+
#include <cstring>
31+
#include <limits>
32+
33+
#include <sys/random.h>
34+
35+
#include <QtLogging>
36+
37+
namespace
38+
{
39+
class RandomLayer
40+
{
41+
// need to satisfy UniformRandomBitGenerator requirements
42+
public:
43+
using result_type = uint32_t;
44+
45+
RandomLayer()
46+
{
47+
}
48+
49+
static constexpr result_type min()
50+
{
51+
return std::numeric_limits<result_type>::min();
52+
}
53+
54+
static constexpr result_type max()
55+
{
56+
return std::numeric_limits<result_type>::max();
57+
}
58+
59+
result_type operator()()
60+
{
61+
const int RETRY_MAX = 3;
62+
63+
for (int i = 0; i < RETRY_MAX; ++i)
64+
{
65+
result_type buf = 0;
66+
const ssize_t result = ::getrandom(&buf, sizeof(buf), 0);
67+
if (result == sizeof(buf)) // success
68+
return buf;
69+
70+
if (result < 0)
71+
qFatal("getrandom() error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
72+
}
73+
74+
qFatal("getrandom() failed. Reason: too many retries.");
75+
}
76+
};
77+
}

src/base/utils/randomlayer_other.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Bittorrent Client using Qt and libtorrent.
3+
* Copyright (C) 2024 Mike Tzou (Chocobo1)
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 <limits>
30+
31+
#include <cerrno>
32+
#include <cstdio>
33+
#include <cstring>
34+
35+
#include <QtLogging>
36+
37+
namespace
38+
{
39+
class RandomLayer
40+
{
41+
// need to satisfy UniformRandomBitGenerator requirements
42+
public:
43+
using result_type = uint32_t;
44+
45+
RandomLayer()
46+
: m_randDev {fopen("/dev/urandom", "rb")}
47+
{
48+
if (!m_randDev)
49+
qFatal("Failed to open /dev/urandom. Reason: %s. Error code: %d.", std::strerror(errno), errno);
50+
}
51+
52+
~RandomLayer()
53+
{
54+
fclose(m_randDev);
55+
}
56+
57+
static constexpr result_type min()
58+
{
59+
return std::numeric_limits<result_type>::min();
60+
}
61+
62+
static constexpr result_type max()
63+
{
64+
return std::numeric_limits<result_type>::max();
65+
}
66+
67+
result_type operator()() const
68+
{
69+
result_type buf = 0;
70+
if (fread(&buf, sizeof(buf), 1, m_randDev) != 1)
71+
qFatal("Read /dev/urandom error. Reason: %s. Error code: %d.", std::strerror(errno), errno);
72+
73+
return buf;
74+
}
75+
76+
private:
77+
FILE *m_randDev = nullptr;
78+
};
79+
}

0 commit comments

Comments
 (0)