summaryrefslogtreecommitdiff
path: root/Zend/zend_API.c
blob: d324d877d8e130c84fbd369cad4a765cf538542d (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski                  |
   +----------------------------------------------------------------------+
   | This source file is subject to version 0.91 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        | 
   | available at through the world-wide-web at                           |
   | http://www.zend.com/license/0_91.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: Andi Gutmans <andi@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   +----------------------------------------------------------------------+
*/


#include "zend.h"
#include "zend_variables.h"
#include "zend_execute.h"
#include "zend_API.h"
#include "modules.h"
#include "zend_constants.h"
#include "zend_operators.h"

#if HAVE_STDARG_H
#include <stdarg.h>
#endif

/* these variables are true statics/globals, and have to be mutex'ed on every access */
static int module_count=0;
HashTable list_destructors, module_registry;

/* this function doesn't check for too many parameters */
ZEND_API int getParameters(int ht, int param_count,...)
{
	void **p;
	int arg_count;
	va_list ptr;
	zval **param, *param_ptr;
	ELS_FETCH();
	ALS_FETCH();

	p = EG(argument_stack).top_element-1;
	arg_count = (ulong) *p;

	if (param_count>arg_count) {
		return FAILURE;
	}

	va_start(ptr, param_count);

	while (param_count>0) {
		param = va_arg(ptr, zval **);
		param_ptr = *(p-param_count);
		if (!PZVAL_IS_REF(param_ptr) && param_ptr->refcount>1) {
			zval *new_tmp;

			new_tmp = (zval *) emalloc(sizeof(zval));
			*new_tmp = *param_ptr;
			zval_copy_ctor(new_tmp);
			INIT_PZVAL(new_tmp);
			param_ptr = new_tmp;
			((zval *) *(p-param_count))->refcount--;
			*(p-param_count) = param_ptr;
		}
		*param = param_ptr;
		param_count--;
	}
	va_end(ptr);

	return SUCCESS;
}


ZEND_API int getParametersArray(int ht, int param_count, zval **argument_array)
{
	void **p;
	int arg_count;
	zval *param_ptr;
	ELS_FETCH();

	p = EG(argument_stack).top_element-1;
	arg_count = (ulong) *p;

	if (param_count>arg_count) {
		return FAILURE;
	}


	while (param_count>0) {
		param_ptr = *(p-param_count);
		if (!PZVAL_IS_REF(param_ptr) && param_ptr->refcount>1) {
			zval *new_tmp;

			new_tmp = (zval *) emalloc(sizeof(zval));
			*new_tmp = *param_ptr;
			zval_copy_ctor(new_tmp);
			INIT_PZVAL(new_tmp);
			param_ptr = new_tmp;
			((zval *) *(p-param_count))->refcount--;
			*(p-param_count) = param_ptr;
		}
		*(argument_array++) = param_ptr;
		param_count--;
	}

	return SUCCESS;
}




/* Zend-optimized Extended functions */
/* this function doesn't check for too many parameters */
ZEND_API int getParametersEx(int param_count,...)
{
	void **p;
	int arg_count;
	va_list ptr;
	zval ***param;
	ELS_FETCH();

	p = EG(argument_stack).top_element-1;
	arg_count = (ulong) *p;

	if (param_count>arg_count) {
		return FAILURE;
	}

	va_start(ptr, param_count);
	while (param_count>0) {
		param = va_arg(ptr, zval ***);
		*param = (zval **) p-(param_count--);
	}
	va_end(ptr);

	return SUCCESS;
}


ZEND_API int getParametersArrayEx(int param_count, zval ***argument_array)
{
	void **p;
	int arg_count;
	ELS_FETCH();

	p = EG(argument_stack).top_element-1;
	arg_count = (ulong) *p;

	if (param_count>arg_count) {
		return FAILURE;
	}

	while (param_count>0) {
		*(argument_array++) = (zval **) p-(param_count--);
	}

	return SUCCESS;
}


ZEND_API int ParameterPassedByReference(int ht, uint n)
{
	void **p;
	ulong arg_count;
	zval *arg;
	ELS_FETCH();

	p = EG(argument_stack).elements+EG(argument_stack).top-1;
	arg_count = (ulong) *p;

	if (n>arg_count) {
		return FAILURE;
	}
	arg = (zval *) *(p-arg_count+n-1);
	return PZVAL_IS_REF(arg);
}


ZEND_API void wrong_param_count()
{
	zend_error(E_WARNING,"Wrong parameter count for %s()",get_active_function_name());
}

	
ZEND_API inline int array_init(zval *arg)
{
	arg->value.ht = (HashTable *) emalloc(sizeof(HashTable));
	if (!arg->value.ht || zend_hash_init(arg->value.ht, 0, NULL, PVAL_PTR_DTOR, 0)) {
		zend_error(E_CORE_ERROR, "Cannot allocate memory for array");
		return FAILURE;
	}
	arg->type = IS_ARRAY;
	return SUCCESS;
}


static void zval_update_const_and_ref(zval **p)
{
	zval_update_constant(*p);
	zval_add_ref(p);
}



ZEND_API inline int object_init_ex(zval *arg, zend_class_entry *class_type)
{
	zval *tmp;

	arg->value.obj.properties = (HashTable *) emalloc(sizeof(HashTable));
	zend_hash_init(arg->value.obj.properties, 0, NULL, PVAL_PTR_DTOR, 0);
	zend_hash_copy(arg->value.obj.properties, &class_type->default_properties, (void (*)(void *)) zval_update_const_and_ref, (void *) &tmp, sizeof(zval *));
	arg->type = IS_OBJECT;
	arg->value.obj.ce = class_type;
	return SUCCESS;
}


ZEND_API inline int object_init(zval *arg)
{
	return object_init_ex(arg, &zend_standard_class_def);
}


ZEND_API inline int add_assoc_long(zval *arg, char *key, long n)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_LONG;
	tmp->value.lval = n;
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_assoc_double(zval *arg, char *key, double d)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_DOUBLE;
	tmp->value.dval = d;
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_assoc_string(zval *arg, char *key, char *str, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_assoc_stringl(zval *arg, char *key, char *str, uint length, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_assoc_function(zval *arg, char *key,void (*function_ptr)(INTERNAL_FUNCTION_PARAMETERS))
{
	zend_error(E_WARNING, "add_assoc_function() is no longer supported");
	return FAILURE;
}


ZEND_API inline int add_index_long(zval *arg, uint index, long n)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_LONG;
	tmp->value.lval = n;
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),NULL);
}


