diff options
28 files changed, 784 insertions, 3449 deletions
diff --git a/plugins/Db3x_mmap/commonheaders.h b/plugins/Db3x_mmap/commonheaders.h index acdc888725..4a31de1cc2 100644 --- a/plugins/Db3x_mmap/commonheaders.h +++ b/plugins/Db3x_mmap/commonheaders.h @@ -52,6 +52,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "resource.h"
#include "version.h"
+extern LIST<CDdxMmap> g_Dbs;
+
#ifdef __GNUC__
#define mir_i64(x) (x##LL)
#else
diff --git a/plugins/Db3x_mmap/database.h b/plugins/Db3x_mmap/database.h index 1a10285c01..83c2fcaea9 100644 --- a/plugins/Db3x_mmap/database.h +++ b/plugins/Db3x_mmap/database.h @@ -47,135 +47,6 @@ DBHeader \--> ...
*/
-//#define DB_RESIZE_GRANULARITY 16384
-#define DB_THIS_VERSION 0x00000700u
-#define DB_SETTINGS_RESIZE_GRANULARITY 128
-
-struct DBSignature {
- char name[15];
- BYTE eof;
-};
-
-static struct DBSignature dbSignature = {"Miranda ICQ DB",0x1A};
-
-#include <pshpack1.h>
-struct DBHeader {
- BYTE signature[16]; // 'Miranda ICQ DB',0,26
- DWORD version; //as 4 bytes, ie 1.2.3.10 = 0x0102030a
- //this version is 0x00000700
- 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
- 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
- DWORD ofsFirstModuleName; //offset to first struct DBModuleName in the chain
-};
-
-#define DBCONTACT_SIGNATURE 0x43DECADEu
-
-struct DBContact
-{
- DWORD signature;
- DWORD ofsNext; //offset to the next contact in the chain. zero if
- //this is the 'user' contact or the last contact
- //in the chain
- 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,ofsLastEvent; //offsets to the first and last DBEvent in
- //the chain for this contact
- DWORD ofsFirstUnreadEvent; //offset to the first (chronological) unread event
- //in the chain, 0 if all are read
- DWORD timestampFirstUnread; //timestamp of the event at ofsFirstUnreadEvent
-};
-
-#define DBMODULENAME_SIGNATURE 0x4DDECADEu
-struct DBModuleName
-{
- DWORD signature;
- DWORD ofsNext; //offset to the next module name in the chain
- BYTE cbName; //number of characters in this module name
- char name[1]; //name, no nul terminator
-};
-
-#define DBCONTACTSETTINGS_SIGNATURE 0x53DECADEu
-struct DBContactSettings
-{
- DWORD signature;
- DWORD ofsNext; //offset to the next contactsettings in the chain
- DWORD ofsModuleName; //offset to the DBModuleName of the owner of these
- //settings
- DWORD cbBlob; //size of the blob in bytes. May be larger than the
- //actual size for reducing the number of moves
- //required using granularity in resizing
- BYTE blob[1]; //the blob. a back-to-back sequence of DBSetting
- //structs, the last has cbName = 0
-};
-
-/* not a valid structure, content is figured out on the fly
-struct DBSetting {
- BYTE cbName; //number of bytes in the name of this setting
- //this = 0 marks the end
- char szName[...]; //setting name, excluding nul
- BYTE dataType; //type of data. see m_database.h, db/contact/getsetting
- union { //a load of types of data, length is defined by dataType
- BYTE bVal; WORD wVal; DWORD dVal;
- struct {
- WORD cbString;
- char szVal[...]; //excludes nul terminator
- };
- struct {
- WORD cbBlob;
- BYTE blobVal[...];
- };
- };
-};
-*/
-
-#define DBEVENT_SIGNATURE 0x45DECADEu
-struct DBEvent
-{
- DWORD signature;
- DWORD ofsPrev,ofsNext; //offset to the previous and next events in the
- //chain. Chain is sorted chronologically
- DWORD ofsModuleName; //offset to a DBModuleName struct of the name of
- //the owner of this event
- DWORD timestamp; //seconds since 00:00:00 01/01/1970
- DWORD flags; //see m_database.h, db/event/add
- WORD eventType; //module-defined event type
- DWORD cbBlob; //number of bytes in the blob
- BYTE blob[1]; //the blob. module-defined formatting
-};
-
-#include <poppack.h>
-
-struct DBCachedGlobalValue
-{
- char* name;
- DBVARIANT value;
-};
-
-struct DBCachedContactValue
-{
- char* name;
- DBVARIANT value;
- DBCachedContactValue* next;
-};
-
-struct DBCachedContactValueList
-{
- HANDLE hContact;
- HANDLE hNext;
- DBCachedContactValue* first;
- DBCachedContactValue* last;
-};
-
-#define MAXCACHEDREADSIZE 65536
-
//#define DBLOGGING
#ifdef _DEBUG
diff --git a/plugins/Db3x_mmap/dbevents.cpp b/plugins/Db3x_mmap/dbevents.cpp index 22245d4515..917d1680b2 100644 --- a/plugins/Db3x_mmap/dbevents.cpp +++ b/plugins/Db3x_mmap/dbevents.cpp @@ -125,7 +125,7 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei) DBWrite(ofsContact,&dbc,sizeof(DBContact));
DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob));
- DBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob);
+ EncodeDBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob);
DBFlush(0);
lck.unlock();
@@ -245,10 +245,10 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) if (bytesToCopy && dbei->pBlob) {
for (int i = 0;;i += MAXCACHEDREADSIZE) {
if (bytesToCopy-i <= MAXCACHEDREADSIZE) {
- CopyMemory(dbei->pBlob+i, DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,bytesToCopy-i,NULL), bytesToCopy-i);
+ DecodeCopyMemory(dbei->pBlob+i, DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,bytesToCopy-i,NULL), bytesToCopy-i);
break;
}
- CopyMemory(dbei->pBlob+i, DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i, MAXCACHEDREADSIZE, NULL), MAXCACHEDREADSIZE);
+ DecodeCopyMemory(dbei->pBlob+i, DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i, MAXCACHEDREADSIZE, NULL), MAXCACHEDREADSIZE);
}
}
return 0;
diff --git a/plugins/Db3x_mmap/dbheaders.cpp b/plugins/Db3x_mmap/dbheaders.cpp index 32ed0091cb..3d18a373fa 100644 --- a/plugins/Db3x_mmap/dbheaders.cpp +++ b/plugins/Db3x_mmap/dbheaders.cpp @@ -25,12 +25,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //the cache has not been loaded when these functions are used
+extern DBSignature dbSignature;
+
int CDdxMmap::CreateDbHeaders()
{
DBContact user;
DWORD bytesWritten;
- CopyMemory(m_dbHeader.signature,&dbSignature,sizeof(m_dbHeader.signature));
+ CopyMemory(m_dbHeader.signature, &dbSignature,sizeof(m_dbHeader.signature));
m_dbHeader.version = DB_THIS_VERSION;
m_dbHeader.ofsFileEnd = sizeof(struct DBHeader);
m_dbHeader.slackSpace = 0;
diff --git a/plugins/Db3x_mmap/dbintf.cpp b/plugins/Db3x_mmap/dbintf.cpp index fffc01d728..83ae3aa4ca 100644 --- a/plugins/Db3x_mmap/dbintf.cpp +++ b/plugins/Db3x_mmap/dbintf.cpp @@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h"
+DBSignature dbSignature = {"Miranda ICQ DB",0x1A};
+
static int stringCompare(const char* p1, const char* p2)
{
return strcmp(p1+1, p2+1);
@@ -98,8 +100,8 @@ CDdxMmap::~CDdxMmap() // update profile last modified time
DWORD bytesWritten;
- SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
- WriteFile(m_hDbFile,&dbSignature,1,&bytesWritten,NULL);
+ SetFilePointer(m_hDbFile, 0, NULL, FILE_BEGIN);
+ WriteFile(m_hDbFile, &dbSignature, 1, &bytesWritten, NULL);
CloseHandle(m_hDbFile);
DeleteCriticalSection(&m_csDbAccess);
@@ -149,3 +151,24 @@ STDMETHODIMP_(void) CDdxMmap::SetCacheSafetyMode(BOOL bIsSet) }
DBFlush(1);
}
+
+void CDdxMmap::EncodeCopyMemory(void *dst, void *src, size_t size)
+{
+ MoveMemory(dst, src, size);
+}
+
+void CDdxMmap::DecodeCopyMemory(void *dst, void *src, size_t size)
+{
+ MoveMemory(dst, src, size);
+}
+
+void CDdxMmap::EncodeDBWrite(DWORD ofs, void *src, int size)
+{
+ DBWrite(ofs, src, size);
+}
+
+void CDdxMmap::DecodeDBWrite(DWORD ofs, void *src, int size)
+{
+ DBWrite(ofs, src, size);
+}
+
diff --git a/plugins/Db3x_mmap/dbintf.h b/plugins/Db3x_mmap/dbintf.h index ac5a1c25b5..4dff427516 100644 --- a/plugins/Db3x_mmap/dbintf.h +++ b/plugins/Db3x_mmap/dbintf.h @@ -23,12 +23,139 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <m_db_int.h>
+/* tree diagram
+
+DBHeader
+ |-->end of file (plain offset)
+ |-->first contact (DBContact)
+ | |-->next contact (DBContact)
+ | | \--> ...
+ | |-->first settings (DBContactSettings)
+ | | |-->next settings (DBContactSettings)
+ | | | \--> ...
+ | | \-->module name (DBModuleName)
+ | \-->first/last/firstunread event
+ |-->user contact (DBContact)
+ | |-->next contact = NULL
+ | |-->first settings as above
+ | \-->first/last/firstunread event as above
+ \-->first module name (DBModuleName)
+ \-->next module name (DBModuleName)
+ \--> ...
+*/
+
+#define DB_THIS_VERSION 0x00000700u
+#define DB_SETTINGS_RESIZE_GRANULARITY 128
+
+struct DBSignature {
+ char name[15];
+ BYTE eof;
+};
+
struct ModuleName
{
char *name;
DWORD ofs;
};
+#include <pshpack1.h>
+struct DBHeader {
+ BYTE signature[16]; // 'Miranda ICQ DB',0,26
+ DWORD version; //as 4 bytes, ie 1.2.3.10 = 0x0102030a
+ //this version is 0x00000700
+ 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
+ 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
+ DWORD ofsFirstModuleName; //offset to first struct DBModuleName in the chain
+};
+
+#define DBCONTACT_SIGNATURE 0x43DECADEu
+
+struct DBContact
+{
+ DWORD signature;
+ DWORD ofsNext; //offset to the next contact in the chain. zero if
+ //this is the 'user' contact or the last contact
+ //in the chain
+ 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,ofsLastEvent; //offsets to the first and last DBEvent in
+ //the chain for this contact
+ DWORD ofsFirstUnreadEvent; //offset to the first (chronological) unread event
+ //in the chain, 0 if all are read
+ DWORD timestampFirstUnread; //timestamp of the event at ofsFirstUnreadEvent
+};
+
+#define DBMODULENAME_SIGNATURE 0x4DDECADEu
+struct DBModuleName
+{
+ DWORD signature;
+ DWORD ofsNext; //offset to the next module name in the chain
+ BYTE cbName; //number of characters in this module name
+ char name[1]; //name, no nul terminator
+};
+
+#define DBCONTACTSETTINGS_SIGNATURE 0x53DECADEu
+struct DBContactSettings
+{
+ DWORD signature;
+ DWORD ofsNext; //offset to the next contactsettings in the chain
+ DWORD ofsModuleName; //offset to the DBModuleName of the owner of these
+ //settings
+ DWORD cbBlob; //size of the blob in bytes. May be larger than the
+ //actual size for reducing the number of moves
+ //required using granularity in resizing
+ BYTE blob[1]; //the blob. a back-to-back sequence of DBSetting
+ //structs, the last has cbName = 0
+};
+
+#define DBEVENT_SIGNATURE 0x45DECADEu
+struct DBEvent
+{
+ DWORD signature;
+ DWORD ofsPrev,ofsNext; //offset to the previous and next events in the
+ //chain. Chain is sorted chronologically
+ DWORD ofsModuleName; //offset to a DBModuleName struct of the name of
+ //the owner of this event
+ DWORD timestamp; //seconds since 00:00:00 01/01/1970
+ DWORD flags; //see m_database.h, db/event/add
+ WORD eventType; //module-defined event type
+ DWORD cbBlob; //number of bytes in the blob
+ BYTE blob[1]; //the blob. module-defined formatting
+};
+
+#include <poppack.h>
+
+struct DBCachedGlobalValue
+{
+ char* name;
+ DBVARIANT value;
+};
+
+struct DBCachedContactValue
+{
+ char* name;
+ DBVARIANT value;
+ DBCachedContactValue* next;
+};
+
+struct DBCachedContactValueList
+{
+ HANDLE hContact;
+ HANDLE hNext;
+ DBCachedContactValue* first;
+ DBCachedContactValue* last;
+};
+
+#define MAXCACHEDREADSIZE 65536
+
struct CDdxMmap : public MIDatabase, public MZeroedObject
{
CDdxMmap(const TCHAR* tszFileName);
@@ -39,7 +166,6 @@ struct CDdxMmap : public MIDatabase, public MZeroedObject int CreateDbHeaders();
int CheckDbHeaders();
void DatabaseCorruption(TCHAR *text);
-
protected:
STDMETHODIMP_(void) SetCacheSafetyMode(BOOL);
@@ -75,7 +201,13 @@ protected: STDMETHODIMP_(BOOL) SetSettingResident(BOOL bIsResident, const char *pszSettingName);
STDMETHODIMP_(BOOL) EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam);
-private:
+protected:
+ virtual void EncodeCopyMemory(void *dst, void *src, size_t size);
+ virtual void DecodeCopyMemory(void *dst, void *src, size_t size);
+ virtual void EncodeDBWrite(DWORD ofs, void *src, int size);
+ virtual void DecodeDBWrite(DWORD ofs, void *src, int size);
+
+protected:
TCHAR* m_tszProfileName;
HANDLE m_hDbFile;
DBHeader m_dbHeader;
@@ -88,8 +220,7 @@ public: UINT_PTR m_flushBuffersTimerId;
DWORD m_flushFailTick;
PBYTE m_pDbCache;
-
-private:
+protected:
PBYTE m_pNull;
HANDLE m_hMap;
DWORD m_dwFileSize;
@@ -156,5 +287,3 @@ private: DWORD GetModuleNameOfs(const char *szName);
char *GetModuleNameByOfs(DWORD ofs);
};
-
-extern LIST<CDdxMmap> g_Dbs;
diff --git a/plugins/Db3x_mmap/dbsettings.cpp b/plugins/Db3x_mmap/dbsettings.cpp index 9423d7141c..49ac018047 100644 --- a/plugins/Db3x_mmap/dbsettings.cpp +++ b/plugins/Db3x_mmap/dbsettings.cpp @@ -26,6 +26,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. DWORD GetModuleNameOfs(const char *szName);
DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index);
+int DBPreset_QuerySetting(const char *szModule, const char *szSetting, DBVARIANT *dbv, BOOL isStatic);
+
DWORD CDdxMmap::GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleName)
{
DWORD ofsThis = dbc->ofsFirstSettings;
@@ -298,21 +300,21 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs return 2;
case DBVT_BYTE: dbcgs->pValue->bVal = pBlob[1]; break;
- case DBVT_WORD: dbcgs->pValue->wVal = *(PWORD)(pBlob+1); break;
- case DBVT_DWORD: dbcgs->pValue->dVal = *(PDWORD)(pBlob+1); break;
+ case DBVT_WORD: DecodeCopyMemory(&(dbcgs->pValue->wVal), (PWORD)(pBlob+1), 2); break;
+ case DBVT_DWORD: DecodeCopyMemory(&(dbcgs->pValue->dVal), (PDWORD)(pBlob+1), 4); break;
case DBVT_UTF8:
case DBVT_ASCIIZ:
NeedBytes(3+*(PWORD)(pBlob+1));
if (isStatic) {
dbcgs->pValue->cchVal--;
if (*(PWORD)(pBlob+1)<dbcgs->pValue->cchVal) dbcgs->pValue->cchVal = *(PWORD)(pBlob+1);
- CopyMemory(dbcgs->pValue->pszVal,pBlob+3,dbcgs->pValue->cchVal);
+ DecodeCopyMemory(dbcgs->pValue->pszVal,pBlob+3,dbcgs->pValue->cchVal);
dbcgs->pValue->pszVal[dbcgs->pValue->cchVal] = 0;
dbcgs->pValue->cchVal = *(PWORD)(pBlob+1);
}
else {
dbcgs->pValue->pszVal = (char*)mir_alloc(1+*(PWORD)(pBlob+1));
- CopyMemory(dbcgs->pValue->pszVal,pBlob+3,*(PWORD)(pBlob+1));
+ DecodeCopyMemory(dbcgs->pValue->pszVal,pBlob+3,*(PWORD)(pBlob+1));
dbcgs->pValue->pszVal[*(PWORD)(pBlob+1)] = 0;
}
break;
@@ -320,19 +322,18 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs NeedBytes(3+*(PWORD)(pBlob+1));
if (isStatic) {
if (*(PWORD)(pBlob+1)<dbcgs->pValue->cpbVal) dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1);
- CopyMemory(dbcgs->pValue->pbVal,pBlob+3,dbcgs->pValue->cpbVal);
+ DecodeCopyMemory(dbcgs->pValue->pbVal,pBlob+3,dbcgs->pValue->cpbVal);
}
else {
dbcgs->pValue->pbVal = (BYTE *)mir_alloc(*(PWORD)(pBlob+1));
- CopyMemory(dbcgs->pValue->pbVal,pBlob+3,*(PWORD)(pBlob+1));
+ DecodeCopyMemory(dbcgs->pValue->pbVal,pBlob+3,*(PWORD)(pBlob+1));
}
dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1);
break;
}
/**** add to cache **********************/
- if ( dbcgs->pValue->type != DBVT_BLOB )
- {
+ if ( dbcgs->pValue->type != DBVT_BLOB ) {
DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 1 );
if ( pCachedValue != NULL )
SetCachedVariant(dbcgs->pValue,pCachedValue);
@@ -348,6 +349,19 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs NeedBytes(1);
} }
+ #ifndef DB3X_EXPORTS
+ /**** nullbie: query info from preset **********************/
+ if (!hContact && DBPreset_QuerySetting(dbcgs->szModule, dbcgs->szSetting, dbcgs->pValue, isStatic)) {
+ /**** add to cache **********************/
+ if ( dbcgs->pValue->type != DBVT_BLOB ) {
+ DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 1 );
+ if ( pCachedValue != NULL )
+ SetCachedVariant(dbcgs->pValue,pCachedValue);
+ }
+ return 0;
+ }
+ #endif
+
/**** add missing setting to cache **********************/
if ( dbcgs->pValue->type != DBVT_BLOB )
{
@@ -675,11 +689,11 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT MoveAlong(1); //skip data type
switch(tmp.value.type) {
case DBVT_BYTE: DBWrite(ofsBlobPtr,&tmp.value.bVal,1); break;
- case DBVT_WORD: DBWrite(ofsBlobPtr,&tmp.value.wVal,2); break;
- case DBVT_DWORD: DBWrite(ofsBlobPtr,&tmp.value.dVal,4); break;
+ case DBVT_WORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.wVal,2); break;
+ case DBVT_DWORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.dVal,4); break;
case DBVT_UTF8:
- case DBVT_ASCIIZ: DBWrite(ofsBlobPtr+2,tmp.value.pszVal,(int)strlen(tmp.value.pszVal)); break;
- case DBVT_BLOB: DBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal); break;
+ case DBVT_ASCIIZ: EncodeDBWrite(ofsBlobPtr+2,tmp.value.pszVal,(int)strlen(tmp.value.pszVal)); break;
+ case DBVT_BLOB: EncodeDBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal); break;
}
//quit
DBFlush(1);
@@ -745,19 +759,19 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT MoveAlong(1);
switch(tmp.value.type) {
case DBVT_BYTE: DBWrite(ofsBlobPtr,&tmp.value.bVal,1); MoveAlong(1); break;
- case DBVT_WORD: DBWrite(ofsBlobPtr,&tmp.value.wVal,2); MoveAlong(2); break;
- case DBVT_DWORD: DBWrite(ofsBlobPtr,&tmp.value.dVal,4); MoveAlong(4); break;
+ case DBVT_WORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.wVal,2); MoveAlong(2); break;
+ case DBVT_DWORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.dVal,4); MoveAlong(4); break;
case DBVT_UTF8:
case DBVT_ASCIIZ:
{ int len = (int)strlen(tmp.value.pszVal);
DBWrite(ofsBlobPtr,&len,2);
- DBWrite(ofsBlobPtr+2,tmp.value.pszVal,len);
+ EncodeDBWrite(ofsBlobPtr+2,tmp.value.pszVal,len);
MoveAlong(2+len);
}
break;
case DBVT_BLOB:
DBWrite(ofsBlobPtr,&tmp.value.cpbVal,2) ;
- DBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal);
+ EncodeDBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal);
MoveAlong(2+tmp.value.cpbVal);
break;
}
diff --git a/plugins/Db3x_mmap/init.cpp b/plugins/Db3x_mmap/init.cpp index 03f4f3c3bb..b38fab0ce2 100644 --- a/plugins/Db3x_mmap/init.cpp +++ b/plugins/Db3x_mmap/init.cpp @@ -45,11 +45,6 @@ LIST<CDdxMmap> g_Dbs(1, (LIST<CDdxMmap>::FTSortFunc)HandleKeySort); /////////////////////////////////////////////////////////////////////////////////////////
-static int getCapability( int flag )
-{
- return 0;
-}
-
// returns 0 if the profile is created, EMKPRF*
static int makeDatabase(const TCHAR *profile, int *error)
{
diff --git a/plugins/Dbx_mmap_SA/commonheaders.h b/plugins/Dbx_mmap_SA/commonheaders.h index 56a9d54ddd..a098644dad 100644 --- a/plugins/Dbx_mmap_SA/commonheaders.h +++ b/plugins/Dbx_mmap_SA/commonheaders.h @@ -57,18 +57,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "m_folders.h"
//own headers
-#include "database.h"
-#include "dbintf.h"
+#include "dbintf_sa.h"
+#include "..\Db3x_mmap\database.h"
#include "resource.h"
#include "version.h"
-extern CRITICAL_SECTION csDbAccess;
-extern struct DBHeader dbHeader;
-extern HANDLE hDbFile;
-extern BOOL bEncoding;
extern HINSTANCE g_hInst;
extern HANDLE hSetPwdMenu;
-extern CDdxMmap* g_Db;
#ifdef __GNUC__
#define mir_i64(x) (x##LL)
@@ -88,6 +83,13 @@ BOOL CALLBACK DlgStdNewPass(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam); BOOL CALLBACK DlgChangePass(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam);
void xModifyMenu(HANDLE hMenu,long flags,const TCHAR* name, HICON hIcon);
+extern DBSignature dbSignature, dbSignatureSecured;
+
+extern LIST<CDdxMmapSA> g_Dbs;
+
+int InitPreset();
+void UninitPreset();
+
typedef struct{
void* (__stdcall *GenerateKey)(char* pwd);
void (__stdcall *FreeKey)(void* key);
@@ -106,7 +108,10 @@ typedef struct{ } Cryptor;
typedef struct{
- char dllname[255];
+ TCHAR dllname[MAX_PATH];
HMODULE hLib;
Cryptor* cryptor;
-} CryptoModule;
\ No newline at end of file +} CryptoModule;
+
+extern Cryptor* CryptoEngine;
+extern void* key;
diff --git a/plugins/Dbx_mmap_SA/database.cpp b/plugins/Dbx_mmap_SA/database.cpp deleted file mode 100644 index 56c44850a1..0000000000 --- a/plugins/Dbx_mmap_SA/database.cpp +++ /dev/null @@ -1,218 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-#include "commonheaders.h"
-
-int ProfileManager(char *szDbDest,int cbDbDest);
-int ShouldAutoCreate(void);
-int CreateDbHeaders(HANDLE hFile);
-int InitialiseDbHeaders(void);
-int InitSettings(void);
-void UninitSettings(void);
-int InitContacts(void);
-int InitEvents(void);
-int InitModuleNames(void);
-void UninitModuleNames(void);
-int InitCache(void);
-void UninitCache(void);
-int InitIni(void);
-void UninitIni(void);
-int InitPreset(void);
-void UninitPreset(void);
-int InitDialogs(void);
-void InitSecurity(void);
-void UnloadSecurity(void);
-
-HANDLE hDbFile = INVALID_HANDLE_VALUE;
-CRITICAL_SECTION csDbAccess;
-struct DBHeader dbHeader;
-TCHAR szDbPath[MAX_PATH];
-
-static void UnloadDatabase(void)
-{
- // update profile last modified time
- DWORD bytesWritten;
- SetFilePointer(hDbFile,0,NULL,FILE_BEGIN);
- WriteFile(hDbFile,dbHeader.signature,sizeof(1),&bytesWritten,NULL);
-
- CloseHandle(hDbFile);
-}
-
-DWORD CreateNewSpace(int bytes)
-{
- DWORD ofsNew;
- ofsNew = dbHeader.ofsFileEnd;
- dbHeader.ofsFileEnd += bytes;
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- log2("newspace %d@%08x",bytes,ofsNew);
- return ofsNew;
-}
-
-void DeleteSpace(DWORD ofs,int bytes)
-{
- if (ofs+bytes == dbHeader.ofsFileEnd) {
- log2("freespace %d@%08x",bytes,ofs);
- dbHeader.ofsFileEnd = ofs;
- } else {
- log2("deletespace %d@%08x",bytes,ofs);
- dbHeader.slackSpace += bytes;
- }
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- DBFill(ofs,bytes);
-}
-
-DWORD ReallocSpace(DWORD ofs,int oldSize,int newSize)
-{
- DWORD ofsNew;
-
- if (oldSize >= newSize) return ofs;
-
- if (ofs+oldSize == dbHeader.ofsFileEnd) {
- ofsNew = ofs;
- dbHeader.ofsFileEnd += newSize-oldSize;
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- log3("adding newspace %d@%08x+%d",newSize,ofsNew,oldSize);
- } else {
- ofsNew = CreateNewSpace(newSize);
- DBMoveChunk(ofsNew,ofs,oldSize);
- DeleteSpace(ofs,oldSize);
- }
- return ofsNew;
-}
-
-void UnloadDatabaseModule(void)
-{
- //UninitIni();
- UninitPreset();
- UninitSettings();
- UninitModuleNames();
- UninitCache();
- UnloadDatabase();
- UnloadSecurity();
- DeleteCriticalSection(&csDbAccess);
-}
-
-INT_PTR GetProfileName(WPARAM wParam, LPARAM lParam)
-{
- TCHAR *p = _tcsrchr(szDbPath, '\\');
- if ( p == 0 ) return 1;
- p++;
- _tcsncpy((TCHAR*)lParam, p, (size_t) wParam);
- return 0;
-}
-
-int LoadDatabaseModule(void)
-{
- InitializeCriticalSection(&csDbAccess);
- log0("DB logging running");
- {
- DWORD dummy = 0;
- hDbFile = CreateFile(szDbPath,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
- if ( hDbFile == INVALID_HANDLE_VALUE ) {
- return 1;
- }
- if ( !ReadFile(hDbFile,&dbHeader,sizeof(dbHeader),&dummy,NULL)) {
- CloseHandle(hDbFile);
- return 1;
- }
- }
- InitSecurity();
- CheckDbHeaders(&dbHeader);
-
- TCHAR szDBName[MAX_PATH];
- GetProfileName((WPARAM)50, (LPARAM)szDBName);
- if (bEncoding && !g_Db->CheckPassword(dbHeader.checkWord, szDBName))
- return 1;
-
- if (InitCache()) return 1;
- if (InitModuleNames()) return 1;
- if (InitContacts()) return 1;
- if (InitSettings()) return 1;
- if (InitEvents()) return 1;
- if (InitPreset()) return 1;
- if (InitDialogs()) return 1;
- return 0;
-}
-
-static DWORD DatabaseCorrupted = 0;
-static TCHAR *msg = NULL;
-static DWORD dwErr = 0;
-
-void __cdecl dbpanic(void *arg)
-{
- if (msg)
- {
- TCHAR err[256];
-
- if (dwErr == ERROR_DISK_FULL)
- msg = TranslateT("Disk is full. Miranda will now shutdown.");
-
- mir_sntprintf(err, SIZEOF(err), msg, TranslateT("Database failure. Miranda will now shutdown."), dwErr);
-
- MessageBox(0,err,TranslateT("Database Error"),MB_SETFOREGROUND|MB_TOPMOST|MB_APPLMODAL|MB_ICONWARNING|MB_OK);
- }
- else
- MessageBox(0,TranslateT("Miranda has detected corruption in your database. This corruption maybe fixed by DBTool. Please download it from http://www.miranda-im.org. Miranda will now shutdown."),
- TranslateT("Database Panic"),MB_SETFOREGROUND|MB_TOPMOST|MB_APPLMODAL|MB_ICONWARNING|MB_OK);
-
- TerminateProcess(GetCurrentProcess(),255);
-}
-
-void DatabaseCorruption(TCHAR *text)
-{
- int kill = 0;
-
- EnterCriticalSection(&csDbAccess);
- if (DatabaseCorrupted == 0) {
- DatabaseCorrupted++;
- kill++;
- msg = text;
- dwErr = GetLastError();
- } else {
- /* db is already corrupted, someone else is dealing with it, wait here
- so that we don't do any more damage */
- LeaveCriticalSection(&csDbAccess);
- Sleep(INFINITE);
- return;
- }
- LeaveCriticalSection(&csDbAccess);
- if (kill) {
- _beginthread(dbpanic,0,NULL);
- Sleep(INFINITE);
- }
-}
-
-#ifdef DBLOGGING
-void DBLog(const char *file,int line,const char *fmt,...)
-{
- FILE *fp;
- va_list vararg;
- char str[1024];
-
- va_start(vararg,fmt);
- mir_vsnprintf(str,sizeof(str),fmt,vararg);
- va_end(vararg);
- fp = fopen("c:\\mirandadatabase.log.txt","at");
- fprintf(fp,"%u: %s %d: %s\n",GetTickCount(),file,line,str);
- fclose(fp);
-}
-#endif
diff --git a/plugins/Dbx_mmap_SA/database.h b/plugins/Dbx_mmap_SA/database.h deleted file mode 100644 index bbacb9670a..0000000000 --- a/plugins/Dbx_mmap_SA/database.h +++ /dev/null @@ -1,232 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-
-//all offsets are relative to the start of the file
-//offsets are 0 if there is nothing in the chain or this is the last in the
-//chain
-
-/* tree diagram
-
-DBHeader
- |-->end of file (plain offset)
- |-->first contact (DBContact)
- | |-->next contact (DBContact)
- | | \--> ...
- | |-->first settings (DBContactSettings)
- | | |-->next settings (DBContactSettings)
- | | | \--> ...
- | | \-->module name (DBModuleName)
- | \-->first/last/firstunread event
- |-->user contact (DBContact)
- | |-->next contact = NULL
- | |-->first settings as above
- | \-->first/last/firstunread event as above
- \-->first module name (DBModuleName)
- \-->next module name (DBModuleName)
- \--> ...
-*/
-
-//#define DB_RESIZE_GRANULARITY 16384
-#define DB_THIS_VERSION 0x00000700u
-#define DB_SETTINGS_RESIZE_GRANULARITY 128
-
-struct DBSignature {
- char name[15];
- BYTE eof;
-};
-
-static struct DBSignature dbSignature = {"Miranda ICQ DB",0x1A};
-static struct DBSignature dbSignatureSecured = {"Miranda ICQ SD",0x1A};
-
-#include <pshpack1.h>
-struct DBHeader {
- BYTE signature[16]; // 'Miranda ICQ DB',0,26
-
- /*
- DWORD version; //as 4 bytes, ie 1.2.3.10 = 0x0102030a
- //this version is 0x00000700
- */
-
- WORD checkWord;
- WORD cryptorUID;
-
- 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
- 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
- DWORD ofsFirstModuleName; //offset to first struct DBModuleName in the chain
-};
-
-#define DBCONTACT_SIGNATURE 0x43DECADEu
-struct DBContact {
- DWORD signature;
- DWORD ofsNext; //offset to the next contact in the chain. zero if
- //this is the 'user' contact or the last contact
- //in the chain
- 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,ofsLastEvent; //offsets to the first and last DBEvent in
- //the chain for this contact
- DWORD ofsFirstUnreadEvent; //offset to the first (chronological) unread event
- //in the chain, 0 if all are read
- DWORD timestampFirstUnread; //timestamp of the event at ofsFirstUnreadEvent
-};
-
-#define DBMODULENAME_SIGNATURE 0x4DDECADEu
-struct DBModuleName {
- DWORD signature;
- DWORD ofsNext; //offset to the next module name in the chain
- BYTE cbName; //number of characters in this module name
- char name[1]; //name, no nul terminator
-};
-
-#define DBCONTACTSETTINGS_SIGNATURE 0x53DECADEu
-struct DBContactSettings {
- DWORD signature;
- DWORD ofsNext; //offset to the next contactsettings in the chain
- DWORD ofsModuleName; //offset to the DBModuleName of the owner of these
- //settings
- DWORD cbBlob; //size of the blob in bytes. May be larger than the
- //actual size for reducing the number of moves
- //required using granularity in resizing
- BYTE blob[1]; //the blob. a back-to-back sequence of DBSetting
- //structs, the last has cbName = 0
-};
-
-/* not a valid structure, content is figured out on the fly
-struct DBSetting {
- BYTE cbName; //number of bytes in the name of this setting
- //this = 0 marks the end
- char szName[...]; //setting name, excluding nul
- BYTE dataType; //type of data. see m_database.h, db/contact/getsetting
- union { //a load of types of data, length is defined by dataType
- BYTE bVal; WORD wVal; DWORD dVal;
- struct {
- WORD cbString;
- char szVal[...]; //excludes nul terminator
- };
- struct {
- WORD cbBlob;
- BYTE blobVal[...];
- };
- };
-};
-*/
-
-#define DBEVENT_SIGNATURE 0x45DECADEu
-struct DBEvent {
- DWORD signature;
- DWORD ofsPrev,ofsNext; //offset to the previous and next events in the
- //chain. Chain is sorted chronologically
- DWORD ofsModuleName; //offset to a DBModuleName struct of the name of
- //the owner of this event
- DWORD timestamp; //seconds since 00:00:00 01/01/1970
- DWORD flags; //see m_database.h, db/event/add
- WORD eventType; //module-defined event type
- DWORD cbBlob; //number of bytes in the blob
- BYTE blob[1]; //the blob. module-defined formatting
-};
-#include <poppack.h>
-
-typedef struct
-{
- BYTE bIsResident;
- char name[1];
-}
- DBCachedSettingName;
-
-typedef struct
-{
- char* name;
- DBVARIANT value;
-}
- DBCachedGlobalValue;
-
-typedef struct DBCachedContactValue_tag
-{
- char* name;
- DBVARIANT value;
- struct DBCachedContactValue_tag* next;
-}
- DBCachedContactValue;
-
-typedef struct
-{
- HANDLE hContact;
- HANDLE hNext;
- DBCachedContactValue* first;
- DBCachedContactValue* last;
-}
- DBCachedContactValueList;
-
-//databasecorruption: with NULL called if any signatures are broken. very very fatal
-void DatabaseCorruption(TCHAR *text);
-PBYTE DBRead(DWORD ofs,int bytesRequired,int *bytesAvail); //any preview result could be invalidated by the next call
-__forceinline PBYTE DBRead(HANDLE hContact,int bytesRequired,int *bytesAvail)
-{ return DBRead((DWORD)hContact, bytesRequired, bytesAvail);
-}
-void DBWrite(DWORD ofs,PVOID pData,int count);
-void DBFill(DWORD ofs,int bytes);
-void DBFlush(int setting);
-void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes);
-DWORD CreateNewSpace(int bytes);
-void DeleteSpace(DWORD ofs,int bytes);
-DWORD ReallocSpace(DWORD ofs,int oldSize,int newSize);
-void GetProfileDirectory(char *szPath,int cbPath);
-int GetDefaultProfilePath(char *szPath,int cbPath,int *specified);
-int ShouldShowProfileManager(void);
-int CheckDbHeaders(struct DBHeader * hdr);
-int CreateDbHeaders(HANDLE hFile);
-int LoadDatabaseModule(void);
-void UnloadDatabaseModule(void);
-
-#define MAXCACHEDREADSIZE 65536
-
-//#define DBLOGGING
-
-#ifdef _DEBUG
-//#define DBLOGGING
-#endif
-#ifdef DBLOGGING
-void DBLog(const char *file,int line,const char *fmt,...);
-#define logg() DBLog(__FILE__,__LINE__,"")
-#define log0(s) DBLog(__FILE__,__LINE__,s)
-#define log1(s,a) DBLog(__FILE__,__LINE__,s,a)
-#define log2(s,a,b) DBLog(__FILE__,__LINE__,s,a,b)
-#define log3(s,a,b,c) DBLog(__FILE__,__LINE__,s,a,b,c)
-#define log4(s,a,b,c,d) DBLog(__FILE__,__LINE__,s,a,b,c,d)
-#else
-#define logg()
-#define log0(s)
-#define log1(s,a)
-#define log2(s,a,b)
-#define log3(s,a,b,c)
-#define log4(s,a,b,c,d)
-#endif
diff --git a/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj b/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj index 5f6e6608a2..725dd669e7 100644 --- a/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj +++ b/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj @@ -73,7 +73,7 @@ <ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\..\include;..\ExternalAPI;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
- <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;DB3X_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
@@ -102,7 +102,7 @@ <ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\..\include;..\ExternalAPI;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
- <PreprocessorDefinitions>WIN64;_DEBUG;_WINDOWS;_USRDLL;DB3X_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN64;_DEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<FunctionLevelLinking>true</FunctionLevelLinking>
@@ -133,7 +133,7 @@ <FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<AdditionalIncludeDirectories>..\..\include;..\ExternalAPI;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
- <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;DB3X_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<StringPooling>true</StringPooling>
<FunctionLevelLinking>true</FunctionLevelLinking>
<PrecompiledHeader>Use</PrecompiledHeader>
@@ -164,7 +164,7 @@ <FavorSizeOrSpeed>Size</FavorSizeOrSpeed>
<OmitFramePointers>true</OmitFramePointers>
<AdditionalIncludeDirectories>..\..\include;..\ExternalAPI;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
- <PreprocessorDefinitions>WIN64;NDEBUG;_WINDOWS;_USRDLL;DB3X_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+ <PreprocessorDefinitions>WIN64;NDEBUG;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<StringPooling>true</StringPooling>
<FunctionLevelLinking>true</FunctionLevelLinking>
<PrecompiledHeader>Use</PrecompiledHeader>
@@ -189,15 +189,16 @@ </Link>
</ItemDefinitionGroup>
<ItemGroup>
- <ClCompile Include="database.cpp" />
- <ClCompile Include="dbcache.cpp" />
- <ClCompile Include="dbcontacts.cpp" />
- <ClCompile Include="dbevents.cpp" />
- <ClCompile Include="dbheaders.cpp" />
- <ClCompile Include="dbintf.cpp" />
- <ClCompile Include="dbmodulechain.cpp" />
+ <ClCompile Include="..\Db3x_mmap\database.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbcache.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbcontacts.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbevents.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbheaders.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbintf.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbmodulechain.cpp" />
+ <ClCompile Include="..\Db3x_mmap\dbsettings.cpp" />
+ <ClCompile Include="dbintf_sa.cpp" />
<ClCompile Include="dbpreset.cpp" />
- <ClCompile Include="dbsettings.cpp" />
<ClCompile Include="dialogs.cpp" />
<ClCompile Include="init.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
@@ -205,9 +206,9 @@ <ClCompile Include="security.cpp" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="..\Db3x_mmap\dbintf.h" />
<ClInclude Include="commonheaders.h" />
- <ClInclude Include="database.h" />
- <ClInclude Include="dbintf.h" />
+ <ClInclude Include="dbintf_sa.h" />
<ClInclude Include="options.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="version.h" />
diff --git a/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj.filters b/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj.filters index a50b10919b..84aae9636b 100644 --- a/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj.filters +++ b/plugins/Dbx_mmap_SA/db3x_mmap_sa_10.vcxproj.filters @@ -13,52 +13,55 @@ <UniqueIdentifier>{4eb47a45-3e87-400a-bfb3-05c2d9cbb32b}</UniqueIdentifier>
<Extensions>ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions>
</Filter>
+ <Filter Include="Mmap files">
+ <UniqueIdentifier>{9b94a455-bdbc-497d-b375-18c090cccc36}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
- <ClCompile Include="database.cpp">
+ <ClCompile Include="dbpreset.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="dbcache.cpp">
+ <ClCompile Include="dialogs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="dbcontacts.cpp">
+ <ClCompile Include="init.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="dbevents.cpp">
+ <ClCompile Include="security.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="dbheaders.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbmodulechain.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="dbmodulechain.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbcache.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="dbpreset.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbcontacts.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="dbsettings.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\database.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="dialogs.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbintf.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="init.cpp">
+ <ClCompile Include="dbintf_sa.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="security.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbheaders.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
- <ClCompile Include="dbintf.cpp">
- <Filter>Source Files</Filter>
+ <ClCompile Include="..\Db3x_mmap\dbsettings.cpp">
+ <Filter>Mmap files</Filter>
+ </ClCompile>
+ <ClCompile Include="..\Db3x_mmap\dbevents.cpp">
+ <Filter>Mmap files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="commonheaders.h">
<Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="database.h">
- <Filter>Header Files</Filter>
- </ClInclude>
<ClInclude Include="options.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -68,9 +71,12 @@ <ClInclude Include="version.h">
<Filter>Header Files</Filter>
</ClInclude>
- <ClInclude Include="dbintf.h">
+ <ClInclude Include="dbintf_sa.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="..\Db3x_mmap\dbintf.h">
+ <Filter>Mmap files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="db3x_mmap.rc">
diff --git a/plugins/Dbx_mmap_SA/dbcache.cpp b/plugins/Dbx_mmap_SA/dbcache.cpp deleted file mode 100644 index 1e5764ae22..0000000000 --- a/plugins/Dbx_mmap_SA/dbcache.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-BOOL safetyMode = TRUE;
-static UINT_PTR flushBuffersTimerId;
-
-static PBYTE pNull = 0;
-static PBYTE pDbCache = NULL;
-static HANDLE hMap = NULL;
-static DWORD dwFileSize = 0;
-static DWORD ChunkSize = 65536;
-static DWORD flushFailTick = 0;
-
-
-void Map()
-{
- hMap = CreateFileMapping(hDbFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL);
-
- if (hMap)
- {
- pDbCache = (PBYTE)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS/*FILE_MAP_WRITE*/, 0, 0 ,0);
- if (!pDbCache)
- DatabaseCorruption( _T("%s (MapViewOfFile failed. Code: %d)"));
- }
- else
- DatabaseCorruption( _T("%s (CreateFileMapping failed. Code: %d)"));
-}
-
-void ReMap(DWORD needed)
-{
- KillTimer(NULL,flushBuffersTimerId);
-
- log3("remapping %d + %d (file end: %d)",dwFileSize,needed,dbHeader.ofsFileEnd);
-
- if (needed > ChunkSize)
- {
- if (needed + dwFileSize > dbHeader.ofsFileEnd + ChunkSize)
- DatabaseCorruption( _T("%s (Too large increment)"));
- else
- {
- DWORD x = dbHeader.ofsFileEnd/ChunkSize;
- dwFileSize = (x+1)*ChunkSize;
- }
- }
- else
- dwFileSize += ChunkSize;
-
-// FlushViewOfFile(pDbCache, 0);
- UnmapViewOfFile(pDbCache);
- pDbCache = NULL;
- CloseHandle(hMap);
-
- Map();
-}
-
-void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes)
-{
- int x = 0;
- log3("move %d %08x->%08x",bytes,ofsSource,ofsDest);
- if (ofsDest+bytes>dwFileSize) ReMap(ofsDest+bytes-dwFileSize);
- if (ofsSource+bytes>dwFileSize) {
- x = ofsSource+bytes-dwFileSize;
- log0("buggy move!");
- _ASSERT(0);
- }
- if (x > 0)
- ZeroMemory(pDbCache+ofsDest+bytes-x, x);
- if (ofsSource < dwFileSize)
- MoveMemory(pDbCache+ofsDest,pDbCache+ofsSource, bytes-x);
-
- logg();
-}
-
-//we are assumed to be in a mutex here
-PBYTE DBRead(DWORD ofs,int bytesRequired,int *bytesAvail)
-{
- // buggy read
- if (ofs>=dwFileSize) {
- log2("read from outside %d@%08x",bytesRequired,ofs);
- if (bytesAvail!=NULL) *bytesAvail = ChunkSize;
- return pNull;
- }
- log3((ofs+bytesRequired>dwFileSize)?"read %d@%08x, only %d avaliable":"read %d@%08x",bytesRequired,ofs,dwFileSize-ofs);
- if (bytesAvail!=NULL) *bytesAvail = dwFileSize - ofs;
- return pDbCache+ofs;
-}
-
-//we are assumed to be in a mutex here
-void DBWrite(DWORD ofs,PVOID pData,int bytes)
-{
- log2("write %d@%08x",bytes,ofs);
- if (ofs+bytes>dwFileSize) ReMap(ofs+bytes-dwFileSize);
- MoveMemory(pDbCache+ofs,pData,bytes);
- logg();
-}
-
-//we are assumed to be in a mutex here
-void DBFill(DWORD ofs,int bytes)
-{
- log2("zerofill %d@%08x",bytes,ofs);
- if (ofs+bytes <= dwFileSize)
- ZeroMemory(pDbCache+ofs,bytes);
- logg();
-}
-
-static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd, UINT message, UINT_PTR idEvent, DWORD dwTime)
-{
- if (!pDbCache) return;
-
- KillTimer(NULL,flushBuffersTimerId);
- log0("tflush1");
- if (FlushViewOfFile(pDbCache, 0) == 0) {
- if (flushFailTick == 0)
- flushFailTick = GetTickCount();
- else if (GetTickCount() - flushFailTick > 5000)
- DatabaseCorruption(NULL);
- }
- else
- flushFailTick = 0;
- log0("tflush2");
-}
-
-void DBFlush(int setting)
-{
- if (!setting) {
- log0("nflush1");
- if (safetyMode && pDbCache) {
- if (FlushViewOfFile(pDbCache, 0) == 0) {
- if (flushFailTick == 0)
- flushFailTick = GetTickCount();
- else if (GetTickCount() - flushFailTick > 5000)
- DatabaseCorruption(NULL);
- }
- else
- flushFailTick = 0;
- }
- log0("nflush2");
- return;
- }
- KillTimer(NULL,flushBuffersTimerId);
- flushBuffersTimerId = SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc);
-}
-
-int InitCache(void)
-{
- DWORD x;
- SYSTEM_INFO sinf;
-
- GetSystemInfo(&sinf);
- ChunkSize = sinf.dwAllocationGranularity;
-
- dwFileSize = GetFileSize(hDbFile, NULL);
-
- // Align to chunk
- x = dwFileSize % ChunkSize;
- if (x) dwFileSize += ChunkSize - x;
-
- Map();
-
- // zero region for reads outside the file
- pNull = (PBYTE)calloc(ChunkSize, 1);
- return 0;
-}
-
-void UninitCache(void)
-{
- KillTimer(NULL,flushBuffersTimerId);
- FlushViewOfFile(pDbCache, 0);
- UnmapViewOfFile(pDbCache);
- CloseHandle(hMap);
- if (pNull) free(pNull);
-}
diff --git a/plugins/Dbx_mmap_SA/dbcontacts.cpp b/plugins/Dbx_mmap_SA/dbcontacts.cpp deleted file mode 100644 index 40a8e4faac..0000000000 --- a/plugins/Dbx_mmap_SA/dbcontacts.cpp +++ /dev/null @@ -1,285 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-extern HANDLE hCacheHeap;
-extern SortedList lContacts;
-extern HANDLE hLastCachedContact;
-
-INT_PTR GetContactSettingStatic(WPARAM wParam,LPARAM lParam);
-void FreeCachedVariant( DBVARIANT* V );
-
-static HANDLE hContactDeletedEvent,hContactAddedEvent;
-
-int InitContacts(void)
-{
- hContactDeletedEvent = CreateHookableEvent(ME_DB_CONTACT_DELETED);
- hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED);
- return 0;
-}
-
-DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index)
-{
- DBCachedContactValueList* VL;
- VL = (DBCachedContactValueList*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList));
- VL->hContact = hContact;
- if (index == -1) List_GetIndex(&lContacts,VL,&index);
- List_Insert(&lContacts,VL,index);
- return VL;
-}
-
-STDMETHODIMP_(LONG) CDdxMmap::GetContactCount(void)
-{
- int ret;
-
- EnterCriticalSection(&csDbAccess);
- ret = dbHeader.contactCount;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-#define proto_module "Protocol"
-#define proto_setting "p"
-
-int CDdxMmap::CheckProto(HANDLE hContact, const char *proto)
-{
- static char protobuf[MAX_PATH] = {0};
- static DBVARIANT dbv;
- static DBCONTACTGETSETTING sVal = {proto_module,proto_setting,&dbv};
-
- dbv.type = DBVT_ASCIIZ;
- dbv.pszVal = protobuf;
- dbv.cchVal = sizeof(protobuf);
-
- if (GetContactSettingStatic(hContact, &sVal) != 0 || dbv.type != DBVT_ASCIIZ)
- return 0;
-
- return !strcmp(protobuf,proto);
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstContact(const char* szProto)
-{
- HANDLE ret = 0;
- EnterCriticalSection(&csDbAccess);
- ret = (HANDLE)dbHeader.ofsFirstContact;
- if (szProto && !CheckProto(ret, szProto))
- ret = FindNextContact(ret, szProto);
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindNextContact(HANDLE hContact, const char* szProto)
-{
- int index;
- DBContact *dbc;
- DBCachedContactValueList VLtemp, *VL = NULL;
- VLtemp.hContact = hContact;
-
- EnterCriticalSection(&csDbAccess);
-
- while(VLtemp.hContact) {
- if ( List_GetIndex(&lContacts,&VLtemp,&index)) {
- VL = ( DBCachedContactValueList* )lContacts.items[index];
- if (VL->hNext != NULL) {
- if (!szProto || CheckProto(VL->hNext, szProto)) {
- LeaveCriticalSection(&csDbAccess);
- return VL->hNext;
- }
- else {
- VLtemp.hContact = VL->hNext;
- continue;
- } } }
-
- dbc = (DBContact*)DBRead((DWORD)VLtemp.hContact,sizeof(DBContact),NULL);
- if (dbc->signature!=DBCONTACT_SIGNATURE)
- break;
- else {
- if ( VL == NULL )
- VL = AddToCachedContactList(VLtemp.hContact,index);
-
- VL->hNext = (HANDLE)dbc->ofsNext;
- if (VL->hNext != NULL && (!szProto || CheckProto(VL->hNext, szProto))) {
- LeaveCriticalSection(&csDbAccess);
- return VL->hNext;
- }
- VLtemp.hContact = VL->hNext;
- } }
- LeaveCriticalSection(&csDbAccess);
- return 0;
-}
-
-STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact)
-{
- DBContact *dbcPrev;
- DWORD ofsThis, ofsNext, ofsFirstEvent;
- struct DBContactSettings *dbcs;
- DBEvent *dbe;
- int index;
-
- if (hContact == NULL)
- return 1;
-
- EnterCriticalSection(&csDbAccess);
- DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- if (hContact == (HANDLE)dbHeader.ofsUser ) {
- LeaveCriticalSection(&csDbAccess);
- log0("FATAL: del of user chain attempted.");
- return 1;
- }
- log0("del contact");
- LeaveCriticalSection(&csDbAccess);
- //call notifier while outside mutex
- NotifyEventHooks(hContactDeletedEvent, (WPARAM)hContact, 0);
- //get back in
- EnterCriticalSection(&csDbAccess);
-
- DBCachedContactValueList VLtemp;
- VLtemp.hContact = hContact;
- if ( List_GetIndex(&lContacts,&VLtemp,&index)) {
- DBCachedContactValueList *VL = (DBCachedContactValueList*)lContacts.items[index];
- DBCachedContactValue* V = VL->first;
- while ( V != NULL ) {
- DBCachedContactValue* V1 = V->next;
- FreeCachedVariant(&V->value);
- HeapFree( hCacheHeap, 0, V );
- V = V1;
- }
- HeapFree( hCacheHeap, 0, VL );
-
- if (VLtemp.hContact == hLastCachedContact)
- hLastCachedContact = NULL;
- List_Remove(&lContacts,index);
- }
-
- dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
- //delete settings chain
- ofsThis = dbc->ofsFirstSettings;
- ofsFirstEvent = dbc->ofsFirstEvent;
- while(ofsThis) {
- dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL);
- ofsNext = dbcs->ofsNext;
- DeleteSpace(ofsThis,offsetof(struct DBContactSettings,blob)+dbcs->cbBlob);
- ofsThis = ofsNext;
- }
- //delete event chain
- ofsThis = ofsFirstEvent;
- while(ofsThis) {
- dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- ofsNext = dbe->ofsNext;
- DeleteSpace(ofsThis,offsetof(DBEvent,blob)+dbe->cbBlob);
- ofsThis = ofsNext;
- }
- //find previous contact in chain and change ofsNext
- dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbHeader.ofsFirstContact == (DWORD)hContact) {
- dbHeader.ofsFirstContact = dbc->ofsNext;
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- }
- else {
- ofsNext = dbc->ofsNext;
- ofsThis = dbHeader.ofsFirstContact;
- dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL);
- while(dbcPrev->ofsNext != (DWORD)hContact) {
- if (dbcPrev->ofsNext == 0)
- DatabaseCorruption(NULL);
- ofsThis = dbcPrev->ofsNext;
- dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL);
- }
- dbcPrev->ofsNext = ofsNext;
- DBWrite(ofsThis,dbcPrev,sizeof(DBContact));
- {
- DBCachedContactValueList VLtemp;
- VLtemp.hContact = (HANDLE)ofsThis;
- if ( List_GetIndex(&lContacts,&VLtemp,&index))
- {
- DBCachedContactValueList *VL = ( DBCachedContactValueList* )lContacts.items[index];
- VL->hNext = ( HANDLE )ofsNext;
- } }
- }
- //delete contact
- DeleteSpace((DWORD)hContact,sizeof(DBContact));
- //decrement contact count
- dbHeader.contactCount--;
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- DBFlush(0);
- //quit
- LeaveCriticalSection(&csDbAccess);
- return 0;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::AddContact(void)
-{
- DBContact dbc;
- DWORD ofsNew;
-
- log0("add contact");
- EnterCriticalSection(&csDbAccess);
- ofsNew = CreateNewSpace(sizeof(DBContact));
- dbc.signature = DBCONTACT_SIGNATURE;
- dbc.eventCount = 0;
- dbc.ofsFirstEvent = dbc.ofsLastEvent = 0;
- dbc.ofsFirstSettings = 0;
- dbc.ofsNext = dbHeader.ofsFirstContact;
- dbc.ofsFirstUnreadEvent = 0;
- dbc.timestampFirstUnread = 0;
- dbHeader.ofsFirstContact = ofsNew;
- dbHeader.contactCount++;
- DBWrite(ofsNew,&dbc,sizeof(DBContact));
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- DBFlush(0);
-
- AddToCachedContactList((HANDLE)ofsNew, -1);
-
- LeaveCriticalSection(&csDbAccess);
- NotifyEventHooks(hContactAddedEvent,(WPARAM)ofsNew,0);
- return (HANDLE)ofsNew;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::IsDbContact(HANDLE hContact)
-{
- DBContact *dbc;
- DWORD ofsContact = (DWORD)hContact;
- int ret;
-
- EnterCriticalSection(&csDbAccess);
- {
- int index;
- DBCachedContactValueList VLtemp;
- VLtemp.hContact = hContact;
- if ( List_GetIndex(&lContacts,&VLtemp,&index))
- ret = TRUE;
- else {
- dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- ret = dbc->signature == DBCONTACT_SIGNATURE;
- if (ret)
- AddToCachedContactList(hContact, index);
- } }
-
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
diff --git a/plugins/Dbx_mmap_SA/dbevents.cpp b/plugins/Dbx_mmap_SA/dbevents.cpp deleted file mode 100644 index 2ee826c589..0000000000 --- a/plugins/Dbx_mmap_SA/dbevents.cpp +++ /dev/null @@ -1,457 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-extern BOOL safetyMode;
-
-DWORD GetModuleNameOfs(const char *szName);
-char *GetModuleNameByOfs(DWORD ofs);
-
-static HANDLE hEventDeletedEvent,hEventAddedEvent,hEventFilterAddedEvent;
-
-int InitEvents(void)
-{
- hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED);
- hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED);
- hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD);
- return 0;
-}
-
-STDMETHODIMP_(LONG) CDdxMmap::GetEventCount(HANDLE hContact)
-{
- LONG ret;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
-
- DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) ret = -1;
- else ret = dbc->eventCount;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
-{
- DBEvent dbe,*dbeTest;
- DWORD ofsNew,ofsModuleName,ofsContact,ofsThis;
- BOOL neednotify;
-
- if (dbei == NULL || dbei->cbSize != sizeof(DBEVENTINFO)) return 0;
- if (dbei->timestamp == 0) return 0;
- if (NotifyEventHooks(hEventFilterAddedEvent, (WPARAM)hContact, (LPARAM)dbei))
- return 0;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0) ofsContact = dbHeader.ofsUser;
- else ofsContact = (DWORD)hContact;
- DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc.signature!=DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 0;
- }
- ofsNew = CreateNewSpace(offsetof(DBEvent,blob)+dbei->cbBlob);
- ofsModuleName = GetModuleNameOfs(dbei->szModule);
-
- dbe.signature = DBEVENT_SIGNATURE;
- dbe.ofsModuleName = ofsModuleName;
- dbe.timestamp = dbei->timestamp;
- dbe.flags = dbei->flags;
- dbe.eventType = dbei->eventType;
- dbe.cbBlob = dbei->cbBlob;
- //find where to put it - sort by timestamp
- if (dbc.eventCount == 0) {
- dbe.ofsPrev = (DWORD)hContact;
- dbe.ofsNext = 0;
- dbe.flags |= DBEF_FIRST;
- dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew;
- }
- else {
- dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent,sizeof(DBEvent),NULL);
- // Should new event be placed before first event in chain?
- if (dbei->timestamp < dbeTest->timestamp) {
- dbe.ofsPrev = (DWORD)hContact;
- dbe.ofsNext = dbc.ofsFirstEvent;
- dbe.flags |= DBEF_FIRST;
- dbc.ofsFirstEvent = ofsNew;
- dbeTest = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
- dbeTest->flags &= ~DBEF_FIRST;
- dbeTest->ofsPrev = ofsNew;
- DBWrite(dbe.ofsNext,dbeTest,sizeof(DBEvent));
- }
- else {
- // Loop through the chain, starting at the end
- ofsThis = dbc.ofsLastEvent;
- dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
- for (;;) {
- // If the new event's timesstamp is equal to or greater than the
- // current dbevent, it will be inserted after. If not, continue
- // with the previous dbevent in chain.
- if (dbe.timestamp >= dbeTest->timestamp) {
- dbe.ofsPrev = ofsThis;
- dbe.ofsNext = dbeTest->ofsNext;
- dbeTest->ofsNext = ofsNew;
- DBWrite(ofsThis, dbeTest, sizeof(DBEvent));
- if (dbe.ofsNext == 0)
- dbc.ofsLastEvent = ofsNew;
- else {
- dbeTest = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
- dbeTest->ofsPrev = ofsNew;
- DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
- }
- break;
- }
- ofsThis = dbeTest->ofsPrev;
- dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
- }
- }
- }
- dbc.eventCount++;
- if (!(dbe.flags&(DBEF_READ|DBEF_SENT))) {
- if (dbe.timestamp<dbc.timestampFirstUnread || dbc.timestampFirstUnread == 0) {
- dbc.timestampFirstUnread = dbe.timestamp;
- dbc.ofsFirstUnreadEvent = ofsNew;
- }
- neednotify = TRUE;
- }
- else neednotify = safetyMode;
-
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob));
- EncodeDBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob);
- DBFlush(0);
-
- LeaveCriticalSection(&csDbAccess);
- log1("add event @ %08x",ofsNew);
-
- // Notify only in safe mode or on really new events
- if (neednotify)
- NotifyEventHooks(hEventAddedEvent, (WPARAM)hContact, (LPARAM)ofsNew);
-
- return (HANDLE)ofsNew;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
-{
- DBContact dbc;
- DWORD ofsContact,ofsThis;
- DBEvent dbe,*dbeNext,*dbePrev;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0) ofsContact = dbHeader.ofsUser;
- else ofsContact = (DWORD)hContact;
- dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbc.signature != DBCONTACT_SIGNATURE || dbe.signature != DBEVENT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- log1("delete event @ %08x",wParam);
- LeaveCriticalSection(&csDbAccess);
- //call notifier while outside mutex
- NotifyEventHooks(hEventDeletedEvent, (WPARAM)hContact, (LPARAM)hDbEvent);
- //get back in
- EnterCriticalSection(&csDbAccess);
- dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- //check if this was the first unread, if so, recalc the first unread
- if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) {
- dbeNext = &dbe;
- for (;;) {
- if (dbeNext->ofsNext == 0) {
- dbc.ofsFirstUnreadEvent = 0;
- dbc.timestampFirstUnread = 0;
- break;
- }
- ofsThis = dbeNext->ofsNext;
- dbeNext = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- if (!(dbeNext->flags&(DBEF_READ|DBEF_SENT))) {
- dbc.ofsFirstUnreadEvent = ofsThis;
- dbc.timestampFirstUnread = dbeNext->timestamp;
- break;
- }
- }
- }
- //get previous and next events in chain and change offsets
- if (dbe.flags&DBEF_FIRST) {
- if (dbe.ofsNext == 0) {
- dbc.ofsFirstEvent = dbc.ofsLastEvent = 0;
- }
- else {
- dbeNext = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
- dbeNext->flags |= DBEF_FIRST;
- dbeNext->ofsPrev = dbe.ofsPrev;
- DBWrite(dbe.ofsNext,dbeNext,sizeof(DBEvent));
- dbc.ofsFirstEvent = dbe.ofsNext;
- }
- }
- else {
- if (dbe.ofsNext == 0) {
- dbePrev = (DBEvent*)DBRead(dbe.ofsPrev,sizeof(DBEvent),NULL);
- dbePrev->ofsNext = 0;
- DBWrite(dbe.ofsPrev,dbePrev,sizeof(DBEvent));
- dbc.ofsLastEvent = dbe.ofsPrev;
- }
- else {
- dbePrev = (DBEvent*)DBRead(dbe.ofsPrev,sizeof(DBEvent),NULL);
- dbePrev->ofsNext = dbe.ofsNext;
- DBWrite(dbe.ofsPrev,dbePrev,sizeof(DBEvent));
- dbeNext = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
- dbeNext->ofsPrev = dbe.ofsPrev;
- DBWrite(dbe.ofsNext,dbeNext,sizeof(DBEvent));
- }
- }
- //delete event
- DeleteSpace((DWORD)hDbEvent,offsetof(DBEvent,blob)+dbe.cbBlob);
- //decrement event count
- dbc.eventCount--;
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- DBFlush(0);
- //quit
- LeaveCriticalSection(&csDbAccess);
- return 0;
-}
-
-STDMETHODIMP_(LONG) CDdxMmap::GetBlobSize(HANDLE hDbEvent)
-{
- LONG ret;
-
- EnterCriticalSection(&csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature != DBEVENT_SIGNATURE) ret = -1;
- else ret = dbe->cbBlob;
-
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-void CDdxMmap::EncodeContactEvents(HANDLE hContact)
-{
- HANDLE hEvent = FindFirstEvent(hContact);
- if (hEvent == 0) return;
- do {
- EncodeEvent(hEvent);
- }
- while (hEvent = FindNextEvent(hEvent));
-}
-
-extern Cryptor* CryptoEngine;
-extern void* key;
-
-void CDdxMmap::EncodeEvent(HANDLE hEvent)
-{
- DBEvent *dbe = (DBEvent*)DBRead((DWORD)hEvent,sizeof(DBEvent),NULL);
- if (dbe->signature = DBEVENT_SIGNATURE)
- CryptoEngine->EncryptMem(DBRead((DWORD)hEvent + offsetof(DBEvent,blob), dbe->cbBlob, NULL), dbe->cbBlob, key);
-}
-
-void CDdxMmap::DecodeEvent(HANDLE hEvent)
-{
- DBEvent *dbe = (DBEvent*)DBRead((DWORD)hEvent,sizeof(DBEvent),NULL);
- if (dbe->signature = DBEVENT_SIGNATURE)
- CryptoEngine->DecryptMem(DBRead((DWORD)hEvent + offsetof(DBEvent,blob), dbe->cbBlob, NULL), dbe->cbBlob, key);
-}
-
-void CDdxMmap::DecodeContactEvents(HANDLE hContact)
-{
- HANDLE hEvent;
-
- hEvent = FindFirstEvent(hContact);
- if (hEvent == 0) return;
- do {
- DecodeEvent(hEvent);
- }
- while (hEvent = FindNextEvent(hEvent));
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei)
-{
- int bytesToCopy,i;
-
- if (dbei == NULL||dbei->cbSize!=sizeof(DBEVENTINFO)) return 1;
- if (dbei->cbBlob>0 && dbei->pBlob == NULL) {
- dbei->cbBlob = 0;
- return 1;
- }
- EnterCriticalSection(&csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature != DBEVENT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName);
- dbei->timestamp = dbe->timestamp;
- dbei->flags = dbe->flags;
- dbei->eventType = dbe->eventType;
- if (dbei->cbBlob<dbe->cbBlob) bytesToCopy = dbei->cbBlob;
- else bytesToCopy = dbe->cbBlob;
- dbei->cbBlob = dbe->cbBlob;
- if (bytesToCopy && dbei->pBlob)
- {
- for (i = 0;;i += MAXCACHEDREADSIZE) {
- if (bytesToCopy-i <= MAXCACHEDREADSIZE) {
- DecodeCopyMemory(dbei->pBlob+i,DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,bytesToCopy-i,NULL),bytesToCopy-i);
- break;
- }
- DecodeCopyMemory(dbei->pBlob+i,DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,MAXCACHEDREADSIZE,NULL),MAXCACHEDREADSIZE);
- }
- }
- LeaveCriticalSection(&csDbAccess);
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
-{
- LONG ret;
- DBEvent *dbe;
- DBContact dbc;
- DWORD ofsThis;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
- dbc = *(DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature!=DBEVENT_SIGNATURE || dbc.signature!=DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return -1;
- }
- if (dbe->flags&DBEF_READ || dbe->flags&DBEF_SENT) {
- ret = (INT_PTR)dbe->flags;
- LeaveCriticalSection(&csDbAccess);
- return ret;
- }
- log1("mark read @ %08x",wParam);
- dbe->flags |= DBEF_READ;
- DBWrite((DWORD)hDbEvent,dbe,sizeof(DBEvent));
- ret = (int)dbe->flags;
- if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) {
- for (;;) {
- if (dbe->ofsNext == 0) {
- dbc.ofsFirstUnreadEvent = 0;
- dbc.timestampFirstUnread = 0;
- break;
- }
- ofsThis = dbe->ofsNext;
- dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- if (!(dbe->flags&(DBEF_READ|DBEF_SENT))) {
- dbc.ofsFirstUnreadEvent = ofsThis;
- dbc.timestampFirstUnread = dbe->timestamp;
- break;
- }
- }
- }
- DBWrite(DWORD(hContact),&dbc,sizeof(DBContact));
- DBFlush(0);
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::GetEventContact(HANDLE hDbEvent)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature != DBEVENT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return (HANDLE)-1;
- }
- while( !(dbe->flags & DBEF_FIRST))
- dbe = (DBEvent*)DBRead(dbe->ofsPrev,sizeof(DBEvent),NULL);
- ret = (HANDLE)dbe->ofsPrev;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstEvent(HANDLE hContact)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
-
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) ret = 0;
- else ret = (HANDLE)dbc->ofsFirstEvent;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstUnreadEvent(HANDLE hContact)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
-
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) ret = 0;
- else ret = (HANDLE)dbc->ofsFirstUnreadEvent;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindLastEvent(HANDLE hContact)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
-
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) ret = 0;
- else ret = (HANDLE)dbc->ofsLastEvent;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindNextEvent(HANDLE hDbEvent)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature != DBEVENT_SIGNATURE) ret = 0;
- else ret = (HANDLE)dbe->ofsNext;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
-
-STDMETHODIMP_(HANDLE) CDdxMmap::FindPrevEvent(HANDLE hDbEvent)
-{
- HANDLE ret;
-
- EnterCriticalSection(&csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
- if (dbe->signature!=DBEVENT_SIGNATURE) ret = 0;
- else if (dbe->flags&DBEF_FIRST) ret = 0;
- else ret = (HANDLE)dbe->ofsPrev;
- LeaveCriticalSection(&csDbAccess);
- return ret;
-}
diff --git a/plugins/Dbx_mmap_SA/dbheaders.cpp b/plugins/Dbx_mmap_SA/dbheaders.cpp deleted file mode 100644 index af42b2b996..0000000000 --- a/plugins/Dbx_mmap_SA/dbheaders.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-//the cache has not been loaded when these functions are used
-
-int CreateDbHeaders(HANDLE hFile)
-{
- DBContact user;
- DWORD bytesWritten;
-
- CopyMemory(dbHeader.signature,&dbSignature,sizeof(dbHeader.signature));
- dbHeader.checkWord = 0x0700;
- dbHeader.cryptorUID = 0x0000;
- dbHeader.ofsFileEnd = sizeof(struct DBHeader);
- dbHeader.slackSpace = 0;
- dbHeader.contactCount = 0;
- dbHeader.ofsFirstContact = 0;
- dbHeader.ofsFirstModuleName = 0;
- dbHeader.ofsUser = 0;
- //create user
- dbHeader.ofsUser = dbHeader.ofsFileEnd;
- dbHeader.ofsFileEnd += sizeof(DBContact);
-
- SetFilePointer(hFile,0,NULL,FILE_BEGIN);
- WriteFile(hFile,&dbHeader,sizeof(dbHeader),&bytesWritten,NULL);
- user.signature = DBCONTACT_SIGNATURE;
- user.ofsNext = 0;
- user.ofsFirstSettings = 0;
- user.eventCount = 0;
- user.ofsFirstEvent = user.ofsLastEvent = 0;
- SetFilePointer(hFile,dbHeader.ofsUser,NULL,FILE_BEGIN);
- WriteFile(hFile,&user,sizeof(DBContact),&bytesWritten,NULL);
- FlushFileBuffers(hFile);
- return 0;
-}
-
-int CheckDbHeaders(struct DBHeader * hdr)
-{
- if (memcmp(hdr->signature,&dbSignatureSecured,sizeof(hdr->signature)) == 0){
- bEncoding = 1;
- }else{
- bEncoding = 0;
- if (memcmp(hdr->signature,&dbSignature,sizeof(hdr->signature))) return 1;
- if (hdr->checkWord!=0x0700) return 2;
- }
- if (hdr->ofsUser == 0) return 3;
- return 0;
-}
-
-int InitialiseDbHeaders(void)
-{
- return 0;
-}
diff --git a/plugins/Dbx_mmap_SA/dbintf.cpp b/plugins/Dbx_mmap_SA/dbintf.cpp deleted file mode 100644 index c30d8206cc..0000000000 --- a/plugins/Dbx_mmap_SA/dbintf.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/*
-
-Miranda NG: the free IM client for Microsoft* Windows*
-
-Copyright 2012 Miranda NG project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-extern BOOL safetyMode;
-
-CDdxMmap::CDdxMmap(const TCHAR* tszFileName)
-{
-}
-
-STDMETHODIMP_(void) CDdxMmap::SetCacheSafetyMode(BOOL bIsSet)
-{
- { mir_cslock lck(csDbAccess);
- safetyMode = bIsSet;
- }
- DBFlush(1);
-}
diff --git a/plugins/Dbx_mmap_SA/dbintf.h b/plugins/Dbx_mmap_SA/dbintf.h deleted file mode 100644 index 74740cd31d..0000000000 --- a/plugins/Dbx_mmap_SA/dbintf.h +++ /dev/null @@ -1,87 +0,0 @@ -/*
-
-Miranda NG: the free IM client for Microsoft* Windows*
-
-Copyright 2012 Miranda NG project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include <m_db_int.h>
-
-struct CDdxMmap : public MIDatabase
-{
- CDdxMmap(const TCHAR* tszFileName);
-
- STDMETHODIMP_(void) SetCacheSafetyMode(BOOL);
-
- STDMETHODIMP_(LONG) GetContactCount(void);
- STDMETHODIMP_(HANDLE) FindFirstContact(const char* szProto = NULL);
- STDMETHODIMP_(HANDLE) FindNextContact(HANDLE hContact, const char* szProto = NULL);
- STDMETHODIMP_(LONG) DeleteContact(HANDLE hContact);
- STDMETHODIMP_(HANDLE) AddContact(void);
- STDMETHODIMP_(BOOL) IsDbContact(HANDLE hContact);
-
- STDMETHODIMP_(LONG) GetEventCount(HANDLE hContact);
- STDMETHODIMP_(HANDLE) AddEvent(HANDLE hContact, DBEVENTINFO *dbei);
- STDMETHODIMP_(BOOL) DeleteEvent(HANDLE hContact, HANDLE hDbEvent);
- STDMETHODIMP_(LONG) GetBlobSize(HANDLE hDbEvent);
- STDMETHODIMP_(BOOL) GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei);
- STDMETHODIMP_(BOOL) MarkEventRead(HANDLE hContact, HANDLE hDbEvent);
- STDMETHODIMP_(HANDLE) GetEventContact(HANDLE hDbEvent);
- STDMETHODIMP_(HANDLE) FindFirstEvent(HANDLE hContact);
- STDMETHODIMP_(HANDLE) FindFirstUnreadEvent(HANDLE hContact);
- STDMETHODIMP_(HANDLE) FindLastEvent(HANDLE hContact);
- STDMETHODIMP_(HANDLE) FindNextEvent(HANDLE hDbEvent);
- STDMETHODIMP_(HANDLE) FindPrevEvent(HANDLE hDbEvent);
-
- STDMETHODIMP_(BOOL) EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam);
-
- STDMETHODIMP_(BOOL) GetContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dbcgs);
- STDMETHODIMP_(BOOL) GetContactSettingStr(HANDLE hContact, DBCONTACTGETSETTING *dbcgs);
- STDMETHODIMP_(BOOL) GetContactSettingStatic(HANDLE hContact, DBCONTACTGETSETTING *dbcgs);
- STDMETHODIMP_(BOOL) FreeVariant(DBVARIANT *dbv);
- STDMETHODIMP_(BOOL) WriteContactSetting(HANDLE hContact, DBCONTACTWRITESETTING *dbcws);
- STDMETHODIMP_(BOOL) DeleteContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dbcgs);
- STDMETHODIMP_(BOOL) EnumContactSettings(HANDLE hContact, DBCONTACTENUMSETTINGS* dbces);
- STDMETHODIMP_(BOOL) SetSettingResident(BOOL bIsResident, const char *pszSettingName);
- STDMETHODIMP_(BOOL) EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam);
-
- int CheckPassword(WORD checkWord, TCHAR *szDBName);
-
- void ChangePwd();
- void EncryptDB();
- void DecryptDB();
- void RecryptDB();
-
-private:
- int CheckProto(HANDLE hContact, const char *proto);
-
- void EncodeContactEvents(HANDLE hContact);
- void EncodeEvent(HANDLE hEvent);
- void DecodeEvent(HANDLE hEvent);
- void DecodeContactEvents(HANDLE hContact);
-
- void DecodeContactSettings(HANDLE hContact);
- void EncodeContactSettings(HANDLE hContact);
-
- void WritePlainHeader();
- void WriteCryptHeader();
-
- void EncodeAll();
- void DecodeAll();
-};
diff --git a/plugins/Dbx_mmap_SA/dbintf_sa.cpp b/plugins/Dbx_mmap_SA/dbintf_sa.cpp new file mode 100644 index 0000000000..b85b3481cb --- /dev/null +++ b/plugins/Dbx_mmap_SA/dbintf_sa.cpp @@ -0,0 +1,234 @@ +/*
+
+Miranda NG: the free IM client for Microsoft* Windows*
+
+Copyright 2012 Miranda NG project,
+all portions of this codebase are copyrighted to the people
+listed in contributors.txt.
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+#include "commonheaders.h"
+
+#define NeedBytes(n) if(bytesRemaining<(n)) pBlob=(PBYTE)DBRead(ofsBlobPtr,(n),&bytesRemaining)
+#define MoveAlong(n) {int x=n; pBlob+=(x); ofsBlobPtr+=(x); bytesRemaining-=(x);}
+#define VLT(n) ((n==DBVT_UTF8)?DBVT_ASCIIZ:n)
+
+extern CDdxMmapSA* g_Db;
+
+int InitDialogs(void);
+
+DBSignature dbSignatureSecured = {"Miranda ICQ SD",0x1A};
+
+CDdxMmapSA::CDdxMmapSA(const TCHAR* tszFileName) :
+ CDdxMmap(tszFileName)
+{
+}
+
+int CDdxMmapSA::Load(bool bSkipInit)
+{
+ if ( CDdxMmap::Load(bSkipInit) != ERROR_SUCCESS)
+ return 1;
+
+ if ( CheckDbHeaders())
+ return 1;
+
+ if (!bSkipInit) {
+ TCHAR* p = _tcsrchr(m_tszProfileName, '\\');
+ if (!p)
+ return 1;
+
+ g_Db = this;
+ if (m_bEncoding && !CheckPassword( LOWORD(m_dbHeader.version), p+1))
+ return 1;
+
+ InitDialogs();
+ }
+
+ return 0;
+}
+
+int CDdxMmapSA::CheckDbHeaders()
+{
+ if ( memcmp(m_dbHeader.signature, &dbSignatureSecured, sizeof(m_dbHeader.signature)) == 0)
+ m_bEncoding = true;
+ else {
+ m_bEncoding = false;
+ if ( memcmp(m_dbHeader.signature,&dbSignature,sizeof(m_dbHeader.signature)))
+ return 1;
+ if ( LOWORD(m_dbHeader.version) != 0x0700)
+ return 2;
+ }
+ if (m_dbHeader.ofsUser == 0)
+ return 3;
+ return 0;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+static DWORD __inline GetSettingValueLength(PBYTE pSetting)
+{
+ if(pSetting[0]&DBVTF_VARIABLELENGTH) return 2+*(PWORD)(pSetting+1);
+ return pSetting[0];
+}
+
+void CDdxMmapSA::EncodeContactSettings(HANDLE hContact)
+{
+ if (!hContact)
+ hContact = (HANDLE)m_dbHeader.ofsUser;
+
+ DBContact *contact = (DBContact *)DBRead((DWORD)hContact, sizeof(DBContact), NULL);
+ if (contact->ofsFirstSettings) {
+ DBContactSettings *setting = (struct DBContactSettings *)DBRead(contact->ofsFirstSettings, sizeof(struct DBContactSettings), NULL);
+ DWORD offset = contact->ofsFirstSettings;
+ while( true ) {
+ DWORD ofsBlobPtr;
+ PBYTE pBlob;
+ int bytesRemaining;
+ DWORD len;
+
+ ofsBlobPtr = offset + offsetof(struct DBContactSettings,blob);
+ pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
+ while(pBlob[0]) {
+ NeedBytes(1);
+ NeedBytes(1+pBlob[0]);
+ MoveAlong(1+pBlob[0]);
+
+ NeedBytes(5);
+
+ switch(pBlob[0]) {
+ case DBVT_DELETED: break;
+ case DBVT_BYTE: break;
+ case DBVT_WORD:
+ CryptoEngine->EncryptMem(pBlob+1, 2, key);
+ break;
+
+ case DBVT_DWORD:
+ CryptoEngine->EncryptMem(pBlob+1, 4, key);
+ break;
+
+ case DBVT_UTF8:
+ case DBVT_ASCIIZ:
+ case DBVT_BLOB:
+ NeedBytes(3+*(PWORD)(pBlob+1));
+ len = *(PWORD)(pBlob+1);
+
+ CryptoEngine->EncryptMem(pBlob+3, len, key);
+ break;
+ }
+ NeedBytes(3);
+ MoveAlong(1+GetSettingValueLength(pBlob));
+ NeedBytes(1);
+ }
+
+ if (!setting->ofsNext)
+ break;
+
+ offset = setting->ofsNext;
+ setting = (struct DBContactSettings *)DBRead(setting->ofsNext, sizeof(struct DBContactSettings), NULL);
+ }
+ }
+}
+
+void CDdxMmapSA::DecodeContactSettings(HANDLE hContact)
+{
+ if (!hContact)
+ hContact = (HANDLE)m_dbHeader.ofsUser;
+
+ DBContact *contact = (DBContact *)DBRead((DWORD)hContact, sizeof(DBContact), NULL);
+ if (contact->ofsFirstSettings){
+ DBContactSettings *setting = (struct DBContactSettings *)DBRead(contact->ofsFirstSettings, sizeof(struct DBContactSettings), NULL);
+ DWORD offset = contact->ofsFirstSettings;
+ while (true) {
+ DWORD ofsBlobPtr;
+ PBYTE pBlob;
+ int bytesRemaining;
+ DWORD len;
+ ofsBlobPtr = offset + offsetof(struct DBContactSettings,blob);
+ pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
+ while(pBlob[0]) {
+ NeedBytes(1);
+ NeedBytes(1+pBlob[0]);
+ //CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0;
+ MoveAlong(1+pBlob[0]);
+
+ NeedBytes(5);
+
+ switch(pBlob[0]) {
+ case DBVT_DELETED: break;
+ case DBVT_BYTE: break;
+ case DBVT_WORD:
+ CryptoEngine->DecryptMem(pBlob+1, 2, key);
+ break;
+
+ case DBVT_DWORD:
+ CryptoEngine->DecryptMem(pBlob+1, 4, key);
+ break;
+
+ case DBVT_UTF8:
+ case DBVT_ASCIIZ:
+ case DBVT_BLOB:
+ NeedBytes(3+*(PWORD)(pBlob+1));
+ len = *(PWORD)(pBlob+1);
+
+ CryptoEngine->DecryptMem(pBlob+3, len, key);
+ break;
+ }
+ NeedBytes(3);
+ MoveAlong(1+GetSettingValueLength(pBlob));
+ NeedBytes(1);
+ }
+
+ if (!setting->ofsNext)
+ break;
+
+ offset = setting->ofsNext;
+ setting = (struct DBContactSettings *)DBRead(setting->ofsNext, sizeof(struct DBContactSettings), NULL);
+ }
+ }
+}
+
+void CDdxMmapSA::EncodeEvent(HANDLE hEvent)
+{
+ DBEvent *dbe = (DBEvent*)DBRead((DWORD)hEvent,sizeof(DBEvent),NULL);
+ if (dbe->signature = DBEVENT_SIGNATURE)
+ CryptoEngine->EncryptMem(DBRead((DWORD)hEvent + offsetof(DBEvent,blob), dbe->cbBlob, NULL), dbe->cbBlob, key);
+}
+
+void CDdxMmapSA::DecodeEvent(HANDLE hEvent)
+{
+ DBEvent *dbe = (DBEvent*)DBRead((DWORD)hEvent,sizeof(DBEvent),NULL);
+ if (dbe->signature = DBEVENT_SIGNATURE)
+ CryptoEngine->DecryptMem(DBRead((DWORD)hEvent + offsetof(DBEvent,blob), dbe->cbBlob, NULL), dbe->cbBlob, key);
+}
+
+void CDdxMmapSA::EncodeContactEvents(HANDLE hContact)
+{
+ HANDLE hEvent = FindFirstEvent(hContact);
+ while (hEvent != 0) {
+ EncodeEvent(hEvent);
+ hEvent = FindNextEvent(hEvent);
+ }
+}
+
+void CDdxMmapSA::DecodeContactEvents(HANDLE hContact)
+{
+ HANDLE hEvent = FindFirstEvent(hContact);
+ while (hEvent != 0) {
+ DecodeEvent(hEvent);
+ hEvent = FindNextEvent(hEvent);
+ }
+}
diff --git a/plugins/Dbx_mmap_SA/dbintf_sa.h b/plugins/Dbx_mmap_SA/dbintf_sa.h new file mode 100644 index 0000000000..265d2a32e0 --- /dev/null +++ b/plugins/Dbx_mmap_SA/dbintf_sa.h @@ -0,0 +1,71 @@ +/*
+
+Miranda NG: the free IM client for Microsoft* Windows*
+
+Copyright 2012 Miranda NG project,
+all portions of this codebase are copyrighted to the people
+listed in contributors.txt.
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+*/
+
+#include <m_db_int.h>
+
+#include "..\Db3x_mmap\dbintf.h"
+
+struct CDdxMmapSA : public CDdxMmap
+{
+ CDdxMmapSA(const TCHAR* tszFileName);
+
+ int CheckPassword(WORD checkWord, TCHAR *szDBName);
+ int CheckDbHeaders(void);
+
+ void ChangePwd();
+ void EncryptDB();
+ void DecryptDB();
+ void RecryptDB();
+
+ int Load(bool bSkipInit);
+
+ bool m_bEncoding, bEncProcess;
+
+protected:
+ virtual void EncodeCopyMemory(void *dst, void *src, size_t size);
+ virtual void DecodeCopyMemory(void *dst, void *src, size_t size);
+ virtual void EncodeDBWrite(DWORD ofs, void *src, int size);
+ virtual void DecodeDBWrite(DWORD ofs, void *src, int size);
+
+protected:
+ int CheckProto(HANDLE hContact, const char *proto);
+
+ void EncoderInit();
+ void EncodeContactEvents(HANDLE hContact);
+ void EncodeEvent(HANDLE hEvent);
+ void DecodeEvent(HANDLE hEvent);
+ void DecodeContactEvents(HANDLE hContact);
+
+ void DecodeContactSettings(HANDLE hContact);
+ void EncodeContactSettings(HANDLE hContact);
+
+ void WritePlainHeader();
+ void WriteCryptHeader();
+
+ void EncodeAll();
+ void DecodeAll();
+
+public:
+ char encryptKey[255];
+ size_t encryptKeyLength;
+};
diff --git a/plugins/Dbx_mmap_SA/dbmodulechain.cpp b/plugins/Dbx_mmap_SA/dbmodulechain.cpp deleted file mode 100644 index 9c3d163e67..0000000000 --- a/plugins/Dbx_mmap_SA/dbmodulechain.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-typedef struct {
- char *name;
- DWORD ofs;
-} ModuleName;
-
-HANDLE hModHeap = NULL;
-static SortedList lMods, lOfs;
-
-static int ModCompare( ModuleName *mn1, ModuleName *mn2 )
-{
- return strcmp( mn1->name, mn2->name );
-}
-
-static int OfsCompare( ModuleName *mn1, ModuleName *mn2 )
-{
- return ( mn1->ofs - mn2->ofs );
-}
-
-void AddToList(char *name, DWORD len, DWORD ofs)
-{
- int index;
- ModuleName *mn = (ModuleName*)HeapAlloc(hModHeap,0,sizeof(ModuleName));
- mn->name = name;
- mn->ofs = ofs;
-
- if (List_GetIndex(&lMods,mn,&index))
- DatabaseCorruption( _T("%s (Module Name not unique)"));
-
- List_Insert(&lMods,mn,index);
-
- if (List_GetIndex(&lOfs,mn,&index))
- DatabaseCorruption( _T("%s (Module Offset not unique)"));
-
- List_Insert(&lOfs,mn,index);
-}
-
-
-int InitModuleNames(void)
-{
- struct DBModuleName *dbmn;
- DWORD ofsThis;
- int nameLen;
- char *mod;
-
- hModHeap = HeapCreate(0, 0, 0);
- lMods.sortFunc = (FSortFunc)ModCompare;
- lMods.increment = 50;
- lOfs.sortFunc = (FSortFunc)OfsCompare;
- lOfs.increment = 50;
-
- ofsThis = dbHeader.ofsFirstModuleName;
- dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL);
- while(ofsThis) {
- if (dbmn->signature!=DBMODULENAME_SIGNATURE) DatabaseCorruption(NULL);
-
- nameLen = dbmn->cbName;
-
- mod = (char*)HeapAlloc(hModHeap,0,nameLen+1);
- CopyMemory(mod,DBRead(ofsThis+offsetof(struct DBModuleName,name),nameLen,NULL),nameLen);
- mod[nameLen] = 0;
-
- AddToList(mod, nameLen, ofsThis);
-
- ofsThis = dbmn->ofsNext;
- dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL);
- }
- return 0;
-}
-
-void UninitModuleNames(void)
-{
- HeapDestroy(hModHeap);
- List_Destroy(&lMods);
- List_Destroy(&lOfs);
-}
-
-static DWORD FindExistingModuleNameOfs(const char *szName)
-{
- static ModuleName *lastmn = NULL;
- ModuleName mn, *pmn;
- int index;
-
- mn.name = (char*)szName;
- mn.ofs = 0;
-
- if (lastmn && ModCompare(&mn,lastmn) == 0)
- return lastmn->ofs;
-
- if (List_GetIndex(&lMods,&mn,&index)) {
- pmn = (ModuleName*)lMods.items[index];
- lastmn = pmn;
- return pmn->ofs;
- }
-
- return 0;
-}
-
-//will create the offset if it needs to
-DWORD GetModuleNameOfs(const char *szName)
-{
- struct DBModuleName dbmn;
- int nameLen;
- DWORD ofsNew,ofsExisting;
- char *mod;
-
- ofsExisting = FindExistingModuleNameOfs(szName);
- if (ofsExisting) return ofsExisting;
-
- nameLen = (int)strlen(szName);
-
- //need to create the module name
- ofsNew = CreateNewSpace(nameLen+offsetof(struct DBModuleName,name));
- dbmn.signature = DBMODULENAME_SIGNATURE;
- dbmn.cbName = nameLen;
- dbmn.ofsNext = dbHeader.ofsFirstModuleName;
- dbHeader.ofsFirstModuleName = ofsNew;
- DBWrite(0,&dbHeader,sizeof(dbHeader));
- DBWrite(ofsNew,&dbmn,offsetof(struct DBModuleName,name));
- DBWrite(ofsNew+offsetof(struct DBModuleName,name),(PVOID)szName,nameLen);
- DBFlush(0);
-
- //add to cache
- mod = (char*)HeapAlloc(hModHeap,0,nameLen+1);
- strcpy(mod,szName);
- AddToList(mod, nameLen, ofsNew);
-
- //quit
- return ofsNew;
-}
-
-char *GetModuleNameByOfs(DWORD ofs)
-{
- static ModuleName *lastmn = NULL;
- ModuleName mn, *pmn;
- int index;
-
- if (lastmn && lastmn->ofs == ofs)
- return lastmn->name;
-
- mn.name = NULL;
- mn.ofs = ofs;
-
- if (List_GetIndex(&lOfs,&mn,&index)) {
- pmn = (ModuleName*)lOfs.items[index];
- lastmn = pmn;
- return pmn->name;
- }
-
- DatabaseCorruption(NULL);
- return NULL;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::EnumModuleNames(DBMODULEENUMPROC pFunc, void *pParam)
-{
- int ret;
- ModuleName *pmn;
- for (int i = 0; i < lMods.realCount; i++) {
- pmn = (ModuleName *)lMods.items[i];
- ret = pFunc(pmn->name, pmn->ofs, (LPARAM)pParam);
- if (ret)
- return ret;
- }
- return 0;
-}
diff --git a/plugins/Dbx_mmap_SA/dbpreset.cpp b/plugins/Dbx_mmap_SA/dbpreset.cpp index 0334f8f5d8..564bbf3c40 100644 --- a/plugins/Dbx_mmap_SA/dbpreset.cpp +++ b/plugins/Dbx_mmap_SA/dbpreset.cpp @@ -26,9 +26,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h"
/* Public API */
-int InitPreset();
-void UninitPreset();
-
int DBPreset_QuerySetting (const char *szModule, const char *szSetting, DBVARIANT *dbv, BOOL isStatic);
int DBPreset_CompareSetting (const char *szModule, const char *szSetting, DBVARIANT *dbv);
diff --git a/plugins/Dbx_mmap_SA/dbsettings.cpp b/plugins/Dbx_mmap_SA/dbsettings.cpp deleted file mode 100644 index 26900dd94e..0000000000 --- a/plugins/Dbx_mmap_SA/dbsettings.cpp +++ /dev/null @@ -1,1154 +0,0 @@ -/*
-
-Miranda IM: the free IM client for Microsoft* Windows*
-
-Copyright 2000-2003 Miranda ICQ/IM project,
-all portions of this codebase are copyrighted to the people
-listed in contributors.txt.
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-*/
-
-#include "commonheaders.h"
-
-int DBPreset_QuerySetting(const char *szModule, const char *szSetting, DBVARIANT *dbv, BOOL isStatic);
-int DBPreset_CompareSetting(const char *szModule, const char *szSetting, DBVARIANT *dbv);
-
-DWORD GetModuleNameOfs(const char *szName);
-DBCachedContactValueList* AddToCachedContactList(HANDLE hContact, int index);
-
-HANDLE hCacheHeap = NULL;
-SortedList lContacts = {0};
-HANDLE hLastCachedContact = NULL;
-static DBCachedContactValueList *LastVL = NULL;
-
-static int mirCp = CP_ACP;
-
-static SortedList lSettings = {0}, lGlobalSettings = {0}, lResidentSettings = {0};
-static HANDLE hSettingChangeEvent = NULL;
-
-static DWORD GetSettingsGroupOfsByModuleNameOfs(DBContact *dbc,DWORD ofsModuleName)
-{
- struct DBContactSettings *dbcs;
- DWORD ofsThis;
-
- ofsThis = dbc->ofsFirstSettings;
- while(ofsThis) {
- dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL);
- if (dbcs->signature!=DBCONTACTSETTINGS_SIGNATURE) DatabaseCorruption(NULL);
- if (dbcs->ofsModuleName == ofsModuleName)
- return ofsThis;
-
- ofsThis = dbcs->ofsNext;
- }
- return 0;
-}
-
-static DWORD __inline GetSettingValueLength(PBYTE pSetting)
-{
- if (pSetting[0]&DBVTF_VARIABLELENGTH) return 2+*(PWORD)(pSetting+1);
- return pSetting[0];
-}
-
-static char* InsertCachedSetting( const char* szName, size_t cbNameLen, int index )
-{
- char* newValue = (char*)HeapAlloc( hCacheHeap, 0, cbNameLen );
- *newValue = 0;
- strcpy(newValue+1,szName+1);
- List_Insert(&lSettings,newValue,index);
- return newValue;
-}
-
-static char* GetCachedSetting(const char *szModuleName,const char *szSettingName, int moduleNameLen, int settingNameLen)
-{
- static char *lastsetting = NULL;
- int index;
- char szFullName[512];
-
- strcpy(szFullName+1,szModuleName);
- szFullName[moduleNameLen+1] = '/';
- strcpy(szFullName+moduleNameLen+2,szSettingName);
-
- if (lastsetting && strcmp(szFullName+1,lastsetting) == 0)
- return lastsetting;
-
- if (List_GetIndex(&lSettings,szFullName,&index))
- lastsetting = (char*)lSettings.items[index]+1;
- else
- lastsetting = InsertCachedSetting( szFullName, settingNameLen+moduleNameLen+3, index )+1;
-
- return lastsetting;
-}
-
-static void SetCachedVariant( DBVARIANT* s /* new */, DBVARIANT* d /* cached */ )
-{
- char* szSave = ( d->type == DBVT_UTF8 || d->type == DBVT_ASCIIZ ) ? d->pszVal : NULL;
-
- memcpy( d, s, sizeof( DBVARIANT ));
- if (( s->type == DBVT_UTF8 || s->type == DBVT_ASCIIZ ) && s->pszVal != NULL ) {
- if ( szSave != NULL )
- d->pszVal = (char*)HeapReAlloc(hCacheHeap,0,szSave,strlen(s->pszVal)+1);
- else
- d->pszVal = (char*)HeapAlloc(hCacheHeap,0,strlen(s->pszVal)+1);
- strcpy(d->pszVal,s->pszVal);
- }
- else if ( szSave != NULL )
- HeapFree(hCacheHeap,0,szSave);
-
-#ifdef DBLOGGING
- switch( d->type ) {
- case DBVT_BYTE: log1( "set cached byte: %d", d->bVal ); break;
- case DBVT_WORD: log1( "set cached word: %d", d->wVal ); break;
- case DBVT_DWORD: log1( "set cached dword: %d", d->dVal ); break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ: log1( "set cached string: '%s'", d->pszVal ); break;
- default: log1( "set cached crap: %d", d->type ); break;
- }
-#endif
-}
-
-void FreeCachedVariant( DBVARIANT* V )
-{
- if (( V->type == DBVT_ASCIIZ || V->type == DBVT_UTF8 ) && V->pszVal != NULL )
- HeapFree(hCacheHeap,0,V->pszVal);
-}
-
-static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllocate )
-{
- int index;
-
- if ( hContact == 0 ) {
- DBCachedGlobalValue Vtemp, *V;
- Vtemp.name = szSetting;
- if ( List_GetIndex(&lGlobalSettings,&Vtemp,&index)) {
- V = (DBCachedGlobalValue*)lGlobalSettings.items[index];
- if ( bAllocate == -1 ) {
- FreeCachedVariant( &V->value );
- List_Remove(&lGlobalSettings,index);
- HeapFree(hCacheHeap,0,V);
- return NULL;
- } }
- else {
- if ( bAllocate != 1 )
- return NULL;
-
- V = (DBCachedGlobalValue*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedGlobalValue));
- V->name = szSetting;
- List_Insert(&lGlobalSettings,V,index);
- }
-
- return &V->value;
- }
- else {
- DBCachedContactValue *V, *V1;
- DBCachedContactValueList VLtemp,*VL;
-
- if (hLastCachedContact == hContact && LastVL) {
- VL = LastVL;
- }
- else {
- VLtemp.hContact = hContact;
-
- if ( !List_GetIndex(&lContacts,&VLtemp,&index))
- {
- if ( bAllocate != 1 )
- return NULL;
-
- VL = AddToCachedContactList(hContact,index);
- }
- else VL = (DBCachedContactValueList*)lContacts.items[index];
-
- LastVL = VL;
- hLastCachedContact = hContact;
- }
-
- for ( V = VL->first; V != NULL; V = V->next)
- if (V->name == szSetting)
- break;
-
- if ( V == NULL ) {
- if ( bAllocate != 1 )
- return NULL;
-
- V = (DBCachedContactValue *)HeapAlloc(hCacheHeap, HEAP_ZERO_MEMORY, sizeof(DBCachedContactValue));
- if (VL->last)
- VL->last->next = V;
- else
- VL->first = V;
- VL->last = V;
- V->name = szSetting;
- }
- else if ( bAllocate == -1 ) {
- LastVL = NULL;
- FreeCachedVariant(&V->value);
- if ( VL->first == V ) {
- VL->first = V->next;
- if (VL->last == V)
- VL->last = V->next; // NULL
- }
- else
- for ( V1 = VL->first; V1 != NULL; V1 = V1->next )
- if ( V1->next == V ) {
- V1->next = V->next;
- if (VL->last == V)
- VL->last = V1;
- break;
- }
- HeapFree(hCacheHeap,0,V);
- return NULL;
- }
-
- return &V->value;
-} }
-
-#define NeedBytes(n) if (bytesRemaining<(n)) pBlob = (PBYTE)DBRead(ofsBlobPtr,(n),&bytesRemaining)
-#define MoveAlong(n) {int x = n; pBlob += (x); ofsBlobPtr += (x); bytesRemaining -= (x);}
-#define VLT(n) ((n == DBVT_UTF8)?DBVT_ASCIIZ:n)
-static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs,int isStatic)
-{
- DBContact *dbc;
- DWORD ofsModuleName,ofsContact,ofsSettingsGroup,ofsBlobPtr;
- int settingNameLen,moduleNameLen;
- int bytesRemaining;
- PBYTE pBlob;
- char* szCachedSettingName;
-
- if ((!dbcgs->szSetting) || (!dbcgs->szModule))
- return 1;
- // the db format can't tolerate more than 255 bytes of space (incl. null) for settings+module name
- settingNameLen = (int)strlen(dbcgs->szSetting);
- moduleNameLen = (int)strlen(dbcgs->szModule);
- if ( settingNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("GetContactSettingWorker() got a > 255 setting name length. \n");
- #endif
- return 1;
- }
- if ( moduleNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("GetContactSettingWorker() got a > 255 module name length. \n");
- #endif
- return 1;
- }
-
- EnterCriticalSection(&csDbAccess);
-
- log3("get [%08p] %s/%s",hContact,dbcgs->szModule,dbcgs->szSetting);
-
- szCachedSettingName = GetCachedSetting(dbcgs->szModule,dbcgs->szSetting,moduleNameLen,settingNameLen);
- {
- DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 0 );
- if ( pCachedValue != NULL ) {
- if ( pCachedValue->type == DBVT_ASCIIZ || pCachedValue->type == DBVT_UTF8 ) {
- int cbOrigLen = dbcgs->pValue->cchVal;
- char* cbOrigPtr = dbcgs->pValue->pszVal;
- memcpy( dbcgs->pValue, pCachedValue, sizeof( DBVARIANT ));
- if ( isStatic ) {
- int cbLen = 0;
- if ( pCachedValue->pszVal != NULL )
- cbLen = (int)strlen( pCachedValue->pszVal );
-
- cbOrigLen--;
- dbcgs->pValue->pszVal = cbOrigPtr;
- if (cbLen<cbOrigLen) cbOrigLen = cbLen;
- CopyMemory(dbcgs->pValue->pszVal,pCachedValue->pszVal,cbOrigLen);
- dbcgs->pValue->pszVal[cbOrigLen] = 0;
- dbcgs->pValue->cchVal = cbLen;
- }
- else {
- dbcgs->pValue->pszVal = (char*)mir_alloc(strlen(pCachedValue->pszVal)+1);
- strcpy(dbcgs->pValue->pszVal,pCachedValue->pszVal);
- }
- }
- else
- memcpy( dbcgs->pValue, pCachedValue, sizeof( DBVARIANT ));
-
- switch( dbcgs->pValue->type ) {
- case DBVT_BYTE: log1( "get cached byte: %d", dbcgs->pValue->bVal ); break;
- case DBVT_WORD: log1( "get cached word: %d", dbcgs->pValue->wVal ); break;
- case DBVT_DWORD: log1( "get cached dword: %d", dbcgs->pValue->dVal ); break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ: log1( "get cached string: '%s'", dbcgs->pValue->pszVal); break;
- default: log1( "get cached crap: %d", dbcgs->pValue->type ); break;
- }
-
- LeaveCriticalSection(&csDbAccess);
- return ( pCachedValue->type == DBVT_DELETED ) ? 1 : 0;
- } }
-
- ofsModuleName = GetModuleNameOfs(dbcgs->szModule);
- if (hContact == NULL) ofsContact = dbHeader.ofsUser;
- else ofsContact = (DWORD)hContact;
- dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc->signature!=DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
- if (ofsSettingsGroup) {
- ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob);
- pBlob = DBRead(ofsBlobPtr,sizeof(struct DBContactSettings),&bytesRemaining);
- while(pBlob[0]) {
- NeedBytes(1+settingNameLen);
- if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen)) {
- MoveAlong(1+settingNameLen);
- NeedBytes(5);
- if (isStatic && pBlob[0]&DBVTF_VARIABLELENGTH && VLT(dbcgs->pValue->type) != VLT(pBlob[0])) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- dbcgs->pValue->type = pBlob[0];
- switch(pBlob[0]) {
- case DBVT_DELETED: { /* this setting is deleted */
- dbcgs->pValue->type = DBVT_DELETED;
- LeaveCriticalSection(&csDbAccess);
- return 2;
- }
- case DBVT_BYTE: dbcgs->pValue->bVal = pBlob[1]; break;
- case DBVT_WORD: DecodeCopyMemory(&(dbcgs->pValue->wVal), (PWORD)(pBlob+1), 2); break;
- case DBVT_DWORD: DecodeCopyMemory(&(dbcgs->pValue->dVal), (PDWORD)(pBlob+1), 4); break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ:
- NeedBytes(3+*(PWORD)(pBlob+1));
- if (isStatic) {
- dbcgs->pValue->cchVal--;
- if (*(PWORD)(pBlob+1)<dbcgs->pValue->cchVal) dbcgs->pValue->cchVal = *(PWORD)(pBlob+1);
- DecodeCopyMemory(dbcgs->pValue->pszVal,pBlob+3,dbcgs->pValue->cchVal);
- dbcgs->pValue->pszVal[dbcgs->pValue->cchVal] = 0;
- dbcgs->pValue->cchVal = *(PWORD)(pBlob+1);
- }
- else {
- dbcgs->pValue->pszVal = (char*)mir_alloc(1+*(PWORD)(pBlob+1));
- DecodeCopyMemory(dbcgs->pValue->pszVal,pBlob+3,*(PWORD)(pBlob+1));
- dbcgs->pValue->pszVal[*(PWORD)(pBlob+1)] = 0;
- }
- break;
- case DBVT_BLOB:
- NeedBytes(3+*(PWORD)(pBlob+1));
- if (isStatic) {
- if (*(PWORD)(pBlob+1)<dbcgs->pValue->cpbVal) dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1);
- DecodeCopyMemory(dbcgs->pValue->pbVal,pBlob+3,dbcgs->pValue->cpbVal);
- }
- else {
- dbcgs->pValue->pbVal = (BYTE *)mir_alloc(*(PWORD)(pBlob+1));
- DecodeCopyMemory(dbcgs->pValue->pbVal,pBlob+3,*(PWORD)(pBlob+1));
- }
- dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1);
- break;
- }
-
- /**** add to cache **********************/
- if ( dbcgs->pValue->type != DBVT_BLOB )
- {
- DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 1 );
- if ( pCachedValue != NULL )
- SetCachedVariant(dbcgs->pValue,pCachedValue);
- }
-
- LeaveCriticalSection(&csDbAccess);
- logg();
- return 0;
- }
- NeedBytes(1);
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- } }
-
- /**** nullbie: query info from preset **********************/
- if (!hContact && DBPreset_QuerySetting(dbcgs->szModule, dbcgs->szSetting, dbcgs->pValue, isStatic))
- {
- /**** add to cache **********************/
- if ( dbcgs->pValue->type != DBVT_BLOB )
- {
- DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 1 );
- if ( pCachedValue != NULL )
- SetCachedVariant(dbcgs->pValue,pCachedValue);
- }
- return 0;
- }
-
- /**** add missing setting to cache **********************/
- if ( dbcgs->pValue->type != DBVT_BLOB )
- {
- DBVARIANT* pCachedValue = GetCachedValuePtr( hContact, szCachedSettingName, 1 );
- if ( pCachedValue != NULL )
- pCachedValue->type = DBVT_DELETED;
- }
-
- LeaveCriticalSection(&csDbAccess);
- logg();
- return 1;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::GetContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dgs)
-{
- dgs->pValue->type = 0;
- if ( GetContactSettingWorker(hContact, dgs, 0 ))
- return 1;
-
- if ( dgs->pValue->type == DBVT_UTF8 ) {
- WCHAR* tmp = NULL;
- char* p = NEWSTR_ALLOCA(dgs->pValue->pszVal);
- if ( mir_utf8decode( p, &tmp ) != NULL ) {
- BOOL bUsed = FALSE;
- int result = WideCharToMultiByte( mirCp, WC_NO_BEST_FIT_CHARS, tmp, -1, NULL, 0, NULL, &bUsed );
-
- mir_free( dgs->pValue->pszVal );
-
- if ( bUsed || result == 0 ) {
- dgs->pValue->type = DBVT_WCHAR;
- dgs->pValue->pwszVal = tmp;
- }
- else {
- dgs->pValue->type = DBVT_ASCIIZ;
- dgs->pValue->pszVal = (char *)mir_alloc(result);
- WideCharToMultiByte( mirCp, WC_NO_BEST_FIT_CHARS, tmp, -1, dgs->pValue->pszVal, result, NULL, NULL );
- mir_free( tmp );
- }
- }
- else {
- dgs->pValue->type = DBVT_ASCIIZ;
- mir_free( tmp );
- } }
-
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::GetContactSettingStr(HANDLE hContact, DBCONTACTGETSETTING *dgs)
-{
- int iSaveType = dgs->pValue->type;
-
- if ( GetContactSettingWorker(hContact, dgs, 0 ))
- return 1;
-
- if ( iSaveType == 0 || iSaveType == dgs->pValue->type )
- return 0;
-
- if ( dgs->pValue->type != DBVT_ASCIIZ && dgs->pValue->type != DBVT_UTF8 )
- return 1;
-
- if ( iSaveType == DBVT_WCHAR ) {
- if ( dgs->pValue->type != DBVT_UTF8 ) {
- int len = MultiByteToWideChar( CP_ACP, 0, dgs->pValue->pszVal, -1, NULL, 0 );
- wchar_t* wszResult = ( wchar_t* )mir_alloc(( len+1 )*sizeof( wchar_t ));
- if ( wszResult == NULL )
- return 1;
-
- MultiByteToWideChar( CP_ACP, 0, dgs->pValue->pszVal, -1, wszResult, len );
- wszResult[ len ] = 0;
- mir_free( dgs->pValue->pszVal );
- dgs->pValue->pwszVal = wszResult;
- }
- else {
- char* savePtr = NEWSTR_ALLOCA(dgs->pValue->pszVal);
- mir_free( dgs->pValue->pszVal );
- if ( !mir_utf8decode( savePtr, &dgs->pValue->pwszVal ))
- return 1;
- }
- }
- else if ( iSaveType == DBVT_UTF8 ) {
- char* tmpBuf = mir_utf8encode( dgs->pValue->pszVal );
- if ( tmpBuf == NULL )
- return 1;
-
- mir_free( dgs->pValue->pszVal );
- dgs->pValue->pszVal = tmpBuf;
- }
- else if ( iSaveType == DBVT_ASCIIZ )
- mir_utf8decode( dgs->pValue->pszVal, NULL );
-
- dgs->pValue->type = iSaveType;
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::GetContactSettingStatic(HANDLE hContact, DBCONTACTGETSETTING *dgs)
-{
- if ( GetContactSettingWorker(hContact, dgs, 1 ))
- return 1;
-
- if ( dgs->pValue->type == DBVT_UTF8 ) {
- mir_utf8decode( dgs->pValue->pszVal, NULL );
- dgs->pValue->type = DBVT_ASCIIZ;
- }
-
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::FreeVariant(DBVARIANT *dbv)
-{
- if ( dbv == 0 ) return 1;
- switch ( dbv->type ) {
- case DBVT_ASCIIZ:
- case DBVT_UTF8:
- case DBVT_WCHAR:
- {
- if ( dbv->pszVal ) mir_free(dbv->pszVal);
- dbv->pszVal = 0;
- break;
- }
- case DBVT_BLOB:
- {
- if ( dbv->pbVal ) mir_free(dbv->pbVal);
- dbv->pbVal = 0;
- break;
- }
- }
- dbv->type = 0;
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *pszSettingName)
-{
- size_t cbSettingNameLen = strlen(pszSettingName) + 2;
- if (cbSettingNameLen < 512)
- {
- char* szSetting;
- int idx;
- char szTemp[512];
- strcpy(szTemp+1, pszSettingName);
-
- EnterCriticalSection(&csDbAccess);
- if ( !List_GetIndex( &lSettings, szTemp, &idx ))
- szSetting = InsertCachedSetting( szTemp, cbSettingNameLen, idx );
- else
- szSetting = (char *)lSettings.items[idx];
-
- *szSetting = (char)bIsResident;
-
- if ( !List_GetIndex( &lResidentSettings, szSetting+1, &idx ))
- {
- if (bIsResident)
- List_Insert(&lResidentSettings,szSetting+1,idx);
- }
- else if (!bIsResident)
- List_Remove(&lResidentSettings,idx);
-
- LeaveCriticalSection(&csDbAccess);
- }
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRITESETTING *dbcws)
-{
- DBCONTACTWRITESETTING tmp;
- DBContact dbc;
- DWORD ofsModuleName;
- struct DBContactSettings dbcs;
- PBYTE pBlob;
- int settingNameLen = 0;
- int moduleNameLen = 0;
- int settingDataLen = 0;
- int bytesRequired,bytesRemaining;
- DWORD ofsContact,ofsSettingsGroup,ofsBlobPtr;
-
- if (dbcws == NULL || dbcws->szSetting == NULL || dbcws->szModule == NULL )
- return 1;
-
- // the db format can't tolerate more than 255 bytes of space (incl. null) for settings+module name
- settingNameLen = (int)strlen(dbcws->szSetting);
- moduleNameLen = (int)strlen(dbcws->szModule);
- if ( settingNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("WriteContactSetting() got a > 255 setting name length. \n");
- #endif
- return 1;
- }
- if ( moduleNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("WriteContactSetting() got a > 255 module name length. \n");
- #endif
- return 1;
- }
-
- tmp = *dbcws;
- if (tmp.value.type == DBVT_WCHAR) {
- if (tmp.value.pszVal != NULL) {
- char* val = mir_utf8encodeW(tmp.value.pwszVal);
- if ( val == NULL )
- return 1;
-
- tmp.value.pszVal = ( char* )alloca( strlen( val )+1 );
- strcpy( tmp.value.pszVal, val );
- mir_free(val);
- tmp.value.type = DBVT_UTF8;
- }
- else return 1;
- }
-
- if (tmp.value.type!=DBVT_BYTE && tmp.value.type!=DBVT_WORD && tmp.value.type!=DBVT_DWORD && tmp.value.type!=DBVT_ASCIIZ && tmp.value.type!=DBVT_UTF8 && tmp.value.type!=DBVT_BLOB)
- return 1;
- if ((!tmp.szModule) || (!tmp.szSetting) || ((tmp.value.type == DBVT_ASCIIZ || tmp.value.type == DBVT_UTF8 )&& tmp.value.pszVal == NULL) || (tmp.value.type == DBVT_BLOB && tmp.value.pbVal == NULL))
- return 1;
-
- // the db can not tolerate strings/blobs longer than 0xFFFF since the format writes 2 lengths
- switch( tmp.value.type ) {
- case DBVT_ASCIIZ: case DBVT_BLOB: case DBVT_UTF8:
- { size_t len = ( tmp.value.type != DBVT_BLOB ) ? (int)strlen(tmp.value.pszVal) : tmp.value.cpbVal;
- if ( len >= 0xFFFF ) {
- #ifdef _DEBUG
- OutputDebugStringA("WriteContactSetting() writing huge string/blob, rejecting ( >= 0xFFFF ) \n");
- #endif
- return 1;
- }
- }
- }
-
- EnterCriticalSection(&csDbAccess);
- {
- char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, moduleNameLen, settingNameLen);
- if ( tmp.value.type != DBVT_BLOB ) {
- DBVARIANT* pCachedValue = GetCachedValuePtr(hContact, szCachedSettingName, 1);
- if ( pCachedValue != NULL ) {
- BOOL bIsIdentical = FALSE;
- if ( pCachedValue->type == tmp.value.type ) {
- switch(tmp.value.type) {
- case DBVT_BYTE: bIsIdentical = pCachedValue->bVal == tmp.value.bVal; break;
- case DBVT_WORD: bIsIdentical = pCachedValue->wVal == tmp.value.wVal; break;
- case DBVT_DWORD: bIsIdentical = pCachedValue->dVal == tmp.value.dVal; break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ: bIsIdentical = strcmp( pCachedValue->pszVal, tmp.value.pszVal ) == 0; break;
- }
- if ( bIsIdentical ) {
- LeaveCriticalSection(&csDbAccess);
- return 0;
- }
- }
- SetCachedVariant(&tmp.value, pCachedValue);
- }
- if ( szCachedSettingName[-1] != 0 ) {
- LeaveCriticalSection(&csDbAccess);
- NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
- return 0;
- }
- }
- else GetCachedValuePtr(hContact, szCachedSettingName, -1);
- }
-
- ofsModuleName = GetModuleNameOfs(tmp.szModule);
- if (hContact == 0)
- ofsContact = dbHeader.ofsUser;
- else
- ofsContact = (DWORD)hContact;
-
- dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc.signature!=DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- log0("write setting");
- //make sure the module group exists
- ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsModuleName);
- if (ofsSettingsGroup == 0) { //module group didn't exist - make it
- if (tmp.value.type&DBVTF_VARIABLELENGTH) {
- if (tmp.value.type == DBVT_ASCIIZ || tmp.value.type == DBVT_UTF8) bytesRequired = (int)strlen(tmp.value.pszVal)+2;
- else if (tmp.value.type == DBVT_BLOB) bytesRequired = tmp.value.cpbVal+2;
- }
- else bytesRequired = tmp.value.type;
- bytesRequired += 2+settingNameLen;
- bytesRequired += (DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY;
- ofsSettingsGroup = CreateNewSpace(bytesRequired+offsetof(struct DBContactSettings,blob));
- dbcs.signature = DBCONTACTSETTINGS_SIGNATURE;
- dbcs.ofsNext = dbc.ofsFirstSettings;
- dbcs.ofsModuleName = ofsModuleName;
- dbcs.cbBlob = bytesRequired;
- dbcs.blob[0] = 0;
- dbc.ofsFirstSettings = ofsSettingsGroup;
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- DBWrite(ofsSettingsGroup,&dbcs,sizeof(struct DBContactSettings));
- ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- }
- else {
- dbcs = *(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),&bytesRemaining);
- //find if the setting exists
- ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- while(pBlob[0]) {
- NeedBytes(settingNameLen+1);
- if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,tmp.szSetting,settingNameLen))
- break;
- NeedBytes(1);
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- if (pBlob[0]) { //setting already existed, and up to end of name is in cache
- MoveAlong(1+settingNameLen);
- //if different type or variable length and length is different
- NeedBytes(3);
- if (pBlob[0]!=tmp.value.type || ((pBlob[0] == DBVT_ASCIIZ || pBlob[0] == DBVT_UTF8) && *(PWORD)(pBlob+1)!=strlen(tmp.value.pszVal)) || (pBlob[0] == DBVT_BLOB && *(PWORD)(pBlob+1)!=tmp.value.cpbVal)) {
- //bin it
- int nameLen,valLen;
- DWORD ofsSettingToCut;
- NeedBytes(3);
- nameLen = 1+settingNameLen;
- valLen = 1+GetSettingValueLength(pBlob);
- ofsSettingToCut = ofsBlobPtr-nameLen;
- MoveAlong(valLen);
- NeedBytes(1);
- while(pBlob[0]) {
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- DBMoveChunk(ofsSettingToCut,ofsSettingToCut+nameLen+valLen,ofsBlobPtr+1-ofsSettingToCut);
- ofsBlobPtr -= nameLen+valLen;
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- }
- else {
- //replace existing setting at pBlob
- MoveAlong(1); //skip data type
- switch(tmp.value.type) {
- case DBVT_BYTE: DBWrite(ofsBlobPtr,&tmp.value.bVal,1); break;
- case DBVT_WORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.wVal,2); break;
- case DBVT_DWORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.dVal,4); break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ: EncodeDBWrite(ofsBlobPtr+2,tmp.value.pszVal,(int)strlen(tmp.value.pszVal)); break;
- case DBVT_BLOB: EncodeDBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal); break;
- }
- //quit
- DBFlush(1);
- LeaveCriticalSection(&csDbAccess);
- //notify
- NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
- return 0;
- }
- }
- }
- //cannot do a simple replace, add setting to end of list
- //pBlob already points to end of list
- //see if it fits
- if (tmp.value.type&DBVTF_VARIABLELENGTH) {
- if (tmp.value.type == DBVT_ASCIIZ || tmp.value.type == DBVT_UTF8) bytesRequired = (int)strlen(tmp.value.pszVal)+2;
- else if (tmp.value.type == DBVT_BLOB) bytesRequired = tmp.value.cpbVal+2;
- }
- else bytesRequired = tmp.value.type;
- bytesRequired += 2+settingNameLen;
- bytesRequired += ofsBlobPtr+1-(ofsSettingsGroup+offsetof(struct DBContactSettings,blob));
- if ((DWORD)bytesRequired>dbcs.cbBlob) {
- //doesn't fit: move entire group
- struct DBContactSettings *dbcsPrev;
- DWORD ofsDbcsPrev,ofsNew;
-
- bytesRequired += (DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY;
- //find previous group to change its offset
- ofsDbcsPrev = dbc.ofsFirstSettings;
- if (ofsDbcsPrev == ofsSettingsGroup) ofsDbcsPrev = 0;
- else {
- dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL);
- while(dbcsPrev->ofsNext!=ofsSettingsGroup) {
- if (dbcsPrev->ofsNext == 0) DatabaseCorruption(NULL);
- ofsDbcsPrev = dbcsPrev->ofsNext;
- dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL);
- }
- }
-
- //create the new one
- ofsNew = ReallocSpace(ofsSettingsGroup, dbcs.cbBlob+offsetof(struct DBContactSettings,blob), bytesRequired+offsetof(struct DBContactSettings,blob));
-
- dbcs.cbBlob = bytesRequired;
-
- DBWrite(ofsNew,&dbcs,offsetof(struct DBContactSettings,blob));
- if (ofsDbcsPrev == 0) {
- dbc.ofsFirstSettings = ofsNew;
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- }
- else {
- dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL);
- dbcsPrev->ofsNext = ofsNew;
- DBWrite(ofsDbcsPrev,dbcsPrev,offsetof(struct DBContactSettings,blob));
- }
- ofsBlobPtr += ofsNew-ofsSettingsGroup;
- ofsSettingsGroup = ofsNew;
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- }
- //we now have a place to put it and enough space: make it
- DBWrite(ofsBlobPtr,&settingNameLen,1);
- DBWrite(ofsBlobPtr+1,(PVOID)tmp.szSetting,settingNameLen);
- MoveAlong(1+settingNameLen);
- DBWrite(ofsBlobPtr,&tmp.value.type,1);
- MoveAlong(1);
- switch(tmp.value.type) {
- case DBVT_BYTE: DBWrite(ofsBlobPtr,&tmp.value.bVal,1); MoveAlong(1); break;
- case DBVT_WORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.wVal,2); MoveAlong(2); break;
- case DBVT_DWORD: EncodeDBWrite(ofsBlobPtr,&tmp.value.dVal,4); MoveAlong(4); break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ:
- { int len = (int)strlen(tmp.value.pszVal);
- DBWrite(ofsBlobPtr,&len,2);
- EncodeDBWrite(ofsBlobPtr+2,tmp.value.pszVal,len);
- MoveAlong(2+len);
- }
- break;
- case DBVT_BLOB:
- DBWrite(ofsBlobPtr,&tmp.value.cpbVal,2);
- EncodeDBWrite(ofsBlobPtr+2,tmp.value.pbVal,tmp.value.cpbVal);
- MoveAlong(2+tmp.value.cpbVal);
- break;
- }
- { BYTE zero = 0;
- DBWrite(ofsBlobPtr,&zero,1);
- }
- //quit
- DBFlush(1);
- LeaveCriticalSection(&csDbAccess);
- //notify
- NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dbcgs)
-{
- DBContact *dbc;
- DWORD ofsModuleName,ofsSettingsGroup,ofsBlobPtr;
- PBYTE pBlob;
- int settingNameLen,moduleNameLen,bytesRemaining;
- char* szCachedSettingName;
- HANDLE saveContact = hContact;
-
- if ((!dbcgs->szModule) || (!dbcgs->szSetting))
- return 1;
- // the db format can't tolerate more than 255 bytes of space (incl. null) for settings+module name
- settingNameLen = (int)strlen(dbcgs->szSetting);
- moduleNameLen = (int)strlen(dbcgs->szModule);
- if ( settingNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("DeleteContactSetting() got a > 255 setting name length. \n");
- #endif
- return 1;
- }
- if ( moduleNameLen > 0xFE )
- {
- #ifdef _DEBUG
- OutputDebugStringA("DeleteContactSetting() got a > 255 module name length. \n");
- #endif
- return 1;
- }
-
- EnterCriticalSection(&csDbAccess);
- ofsModuleName = GetModuleNameOfs(dbcgs->szModule);
- if (hContact == 0)
- hContact = (HANDLE)dbHeader.ofsUser;
-
- dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- //make sure the module group exists
- ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
- if (ofsSettingsGroup == 0) {
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- //find if the setting exists
- ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- while(pBlob[0]) {
- NeedBytes(settingNameLen+1);
- if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen))
- break;
- NeedBytes(1);
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- if (!pBlob[0]) { //setting didn't exist
- LeaveCriticalSection(&csDbAccess);
- return 1;
- }
- { //bin it
- int nameLen,valLen;
- DWORD ofsSettingToCut;
- MoveAlong(1+settingNameLen);
- NeedBytes(3);
- nameLen = 1+settingNameLen;
- valLen = 1+GetSettingValueLength(pBlob);
- ofsSettingToCut = ofsBlobPtr-nameLen;
- MoveAlong(valLen);
- NeedBytes(1);
- while(pBlob[0]) {
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- DBMoveChunk(ofsSettingToCut,ofsSettingToCut+nameLen+valLen,ofsBlobPtr+1-ofsSettingToCut);
- }
-
- szCachedSettingName = GetCachedSetting(dbcgs->szModule,dbcgs->szSetting,moduleNameLen,settingNameLen);
- GetCachedValuePtr(saveContact, szCachedSettingName, -1);
-
- //quit
- DBFlush(1);
- LeaveCriticalSection(&csDbAccess);
- { //notify
- DBCONTACTWRITESETTING dbcws = {0};
- dbcws.szModule = dbcgs->szModule;
- dbcws.szSetting = dbcgs->szSetting;
- dbcws.value.type = DBVT_DELETED;
- NotifyEventHooks(hSettingChangeEvent, (WPARAM)saveContact, (LPARAM)&dbcws);
- }
- return 0;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUMSETTINGS* dbces)
-{
- DBContact *dbc;
- DWORD ofsModuleName,ofsContact,ofsBlobPtr;
- int bytesRemaining, result;
- PBYTE pBlob;
- char szSetting[256];
-
- if (!dbces->szModule)
- return -1;
-
- EnterCriticalSection(&csDbAccess);
-
- ofsModuleName = GetModuleNameOfs(dbces->szModule);
- if (hContact == 0) ofsContact = dbHeader.ofsUser;
- else ofsContact = (DWORD)hContact;
- dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc->signature!=DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&csDbAccess);
- return -1;
- }
- dbces->ofsSettings = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
- if (!dbces->ofsSettings) {
- LeaveCriticalSection(&csDbAccess);
- return -1;
- }
- ofsBlobPtr = dbces->ofsSettings+offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- if (pBlob[0] == 0) {
- LeaveCriticalSection(&csDbAccess);
- return -1;
- }
- result = 0;
- while(pBlob[0]) {
- NeedBytes(1);
- NeedBytes(1+pBlob[0]);
- CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0;
- result = (dbces->pfnEnumProc)(szSetting,dbces->lParam);
- MoveAlong(1+pBlob[0]);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- LeaveCriticalSection(&csDbAccess);
- return result;
-}
-
-STDMETHODIMP_(BOOL) CDdxMmap::EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam)
-{
- int i;
- int ret;
- for (i = 0; i < lResidentSettings.realCount; i++) {
- ret = pFunc((char*)lResidentSettings.items[i], 0, (LPARAM)pParam);
- if (ret) return ret;
- }
- return 0;
-}
-
-extern Cryptor* CryptoEngine;
-extern void* key;
-
-void CDdxMmap::EncodeContactSettings(HANDLE hContact)
-{
- DBContact * contact;
- struct DBContactSettings * setting;
- DWORD offset;
-
- if (!hContact) hContact = (HANDLE)dbHeader.ofsUser;
- contact = (DBContact *)DBRead((DWORD)hContact, sizeof(DBContact), NULL);
- if (contact -> ofsFirstSettings){
- setting = (struct DBContactSettings *)DBRead(contact -> ofsFirstSettings, sizeof(struct DBContactSettings), NULL);
- offset = contact -> ofsFirstSettings;
- do{
- DWORD ofsBlobPtr;
- PBYTE pBlob;
- int bytesRemaining;
- DWORD len;
- /*struct DBModuleName * name;
- char namestr[100];
- name = DBRead(setting->ofsModuleName, sizeof(struct DBModuleName), &bytesRemaining);
- memcpy(namestr, name->name, name->cbName);
- namestr[name->cbName] = '\0';
- MessageBox(0, namestr, "Module", MB_OK);*/
- ofsBlobPtr = offset + offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- while(pBlob[0]) {
- NeedBytes(1);
- NeedBytes(1+pBlob[0]);
- //CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0;
- MoveAlong(1+pBlob[0]);
-
- NeedBytes(5);
-
- switch(pBlob[0]) {
- case DBVT_DELETED: break;
- case DBVT_BYTE: break;
- case DBVT_WORD:
- {
- CryptoEngine->EncryptMem(pBlob+1, 2, key);
- break;
- }
- case DBVT_DWORD:
- {
- CryptoEngine->EncryptMem(pBlob+1, 4, key);
- break;
- }
- case DBVT_UTF8:
- case DBVT_ASCIIZ:
- case DBVT_BLOB:
- {
- NeedBytes(3+*(PWORD)(pBlob+1));
- len = *(PWORD)(pBlob+1);
-
- CryptoEngine->EncryptMem(pBlob+3, len, key);
- break;
- }
- }
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
-
- if (setting -> ofsNext){
- offset = setting -> ofsNext;
- setting = (struct DBContactSettings *)DBRead(setting -> ofsNext, sizeof(struct DBContactSettings), NULL);
- }
- else
- break;
- }while(1);
- }
-}
-
-void CDdxMmap::DecodeContactSettings(HANDLE hContact)
-{
- DBContact * contact;
- struct DBContactSettings * setting;
- DWORD offset;
-
-
- if (!hContact) hContact = (HANDLE)dbHeader.ofsUser;
- contact = (DBContact *)DBRead((DWORD)hContact, sizeof(DBContact), NULL);
- if (contact -> ofsFirstSettings){
- setting = (struct DBContactSettings *)DBRead(contact -> ofsFirstSettings, sizeof(struct DBContactSettings), NULL);
- offset = contact -> ofsFirstSettings;
- do{
- DWORD ofsBlobPtr;
- PBYTE pBlob;
- int bytesRemaining;
- DWORD len;
- ofsBlobPtr = offset + offsetof(struct DBContactSettings,blob);
- pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- while(pBlob[0]) {
- NeedBytes(1);
- NeedBytes(1+pBlob[0]);
- //CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0;
- MoveAlong(1+pBlob[0]);
-
- NeedBytes(5);
-
- switch(pBlob[0]) {
- case DBVT_DELETED: break;
- case DBVT_BYTE: break;
- case DBVT_WORD:
- {
- CryptoEngine->DecryptMem(pBlob+1, 2, key);
- break;
- }
- case DBVT_DWORD:
- {
- CryptoEngine->DecryptMem(pBlob+1, 4, key);
- break;
- }
- case DBVT_UTF8:
- case DBVT_ASCIIZ:
- case DBVT_BLOB:
- {
- NeedBytes(3+*(PWORD)(pBlob+1));
- len = *(PWORD)(pBlob+1);
-
- CryptoEngine->DecryptMem(pBlob+3, len, key);
- break;
- }
- }
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
-
- if (setting -> ofsNext){
- offset = setting -> ofsNext;
- setting = (struct DBContactSettings *)DBRead(setting -> ofsNext, sizeof(struct DBContactSettings), NULL);
- }
- else
- break;
- }while(1);
- }
-
-}
-/////////////////////////////////////////////////////////////////////////////////////////
-//
-// Module initialization procedure
-
-static int stringCompare( DBCachedSettingName* p1, DBCachedSettingName* p2 )
-{
- return strcmp( p1->name, p2->name );
-}
-
-static int stringCompare2( char* p1, char* p2 )
-{
- return strcmp( p1, p2);
-}
-
-int InitSettings(void)
-{
- hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED);
-
- hCacheHeap = HeapCreate(0, 0, 0);
- lSettings.sortFunc = (FSortFunc)stringCompare;
- lSettings.increment = 100;
- lContacts.sortFunc = HandleKeySort;
- lContacts.increment = 50;
- lGlobalSettings.sortFunc = HandleKeySort;
- lGlobalSettings.increment = 50;
- lResidentSettings.sortFunc = (FSortFunc)stringCompare2;
- lResidentSettings.increment = 50;
-
- mirCp = CallService( MS_LANGPACK_GETCODEPAGE, 0, 0 );
- return 0;
-}
-
-void UninitSettings(void)
-{
- HeapDestroy(hCacheHeap);
- List_Destroy(&lContacts);
- List_Destroy(&lSettings);
- List_Destroy(&lGlobalSettings);
- List_Destroy(&lResidentSettings);
-}
diff --git a/plugins/Dbx_mmap_SA/dialogs.cpp b/plugins/Dbx_mmap_SA/dialogs.cpp index d690f477e0..8445b69ef7 100644 --- a/plugins/Dbx_mmap_SA/dialogs.cpp +++ b/plugins/Dbx_mmap_SA/dialogs.cpp @@ -6,8 +6,9 @@ #define MS_DB_CHANGEPASSWORD "DB/ChangePassword"
-extern char encryptKey[255];
-extern size_t encryptKeyLength;
+extern LIST<CryptoModule> arCryptors;
+
+CDdxMmapSA* g_Db;
HANDLE hSetPwdMenu;
INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
@@ -42,7 +43,6 @@ BOOL IsDlgItemEnabled(HWND hwndDlg, int iIDCtrl) return IsWindowEnabled(hwndCtrl);
}
-
static int OptionsInit(WPARAM wParam, LPARAM lParam)
{
OPTIONSDIALOGPAGE odp = { 0 };
@@ -55,18 +55,15 @@ static int OptionsInit(WPARAM wParam, LPARAM lParam) odp.ptszGroup = LPGENT("Services");
odp.pfnDlgProc = DlgProcOptions;
Options_AddPage(wParam, &odp);
-
return 0;
}
INT_PTR ChangePassword(WPARAM wParam, LPARAM lParam)
{
- if (g_Db) {
- if (bEncoding)
- g_Db->ChangePwd();
- else
- g_Db->EncryptDB();
- }
+ if (g_Db->m_bEncoding)
+ g_Db->ChangePwd();
+ else
+ g_Db->EncryptDB();
return 0;
}
@@ -86,18 +83,14 @@ void xModifyMenu(HANDLE hMenu,long flags,const TCHAR* name, HICON hIcon) CallService(MS_CLIST_MODIFYMENUITEM,(WPARAM)hMenu,(LPARAM)&menu);
}
-static int ModulesLoad(WPARAM wParam, LPARAM lParam)
+int InitMenus(WPARAM, LPARAM)
{
- CLISTMENUITEM menu = {0};
- SKINICONDESC sid = {0};
- TCHAR szFile[MAX_PATH];
- //HANDLE hFirst;
-
HookEvent(ME_OPT_INITIALISE, OptionsInit);
- // icolib init
+ TCHAR szFile[MAX_PATH];
GetModuleFileName(g_hInst, szFile, MAX_PATH);
+ SKINICONDESC sid = {0};
sid.cbSize = sizeof(sid);
sid.ptszDefaultFile = szFile;
sid.flags = SIDF_ALL_TCHAR;
@@ -110,53 +103,28 @@ static int ModulesLoad(WPARAM wParam, LPARAM lParam) sid.ptszDescription = LPGENT("Change Password");
sid.pszName = "password";
sid.iDefaultIndex = -IDI_ICON3;
- Skin_AddIcon(&sid);
-
- menu.cbSize = sizeof(menu);
- menu.flags = CMIM_ALL | CMIF_TCHAR;
-
- menu.hIcon = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"database");
+ HANDLE hIcon = Skin_AddIcon(&sid);
// main menu item
- menu.ptszName = (bEncoding) ? LPGENT("Change password") : LPGENT("Set password");
+ CLISTMENUITEM menu = {0};
+ menu.cbSize = sizeof(menu);
+ menu.flags = CMIM_ALL | CMIF_TCHAR | CMIF_ICONFROMICOLIB;
+ menu.icolibItem = hIcon;
+ menu.ptszName = (g_Db->m_bEncoding) ? LPGENT("Change password") : LPGENT("Set password");
menu.ptszPopupName = LPGENT("Database");
menu.pszService = MS_DB_CHANGEPASSWORD;
menu.position = 500100000;
-
hSetPwdMenu = Menu_AddMainMenuItem(&menu);
-
- ZeroMemory(&menu,sizeof(menu));
- menu.cbSize = sizeof(menu);
- menu.flags = CMIM_ICON;
- menu.hIcon = (HICON)CallService(MS_SKIN2_GETICON, 0, (LPARAM)"password");
- CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hSetPwdMenu, (LPARAM)&menu);
-
- return 0;
-}
-
-int UnloadOptions()
-{
- OleUninitialize();
return 0;
}
int InitDialogs()
{
- OleInitialize(0);
+ HookEvent(ME_SYSTEM_MODULESLOADED, InitMenus);
CreateServiceFunction(MS_DB_CHANGEPASSWORD, ChangePassword);
- HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoad);
return 0;
}
-extern Cryptor* CryptoEngine;
-
-extern int ModulesCount;
-extern CryptoModule* Modules[100];
-
-//ugly, i know
-#undef LVM_SETITEMTEXT
-#define LVM_SETITEMTEXT LVM_SETITEMTEXTA
-
int ImageList_AddIcon_IconLibLoaded(HIMAGELIST hIml, char* name)
{
HICON hIcon = (HICON)CallService(MS_SKIN2_GETICON, (WPARAM)NULL, (LPARAM)name);
@@ -207,8 +175,8 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP uid = DBGetContactSettingWord(NULL, "SecureMMAP", "CryptoModule", 0);
- for (i = 0; i < ModulesCount; i++) {
- char buf[100];
+ for (i = 0; i < arCryptors.getCount(); i++) {
+ TCHAR buf[100];
item.mask = LVIF_TEXT;
item.iItem = i;
@@ -216,18 +184,19 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP item.pszText = NULL;
iRow = ListView_InsertItem(hwndList, &item);
- ListView_SetItemText(hwndList, iRow, 1, (LPWSTR)Modules[i]->dllname);
- ListView_SetItemText(hwndList, iRow, 2, (LPWSTR)Modules[i]->cryptor->Name);
- mir_snprintf(buf,SIZEOF(buf),"%d.%d.%d.%d", HIBYTE(HIWORD(Modules[i]->cryptor->Version)), LOBYTE(HIWORD(Modules[i]->cryptor->Version)), HIBYTE(LOWORD(Modules[i]->cryptor->Version)), LOBYTE(LOWORD(Modules[i]->cryptor->Version)));
- ListView_SetItemText(hwndList, iRow, 3, (LPWSTR)buf);
+ ListView_SetItemText(hwndList, iRow, 1, arCryptors[i]->dllname);
+ _tcsncpy(buf, _A2T(arCryptors[i]->cryptor->Name), SIZEOF(buf));
+ ListView_SetItemText(hwndList, iRow, 2, buf);
+ mir_sntprintf(buf,SIZEOF(buf),_T("%d.%d.%d.%d"), HIBYTE(HIWORD(arCryptors[i]->cryptor->Version)), LOBYTE(HIWORD(arCryptors[i]->cryptor->Version)), HIBYTE(LOWORD(arCryptors[i]->cryptor->Version)), LOBYTE(LOWORD(arCryptors[i]->cryptor->Version)));
+ ListView_SetItemText(hwndList, iRow, 3, buf);
- if (uid == Modules[i]->cryptor->uid && bEncoding)
+ if (uid == arCryptors[i]->cryptor->uid && g_Db->m_bEncoding)
ListView_SetCheckState(hwndList, i, 1);
item.mask = LVIF_IMAGE;
item.iItem = iRow;
item.iSubItem = 0;
- item.iImage = ( CryptoEngine == Modules[i]->cryptor && bEncoding ) ? 0 : 1;
+ item.iImage = ( CryptoEngine == arCryptors[i]->cryptor && g_Db->m_bEncoding ) ? 0 : 1;
ListView_SetItem( hwndList, &item );
}
@@ -257,7 +226,7 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP if ( hdr && hdr->hdr.code == LVN_ITEMCHANGED && IsWindowVisible(hdr->hdr.hwndFrom) && hdr->iItem != (-1)) {
iIndex = hdr->iItem;
if (hdr->uNewState & 0x2000){
- for (i = 0; i < ModulesCount; i++) {
+ for (i = 0; i < arCryptors.getCount(); i++) {
if (i != iIndex) ListView_SetCheckState(hwndList, i, 0);
}
SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
@@ -268,16 +237,16 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP break;
}
if (hdr->uNewState & LVIS_SELECTED){
- SetDlgItemTextA(hwndDlg, IDC_AUTHOR, Modules[iIndex]->cryptor->Author);
+ SetDlgItemTextA(hwndDlg, IDC_AUTHOR, arCryptors[iIndex]->cryptor->Author);
{
- TCHAR* info_t = mir_a2t((char*)(Modules[iIndex]->cryptor->Info));
+ TCHAR* info_t = mir_a2t((char*)(arCryptors[iIndex]->cryptor->Info));
SetDlgItemText(hwndDlg, IDC_INFO, TranslateTS(info_t));
mir_free(info_t);
}
- SetDlgItemTextA(hwndDlg, IDC_SITE, Modules[iIndex]->cryptor->Site);
- SetDlgItemTextA(hwndDlg, IDC_EMAIL, Modules[iIndex]->cryptor->Email);
- SetDlgItemTextA(hwndDlg, IDC_ENC, Modules[iIndex]->cryptor->Name);
- SetDlgItemInt(hwndDlg, IDC_UID, Modules[iIndex]->cryptor->uid, 0);
+ SetDlgItemTextA(hwndDlg, IDC_SITE, arCryptors[iIndex]->cryptor->Site);
+ SetDlgItemTextA(hwndDlg, IDC_EMAIL, arCryptors[iIndex]->cryptor->Email);
+ SetDlgItemTextA(hwndDlg, IDC_ENC, arCryptors[iIndex]->cryptor->Name);
+ SetDlgItemInt(hwndDlg, IDC_UID, arCryptors[iIndex]->cryptor->uid, 0);
} else {
SetDlgItemTextA(hwndDlg, IDC_AUTHOR, "");
SetDlgItemTextA(hwndDlg, IDC_INFO, "");
@@ -291,7 +260,7 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP }
if (((LPNMHDR)lParam)->code == PSN_APPLY ) {
int alg = -1;
- for (i = 0; i < ModulesCount; i++) {
+ for (i = 0; i < arCryptors.getCount(); i++) {
if (ListView_GetCheckState(hwndList, i)) {
alg = i;
break;
@@ -299,30 +268,30 @@ INT_PTR CALLBACK DlgProcOptions(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lP }
if (alg > -1){
- if (!bEncoding){
- DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", Modules[alg]->cryptor->uid);
+ if (!g_Db->m_bEncoding){
+ DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", arCryptors[alg]->cryptor->uid);
g_Db->EncryptDB();
}
else {
- if (Modules[alg]->cryptor->uid != DBGetContactSettingWord(NULL, "SecureMMAP", "CryptoModule", -1)) {
- DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", Modules[alg]->cryptor->uid);
+ if (arCryptors[alg]->cryptor->uid != DBGetContactSettingWord(NULL, "SecureMMAP", "CryptoModule", -1)) {
+ DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", arCryptors[alg]->cryptor->uid);
g_Db->RecryptDB();
}
}
}
- else if (bEncoding)
+ else if (g_Db->m_bEncoding)
g_Db->DecryptDB();
uid = DBGetContactSettingWord(NULL, "SecureMMAP", "CryptoModule", 0);
- for (i = 0; i < ModulesCount; i++) {
- if (uid == Modules[i]->cryptor->uid && bEncoding)
+ for (i = 0; i < arCryptors.getCount(); i++) {
+ if (uid == arCryptors[i]->cryptor->uid && g_Db->m_bEncoding)
ListView_SetCheckState(hwndList, i, 1);
item.mask = LVIF_IMAGE;
item.iItem = i;
item.iSubItem = 0;
- item.iImage = ( CryptoEngine == Modules[i]->cryptor && bEncoding ) ? 0 : 1;
+ item.iImage = ( CryptoEngine == arCryptors[i]->cryptor && g_Db->m_bEncoding ) ? 0 : 1;
ListView_SetItem( hwndList, &item );
}
@@ -396,7 +365,7 @@ BOOL CALLBACK DlgStdInProc(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam) switch( LOWORD(wParam)) {
case IDOK:
if (!GetWindowLongPtr(hDlg,GWLP_USERDATA)) {
- encryptKeyLength = GetDlgItemTextA(hDlg, IDC_USERPASS, encryptKey, 254);
+ g_Db->encryptKeyLength = GetDlgItemTextA(hDlg, IDC_USERPASS, g_Db->encryptKey, 254);
EndDialog(hDlg,IDOK);
}
break;
@@ -459,8 +428,8 @@ BOOL CALLBACK DlgStdNewPass(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam) else {
GetDlgItemTextA(hDlg, IDC_USERPASS2, pass2, 254);
if (!strcmp(pass1, pass2)) {
- encryptKeyLength = strlen(pass1);
- strcpy(encryptKey, pass1);
+ g_Db->encryptKeyLength = strlen(pass1);
+ strcpy(g_Db->encryptKey, pass1);
EndDialog(hDlg,IDOK);
}
else {
@@ -525,7 +494,7 @@ BOOL CALLBACK DlgChangePass(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam) if (uid == IDOK) {
char pass1[255], pass2[255], oldpass[255];
GetDlgItemTextA(hDlg, IDC_OLDPASS, oldpass, 254);
- if (strcmp(oldpass, encryptKey)) {
+ if (strcmp(oldpass, g_Db->encryptKey)) {
SetWindowText(GetDlgItem(hDlg, IDC_HEADERBAR), TranslateT("Wrong password!"));
SendMessage(GetDlgItem(hDlg, IDC_HEADERBAR), WM_NCPAINT, 0, 0);
break;
@@ -552,7 +521,7 @@ BOOL CALLBACK DlgChangePass(HWND hDlg, UINT uMsg,WPARAM wParam,LPARAM lParam) else if (uid == IDREMOVE) {
char oldpass[255];
GetDlgItemTextA(hDlg, IDC_OLDPASS, oldpass, 254);
- if (strcmp(oldpass, encryptKey)) {
+ if (strcmp(oldpass, g_Db->encryptKey)) {
SetWindowText(GetDlgItem(hDlg, IDC_HEADERBAR), TranslateT("Wrong password!"));
SendMessage(GetDlgItem(hDlg, IDC_HEADERBAR), WM_NCPAINT, 0, 0);
break;
diff --git a/plugins/Dbx_mmap_SA/init.cpp b/plugins/Dbx_mmap_SA/init.cpp index cf06474dde..2566e14471 100644 --- a/plugins/Dbx_mmap_SA/init.cpp +++ b/plugins/Dbx_mmap_SA/init.cpp @@ -23,21 +23,20 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "commonheaders.h"
-int hLangpack;
+void InitSecurity(void);
+void UnloadSecurity(void);
-extern TCHAR szDbPath[MAX_PATH];
+int hLangpack;
HINSTANCE g_hInst = NULL;
-CDdxMmap* g_Db = NULL;
-
PLUGININFOEX pluginInfo = {
sizeof(PLUGININFOEX),
__PLUGIN_NAME,
__VERSION_DWORD,
"Provides Miranda database support: global settings, contacts, history, settings per contact. Enhanced modification with Encryption support.",
"Miranda-IM project, modification by FYR and chaos.persei, nullbie, Billy_Bons",
- "chaos.persei@gmail.com; ashpynov@gmail.com; bio@msx.ru; ghazan@miranda-im.org",
+ "chaos.persei@gmail.com; ashpynov@gmail.com; bio@msx.ru; ghazan@miranda.im",
"Copyright 2000-2011 Miranda IM project, FYR, chaos.persei, induction, nullbie",
"http://dbmmapmod.googlecode.com/",
UNICODE_AWARE,
@@ -45,132 +44,98 @@ PLUGININFOEX pluginInfo = { { 0x28ff9b91, 0x3e4d, 0x4f1c, { 0xb4, 0x7c, 0xc6, 0x41, 0xb0, 0x37, 0xff, 0x40 } }
};
+LIST<CDdxMmapSA> g_Dbs(1, (LIST<CDdxMmapSA>::FTSortFunc)HandleKeySort);
+
/////////////////////////////////////////////////////////////////////////////////////////
// returns 0 if the profile is created, EMKPRF*
static int makeDatabase(const TCHAR *profile, int *error)
{
- HANDLE hFile = CreateFile(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
- if ( hFile != INVALID_HANDLE_VALUE ) {
- CreateDbHeaders(hFile);
- CloseHandle(hFile);
+ CDdxMmapSA *tmp = new CDdxMmapSA(profile);
+ if (tmp->Create() == ERROR_SUCCESS) {
+ tmp->CreateDbHeaders();
+ delete tmp;
return 0;
}
- if ( error != NULL ) *error = EMKPRF_CREATEFAILED;
+ delete tmp;
+ if (error != NULL) *error = EMKPRF_CREATEFAILED;
return 1;
}
// returns 0 if the given profile has a valid header
-static int grokHeader(const TCHAR *profile, int *error )
+static int grokHeader(const TCHAR *profile, int *error)
{
- int rc = 1;
- int chk = 0;
- struct DBHeader hdr;
- HANDLE hFile = INVALID_HANDLE_VALUE;
- DWORD dummy = 0;
-
- hFile = CreateFile(profile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
- if ( hFile == INVALID_HANDLE_VALUE ) {
- if ( error != NULL ) *error = EGROKPRF_CANTREAD;
+ CDdxMmapSA *tmp = new CDdxMmapSA(profile);
+ if (tmp->Load(true) != ERROR_SUCCESS) {
+ delete tmp;
+ if (error != NULL) *error = EGROKPRF_CANTREAD;
return 1;
}
- // read the header, which can fail (for various reasons)
- if ( !ReadFile(hFile, &hdr, sizeof(struct DBHeader), &dummy, NULL)) {
- if ( error != NULL) *error = EGROKPRF_CANTREAD;
- CloseHandle(hFile);
- return 1;
- }
- chk = CheckDbHeaders(&hdr);
+
+ int chk = tmp->CheckDbHeaders();
+ delete tmp;
if ( chk == 0 ) {
// all the internal tests passed, hurrah
- rc = 0;
- if ( error != NULL ) *error = 0;
- } else {
- // didn't pass at all, or some did.
- switch ( chk ) {
- case 1:
- {
- // "Miranda ICQ DB" wasn't present
- if ( error != NULL ) *error = EGROKPRF_UNKHEADER;
- break;
- }
- case 2:
- {
- // header was present, but version information newer
- if ( error != NULL ) *error = EGROKPRF_VERNEWER;
- break;
- }
- case 3:
- {
- // header/version OK, internal data missing
- if ( error != NULL ) *error = EGROKPRF_DAMAGED;
- break;
- }
- } // switch
- } //if
- CloseHandle(hFile);
- return rc;
+ if (error != NULL) *error = 0;
+ return 0;
+ }
+
+ // didn't pass at all, or some did.
+ switch ( chk ) {
+ case 1:
+ // "Miranda ICQ DB" wasn't present
+ if (error != NULL) *error = EGROKPRF_UNKHEADER;
+ break;
+
+ case 2:
+ // header was present, but version information newer
+ if (error != NULL) *error = EGROKPRF_VERNEWER;
+ break;
+
+ case 3:
+ // header/version OK, internal data missing
+ if (error != NULL) *error = EGROKPRF_DAMAGED;
+ break;
+ }
+
+ return 1;
}
// returns 0 if all the APIs are injected otherwise, 1
static MIDatabase* LoadDatabase(const TCHAR *profile)
{
- if (g_Db) delete g_Db;
- g_Db = new CDdxMmap(profile);
-
- // don't need thread notifications
- _tcsncpy(szDbPath, profile, SIZEOF(szDbPath));
-
// set the memory, lists & UTF8 manager
mir_getLP( &pluginInfo );
- { // Are we running under unicode Miranda core ?
- char szVer[MAX_PATH];
- CallService(MS_SYSTEM_GETVERSIONTEXT, MAX_PATH, (LPARAM)szVer);
- _strlwr(szVer); // make sure it is lowercase
-
- /* WARNING!!! You CANNOT remove this code */
- if (strstr(szVer, "coffee") != NULL)
- {
- // We are running under damn violators
- void (*f)();
-
- MessageBox(0, TranslateT("Running mmap_sa is forbidden under license violating products, sorry"), TranslateT("Warning!"), MB_OK);
-
- f = NULL;
- f();
- }
- /* end of protected code */
+ CDdxMmapSA* db = new CDdxMmapSA(profile);
+ if (db->Load(false) != ERROR_SUCCESS) {
+ delete db;
+ return NULL;
}
- // inject all APIs and hooks into the core
- LoadDatabaseModule();
-
- return g_Db;
+ g_Dbs.insert(db);
+ return db;
}
static int UnloadDatabase(MIDatabase* db)
{
- UnloadDatabaseModule();
- delete g_Db; g_Db = 0;
+ g_Dbs.remove((CDdxMmapSA*)db);
+ delete (CDdxMmapSA*)db;
return 0;
}
-static DATABASELINK dblink = {
+static DATABASELINK dblink =
+{
sizeof(DATABASELINK),
- "db3x secured_mmap driver",
- _T("db3x mmap database support"),
+ "db3x secure mmap driver",
+ _T("db3x secure mmap database support"),
makeDatabase,
grokHeader,
LoadDatabase,
- UnloadDatabase,
+ UnloadDatabase
};
-BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved)
-{
- g_hInst = hInstDLL;
- return TRUE;
-}
+/////////////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) PLUGININFOEX * MirandaPluginInfoEx(DWORD mirandaVersion)
{
@@ -181,10 +146,23 @@ extern "C" __declspec(dllexport) const MUUID MirandaInterfaces[] = {MIID_DATABAS extern "C" __declspec(dllexport) int Load(void)
{
+ InitSecurity();
+ InitPreset();
+
+ RegisterDatabasePlugin(&dblink);
return 0;
}
extern "C" __declspec(dllexport) int Unload(void)
{
+ g_Dbs.destroy();
+ UnloadSecurity();
+ UninitPreset();
return 0;
-}
\ No newline at end of file +}
+
+BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID reserved)
+{
+ g_hInst = hInstDLL;
+ return TRUE;
+}
diff --git a/plugins/Dbx_mmap_SA/security.cpp b/plugins/Dbx_mmap_SA/security.cpp index 50b23f4312..4ecf5f745b 100644 --- a/plugins/Dbx_mmap_SA/security.cpp +++ b/plugins/Dbx_mmap_SA/security.cpp @@ -2,20 +2,12 @@ #include "commonheaders.h"
-BOOL bEncoding;
-BOOL bEncProcess = 0;
-
-char encryptKey[255];
-size_t encryptKeyLength;
-
int wrongPass = 0;
void* key;
Cryptor* CryptoEngine = NULL;
-int ModulesCount = 0;
-CryptoModule* Modules[100];
-
+LIST<CryptoModule> arCryptors(1);
void zero_fill(BYTE * pBuf, size_t bufSize)
{
@@ -26,83 +18,76 @@ void zero_fill(BYTE * pBuf, size_t bufSize) void InitSecurity()
{
- HMODULE hLib;
- WIN32_FIND_DATAA fd;
-
Cryptor* (__stdcall *GetCryptor)();
- HANDLE hFile = FindFirstFileA(".\\plugins\\cryptors\\*.dll", &fd);
-
- ModulesCount = 0;
- while (hFile != INVALID_HANDLE_VALUE)
- {
- char tmp[255];
- strcpy(tmp, ".\\plugins\\cryptors\\");
- strcat(tmp, fd.cFileName);
-
- hLib = LoadLibraryA(tmp);
+ TCHAR tszPath[MAX_PATH];
+ GetModuleFileName(g_hInst, tszPath, SIZEOF(tszPath));
+ TCHAR *p = _tcsrchr(tszPath, '\\')+1; _tcscpy(p, _T("cryptors\\*.dll"));
+
+ WIN32_FIND_DATA fd;
+ HANDLE hFile = FindFirstFile(tszPath, &fd);
+ while (hFile != INVALID_HANDLE_VALUE) {
+ mir_sntprintf(p, MAX_PATH - (p-tszPath), _T("cryptors\\%s"), fd.cFileName);
+ HMODULE hLib = LoadLibrary(tszPath);
if (hLib){
GetCryptor = (Cryptor* (__stdcall *)()) GetProcAddress(hLib, "GetCryptor");
if (GetCryptor){
- Modules[ModulesCount] = (CryptoModule*) malloc(sizeof(CryptoModule));
- Modules[ModulesCount]->cryptor = GetCryptor();
- strcpy(Modules[ModulesCount]->dllname, fd.cFileName);
- Modules[ModulesCount]->hLib = hLib;
-
- ModulesCount++;
- }else{
- FreeLibrary(hLib);
+ CryptoModule* newItem = (CryptoModule*) malloc(sizeof(CryptoModule));
+ newItem->cryptor = GetCryptor();
+ _tcsncpy(newItem->dllname, fd.cFileName, MAX_PATH);
+ newItem->hLib = hLib;
+ arCryptors.insert(newItem);
}
+ else FreeLibrary(hLib);
}
- if (ModulesCount >= 100) break;
- if (!FindNextFileA(hFile, &fd)) break;
+ if (!FindNextFile(hFile, &fd))
+ break;
}
}
void UnloadSecurity()
{
- int i;
-
- if (CryptoEngine) CryptoEngine->FreeKey(key);
+ if (CryptoEngine)
+ CryptoEngine->FreeKey(key);
- for (i = 0; i < ModulesCount; i++)
- {
- FreeLibrary(Modules[i]->hLib);
- free(Modules[i]);
+ for (int i = 0; i < arCryptors.getCount(); i++) {
+ FreeLibrary(arCryptors[i]->hLib);
+ free(arCryptors[i]);
}
+ arCryptors.destroy();
}
-void EncoderInit()
+void CDdxMmapSA::EncoderInit()
{
- if (!bEncoding) return;
+ if (!m_bEncoding) return;
encryptKey[encryptKeyLength] = 0;
key = CryptoEngine->GenerateKey(encryptKey);
}
-void EncodeCopyMemory(void * dst, void * src, size_t size )
+void CDdxMmapSA::EncodeCopyMemory(void * dst, void * src, size_t size )
{
memcpy(dst, src, size);
- if (!bEncoding)
+ if (!m_bEncoding)
return;
CryptoEngine->EncryptMem((BYTE *)dst, (int)size, key);
}
-void DecodeCopyMemory(void * dst, void * src, size_t size )
+void CDdxMmapSA::DecodeCopyMemory(void * dst, void * src, size_t size )
{
memcpy(dst, src, size);
- if (!bEncoding)
+ if (!m_bEncoding)
return;
CryptoEngine->DecryptMem((BYTE *)dst, (int)size, key);
}
-void EncodeDBWrite(DWORD ofs, void * src, size_t size)
+void CDdxMmapSA::EncodeDBWrite(DWORD ofs, void *src, int size)
{
- if (bEncoding)
+ if (m_bEncoding)
{
BYTE * buf;
@@ -117,64 +102,55 @@ void EncodeDBWrite(DWORD ofs, void * src, size_t size) }
}
-void DecodeDBWrite(DWORD ofs, void * src, size_t size)
+void CDdxMmapSA::DecodeDBWrite(DWORD ofs, void *src, int size)
{
-
- if (bEncoding)
- {
- BYTE * buf;
-
- buf = (BYTE*)GlobalAlloc(GPTR, sizeof(BYTE)*size);
+ if (m_bEncoding) {
+ BYTE *buf = (BYTE*)GlobalAlloc(GPTR, sizeof(BYTE)*size);
DecodeCopyMemory(buf, src, size);
DBWrite(ofs, buf, (int)size);
GlobalFree(buf);
}
- else
- {
- DBWrite(ofs, src, (int)size);
- }
+ else DBWrite(ofs, src, (int)size);
}
int bCheckingPass = 0;
-int CDdxMmap::CheckPassword(WORD checkWord, TCHAR *szDBName)
+int CDdxMmapSA::CheckPassword(WORD checkWord, TCHAR *szDBName)
{
- WORD ver;
- int res;
+ if (bCheckingPass)
+ return 0;
- if (bCheckingPass) return 0;
bCheckingPass = 1;
- {
- int i;
- int Found = 0;
- for (i = 0; i < ModulesCount; i++) {
- if (dbHeader.cryptorUID == Modules[i]->cryptor->uid){
- CryptoEngine = Modules[i]->cryptor;
- Found = 1;
- break;
- }
- }
- if (!Found){
- MessageBoxA(0, "Sorry, but your database encrypted with unknown module", "Error", MB_OK);
- bCheckingPass = 0;
- return 0;
+ int Found = 0;
+ for (int i = 0; i < arCryptors.getCount(); i++) {
+ if ( HIWORD(m_dbHeader.version) == arCryptors[i]->cryptor->uid){
+ CryptoEngine = arCryptors[i]->cryptor;
+ Found = 1;
+ break;
}
}
+ if (!Found){
+ MessageBoxA(0, "Sorry, but your database encrypted with unknown module", "Error", MB_OK);
+ bCheckingPass = 0;
+ return 0;
+ }
- while(1){
- res = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)DlgStdInProc, (LPARAM)szDBName);
- if (res == IDCANCEL)
- {
+ while(1) {
+ int res = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_LOGIN), NULL, (DLGPROC)DlgStdInProc, (LPARAM)szDBName);
+ if (res == IDCANCEL) {
wrongPass = 0;
bCheckingPass = 0;
return 0;
}
- if (encryptKeyLength < 1) continue;
+ if (encryptKeyLength < 1)
+ continue;
+
EncoderInit();
+
+ WORD ver;
DecodeCopyMemory(&ver, &checkWord, sizeof(checkWord));
- if (ver == 0x5195)
- {
+ if (ver == 0x5195) {
wrongPass = 0;
bCheckingPass = 0;
return 1;
@@ -190,7 +166,7 @@ int SelectEncoder() WORD uid;
int i;
- if (ModulesCount == 0){
+ if (arCryptors.getCount() == 0){
MessageBox(0, TranslateT("Crypto modules not found"), TranslateT("Error"), MB_OK);
return 1;
}
@@ -199,29 +175,29 @@ int SelectEncoder() if (uid == 0){
MessageBox(0, TranslateT("Crypto module hasn't been chosen, using first one found"), TranslateT("Notice"), MB_OK);
- DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", Modules[0]->cryptor->uid);
- CryptoEngine = Modules[0]->cryptor;
+ DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", arCryptors[0]->cryptor->uid);
+ CryptoEngine = arCryptors[0]->cryptor;
}
else{
int Found = 0;
- for (i = 0; i < ModulesCount; i++) {
- if (Modules[i]->cryptor->uid == uid){
- CryptoEngine = Modules[i]->cryptor;
+ for (i = 0; i < arCryptors.getCount(); i++) {
+ if (arCryptors[i]->cryptor->uid == uid){
+ CryptoEngine = arCryptors[i]->cryptor;
Found = 1;
break;
}
}
if (!Found){
MessageBox(0, TranslateT("Crypto module hasn't been chosen, using first one found"), TranslateT("Notice"), MB_OK);
- DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", Modules[0]->cryptor->uid);
- CryptoEngine = Modules[0]->cryptor;
+ DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", arCryptors[0]->cryptor->uid);
+ CryptoEngine = arCryptors[0]->cryptor;
}
}
return 0;
}
-void CDdxMmap::EncodeAll()
+void CDdxMmapSA::EncodeAll()
{
HANDLE hContact;
@@ -238,7 +214,7 @@ void CDdxMmap::EncodeAll() EncodeContactSettings(NULL);
}
-void CDdxMmap::DecodeAll()
+void CDdxMmapSA::DecodeAll()
{
HANDLE hContact;
@@ -253,48 +229,39 @@ void CDdxMmap::DecodeAll() DecodeContactSettings(NULL);
}
-void CDdxMmap::WritePlainHeader()
+void CDdxMmapSA::WritePlainHeader()
{
DWORD bytesWritten;
- memcpy(dbHeader.signature, &dbSignature, sizeof(dbHeader.signature));
- SetFilePointer(hDbFile,0,NULL,FILE_BEGIN);
- WriteFile(hDbFile,dbHeader.signature,sizeof(dbHeader.signature),&bytesWritten,NULL);
- {
- WORD checkWord;
- checkWord = 0x0700;
- memcpy(&dbHeader.checkWord, &checkWord, sizeof(checkWord));
- WriteFile(hDbFile,&dbHeader.checkWord,sizeof(dbHeader.checkWord),&bytesWritten,NULL);
+ memcpy(m_dbHeader.signature, &dbSignature, sizeof(m_dbHeader.signature));
+ SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
+ WriteFile(m_hDbFile,m_dbHeader.signature,sizeof(m_dbHeader.signature),&bytesWritten,NULL);
- dbHeader.cryptorUID = 0x0000; //no encryption
- WriteFile(hDbFile,&dbHeader.cryptorUID,sizeof(dbHeader.cryptorUID),&bytesWritten,NULL);
- }
+ m_dbHeader.version = MAKELONG(0x0700, 0x0000); //no encryption
+ WriteFile(m_hDbFile,&m_dbHeader.version, sizeof(m_dbHeader.version),&bytesWritten,NULL);
}
-void CDdxMmap::WriteCryptHeader()
+void CDdxMmapSA::WriteCryptHeader()
{
DWORD bytesWritten;
- memcpy(dbHeader.signature, &dbSignatureSecured, sizeof(dbHeader.signature));
- SetFilePointer(hDbFile,0,NULL,FILE_BEGIN);
- WriteFile(hDbFile,dbHeader.signature,sizeof(dbHeader.signature),&bytesWritten,NULL);
- {
- WORD checkWord;
- checkWord = 0x5195;
- EncodeCopyMemory(&dbHeader.checkWord, &checkWord, sizeof(checkWord));
- WriteFile(hDbFile,&dbHeader.checkWord,sizeof(dbHeader.checkWord),&bytesWritten,NULL);
+ memcpy(m_dbHeader.signature, &dbSignatureSecured, sizeof(m_dbHeader.signature));
+ SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
+ WriteFile(m_hDbFile,m_dbHeader.signature,sizeof(m_dbHeader.signature),&bytesWritten,NULL);
- dbHeader.cryptorUID = CryptoEngine->uid;
- WriteFile(hDbFile,&dbHeader.cryptorUID,sizeof(dbHeader.cryptorUID),&bytesWritten,NULL);
- }
+ WORD checkWord = 0x5195, cryptWord;
+ EncodeCopyMemory(&cryptWord, &checkWord, sizeof(checkWord));
+ m_dbHeader.version = MAKELONG(cryptWord, CryptoEngine->uid);
+ WriteFile(m_hDbFile,&m_dbHeader.version, sizeof(m_dbHeader.version),&bytesWritten,NULL);
}
-void CDdxMmap::EncryptDB()
+void CDdxMmapSA::EncryptDB()
{
int action = 0;
- if (bEncProcess) return;
+ if (bEncProcess)
+ return;
- if (memcmp(dbHeader.signature, &dbSignatureSecured, sizeof(dbHeader.signature)) == 0){
+ if (memcmp(m_dbHeader.signature, &dbSignatureSecured, sizeof(m_dbHeader.signature)) == 0){
MessageBox(0, TranslateT("DB is already secured!"), TranslateT("Error"), MB_OK);
return;
}
@@ -312,14 +279,14 @@ void CDdxMmap::EncryptDB() return;
}
- EnterCriticalSection(&csDbAccess);
+ EnterCriticalSection(&m_csDbAccess);
- bEncoding = 1;
+ m_bEncoding = 1;
EncoderInit();
EncodeAll();
- LeaveCriticalSection(&csDbAccess);
+ LeaveCriticalSection(&m_csDbAccess);
WriteCryptHeader();
@@ -328,12 +295,12 @@ void CDdxMmap::EncryptDB() bEncProcess = 0;
}
-void CDdxMmap::DecryptDB()
+void CDdxMmapSA::DecryptDB()
{
char oldKey[255];
strcpy(oldKey, encryptKey);
- if ( !CheckPassword(dbHeader.checkWord, TranslateT("current database"))) {
+ if ( !CheckPassword( HIWORD(m_dbHeader.version), TranslateT("current database"))) {
strcpy(encryptKey, oldKey);
encryptKeyLength = strlen(oldKey);
return;
@@ -341,11 +308,11 @@ void CDdxMmap::DecryptDB() WritePlainHeader();
- EnterCriticalSection(&csDbAccess);
+ EnterCriticalSection(&m_csDbAccess);
DecodeAll();
- LeaveCriticalSection(&csDbAccess);
+ LeaveCriticalSection(&m_csDbAccess);
- bEncoding = 0;
+ m_bEncoding = 0;
zero_fill((BYTE *)encryptKey, sizeof encryptKey);
@@ -358,9 +325,9 @@ void CDdxMmap::DecryptDB() CryptoEngine = NULL;
}
-void CDdxMmap::RecryptDB()
+void CDdxMmapSA::RecryptDB()
{
- EnterCriticalSection(&csDbAccess);
+ EnterCriticalSection(&m_csDbAccess);
DecodeAll();
@@ -368,7 +335,7 @@ void CDdxMmap::RecryptDB() SelectEncoder();
- bEncoding = 1;
+ m_bEncoding = 1;
EncoderInit();
@@ -376,10 +343,10 @@ void CDdxMmap::RecryptDB() WriteCryptHeader();
- LeaveCriticalSection(&csDbAccess);
+ LeaveCriticalSection(&m_csDbAccess);
}
-void CDdxMmap::ChangePwd()
+void CDdxMmapSA::ChangePwd()
{
char newpass[255] = {0};
@@ -388,7 +355,7 @@ void CDdxMmap::ChangePwd() if (action == IDCANCEL || (action == IDOK && !strlen(newpass)))
return;
- EnterCriticalSection(&csDbAccess);
+ EnterCriticalSection(&m_csDbAccess);
DecodeAll();
@@ -397,7 +364,7 @@ void CDdxMmap::ChangePwd() if (action == IDREMOVE){
WritePlainHeader();
- bEncoding = 0;
+ m_bEncoding = 0;
CryptoEngine = NULL;
DBWriteContactSettingWord(NULL, "SecureMMAP", "CryptoModule", 0);
@@ -410,7 +377,7 @@ void CDdxMmap::ChangePwd() strcpy(encryptKey, newpass);
encryptKeyLength = strlen(newpass);
- bEncoding = 1;
+ m_bEncoding = 1;
EncoderInit();
@@ -421,5 +388,5 @@ void CDdxMmap::ChangePwd() zero_fill((BYTE *)newpass, sizeof newpass);
- LeaveCriticalSection(&csDbAccess);
+ LeaveCriticalSection(&m_csDbAccess);
}
\ No newline at end of file diff --git a/plugins/Dbx_mmap_SA/version.h b/plugins/Dbx_mmap_SA/version.h index 7a58874270..57928c3905 100644 --- a/plugins/Dbx_mmap_SA/version.h +++ b/plugins/Dbx_mmap_SA/version.h @@ -3,4 +3,4 @@ #define __FILEVERSION_STRING MIRANDA_VERSION_FILEVERSION
#define __VERSION_STRING MIRANDA_VERSION_STRING
#define __VERSION_DWORD MIRANDA_VERSION_DWORD
-#define __PLUGIN_NAME "Miranda SecuredMMAP database driver"
+#define __PLUGIN_NAME "Miranda NG secure database driver"
|