server.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "widl.h"
  26. #include "utils.h"
  27. #include "parser.h"
  28. #include "header.h"
  29. #include "typegen.h"
  30. static FILE* server;
  31. static int indent = 0;
  32. static void print_server(const char *format, ...) __attribute__((format (printf, 1, 2)));
  33. static void print_server(const char *format, ...)
  34. {
  35. va_list va;
  36. va_start(va, format);
  37. print(server, indent, format, va);
  38. va_end(va);
  39. }
  40. static void write_function_stub(const type_t *iface, const var_t *func, unsigned int proc_offset)
  41. {
  42. const var_t *var;
  43. unsigned char explicit_fc, implicit_fc;
  44. int has_full_pointer = is_full_pointer_function(func);
  45. const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
  46. type_t *ret_type = type_function_get_rettype(func->declspec.type);
  47. if (is_interpreted_func( iface, func )) return;
  48. print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func));
  49. indent++;
  50. print_server("__DECL_EXCEPTION_FRAME\n");
  51. print_server("MIDL_STUB_MESSAGE _StubMsg;\n");
  52. /* Declare arguments */
  53. declare_stub_args(server, indent, func);
  54. indent--;
  55. print_server("};\n\n");
  56. print_server("static void __finally_%s_%s(", iface->name, get_name(func));
  57. fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func));
  58. indent++;
  59. write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
  60. if (!is_void(ret_type))
  61. write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE);
  62. if (has_full_pointer)
  63. write_full_pointer_free(server, indent, func);
  64. indent--;
  65. print_server("}\n\n");
  66. print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func));
  67. /* write the functions body */
  68. fprintf(server, "{\n");
  69. indent++;
  70. print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func));
  71. if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n");
  72. fprintf(server, "\n");
  73. print_server("NdrServerInitializeNew(\n");
  74. indent++;
  75. print_server("_pRpcMessage,\n");
  76. print_server("&__frame->_StubMsg,\n");
  77. print_server("&%s_StubDesc);\n", iface->name);
  78. indent--;
  79. fprintf(server, "\n");
  80. print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func));
  81. write_parameters_init(server, indent, func, "__frame->");
  82. if (explicit_fc == FC_BIND_PRIMITIVE)
  83. {
  84. print_server("__frame->%s = _pRpcMessage->Handle;\n", handle_var->name);
  85. fprintf(server, "\n");
  86. }
  87. print_server("RpcTryFinally\n");
  88. print_server("{\n");
  89. indent++;
  90. print_server("RpcTryExcept\n");
  91. print_server("{\n");
  92. indent++;
  93. if (has_full_pointer)
  94. write_full_pointer_init(server, indent, func, TRUE);
  95. if (type_function_get_args(func->declspec.type))
  96. {
  97. print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n");
  98. indent++;
  99. print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n",
  100. proc_offset);
  101. indent--;
  102. fprintf(server, "\n");
  103. /* unmarshall arguments */
  104. write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
  105. }
  106. print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n");
  107. print_server("{\n");
  108. indent++;
  109. print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
  110. indent--;
  111. print_server("}\n");
  112. indent--;
  113. print_server("}\n");
  114. print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n");
  115. print_server("{\n");
  116. indent++;
  117. print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
  118. indent--;
  119. print_server("}\n");
  120. print_server("RpcEndExcept\n");
  121. fprintf(server, "\n");
  122. /* Assign 'out' arguments */
  123. assign_stub_out_args(server, indent, func, "__frame->");
  124. /* Call the real server function */
  125. if (is_context_handle(ret_type))
  126. {
  127. print_server("__frame->_RetVal = NDRSContextUnmarshall((char*)0, _pRpcMessage->DataRepresentation);\n");
  128. print_server("*((");
  129. write_type_decl(server, type_function_get_ret(func->declspec.type), NULL);
  130. fprintf(server, "*)NDRSContextValue(__frame->_RetVal)) = ");
  131. }
  132. else
  133. print_server("%s", is_void(ret_type) ? "" : "__frame->_RetVal = ");
  134. fprintf(server, "%s%s", prefix_server, get_name(func));
  135. if (type_function_get_args(func->declspec.type))
  136. {
  137. int first_arg = 1;
  138. fprintf(server, "(\n");
  139. indent++;
  140. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  141. {
  142. if (first_arg)
  143. first_arg = 0;
  144. else
  145. fprintf(server, ",\n");
  146. if (is_context_handle(var->declspec.type))
  147. {
  148. /* if the context_handle attribute appears in the chain of types
  149. * without pointers being followed, then the context handle must
  150. * be direct, otherwise it is a pointer */
  151. const char *ch_ptr = is_aliaschain_attr(var->declspec.type, ATTR_CONTEXTHANDLE) ? "*" : "";
  152. print_server("(");
  153. write_type_decl_left(server, &var->declspec);
  154. fprintf(server, ")%sNDRSContextValue(__frame->%s)", ch_ptr, var->name);
  155. }
  156. else
  157. {
  158. print_server("%s__frame->%s", is_array(var->declspec.type) && !type_array_is_decl_as_ptr(var->declspec.type) ? "*" : "", var->name);
  159. }
  160. }
  161. fprintf(server, ");\n");
  162. indent--;
  163. }
  164. else
  165. {
  166. fprintf(server, "();\n");
  167. }
  168. if (has_out_arg_or_return(func))
  169. {
  170. write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
  171. if (!is_void(ret_type))
  172. write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
  173. print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n");
  174. fprintf(server, "\n");
  175. print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n");
  176. print_server("if (_Status)\n");
  177. indent++;
  178. print_server("RpcRaiseException(_Status);\n");
  179. indent--;
  180. fprintf(server, "\n");
  181. print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n");
  182. fprintf(server, "\n");
  183. }
  184. /* marshall arguments */
  185. write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
  186. /* marshall the return value */
  187. if (!is_void(ret_type))
  188. write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
  189. indent--;
  190. print_server("}\n");
  191. print_server("RpcFinally\n");
  192. print_server("{\n");
  193. indent++;
  194. print_server("__finally_%s_%s( __frame );\n", iface->name, get_name(func));
  195. indent--;
  196. print_server("}\n");
  197. print_server("RpcEndFinally\n");
  198. /* calculate buffer length */
  199. fprintf(server, "\n");
  200. print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
  201. indent--;
  202. fprintf(server, "}\n");
  203. fprintf(server, "\n");
  204. }
  205. static void write_function_stubs(type_t *iface, unsigned int *proc_offset)
  206. {
  207. const statement_t *stmt;
  208. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  209. {
  210. var_t *func = stmt->u.var;
  211. write_function_stub( iface, func, *proc_offset );
  212. /* update proc_offset */
  213. func->procstring_offset = *proc_offset;
  214. *proc_offset += get_size_procformatstring_func( iface, func );
  215. }
  216. }
  217. static void write_dispatchtable(type_t *iface)
  218. {
  219. unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
  220. unsigned int method_count = 0;
  221. const statement_t *stmt;
  222. print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name);
  223. print_server("{\n");
  224. indent++;
  225. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  226. {
  227. var_t *func = stmt->u.var;
  228. if (is_interpreted_func( iface, func ))
  229. print_server("%s,\n", get_stub_mode() == MODE_Oif ? "NdrServerCall2" : "NdrServerCall");
  230. else
  231. print_server("%s_%s,\n", iface->name, get_name(func));
  232. method_count++;
  233. }
  234. print_server("0\n");
  235. indent--;
  236. print_server("};\n");
  237. print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  238. print_server("{\n");
  239. indent++;
  240. print_server("%u,\n", method_count);
  241. print_server("%s_table\n", iface->name);
  242. indent--;
  243. print_server("};\n");
  244. fprintf(server, "\n");
  245. }
  246. static void write_routinetable(type_t *iface)
  247. {
  248. const statement_t *stmt;
  249. print_server( "static const SERVER_ROUTINE %s_ServerRoutineTable[] =\n", iface->name );
  250. print_server( "{\n" );
  251. indent++;
  252. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  253. {
  254. var_t *func = stmt->u.var;
  255. if (is_local( func->attrs )) continue;
  256. print_server( "(void *)%s%s,\n", prefix_server, get_name(func));
  257. }
  258. indent--;
  259. print_server( "};\n\n" );
  260. }
  261. static void write_rundown_routines(void)
  262. {
  263. context_handle_t *ch;
  264. int count = list_count( &context_handle_list );
  265. if (!count) return;
  266. print_server( "static const NDR_RUNDOWN RundownRoutines[] =\n" );
  267. print_server( "{\n" );
  268. indent++;
  269. LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
  270. {
  271. print_server( "%s_rundown", ch->name );
  272. if (--count) fputc( ',', server );
  273. fputc( '\n', server );
  274. }
  275. indent--;
  276. print_server( "};\n\n" );
  277. }
  278. static void write_serverinfo(type_t *iface)
  279. {
  280. print_server( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
  281. print_server( "{\n" );
  282. indent++;
  283. print_server( "&%s_StubDesc,\n", iface->name );
  284. print_server( "%s_ServerRoutineTable,\n", iface->name );
  285. print_server( "__MIDL_ProcFormatString.Format,\n" );
  286. print_server( "%s_FormatStringOffsetTable,\n", iface->name );
  287. print_server( "0,\n" );
  288. print_server( "0,\n" );
  289. print_server( "0,\n" );
  290. print_server( "0\n" );
  291. indent--;
  292. print_server( "};\n\n" );
  293. }
  294. static void write_stubdescdecl(type_t *iface)
  295. {
  296. print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name);
  297. fprintf(server, "\n");
  298. }
  299. static void write_stubdescriptor(type_t *iface, int expr_eval_routines)
  300. {
  301. print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name);
  302. print_server("{\n");
  303. indent++;
  304. print_server("(void *)& %s___RpcServerInterface,\n", iface->name);
  305. print_server("MIDL_user_allocate,\n");
  306. print_server("MIDL_user_free,\n");
  307. print_server("{\n");
  308. indent++;
  309. print_server("0,\n");
  310. indent--;
  311. print_server("},\n");
  312. if (!list_empty( &context_handle_list ))
  313. print_server("RundownRoutines,\n");
  314. else
  315. print_server("0,\n");
  316. print_server("0,\n");
  317. if (expr_eval_routines)
  318. print_server("ExprEvalRoutines,\n");
  319. else
  320. print_server("0,\n");
  321. print_server("0,\n");
  322. print_server("__MIDL_TypeFormatString.Format,\n");
  323. print_server("1, /* -error bounds_check flag */\n");
  324. print_server("0x%x, /* Ndr library version */\n", get_stub_mode() == MODE_Oif ? 0x50002 : 0x10001);
  325. print_server("0,\n");
  326. print_server("0x50200ca, /* MIDL Version 5.2.202 */\n");
  327. print_server("0,\n");
  328. print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
  329. print_server("0, /* notify & notify_flag routine table */\n");
  330. print_server("1, /* Flags */\n");
  331. print_server("0, /* Reserved3 */\n");
  332. print_server("0, /* Reserved4 */\n");
  333. print_server("0 /* Reserved5 */\n");
  334. indent--;
  335. print_server("};\n");
  336. fprintf(server, "\n");
  337. }
  338. static void write_serverinterfacedecl(type_t *iface)
  339. {
  340. unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
  341. UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
  342. const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT);
  343. if (endpoints) write_endpoints( server, iface->name, endpoints );
  344. print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  345. print_server( "static const MIDL_SERVER_INFO %s_ServerInfo;\n", iface->name );
  346. fprintf(server, "\n");
  347. print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name );
  348. print_server("{\n");
  349. indent++;
  350. print_server("sizeof(RPC_SERVER_INTERFACE),\n");
  351. print_server("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n",
  352. uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
  353. uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
  354. uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver));
  355. print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */
  356. print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  357. if (endpoints)
  358. {
  359. print_server("%u,\n", list_count(endpoints));
  360. print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name);
  361. }
  362. else
  363. {
  364. print_server("0,\n");
  365. print_server("0,\n");
  366. }
  367. print_server("0,\n");
  368. print_server("&%s_ServerInfo,\n", iface->name);
  369. print_server("0,\n");
  370. indent--;
  371. print_server("};\n");
  372. if (old_names)
  373. print_server("RPC_IF_HANDLE %s_ServerIfHandle DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
  374. iface->name, iface->name);
  375. else
  376. print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec DECLSPEC_HIDDEN = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n",
  377. prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name);
  378. fprintf(server, "\n");
  379. }
  380. static void init_server(void)
  381. {
  382. if (server)
  383. return;
  384. if (!(server = fopen(server_name, "w")))
  385. error("Could not open %s for output\n", server_name);
  386. print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
  387. print_server("#include <string.h>\n");
  388. fprintf(server, "\n");
  389. print_server("#include \"%s\"\n", header_name);
  390. print_server("\n");
  391. print_server( "#ifndef DECLSPEC_HIDDEN\n");
  392. print_server( "#define DECLSPEC_HIDDEN\n");
  393. print_server( "#endif\n");
  394. print_server( "\n");
  395. }
  396. static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset)
  397. {
  398. const statement_t *stmt;
  399. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  400. {
  401. if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
  402. {
  403. type_t *iface = stmt->u.type;
  404. if (!need_stub(iface))
  405. continue;
  406. fprintf(server, "/*****************************************************************************\n");
  407. fprintf(server, " * %s interface\n", iface->name);
  408. fprintf(server, " */\n");
  409. fprintf(server, "\n");
  410. if (statements_has_func(type_iface_get_stmts(iface)))
  411. {
  412. write_serverinterfacedecl(iface);
  413. write_stubdescdecl(iface);
  414. write_function_stubs(iface, proc_offset);
  415. print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
  416. print_server("#error Invalid build platform for this stub.\n");
  417. print_server("#endif\n");
  418. fprintf(server, "\n");
  419. write_procformatstring_offsets( server, iface );
  420. write_stubdescriptor(iface, expr_eval_routines);
  421. write_dispatchtable(iface);
  422. write_routinetable(iface);
  423. write_serverinfo(iface);
  424. }
  425. }
  426. }
  427. }
  428. static void write_server_routines(const statement_list_t *stmts)
  429. {
  430. unsigned int proc_offset = 0;
  431. int expr_eval_routines;
  432. if (need_inline_stubs_file( stmts ))
  433. {
  434. write_exceptions( server );
  435. print_server("\n");
  436. print_server("struct __server_frame\n");
  437. print_server("{\n");
  438. print_server(" __DECL_EXCEPTION_FRAME\n");
  439. print_server(" MIDL_STUB_MESSAGE _StubMsg;\n");
  440. print_server("};\n");
  441. print_server("\n");
  442. print_server("static int __server_filter( struct __server_frame *__frame )\n");
  443. print_server( "{\n");
  444. print_server( " return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n");
  445. print_server( " (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n");
  446. print_server( " (__frame->code == RPC_X_BAD_STUB_DATA) ||\n");
  447. print_server( " (__frame->code == RPC_S_INVALID_BOUND);\n");
  448. print_server( "}\n");
  449. print_server( "\n");
  450. }
  451. write_formatstringsdecl(server, indent, stmts, need_stub);
  452. expr_eval_routines = write_expr_eval_routines(server, server_token);
  453. if (expr_eval_routines)
  454. write_expr_eval_routine_list(server, server_token);
  455. write_user_quad_list(server);
  456. write_rundown_routines();
  457. write_server_stmts(stmts, expr_eval_routines, &proc_offset);
  458. write_procformatstring(server, stmts, need_stub);
  459. write_typeformatstring(server, stmts, need_stub);
  460. }
  461. void write_server(const statement_list_t *stmts)
  462. {
  463. if (!do_server)
  464. return;
  465. if (do_everything && !need_stub_files(stmts))
  466. return;
  467. init_server();
  468. if (!server)
  469. return;
  470. write_server_routines( stmts );
  471. fclose(server);
  472. }