2012-04-29 02:28:10 +04:00
|
|
|
/**
|
2012-05-05 02:32:34 +04:00
|
|
|
* WinPR: Windows Portable Runtime
|
2012-04-29 02:28:10 +04:00
|
|
|
* Network Data Representation (NDR)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-05-05 03:36:35 +04:00
|
|
|
#include <stdio.h>
|
2012-05-06 02:42:35 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2012-05-05 03:36:35 +04:00
|
|
|
|
2012-05-05 02:32:34 +04:00
|
|
|
#include <winpr/ndr.h>
|
2012-04-29 02:28:10 +04:00
|
|
|
|
2012-05-05 05:50:32 +04:00
|
|
|
#include "ndr_array.h"
|
|
|
|
#include "ndr_context.h"
|
|
|
|
#include "ndr_pointer.h"
|
|
|
|
#include "ndr_simple.h"
|
|
|
|
#include "ndr_string.h"
|
|
|
|
#include "ndr_structure.h"
|
|
|
|
#include "ndr_union.h"
|
2012-05-04 00:38:48 +04:00
|
|
|
|
2012-05-05 05:50:32 +04:00
|
|
|
#include "ndr_private.h"
|
2012-05-02 08:17:44 +04:00
|
|
|
|
2012-05-04 00:38:48 +04:00
|
|
|
/**
|
2012-05-05 05:50:32 +04:00
|
|
|
* MSRPC NDR Types Technical Overview:
|
|
|
|
* http://dvlabs.tippingpoint.com/blog/2007/11/24/msrpc-ndr-types/
|
2012-05-04 00:38:48 +04:00
|
|
|
*/
|
2012-05-02 08:17:44 +04:00
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
void NdrPrintParamAttributes(PARAM_ATTRIBUTES attributes)
|
2012-05-01 05:12:34 +04:00
|
|
|
{
|
|
|
|
if (attributes.ServerAllocSize)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("ServerAllocSize, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.SaveForAsyncFinish)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("SaveForAsyncFinish, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsDontCallFreeInst)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsDontCallFreeInst, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsSimpleRef)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsSimpleRef, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsByValue)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsByValue, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsBasetype)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsBaseType, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsReturn)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsReturn, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsOut)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsOut, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsIn)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsIn, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.IsPipe)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("IsPipe, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.MustFree)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("MustFree, ");
|
2012-05-01 05:12:34 +04:00
|
|
|
if (attributes.MustSize)
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("MustSize, ");
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
void NdrProcessParam(PMIDL_STUB_MESSAGE pStubMsg, NDR_PHASE phase, unsigned char* pMemory, NDR_PARAM* param)
|
2012-05-01 05:12:34 +04:00
|
|
|
{
|
2012-05-02 07:09:18 +04:00
|
|
|
unsigned char type;
|
2012-05-01 05:12:34 +04:00
|
|
|
PFORMAT_STRING pFormat;
|
|
|
|
|
|
|
|
/* Parameter Descriptors: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374362/ */
|
|
|
|
|
|
|
|
if (param->Attributes.IsBasetype)
|
|
|
|
{
|
|
|
|
pFormat = ¶m->Type.FormatChar;
|
|
|
|
|
|
|
|
if (param->Attributes.IsSimpleRef)
|
|
|
|
pMemory = *(unsigned char**) pMemory;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pFormat = &pStubMsg->StubDesc->pFormatTypes[param->Type.Offset];
|
|
|
|
|
|
|
|
if (!(param->Attributes.IsByValue))
|
|
|
|
pMemory = *(unsigned char**) pMemory;
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
type = (pFormat[0] & 0x7F);
|
|
|
|
|
|
|
|
if (type > FC_PAD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (phase == NDR_PHASE_SIZE)
|
|
|
|
{
|
|
|
|
NDR_TYPE_SIZE_ROUTINE pfnSizeRoutine = pfnSizeRoutines[type];
|
|
|
|
|
|
|
|
if (pfnSizeRoutine)
|
|
|
|
pfnSizeRoutine(pStubMsg, pMemory, pFormat);
|
|
|
|
}
|
|
|
|
else if (phase == NDR_PHASE_MARSHALL)
|
|
|
|
{
|
|
|
|
NDR_TYPE_MARSHALL_ROUTINE pfnMarshallRoutine = pfnMarshallRoutines[type];
|
|
|
|
|
|
|
|
if (pfnMarshallRoutine)
|
|
|
|
pfnMarshallRoutine(pStubMsg, pMemory, *pFormat);
|
|
|
|
}
|
|
|
|
else if (phase == NDR_PHASE_UNMARSHALL)
|
|
|
|
{
|
|
|
|
NDR_TYPE_UNMARSHALL_ROUTINE pfnUnmarshallRoutine = pfnUnmarshallRoutines[type];
|
|
|
|
|
|
|
|
if (pfnUnmarshallRoutine)
|
|
|
|
pfnUnmarshallRoutine(pStubMsg, pMemory, *pFormat);
|
|
|
|
}
|
|
|
|
else if (phase == NDR_PHASE_FREE)
|
|
|
|
{
|
|
|
|
NDR_TYPE_FREE_ROUTINE pfnFreeRoutine = pfnFreeRoutines[type];
|
|
|
|
|
|
|
|
if (pfnFreeRoutine)
|
|
|
|
pfnFreeRoutine(pStubMsg, pMemory, pFormat);
|
|
|
|
}
|
2012-05-01 05:12:34 +04:00
|
|
|
}
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
void NdrProcessParams(PMIDL_STUB_MESSAGE pStubMsg, PFORMAT_STRING pFormat, NDR_PHASE phase, void** fpuArgs, unsigned short numberParams)
|
2012-04-29 07:37:07 +04:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
NDR_PARAM* params;
|
|
|
|
PFORMAT_STRING fmt;
|
|
|
|
unsigned char* arg;
|
2012-05-01 05:12:34 +04:00
|
|
|
unsigned char type;
|
2012-04-29 07:37:07 +04:00
|
|
|
|
|
|
|
params = (NDR_PARAM*) pFormat;
|
|
|
|
|
|
|
|
printf("Params = \n{\n");
|
|
|
|
|
|
|
|
for (i = 0; i < numberParams; i++)
|
|
|
|
{
|
|
|
|
arg = pStubMsg->StackTop + params[i].StackOffset;
|
|
|
|
fmt = (PFORMAT_STRING) &pStubMsg->StubDesc->pFormatTypes[params[i].Type.Offset];
|
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
#ifdef __x86_64__
|
|
|
|
if ((params[i].Attributes.IsBasetype) &&
|
|
|
|
!(params[i].Attributes.IsSimpleRef) &&
|
|
|
|
((params[i].Type.FormatChar) == FC_FLOAT) && !fpuArgs)
|
|
|
|
{
|
2012-05-05 01:23:26 +04:00
|
|
|
float tmp = *(double*) arg;
|
2012-05-01 05:12:34 +04:00
|
|
|
arg = (unsigned char*) &tmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-29 07:37:07 +04:00
|
|
|
printf("\t#%d\t", i);
|
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
type = (params[i].Attributes.IsBasetype) ? params[i].Type.FormatChar : *fmt;
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
printf(" type %s (0x%02X) ", FC_TYPE_STRINGS[type], type);
|
2012-05-01 05:12:34 +04:00
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
NdrPrintParamAttributes(params[i].Attributes);
|
2012-04-29 07:37:07 +04:00
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
if (params[i].Attributes.IsIn)
|
2012-04-29 07:37:07 +04:00
|
|
|
{
|
2012-05-02 07:09:18 +04:00
|
|
|
NdrProcessParam(pStubMsg, phase, arg, ¶ms[i]);
|
2012-04-29 07:37:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void NdrClientInitializeNew(PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg,
|
|
|
|
PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum)
|
2012-04-29 02:28:10 +04:00
|
|
|
{
|
2012-04-29 07:37:07 +04:00
|
|
|
pRpcMessage->Handle = NULL;
|
|
|
|
pRpcMessage->RpcFlags = 0;
|
|
|
|
pRpcMessage->ProcNum = ProcNum;
|
|
|
|
pRpcMessage->DataRepresentation = 0;
|
|
|
|
pRpcMessage->ReservedForRuntime = NULL;
|
|
|
|
pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
|
|
|
|
|
|
|
|
pStubMsg->RpcMsg = pRpcMessage;
|
|
|
|
pStubMsg->BufferStart = NULL;
|
|
|
|
pStubMsg->BufferEnd = NULL;
|
|
|
|
pStubMsg->BufferLength = 0;
|
|
|
|
pStubMsg->StackTop = NULL;
|
|
|
|
pStubMsg->StubDesc = pStubDesc;
|
2012-05-02 08:17:44 +04:00
|
|
|
pStubMsg->IgnoreEmbeddedPointers = 0;
|
|
|
|
pStubMsg->PointerLength = 0;
|
2012-04-29 07:37:07 +04:00
|
|
|
}
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
void NdrPrintOptFlags(INTERPRETER_OPT_FLAGS optFlags)
|
|
|
|
{
|
|
|
|
if (optFlags.ClientMustSize)
|
|
|
|
printf("ClientMustSize, ");
|
|
|
|
if (optFlags.ServerMustSize)
|
|
|
|
printf("ServerMustSize, ");
|
|
|
|
if (optFlags.HasAsyncUuid)
|
|
|
|
printf("HasAsyncUiid, ");
|
|
|
|
if (optFlags.HasAsyncHandle)
|
|
|
|
printf("HasAsyncHandle, ");
|
|
|
|
if (optFlags.HasReturn)
|
|
|
|
printf("HasReturn, ");
|
|
|
|
if (optFlags.HasPipes)
|
|
|
|
printf("HasPipes, ");
|
|
|
|
if (optFlags.HasExtensions)
|
|
|
|
printf("HasExtensions, ");
|
|
|
|
}
|
|
|
|
|
|
|
|
void NdrPrintExtFlags(INTERPRETER_OPT_FLAGS2 extFlags)
|
|
|
|
{
|
|
|
|
if (extFlags.HasNewCorrDesc)
|
|
|
|
printf("HasNewCorrDesc, ");
|
|
|
|
if (extFlags.ClientCorrCheck)
|
|
|
|
printf("ClientCorrCheck, ");
|
|
|
|
if (extFlags.ServerCorrCheck)
|
|
|
|
printf("ServerCorrCheck, ");
|
|
|
|
if (extFlags.HasNotify)
|
|
|
|
printf("HasNotify, ");
|
|
|
|
if (extFlags.HasNotify2)
|
|
|
|
printf("HasNotify2, ");
|
|
|
|
}
|
|
|
|
|
|
|
|
CLIENT_CALL_RETURN NdrClientCall(PMIDL_STUB_DESC pStubDescriptor, PFORMAT_STRING pFormat, void** stackTop, void** fpuStack)
|
2012-04-29 07:37:07 +04:00
|
|
|
{
|
|
|
|
RPC_MESSAGE rpcMsg;
|
2012-04-29 06:27:04 +04:00
|
|
|
unsigned short procNum;
|
|
|
|
unsigned short stackSize;
|
|
|
|
unsigned char numberParams;
|
2012-05-01 05:12:34 +04:00
|
|
|
unsigned char handleType;
|
2012-04-29 07:37:07 +04:00
|
|
|
MIDL_STUB_MESSAGE stubMsg;
|
2012-05-01 05:12:34 +04:00
|
|
|
INTERPRETER_FLAGS flags;
|
|
|
|
INTERPRETER_OPT_FLAGS optFlags;
|
|
|
|
INTERPRETER_OPT_FLAGS2 extFlags;
|
|
|
|
NDR_PROC_HEADER* procHeader;
|
|
|
|
NDR_OI2_PROC_HEADER* oi2ProcHeader;
|
2012-04-29 04:48:26 +04:00
|
|
|
CLIENT_CALL_RETURN client_call_return;
|
2012-04-29 02:28:10 +04:00
|
|
|
|
2012-04-29 06:27:04 +04:00
|
|
|
procNum = stackSize = numberParams = 0;
|
|
|
|
procHeader = (NDR_PROC_HEADER*) &pFormat[0];
|
|
|
|
|
2012-04-29 04:48:26 +04:00
|
|
|
client_call_return.Pointer = NULL;
|
2012-04-29 02:28:10 +04:00
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
handleType = procHeader->HandleType;
|
|
|
|
flags = procHeader->OldOiFlags;
|
2012-04-29 06:27:04 +04:00
|
|
|
procNum = procHeader->ProcNum;
|
|
|
|
stackSize = procHeader->StackSize;
|
|
|
|
pFormat += sizeof(NDR_PROC_HEADER);
|
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
/* The Header: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378707/ */
|
|
|
|
/* Procedure Header Descriptor: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374387/ */
|
|
|
|
/* Handles: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373932/ */
|
|
|
|
|
|
|
|
printf("Oi Header: HandleType: 0x%02X OiFlags: 0x%02X ProcNum: %d StackSize: 0x%04X\n",
|
|
|
|
handleType, *((unsigned char*) &flags),
|
|
|
|
(unsigned short) procNum, (unsigned short) stackSize);
|
|
|
|
|
|
|
|
if (handleType > 0)
|
2012-04-29 06:27:04 +04:00
|
|
|
{
|
2012-05-01 05:12:34 +04:00
|
|
|
/* implicit handle */
|
|
|
|
printf("Implicit Handle\n");
|
|
|
|
oi2ProcHeader = (NDR_OI2_PROC_HEADER*) &pFormat[0];
|
|
|
|
pFormat += sizeof(NDR_OI2_PROC_HEADER);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* explicit handle */
|
|
|
|
printf("Explicit Handle\n");
|
|
|
|
oi2ProcHeader = (NDR_OI2_PROC_HEADER*) &pFormat[6];
|
|
|
|
pFormat += sizeof(NDR_OI2_PROC_HEADER) + 6;
|
|
|
|
}
|
2012-04-29 06:27:04 +04:00
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
optFlags = oi2ProcHeader->Oi2Flags;
|
|
|
|
numberParams = oi2ProcHeader->NumberParams;
|
2012-04-29 06:27:04 +04:00
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
printf("Oi2 Header: Oi2Flags: 0x%02X, NumberParams: %d ClientBufferSize: %d ServerBufferSize: %d\n",
|
2012-05-01 05:12:34 +04:00
|
|
|
*((unsigned char*) &optFlags),
|
2012-05-02 07:09:18 +04:00
|
|
|
(unsigned char) numberParams,
|
|
|
|
oi2ProcHeader->ClientBufferSize,
|
|
|
|
oi2ProcHeader->ServerBufferSize);
|
|
|
|
|
|
|
|
printf("Oi2Flags: ");
|
|
|
|
NdrPrintOptFlags(optFlags);
|
|
|
|
printf("\n");
|
2012-05-01 05:12:34 +04:00
|
|
|
|
|
|
|
NdrClientInitializeNew(&rpcMsg, &stubMsg, pStubDescriptor, procNum);
|
|
|
|
|
|
|
|
if (optFlags.HasExtensions)
|
|
|
|
{
|
|
|
|
NDR_PROC_HEADER_EXTS* extensions = (NDR_PROC_HEADER_EXTS*) pFormat;
|
|
|
|
|
|
|
|
pFormat += extensions->Size;
|
|
|
|
extFlags = extensions->Flags2;
|
|
|
|
|
|
|
|
printf("Extensions: Size: %d, flags2: 0x%02X\n",
|
|
|
|
extensions->Size, *((unsigned char*) &extensions->Flags2));
|
2012-04-29 06:27:04 +04:00
|
|
|
|
2012-05-01 05:12:34 +04:00
|
|
|
#ifdef __x86_64__
|
|
|
|
if (extensions->Size > sizeof(*extensions) && fpuStack)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned short fpuMask = *(unsigned short*) (extensions + 1);
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++, fpuMask >>= 2)
|
|
|
|
{
|
|
|
|
switch (fpuMask & 3)
|
|
|
|
{
|
|
|
|
case 1: *(float*) &stackTop[i] = *(float*) &fpuStack[i];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: *(double*) &stackTop[i] = *(double*) &fpuStack[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 06:27:04 +04:00
|
|
|
}
|
2012-05-01 05:12:34 +04:00
|
|
|
#endif
|
2012-04-29 06:27:04 +04:00
|
|
|
}
|
|
|
|
|
2012-04-29 07:37:07 +04:00
|
|
|
stubMsg.StackTop = (unsigned char*) stackTop;
|
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
printf("ExtFlags: ");
|
|
|
|
NdrPrintExtFlags(extFlags);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
NdrProcessParams(&stubMsg, pFormat, NDR_PHASE_SIZE, fpuStack, numberParams);
|
2012-04-29 06:27:04 +04:00
|
|
|
|
2012-05-02 07:09:18 +04:00
|
|
|
printf("stubMsg BufferLength: %d\n", (int) stubMsg.BufferLength);
|
2012-04-29 07:37:07 +04:00
|
|
|
|
|
|
|
return client_call_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CLIENT_CALL_RETURN NdrClientCall2(PMIDL_STUB_DESC pStubDescriptor, PFORMAT_STRING pFormat, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
CLIENT_CALL_RETURN client_call_return;
|
|
|
|
|
|
|
|
va_start(args, pFormat);
|
2012-05-02 07:09:18 +04:00
|
|
|
client_call_return = NdrClientCall(pStubDescriptor, pFormat, va_arg(args, void**), NULL);
|
2012-04-29 07:37:07 +04:00
|
|
|
va_end(args);
|
|
|
|
|
2012-04-29 04:48:26 +04:00
|
|
|
return client_call_return;
|
2012-04-29 02:28:10 +04:00
|
|
|
}
|