1 /* 2 This file is part of Telegram Desktop, 3 the official desktop application for the Telegram messaging service. 4 5 For license and copyright information please follow this link: 6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL 7 */ 8 #pragma once 9 10 #include "storage/storage_account.h" 11 12 #include <QtCore/QBuffer> 13 14 namespace Storage { 15 namespace details { 16 17 [[nodiscard]] QString ToFilePart(FileKey val); 18 [[nodiscard]] bool KeyAlreadyUsed(QString &name); 19 [[nodiscard]] FileKey GenerateKey(const QString &basePath); 20 void ClearKey(const FileKey &key, const QString &basePath); 21 22 [[nodiscard]] bool CheckStreamStatus(QDataStream &stream); 23 [[nodiscard]] MTP::AuthKeyPtr CreateLocalKey( 24 const QByteArray &passcode, 25 const QByteArray &salt); 26 [[nodiscard]] MTP::AuthKeyPtr CreateLegacyLocalKey( 27 const QByteArray &passcode, 28 const QByteArray &salt); 29 30 struct FileReadDescriptor final { 31 ~FileReadDescriptor(); 32 33 int32 version = 0; 34 QByteArray data; 35 QBuffer buffer; 36 QDataStream stream; 37 }; 38 39 struct EncryptedDescriptor final { 40 EncryptedDescriptor(); 41 explicit EncryptedDescriptor(uint32 size); 42 ~EncryptedDescriptor(); 43 44 void finish(); 45 46 QByteArray data; 47 QBuffer buffer; 48 QDataStream stream; 49 }; 50 51 [[nodiscard]] QByteArray PrepareEncrypted( 52 EncryptedDescriptor &data, 53 const MTP::AuthKeyPtr &key); 54 55 class FileWriteDescriptor final { 56 public: 57 FileWriteDescriptor( 58 const FileKey &key, 59 const QString &basePath, 60 bool sync = false); 61 FileWriteDescriptor( 62 const QString &name, 63 const QString &basePath, 64 bool sync = false); 65 ~FileWriteDescriptor(); 66 67 void writeData(const QByteArray &data); 68 void writeEncrypted( 69 EncryptedDescriptor &data, 70 const MTP::AuthKeyPtr &key); 71 72 private: 73 void init(const QString &name); 74 void finish(); 75 76 const QString _basePath; 77 QBuffer _buffer; 78 QDataStream _stream; 79 QByteArray _safeData; 80 QString _base; 81 HashMd5 _md5; 82 int _fullSize = 0; 83 bool _sync = false; 84 85 }; 86 87 bool ReadFile( 88 FileReadDescriptor &result, 89 const QString &name, 90 const QString &basePath); 91 92 bool DecryptLocal( 93 EncryptedDescriptor &result, 94 const QByteArray &encrypted, 95 const MTP::AuthKeyPtr &key); 96 97 bool ReadEncryptedFile( 98 FileReadDescriptor &result, 99 const QString &name, 100 const QString &basePath, 101 const MTP::AuthKeyPtr &key); 102 103 bool ReadEncryptedFile( 104 FileReadDescriptor &result, 105 const FileKey &fkey, 106 const QString &basePath, 107 const MTP::AuthKeyPtr &key); 108 109 void Sync(); 110 void Finish(); 111 112 } // namespace details 113 } // namespace Storage 114