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 --- include/m_db_int.h | 12 ++++ libs/win32/mir_app.lib | Bin 127416 -> 130060 bytes libs/win64/mir_app.lib | Bin 122916 -> 125634 bytes plugins/Db3x_mmap/src/dbcontacts.cpp | 58 ------------------ plugins/Db3x_mmap/src/dbcrypt.cpp | 2 +- plugins/Db3x_mmap/src/dbintf.h | 16 ++--- plugins/Db3x_mmap/src/dbsettings.cpp | 17 ------ plugins/Db3x_mmap/src/dbtool/disk.cpp | 2 +- plugins/Dbx_mdb/src/dbcontacts.cpp | 89 +++------------------------- plugins/Dbx_mdb/src/dbcrypt.cpp | 78 +++++++++--------------- plugins/Dbx_mdb/src/dbintf.h | 9 +-- plugins/Dbx_mdb/src/dbsettings.cpp | 17 ------ plugins/Dbx_mdb/src/ui.cpp | 2 +- src/mir_app/src/MDatabaseCommon.cpp | 108 ++++++++++++++++++++++++++++++++++ src/mir_app/src/mir_app.def | 7 +++ src/mir_app/src/mir_app64.def | 7 +++ src/mir_core/src/db.cpp | 16 +---- 17 files changed, 181 insertions(+), 259 deletions(-) diff --git a/include/m_db_int.h b/include/m_db_int.h index ba93fad62c..883a109894 100644 --- a/include/m_db_int.h +++ b/include/m_db_int.h @@ -109,6 +109,7 @@ interface MIR_APP_EXPORT MIDatabase STDMETHOD_(MEVENT, FindNextEvent)(MCONTACT contactID, MEVENT hDbEvent) PURE; STDMETHOD_(MEVENT, FindPrevEvent)(MCONTACT contactID, MEVENT hDbEvent) PURE; + STDMETHOD_(BOOL, DeleteModule)(MCONTACT contactID, LPCSTR szModule) PURE; STDMETHOD_(BOOL, EnumModuleNames)(DBMODULEENUMPROC pFunc, void *pParam) PURE; STDMETHOD_(BOOL, GetContactSetting)(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv) PURE; @@ -140,9 +141,20 @@ protected: protected: MDatabaseCommon(); + int CheckProto(DBCachedContact *cc, const char *proto); + STDMETHOD_(BOOL, GetContactSettingWorker)(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv, int isStatic) PURE; public: + STDMETHODIMP_(BOOL) DeleteModule(MCONTACT contactID, LPCSTR szModule); + + STDMETHODIMP_(MCONTACT) FindFirstContact(const char *szProto = nullptr); + STDMETHODIMP_(MCONTACT) FindNextContact(MCONTACT contactID, const char *szProto = nullptr); + + STDMETHODIMP_(BOOL) MetaDetouchSub(DBCachedContact *cc, int nSub); + STDMETHODIMP_(BOOL) MetaSetDefault(DBCachedContact *cc); + + STDMETHODIMP_(BOOL) IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting); STDMETHODIMP_(BOOL) GetContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv); STDMETHODIMP_(BOOL) GetContactSettingStr(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv); STDMETHODIMP_(BOOL) GetContactSettingStatic(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv); diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index e6769d1692..0b18135672 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index d50aca166c..70b27a44f0 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/plugins/Db3x_mmap/src/dbcontacts.cpp b/plugins/Db3x_mmap/src/dbcontacts.cpp index 55e4aa8c23..b9d1eb614d 100644 --- a/plugins/Db3x_mmap/src/dbcontacts.cpp +++ b/plugins/Db3x_mmap/src/dbcontacts.cpp @@ -23,23 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" -int CDb3Mmap::CheckProto(DBCachedContact *cc, const char *proto) -{ - if (cc->szProto == nullptr) { - char protobuf[MAX_PATH] = { 0 }; - DBVARIANT dbv; - dbv.type = DBVT_ASCIIZ; - dbv.pszVal = protobuf; - dbv.cchVal = sizeof(protobuf); - if (GetContactSettingStatic(cc->contactID, "Protocol", "p", &dbv) != 0 || (dbv.type != DBVT_ASCIIZ)) - return 0; - - cc->szProto = m_cache->GetCachedSetting(nullptr, protobuf, 0, (int)mir_strlen(protobuf)); - } - - return !mir_strcmp(cc->szProto, proto); -} - STDMETHODIMP_(LONG) CDb3Mmap::GetContactCount(void) { mir_cslock lck(m_csDbAccess); @@ -51,36 +34,6 @@ STDMETHODIMP_(LONG) CDb3Mmap::GetContactSize(void) return sizeof(DBCachedContact); } -STDMETHODIMP_(MCONTACT) CDb3Mmap::FindFirstContact(const char *szProto) -{ - mir_cslock lck(m_csDbAccess); - DBCachedContact *cc = m_cache->GetFirstContact(); - if (cc == nullptr) - return 0; - - if (!szProto || CheckProto(cc, szProto)) - return cc->contactID; - - return FindNextContact(cc->contactID, szProto); -} - -STDMETHODIMP_(MCONTACT) CDb3Mmap::FindNextContact(MCONTACT contactID, const char *szProto) -{ - mir_cslock lck(m_csDbAccess); - while (contactID) { - DBCachedContact *cc = m_cache->GetNextContact(contactID); - if (cc == nullptr) - break; - - if (!szProto || CheckProto(cc, szProto)) - return cc->contactID; - - contactID = cc->contactID; - } - - return 0; -} - STDMETHODIMP_(LONG) CDb3Mmap::DeleteContact(MCONTACT contactID) { if (contactID == 0) // global contact cannot be removed @@ -206,17 +159,6 @@ STDMETHODIMP_(BOOL) CDb3Mmap::IsDbContact(MCONTACT contactID) ///////////////////////////////////////////////////////////////////////////////////////// // metacontacts support -BOOL CDb3Mmap::MetaDetouchSub(DBCachedContact *cc, int nSub) -{ - db_delete_module(cc->pSubs[nSub], META_PROTO); - return 0; -} - -BOOL CDb3Mmap::MetaSetDefault(DBCachedContact *cc) -{ - return db_set_dw(cc->contactID, META_PROTO, "Default", cc->nDefault); -} - static int SortEvent(const DBEvent *p1, const DBEvent *p2) { return (LONG)p1->timestamp - (LONG)p2->timestamp; diff --git a/plugins/Db3x_mmap/src/dbcrypt.cpp b/plugins/Db3x_mmap/src/dbcrypt.cpp index d2368e5b68..608cfa689d 100644 --- a/plugins/Db3x_mmap/src/dbcrypt.cpp +++ b/plugins/Db3x_mmap/src/dbcrypt.cpp @@ -225,7 +225,7 @@ void CDb3Mmap::StoreKey() SecureZeroMemory(pKey, iKeyLength); } -void CDb3Mmap::SetPassword(LPCTSTR ptszPassword) +void CDb3Mmap::SetPassword(const wchar_t *ptszPassword) { if (ptszPassword == nullptr || *ptszPassword == 0) { m_bUsesPassword = false; diff --git a/plugins/Db3x_mmap/src/dbintf.h b/plugins/Db3x_mmap/src/dbintf.h index 1a6024bb20..4a194063a7 100644 --- a/plugins/Db3x_mmap/src/dbintf.h +++ b/plugins/Db3x_mmap/src/dbintf.h @@ -89,13 +89,13 @@ struct ModuleName #include struct DBHeader { - BYTE signature[16]; // 'Miranda ICQ DB',0,26 + BYTE signature[16]; // 'Miranda ICQ DB',0,26 DWORD version; // as 4 bytes, ie 1.2.3.10 = 0x0102030a DWORD ofsFileEnd; // offset of the end of the database - place to write new structures DWORD slackSpace; // a counter of the number of bytes that have been - // wasted so far due to deleting structures and/or - // re-making them at the end. We should compact when - // this gets above a threshold + // wasted so far due to deleting structures and/or + // re-making them at the end. We should compact when + // this gets above a threshold DWORD contactCount; // number of contacts in the chain,excluding the user DWORD ofsFirstContact; // offset to first DBContact in the chain DWORD ofsUser; // offset to DBContact representing the user @@ -111,7 +111,7 @@ struct DBContact DWORD ofsFirstSettings; // offset to the first DBContactSettings in the chain for this contact. DWORD eventCount; // number of events in the chain for this contact DWORD ofsFirstEvent, // offsets to the first and - ofsLastEvent; // last DBEvent in the chain for this contact + ofsLastEvent; // last DBEvent in the chain for this contact DWORD ofsFirstUnread; // offset to the first (chronological) unread event in the chain, 0 if all are read DWORD tsFirstUnread; // timestamp of the event at ofsFirstUnread DWORD dwContactID; @@ -212,8 +212,6 @@ public: STDMETHODIMP_(void) SetCacheSafetyMode(BOOL); STDMETHODIMP_(LONG) GetContactCount(void); - STDMETHODIMP_(MCONTACT) FindFirstContact(const char *szProto = nullptr); - STDMETHODIMP_(MCONTACT) FindNextContact(MCONTACT contactID, const char *szProto = nullptr); STDMETHODIMP_(LONG) DeleteContact(MCONTACT contactID); STDMETHODIMP_(MCONTACT) AddContact(void); STDMETHODIMP_(BOOL) IsDbContact(MCONTACT contactID); @@ -239,10 +237,7 @@ public: STDMETHODIMP_(BOOL) DeleteContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting); STDMETHODIMP_(BOOL) EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnumProc, const char *szModule, void *param); STDMETHODIMP_(BOOL) EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam); - STDMETHODIMP_(BOOL) IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting); - STDMETHODIMP_(BOOL) MetaDetouchSub(DBCachedContact *cc, int nSub); - STDMETHODIMP_(BOOL) MetaSetDefault(DBCachedContact *cc); STDMETHODIMP_(BOOL) MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); STDMETHODIMP_(BOOL) MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); @@ -298,7 +293,6 @@ protected: DWORD m_dwFileSize, m_dwMaxContactId; HANDLE hSettingChangeEvent, hContactDeletedEvent, hContactAddedEvent, hEventMarkedRead; - int CheckProto(DBCachedContact *cc, const char *proto); DWORD CreateNewSpace(int bytes); void DeleteSpace(DWORD ofs, int bytes); DWORD ReallocSpace(DWORD ofs, int oldSize, int newSize); diff --git a/plugins/Db3x_mmap/src/dbsettings.cpp b/plugins/Db3x_mmap/src/dbsettings.cpp index 76225aec4c..758f80c769 100644 --- a/plugins/Db3x_mmap/src/dbsettings.cpp +++ b/plugins/Db3x_mmap/src/dbsettings.cpp @@ -27,23 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define VLT(n) ((n == DBVT_UTF8 || n == DBVT_ENCRYPTED) ? DBVT_ASCIIZ : n) -BOOL CDb3Mmap::IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting) -{ - if (!_strnicmp(szSetting, "password", 8)) return true; - if (!mir_strcmp(szSetting, "NLProxyAuthPassword")) return true; - if (!mir_strcmp(szSetting, "LNPassword")) return true; - if (!mir_strcmp(szSetting, "FileProxyPassword")) return true; - if (!mir_strcmp(szSetting, "TokenSecret")) return true; - - if (!mir_strcmp(szModule, "SecureIM")) { - if (!mir_strcmp(szSetting, "pgp")) return true; - if (!mir_strcmp(szSetting, "pgpPrivKey")) return true; - } - return false; -} - -///////////////////////////////////////////////////////////////////////////////////////// - static bool ValidLookupName(LPCSTR szModule, LPCSTR szSetting) { if (!mir_strcmp(szModule, META_PROTO)) diff --git a/plugins/Db3x_mmap/src/dbtool/disk.cpp b/plugins/Db3x_mmap/src/dbtool/disk.cpp index da18f9a1d6..3310c7dd67 100644 --- a/plugins/Db3x_mmap/src/dbtool/disk.cpp +++ b/plugins/Db3x_mmap/src/dbtool/disk.cpp @@ -72,7 +72,7 @@ int CDb3Mmap::ReadSegment(DWORD ofs, PVOID buf, int cbBytes) DWORD CDb3Mmap::WriteSegment(DWORD ofs, PVOID buf, int cbBytes) { - DWORD bytesWritten; + DWORD bytesWritten; if (cb->bCheckOnly) return 0xbfbfbfbf; if (ofs == WSOFS_END) { ofs = m_dbHeader.ofsFileEnd; diff --git a/plugins/Dbx_mdb/src/dbcontacts.cpp b/plugins/Dbx_mdb/src/dbcontacts.cpp index 3ae1d7c973..63de975d68 100644 --- a/plugins/Dbx_mdb/src/dbcontacts.cpp +++ b/plugins/Dbx_mdb/src/dbcontacts.cpp @@ -23,23 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" -int CDbxMdb::CheckProto(DBCachedContact *cc, const char *proto) -{ - if (cc->szProto == NULL) { - char protobuf[MAX_PATH] = { 0 }; - DBVARIANT dbv; - dbv.type = DBVT_ASCIIZ; - dbv.pszVal = protobuf; - dbv.cchVal = sizeof(protobuf); - if (GetContactSettingStatic(cc->contactID, "Protocol", "p", &dbv) != 0 || (dbv.type != DBVT_ASCIIZ)) - return 0; - - cc->szProto = m_cache->GetCachedSetting(NULL, protobuf, 0, (int)strlen(protobuf)); - } - - return !strcmp(cc->szProto, proto); -} - STDMETHODIMP_(LONG) CDbxMdb::GetContactCount(void) { return m_contactCount; @@ -50,51 +33,16 @@ STDMETHODIMP_(LONG) CDbxMdb::GetContactSize(void) return sizeof(DBCachedContact); } -STDMETHODIMP_(MCONTACT) CDbxMdb::FindFirstContact(const char *szProto) -{ - DBCachedContact *cc = m_cache->GetFirstContact(); - if (cc == NULL) - return NULL; - - if (cc->contactID == 0) - if ((cc = m_cache->GetNextContact(0)) == NULL) - return NULL; - - if (!szProto || CheckProto(cc, szProto)) - return cc->contactID; - - return FindNextContact(cc->contactID, szProto); -} - -STDMETHODIMP_(MCONTACT) CDbxMdb::FindNextContact(MCONTACT contactID, const char *szProto) -{ - while (contactID) { - DBCachedContact *cc = m_cache->GetNextContact(contactID); - if (cc == NULL) - break; - - if (!szProto || CheckProto(cc, szProto)) - return cc->contactID; - - contactID = cc->contactID; - } - - return NULL; -} - STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID) { if (contactID == 0) // global contact cannot be removed return 1; NotifyEventHooks(hContactDeletedEvent, contactID, 0); - - { OBJLIST events(50); GatherContactHistory(contactID, events); - while (events.getCount()) - { + while (events.getCount()) { DeleteEvent(contactID, events[0].eventId); events.remove(0); } @@ -108,8 +56,7 @@ STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID) key.iov_len = sizeof(keyS); key.iov_base = &keyS; - for (int res = mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE); res == MDBX_SUCCESS; res = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) - { + for (int res = mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE); res == MDBX_SUCCESS; res = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) { const DBSettingKey *pKey = (const DBSettingKey*)key.iov_base; if (pKey->hContact != contactID) break; @@ -120,8 +67,7 @@ STDMETHODIMP_(LONG) CDbxMdb::DeleteContact(MCONTACT contactID) } MDBX_val key = { &contactID, sizeof(MCONTACT) }; - for (;; Remap()) - { + for (;; Remap()) { txn_ptr trnlck(m_pMdbEnv); MDBX_CHECK(mdbx_del(trnlck, m_dbContacts, &key, nullptr), 1); if (trnlck.commit() == MDBX_SUCCESS) @@ -160,20 +106,6 @@ STDMETHODIMP_(BOOL) CDbxMdb::IsDbContact(MCONTACT contactID) return (cc != NULL); } -///////////////////////////////////////////////////////////////////////////////////////// -// metacontacts support - -BOOL CDbxMdb::MetaDetouchSub(DBCachedContact *cc, int nSub) -{ - db_delete_module(cc->pSubs[nSub], META_PROTO); - return 0; -} - -BOOL CDbxMdb::MetaSetDefault(DBCachedContact *cc) -{ - return db_set_dw(cc->contactID, META_PROTO, "Default", cc->nDefault); -} - ///////////////////////////////////////////////////////////////////////////////////////// void CDbxMdb::GatherContactHistory(MCONTACT hContact, LIST &list) @@ -184,8 +116,7 @@ void CDbxMdb::GatherContactHistory(MCONTACT hContact, LIST &list) txn_ptr_ro trnlck(m_txn); cursor_ptr_ro cursor(m_curEventsSort); - for (int res = mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE); res == MDBX_SUCCESS; res = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) - { + for (int res = mdbx_cursor_get(cursor, &key, &data, MDBX_SET_RANGE); res == MDBX_SUCCESS; res = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) { const DBEventSortingKey *pKey = (const DBEventSortingKey*)key.iov_base; if (pKey->hContact != hContact) return; @@ -202,8 +133,7 @@ BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) for (int i = 0; i < list.getCount(); i++) { EventItem *EI = list[i]; - for (;; Remap()) - { + for (;; Remap()) { txn_ptr trnlck(m_pMdbEnv); DBEventSortingKey insVal = { EI->eventId, EI->ts, ccMeta->contactID }; MDBX_val key = { &insVal, sizeof(insVal) }, data = { (void*)"", 1 }; @@ -217,8 +147,7 @@ BOOL CDbxMdb::MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) MDBX_val keyc = { &ccMeta->contactID, sizeof(MCONTACT) }, datac = { &ccMeta->dbc, sizeof(ccMeta->dbc) }; - for (;; Remap()) - { + for (;; Remap()) { txn_ptr trnlck(m_pMdbEnv); MDBX_CHECK(mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1); if (trnlck.commit() == MDBX_SUCCESS) @@ -251,8 +180,7 @@ BOOL CDbxMdb::MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub) MDBX_val keyc = { &ccMeta->contactID, sizeof(MCONTACT) }, datac = { &ccMeta->dbc, sizeof(ccMeta->dbc) }; - for (;; Remap()) - { + for (;; Remap()) { txn_ptr trnlck(m_pMdbEnv); MDBX_CHECK(mdbx_put(trnlck, m_dbContacts, &keyc, &datac, 0), 1); if (trnlck.commit() == MDBX_SUCCESS) @@ -297,8 +225,7 @@ void CDbxMdb::FillContacts() cursor_ptr_ro cursor(m_curContacts); 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) { DBCachedContact *cc = m_cache->AddContactToCache(*(MCONTACT*)key.iov_base); cc->dbc = *(DBContact*)data.iov_base; 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 +} diff --git a/plugins/Dbx_mdb/src/dbintf.h b/plugins/Dbx_mdb/src/dbintf.h index 4c1c757aed..c3cf04c1cf 100644 --- a/plugins/Dbx_mdb/src/dbintf.h +++ b/plugins/Dbx_mdb/src/dbintf.h @@ -139,7 +139,7 @@ struct CDbxMdb : public MDatabaseCommon, public MIDatabaseChecker, public MZeroe void DatabaseCorruption(const TCHAR *ptszText); void StoreKey(void); - void SetPassword(const TCHAR *ptszPassword); + void SetPassword(const wchar_t *ptszPassword); void UpdateMenuItem(void); int PrepareCheck(int*); @@ -154,8 +154,6 @@ public: STDMETHODIMP_(void) SetCacheSafetyMode(BOOL); STDMETHODIMP_(LONG) GetContactCount(void); - STDMETHODIMP_(MCONTACT) FindFirstContact(const char *szProto = NULL); - STDMETHODIMP_(MCONTACT) FindNextContact(MCONTACT contactID, const char *szProto = NULL); STDMETHODIMP_(LONG) DeleteContact(MCONTACT contactID); STDMETHODIMP_(MCONTACT) AddContact(void); STDMETHODIMP_(BOOL) IsDbContact(MCONTACT contactID); @@ -181,10 +179,7 @@ public: STDMETHODIMP_(BOOL) DeleteContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting); STDMETHODIMP_(BOOL) EnumContactSettings(MCONTACT hContact, DBSETTINGENUMPROC pfnEnumProc, const char *szModule, void *param); STDMETHODIMP_(BOOL) EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam); - STDMETHODIMP_(BOOL) IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting); - STDMETHODIMP_(BOOL) MetaDetouchSub(DBCachedContact *cc, int nSub); - STDMETHODIMP_(BOOL) MetaSetDefault(DBCachedContact *cc); STDMETHODIMP_(BOOL) MetaMergeHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); STDMETHODIMP_(BOOL) MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub); @@ -218,8 +213,6 @@ protected: HANDLE hSettingChangeEvent, hContactDeletedEvent, hContactAddedEvent, hEventMarkedRead; - int CheckProto(DBCachedContact *cc, const char *proto); - //////////////////////////////////////////////////////////////////////////// // settings diff --git a/plugins/Dbx_mdb/src/dbsettings.cpp b/plugins/Dbx_mdb/src/dbsettings.cpp index 5e287292f5..5c1750b23c 100644 --- a/plugins/Dbx_mdb/src/dbsettings.cpp +++ b/plugins/Dbx_mdb/src/dbsettings.cpp @@ -25,23 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define VLT(n) ((n == DBVT_UTF8 || n == DBVT_ENCRYPTED)?DBVT_ASCIIZ:n) -BOOL CDbxMdb::IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting) -{ - if (!_strnicmp(szSetting, "password", 8)) return true; - if (!strcmp(szSetting, "NLProxyAuthPassword")) return true; - if (!strcmp(szSetting, "LNPassword")) return true; - if (!strcmp(szSetting, "FileProxyPassword")) return true; - if (!strcmp(szSetting, "TokenSecret")) return true; - - if (!strcmp(szModule, "SecureIM")) { - if (!strcmp(szSetting, "pgp")) return true; - if (!strcmp(szSetting, "pgpPrivKey")) return true; - } - return false; -} - -///////////////////////////////////////////////////////////////////////////////////////// - static bool ValidLookupName(LPCSTR szModule, LPCSTR szSetting) { if (!strcmp(szModule, META_PROTO)) diff --git a/plugins/Dbx_mdb/src/ui.cpp b/plugins/Dbx_mdb/src/ui.cpp index a679984f77..b6fe84f78b 100644 --- a/plugins/Dbx_mdb/src/ui.cpp +++ b/plugins/Dbx_mdb/src/ui.cpp @@ -99,7 +99,7 @@ static INT_PTR CALLBACK sttChangePassword(HWND hwndDlg, UINT uMsg, WPARAM wParam } else { // param->db->WriteSignature(dbSignatureU); - param->db->SetPassword(NULL); + param->db->SetPassword(nullptr); param->db->StoreKey(); EndDialog(hwndDlg, IDREMOVE); } diff --git a/src/mir_app/src/MDatabaseCommon.cpp b/src/mir_app/src/MDatabaseCommon.cpp index 66e16968c4..67e13b8614 100644 --- a/src/mir_app/src/MDatabaseCommon.cpp +++ b/src/mir_app/src/MDatabaseCommon.cpp @@ -35,6 +35,114 @@ MDatabaseCommon::MDatabaseCommon() : m_codePage = Langpack_GetDefaultCodePage(); } +///////////////////////////////////////////////////////////////////////////////////////// + +int MDatabaseCommon::CheckProto(DBCachedContact *cc, const char *proto) +{ + if (cc->szProto == nullptr) { + char protobuf[MAX_PATH] = { 0 }; + DBVARIANT dbv; + dbv.type = DBVT_ASCIIZ; + dbv.pszVal = protobuf; + dbv.cchVal = sizeof(protobuf); + if (GetContactSettingStatic(cc->contactID, "Protocol", "p", &dbv) != 0 || (dbv.type != DBVT_ASCIIZ)) + return 0; + + cc->szProto = m_cache->GetCachedSetting(nullptr, protobuf, 0, (int)mir_strlen(protobuf)); + } + + return !mir_strcmp(cc->szProto, proto); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +static int sttEnumVars(const char *szVarName, void *param) +{ + LIST* vars = (LIST*)param; + vars->insert(mir_strdup(szVarName)); + return 0; +} + +BOOL MDatabaseCommon::DeleteModule(MCONTACT hContact, LPCSTR szModule) +{ + LIST vars(20); + EnumContactSettings(hContact, sttEnumVars, szModule, &vars); + + for (int i = vars.getCount() - 1; i >= 0; i--) { + DeleteContactSetting(hContact, szModule, vars[i]); + mir_free(vars[i]); + } + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +STDMETHODIMP_(MCONTACT) MDatabaseCommon::FindFirstContact(const char *szProto) +{ + mir_cslock lck(m_csDbAccess); + DBCachedContact *cc = m_cache->GetFirstContact(); + if (cc == nullptr) + return 0; + + if (!szProto || CheckProto(cc, szProto)) + return cc->contactID; + + return FindNextContact(cc->contactID, szProto); +} + +STDMETHODIMP_(MCONTACT) MDatabaseCommon::FindNextContact(MCONTACT contactID, const char *szProto) +{ + mir_cslock lck(m_csDbAccess); + while (contactID) { + DBCachedContact *cc = m_cache->GetNextContact(contactID); + if (cc == nullptr) + break; + + if (!szProto || CheckProto(cc, szProto)) + return cc->contactID; + + contactID = cc->contactID; + } + + return 0; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +BOOL MDatabaseCommon::IsSettingEncrypted(LPCSTR szModule, LPCSTR szSetting) +{ + if (!_strnicmp(szSetting, "password", 8)) return true; + if (!mir_strcmp(szSetting, "NLProxyAuthPassword")) return true; + if (!mir_strcmp(szSetting, "LNPassword")) return true; + if (!mir_strcmp(szSetting, "FileProxyPassword")) return true; + if (!mir_strcmp(szSetting, "TokenSecret")) return true; + + if (!mir_strcmp(szModule, "SecureIM")) { + if (!mir_strcmp(szSetting, "pgp")) return true; + if (!mir_strcmp(szSetting, "pgpPrivKey")) return true; + } + return false; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +BOOL MDatabaseCommon::MetaDetouchSub(DBCachedContact *cc, int nSub) +{ + return DeleteModule(cc->pSubs[nSub], META_PROTO); +} + +BOOL MDatabaseCommon::MetaSetDefault(DBCachedContact *cc) +{ + DBCONTACTWRITESETTING cws; + cws.szModule = META_PROTO; + cws.szSetting = "Default"; + cws.value.type = DBVT_DWORD; + cws.value.dVal = cc->nDefault; + return WriteContactSetting(cc->contactID, &cws); +} + +///////////////////////////////////////////////////////////////////////////////////////// + STDMETHODIMP_(BOOL) MDatabaseCommon::GetContactSetting(MCONTACT contactID, LPCSTR szModule, LPCSTR szSetting, DBVARIANT *dbv) { dbv->type = 0; diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 90a77bfe25..cff4a26030 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -479,3 +479,10 @@ Clist_SetStatusMode @464 ?GetContactSettingStatic@MDatabaseCommon@@UAGHIPBD0PAUDBVARIANT@@@Z @484 NONAME ?GetContactSettingStr@MDatabaseCommon@@UAGHIPBD0PAUDBVARIANT@@@Z @485 NONAME ?SetSettingResident@MDatabaseCommon@@UAGHHPBD@Z @486 NONAME +?IsSettingEncrypted@MDatabaseCommon@@UAGHPBD0@Z @487 NONAME +?CheckProto@MDatabaseCommon@@IAEHPAUDBCachedContact@@PBD@Z @488 NONAME +?DeleteModule@MDatabaseCommon@@UAGHIPBD@Z @489 NONAME +?FindFirstContact@MDatabaseCommon@@UAGIPBD@Z @490 NONAME +?FindNextContact@MDatabaseCommon@@UAGIIPBD@Z @491 NONAME +?MetaDetouchSub@MDatabaseCommon@@UAGHPAUDBCachedContact@@H@Z @492 NONAME +?MetaSetDefault@MDatabaseCommon@@UAGHPAUDBCachedContact@@@Z @493 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index 2bb0247a06..4bb67009d7 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -479,3 +479,10 @@ Clist_SetStatusMode @464 ?GetContactSettingStatic@MDatabaseCommon@@UEAAHIPEBD0PEAUDBVARIANT@@@Z @484 NONAME ?GetContactSettingStr@MDatabaseCommon@@UEAAHIPEBD0PEAUDBVARIANT@@@Z @485 NONAME ?SetSettingResident@MDatabaseCommon@@UEAAHHPEBD@Z @486 NONAME +?IsSettingEncrypted@MDatabaseCommon@@UEAAHPEBD0@Z @487 NONAME +?CheckProto@MDatabaseCommon@@IEAAHPEAUDBCachedContact@@PEBD@Z @488 NONAME +?DeleteModule@MDatabaseCommon@@UEAAHIPEBD@Z @489 NONAME +?FindFirstContact@MDatabaseCommon@@UEAAIPEBD@Z @490 NONAME +?FindNextContact@MDatabaseCommon@@UEAAIIPEBD@Z @491 NONAME +?MetaDetouchSub@MDatabaseCommon@@UEAAHPEAUDBCachedContact@@H@Z @492 NONAME +?MetaSetDefault@MDatabaseCommon@@UEAAHPEAUDBCachedContact@@@Z @493 NONAME diff --git a/src/mir_core/src/db.cpp b/src/mir_core/src/db.cpp index 15e9f2465d..7aa591db19 100644 --- a/src/mir_core/src/db.cpp +++ b/src/mir_core/src/db.cpp @@ -45,23 +45,9 @@ MIR_CORE_DLL(MIDatabase*) db_get_current() return currDb; } -static int sttEnumVars(const char *szVarName, void *param) -{ - LIST* vars = (LIST*)param; - vars->insert(mir_strdup(szVarName)); - return 0; -} - MIR_CORE_DLL(int) db_delete_module(MCONTACT hContact, const char *szModuleName) { - LIST vars(20); - db_enum_settings(hContact, sttEnumVars, szModuleName, &vars); - - for (int i = vars.getCount() - 1; i >= 0; i--) { - db_unset(hContact, szModuleName, vars[i]); - mir_free(vars[i]); - } - return 0; + return (currDb) ? currDb->DeleteModule(hContact, szModuleName) : 0; } ///////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3