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-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-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>
|
2022-08-26 21:45:38 +00:00
|
|
|
#include <QtCore/QMimeData>
|
2018-06-14 18:34:53 +00:00
|
|
|
#include <QtNetwork/QNetworkProxy>
|
|
|
|
#include <cmath>
|
|
|
|
#include <set>
|
2022-11-27 13:16:43 +00:00
|
|
|
#include <filesystem>
|
2018-06-14 18:34:53 +00:00
|
|
|
|
2022-09-28 16:01:59 +00:00
|
|
|
#if __has_include(<ksandbox.h>)
|
|
|
|
#include <ksandbox.h>
|
2022-09-19 02:58:27 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-07 08:39:07 +00:00
|
|
|
#if __has_include(<ksandbox.h>)
|
2022-09-19 02:58:27 +00:00
|
|
|
inline QString IconName() {
|
|
|
|
static const auto Result = KSandbox::isFlatpak()
|
2022-11-07 08:39:07 +00:00
|
|
|
? qEnvironmentVariable("FLATPAK_ID")
|
2022-11-29 21:46:36 +00:00
|
|
|
: u"telegram"_q;
|
2022-09-19 02:58:27 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-27 13:16:43 +00:00
|
|
|
inline bool CanReadDirectory(const QString &path) {
|
|
|
|
#ifndef Q_OS_MAC // directory_iterator since 10.15
|
2023-08-25 20:53:51 +00:00
|
|
|
std::error_code error;
|
|
|
|
std::filesystem::directory_iterator(path.toStdString(), error);
|
|
|
|
return !error;
|
2022-11-27 13:16:43 +00:00
|
|
|
#else
|
|
|
|
Unexpected("Not implemented.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
} // namespace base
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
QString translitRusEng(const QString &rus);
|
|
|
|
QString rusKeyboardLayoutSwitch(const QString &from);
|
|
|
|
|
|
|
|
inline int rowscount(int fullCount, int countPerRow) {
|
|
|
|
return (fullCount + countPerRow - 1) / countPerRow;
|
|
|
|
}
|
|
|
|
inline int floorclamp(int value, int step, int lowest, int highest) {
|
2021-01-23 03:29:50 +00:00
|
|
|
return std::clamp(value / step, lowest, highest);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
inline int floorclamp(float64 value, int step, int lowest, int highest) {
|
2021-01-23 03:29:50 +00:00
|
|
|
return std::clamp(
|
|
|
|
static_cast<int>(std::floor(value / step)),
|
|
|
|
lowest,
|
|
|
|
highest);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
inline int ceilclamp(int value, int step, int lowest, int highest) {
|
2021-01-23 03:29:50 +00:00
|
|
|
return std::clamp((value + step - 1) / step, lowest, highest);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) {
|
2021-01-23 03:29:50 +00:00
|
|
|
return std::clamp(
|
|
|
|
static_cast<int>(std::ceil(value / step)),
|
|
|
|
lowest,
|
|
|
|
highest);
|
2016-09-29 11:37:16 +00:00
|
|
|
}
|