-
Notifications
You must be signed in to change notification settings - Fork 29
/
desktop_hook.c
992 lines (740 loc) · 24.4 KB
/
desktop_hook.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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
/*
Copyright (C) 2011 Jay Satiro <[email protected]>
All rights reserved.
This file is part of GetHooks.
GetHooks 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.
GetHooks 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 GetHooks. If not, see <https://www.gnu.org/licenses/>.
*/
/**
This file contains functions for a desktop hook store (linked list of desktop and hook information).
Each function is documented in the comment block above its definition.
There are multiple desktop hook stores: Each snapshot has its own desktop hook store.
Each desktop hook store depends on all the other information in the snapshot (GUI, SPI, etc.)
-
create_desktop_hook_store()
Create a desktop hook store and its descendants or die.
-
-
add_desktop_hook_item()
Create a desktop hook item and append it to the desktop hook store's linked list.
-
-
match_hook_process_name()
Match a hook struct's associated GUI threads' process names to the passed in name.
-
-
match_hook_process_id()
Match a hook struct's associated GUI threads' process ids to the passed in pid.
-
-
match_hook_thread_id()
Match a hook struct's associated GUI threads' ids to the passed in tid.
-
-
is_HOOK_id_wanted()
Check the user-specified configuration to determine if a HOOK id should be processed.
-
-
is_hook_wanted()
Check the user-specified configuration to determine if a hook struct should be processed.
-
-
compare_hook()
Compare two hook structs according their HANDLEENTRY info.
-
-
init_desktop_hook_store()
Initialize the desktop hook store by recording the hooks for each desktop.
-
-
print_hook_anomalies()
Print any anomalies found in a hook struct.
-
-
print_hook()
Print a hook struct.
-
-
print_hook_array()
Print a desktop hook item's array of hook structs.
-
-
print_desktop_hook_item()
Print an item from a desktop hook store's linked list.
-
-
print_desktop_hook_store()
Print a desktop hook store and all its descendants.
-
-
free_desktop_hook_item()
Free a desktop hook item (dirty -- linked list isn't updated).
-
-
free_desktop_hook_store()
Free a desktop hook store and all its descendants.
-
*/
#include <stdio.h>
#include "util.h"
#include "snapshot.h"
#include "desktop_hook.h"
/* the global stores */
#include "global.h"
static struct desktop_hook_item *add_desktop_hook_item(
struct desktop_hook_list *const store, // in
struct desktop_item *const desktop // in
);
static void free_desktop_hook_item(
struct desktop_hook_item **const in // in deref
);
/* create_desktop_hook_store()
Create a desktop hook store and its descendants or die.
*/
void create_desktop_hook_store(
struct desktop_hook_list **const out // out deref
)
{
struct desktop_hook_list *desktop_hooks = NULL;
FAIL_IF( !out );
FAIL_IF( *out );
/* allocate a desktop hook store */
desktop_hooks = must_calloc( 1, sizeof( *desktop_hooks ) );
*out = desktop_hooks;
return;
}
/* add_desktop_hook_item()
Create a desktop hook item and append it to the desktop hook store's linked list.
returns on success a pointer to the desktop hook item that was added to the list.
if there is already an existing item with the same desktop a pointer to it is returned.
returns NULL on fail
*/
static struct desktop_hook_item *add_desktop_hook_item(
struct desktop_hook_list *const store, // in
struct desktop_item *const desktop // in
)
{
struct desktop_hook_item *item = NULL;
FAIL_IF( !store );
FAIL_IF( !desktop );
/* check if there is already an entry for this desktop */
for( item = store->head; item; item = item->next )
{
if( item->desktop == desktop )
return item;
}
/* create a new item and add it to the list */
item = must_calloc( 1, sizeof( *item ) );
item->desktop = desktop;
/* the allocated/maximum number of elements in the array pointed to by hook.
65535 is the maximum number of user objects
https://docs.microsoft.com/en-us/windows/win32/sysinfo/user-objects?redirectedfrom=MSDN
https://web.archive.org/web/20111211080445/http://blogs.technet.com/b/markrussinovich/archive/2010/02/24/3315174.aspx
*/
item->hook_max = 65535;
/* allocate an array of hook structs */
item->hook = must_calloc( item->hook_max, sizeof( *item->hook ) );
/* the desktop hook item is initialized. add the new item to the end of the list */
if( !store->head )
{
store->head = item;
store->tail = item;
}
else
{
store->tail->next = item;
store->tail = item;
}
return item;
}
/* match_hook_process_name()
Match a hook struct's associated GUI threads' process names to the passed in name.
returns nonzero on success ('name' matched one of the hook struct's GUI thread process names)
*/
int match_hook_process_name(
const struct hook *const hook, // in
const WCHAR *const name // in
)
{
FAIL_IF( !hook );
FAIL_IF( !name );
if( ( hook->owner && match_gui_process_name( hook->owner, name ) )
|| ( hook->origin && match_gui_process_name( hook->origin, name ) )
|| ( hook->target && match_gui_process_name( hook->target, name ) )
)
return TRUE;
else
return FALSE;
}
/* match_hook_process_id()
Match a hook struct's associated GUI threads' process ids to the passed in pid.
returns nonzero on success ('pid' matched one of the hook struct's GUI threads' process pids)
*/
int match_hook_process_id(
const struct hook *const hook, // in
const unsigned __int64 pid // in
)
{
FAIL_IF( !hook );
if( ( hook->owner && match_gui_process_id( hook->owner, pid ) )
|| ( hook->origin && match_gui_process_id( hook->origin, pid ) )
|| ( hook->target && match_gui_process_id( hook->target, pid ) )
)
return TRUE;
else
return FALSE;
}
/* match_hook_thread_id()
Match a hook struct's associated GUI threads' ids to the passed in tid.
returns nonzero on success ('tid' matched one of the hook struct's GUI threads' ids)
*/
int match_hook_thread_id(
const struct hook *const hook, // in
const unsigned __int64 tid // in
)
{
FAIL_IF( !hook );
if( ( hook->owner && match_gui_thread_id( hook->owner, tid ) )
|| ( hook->origin && match_gui_thread_id( hook->origin, tid ) )
|| ( hook->target && match_gui_thread_id( hook->target, tid ) )
)
return TRUE;
else
return FALSE;
}
/* is_HOOK_id_wanted()
Check the user-specified configuration to determine if a HOOK id should be processed.
The user can filter hook ids (eg WH_MOUSE).
returns nonzero if the HOOK id should be processed
*/
int is_HOOK_id_wanted(
const int id // in
)
{
/* if there is a list of HOOK ids to include/exclude */
if( G->config->hooklist->init_time
&& ( ( G->config->hooklist->type == LIST_INCLUDE_HOOK )
|| ( G->config->hooklist->type == LIST_EXCLUDE_HOOK )
)
)
{
unsigned yes = 0;
const struct list_item *item = NULL;
for( item = G->config->hooklist->head; ( item && !yes ); item = item->next )
yes = ( item->id == id ); // match HOOK id
if( ( yes && ( G->config->hooklist->type == LIST_EXCLUDE_HOOK ) )
|| ( !yes && ( G->config->hooklist->type == LIST_INCLUDE_HOOK ) )
)
return FALSE; // the HOOK id is not wanted
}
return TRUE; // the HOOK id is wanted
}
/* is_hook_wanted()
Check the user-specified configuration to determine if a hook struct should be processed.
The user can filter hooks (eg WH_MOUSE) and programs (eg notepad.exe).
init_desktop_hook_store() calls this function to set hook->ignore when initializing each hook.
This function should not access hook->ignore.
returns nonzero if the hook struct should be processed
*/
int is_hook_wanted(
const struct hook *const hook // in
)
{
FAIL_IF( !hook );
/* if the user requested to ignore internal hooks then any HOOK (aka hook->object) with the
same owner, origin and target thread info should be ignored.
HOOK owner GUI thread info kernel address: hook->entry.pOwner
The related user mode thread info obtained by this program: hook->owner
HOOK origin GUI thread info kernel address: hook->object.pti
The related user mode thread info obtained by this program: hook->origin
HOOK target GUI thread info kernel address: hook->object.ptiHooked
The related user mode thread info obtained by this program: hook->target
*/
if( ( G->config->flags & CFG_IGNORE_INTERNAL_HOOKS )
&& hook->entry.pOwner
&& ( hook->owner == hook->origin )
&& ( hook->entry.pOwner == hook->object.pti )
&& ( hook->owner == hook->target )
&& ( hook->entry.pOwner == hook->object.ptiHooked )
)
return FALSE;
/* if the user requested to ignore known hooks then any HOOK (aka hook->object) with known
owner, origin and target thread user mode info should be ignored.
as a special case if the HOOK is global and valid then the target is considered known even
though hook->target and hook->object.ptiHooked don't point to anything.
*/
if( ( G->config->flags & CFG_IGNORE_KNOWN_HOOKS )
&& hook->owner
&& hook->origin
&& ( hook->target
|| ( ( hook->object.flags & HF_GLOBAL ) && !hook->object.ptiHooked )
)
)
return FALSE;
/* if the user requested to ignore targeted hooks then any HOOK (aka hook->object) with a
target thread should be ignored.
*/
if( ( G->config->flags & CFG_IGNORE_TARGETED_HOOKS )
&& ( hook->target || hook->object.ptiHooked )
)
return FALSE;
/* if there is a list of programs to include/exclude */
if( G->config->proglist->init_time
&& ( ( G->config->proglist->type == LIST_INCLUDE_PROG )
|| ( G->config->proglist->type == LIST_EXCLUDE_PROG )
)
)
{
unsigned yes = 0;
const struct list_item *item = NULL;
for( item = G->config->proglist->head; ( item && !yes ); item = item->next )
{
if( item->name ) // match program name
yes = !!match_hook_process_name( hook, item->name );
else // match PID/TID
{
yes = !!match_hook_process_id( hook, (unsigned __int64)item->id );
if( !yes )
yes = !!match_hook_thread_id( hook, (unsigned __int64)item->id );
}
}
if( ( yes && ( G->config->proglist->type == LIST_EXCLUDE_PROG ) )
|| ( !yes && ( G->config->proglist->type == LIST_INCLUDE_PROG ) )
)
return FALSE; // the hook is not wanted
}
return is_HOOK_id_wanted( hook->object.iHook );
}
/* compare_hook()
Compare two hook structs according their HANDLEENTRY info.
Compare the HANDLEENTRY pHead, which was the kernel address of the associated HOOK struct
at that point in time (before the HOOK was copied to hook->object). If the pHead is the same then
check what was the HANDLEENTRY's index in the list (aheList) at that point in time (before the
HANDLEENTRY was copied to hook->entry). If the index is the same then check the HOOK's handle.
If the handles compare the same as well then assume the two hook structs have information on the
same HOOK. This will happen when comparing the hook info from two different system snapshots.
There is no way to passively determine whether or not a HOOK is actually the same. This function
essentially provides a best guess. Windows reuses the memory locations and all the other associated
information can change.
qsort() callback: this function is called to sort the hook array
returns -1 if p1's entry is less than p2's entry
returns 1 if p1's entry is greater than p2's entry
returns 0 if p1's entry is the same as p2's entry
*/
int compare_hook(
const void *const p1, // in
const void *const p2 // in
)
{
const struct hook *const a = p1;
const struct hook *const b = p2;
if( a->entry.pHead < b->entry.pHead )
return -1;
else if( a->entry.pHead > b->entry.pHead )
return 1;
else if( a->entry_index < b->entry_index )
return -1;
else if( a->entry_index > b->entry_index )
return 1;
else if( a->object.head.h < b->object.head.h )
return -1;
else if( a->object.head.h > b->object.head.h )
return 1;
else
return 0;
}
/* init_desktop_hook_store()
Initialize the desktop hook store by recording the hooks for each desktop.
The desktop hook store depends on all global stores.
The spi and gui info from its parent snapshot store is used to identify the threads associated with
each hook and is optional.
returns nonzero on success
*/
int init_desktop_hook_store(
const struct snapshot *const parent // in
)
{
unsigned i = 0;
__int64 first_fail_time = 0;
struct desktop_hook_list *store = NULL;
struct desktop_hook_item *item = NULL;
FAIL_IF( !G ); // The global store must exist.
FAIL_IF( !G->prog->init_time ); // The program store must be initialized.
FAIL_IF( !G->config->init_time ); // The configuration store must be initialized.
FAIL_IF( !G->desktops->init_time ); // The desktop store must be initialized.
FAIL_IF( !parent ); // The snapshot parent of the desktop_hook store must always be passed in.
/* valid spi and gui arrays are expected if passive mode isn't enabled */
FAIL_IF( !parent->init_time_spi && !( G->config->flags & CFG_COMPLETELY_PASSIVE ) );
FAIL_IF( !parent->init_time_gui && !( G->config->flags & CFG_COMPLETELY_PASSIVE ) );
FAIL_IF( GetCurrentThreadId() != G->prog->dwMainThreadId ); // main thread only
retry:
item = NULL;
store = parent->desktop_hooks;
/* this store is reused. do a soft reset */
store->init_time = 0;
/* if the desktop hook store does not have a list of desktops yet create it */
if( !store->head )
{
struct desktop_item *current = NULL;
/* add the desktops from the global desktop store */
for( current = G->desktops->head; current; current = current->next )
add_desktop_hook_item( store, current );
}
else // the desktop hook list already exists. reuse it.
{
struct desktop_hook_item *current = NULL;
/* soft reset on each desktop hook item's array of hooks */
for( current = store->head; current; current = current->next )
current->hook_count = 0; // soft reset of hook array
}
SwitchToThread();
/* for every handle if it is a HOOK then add it to the desktop's hook array */
for( i = 0; i < *G->prog->pcHandleEntries; ++i )
{
/* copy the HANDLEENTRY struct from the list of entries in the shared info section.
the info may change so it can't just be pointed to.
*/
HANDLEENTRY entry = G->prog->pSharedInfo->aheList[ i ];
struct hook *hook = NULL;
if( G->config->verbose >= 9 )
{
printf( "\n*G->prog->pcHandleEntries: %lu\n", *G->prog->pcHandleEntries );
printf( "Now printing G->prog->pSharedInfo->aheList[ %u ]\n", i );
print_HANDLEENTRY( &entry );
}
if( entry.bType != TYPE_HOOK ) /* not for a HOOK object */
continue;
/* Check to see if the HOOK is located on a desktop we're attached to */
for( item = store->head; item; item = item->next )
{
if( ( (uintptr_t)entry.pHead
< ( (uintptr_t)item->desktop->pDeskInfo->pvDesktopLimit - sizeof( HOOK ) )
)
&& ( (uintptr_t)entry.pHead >= (uintptr_t)item->desktop->pDeskInfo->pvDesktopBase )
) /* The HOOK is on an accessible desktop */
break;
}
if( !item ) /* The HOOK is on an inaccessible desktop */
{
if( G->config->verbose >= 9 )
printf( "The above HANDLEENTRY points to a HOOK on an inaccessible desktop.\n" );
continue;
}
else
{
if( G->config->verbose >= 9 )
{
printf( "The above HANDLEENTRY points to a HOOK on desktop '%ls'.\n",
item->desktop->pwszDesktopName
);
}
}
hook = &item->hook[ item->hook_count ];
hook->entry_index = i;
hook->entry = entry;
/* copy the HOOK struct from the desktop heap.
the info may change so it can't just be pointed to.
*/
hook->object =
*(HOOK *)( (uintptr_t)hook->entry.pHead - (uintptr_t)item->desktop->pvClientDelta );
/* search the gui threads to find the owner origin and target of the HOOK.
the HANDLEENTRY and HOOK must be copied before calling find_Win32ThreadInfo()
*/
hook->owner = find_Win32ThreadInfo( parent, hook->entry.pOwner );
hook->origin = find_Win32ThreadInfo( parent, hook->object.pti );
hook->target = find_Win32ThreadInfo( parent, hook->object.ptiHooked );
/* 'ignore' should be the last member of the hook to set. is_hook_wanted() relies on all
the other information in the hook, and if it is called before the other members are set
the hook may point to old (and now invalid) information and the result will be incorrect.
*/
hook->ignore = !is_hook_wanted( hook );
item->hook_count++;
if( item->hook_count >= item->hook_max )
{
MSG_ERROR( "Too many HOOK objects!" );
printf( "item->hook_count: %u\n", item->hook_count );
printf( "item->hook_max: %u\n", item->hook_max );
if( item->hook_count > item->hook_max )
{
printf( "Setting hook_count to hook_max.\n" );
item->hook_count = item->hook_max;
}
return FALSE;
}
}
/* sort the hook array for each desktop according to its position in the heap */
for( item = store->head; item; item = item->next )
{
/* sort according to HANDLEENTRY's entry.pHead */
qsort(
item->hook,
item->hook_count,
sizeof( *item->hook ),
compare_hook
);
/* search for invalid or duplicate entry.pHead */
for( i = 1; i < item->hook_count; ++i )
{
const struct hook *const a = &item->hook[ i - 1 ];
const struct hook *const b = &item->hook[ i ];
__int64 now = 0;
/* The HANDLEENTRY's pHead is the HOOK address in the kernel. Each HOOK address should be
unique. If it is not then that means either multiple handles in the kernel are pointing to the
same HOOK, or at the exact moments this program read the shared memory a HOOK was destroyed and
its address was reused in those same moments. The former is suspect but the latter is not.
Empirical testing on Vista x86 SP2 shows the latter can happen in rare cases. There's no way to
be sure which situation we're in other than to retry. Retrying just once should be enough but
here I'm making it a full second. If dupes persist then this store's initialization has failed.
*/
if( a->entry.pHead
&& b->entry.pHead
&& ( a->entry.pHead != b->entry.pHead )
)
{
// pHead is ok
continue;
}
GetSystemTimeAsFileTime( (FILETIME *)&now );
if( !first_fail_time )
first_fail_time = now;
/* retry for 1 second (10,000,000 100-nanosecond intervals),
or if ignoring failed queries retry indefinitely
*/
if( ( ( now - first_fail_time ) <= 10000000 )
|| ( G->config->flags & CFG_IGNORE_FAILED_QUERIES )
)
{
if( ( G->config->verbose >= 1 )
&& !( G->config->flags & CFG_IGNORE_FAILED_QUERIES )
&& ( first_fail_time == now )
)
MSG_WARNING( "Duplicate pHead detected. Retrying..." );
if( G->config->polling != 0 )
Sleep( 1 ); // so as not to suck up cpu
goto retry;
}
if( a->entry.pHead == b->entry.pHead )
{
MSG_ERROR( "Duplicate pHead." );
print_hook( a );
print_hook( b );
return FALSE;
}
if( !a->entry.pHead )
{
MSG_ERROR( "Invalid pHead." );
print_hook( a );
return FALSE;
}
if( !b->entry.pHead )
{
MSG_ERROR( "Invalid pHead." );
print_hook( b );
return FALSE;
}
}
}
/* the desktop hook store has been initialized */
GetSystemTimeAsFileTime( (FILETIME *)&store->init_time );
return TRUE;
}
/* print_hook_anomalies()
Print any anomalies found in a hook struct.
if 'hook' is NULL this function returns without having printed anything.
*/
void print_hook_anomalies(
const struct hook *const hook // in
)
{
if( !hook )
return;
if( hook->entry.pHead && hook->object.pSelf && ( hook->entry.pHead != hook->object.pSelf ) )
{
printf( "ERROR: The HOOK's pointer to itself is incorrect.\n" );
PRINT_HEX( hook->entry.pHead );
PRINT_HEX( hook->object.pSelf );
}
print_HOOK_anomalies( &hook->object );
if( ( hook->object.flags & HF_GLOBAL ) && hook->target )
{
printf( "ERROR: The global HOOK " );
PRINT_HEX_BARE( hook->object.head.h );
printf( " @ " );
PRINT_HEX_BARE( hook->entry.pHead );
printf( " has a target address even though global HOOKs aren't supposed to have them.\n" );
}
if( hook->entry.pHead ) // there is a HANDLEENTRY for this HOOK
{
if( ( ( (DWORD)hook->object.head.h & 0xFFFF ) != hook->entry_index )
|| ( ( (DWORD)hook->object.head.h >> 16 ) != hook->entry.wUniq )
)
{
printf( "ERROR: The handle check failed for HOOK handle " );
PRINT_HEX_BARE( hook->object.head.h );
printf( " @ " );
PRINT_HEX_BARE( hook->entry.pHead );
printf( ".\n" );
}
}
return;
}
/* print_hook()
Print a hook struct.
if 'hook' is NULL this function returns without having printed anything.
*/
void print_hook(
const struct hook *const hook // in
)
{
const char *const objname = "hook struct";
if( !hook )
return;
PRINT_SEP_BEGIN( objname );
printf( "hook->ignore: %s\n", ( hook->ignore ? "TRUE" : "FALSE" ) );
printf( "\nhook->entry_index: %u\n", hook->entry_index );
print_HANDLEENTRY( &hook->entry );
print_HOOK( &hook->object );
if( hook->owner )
{
printf( "\nhook->owner GUI info:\n" );
print_gui( hook->owner );
}
if( hook->origin )
{
printf( "\nhook->origin GUI info:\n" );
print_gui( hook->origin );
}
if( hook->target )
{
printf( "\nhook->target GUI info:\n" );
print_gui( hook->target );
}
printf( "\n" );
PRINT_SEP_END( objname );
return;
}
/* print_hook_array()
Print a desktop hook item's array of hook structs.
if 'item' is NULL this function returns without having printed anything.
*/
void print_hook_array(
const struct desktop_hook_item *const item // in
)
{
const char *const objname = "array of hook structs";
unsigned i = 0;
if( !item )
return;
PRINT_SEP_BEGIN( objname );
printf( "item->hook_max: %u\n", item->hook_max );
printf( "item->hook_count: %u\n", item->hook_count );
if( item->hook )
{
for( i = 0; ( ( i < item->hook_count ) && ( i < item->hook_max ) ); ++i )
print_hook( &item->hook[ i ] );
}
else
{
printf( "item->hook: NULL\n" );
}
PRINT_SEP_END( objname );
return;
}
/* print_desktop_hook_item()
Print an item from a desktop hook store's linked list.
if 'item' is NULL this function returns without having printed anything.
*/
void print_desktop_hook_item(
const struct desktop_hook_item *const item // in
)
{
const char *const objname = "Desktop Hook Item";
if( !item )
return;
PRINT_SEP_BEGIN( objname );
if( item->desktop )
printf( "item->desktop->pwszDesktopName: %ls\n", item->desktop->pwszDesktopName );
else
MSG_ERROR( "item->desktop == NULL" );
print_hook_array( item );
PRINT_SEP_END( objname );
return;
}
/* print_desktop_hook_store()
Print a desktop hook store and all its descendants.
if 'store' is NULL this function returns without having printed anything.
*/
void print_desktop_hook_store(
const struct desktop_hook_list *const store // in
)
{
struct desktop_hook_item *item = NULL;
const char *const objname = "Desktop Hook List Store";
if( !store )
return;
PRINT_DBLSEP_BEGIN( objname );
print_init_time( "store->init_time", store->init_time );
PRINT_HEX( store->head );
for( item = store->head; item; item = item->next )
{
PRINT_HEX( item );
print_desktop_hook_item( item );
printf( "\n" );
}
PRINT_HEX( store->tail );
PRINT_DBLSEP_END( objname );
return;
}
/* free_desktop_hook_item()
Free a desktop hook item (dirty -- linked list isn't updated).
this function then sets the desktop hook item pointer to NULL and returns.
this function has no regard for the other items in the list and should only be called by
free_desktop_hook_store().
'in' is a pointer to a pointer to the desktop hook item, which contains a desktop and its hooks.
if( !in || !*in ) then this function returns.
*/
static void free_desktop_hook_item(
struct desktop_hook_item **const in // in deref
)
{
if( !in || !*in )
return;
free( (*in)->hook );
free( (*in) );
*in = NULL;
return;
}
/* free_desktop_hook_store()
Free a desktop hook store and all its descendants.
this function then sets the desktop hook store pointer to NULL and returns
'in' is a pointer to a pointer to the desktop hook store, which contains a linked list of desktops
and their hooks.
if( !in || !*in ) then this function returns.
*/
void free_desktop_hook_store(
struct desktop_hook_list **const in // in deref
)
{
if( !in || !*in )
return;
if( (*in)->head )
{
struct desktop_hook_item *current = NULL, *p = NULL;
for( current = (*in)->head; current; current = p )
{
p = current->next;
free_desktop_hook_item( ¤t );
}
}
free( (*in) );
*in = NULL;
return;
}