Keep track of per-column information inside the listview.
[wine] / dlls / rpcrt4 / rpc_binding.c
1 /*
2  * RPC binding API
3  *
4  * Copyright 2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * TODO:
21  *  - a whole lot
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "winerror.h"
31 #include "winreg.h"
32 #include "wine/unicode.h"
33
34 #include "rpc.h"
35
36 #include "wine/debug.h"
37
38 #include "rpc_binding.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41
42 LPSTR RPCRT4_strndupA(LPSTR src, INT slen)
43 {
44   DWORD len;
45   LPSTR s;
46   if (!src) return NULL;
47   if (slen == -1) slen = strlen(src);
48   len = slen;
49   s = HeapAlloc(GetProcessHeap(), 0, len+1);
50   memcpy(s, src, len);
51   s[len] = 0;
52   return s;
53 }
54
55 LPSTR RPCRT4_strdupWtoA(LPWSTR src)
56 {
57   DWORD len;
58   LPSTR s;
59   if (!src) return NULL;
60   len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
61   s = HeapAlloc(GetProcessHeap(), 0, len);
62   WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
63   return s;
64 }
65
66 LPWSTR RPCRT4_strdupAtoW(LPSTR src)
67 {
68   DWORD len;
69   LPWSTR s;
70   if (!src) return NULL;
71   len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
72   s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
73   MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
74   return s;
75 }
76
77 LPWSTR RPCRT4_strndupW(LPWSTR src, INT slen)
78 {
79   DWORD len;
80   LPWSTR s;
81   if (!src) return NULL;
82   if (slen == -1) slen = strlenW(src);
83   len = slen;
84   s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
85   memcpy(s, src, len*sizeof(WCHAR));
86   s[len] = 0;
87   return s;
88 }
89
90 void RPCRT4_strfree(LPSTR src)
91 {
92   if (src) HeapFree(GetProcessHeap(), 0, src);
93 }
94
95 RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPSTR Protseq)
96 {
97   RpcBinding* NewBinding;
98
99   NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
100   NewBinding->refs = 1;
101   NewBinding->server = server;
102   NewBinding->Protseq = RPCRT4_strdupA(Protseq);
103
104   TRACE("binding: %p\n", NewBinding);
105   *Binding = NewBinding;
106
107   return RPC_S_OK;
108 }
109
110 RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPWSTR Protseq)
111 {
112   RpcBinding* NewBinding;
113   if (Binding)
114     TRACE("  (*Binding == ^%p, server == %s, Protseq == \"%s\")\n", *Binding, server ? "Yes" : "No", debugstr_w(Protseq));
115   else {
116     ERR("!RpcBinding?\n"); 
117     *((char *)0) = 0; /* we will crash below anyhow... */
118   }
119
120   NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
121   NewBinding->refs = 1;
122   NewBinding->server = server;
123   NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
124
125   TRACE("binding: %p\n", NewBinding);
126   *Binding = NewBinding;
127
128   return RPC_S_OK;
129 }
130
131 RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPSTR NetworkAddr,  LPSTR Endpoint,  LPSTR NetworkOptions)
132 {
133   
134   TRACE("  (RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding, NetworkAddr, Endpoint, NetworkOptions);
135
136   RPCRT4_strfree(Binding->NetworkAddr);
137   Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
138   RPCRT4_strfree(Binding->Endpoint);
139   Binding->Endpoint = RPCRT4_strdupA(Endpoint);
140
141   return RPC_S_OK;
142 }
143
144 RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPWSTR NetworkAddr, LPWSTR Endpoint, LPWSTR NetworkOptions)
145 {
146
147   TRACE("  (RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding, 
148    debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
149
150   RPCRT4_strfree(Binding->NetworkAddr);
151   Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
152   RPCRT4_strfree(Binding->Endpoint);
153   Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
154
155   return RPC_S_OK;
156 }
157
158 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPSTR Endpoint)
159 {
160   RPCRT4_strfree(Binding->Endpoint);
161   Binding->Endpoint = RPCRT4_strdupA(Endpoint);
162
163   return RPC_S_OK;
164 }
165
166 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, UUID* ObjectUuid)
167 {
168   TRACE("  (*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid)); 
169   if (ObjectUuid) memcpy(&Binding->ObjectUuid, ObjectUuid, sizeof(UUID));
170   else UuidCreateNil(&Binding->ObjectUuid);
171   return RPC_S_OK;
172 }
173
174 RPC_STATUS RPCRT4_SpawnBinding(RpcBinding** Binding, RpcBinding* OldBinding)
175 {
176   RpcBinding* NewBinding;
177   if (Binding)
178     TRACE("  (*RpcBinding == ^%p, OldBinding == ^%p)\n", *Binding, OldBinding);
179   else {
180     ERR("!RpcBinding?"); 
181     /* we will crash below anyhow... */
182     *((char *)0) = 0;
183   }
184
185   NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
186   NewBinding->refs = 1;
187   NewBinding->server = OldBinding->server;
188   NewBinding->Protseq = RPCRT4_strdupA(OldBinding->Protseq);
189   NewBinding->NetworkAddr = RPCRT4_strdupA(OldBinding->NetworkAddr);
190   NewBinding->Endpoint = RPCRT4_strdupA(OldBinding->Endpoint);
191   /* because of the way named pipes work, we'll transfer the connected pipe
192    * to the child, then reopen the server binding to continue listening */
193   NewBinding->conn = OldBinding->conn;
194   NewBinding->ovl = OldBinding->ovl;
195   OldBinding->conn = 0;
196   memset(&OldBinding->ovl, 0, sizeof(OldBinding->ovl));
197   *Binding = NewBinding;
198   RPCRT4_OpenBinding(OldBinding);
199
200   return RPC_S_OK;
201 }
202
203 RPC_STATUS RPCRT4_ExportBinding(RpcBinding** Binding, RpcBinding* OldBinding)
204 {
205   InterlockedIncrement(&OldBinding->refs);
206   *Binding = OldBinding;
207   return RPC_S_OK;
208 }
209
210 RPC_STATUS RPCRT4_DestroyBinding(RpcBinding* Binding)
211 {
212   if (InterlockedDecrement(&Binding->refs))
213     return RPC_S_OK;
214
215   TRACE("binding: %p\n", Binding);
216   RPCRT4_CloseBinding(Binding);
217   RPCRT4_strfree(Binding->Endpoint);
218   RPCRT4_strfree(Binding->NetworkAddr);
219   RPCRT4_strfree(Binding->Protseq);
220   HeapFree(GetProcessHeap(), 0, Binding);
221   return RPC_S_OK;
222 }
223
224 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding)
225 {
226   TRACE("  (Binding == ^%p)\n", Binding);
227   if (!Binding->conn) {
228     if (Binding->server) { /* server */
229       /* protseq=ncalrpc: supposed to use NT LPC ports,
230        * but we'll implement it with named pipes for now */
231       if (strcmp(Binding->Protseq, "ncalrpc") == 0) {
232         static LPSTR prefix = "\\\\.\\pipe\\lrpc\\";
233         LPSTR pname;
234         pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Binding->Endpoint) + 1);
235         strcat(strcpy(pname, prefix), Binding->Endpoint);
236        TRACE("listening on %s\n", pname);
237         Binding->conn = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
238                                          0, PIPE_UNLIMITED_INSTANCES, 0, 0, 5000, NULL);
239         HeapFree(GetProcessHeap(), 0, pname);
240         memset(&Binding->ovl, 0, sizeof(Binding->ovl));
241         Binding->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
242         if (!ConnectNamedPipe(Binding->conn, &Binding->ovl)) {
243           DWORD err = GetLastError();
244           if (err == ERROR_PIPE_CONNECTED) {
245             SetEvent(Binding->ovl.hEvent);
246             return RPC_S_OK;
247           }
248           return err;
249         }
250       }
251       /* protseq=ncacn_np: named pipes */
252       else if (strcmp(Binding->Protseq, "ncacn_np") == 0) {
253         static LPSTR prefix = "\\\\.";
254         LPSTR pname;
255         pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Binding->Endpoint) + 1);
256         strcat(strcpy(pname, prefix), Binding->Endpoint);
257        TRACE("listening on %s\n", pname);
258         Binding->conn = CreateNamedPipeA(pname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
259                                          0, PIPE_UNLIMITED_INSTANCES, 0, 0, 5000, NULL);
260         HeapFree(GetProcessHeap(), 0, pname);
261         memset(&Binding->ovl, 0, sizeof(Binding->ovl));
262         Binding->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
263         if (!ConnectNamedPipe(Binding->conn, &Binding->ovl)) {
264           DWORD err = GetLastError();
265           if (err == ERROR_PIPE_CONNECTED) {
266             SetEvent(Binding->ovl.hEvent);
267             return RPC_S_OK;
268           }
269           return err;
270         }
271       }
272       else {
273        ERR("protseq %s not supported\n", Binding->Protseq);
274        return RPC_S_PROTSEQ_NOT_SUPPORTED;
275       }
276     }
277     else { /* client */
278       /* protseq=ncalrpc: supposed to use NT LPC ports,
279        * but we'll implement it with named pipes for now */
280       if (strcmp(Binding->Protseq, "ncalrpc") == 0) {
281         static LPSTR prefix = "\\\\.\\pipe\\lrpc\\";
282         LPSTR pname;
283        HANDLE conn;
284        DWORD err;
285
286         pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Binding->Endpoint) + 1);
287         strcat(strcpy(pname, prefix), Binding->Endpoint);
288        TRACE("connecting to %s\n", pname);
289        while (TRUE) {
290          if (WaitNamedPipeA(pname, NMPWAIT_WAIT_FOREVER)) {
291            conn = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
292                               OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
293            if (conn != INVALID_HANDLE_VALUE) break;
294            err = GetLastError();
295            if (err == ERROR_PIPE_BUSY) continue;
296            TRACE("connection failed, error=%lx\n", err);
297            HeapFree(GetProcessHeap(), 0, pname);
298            return err;
299          }
300          else {
301            err = GetLastError();
302            TRACE("connection failed, error=%lx\n", err);
303            HeapFree(GetProcessHeap(), 0, pname);
304            return err;
305          }
306        }
307
308        /* success */
309        HeapFree(GetProcessHeap(), 0, pname);
310         memset(&Binding->ovl, 0, sizeof(Binding->ovl));
311         Binding->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
312        Binding->conn = conn;
313       }
314       /* protseq=ncacn_np: named pipes */
315       else if (strcmp(Binding->Protseq, "ncacn_np") == 0) {
316         static LPSTR prefix = "\\\\.";
317         LPSTR pname;
318        HANDLE conn;
319        DWORD err;
320
321         pname = HeapAlloc(GetProcessHeap(), 0, strlen(prefix) + strlen(Binding->Endpoint) + 1);
322         strcat(strcpy(pname, prefix), Binding->Endpoint);
323        TRACE("connecting to %s\n", pname);
324        conn = CreateFileA(pname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
325                           OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
326        if (conn == INVALID_HANDLE_VALUE) {
327          err = GetLastError();
328          /* we don't need to handle ERROR_PIPE_BUSY here,
329           * the doc says that it is returned to the app */
330          TRACE("connection failed, error=%lx\n", err);
331          HeapFree(GetProcessHeap(), 0, pname);
332          return err;
333        }
334
335        /* success */
336        HeapFree(GetProcessHeap(), 0, pname);
337         memset(&Binding->ovl, 0, sizeof(Binding->ovl));
338         Binding->ovl.hEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
339        Binding->conn = conn;
340       }
341       else {
342        ERR("protseq %s not supported\n", Binding->Protseq);
343        return RPC_S_PROTSEQ_NOT_SUPPORTED;
344       }
345     }
346   }
347   return RPC_S_OK;
348 }
349
350 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding)
351 {
352   TRACE("  (Binding == ^%p)\n", Binding);
353   if (Binding->conn) {
354     CancelIo(Binding->conn);
355     CloseHandle(Binding->conn);
356     Binding->conn = 0;
357   }
358   if (Binding->ovl.hEvent) {
359     CloseHandle(Binding->ovl.hEvent);
360     Binding->ovl.hEvent = 0;
361   }
362   return RPC_S_OK;
363 }
364
365 /* utility functions for string composing and parsing */
366 static unsigned RPCRT4_strcopyA(LPSTR data, LPCSTR src)
367 {
368   unsigned len = strlen(src);
369   memcpy(data, src, len*sizeof(CHAR));
370   return len;
371 }
372
373 static unsigned RPCRT4_strcopyW(LPWSTR data, LPCWSTR src)
374 {
375   unsigned len = strlenW(src);
376   memcpy(data, src, len*sizeof(WCHAR));
377   return len;
378 }
379
380 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
381 {
382   DWORD len = strlen(dst), slen = strlen(src);
383   LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
384   if (!ndst) HeapFree(GetProcessHeap(), 0, dst);
385   ndst[len] = ',';
386   memcpy(ndst+len+1, src, slen*sizeof(CHAR));
387   ndst[len+slen+1] = 0;
388   return ndst;
389 }
390
391 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
392 {
393   DWORD len = strlenW(dst), slen = strlenW(src);
394   LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
395   if (!ndst) HeapFree(GetProcessHeap(), 0, dst);
396   ndst[len] = ',';
397   memcpy(ndst+len+1, src, slen*sizeof(WCHAR));
398   ndst[len+slen+1] = 0;
399   return ndst;
400 }
401
402
403 /***********************************************************************
404  *             RpcStringBindingComposeA (RPCRT4.@)
405  */
406 RPC_STATUS WINAPI RpcStringBindingComposeA( LPSTR ObjUuid, LPSTR Protseq,
407                                            LPSTR NetworkAddr, LPSTR Endpoint,
408                                            LPSTR Options, LPSTR* StringBinding )
409 {
410   DWORD len = 1;
411   LPSTR data;
412
413   TRACE( "(%s,%s,%s,%s,%s,%p)\n",
414         debugstr_a( ObjUuid ), debugstr_a( Protseq ),
415         debugstr_a( NetworkAddr ), debugstr_a( Endpoint ),
416         debugstr_a( Options ), StringBinding );
417
418   if (ObjUuid && *ObjUuid) len += strlen(ObjUuid) + 1;
419   if (Protseq && *Protseq) len += strlen(Protseq) + 1;
420   if (NetworkAddr && *NetworkAddr) len += strlen(NetworkAddr);
421   if (Endpoint && *Endpoint) len += strlen(Endpoint) + 2;
422   if (Options && *Options) len += strlen(Options) + 2;
423
424   data = HeapAlloc(GetProcessHeap(), 0, len);
425   *StringBinding = data;
426
427   if (ObjUuid && *ObjUuid) {
428     data += RPCRT4_strcopyA(data, ObjUuid);
429     *data++ = '@';
430   }
431   if (Protseq && *Protseq) {
432     data += RPCRT4_strcopyA(data, Protseq);
433     *data++ = ':';
434   }
435   if (NetworkAddr && *NetworkAddr) {
436     data += RPCRT4_strcopyA(data, NetworkAddr);
437   }
438   if ((Endpoint && *Endpoint) ||
439       (Options && *Options)) {
440     *data++ = '[';
441     if (Endpoint && *Endpoint) {
442       data += RPCRT4_strcopyA(data, Endpoint);
443       if (Options && *Options) *data++ = ',';
444     }
445     if (Options && *Options) {
446       data += RPCRT4_strcopyA(data, Options);
447     }
448     *data++ = ']';
449   }
450   *data = 0;
451
452   return RPC_S_OK;
453 }
454
455 /***********************************************************************
456  *             RpcStringBindingComposeW (RPCRT4.@)
457  */
458 RPC_STATUS WINAPI RpcStringBindingComposeW( LPWSTR ObjUuid, LPWSTR Protseq,
459                                             LPWSTR NetworkAddr, LPWSTR Endpoint,
460                                             LPWSTR Options, LPWSTR* StringBinding )
461 {
462   DWORD len = 1;
463   LPWSTR data;
464
465   TRACE("(%s,%s,%s,%s,%s,%p)\n",
466        debugstr_w( ObjUuid ), debugstr_w( Protseq ),
467        debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
468        debugstr_w( Options ), StringBinding);
469
470   if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) + 1;
471   if (Protseq && *Protseq) len += strlenW(Protseq) + 1;
472   if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr);
473   if (Endpoint && *Endpoint) len += strlenW(Endpoint) + 2;
474   if (Options && *Options) len += strlenW(Options) + 2;
475
476   data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
477   *StringBinding = data;
478
479   if (ObjUuid && *ObjUuid) {
480     data += RPCRT4_strcopyW(data, ObjUuid);
481     *data++ = '@';
482   }
483   if (Protseq && *Protseq) {
484     data += RPCRT4_strcopyW(data, Protseq);
485     *data++ = ':';
486   }
487   if (NetworkAddr && *NetworkAddr) {
488     data += RPCRT4_strcopyW(data, NetworkAddr);
489   }
490   if ((Endpoint && *Endpoint) ||
491       (Options && *Options)) {
492     *data++ = '[';
493     if (Endpoint && *Endpoint) {
494       data += RPCRT4_strcopyW(data, Endpoint);
495       if (Options && *Options) *data++ = ',';
496     }
497     if (Options && *Options) {
498       data += RPCRT4_strcopyW(data, Options);
499     }
500     *data++ = ']';
501   }
502   *data = 0;
503
504   return RPC_S_OK;
505 }
506
507
508 /***********************************************************************
509  *             RpcStringBindingParseA (RPCRT4.@)
510  */
511 RPC_STATUS WINAPI RpcStringBindingParseA( LPSTR StringBinding, LPSTR *ObjUuid,
512                                           LPSTR *Protseq, LPSTR *NetworkAddr,
513                                           LPSTR *Endpoint, LPSTR *Options)
514 {
515   CHAR *data, *next;
516   static const char ep_opt[] = "endpoint=";
517
518   TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a(StringBinding),
519        ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
520
521   if (ObjUuid) *ObjUuid = NULL;
522   if (Protseq) *Protseq = NULL;
523   if (NetworkAddr) *NetworkAddr = NULL;
524   if (Endpoint) *Endpoint = NULL;
525   if (Options) *Options = NULL;
526
527   data = StringBinding;
528
529   next = strchr(data, '@');
530   if (next) {
531     if (ObjUuid) *ObjUuid = RPCRT4_strndupA(data, next - data);
532     data = next+1;
533   }
534
535   next = strchr(data, ':');
536   if (next) {
537     if (Protseq) *Protseq = RPCRT4_strndupA(data, next - data);
538     data = next+1;
539   }
540
541   next = strchr(data, '[');
542   if (next) {
543     CHAR *close, *opt;
544
545     if (NetworkAddr) *NetworkAddr = RPCRT4_strndupA(data, next - data);
546     data = next+1;
547     close = strchr(data, ']');
548     if (!close) goto fail;
549
550     /* tokenize options */
551     while (data < close) {
552       next = strchr(data, ',');
553       if (!next || next > close) next = close;
554       /* FIXME: this is kind of inefficient */
555       opt = RPCRT4_strndupA(data, next - data);
556       data = next+1;
557
558       /* parse option */
559       next = strchr(opt, '=');
560       if (!next) {
561        /* not an option, must be an endpoint */
562        if (*Endpoint) goto fail;
563        *Endpoint = opt;
564       }
565       else {
566        if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
567          /* endpoint option */
568          if (*Endpoint) goto fail;
569          *Endpoint = RPCRT4_strdupA(next+1);
570          HeapFree(GetProcessHeap(), 0, opt);
571        }
572        else {
573          /* network option */
574          if (*Options) {
575             /* FIXME: this is kind of inefficient */
576            *Options = RPCRT4_strconcatA(*Options, opt);
577            HeapFree(GetProcessHeap(), 0, opt);
578          }
579          else *Options = opt;
580        }
581       }
582     }
583
584     data = close+1;
585     if (*data) goto fail;
586   }
587   else if (NetworkAddr) *NetworkAddr = RPCRT4_strdupA(data);
588   return RPC_S_OK;
589
590 fail:
591   if (ObjUuid) RpcStringFreeA(ObjUuid);
592   if (Protseq) RpcStringFreeA(Protseq);
593   if (NetworkAddr) RpcStringFreeA(NetworkAddr);
594   if (Endpoint) RpcStringFreeA(Endpoint);
595   if (Options) RpcStringFreeA(Options);
596   return RPC_S_INVALID_STRING_BINDING;
597 }
598
599 /***********************************************************************
600  *             RpcStringBindingParseW (RPCRT4.@)
601  */
602 RPC_STATUS WINAPI RpcStringBindingParseW( LPWSTR StringBinding, LPWSTR *ObjUuid,
603                                           LPWSTR *Protseq, LPWSTR *NetworkAddr,
604                                           LPWSTR *Endpoint, LPWSTR *Options)
605 {
606   WCHAR *data, *next;
607   static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
608
609   TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
610        ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
611
612   if (ObjUuid) *ObjUuid = NULL;
613   if (Protseq) *Protseq = NULL;
614   if (NetworkAddr) *NetworkAddr = NULL;
615   if (Endpoint) *Endpoint = NULL;
616   if (Options) *Options = NULL;
617
618   data = StringBinding;
619
620   next = strchrW(data, '@');
621   if (next) {
622     if (ObjUuid) *ObjUuid = RPCRT4_strndupW(data, next - data);
623     data = next+1;
624   }
625
626   next = strchrW(data, ':');
627   if (next) {
628     if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
629     data = next+1;
630   }
631
632   next = strchrW(data, '[');
633   if (next) {
634     WCHAR *close, *opt;
635
636     if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
637     data = next+1;
638     close = strchrW(data, ']');
639     if (!close) goto fail;
640
641     /* tokenize options */
642     while (data < close) {
643       next = strchrW(data, ',');
644       if (!next || next > close) next = close;
645       /* FIXME: this is kind of inefficient */
646       opt = RPCRT4_strndupW(data, next - data);
647       data = next+1;
648
649       /* parse option */
650       next = strchrW(opt, '=');
651       if (!next) {
652        /* not an option, must be an endpoint */
653        if (*Endpoint) goto fail;
654        *Endpoint = opt;
655       }
656       else {
657        if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
658          /* endpoint option */
659          if (*Endpoint) goto fail;
660          *Endpoint = RPCRT4_strdupW(next+1);
661          HeapFree(GetProcessHeap(), 0, opt);
662        }
663        else {
664          /* network option */
665          if (*Options) {
666             /* FIXME: this is kind of inefficient */
667            *Options = RPCRT4_strconcatW(*Options, opt);
668            HeapFree(GetProcessHeap(), 0, opt);
669          }
670          else *Options = opt;
671        }
672       }
673     }
674
675     data = close+1;
676     if (*data) goto fail;
677   }
678   else if (NetworkAddr) *NetworkAddr = RPCRT4_strdupW(data);
679
680   return RPC_S_OK;
681 fail:
682   if (ObjUuid) RpcStringFreeW(ObjUuid);
683   if (Protseq) RpcStringFreeW(Protseq);
684   if (NetworkAddr) RpcStringFreeW(NetworkAddr);
685   if (Endpoint) RpcStringFreeW(Endpoint);
686   if (Options) RpcStringFreeW(Options);
687   return RPC_S_INVALID_STRING_BINDING;
688 }
689
690 /***********************************************************************
691  *             RpcBindingFree (RPCRT4.@)
692  */
693 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
694 {
695   RPC_STATUS status;
696   TRACE("(%p) = %p\n", Binding, *Binding);
697   status = RPCRT4_DestroyBinding(*Binding);
698   if (status == RPC_S_OK) *Binding = 0;
699   return status;
700 }
701   
702 /***********************************************************************
703  *             RpcBindingVectorFree (RPCRT4.@)
704  */
705 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
706 {
707   RPC_STATUS status;
708   unsigned long c;
709
710   TRACE("(%p)\n", BindingVector);
711   for (c=0; c<(*BindingVector)->Count; c++) {
712     status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
713   }
714   HeapFree(GetProcessHeap(), 0, *BindingVector);
715   *BindingVector = NULL;
716   return RPC_S_OK;
717 }
718   
719 /***********************************************************************
720  *             RpcBindingInqObject (RPCRT4.@)
721  */
722 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
723 {
724   RpcBinding* bind = (RpcBinding*)Binding;
725
726   TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
727   memcpy(ObjectUuid, &bind->ObjectUuid, sizeof(UUID));
728   return RPC_S_OK;
729 }
730   
731 /***********************************************************************
732  *             RpcBindingSetObject (RPCRT4.@)
733  */
734 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
735 {
736   RpcBinding* bind = (RpcBinding*)Binding;
737
738   TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
739   if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
740   return RPCRT4_SetBindingObject(Binding, ObjectUuid);
741 }
742
743 /***********************************************************************
744  *             RpcBindingFromStringBindingA (RPCRT4.@)
745  */
746 RPC_STATUS WINAPI RpcBindingFromStringBindingA( LPSTR StringBinding, RPC_BINDING_HANDLE* Binding )
747 {
748   RPC_STATUS ret;
749   RpcBinding* bind = NULL;
750   LPSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
751   UUID Uuid;
752
753   TRACE("(%s,%p)\n", debugstr_a(StringBinding), Binding);
754
755   ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
756                               &NetworkAddr, &Endpoint, &Options);
757   if (ret != RPC_S_OK) return ret;
758
759   ret = UuidFromStringA(ObjectUuid, &Uuid);
760
761   if (ret == RPC_S_OK)
762     ret = RPCRT4_CreateBindingA(&bind, FALSE, Protseq);
763   if (ret == RPC_S_OK)
764     ret = RPCRT4_SetBindingObject(bind, &Uuid);
765   if (ret == RPC_S_OK)
766     ret = RPCRT4_CompleteBindingA(bind, NetworkAddr, Endpoint, Options);
767
768   RpcStringFreeA(&Options);
769   RpcStringFreeA(&Endpoint);
770   RpcStringFreeA(&NetworkAddr);
771   RpcStringFreeA(&Protseq);
772   RpcStringFreeA(&ObjectUuid);
773
774   if (ret == RPC_S_OK) *Binding = (RPC_BINDING_HANDLE)bind;
775   else RPCRT4_DestroyBinding(bind);
776
777   return ret;
778 }
779
780 /***********************************************************************
781  *             RpcBindingFromStringBindingW (RPCRT4.@)
782  */
783 RPC_STATUS WINAPI RpcBindingFromStringBindingW( LPWSTR StringBinding, RPC_BINDING_HANDLE* Binding )
784 {
785   RPC_STATUS ret;
786   RpcBinding* bind = NULL;
787   LPWSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
788   UUID Uuid;
789
790   TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
791
792   ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
793                               &NetworkAddr, &Endpoint, &Options);
794   if (ret != RPC_S_OK) return ret;
795
796   ret = UuidFromStringW(ObjectUuid, &Uuid);
797
798   if (ret == RPC_S_OK)
799     ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
800   if (ret == RPC_S_OK)
801     ret = RPCRT4_SetBindingObject(bind, &Uuid);
802   if (ret == RPC_S_OK)
803     ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
804
805   RpcStringFreeW(&Options);
806   RpcStringFreeW(&Endpoint);
807   RpcStringFreeW(&NetworkAddr);
808   RpcStringFreeW(&Protseq);
809   RpcStringFreeW(&ObjectUuid);
810
811   if (ret == RPC_S_OK) *Binding = (RPC_BINDING_HANDLE)bind;
812   else RPCRT4_DestroyBinding(bind);
813
814   return ret;
815 }
816   
817 /***********************************************************************
818  *             RpcBindingToStringBindingA (RPCRT4.@)
819  */
820 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, LPSTR* StringBinding )
821 {
822   RPC_STATUS ret;
823   RpcBinding* bind = (RpcBinding*)Binding;
824   LPSTR ObjectUuid;
825
826   TRACE("(%p,%p)\n", Binding, StringBinding);
827
828   ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
829   if (ret != RPC_S_OK) return ret;
830
831   ret = RpcStringBindingComposeA(ObjectUuid, bind->Protseq, bind->NetworkAddr,
832                                  bind->Endpoint, NULL, StringBinding);
833
834   RpcStringFreeA(&ObjectUuid);
835
836   return ret;
837 }
838   
839 /***********************************************************************
840  *             RpcBindingToStringBindingW (RPCRT4.@)
841  */
842 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, LPWSTR* StringBinding )
843 {
844   RPC_STATUS ret;
845   LPSTR str = NULL;
846   TRACE("(%p,%p)\n", Binding, StringBinding);
847   ret = RpcBindingToStringBindingA(Binding, &str);
848   *StringBinding = RPCRT4_strdupAtoW(str);
849   RpcStringFreeA(&str);
850   return ret;
851 }
852   
853 /***********************************************************************
854  *             I_RpcBindingSetAsync (RPCRT4.@)
855  * NOTES
856  *  Exists in win9x and winNT, but with different number of arguments
857  *  (9x version has 3 arguments, NT has 2).
858  */
859 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn, unsigned long ServerTid )
860 {
861   RpcBinding* bind = (RpcBinding*)Binding;
862
863   TRACE( "(%p,%p,%ld): stub\n", Binding, BlockingFn, ServerTid );
864
865   bind->BlockingFn = BlockingFn;
866   bind->ServerTid = ServerTid;
867
868   return RPC_S_OK;
869 }