forked from eiz/SynchronousAudioRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.cpp
More file actions
698 lines (549 loc) · 19.9 KB
/
utility.cpp
File metadata and controls
698 lines (549 loc) · 19.9 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// SynchronousAudioRouter
// Copyright (C) 2015 Mackenzie Straight
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SynchronousAudioRouter. If not, see <http://www.gnu.org/licenses/>.
#include "sar.h"
DRIVER_CANCEL SarCancelHandleQueueIrp;
RTL_GENERIC_COMPARE_ROUTINE SarCompareTableEntry;
RTL_GENERIC_ALLOCATE_ROUTINE SarAllocateTableEntry;
RTL_GENERIC_FREE_ROUTINE SarFreeTableEntry;
RTL_AVL_COMPARE_ROUTINE SarCompareStringTableEntry;
RTL_AVL_ALLOCATE_ROUTINE SarAllocateStringTableEntry;
RTL_AVL_FREE_ROUTINE SarFreeStringTableEntry;
SarDriverExtension *SarGetDriverExtension(PDRIVER_OBJECT driverObject)
{
return (SarDriverExtension *)IoGetDriverObjectExtension(
driverObject, DriverEntry);
}
SarDriverExtension *SarGetDriverExtensionFromIrp(PIRP irp)
{
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
return SarGetDriverExtension(irpStack->DeviceObject->DriverObject);
}
SarControlContext *SarGetControlContextFromFileObject(
SarDriverExtension *extension, PFILE_OBJECT fileObject)
{
SarControlContext *controlContext;
ExAcquireFastMutex(&extension->mutex);
controlContext = (SarControlContext *)SarGetTableEntry(
&extension->controlContextTable, fileObject);
ExReleaseFastMutex(&extension->mutex);
return controlContext;
}
// TODO: gotta go fast
#ifndef SAR_NDIS
SarEndpoint *SarGetEndpointFromIrp(PIRP irp, BOOLEAN retain)
{
PKSFILTER filter = KsGetFilterFromIrp(irp);
PKSFILTERFACTORY factory = KsFilterGetParentFilterFactory(filter);
SarDriverExtension *extension = SarGetDriverExtensionFromIrp(irp);
PVOID restartKey = nullptr;
SarEndpoint *found = nullptr;
ExAcquireFastMutex(&extension->mutex);
FOR_EACH_GENERIC(
&extension->controlContextTable, SarTableEntry,
tableEntry, restartKey) {
SarControlContext *controlContext =
(SarControlContext *)tableEntry->value;
ExAcquireFastMutex(&controlContext->mutex);
PLIST_ENTRY entry = controlContext->endpointList.Flink;
SAR_TRACE("Searching for factory %p", factory);
while (entry != &controlContext->endpointList) {
SarEndpoint *endpoint =
CONTAINING_RECORD(entry, SarEndpoint, listEntry);
entry = entry->Flink;
SAR_TRACE(" Endpoint %p, topology %p", endpoint->filterFactory, endpoint->topologyFilterFactory);
if (endpoint->filterFactory == factory || endpoint->topologyFilterFactory == factory) {
found = endpoint;
if (retain) {
SarRetainControlContext(controlContext);
SarRetainEndpoint(endpoint);
}
break;
}
}
ExReleaseFastMutex(&controlContext->mutex);
if (found) {
break;
}
}
ExReleaseFastMutex(&extension->mutex);
return found;
}
#endif
NTSTATUS SarStringDuplicate(PUNICODE_STRING str, PCUNICODE_STRING src)
{
PWCH buffer = (PWCH)ExAllocatePoolWithTag(
NonPagedPool, src->MaximumLength, SAR_TAG);
if (!buffer) {
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlCopyMemory(buffer, src->Buffer, src->MaximumLength);
str->Length = src->Length;
str->MaximumLength = src->MaximumLength;
str->Buffer = buffer;
return STATUS_SUCCESS;
}
#ifndef SAR_NDIS
NTSTATUS SarReadEndpointRegisters(
SarEndpointRegisters *regs, SarEndpoint *endpoint)
{
SarEndpointProcessContext *context;
NTSTATUS status;
status = SarGetOrCreateEndpointProcessContext(
endpoint, PsGetCurrentProcess(), &context);
if (!NT_SUCCESS(status)) {
return status;
}
__try {
SarEndpointRegisters *source =
&context->registerFileUVA[endpoint->index];
ProbeForRead(
source, sizeof(SarEndpointRegisters), TYPE_ALIGNMENT(ULONG));
RtlCopyMemory(regs, source, sizeof(SarEndpointRegisters));
} __except(EXCEPTION_EXECUTE_HANDLER) {
return GetExceptionCode();
}
return STATUS_SUCCESS;
}
NTSTATUS SarWriteEndpointRegisters(
SarEndpointRegisters *regs, SarEndpoint *endpoint)
{
SarEndpointProcessContext *context;
NTSTATUS status;
status = SarGetOrCreateEndpointProcessContext(
endpoint, PsGetCurrentProcess(), &context);
if (!NT_SUCCESS(status)) {
return status;
}
__try {
SarEndpointRegisters *dest = &context->registerFileUVA[endpoint->index];
ProbeForWrite(dest,
sizeof(SarEndpointRegisters), TYPE_ALIGNMENT(ULONG));
dest->positionRegister = regs->positionRegister;
dest->bufferOffset = regs->bufferOffset;
dest->bufferSize = regs->bufferSize;
dest->notificationCount = regs->notificationCount;
dest->activeChannelCount = regs->activeChannelCount;
MemoryBarrier();
InterlockedExchange((LONG *)&dest->generation, (ULONG)regs->generation);
} __except(EXCEPTION_EXECUTE_HANDLER) {
return GetExceptionCode();
}
return STATUS_SUCCESS;
}
#endif
VOID SarStringFree(PUNICODE_STRING str)
{
ExFreePoolWithTag(str->Buffer, SAR_TAG);
}
NTSTATUS SarReadUserBuffer(PVOID dest, PIRP irp, ULONG size)
{
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
if (irpStack->Parameters.DeviceIoControl.InputBufferLength < size) {
return STATUS_BUFFER_OVERFLOW;
}
__try {
if (irp->RequestorMode != KernelMode) {
ProbeForRead(
irpStack->Parameters.DeviceIoControl.Type3InputBuffer, size,
TYPE_ALIGNMENT(ULONG));
}
RtlCopyMemory(
dest, irpStack->Parameters.DeviceIoControl.Type3InputBuffer, size);
return STATUS_SUCCESS;
} __except(EXCEPTION_EXECUTE_HANDLER) {
return GetExceptionCode();
}
}
NTSTATUS SarWriteUserBuffer(PVOID src, PIRP irp, ULONG size)
{
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
irp->IoStatus.Information = size;
if (irpStack->Parameters.DeviceIoControl.OutputBufferLength == 0) {
return STATUS_BUFFER_OVERFLOW;
}
if (irpStack->Parameters.DeviceIoControl.OutputBufferLength < size) {
return STATUS_BUFFER_TOO_SMALL;
}
__try {
ProbeForWrite(irp->UserBuffer, size, TYPE_ALIGNMENT(ULONG));
RtlCopyMemory(irp->UserBuffer, src, size);
return STATUS_SUCCESS;
} __except(EXCEPTION_EXECUTE_HANDLER) {
return GetExceptionCode();
}
}
void SarInitializeHandleQueue(SarHandleQueue *queue)
{
KeInitializeSpinLock(&queue->lock);
InitializeListHead(&queue->pendingIrps);
InitializeListHead(&queue->pendingItems);
}
NTSTATUS SarTransferQueuedHandle(
PIRP irp, HANDLE kernelTargetProcessHandle, ULONG responseIndex,
HANDLE kernelProcessHandle, HANDLE userHandle, ULONG64 associatedData)
{
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
if (irpStack->Parameters.DeviceIoControl.OutputBufferLength <
sizeof(SarHandleQueueResponse) * (responseIndex + 1)) {
return STATUS_BUFFER_TOO_SMALL;
}
SarHandleQueueResponse *response =
&((SarHandleQueueResponse *)irp->AssociatedIrp.SystemBuffer)
[responseIndex];
response->associatedData = associatedData;
response->handle = nullptr;
HANDLE result = nullptr;
NTSTATUS status;
status = ZwDuplicateObject(
kernelProcessHandle, userHandle,
kernelTargetProcessHandle, &result, 0, 0,
DUPLICATE_SAME_ACCESS);
response->handle = result;
return status;
}
void SarCancelAllHandleQueueIrps(SarHandleQueue *handleQueue)
{
KIRQL irql;
LIST_ENTRY pendingIrqsToCancel;
InitializeListHead(&pendingIrqsToCancel);
KeAcquireSpinLock(&handleQueue->lock, &irql);
if (!IsListEmpty(&handleQueue->pendingIrps)) {
PLIST_ENTRY entry = handleQueue->pendingIrps.Flink;
RemoveEntryList(&handleQueue->pendingIrps);
InitializeListHead(&handleQueue->pendingIrps);
AppendTailList(&pendingIrqsToCancel, entry);
}
KeReleaseSpinLock(&handleQueue->lock, irql);
while (!IsListEmpty(&pendingIrqsToCancel)) {
SarHandleQueueIrp *pendingIrp =
CONTAINING_RECORD(pendingIrqsToCancel.Flink, SarHandleQueueIrp, listEntry);
PIRP irp = pendingIrp->irp;
RemoveEntryList(&pendingIrp->listEntry);
ZwClose(pendingIrp->kernelProcessHandle);
ExFreePoolWithTag(pendingIrp, SAR_TAG);
SAR_INFO("Cancelling IRP %p", irp);
irp->IoStatus.Information = 0;
irp->IoStatus.Status = STATUS_CANCELLED;
IoSetCancelRoutine(irp, nullptr);
IoCompleteRequest(irp, IO_NO_INCREMENT);
}
}
void SarCancelHandleQueueIrp(PDEVICE_OBJECT deviceObject, PIRP irp)
{
UNREFERENCED_PARAMETER(deviceObject);
IoReleaseCancelSpinLock(irp->CancelIrql);
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
SarDriverExtension *extension = SarGetDriverExtensionFromIrp(irp);
SarControlContext *controlContext =
SarGetControlContextFromFileObject(extension, irpStack->FileObject);
PLIST_ENTRY entry;
SarHandleQueueIrp *toCancel = nullptr;
KIRQL irql;
if (!controlContext) {
SAR_WARNING("Received Cancel IRP without control context");
return;
}
KeAcquireSpinLock(&controlContext->handleQueue.lock, &irql);
entry = controlContext->handleQueue.pendingIrps.Flink;
while (entry != &controlContext->handleQueue.pendingIrps) {
SarHandleQueueIrp *pendingIrp =
CONTAINING_RECORD(entry, SarHandleQueueIrp, listEntry);
entry = entry->Flink;
if (pendingIrp->irp == irp) {
RemoveEntryList(&pendingIrp->listEntry);
toCancel = pendingIrp;
break;
}
}
KeReleaseSpinLock(&controlContext->handleQueue.lock, irql);
if (toCancel) {
ZwClose(toCancel->kernelProcessHandle);
ExFreePoolWithTag(toCancel, SAR_TAG);
irp->IoStatus.Information = 0;
irp->IoStatus.Status = STATUS_CANCELLED;
IoCompleteRequest(irp, IO_NO_INCREMENT);
}
}
NTSTATUS SarPostHandleQueue(
SarHandleQueue *queue, HANDLE userHandle, ULONG64 associatedData)
{
NTSTATUS status = STATUS_SUCCESS;
HANDLE kernelProcessHandle = nullptr;
SarHandleQueueIrp *queuedIrp = nullptr;
KIRQL irql;
status = ObOpenObjectByPointerWithTag(
PsGetCurrentProcess(), OBJ_KERNEL_HANDLE,
nullptr, GENERIC_ALL, nullptr,
KernelMode, SAR_TAG, &kernelProcessHandle);
if (!NT_SUCCESS(status)) {
return status;
}
KeAcquireSpinLock(&queue->lock, &irql);
if (!IsListEmpty(&queue->pendingIrps)) {
PLIST_ENTRY entry = queue->pendingIrps.Flink;
queuedIrp =
CONTAINING_RECORD(entry, SarHandleQueueIrp, listEntry);
RemoveEntryList(entry);
}
if (!queuedIrp) {
SarHandleQueueItem *item = (SarHandleQueueItem *)ExAllocatePoolWithTag(
NonPagedPool, sizeof(SarHandleQueueItem), SAR_TAG);
if (!item) {
KeReleaseSpinLock(&queue->lock, irql);
ZwClose(kernelProcessHandle);
return STATUS_INSUFFICIENT_RESOURCES;
}
item->associatedData = associatedData;
item->userHandle = userHandle;
item->kernelProcessHandle = kernelProcessHandle;
InsertTailList(&queue->pendingItems, &item->listEntry);
}
KeReleaseSpinLock(&queue->lock, irql);
if (queuedIrp) {
status = SarTransferQueuedHandle(
queuedIrp->irp, queuedIrp->kernelProcessHandle,
0, kernelProcessHandle, userHandle, associatedData);
SAR_DEBUG("complete handle queue");
queuedIrp->irp->IoStatus.Status = status;
queuedIrp->irp->IoStatus.Information = sizeof(SarHandleQueueResponse);
IoSetCancelRoutine(queuedIrp->irp, nullptr);
IoCompleteRequest(queuedIrp->irp, IO_NO_INCREMENT);
ZwClose(kernelProcessHandle);
ExFreePoolWithTag(queuedIrp, SAR_TAG);
}
return status;
}
NTSTATUS SarWaitHandleQueue(SarHandleQueue *queue, PIRP irp)
{
NTSTATUS status = STATUS_SUCCESS;
HANDLE kernelProcessHandle = nullptr;
PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(irp);
DWORD maxItems = irpStack->Parameters.DeviceIoControl.OutputBufferLength /
sizeof(SarHandleQueueResponse);
DWORD nextItem = 0;
LIST_ENTRY toComplete;
KIRQL irql;
InitializeListHead(&toComplete);
irp->IoStatus.Information = 0;
if (maxItems == 0) {
irp->IoStatus.Information = sizeof(SarHandleQueueResponse);
irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return STATUS_BUFFER_TOO_SMALL;
}
status = ObOpenObjectByPointerWithTag(
PsGetCurrentProcess(), OBJ_KERNEL_HANDLE,
nullptr, GENERIC_ALL, nullptr,
KernelMode, SAR_TAG, &kernelProcessHandle);
if (!NT_SUCCESS(status)) {
return status;
}
KeAcquireSpinLock(&queue->lock, &irql);
while (!IsListEmpty(&queue->pendingItems) && nextItem < maxItems) {
PLIST_ENTRY entry = RemoveHeadList(&queue->pendingItems);
InsertTailList(&toComplete, entry);
nextItem++;
}
if (IsListEmpty(&toComplete)) {
SarHandleQueueIrp *queuedIrp = (SarHandleQueueIrp *)
ExAllocatePoolWithTag(
NonPagedPool, sizeof(SarHandleQueueIrp), SAR_TAG);
if (!queuedIrp) {
KeReleaseSpinLock(&queue->lock, irql);
ZwClose(kernelProcessHandle);
return STATUS_INSUFFICIENT_RESOURCES;
}
IoMarkIrpPending(irp);
IoSetCancelRoutine(irp, SarCancelHandleQueueIrp);
queuedIrp->irp = irp;
queuedIrp->kernelProcessHandle = kernelProcessHandle;
InsertTailList(&queue->pendingIrps, &queuedIrp->listEntry);
status = STATUS_PENDING;
}
KeReleaseSpinLock(&queue->lock, irql);
if (!IsListEmpty(&toComplete)) {
nextItem = 0;
while (!IsListEmpty(&toComplete)) {
PLIST_ENTRY entry = RemoveHeadList(&toComplete);
SarHandleQueueItem *queueItem =
CONTAINING_RECORD(entry, SarHandleQueueItem, listEntry);
status = SarTransferQueuedHandle(
irp, kernelProcessHandle, nextItem++,
queueItem->kernelProcessHandle, queueItem->userHandle,
queueItem->associatedData);
ZwClose(queueItem->kernelProcessHandle);
ExFreePoolWithTag(queueItem, SAR_TAG);
irp->IoStatus.Information += sizeof(SarHandleQueueResponse);
if (!NT_SUCCESS(status)) {
break;
}
}
ZwClose(kernelProcessHandle);
}
return status;
}
RTL_GENERIC_COMPARE_RESULTS NTAPI SarCompareTableEntry(
PRTL_GENERIC_TABLE table, PVOID lhs, PVOID rhs)
{
UNREFERENCED_PARAMETER(table);
SarTableEntry *le = (SarTableEntry *)lhs;
SarTableEntry *re = (SarTableEntry *)rhs;
return le->key < re->key ? GenericLessThan :
le->key == re->key ? GenericEqual : GenericGreaterThan;
}
PVOID NTAPI SarAllocateTableEntry(PRTL_GENERIC_TABLE table, CLONG byteSize)
{
UNREFERENCED_PARAMETER(table);
return ExAllocatePoolWithTag(NonPagedPool, byteSize, SAR_TAG);
}
VOID NTAPI SarFreeTableEntry(PRTL_GENERIC_TABLE table, PVOID buffer)
{
UNREFERENCED_PARAMETER(table);
ExFreePoolWithTag(buffer, SAR_TAG);
}
VOID SarInitializeTable(PRTL_GENERIC_TABLE table)
{
RtlInitializeGenericTable(table,
SarCompareTableEntry, SarAllocateTableEntry, SarFreeTableEntry,
nullptr);
}
NTSTATUS SarInsertTableEntry(PRTL_GENERIC_TABLE table, PVOID key, PVOID value)
{
SarTableEntry entry = { key, value };
BOOLEAN isNew = FALSE;
PVOID result =
RtlInsertElementGenericTable(table, &entry, sizeof(entry), &isNew);
if (!result) {
return STATUS_INSUFFICIENT_RESOURCES;
}
if (!isNew) {
return STATUS_OBJECT_NAME_EXISTS;
}
return STATUS_SUCCESS;
}
BOOLEAN SarRemoveTableEntry(PRTL_GENERIC_TABLE table, PVOID key)
{
SarTableEntry entry = { key, nullptr };
return RtlDeleteElementGenericTable(table, &entry);
}
PVOID SarGetTableEntry(PRTL_GENERIC_TABLE table, PVOID key)
{
SarTableEntry entry = { key, nullptr };
SarTableEntry *result =
(SarTableEntry *)RtlLookupElementGenericTable(table, &entry);
if (result) {
return result->value;
}
return nullptr;
}
RTL_GENERIC_COMPARE_RESULTS NTAPI SarCompareStringTableEntry(
PRTL_AVL_TABLE table, PVOID lhs, PVOID rhs)
{
UNREFERENCED_PARAMETER(table);
SarStringTableEntry *le = (SarStringTableEntry *)lhs;
SarStringTableEntry *re = (SarStringTableEntry *)rhs;
LONG result = RtlCompareUnicodeString(&le->key, &re->key, TRUE);
return result < 0 ? GenericLessThan :
result > 0 ? GenericGreaterThan : GenericEqual;
}
NTSTATUS SarInsertStringTableEntry(
PRTL_AVL_TABLE table, PUNICODE_STRING key, PVOID value)
{
NTSTATUS status = STATUS_SUCCESS;
SarStringTableEntry entry = { {}, value };
BOOLEAN isNew = FALSE;
PVOID result = nullptr;
status = SarStringDuplicate(&entry.key, key);
if (!NT_SUCCESS(status)) {
goto err;
}
result = RtlInsertElementGenericTableAvl(
table, &entry, sizeof(entry), &isNew);
if (!result) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto err;
}
if (!isNew) {
status = STATUS_OBJECT_NAME_EXISTS;
goto err;
}
return STATUS_SUCCESS;
err:
if (entry.key.Buffer) {
SarStringFree(&entry.key);
}
return status;
}
BOOLEAN SarRemoveStringTableEntry(
PRTL_AVL_TABLE table, PUNICODE_STRING key)
{
SarStringTableEntry entry = { *key, nullptr };
return RtlDeleteElementGenericTableAvl(table, &entry);
}
PVOID NTAPI SarAllocateStringTableEntry(PRTL_AVL_TABLE table, CLONG byteSize)
{
UNREFERENCED_PARAMETER(table);
return ExAllocatePoolWithTag(NonPagedPool, byteSize, SAR_TAG);
}
VOID NTAPI SarFreeStringTableEntry(PRTL_AVL_TABLE table, PVOID buffer)
{
UNREFERENCED_PARAMETER(table);
SarStringTableEntry *entry = (SarStringTableEntry *)(
(PCHAR)buffer + sizeof(RTL_BALANCED_LINKS));
if (entry->key.Buffer) {
SarStringFree(&entry->key);
}
ExFreePoolWithTag(buffer, SAR_TAG);
}
PVOID SarGetStringTableEntry(PRTL_AVL_TABLE table, PCUNICODE_STRING key)
{
SarStringTableEntry entry = { *key, nullptr };
SarStringTableEntry *result =
(SarStringTableEntry *)RtlLookupElementGenericTableAvl(table, &entry);
if (result) {
return result->value;
}
return nullptr;
}
VOID SarInitializeStringTable(PRTL_AVL_TABLE table)
{
RtlInitializeGenericTableAvl(table,
SarCompareStringTableEntry,
SarAllocateStringTableEntry,
SarFreeStringTableEntry,
nullptr);
}
VOID SarClearStringTable(PRTL_AVL_TABLE table, VOID (*freeCb)(PVOID))
{
PVOID entry;
while ((entry = RtlGetElementGenericTableAvl(table, 0)) != NULL) {
SarStringTableEntry *stEntry = (SarStringTableEntry *)entry;
PVOID value = stEntry->value;
RtlDeleteElementGenericTableAvl(table, entry);
freeCb(value);
}
NT_ASSERT(RtlIsGenericTableEmptyAvl(table));
}
NTSTATUS SarCopyProcessUser(PEPROCESS process, PTOKEN_USER *outTokenUser)
{
NT_ASSERT(process);
NT_ASSERT(outTokenUser);
PACCESS_TOKEN token = PsReferencePrimaryToken(process);
NTSTATUS status = SeQueryInformationToken(
token, TokenUser, (PVOID *)outTokenUser);
PsDereferencePrimaryToken(token);
return status;
}