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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "ctx.h"

#define TYPE_FORMAT_STRING_SIZE   23
#define PROC_FORMAT_STRING_SIZE   21
#define TRANSMIT_AS_TABLE_SIZE    0
#define WIRE_MARSHAL_TABLE_SIZE   0

typedef struct _MIDL_TYPE_FORMAT_STRING
    {
    short          Pad;
    unsigned char  Format[ TYPE_FORMAT_STRING_SIZE ];
    } MIDL_TYPE_FORMAT_STRING;

typedef struct _MIDL_PROC_FORMAT_STRING
    {
    short          Pad;
    unsigned char  Format[ PROC_FORMAT_STRING_SIZE ];
} MIDL_PROC_FORMAT_STRING;

extern const MIDL_STUB_DESC hello_StubDesc;
extern const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;
//extern const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;*/

/*****************************************************************
 * Modified from midl-generated stubs                            *
 *****************************************************************/


void m_CtxOpen(
    /* [out] */ PCTXTYPE __RPC_FAR *pphContext,
    /* [in] */ long Value)
{
    RPC_BINDING_HANDLE _Handle	=	0;

    RPC_MESSAGE _RpcMessage;

    MIDL_STUB_MESSAGE _StubMsg;

	char *ctx, *buf;
	int i;

	printf("\n*******************************************************************\n");
	printf("**** CtxOpen()                                                  ***\n");
	printf("*******************************************************************\n\n");

    if(!pphContext)
        {
        RpcRaiseException(RPC_X_NULL_REF_POINTER);
        }
    RpcTryFinally
        {
        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&hello_StubDesc,
                          0);


        _Handle = hBinding;


        _StubMsg.BufferLength = 4U;
        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );

        *(( long __RPC_FAR * )_StubMsg.Buffer)++ = Value;

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

        if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
            NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[0] );


        printf("Before NdrClientContextUnmarshall: Buflen=%d\nBuffer: ",  _StubMsg.RpcMsg->BufferLength);
        for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.RpcMsg->BufferLength; i++)
        	printf("0x%x, ", buf[i] & 0x0FF);
        printf("\n\n");

        *pphContext = (void *)0;
        NdrClientContextUnmarshall(
                              ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                              ( NDR_CCONTEXT __RPC_FAR * )pphContext,
                              _Handle);

        ctx = (char*)*pphContext;
        printf("\nNdrClientContextUnmarshall returned: handle=0x%p\n", ctx);
        printf("00: 0x%x <- obviously pointer to binding handle copyed from _Handle\n", *((int*)ctx));
        ctx+=4;
        printf("04: 0x%x <- unknown field\n", *((int*)ctx));
        printf("08: ");

        for(ctx+=4, i = 0; i < 20; i++)
        	printf("0x%x,", *(ctx+i) & 0x0FF); printf(" <- ndr 20 bytes\n\n");

        printf("Buflen=%d, Buffer: ", _StubMsg.BufferLength);
        for(buf = _StubMsg.BufferStart; buf < _StubMsg.BufferEnd; buf++)
        	printf("0x%x,", *buf & 0x0FF);
        printf("\n");


        }
    RpcFinally
        {
        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );

        }
    RpcEndFinally

}

void m_CtxOpen2(
    /* [out] */ PCTXTYPE __RPC_FAR *pphContext,
    /* [in] */ long Value)
{

    RPC_BINDING_HANDLE _Handle	=	0;

    RPC_MESSAGE _RpcMessage;

    MIDL_STUB_MESSAGE _StubMsg;

    char buf[255];

        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&hello_StubDesc,
                          0);


        _Handle = hBinding;


        _StubMsg.BufferLength = 4U;
        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );

        *(( long __RPC_FAR * )_StubMsg.Buffer)++ = Value;

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

        if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
            NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[0] );

        *pphContext = (void *)0;

        RpcTryExcept
        {
        NdrClientContextUnmarshall(
                              ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                              NULL,
                              _Handle);
        }
        RpcExcept(1)
        {
        	printf("NdrClientContextUnmarshall reported exception = %d\n", RpcExceptionCode());
        }
        RpcEndExcept



        RpcTryExcept
        {
        NdrClientContextUnmarshall(
                              ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                              (NDR_CCONTEXT __RPC_FAR * )pphContext,
                              NULL);
        }
        RpcExcept(1)
        {
        	printf("NdrClientContextUnmarshall reported exception = %d\n", RpcExceptionCode());
        }
        RpcEndExcept


        RpcTryExcept
        {
        NdrClientContextMarshall(
                            ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                            NULL,
                            1);
        }
        RpcExcept(1)
        {
        	printf("NdrClientContextMarshall reported exception = %d\n", RpcExceptionCode());
        }
        RpcEndExcept

        RpcTryExcept
        {
		NDRCContextUnmarshall( NULL, _Handle, buf, _RpcMessage.DataRepresentation  );
        }
        RpcExcept(1)
        {
        	printf("NDRCContextUnmarshall reported exception = %d\n", RpcExceptionCode());
        }
        RpcEndExcept


        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );


}

void m_CtxHello(
    /* [in] */ PCTXTYPE phContext)
{

    RPC_BINDING_HANDLE _Handle	=	0;

    RPC_MESSAGE _RpcMessage;

    MIDL_STUB_MESSAGE _StubMsg;

    char *buf;
    int i;

	printf("\n*******************************************************************\n");
	printf("**** CtxHello()                                                 ***\n");
	printf("*******************************************************************\n\n");

    RpcTryFinally
        {
        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&hello_StubDesc,
                          1);


        if(phContext != 0)
            {
            _Handle = NDRCContextBinding(( NDR_CCONTEXT  )phContext);;

            }
        else
            {
            RpcRaiseException(RPC_X_SS_IN_NULL_CONTEXT);
            }

        _StubMsg.BufferLength = 20U;
        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );

        NdrClientContextMarshall(
                            ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                            ( NDR_CCONTEXT  )phContext,
                            1);
        printf("After NdrClientContextMarshall: Buflen=%d\nBuffer: ", _StubMsg.BufferLength );
        for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.BufferLength; i++)
        	printf("0x%x, ", buf[i] & 0x0FF);
        printf("\n\n");

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

        }
    RpcFinally
        {
        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );

        }
    RpcEndFinally

}


