storage_file_utilities.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "storage/storage_account.h"
  9. #include <QtCore/QBuffer>
  10. namespace Storage {
  11. namespace details {
  12. [[nodiscard]] QString ToFilePart(FileKey val);
  13. [[nodiscard]] bool KeyAlreadyUsed(QString &name);
  14. [[nodiscard]] FileKey GenerateKey(const QString &basePath);
  15. void ClearKey(const FileKey &key, const QString &basePath);
  16. [[nodiscard]] bool CheckStreamStatus(QDataStream &stream);
  17. [[nodiscard]] MTP::AuthKeyPtr CreateLocalKey(
  18. const QByteArray &passcode,
  19. const QByteArray &salt);
  20. [[nodiscard]] MTP::AuthKeyPtr CreateLegacyLocalKey(
  21. const QByteArray &passcode,
  22. const QByteArray &salt);
  23. struct FileReadDescriptor final {
  24. ~FileReadDescriptor();
  25. int32 version = 0;
  26. QByteArray data;
  27. QBuffer buffer;
  28. QDataStream stream;
  29. };
  30. struct EncryptedDescriptor final {
  31. EncryptedDescriptor();
  32. explicit EncryptedDescriptor(uint32 size);
  33. ~EncryptedDescriptor();
  34. void finish();
  35. QByteArray data;
  36. QBuffer buffer;
  37. QDataStream stream;
  38. };
  39. [[nodiscard]] QByteArray PrepareEncrypted(
  40. EncryptedDescriptor &data,
  41. const MTP::AuthKeyPtr &key);
  42. class FileWriteDescriptor final {
  43. public:
  44. FileWriteDescriptor(
  45. const FileKey &key,
  46. const QString &basePath,
  47. bool sync = false);
  48. FileWriteDescriptor(
  49. const QString &name,
  50. const QString &basePath,
  51. bool sync = false);
  52. ~FileWriteDescriptor();
  53. void writeData(const QByteArray &data);
  54. void writeEncrypted(
  55. EncryptedDescriptor &data,
  56. const MTP::AuthKeyPtr &key);
  57. private:
  58. void init(const QString &name);
  59. void finish();
  60. const QString _basePath;
  61. QBuffer _buffer;
  62. QDataStream _stream;
  63. QByteArray _safeData;
  64. QString _base;
  65. HashMd5 _md5;
  66. int _fullSize = 0;
  67. bool _sync = false;
  68. };
  69. bool ReadFile(
  70. FileReadDescriptor &result,
  71. const QString &name,
  72. const QString &basePath);
  73. bool DecryptLocal(
  74. EncryptedDescriptor &result,
  75. const QByteArray &encrypted,
  76. const MTP::AuthKeyPtr &key);
  77. bool ReadEncryptedFile(
  78. FileReadDescriptor &result,
  79. const QString &name,
  80. const QString &basePath,
  81. const MTP::AuthKeyPtr &key);
  82. bool ReadEncryptedFile(
  83. FileReadDescriptor &result,
  84. const FileKey &fkey,
  85. const QString &basePath,
  86. const MTP::AuthKeyPtr &key);
  87. void Sync();
  88. void Finish();
  89. } // namespace details
  90. } // namespace Storage