📄 util.cpp
字号:
// RawPacket.cpp : main source file for MdiTest3.exe
//
#include "stdafx.h"
#include "util.h"
/**************************************************************
* 用于在一个字符串中查找子串(ANSI)
* char* __fastcall stristrA(const char* pszMain, const char* pszSub)
* 成功:返回字符串的起始位置指针,pszMain为源字符串,pszSub为要查找的目标
* 失败:返回NULL
*
****************************************************************/
char* __fastcall stristrA(const char* pszMain, const char* pszSub)
{
pszMain; // compiler thinks these are unreferenced because
pszSub; // they are in ecx and edx registers
char* pszTmp1;
char* pszTmp2;
char lowerch, upperch;
if(lstrlen(pszMain)==0||lstrlen(pszSub)==0)return NULL;
// We keep the first character of pszSub in lowerch and upperch (lower and
// upper case). First we loop trying to find a match for this character. Once
// we have found a match, we start with the second character of both pszMain
// and pszSub and walk through both strings doing a CharLower on both
// characters before comparing. If we make it all the way through pszSub with
// matches, then we bail with a pointer to the string's location in pszMain.
_asm {
mov ecx,pszMain
mov edx,pszSub
mov esi, ecx //
mov edi, edx // pszSub
// Check for NULL pointers
test ecx, ecx
je short NoMatch // NULL pointer for pszMain
test edx, edx
je short NoMatch // NULL pointer for pszSub
sub eax, eax
mov al, [edi]
push eax
call DWORD PTR CharLower
mov lowerch, al
push eax
call DWORD PTR CharUpper
mov upperch, al
push edi // increment the second string pointer
call DWORD PTR CharNext
mov edi, eax
mov pszTmp2, edi
mov edi, DWORD PTR CharNext // faster to call through a register
Loop1:
mov al, [esi]
test al, al
je short NoMatch // end of main string, so no match
cmp al, lowerch
je short CheckString // lowercase match?
cmp al, upperch
je short CheckString // upppercase match?
push esi
call edi // Call CharNext to update main string pointer
mov esi, eax
jmp short Loop1
CheckString:
mov pszTmp1, esi // save current pszMain pointer in case its a match
push esi
call edi // first character of both strings match,
mov esi, eax // so move to next pszMain character
mov edi, pszTmp2
mov al, [edi]
jmp short Branch1
Loop3:
push esi
call DWORD PTR CharNext // CharNext to change pszMain pointer
mov esi, eax
push edi
call DWORD PTR CharNext // CharNext to change pszSub pointer
mov edi, eax
mov al, [edi]
Branch1:
test al, al
je short Match // zero in sub string, means we've got a match
cmp al, [esi]
je short Loop3
// Doesn't match, but might be simply a case mismatch. Lower-case both
// characters and compare again
sub ecx, ecx
mov cl, al // character from pszSub
push ecx
call DWORD PTR CharLower
mov cl, al
sub eax, eax
mov al, [esi] // character from pszMain
push ecx // preserve register
push eax
call DWORD PTR CharLower
pop ecx
cmp al, cl
je short Loop3 // we still have a match, keep checking
// No match, put everything back, update pszMain to the next character
// and try again from the top
mov esi, pszTmp1
mov edi, DWORD PTR CharNext
push esi
call edi
mov esi, eax
jmp short Loop1
Match:
mov eax, pszTmp1
jmp short Done // Don't just return -- always let the C portion of the code handle the return
NoMatch:
sub eax, eax
Done:
}
// Note lack of return in the C portion of the code. Return value is always in
// eax register which we have set by the time we get here
}
/**************************************************************
* 用于在一个字符串中查找子串(UNICODE)
* char* __fastcall stristrA(const char* pszMain, const char* pszSub)
* 成功:返回字符串的起始位置指针,pszMain为源字符串,pszSub为要查找的目标
* 失败:返回NULL
*
****************************************************************/
WCHAR* __fastcall stristrW(const WCHAR* pszMain, const WCHAR* pszSub)
{
pszMain; // compiler thinks these are unreferenced
pszSub;
WCHAR* pszTmp1;
WCHAR* pszTmp2;
WCHAR lowerch, upperch;
if(lstrlenW (pszMain)==0||lstrlenW(pszSub)==0)return NULL;
// We keep the first character of pszSub in lowerch and upperch (lower and
// upper case). First we loop trying to find a match for this character. Once
// we have found a match, we start with the second character of both pszMain
// and pszSub and walk through both strings doing a CharLower on both
// characters before comparing. If we make it all the way through pszSub with
// matches, then we bail with a pointer to the strings location in pszMain.
_asm {
mov ecx,pszMain
mov edx,pszSub
mov esi, ecx // pszMain
mov edi, edx // pszSub
// Check for NULL pointers
test ecx, ecx
je short NoMatch // NULL pointer for pszMain
test edx, edx
je short NoMatch // NULL pointer for pszSub
sub eax, eax
mov ax, [edi]
push eax
call DWORD PTR CharLowerW
mov lowerch, ax
push eax
call DWORD PTR CharUpperW
mov upperch, ax
lea edi, [edi+2]
mov pszTmp2, edi
Loop1:
mov ax, [esi]
test ax, ax
je short NoMatch // end of main string, so no match
cmp ax, lowerch
je short CheckString // lowercase match?
cmp ax, upperch
je short CheckString // upppercase match?
lea esi, [esi+2]
jmp short Loop1
CheckString:
mov pszTmp1, esi // save current pszMain pointer
lea esi, [esi+2]
mov edi, pszTmp2
mov ax, [edi]
jmp short Branch1
Loop3:
lea esi, [esi+2]
lea edi, [edi+2]
mov ax, [edi]
Branch1:
test ax, ax
je short Match // zero in main string, means we've got a match
cmp ax, [esi]
je short Loop3
// Doesn't match, but might be simply a case mismatch. Lower-case both
// characters and compare again
sub ecx, ecx
mov cx, ax // character from pszSub
push ecx
call DWORD PTR CharLowerW
mov cx, ax
sub eax, eax
mov ax, [esi] // character from pszMain
push ecx // preserve register
push eax
call DWORD PTR CharLowerW
pop ecx
cmp ax, cx
je short Loop3 // we still have a match, keep checking
// No match, put everything back, update pszMain to the next character
// and try again from the top
mov esi, pszTmp1
lea esi, [esi+2]
jmp short Loop1
Match:
mov eax, pszTmp1
jmp short Done
NoMatch:
sub eax, eax
Done:
}
// Note lack of return in the C portion of the code. Return value is always in
// eax register which we have set by the time we get here
}
BOOL IsWindowsNT()
{
BOOL bRet = FALSE;
BOOL bOsVersionInfoEx;
OSVERSIONINFOEX osvi;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return bRet;
}
if (osvi.dwPlatformId & VER_PLATFORM_WIN32_NT )
{
bRet = TRUE;
}
return bRet;
}
BOOL IsWindows2k()
{
BOOL bRet = FALSE;
BOOL bOsVersionInfoEx;
OSVERSIONINFOEX osvi;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return bRet;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion >= 5)
{
bRet = TRUE;
}
return bRet;
}
//CheckSum:计算校验和的子函数
DWORD checksum(BYTE *buffer, int size)
{
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(BYTE);
}
if(size )
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (DWORD)(~cksum);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -