From 355c41643beadff74d490dc36f2c0432a2286e4c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 24 Nov 2017 13:15:32 +0300 Subject: more common database code moved to MDatabaseCommon --- plugins/Dbx_mdb/src/dbcrypt.cpp | 78 +++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 49 deletions(-) (limited to 'plugins/Dbx_mdb/src/dbcrypt.cpp') diff --git a/plugins/Dbx_mdb/src/dbcrypt.cpp b/plugins/Dbx_mdb/src/dbcrypt.cpp index 3699b97e6a..9e382ae84d 100644 --- a/plugins/Dbx_mdb/src/dbcrypt.cpp +++ b/plugins/Dbx_mdb/src/dbcrypt.cpp @@ -25,8 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ///////////////////////////////////////////////////////////////////////////////////////// -char DBKey_Crypto_Provider [] = "Provider"; -char DBKey_Crypto_Key [] = "Key"; +char DBKey_Crypto_Provider[] = "Provider"; +char DBKey_Crypto_Key[] = "Key"; char DBKey_Crypto_IsEncrypted[] = "EncryptedDB"; CRYPTO_PROVIDER* CDbxMdb::SelectProvider() @@ -40,8 +40,7 @@ CRYPTO_PROVIDER* CDbxMdb::SelectProvider() bool bTotalCrypt = false; - if (iNumProvs > 1) - { + if (iNumProvs > 1) { CSelectCryptoDialog dlg(ppProvs, iNumProvs); dlg.DoModal(); pProv = dlg.GetSelected(); @@ -49,8 +48,7 @@ CRYPTO_PROVIDER* CDbxMdb::SelectProvider() } else pProv = ppProvs[0]; - for (;; Remap()) - { + for (;; Remap()) { txn_ptr txn(m_pMdbEnv); MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value = { pProv->pszName, mir_strlen(pProv->pszName) + 1 }; @@ -73,36 +71,29 @@ int CDbxMdb::InitCrypt() txn_ptr_ro txn(m_txn); MDBX_val key = { DBKey_Crypto_Provider, sizeof(DBKey_Crypto_Provider) }, value; - if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS) - { + if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS) { pProvider = Crypto_GetProvider((const char*)value.iov_base); if (pProvider == nullptr) pProvider = SelectProvider(); } - else - { - pProvider = SelectProvider(); - } - if (pProvider == nullptr) + else pProvider = SelectProvider(); + + if (pProvider == nullptr) return 1; if ((m_crypto = pProvider->pFactory()) == nullptr) return 3; key.iov_len = sizeof(DBKey_Crypto_Key); key.iov_base = DBKey_Crypto_Key; - if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS && (value.iov_len == m_crypto->getKeyLength())) - { - if (!m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) - { + if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS && (value.iov_len == m_crypto->getKeyLength())) { + if (!m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) { DlgChangePassParam param = { this }; CEnterPasswordDialog dlg(¶m); - while (true) - { + while (true) { if (-128 != dlg.DoModal()) return 4; m_crypto->setPassword(pass_ptrA(mir_utf8encodeW(param.newPass))); - if (m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) - { + if (m_crypto->setKey((const BYTE*)value.iov_base, value.iov_len)) { m_bUsesPassword = true; SecureZeroMemory(¶m, sizeof(param)); break; @@ -111,18 +102,17 @@ int CDbxMdb::InitCrypt() } } } - else - { + else { if (!m_crypto->generateKey()) return 6; StoreKey(); } key.iov_len = sizeof(DBKey_Crypto_IsEncrypted); key.iov_base = DBKey_Crypto_IsEncrypted; - + if (mdbx_get(txn, m_dbCrypto, &key, &value) == MDBX_SUCCESS) m_bEncrypted = *(const bool*)value.iov_base; - else + else m_bEncrypted = false; InitDialogs(); @@ -135,8 +125,7 @@ void CDbxMdb::StoreKey() BYTE *pKey = (BYTE*)_alloca(iKeyLength); m_crypto->getKey(pKey, iKeyLength); - for (;; Remap()) - { + for (;; Remap()) { txn_ptr txn(m_pMdbEnv); MDBX_val key = { DBKey_Crypto_Key, sizeof(DBKey_Crypto_Key) }, value = { pKey, iKeyLength }; mdbx_put(txn, m_dbCrypto, &key, &value, 0); @@ -146,15 +135,13 @@ void CDbxMdb::StoreKey() SecureZeroMemory(pKey, iKeyLength); } -void CDbxMdb::SetPassword(LPCTSTR ptszPassword) +void CDbxMdb::SetPassword(const wchar_t *ptszPassword) { - if (ptszPassword == NULL || *ptszPassword == 0) - { + if (ptszPassword == NULL || *ptszPassword == 0) { m_bUsesPassword = false; m_crypto->setPassword(NULL); } - else - { + else { m_bUsesPassword = true; m_crypto->setPassword(pass_ptrA(mir_utf8encodeW(ptszPassword))); } @@ -168,7 +155,6 @@ int CDbxMdb::EnableEncryption(bool bEncrypted) if (m_bEncrypted == bEncrypted) return 0; - { txn_ptr_ro txn(m_txn); @@ -181,14 +167,12 @@ int CDbxMdb::EnableEncryption(bool bEncrypted) { cursor_ptr_ro cursor(m_curEvents); MDBX_val key, data; - while (mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT) == MDBX_SUCCESS) - { + while (mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT) == MDBX_SUCCESS) { const MEVENT hDbEvent = *(const MEVENT*)key.iov_base; lstEvents.push_back(hDbEvent); } } - for (auto it = lstEvents.begin(); it != lstEvents.end(); ++it) - { + for (auto it = lstEvents.begin(); it != lstEvents.end(); ++it) { MEVENT &hDbEvent = *it; MDBX_val key = { &hDbEvent, sizeof(MEVENT) }, data; mdbx_get(txn, m_dbEvents, &key, &data); @@ -196,27 +180,23 @@ int CDbxMdb::EnableEncryption(bool bEncrypted) const DBEvent *dbEvent = (const DBEvent*)data.iov_base; const BYTE *pBlob = (BYTE*)(dbEvent + 1); - if (((dbEvent->flags & DBEF_ENCRYPTED) != 0) != bEncrypted) - { + if (((dbEvent->flags & DBEF_ENCRYPTED) != 0) != bEncrypted) { mir_ptr pNewBlob; size_t nNewBlob; uint32_t dwNewFlags; - if (dbEvent->flags & DBEF_ENCRYPTED) - { + if (dbEvent->flags & DBEF_ENCRYPTED) { pNewBlob = (BYTE*)m_crypto->decodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); dwNewFlags = dbEvent->flags & (~DBEF_ENCRYPTED); } - else - { + else { pNewBlob = m_crypto->encodeBuffer(pBlob, dbEvent->cbBlob, &nNewBlob); dwNewFlags = dbEvent->flags | DBEF_ENCRYPTED; } - for (;; Remap()) - { + for (;; Remap()) { txn_ptr txn(m_pMdbEnv); - data.iov_len = sizeof(DBEvent)+nNewBlob; + data.iov_len = sizeof(DBEvent) + nNewBlob; MDBX_CHECK(mdbx_put(txn, m_dbEvents, &key, &data, MDBX_RESERVE), 1); DBEvent *pNewDBEvent = (DBEvent *)data.iov_base; @@ -233,14 +213,14 @@ int CDbxMdb::EnableEncryption(bool bEncrypted) } } - for (;; Remap()) - { + for (;; Remap()) { txn_ptr txn(m_pMdbEnv); MDBX_val key = { DBKey_Crypto_IsEncrypted, sizeof(DBKey_Crypto_IsEncrypted) }, value = { &bEncrypted, sizeof(bool) }; MDBX_CHECK(mdbx_put(txn, m_dbCrypto, &key, &value, 0), 1); if (txn.commit() == MDBX_SUCCESS) break; } + m_bEncrypted = bEncrypted; return 0; -} \ No newline at end of file +} -- cgit v1.2.3