Winspool expects NULLs to be preserved in ascii to unicode
[wine] / dlls / msvcrt / cpp.c
1 /*
2  * msvcrt.dll C++ objects
3  *
4  * Copyright 2000 Jon Griffiths
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 "msvcrt.h"
22 #include "msvcrt/eh.h"
23 #include "msvcrt/stdlib.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28
29
30 typedef void (*v_table_ptr)();
31
32 static v_table_ptr exception_vtable[2];
33 static v_table_ptr bad_typeid_vtable[3];
34 static v_table_ptr __non_rtti_object_vtable[3];
35 static v_table_ptr bad_cast_vtable[3];
36 static v_table_ptr type_info_vtable[1];
37
38 typedef struct __exception
39 {
40   v_table_ptr *vtable;
41   const char *name;
42   int do_free; /* FIXME: take string copy with char* ctor? */
43 } exception;
44
45 typedef struct __bad_typeid
46 {
47   exception base;
48 } bad_typeid;
49
50 typedef struct ____non_rtti_object
51 {
52   bad_typeid base;
53 } __non_rtti_object;
54
55 typedef struct __bad_cast
56 {
57   exception base;
58 } bad_cast;
59
60 typedef struct __type_info
61 {
62   v_table_ptr *vtable;
63   void *data;
64   char name[1];
65 } type_info;
66
67 typedef struct _rtti_base_descriptor
68 {
69   type_info *type_descriptor;
70   int num_base_classes;
71   int base_class_offset;
72   unsigned int flags;
73 } rtti_base_descriptor;
74
75 typedef struct _rtti_base_array
76 {
77   rtti_base_descriptor *bases[1]; /* First element is the class itself */
78 } rtti_base_array;
79
80 typedef struct _rtti_object_hierachy
81 {
82   int unknown1;
83   int unknown2;
84   int array_len; /* Size of the array pointed to by 'base_classes' */
85   rtti_base_array *base_classes;
86 } rtti_object_hierachy;
87
88 typedef struct _rtti_object_locator
89 {
90   int unknown1;
91   int base_class_offset;
92   unsigned int flags;
93   type_info *type_descriptor;
94   rtti_object_hierachy *type_hierachy;
95 } rtti_object_locator;
96
97 /******************************************************************
98  *              ??0exception@@QAE@ABQBD@Z (MSVCRT.@)
99  */
100 void MSVCRT_exception_ctor(exception * _this, const char ** name)
101 {
102   TRACE("(%p %s)\n",_this,*name);
103   _this->vtable = exception_vtable;
104   _this->name = *name;
105   TRACE("name = %s\n",_this->name);
106   _this->do_free = 0; /* FIXME */
107 }
108
109 /******************************************************************
110  *              ??0exception@@QAE@ABV0@@Z (MSVCRT.@)
111  */
112 void MSVCRT_exception_copy_ctor(exception * _this, const exception * rhs)
113 {
114   TRACE("(%p %p)\n",_this,rhs);
115   if (_this != rhs)
116     memcpy (_this, rhs, sizeof (*_this));
117   TRACE("name = %s\n",_this->name);
118 }
119
120 /******************************************************************
121  *              ??0exception@@QAE@XZ (MSVCRT.@)
122  */
123 void MSVCRT_exception_default_ctor(exception * _this)
124 {
125   TRACE("(%p)\n",_this);
126   _this->vtable = exception_vtable;
127   _this->name = "";
128   _this->do_free = 0; /* FIXME */
129 }
130
131 /******************************************************************
132  *              ??1exception@@UAE@XZ (MSVCRT.@)
133  */
134 void MSVCRT_exception_dtor(exception * _this)
135 {
136   TRACE("(%p)\n",_this);
137 }
138
139 /******************************************************************
140  *              ??4exception@@QAEAAV0@ABV0@@Z (MSVCRT.@)
141  */
142 exception * MSVCRT_exception_opequals(exception * _this, const exception * rhs)
143 {
144   TRACE("(%p %p)\n",_this,rhs);
145   memcpy (_this, rhs, sizeof (*_this));
146   TRACE("name = %s\n",_this->name);
147   return _this;
148 }
149
150 /******************************************************************
151  *              ??_Eexception@@UAEPAXI@Z (MSVCRT.@)
152  */
153 void * MSVCRT_exception__unknown_E(exception * _this, unsigned int arg1)
154 {
155   TRACE("(%p %d)\n",_this,arg1);
156   _purecall();
157   return NULL;
158 }
159
160 /******************************************************************
161  *              ??_Gexception@@UAEPAXI@Z (MSVCRT.@)
162  */
163 void * MSVCRT_exception__unknown_G(exception * _this, unsigned int arg1)
164 {
165   TRACE("(%p %d)\n",_this,arg1);
166   _purecall();
167   return NULL;
168 }
169
170 /******************************************************************
171  *              ?what@exception@@UBEPBDXZ (MSVCRT.@)
172  */
173 const char * MSVCRT_what_exception(exception * _this)
174 {
175   TRACE("(%p) returning %s\n",_this,_this->name);
176   return _this->name;
177 }
178
179
180 /******************************************************************
181  *              ?set_terminate@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
182  */
183 terminate_function MSVCRT_set_terminate(terminate_function func)
184 {
185     MSVCRT_thread_data *data = msvcrt_get_thread_data();
186     terminate_function previous = data->terminate_handler;
187     TRACE("(%p) returning %p\n",func,previous);
188     data->terminate_handler = func;
189     return previous;
190 }
191
192 /******************************************************************
193  *              ?set_unexpected@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
194  */
195 unexpected_function MSVCRT_set_unexpected(unexpected_function func)
196 {
197     MSVCRT_thread_data *data = msvcrt_get_thread_data();
198     unexpected_function previous = data->unexpected_handler;
199     TRACE("(%p) returning %p\n",func,previous);
200     data->unexpected_handler = func;
201     return previous;
202 }
203
204 /******************************************************************
205  *              ?_set_se_translator@@YAP6AXIPAU_EXCEPTION_POINTERS@@@ZP6AXI0@Z@Z  (MSVCRT.@)
206  */
207 _se_translator_function MSVCRT__set_se_translator(_se_translator_function func)
208 {
209     MSVCRT_thread_data *data = msvcrt_get_thread_data();
210     _se_translator_function previous = data->se_translator;
211     TRACE("(%p) returning %p\n",func,previous);
212     data->se_translator = func;
213     return previous;
214 }
215
216 /******************************************************************
217  *              ?terminate@@YAXXZ (MSVCRT.@)
218  */
219 void MSVCRT_terminate(void)
220 {
221     MSVCRT_thread_data *data = msvcrt_get_thread_data();
222     if (data->terminate_handler) data->terminate_handler();
223     MSVCRT_abort();
224 }
225
226 /******************************************************************
227  *              ?unexpected@@YAXXZ (MSVCRT.@)
228  */
229 void MSVCRT_unexpected(void)
230 {
231     MSVCRT_thread_data *data = msvcrt_get_thread_data();
232     if (data->unexpected_handler) data->unexpected_handler();
233     MSVCRT_terminate();
234 }
235
236
237 /******************************************************************
238  *              ??0bad_typeid@@QAE@ABV0@@Z (MSVCRT.@)
239  */
240 void MSVCRT_bad_typeid_copy_ctor(bad_typeid * _this, const bad_typeid * rhs)
241 {
242   TRACE("(%p %p)\n",_this,rhs);
243   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
244 }
245
246 /******************************************************************
247  *              ??0bad_typeid@@QAE@PBD@Z (MSVCRT.@)
248  */
249 void MSVCRT_bad_typeid_ctor(bad_typeid * _this, const char * name)
250 {
251   TRACE("(%p %s)\n",_this,name);
252   MSVCRT_exception_ctor(&_this->base, &name);
253   _this->base.vtable = bad_typeid_vtable;
254 }
255
256 /******************************************************************
257  *              ??1bad_typeid@@UAE@XZ (MSVCRT.@)
258  */
259 void MSVCRT_bad_typeid_dtor(bad_typeid * _this)
260 {
261   TRACE("(%p)\n",_this);
262   MSVCRT_exception_dtor(&_this->base);
263 }
264
265 /******************************************************************
266  *              ??4bad_typeid@@QAEAAV0@ABV0@@Z (MSVCRT.@)
267  */
268 bad_typeid * MSVCRT_bad_typeid_opequals(bad_typeid * _this, const bad_typeid * rhs)
269 {
270   TRACE("(%p %p)\n",_this,rhs);
271   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
272   return _this;
273 }
274
275 /******************************************************************
276  *              ??0__non_rtti_object@@QAE@ABV0@@Z (MSVCRT.@)
277  */
278 void MSVCRT___non_rtti_object_copy_ctor(__non_rtti_object * _this,
279                                                 const __non_rtti_object * rhs)
280 {
281   TRACE("(%p %p)\n",_this,rhs);
282   MSVCRT_bad_typeid_copy_ctor(&_this->base,&rhs->base);
283 }
284
285 /******************************************************************
286  *              ??0__non_rtti_object@@QAE@PBD@Z (MSVCRT.@)
287  */
288 void MSVCRT___non_rtti_object_ctor(__non_rtti_object * _this,
289                                            const char * name)
290 {
291   TRACE("(%p %s)\n",_this,name);
292   MSVCRT_bad_typeid_ctor(&_this->base,name);
293   _this->base.base.vtable = __non_rtti_object_vtable;
294 }
295
296 /******************************************************************
297  *              ??1__non_rtti_object@@UAE@XZ (MSVCRT.@)
298  */
299 void MSVCRT___non_rtti_object_dtor(__non_rtti_object * _this)
300 {
301   TRACE("(%p)\n",_this);
302   MSVCRT_bad_typeid_dtor(&_this->base);
303 }
304
305 /******************************************************************
306  *              ??4__non_rtti_object@@QAEAAV0@ABV0@@Z (MSVCRT.@)
307  */
308 __non_rtti_object * MSVCRT___non_rtti_object_opequals(__non_rtti_object * _this,
309                                                               const __non_rtti_object *rhs)
310 {
311   TRACE("(%p %p)\n",_this,rhs);
312   memcpy (_this, rhs, sizeof (*_this));
313   TRACE("name = %s\n",_this->base.base.name);
314   return _this;
315 }
316
317 /******************************************************************
318  *              ??_E__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
319  */
320 void * MSVCRT___non_rtti_object__unknown_E(__non_rtti_object * _this, unsigned int arg1)
321 {
322   TRACE("(%p %d)\n",_this,arg1);
323   _purecall();
324   return NULL;
325 }
326
327 /******************************************************************
328  *              ??_G__non_rtti_object@@UAEPAXI@Z (MSVCRT.@)
329  */
330 void * MSVCRT___non_rtti_object__unknown_G(__non_rtti_object * _this, unsigned int arg1)
331 {
332   TRACE("(%p %d)\n",_this,arg1);
333   _purecall();
334   return NULL;
335 }
336
337 /******************************************************************
338  *              ??0bad_cast@@QAE@ABQBD@Z (MSVCRT.@)
339  */
340 void MSVCRT_bad_cast_ctor(bad_cast * _this, const char ** name)
341 {
342   TRACE("(%p %s)\n",_this,*name);
343   MSVCRT_exception_ctor(&_this->base, name);
344   _this->base.vtable = bad_cast_vtable;
345 }
346
347 /******************************************************************
348  *              ??0bad_cast@@QAE@ABV0@@Z (MSVCRT.@)
349  */
350 void MSVCRT_bad_cast_copy_ctor(bad_cast * _this, const bad_cast * rhs)
351 {
352   TRACE("(%p %p)\n",_this,rhs);
353   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
354 }
355
356 /******************************************************************
357  *              ??1bad_cast@@UAE@XZ (MSVCRT.@)
358  */
359 void MSVCRT_bad_cast_dtor(bad_cast * _this)
360 {
361   TRACE("(%p)\n",_this);
362   MSVCRT_exception_dtor(&_this->base);
363 }
364
365 /******************************************************************
366  *              ??4bad_cast@@QAEAAV0@ABV0@@Z (MSVCRT.@)
367  */
368 bad_cast * MSVCRT_bad_cast_opequals(bad_cast * _this, const bad_cast * rhs)
369 {
370   TRACE("(%p %p)\n",_this,rhs);
371   MSVCRT_exception_copy_ctor(&_this->base,&rhs->base);
372   return _this;
373 }
374
375 /******************************************************************
376  *              ??8type_info@@QBEHABV0@@Z (MSVCRT.@)
377  */
378 int __stdcall MSVCRT_type_info_opequals_equals(type_info * _this, const type_info * rhs)
379 {
380   TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
381   return _this->name == rhs->name;
382 }
383
384 /******************************************************************
385  *              ??9type_info@@QBEHABV0@@Z (MSVCRT.@)
386  */
387 int __stdcall MSVCRT_type_info_opnot_equals(type_info * _this, const type_info * rhs)
388 {
389   TRACE("(%p %p) returning %d\n",_this,rhs,_this->name == rhs->name);
390   return _this->name != rhs->name;
391 }
392
393 /******************************************************************
394  *              ??1type_info@@UAE@XZ (MSVCRT.@)
395  */
396 void MSVCRT_type_info_dtor(type_info * _this)
397 {
398   TRACE("(%p)\n",_this);
399   if (_this->data)
400     MSVCRT_free(_this->data);
401 }
402
403 /******************************************************************
404  *              ?name@type_info@@QBEPBDXZ (MSVCRT.@)
405  */
406 const char * __stdcall MSVCRT_type_info_name(type_info * _this)
407 {
408   TRACE("(%p) returning %s\n",_this,_this->name);
409   return _this->name;
410 }
411
412 /******************************************************************
413  *              ?raw_name@type_info@@QBEPBDXZ (MSVCRT.@)
414  */
415 const char * __stdcall MSVCRT_type_info_raw_name(type_info * _this)
416 {
417   TRACE("(%p) returning %s\n",_this,_this->name);
418   return _this->name;
419 }
420
421
422 /******************************************************************
423  *              __RTtypeid (MSVCRT.@)
424  */
425 type_info* MSVCRT___RTtypeid(type_info *cppobj)
426 {
427   /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
428   TRACE("(%p)\n",cppobj);
429
430   if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
431       !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
432       !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
433   {
434     rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
435     return obj_locator->type_descriptor;
436   }
437   /* FIXME: throw a C++ exception */
438   FIXME("Should throw(bad_typeid). Creating NULL reference, expect crash!\n");
439   return NULL;
440 }
441
442 /******************************************************************
443  *              __RTDynamicCast (MSVCRT.@)
444  */
445 void* MSVCRT___RTDynamicCast(type_info *cppobj, int unknown,
446                              type_info *src, type_info *dst,
447                              int do_throw)
448 {
449   /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
450   TRACE("(%p,%d,%p,%p,%d)\n",cppobj, unknown, src, dst, do_throw);
451
452   if (unknown)
453     FIXME("Unknown prameter is non-zero: please report\n");
454
455   /* To cast an object at runtime:
456    * 1.Find out the true type of the object from the typeinfo at vtable[-1]
457    * 2.Search for the destination type in the class heirachy
458    * 3.If destination type is found, return base object address + dest offset
459    *   Otherwise, fail the cast
460    */
461   if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
462       !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
463       !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
464   {
465     int count = 0;
466     rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
467     rtti_object_hierachy *obj_bases = obj_locator->type_hierachy;
468     rtti_base_descriptor **base_desc = obj_bases->base_classes->bases;
469     int src_offset = obj_locator->base_class_offset, dst_offset = -1;
470
471     while (count < obj_bases->array_len)
472     {
473       type_info *typ = (*base_desc)->type_descriptor;
474
475       if (!strcmp(typ->name, dst->name))
476       {
477         dst_offset = (*base_desc)->base_class_offset;
478         break;
479       }
480       base_desc++;
481       count++;
482     }
483     if (dst_offset >= 0)
484       return (void*)((unsigned long)cppobj - src_offset + dst_offset);
485   }
486
487   /* VC++ sets do_throw to 1 when the result of a dynamic_cast is assigned
488    * to a reference, since references cannot be NULL.
489    */
490   if (do_throw)
491     FIXME("Should throw(bad_cast). Creating NULL reference, expect crash!\n");
492   return NULL;
493 }
494
495
496 /******************************************************************
497  *              __RTCastToVoid (MSVCRT.@)
498  */
499 void* MSVCRT___RTCastToVoid(type_info *cppobj)
500 {
501   /* Note: cppobj _isn't_ a type_info, we use that struct for its vtable ptr */
502   TRACE("(%p)\n",cppobj);
503
504   /* Casts to void* simply cast to the base object */
505   if (!IsBadReadPtr(cppobj, sizeof(void *)) &&
506       !IsBadReadPtr(cppobj->vtable - 1,sizeof(void *)) &&
507       !IsBadReadPtr((void*)cppobj->vtable[-1], sizeof(rtti_object_locator)))
508   {
509     rtti_object_locator *obj_locator = (rtti_object_locator *)cppobj->vtable[-1];
510     int src_offset = obj_locator->base_class_offset;
511
512     return (void*)((unsigned long)cppobj - src_offset);
513   }
514   return NULL;
515 }
516
517
518 /* INTERNAL: Set up vtables
519  * FIXME:should be static, cope with versions?
520  */
521 void msvcrt_init_vtables(void)
522 {
523   exception_vtable[0] = MSVCRT_exception_dtor;
524   exception_vtable[1] = (void*)MSVCRT_what_exception;
525
526   bad_typeid_vtable[0] = MSVCRT_bad_typeid_dtor;
527   bad_typeid_vtable[1] = exception_vtable[1];
528   bad_typeid_vtable[2] = _purecall; /* FIXME */
529
530   __non_rtti_object_vtable[0] = MSVCRT___non_rtti_object_dtor;
531   __non_rtti_object_vtable[1] = bad_typeid_vtable[1];
532   __non_rtti_object_vtable[2] = bad_typeid_vtable[2];
533
534   bad_cast_vtable[0] = MSVCRT_bad_cast_dtor;
535   bad_cast_vtable[1] = exception_vtable[1];
536   bad_cast_vtable[2] = _purecall; /* FIXME */
537
538   type_info_vtable[0] = MSVCRT_type_info_dtor;
539
540 }