void m_CtxClose(
    /* [out][in] */ PCTXTYPE __RPC_FAR *pphContext)
{

    RPC_BINDING_HANDLE _Handle	=	0;

    RPC_MESSAGE _RpcMessage;

    MIDL_STUB_MESSAGE _StubMsg;
    char *buf;
    int i;

	printf("\n*******************************************************************\n");
	printf("**** CtxClose()                                                 ***\n");
	printf("*******************************************************************\n\n");

    if(!pphContext)
        {
        RpcRaiseException(RPC_X_NULL_REF_POINTER);
        }
    RpcTryFinally
        {
        NdrClientInitializeNew(
                          ( PRPC_MESSAGE  )&_RpcMessage,
                          ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                          ( PMIDL_STUB_DESC  )&hello_StubDesc,
                          2);


        if(*pphContext != 0)
            {
            _Handle = NDRCContextBinding(( NDR_CCONTEXT  )*pphContext);;

            }

        _StubMsg.BufferLength = 20U;
        NdrGetBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg, _StubMsg.BufferLength, _Handle );

        NdrClientContextMarshall(
                            ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                            ( NDR_CCONTEXT  )*pphContext,
                            0);

        NdrSendReceive( (PMIDL_STUB_MESSAGE) &_StubMsg, (unsigned char __RPC_FAR *) _StubMsg.Buffer );

        if ( (_RpcMessage.DataRepresentation & 0X0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION )
            NdrConvert( (PMIDL_STUB_MESSAGE) &_StubMsg, (PFORMAT_STRING) &__MIDL_ProcFormatString.Format[14] );


      printf("Before NdrClientContextUnmarshall: Buflen=%d\nBuffer: ",  _StubMsg.BufferLength );
        for(buf = _StubMsg.Buffer, i = 0; i < _StubMsg.BufferLength; i++)
        	printf("0x%x, ", buf[i] & 0x0FF);
        printf("\n\n");

        NdrClientContextUnmarshall(
                              ( PMIDL_STUB_MESSAGE  )&_StubMsg,
                              ( NDR_CCONTEXT __RPC_FAR * )pphContext,
                              _Handle);


		printf("\nNdrClientContextUnmarshall returned: handle=0x%p\n", *pphContext);


        }
    RpcFinally
        {
        NdrFreeBuffer( (PMIDL_STUB_MESSAGE) &_StubMsg );

        }
    RpcEndFinally

}

int interactive = 0;
void interact()
{
	if(interactive)
	{
		printf("\nPress any key to continue...");
		getch();
		printf("\n\n");
	}
}

void main(int argc, char **argv)
{
	RPC_STATUS status;
	unsigned long ulCode;<--- Unused variable: ulCode
	PCTXTYPE hContext;
	char *pszStringBinding = NULL;
	RPC_BINDING_HANDLE Handle =	0;
	char buffer[255];

	int test_num = 0;
	int test_value = 31337;

	if(argc<2)
	{
		printf("USAGE: client.exe <test_number> [test_value] [interactive]\n"
			"Available tests:\n"
			"0. General test\n"
			"1. NULL pointer test\n"
			"2. Context rundown routine");
		return;
	}

	test_num = atoi(argv[1]);
	if(argc>2) test_value = atoi(argv[2]);
	if(argc>3) interactive = 1;

	status = RpcStringBindingCompose(NULL,
		"ncacn_np",
		NULL,
		"\\pipe\\hello",
		NULL,
		&pszStringBinding);

	if (status)
	{
		printf("RpcStringBindingCompose %x\n", status);
		exit(status);
	}

	status = RpcBindingFromStringBinding(pszStringBinding, &hBinding);

	if (status)
	{
		printf("RpcBindingFromStringBinding %x\n", status);
		exit(status);
	}

	RpcStringFree(&pszStringBinding);

	switch(test_num)
	{
	case 0:
		m_CtxOpen(&hContext, test_value);
		RpcBindingFree(&hBinding);
		m_CtxHello(hContext);
		interact();
		m_CtxClose(&hContext);
		break;
	case 1:
		/////////////////////////////////////////////////////////////////////////////////////////
		RpcTryExcept
		{
			Handle = NDRCContextBinding(NULL);
			printf("NDRCContextBinding(NULL) returned %p\n", Handle);
		}
		RpcExcept(1)
		{
			 printf("NDRCContextBinding(NULL) reported exception = %d\n", RpcExceptionCode());
		}
		RpcEndExcept

		m_CtxOpen2(&hContext, test_value);

		/////////////////////////////////////////////////////////////////////////////////////////
		RpcTryExcept
		{
			NDRCContextMarshall(NULL, &buffer);
			printf("NDRCContextMarshall(NULL) returned %p\n", Handle);
		}
		RpcExcept(1)
		{
			 printf("NDRCContextMarshall(NULL) reported exception = %d\n", RpcExceptionCode());
		}
		RpcEndExcept
		/////////////////////////////////////////////////////////////////////////////////////////
		break;
	case 2:
		CtxOpen(&hContext, test_value);
		interact();
		ExitProcess(0);
		break;<--- Unused variable value 'break'
	default:
		printf("Unknown test %d\n", test_num);
	}

}


void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
	return(malloc(len));
}

void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
	free(ptr);
}