ZEND_API inline int add_index_double(zval *arg, uint index, double d)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_DOUBLE;
	tmp->value.dval = d;
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),NULL);
}


ZEND_API inline int add_index_string(zval *arg, uint index, char *str, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_index_stringl(zval *arg, uint index, char *str, uint length, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),NULL);
}


ZEND_API inline int add_next_index_long(zval *arg, long n)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_LONG;
	tmp->value.lval = n;
	INIT_PZVAL(tmp);
	return zend_hash_next_index_insert(arg->value.ht, &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_next_index_double(zval *arg, double d)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_DOUBLE;
	tmp->value.dval = d;
	INIT_PZVAL(tmp);
	return zend_hash_next_index_insert(arg->value.ht, &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_next_index_string(zval *arg, char *str, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_next_index_insert(arg->value.ht, &tmp, sizeof(zval *),NULL);
}


ZEND_API inline int add_next_index_stringl(zval *arg, char *str, uint length, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_next_index_insert(arg->value.ht, &tmp, sizeof(zval *),NULL);
}


ZEND_API inline int add_get_assoc_string(zval *arg, char *key, char *str, void **dest, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), dest);
}


ZEND_API inline int add_get_assoc_stringl(zval *arg, char *key, char *str, uint length, void **dest, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), dest);
}


ZEND_API inline int add_get_index_long(zval *arg, uint index, long l, void **dest)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_LONG;
	tmp->value.lval = l;
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),dest);
}


ZEND_API inline int add_get_index_double(zval *arg, uint index, double d, void **dest)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_DOUBLE;
	tmp->value.dval= d;
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),dest);
}


ZEND_API inline int add_get_index_string(zval *arg, uint index, char *str, void **dest, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),dest);
}


