utils.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/basic_types.h"
  9. #include "base/flags.h"
  10. #include "base/algorithm.h"
  11. #include "base/bytes.h"
  12. #include <crl/crl_time.h>
  13. #include <QtCore/QReadWriteLock>
  14. #include <QtCore/QRegularExpression>
  15. #include <QtCore/QMimeData>
  16. #include <QtNetwork/QNetworkProxy>
  17. #include <cmath>
  18. #include <set>
  19. #include <filesystem>
  20. #define qsl(s) QStringLiteral(s)
  21. namespace base {
  22. template <typename Value, typename From, typename Till>
  23. inline bool in_range(Value &&value, From &&from, Till &&till) {
  24. return (value >= from) && (value < till);
  25. }
  26. inline bool CanReadDirectory(const QString &path) {
  27. #ifndef Q_OS_MAC // directory_iterator since 10.15
  28. std::error_code error;
  29. std::filesystem::directory_iterator(path.toStdString(), error);
  30. return !error;
  31. #else
  32. Unexpected("Not implemented.");
  33. #endif
  34. }
  35. } // namespace base
  36. static const int32 ScrollMax = INT_MAX;
  37. extern uint64 _SharedMemoryLocation[];
  38. template <typename T, unsigned int N>
  39. T *SharedMemoryLocation() {
  40. static_assert(N < 4, "Only 4 shared memory locations!");
  41. return reinterpret_cast<T*>(_SharedMemoryLocation + N);
  42. }
  43. inline void mylocaltime(struct tm * _Tm, const time_t * _Time) {
  44. #ifdef Q_OS_WIN
  45. localtime_s(_Tm, _Time);
  46. #else
  47. localtime_r(_Time, _Tm);
  48. #endif
  49. }
  50. namespace ThirdParty {
  51. void start();
  52. void finish();
  53. } // namespace ThirdParty
  54. const static uint32 _md5_block_size = 64;
  55. class HashMd5 {
  56. public:
  57. HashMd5(const void *input = 0, uint32 length = 0);
  58. void feed(const void *input, uint32 length);
  59. int32 *result();
  60. private:
  61. void init();
  62. void finalize();
  63. void transform(const uchar *block);
  64. bool _finalized;
  65. uchar _buffer[_md5_block_size];
  66. uint32 _count[2];
  67. uint32 _state[4];
  68. uchar _digest[16];
  69. };
  70. int32 *hashSha1(const void *data, uint32 len, void *dest); // dest - ptr to 20 bytes, returns (int32*)dest
  71. inline std::array<char, 20> hashSha1(const void *data, int size) {
  72. auto result = std::array<char, 20>();
  73. hashSha1(data, size, result.data());
  74. return result;
  75. }
  76. int32 *hashSha256(const void *data, uint32 len, void *dest); // dest - ptr to 32 bytes, returns (int32*)dest
  77. inline std::array<char, 32> hashSha256(const void *data, int size) {
  78. auto result = std::array<char, 32>();
  79. hashSha256(data, size, result.data());
  80. return result;
  81. }
  82. int32 *hashMd5(const void *data, uint32 len, void *dest); // dest = ptr to 16 bytes, returns (int32*)dest
  83. inline std::array<char, 16> hashMd5(const void *data, int size) {
  84. auto result = std::array<char, 16>();
  85. hashMd5(data, size, result.data());
  86. return result;
  87. }
  88. char *hashMd5Hex(const int32 *hashmd5, void *dest); // dest = ptr to 32 bytes, returns (char*)dest
  89. inline char *hashMd5Hex(const void *data, uint32 len, void *dest) { // dest = ptr to 32 bytes, returns (char*)dest
  90. return hashMd5Hex(HashMd5(data, len).result(), dest);
  91. }
  92. inline std::array<char, 32> hashMd5Hex(const void *data, int size) {
  93. auto result = std::array<char, 32>();
  94. hashMd5Hex(data, size, result.data());
  95. return result;
  96. }
  97. QString translitRusEng(const QString &rus);
  98. QString rusKeyboardLayoutSwitch(const QString &from);
  99. inline int rowscount(int fullCount, int countPerRow) {
  100. return (fullCount + countPerRow - 1) / countPerRow;
  101. }
  102. inline int floorclamp(int value, int step, int lowest, int highest) {
  103. return std::clamp(value / step, lowest, highest);
  104. }
  105. inline int floorclamp(float64 value, int step, int lowest, int highest) {
  106. return std::clamp(
  107. static_cast<int>(std::floor(value / step)),
  108. lowest,
  109. highest);
  110. }
  111. inline int ceilclamp(int value, int step, int lowest, int highest) {
  112. return std::clamp((value + step - 1) / step, lowest, highest);
  113. }
  114. inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) {
  115. return std::clamp(
  116. static_cast<int>(std::ceil(value / step)),
  117. lowest,
  118. highest);
  119. }