summaryrefslogtreecommitdiff
path: root/plugins/SMS/src/AdditionalFunctions/MemoryCompare.h
blob: de663c6d8aaf56e389fd843f1375f0bc08a24af9 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#if !defined(AFX_MEMORYCOMPARE__H__INCLUDED_)
#define AFX_MEMORYCOMPARE__H__INCLUDED_

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


//  If the string pointed to by lpString1 is less than the string pointed 
// to by lpString2, the return value is negative. 
//  If the string pointed to by lpString1 is greater than the string pointed 
// to by lpString2, the return value is positive. 
//  If the strings are equal, the return value is zero. 
//
// lpString1<lpString2 >> ret=1=CSTR_LESS_THAN
// lpString1=lpString2 >> ret=2=CSTR_EQUAL
// lpString1>lpString2 >> ret=3=CSTR_GREATER_THAN


/*__inline DWORD MemoryCompare(LPCVOID lpcSource1,SIZE_T dwSource1Size,LPCVOID lpcSource2,SIZE_T dwSource2Size)
{
	DWORD dwRet;

	__asm
	{
		mov			ecx,dwSource1Size	//; ecx = Source string 1 Size
		cmp			ecx,dwSource2Size	//; ñâåðÿåì äëèííû ó÷àñòêîâ ïàìÿòè
		jg			short greater_than
		jl			short less_than
		test		ecx,ecx
		jz			short equal			//; NULL=NULL

		mov			esi,lpcSource1		//; edi = Source string 1
		mov			edi,lpcSource2		//; esi = Source string 2
		cmp			edi,esi				//; ñâåðÿåì óêàçàòåëè íà ó÷àñòêè ïàìÿòè
		je			short equal			//; ýòî îäèí è òîòæå ó÷àñòîê, îíè åñòåñòâåííî ðàâíû

		test		esi,esi				//; lpcSource1=NULL, lpcSource1<lpcSource2
		jz			short less_than		//; CSTR_LESS_THAN

		test		edi,edi				//; lpcSource2=NULL, lpcSource1>lpcSource2
		jz			short greater_than	//; CSTR_GREATER_THAN

		//cld								//; ñêàíèðóÿ â ïðÿìîì íàïðàâëåíèè

		repe		cmpsb				//;	öèêë ñðàâíåíèÿ.
	cmp_loop:
		//sub			ecx,4
		//jz			short equal

		//inc			esi
		//inc			edi
		//mov			al,byte ptr [esi]
		//cmpsd
		//cmp			al,byte ptr [edi]
		//je			short cmp_loop
		jg			short greater_than
		jl			short less_than

	equal:								//; åñëè ìû ïîïàëè ñþäà, çíà÷èò, îíè
		mov			dwRet,CSTR_EQUAL	//; ñîâïàäàþò (match)
		jmp			end_func
	less_than:							//; íå ñîâïàäàþò
		mov			dwRet,CSTR_LESS_THAN
		jmp			end_func
	greater_than:
		mov			dwRet,CSTR_GREATER_THAN

	end_func:
	}
return(dwRet);
}//*/

__inline DWORD MemoryCompare(LPCVOID lpcSource1,SIZE_T dwSource1Size,LPCVOID lpcSource2,SIZE_T dwSource2Size)
{
	DWORD dwRet;

	if (dwSource1Size==dwSource2Size)
	{
		if (lpcSource1==lpcSource2)
		{
			dwRet=CSTR_EQUAL;
		}else{
			if (lpcSource1 && lpcSource2)
			{
#ifdef _INC_MEMORY
				dwRet=(2+memcmp(lpcSource1,lpcSource2,dwSource1Size));
#else
				SIZE_T dwDiffPosition;

				//dwDiffPosition=RtlCompareMemory(lpcSource1,lpcSource2,dwSource1Size);
				for(dwDiffPosition=0; (dwDiffPosition<dwSource1Size) && (((const BYTE*)lpcSource1)[dwDiffPosition]==((const BYTE*)lpcSource2)[dwDiffPosition]); dwDiffPosition++);
				if (dwDiffPosition==dwSource1Size)
				{
					dwRet=CSTR_EQUAL;
				}else{
					if ((*((BYTE*)(((SIZE_T)lpcSource1)+dwDiffPosition)))>(*((BYTE*)(((SIZE_T)lpcSource2)+dwDiffPosition))))
					{
						dwRet=CSTR_GREATER_THAN;
					}else{
						dwRet=CSTR_LESS_THAN;
					}
				}
#endif
			}else{
				if (lpcSource1)
				{//lpcSource2==NULL
					dwRet=CSTR_GREATER_THAN;
				}else{//lpcSource1==NULL
					dwRet=CSTR_LESS_THAN;
				}
			}
		}
	}else{
		if (dwSource1Size<dwSource2Size)
		{
			dwRet=CSTR_LESS_THAN;
		}else{
			dwRet=CSTR_GREATER_THAN;
		}
	}
return(dwRet);
}//*/


/*
__inline DWORD MemoryCompareEx(LPCVOID lpcSource1,SIZE_T dwSource1Size,LPCVOID lpcSource2,SIZE_T dwSource2Size,SIZE_T *pdwDiffPosition)
{
	DWORD dwRet;

	if (dwSource1Size==dwSource2Size)
	{
		if (lpcSource1==lpcSource2)
		{
			dwRet=CSTR_EQUAL;
		}else{
			if (lpcSource1 && lpcSource2)
			{
				SIZE_T dwDiffPosition;

				dwDiffPosition=RtlCompareMemory(lpcSource1,lpcSource2,dwSource1Size);
				if (dwDiffPosition==dwSource1Size)
				{
					dwRet=CSTR_EQUAL;
				}else{
					if ((*((BYTE*)(((SIZE_T)lpcSource1)+dwDiffPosition)))>(*((BYTE*)(((SIZE_T)lpcSource2)+dwDiffPosition))))
					{
						dwRet=CSTR_GREATER_THAN;
					}else{
						dwRet=CSTR_LESS_THAN;
					}
				}

				if (pdwDiffPosition) (*pdwDiffPosition)=dwDiffPosition;
			}else{
				if (lpcSource1)
				{//lpcSource2==NULL
					dwRet=CSTR_GREATER_THAN;
				}else{//lpcSource1==NULL
					dwRet=CSTR_LESS_THAN;
				}
			}
		}
	}else{
		if (dwSource1Size<dwSource2Size)
		{
			dwRet=CSTR_LESS_THAN;
		}else{
			dwRet=CSTR_GREATER_THAN;
		}
	}
return(dwRet);
}
*/


#endif // !defined(AFX_MEMORYCOMPARE__H__INCLUDED_)