how works "compare HEX"
BlackMage2 opened this issue · comments
BlackMage2 commented
how works the function "compare HEX"? with me it is always grayed out
chcg commented
See
NPP_HexEdit/HexEditor/src/Hex.cpp
Lines 1460 to 1589 in baea843
void DoCompare(void) | |
{ | |
TCHAR szFile[MAX_PATH]; | |
BOOL doMatch = TRUE; | |
tHexProp hexProp1 = hexEdit1.GetHexProp(); | |
tHexProp hexProp2 = hexEdit2.GetHexProp(); | |
tCmpResult cmpResult = { 0 }; | |
if ((hexProp1.bits != hexProp2.bits) || (hexProp1.columns != hexProp2.columns) || (hexProp1.isBin != hexProp2.isBin)) | |
{ | |
/* ask which hex edits should be used to have same settings in both */ | |
CompareDlg dlg; | |
dlg.init((HINSTANCE)g_hModule, nppData); | |
if (dlg.doDialog(&hexEdit1, &hexEdit2, currentSC) == IDCANCEL) { | |
return; | |
} | |
} | |
/* create file for compare results */ | |
_tcscpy(cmpResult.szFileName, cmparePath); | |
_tcscpy(szFile, ::PathFindFileName(hexEdit1.GetHexProp().szFileName)); | |
::PathRemoveExtension(szFile); | |
::PathAppend(cmpResult.szFileName, szFile); | |
_tcscat(cmpResult.szFileName, _T("_")); | |
_tcscpy(szFile, ::PathFindFileName(hexEdit2.GetHexProp().szFileName)); | |
::PathRemoveExtension(szFile); | |
_tcscat(cmpResult.szFileName, szFile); | |
_tcscat(cmpResult.szFileName, _T(".cmp")); | |
cmpResult.hFile = ::CreateFile(cmpResult.szFileName, | |
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, | |
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
if (cmpResult.hFile != INVALID_HANDLE_VALUE) | |
{ | |
LPSTR buffer1 = (LPSTR)new CHAR[COMP_BLOCK + 1]; | |
LPSTR buffer2 = (LPSTR)new CHAR[COMP_BLOCK + 1]; | |
/* get text size to comapre */ | |
INT maxLength1 = ScintillaMsg(nppData._scintillaMainHandle, SCI_GETTEXTLENGTH) + 1; | |
INT maxLength2 = ScintillaMsg(nppData._scintillaSecondHandle, SCI_GETTEXTLENGTH) + 1; | |
/* get max length */ | |
INT curPos = 0; | |
INT maxLength = maxLength1; | |
INT minLength = maxLength1; | |
if (maxLength2 > maxLength1) { | |
maxLength = maxLength2; | |
} | |
else { | |
minLength = maxLength2; | |
} | |
while (curPos < minLength) | |
{ | |
CHAR val = FALSE; | |
INT posSrc = 0; | |
INT length1 = ((maxLength1 - curPos) > COMP_BLOCK ? COMP_BLOCK : (maxLength1 % COMP_BLOCK)); | |
INT length2 = ((maxLength2 - curPos) > COMP_BLOCK ? COMP_BLOCK : (maxLength2 % COMP_BLOCK)); | |
ScintillaGetText(nppData._scintillaMainHandle, buffer1, curPos, length1 - 1); | |
ScintillaGetText(nppData._scintillaSecondHandle, buffer2, curPos, length2 - 1); | |
while ((posSrc < length1) && (posSrc < length2)) | |
{ | |
DWORD hasWritten = 0; | |
if (buffer1[posSrc] != buffer2[posSrc]) | |
{ | |
val = TRUE; | |
doMatch = FALSE; | |
} | |
/* increment source buffer */ | |
posSrc++; | |
/* write to file */ | |
if (hexProp1.bits == 1) { | |
::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL); | |
val = FALSE; | |
} | |
else if ((posSrc % hexProp1.bits) == 0) { | |
::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL); | |
val = FALSE; | |
} | |
} | |
/* increment file position */ | |
curPos += posSrc; | |
} | |
if (doMatch == TRUE) | |
{ | |
if (NLMessageBox((HINSTANCE)g_hModule, nppData._nppHandle, _T("MsgBox CompMatch"), MB_OK) == FALSE) | |
::MessageBox(nppData._nppHandle, _T("Files Match."), _T("Hex-Editor Compare"), MB_OK); | |
::CloseHandle(cmpResult.hFile); | |
::DeleteFile(cmpResult.szFileName); | |
} | |
else | |
{ | |
DWORD hasWritten = 0; | |
CHAR val = TRUE; | |
for (INT i = (minLength / hexProp1.bits); i < (maxLength / static_cast<INT>(hexProp1.bits)); i++) { | |
::WriteFile(cmpResult.hFile, &val, sizeof(val), &hasWritten, NULL); | |
} | |
/* create two structures for each view */ | |
tCmpResult* pCmpResult1 = (tCmpResult*)new tCmpResult; | |
tCmpResult* pCmpResult2 = (tCmpResult*)new tCmpResult; | |
if ((pCmpResult1 != NULL) && (pCmpResult2 != NULL)) | |
{ | |
/* set data */ | |
*pCmpResult1 = cmpResult; | |
*pCmpResult2 = cmpResult; | |
hexEdit1.SetCompareResult(pCmpResult1, pCmpResult2); | |
hexEdit2.SetCompareResult(pCmpResult2, pCmpResult1); | |
} | |
else | |
{ | |
delete pCmpResult1; | |
delete pCmpResult2; | |
} | |
} | |
delete[] buffer1; | |
delete[] buffer2; | |
} | |
} |
Two views (via context menu "Move to other view") are needed with hex mode active to have the menu command available.
But I encountered some crashes of N++ and access violations when I tried to use this option.