summaryrefslogtreecommitdiff
path: root/plugins/SMS/src/AdditionalFunctions/MemoryFind.h
blob: dbc5f1a2d1cff82886821ba76b8ba1ca73524914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#if !defined(AFX_MEMORYFIND__H__INCLUDED_)
#define AFX_MEMORYFIND__H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

__inline LPVOID MemoryFind(SIZE_T dwFrom,LPCVOID lpcSource,SIZE_T dwSourceSize,LPCVOID lpcWhatFind,SIZE_T dwWhatFindSize)
{
	LPVOID lpRet=NULL;

	__asm
	{
		push	ebx					// ñîõðàíÿåì ðåãèñòð
		push	edi					// ñîõðàíÿåì ðåãèñòð
		push	esi					// ñîõðàíÿåì ðåãèñòð

		mov			ecx,dwSourceSize	//; ecx = Source string Size
		test		ecx,ecx				// is size unknown?
		jz			short end_func

		mov			edx,dwWhatFindSize	//; edx = WhatFind string Size
		test		edx,edx				// is size unknown?
		jz			short end_func

		mov			ebx,dwFrom			// ebx - start pos in Source string
		mov			edi,lpcSource		//; edi = Source string
		mov			esi,lpcWhatFind		//; esi = WhatFind string

		cmp			ebx,ecx				// ïðîâåðêà ecx(=len)=>ulFrom
		jae			short end_func

		add			edi,ebx				// ñäâèãàåì íà÷àëî íà ulFrom(íà÷ ñìåùåí)
		sub			ecx,ebx				// óìåíüøàåì äëèííó SourceSize íà ulFrom(íà÷ ñìåùåí)

		cmp			ecx,edx				// ïðîâåðêà NEWSourceSize ??? ulWhatFindSize
		je			short begin_memorycompare	// NEWulSourceSize==ulWhatFindSize, Source ??? WhatFind
		jl			short end_func		// NEWulSourceSize<ulWhatFindSize, => Source!=WhatFind

		sub			ecx,edx				// óìåíüøàåì äëèííó SourceSize íà ulWhatFindSize
		inc			ecx

        mov			al,[esi]			//; al=search byte
		dec			edi
		cld								//; ñêàíèðóÿ â ïðÿìîì íàïðàâëåíèè

	find_loop:
		test		ecx,ecx
		jz			short end_func
		inc			edi
		repne		scasb				//; find that byte
        dec			edi					//; di points to byte which stopped scan

        cmp			[edi],al			//; see if we have a hit
        jne			short end_func		//; yes, point to byte

	begin_memorycompare:
		push		esi
		push		edi
		push		ecx
		mov			ecx,edx				//;	ulWhatFindSize áàéòîâ (CX èñïîëüçóåòñÿ â REPE),
		repe		cmpsb				//;	ñðàâíèâàåì èõ.
		pop			ecx
		pop			edi
		pop			esi
		jne			short find_loop		//; ïðèçíàê ZF = 0, åñëè ñðàâíèâàåìûå
										//; ñòðîêè íå ñîâïàäàþò (mismatch) match:
										//; åñëè ìû ïîïàëè ñþäà, çíà÷èò, îíè
										//; ñîâïàäàþò (match)
		mov			lpRet,edi			//; ax=pointer to byte
	end_func:

		pop		esi					// âîññòàíàâëèâàåì ñîäåðæèìîå ðåãèñòðà
		pop		edi					// âîññòàíàâëèâàåì ñîäåðæèìîå ðåãèñòðà
		pop		ebx					// âîññòàíàâëèâàåì ñîäåðæèìîå ðåãèñòðà
	}
return(lpRet);
}


#endif // !defined(AFX_MEMORYFIND__H__INCLUDED_)