oleaut32: Use wire sizes for marshaled safe arrays of bstrs and variants; fixes a...
[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
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32
33 #include "objbase.h"
34
35 #include "rpcproxy.h"
36
37 #include "wine/debug.h"
38
39 #include "ndr_misc.h"
40 #include "rpcndr.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
43
44 /************************************************************************
45  *             NdrClientInitializeNew [RPCRT4.@]
46  */
47 void WINAPI NdrClientInitializeNew( PRPC_MESSAGE pRpcMessage, PMIDL_STUB_MESSAGE pStubMsg, 
48                                     PMIDL_STUB_DESC pStubDesc, unsigned int ProcNum )
49 {
50   TRACE("(pRpcMessage == ^%p, pStubMsg == ^%p, pStubDesc == ^%p, ProcNum == %d)\n",
51     pRpcMessage, pStubMsg, pStubDesc, ProcNum);
52
53   pRpcMessage->Handle = NULL;
54   pRpcMessage->ProcNum = ProcNum | RPC_FLAGS_VALID_BIT;
55   pRpcMessage->RpcInterfaceInformation = pStubDesc->RpcInterfaceInformation;
56   pRpcMessage->RpcFlags = 0;
57   pRpcMessage->ReservedForRuntime = NULL;
58   pRpcMessage->DataRepresentation = NDR_LOCAL_DATA_REPRESENTATION;
59
60   pStubMsg->RpcMsg = pRpcMessage;
61   pStubMsg->BufferStart = NULL;
62   pStubMsg->BufferEnd = NULL;
63   pStubMsg->BufferLength = 0;
64   pStubMsg->IsClient = TRUE;
65   pStubMsg->ReuseBuffer = FALSE;
66   pStubMsg->pAllocAllNodesContext = NULL;
67   pStubMsg->pPointerQueueState = NULL;
68   pStubMsg->IgnoreEmbeddedPointers = 0;
69   pStubMsg->PointerBufferMark = NULL;
70   pStubMsg->CorrDespIncrement = 0;
71   pStubMsg->uFlags = 0;
72   pStubMsg->UniquePtrCount = 0;
73   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
74   pStubMsg->pfnFree = pStubDesc->pfnFree;
75   pStubMsg->StackTop = NULL;
76   pStubMsg->StubDesc = pStubDesc;
77   pStubMsg->FullPtrRefId = 0;
78   pStubMsg->PointerLength = 0;
79   pStubMsg->fInDontFree = 0;
80   pStubMsg->fDontCallFreeInst = 0;
81   pStubMsg->fInOnlyParam = 0;
82   pStubMsg->fHasReturn = 0;
83   pStubMsg->fHasExtensions = 0;
84   pStubMsg->fHasNewCorrDesc = 0;
85   pStubMsg->fIsIn = 0;
86   pStubMsg->fIsOut = 0;
87   pStubMsg->fIsOicf = 0;
88   pStubMsg->fBufferValid = 0;
89   pStubMsg->fHasMemoryValidateCallback = 0;
90   pStubMsg->fInFree = 0;
91   pStubMsg->fNeedMCCP = 0;
92   pStubMsg->fUnused = 0;
93   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
94   pStubMsg->pvDestContext = NULL;
95   pStubMsg->pRpcChannelBuffer = NULL;
96   pStubMsg->pArrayInfo = NULL;
97   pStubMsg->dwStubPhase = 0;
98   /* FIXME: LowStackMark */
99   pStubMsg->pAsyncMsg = NULL;
100   pStubMsg->pCorrInfo = NULL;
101   pStubMsg->pCorrMemory = NULL;
102   pStubMsg->pMemoryList = NULL;
103 }
104
105 /***********************************************************************
106  *             NdrServerInitializeNew [RPCRT4.@]
107  */
108 unsigned char* WINAPI NdrServerInitializeNew( PRPC_MESSAGE pRpcMsg, PMIDL_STUB_MESSAGE pStubMsg,
109                                               PMIDL_STUB_DESC pStubDesc )
110 {
111   TRACE("(pRpcMsg == ^%p, pStubMsg == ^%p, pStubDesc == ^%p)\n", pRpcMsg, pStubMsg, pStubDesc);
112
113   pStubMsg->RpcMsg = pRpcMsg;
114   pStubMsg->Buffer = pStubMsg->BufferStart = pRpcMsg->Buffer;
115   pStubMsg->BufferEnd = pStubMsg->Buffer + pRpcMsg->BufferLength;
116   pStubMsg->BufferLength = pRpcMsg->BufferLength;
117   pStubMsg->IsClient = FALSE;
118   pStubMsg->ReuseBuffer = FALSE;
119   pStubMsg->pAllocAllNodesContext = NULL;
120   pStubMsg->pPointerQueueState = NULL;
121   pStubMsg->IgnoreEmbeddedPointers = 0;
122   pStubMsg->PointerBufferMark = NULL;
123   pStubMsg->uFlags = 0;
124   pStubMsg->UniquePtrCount = 0;
125   pStubMsg->pfnAllocate = pStubDesc->pfnAllocate;
126   pStubMsg->pfnFree = pStubDesc->pfnFree;
127   pStubMsg->StackTop = NULL;
128   pStubMsg->StubDesc = pStubDesc;
129   pStubMsg->FullPtrXlatTables = NULL;
130   pStubMsg->FullPtrRefId = 0;
131   pStubMsg->PointerLength = 0;
132   pStubMsg->fInDontFree = 0;
133   pStubMsg->fDontCallFreeInst = 0;
134   pStubMsg->fInOnlyParam = 0;
135   pStubMsg->fHasReturn = 0;
136   pStubMsg->fHasExtensions = 0;
137   pStubMsg->fHasNewCorrDesc = 0;
138   pStubMsg->fIsIn = 0;
139   pStubMsg->fIsOut = 0;
140   pStubMsg->fIsOicf = 0;
141   pStubMsg->fHasMemoryValidateCallback = 0;
142   pStubMsg->fInFree = 0;
143   pStubMsg->fNeedMCCP = 0;
144   pStubMsg->fUnused = 0;
145   pStubMsg->dwDestContext = MSHCTX_DIFFERENTMACHINE;
146   pStubMsg->pvDestContext = NULL;
147   pStubMsg->pRpcChannelBuffer = NULL;
148   pStubMsg->pArrayInfo = NULL;
149   pStubMsg->dwStubPhase = 0;
150   /* FIXME: LowStackMark */
151   pStubMsg->pAsyncMsg = NULL;
152   pStubMsg->pCorrInfo = NULL;
153   pStubMsg->pCorrMemory = NULL;
154   pStubMsg->pMemoryList = NULL;
155
156   return NULL;
157 }
158
159 /***********************************************************************
160  *           NdrGetBuffer [RPCRT4.@]
161  */
162 unsigned char *WINAPI NdrGetBuffer(PMIDL_STUB_MESSAGE stubmsg, ULONG buflen, RPC_BINDING_HANDLE handle)
163 {
164   RPC_STATUS status;
165
166   TRACE("(stubmsg == ^%p, buflen == %u, handle == %p)\n", stubmsg, buflen, handle);
167   
168   stubmsg->RpcMsg->Handle = handle;
169   stubmsg->RpcMsg->BufferLength = buflen;
170
171   status = I_RpcGetBuffer(stubmsg->RpcMsg);
172   if (status != RPC_S_OK)
173     RpcRaiseException(status);
174
175   stubmsg->Buffer = stubmsg->RpcMsg->Buffer;
176   stubmsg->fBufferValid = TRUE;
177   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
178   return stubmsg->Buffer;
179 }
180 /***********************************************************************
181  *           NdrFreeBuffer [RPCRT4.@]
182  */
183 void WINAPI NdrFreeBuffer(PMIDL_STUB_MESSAGE pStubMsg)
184 {
185   TRACE("(pStubMsg == ^%p)\n", pStubMsg);
186   if (pStubMsg->fBufferValid)
187   {
188     I_RpcFreeBuffer(pStubMsg->RpcMsg);
189     pStubMsg->fBufferValid = FALSE;
190   }
191 }
192
193 /************************************************************************
194  *           NdrSendReceive [RPCRT4.@]
195  */
196 unsigned char *WINAPI NdrSendReceive( PMIDL_STUB_MESSAGE stubmsg, unsigned char *buffer  )
197 {
198   RPC_STATUS status;
199
200   TRACE("(stubmsg == ^%p, buffer == ^%p)\n", stubmsg, buffer);
201
202   /* FIXME: how to handle errors? (raise exception?) */
203   if (!stubmsg) {
204     ERR("NULL stub message.  No action taken.\n");
205     return NULL;
206   }
207   if (!stubmsg->RpcMsg) {
208     ERR("RPC Message not present in stub message.  No action taken.\n");
209     return NULL;
210   }
211
212   /* avoid sending uninitialised parts of the buffer on the wire */
213   stubmsg->RpcMsg->BufferLength = buffer - (unsigned char *)stubmsg->RpcMsg->Buffer;
214   status = I_RpcSendReceive(stubmsg->RpcMsg);
215   if (status != RPC_S_OK)
216     RpcRaiseException(status);
217
218   stubmsg->BufferLength = stubmsg->RpcMsg->BufferLength;
219   stubmsg->BufferStart = stubmsg->RpcMsg->Buffer;
220   stubmsg->BufferEnd = stubmsg->BufferStart + stubmsg->BufferLength;
221   stubmsg->Buffer = stubmsg->BufferStart;
222
223   /* FIXME: is this the right return value? */
224   return NULL;
225 }
226
227 /************************************************************************
228  *           NdrMapCommAndFaultStatus [RPCRT4.@]
229  */
230 RPC_STATUS RPC_ENTRY NdrMapCommAndFaultStatus( PMIDL_STUB_MESSAGE pStubMsg,
231                                                ULONG *pCommStatus,
232                                                ULONG *pFaultStatus,
233                                                RPC_STATUS Status )
234 {
235     TRACE("(%p, %p, %p, %d)\n", pStubMsg, pCommStatus, pFaultStatus, Status);
236
237     switch (Status)
238     {
239     case ERROR_INVALID_HANDLE:
240     case RPC_S_INVALID_BINDING:
241     case RPC_S_UNKNOWN_IF:
242     case RPC_S_SERVER_UNAVAILABLE:
243     case RPC_S_SERVER_TOO_BUSY:
244     case RPC_S_CALL_FAILED_DNE:
245     case RPC_S_PROTOCOL_ERROR:
246     case RPC_S_UNSUPPORTED_TRANS_SYN:
247     case RPC_S_UNSUPPORTED_TYPE:
248     case RPC_S_PROCNUM_OUT_OF_RANGE:
249     case EPT_S_NOT_REGISTERED:
250     case RPC_S_COMM_FAILURE:
251         *pCommStatus = Status;
252         *pFaultStatus = 0;
253         break;
254     default:
255         *pCommStatus = 0;
256         *pFaultStatus = Status;
257     }
258
259     return RPC_S_OK;
260 }