2016-09-29 11:37:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-09-29 11:37:16 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-09-29 11:37:16 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
#include "logs.h"
|
2018-07-30 20:34:23 +00:00
|
|
|
#include "base/basic_types.h"
|
2017-08-31 16:28:58 +00:00
|
|
|
#include "base/flags.h"
|
2017-09-04 11:24:35 +00:00
|
|
|
#include "base/algorithm.h"
|
2018-06-02 14:29:21 +00:00
|
|
|
#include "base/assertion.h"
|
2018-06-25 19:22:03 +00:00
|
|
|
#include "base/bytes.h"
|
2017-03-04 08:59:10 +00:00
|
|
|
|
2019-07-10 15:03:15 +00:00
|
|
|
#include <crl/crl_time.h>
|
2018-06-14 18:34:53 +00:00
|
|
|
#include <QtCore/QReadWriteLock>
|
|
|
|
#include <QtCore/QRegularExpression>
|
|
|
|
#include <QtNetwork/QNetworkProxy>
|
|
|
|
#include <cmath>
|
|
|
|
#include <set>
|
|
|
|
|
2018-07-30 20:34:23 +00:00
|
|
|
#define qsl(s) QStringLiteral(s)
|
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
namespace base {
|
2016-11-04 11:14:47 +00:00
|
|
|
|
2017-02-24 17:15:41 +00:00
|
|
|
template <typename T>
|
|
|
|
using set_of_unique_ptr = std::set<std::unique_ptr<T>, base::pointer_comparator<T>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using set_of_shared_ptr = std::set<std::shared_ptr<T>, base::pointer_comparator<T>>;
|
|
|
|
|
2018-01-07 12:04:34 +00:00
|
|
|
template <typename Value, typename From, typename Till>
|
|
|
|
inline bool in_range(Value &&value, From &&from, Till &&till) {
|
|
|
|
return (value >= from) && (value < till);
|
|
|
|
}
|
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
} // namespace base
|
2016-09-29 11:37:16 +00:00
|
|
|
|
2016-11-28 15:45:07 +00:00
|
|
|
// using for_const instead of plain range-based for loop to ensure usage of const_iterator
|
|
|
|
// it is important for the copy-on-write Qt containers
|
|
|
|
// if you have "QVector<T*> v" then "for (T * const p : v)" will still call QVector::detach(),
|
|
|
|
// while "for_const (T *p, v)" won't and "for_const (T *&p, v)" won't compile
|
2017-02-21 13:45:56 +00:00
|
|
|
#define for_const(range_declaration, range_expression) for (range_declaration : std::as_const(range_expression))
|
2016-11-28 15:45:07 +00:00
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
static const int32 ScrollMax = INT_MAX;
|
|
|
|
|
|
|
|
extern uint64 _SharedMemoryLocation[];
|
|
|
|
template <typename T, unsigned int N>
|
|
|
|
T *SharedMemoryLocation() {
|
|
|
|
static_assert(N < 4, "Only 4 shared memory locations!");
|
|
|
|
return reinterpret_cast<T*>(_SharedMemoryLocation + N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// see https://github.com/boostcon/cppnow_presentations_2012/blob/master/wed/schurr_cpp11_tools_for_class_authors.pdf
|
|
|
|
class str_const { // constexpr string
|
|
|
|
public:
|
2019-08-26 16:36:23 +00:00
|
|
|
constexpr str_const(const char *str, std::size_t size)
|
|
|
|
: _str(str)
|
|
|
|
, _size(size) {
|
|
|
|
}
|
|
|
|
template <std::size_t N>
|
|
|
|
constexpr str_const(const char(&a)[N]) : str_const(a, N - 1) {
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
constexpr char operator[](std::size_t n) const {
|
|
|
|
return (n < _size) ? _str[n] :
|
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
throw std::out_of_range("");
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
throw std::exception();
|
|
|
|
#endif // OS_MAC_OLD
|
|
|
|
}
|
|
|
|
constexpr std::size_t size() const { return _size; }
|
2019-08-26 16:36:23 +00:00
|
|
|
constexpr const char *c_str() const { return _str; }
|
2016-09-29 11:37:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* const _str;
|
|
|
|
const std::size_t _size;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
inline QString str_const_toString(const str_const &str) {
|
|
|
|
return QString::fromUtf8(str.c_str(), str.size());
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
inline QByteArray str_const_toByteArray(const str_const &str) {
|
|
|
|
return QByteArray::fromRawData(str.c_str(), str.size());
|
|
|
|
}
|
|
|
|
|
2018-06-02 14:29:21 +00:00
|
|
|
int GetNextRequestId();
|
2016-09-29 11:37:16 +00:00
|
|
|
|
|
|
|
inline void mylocaltime(struct tm * _Tm, const time_t * _Time) {
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
localtime_s(_Tm, _Time);
|
|
|
|
#else
|
|
|
|
localtime_r(_Time, _Tm);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ThirdParty {
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void finish();
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
} // namespace ThirdParty
|
2016-09-29 11:37:16 +00:00
|
|
|
|
|
|
|
const static uint32 _md5_block_size = 64;
|
|
|
|
class HashMd5 {
|
|
|
|
public:
|
|
|
|
|
|
|
|
HashMd5(const void *input = 0, uint32 length = 0);
|
|
|
|
void feed(const void *input, uint32 length);
|
|
|
|
int32 *result();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void init();
|
|
|
|
void finalize();
|
|
|
|
void transform(const uchar *block);
|
|
|
|
|
|
|
|
bool _finalized;
|
|
|
|
uchar _buffer[_md5_block_size];
|
|
|
|
uint32 _count[2];
|
|
|
|
uint32 _state[4];
|
|
|
|
uchar _digest[16];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int32 *hashSha1(const void *data, uint32 len, void *dest); // dest - ptr to 20 bytes, returns (int32*)dest
|
2017-08-01 16:46:47 +00:00
|
|
|
inline std::array<char, 20> hashSha1(const void *data, int size) {
|
2017-02-22 15:18:26 +00:00
|
|
|
auto result = std::array<char, 20>();
|
2017-08-01 16:46:47 +00:00
|
|
|
hashSha1(data, size, result.data());
|
2017-02-22 15:18:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
int32 *hashSha256(const void *data, uint32 len, void *dest); // dest - ptr to 32 bytes, returns (int32*)dest
|
2017-02-22 15:18:26 +00:00
|
|
|
inline std::array<char, 32> hashSha256(const void *data, int size) {
|
|
|
|
auto result = std::array<char, 32>();
|
2017-08-01 16:46:47 +00:00
|
|
|
hashSha256(data, size, result.data());
|
2017-02-22 15:18:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
int32 *hashMd5(const void *data, uint32 len, void *dest); // dest = ptr to 16 bytes, returns (int32*)dest
|
2017-02-22 15:18:26 +00:00
|
|
|
inline std::array<char, 16> hashMd5(const void *data, int size) {
|
|
|
|
auto result = std::array<char, 16>();
|
|
|
|
hashMd5(data, size, result.data());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
char *hashMd5Hex(const int32 *hashmd5, void *dest); // dest = ptr to 32 bytes, returns (char*)dest
|
|
|
|
inline char *hashMd5Hex(const void *data, uint32 len, void *dest) { // dest = ptr to 32 bytes, returns (char*)dest
|
|
|
|
return hashMd5Hex(HashMd5(data, len).result(), dest);
|
|
|
|
}
|
2017-02-22 15:18:26 +00:00
|
|
|
inline std::array<char, 32> hashMd5Hex(const void *data, int size) {
|
|
|
|
auto result = std::array<char, 32>();
|
|
|
|
hashMd5Hex(data, size, result.data());
|
|
|
|
return result;
|
|
|
|
}
|
2016-09-29 11:37:16 +00:00
|
|
|
|
|
|
|
// good random (using openssl implementation)
|
|
|
|
void memset_rand(void *data, uint32 len);
|
|
|
|
template <typename T>
|
|
|
|
T rand_value() {
|
|
|
|
T result;
|
|
|
|
memset_rand(&result, sizeof(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReadLockerAttempt {
|
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
ReadLockerAttempt(not_null<QReadWriteLock*> lock) : _lock(lock), _locked(_lock->tryLockForRead()) {
|
2017-06-26 17:38:16 +00:00
|
|
|
}
|
|
|
|
ReadLockerAttempt(const ReadLockerAttempt &other) = delete;
|
|
|
|
ReadLockerAttempt &operator=(const ReadLockerAttempt &other) = delete;
|
|
|
|
ReadLockerAttempt(ReadLockerAttempt &&other) : _lock(other._lock), _locked(base::take(other._locked)) {
|
|
|
|
}
|
|
|
|
ReadLockerAttempt &operator=(ReadLockerAttempt &&other) {
|
|
|
|
_lock = other._lock;
|
|
|
|
_locked = base::take(other._locked);
|
2017-06-28 07:24:06 +00:00
|
|
|
return *this;
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
~ReadLockerAttempt() {
|
2017-06-26 17:38:16 +00:00
|
|
|
if (_locked) {
|
|
|
|
_lock->unlock();
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const {
|
2017-06-26 17:38:16 +00:00
|
|
|
return _locked;
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<QReadWriteLock*> _lock;
|
2017-06-26 17:38:16 +00:00
|
|
|
bool _locked = false;
|
2016-09-29 11:37:16 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static const QRegularExpression::PatternOptions reMultiline(QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline T snap(const T &v, const T &_min, const T &_max) {
|
|
|
|
return (v < _min) ? _min : ((v > _max) ? _max : v);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString translitRusEng(const QString &rus);
|
|
|
|
QString rusKeyboardLayoutSwitch(const QString &from);
|
|
|
|
|
|
|
|
enum DBINotifyView {
|
|
|
|
dbinvShowPreview = 0,
|
|
|
|
dbinvShowName = 1,
|
|
|
|
dbinvShowNothing = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum DBIWorkMode {
|
|
|
|
dbiwmWindowAndTray = 0,
|
|
|
|
dbiwmTrayOnly = 1,
|
|
|
|
dbiwmWindowOnly = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProxyData {
|
2018-11-05 13:58:24 +00:00
|
|
|
enum class Settings {
|
|
|
|
System,
|
|
|
|
Enabled,
|
|
|
|
Disabled,
|
|
|
|
};
|
2018-04-23 10:24:03 +00:00
|
|
|
enum class Type {
|
|
|
|
None,
|
|
|
|
Socks5,
|
|
|
|
Http,
|
2018-04-24 08:46:27 +00:00
|
|
|
Mtproto,
|
2018-04-23 10:24:03 +00:00
|
|
|
};
|
2019-07-08 16:02:45 +00:00
|
|
|
enum class Status {
|
|
|
|
Valid,
|
|
|
|
Unsupported,
|
|
|
|
Invalid,
|
|
|
|
};
|
2018-04-27 16:16:50 +00:00
|
|
|
|
2018-04-23 10:24:03 +00:00
|
|
|
Type type = Type::None;
|
2016-09-29 11:37:16 +00:00
|
|
|
QString host;
|
|
|
|
uint32 port = 0;
|
|
|
|
QString user, password;
|
2018-04-27 16:16:50 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
std::vector<QString> resolvedIPs;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time resolvedExpireAt = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
|
2019-07-08 16:02:45 +00:00
|
|
|
[[nodiscard]] bool valid() const;
|
|
|
|
[[nodiscard]] Status status() const;
|
|
|
|
[[nodiscard]] bool supportsCalls() const;
|
|
|
|
[[nodiscard]] bool tryCustomResolve() const;
|
|
|
|
[[nodiscard]] bytes::vector secretFromMtprotoPassword() const;
|
|
|
|
[[nodiscard]] explicit operator bool() const;
|
|
|
|
[[nodiscard]] bool operator==(const ProxyData &other) const;
|
|
|
|
[[nodiscard]] bool operator!=(const ProxyData &other) const;
|
|
|
|
|
|
|
|
[[nodiscard]] static bool ValidMtprotoPassword(const QString &password);
|
|
|
|
[[nodiscard]] static Status MtprotoPasswordStatus(
|
|
|
|
const QString &password);
|
2018-04-27 16:16:50 +00:00
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
};
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
ProxyData ToDirectIpProxy(const ProxyData &proxy, int ipIndex = 0);
|
2018-04-30 15:49:03 +00:00
|
|
|
QNetworkProxy ToNetworkProxy(const ProxyData &proxy);
|
|
|
|
|
2016-09-29 11:37:16 +00:00
|
|
|
static const int MatrixRowShift = 40000;
|
|
|
|
|
|
|
|
inline int rowscount(int fullCount, int countPerRow) {
|
|
|
|
return (fullCount + countPerRow - 1) / countPerRow;
|
|
|
|
}
|
|
|
|
inline int floorclamp(int value, int step, int lowest, int highest) {
|
|
|
|
return qMin(qMax(value / step, lowest), highest);
|
|
|
|
}
|
|
|
|
inline int floorclamp(float64 value, int step, int lowest, int highest) {
|
|
|
|
return qMin(qMax(static_cast<int>(std::floor(value / step)), lowest), highest);
|
|
|
|
}
|
|
|
|
inline int ceilclamp(int value, int step, int lowest, int highest) {
|
|
|
|
return qMax(qMin((value + step - 1) / step, highest), lowest);
|
|
|
|
}
|
|
|
|
inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) {
|
|
|
|
return qMax(qMin(static_cast<int>(std::ceil(value / step)), highest), lowest);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32 FullArcLength = 360 * 16;
|
|
|
|
static int32 QuarterArcLength = (FullArcLength / 4);
|
|
|
|
static int32 MinArcLength = (FullArcLength / 360);
|
|
|
|
static int32 AlmostFullArcLength = (FullArcLength - MinArcLength);
|
|
|
|
|
|
|
|
// This pointer is used for global non-POD variables that are allocated
|
|
|
|
// on demand by createIfNull(lambda) and are never automatically freed.
|
|
|
|
template <typename T>
|
|
|
|
class NeverFreedPointer {
|
|
|
|
public:
|
|
|
|
NeverFreedPointer() = default;
|
|
|
|
NeverFreedPointer(const NeverFreedPointer<T> &other) = delete;
|
|
|
|
NeverFreedPointer &operator=(const NeverFreedPointer<T> &other) = delete;
|
|
|
|
|
|
|
|
template <typename... Args>
|
2016-10-12 19:34:25 +00:00
|
|
|
void createIfNull(Args&&... args) {
|
2016-09-29 11:37:16 +00:00
|
|
|
if (isNull()) {
|
2017-02-21 13:45:56 +00:00
|
|
|
reset(new T(std::forward<Args>(args)...));
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
T *data() const {
|
|
|
|
return _p;
|
|
|
|
}
|
|
|
|
T *release() {
|
2016-10-07 16:45:45 +00:00
|
|
|
return base::take(_p);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
void reset(T *p = nullptr) {
|
|
|
|
delete _p;
|
|
|
|
_p = p;
|
|
|
|
}
|
|
|
|
bool isNull() const {
|
|
|
|
return data() == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
T *operator->() const {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
T &operator*() const {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!isNull());
|
2016-09-29 11:37:16 +00:00
|
|
|
return *data();
|
|
|
|
}
|
|
|
|
explicit operator bool() const {
|
|
|
|
return !isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *_p;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// This pointer is used for static non-POD variables that are allocated
|
|
|
|
// on first use by constructor and are never automatically freed.
|
|
|
|
template <typename T>
|
|
|
|
class StaticNeverFreedPointer {
|
|
|
|
public:
|
|
|
|
explicit StaticNeverFreedPointer(T *p) : _p(p) {
|
|
|
|
}
|
|
|
|
StaticNeverFreedPointer(const StaticNeverFreedPointer<T> &other) = delete;
|
|
|
|
StaticNeverFreedPointer &operator=(const StaticNeverFreedPointer<T> &other) = delete;
|
|
|
|
|
|
|
|
T *data() const {
|
|
|
|
return _p;
|
|
|
|
}
|
|
|
|
T *release() {
|
2016-10-07 16:45:45 +00:00
|
|
|
return base::take(_p);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
void reset(T *p = nullptr) {
|
|
|
|
delete _p;
|
|
|
|
_p = p;
|
|
|
|
}
|
|
|
|
bool isNull() const {
|
|
|
|
return data() == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
T *operator->() const {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
T &operator*() const {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(!isNull());
|
2016-09-29 11:37:16 +00:00
|
|
|
return *data();
|
|
|
|
}
|
|
|
|
explicit operator bool() const {
|
|
|
|
return !isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *_p = nullptr;
|
|
|
|
|
|
|
|
};
|