From f6d53c90114069402b549aa7c0bc79044aace51d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 6 Jan 2014 16:27:44 +0000 Subject: huh, finally: total database encryption, including histories git-svn-id: http://svn.miranda-ng.org/main/trunk@7522 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Db3x_mmap/src/dbtool/aggressive.cpp | 14 +-- plugins/Db3x_mmap/src/dbtool/contactchain.cpp | 38 +++--- plugins/Db3x_mmap/src/dbtool/disk.cpp | 25 ++-- plugins/Db3x_mmap/src/dbtool/eventchain.cpp | 153 ++++++++++++------------- plugins/Db3x_mmap/src/dbtool/finaltasks.cpp | 5 +- plugins/Db3x_mmap/src/dbtool/initialchecks.cpp | 22 ++-- plugins/Db3x_mmap/src/dbtool/modulechain.cpp | 139 +++++++++++----------- plugins/Db3x_mmap/src/dbtool/settingschain.cpp | 40 ++++--- plugins/Db3x_mmap/src/dbtool/user.cpp | 25 ++-- 9 files changed, 232 insertions(+), 229 deletions(-) (limited to 'plugins/Db3x_mmap/src/dbtool') diff --git a/plugins/Db3x_mmap/src/dbtool/aggressive.cpp b/plugins/Db3x_mmap/src/dbtool/aggressive.cpp index 0a47754920..92266f37b3 100644 --- a/plugins/Db3x_mmap/src/dbtool/aggressive.cpp +++ b/plugins/Db3x_mmap/src/dbtool/aggressive.cpp @@ -26,27 +26,27 @@ int CDb3Base::WorkAggressive(int firstTime) if (!cb->bAggressive) return ERROR_NO_MORE_ITEMS; - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Performing aggressive pass")); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Performing aggressive pass")); ofsAggrCur = 0; cb->spaceProcessed = 0; } - - int blockBytes = min(BLOCKSIZE+3,(int)(sourceFileSize-ofsAggrCur)); + + int blockBytes = min(BLOCKSIZE + 3, (int)(sourceFileSize - ofsAggrCur)); if (blockBytes <= 0) return ERROR_NO_MORE_ITEMS; BYTE *buf = m_pDbCache + ofsAggrCur; blockBytes -= 3; - for (int i=0; i < blockBytes; i++) { + for (int i = 0; i < blockBytes; i++) { if (buf[i]) { if ((*(PDWORD)&buf[i] & 0x00FFFFFF) != 0xDECADE) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Aggressive: random junk at %08X: skipping"),ofsAggrCur+i); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Aggressive: random junk at %08X: skipping"), ofsAggrCur + i); else //TODO: give user the option of placing manually - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Aggressive: unlinked data at %08X: can't automatically place"),ofsAggrCur+i); + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Aggressive: unlinked data at %08X: can't automatically place"), ofsAggrCur + i); for (; i < blockBytes; i++) - if (buf[i] == 0) {i--; break;} + if (buf[i] == 0) { i--; break; } } } ofsAggrCur += BLOCKSIZE; diff --git a/plugins/Db3x_mmap/src/dbtool/contactchain.cpp b/plugins/Db3x_mmap/src/dbtool/contactchain.cpp index 80c4b62168..d1aa5a933e 100644 --- a/plugins/Db3x_mmap/src/dbtool/contactchain.cpp +++ b/plugins/Db3x_mmap/src/dbtool/contactchain.cpp @@ -19,9 +19,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "..\commonheaders.h" -static DWORD ofsThisContact,ofsDestPrevContact; +static DWORD ofsThisContact, ofsDestPrevContact; static DWORD contactCount; -static DWORD ofsDestThis,ofsNextContact; +static DWORD ofsDestThis, ofsNextContact; static int phase; static DBContact dbc; @@ -31,7 +31,7 @@ int CDb3Base::WorkContactChain(int firstTime) int ret; if (firstTime) { - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Processing contact chain")); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Processing contact chain")); ofsDestPrevContact = 0; ofsThisContact = m_dbHeader.ofsFirstContact; contactCount = 0; @@ -39,54 +39,58 @@ int CDb3Base::WorkContactChain(int firstTime) phase = 0; } - switch(phase) { + switch (phase) { case 0: if (ofsThisContact == 0) { LBL_FinishUp: if (contactCount != m_dbHeader.contactCount) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Contact count marked wrongly: correcting")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Contact count marked wrongly: correcting")); m_dbHeader.contactCount = contactCount; return ERROR_NO_MORE_ITEMS; } - if (!SignatureValid(ofsThisContact,DBCONTACT_SIGNATURE)) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Contact chain corrupted, further entries ignored")); + if (!SignatureValid(ofsThisContact, DBCONTACT_SIGNATURE)) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Contact chain corrupted, further entries ignored")); goto LBL_FinishUp; } - if (ReadSegment(ofsThisContact,&dbc,sizeof(dbc)) != ERROR_SUCCESS) + if (ReadSegment(ofsThisContact, &dbc, sizeof(dbc)) != ERROR_SUCCESS) goto LBL_FinishUp; ofsNextContact = dbc.ofsNext; dbc.ofsNext = 0; if (!cb->bCheckOnly) { - if ((ofsDestThis = WriteSegment(WSOFS_END,&dbc,sizeof(dbc))) == WS_ERROR) + if ((ofsDestThis = WriteSegment(WSOFS_END, &dbc, sizeof(dbc))) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; - if (ofsDestPrevContact) - WriteSegment(ofsDestPrevContact+offsetof(DBContact,ofsNext),&ofsDestThis,sizeof(DWORD)); - else + if (ofsDestPrevContact) + WriteSegment(ofsDestPrevContact + offsetof(DBContact, ofsNext), &ofsDestThis, sizeof(DWORD)); + else m_dbHeader.ofsFirstContact = ofsDestThis; - } else - ofsDestThis = ofsThisContact; // needed in event chain worker + } + else ofsDestThis = ofsThisContact; // needed in event chain worker + contactCount++; phase++; first = 1; + //fall thru case 1: - ret = WorkSettingsChain(ofsDestThis,&dbc,first); + ret = WorkSettingsChain(ofsDestThis, &dbc, first); if (ret == ERROR_NO_MORE_ITEMS) { phase++; first = 1; } else if (ret) return ret; else break; + //fall thru case 2: - ret = WorkEventChain(ofsDestThis,&dbc,first); + ret = WorkEventChain(ofsDestThis, &dbc, first); if (ret == ERROR_NO_MORE_ITEMS) { phase++; first = 1; } else if (ret) return ret; else break; + //fall thru case 3: - if (WriteSegment(ofsDestThis,&dbc,sizeof(DBContact)) == WS_ERROR) + if (WriteSegment(ofsDestThis, &dbc, sizeof(DBContact)) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; ofsDestPrevContact = ofsDestThis; ofsThisContact = ofsNextContact; diff --git a/plugins/Db3x_mmap/src/dbtool/disk.cpp b/plugins/Db3x_mmap/src/dbtool/disk.cpp index a5178c8ea2..d22afb70d9 100644 --- a/plugins/Db3x_mmap/src/dbtool/disk.cpp +++ b/plugins/Db3x_mmap/src/dbtool/disk.cpp @@ -21,28 +21,24 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. int CDb3Base::SignatureValid(DWORD ofs, DWORD signature) { - DWORD sig; - - if (ofs+sizeof(sig) >= sourceFileSize) { + if (ofs + sizeof(DWORD) >= sourceFileSize) { cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Invalid offset found (database truncated?)")); return 0; } - sig = *(DWORD*)(m_pDbCache+ofs); - + DWORD sig = *(DWORD*)(m_pDbCache + ofs); return sig == signature; } int CDb3Base::PeekSegment(DWORD ofs, PVOID buf, int cbBytes) { - DWORD bytesRead; - if (ofs >= sourceFileSize) { cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Invalid offset found")); return ERROR_SEEK; } - if (ofs+cbBytes > sourceFileSize) + DWORD bytesRead; + if (ofs + cbBytes > sourceFileSize) bytesRead = sourceFileSize - ofs; else bytesRead = cbBytes; @@ -52,7 +48,7 @@ int CDb3Base::PeekSegment(DWORD ofs, PVOID buf, int cbBytes) return ERROR_READ_FAULT; } - CopyMemory(buf, m_pDbCache+ofs, bytesRead); + CopyMemory(buf, m_pDbCache + ofs, bytesRead); if ((int)bytesReadbAggressive) { - if (ofs+cbBytes > sourceFileSize) { + if (ofs + cbBytes > sourceFileSize) { cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Can't write to working file, aggressive mode may be too aggressive now")); - ZeroMemory(m_pDbCache+ofs, sourceFileSize-ofs); + ZeroMemory(m_pDbCache + ofs, sourceFileSize - ofs); } - else - ZeroMemory(m_pDbCache+ofs, cbBytes); + else ZeroMemory(m_pDbCache + ofs, cbBytes); } cb->spaceProcessed += cbBytes; return ERROR_SUCCESS; @@ -85,7 +80,7 @@ DWORD CDb3Base::WriteSegment(DWORD ofs, PVOID buf, int cbBytes) } SetFilePointer(cb->hOutFile, ofs, NULL, FILE_BEGIN); WriteFile(cb->hOutFile, buf, cbBytes, &bytesWritten, NULL); - if ((int)bytesWrittenpfnAddLogMessage(STATUS_FATAL, TranslateT("Can't write to output file - disk full? (%u)"), GetLastError()); return WS_ERROR; } @@ -101,7 +96,7 @@ int CDb3Base::ReadWrittenSegment(DWORD ofs, PVOID buf, int cbBytes) SetFilePointer(cb->hOutFile, ofs, NULL, FILE_BEGIN); ReadFile(cb->hOutFile, buf, cbBytes, &bytesRead, NULL); - if ((int)bytesReadblob) + 1, msglenW = 0; - if (msglen != (int) dbei->cbBlob) { + if (msglen != (int)dbei->cbBlob) { int i, count = ((dbei->cbBlob - msglen) / sizeof(WCHAR)); - WCHAR* p = (WCHAR*)&dbei->blob[ msglen ]; - for ( i = 0; i < count; i++) { + WCHAR* p = (WCHAR*)&dbei->blob[msglen]; + for (i = 0; i < count; i++) { if (p[i] == 0) { msglenW = i; break; @@ -53,34 +55,35 @@ void CDb3Base::ConvertOldEvent(DBEvent*& dbei) } if (msglenW > 0 && msglenW <= msglen) { - char* utf8str = Utf8EncodeW((WCHAR*)&dbei->blob[ msglen ]); + char* utf8str = Utf8EncodeW((WCHAR*)&dbei->blob[msglen]); if (utf8str == NULL) return; - dbei->cbBlob = (DWORD)strlen(utf8str)+1; + dbei->cbBlob = (DWORD)strlen(utf8str) + 1; dbei->flags |= DBEF_UTF; - if (offsetof(DBEvent,blob)+dbei->cbBlob > memsize) { - memsize = offsetof(DBEvent,blob)+dbei->cbBlob; + if (offsetof(DBEvent, blob) + dbei->cbBlob > memsize) { + memsize = offsetof(DBEvent, blob) + dbei->cbBlob; memblock = (DBEvent*)realloc(memblock, memsize); dbei = memblock; } memcpy(&dbei->blob, utf8str, dbei->cbBlob); mir_free(utf8str); -} } + } +} -void CDb3Base::WriteOfsNextToPrevious(DWORD ofsPrev,DBContact *dbc,DWORD ofsNext) +void CDb3Base::WriteOfsNextToPrevious(DWORD ofsPrev, DBContact *dbc, DWORD ofsNext) { if (ofsPrev) - WriteSegment(ofsPrev+offsetof(DBEvent,ofsNext),&ofsNext,sizeof(DWORD)); + WriteSegment(ofsPrev + offsetof(DBEvent, ofsNext), &ofsNext, sizeof(DWORD)); else dbc->ofsFirstEvent = ofsNext; } -void CDb3Base::FinishUp(DWORD ofsLast,DBContact *dbc) +void CDb3Base::FinishUp(DWORD ofsLast, DBContact *dbc) { - WriteOfsNextToPrevious(ofsLast,dbc,0); + WriteOfsNextToPrevious(ofsLast, dbc, 0); if (eventCount != dbc->eventCount) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event count marked wrongly: correcting")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event count marked wrongly: correcting")); dbc->eventCount = eventCount; dbc->ofsLastEvent = ofsLast; if (cb->bMarkRead) { @@ -100,7 +103,7 @@ void CDb3Base::FinishUp(DWORD ofsLast,DBContact *dbc) DWORD CDb3Base::WriteEvent(DBEvent *dbe) { - DWORD ofs = WriteSegment(WSOFS_END, dbe, offsetof(DBEvent,blob)+dbe->cbBlob); + DWORD ofs = WriteSegment(WSOFS_END, dbe, offsetof(DBEvent, blob) + dbe->cbBlob); if (ofs == WS_ERROR) { free(memblock); memblock = NULL; @@ -110,9 +113,9 @@ DWORD CDb3Base::WriteEvent(DBEvent *dbe) return ofs; } -int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) +int CDb3Base::WorkEventChain(DWORD ofsContact, DBContact *dbc, int firstTime) { - DBEvent *dbeNew,dbeOld; + DBEvent *dbeNew, dbeOld; DBEvent *dbePrev = NULL; DWORD ofsDestThis; int isUnread = 0; @@ -133,88 +136,90 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) dbc->ofsFirstUnreadEvent = 0; dbc->timestampFirstUnread = 0; return ERROR_NO_MORE_ITEMS; - } } + } + } if (ofsThisEvent == 0) { - FinishUp(ofsDestPrevEvent,dbc); + FinishUp(ofsDestPrevEvent, dbc); return ERROR_NO_MORE_ITEMS; } - if ( !SignatureValid(ofsThisEvent,DBEVENT_SIGNATURE)) { + if (!SignatureValid(ofsThisEvent, DBEVENT_SIGNATURE)) { DWORD ofsNew = 0; DWORD ofsTmp = dbc->ofsLastEvent; if (!backLookup && ofsTmp) { backLookup = 1; - while ( SignatureValid(ofsTmp,DBEVENT_SIGNATURE)) { - if (PeekSegment(ofsTmp,&dbeOld,sizeof(dbeOld)) != ERROR_SUCCESS) + while (SignatureValid(ofsTmp, DBEVENT_SIGNATURE)) { + if (PeekSegment(ofsTmp, &dbeOld, sizeof(dbeOld)) != ERROR_SUCCESS) break; ofsNew = ofsTmp; ofsTmp = dbeOld.ofsPrev; } } if (ofsNew) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event chain corrupted, trying to recover...")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event chain corrupted, trying to recover...")); ofsThisEvent = ofsNew; } else { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Event chain corrupted, further entries ignored")); - FinishUp(ofsDestPrevEvent,dbc); + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Event chain corrupted, further entries ignored")); + FinishUp(ofsDestPrevEvent, dbc); return ERROR_NO_MORE_ITEMS; } } - if (PeekSegment(ofsThisEvent,&dbeOld,sizeof(dbeOld)) != ERROR_SUCCESS) { - FinishUp(ofsDestPrevEvent,dbc); + if (PeekSegment(ofsThisEvent, &dbeOld, sizeof(dbeOld)) != ERROR_SUCCESS) { + FinishUp(ofsDestPrevEvent, dbc); return ERROR_NO_MORE_ITEMS; } if (firstTime) { - if (!(dbeOld.flags&DBEF_FIRST)) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("First event not marked as such: correcting")); + if (!(dbeOld.flags & DBEF_FIRST)) { + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("First event not marked as such: correcting")); dbeOld.flags |= DBEF_FIRST; } dbeOld.ofsPrev = ofsContact; lastTimestamp = dbeOld.timestamp; } - else if (dbeOld.flags&DBEF_FIRST) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event marked as first which is not: correcting")); + else if (dbeOld.flags & DBEF_FIRST) { + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event marked as first which is not: correcting")); dbeOld.flags &= ~DBEF_FIRST; } - if (dbeOld.flags & ~(DBEF_FIRST | DBEF_READ | DBEF_SENT | DBEF_RTL | DBEF_UTF)) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Extra flags found in event: removing")); - dbeOld.flags &= (DBEF_FIRST | DBEF_READ | DBEF_SENT | DBEF_RTL | DBEF_UTF); + if (dbeOld.flags & ~DBEF_ALL) { + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Extra flags found in event: removing")); + dbeOld.flags &= DBEF_ALL; } if (!(dbeOld.flags & (DBEF_READ | DBEF_SENT))) { if (cb->bMarkRead) dbeOld.flags |= DBEF_READ; else if (ofsFirstUnread == 0) { if (dbc->ofsFirstUnreadEvent != ofsThisEvent || dbc->timestampFirstUnread != dbeOld.timestamp) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("First unread event marked wrong: fixing")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("First unread event marked wrong: fixing")); isUnread = 1; - } } + } + } - if (dbeOld.cbBlob > 1024*1024 || dbeOld.cbBlob == 0) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Infeasibly large event blob: skipping")); + if (dbeOld.cbBlob > 1024 * 1024 || dbeOld.cbBlob == 0) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Infeasibly large event blob: skipping")); ofsThisEvent = dbeOld.ofsNext; return ERROR_SUCCESS; } if (dbePrevEvent && dbeOld.timestamp == lastTimestamp) { - int len = offsetof(DBEvent,blob)+dbePrevEvent->cbBlob; + int len = offsetof(DBEvent, blob) + dbePrevEvent->cbBlob; dbePrev = (DBEvent*)malloc(len); memcpy(dbePrev, dbePrevEvent, len); } - if (offsetof(DBEvent,blob)+dbeOld.cbBlob > memsize) { - memsize = offsetof(DBEvent,blob)+dbeOld.cbBlob; + if (offsetof(DBEvent, blob) + dbeOld.cbBlob > memsize) { + memsize = offsetof(DBEvent, blob) + dbeOld.cbBlob; memblock = (DBEvent*)realloc(memblock, memsize); } dbeNew = memblock; - if (ReadSegment(ofsThisEvent,dbeNew,offsetof(DBEvent,blob)+dbeOld.cbBlob) != ERROR_SUCCESS) { - FinishUp(ofsDestPrevEvent,dbc); + if (ReadSegment(ofsThisEvent, dbeNew, offsetof(DBEvent, blob) + dbeOld.cbBlob) != ERROR_SUCCESS) { + FinishUp(ofsDestPrevEvent, dbc); return ERROR_NO_MORE_ITEMS; } @@ -224,7 +229,7 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) } if (!firstTime && dbeOld.ofsPrev != ofsPrevEvent) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event not backlinked correctly: fixing")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event not backlinked correctly: fixing")); dbeNew->flags = dbeOld.flags; dbeNew->ofsPrev = ofsDestPrevEvent; @@ -244,12 +249,12 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) if (dbePrev) { if (dbePrev->cbBlob == dbeNew->cbBlob && - dbePrev->ofsModuleName == dbeNew->ofsModuleName && - dbePrev->eventType == dbeNew->eventType && - (dbePrev->flags & DBEF_SENT) == (dbeNew->flags & DBEF_SENT) && + dbePrev->ofsModuleName == dbeNew->ofsModuleName && + dbePrev->eventType == dbeNew->eventType && + (dbePrev->flags & DBEF_SENT) == (dbeNew->flags & DBEF_SENT) && !memcmp(dbePrev->blob, dbeNew->blob, dbeNew->cbBlob) ) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Duplicate event was found: skipping")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Duplicate event was found: skipping")); if (dbc->eventCount) dbc->eventCount--; free(dbePrev); @@ -260,37 +265,31 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) } free(dbePrev); } - else if (!firstTime && dbeNew->timestamp < lastTimestamp) - { + else if (!firstTime && dbeNew->timestamp < lastTimestamp) { DWORD found = 0; DBEvent dbeTmp; DWORD ofsTmp; - if (cb->bCheckOnly) - { - if (!cb->bAggressive) - { + if (cb->bCheckOnly) { + if (!cb->bAggressive) { ofsTmp = dbeOld.ofsPrev; - while(PeekSegment(ofsTmp,&dbeTmp,sizeof(dbeTmp)) == ERROR_SUCCESS) - { + while (PeekSegment(ofsTmp, &dbeTmp, sizeof(dbeTmp)) == ERROR_SUCCESS) { if (dbeTmp.ofsPrev == ofsContact) { - found = 1; - break; + found = 1; + break; } if (dbeTmp.timestamp < dbeNew->timestamp) { - found = 2; - break; + found = 2; + break; } ofsTmp = dbeTmp.ofsPrev; } } - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event position in chain is not correct")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event position in chain is not correct")); } - else - { + else { ofsTmp = ofsDestPrevEvent; - while(ReadWrittenSegment(ofsTmp,&dbeTmp,sizeof(dbeTmp)) == ERROR_SUCCESS) - { + while (ReadWrittenSegment(ofsTmp, &dbeTmp, sizeof(dbeTmp)) == ERROR_SUCCESS) { if (dbeTmp.ofsPrev == ofsContact) { found = 1; break; @@ -302,9 +301,9 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) ofsTmp = dbeTmp.ofsPrev; } if (found) - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event position in chain is not correct: fixing")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event position in chain is not correct: fixing")); else - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Event position in chain is not correct: unable to fix")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Event position in chain is not correct: unable to fix")); } // insert before FIRST @@ -317,16 +316,16 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) if (!ofsDestThis) return ERROR_HANDLE_DISK_FULL; - if (isUnread && timestampFirstUnread >= dbeNew->timestamp) { + if (isUnread && timestampFirstUnread >= dbeNew->timestamp) { ofsFirstUnread = ofsDestThis; timestampFirstUnread = dbeNew->timestamp; } // fix first event - WriteOfsNextToPrevious(0,dbc,ofsDestThis); + WriteOfsNextToPrevious(0, dbc, ofsDestThis); // fix next event - WriteSegment(dbeNew->ofsNext+offsetof(DBEvent,ofsPrev),&ofsDestThis,sizeof(DWORD)); - dbeTmp.flags &= ~DBEF_FIRST; - WriteSegment(dbeNew->ofsNext+offsetof(DBEvent,flags),&dbeTmp.flags,sizeof(DWORD)); + WriteSegment(dbeNew->ofsNext + offsetof(DBEvent, ofsPrev), &ofsDestThis, sizeof(DWORD)); + dbeTmp.flags &= ~DBEF_FIRST; + WriteSegment(dbeNew->ofsNext + offsetof(DBEvent, flags), &dbeTmp.flags, sizeof(DWORD)); } else if (found == 2 && !cb->bCheckOnly) { @@ -337,14 +336,14 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) if (!ofsDestThis) return ERROR_HANDLE_DISK_FULL; - if (isUnread && timestampFirstUnread >= dbeNew->timestamp) { + if (isUnread && timestampFirstUnread >= dbeNew->timestamp) { ofsFirstUnread = ofsDestThis; timestampFirstUnread = dbeNew->timestamp; } // fix previous event - WriteOfsNextToPrevious(dbeNew->ofsPrev,dbc,ofsDestThis); + WriteOfsNextToPrevious(dbeNew->ofsPrev, dbc, ofsDestThis); // fix next event - WriteSegment(dbeNew->ofsNext+offsetof(DBEvent,ofsPrev),&ofsDestThis,sizeof(DWORD)); + WriteSegment(dbeNew->ofsNext + offsetof(DBEvent, ofsPrev), &ofsDestThis, sizeof(DWORD)); } if (found) { @@ -369,7 +368,7 @@ int CDb3Base::WorkEventChain(DWORD ofsContact,DBContact *dbc,int firstTime) } eventCount++; - WriteOfsNextToPrevious(ofsDestPrevEvent,dbc,ofsDestThis); + WriteOfsNextToPrevious(ofsDestPrevEvent, dbc, ofsDestThis); ofsDestPrevEvent = ofsDestThis; ofsPrevEvent = ofsThisEvent; diff --git a/plugins/Db3x_mmap/src/dbtool/finaltasks.cpp b/plugins/Db3x_mmap/src/dbtool/finaltasks.cpp index a307c0b9fb..a7f9c8ef46 100644 --- a/plugins/Db3x_mmap/src/dbtool/finaltasks.cpp +++ b/plugins/Db3x_mmap/src/dbtool/finaltasks.cpp @@ -16,14 +16,15 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + #include "..\commonheaders.h" int CDb3Base::WorkFinalTasks(int firstTime) { FreeModuleChain(); - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Processing final tasks")); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Processing final tasks")); m_dbHeader.slackSpace = 0; - if (WriteSegment(0,&m_dbHeader,sizeof(m_dbHeader)) == WS_ERROR) + if (WriteSegment(0, &m_dbHeader, sizeof(m_dbHeader)) == WS_ERROR) return ERROR_WRITE_FAULT; if (m_hDbFile) { diff --git a/plugins/Db3x_mmap/src/dbtool/initialchecks.cpp b/plugins/Db3x_mmap/src/dbtool/initialchecks.cpp index 6258f59c16..2d9dfae04c 100644 --- a/plugins/Db3x_mmap/src/dbtool/initialchecks.cpp +++ b/plugins/Db3x_mmap/src/dbtool/initialchecks.cpp @@ -16,18 +16,20 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + #include "..\commonheaders.h" int CDb3Base::WorkInitialCheckHeaders() { if (memcmp(m_dbHeader.signature, &dbSignatureU, sizeof(m_dbHeader.signature)) && + memcmp(m_dbHeader.signature, &dbSignatureE, sizeof(m_dbHeader.signature)) && memcmp(m_dbHeader.signature, &dbSignatureIM, sizeof(m_dbHeader.signature))) { - cb->pfnAddLogMessage(STATUS_FATAL,TranslateT("Database signature is corrupted, automatic repair is impossible")); + cb->pfnAddLogMessage(STATUS_FATAL, TranslateT("Database signature is corrupted, automatic repair is impossible")); return ERROR_BAD_FORMAT; } if (m_dbHeader.version != DB_OLD_VERSION && m_dbHeader.version != DB_THIS_VERSION) { - cb->pfnAddLogMessage(STATUS_FATAL,TranslateT("Database is marked as belonging to an unknown version of Miranda")); + cb->pfnAddLogMessage(STATUS_FATAL, TranslateT("Database is marked as belonging to an unknown version of Miranda")); return ERROR_BAD_FORMAT; } return ERROR_SUCCESS; @@ -35,10 +37,10 @@ int CDb3Base::WorkInitialCheckHeaders() int CDb3Base::WorkInitialChecks(int firstTime) { - sourceFileSize = GetFileSize(m_hDbFile,NULL); + sourceFileSize = GetFileSize(m_hDbFile, NULL); if (sourceFileSize == 0) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("Database is newly created and has no data to process")); - cb->pfnAddLogMessage(STATUS_SUCCESS,TranslateT("Processing completed successfully")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Database is newly created and has no data to process")); + cb->pfnAddLogMessage(STATUS_SUCCESS, TranslateT("Processing completed successfully")); return ERROR_INVALID_DATA; } @@ -50,21 +52,21 @@ int CDb3Base::WorkInitialChecks(int firstTime) if (m_hMap) m_pDbCache = (BYTE*)MapViewOfFile(m_hMap, cb->bAggressive ? FILE_MAP_COPY : FILE_MAP_READ, 0, 0, 0); else { - cb->pfnAddLogMessage(STATUS_FATAL,TranslateT("Can't create file mapping (%u)"),GetLastError()); + cb->pfnAddLogMessage(STATUS_FATAL, TranslateT("Can't create file mapping (%u)"), GetLastError()); return ERROR_ACCESS_DENIED; } if (!m_pDbCache) { - cb->pfnAddLogMessage(STATUS_FATAL,TranslateT("Can't create map view of file (%u)"),GetLastError()); + cb->pfnAddLogMessage(STATUS_FATAL, TranslateT("Can't create map view of file (%u)"), GetLastError()); return ERROR_ACCESS_DENIED; } - if (ReadSegment(0,&m_dbHeader,sizeof(m_dbHeader)) != ERROR_SUCCESS) + if (ReadSegment(0, &m_dbHeader, sizeof(m_dbHeader)) != ERROR_SUCCESS) return ERROR_READ_FAULT; - if (WriteSegment(0,&m_dbHeader,sizeof(m_dbHeader)) == WS_ERROR) + if (WriteSegment(0, &m_dbHeader, sizeof(m_dbHeader)) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; - cb->spaceUsed = m_dbHeader.ofsFileEnd-m_dbHeader.slackSpace; + cb->spaceUsed = m_dbHeader.ofsFileEnd - m_dbHeader.slackSpace; m_dbHeader.ofsFileEnd = sizeof(m_dbHeader); return ERROR_NO_MORE_ITEMS; } diff --git a/plugins/Db3x_mmap/src/dbtool/modulechain.cpp b/plugins/Db3x_mmap/src/dbtool/modulechain.cpp index 0326166d7f..c846455474 100644 --- a/plugins/Db3x_mmap/src/dbtool/modulechain.cpp +++ b/plugins/Db3x_mmap/src/dbtool/modulechain.cpp @@ -32,10 +32,10 @@ static int last_mod = 0; int CDb3Base::WorkModuleChain(int firstTime) { - DBModuleName moduleName,*newModName; + DBModuleName moduleName, *newModName; if (firstTime) { - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Processing module name chain")); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Processing module name chain")); modChainCount = 0; last_mod = 0; free(modChain); @@ -43,93 +43,90 @@ int CDb3Base::WorkModuleChain(int firstTime) phase = 0; ofsCurrent = m_dbHeader.ofsFirstModuleName; } - switch(phase) { - case 0: - if (ofsCurrent == 0) { - phase++; - return ERROR_SUCCESS; - } - if (!SignatureValid(ofsCurrent,DBMODULENAME_SIGNATURE)) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Module chain corrupted, further entries ignored")); - phase++; - return ERROR_SUCCESS; - } - if (PeekSegment(ofsCurrent,&moduleName,offsetof(DBModuleName,name)) != ERROR_SUCCESS) { - phase++; - return ERROR_SUCCESS; - } - modChain = (ModChainEntry*)realloc(modChain,sizeof(ModChainEntry)*++modChainCount); - - modChain[modChainCount-1].ofsOld = ofsCurrent; - modChain[modChainCount-1].size = offsetof(DBModuleName,name)+moduleName.cbName; - modChain[modChainCount-1].ofsNew = 0; - - if (moduleName.cbName) - PeekSegment(ofsCurrent+offsetof(DBModuleName,name),&modChain[modChainCount-1].name,moduleName.cbName); - modChain[modChainCount-1].name[moduleName.cbName] = 0; - ofsCurrent = moduleName.ofsNext; - break; - case 1: - ofsLast = 0; - iCurrentModName = 0; - m_dbHeader.ofsFirstModuleName = 0; + + switch (phase) { + case 0: + if (ofsCurrent == 0) { + phase++; + return ERROR_SUCCESS; + } + if (!SignatureValid(ofsCurrent, DBMODULENAME_SIGNATURE)) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Module chain corrupted, further entries ignored")); + phase++; + return ERROR_SUCCESS; + } + if (PeekSegment(ofsCurrent, &moduleName, offsetof(DBModuleName, name)) != ERROR_SUCCESS) { phase++; - case 2: - if (iCurrentModName >= modChainCount) { - DWORD dw = 0; - if (ofsLast) WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&dw,sizeof(DWORD)); + return ERROR_SUCCESS; + } + modChain = (ModChainEntry*)realloc(modChain, sizeof(ModChainEntry)*++modChainCount); + + modChain[modChainCount - 1].ofsOld = ofsCurrent; + modChain[modChainCount - 1].size = offsetof(DBModuleName, name) + moduleName.cbName; + modChain[modChainCount - 1].ofsNew = 0; + + if (moduleName.cbName) + PeekSegment(ofsCurrent + offsetof(DBModuleName, name), &modChain[modChainCount - 1].name, moduleName.cbName); + modChain[modChainCount - 1].name[moduleName.cbName] = 0; + ofsCurrent = moduleName.ofsNext; + break; + case 1: + ofsLast = 0; + iCurrentModName = 0; + m_dbHeader.ofsFirstModuleName = 0; + phase++; + case 2: + if (iCurrentModName >= modChainCount) { + DWORD dw = 0; + if (ofsLast) WriteSegment(ofsLast + offsetof(DBModuleName, ofsNext), &dw, sizeof(DWORD)); + return ERROR_NO_MORE_ITEMS; + } + if (modChain[iCurrentModName].ofsNew == 0) { + newModName = (DBModuleName*)_alloca(modChain[iCurrentModName].size); + if (ReadSegment(modChain[iCurrentModName].ofsOld, newModName, modChain[iCurrentModName].size) != ERROR_SUCCESS) return ERROR_NO_MORE_ITEMS; - } - if (modChain[iCurrentModName].ofsNew == 0) { - newModName = (DBModuleName*)_alloca(modChain[iCurrentModName].size); - if (ReadSegment(modChain[iCurrentModName].ofsOld,newModName,modChain[iCurrentModName].size) != ERROR_SUCCESS) - return ERROR_NO_MORE_ITEMS; - if ((modChain[iCurrentModName].ofsNew = WriteSegment(WSOFS_END,newModName,modChain[iCurrentModName].size)) == WS_ERROR) - return ERROR_HANDLE_DISK_FULL; - { // check duplicated modulenames - int i, n = 0; - for (i = iCurrentModName+1;ipfnAddLogMessage(STATUS_WARNING,TranslateT("Module name '%s' is not unique: %d duplicates found"), pszModuleName, n); - } + if ((modChain[iCurrentModName].ofsNew = WriteSegment(WSOFS_END, newModName, modChain[iCurrentModName].size)) == WS_ERROR) + return ERROR_HANDLE_DISK_FULL; + + // check duplicated modulenames + int i, n = 0; + for (i = iCurrentModName + 1; i < modChainCount; i++) + if (!strcmp(modChain[i].name, modChain[iCurrentModName].name)) { + modChain[i].ofsNew = modChain[iCurrentModName].ofsNew; + n++; } - if (iCurrentModName == 0) - m_dbHeader.ofsFirstModuleName = modChain[iCurrentModName].ofsNew; - else - if (WriteSegment(ofsLast+offsetof(DBModuleName,ofsNext),&modChain[iCurrentModName].ofsNew,sizeof(DWORD)) == WS_ERROR) - return ERROR_HANDLE_DISK_FULL; - ofsLast = modChain[iCurrentModName].ofsNew; + if (n) { + TCHAR szModuleName[257]; + MultiByteToWideChar(CP_ACP, 0, modChain[iCurrentModName].name, -1, szModuleName, SIZEOF(szModuleName)); + TCHAR *pszModuleName = szModuleName; + + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("Module name '%s' is not unique: %d duplicates found"), pszModuleName, n); } - iCurrentModName++; - break; + + if (iCurrentModName == 0) + m_dbHeader.ofsFirstModuleName = modChain[iCurrentModName].ofsNew; + else if (WriteSegment(ofsLast + offsetof(DBModuleName, ofsNext), &modChain[iCurrentModName].ofsNew, sizeof(DWORD)) == WS_ERROR) + return ERROR_HANDLE_DISK_FULL; + ofsLast = modChain[iCurrentModName].ofsNew; + } + iCurrentModName++; + break; } return ERROR_SUCCESS; } DWORD CDb3Base::ConvertModuleNameOfs(DWORD ofsOld) { - int i; - if (modChain[last_mod].ofsOld == ofsOld) return modChain[last_mod].ofsNew; - for (i = 0;ipfnAddLogMessage(STATUS_ERROR,TranslateT("Invalid module name offset, skipping data")); + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Invalid module name offset, skipping data")); return 0; } diff --git a/plugins/Db3x_mmap/src/dbtool/settingschain.cpp b/plugins/Db3x_mmap/src/dbtool/settingschain.cpp index aaa52d9e22..1311b58661 100644 --- a/plugins/Db3x_mmap/src/dbtool/settingschain.cpp +++ b/plugins/Db3x_mmap/src/dbtool/settingschain.cpp @@ -19,11 +19,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "..\commonheaders.h" -static DWORD ofsThisSettings,ofsDestPrevSettings; +static DWORD ofsThisSettings, ofsDestPrevSettings; -int CDb3Base::WorkSettingsChain(DWORD ofsContact,DBContact *dbc,int firstTime) +int CDb3Base::WorkSettingsChain(DWORD ofsContact, DBContact *dbc, int firstTime) { - DBContactSettings *dbcsNew,dbcsOld; DWORD ofsDestThis; int ret; @@ -35,39 +34,44 @@ int CDb3Base::WorkSettingsChain(DWORD ofsContact,DBContact *dbc,int firstTime) if (ofsThisSettings == 0) return ERROR_NO_MORE_ITEMS; - if (!SignatureValid(ofsThisSettings,DBCONTACTSETTINGS_SIGNATURE)) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Settings chain corrupted, further entries ignored")); + if (!SignatureValid(ofsThisSettings, DBCONTACTSETTINGS_SIGNATURE)) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Settings chain corrupted, further entries ignored")); return ERROR_NO_MORE_ITEMS; } - if (PeekSegment(ofsThisSettings,&dbcsOld,sizeof(dbcsOld)) != ERROR_SUCCESS) + DBContactSettings dbcsOld; + if (PeekSegment(ofsThisSettings, &dbcsOld, sizeof(dbcsOld)) != ERROR_SUCCESS) return ERROR_NO_MORE_ITEMS; - if (dbcsOld.cbBlob>256*1024 || dbcsOld.cbBlob == 0) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("Infeasibly large settings blob: skipping")); + + if (dbcsOld.cbBlob > 256 * 1024 || dbcsOld.cbBlob == 0) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("Infeasibly large settings blob: skipping")); ofsThisSettings = dbcsOld.ofsNext; return ERROR_SUCCESS; } - dbcsNew = (DBContactSettings*)_alloca(offsetof(DBContactSettings,blob)+dbcsOld.cbBlob); - if ((ret = ReadSegment(ofsThisSettings,dbcsNew,offsetof(DBContactSettings,blob)+dbcsOld.cbBlob)) != ERROR_SUCCESS) { - if (ret != ERROR_HANDLE_EOF) { //eof is OK because blank space at the end doesn't matter + + DBContactSettings *dbcsNew = (DBContactSettings*)_alloca(offsetof(DBContactSettings, blob) + dbcsOld.cbBlob); + if ((ret = ReadSegment(ofsThisSettings, dbcsNew, offsetof(DBContactSettings, blob) + dbcsOld.cbBlob)) != ERROR_SUCCESS) + if (ret != ERROR_HANDLE_EOF) //eof is OK because blank space at the end doesn't matter return ERROR_NO_MORE_ITEMS; - } - } + if ((dbcsNew->ofsModuleName = ConvertModuleNameOfs(dbcsOld.ofsModuleName)) == 0) { ofsThisSettings = dbcsOld.ofsNext; return ERROR_SUCCESS; } + if (dbcsNew->blob[0] == 0) { - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Empty settings group at %08X: skipping"),ofsThisSettings); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Empty settings group at %08X: skipping"), ofsThisSettings); ofsThisSettings = dbcsOld.ofsNext; return ERROR_SUCCESS; } dbcsNew->ofsNext = 0; //TODO? validate all settings in blob/compact if necessary - if ((ofsDestThis = WriteSegment(WSOFS_END,dbcsNew,offsetof(DBContactSettings,blob)+dbcsNew->cbBlob)) == WS_ERROR) { + if ((ofsDestThis = WriteSegment(WSOFS_END, dbcsNew, offsetof(DBContactSettings, blob) + dbcsNew->cbBlob)) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; - } - if (ofsDestPrevSettings) WriteSegment(ofsDestPrevSettings+offsetof(DBContactSettings,ofsNext),&ofsDestThis,sizeof(DWORD)); - else dbc->ofsFirstSettings = ofsDestThis; + + if (ofsDestPrevSettings) + WriteSegment(ofsDestPrevSettings + offsetof(DBContactSettings, ofsNext), &ofsDestThis, sizeof(DWORD)); + else + dbc->ofsFirstSettings = ofsDestThis; ofsDestPrevSettings = ofsDestThis; ofsThisSettings = dbcsOld.ofsNext; return ERROR_SUCCESS; diff --git a/plugins/Db3x_mmap/src/dbtool/user.cpp b/plugins/Db3x_mmap/src/dbtool/user.cpp index ac200d2409..7b56e29c06 100644 --- a/plugins/Db3x_mmap/src/dbtool/user.cpp +++ b/plugins/Db3x_mmap/src/dbtool/user.cpp @@ -28,32 +28,32 @@ int CDb3Base::WorkUser(int firstTime) int first = 0; if (firstTime) { - cb->pfnAddLogMessage(STATUS_MESSAGE,TranslateT("Processing user data")); - if ( !SignatureValid(m_dbHeader.ofsUser, DBCONTACT_SIGNATURE)) { - cb->pfnAddLogMessage(STATUS_ERROR,TranslateT("User corrupted, this could cause major problems")); + cb->pfnAddLogMessage(STATUS_MESSAGE, TranslateT("Processing user data")); + if (!SignatureValid(m_dbHeader.ofsUser, DBCONTACT_SIGNATURE)) { + cb->pfnAddLogMessage(STATUS_ERROR, TranslateT("User corrupted, this could cause major problems")); return ERROR_NO_MORE_ITEMS; } - if ( ReadSegment(m_dbHeader.ofsUser,&user,sizeof(DBContact)) != ERROR_SUCCESS) + if (ReadSegment(m_dbHeader.ofsUser, &user, sizeof(DBContact)) != ERROR_SUCCESS) return ERROR_NO_MORE_ITEMS; if (user.ofsNext) { - cb->pfnAddLogMessage(STATUS_WARNING,TranslateT("More than one user contact: keeping only first")); + cb->pfnAddLogMessage(STATUS_WARNING, TranslateT("More than one user contact: keeping only first")); user.ofsNext = 0; } - if ((ofsUser = WriteSegment(WSOFS_END,&user,sizeof(DBContact))) == WS_ERROR) + if ((ofsUser = WriteSegment(WSOFS_END, &user, sizeof(DBContact))) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; m_dbHeader.ofsUser = ofsUser; phase = 0; first = 1; } - + int ret; - switch(phase) { + switch (phase) { case 0: - ret = WorkSettingsChain(ofsUser,&user,first); + ret = WorkSettingsChain(ofsUser, &user, first); if (ret == ERROR_NO_MORE_ITEMS) { phase++; first = 1; } @@ -61,13 +61,14 @@ int CDb3Base::WorkUser(int firstTime) else break; case 1: - ret = WorkEventChain(ofsUser,&user,first); + ret = WorkEventChain(ofsUser, &user, first); if (ret == ERROR_NO_MORE_ITEMS) { - if (WriteSegment(ofsUser,&user,sizeof(DBContact)) == WS_ERROR) + if (WriteSegment(ofsUser, &user, sizeof(DBContact)) == WS_ERROR) return ERROR_HANDLE_DISK_FULL; return ERROR_NO_MORE_ITEMS; } - else if (ret) return ret; + else if (ret) + return ret; break; } return ERROR_SUCCESS; -- cgit v1.2.3