hlink: Added HlinkGetSpecialReference implementation.
[wine] / dlls / rpcrt4 / ndr_clientserver.c
1 /*
2  * MIDL proxy/stub stuff
3  *
4  * Copyright 2002 Ove Kåven, TransGaming Technologies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * TODO:
21  *  - figure out whether we *really* got this right
22  *  - check for errors and throw exceptions
23  */
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #define COBJMACROS
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winerror.h"
35
36 #include "objbase.h"
37
38 #include "rpcproxy.h"
39
40 #include "wine/debug.h"
41
42 #include "ndr_misc.h"
43 #include "rpcndr.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
46
47 /************************************************************************
48  *             NdrClientInitializeNew [RPCRT4.@]
49  */
50 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
51                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
52 {
53   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
54     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
55
56   assert( pRpcMessage && pStubMsg && pStubDesc );
57
58   pRpcMessage->Handle = NULL;
59   pRpcMessage->ProcNum = ProcNum;
60   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
61   pRpcMessage->RpcFlags = 0;
62   pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
63
64   pStubMsg->RpcMsg = pRpcMessage;
65   pStubMsg->BufferStart = NULL;
66   pStubMsg->BufferEnd = NULL;
67   pStubMsg->BufferLength = 0;
68   pStubMsg->IsClient = TRUE;
69   pStubMsg->ReuseBuffer = FALSE;
70   pStubMsg->pAllocAllNodesContext = NULL;
71   pStubMsg->pPointerQueueState = NULL;
72   pStubMsg->IgnoreEmbeddedPointers = 0;
73   pStubMsg->PointerBufferMark = NULL;
74   pStubMsg->fBufferValid = 0;
75   pStubMsg->uFlags = 0;
76   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
77   pStubMsg->pfnFree = pStubDesc->pfnFree;
78   pStubMsg->StackTop = NULL;
79   pStubMsg->StubDesc = pStubDesc;
80   pStubMsg->FullPtrRefId = 0;
81   pStubMsg->PointerLength = 0;
82   pStubMsg->fInDontFree = 0;
83   pStubMsg->fDontCallFreeInst = 0;
84   pStubMsg->fInOnlyParam = 0;
85   pStubMsg->fHasReturn = 0;
86   pStubMsg->fHasExtensions = 0;
87   pStubMsg->fHasNewCorrDesc = 0;
88   pStubMsg->fUnused = 0;
89   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
90   pStubMsg->pvDestContext = NULL;
91   pStubMsg->pRpcChannelBuffer = NULL;
92   pStubMsg->pArrayInfo = NULL;
93   pStubMsg->dwStubPhase = 0;
94   /* FIXME: LowStackMark */
95   pStubMsg->pAsyncMsg = NULL;
96   pStubMsg->pCorrInfo = NULL;
97   pStubMsg->pCorrMemory = NULL;
98   pStubMsg->pMemoryList = NULL;
99 }
100
101 /***********************************************************************
102  *             NdrServerInitializeNew [RPCRT4.@]
103  */
104 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
105                                               PMIDL_STUB_DESC pStubDesc )
106 {
107   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
108
109   assert( pRpcMsg && pStubMsg && pStubDesc );
110
111   /* not everyone allocates stack space for w2kReserved */
112   memset(pStubMsg, 0, FIELD_OFFSET(MIDL_STUB_MESSAGE,pCSInfo));
113
114   pStubMsg->ReuseBuffer = TRUE;
115   pStubMsg->IsClient = FALSE;
116   pStubMsg->StubDesc = pStubDesc;
117   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
118   pStubMsg->pfnFree = pStubDesc->pfnFree;
119   pStubMsg->RpcMsg = pRpcMsg;
120   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
121   pStubMsg->BufferLength = pRpcMsg->BufferLength;
122   pStubMsg->BufferEnd = pStubMsg->Buffer + pStubMsg->BufferLength;
123
124   /* FIXME: determine the proper return value */
125   return NULL;
126 }
127
128 /***********************************************************************
129  *           NdrGetBuffer [RPCRT4.@]
130  */
131 unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
132 {
133   TRACE("(stubmsg == ^%p, buflen == %u, handle == %p): wild guess.\n", stubmsg, buflen, handle);
134   
135   assert( stubmsg && stubmsg->RpcMsg );
136
137   /* I guess this is our chance to put the binding handle into the RPC_MESSAGE */
138   stubmsg->RpcMsg->Handle = handle;
139   
140   stubmsg->RpcMsg->BufferLength = buflen;
141   if (I_RpcGetBuffer(stubmsg->RpcMsg) != S_OK)
142     return NULL;
143
144   stubmsg->Buffer = stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
145   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
146   stubmsg->BufferEnd = stubmsg->Buffer + stubmsg->BufferLength;
147   return (stubmsg->Buffer = (unsigned char *)stubmsg->RpcMsg->Buffer);
148 }
149 /***********************************************************************
150  *           NdrFreeBuffer [RPCRT4.@]
151  */
152 void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
153 {
154   TRACE("(pStubMsg == ^%p): wild guess.\n", pStubMsg);
155   I_RpcFreeBuffer(pStubMsg->RpcMsg);
156   pStubMsg->BufferLength = 0;
157   pStubMsg->Buffer = pStubMsg->BufferEnd = (unsigned char *)(pStubMsg->RpcMsg->Buffer = NULL);
158 }
159
160 /************************************************************************
161  *           NdrSendReceive [RPCRT4.@]
162  */
163 unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
164 {
165   RPC_STATUS status;
166
167   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
168
169   /* FIXME: how to handle errors? (raise exception?) */
170   if (!stubmsg) {
171     ERR("NULL stub message.  No action taken.\n");
172     return NULL;
173   }
174   if (!stubmsg->RpcMsg) {
175     ERR("RPC Message not present in stub message.  No action taken.\n");
176     return NULL;
177   }
178
179   stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
180   status = I_RpcSendReceive(stubmsg->RpcMsg);
181   if (status != RPC_S_OK)
182     RpcRaiseException(status);
183
184   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
185   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
186   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
187   stubmsg->Buffer = stubmsg->BufferStart;
188
189   /* FIXME: is this the right return value? */
190   return NULL;
191 }
192
193 /************************************************************************
194  *           NdrMapCommAndFaultStatus [RPCRT4.@]
195  */
196 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
197                                                ULONG *pCommStatus,
198                                                ULONG *pFaultStatus,
199                                                RPC_STATUS Status )
200 {
201     FIXME("(%p, %p, %p, %ld): stub\n", pStubMsg, pCommStatus, pFaultStatus, Status);
202
203     *pCommStatus = 0;
204     *pFaultStatus = 0;
205
206     return RPC_S_OK;
207 }