From 02f8fe19c0fa18adc3f50e496bbe887154cfc799 Mon Sep 17 00:00:00 2001 From: Jorrit Wronski Date: Fri, 23 Dec 2016 07:53:55 +0100 Subject: [PATCH] Fixed some documentation and corrected the encryption detection. --- include/SQLiteCpp/Database.h | 18 ++++++++++++------ src/Database.cpp | 29 ++++++++++++++++------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/include/SQLiteCpp/Database.h b/include/SQLiteCpp/Database.h index fd624d5..293c330 100644 --- a/include/SQLiteCpp/Database.h +++ b/include/SQLiteCpp/Database.h @@ -375,25 +375,29 @@ public: * This is the equivalent of the sqlite3_key call and should thus be called * directly after opening the database. If the database is unencrypted, * this methods encrypts it immediately. + * Normal database -> call db.key("secret") -> encrypted database, database ready + * Encrypted database -> call db.key("secret") -> database ready * - * @param[in] key Key to decode/encode the database + * @param[in] aKey Key to decode/encode the database * * @throw SQLite::Exception in case of error */ - void key(const std::string& aKey) const noexcept; // nothrow + void key(const std::string& aKey) const; /** * @brief Reset the key for the current sqlite database instance. * * This is the equivalent of the sqlite3_rekey call and should thus be called * after the database has been opened with a valid key. To decrypt a - * database, call this method with a NULL pointer. + * database, call this method with an empty string. + * Encrypted database -> call db.key("secret") -> call db.rekey("newsecret") -> change key, database ready + * Encrypted database -> call db.key("secret") -> call db.rekey("") -> decrypted database, database ready * - * @param[in] nkey New key to encode the database + * @param[in] aNewKey New key to encode the database * * @throw SQLite::Exception in case of error */ - void rekey(const std::string& aNewKey) const noexcept; // nothrow + void rekey(const std::string& aNewKey) const; /** * @brief Test if a file contains an unencrypted database. @@ -406,8 +410,10 @@ public: * @param[in] aFilename path/uri to a file * * @return true if the database has the standard header. + * + * @throw SQLite::Exception in case of error */ - const bool isUnencrypted(const std::string& aFilename) const noexcept; // nothrow + const bool isUnencrypted(const std::string& aFilename) const; private: /// @{ Database must be non-copyable diff --git a/src/Database.cpp b/src/Database.cpp index 30cf177..f24a7fd 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -56,7 +56,7 @@ Database::Database(const char* apFilename, mpSQLite(NULL), mFilename(apFilename) { - int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); + const int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs); if (SQLITE_OK != ret) { const SQLite::Exception exception(mpSQLite, ret); // must create before closing @@ -77,7 +77,7 @@ Database::Database(const std::string& aFilename, mpSQLite(NULL), mFilename(aFilename) { - int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str()); + const int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str()); if (SQLITE_OK != ret) { const SQLite::Exception exception(mpSQLite, ret); // must create before closing @@ -107,8 +107,8 @@ Database::~Database() noexcept // nothrow * @brief Set a busy handler that sleeps for a specified amount of time when a table is locked. * * This is useful in multithreaded program to handle case where a table is locked for writting by a thread. - * Any other thread cannot access the table and will receive a SQLITE_BUSY error: - * setting a timeout will wait and retry up to the time specified before returning this SQLITE_BUSY error. + * Any other thread cannot access the table and will receive a SQLITE_BUSY error: + * setting a timeout will wait and retry up to the time specified before returning this SQLITE_BUSY error. * Reading the value of timeout for current connection can be done with SQL query "PRAGMA busy_timeout;". * Default busy timeout is 0ms. * @@ -229,12 +229,12 @@ void Database::loadExtension(const char* apExtensionName, const char *apEntryPoi } //Set the key for the current sqlite database instance. -void Database::key(const std::string& aKey) const noexcept // nothrow +void Database::key(const std::string& aKey) const { int pass_len = aKey.length(); #ifdef SQLITE_HAS_CODEC if (pass_len > 0) { - int ret = sqlite3_key(mpSQLite, aKey.c_str(), pass_len); + const int ret = sqlite3_key(mpSQLite, aKey.c_str(), pass_len); check(ret); } #else @@ -246,15 +246,15 @@ void Database::key(const std::string& aKey) const noexcept // nothrow } // Reset the key for the current sqlite database instance. -void Database::rekey(const std::string& aNewKey) const noexcept // nothrow +void Database::rekey(const std::string& aNewKey) const { #ifdef SQLITE_HAS_CODEC int pass_len = aNewKey.length(); if (pass_len > 0) { - int ret = sqlite3_rekey(mpSQLite, aNewKey.c_str(), pass_len); + const int ret = sqlite3_rekey(mpSQLite, aNewKey.c_str(), pass_len); check(ret); } else { - int ret = sqlite3_rekey(mpSQLite, nullptr, 0); + const int ret = sqlite3_rekey(mpSQLite, nullptr, 0); check(ret); } #else @@ -264,9 +264,8 @@ void Database::rekey(const std::string& aNewKey) const noexcept // nothrow } //Test if a file contains an unencrypted database. -const bool Database::isUnencrypted(const std::string& aFilename) const noexcept // nothrow +const bool Database::isUnencrypted(const std::string& aFilename) const { - bool encrypted_db = false; if (aFilename.length() > 0) { std::ifstream fileBuffer(aFilename, std::ios::in | std::ios::binary); char header[16]; @@ -274,10 +273,14 @@ const bool Database::isUnencrypted(const std::string& aFilename) const noexcept fileBuffer.seekg(0, std::ios::beg); fileBuffer.getline(header, 16); fileBuffer.close(); + } else { + const SQLite::Exception exception("Error opening file: " + aFilename); + throw exception; } - encrypted_db = strncmp(header, "SQLite format 3\000", 16) != 0; + return strncmp(header, "SQLite format 3\000", 16) == 0; } - return encrypted_db; + const SQLite::Exception exception("Could not open database, the aFilename parameter was empty."); + throw exception; } } // namespace SQLite