summaryrefslogtreecommitdiff
path: root/ext/satellite/zval_to_namedvalue.c
blob: 1d0a3dbff36d37c7879e427410f633a5d8e65a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: David Eriksson <david@2good.com>                            |
   +----------------------------------------------------------------------+
 */

/*
 * $Id$
 * vim: syntax=c tabstop=2 shiftwidth=2
 */

/* -----------------------------------------------------------------------
 * 
 * Functions to convert a zval to a CORBA_NamedValue
 *
 * -----------------------------------------------------------------------
 */
#include <zend_API.h>
#include "zval_to_namedvalue.h"
#include "object.h"
#include "struct.h"
#include "common.h"
#include "corba.h"

#include <ORBitutil/util.h>		/* for ALIGN_ADDRESS() */

/*
 * value should be a CORBA_boolean *
 */
static zend_bool satellite_zval_to_namedvalue_boolean(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	CORBA_boolean * p_value = (CORBA_boolean *)pDestination->argument._value;

	pDestination->len = sizeof(CORBA_boolean);

	if (pSource == NULL)
		return TRUE;
		
	if (pSource->type != IS_BOOL && pSource->type != IS_LONG)
		return FALSE;

	*p_value = pSource->value.lval != 0;

	return TRUE;
}


/*
 * value should be a double *
 */
static zend_bool satellite_zval_to_namedvalue_double(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	CORBA_double * p_value = (CORBA_double *)pDestination->argument._value;

	pDestination->len = sizeof(CORBA_double);

	if (pSource == NULL)
		return TRUE;
		
	/*convert_to_double(pSource);*/
	if (pSource->type != IS_DOUBLE)
		return FALSE;

	*p_value = pSource->value.dval;

	return TRUE;
}

/*
 * value should be a long *
 */
static zend_bool satellite_zval_to_namedvalue_long(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	CORBA_long * p_value = (CORBA_long *)pDestination->argument._value;
	pDestination->len = sizeof(CORBA_long);

	if (pSource == NULL)
		return TRUE;
	
	/*convert_to_long(pSource);*/
	if (pSource->type != IS_LONG)
		return FALSE;

	*p_value = pSource->value.lval;

	return TRUE;
}

/*
 * value should be a short *
 */
static zend_bool satellite_zval_to_namedvalue_short(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	CORBA_short * p_value = (CORBA_short *)pDestination->argument._value;
	pDestination->len = sizeof(CORBA_short);

	if (pSource == NULL)
		return TRUE;
	
/*	convert_to_long((zval *)pSource);*/	/* discard const */;
	if (pSource->type != IS_LONG)
	{
/*		printf("source value type is %i in satellite_zval_to_namedvalue_short\n", pSource->type);*/
		return FALSE;
	}

	*p_value = pSource->value.lval;

	return TRUE;
}

/*
 * convert an OrbitObject to a CORBA object
 *
 * value should just be a CORBA_Object
 */
static zend_bool satellite_zval_to_namedvalue_objref(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	OrbitObject * pObject = NULL;
	CORBA_Object * p_value = (CORBA_Object*)&pDestination->argument._value;
		
	pDestination->len = 1;

	if (pSource == NULL)
		return TRUE; /* nothing else to do */

	if (pSource->type != IS_OBJECT)
		goto error;

	/* see that it's a corba object */
	if (strcmp(pSource->value.obj.ce->name, "OrbitObject") != 0)
		goto error; /* bad class type */

	pObject = OrbitObject_RetrieveData(pSource);
	if (pObject == NULL)
		goto error; /* unexpected error */
		
	*p_value = OrbitObject_GetCorbaObject(pObject);
	
	/* debug */
/*	(CORBA_Object)pDestination->argument._value = OrbitObject_GetCorbaObject(pObject);*/

	if (*p_value == NULL)
	{
/*		printf("NULL object in satellite_zval_to_namedvalue_objref\n");*/
		goto error;
	}

/*	printf("satellite_zval_to_namedvalue_objref: %s\n", (*p_value)->type_id);*/
		
	return TRUE;

error:
	return FALSE;
}

/*
 * convert a PHP array to a CORBA sequence
 *
 * value should be a CORBA_sequence_octet *
 */
