From 4bc88d46fa9859615521f436511d4f102f20eb67 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 18 Jul 2012 14:11:28 +0000 Subject: databases are still static, but are controlled via classes git-svn-id: http://svn.miranda-ng.org/main/trunk@1014 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Db3x/db3x_10.vcxproj | 3 +- plugins/Db3x/db3x_10.vcxproj.filters | 7 +- plugins/Db3x/src/commonheaders.h | 5 +- plugins/Db3x/src/database.cpp | 41 ++-- plugins/Db3x/src/database.h | 15 +- plugins/Db3x/src/dbcache.cpp | 92 ++++---- plugins/Db3x/src/dbcontacts.cpp | 268 +++++++++++------------ plugins/Db3x/src/dbevents.cpp | 392 ++++++++++++++++----------------- plugins/Db3x/src/dbheaders.cpp | 40 ++-- plugins/Db3x/src/dbintf.cpp | 38 ++++ plugins/Db3x/src/dbintf.h | 68 ++++++ plugins/Db3x/src/dbmodulechain.cpp | 71 +++--- plugins/Db3x/src/dbsettings.cpp | 406 +++++++++++++++++------------------ plugins/Db3x/src/encrypt.cpp | 66 ------ plugins/Db3x/src/init.cpp | 49 +++-- 15 files changed, 785 insertions(+), 776 deletions(-) create mode 100644 plugins/Db3x/src/dbintf.cpp create mode 100644 plugins/Db3x/src/dbintf.h delete mode 100644 plugins/Db3x/src/encrypt.cpp (limited to 'plugins/Db3x') diff --git a/plugins/Db3x/db3x_10.vcxproj b/plugins/Db3x/db3x_10.vcxproj index bd702267d1..441e103858 100644 --- a/plugins/Db3x/db3x_10.vcxproj +++ b/plugins/Db3x/db3x_10.vcxproj @@ -204,9 +204,9 @@ + - Create @@ -214,6 +214,7 @@ + diff --git a/plugins/Db3x/db3x_10.vcxproj.filters b/plugins/Db3x/db3x_10.vcxproj.filters index 19e4fdcf15..682ea4edc7 100644 --- a/plugins/Db3x/db3x_10.vcxproj.filters +++ b/plugins/Db3x/db3x_10.vcxproj.filters @@ -36,10 +36,10 @@ Source Files - + Source Files - + Source Files @@ -50,6 +50,9 @@ Header Files + + Header Files + diff --git a/plugins/Db3x/src/commonheaders.h b/plugins/Db3x/src/commonheaders.h index cf4b2d4985..52734be2d0 100644 --- a/plugins/Db3x/src/commonheaders.h +++ b/plugins/Db3x/src/commonheaders.h @@ -43,11 +43,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include #include +#include #include #include "version.h" #include "database.h" +#include "dbintf.h" #include "resource.h" extern HANDLE hDbFile; @@ -55,7 +58,7 @@ extern CRITICAL_SECTION csDbAccess; extern struct DBHeader dbHeader; extern HANDLE hCacheHeap; extern SortedList lContacts; -extern char szDbPath[MAX_PATH]; +extern TCHAR szDbPath[MAX_PATH]; #ifdef __GNUC__ #define mir_i64(x) (x##LL) diff --git a/plugins/Db3x/src/database.cpp b/plugins/Db3x/src/database.cpp index 0172bde5b2..a9054bb591 100644 --- a/plugins/Db3x/src/database.cpp +++ b/plugins/Db3x/src/database.cpp @@ -31,8 +31,6 @@ void UninitSettings(void); int InitContacts(void); void UninitContacts(void); int InitEvents(void); -void UninitEvents(void); -int InitCrypt(void); int InitModuleNames(void); void UninitModuleNames(void); int InitCache(void); @@ -40,10 +38,10 @@ void UninitCache(void); int InitIni(void); void UninitIni(void); -HANDLE hDbFile=INVALID_HANDLE_VALUE; +HANDLE hDbFile = INVALID_HANDLE_VALUE; CRITICAL_SECTION csDbAccess; struct DBHeader dbHeader; -char szDbPath[MAX_PATH]; +TCHAR szDbPath[MAX_PATH]; static void UnloadDatabase(void) { @@ -54,8 +52,8 @@ DWORD CreateNewSpace(int bytes) { DWORD ofsNew; - ofsNew=dbHeader.ofsFileEnd; - dbHeader.ofsFileEnd+=bytes; + ofsNew = dbHeader.ofsFileEnd; + dbHeader.ofsFileEnd += bytes; DBWrite(0,&dbHeader,sizeof(dbHeader)); log2("newspace %d@%08x",bytes,ofsNew); return ofsNew; @@ -65,9 +63,9 @@ void DeleteSpace(DWORD ofs,int bytes) { PBYTE buf; log2("deletespace %d@%08x",bytes,ofs); - dbHeader.slackSpace+=bytes; + dbHeader.slackSpace += bytes; DBWrite(0,&dbHeader,sizeof(dbHeader)); - buf=(PBYTE)mir_alloc(bytes); + buf = (PBYTE)mir_alloc(bytes); memset(buf,0,bytes); DBWrite(ofs,buf,bytes); mir_free(buf); @@ -75,8 +73,6 @@ void DeleteSpace(DWORD ofs,int bytes) void UnloadDatabaseModule(void) { - //UninitIni(); - UninitEvents(); UninitSettings(); UninitContacts(); UninitModuleNames(); @@ -90,8 +86,8 @@ int LoadDatabaseModule(void) InitializeCriticalSection(&csDbAccess); log0("DB logging running"); { - DWORD dummy=0; - hDbFile=CreateFileA(szDbPath,GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL); + 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; } @@ -100,17 +96,16 @@ int LoadDatabaseModule(void) return 1; } } - //if(ParseCommandLine()) return 1; - if(InitCache()) return 1; - if(InitModuleNames()) return 1; - if(InitContacts()) return 1; - if(InitSettings()) return 1; - if(InitEvents()) return 1; - if(InitCrypt()) return 1; + //if (ParseCommandLine()) return 1; + if (InitCache()) return 1; + if (InitModuleNames()) return 1; + if (InitContacts()) return 1; + if (InitSettings()) return 1; + if (InitEvents()) return 1; return 0; } -static DWORD DatabaseCorrupted=0; +static DWORD DatabaseCorrupted = 0; void __cdecl dbpanic(void *arg) { @@ -121,10 +116,10 @@ void __cdecl dbpanic(void *arg) void DatabaseCorruption(void) { - int kill=0; + int kill = 0; EnterCriticalSection(&csDbAccess); - if (DatabaseCorrupted==0) { + if (DatabaseCorrupted == 0) { DatabaseCorrupted++; kill++; } else { @@ -151,7 +146,7 @@ void DBLog(const char *file,int line,const char *fmt,...) va_start(vararg,fmt); mir_vsnprintf(str,sizeof(str),fmt,vararg); va_end(vararg); - fp=fopen("c:\\mirandadatabase.log.txt","at"); + fp = fopen("c:\\mirandadatabase.log.txt","at"); fprintf(fp,"%u: %s %d: %s\n",GetTickCount(),file,line,str); fclose(fp); } diff --git a/plugins/Db3x/src/database.h b/plugins/Db3x/src/database.h index 334e47002d..2394655e73 100644 --- a/plugins/Db3x/src/database.h +++ b/plugins/Db3x/src/database.h @@ -39,7 +39,7 @@ DBHeader | | \-->module name (DBModuleName) | \-->first/last/firstunread event |-->user contact (DBContact) - | |-->next contact=NULL + | |-->next contact = NULL | |-->first settings as above | \-->first/last/firstunread event as above \-->first module name (DBModuleName) @@ -54,7 +54,7 @@ DBHeader #include struct DBHeader { BYTE signature[16]; // 'Miranda ICQ DB',0,26 - DWORD version; //as 4 bytes, ie 1.2.3.10=0x0102030a + 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 @@ -63,8 +63,8 @@ struct DBHeader { //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 struct DBContact in the chain - DWORD ofsUser; //offset to struct DBContact representing 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 }; @@ -102,13 +102,13 @@ struct DBContactSettings { //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 + //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 + //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 @@ -174,6 +174,9 @@ typedef struct //databasecorruption: called if any signatures are broken. very very fatal void DatabaseCorruption(void); 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 DBFlush(int setting); void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes); diff --git a/plugins/Db3x/src/dbcache.cpp b/plugins/Db3x/src/dbcache.cpp index 10d34e1ec3..c68db53a0a 100644 --- a/plugins/Db3x/src/dbcache.cpp +++ b/plugins/Db3x/src/dbcache.cpp @@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define CACHESECTIONSIZE 4096 #define CACHESECTIONCOUNT 32 -static BOOL safetyMode=TRUE; +static BOOL safetyMode = TRUE; static PBYTE pDbCache; static DWORD lastUseCounter; struct DBCacheSectionInfo { @@ -37,23 +37,23 @@ struct DBCacheSectionInfo { static __inline int FindSectionForOffset(const DWORD ofs) { int i; - for(i=0;i=cacheSectionInfo[i].ofsBase && ofs= cacheSectionInfo[i].ofsBase && ofs=cacheSectionInfo[i].ofsBase && ofs=cacheSectionInfo[i].ofsBase+CACHESECTIONSIZE) //don't finish at end + for(i = 0;i= cacheSectionInfo[i].ofsBase && ofs= cacheSectionInfo[i].ofsBase+CACHESECTIONSIZE) //don't finish at end CopyMemory(pDbCache+i*CACHESECTIONSIZE,(PBYTE)pData+cacheSectionInfo[i].ofsBase-ofs,CACHESECTIONSIZE); else CopyMemory(pDbCache+i*CACHESECTIONSIZE,(PBYTE)pData+cacheSectionInfo[i].ofsBase-ofs,bytes-(cacheSectionInfo[i].ofsBase-ofs)); } else { //start at beginning - if(ofs+bytes>=cacheSectionInfo[i].ofsBase+CACHESECTIONSIZE) //don't finish at end + if (ofs+bytes >= cacheSectionInfo[i].ofsBase+CACHESECTIONSIZE) //don't finish at end CopyMemory(pDbCache+i*CACHESECTIONSIZE+ofs-cacheSectionInfo[i].ofsBase,pData,cacheSectionInfo[i].ofsBase+CACHESECTIONSIZE-ofs); else CopyMemory(pDbCache+i*CACHESECTIONSIZE+ofs-cacheSectionInfo[i].ofsBase,pData,bytes); } @@ -162,7 +162,7 @@ void DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes) PBYTE buf; log3("move %d %08x->%08x",bytes,ofsSource,ofsDest); - buf=(PBYTE)mir_alloc(bytes); + buf = (PBYTE)mir_alloc(bytes); SetFilePointer(hDbFile,ofsSource,NULL,FILE_BEGIN); ReadFile(hDbFile,buf,bytes,&bytesRead,NULL); DBWrite(ofsDest,buf,bytes); @@ -183,35 +183,33 @@ void DBFlush(int setting) { if (!setting) { log0("nflush1"); - if(safetyMode) FlushFileBuffers(hDbFile); + if (safetyMode) FlushFileBuffers(hDbFile); log0("nflush2"); return; } KillTimer(NULL,flushBuffersTimerId); - flushBuffersTimerId=SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); + flushBuffersTimerId = SetTimer(NULL,flushBuffersTimerId,50,DoBufferFlushTimerProc); } static INT_PTR CacheSetSafetyMode(WPARAM wParam,LPARAM lParam) { EnterCriticalSection(&csDbAccess); - safetyMode=wParam; + safetyMode = wParam; LeaveCriticalSection(&csDbAccess); - if(safetyMode) FlushFileBuffers(hDbFile); + if (safetyMode) FlushFileBuffers(hDbFile); return 0; } int InitCache(void) { - int i; - DWORD bytesRead; - - CreateServiceFunction(MS_DB_SETSAFETYMODE,CacheSetSafetyMode); - pDbCache=(PBYTE)mir_alloc(CACHESECTIONSIZE*CACHESECTIONCOUNT); - lastUseCounter=CACHESECTIONCOUNT; - for(i=0;ihNext != NULL) { - if (!lParam || CheckProto(VL->hNext,(const char*)lParam)) { - LeaveCriticalSection(&csDbAccess); - return (INT_PTR)VL->hNext; - } + if ( !szProto || CheckProto(VL->hNext, szProto)) + return VL->hNext; VLtemp.hContact = VL->hNext; continue; } } - dbc=(struct DBContact*)DBRead((DWORD)VLtemp.hContact,sizeof(struct DBContact),NULL); - if (dbc->signature!=DBCONTACT_SIGNATURE) + DBContact *dbc = (DBContact*)DBRead(VLtemp.hContact,sizeof(DBContact),NULL); + if (dbc->signature != DBCONTACT_SIGNATURE) break; - else { - if ( VL == NULL ) { - VL = (DBCachedContactValueList*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList)); - VL->hContact = VLtemp.hContact; - List_Insert(&lContacts,VL,index); - } - VL->hNext = (HANDLE)dbc->ofsNext; - if (VL->hNext != NULL && (!lParam || CheckProto(VL->hNext,(const char*)lParam))) { - LeaveCriticalSection(&csDbAccess); - return (INT_PTR)VL->hNext; - } - VLtemp.hContact = VL->hNext; - } } - LeaveCriticalSection(&csDbAccess); + + if ( VL == NULL ) { + VL = (DBCachedContactValueList*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList)); + VL->hContact = VLtemp.hContact; + List_Insert(&lContacts,VL,index); + } + VL->hNext = (HANDLE)dbc->ofsNext; + if (VL->hNext != NULL && (!szProto || CheckProto(VL->hNext, szProto))) + return VL->hNext; + + VLtemp.hContact = VL->hNext; + } return 0; } -static INT_PTR DeleteContact(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact) { - struct DBContact *dbc,*dbcPrev; + DBContact *dbcPrev; DWORD ofsThis,ofsNext,ofsFirstEvent; - struct DBContactSettings *dbcs; - struct DBEvent *dbe; + DBContactSettings *dbcs; + DBEvent *dbe; int index; - if ((HANDLE)wParam==NULL) return 1; - EnterCriticalSection(&csDbAccess); - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) { - LeaveCriticalSection(&csDbAccess); - return 1; - } - if ( (HANDLE)wParam == (HANDLE)dbHeader.ofsUser ) { - LeaveCriticalSection(&csDbAccess); - log0("FATAL: del of user chain attempted."); - return 1; + if (hContact == NULL) return 1; + { + mir_cslock lck(csDbAccess); + + DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); + if (dbc->signature != DBCONTACT_SIGNATURE) + return 1; + + if ((DWORD)hContact == dbHeader.ofsUser ) { + log0("FATAL: del of user chain attempted."); + return 1; + } + + log0("del contact"); } - log0("del contact"); - LeaveCriticalSection(&csDbAccess); + //call notifier while outside mutex - NotifyEventHooks(hContactDeletedEvent,wParam,0); + NotifyEventHooks(hContactDeletedEvent, (WPARAM)hContact, 0); + //get back in - EnterCriticalSection(&csDbAccess); + mir_cslock lck(csDbAccess); - { DBCachedContactValueList VLtemp; - VLtemp.hContact = (HANDLE)wParam; - if ( List_GetIndex(&lContacts,&VLtemp,&index)) - { - DBCachedContactValueList *VL = ( DBCachedContactValueList* )lContacts.items[index]; - DBCachedContactValue* V = VL->first; - while ( V != NULL ) { - DBCachedContactValue* V1 = V->next; - if ( V->value.type == DBVT_ASCIIZ ) - HeapFree( hCacheHeap, 0, V->value.pszVal ); - HeapFree( hCacheHeap, 0, V ); - V = V1; - } - HeapFree( hCacheHeap, 0, VL ); - - List_Remove(&lContacts,index); - } } - - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); + 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; + if ( V->value.type == DBVT_ASCIIZ ) + HeapFree( hCacheHeap, 0, V->value.pszVal ); + HeapFree( hCacheHeap, 0, V ); + V = V1; + } + HeapFree( hCacheHeap, 0, VL ); + + List_Remove(&lContacts,index); + } + + DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL); //delete settings chain - ofsThis=dbc->ofsFirstSettings; - ofsFirstEvent=dbc->ofsFirstEvent; + ofsThis = dbc->ofsFirstSettings; + ofsFirstEvent = dbc->ofsFirstEvent; while(ofsThis) { - dbcs=(struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); - ofsNext=dbcs->ofsNext; + dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); + ofsNext = dbcs->ofsNext; DeleteSpace(ofsThis,offsetof(struct DBContactSettings,blob)+dbcs->cbBlob); - ofsThis=ofsNext; + ofsThis = ofsNext; } //delete event chain - ofsThis=ofsFirstEvent; + ofsThis = ofsFirstEvent; while(ofsThis) { - dbe=(struct DBEvent*)DBRead(ofsThis,sizeof(struct DBEvent),NULL); - ofsNext=dbe->ofsNext; - DeleteSpace(ofsThis,offsetof(struct DBEvent,blob)+dbe->cbBlob); - ofsThis=ofsNext; + 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=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbHeader.ofsFirstContact==wParam) { - dbHeader.ofsFirstContact=dbc->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=(struct DBContact*)DBRead(ofsThis,sizeof(struct DBContact),NULL); - while(dbcPrev->ofsNext!=wParam) { - if(dbcPrev->ofsNext==0) DatabaseCorruption(); - ofsThis=dbcPrev->ofsNext; - dbcPrev=(struct DBContact*)DBRead(ofsThis,sizeof(struct DBContact),NULL); + ofsNext = dbc->ofsNext; + ofsThis = dbHeader.ofsFirstContact; + dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL); + while(dbcPrev->ofsNext!= (DWORD)hContact) { + if (dbcPrev->ofsNext == 0) DatabaseCorruption(); + ofsThis = dbcPrev->ofsNext; + dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL); } - dbcPrev->ofsNext=ofsNext; - DBWrite(ofsThis,dbcPrev,sizeof(struct DBContact)); + dbcPrev->ofsNext = ofsNext; + DBWrite(ofsThis,dbcPrev,sizeof(DBContact)); { DBCachedContactValueList VLtemp; VLtemp.hContact = (HANDLE)ofsThis; @@ -221,34 +203,34 @@ static INT_PTR DeleteContact(WPARAM wParam,LPARAM lParam) } } } //delete contact - DeleteSpace(wParam,sizeof(struct DBContact)); + DeleteSpace((DWORD)hContact,sizeof(DBContact)); //decrement contact count dbHeader.contactCount--; DBWrite(0,&dbHeader,sizeof(dbHeader)); DBFlush(0); + //quit - LeaveCriticalSection(&csDbAccess); return 0; } -static INT_PTR AddContact(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::AddContact(void) { - struct DBContact dbc; + DBContact dbc; DWORD ofsNew; log0("add contact"); EnterCriticalSection(&csDbAccess); - ofsNew=CreateNewSpace(sizeof(struct 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; + 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(struct DBContact)); + DBWrite(ofsNew,&dbc,sizeof(DBContact)); DBWrite(0,&dbHeader,sizeof(dbHeader)); DBFlush(0); @@ -263,29 +245,29 @@ static INT_PTR AddContact(WPARAM wParam,LPARAM lParam) LeaveCriticalSection(&csDbAccess); NotifyEventHooks(hContactAddedEvent,(WPARAM)ofsNew,0); - return (INT_PTR)ofsNew; + return (HANDLE)ofsNew; } -static INT_PTR IsDbContact(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::IsDbContact(HANDLE hContact) { - struct DBContact dbc; - DWORD ofsContact=(DWORD)wParam; + DBContact dbc; + DWORD ofsContact = (DWORD)hContact; INT_PTR ret; EnterCriticalSection(&csDbAccess); { int index; DBCachedContactValueList VLtemp,*VL; - VLtemp.hContact = (HANDLE)wParam; + VLtemp.hContact = hContact; if ( List_GetIndex(&lContacts,&VLtemp,&index)) ret = TRUE; else { - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - ret=dbc.signature==DBCONTACT_SIGNATURE; + dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); + ret = dbc.signature == DBCONTACT_SIGNATURE; if (ret) { VL = (DBCachedContactValueList*)HeapAlloc(hCacheHeap,HEAP_ZERO_MEMORY,sizeof(DBCachedContactValueList)); - VL->hContact = (HANDLE)wParam; + VL->hContact = hContact; List_Insert(&lContacts,VL,index); } } } diff --git a/plugins/Db3x/src/dbevents.cpp b/plugins/Db3x/src/dbevents.cpp index be50babe57..2deb70c647 100644 --- a/plugins/Db3x/src/dbevents.cpp +++ b/plugins/Db3x/src/dbevents.cpp @@ -28,398 +28,380 @@ char *GetModuleNameByOfs(DWORD ofs); static HANDLE hEventDeletedEvent,hEventAddedEvent,hEventFilterAddedEvent; -static INT_PTR GetEventCount(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(LONG) CDdxMmap::GetEventCount(HANDLE hContact) { INT_PTR ret; - struct DBContact *dbc; + DBContact *dbc; EnterCriticalSection(&csDbAccess); - if(wParam==0) wParam=dbHeader.ofsUser; - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) ret=-1; - else ret=dbc->eventCount; + if (hContact == 0) + hContact = (HANDLE)dbHeader.ofsUser; + dbc = (DBContact*)DBRead(hContact, sizeof(DBContact),NULL); + if (dbc->signature!= DBCONTACT_SIGNATURE) ret = -1; + else ret = dbc->eventCount; LeaveCriticalSection(&csDbAccess); return ret; } -static INT_PTR AddEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei) { - DBEVENTINFO *dbei=(DBEVENTINFO*)lParam; - struct DBContact dbc; - struct DBEvent dbe,*dbeTest; + DBContact dbc; + DBEvent dbe,*dbeTest; DWORD ofsNew,ofsModuleName,ofsContact,ofsThis; - if(dbei==NULL||dbei->cbSize!=sizeof(DBEVENTINFO)) return 0; - if(dbei->timestamp==0) return 0; - if (NotifyEventHooks(hEventFilterAddedEvent,wParam,lParam)) { + 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(wParam==0) ofsContact=dbHeader.ofsUser; - else ofsContact=wParam; - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - if(dbc.signature!=DBCONTACT_SIGNATURE) { + if (hContact == 0) + hContact = (HANDLE)dbHeader.ofsUser; + else + ofsContact = (DWORD)hContact; + dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL); + if (dbc.signature!= DBCONTACT_SIGNATURE) { LeaveCriticalSection(&csDbAccess); return 0; } - ofsNew=CreateNewSpace(offsetof(struct 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; + 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=wParam; - dbe.ofsNext=0; - dbe.flags|=DBEF_FIRST; - dbc.ofsFirstEvent=dbc.ofsLastEvent=ofsNew; + if (dbc.eventCount == 0) { + dbe.ofsPrev = (DWORD)hContact; + dbe.ofsNext = 0; + dbe.flags |= DBEF_FIRST; + dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew; } else { - dbeTest=(struct DBEvent*)DBRead(dbc.ofsFirstEvent,sizeof(struct DBEvent),NULL); + 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=wParam; - dbe.ofsNext=dbc.ofsFirstEvent; - dbe.flags|=DBEF_FIRST; - dbc.ofsFirstEvent=ofsNew; - dbeTest=(struct DBEvent*)DBRead(dbe.ofsNext,sizeof(struct DBEvent),NULL); - dbeTest->flags&=~DBEF_FIRST; - dbeTest->ofsPrev=ofsNew; - DBWrite(dbe.ofsNext,dbeTest,sizeof(struct DBEvent)); + 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 = (struct DBEvent*)DBRead(ofsThis, sizeof(struct DBEvent), NULL); + 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) { + if (dbe.timestamp >= dbeTest->timestamp) { dbe.ofsPrev = ofsThis; dbe.ofsNext = dbeTest->ofsNext; dbeTest->ofsNext = ofsNew; - DBWrite(ofsThis, dbeTest, sizeof(struct DBEvent)); + DBWrite(ofsThis, dbeTest, sizeof(DBEvent)); if (dbe.ofsNext == 0) dbc.ofsLastEvent = ofsNew; else { - dbeTest = (struct DBEvent*)DBRead(dbe.ofsNext, sizeof(struct DBEvent), NULL); + dbeTest = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL); dbeTest->ofsPrev = ofsNew; - DBWrite(dbe.ofsNext, dbeTest, sizeof(struct DBEvent)); + DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent)); } break; } ofsThis = dbeTest->ofsPrev; - dbeTest = (struct DBEvent*)DBRead(ofsThis, sizeof(struct DBEvent), NULL); + dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL); } } } dbc.eventCount++; if (!(dbe.flags&(DBEF_READ|DBEF_SENT))) { - if(dbe.timestamppBlob,dbei->cbBlob); + DBWrite(ofsContact,&dbc,sizeof(DBContact)); + DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob)); + DBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob); DBFlush(0); LeaveCriticalSection(&csDbAccess); log1("add event @ %08x",ofsNew); - NotifyEventHooks(hEventAddedEvent,wParam,(LPARAM)ofsNew); - return (INT_PTR)ofsNew; + NotifyEventHooks(hEventAddedEvent,(WPARAM)hContact,(LPARAM)ofsNew); + return (HANDLE)ofsNew; } -static INT_PTR DeleteEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent) { - struct DBContact dbc; + DBContact dbc; DWORD ofsContact,ofsThis; - struct DBEvent dbe,*dbeNext,*dbePrev; + DBEvent dbe,*dbeNext,*dbePrev; EnterCriticalSection(&csDbAccess); - if(wParam==0) ofsContact=dbHeader.ofsUser; - else ofsContact=wParam; - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - dbe=*(struct DBEvent*)DBRead(lParam,sizeof(struct DBEvent),NULL); - if(dbc.signature!=DBCONTACT_SIGNATURE || dbe.signature!=DBEVENT_SIGNATURE) { + if (hContact == 0) + hContact = (HANDLE)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,lParam); + NotifyEventHooks(hEventDeletedEvent, (WPARAM)hContact, (LPARAM)hDbEvent); + //get back in EnterCriticalSection(&csDbAccess); - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - dbe=*(struct DBEvent*)DBRead(lParam,sizeof(struct DBEvent),NULL); + 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)lParam) { - dbeNext=&dbe; + if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) { + dbeNext = &dbe; for (;;) { - if(dbeNext->ofsNext==0) { - dbc.ofsFirstUnreadEvent=0; - dbc.timestampFirstUnread=0; + if (dbeNext->ofsNext == 0) { + dbc.ofsFirstUnreadEvent = 0; + dbc.timestampFirstUnread = 0; break; } - ofsThis=dbeNext->ofsNext; - dbeNext=(struct DBEvent*)DBRead(ofsThis,sizeof(struct DBEvent),NULL); + ofsThis = dbeNext->ofsNext; + dbeNext = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL); if (!(dbeNext->flags&(DBEF_READ|DBEF_SENT))) { - dbc.ofsFirstUnreadEvent=ofsThis; - dbc.timestampFirstUnread=dbeNext->timestamp; + 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; + if (dbe.flags&DBEF_FIRST) { + if (dbe.ofsNext == 0) { + dbc.ofsFirstEvent = dbc.ofsLastEvent = 0; } else { - dbeNext=(struct DBEvent*)DBRead(dbe.ofsNext,sizeof(struct DBEvent),NULL); - dbeNext->flags|=DBEF_FIRST; - dbeNext->ofsPrev=dbe.ofsPrev; - DBWrite(dbe.ofsNext,dbeNext,sizeof(struct DBEvent)); - dbc.ofsFirstEvent=dbe.ofsNext; + 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=(struct DBEvent*)DBRead(dbe.ofsPrev,sizeof(struct DBEvent),NULL); - dbePrev->ofsNext=0; - DBWrite(dbe.ofsPrev,dbePrev,sizeof(struct DBEvent)); - dbc.ofsLastEvent=dbe.ofsPrev; + 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=(struct DBEvent*)DBRead(dbe.ofsPrev,sizeof(struct DBEvent),NULL); - dbePrev->ofsNext=dbe.ofsNext; - DBWrite(dbe.ofsPrev,dbePrev,sizeof(struct DBEvent)); - dbeNext=(struct DBEvent*)DBRead(dbe.ofsNext,sizeof(struct DBEvent),NULL); - dbeNext->ofsPrev=dbe.ofsPrev; - DBWrite(dbe.ofsNext,dbeNext,sizeof(struct DBEvent)); + 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(lParam,offsetof(struct DBEvent,blob)+dbe.cbBlob); + DeleteSpace((DWORD)hDbEvent,offsetof(DBEvent,blob)+dbe.cbBlob); //decrement event count dbc.eventCount--; - DBWrite(ofsContact,&dbc,sizeof(struct DBContact)); + DBWrite(ofsContact,&dbc,sizeof(DBContact)); DBFlush(0); //quit LeaveCriticalSection(&csDbAccess); return 0; } -static INT_PTR GetBlobSize(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(LONG) CDdxMmap::GetBlobSize(HANDLE hDbEvent) { INT_PTR ret; - struct DBEvent *dbe; EnterCriticalSection(&csDbAccess); - dbe=(struct DBEvent*)DBRead(wParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE) ret=-1; - else ret=dbe->cbBlob; + DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); + if (dbe->signature!= DBEVENT_SIGNATURE) ret = -1; + else ret = dbe->cbBlob; LeaveCriticalSection(&csDbAccess); return ret; } -static INT_PTR GetEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) { - struct DBEvent *dbe; - DBEVENTINFO *dbei=(DBEVENTINFO*)lParam; int bytesToCopy,i; - if(dbei==NULL||dbei->cbSize!=sizeof(DBEVENTINFO)) return 1; - if(dbei->cbBlob > 0 && dbei->pBlob == NULL) { + if (dbei == NULL||dbei->cbSize!= sizeof(DBEVENTINFO)) return 1; + if (dbei->cbBlob > 0 && dbei->pBlob == NULL) { dbei->cbBlob = 0; return 1; } EnterCriticalSection(&csDbAccess); - dbe=(struct DBEvent*)DBRead(wParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE) { + 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->cbBlobcbBlob) bytesToCopy=dbei->cbBlob; - else bytesToCopy=dbe->cbBlob; - dbei->cbBlob=dbe->cbBlob; - for(i=0;;i+=MAXCACHEDREADSIZE) { - if(bytesToCopy-i<=MAXCACHEDREADSIZE) { - CopyMemory(dbei->pBlob+i,DBRead(wParam+offsetof(struct DBEvent,blob)+i,bytesToCopy-i,NULL),bytesToCopy-i); + dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName); + dbei->timestamp = dbe->timestamp; + dbei->flags = dbe->flags; + dbei->eventType = dbe->eventType; + if (dbei->cbBlobcbBlob) bytesToCopy = dbei->cbBlob; + else bytesToCopy = dbe->cbBlob; + dbei->cbBlob = dbe->cbBlob; + for(i = 0;;i += MAXCACHEDREADSIZE) { + if (bytesToCopy-i <= MAXCACHEDREADSIZE) { + CopyMemory(dbei->pBlob+i,DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,bytesToCopy-i,NULL),bytesToCopy-i); break; } - CopyMemory(dbei->pBlob+i,DBRead(wParam+offsetof(struct DBEvent,blob)+i,MAXCACHEDREADSIZE,NULL),MAXCACHEDREADSIZE); + CopyMemory(dbei->pBlob+i,DBRead(DWORD(hDbEvent)+offsetof(DBEvent,blob)+i,MAXCACHEDREADSIZE,NULL),MAXCACHEDREADSIZE); } LeaveCriticalSection(&csDbAccess); return 0; } -static INT_PTR MarkEventRead(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent) { INT_PTR ret; - struct DBEvent *dbe; - struct DBContact dbc; DWORD ofsThis; EnterCriticalSection(&csDbAccess); - if(wParam==0) wParam=dbHeader.ofsUser; - dbc=*(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - dbe=(struct DBEvent*)DBRead(lParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE || dbc.signature!=DBCONTACT_SIGNATURE) { + if (hContact == 0) + hContact = (HANDLE)dbHeader.ofsUser; + DBContact dbc = *(DBContact*)DBRead(hContact,sizeof(DBContact),NULL); + DBEvent *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; + 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(lParam,dbe,sizeof(struct DBEvent)); - ret=(INT_PTR)dbe->flags; - if(dbc.ofsFirstUnreadEvent==(DWORD)lParam) { + dbe->flags |= DBEF_READ; + DBWrite((DWORD)hDbEvent, dbe, sizeof(DBEvent)); + ret = (INT_PTR)dbe->flags; + if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) { for (;;) { - if(dbe->ofsNext==0) { - dbc.ofsFirstUnreadEvent=0; - dbc.timestampFirstUnread=0; + if (dbe->ofsNext == 0) { + dbc.ofsFirstUnreadEvent = 0; + dbc.timestampFirstUnread = 0; break; } - ofsThis=dbe->ofsNext; - dbe=(struct DBEvent*)DBRead(ofsThis,sizeof(struct DBEvent),NULL); + ofsThis = dbe->ofsNext; + dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL); if (!(dbe->flags&(DBEF_READ|DBEF_SENT))) { - dbc.ofsFirstUnreadEvent=ofsThis; - dbc.timestampFirstUnread=dbe->timestamp; + dbc.ofsFirstUnreadEvent = ofsThis; + dbc.timestampFirstUnread = dbe->timestamp; break; } } } - DBWrite(wParam,&dbc,sizeof(struct DBContact)); + DBWrite((DWORD)hContact,&dbc,sizeof(DBContact)); DBFlush(0); LeaveCriticalSection(&csDbAccess); return ret; } -static INT_PTR GetEventContact(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::GetEventContact(HANDLE hDbEvent) { - INT_PTR ret; - struct DBEvent *dbe; + HANDLE ret; EnterCriticalSection(&csDbAccess); - dbe=(struct DBEvent*)DBRead(wParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE) { + DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); + if (dbe->signature!= DBEVENT_SIGNATURE) { LeaveCriticalSection(&csDbAccess); - return -1; + return (HANDLE)-1; } - while(!(dbe->flags&DBEF_FIRST)) - dbe=(struct DBEvent*)DBRead(dbe->ofsPrev,sizeof(struct DBEvent),NULL); - ret=(INT_PTR)dbe->ofsPrev; + while(!(dbe->flags & DBEF_FIRST)) + dbe = (DBEvent*)DBRead(dbe->ofsPrev,sizeof(DBEvent),NULL); + ret = (HANDLE)dbe->ofsPrev; LeaveCriticalSection(&csDbAccess); return ret; } -static INT_PTR FindFirstEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstEvent(HANDLE hContact) { - INT_PTR ret; - struct DBContact *dbc; + HANDLE ret; EnterCriticalSection(&csDbAccess); - if(wParam==0) wParam=dbHeader.ofsUser; - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) ret=0; - else ret=(INT_PTR)dbc->ofsFirstEvent; + 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; } -static INT_PTR FindFirstUnreadEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::FindFirstUnreadEvent(HANDLE hContact) { - INT_PTR ret; - struct DBContact *dbc; + HANDLE ret; EnterCriticalSection(&csDbAccess); - if(wParam==0) wParam=dbHeader.ofsUser; - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) ret=0; - else ret=(INT_PTR)dbc->ofsFirstUnreadEvent; + 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; } -static INT_PTR FindLastEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::FindLastEvent(HANDLE hContact) { - INT_PTR ret; - struct DBContact *dbc; + HANDLE ret; EnterCriticalSection(&csDbAccess); - if(wParam==0) wParam=dbHeader.ofsUser; - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) ret=0; - else ret=(INT_PTR)dbc->ofsLastEvent; + 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; } -static INT_PTR FindNextEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::FindNextEvent(HANDLE hDbEvent) { - INT_PTR ret; - struct DBEvent *dbe; + HANDLE ret; EnterCriticalSection(&csDbAccess); - dbe=(struct DBEvent*)DBRead(wParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE) ret=0; - else ret=(INT_PTR)dbe->ofsNext; + DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL); + if (dbe->signature!= DBEVENT_SIGNATURE) ret = 0; + else ret = (HANDLE)dbe->ofsNext; LeaveCriticalSection(&csDbAccess); return ret; } -static INT_PTR FindPrevEvent(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(HANDLE) CDdxMmap::FindPrevEvent(HANDLE hDbEvent) { - INT_PTR ret; - struct DBEvent *dbe; + HANDLE ret; EnterCriticalSection(&csDbAccess); - dbe=(struct DBEvent*)DBRead(wParam,sizeof(struct DBEvent),NULL); - if(dbe->signature!=DBEVENT_SIGNATURE) ret=0; - else if(dbe->flags&DBEF_FIRST) ret=0; - else ret=(INT_PTR)dbe->ofsPrev; + 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; } int InitEvents(void) { - CreateServiceFunction(MS_DB_EVENT_GETCOUNT,GetEventCount); - CreateServiceFunction(MS_DB_EVENT_ADD,AddEvent); - CreateServiceFunction(MS_DB_EVENT_DELETE,DeleteEvent); - CreateServiceFunction(MS_DB_EVENT_GETBLOBSIZE,GetBlobSize); - CreateServiceFunction(MS_DB_EVENT_GET,GetEvent); - CreateServiceFunction(MS_DB_EVENT_MARKREAD,MarkEventRead); - CreateServiceFunction(MS_DB_EVENT_GETCONTACT,GetEventContact); - CreateServiceFunction(MS_DB_EVENT_FINDFIRST,FindFirstEvent); - CreateServiceFunction(MS_DB_EVENT_FINDFIRSTUNREAD,FindFirstUnreadEvent); - CreateServiceFunction(MS_DB_EVENT_FINDLAST,FindLastEvent); - CreateServiceFunction(MS_DB_EVENT_FINDNEXT,FindNextEvent); - CreateServiceFunction(MS_DB_EVENT_FINDPREV,FindPrevEvent); - hEventDeletedEvent=CreateHookableEvent(ME_DB_EVENT_DELETED); - hEventAddedEvent=CreateHookableEvent(ME_DB_EVENT_ADDED); - hEventFilterAddedEvent=CreateHookableEvent(ME_DB_EVENT_FILTER_ADD); + hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED); + hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED); + hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD); return 0; } - -void UninitEvents(void) -{ - DestroyHookableEvent(hEventAddedEvent); hEventAddedEvent = 0; - DestroyHookableEvent(hEventDeletedEvent); hEventDeletedEvent = 0; - DestroyHookableEvent(hEventFilterAddedEvent); hEventFilterAddedEvent = 0; -} diff --git a/plugins/Db3x/src/dbheaders.cpp b/plugins/Db3x/src/dbheaders.cpp index 8320102888..7092dc4630 100644 --- a/plugins/Db3x/src/dbheaders.cpp +++ b/plugins/Db3x/src/dbheaders.cpp @@ -27,44 +27,44 @@ struct DBSignature { char name[15]; BYTE eof; }; -static struct DBSignature dbSignature={"Miranda ICQ DB",0x1A}; +static struct DBSignature dbSignature = {"Miranda ICQ DB",0x1A}; //the cache has not been loaded when these functions are used int CreateDbHeaders(HANDLE hFile) { - struct DBContact user; + DBContact user; DWORD bytesWritten; CopyMemory(dbHeader.signature,&dbSignature,sizeof(dbHeader.signature)); - dbHeader.version=DB_THIS_VERSION; - dbHeader.ofsFileEnd=sizeof(struct DBHeader); - dbHeader.slackSpace=0; - dbHeader.contactCount=0; - dbHeader.ofsFirstContact=0; - dbHeader.ofsFirstModuleName=0; - dbHeader.ofsUser=0; + dbHeader.version = DB_THIS_VERSION; + 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(struct DBContact); + 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; + 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(struct DBContact),&bytesWritten,NULL); + WriteFile(hFile,&user,sizeof(DBContact),&bytesWritten,NULL); FlushFileBuffers(hFile); return 0; } int CheckDbHeaders(struct DBHeader * hdr) { - if(memcmp(hdr->signature,&dbSignature,sizeof(hdr->signature))) return 1; - if(hdr->version!=DB_THIS_VERSION) return 2; - if(hdr->ofsUser==0) return 3; + if (memcmp(hdr->signature,&dbSignature,sizeof(hdr->signature))) return 1; + if (hdr->version!= DB_THIS_VERSION) return 2; + if (hdr->ofsUser == 0) return 3; return 0; } diff --git a/plugins/Db3x/src/dbintf.cpp b/plugins/Db3x/src/dbintf.cpp new file mode 100644 index 0000000000..c30d8206cc --- /dev/null +++ b/plugins/Db3x/src/dbintf.cpp @@ -0,0 +1,38 @@ +/* + +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/Db3x/src/dbintf.h b/plugins/Db3x/src/dbintf.h new file mode 100644 index 0000000000..6e9714640b --- /dev/null +++ b/plugins/Db3x/src/dbintf.h @@ -0,0 +1,68 @@ +/* + +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 + +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); + +private: + int CheckProto(HANDLE hContact, const char *proto); + + BOOL safetyMode; +}; diff --git a/plugins/Db3x/src/dbmodulechain.cpp b/plugins/Db3x/src/dbmodulechain.cpp index f130937af2..fb7c6e4ce3 100644 --- a/plugins/Db3x/src/dbmodulechain.cpp +++ b/plugins/Db3x/src/dbmodulechain.cpp @@ -39,40 +39,39 @@ int InitModuleNames(void) DWORD ofsThis,ofsNext; int nameLen; - moduleNameCount=0; - moduleName=NULL; - ofsThis=dbHeader.ofsFirstModuleName; - dbmn=(struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); + moduleNameCount = 0; + moduleName = NULL; + ofsThis = dbHeader.ofsFirstModuleName; + dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); while(ofsThis) { - if(dbmn->signature!=DBMODULENAME_SIGNATURE) DatabaseCorruption(); - moduleName=(struct ModuleName*)mir_realloc(moduleName,sizeof(struct ModuleName)*(moduleNameCount+1)); - moduleName[moduleNameCount].ofs=ofsThis; - moduleName[moduleNameCount].hash=dbmn->cbName; //very very simple hash so far - moduleName[moduleNameCount].name=(char*)mir_alloc(dbmn->cbName+1); - ofsNext=dbmn->ofsNext; - nameLen=dbmn->cbName; + if (dbmn->signature!= DBMODULENAME_SIGNATURE) DatabaseCorruption(); + moduleName = (struct ModuleName*)mir_realloc(moduleName,sizeof(struct ModuleName)*(moduleNameCount+1)); + moduleName[moduleNameCount].ofs = ofsThis; + moduleName[moduleNameCount].hash = dbmn->cbName; //very very simple hash so far + moduleName[moduleNameCount].name = (char*)mir_alloc(dbmn->cbName+1); + ofsNext = dbmn->ofsNext; + nameLen = dbmn->cbName; CopyMemory(moduleName[moduleNameCount].name,DBRead(ofsThis+offsetof(struct DBModuleName,name),nameLen,NULL),nameLen); - moduleName[moduleNameCount].name[nameLen]=0; + moduleName[moduleNameCount].name[nameLen] = 0; moduleNameCount++; - ofsThis=ofsNext; - dbmn=(struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); + ofsThis = ofsNext; + dbmn = (struct DBModuleName*)DBRead(ofsThis,sizeof(struct DBModuleName),NULL); } - CreateServiceFunction(MS_DB_MODULES_ENUM,EnumModuleNames); return 0; } void UninitModuleNames(void) { int i; - for(i=0;iofsFirstSettings; + ofsThis = dbc->ofsFirstSettings; while(ofsThis) { - dbcs=(struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); - if(dbcs->signature!=DBCONTACTSETTINGS_SIGNATURE) DatabaseCorruption(); - if(dbcs->ofsModuleName==ofsModuleName) { - settingsGroupOfsCache[nextSGOCacheEntry].ofsContact=ofsContact; - settingsGroupOfsCache[nextSGOCacheEntry].ofsModuleName=ofsModuleName; - settingsGroupOfsCache[nextSGOCacheEntry].ofsSettingsGroup=ofsThis; - if (++nextSGOCacheEntry==SETTINGSGROUPOFSCOUNT) nextSGOCacheEntry=0; + dbcs = (struct DBContactSettings*)DBRead(ofsThis,sizeof(struct DBContactSettings),NULL); + if (dbcs->signature!= DBCONTACTSETTINGS_SIGNATURE) DatabaseCorruption(); + if (dbcs->ofsModuleName == ofsModuleName) { + settingsGroupOfsCache[nextSGOCacheEntry].ofsContact = ofsContact; + settingsGroupOfsCache[nextSGOCacheEntry].ofsModuleName = ofsModuleName; + settingsGroupOfsCache[nextSGOCacheEntry].ofsSettingsGroup = ofsThis; + if (++nextSGOCacheEntry == SETTINGSGROUPOFSCOUNT) nextSGOCacheEntry = 0; return ofsThis; } - ofsThis=dbcs->ofsNext; + ofsThis = dbcs->ofsNext; } return 0; } static void InvalidateSettingsGroupOfsCacheEntry(DWORD ofsSettingsGroup) { - int i; - - for(i=0;ifirst; V != NULL; V = V->next) - if (strcmp(V->name,szSetting)==0) + if (strcmp(V->name,szSetting) == 0) break; if ( V == NULL ) { @@ -203,7 +201,7 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo FreeCachedVariant(&V->value); if ( VL->first == V ) VL->first = V->next; - for ( V1=VL->first; V1 != NULL; V1 = V1->next ) + for ( V1 = VL->first; V1 != NULL; V1 = V1->next ) if ( V1->next == V ) { V1->next = V->next; break; @@ -215,12 +213,12 @@ static DBVARIANT* GetCachedValuePtr( HANDLE hContact, char* szSetting, int bAllo 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) +#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) { - struct DBContact dbc; + DBContact dbc; struct DBContactSettings dbcs; DWORD ofsModuleName,ofsContact,ofsSettingsGroup,ofsBlobPtr; int settingNameLen; @@ -230,7 +228,7 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING if ((!dbcgs->szSetting) || (!dbcgs->szModule)) return 1; - settingNameLen=(int)strlen(dbcgs->szSetting); + settingNameLen = (int)strlen(dbcgs->szSetting); EnterCriticalSection(&csDbAccess); @@ -251,10 +249,10 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING cbOrigLen--; dbcgs->pValue->pszVal = cbOrigPtr; - if(cbLenpValue->pszVal,pCachedValue->pszVal,cbOrigLen); - dbcgs->pValue->pszVal[cbOrigLen]=0; - dbcgs->pValue->cchVal=cbLen; + dbcgs->pValue->pszVal[cbOrigLen] = 0; + dbcgs->pValue->cchVal = cbLen; } else { dbcgs->pValue->pszVal = (char*)mir_alloc(strlen(pCachedValue->pszVal)+1); @@ -277,65 +275,65 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING return ( pCachedValue->type == DBVT_DELETED ) ? 1 : 0; } } - ofsModuleName=GetModuleNameOfs(dbcgs->szModule); - if(hContact==NULL) ofsContact=dbHeader.ofsUser; - else ofsContact=(DWORD)hContact; - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - if(dbc.signature!=DBCONTACT_SIGNATURE) { + 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,ofsContact,ofsModuleName); - if(ofsSettingsGroup) { - dbcs=*(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),&bytesRemaining); - ofsBlobPtr=ofsSettingsGroup+offsetof(struct DBContactSettings,blob); - pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); + ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsContact,ofsModuleName); + if (ofsSettingsGroup) { + dbcs = *(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),&bytesRemaining); + ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); + pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); while(pBlob[0]) { NeedBytes(1+settingNameLen); - if(pBlob[0]==settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,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])) { + if (isStatic && pBlob[0]&DBVTF_VARIABLELENGTH && VLT(dbcgs->pValue->type) != VLT(pBlob[0])) { LeaveCriticalSection(&csDbAccess); return 1; } - dbcgs->pValue->type=pBlob[0]; + dbcgs->pValue->type = pBlob[0]; switch(pBlob[0]) { case DBVT_DELETED: { /* this setting is deleted */ - dbcgs->pValue->type=DBVT_DELETED; + dbcgs->pValue->type = DBVT_DELETED; LeaveCriticalSection(&csDbAccess); 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_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_UTF8: case DBVT_ASCIIZ: NeedBytes(3+*(PWORD)(pBlob+1)); - if(isStatic) { + if (isStatic) { dbcgs->pValue->cchVal--; - if (*(PWORD)(pBlob+1)pValue->cchVal) dbcgs->pValue->cchVal=*(PWORD)(pBlob+1); + if (*(PWORD)(pBlob+1)pValue->cchVal) dbcgs->pValue->cchVal = *(PWORD)(pBlob+1); CopyMemory(dbcgs->pValue->pszVal,pBlob+3,dbcgs->pValue->cchVal); - dbcgs->pValue->pszVal[dbcgs->pValue->cchVal]=0; - dbcgs->pValue->cchVal=*(PWORD)(pBlob+1); + dbcgs->pValue->pszVal[dbcgs->pValue->cchVal] = 0; + dbcgs->pValue->cchVal = *(PWORD)(pBlob+1); } else { - dbcgs->pValue->pszVal=(char*)mir_alloc(1+*(PWORD)(pBlob+1)); + dbcgs->pValue->pszVal = (char*)mir_alloc(1+*(PWORD)(pBlob+1)); CopyMemory(dbcgs->pValue->pszVal,pBlob+3,*(PWORD)(pBlob+1)); - dbcgs->pValue->pszVal[*(PWORD)(pBlob+1)]=0; + dbcgs->pValue->pszVal[*(PWORD)(pBlob+1)] = 0; } break; case DBVT_BLOB: NeedBytes(3+*(PWORD)(pBlob+1)); - if(isStatic) { - if (*(PWORD)(pBlob+1)pValue->cpbVal) dbcgs->pValue->cpbVal=*(PWORD)(pBlob+1); + if (isStatic) { + if (*(PWORD)(pBlob+1)pValue->cpbVal) dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1); CopyMemory(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)); } - dbcgs->pValue->cpbVal=*(PWORD)(pBlob+1); + dbcgs->pValue->cpbVal = *(PWORD)(pBlob+1); break; } @@ -371,11 +369,10 @@ static __inline int GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING return 1; } -static INT_PTR GetContactSetting(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::GetContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dgs) { - DBCONTACTGETSETTING* dgs = ( DBCONTACTGETSETTING* )lParam; dgs->pValue->type = 0; - if ( GetContactSettingWorker(( HANDLE )wParam, dgs, 0 )) + if ( GetContactSettingWorker(hContact, dgs, 0)) return 1; if ( dgs->pValue->type == DBVT_UTF8 ) { @@ -406,12 +403,11 @@ static INT_PTR GetContactSetting(WPARAM wParam,LPARAM lParam) return 0; } -static INT_PTR GetContactSettingStr(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::GetContactSettingStr(HANDLE hContact, DBCONTACTGETSETTING *dgs) { - DBCONTACTGETSETTING* dgs = (DBCONTACTGETSETTING*)lParam; int iSaveType = dgs->pValue->type; - if ( GetContactSettingWorker(( HANDLE )wParam, dgs, 0 )) + if ( GetContactSettingWorker(hContact, dgs, 0)) return 1; if ( iSaveType == 0 || iSaveType == dgs->pValue->type ) @@ -454,10 +450,9 @@ static INT_PTR GetContactSettingStr(WPARAM wParam,LPARAM lParam) return 0; } -INT_PTR GetContactSettingStatic(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::GetContactSettingStatic(HANDLE hContact, DBCONTACTGETSETTING *dgs) { - DBCONTACTGETSETTING* dgs = (DBCONTACTGETSETTING*)lParam; - if ( GetContactSettingWorker(( HANDLE )wParam, dgs, 1 )) + if ( GetContactSettingWorker(hContact, dgs, 1)) return 1; if ( dgs->pValue->type == DBVT_UTF8 ) { @@ -468,9 +463,8 @@ INT_PTR GetContactSettingStatic(WPARAM wParam,LPARAM lParam) return 0; } -static INT_PTR FreeVariant(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::FreeVariant(DBVARIANT *dbv) { - DBVARIANT *dbv=(DBVARIANT*)lParam; if ( dbv == 0 ) return 1; switch ( dbv->type ) { case DBVT_ASCIIZ: @@ -478,27 +472,27 @@ static INT_PTR FreeVariant(WPARAM wParam,LPARAM lParam) case DBVT_WCHAR: { if ( dbv->pszVal ) mir_free(dbv->pszVal); - dbv->pszVal=0; + dbv->pszVal = 0; break; } case DBVT_BLOB: { if ( dbv->pbVal ) mir_free(dbv->pbVal); - dbv->pbVal=0; + dbv->pbVal = 0; break; } } - dbv->type=0; + dbv->type = 0; return 0; } -static INT_PTR SetSettingResident(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *pszSettingName) { char* szSetting; - size_t cbSettingNameLen = strlen(( char* )lParam ); + size_t cbSettingNameLen = strlen(pszSettingName); int idx; - char* szTemp = ( char* )alloca( cbSettingNameLen+2 ); - strcpy( szTemp+1, ( char* )lParam ); + char* szTemp = (char*)alloca( cbSettingNameLen+2 ); + strcpy( szTemp+1, pszSettingName); EnterCriticalSection(&csDbAccess); if ( !List_GetIndex( &lSettings, szTemp, &idx )) @@ -506,27 +500,26 @@ static INT_PTR SetSettingResident(WPARAM wParam,LPARAM lParam) else szSetting = (char *)lSettings.items[idx]; - *szSetting = (char)wParam; + *szSetting = (char)bIsResident; LeaveCriticalSection(&csDbAccess); return 0; } -static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRITESETTING *dbcws) { - DBCONTACTWRITESETTING *dbcws=(DBCONTACTWRITESETTING*)lParam; DBCONTACTWRITESETTING tmp; - struct DBContact dbc; + DBContact dbc; DWORD ofsModuleName; struct DBContactSettings dbcs; PBYTE pBlob; - int settingNameLen=0; - int moduleNameLen=0; - int settingDataLen=0; + 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 ) + if (dbcws == NULL || dbcws->szSetting == NULL || dbcws->szModule == NULL ) return 1; tmp = *dbcws; @@ -544,13 +537,13 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) 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) + 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 format can't tolerate more than 255 bytes of space (incl. null) for settings+module name - settingNameLen=(int)strlen(tmp.szSetting); - moduleNameLen=(int)strlen(tmp.szModule); + settingNameLen = (int)strlen(tmp.szSetting); + moduleNameLen = (int)strlen(tmp.szModule); if ( settingNameLen > 0xFE ) { #ifdef _DEBUG @@ -570,9 +563,9 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) switch( tmp.value.type ) { case DBVT_ASCIIZ: case DBVT_BLOB: case DBVT_UTF8: { int len = ( tmp.value.type != DBVT_BLOB ) ? (int)strlen(tmp.value.pszVal) : tmp.value.cpbVal; - if ( len >= 0xFFFF ) { + if ( len >= 0xFFFF ) { #ifdef _DEBUG - OutputDebugString(_T("WriteContactSetting() writing huge string/blob, rejecting ( >= 0xFFFF ) \n")); + OutputDebugString(_T("WriteContactSetting() writing huge string/blob, rejecting ( >= 0xFFFF ) \n")); #endif return 1; } @@ -583,7 +576,7 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) { char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, settingNameLen); if ( tmp.value.type != DBVT_BLOB ) { - DBVARIANT* pCachedValue = GetCachedValuePtr((HANDLE)wParam, szCachedSettingName, 1); + DBVARIANT* pCachedValue = GetCachedValuePtr(hContact, szCachedSettingName, 1); if ( pCachedValue != NULL ) { BOOL bIsIdentical = FALSE; if ( pCachedValue->type == tmp.value.type ) { @@ -603,53 +596,55 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) } if ( szCachedSettingName[-1] != 0 ) { LeaveCriticalSection(&csDbAccess); - NotifyEventHooks(hSettingChangeEvent,wParam,(LPARAM)&tmp); + NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp); return 0; } } - else GetCachedValuePtr((HANDLE)wParam, szCachedSettingName, -1); + else GetCachedValuePtr(hContact, szCachedSettingName, -1); } - ofsModuleName=GetModuleNameOfs(tmp.szModule); - if(wParam==0) ofsContact=dbHeader.ofsUser; - else ofsContact=wParam; + ofsModuleName = GetModuleNameOfs(tmp.szModule); + if (hContact == 0) + ofsContact = dbHeader.ofsUser; + else + ofsContact = (DWORD)hContact; - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - if(dbc.signature!=DBCONTACT_SIGNATURE) { + 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,ofsContact,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; + ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsContact,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(struct DBContact)); + 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); + ofsBlobPtr = ofsSettingsGroup+offsetof(struct DBContactSettings,blob); + pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); } else { - dbcs=*(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),&bytesRemaining); + 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); + 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)) + if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,tmp.szSetting,settingNameLen)) break; NeedBytes(1); MoveAlong(pBlob[0]+1); @@ -657,18 +652,18 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) MoveAlong(1+GetSettingValueLength(pBlob)); NeedBytes(1); } - if(pBlob[0]) { //setting already existed, and up to end of name is in cache + 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)) { + 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; + nameLen = 1+settingNameLen; + valLen = 1+GetSettingValueLength(pBlob); + ofsSettingToCut = ofsBlobPtr-nameLen; MoveAlong(valLen); NeedBytes(1); while(pBlob[0]) { @@ -678,8 +673,8 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) NeedBytes(1); } DBMoveChunk(ofsSettingToCut,ofsSettingToCut+nameLen+valLen,ofsBlobPtr+1-ofsSettingToCut); - ofsBlobPtr-=nameLen+valLen; - pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); + ofsBlobPtr -= nameLen+valLen; + pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); } else { //replace existing setting at pBlob @@ -696,7 +691,7 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) DBFlush(1); LeaveCriticalSection(&csDbAccess); //notify - NotifyEventHooks(hSettingChangeEvent,wParam,(LPARAM)&tmp); + NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp); return 0; } } @@ -704,51 +699,51 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) //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; + 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)); + 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,oldSize,ofsNew; InvalidateSettingsGroupOfsCacheEntry(ofsSettingsGroup); - bytesRequired+=(DB_SETTINGS_RESIZE_GRANULARITY-(bytesRequired%DB_SETTINGS_RESIZE_GRANULARITY))%DB_SETTINGS_RESIZE_GRANULARITY; + 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; + 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(); - ofsDbcsPrev=dbcsPrev->ofsNext; - dbcsPrev=(struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); + dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); + while(dbcsPrev->ofsNext!= ofsSettingsGroup) { + if (dbcsPrev->ofsNext == 0) DatabaseCorruption(); + ofsDbcsPrev = dbcsPrev->ofsNext; + dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); } } //create the new one - ofsNew=CreateNewSpace(bytesRequired+offsetof(struct DBContactSettings,blob)); + ofsNew = CreateNewSpace(bytesRequired+offsetof(struct DBContactSettings,blob)); //copy across DBMoveChunk(ofsNew,ofsSettingsGroup,bytesRequired+offsetof(struct DBContactSettings,blob)); - oldSize=dbcs.cbBlob; - dbcs.cbBlob=bytesRequired; + oldSize = dbcs.cbBlob; + dbcs.cbBlob = bytesRequired; DBWrite(ofsNew,&dbcs,offsetof(struct DBContactSettings,blob)); - if(ofsDbcsPrev==0) { - dbc.ofsFirstSettings=ofsNew; - DBWrite(ofsContact,&dbc,sizeof(struct DBContact)); + if (ofsDbcsPrev == 0) { + dbc.ofsFirstSettings = ofsNew; + DBWrite(ofsContact,&dbc,sizeof(DBContact)); } else { - dbcsPrev=(struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); - dbcsPrev->ofsNext=ofsNew; + dbcsPrev = (struct DBContactSettings*)DBRead(ofsDbcsPrev,sizeof(struct DBContactSettings),NULL); + dbcsPrev->ofsNext = ofsNew; DBWrite(ofsDbcsPrev,dbcsPrev,offsetof(struct DBContactSettings,blob)); } DeleteSpace(ofsSettingsGroup,oldSize+offsetof(struct DBContactSettings,blob)); - ofsBlobPtr+=ofsNew-ofsSettingsGroup; - ofsSettingsGroup=ofsNew; - pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); + 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); @@ -762,7 +757,7 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) case DBVT_DWORD: DBWrite(ofsBlobPtr,&tmp.value.dVal,4); MoveAlong(4); break; case DBVT_UTF8: case DBVT_ASCIIZ: - { int len=(int)strlen(tmp.value.pszVal); + { int len = (int)strlen(tmp.value.pszVal); DBWrite(ofsBlobPtr,&len,2); DBWrite(ofsBlobPtr+2,tmp.value.pszVal,len); MoveAlong(2+len); @@ -774,63 +769,63 @@ static INT_PTR WriteContactSetting(WPARAM wParam,LPARAM lParam) MoveAlong(2+tmp.value.cpbVal); break; } - { BYTE zero=0; + { BYTE zero = 0; DBWrite(ofsBlobPtr,&zero,1); } //quit DBFlush(1); LeaveCriticalSection(&csDbAccess); //notify - NotifyEventHooks(hSettingChangeEvent, wParam, (LPARAM)&tmp); + NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp); return 0; } -static INT_PTR DeleteContactSetting(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGETSETTING *dbcgs) { - DBCONTACTGETSETTING *dbcgs=(DBCONTACTGETSETTING*)lParam; - struct DBContact *dbc; + DBContact *dbc; DWORD ofsModuleName,ofsSettingsGroup,ofsBlobPtr; struct DBContactSettings dbcs; PBYTE pBlob; - int settingNameLen=(int)strlen(dbcgs->szSetting),bytesRemaining; + int settingNameLen = (int)strlen(dbcgs->szSetting),bytesRemaining; char* szCachedSettingName; - WPARAM saveWparam = wParam; + HANDLE saveContact = hContact; if ((!dbcgs->szModule) || (!dbcgs->szSetting)) return 1; EnterCriticalSection(&csDbAccess); - ofsModuleName=GetModuleNameOfs(dbcgs->szModule); - if(wParam==0) wParam=dbHeader.ofsUser; + ofsModuleName = GetModuleNameOfs(dbcgs->szModule); + if (hContact == 0) + hContact = (HANDLE)dbHeader.ofsUser; - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) { + 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,wParam,ofsModuleName); - if(ofsSettingsGroup==0) { + ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc, (DWORD)hContact,ofsModuleName); + if (ofsSettingsGroup == 0) { LeaveCriticalSection(&csDbAccess); return 1; } - dbc=(struct DBContact*)DBRead(wParam,sizeof(struct DBContact),NULL); - if(dbc->signature!=DBCONTACT_SIGNATURE) { + dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL); + if (dbc->signature!= DBCONTACT_SIGNATURE) { LeaveCriticalSection(&csDbAccess); return 1; } - ofsSettingsGroup=GetSettingsGroupOfsByModuleNameOfs(dbc,wParam,ofsModuleName); - if(ofsSettingsGroup==0) { + ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,(DWORD)hContact,ofsModuleName); + if (ofsSettingsGroup == 0) { LeaveCriticalSection(&csDbAccess); return 1; } - dbcs=*(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),NULL); + dbcs = *(struct DBContactSettings*)DBRead(ofsSettingsGroup,sizeof(struct DBContactSettings),NULL); //find if the setting exists - ofsBlobPtr=ofsSettingsGroup+offsetof(struct DBContactSettings,blob); - pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); + 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)) + if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen)) break; NeedBytes(1); MoveAlong(pBlob[0]+1); @@ -847,9 +842,9 @@ static INT_PTR DeleteContactSetting(WPARAM wParam,LPARAM lParam) DWORD ofsSettingToCut; MoveAlong(1+settingNameLen); NeedBytes(3); - nameLen=1+settingNameLen; - valLen=1+GetSettingValueLength(pBlob); - ofsSettingToCut=ofsBlobPtr-nameLen; + nameLen = 1+settingNameLen; + valLen = 1+GetSettingValueLength(pBlob); + ofsSettingToCut = ofsBlobPtr-nameLen; MoveAlong(valLen); NeedBytes(1); while(pBlob[0]) { @@ -862,25 +857,24 @@ static INT_PTR DeleteContactSetting(WPARAM wParam,LPARAM lParam) } szCachedSettingName = GetCachedSetting(dbcgs->szModule,dbcgs->szSetting,settingNameLen); - GetCachedValuePtr((HANDLE)saveWparam, szCachedSettingName, -1 ); + GetCachedValuePtr(saveContact, szCachedSettingName, -1 ); //quit DBFlush(1); LeaveCriticalSection(&csDbAccess); { //notify DBCONTACTWRITESETTING dbcws; - dbcws.szModule=dbcgs->szModule; - dbcws.szSetting=dbcgs->szSetting; - dbcws.value.type=DBVT_DELETED; - NotifyEventHooks(hSettingChangeEvent,saveWparam,(LPARAM)&dbcws); + dbcws.szModule = dbcgs->szModule; + dbcws.szSetting = dbcgs->szSetting; + dbcws.value.type = DBVT_DELETED; + NotifyEventHooks(hSettingChangeEvent,(WPARAM)saveContact,(LPARAM)&dbcws); } return 0; } -static INT_PTR EnumContactSettings(WPARAM wParam,LPARAM lParam) +STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUMSETTINGS* dbces) { - DBCONTACTENUMSETTINGS *dbces=(DBCONTACTENUMSETTINGS*)lParam; - struct DBContact dbc; + DBContact dbc; struct DBContactSettings dbcs; DWORD ofsModuleName,ofsContact,ofsBlobPtr; int bytesRemaining, result; @@ -892,23 +886,25 @@ static INT_PTR EnumContactSettings(WPARAM wParam,LPARAM lParam) EnterCriticalSection(&csDbAccess); - ofsModuleName=GetModuleNameOfs(dbces->szModule); - if(wParam==0) ofsContact=dbHeader.ofsUser; - else ofsContact=wParam; - dbc=*(struct DBContact*)DBRead(ofsContact,sizeof(struct DBContact),NULL); - if(dbc.signature!=DBCONTACT_SIGNATURE) { + 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,ofsContact,ofsModuleName); + dbces->ofsSettings = GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsContact,ofsModuleName); if (!dbces->ofsSettings) { LeaveCriticalSection(&csDbAccess); return -1; } - dbcs=*(struct DBContactSettings*)DBRead(dbces->ofsSettings,sizeof(struct DBContactSettings),&bytesRemaining); - ofsBlobPtr=dbces->ofsSettings+offsetof(struct DBContactSettings,blob); - pBlob=(PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); - if(pBlob[0]==0) { + dbcs = *(struct DBContactSettings*)DBRead(dbces->ofsSettings,sizeof(struct DBContactSettings),&bytesRemaining); + ofsBlobPtr = dbces->ofsSettings+offsetof(struct DBContactSettings,blob); + pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining); + if (pBlob[0] == 0) { LeaveCriticalSection(&csDbAccess); return -1; } @@ -916,7 +912,7 @@ static INT_PTR EnumContactSettings(WPARAM wParam,LPARAM lParam) while(pBlob[0]) { NeedBytes(1); NeedBytes(1+pBlob[0]); - CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]]=0; + CopyMemory(szSetting,pBlob+1,pBlob[0]); szSetting[pBlob[0]] = 0; result = (dbces->pfnEnumProc)(szSetting,dbces->lParam); MoveAlong(1+pBlob[0]); NeedBytes(3); @@ -927,6 +923,15 @@ static INT_PTR EnumContactSettings(WPARAM wParam,LPARAM lParam) return result; } +STDMETHODIMP_(BOOL) CDdxMmap::EnumResidentSettings(DBMODULEENUMPROC pFunc, void *pParam) +{ + for(int i = 0; i < lSettings.realCount; i++) { + int ret = pFunc((char *)lSettings.items[i], 0, (LPARAM)pParam); + if (ret) return ret; + } + return 0; +} + ///////////////////////////////////////////////////////////////////////////////////////// // // Module initialization procedure @@ -943,15 +948,6 @@ static int stringCompare2( DBCachedGlobalValue* p1, DBCachedGlobalValue* p2 ) int InitSettings(void) { - CreateServiceFunction(MS_DB_CONTACT_GETSETTING,GetContactSetting); - CreateServiceFunction(MS_DB_CONTACT_GETSETTING_STR,GetContactSettingStr); - CreateServiceFunction(MS_DB_CONTACT_GETSETTINGSTATIC,GetContactSettingStatic); - CreateServiceFunction(MS_DB_CONTACT_FREEVARIANT,FreeVariant); - CreateServiceFunction(MS_DB_CONTACT_WRITESETTING,WriteContactSetting); - CreateServiceFunction(MS_DB_CONTACT_DELETESETTING,DeleteContactSetting); - CreateServiceFunction(MS_DB_CONTACT_ENUMSETTINGS,EnumContactSettings); - CreateServiceFunction(MS_DB_SETSETTINGRESIDENT,SetSettingResident); - hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED); mirCp = CallService( MS_LANGPACK_GETCODEPAGE, 0, 0 ); diff --git a/plugins/Db3x/src/encrypt.cpp b/plugins/Db3x/src/encrypt.cpp deleted file mode 100644 index 7426a50cd7..0000000000 --- a/plugins/Db3x/src/encrypt.cpp +++ /dev/null @@ -1,66 +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" - -//VERY VERY VERY BASIC ENCRYPTION FUNCTION - - -void Encrypt(char*msg,BOOL up) -{ - int i; - int jump; - if (up) - { - jump=5; - } - else - { - jump=-5; - } - - for (i=0;msg[i];i++) - { - msg[i]=msg[i]+jump; - } - -} - -static INT_PTR EncodeString(WPARAM wParam,LPARAM lParam) -{ - Encrypt((char*)lParam,TRUE); - return 0; -} - -static INT_PTR DecodeString(WPARAM wParam,LPARAM lParam) -{ - Encrypt((char*)lParam,FALSE); - return 0; -} - -int InitCrypt(void) -{ - CreateServiceFunction(MS_DB_CRYPT_ENCODESTRING,EncodeString); - CreateServiceFunction(MS_DB_CRYPT_DECODESTRING,DecodeString); - return 0; -} diff --git a/plugins/Db3x/src/init.cpp b/plugins/Db3x/src/init.cpp index f9b1269bda..b2b3f1fa72 100644 --- a/plugins/Db3x/src/init.cpp +++ b/plugins/Db3x/src/init.cpp @@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int hLangpack; -HINSTANCE g_hInst=NULL; +HINSTANCE g_hInst = NULL; static PLUGININFOEX pluginInfo = { sizeof(PLUGININFOEX), @@ -40,6 +40,8 @@ static PLUGININFOEX pluginInfo = { {0x1394a3ab, 0x2585, 0x4196, { 0x8f, 0x72, 0xe, 0xae, 0xc2, 0x45, 0xe, 0x11 }} //{1394A3AB-2585-4196-8F72-0EAEC2450E11} }; +CDdxMmap* g_Db = NULL; + ///////////////////////////////////////////////////////////////////////////////////////// static int getCapability( int flag ) @@ -48,43 +50,43 @@ static int getCapability( int flag ) } // returns 0 if the profile is created, EMKPRF* -static int makeDatabase(char * profile, int * error) +static int makeDatabase(TCHAR *profile, int * error) { - HANDLE hFile=CreateFileA(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); + HANDLE hFile = CreateFile(profile, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if ( hFile != INVALID_HANDLE_VALUE ) { CreateDbHeaders(hFile); CloseHandle(hFile); return 0; } - if ( error != NULL ) *error=EMKPRF_CREATEFAILED; + if ( error != NULL ) *error = EMKPRF_CREATEFAILED; return 1; } // returns 0 if the given profile has a valid header -static int grokHeader( char * profile, int * error ) +static int grokHeader(TCHAR *profile, int * error ) { - int rc=1; - int chk=0; + int rc = 1; + int chk = 0; struct DBHeader hdr; HANDLE hFile = INVALID_HANDLE_VALUE; - DWORD dummy=0; + DWORD dummy = 0; - hFile = CreateFileA(profile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + hFile = CreateFile(profile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if ( hFile == INVALID_HANDLE_VALUE ) { - if ( error != NULL ) *error=EGROKPRF_CANTREAD; + 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; + if ( error != NULL) *error = EGROKPRF_CANTREAD; CloseHandle(hFile); return 1; } - chk=CheckDbHeaders(&hdr); + chk = CheckDbHeaders(&hdr); if ( chk == 0 ) { // all the internal tests passed, hurrah - rc=0; - if ( error != NULL ) *error=0; + rc = 0; + if ( error != NULL ) *error = 0; } else { // didn't pass at all, or some did. switch ( chk ) { @@ -97,13 +99,13 @@ static int grokHeader( char * profile, int * error ) case 2: { // header was present, but version information newer - if ( error != NULL ) *error= EGROKPRF_VERNEWER; + if ( error != NULL ) *error = EGROKPRF_VERNEWER; break; } case 3: { // header/version OK, internal data missing - if ( error != NULL ) *error=EGROKPRF_DAMAGED; + if ( error != NULL ) *error = EGROKPRF_DAMAGED; break; } } // switch @@ -113,15 +115,20 @@ static int grokHeader( char * profile, int * error ) } // returns 0 if all the APIs are injected otherwise, 1 -static int LoadDatabase( char* profile ) +static MIDatabase* LoadDatabase(TCHAR *profile) { + if (g_Db) delete g_Db; + g_Db = new CDdxMmap(profile); + // don't need thread notifications - strncpy(szDbPath, profile, sizeof(szDbPath)); + _tcsncpy(szDbPath, profile, SIZEOF(szDbPath)); mir_getLP(&pluginInfo); // inject all APIs and hooks into the core - return LoadDatabaseModule(); + LoadDatabaseModule(); + + return g_Db; } static int UnloadDatabase(int wasLoaded) @@ -131,9 +138,9 @@ static int UnloadDatabase(int wasLoaded) return 0; } -static int getFriendlyName( char * buf, size_t cch, int shortName ) +static int getFriendlyName( TCHAR *buf, size_t cch, int shortName ) { - strncpy(buf,shortName ? "db3x driver" : "db3x database support",cch); + _tcsncpy(buf, shortName ? _T("db3x driver") : _T("db3x database support"), cch); return 0; } -- cgit v1.2.3