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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#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);
if(P == NULL) return (appBLOCK)NULL;
return P->B + (A - P->A);
}
appTRIPLET**
checkArrNode(appBLOCK A, appNUMBER k, PRTL_AVL_TABLE Table)
{
ULONG i;
appBLOCK checkPoint;
appTRIPLET PointLeft, PointRight,
*IterLeft, *IterRight,
*EdgeLeft, *EdgeRight,
*EdgeInnerLeft, *EdgeInnerRight,
*tmp, *ptr,
**Buffer;
PVOID restart;
BOOLEAN ok;
//Searching points
PointLeft.A = A;
PointLeft.k = 1;
PointRight.A = A + k - 1;
PointRight.k = 1;
//Check edge points
if(!existNode(PointLeft.A, Table) || !existNode(PointRight.A - 1, Table))
{
return NULL;
}
//Iterate crossing intervals, check interruptions
IterLeft = findNode(PointLeft.A, Table);
IterRight = nextNode(PointRight.A, Table);
EdgeLeft = IterLeft;
EdgeRight = findNode(PointRight.A, Table);
restart = (PVOID)((char*)(IterLeft) - 0x20);
ptr = IterLeft;
checkPoint = ptr->A;
for(i = 0; ptr != IterRight; i++)
{
if(ptr->A != checkPoint)
{
return NULL;
}
checkPoint += ptr->k;
ptr = nextNode(ptr->A, Table);
}
//Allocate memory, insert pointers
Buffer = (appTRIPLET**) malloc(sizeof(appTRIPLET*) * (i + 3));
EdgeInnerLeft = (appTRIPLET* ) malloc(sizeof(appTRIPLET ));
EdgeInnerRight = (appTRIPLET* ) malloc(sizeof(appTRIPLET ));
//Left Edge
restart = (PVOID)((char*)(IterLeft) - 0x20);
ptr = IterLeft;
if(IterLeft->A == A && IterLeft->k == k) // case 1
{
free(EdgeInnerLeft);
printf("Buffer[%d] = NULL\n", i); Buffer[i] = NULL;
printf("Buffer[%d] = ptr\n", i+1); Buffer[i+1] = ptr;
printf("Buffer[%d] = NULL\n", i+2); Buffer[i+2] = NULL;
printf("Buffer[%d] = NULL\n", i+3); Buffer[i+3] = NULL;
return Buffer;
}
else
if(A < IterLeft->A + IterLeft->k || A + k < IterLeft->A + IterLeft->k) // case 2 3 4
{
EdgeInnerLeft->A = A;
EdgeInnerLeft->B = IterLeft->B + (A - IterLeft->A);
EdgeInnerLeft->k = k;
printf("Buffer[%d] = NULL\n", i); Buffer[i] = EdgeInnerLeft;
printf("Buffer[%d] = NULL\n", i+1); Buffer[i+1] = NULL;
printf("Buffer[%d] = NULL\n", i+2); Buffer[i+2] = NULL;
printf("Buffer[%d] = NULL\n", i+3); Buffer[i+3] = NULL;
Return Buffer;
}
// case 5 6 7 8
EdgeInnerLeft->A = A;
EdgeInnerLeft->B = IterLeft->B + (A - IterLeft->A);
EdgeInnerLeft->k = IterLeft->k - (A - IterLeft->A);
printf("Buffer[%d] = EdgeInnerLeft\n", i); Buffer[i] = EdgeInnerLeft;
i++;
printf("Buffer[%d] = NULL\n", i); Buffer[i] = NULL;
//Middle
for(i = 0; ptr != IterRight; i++, ptr = nextNode(ptr->A, Table))
{
else
if(nextNode(ptr->A, Table) == IterRight)
{
printf("Buffer[%d] = NULL\n", i); Buffer[i] = NULL;
i++;
if(A == ptr->A)
{
free(EdgeInnerRight);
}
else
if(A + k == ptr->A + ptr->k)
{
free(EdgeInnerRight);
printf("Buffer[%d] = ptr\n", i); Buffer[i] = ptr;
i++;
}
else
{
EdgeInnerRight->A = ptr->A;
EdgeInnerRight->B = ptr->B;
EdgeInnerRight->k = A + k - ptr->A;
printf("Buffer[%d] = EdgeInnerRight\n", i); Buffer[i] = EdgeInnerRight;
i++;
}
printf("Buffer[%d] = NULL\n", i); Buffer[i] = NULL;
break;
}
else
{
printf("Buffer[%d] = ptr\n", i); Buffer[i] = ptr;
}
}
//Right Edge
printf("---Buffer[0]->A = %d\n", Buffer[0]->A);
printf("return Buffer\n\n"); return Buffer;
}
BOOLEAN
mapNode(appBLOCK A, appBLOCK B, appNUMBER k, PRTL_AVL_TABLE Table)
{
BOOLEAN ok;
appTRIPLET New,
PointLeft, PointRight,
*IterLeft, *IterRight,
*EdgeLeft, *EdgeRight,
EdgeOuterLeft, EdgeOuterRight,
*tmp, *ptr;
PVOID restart;
//Find the edge intervals
PointLeft.A = A;
PointLeft.k = 1;
PointRight.A = A + k;
PointRight.k = 1;
if(existNode(PointLeft.A, Table))
{
IterLeft = findNode(PointLeft.A, Table);
EdgeLeft = IterLeft;
}
else
{
IterLeft = nextNode(PointLeft.A, Table);
EdgeLeft = NULL;
}
if(existNode(PointRight.A, Table))
{
EdgeRight = findNode(PointRight.A, Table);
IterRight = nextNode(PointRight.A, Table);
}
else
{
IterRight = nextNode(PointRight.A, Table);
EdgeRight = NULL;
}
//Split edge intervals
if(EdgeLeft != NULL)
{
EdgeOuterLeft.A = EdgeLeft->A;
EdgeOuterLeft.B = EdgeLeft->B;
EdgeOuterLeft.k = PointLeft.A - EdgeLeft->A;
// EdgeLeft = &EdgeOuterLeft;
}
if(EdgeRight != NULL)
{
EdgeOuterRight.A = PointRight.A;
EdgeOuterRight.B = EdgeRight->B + (PointRight.A - EdgeRight->A);
EdgeOuterRight.k = (EdgeRight->A + EdgeRight->k) - PointRight.A;
// EdgeRight = &PointRight;
}
// Delete crossing intervals
restart = (PVOID)((char*)(IterLeft) - 0x20);
for(ptr = IterLeft; ptr != IterRight; )
{
tmp = ptr;
ptr = nextNode(ptr->A, Table);
RtlDeleteElementGenericTableAvl(Table, tmp);
}
//Add the main interval
New.A = A;
New.B = B;
New.k = k;
RtlInsertElementGenericTableAvl(Table, &New, sizeof(New), &ok);
//Add the splitted intervals
if(EdgeLeft != NULL) addNode(EdgeOuterLeft.A, EdgeOuterLeft.B, EdgeOuterLeft.k, Table);
if(EdgeRight != NULL) addNode(EdgeOuterRight.A, EdgeOuterRight.B, EdgeOuterRight.k, Table);
return ok;
}
BOOLEAN
unmapNode(appBLOCK A, appNUMBER k, PRTL_AVL_TABLE Table)
{
BOOLEAN ok;
mapNode(A, (appBLOCK)NULL, k, Table);
ok = deleteNode(A, Table);
return ok;
}