static zend_bool satellite_zval_to_namedvalue_sequence(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	CORBA_sequence_octet * p_sequence_octet = 
		(CORBA_sequence_octet *)pDestination->argument._value;
	HashTable * p_hashtable = NULL;
	int member_count = 0;

	if (pSource == NULL)
		return TRUE;	/* nothing to do */

	if (pSource->type != IS_ARRAY)
		goto error;	/* bad source type */

	p_hashtable = pSource->value.ht;
	member_count = zend_hash_num_elements(p_hashtable);

	/* prepare sequence octet */
	ORBit_sequence_octet_init(p_sequence_octet, member_count);

	/*
	 * XXX: potential bug nest follows
	 */
	if (member_count > 0)
	{
		CORBA_NamedValue destination_value;
		zval * p_source_value;
		HashPosition position = NULL;
		int i;
		void ** p_members = (void **)p_sequence_octet->_buffer;

		/* prepare to start walking the hash table */
		zend_hash_internal_pointer_reset_ex(p_hashtable, &position);

		for(i = 0; i < member_count; i++)
		{
			if (position == NULL)
				goto error;

#if 0
			/* the function call gets messed up */
			if (zend_hash_get_current_data_ex(
						p_hashtable, /* XXX */, &position) == FAILURE)
				goto error;
#else
			p_source_value = *(zval**)position->pData;
#endif
			
			memset(&destination_value, 0, sizeof(destination_value));
			
			destination_value.argument._type = CORBA_TypeCode_content_type(
					pDestination->argument._type, orbit_get_environment());

			if (orbit_caught_exception())
				goto error;

			/* now get a NamedValue for this element */
			if (!orbit_zval_to_namedvalue(p_source_value, &destination_value))
				goto error;
			
			/* put values in our value */
			p_members[i] = *(void**)destination_value.argument._value;
			
			/* get next in hashtable */
			if (zend_hash_move_forward_ex(p_hashtable, &position) == FAILURE)
				goto error;
		}
	}
	
/*	pDestination->argument._value = p_sequence_octet;*/
	return TRUE;
	
error:
	ORBit_sequence_octet_fini(p_sequence_octet); /* appropriate destruction? */
	return FALSE; 
}

/*
 * convert a PHP string to a CORBA string
 *
 * value should be a char **
 */
