summaryrefslogtreecommitdiff
path: root/plugins/Db3x_mmap/src/dbevents.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Db3x_mmap/src/dbevents.cpp')
-rw-r--r--plugins/Db3x_mmap/src/dbevents.cpp161
1 files changed, 88 insertions, 73 deletions
diff --git a/plugins/Db3x_mmap/src/dbevents.cpp b/plugins/Db3x_mmap/src/dbevents.cpp
index a998bc2484..d45c37eaa8 100644
--- a/plugins/Db3x_mmap/src/dbevents.cpp
+++ b/plugins/Db3x_mmap/src/dbevents.cpp
@@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
DWORD GetModuleNameOfs(const char *szName);
char *GetModuleNameByOfs(DWORD ofs);
-static HANDLE hEventDeletedEvent,hEventAddedEvent,hEventFilterAddedEvent;
+static HANDLE hEventDeletedEvent, hEventAddedEvent, hEventFilterAddedEvent;
STDMETHODIMP_(LONG) CDb3Base::GetEventCount(HANDLE hContact)
{
@@ -34,7 +34,7 @@ STDMETHODIMP_(LONG) CDb3Base::GetEventCount(HANDLE hContact)
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
+ DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
return (dbc->signature != DBCONTACT_SIGNATURE) ? -1 : dbc->eventCount;
}
@@ -45,25 +45,37 @@ STDMETHODIMP_(HANDLE) CDb3Base::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
if (NotifyEventHooks(hEventFilterAddedEvent, (WPARAM)hContact, (LPARAM)dbei))
return 0;
+ DBEvent dbe;
+ dbe.signature = DBEVENT_SIGNATURE;
+ dbe.timestamp = dbei->timestamp;
+ dbe.flags = dbei->flags;
+ dbe.eventType = dbei->eventType;
+ dbe.cbBlob = dbei->cbBlob;
+ BYTE *pBlob = dbei->pBlob;
+
+ mir_ptr<BYTE> pCryptBlob;
+ if (m_bEncrypted) {
+ size_t len;
+ BYTE *pResult = m_crypto->encodeBuffer(pBlob, dbe.cbBlob, &len);
+ if (pResult != NULL) {
+ pCryptBlob = pBlob = pResult;
+ dbe.cbBlob = (DWORD)len;
+ dbe.flags |= DBEF_ENCRYPTED;
+ }
+ }
+
bool neednotify;
mir_cslockfull lck(m_csDbAccess);
DWORD ofsContact = (hContact == 0) ? m_dbHeader.ofsUser : (DWORD)hContact;
- DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
+ DBContact dbc = *(DBContact*)DBRead(ofsContact, sizeof(DBContact), NULL);
if (dbc.signature != DBCONTACT_SIGNATURE)
- return 0;
+ return 0;
- DWORD ofsNew = CreateNewSpace(offsetof(DBEvent,blob) + dbei->cbBlob);
- DWORD ofsModuleName = GetModuleNameOfs(dbei->szModule);
+ DWORD ofsNew = CreateNewSpace(offsetof(DBEvent, blob) + dbe.cbBlob);
- DBEvent dbe;
- 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
+ dbe.ofsModuleName = GetModuleNameOfs(dbei->szModule);
+ // find where to put it - sort by timestamp
if (dbc.eventCount == 0) {
dbe.ofsPrev = (DWORD)hContact;
dbe.ofsNext = 0;
@@ -71,17 +83,17 @@ STDMETHODIMP_(HANDLE) CDb3Base::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew;
}
else {
- DBEvent *dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent,sizeof(DBEvent),NULL);
+ DBEvent *dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent, sizeof(DBEvent), NULL);
// Should new event be placed before first event in chain?
- if (dbei->timestamp < dbeTest->timestamp) {
+ if (dbe.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 = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
dbeTest->flags &= ~DBEF_FIRST;
dbeTest->ofsPrev = ofsNew;
- DBWrite(dbe.ofsNext,dbeTest,sizeof(DBEvent));
+ DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
}
else {
// Loop through the chain, starting at the end
@@ -112,22 +124,22 @@ STDMETHODIMP_(HANDLE) CDb3Base::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
}
dbc.eventCount++;
- if (!(dbe.flags&(DBEF_READ|DBEF_SENT))) {
- if (dbe.timestamp<dbc.timestampFirstUnread || dbc.timestampFirstUnread == 0) {
+ if (!(dbe.flags & (DBEF_READ | DBEF_SENT))) {
+ if (dbe.timestamp < dbc.timestampFirstUnread || dbc.timestampFirstUnread == 0) {
dbc.timestampFirstUnread = dbe.timestamp;
dbc.ofsFirstUnreadEvent = ofsNew;
}
- neednotify = TRUE;
+ neednotify = true;
}
else neednotify = m_safetyMode;
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob));
- EncodeDBWrite(ofsNew + offsetof(DBEvent, blob), dbei->pBlob, dbei->cbBlob); // encode
+ DBWrite(ofsContact, &dbc, sizeof(DBContact));
+ DBWrite(ofsNew, &dbe, offsetof(DBEvent, blob));
+ EncodeDBWrite(ofsNew + offsetof(DBEvent, blob), pBlob, dbe.cbBlob);
DBFlush(0);
lck.unlock();
-
- log1("add event @ %08x",ofsNew);
+
+ log1("add event @ %08x", ofsNew);
// Notify only in safe mode or on really new events
if (neednotify)
@@ -148,15 +160,15 @@ STDMETHODIMP_(BOOL) CDb3Base::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
lck.unlock();
log1("delete event @ %08x", hContact);
-
+
//call notifier while outside mutex
- NotifyEventHooks(hEventDeletedEvent,(WPARAM)hContact, (LPARAM)hDbEvent);
+ NotifyEventHooks(hEventDeletedEvent, (WPARAM)hContact, (LPARAM)hDbEvent);
//get back in
lck.lock();
- dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(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)hDbEvent) {
DBEvent *dbeNext = &dbe;
@@ -167,8 +179,8 @@ STDMETHODIMP_(BOOL) CDb3Base::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
break;
}
DWORD ofsThis = dbeNext->ofsNext;
- dbeNext = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- if ( !(dbeNext->flags & (DBEF_READ | DBEF_SENT))) {
+ dbeNext = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
+ if (!(dbeNext->flags & (DBEF_READ | DBEF_SENT))) {
dbc.ofsFirstUnreadEvent = ofsThis;
dbc.timestampFirstUnread = dbeNext->timestamp;
break;
@@ -177,39 +189,39 @@ STDMETHODIMP_(BOOL) CDb3Base::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
}
//get previous and next events in chain and change offsets
- if (dbe.flags&DBEF_FIRST) {
+ if (dbe.flags & DBEF_FIRST) {
if (dbe.ofsNext == 0)
dbc.ofsFirstEvent = dbc.ofsLastEvent = 0;
else {
- DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
+ DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
dbeNext->flags |= DBEF_FIRST;
dbeNext->ofsPrev = dbe.ofsPrev;
- DBWrite(dbe.ofsNext,dbeNext,sizeof(DBEvent));
+ DBWrite(dbe.ofsNext, dbeNext, sizeof(DBEvent));
dbc.ofsFirstEvent = dbe.ofsNext;
}
}
else {
if (dbe.ofsNext == 0) {
- DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev,sizeof(DBEvent),NULL);
+ DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev, sizeof(DBEvent), NULL);
dbePrev->ofsNext = 0;
- DBWrite(dbe.ofsPrev,dbePrev,sizeof(DBEvent));
+ DBWrite(dbe.ofsPrev, dbePrev, sizeof(DBEvent));
dbc.ofsLastEvent = dbe.ofsPrev;
}
else {
- DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev,sizeof(DBEvent),NULL);
+ DBEvent *dbePrev = (DBEvent*)DBRead(dbe.ofsPrev, sizeof(DBEvent), NULL);
dbePrev->ofsNext = dbe.ofsNext;
- DBWrite(dbe.ofsPrev,dbePrev,sizeof(DBEvent));
-
- DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
+ DBWrite(dbe.ofsPrev, dbePrev, sizeof(DBEvent));
+
+ DBEvent *dbeNext = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
dbeNext->ofsPrev = dbe.ofsPrev;
- DBWrite(dbe.ofsNext,dbeNext,sizeof(DBEvent));
+ DBWrite(dbe.ofsNext, dbeNext, sizeof(DBEvent));
}
}
//delete event
- DeleteSpace((DWORD)hDbEvent, offsetof(DBEvent,blob)+dbe.cbBlob);
+ DeleteSpace((DWORD)hDbEvent, offsetof(DBEvent, blob) + dbe.cbBlob);
//decrement event count
dbc.eventCount--;
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
+ DBWrite(ofsContact, &dbc, sizeof(DBContact));
DBFlush(0);
return 0;
}
@@ -230,9 +242,9 @@ STDMETHODIMP_(BOOL) CDb3Base::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei)
}
mir_cslock lck(m_csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+ DBEvent *dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
if (dbe->signature != DBEVENT_SIGNATURE)
- return 1;
+ return 1;
dbei->szModule = GetModuleNameByOfs(dbe->ofsModuleName);
dbei->timestamp = dbe->timestamp;
@@ -241,37 +253,40 @@ STDMETHODIMP_(BOOL) CDb3Base::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei)
int bytesToCopy = (dbei->cbBlob < dbe->cbBlob) ? dbei->cbBlob : dbe->cbBlob;
dbei->cbBlob = dbe->cbBlob;
if (bytesToCopy && dbei->pBlob) {
- for (int i = 0;;i += MAXCACHEDREADSIZE) {
- if (bytesToCopy-i <= MAXCACHEDREADSIZE) {
- DecodeCopyMemory(dbei->pBlob + i, DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob) + i, bytesToCopy - i, NULL), bytesToCopy - i); // decode
- break;
- }
- DecodeCopyMemory(dbei->pBlob + i, DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob) + i, MAXCACHEDREADSIZE, NULL), MAXCACHEDREADSIZE); // decode
+ BYTE *pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob), bytesToCopy, NULL);
+ if (dbe->flags & DBEF_ENCRYPTED) {
+ dbei->flags &= ~DBEF_ENCRYPTED;
+ size_t len;
+ BYTE* pBlob = (BYTE*)m_crypto->decodeBuffer(pSrc, dbe->cbBlob, &len);
+ if (pBlob == NULL)
+ return 1;
+
+ memcpy(dbei->pBlob, pBlob, bytesToCopy);
+ if (bytesToCopy > len)
+ memset(dbei->pBlob + len, 0, bytesToCopy - len);
+ mir_free(pBlob);
}
+ else DecodeCopyMemory(dbei->pBlob, pSrc, bytesToCopy);
}
return 0;
}
STDMETHODIMP_(BOOL) CDb3Base::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
{
- DBEvent *dbe;
- DBContact dbc;
- DWORD ofsThis;
-
mir_cslock lck(m_csDbAccess);
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
- dbc = *(DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
- dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
+ 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)
- return -1;
+ return -1;
if ((dbe->flags & DBEF_READ) || (dbe->flags & DBEF_SENT))
return (INT_PTR)dbe->flags;
//log1("mark read @ %08x", hContact);
dbe->flags |= DBEF_READ;
- DBWrite((DWORD)hDbEvent,dbe,sizeof(DBEvent));
+ DBWrite((DWORD)hDbEvent, dbe, sizeof(DBEvent));
BOOL ret = dbe->flags;
if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) {
for (;;) {
@@ -280,9 +295,9 @@ STDMETHODIMP_(BOOL) CDb3Base::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
dbc.timestampFirstUnread = 0;
break;
}
- ofsThis = dbe->ofsNext;
- dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- if ( !(dbe->flags & (DBEF_READ | DBEF_SENT))) {
+ DWORD 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;
@@ -297,13 +312,13 @@ STDMETHODIMP_(BOOL) CDb3Base::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
STDMETHODIMP_(HANDLE) CDb3Base::GetEventContact(HANDLE hDbEvent)
{
mir_cslock lck(m_csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+ DBEvent *dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
if (dbe->signature != DBEVENT_SIGNATURE)
- return (HANDLE)-1;
+ return (HANDLE)-1;
while (!(dbe->flags & DBEF_FIRST))
- dbe = (DBEvent*)DBRead(dbe->ofsPrev,sizeof(DBEvent),NULL);
-
+ dbe = (DBEvent*)DBRead(dbe->ofsPrev, sizeof(DBEvent), NULL);
+
return (HANDLE)dbe->ofsPrev;
}
@@ -312,7 +327,7 @@ STDMETHODIMP_(HANDLE) CDb3Base::FindFirstEvent(HANDLE hContact)
mir_cslock lck(m_csDbAccess);
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
-
+
DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
return (dbc->signature != DBCONTACT_SIGNATURE) ? 0 : (HANDLE)dbc->ofsFirstEvent;
}
@@ -323,7 +338,7 @@ STDMETHODIMP_(HANDLE) CDb3Base::FindFirstUnreadEvent(HANDLE hContact)
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
+ DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
return (dbc->signature != DBCONTACT_SIGNATURE) ? 0 : (HANDLE)dbc->ofsFirstUnreadEvent;
}
@@ -333,21 +348,21 @@ STDMETHODIMP_(HANDLE) CDb3Base::FindLastEvent(HANDLE hContact)
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
- DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
+ DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
return (dbc->signature != DBCONTACT_SIGNATURE) ? 0 : (HANDLE)dbc->ofsLastEvent;
}
STDMETHODIMP_(HANDLE) CDb3Base::FindNextEvent(HANDLE hDbEvent)
{
mir_cslock lck(m_csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+ DBEvent *dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
return (dbe->signature != DBEVENT_SIGNATURE) ? 0 : (HANDLE)dbe->ofsNext;
}
STDMETHODIMP_(HANDLE) CDb3Base::FindPrevEvent(HANDLE hDbEvent)
{
mir_cslock lck(m_csDbAccess);
- DBEvent *dbe = (DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+ DBEvent *dbe = (DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
if (dbe->signature != DBEVENT_SIGNATURE) return 0;
return (dbe->flags & DBEF_FIRST) ? 0 : (HANDLE)dbe->ofsPrev;
}