forked from silverf0x/RpcView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternalRpcDecompiler.cpp
1352 lines (1065 loc) · 38.3 KB
/
internalRpcDecompiler.cpp
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
993
994
995
996
997
998
999
1000
#include "internalRpcDecompTypeDefs.h"
#include "internalRpcDecompiler.h"
#include "internalRpcUtils.h"
#include "internalComplexTypesStrings.h"
#include "internalComplexTypesArrays.h"
#include "internalComplexTypesPointers.h"
#include "InternalComplexTypesMisc.h"
#include "internalComplexTypesUnions.h"
#include "internalComplexTypesStructs.h"
#include <stdio.h>
#include <sstream>
#include <iostream>
#include "internalTypeTools.h"
///\warning Global variable is64B to set according to the real RPC binary information to decompile. The code design is to be adapted.
BOOL is64B;
//#ifdef _AMD64_
//boolean is64B = TRUE;
//#else
//boolean is64B = FALSE;
//#endif
///\warning Global variable robustFlagWasSet to set according to the real RPC binary information to decompile. The code design is to be adapted.
/// robustFlagWasSet to set according to the real environment (/robust used or not, see following line)
/// robustFlagWasSet could be set to the w2kHeaderToDecode.interpreter_opt_flag2.HasNewCorrDesc value read from a procedure descriptor.
boolean robustFlagWasSet;
// ------------------------------------------------------------------------------------------------
BOOL __fastcall getAllTypesSortedInAList(
_In_ VOID* pContext,
_In_ std::list<TypeToDefine> & listTypesToDefine,
_Inout_ std::list<TypeToDefine>& listAllTypesSorted,
_Inout_ std::ostringstream& oss)
{
BOOL bResult;
listTypesToDefine.sort();
listTypesToDefine.unique();
while(!listTypesToDefine.empty())
{
// list used to store addition type that might be embeded in current type
std::list<TypeToDefine> listAdditionalType;
TypeToDefine TypeToDefineToDefine = listTypesToDefine.front();
// remove first element from list
listTypesToDefine.pop_front();
// print it
bResult = printType(
pContext,
TypeToDefineToDefine,
listAdditionalType,
oss);
// if we're unable to Print type we abort
if(bResult == FALSE)
{
return FALSE;
}
listAllTypesSorted.push_back(TypeToDefineToDefine);
// merge listTypesToDefine and ListAdditionalTypes
if(!listAdditionalType.empty())
{
//listTypesToDefine.merge(listAdditionalType);
listTypesToDefine.splice(listTypesToDefine.end(), listAdditionalType);
}
// remove from listTypesToDefine elements that already are in
for(auto iter = listAllTypesSorted.begin(); iter != listAllTypesSorted.end(); iter++)
{
listTypesToDefine.remove(*iter);
}
listTypesToDefine.sort();
listTypesToDefine.unique();
}
listAllTypesSorted.unique();
listAllTypesSorted.sort();
return TRUE;
}
// ---------------------------------------------------------------------------------------------
BOOL __fastcall dumpTypeList(
_In_ VOID* pContext,
_In_ std::list<TypeToDefine> & listTypesToDefine,
_Inout_ std::ostringstream& oss)
{
std::list<TypeToDefine> listAdditionalType;
BOOL bResult;
listTypesToDefine.sort();
listTypesToDefine.unique();
for(auto iter=listTypesToDefine.begin(); iter!=listTypesToDefine.end(); iter++)
{
bResult = printType(
pContext,
*iter,
listAdditionalType,
oss);
if(bResult == FALSE)
{
oss << std::endl << "[ERROR] dumpTypeList : unable to print type\n" << std::endl;
return FALSE;
}
}
return TRUE;
}
// ---------------------------------------------------------------------------------------------
BOOL __fastcall RpcDecompilerPrintAllTypesInList
(
_In_ VOID* pContext,
_In_ std::list<TypeToDefine> & listProcTypes,
_Out_ std::ostringstream& oss
)
{
std::list<TypeToDefine> listAllTypesSorted;
std::ostringstream ossUseless;
/*oss<<"/*<DEBUG purpose>"<<std::endl;
for(auto iter = listProcTypes.begin(); iter != listProcTypes.end(); iter++)
{
oss<<"offset : "<<iter->uOffset<<" ";
}
oss<<"</DEBUG purpose>"<<std::endl;*/
// recursively explore all list in order to get a complete list of types
if (getAllTypesSortedInAList(pContext, listProcTypes, listAllTypesSorted, ossUseless) == FALSE)
{
oss<<"[ERROR] unable to get list of all types sorted" << std::endl;
return FALSE;
}
if( dumpTypeList(pContext, listAllTypesSorted, oss) == FALSE)
{
oss<<"[ERROR] unable to dump type list" << std::endl;
}
return TRUE;
}
//----------------------------------------------------------------------------
BOOL __fastcall RpcDecompilerDecodeAndPrintPrototypeReturnType(
_In_ VOID* pContext,
_In_ UINT formatStringOffset,
_Out_ UINT * paramOffset,
_Out_ UINT * sizeOfProcDescr,
_Inout_ IdlFunctionDesc& IdlFunctionDesc,
_Inout_ std::list<TypeToDefine>& listProcType,
_Inout_ std::ostringstream& oss)
{
BOOL bResult = FALSE;
RpcDecompilerCtxt_T * pRpcDecompilerCtxt = (RpcDecompilerCtxt_T *) pContext;
Oi_Header_1stPart_t oiHeader1stPartToDecode;
Oi_Header_RpcFlags_t * oiHeaderRpcFlagsToDecode = NULL;
Oi_Header_3rdPart_t oiHeader3rdPartToDecode;
ExplicitHandle_t * oiHeaderExplicitHandleToDecode = NULL;
Oif_Header_t oifHeaderToDecode;
Win2kExt_Header_t w2kHeaderToDecode;
UINT currentOffset = formatStringOffset;
UINT numParam = 0;
BOOL isReturnParam = FALSE;
BOOL returnTypeHasBeenPrinted = FALSE;
UINT paramSizeInBytes = 0;
memset(&oiHeader1stPartToDecode, 0, sizeof(Oi_Header_1stPart_t));
memset(&oiHeader3rdPartToDecode, 0, sizeof(Oi_Header_3rdPart_t));
memset(&oifHeaderToDecode, 0, sizeof(Oif_Header_t));
memset(&w2kHeaderToDecode, 0, sizeof(Win2kExt_Header_t));
//following checks already done by the caller function
/* if (pRpcDecompilerCtxt == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcViewHelper == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcDecompilerInfo == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString == NULL) goto End;*/
RPC_DEBUG_FN((UCHAR*)"\n\n*************** RpcDecompilerDecodeAndPrintPrototypeReturnType ******************");
/**********************************************************************
* Decode the Oi Header
***********************************************************************/
//Decode the Oi header 1st part
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&oiHeader1stPartToDecode,
sizeof(Oi_Header_1stPart_t)
);
currentOffset += sizeof(Oi_Header_1stPart_t);
RPC_DEBUG_FN((UCHAR*)"\n\n");
RPC_DEBUG_FN((UCHAR*)"\noiHeader1stPartToDecode.handle_type = 0x%x", oiHeader1stPartToDecode.handle_type);
RPC_DEBUG_FN((UCHAR*)"\noiHeader1stPartToDecode.oi_flags = 0x%x", oiHeader1stPartToDecode.oi_flags);
//Decode the Oi header Rpc Flags
if(oiHeader1stPartToDecode.oi_flags & Oi_HAS_RPCFLAGS)
{
oiHeaderRpcFlagsToDecode = (Oi_Header_RpcFlags_t *) RPC_ALLOC_FN(sizeof(Oi_Header_RpcFlags_t));
if(oiHeaderRpcFlagsToDecode == NULL)
{
RPC_DEBUG_FN((UCHAR*)"\n!!! source = %s line = %d - RpcAlloc returned NULL pointer", __FILE__, __LINE__);
goto End;
}
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
oiHeaderRpcFlagsToDecode,
sizeof(Oi_Header_RpcFlags_t)
);
currentOffset += sizeof(Oi_Header_RpcFlags_t);
//RPC_DEBUG_FN((UCHAR*)"\noiHeaderRpcFlagsToDecode->rpc_flags = 0x%x", oiHeaderRpcFlagsToDecode->rpc_flags);
}
//Decode the Oi header 3rd part
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&oiHeader3rdPartToDecode,
sizeof(Oi_Header_3rdPart_t)
);
currentOffset += sizeof(Oi_Header_3rdPart_t);
RPC_DEBUG_FN((UCHAR*)"\noiHeader3rdPartToDecode.procNum = 0x%x", oiHeader3rdPartToDecode.procNum);
RPC_DEBUG_FN((UCHAR*)"\noiHeader3rdPartToDecode.stack_size = 0x%x", oiHeader3rdPartToDecode.stack_size);
//Decode the Oi header explicit handle
//TODO : creer une fonction qui fait cela, cad GetExplicitHandleSize
if(oiHeader1stPartToDecode.handle_type == FC_EXPLICIT_HANDLE)
{
oiHeaderExplicitHandleToDecode = (ExplicitHandle_t *) RPC_ALLOC_FN(sizeof(ExplicitHandle_t));
if(oiHeaderExplicitHandleToDecode == NULL)
{
RPC_DEBUG_FN((UCHAR*)"!!! source = %s line = %d - RpcAlloc returned NULL pointer", __FILE__, __LINE__);
goto End;
}
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
oiHeaderExplicitHandleToDecode,
EXPLICIT_HANDLE_MIN_SIZE
);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->htype = 0x%x", oiHeaderExplicitHandleToDecode->htype);
switch(oiHeaderExplicitHandleToDecode->htype)
{
case FC_BIND_PRIMITIVE:
#if EXPLICIT_HANDLE_MIN_SIZE != EXPLICIT_HANDLE_PRIMITIVE_SIZE
#error check the value of EXPLICIT_HANDLE_MIN_SIZE, EXPLICIT_HANDLE_PRIMITIVE_SIZE and the code
#endif
currentOffset += EXPLICIT_HANDLE_PRIMITIVE_SIZE;
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandlePrimitive.flag = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandlePrimitive.flag);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandlePrimitive.offset = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandlePrimitive.offset);
break;
case FC_BIND_GENERIC:
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
oiHeaderExplicitHandleToDecode,
EXPLICIT_HANDLE_GENERIC_SIZE
);
currentOffset += EXPLICIT_HANDLE_GENERIC_SIZE;
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.flagAndSize = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.flagAndSize);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.offset = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.offset);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.binding_routine_pair_index = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.binding_routine_pair_index);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.pad = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleGeneric.pad);
break;
case FC_BIND_CONTEXT:
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
oiHeaderExplicitHandleToDecode,
EXPLICIT_HANDLE_CONTEXT_SIZE
);
currentOffset += EXPLICIT_HANDLE_CONTEXT_SIZE;
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.flags = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.flags);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.offset = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.offset);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.context_rundown_routine_index = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.context_rundown_routine_index);
RPC_DEBUG_FN((UCHAR*)"\noiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.param_num = 0x%x", oiHeaderExplicitHandleToDecode->hContent.explicitHandleContext.param_num);
break;
default:
RPC_DEBUG_FN((UCHAR*)"\n!!! source = %s line = %d - ERREUR : oiHeaderExplicitHandleToDecode->htype undefined value. value = 0x%x", __FILE__, __LINE__, oiHeaderExplicitHandleToDecode->htype);
bResult = FALSE;
goto End;
break;
} // switch(oiHeaderExplicitHandleToDecode->htype)
}// if(oiHeader1stPartToDecode.handle_type == FC_EXPLICIT_HANDLE)
/**********************************************************************
* Decode the Oif Header (necessary for 64bits)
***********************************************************************/
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&oifHeaderToDecode,
OIF_EXT_HEADER_SIZE
);
currentOffset += OIF_EXT_HEADER_SIZE;
RPC_DEBUG_FN((UCHAR*)"\noifHeaderToDecode.constant_client_buffer_size = 0x%x", oifHeaderToDecode.constant_client_buffer_size);
RPC_DEBUG_FN((UCHAR*)"\noifHeaderToDecode.constant_server_buffer_size = 0x%x", oifHeaderToDecode.constant_server_buffer_size);
RPC_DEBUG_FN((UCHAR*)"\noifHeaderToDecode.interpreter_opt_flag = 0x%x", oifHeaderToDecode.interpreter_opt_flag);
RPC_DEBUG_FN((UCHAR*)"\noifHeaderToDecode.number_of_param = 0x%x", oifHeaderToDecode.number_of_param);
#ifdef DBG_DECOMP
oss << "/* is Async bit set : ";
if(oifHeaderToDecode.interpreter_opt_flag.HasAsyncUuid)
{
oss << " TRUE */";
}
else
{
oss << " FALSE */";
}
#endif
/**********************************************************************
* Decode the Win2kExt Header
***********************************************************************/
// TODO : need to identify which precise condition trigger the apparition of a win2KExt header
if(pRpcDecompilerCtxt->pRpcDecompilerInfo->NDRVersion != 0x20000)
{
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&(w2kHeaderToDecode.extension_version),
sizeof(w2kHeaderToDecode.extension_version)
);
switch (w2kHeaderToDecode.extension_version)
{
case WIN2K_EXT_HEADER_32B_SIZE:
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&w2kHeaderToDecode,
WIN2K_EXT_HEADER_32B_SIZE
);
currentOffset += WIN2K_EXT_HEADER_32B_SIZE;
break;
case WIN2K_EXT_HEADER_64B_SIZE:
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + currentOffset),
&w2kHeaderToDecode,
WIN2K_EXT_HEADER_64B_SIZE
);
currentOffset += WIN2K_EXT_HEADER_64B_SIZE;
break;
default:
RPC_DEBUG_FN((UCHAR*)"\n!!! source = %s line = %d - ERREUR : w2kHeaderToDecode.extension_version undefined value. value = 0x%x", __FILE__, __LINE__, w2kHeaderToDecode.extension_version);
bResult = FALSE;
goto End;
}
// parse Win32Ext Header
IdlFunctionDesc.parseWin32ExtHeader(&w2kHeaderToDecode);
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.extension_version = 0x%x", w2kHeaderToDecode.extension_version);
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.interpreter_opt_flag2 = 0x%x", w2kHeaderToDecode.interpreter_opt_flag2);
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.clientCorrHint = 0x%x", w2kHeaderToDecode.clientCorrHint);
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.serverCorrHint = 0x%x", w2kHeaderToDecode.serverCorrHint);
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.notifyIndex = 0x%x", w2kHeaderToDecode.notifyIndex);
if(w2kHeaderToDecode.extension_version == WIN2K_EXT_HEADER_64B_SIZE)
{
RPC_DEBUG_FN((UCHAR*)"\nw2kHeaderToDecode.floatDoubleMask = 0x%x", w2kHeaderToDecode.floatDoubleMask);
}
}//endif
//TODO
// Print the handle type information to take into account
switch(oiHeader1stPartToDecode.handle_type)
{
case FC_BIND_CONTEXT:
break;
case FC_BIND_PRIMITIVE:
oss << "\n/*********************************************************";
oss << "\n * Add the following line in header of the the ACF file:";
//oss << "\n *\timplicit_handle (handle_t "<<narrow(std::wstring(DEFAULT_IF_NAME))<<"_IfHandle)";
oss << "\n *\timplicit_handle (handle_t "<< DEFAULT_IF_NAME <<"_IfHandle)";
oss << "\n *********************************************************/\n";
break;
case FC_AUTO_HANDLE:
break;
case FC_CALLBACK_HANDLE:
oss << "[callback]\t";
break;
case FC_EXPLICIT_HANDLE:
break;
default:
RPC_DEBUG_FN((UCHAR*)"\n!!! source = %s line = %d - ERREUR : oiHeader1stPartToDecode.handle_type undefined value. value = 0x%x", __FILE__, __LINE__, oiHeader1stPartToDecode.handle_type);
bResult = FALSE;
goto End;
break;
}
// Compute the nbParamToPrint and the paramOffset
*paramOffset = currentOffset;
IdlFunctionDesc.setNbParam(oifHeaderToDecode.number_of_param); //TODO : cas du oi header ?
// Find and print the procedure return type
if( oifHeaderToDecode.interpreter_opt_flag.HasReturn )
{
if (oifHeaderToDecode.number_of_param <= 0)
{
//No parameter to be printed whereas the procedure is supposed to return something...
RPC_DEBUG_FN((UCHAR*)"\n!!! RpcDecompilerDecodeAndPrintPrototypeReturnType: file = %s, line = %d, *nbParamToPrint = %d whereas oifHeaderToDecode.interpreter_opt_flag.HasReturn",
__FILE__, __LINE__, oifHeaderToDecode.number_of_param);
goto End;
}
else //(oifHeaderToDecode.number_of_param > 0)
{
//Find and print the return parameter type. Note: as soon as a return type is printed, the loop ends
numParam = 1;
while( (! returnTypeHasBeenPrinted) && (numParam <= oifHeaderToDecode.number_of_param) )
{
RPC_DEBUG_FN((UCHAR*)"\nRpcDecompilerDecodeAndPrintPrototypeReturnType: numParam = 0x%x on total to analyse = 0x%x", numParam, oifHeaderToDecode.number_of_param);
bResult = RpcDecompilerGetReturnParamInfo(/* in */ pRpcDecompilerCtxt, /* in */ currentOffset, /* in */ paramDescrOif, /* out */ &isReturnParam);
if (bResult == FALSE) goto End;
if(isReturnParam)
{
//Return param is found, to be printed
//RPC_PRINT_FN((UCHAR*)"\t");
oss << "\t";
bResult = RpcDecompilerPrintParam(
pRpcDecompilerCtxt,
/* in */ currentOffset,
/* in */ paramDescrOif,
/* out */ ¶mSizeInBytes,
/* in */ IdlFunctionDesc,
/* in/out */ listProcType,
/* in, out */oss);
if (bResult == FALSE || paramSizeInBytes == RPC_DECOMPILER_INVALID_PARAM_SIZE) goto End;
returnTypeHasBeenPrinted = TRUE;
}
currentOffset += OIF_PARAM_SIZE;
numParam++;
}
if(! returnTypeHasBeenPrinted)
{
//No parameter to be printed whereas the procedure is supposed to return something...
RPC_DEBUG_FN((UCHAR*)"\n!!! RpcDecompilerDecodeAndPrintPrototypeReturnType: file = %s, line = %d, no return param found whereas oifHeaderToDecode.interpreter_opt_flag.HasReturn",
__FILE__, __LINE__);
goto End;
}
}// (*nbParamToPrint > 0)
}//( oifHeaderToDecode.interpreter_opt_flag.HasReturn )
else
{
//RPC_PRINT_FN((UCHAR*)"\tvoid");
oss <<"\tvoid ";
RPC_DEBUG_FN((UCHAR*)"\tvoid ");
}// !( oifHeaderToDecode.interpreter_opt_flag.HasReturn )
End:
*sizeOfProcDescr = currentOffset - formatStringOffset;
if(oiHeaderRpcFlagsToDecode != NULL) RPC_FREE_FN(oiHeaderRpcFlagsToDecode);
if(oiHeaderExplicitHandleToDecode != NULL) RPC_FREE_FN(oiHeaderExplicitHandleToDecode);
return (bResult);
}
//------------------------------------------------------------------------------
BOOL __fastcall RpcDecompilerPrintPrototypeName(
_In_ VOID* pContext,
_In_ UINT ProcIndex,
_Inout_ std::ostringstream& oss)
{
RpcDecompilerCtxt_T * pRpcDecompilerCtxt = (RpcDecompilerCtxt_T *) pContext;
//following checks already done by the caller function
/* if (pRpcDecompilerCtxt == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcViewHelper == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcDecompilerInfo == NULL) goto End;*/
if(pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable == NULL)
{
return FALSE;
}
RPC_DEBUG_FN((UCHAR*)"\n\n************************** RpcDecompilerPrintPrototypeName ************************");
if(pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable[ProcIndex] == NULL)
{
//oss << "_Function_0x" << std::hex << (long)(pFunction - pModuleBase) << "(";
oss << " _Function_" << std::dec << ProcIndex << "(";
}
else
{
size_t sz = wcslen(pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable[ProcIndex]) + 1;
size_t szConverted = 0;
char* pTmp = (char*)pRpcDecompilerCtxt->pRpcViewHelper->RpcAlloc(sz);
if(pTmp != NULL)
{
ZeroMemory(pTmp, sz);
wcstombs_s(&szConverted, pTmp, sz, pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable[ProcIndex], sz);
oss << pTmp << "(";
pRpcDecompilerCtxt->pRpcViewHelper->RpcFree(pTmp);
}
//oss << narrow(std::wstring(pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable[ProcIndex])) << "(";
}// if(pRpcDecompilerCtxt->pRpcDecompilerInfo->ppProcNameTable[ProcIndex] == NULL)
return TRUE;
}
//------------------------------------------------------------------------------
BOOL __fastcall RpcDecompilerGetReturnParamInfo(
_In_ VOID* pContext,
_In_ UINT paramOffset,
_In_ ParamID_E paramDescrFormat,
_Out_ BOOL * isReturnParam)
{
BOOL bResult = FALSE;
RpcDecompilerCtxt_T * pRpcDecompilerCtxt = (RpcDecompilerCtxt_T *) pContext;
//UINT formatStringOffset = 0;
ProcFormatStringParam_U paramToAnalyze;
//following checks already done by the caller function
/* if (pRpcDecompilerCtxt == NULL) goto End;
if (pRpcDecompilerCtxt->pRpcViewHelper == NULL) goto End;
if(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString == NULL) goto End; */
RPC_DEBUG_FN((UCHAR*)"\n\n******************* RpcDecompilerGetReturnParamInfo *****************");
switch(paramDescrFormat)
{
case paramDescrOi:
//TODO
//paramSizeInBytes = OI_PARAM_BASETYPE_SIZE;
//paramSizeInBytes = OI_PARAM_OTHERTYPE_SIZE;
RPC_DEBUG_FN((UCHAR*)"\n!!! source = %s ligne = %d - RpcDecompilerGetReturnParamInfo : paramDescrOi not handled", __FILE__, __LINE__);
goto End;
break;
case paramDescrOif:
RPC_GET_PROCESS_DATA(
(pRpcDecompilerCtxt->pRpcDecompilerInfo->pProcFormatString + paramOffset),
¶mToAnalyze,
sizeof(paramToAnalyze)
);
RPC_DEBUG_FN((UCHAR*)"\nRpcDecompilerGetReturnParamInfo: paramToPrint.oif_Format.paramAttributes = 0x%x", paramToAnalyze.oif_Format.paramAttributes);
*isReturnParam = paramToAnalyze.oif_Format.paramAttributes.IsReturn;
break;
default:
RPC_DEBUG_FN((UCHAR*)"\n!!! RpcDecompilerGetReturnParamInfo : ERROR, paramDescrFormat not recognized");
bResult = FALSE;
goto End;
break;
}
End:
return bResult;
}
// -----------------------------------------------------------------------------------------------
DWORD __fastcall getSimpleTypeMemorySize(_In_ FC_TYPE fcType)
{
switch(fcType)
{
case FC_BYTE:
case FC_CHAR:
case FC_SMALL:
case FC_USMALL:
return 1;
case FC_WCHAR:
case FC_SHORT:
case FC_USHORT:
return 2;
case FC_LONG:
case FC_ULONG:
case FC_ENUM16:
case FC_ENUM32:
case FC_ERROR_STATUS_T:
return 4;
case FC_FLOAT:
case FC_HYPER:
case FC_DOUBLE:
case FC_INT3264:
case FC_UINT3264:
return 8;
case FC_ZERO:
return 0;
case FC_IGNORE:
return POINTER_SIZE;
default:
return (DWORD)-1;
}
}
// ------------------------------------------------------------------------------------------------
BOOL __fastcall processSimpleType(
_In_ VOID* pContext,
_In_ FC_TYPE fcType,
_Inout_ ParamDesc& paramDesc,
_Inout_ std::ostringstream& oss)
{
if (printSimpleType(pContext, fcType, oss) == FALSE)
{
RPC_DEBUG_FN((UCHAR*)"[ERROR] processSimpleType : unknow type\n");
return FALSE;
}
// get simple type size
//paramDesc.setMemorySize(getSimpleTypeMemorySize(fcType));
displayPtrLevel(paramDesc.getuPtrLevel(), oss);
oss<<paramDesc.getStrTypeName();
return TRUE;
}
//------------------------------------------------------------------------------
BOOL __fastcall printSimpleType(
_In_ VOID* pContext,
_In_ FC_TYPE fcType,
_Inout_ std::ostringstream& oss)
{
UNREFERENCED_PARAMETER(pContext);
switch(fcType)
{
case FC_BYTE:
oss<<"byte ";
break;
case FC_CHAR:
oss<<"char ";
break;
case FC_SMALL:
oss<<"small ";
break;
case FC_USMALL:
oss<<"unsigned small ";
break;
case FC_WCHAR:
oss <<"wchar_t ";
break;
case FC_SHORT:
oss << "short ";
break;
case FC_USHORT:
oss << "unsigned short ";
break;
case FC_LONG:
oss << "long ";
break;
case FC_ULONG:
oss << "unsigned long ";
break;
case FC_FLOAT:
oss << "float ";
break;
case FC_HYPER:
oss << "hyper ";
break;
case FC_DOUBLE:
oss << "double ";
break;
case FC_ENUM16:
oss << "/* enum_16 */ short ";
break;
case FC_ENUM32:
oss << " /* enum_32 : typedef [v1_enum] enum */ long ";
break;
case FC_ERROR_STATUS_T:
oss << "error_status_t ";
break;
case FC_IGNORE:
oss << "void* /* FC_IGNORE */ ";
break;
case FC_INT3264:
oss <<"__int3264 ";
break;
case FC_UINT3264:
oss << "unsigned __int3264 ";
break;
default :
oss << "[ERROR] parseBaseType : unknown type ("<<fcType<<")";
return FALSE; // TODO : ret FALSE
}
return TRUE;
}
//------------------------------------------------------------------------------
BOOL __fastcall rpcDumpType(
_In_ VOID* pContext,
_In_ ParamDesc& paramDesc,
_Inout_ std::list<TypeToDefine>& listProcTypes,
_Inout_ std::ostringstream& oss)
{
BOOL bResult = TRUE;
RpcDecompilerCtxt_T * pRpcDecompilerCtxt = (RpcDecompilerCtxt_T *) pContext;
BYTE bFC_TYPE;
RPC_GET_PROCESS_DATA(
paramDesc.getRva(),
&bFC_TYPE,
sizeof(bFC_TYPE));
if(bResult == FALSE)
{
RPC_ERROR_FN("RpcGetProcessData failed\n");
return FALSE;
}
paramDesc.setFcType((FC_TYPE)bFC_TYPE);
switch(paramDesc.getFcType())
{
//------------------------------------
// Simple type
//------------------------------------
case FC_BYTE:
case FC_CHAR:
case FC_SMALL:
case FC_WCHAR:
case FC_SHORT:
case FC_USHORT:
case FC_LONG:
case FC_ULONG:
case FC_FLOAT:
case FC_HYPER:
case FC_DOUBLE:
case FC_ENUM16:
case FC_ENUM32:
case FC_ERROR_STATUS_T:
case FC_IGNORE:
case FC_INT3264:
case FC_UINT3264:
//processSimpleType(pContext,
bResult = processSimpleType(pContext, (FC_TYPE)bFC_TYPE, paramDesc, oss);
if (bResult==FALSE) RPC_ERROR_FN("processSimpleType failed\n");
break;
//------------------------------------
// Arrays
//------------------------------------
case FC_CARRAY:
bResult = processConformantArray(pContext,paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processConformantArray failed\n");
break;
case FC_CVARRAY:
bResult = processConformantVaryingArray(pContext,paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processConformantVaryingArray failed\n");
break;
case FC_SMFARRAY:
case FC_LGFARRAY:
bResult = processFixedSizeArray(pContext,paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processFixedSizeArray failed\n");
break;
case FC_SMVARRAY:
case FC_LGVARRAY:
bResult = processVaryingArray(pContext,paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processVaryingArray failed\n");
break;
case FC_BOGUS_ARRAY:
bResult = processComplexArray(pContext,paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processComplexArray failed\n");
break;
//------------------------------------
// Structures
//------------------------------------
case FC_STRUCT:
case FC_PSTRUCT:
case FC_CSTRUCT:
case FC_CPSTRUCT:
case FC_CVSTRUCT:
case FC_BOGUS_STRUCT:
case FC_HARD_STRUCT:
bResult = processStructure(pContext, paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processStructure failed\n");
break;
//------------------------------------
// Unions
//------------------------------------
case FC_ENCAPSULATED_UNION:
bResult = processEncapsulatedUnion(
pContext,
paramDesc.getRva(),
(FC_TYPE)bFC_TYPE,
paramDesc,
listProcTypes,
oss);
if (bResult == FALSE) RPC_ERROR_FN("processEncapsulatedUnion failed\n");
break;
case FC_NON_ENCAPSULATED_UNION:
bResult = processNonEncapsulatedUnion(
pContext,
paramDesc.getRva(),
(FC_TYPE)bFC_TYPE,
paramDesc,
listProcTypes,
oss);
if (bResult == FALSE) RPC_ERROR_FN("processNonEncapsulatedUnion failed\n");
break;
//------------------------------------
// Pointers
//------------------------------------
case FC_RP:
case FC_UP:
case FC_FP:
case FC_OP:
bResult = processStandardPointer(
pContext,
paramDesc.getRva(),
(FC_TYPE)bFC_TYPE,
paramDesc,
listProcTypes,
oss);
if (bResult == FALSE) RPC_ERROR_FN("processStandardPointer failed\n");
break;
case FC_IP:
bResult = processInterfacePointer(
pContext,
paramDesc.getRva(),
paramDesc,
oss);
if (bResult == FALSE) RPC_ERROR_FN("processInterfacePointer failed\n");
break;
case FC_BYTE_COUNT_POINTER:
bResult = processByteCountPointer(
pContext,
paramDesc.getRva(),
paramDesc,
listProcTypes,
oss
);
if (bResult == FALSE) RPC_ERROR_FN("processByteCountPointer failed\n");
break;
//------------------------------------
// Strings
//------------------------------------
case FC_C_CSTRING:
case FC_C_WSTRING:
bResult = processConformantString(pContext, paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, oss);
if (bResult == FALSE) RPC_ERROR_FN("processConformantString failed\n");
break;
case FC_WSTRING:
case FC_CSTRING:
bResult = processNonConformantString(pContext, paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, oss);
if (bResult == FALSE) RPC_ERROR_FN("processNonConformantString failed\n");
break;
case FC_SSTRING:
case FC_C_SSTRING:
bResult = processStructureString(pContext, paramDesc.getRva(), (FC_TYPE)bFC_TYPE, paramDesc, oss);
if (bResult == FALSE) RPC_ERROR_FN("processStructureString failed\n");
break;
//------------------------------------
// Misc ...
//------------------------------------
case FC_BIND_CONTEXT:
bResult = processBindContext(pContext,paramDesc.getRva(), paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processBindContext failed\n");
break;
case FC_BLKHOLE:
bResult = process_FC_BLKHOLE(pContext, paramDesc.getRva(), paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("process_FC_BLKHOLE failed\n");
break;
case FC_RANGE:
bResult = processRange(pContext, paramDesc.getRva(), paramDesc, listProcTypes, oss);
if (bResult == FALSE) RPC_ERROR_FN("processRange failed\n");
break;
case FC_USER_MARSHAL:
bResult = processUserMarshal(pContext, paramDesc.getRva(), paramDesc, listProcTypes, oss);