static zend_bool satellite_zval_to_namedvalue_string(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{
	char ** p_value = (char **)pDestination->argument._value;
	
	if (pSource == NULL)
		return TRUE; 	/* do nothing */

#if 0
	convert_to_string((zval *)pSource);	/* discard const */
	if (pSource->type == IS_NULL)
		return TRUE; 	/* do nothing */
#endif
	
	if (pSource->type != IS_STRING)
		goto error;

	*p_value 						= CORBA_string_dup(pSource->value.str.val);
	pDestination->len		= strlen(*p_value);

	return TRUE;

error:
	return FALSE;
}

/*
 * convert an OrbitStruct object to a CORBA structure
 *
 * XXX: this is trouble!
 *
 */
static zend_bool satellite_zval_to_namedvalue_struct(const zval * pSource, 
		CORBA_NamedValue * pDestination) 
{ 
	OrbitStruct * p_struct = NULL;
	int member_count = 0;
	CORBA_TypeCode type = NULL;
	
	pDestination->len = 1;
	
	/* nothing else to do if we have no source object */
	if (pSource == NULL)
		return TRUE;

	/* see that it's an object */
	if (pSource->type != IS_OBJECT)
		goto error; /* bad source type */

	/* see that it's a structure object */
	if (strcmp(pSource->value.obj.ce->name, "OrbitStruct") != 0)
		goto error; /* bad class type */
		
	/* get struct info */
	p_struct = OrbitStruct_RetrieveData(pSource);
	if (p_struct == NULL)
		goto error; /* unexpected error */

	/* make type a shortcut to the typecode */
	type = pDestination->argument._type;

	/* check respository ids */
	{
		char * p_source_repository_id =	OrbitStruct_GetRepositoryId(p_struct);
		char * p_destination_repository_id = CORBA_TypeCode_id(type, orbit_get_environment());

		if (orbit_caught_exception())
			goto error;

		if (strcmp(p_destination_repository_id, p_source_repository_id) != 0)
			goto error;	/* bad struct */
	}

	/* now we know the source and destination matches! */
	member_count = CORBA_TypeCode_member_count(type, orbit_get_environment());
	if (orbit_caught_exception())
		goto error;

	if (member_count > 0)
	{
		int i;
		CORBA_NamedValue destination_value;
		zval source_value;
		zend_bool success;
		char * p_buffer = (char *)pDestination->argument._value;
		int element_size = 0;

		for (i = 0; i < member_count; i++)
		{
			int alignment;
			/* get member name */
			char * p_name = 
				CORBA_TypeCode_member_name(type, i, orbit_get_environment());
			if (orbit_caught_exception())
				goto error;

			/* get source value */
			success = OrbitStruct_GetProperty(p_struct, p_name, &source_value);
			if (!success)
			{
				goto error;
			}

			/* prepare destination value */
			memset(&destination_value, 0, sizeof(CORBA_NamedValue));
			destination_value.argument._type = 
					CORBA_TypeCode_member_type(type, i, orbit_get_environment());
			if (destination_value.argument._type == NULL)
				goto error;

			/* convert member */
			success = orbit_zval_to_namedvalue(&source_value, &destination_value);
			if (!success)
				goto error;

			element_size = ORBit_gather_alloc_info(destination_value.argument._type);
			
			/* align pointer and set value */
			alignment = ORBit_find_alignment(destination_value.argument._type);
			p_buffer = ALIGN_ADDRESS(p_buffer, alignment);

			/* copy value to struct */
			memcpy(p_buffer, destination_value.argument._value, element_size);
			p_buffer += element_size;
		}
	}
	
	return TRUE;

error:
	return FALSE;
}

/*
 * public function to convert form a zval to CORBA_NamedValue
 *
 * The data type specific functions does not own 
 * pDestination->argument._value!!!
 */
zend_bool orbit_zval_to_namedvalue(const zval * pSource, 
		CORBA_NamedValue * pDestination)
{
	zend_bool success = FALSE;
	CORBA_TCKind kind; 
	
	if (pDestination->argument._type == NULL)
	{
/*		printf("pDestination->argument._type is NULL in orbit_zval_to_namedvalue\n");*/
		return FALSE;
	}
	
	kind = CORBA_TypeCode_kind(
			pDestination->argument._type, 
			orbit_get_environment());

	if (orbit_caught_exception())
		return FALSE;

	/* allocate memory for value */
	if (pSource != NULL)
	{
		/* use special function to allocate value -- good or bad idea? */
		pDestination->argument._value = 
			ORBit_alloc_tcval(pDestination->argument._type, 1);
	}
	
	switch (kind)
	{
		case CORBA_tk_boolean:
			success = satellite_zval_to_namedvalue_boolean(pSource, pDestination);
			break;

		case CORBA_tk_double:
			success = satellite_zval_to_namedvalue_double(pSource, pDestination);
			break;

		case CORBA_tk_enum:
		case CORBA_tk_long:
			success = satellite_zval_to_namedvalue_long(pSource, pDestination);
			break;

		case CORBA_tk_objref:
			success = satellite_zval_to_namedvalue_objref(pSource, pDestination);
			break;

		case CORBA_tk_sequence:
			success = satellite_zval_to_namedvalue_sequence(pSource, pDestination);
			break;

		case CORBA_tk_short:
			success = satellite_zval_to_namedvalue_short(pSource, pDestination);
			break;

		case CORBA_tk_string:	/* 18 */
			success = satellite_zval_to_namedvalue_string(pSource, pDestination);
			break;

		case CORBA_tk_struct:		/* 15 */
			success = satellite_zval_to_namedvalue_struct(pSource, pDestination);
			break;

		default:
/*			printf("unsupported corba TCKind %i\n", kind);*/
/*			php_error(E_WARNING, "unsupported corba TCKind %i", kind);*/
	}

	if (!success)
	{
/*			printf("orbit_zval_to_namedvalue failed for TCKind %i\n", kind);*/
		/* 
		 * does this always crash? then we better look for another way of
		 * freeing the data!
		 */
/*		CORBA_free(pDestination->argument._value); */
		pDestination->argument._value = NULL;
	}

	return success;
}