diff --git a/ParseHexString.cpp b/ParseHexString.cpp index d1416da..72bbaa5 100644 --- a/ParseHexString.cpp +++ b/ParseHexString.cpp @@ -20,18 +20,17 @@ #include "hwapi.h" -// Plug-in Command constants -#define FILL_RANDOM_COMMAND _T("Selection Example\\Fill Random Bytes") -#define CLIP_SELECTION_COMMAND _T("Selection Example\\Clip to Selection") -#define COPY_NEW_FILE_COMMAND _T("Selection Example\\Copy to New Document") +#define IsHexChar(a) ((a >= '0' && a <= '9') || (a >= 'A' && a <= 'F') || (a >= 'a' && a <= 'f')) +#define IsSkipChar(a) ((a == ' ') || (a == 0xd) || (a == 0xa) || (a == '\t')) +#define IsNumber(a) ((a >= '0' && a <= '9')) +#define IsUpper(a) ((a >= 'a' && a <= 'f')) +#define IsLower(a) ((a >= 'A' && a <= 'F')) -// Block size used during copy operation (64K chunks) -#define COPY_BLOCK_SIZE 0x10000 +// Plug-in Command constants +#define PARSE_HEX_STRING _T("Parse Hex String\\Parse") // Forward declarations (helper functions that perform tasks) -BOOL doFillRandom(HWSESSION hSession, HWDOCUMENT hDoc) ; -BOOL doClipSelection(HWSESSION hSession, HWDOCUMENT hDoc) ; -BOOL doCopyToNewFile(HWSESSION hSession, HWDOCUMENT hDoc) ; +BOOL doParseHexString(HWSESSION hSession, HWDOCUMENT hDoc); // DllMain BOOL APIENTRY DllMain(HANDLE hModule, @@ -45,11 +44,9 @@ BOOL APIENTRY DllMain(HANDLE hModule, HWAPIEP BOOL HWPLUGIN_Identify(LPTSTR lpstrPluginCommand, size_t nMaxPluginCommand) { - _sntprintf(lpstrPluginCommand, nMaxPluginCommand, - _T("%s;%s;%s"), - FILL_RANDOM_COMMAND, - CLIP_SELECTION_COMMAND, - COPY_NEW_FILE_COMMAND) ; + _sntprintf(lpstrPluginCommand, nMaxPluginCommand, + _T("%s"), + PARSE_HEX_STRING); return TRUE ; } @@ -60,17 +57,7 @@ HWAPIEP DWORD HWPLUGIN_RequestCapabilities(LPCTSTR lpstrPluginCommand) { // Return unique capabilities for each Plug-in command - if (_tcsicmp(lpstrPluginCommand, FILL_RANDOM_COMMAND) == 0) - { - // Fill Random requires both a file and a selection range - return HWPLUGIN_CAP_FILE_REQUIRE | HWPLUGIN_CAP_SELECTION_REQUIRE ; - } - else if (_tcsicmp(lpstrPluginCommand, CLIP_SELECTION_COMMAND) == 0) - { - // Clip Selection requires both a file and a selection range - return HWPLUGIN_CAP_FILE_REQUIRE | HWPLUGIN_CAP_SELECTION_REQUIRE ; - } - else if (_tcsicmp(lpstrPluginCommand, COPY_NEW_FILE_COMMAND) == 0) + if (_tcsicmp(lpstrPluginCommand, PARSE_HEX_STRING) == 0) { // Copy to new file require a file, but selection is optional return HWPLUGIN_CAP_FILE_REQUIRE ; @@ -85,20 +72,10 @@ HWAPIEP BOOL HWPLUGIN_Execute( LPCTSTR lpstrPluginCommand, HWDOCUMENT hDocument ) { // Delegate plug-in command to helper functioms - if (_tcsicmp(lpstrPluginCommand, FILL_RANDOM_COMMAND) == 0) + if (_tcsicmp(lpstrPluginCommand, PARSE_HEX_STRING) == 0) { - // Fill w/ Random Bytes - return doFillRandom(hSession, hDocument) ; - } - else if (_tcsicmp(lpstrPluginCommand, CLIP_SELECTION_COMMAND) == 0) - { - // Clip the document to the selection - return doClipSelection(hSession, hDocument) ; - } - else if (_tcsicmp(lpstrPluginCommand, COPY_NEW_FILE_COMMAND) == 0) - { - // Copy selection/entire file to a new document - return doCopyToNewFile(hSession, hDocument) ; + // parse hex string + return doParseHexString(hSession, hDocument) ; } else { @@ -107,267 +84,102 @@ HWAPIEP BOOL HWPLUGIN_Execute( LPCTSTR lpstrPluginCommand, } } -// Fill w/ random byte implementation -BOOL doFillRandom(HWSESSION hSession, HWDOCUMENT hDoc) +BOOL doParseHexString(HWSESSION hSession, HWDOCUMENT hDoc) { - BOOL bRC = FALSE ; - QWORD qwStartPosition ; - QWORD qwLength ; - - // Check readonly document status - BOOL bReadOnly = TRUE ; - hwGetReadOnly(hDoc, &bReadOnly) ; - if (bReadOnly) - { - MessageBox(hwGetWindowHandle(hSession), - _T("Document is read-only; cannot perform operation."), - _T("Error"), - MB_ICONSTOP | MB_APPLMODAL) ; - } - else + BOOL bReturn = FALSE; + QWORD qwStartPosition; + QWORD qwLength; + HWND hMain = hwGetWindowHandle(hSession); + + // Check readonly document status + BOOL bReadOnly = TRUE; + hwGetReadOnly(hDoc, &bReadOnly); + if (bReadOnly) + { + MessageBox(hMain, + _T("Document is read-only; cannot perform operation."), + _T("Error"), + MB_ICONSTOP | MB_APPLMODAL); + return bReturn; + } + + // Obtain starting position and length + if ((hwGetCaretPosition(hDoc, &qwStartPosition) == HWAPI_RESULT_SUCCESS) && + (hwGetSelection(hDoc, &qwLength) == HWAPI_RESULT_SUCCESS)) { - // Seed random generator - srand( (unsigned)time( NULL ) ); + HANDLE hClip = NULL; + LPSTR pData = NULL; + LPSTR pStr = NULL; - // Obtain starting position and length - if ((hwGetCaretPosition(hDoc, &qwStartPosition) == HWAPI_RESULT_SUCCESS) && - (hwGetSelection(hDoc, &qwLength) == HWAPI_RESULT_SUCCESS)) + __try { - bRC = TRUE ; + // Group all changes into a single undo operation + hwUndoBeginGroup(hDoc); + + if (!IsClipboardFormatAvailable(CF_TEXT)) + __leave; + if (!OpenClipboard(hMain)) + { + MessageBox(hMain, _T("发开剪切板失败!"), _T("错误"), MB_OK); + __leave; + } - // Group all changes into a single undo operation - hwUndoBeginGroup(hDoc) ; + hClip = GetClipboardData(CF_TEXT); + if (!hClip) + __leave; + pData = (LPSTR)GlobalLock(hClip); - // Replace each byte in the range with random data. - // - // NOTE: This method is only recommended for small selections and - // is fairly slow. For larger selections, one should work with - // large chunks of data (e.g. 64K blocks) instead of individual - // bytes. - for (QWORD i=0; i 0) - { - if (hwDeleteAt(hDoc, qwStartPosition + qwLength, qwCutLength) - != HWAPI_RESULT_SUCCESS) - { - // Error errors to output log - hwOutputLog(hSession, HWLOG_ERR, - _T("Failed to delete range [%08I64X .. %08I64X]"), - qwStartPosition + qwLength, - qwStartPosition + qwLength + qwCutLength) ; - bRC = FALSE ; - } - } - - // Remove data prior to selection - if (qwStartPosition > 0) - { - if (hwDeleteAt(hDoc, 0, qwStartPosition) - != HWAPI_RESULT_SUCCESS) - { - // Error errors to output log - hwOutputLog(hSession, HWLOG_ERR, - _T("Failed to delete range [%08I64X .. %08I64X]"), - 0, - qwStartPosition) ; - bRC = FALSE ; - } - } - - // Commit the undo group - hwUndoEndGroup(hDoc) ; - - // Reset caret position and selection - hwSetCaretPosition(hDoc, 0) ; - hwSetSelection(hDoc, qwLength) ; - bRC = TRUE ; - } - } - - return bRC ; -} - - -// Copy selection/entire to new document implementation -BOOL doCopyToNewFile(HWSESSION hSession, HWDOCUMENT hDoc) -{ - BOOL bRC = FALSE ; - QWORD qwStartPosition ; - QWORD qwLength ; - QWORD qwDocSize ; - - // Obtain starting position, length, and document size - if ((hwGetCaretPosition(hDoc, &qwStartPosition) == HWAPI_RESULT_SUCCESS) && - (hwGetSelection(hDoc, &qwLength) == HWAPI_RESULT_SUCCESS) && - (hwGetDocumentSize(hDoc, &qwDocSize) == HWAPI_RESULT_SUCCESS)) - { - // If no selection, use entire document - if (qwLength == 0) - { - qwStartPosition = 0 ; - qwLength = qwDocSize ; - } - - // Create a new document - HWDOCUMENT hNewDoc = hwNewDocument(hSession) ; - if (hNewDoc != NULL) - { - bRC = TRUE ; - - // No need to allow undo for a new document - hwUndoDisable(hNewDoc) ; - - QWORD qwBytesLeft = qwLength ; - QWORD qwBytesCopied = 0 ; - char cBuffer[COPY_BLOCK_SIZE] ; - - // Init tick count for progress indicator - DWORD dwNextProgress = GetTickCount() + 1000 ; - QWORD qwLastProgressCount = 0 ; - - // Copy Data - while (qwBytesLeft > 0) - { - QWORD qwBytesToCopy = __min(COPY_BLOCK_SIZE, qwLength - qwBytesCopied) ; - if (hwReadAt(hDoc, qwStartPosition + qwBytesCopied, cBuffer, qwBytesToCopy) - == HWAPI_RESULT_SUCCESS) - { - if (hwInsertAt(hNewDoc, qwBytesCopied, cBuffer, qwBytesToCopy) == HWAPI_RESULT_SUCCESS) + else if (IsSkipChar(*p1)) { - qwBytesLeft -= qwBytesToCopy ; - qwBytesCopied += qwBytesToCopy ; + p1 += 1; } else { - // Report errors to output conosole - hwOutputLog(hSession, HWLOG_ERR, - _T("Failed to insert at offset %08I64X"), - qwBytesCopied) ; - bRC = FALSE ; - break ; + // error + u2 = 0; + break; } } - else - { - // Report errors to output conosole - hwOutputLog(hSession, HWLOG_ERR, - _T("Failed to read at offset %08I64X"), - qwStartPosition + qwBytesCopied) ; - bRC = FALSE ; - break ; - } - // Update progress every 1 second (1000 ms) - if (GetTickCount() > dwNextProgress) - { - dwNextProgress = GetTickCount() + 1000 ; - QWORD qwCopied = qwBytesCopied - qwLastProgressCount ; - qwLastProgressCount = qwBytesCopied ; - - TCHAR cStatus[256] ; - - _sntprintf(cStatus, sizeof(cStatus) / sizeof(TCHAR), - _T("Copying: %I64d of %I64d (%I64d KB/sec)"), - qwBytesCopied, qwLength, qwCopied / 1024) ; - - QWORD percentComplete = (qwBytesCopied * 100) / qwLength ; - if (hwUpdateProgress(hSession, (int) percentComplete, cStatus) != - HWAPI_RESULT_SUCCESS) - { - // Error indicates user cancel - hwOutputLog(hSession, HWLOG_INFO, - _T("User aborted operation")) ; - bRC = FALSE ; - break ; - } - } - } + hwInsertAt(hDoc, qwStartPosition, pStr, u2); - // Enable Undo for future operations - hwUndoEnable(hNewDoc) ; - } + if (p1 != (pData + uDataLen)) + { + MessageBox(hMain, _T("解析缺失部分末尾数据!"), _T("警告"), MB_OK); + } + } + } + __finally + { + if (pStr) + delete[] pStr; + if (pData) + GlobalUnlock(pData); + CloseClipboard(); + // Commit the undo group + hwUndoEndGroup(hDoc); + } } - return bRC ; + return bReturn; } diff --git a/ParseHexString.vcxproj b/ParseHexString.vcxproj index a03faba..c282b20 100644 --- a/ParseHexString.vcxproj +++ b/ParseHexString.vcxproj @@ -106,7 +106,7 @@ WIN32;_DEBUG;_WINDOWS;_USRDLL;FILLRANDOM_EXPORTS;%(PreprocessorDefinitions) true EnableFastChecks - MultiThreadedDebugDLL + MultiThreadedDebug Use Level3 EditAndContinue @@ -130,7 +130,7 @@ WIN32;_DEBUG;_WINDOWS;_USRDLL;FILLRANDOM_EXPORTS;%(PreprocessorDefinitions) true EnableFastChecks - MultiThreadedDebugDLL + MultiThreadedDebug Use Level3 ProgramDatabase @@ -148,7 +148,7 @@ $(SolutionDir)include;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;FILLRANDOM_EXPORTS;%(PreprocessorDefinitions) - MultiThreadedDLL + MultiThreaded Use Level3 ProgramDatabase @@ -171,7 +171,7 @@ $(SolutionDir)include;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;FILLRANDOM_EXPORTS;%(PreprocessorDefinitions) - MultiThreadedDLL + MultiThreaded Use Level3 ProgramDatabase diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..7b11649 --- /dev/null +++ b/Readme.md @@ -0,0 +1,4 @@ +# Hex Workshop v6.7 + +灏哷hex string`瑙f瀽涓轰簩杩涘埗鏁版嵁鐨勬彃浠躲 +