@@ -25,15 +25,6 @@ template <> static inline uint32_t FixEndianness(uint32_t val) { return __builti
2525template <> static inline uint64_t FixEndianness (uint64_t val) { return __builtin_bswap64 (val); }
2626#endif
2727
28- static const char * MMapEncryptedFileErrors[] = {
29- " " ,
30- " Encrypted file doesn't have a full header" ,
31- " Encrypted file header's magic number doesn't match" ,
32- " Unsupported version" ,
33- " Encrypted file is shorter than the header declares" ,
34- " Wrong encryption key"
35- };
36-
3728class MMapEncryptedFile
3829{
3930public:
@@ -68,10 +59,10 @@ class MMapEncryptedFile
6859 void open (const std::string& filePath, const uint8_t *key, bool readOnly = false ) {
6960 file_.open (filePath, readOnly);
7061 aes_.setKey (key);
71- int result = readHeader ();
72- if (result != 0 )
73- {
74- throw std::runtime_error (std::string (" Failed to open encrypted file: " ) + filePath + " , error: " + MMapEncryptedFileErrors[result] );
62+ try {
63+ readHeader ();
64+ } catch ( const std::exception& e) {
65+ throw std::runtime_error (std::string (" Failed to open encrypted file: " ) + filePath + " , error: " + e. what () );
7566 }
7667 // resize the file to the correct size (for handling the case when the app crashes before the file is closed)
7768 if (!file_.readOnly ()) [[likely]] {
@@ -173,36 +164,44 @@ class MMapEncryptedFile
173164
174165 static const uint16_t MAGIC = 0xDA7A ;
175166
176- int readHeader () {
167+ void readHeader () {
177168 if (file_.size () == 0 ) {
178169 initHeader ();
179- return 0 ;
170+ return ;
180171 }
181172 if (file_.size () < sizeof (EncryptedFileHeader)) {
182- return 1 ;
173+ throw std::runtime_error ( " Encrypted file doesn't have a full header, size: " + std::to_string (file_. size ())) ;
183174 }
184175
185176 EncryptedFileHeader* header = reinterpret_cast <EncryptedFileHeader*>(file_.data ());
186177 if (FixEndianness (header->magic ) != MAGIC) {
187- return 2 ;
178+ throw std::runtime_error (" Encrypted file header's magic number doesn't match, expected: " +
179+ std::to_string (MAGIC) +
180+ " , got: " +
181+ std::to_string (FixEndianness (header->magic )));
188182 }
183+
189184 if (FixEndianness (header->version ) != 1 ) {
190- return 3 ;
185+ throw std::runtime_error ( " Unsupported version: " + std::to_string ( FixEndianness (header-> version ))) ;
191186 }
187+
192188 if (FixEndianness (header->size ) + sizeof (EncryptedFileHeader) > file_.size ()) {
193- return 4 ;
189+ throw std::runtime_error (" Encrypted file is shorter than the header declares, declared size: " +
190+ std::to_string (sizeof (EncryptedFileHeader)) + " +" +
191+ std::to_string (FixEndianness (header->size )) +
192+ " , actual size: " +
193+ std::to_string (file_.size ()));
194194 }
195195
196196 // decrypt data key
197197 uint8_t dataKey[16 ];
198198 aes_.decryptBlock (header->encryptedDataKey , dataKey);
199199
200200 if (FixEndianness (header->keyHash ) != hashIVAndKey (header->iv , dataKey)) {
201- return 5 ;
201+ return throw std::runtime_error ( " Wrong encryption key, hash mismatch " ) ;
202202 }
203203
204204 aesData_.setKey (dataKey);
205- return 0 ;
206205 }
207206
208207 void initHeader () {
0 commit comments