rpcrt4: Correctly align the buffer before reading/writing structure data.
[wine] / tools / widl / client.c
1 /*
2  * IDL Compiler
3  *
4  * Copyright 2005-2006 Eric Kohl
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
21 #include "config.h"
22 #include "wine/port.h"
23  
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <signal.h>
31
32 #include "windef.h"
33
34 #include "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
38
39 #include "widltypes.h"
40 #include "typelib.h"
41 #include "typelib_struct.h"
42 #include "typegen.h"
43
44 #define END_OF_LIST(list)       \
45   do {                          \
46     if (list) {                 \
47       while (NEXT_LINK(list))   \
48         list = NEXT_LINK(list); \
49     }                           \
50   } while(0)
51
52 static FILE* client;
53 static int indent = 0;
54
55 static int print_client( const char *format, ... )
56 {
57     va_list va;
58     int i, r;
59
60     va_start(va, format);
61     for (i = 0; i < indent; i++)
62         fprintf(client, "    ");
63     r = vfprintf(client, format, va);
64     va_end(va);
65     return r;
66 }
67
68
69 static void print_message_buffer_size(const func_t *func)
70 {
71     unsigned int total_size = 0;
72
73     if (func->args)
74     {
75         const var_t *var = func->args;
76         while (NEXT_LINK(var)) var = NEXT_LINK(var);
77         while (var)
78         {
79             unsigned int alignment;
80
81             total_size += get_required_buffer_size(var, &alignment, PASS_IN);
82             total_size += alignment;
83
84             var = PREV_LINK(var);
85         }
86     }
87     fprintf(client, " %u", total_size);
88 }
89
90 static void check_pointers(const func_t *func)
91 {
92     var_t *var;
93     int pointer_type;
94
95     if (!func->args)
96         return;
97
98     var = func->args;
99     while (NEXT_LINK(var)) var = NEXT_LINK(var);
100     while (var)
101     {
102         pointer_type = get_attrv(var->attrs, ATTR_POINTERTYPE);
103         if (!pointer_type)
104             pointer_type = RPC_FC_RP;
105
106         if (pointer_type == RPC_FC_RP)
107         {
108             if (var->ptr_level >= 1)
109             {
110                 print_client("if (!%s)\n", var->name);
111                 print_client("{\n");
112                 indent++;
113                 print_client("RpcRaiseException(RPC_X_NULL_REF_POINTER);\n");
114                 indent--;
115                 print_client("}\n\n");
116             }
117         }
118
119         var = PREV_LINK(var);
120     }
121 }
122
123 static void write_function_stubs(type_t *iface)
124 {
125     const func_t *func = iface->funcs;
126     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
127     int explicit_handle = is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE);
128     var_t *var;
129     int method_count = 0;
130     unsigned int proc_offset = 0;
131     unsigned int type_offset = 2;
132
133     while (NEXT_LINK(func)) func = NEXT_LINK(func);
134     while (func)
135     {
136         const var_t *def = func->def;
137         const var_t* explicit_handle_var;
138         unsigned int type_offset_func;
139
140         /* check for a defined binding handle */
141         explicit_handle_var = get_explicit_handle_var(func);
142         if (explicit_handle)
143         {
144             if (!explicit_handle_var)
145             {
146                 error("%s() does not define an explicit binding handle!\n", def->name);
147                 return;
148             }
149         }
150         else if (implicit_handle)
151         {
152             if (explicit_handle_var)
153             {
154                 error("%s() must not define a binding handle!\n", def->name);
155                 return;
156             }
157         }
158
159         write_type(client, def->type, def, def->tname);
160         fprintf(client, " ");
161         write_name(client, def);
162         fprintf(client, "(\n");
163         indent++;
164         if (func->args)
165             write_args(client, func->args, iface->name, 0, TRUE);
166         else
167             print_client("void");
168         fprintf(client, ")\n");
169         indent--;
170
171         /* write the functions body */
172         fprintf(client, "{\n");
173         indent++;
174
175         /* declare return value '_RetVal' */
176         if (!is_void(def->type, NULL))
177         {
178             print_client("");
179             write_type(client, def->type, def, def->tname);
180             fprintf(client, " _RetVal;\n");
181         }
182
183         if (implicit_handle || explicit_handle_var)
184             print_client("RPC_BINDING_HANDLE _Handle = 0;\n");
185
186         print_client("RPC_MESSAGE _RpcMessage;\n");
187         print_client("MIDL_STUB_MESSAGE _StubMsg;\n");
188         fprintf(client, "\n");
189
190         /* check pointers */
191         check_pointers(func);
192
193         print_client("RpcTryFinally\n");
194         print_client("{\n");
195         indent++;
196
197         print_client("NdrClientInitializeNew(\n");
198         indent++;
199         print_client("(PRPC_MESSAGE)&_RpcMessage,\n");
200         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
201         print_client("(PMIDL_STUB_DESC)&%s_StubDesc,\n", iface->name);
202         print_client("%d);\n", method_count);
203         indent--;
204         fprintf(client, "\n");
205
206         if (implicit_handle)
207         {
208             print_client("_Handle = %s;\n", implicit_handle);
209             fprintf(client, "\n");
210         }
211         else if (explicit_handle_var)
212         {
213             print_client("_Handle = %s;\n", explicit_handle_var->name);
214             fprintf(client, "\n");
215         }
216
217         /* emit the message buffer size */
218         print_client("_StubMsg.BufferLength =");
219         print_message_buffer_size(func);
220         fprintf(client, ";\n");
221
222         type_offset_func = type_offset;
223         write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_BUFFERSIZE);
224
225         print_client("NdrGetBuffer(\n");
226         indent++;
227         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
228         print_client("_StubMsg.BufferLength,\n");
229         if (implicit_handle || explicit_handle_var)
230             print_client("_Handle);\n");
231         else
232             print_client("%s__MIDL_AutoBindHandle);\n", iface->name);
233         indent--;
234         fprintf(client, "\n");
235
236
237         /* make a copy so we don't increment the type offset twice */
238         type_offset_func = type_offset;
239
240         /* marshal arguments */
241         write_remoting_arguments(client, indent, func, &type_offset_func, PASS_IN, PHASE_MARSHAL);
242
243         /* send/receive message */
244         /* print_client("NdrNsSendReceive(\n"); */
245         /* print_client("(unsigned char *)_StubMsg.Buffer,\n"); */
246         /* print_client("(RPC_BINDING_HANDLE *) &%s__MIDL_AutoBindHandle);\n", iface->name); */
247         print_client("NdrSendReceive(\n");
248         indent++;
249         print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
250         print_client("(unsigned char *)_StubMsg.Buffer);\n\n");
251         indent--;
252
253         print_client("_StubMsg.BufferStart = (unsigned char *)_RpcMessage.Buffer;\n");
254         print_client("_StubMsg.BufferEnd = _StubMsg.BufferStart + _RpcMessage.BufferLength;\n");
255
256         if (has_out_arg_or_return(func))
257         {
258             fprintf(client, "\n");
259
260             print_client("if ((_RpcMessage.DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
261             indent++;
262             print_client("NdrConvert(\n");
263             indent++;
264             print_client("(PMIDL_STUB_MESSAGE)&_StubMsg,\n");
265             print_client("(PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", proc_offset);
266             indent -= 2;
267         }
268
269         /* unmarshall arguments */
270         fprintf(client, "\n");
271         write_remoting_arguments(client, indent, func, &type_offset, PASS_OUT, PHASE_UNMARSHAL);
272
273         /* unmarshal return value */
274         if (!is_void(def->type, NULL))
275             print_phase_basetype(client, indent, PHASE_UNMARSHAL, PASS_RETURN, def, "_RetVal");
276
277         /* update proc_offset */
278         if (func->args)
279         {
280             var = func->args;
281             while (NEXT_LINK(var)) var = NEXT_LINK(var);
282             while (var)
283             {
284                 proc_offset += get_size_procformatstring_var(var);
285                 var = PREV_LINK(var);
286             }
287         }
288         if (!is_void(def->type, NULL))
289             proc_offset += get_size_procformatstring_var(def);
290         else
291             proc_offset += 2; /* FC_END and FC_PAD */
292
293         indent--;
294         print_client("}\n");
295         print_client("RpcFinally\n");
296         print_client("{\n");
297         indent++;
298
299
300         /* FIXME: emit client finally code */
301
302         print_client("NdrFreeBuffer((PMIDL_STUB_MESSAGE)&_StubMsg);\n");
303
304         indent--;
305         print_client("}\n");
306         print_client("RpcEndFinally\n");
307
308
309         /* emit return code */
310         if (!is_void(def->type, NULL))
311         {
312             fprintf(client, "\n");
313             print_client("return _RetVal;\n");
314         }
315
316         indent--;
317         fprintf(client, "}\n");
318         fprintf(client, "\n");
319
320         method_count++;
321         func = PREV_LINK(func);
322     }
323 }
324
325
326 static void write_bindinghandledecl(type_t *iface)
327 {
328     print_client("static RPC_BINDING_HANDLE %s__MIDL_AutoBindHandle;\n", iface->name);
329     fprintf(client, "\n");
330 }
331
332
333 static void write_stubdescdecl(type_t *iface)
334 {
335     print_client("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
336     fprintf(client, "\n");
337 }
338
339
340 static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
341 {
342     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
343
344     print_client("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
345     print_client("{\n");
346     indent++;
347     print_client("(void *)& %s___RpcClientInterface,\n", iface->name);
348     print_client("MIDL_user_allocate,\n");
349     print_client("MIDL_user_free,\n");
350     print_client("{\n");
351     indent++;
352     if (implicit_handle)
353         print_client("&%s,\n", implicit_handle);
354     else
355         print_client("&%s__MIDL_AutoBindHandle,\n", iface->name);
356     indent--;
357     print_client("},\n");
358     print_client("0,\n");
359     print_client("0,\n");
360     if (expr_eval_routines)
361         print_client("ExprEvalRoutines,\n");
362     else
363         print_client("0,\n");
364     print_client("0,\n");
365     print_client("__MIDL_TypeFormatString.Format,\n");
366     print_client("1, /* -error bounds_check flag */\n");
367     print_client("0x10001, /* Ndr library version */\n");
368     print_client("0,\n");
369     print_client("0x50100a4, /* MIDL Version 5.1.164 */\n");
370     print_client("0,\n");
371     print_client("0,\n");
372     print_client("0,  /* notify & notify_flag routine table */\n");
373     print_client("1,  /* Flags */\n");
374     print_client("0,  /* Reserved3 */\n");
375     print_client("0,  /* Reserved4 */\n");
376     print_client("0   /* Reserved5 */\n");
377     indent--;
378     print_client("};\n");
379     fprintf(client, "\n");
380 }
381
382
383 static void write_clientinterfacedecl(type_t *iface)
384 {
385     unsigned long ver = get_attrv(iface->attrs, ATTR_VERSION);
386     const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
387
388     print_client("static const RPC_CLIENT_INTERFACE %s___RpcClientInterface =\n", iface->name );
389     print_client("{\n");
390     indent++;
391     print_client("sizeof(RPC_CLIENT_INTERFACE),\n");
392     print_client("{{0x%08lx,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
393                  uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
394                  uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
395                  uuid->Data4[7], LOWORD(ver), HIWORD(ver));
396     print_client("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
397     print_client("0,\n");
398     print_client("0,\n");
399     print_client("0,\n");
400     print_client("0,\n");
401     print_client("0,\n");
402     print_client("0,\n");
403     indent--;
404     print_client("};\n");
405     if (old_names)
406         print_client("RPC_IF_HANDLE %s_ClientIfHandle = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
407                      iface->name, iface->name);
408     else
409         print_client("RPC_IF_HANDLE %s_v%d_%d_c_ifspec = (RPC_IF_HANDLE)& %s___RpcClientInterface;\n",
410                      iface->name, LOWORD(ver), HIWORD(ver), iface->name);
411     fprintf(client, "\n");
412 }
413
414
415 static void write_formatdesc( const char *str )
416 {
417     print_client("typedef struct _MIDL_%s_FORMAT_STRING\n", str );
418     print_client("{\n");
419     indent++;
420     print_client("short Pad;\n");
421     print_client("unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
422     indent--;
423     print_client("} MIDL_%s_FORMAT_STRING;\n", str);
424     print_client("\n");
425 }
426
427
428 static void write_formatstringsdecl(type_t *iface)
429 {
430     print_client("#define TYPE_FORMAT_STRING_SIZE %d\n",
431                  get_size_typeformatstring(iface));
432
433     print_client("#define PROC_FORMAT_STRING_SIZE %d\n",
434                  get_size_procformatstring(iface));
435
436     fprintf(client, "\n");
437     write_formatdesc("TYPE");
438     write_formatdesc("PROC");
439     fprintf(client, "\n");
440     print_client("static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
441     print_client("static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
442     print_client("\n");
443 }
444
445
446 static void write_implicithandledecl(type_t *iface)
447 {
448     const char *implicit_handle = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
449
450     if (implicit_handle)
451     {
452         fprintf(client, "handle_t %s;\n", implicit_handle);
453         fprintf(client, "\n");
454     }
455 }
456
457
458 static void init_client(void)
459 {
460     if (client) return;
461     if (!(client = fopen(client_name, "w")))
462         error("Could not open %s for output\n", client_name);
463
464     print_client("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
465     print_client("#include <string.h>\n");
466     print_client("#ifdef _ALPHA_\n");
467     print_client("#include <stdarg.h>\n");
468     print_client("#endif\n");
469     fprintf(client, "\n");
470     print_client("#include \"%s\"\n", header_name);
471     fprintf(client, "\n");
472 }
473
474
475 void write_client(ifref_t *ifaces)
476 {
477     ifref_t *iface = ifaces;
478
479     if (!do_client)
480         return;
481     if (!iface)
482         return;
483     END_OF_LIST(iface);
484
485     init_client();
486     if (!client)
487         return;
488
489     for (; iface; iface = PREV_LINK(iface))
490     {
491         if (is_object(iface->iface->attrs) || is_local(iface->iface->attrs))
492             continue;
493
494         fprintf(client, "/*****************************************************************************\n");
495         fprintf(client, " * %s interface\n", iface->iface->name);
496         fprintf(client, " */\n");
497         fprintf(client, "\n");
498
499         if (iface->iface->funcs)
500         {
501             int expr_eval_routines;
502
503             write_formatstringsdecl(iface->iface);
504             write_implicithandledecl(iface->iface);
505     
506             write_clientinterfacedecl(iface->iface);
507             write_stubdescdecl(iface->iface);
508             write_bindinghandledecl(iface->iface);
509     
510             write_function_stubs(iface->iface);
511
512             print_client("#if !defined(__RPC_WIN32__)\n");
513             print_client("#error  Invalid build platform for this stub.\n");
514             print_client("#endif\n");
515             fprintf(client, "\n");
516
517             write_procformatstring(client, iface->iface);
518             write_typeformatstring(client, iface->iface);
519
520             fprintf(client, "\n");
521
522             expr_eval_routines = write_expr_eval_routines(client, iface->iface->name);
523             if (expr_eval_routines)
524                 write_expr_eval_routine_list(client, iface->iface->name);
525             write_stubdescriptor(iface->iface, expr_eval_routines);
526         }
527     }
528
529     fclose(client);
530 }