ZEND_API inline int add_get_index_stringl(zval *arg, uint index, char *str, uint length, void **dest, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_index_update(arg->value.ht, index, (void *) &tmp, sizeof(zval *),dest);
}


ZEND_API inline int add_property_long(zval *arg, char *key, long n)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_LONG;
	tmp->value.lval = n;
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.obj.properties, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_property_double(zval *arg, char *key, double d)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_DOUBLE;
	tmp->value.dval = d;
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.obj.properties, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_property_string(zval *arg, char *key, char *str, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = strlen(str);
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.obj.properties, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


ZEND_API inline int add_property_stringl(zval *arg, char *key, char *str, uint length, int duplicate)
{
	zval *tmp = (zval *) emalloc(sizeof(zval));

	tmp->type = IS_STRING;
	tmp->value.str.len = length;
	if (duplicate) {
		tmp->value.str.val = estrndup(str,tmp->value.str.len);
	} else {
		tmp->value.str.val = str;
	}
	INIT_PZVAL(tmp);
	return zend_hash_update(arg->value.obj.properties, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
}


int zend_startup_module(zend_module_entry *module)
{
	if (module) {
		module->module_number = zend_next_free_module();
		if (module->module_startup_func) {
			if (module->module_startup_func(MODULE_PERSISTENT, module->module_number)==FAILURE) {
				zend_error(E_CORE_ERROR,"Unable to start %s module",module->name);
				return FAILURE;
			}
		}
		module->type = MODULE_PERSISTENT;
		zend_register_module(module);
	}
	return SUCCESS;
}


ZEND_API int _register_list_destructors(void (*list_destructor)(void *), void (*plist_destructor)(void *), int module_number)
{
	list_destructors_entry ld;
	
#if 0
	printf("Registering destructors %d for module %d\n", list_destructors.nNextFreeElement, module_number);
#endif
	
	ld.list_destructor=(void (*)(void *)) list_destructor;
	ld.plist_destructor=(void (*)(void *)) plist_destructor;
	ld.module_number = module_number;
	ld.resource_id = list_destructors.nNextFreeElement;
	
	if (zend_hash_next_index_insert(&list_destructors,(void *) &ld,sizeof(list_destructors_entry),NULL)==FAILURE) {
		return FAILURE;
	}
	return list_destructors.nNextFreeElement-1;
}


/* registers all functions in *library_functions in the function hash */
int zend_register_functions(zend_function_entry *functions, HashTable *function_table)
{
	zend_function_entry *ptr = functions;
	zend_internal_function internal_function;
	int count=0,unload=0;
	HashTable *target_function_table = function_table;
	CLS_FETCH();

	if (!target_function_table) {
		target_function_table = CG(function_table);
	}
	internal_function.type = ZEND_INTERNAL_FUNCTION;
	
	while (ptr->fname) {
		internal_function.handler = ptr->handler;
		internal_function.arg_types = ptr->func_arg_types;
		internal_function.function_name = ptr->fname;
		if (!internal_function.handler) {
			zend_error(E_CORE_WARNING,"Null function defined as active function");
			zend_unregister_functions(functions, count, target_function_table);
			return FAILURE;
		}
		if (zend_hash_add(target_function_table, ptr->fname, strlen(ptr->fname)+1, &internal_function, sizeof(zend_internal_function), NULL) == FAILURE) {
			unload=1;
			break;
		}
		ptr++;
		count++;
	}
	if (unload) { /* before unloading, display all remaining bad function in the module */
		while (ptr->fname) {
			if (zend_hash_exists(target_function_table, ptr->fname, strlen(ptr->fname)+1)) {
				zend_error(E_CORE_WARNING, "Function registration failed - duplicate name - %s",ptr->fname);
			}
			ptr++;
		}
		zend_unregister_functions(functions, count, target_function_table);
		return FAILURE;
	}
	return SUCCESS;
}

/* count=-1 means erase all functions, otherwise, 
 * erase the first count functions
 */
void zend_unregister_functions(zend_function_entry *functions, int count, HashTable *function_table)
{
	zend_function_entry *ptr = functions;
	int i=0;
	HashTable *target_function_table = function_table;
	CLS_FETCH();

	if (!target_function_table) {
		target_function_table = CG(function_table);
	}
	while (ptr->fname) {
		if (count!=-1 && i>=count) {
			break;
		}
#if 0
		zend_printf("Unregistering %s()\n", ptr->fname);
#endif
		zend_hash_del(target_function_table, ptr->fname, strlen(ptr->fname)+1);
		ptr++;
		i++;
	}
}


ZEND_API int zend_register_module(zend_module_entry *module)
{
#if 0
	zend_printf("%s:  Registering module %d\n",module->name, module->module_number);
#endif
	if (module->functions && zend_register_functions(module->functions, NULL)==FAILURE) {
		zend_error(E_CORE_WARNING,"%s:  Unable to register functions, unable to load",module->name);
		return FAILURE;
	}
	module->module_started=1;
	return zend_hash_add(&module_registry, module->name,strlen(module->name)+1,(void *)module,sizeof(zend_module_entry),NULL);
}


void module_destructor(zend_module_entry *module)
{
	if (module->type == MODULE_TEMPORARY) {
		zend_hash_apply_with_argument(&list_destructors, (int (*)(void *,void *)) clean_module_resource_destructors, (void *) &(module->module_number));
		clean_module_constants(module->module_number);
	}

	if (module->request_started && module->request_shutdown_func) {
#if 0
		zend_printf("%s:  Request shutdown\n",module->name);
#endif
		module->request_shutdown_func(module->type, module->module_number);
	}
	module->request_started=0;
	if (module->module_started && module->module_shutdown_func) {
#if 0
		zend_printf("%s:  Module shutdown\n",module->name);
#endif
		module->module_shutdown_func(module->type, module->module_number);
	}
	module->module_started=0;
	if (module->functions) {
		zend_unregister_functions(module->functions, -1, NULL);
	}

#if HAVE_LIBDL
	if (module->handle) {
		dlclose(module->handle);
	}
#endif
}


/* call request startup for all modules */
int module_registry_request_startup(zend_module_entry *module)
{
	if (!module->request_started && module->request_startup_func) {
#if 0
		zend_printf("%s:  Request startup\n",module->name);
#endif
		module->request_startup_func(module->type, module->module_number);
	}
	module->request_started=1;
	return 0;
}


/* for persistent modules - call request shutdown and flag NOT to erase
 * for temporary modules - do nothing, and flag to erase
 */
int module_registry_cleanup(zend_module_entry *module)
{
	switch(module->type) {
		case MODULE_PERSISTENT:
			if (module->request_started && module->request_shutdown_func) {
#if 0
				zend_printf("%s:  Request shutdown\n",module->name);
#endif
				module->request_shutdown_func(module->type, module->module_number);
			}
			module->request_started=0;
			return 0;
			break;
		case MODULE_TEMPORARY:
			return 1;
			break;
	}
	return 0;
}


/* return the next free module number */
int zend_next_free_module(void)
{
	return ++module_count;
}


ZEND_API zend_class_entry *register_internal_class(zend_class_entry *class_entry)
{
	zend_class_entry *register_class;
	char *lowercase_name = zend_strndup(class_entry->name, class_entry->name_length);
	CLS_FETCH();

	zend_str_tolower(lowercase_name, class_entry->name_length);

	class_entry->type = ZEND_INTERNAL_CLASS;
	class_entry->parent = NULL;
	class_entry->refcount = (int *) malloc(sizeof(int));
	*class_entry->refcount = 1;
	zend_hash_init(&class_entry->default_properties, 0, NULL, PVAL_PTR_DTOR, 1);
	zend_hash_init(&class_entry->function_table, 0, NULL, ZEND_FUNCTION_DTOR, 1);

	zend_hash_update(CG(class_table), lowercase_name, class_entry->name_length+1, class_entry, sizeof(zend_class_entry), (void **) &register_class);
	free(lowercase_name);
	
	if (class_entry->builtin_functions) {
		zend_register_functions(class_entry->builtin_functions, &class_entry->function_table);
	}
	return register_class;
}


ZEND_API zend_module_entry *zend_get_module(int module_number)
{
	zend_module_entry *module;

	if (zend_hash_index_find(&module_registry, module_number, (void **) &module)==SUCCESS) {
		return module;
	} else {
		return NULL;
	}
}