app.avl.c

  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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <ntddk.h>
#include <app.avl.h>
RTL_GENERIC_COMPARE_RESULTS
CompareRoutine(
__in struct _RTL_AVL_TABLE *Table,
__in PVOID FirstStruct,
__in PVOID SecondStruct
)
{
int i, j, k1, k2;
appBLOCK A1, A2;
A1 = ((struct appTRIPLET*)FirstStruct)->A;
k1 = ((struct appTRIPLET*)FirstStruct)->k;
A2 = ((struct appTRIPLET*)SecondStruct)->A;
k2 = ((struct appTRIPLET*)SecondStruct)->k;
if (k1 == 1 && (A1 >= A2 && A1 < A2 + k2)) return GenericEqual;
if (k2 == 1 && (A2 >= A1 && A2 < A1 + k1)) return GenericEqual;
if (A1 > A2) return GenericGreaterThan;
if (A1 < A2) return GenericLessThan;
return GenericEqual;
}
PVOID
Allocate_Routine(
__in struct _RTL_AVL_TABLE *Table,
__in CLONG ByteSize
)
{
void *temp;
temp = malloc(ByteSize);
return temp;
}
VOID
FreeRoutine(
__in struct _RTL_AVL_TABLE *Table,
__in PVOID Buffer
)
{
free(Buffer);
}
appTRIPLET*
addNode(appBLOCK A, appBLOCK B, appNUMBER k, PRTL_AVL_TABLE Table)
{
appTRIPLET T = {A, B, k}, *ptr;
BOOLEAN ok;
ptr = RtlInsertElementGenericTableAvl(Table, &T, sizeof(T), &ok);
return ptr;
}
BOOLEAN
existNode(appBLOCK A, PRTL_AVL_TABLE Table)
{
appTRIPLET T, *P;
T.A = A;
T.k = 1;
P = (appTRIPLET*)RtlLookupElementGenericTableAvl(Table, &T);
if(P == NULL) return FALSE;
return TRUE;
}
appTRIPLET*
findNode(appBLOCK A, PRTL_AVL_TABLE Table)
{
appTRIPLET T, *P;
T.A = A;
T.k = 1;
P = RtlLookupElementGenericTableAvl(Table, &T);
return P;
}
BOOLEAN
deleteNode(appBLOCK A, PRTL_AVL_TABLE Table)
{
BOOLEAN ok;
appTRIPLET *P = findNode(A, Table);
ok = RtlDeleteElementGenericTableAvl(Table, P);
return ok;
}
appTRIPLET*
nextNode(appBLOCK A, PRTL_AVL_TABLE Table)
{
appTRIPLET *ptr, *temp;
PVOID restart;
temp = findNode(A, Table);
if(temp != NULL)
{
restart = (PVOID)((char*)(temp) - 0x20);
ptr = (appTRIPLET*)RtlEnumerateGenericTableWithoutSplayingAvl(Table, &restart);
return ptr;
}
temp = addNode(A, (appBLOCK)NULL, 1, Table);
restart = (PVOID)((char*)(temp) - 0x20);
ptr = (appTRIPLET*)RtlEnumerateGenericTableWithoutSplayingAvl(Table, &restart);
deleteNode(A, Table);
return ptr;
}
appBLOCK
checkNode(appBLOCK A, PRTL_AVL_TABLE Table)
{
appTRIPLET T, *P = findNode(A, Table);
return P->B + (A - P->A);
}
BOOLEAN
mapNode(appBLOCK A, appBLOCK B, appNUMBER k, PRTL_AVL_TABLE Table)
{
BOOLEAN ok;
ULONG TableLength = RtlNumberGenericTableElementsAvl(Table);
appTRIPLET T, Left, Right, *PLeft, *PRight, *PL, *PR, *temp, *ptr;
PVOID restart;
//Find the edge intervals
Left.A = A;
Left.k = 1;
Right.A = A + k;
Right.k = 1;
if(existNode(Left.A, Table))
{
PLeft = findNode(Left.A, Table);
PL = PLeft;
}
else
{
PLeft = nextNode(Left.A, Table);
PL = NULL;
}
if(existNode(Right.A, Table))
{
PR = findNode(Right.A, Table);
PRight = nextNode(Right.A, Table);
}
else
{
PRight = nextNode(Right.A, Table);
PR = NULL;
}
//Split edge intervals
if(PL != NULL)
{
Left.A = PL->A;
Left.B = PL->B;
Left.k = A - PL->A;
PL = &Left;
}
if(PR != NULL)
{
Right.A = A + k;
Right.B = PR->B + ((A + k) - PR->A);
Right.k = PR->A + PR->k - (A + k);
PR = &Right;
}
// Delete crossing intervals
restart = (PVOID)((char*)(PLeft) - 0x20);
for(ptr = PLeft; ptr != PRight; )
{
temp = ptr;
ptr = nextNode(ptr->A, Table);
RtlDeleteElementGenericTableAvl(Table, temp);
}
//Add the main interval
T.A = A;
T.B = B;
T.k = k;
RtlInsertElementGenericTableAvl(Table, &T, sizeof(T), &ok);
//Add the splitted intervals
if(PL != NULL) addNode(PL->A, PL->B, PL->k, Table);
if(PR != NULL) addNode(PR->A, PR->B, PR->k, Table);
return ok;
}
BOOLEAN
unmapNode(appBLOCK A, appNUMBER k, PRTL_AVL_TABLE Table)
{
return FALSE;
// BOOLEAN ok;
// appTRIPLET *P = findNode(A, Table);
// ok = RtlDeleteElementGenericTableAvl(Table, P);
// return ok;
}