cvs: pear /PECL/bcompiler bcompiler.c
| From: | Alan Knowles | Date: | Tue, 24 Dec 2002 04:42:32 +0000 |
| Subject: | cvs: pear /PECL/bcompiler bcompiler.c | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-7958@lists.php.net to get a copy of this message | ||
alan_k Mon Dec 23 23:42:32 2002 EDT
Modified files:
/pear/PECL/bcompiler bcompiler.c
Log:
added patch by Valentin Solop - support for include/require handling as per apc (eg. non variable
includes work ok..), can compile as zend module, tidied up indentation / tabs
Index: pear/PECL/bcompiler/bcompiler.c diff -u pear/PECL/bcompiler/bcompiler.c:1.19 pear/PECL/bcompiler/bcompiler.c:1.20 --- pear/PECL/bcompiler/bcompiler.c:1.19 Sun Dec 15 22:33:30 2002 +++ pear/PECL/bcompiler/bcompiler.c Mon Dec 23 23:42:32 2002 @@ -19,7 +19,7 @@ | Mike Bretz <mike@metropolis-ag.de> | +----------------------------------------------------------------------+ */ -/* $Id: bcompiler.c,v 1.19 2002/12/16 03:33:30 alan_k Exp $ */ +/* $Id: bcompiler.c,v 1.20 2002/12/24 04:42:32 alan_k Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -31,7 +31,7 @@ #include "ext/standard/file.h" #include "php_bcompiler.h" #include "zend.h" - +#include "zend_extensions.h" #include <stdlib.h> /* this should be in php.h #include <assert.h> */ @@ -48,7 +48,7 @@ /* Debugging */ -#define DEBUGLEVEL 0 +#define DEBUGLEVEL 0 #if DEBUGLEVEL #define BCOMPILER_DEBUG(args) (printf("DEBUG: "), printf args) #else @@ -58,6 +58,8 @@ /* we use globals */ ZEND_DECLARE_MODULE_GLOBALS(bcompiler) +extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); +static zend_op_array *(*bcompiler_saved_zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); /* {{{ bcompiler_functions[] @@ -67,7 +69,7 @@ function_entry bcompiler_functions[] = { PHP_FE(bcompiler_write_header, NULL) PHP_FE(bcompiler_write_class, NULL) - PHP_FE(bcompiler_write_function, NULL) + PHP_FE(bcompiler_write_function, NULL) PHP_FE(bcompiler_write_included_filename, NULL) PHP_FE(bcompiler_write_footer, NULL) PHP_FE(bcompiler_write_exe_footer, NULL) @@ -89,8 +91,8 @@ bcompiler_functions, PHP_MINIT(bcompiler), PHP_MSHUTDOWN(bcompiler), - PHP_RINIT(bcompiler), - PHP_RSHUTDOWN(bcompiler), + PHP_RINIT(bcompiler), + PHP_RSHUTDOWN(bcompiler), PHP_MINFO(bcompiler), #if ZEND_MODULE_API_NO >= 20010901 BCOMPILER_VERSION BCOMPILER_STREAMS, /* Replace with version number for your extension */ @@ -103,7 +105,199 @@ ZEND_GET_MODULE(bcompiler) #endif +ZEND_DLEXPORT int bcompiler_zend_startup(zend_extension *extension) +{ + return zend_startup_module(&bcompiler_module_entry); +} + +ZEND_DLEXPORT void bcompiler_zend_shutdown(zend_extension *extension) +{ + +} + +#ifndef ZEND_EXT_API +#define ZEND_EXT_API ZEND_DLEXPORT +#endif +ZEND_EXTENSION(); + +ZEND_DLEXPORT zend_extension zend_extension_entry = { + "bcompiler", + BCOMPILER_VERSION, + "Alan Knowles (php_module), Dan Cowgill and George Schlossnagle", + "http://apc.communityconnect.com, http://www.php.net", + "Copyright (c) 2000-2001 Community Connect Inc. (apc_* functions), 1997-2002 The PHP Group (remaining code)", + bcompiler_zend_startup, + bcompiler_zend_shutdown, + NULL, /* activate_func_t */ + NULL, /* deactivate_func_t */ + NULL, /* message_handler_func_t */ + NULL, /* op_array_handler_func_t */ + NULL, /* statement_handler_func_t */ + NULL, /* fcall_begin_handler_func_t */ + NULL, /* fcall_end_handler_func_t */ + NULL, /* op_array_ctor_func_t */ + NULL, /* op_array_dtor_func_t */ +#ifdef COMPAT_ZEND_EXTENSION_PROPERTIES + NULL, /* api_no_check */ + COMPAT_ZEND_EXTENSION_PROPERTIES +#else + STANDARD_ZEND_EXTENSION_PROPERTIES +#endif +}; + +/* {{{ + */ +zend_op_array *dummy_op_array() { + zval *zstring = NULL; + char *eval_desc = NULL; + zend_op_array *result; + TSRMLS_FETCH(); + MAKE_STD_ZVAL(zstring); + ZVAL_STRING(zstring, "return true;", 1); + eval_desc = zend_make_compiled_string_description("compiled code" TSRMLS_CC); + result = compile_string(zstring, eval_desc TSRMLS_CC); + efree(eval_desc); + zval_dtor(zstring); + return result; +} +/* }}} */ + +/* bad news: bzip2 stream wrapper opens _any_ existing file and treat it as valid stream. + so, we check bzip2 magic string manually ... */ +static int is_bz2_file_handle(FILE *handle) +{ + long pos = 0; + int result = 0; + char *magic = NULL; + + if(handle) { + pos = ftell(handle); + fseek(handle, 0, 0); + magic = (char *) emalloc(3); memset(magic, 0, 3); + fread(magic, 1, 2, handle); + fseek(handle, pos, SEEK_SET); + if(strncmp("BZ", magic, 2) == 0) { + result = 1; + } + efree(magic); + } + return result; +} + +static int is_valid_file_name(char *filename) +{ + int result = 1; + int len = strlen(filename); + if(len) { + result = strncasecmp(filename, "http://", 7); + result &= strncasecmp(filename + len - 1, "-", 1); + } + return result; +} +static void *bz2_stream_open(char *file_name) +{ + int pathlen = 0; + char *path = NULL; + void *stream = NULL; + + TSRMLS_FETCH(); + pathlen = strlen("compress.bzip2://") + strlen(file_name) + 1; + path = emalloc(pathlen); memset(path, 0, pathlen); + strcpy(path, "compress.bzip2://"); + strcat(path, file_name); + stream = php_stream_open_wrapper(path, "r", REPORT_ERRORS, NULL); + efree(path); + return stream; +} + +/* + type + ZEND_EVAL 1<<0 + ZEND_INCLUDE 1<<1 + ZEND_INCLUDE_ONCE 1<<2 + ZEND_REQUIRE 1<<3 + ZEND_REQUIRE_ONCE 1<<4 +*/ +zend_op_array *bcompiler_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) +{ + int test = -1; + void *stream = NULL; + zend_op_array *op_array = NULL; + char *filename = NULL; + FILE *fp; + +#ifdef PHP_HAVE_STREAMS + test = -1; + switch(file_handle->type) + { + case ZEND_HANDLE_FILENAME: /* 0 */ + if(file_handle->opened_path) { + filename = file_handle->opened_path; + } else { + filename = file_handle->filename; + } + + if (is_valid_file_name(filename)) { + fp = zend_fopen(filename, NULL); + if(is_bz2_file_handle(fp)) { + stream = bz2_stream_open(filename); + } + fclose(fp); + } + break; + + case ZEND_HANDLE_FD: /* 1 */ + break; + + case ZEND_HANDLE_FP: /* 2 */ + if(file_handle->opened_path) { + filename = file_handle->opened_path; + } else { + filename = file_handle->filename; + } + if (is_valid_file_name(filename)) { + fp = zend_fopen(filename, NULL); + if(is_bz2_file_handle(fp)) { + stream = bz2_stream_open(filename); + } + fclose(fp); + } + break; + + case ZEND_HANDLE_STDIOSTREAM: /* 3 */ + break; + + case ZEND_HANDLE_FSTREAM: /* 4 */ + break; + } +#else + /* < 4.3 use current file pointer. WARNING!!! bcompiled file must be plain... no compression supported */ + /*TODO if (!file_handle->handle.fp) { fp = try_to_open(file_handle.->file_name); stream = fp; } else */ + stream = file_handle->handle.fp; +#endif + + if(stream) { + BCOMPILERG(stream) = stream; + test = apc_deserialize_magic(TSRMLS_C); + } + + if (test == 0) { + bcompiler_read(TSRMLS_C); + BCOMPILERG(buffer) = (char *) emalloc(1); /* avoid 1 byte memory leak */ + op_array = dummy_op_array(); + } else { + /* no bcompiler magic - just pass it to zend... */ + op_array = bcompiler_saved_zend_compile_file(file_handle, type TSRMLS_CC); + } + +#ifdef PHP_HAVE_STREAMS + if(stream) { + php_stream_close(stream); + } +#endif + return op_array; +} /* {{{ php_bcompiler_init_globals */ @@ -126,6 +320,10 @@ { ZEND_INIT_MODULE_GLOBALS(bcompiler, php_bcompiler_init_globals, NULL); + + bcompiler_saved_zend_compile_file = zend_compile_file; + zend_compile_file = bcompiler_compile_file; + return SUCCESS; } /* }}} */ @@ -134,6 +332,7 @@ */ PHP_MSHUTDOWN_FUNCTION(bcompiler) { + zend_compile_file = bcompiler_saved_zend_compile_file; return SUCCESS; } /* }}} */ @@ -181,8 +380,8 @@ int apclib_alignword_int(int x) { - typedef union { void* p; int i; } word_t; - return sizeof(word_t) * (1 + ((x-1)/sizeof(word_t))); + typedef union { void* p; int i; } word_t; + return sizeof(word_t) * (1 + ((x-1)/sizeof(word_t))); } /* @@ -207,20 +406,20 @@ /* SERIALIZE_SCALAR: write a scalar value to dst */ -#define SERIALIZE_SCALAR(x, type) { \ +#define SERIALIZE_SCALAR(x, type) { \ if (BCOMPILERG(stream) != NULL) { \ - char _buf[255]; \ - BCOMPILER_DEBUG((" scalar: %i\n", (int) x)); \ - *(type*) _buf = x; \ - php_stream_write((php_stream *) BCOMPILERG(stream) , _buf, (size_t) apclib_alignword_int(sizeof(type))); \ + char _buf[255]; \ + BCOMPILER_DEBUG((" scalar: %i\n", (int) x)); \ + *(type*) _buf = x; \ + php_stream_write((php_stream *) BCOMPILERG(stream) , _buf, (size_t) apclib_alignword_int(sizeof(type))); \ } \ } /* DESERIALIZE_SCALAR: read a scalar value from src */ #define DESERIALIZE_SCALAR(xp, type) { \ if (php_stream_eof(BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), sizeof(type) + 1); \ php_stream_read((php_stream *) BCOMPILERG(stream) , BCOMPILERG(buffer) , (size_t) apclib_alignword_int(sizeof(type))); \ @@ -233,8 +432,8 @@ /* STORE_BYTES: memcpy wrapper, writes to dst buffer */ #define STORE_BYTES(bytes, n) { \ if (BCOMPILERG(stream) != NULL) { \ - BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ - php_stream_write((php_stream *) BCOMPILERG(stream) , (char *)bytes, n); \ + BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ + php_stream_write((php_stream *) BCOMPILERG(stream) , (char *)bytes, n); \ } \ } @@ -242,8 +441,8 @@ /* LOAD_BYTES: memcpy wrapper, reads from src buffer */ #define LOAD_BYTES(bytes, n) { \ if (php_stream_eof(BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), n + 1); \ php_stream_read((php_stream *) BCOMPILERG(stream) , BCOMPILERG(buffer) , n); \ @@ -267,18 +466,18 @@ #define SERIALIZE_SCALAR(x, type) { \ if (BCOMPILERG(stream) != NULL) { \ - char _buf[255]; \ - BCOMPILER_DEBUG((" scalar: %i\n",(type) x)); \ - *(type*) _buf = x; \ - fwrite(_buf, 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ + char _buf[255]; \ + BCOMPILER_DEBUG((" scalar: %i\n",(type) x)); \ + *(type*) _buf = x; \ + fwrite(_buf, 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ } \ } /* DESERIALIZE_SCALAR: read a scalar value from src */ #define DESERIALIZE_SCALAR(xp, type) { \ if (feof((FILE*) BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), sizeof(type) + 1); \ fread(BCOMPILERG(buffer) , 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ @@ -290,8 +489,8 @@ /* STORE_BYTES: memcpy wrapper, writes to dst buffer */ #define STORE_BYTES(bytes, n) { \ if (BCOMPILERG(stream) != NULL) { \ - BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ - fwrite((char *)bytes, 1, n, (FILE*) BCOMPILERG(stream)); \ + BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ + fwrite((char *)bytes, 1, n, (FILE*) BCOMPILERG(stream)); \ } \ } @@ -299,8 +498,8 @@ /* LOAD_BYTES: memcpy wrapper, reads from src buffer */ #define LOAD_BYTES(bytes, n) { \ if (feof((FILE*) BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), n + 1); \ fread(BCOMPILERG(buffer) ,1, n, (FILE*) BCOMPILERG(stream)); \ @@ -328,13 +527,13 @@ #define CALLBACK_ADD_STRING(str,strlen) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), str, strlen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), str, strlen , 1); \ } \ } #define CALLBACK_ADD_KEY_STRING(key, str,strlen) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), key, str, strlen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), key, str, strlen , 1); \ } \ } @@ -343,12 +542,12 @@ int len; \ len = sprintf(out,str,val); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), out, len , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), out, len , 1); \ } \ } #define CALLBACK_ADD_LONG(key, val) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_long(BCOMPILERG(callback_value), key, val); \ + add_assoc_long(BCOMPILERG(callback_value), key, val); \ } \ } #define CALLBACK_ADD_SUB_LONG(kkey, val) { \ @@ -356,7 +555,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_long(BCOMPILERG(callback_value), out, val); \ + add_assoc_long(BCOMPILERG(callback_value), out, val); \ } \ } #define CALLBACK_ADD_SUB_DOUBLE(kkey, val) { \ @@ -364,7 +563,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_double(BCOMPILERG(callback_value), out, val); \ + add_assoc_double(BCOMPILERG(callback_value), out, val); \ } \ } #define CALLBACK_ADD_SUB_STRING(kkey, sval,slen) { \ @@ -372,7 +571,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), out, sval ,slen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), out, sval ,slen , 1); \ } \ } @@ -380,7 +579,7 @@ #define CALLBACK_ADD_DOUBLE(key,val) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_double(BCOMPILERG(callback_value), key, val); \ + add_assoc_double(BCOMPILERG(callback_value), key, val); \ } \ } @@ -388,25 +587,25 @@ /* CALLBACK START (opcode) */ #define CALLBACK_START(code) { \ if (BCOMPILERG(callback) != NULL) { \ - char *str; \ - MAKE_STD_ZVAL(BCOMPILERG(callback_value)); \ - array_init(BCOMPILERG(callback_value)); \ - /*Optype - */ \ - str = (char *) getOpcodeName(code ); \ - add_assoc_long(BCOMPILERG(callback_value), "OPCODE", code); \ - add_assoc_stringl(BCOMPILERG(callback_value), "OPCODE_STR", str, strlen(str) , 1); \ + char *str; \ + MAKE_STD_ZVAL(BCOMPILERG(callback_value)); \ + array_init(BCOMPILERG(callback_value)); \ + /*Optype - */ \ + str = (char *) getOpcodeName(code ); \ + add_assoc_long(BCOMPILERG(callback_value), "OPCODE", code); \ + add_assoc_stringl(BCOMPILERG(callback_value), "OPCODE_STR", str, strlen(str) , 1); \ } \ } #define CALLBACK_END() { \ zval myretval; \ - zval *sendval; \ + zval *sendval; \ if (BCOMPILERG(callback) != NULL) { \ - sendval = BCOMPILERG(callback_value); \ - zval_copy_ctor(sendval); \ - if(!call_user_function( EG(function_table), NULL, BCOMPILERG(callback), &myretval, 1, &sendval TSRMLS_CC )) { \ - zval_dtor(BCOMPILERG(callback_value)); \ - } \ + sendval = BCOMPILERG(callback_value); \ + zval_copy_ctor(sendval); \ + if(!call_user_function( EG(function_table), NULL, BCOMPILERG(callback), &myretval, 1, &sendval TSRMLS_CC )) { \ + zval_dtor(BCOMPILERG(callback_value)); \ + } \ } \ } @@ -440,7 +639,7 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + zend_str_tolower(class_name, class_len); @@ -509,13 +708,13 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + zend_str_tolower(function_name, function_len); zend_hash_find(CG(function_table), function_name, function_len+1, (void **)&fe); if (!fe) { - php_error(E_WARNING, "Could not find function"); + php_error(E_WARNING, "Could not find function"); RETURN_NULL(); } BCOMPILERG(stream) = stream; @@ -598,7 +797,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -623,14 +822,14 @@ Writes the end zero*/ PHP_FUNCTION(bcompiler_write_footer) { - + zval *rsrc; void *stream; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -662,7 +861,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &rsrc, &start) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -691,19 +890,19 @@ int test =0; void *stream; - + zend_class_entry *ce = NULL; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; @@ -712,7 +911,7 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } BCOMPILER_DEBUG(("STARTING CLASS\n")); bcompiler_read( TSRMLS_C); @@ -776,8 +975,8 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - - + + BCOMPILERG(stream) = stream; @@ -785,8 +984,8 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); - return; - + return; + } bcompiler_read( TSRMLS_C); @@ -849,7 +1048,7 @@ MAKE_STD_ZVAL(zmode); ZVAL_STRING(zmode, "r", 1); args[1] = &zmode; - + call_user_function_ex(CG(function_table), NULL, zfopen, &rsrc, 2, args, 0, NULL TSRMLS_CC ); #ifdef PHP_HAVE_STREAMS @@ -858,7 +1057,7 @@ ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; pos = (long) ((strlen(BCOMPILER_MAGIC_HEADER)+4) * -1); // +4 cause we need to read the string size BCOMPILER_DEBUG(("move to start: %d\n", pos)); @@ -874,13 +1073,13 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } /* now read the length */ pos = pos - 4; BCOMPILER_DEBUG(("move to start: %d\n", pos)); - + #ifdef PHP_HAVE_STREAMS php_stream_seek(stream, pos , SEEK_END); #else @@ -919,7 +1118,7 @@ ZEND_FETCH_RESOURCE(stream, void *, &rsrc2, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; @@ -928,10 +1127,10 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } bcompiler_read( TSRMLS_C); - + #ifdef PHP_HAVE_STREAMS php_stream_close(stream); #else @@ -958,9 +1157,9 @@ DESERIALIZE_SCALAR(&item, char); while (item) { - + switch (item) { - + case BCOMPILER_CLASS_ENTRY: BCOMPILER_DEBUG(("STARTING CLASS\n")); apc_create_zend_class_entry(&zc TSRMLS_CC); @@ -1001,7 +1200,7 @@ } break; - } + } DESERIALIZE_SCALAR(&item, char); } @@ -1011,6 +1210,8 @@ + + /* ---------------------------------------------------------------------------- * * from apc_lib.h -- GENERALLY STUFF that doesnt need GLOBALS! @@ -1056,10 +1257,11 @@ void apclib_free_string(char **string) { - if (*string == NULL) - return; - efree(*string); - *string = NULL; + if (*string == NULL) { + return; + } + efree(*string); + *string = NULL; } @@ -1076,9 +1278,9 @@ typedef struct namearray_t namearray_t; struct namearray_t { - char* strings; /* array of null-terminated names */ - int length; /* logical size of strings */ - int size; /* physical size of strings */ + char* strings; /* array of null-terminated names */ + int length; /* logical size of strings */ + int size; /* physical size of strings */ }; @@ -1207,7 +1409,7 @@ "EXT_NOP", /* 104 */ "TICKS", /* 105 */ "SEND_VAR_NO_REF", /* 106 */ - + "ZEND_CATCH", /* 107 */ "ZEND_THROW", /* 108 */ "ZEND_FETCH_CLASS", /* 109 */ @@ -1242,7 +1444,7 @@ "ZEND_ASSIGN_OBJ", /* 136 */ "ZEND_MAKE_VAR", /* 137 */ - + "BEGIN_HASH_DEF", /* 138 */ @@ -1260,7 +1462,7 @@ const char* getOpcodeName(int op) { if (op < 0 || op >= NUM_OPCODES) { - return "(unknown)"; + return "(unknown)"; } return opcodes[op]; } @@ -1292,7 +1494,7 @@ BCOMPILER_DEBUG(("base: %s",base->name)); if (!base->parent) { - return; // 'parent' has no children, nothing to do + return; // 'parent' has no children, nothing to do } child = base->parent; @@ -1342,30 +1544,30 @@ void apc_serialize_zstring(char* string, int len TSRMLS_DC) { - /* by convention, mark null strings with a length of -1 */ - if (len <= 0 || string == NULL) { - SERIALIZE_SCALAR(-1, int); - return; - } -// len = strlen(string); - SERIALIZE_SCALAR(len, int); - STORE_BYTES(string, len); + /* by convention, mark null strings with a length of -1 */ + if (len <= 0 || string == NULL) { + SERIALIZE_SCALAR(-1, int); + return; + } + // len = strlen(string); + SERIALIZE_SCALAR(len, int); + STORE_BYTES(string, len); } void apc_create_string(char** string TSRMLS_DC) { - int len = 0; + int len = 0; - DESERIALIZE_SCALAR(&len, int); - if (len <= 0) { - /* by convention, empty marked strings (len is -1) are returned as an empty string */ - *string = (char*) emalloc(1); - (*string)[0] = '\0'; - return; - } - *string = (char*) emalloc(len + 1); - LOAD_BYTES(*string, len); - (*string)[len] = '\0'; + DESERIALIZE_SCALAR(&len, int); + if (len <= 0) { + /* by convention, empty marked strings (len is -1) are returned as an empty string */ + *string = (char*) emalloc(1); + (*string)[0] = '\0'; + return; + } + *string = (char*) emalloc(len + 1); + LOAD_BYTES(*string, len); + (*string)[len] = '\0'; } void apc_serialize_arg_types(zend_uchar* arg_types TSRMLS_DC) @@ -1437,7 +1639,7 @@ SERIALIZE_SCALAR(list->persistent, unsigned char); SERIALIZE_SCALAR(zend_llist_count(list), int); zend_llist_apply_with_argument(list, store_zend_llist_element, - &list->size TSRMLS_CC); + &list->size TSRMLS_CC); } void apc_deserialize_zend_llist(zend_llist* list TSRMLS_DC) @@ -1508,12 +1710,12 @@ void apc_serialize_hashtable(HashTable* ht, void* funcptr TSRMLS_DC) { - char exists; /* for identifying null lists */ + char exists; /* for identifying null lists */ Bucket* p; void (*serialize_bucket)(void* TSRMLS_DC); - + serialize_bucket = (void(*)(void* TSRMLS_DC)) funcptr; exists = (ht != NULL) ? 1 : 0; @@ -1523,7 +1725,7 @@ } - + /* Serialize the hash meta-data. */ SERIALIZE_SCALAR(ht->nTableSize, uint); SERIALIZE_SCALAR(ht->pDestructor, void*); @@ -1589,54 +1791,50 @@ * we can just deserialize that many hashtable elements. */ for (j = 0; j < (int) nNumOfElements; j++) { - DESERIALIZE_SCALAR(&h, ulong); - DESERIALIZE_SCALAR(&nKeyLength, uint); - apc_create_string(&arKey TSRMLS_CC); - deserialize_bucket(&pData TSRMLS_CC); - - /* If nKeyLength is non-zero, this element is a hashed key/value - * pair. Otherwise, it is an array element with a numeric index */ - BCOMPILER_DEBUG(("-----------------------------\nFUNC %s\n",arKey)); - - if (nKeyLength != 0) { - if(datasize == sizeof(void*)) { - zend_hash_add_or_update(ht, arKey, nKeyLength, &pData, - datasize, NULL, HASH_ADD); - } - else { - zend_hash_add_or_update(ht, arKey, nKeyLength, pData, - datasize, NULL, HASH_ADD); - } + DESERIALIZE_SCALAR(&h, ulong); + DESERIALIZE_SCALAR(&nKeyLength, uint); + apc_create_string(&arKey TSRMLS_CC); + deserialize_bucket(&pData TSRMLS_CC); + + /* If nKeyLength is non-zero, this element is a hashed key/value + * pair. Otherwise, it is an array element with a numeric index */ + BCOMPILER_DEBUG(("-----------------------------\nFUNC %s\n",arKey)); + + if (nKeyLength != 0) { + if(datasize == sizeof(void*)) { + zend_hash_add_or_update(ht, arKey, nKeyLength, &pData, + datasize, NULL, HASH_ADD); + } else { + zend_hash_add_or_update(ht, arKey, nKeyLength, pData, + datasize, NULL, HASH_ADD); } - else { /* numeric index, not key string */ - if(datasize == sizeof(void*)) { - zend_hash_index_update(ht, h, &pData, datasize, NULL); - } - else { - zend_hash_index_update(ht, h, pData, datasize, NULL); - } - } - if (dptr != NULL) { - free_bucket(&pData); + } else { /* numeric index, not key string */ + if(datasize == sizeof(void*)) { + zend_hash_index_update(ht, h, &pData, datasize, NULL); + } else { + zend_hash_index_update(ht, h, pData, datasize, NULL); } - // else { - // BCOMPILER_DEBUG((stderr, "Do not free: %s\n", arKey)); - // } - apclib_free_string(&arKey); // ADDED BY MPB + } + if (dptr != NULL) { + free_bucket(&pData); + } + // else { + // BCOMPILER_DEBUG((stderr, "Do not free: %s\n", arKey)); + // } + apclib_free_string(&arKey); // ADDED BY MPB } } void apc_create_hashtable(HashTable** ht, void* funcptr, void* dptr, int datasize TSRMLS_DC) { - char exists; /* for identifying null hashtables */ + char exists; /* for identifying null hashtables */ /*PEEK_SCALAR(&exists, char); */ DESERIALIZE_SCALAR(&exists, char); if (exists==1) { *ht = (HashTable*) emalloc(sizeof(HashTable)); apc_deserialize_hashtable(*ht, funcptr, dptr, datasize,exists TSRMLS_CC); - } - else { + } else { /* DESERIALIZE_SCALAR(&exists, char); */ *ht = 0; } @@ -1651,44 +1849,45 @@ * determine exactly what it's type is, then serialize the * appropriate structure. */ CALLBACK_ADD_SUB_LONG("u.constant.type",type); - switch (type) { - case IS_RESOURCE: - case IS_BOOL: - case IS_LONG: + switch (type) + { + case IS_RESOURCE: + case IS_BOOL: + case IS_LONG: CALLBACK_ADD_SUB_LONG("u.constant.val",zv->lval); CALLBACK_ADD_LONG(BCOMPILERG(callback_key),zv->lval); SERIALIZE_SCALAR(zv->lval, long); - break; - case IS_DOUBLE: + break; + case IS_DOUBLE: CALLBACK_ADD_SUB_DOUBLE("u.constant.val",zv->dval); CALLBACK_ADD_DOUBLE(BCOMPILERG(callback_key),zv->dval); SERIALIZE_SCALAR(zv->dval, double); break; - case IS_NULL: + case IS_NULL: /* null value, do nothing */ break; - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: CALLBACK_ADD_SUB_STRING("u.constant.val",zv->str.val, zv->str.len); CALLBACK_ADD_STRING(zv->str.val, zv->str.len); apc_serialize_zstring(zv->str.val, zv->str.len TSRMLS_CC); SERIALIZE_SCALAR(zv->str.len, int); break; - case IS_ARRAY: + case IS_ARRAY: CALLBACK_ADD_STRING("ARRAY?", 6); apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC); break; - case IS_CONSTANT_ARRAY: + case IS_CONSTANT_ARRAY: CALLBACK_ADD_STRING("CARRAY?", 7); apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC); break; - case IS_OBJECT: + case IS_OBJECT: CALLBACK_ADD_STRING("OBJECT?", 7); apc_serialize_zend_class_entry(zv->obj.ce TSRMLS_CC); apc_serialize_hashtable(zv->obj.properties, apc_serialize_zval_ptr TSRMLS_CC); break; - default: + default: /* The above list enumerates all types. If we get here, * something very very bad has happened. */ assert(0); @@ -1699,39 +1898,40 @@ { /* We peeked ahead in the calling routine to deserialize the * type. Now we just deserialize. */ - switch(type) { - case IS_RESOURCE: - case IS_BOOL: - case IS_LONG: + switch(type) + { + case IS_RESOURCE: + case IS_BOOL: + case IS_LONG: DESERIALIZE_SCALAR(&zv->lval, int); break; - case IS_NULL: + case IS_NULL: /* null value, do nothing */ break; - case IS_DOUBLE: + case IS_DOUBLE: DESERIALIZE_SCALAR(&zv->dval, double); break; - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: apc_create_string(&zv->str.val TSRMLS_CC); DESERIALIZE_SCALAR(&zv->str.len, int); break; - case IS_ARRAY: + case IS_ARRAY: apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC); break; - case IS_CONSTANT_ARRAY: + case IS_CONSTANT_ARRAY: apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC); break; - case IS_OBJECT: + case IS_OBJECT: apc_create_zend_class_entry(&zv->obj.ce TSRMLS_CC); apc_create_hashtable(&zv->obj.properties, apc_create_zval, NULL, sizeof(zval *) TSRMLS_CC); break; - default: - /* The above list enumerates all types. If we get here, - * something very very bad has happened. */ + default: + /* The above list enumerates all types. If we get here, + * something very very bad has happened. */ assert(0); - } + } } @@ -1769,17 +1969,18 @@ } void apc_free_zval(zval** zv TSRMLS_DC) { - if (*zv != NULL) { - switch((*zv)->type) { - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: - apclib_free_string(&(*zv)->value.str.val); - break; - } - efree(*zv); - } - *zv = NULL; + if (*zv != NULL) { + switch((*zv)->type) + { + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: + apclib_free_string(&(*zv)->value.str.val); + break; + } + efree(*zv); + } + *zv = NULL; } @@ -1854,8 +2055,8 @@ NOT::call,set etc: - would these point to a 'method?' !! ZEND_DECLARE_FUNCTION_OR_CLASS */ - - + + zend_function_entry* zfe; int count, i, exists; @@ -1878,7 +2079,6 @@ if (exists) { /*Parent*/ - apc_serialize_zstring(zce->parent->name, zce->parent->name_length TSRMLS_CC); } @@ -1938,8 +2138,8 @@ // manually inheriting methods from the parent (PHP will perform // the inheritance). - char* parent_name; // name of parent class - + char* parent_name; // name of parent class + apc_create_string(&parent_name TSRMLS_CC); /*if ((zend_hash_find(CG(class_table), parent_name, @@ -1958,13 +2158,13 @@ zce->refcount = (int*) emalloc(sizeof(int)); DESERIALIZE_SCALAR(zce->refcount, int); DESERIALIZE_SCALAR(&zce->constants_updated, zend_bool); - BCOMPILER_DEBUG(("-----------------------------\nFUNC TABLE:\n")); - DESERIALIZE_SCALAR(&exists, char); + BCOMPILER_DEBUG(("-----------------------------\nFUNC TABLE:\n")); + DESERIALIZE_SCALAR(&exists, char); apc_deserialize_hashtable(&zce->function_table, (void*) apc_create_zend_function, (void*) apc_free_zend_function, - (int) sizeof(zend_function) , (char) exists TSRMLS_CC); + (int) sizeof(zend_function) , (char) exists TSRMLS_CC); BCOMPILER_DEBUG(("-----------------------------\nDEFAYLT PROPS:\n")); - DESERIALIZE_SCALAR(&exists, char); + DESERIALIZE_SCALAR(&exists, char); apc_deserialize_hashtable(&zce->default_properties, (void*) apc_create_zval, (void*) NULL, (int) sizeof(zval *), (char)exists TSRMLS_CC); // see apc_serialize_zend_class_entry() for a description of the @@ -1974,7 +2174,7 @@ zce->builtin_functions = NULL; if (count > 0) { zce->builtin_functions = (zend_function_entry*) - emalloc((count+1) * sizeof(zend_function_entry)); + emalloc((count+1) * sizeof(zend_function_entry)); zce->builtin_functions[count].fname = NULL; for (i = 0; i < count; i++) { apc_deserialize_zend_function_entry(&zce->builtin_functions[i] TSRMLS_CC); @@ -2000,7 +2200,7 @@ */ /* if (zce->parent) { - bcompiler_inherit(zce TSRMLS_CC); + bcompiler_inherit(zce TSRMLS_CC); } */ } @@ -2028,37 +2228,37 @@ * a bitwise copy. */ CALLBACK_ADD_SUB_LONG("op_type" ,(long) zn->op_type); CALLBACK_ADD_SUB_LONG("u.fetch_type" ,(int) zn->u.fetch_type); - CALLBACK_ADD_SUB_LONG("u.EA.type" ,(int) zn->u.EA.type); - + CALLBACK_ADD_SUB_LONG("u.EA.type" ,(int) zn->u.EA.type); + switch(zn->op_type) { - case IS_CONST: - if (&zn->u.fetch_type == ZEND_FETCH_GLOBAL) { - CALLBACK_ADD_STRING("**GLOBAL",8); - } - apc_serialize_zval(&zn->u.constant TSRMLS_CC); - break; - - case IS_VAR: - CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); - CALLBACK_ADD_STRING_LONG("VAR:%i",(long) zn->u.var); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - case IS_TMP_VAR: - CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); - CALLBACK_ADD_STRING_LONG("TMPVAR:%i",(long) zn->u.var); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - case IS_UNUSED: - /* CALLBACK_ADD_STRING("**NOTUSED",9); */ - CALLBACK_ADD_SUB_LONG("u.opline_num" ,(long) zn->u.opline_num); - - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - - default: - CALLBACK_ADD_STRING_LONG("**UNKNOWN",(long) sizeof(zn->u)); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; + case IS_CONST: + if (&zn->u.fetch_type == ZEND_FETCH_GLOBAL) { + CALLBACK_ADD_STRING("**GLOBAL",8); + } + apc_serialize_zval(&zn->u.constant TSRMLS_CC); + break; + + case IS_VAR: + CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); + CALLBACK_ADD_STRING_LONG("VAR:%i",(long) zn->u.var); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + case IS_TMP_VAR: + CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); + CALLBACK_ADD_STRING_LONG("TMPVAR:%i",(long) zn->u.var); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + case IS_UNUSED: + /* CALLBACK_ADD_STRING("**NOTUSED",9); */ + CALLBACK_ADD_SUB_LONG("u.opline_num" ,(long) zn->u.opline_num); + + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + + default: + CALLBACK_ADD_STRING_LONG("**UNKNOWN",(long) sizeof(zn->u)); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; } } @@ -2070,11 +2270,12 @@ * otherwise, it is too case-dependent (or inscrutable), so we do * a bitwise copy. */ - switch(zn->op_type) { - case IS_CONST: + switch(zn->op_type) + { + case IS_CONST: apc_deserialize_zval(&zn->u.constant TSRMLS_CC); break; - default: + default: LOAD_BYTES(&zn->u, sizeof(zn->u)); break; } @@ -2092,13 +2293,13 @@ SERIALIZE_SCALAR(zo->opcode, zend_uchar); BCOMPILERG(callback_key)="RESULT"; - apc_serialize_znode(&zo->result TSRMLS_CC); + apc_serialize_znode(&zo->result TSRMLS_CC); BCOMPILERG(callback_key)="OP1"; - apc_serialize_znode(&zo->op1 TSRMLS_CC); + apc_serialize_znode(&zo->op1 TSRMLS_CC); BCOMPILERG(callback_key)="OP2"; - apc_serialize_znode(&zo->op2 TSRMLS_CC); + apc_serialize_znode(&zo->op2 TSRMLS_CC); SERIALIZE_SCALAR(zo->extended_value, ulong); if (zo->extended_value != 0) { @@ -2106,18 +2307,19 @@ } CALLBACK_ADD_LONG("LINE",(long) id); /* add some useful info! */ - switch (zo->opcode) { - case ZEND_JMP: - CALLBACK_ADD_LONG("JMP",zo->op1.u.opline_num); - break; - case ZEND_JMPZ: - CALLBACK_ADD_LONG("JMP",zo->op2.u.opline_num); - break; + switch (zo->opcode) + { + case ZEND_JMP: + CALLBACK_ADD_LONG("JMP",zo->op1.u.opline_num); + break; + case ZEND_JMPZ: + CALLBACK_ADD_LONG("JMP",zo->op2.u.opline_num); + break; } CALLBACK_END(); - SERIALIZE_SCALAR(zo->lineno, uint); + SERIALIZE_SCALAR(zo->lineno, uint); } void apc_deserialize_zend_op(zend_op* zo TSRMLS_DC) @@ -2171,7 +2373,7 @@ CALLBACK_ADD_LONG("USES_GLOBALS",(long) zoa->uses_globals); CALLBACK_END(); for (i = 0; i < (int) zoa->last; i++) { - + apc_serialize_zend_op(i, &zoa->opcodes[i] TSRMLS_CC); } @@ -2223,134 +2425,140 @@ apc_deserialize_zend_op(&zoa->opcodes[i] TSRMLS_CC); if(zoa->opcodes[i].opcode == ZEND_DECLARE_FUNCTION_OR_CLASS) { HashTable* table; - char* op2str; /* op2str and op2len are for convenience */ + char* op2str; /* op2str and op2len are for convenience */ int op2len; - + exists = 1; - + op2str = zoa->opcodes[i].op2.u.constant.value.str.val; op2len = zoa->opcodes[i].op2.u.constant.value.str.len; - - switch(zoa->opcodes[i].extended_value) { - /* a run-time function declaration */ - case ZEND_DECLARE_FUNCTION: { - zend_function* function; - - table = CG(function_table); - - if (zend_hash_find(table, op2str, op2len + 1, - (void**) &function) == SUCCESS) + + switch(zoa->opcodes[i].extended_value) + { + /* a run-time function declaration */ + case ZEND_DECLARE_FUNCTION: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - } break; - - /* a run-time class declaration */ - case ZEND_DECLARE_CLASS: { - zend_class_entry *ce; - - table = CG(class_table); - - if (zend_hash_find(table, op2str, op2len + 1, - (void**) &ce) == SUCCESS) + zend_function* function; + + table = CG(function_table); + + if (zend_hash_find(table, op2str, op2len + 1, + (void**) &function) == SUCCESS) + { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + } + break; + + /* a run-time class declaration */ + case ZEND_DECLARE_CLASS: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - } break; + zend_class_entry *ce; + + table = CG(class_table); + + if (zend_hash_find(table, op2str, op2len + 1, + (void**) &ce) == SUCCESS) { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + } + break; /* a run-time derived class declaration */ - case ZEND_DECLARE_INHERITED_CLASS: { - zend_class_entry *ce; - char* class_name; - char* parent_name; - int class_name_length; - namearray_t* children = 0; - int minSize; - - table = CG(class_table); - - /* op2str is a class name of the form "base:derived", - * where derived is the class being declared and base - * is its parent in the class hierarchy. Extract the - * two names into parent_name and class_name. */ - - parent_name = apclib_estrdup(op2str); - if ((class_name = strchr(parent_name, ':')) == 0) { - zend_error(E_CORE_ERROR,"Invalid runtime class entry"); - } - *class_name++ = '\0'; /* advance past ':' */ - - if (zend_hash_find(table, class_name, strlen(class_name)+1, - (void**) &ce) == SUCCESS) + case ZEND_DECLARE_INHERITED_CLASS: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - - /* The parent class hasn't been compiled yet, most likely - * because it is defined in an included file. We must defer - * this inheritance until the parent class is created. Add - * a tuple for this class and its parent to the deferred - * inheritance table */ - - class_name_length = strlen(class_name); - //children = apc_nametable_retrieve(deferred_inheritance, - // parent_name TSRMLS_CC); - - if (children == 0) { - /* Create and initialize a new namearray_t for this - * class's parent and insert into the deferred - * inheritance table. */ - - children = (namearray_t*) malloc(sizeof(namearray_t)); - children->length = 0; - children->size = class_name_length + 1; - children->strings = (char*) malloc(children->size); - - //apc_nametable_insert(deferred_inheritance, - // parent_name, children TSRMLS_CC); - } - - /* a deferred class can't be a top-level parent */ - //apc_nametable_insert(non_inheritors, class_name, NULL TSRMLS_CC); - - - minSize = children->length + class_name_length + 1; - - if (minSize > children->size) { - /* The strings array (children->strings) is not big - * enough to store this class name. Expand the array - * using an exponential resize. */ - - while (minSize > children->size) { - children->size *= 2; + zend_class_entry *ce; + char* class_name; + char* parent_name; + int class_name_length; + namearray_t* children = 0; + int minSize; + + table = CG(class_table); + + /* op2str is a class name of the form "base:derived", + * where derived is the class being declared and base + * is its parent in the class hierarchy. Extract the + * two names into parent_name and class_name. */ + + parent_name = apclib_estrdup(op2str); + if ((class_name = strchr(parent_name, ':')) == 0) { + zend_error(E_CORE_ERROR,"Invalid runtime class entry"); } - children->strings = apclib_erealloc(children->strings, - children->size); - } - memcpy(children->strings + children->length, + *class_name++ = '\0'; /* advance past ':' */ + + if (zend_hash_find(table, class_name, strlen(class_name)+1, + (void**) &ce) == SUCCESS) + { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + + /* The parent class hasn't been compiled yet, most likely + * because it is defined in an included file. We must defer + * this inheritance until the parent class is created. Add + * a tuple for this class and its parent to the deferred + * inheritance table */ + + class_name_length = strlen(class_name); + //children = apc_nametable_retrieve(deferred_inheritance, + // parent_name TSRMLS_CC); + + if (children == 0) { + /* Create and initialize a new namearray_t for this + * class's parent and insert into the deferred + * inheritance table. */ + + children = (namearray_t*) malloc(sizeof(namearray_t)); + children->length = 0; + children->size = class_name_length + 1; + children->strings = (char*) malloc(children->size); + + //apc_nametable_insert(deferred_inheritance, + // parent_name, children TSRMLS_CC); + } + + /* a deferred class can't be a top-level parent */ + //apc_nametable_insert(non_inheritors, class_name, NULL TSRMLS_CC); + + + minSize = children->length + class_name_length + 1; + + if (minSize > children->size) { + /* The strings array (children->strings) is not big + * enough to store this class name. Expand the array + * using an exponential resize. */ + + while (minSize > children->size) { + children->size *= 2; + } + children->strings = apclib_erealloc(children->strings, + children->size); + } + memcpy(children->strings + children->length, class_name, class_name_length + 1); - children->length += class_name_length + 1; - } break; - - default: + children->length += class_name_length + 1; + } + break; + + default: break; } } @@ -2365,9 +2573,9 @@ zoa->brk_cont_array = NULL; if (exists) { zoa->brk_cont_array = (zend_brk_cont_element*) - emalloc(zoa->last_brk_cont * sizeof(zend_brk_cont_element)); + emalloc(zoa->last_brk_cont * sizeof(zend_brk_cont_element)); LOAD_BYTES(zoa->brk_cont_array, zoa->last_brk_cont * - sizeof(zend_brk_cont_element)); + sizeof(zend_brk_cont_element)); } apc_create_hashtable(&zoa->static_variables, apc_create_zval, NULL, sizeof(zval *) TSRMLS_CC); @@ -2400,7 +2608,7 @@ SERIALIZE_SCALAR(zif->type, zend_uchar); apc_serialize_arg_types(zif->arg_types TSRMLS_CC); apc_serialize_string(zif->function_name TSRMLS_CC); - SERIALIZE_SCALAR(zif->handler, void*); + SERIALIZE_SCALAR(zif->handler, void*); } void apc_deserialize_zend_internal_function(zend_internal_function* zif TSRMLS_DC) @@ -2420,7 +2628,7 @@ SERIALIZE_SCALAR(zof->type, zend_uchar); apc_serialize_arg_types(zof->arg_types TSRMLS_CC); apc_serialize_string(zof->function_name TSRMLS_CC); - SERIALIZE_SCALAR(zof->var, zend_uint); + SERIALIZE_SCALAR(zof->var, zend_uint); } void apc_deserialize_zend_overloaded_function(zend_overloaded_function* zof TSRMLS_DC) @@ -2444,19 +2652,20 @@ SERIALIZE_SCALAR(zf->type, zend_uchar); BCOMPILER_DEBUG(("start serialize zend function\n")); - switch(zf->type) { - case ZEND_INTERNAL_FUNCTION: + switch(zf->type) + { + case ZEND_INTERNAL_FUNCTION: apc_serialize_zend_internal_function(&zf->internal_function TSRMLS_CC); break; - case ZEND_OVERLOADED_FUNCTION: + case ZEND_OVERLOADED_FUNCTION: apc_serialize_zend_overloaded_function(&zf->overloaded_function TSRMLS_CC); break; - case ZEND_USER_FUNCTION: - case ZEND_EVAL_CODE: + case ZEND_USER_FUNCTION: + case ZEND_EVAL_CODE: BCOMPILER_DEBUG(("start serialize op array\n")); apc_serialize_zend_op_array(&zf->op_array TSRMLS_CC); break; - default: + default: /* the above are all valid zend_function types. If we hit this * case something has gone very very wrong. */ assert(0); @@ -2467,20 +2676,21 @@ { /* PEEK_SCALAR(&zf->type, zend_uchar); */ DESERIALIZE_SCALAR(&zf->type, zend_uchar); - switch(zf->type) { - case ZEND_INTERNAL_FUNCTION: + switch(zf->type) + { + case ZEND_INTERNAL_FUNCTION: apc_deserialize_zend_internal_function(&zf->internal_function TSRMLS_CC); break; - case ZEND_OVERLOADED_FUNCTION: + case ZEND_OVERLOADED_FUNCTION: apc_deserialize_zend_overloaded_function(&zf->overloaded_function TSRMLS_CC); break; - case ZEND_USER_FUNCTION: - case ZEND_EVAL_CODE: + case ZEND_USER_FUNCTION: + case ZEND_EVAL_CODE: apc_deserialize_zend_op_array(&zf->op_array, 0 TSRMLS_CC); break; - default: + default: /* the above are all valid zend_function types. If we hit this - * case something has gone very very wrong. */ + * case something has gone very very wrong. */ assert(0); } } @@ -2492,9 +2702,10 @@ } void apc_free_zend_function(zend_function** zf TSRMLS_DC) { - if (*zf != NULL) - efree(*zf); - *zf = NULL; + if (*zf != NULL) { + efree(*zf); + } + *zf = NULL; }
Index: pear/PECL/bcompiler/bcompiler.c diff -u pear/PECL/bcompiler/bcompiler.c:1.19 pear/PECL/bcompiler/bcompiler.c:1.20 --- pear/PECL/bcompiler/bcompiler.c:1.19 Sun Dec 15 22:33:30 2002 +++ pear/PECL/bcompiler/bcompiler.c Mon Dec 23 23:42:32 2002 @@ -19,7 +19,7 @@ | Mike Bretz <mike@metropolis-ag.de> | +----------------------------------------------------------------------+ */ -/* $Id: bcompiler.c,v 1.19 2002/12/16 03:33:30 alan_k Exp $ */ +/* $Id: bcompiler.c,v 1.20 2002/12/24 04:42:32 alan_k Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -31,7 +31,7 @@ #include "ext/standard/file.h" #include "php_bcompiler.h" #include "zend.h" - +#include "zend_extensions.h" #include <stdlib.h> /* this should be in php.h #include <assert.h> */ @@ -48,7 +48,7 @@ /* Debugging */ -#define DEBUGLEVEL 0 +#define DEBUGLEVEL 0 #if DEBUGLEVEL #define BCOMPILER_DEBUG(args) (printf("DEBUG: "), printf args) #else @@ -58,6 +58,8 @@ /* we use globals */ ZEND_DECLARE_MODULE_GLOBALS(bcompiler) +extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); +static zend_op_array *(*bcompiler_saved_zend_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC); /* {{{ bcompiler_functions[] @@ -67,7 +69,7 @@ function_entry bcompiler_functions[] = { PHP_FE(bcompiler_write_header, NULL) PHP_FE(bcompiler_write_class, NULL) - PHP_FE(bcompiler_write_function, NULL) + PHP_FE(bcompiler_write_function, NULL) PHP_FE(bcompiler_write_included_filename, NULL) PHP_FE(bcompiler_write_footer, NULL) PHP_FE(bcompiler_write_exe_footer, NULL) @@ -89,8 +91,8 @@ bcompiler_functions, PHP_MINIT(bcompiler), PHP_MSHUTDOWN(bcompiler), - PHP_RINIT(bcompiler), - PHP_RSHUTDOWN(bcompiler), + PHP_RINIT(bcompiler), + PHP_RSHUTDOWN(bcompiler), PHP_MINFO(bcompiler), #if ZEND_MODULE_API_NO >= 20010901 BCOMPILER_VERSION BCOMPILER_STREAMS, /* Replace with version number for your extension */ @@ -103,7 +105,199 @@ ZEND_GET_MODULE(bcompiler) #endif +ZEND_DLEXPORT int bcompiler_zend_startup(zend_extension *extension) +{ + return zend_startup_module(&bcompiler_module_entry); +} + +ZEND_DLEXPORT void bcompiler_zend_shutdown(zend_extension *extension) +{ + +} + +#ifndef ZEND_EXT_API +#define ZEND_EXT_API ZEND_DLEXPORT +#endif +ZEND_EXTENSION(); + +ZEND_DLEXPORT zend_extension zend_extension_entry = { + "bcompiler", + BCOMPILER_VERSION, + "Alan Knowles (php_module), Dan Cowgill and George Schlossnagle", + "http://apc.communityconnect.com, http://www.php.net", + "Copyright (c) 2000-2001 Community Connect Inc. (apc_* functions), 1997-2002 The PHP Group (remaining code)", + bcompiler_zend_startup, + bcompiler_zend_shutdown, + NULL, /* activate_func_t */ + NULL, /* deactivate_func_t */ + NULL, /* message_handler_func_t */ + NULL, /* op_array_handler_func_t */ + NULL, /* statement_handler_func_t */ + NULL, /* fcall_begin_handler_func_t */ + NULL, /* fcall_end_handler_func_t */ + NULL, /* op_array_ctor_func_t */ + NULL, /* op_array_dtor_func_t */ +#ifdef COMPAT_ZEND_EXTENSION_PROPERTIES + NULL, /* api_no_check */ + COMPAT_ZEND_EXTENSION_PROPERTIES +#else + STANDARD_ZEND_EXTENSION_PROPERTIES +#endif +}; + +/* {{{ + */ +zend_op_array *dummy_op_array() { + zval *zstring = NULL; + char *eval_desc = NULL; + zend_op_array *result; + TSRMLS_FETCH(); + MAKE_STD_ZVAL(zstring); + ZVAL_STRING(zstring, "return true;", 1); + eval_desc = zend_make_compiled_string_description("compiled code" TSRMLS_CC); + result = compile_string(zstring, eval_desc TSRMLS_CC); + efree(eval_desc); + zval_dtor(zstring); + return result; +} +/* }}} */ + +/* bad news: bzip2 stream wrapper opens _any_ existing file and treat it as valid stream. + so, we check bzip2 magic string manually ... */ +static int is_bz2_file_handle(FILE *handle) +{ + long pos = 0; + int result = 0; + char *magic = NULL; + + if(handle) { + pos = ftell(handle); + fseek(handle, 0, 0); + magic = (char *) emalloc(3); memset(magic, 0, 3); + fread(magic, 1, 2, handle); + fseek(handle, pos, SEEK_SET); + if(strncmp("BZ", magic, 2) == 0) { + result = 1; + } + efree(magic); + } + return result; +} + +static int is_valid_file_name(char *filename) +{ + int result = 1; + int len = strlen(filename); + if(len) { + result = strncasecmp(filename, "http://", 7); + result &= strncasecmp(filename + len - 1, "-", 1); + } + return result; +} +static void *bz2_stream_open(char *file_name) +{ + int pathlen = 0; + char *path = NULL; + void *stream = NULL; + + TSRMLS_FETCH(); + pathlen = strlen("compress.bzip2://") + strlen(file_name) + 1; + path = emalloc(pathlen); memset(path, 0, pathlen); + strcpy(path, "compress.bzip2://"); + strcat(path, file_name); + stream = php_stream_open_wrapper(path, "r", REPORT_ERRORS, NULL); + efree(path); + return stream; +} + +/* + type + ZEND_EVAL 1<<0 + ZEND_INCLUDE 1<<1 + ZEND_INCLUDE_ONCE 1<<2 + ZEND_REQUIRE 1<<3 + ZEND_REQUIRE_ONCE 1<<4 +*/ +zend_op_array *bcompiler_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) +{ + int test = -1; + void *stream = NULL; + zend_op_array *op_array = NULL; + char *filename = NULL; + FILE *fp; + +#ifdef PHP_HAVE_STREAMS + test = -1; + switch(file_handle->type) + { + case ZEND_HANDLE_FILENAME: /* 0 */ + if(file_handle->opened_path) { + filename = file_handle->opened_path; + } else { + filename = file_handle->filename; + } + + if (is_valid_file_name(filename)) { + fp = zend_fopen(filename, NULL); + if(is_bz2_file_handle(fp)) { + stream = bz2_stream_open(filename); + } + fclose(fp); + } + break; + + case ZEND_HANDLE_FD: /* 1 */ + break; + + case ZEND_HANDLE_FP: /* 2 */ + if(file_handle->opened_path) { + filename = file_handle->opened_path; + } else { + filename = file_handle->filename; + } + if (is_valid_file_name(filename)) { + fp = zend_fopen(filename, NULL); + if(is_bz2_file_handle(fp)) { + stream = bz2_stream_open(filename); + } + fclose(fp); + } + break; + + case ZEND_HANDLE_STDIOSTREAM: /* 3 */ + break; + + case ZEND_HANDLE_FSTREAM: /* 4 */ + break; + } +#else + /* < 4.3 use current file pointer. WARNING!!! bcompiled file must be plain... no compression supported */ + /*TODO if (!file_handle->handle.fp) { fp = try_to_open(file_handle.->file_name); stream = fp; } else */ + stream = file_handle->handle.fp; +#endif + + if(stream) { + BCOMPILERG(stream) = stream; + test = apc_deserialize_magic(TSRMLS_C); + } + + if (test == 0) { + bcompiler_read(TSRMLS_C); + BCOMPILERG(buffer) = (char *) emalloc(1); /* avoid 1 byte memory leak */ + op_array = dummy_op_array(); + } else { + /* no bcompiler magic - just pass it to zend... */ + op_array = bcompiler_saved_zend_compile_file(file_handle, type TSRMLS_CC); + } + +#ifdef PHP_HAVE_STREAMS + if(stream) { + php_stream_close(stream); + } +#endif + return op_array; +} /* {{{ php_bcompiler_init_globals */ @@ -126,6 +320,10 @@ { ZEND_INIT_MODULE_GLOBALS(bcompiler, php_bcompiler_init_globals, NULL); + + bcompiler_saved_zend_compile_file = zend_compile_file; + zend_compile_file = bcompiler_compile_file; + return SUCCESS; } /* }}} */ @@ -134,6 +332,7 @@ */ PHP_MSHUTDOWN_FUNCTION(bcompiler) { + zend_compile_file = bcompiler_saved_zend_compile_file; return SUCCESS; } /* }}} */ @@ -181,8 +380,8 @@ int apclib_alignword_int(int x) { - typedef union { void* p; int i; } word_t; - return sizeof(word_t) * (1 + ((x-1)/sizeof(word_t))); + typedef union { void* p; int i; } word_t; + return sizeof(word_t) * (1 + ((x-1)/sizeof(word_t))); } /* @@ -207,20 +406,20 @@ /* SERIALIZE_SCALAR: write a scalar value to dst */ -#define SERIALIZE_SCALAR(x, type) { \ +#define SERIALIZE_SCALAR(x, type) { \ if (BCOMPILERG(stream) != NULL) { \ - char _buf[255]; \ - BCOMPILER_DEBUG((" scalar: %i\n", (int) x)); \ - *(type*) _buf = x; \ - php_stream_write((php_stream *) BCOMPILERG(stream) , _buf, (size_t) apclib_alignword_int(sizeof(type))); \ + char _buf[255]; \ + BCOMPILER_DEBUG((" scalar: %i\n", (int) x)); \ + *(type*) _buf = x; \ + php_stream_write((php_stream *) BCOMPILERG(stream) , _buf, (size_t) apclib_alignword_int(sizeof(type))); \ } \ } /* DESERIALIZE_SCALAR: read a scalar value from src */ #define DESERIALIZE_SCALAR(xp, type) { \ if (php_stream_eof(BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), sizeof(type) + 1); \ php_stream_read((php_stream *) BCOMPILERG(stream) , BCOMPILERG(buffer) , (size_t) apclib_alignword_int(sizeof(type))); \ @@ -233,8 +432,8 @@ /* STORE_BYTES: memcpy wrapper, writes to dst buffer */ #define STORE_BYTES(bytes, n) { \ if (BCOMPILERG(stream) != NULL) { \ - BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ - php_stream_write((php_stream *) BCOMPILERG(stream) , (char *)bytes, n); \ + BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ + php_stream_write((php_stream *) BCOMPILERG(stream) , (char *)bytes, n); \ } \ } @@ -242,8 +441,8 @@ /* LOAD_BYTES: memcpy wrapper, reads from src buffer */ #define LOAD_BYTES(bytes, n) { \ if (php_stream_eof(BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), n + 1); \ php_stream_read((php_stream *) BCOMPILERG(stream) , BCOMPILERG(buffer) , n); \ @@ -267,18 +466,18 @@ #define SERIALIZE_SCALAR(x, type) { \ if (BCOMPILERG(stream) != NULL) { \ - char _buf[255]; \ - BCOMPILER_DEBUG((" scalar: %i\n",(type) x)); \ - *(type*) _buf = x; \ - fwrite(_buf, 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ + char _buf[255]; \ + BCOMPILER_DEBUG((" scalar: %i\n",(type) x)); \ + *(type*) _buf = x; \ + fwrite(_buf, 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ } \ } /* DESERIALIZE_SCALAR: read a scalar value from src */ #define DESERIALIZE_SCALAR(xp, type) { \ if (feof((FILE*) BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), sizeof(type) + 1); \ fread(BCOMPILERG(buffer) , 1, (size_t) apclib_alignword_int(sizeof(type)), (FILE*) BCOMPILERG(stream)); \ @@ -290,8 +489,8 @@ /* STORE_BYTES: memcpy wrapper, writes to dst buffer */ #define STORE_BYTES(bytes, n) { \ if (BCOMPILERG(stream) != NULL) { \ - BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ - fwrite((char *)bytes, 1, n, (FILE*) BCOMPILERG(stream)); \ + BCOMPILER_DEBUG((" STORE: %s \n",bytes)); \ + fwrite((char *)bytes, 1, n, (FILE*) BCOMPILERG(stream)); \ } \ } @@ -299,8 +498,8 @@ /* LOAD_BYTES: memcpy wrapper, reads from src buffer */ #define LOAD_BYTES(bytes, n) { \ if (feof((FILE*) BCOMPILERG(stream))) { \ - php_error(E_WARNING, "bcompiler: Read Past End of File"); \ - return; \ + php_error(E_WARNING, "bcompiler: Read Past End of File"); \ + return; \ } \ BCOMPILERG(buffer) = (char*) erealloc(BCOMPILERG(buffer), n + 1); \ fread(BCOMPILERG(buffer) ,1, n, (FILE*) BCOMPILERG(stream)); \ @@ -328,13 +527,13 @@ #define CALLBACK_ADD_STRING(str,strlen) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), str, strlen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), str, strlen , 1); \ } \ } #define CALLBACK_ADD_KEY_STRING(key, str,strlen) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), key, str, strlen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), key, str, strlen , 1); \ } \ } @@ -343,12 +542,12 @@ int len; \ len = sprintf(out,str,val); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), out, len , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), BCOMPILERG(callback_key), out, len , 1); \ } \ } #define CALLBACK_ADD_LONG(key, val) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_long(BCOMPILERG(callback_value), key, val); \ + add_assoc_long(BCOMPILERG(callback_value), key, val); \ } \ } #define CALLBACK_ADD_SUB_LONG(kkey, val) { \ @@ -356,7 +555,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_long(BCOMPILERG(callback_value), out, val); \ + add_assoc_long(BCOMPILERG(callback_value), out, val); \ } \ } #define CALLBACK_ADD_SUB_DOUBLE(kkey, val) { \ @@ -364,7 +563,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_double(BCOMPILERG(callback_value), out, val); \ + add_assoc_double(BCOMPILERG(callback_value), out, val); \ } \ } #define CALLBACK_ADD_SUB_STRING(kkey, sval,slen) { \ @@ -372,7 +571,7 @@ strcpy(out, BCOMPILERG(callback_key)); \ sprintf(out,"%s.%s",out, kkey); \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_stringl(BCOMPILERG(callback_value), out, sval ,slen , 1); \ + add_assoc_stringl(BCOMPILERG(callback_value), out, sval ,slen , 1); \ } \ } @@ -380,7 +579,7 @@ #define CALLBACK_ADD_DOUBLE(key,val) { \ if (BCOMPILERG(callback) != NULL) { \ - add_assoc_double(BCOMPILERG(callback_value), key, val); \ + add_assoc_double(BCOMPILERG(callback_value), key, val); \ } \ } @@ -388,25 +587,25 @@ /* CALLBACK START (opcode) */ #define CALLBACK_START(code) { \ if (BCOMPILERG(callback) != NULL) { \ - char *str; \ - MAKE_STD_ZVAL(BCOMPILERG(callback_value)); \ - array_init(BCOMPILERG(callback_value)); \ - /*Optype - */ \ - str = (char *) getOpcodeName(code ); \ - add_assoc_long(BCOMPILERG(callback_value), "OPCODE", code); \ - add_assoc_stringl(BCOMPILERG(callback_value), "OPCODE_STR", str, strlen(str) , 1); \ + char *str; \ + MAKE_STD_ZVAL(BCOMPILERG(callback_value)); \ + array_init(BCOMPILERG(callback_value)); \ + /*Optype - */ \ + str = (char *) getOpcodeName(code ); \ + add_assoc_long(BCOMPILERG(callback_value), "OPCODE", code); \ + add_assoc_stringl(BCOMPILERG(callback_value), "OPCODE_STR", str, strlen(str) , 1); \ } \ } #define CALLBACK_END() { \ zval myretval; \ - zval *sendval; \ + zval *sendval; \ if (BCOMPILERG(callback) != NULL) { \ - sendval = BCOMPILERG(callback_value); \ - zval_copy_ctor(sendval); \ - if(!call_user_function( EG(function_table), NULL, BCOMPILERG(callback), &myretval, 1, &sendval TSRMLS_CC )) { \ - zval_dtor(BCOMPILERG(callback_value)); \ - } \ + sendval = BCOMPILERG(callback_value); \ + zval_copy_ctor(sendval); \ + if(!call_user_function( EG(function_table), NULL, BCOMPILERG(callback), &myretval, 1, &sendval TSRMLS_CC )) { \ + zval_dtor(BCOMPILERG(callback_value)); \ + } \ } \ } @@ -440,7 +639,7 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + zend_str_tolower(class_name, class_len); @@ -509,13 +708,13 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + zend_str_tolower(function_name, function_len); zend_hash_find(CG(function_table), function_name, function_len+1, (void **)&fe); if (!fe) { - php_error(E_WARNING, "Could not find function"); + php_error(E_WARNING, "Could not find function"); RETURN_NULL(); } BCOMPILERG(stream) = stream; @@ -598,7 +797,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -623,14 +822,14 @@ Writes the end zero*/ PHP_FUNCTION(bcompiler_write_footer) { - + zval *rsrc; void *stream; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -662,7 +861,7 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &rsrc, &start) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else @@ -691,19 +890,19 @@ int test =0; void *stream; - + zend_class_entry *ce = NULL; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &rsrc) == FAILURE) { return; } - + #ifdef PHP_HAVE_STREAMS ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_stream()); #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; @@ -712,7 +911,7 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } BCOMPILER_DEBUG(("STARTING CLASS\n")); bcompiler_read( TSRMLS_C); @@ -776,8 +975,8 @@ #else ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - - + + BCOMPILERG(stream) = stream; @@ -785,8 +984,8 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); - return; - + return; + } bcompiler_read( TSRMLS_C); @@ -849,7 +1048,7 @@ MAKE_STD_ZVAL(zmode); ZVAL_STRING(zmode, "r", 1); args[1] = &zmode; - + call_user_function_ex(CG(function_table), NULL, zfopen, &rsrc, 2, args, 0, NULL TSRMLS_CC ); #ifdef PHP_HAVE_STREAMS @@ -858,7 +1057,7 @@ ZEND_FETCH_RESOURCE(stream, void *, &rsrc, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; pos = (long) ((strlen(BCOMPILER_MAGIC_HEADER)+4) * -1); // +4 cause we need to read the string size BCOMPILER_DEBUG(("move to start: %d\n", pos)); @@ -874,13 +1073,13 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } /* now read the length */ pos = pos - 4; BCOMPILER_DEBUG(("move to start: %d\n", pos)); - + #ifdef PHP_HAVE_STREAMS php_stream_seek(stream, pos , SEEK_END); #else @@ -919,7 +1118,7 @@ ZEND_FETCH_RESOURCE(stream, void *, &rsrc2, -1, "File-Handle", php_file_le_fopen()); #endif - + BCOMPILERG(stream) = stream; @@ -928,10 +1127,10 @@ if (test != 0) { php_error(E_WARNING, "Could not find Magic header in stream"); return; - + } bcompiler_read( TSRMLS_C); - + #ifdef PHP_HAVE_STREAMS php_stream_close(stream); #else @@ -958,9 +1157,9 @@ DESERIALIZE_SCALAR(&item, char); while (item) { - + switch (item) { - + case BCOMPILER_CLASS_ENTRY: BCOMPILER_DEBUG(("STARTING CLASS\n")); apc_create_zend_class_entry(&zc TSRMLS_CC); @@ -1001,7 +1200,7 @@ } break; - } + } DESERIALIZE_SCALAR(&item, char); } @@ -1011,6 +1210,8 @@ + + /* ---------------------------------------------------------------------------- * * from apc_lib.h -- GENERALLY STUFF that doesnt need GLOBALS! @@ -1056,10 +1257,11 @@ void apclib_free_string(char **string) { - if (*string == NULL) - return; - efree(*string); - *string = NULL; + if (*string == NULL) { + return; + } + efree(*string); + *string = NULL; } @@ -1076,9 +1278,9 @@ typedef struct namearray_t namearray_t; struct namearray_t { - char* strings; /* array of null-terminated names */ - int length; /* logical size of strings */ - int size; /* physical size of strings */ + char* strings; /* array of null-terminated names */ + int length; /* logical size of strings */ + int size; /* physical size of strings */ }; @@ -1207,7 +1409,7 @@ "EXT_NOP", /* 104 */ "TICKS", /* 105 */ "SEND_VAR_NO_REF", /* 106 */ - + "ZEND_CATCH", /* 107 */ "ZEND_THROW", /* 108 */ "ZEND_FETCH_CLASS", /* 109 */ @@ -1242,7 +1444,7 @@ "ZEND_ASSIGN_OBJ", /* 136 */ "ZEND_MAKE_VAR", /* 137 */ - + "BEGIN_HASH_DEF", /* 138 */ @@ -1260,7 +1462,7 @@ const char* getOpcodeName(int op) { if (op < 0 || op >= NUM_OPCODES) { - return "(unknown)"; + return "(unknown)"; } return opcodes[op]; } @@ -1292,7 +1494,7 @@ BCOMPILER_DEBUG(("base: %s",base->name)); if (!base->parent) { - return; // 'parent' has no children, nothing to do + return; // 'parent' has no children, nothing to do } child = base->parent; @@ -1342,30 +1544,30 @@ void apc_serialize_zstring(char* string, int len TSRMLS_DC) { - /* by convention, mark null strings with a length of -1 */ - if (len <= 0 || string == NULL) { - SERIALIZE_SCALAR(-1, int); - return; - } -// len = strlen(string); - SERIALIZE_SCALAR(len, int); - STORE_BYTES(string, len); + /* by convention, mark null strings with a length of -1 */ + if (len <= 0 || string == NULL) { + SERIALIZE_SCALAR(-1, int); + return; + } + // len = strlen(string); + SERIALIZE_SCALAR(len, int); + STORE_BYTES(string, len); } void apc_create_string(char** string TSRMLS_DC) { - int len = 0; + int len = 0; - DESERIALIZE_SCALAR(&len, int); - if (len <= 0) { - /* by convention, empty marked strings (len is -1) are returned as an empty string */ - *string = (char*) emalloc(1); - (*string)[0] = '\0'; - return; - } - *string = (char*) emalloc(len + 1); - LOAD_BYTES(*string, len); - (*string)[len] = '\0'; + DESERIALIZE_SCALAR(&len, int); + if (len <= 0) { + /* by convention, empty marked strings (len is -1) are returned as an empty string */ + *string = (char*) emalloc(1); + (*string)[0] = '\0'; + return; + } + *string = (char*) emalloc(len + 1); + LOAD_BYTES(*string, len); + (*string)[len] = '\0'; } void apc_serialize_arg_types(zend_uchar* arg_types TSRMLS_DC) @@ -1437,7 +1639,7 @@ SERIALIZE_SCALAR(list->persistent, unsigned char); SERIALIZE_SCALAR(zend_llist_count(list), int); zend_llist_apply_with_argument(list, store_zend_llist_element, - &list->size TSRMLS_CC); + &list->size TSRMLS_CC); } void apc_deserialize_zend_llist(zend_llist* list TSRMLS_DC) @@ -1508,12 +1710,12 @@ void apc_serialize_hashtable(HashTable* ht, void* funcptr TSRMLS_DC) { - char exists; /* for identifying null lists */ + char exists; /* for identifying null lists */ Bucket* p; void (*serialize_bucket)(void* TSRMLS_DC); - + serialize_bucket = (void(*)(void* TSRMLS_DC)) funcptr; exists = (ht != NULL) ? 1 : 0; @@ -1523,7 +1725,7 @@ } - + /* Serialize the hash meta-data. */ SERIALIZE_SCALAR(ht->nTableSize, uint); SERIALIZE_SCALAR(ht->pDestructor, void*); @@ -1589,54 +1791,50 @@ * we can just deserialize that many hashtable elements. */ for (j = 0; j < (int) nNumOfElements; j++) { - DESERIALIZE_SCALAR(&h, ulong); - DESERIALIZE_SCALAR(&nKeyLength, uint); - apc_create_string(&arKey TSRMLS_CC); - deserialize_bucket(&pData TSRMLS_CC); - - /* If nKeyLength is non-zero, this element is a hashed key/value - * pair. Otherwise, it is an array element with a numeric index */ - BCOMPILER_DEBUG(("-----------------------------\nFUNC %s\n",arKey)); - - if (nKeyLength != 0) { - if(datasize == sizeof(void*)) { - zend_hash_add_or_update(ht, arKey, nKeyLength, &pData, - datasize, NULL, HASH_ADD); - } - else { - zend_hash_add_or_update(ht, arKey, nKeyLength, pData, - datasize, NULL, HASH_ADD); - } + DESERIALIZE_SCALAR(&h, ulong); + DESERIALIZE_SCALAR(&nKeyLength, uint); + apc_create_string(&arKey TSRMLS_CC); + deserialize_bucket(&pData TSRMLS_CC); + + /* If nKeyLength is non-zero, this element is a hashed key/value + * pair. Otherwise, it is an array element with a numeric index */ + BCOMPILER_DEBUG(("-----------------------------\nFUNC %s\n",arKey)); + + if (nKeyLength != 0) { + if(datasize == sizeof(void*)) { + zend_hash_add_or_update(ht, arKey, nKeyLength, &pData, + datasize, NULL, HASH_ADD); + } else { + zend_hash_add_or_update(ht, arKey, nKeyLength, pData, + datasize, NULL, HASH_ADD); } - else { /* numeric index, not key string */ - if(datasize == sizeof(void*)) { - zend_hash_index_update(ht, h, &pData, datasize, NULL); - } - else { - zend_hash_index_update(ht, h, pData, datasize, NULL); - } - } - if (dptr != NULL) { - free_bucket(&pData); + } else { /* numeric index, not key string */ + if(datasize == sizeof(void*)) { + zend_hash_index_update(ht, h, &pData, datasize, NULL); + } else { + zend_hash_index_update(ht, h, pData, datasize, NULL); } - // else { - // BCOMPILER_DEBUG((stderr, "Do not free: %s\n", arKey)); - // } - apclib_free_string(&arKey); // ADDED BY MPB + } + if (dptr != NULL) { + free_bucket(&pData); + } + // else { + // BCOMPILER_DEBUG((stderr, "Do not free: %s\n", arKey)); + // } + apclib_free_string(&arKey); // ADDED BY MPB } } void apc_create_hashtable(HashTable** ht, void* funcptr, void* dptr, int datasize TSRMLS_DC) { - char exists; /* for identifying null hashtables */ + char exists; /* for identifying null hashtables */ /*PEEK_SCALAR(&exists, char); */ DESERIALIZE_SCALAR(&exists, char); if (exists==1) { *ht = (HashTable*) emalloc(sizeof(HashTable)); apc_deserialize_hashtable(*ht, funcptr, dptr, datasize,exists TSRMLS_CC); - } - else { + } else { /* DESERIALIZE_SCALAR(&exists, char); */ *ht = 0; } @@ -1651,44 +1849,45 @@ * determine exactly what it's type is, then serialize the * appropriate structure. */ CALLBACK_ADD_SUB_LONG("u.constant.type",type); - switch (type) { - case IS_RESOURCE: - case IS_BOOL: - case IS_LONG: + switch (type) + { + case IS_RESOURCE: + case IS_BOOL: + case IS_LONG: CALLBACK_ADD_SUB_LONG("u.constant.val",zv->lval); CALLBACK_ADD_LONG(BCOMPILERG(callback_key),zv->lval); SERIALIZE_SCALAR(zv->lval, long); - break; - case IS_DOUBLE: + break; + case IS_DOUBLE: CALLBACK_ADD_SUB_DOUBLE("u.constant.val",zv->dval); CALLBACK_ADD_DOUBLE(BCOMPILERG(callback_key),zv->dval); SERIALIZE_SCALAR(zv->dval, double); break; - case IS_NULL: + case IS_NULL: /* null value, do nothing */ break; - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: CALLBACK_ADD_SUB_STRING("u.constant.val",zv->str.val, zv->str.len); CALLBACK_ADD_STRING(zv->str.val, zv->str.len); apc_serialize_zstring(zv->str.val, zv->str.len TSRMLS_CC); SERIALIZE_SCALAR(zv->str.len, int); break; - case IS_ARRAY: + case IS_ARRAY: CALLBACK_ADD_STRING("ARRAY?", 6); apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC); break; - case IS_CONSTANT_ARRAY: + case IS_CONSTANT_ARRAY: CALLBACK_ADD_STRING("CARRAY?", 7); apc_serialize_hashtable(zv->ht, apc_serialize_zval_ptr TSRMLS_CC); break; - case IS_OBJECT: + case IS_OBJECT: CALLBACK_ADD_STRING("OBJECT?", 7); apc_serialize_zend_class_entry(zv->obj.ce TSRMLS_CC); apc_serialize_hashtable(zv->obj.properties, apc_serialize_zval_ptr TSRMLS_CC); break; - default: + default: /* The above list enumerates all types. If we get here, * something very very bad has happened. */ assert(0); @@ -1699,39 +1898,40 @@ { /* We peeked ahead in the calling routine to deserialize the * type. Now we just deserialize. */ - switch(type) { - case IS_RESOURCE: - case IS_BOOL: - case IS_LONG: + switch(type) + { + case IS_RESOURCE: + case IS_BOOL: + case IS_LONG: DESERIALIZE_SCALAR(&zv->lval, int); break; - case IS_NULL: + case IS_NULL: /* null value, do nothing */ break; - case IS_DOUBLE: + case IS_DOUBLE: DESERIALIZE_SCALAR(&zv->dval, double); break; - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: apc_create_string(&zv->str.val TSRMLS_CC); DESERIALIZE_SCALAR(&zv->str.len, int); break; - case IS_ARRAY: + case IS_ARRAY: apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC); break; - case IS_CONSTANT_ARRAY: + case IS_CONSTANT_ARRAY: apc_create_hashtable(&zv->ht, apc_create_zval, NULL, sizeof(void*) TSRMLS_CC); break; - case IS_OBJECT: + case IS_OBJECT: apc_create_zend_class_entry(&zv->obj.ce TSRMLS_CC); apc_create_hashtable(&zv->obj.properties, apc_create_zval, NULL, sizeof(zval *) TSRMLS_CC); break; - default: - /* The above list enumerates all types. If we get here, - * something very very bad has happened. */ + default: + /* The above list enumerates all types. If we get here, + * something very very bad has happened. */ assert(0); - } + } } @@ -1769,17 +1969,18 @@ } void apc_free_zval(zval** zv TSRMLS_DC) { - if (*zv != NULL) { - switch((*zv)->type) { - case IS_CONSTANT: - case IS_STRING: - case FLAG_IS_BC: - apclib_free_string(&(*zv)->value.str.val); - break; - } - efree(*zv); - } - *zv = NULL; + if (*zv != NULL) { + switch((*zv)->type) + { + case IS_CONSTANT: + case IS_STRING: + case FLAG_IS_BC: + apclib_free_string(&(*zv)->value.str.val); + break; + } + efree(*zv); + } + *zv = NULL; } @@ -1854,8 +2055,8 @@ NOT::call,set etc: - would these point to a 'method?' !! ZEND_DECLARE_FUNCTION_OR_CLASS */ - - + + zend_function_entry* zfe; int count, i, exists; @@ -1878,7 +2079,6 @@ if (exists) { /*Parent*/ - apc_serialize_zstring(zce->parent->name, zce->parent->name_length TSRMLS_CC); } @@ -1938,8 +2138,8 @@ // manually inheriting methods from the parent (PHP will perform // the inheritance). - char* parent_name; // name of parent class - + char* parent_name; // name of parent class + apc_create_string(&parent_name TSRMLS_CC); /*if ((zend_hash_find(CG(class_table), parent_name, @@ -1958,13 +2158,13 @@ zce->refcount = (int*) emalloc(sizeof(int)); DESERIALIZE_SCALAR(zce->refcount, int); DESERIALIZE_SCALAR(&zce->constants_updated, zend_bool); - BCOMPILER_DEBUG(("-----------------------------\nFUNC TABLE:\n")); - DESERIALIZE_SCALAR(&exists, char); + BCOMPILER_DEBUG(("-----------------------------\nFUNC TABLE:\n")); + DESERIALIZE_SCALAR(&exists, char); apc_deserialize_hashtable(&zce->function_table, (void*) apc_create_zend_function, (void*) apc_free_zend_function, - (int) sizeof(zend_function) , (char) exists TSRMLS_CC); + (int) sizeof(zend_function) , (char) exists TSRMLS_CC); BCOMPILER_DEBUG(("-----------------------------\nDEFAYLT PROPS:\n")); - DESERIALIZE_SCALAR(&exists, char); + DESERIALIZE_SCALAR(&exists, char); apc_deserialize_hashtable(&zce->default_properties, (void*) apc_create_zval, (void*) NULL, (int) sizeof(zval *), (char)exists TSRMLS_CC); // see apc_serialize_zend_class_entry() for a description of the @@ -1974,7 +2174,7 @@ zce->builtin_functions = NULL; if (count > 0) { zce->builtin_functions = (zend_function_entry*) - emalloc((count+1) * sizeof(zend_function_entry)); + emalloc((count+1) * sizeof(zend_function_entry)); zce->builtin_functions[count].fname = NULL; for (i = 0; i < count; i++) { apc_deserialize_zend_function_entry(&zce->builtin_functions[i] TSRMLS_CC); @@ -2000,7 +2200,7 @@ */ /* if (zce->parent) { - bcompiler_inherit(zce TSRMLS_CC); + bcompiler_inherit(zce TSRMLS_CC); } */ } @@ -2028,37 +2228,37 @@ * a bitwise copy. */ CALLBACK_ADD_SUB_LONG("op_type" ,(long) zn->op_type); CALLBACK_ADD_SUB_LONG("u.fetch_type" ,(int) zn->u.fetch_type); - CALLBACK_ADD_SUB_LONG("u.EA.type" ,(int) zn->u.EA.type); - + CALLBACK_ADD_SUB_LONG("u.EA.type" ,(int) zn->u.EA.type); + switch(zn->op_type) { - case IS_CONST: - if (&zn->u.fetch_type == ZEND_FETCH_GLOBAL) { - CALLBACK_ADD_STRING("**GLOBAL",8); - } - apc_serialize_zval(&zn->u.constant TSRMLS_CC); - break; - - case IS_VAR: - CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); - CALLBACK_ADD_STRING_LONG("VAR:%i",(long) zn->u.var); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - case IS_TMP_VAR: - CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); - CALLBACK_ADD_STRING_LONG("TMPVAR:%i",(long) zn->u.var); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - case IS_UNUSED: - /* CALLBACK_ADD_STRING("**NOTUSED",9); */ - CALLBACK_ADD_SUB_LONG("u.opline_num" ,(long) zn->u.opline_num); - - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; - - default: - CALLBACK_ADD_STRING_LONG("**UNKNOWN",(long) sizeof(zn->u)); - STORE_BYTES(&zn->u, sizeof(zn->u)); - break; + case IS_CONST: + if (&zn->u.fetch_type == ZEND_FETCH_GLOBAL) { + CALLBACK_ADD_STRING("**GLOBAL",8); + } + apc_serialize_zval(&zn->u.constant TSRMLS_CC); + break; + + case IS_VAR: + CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); + CALLBACK_ADD_STRING_LONG("VAR:%i",(long) zn->u.var); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + case IS_TMP_VAR: + CALLBACK_ADD_SUB_LONG("u.var" ,(long) zn->u.var); + CALLBACK_ADD_STRING_LONG("TMPVAR:%i",(long) zn->u.var); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + case IS_UNUSED: + /* CALLBACK_ADD_STRING("**NOTUSED",9); */ + CALLBACK_ADD_SUB_LONG("u.opline_num" ,(long) zn->u.opline_num); + + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; + + default: + CALLBACK_ADD_STRING_LONG("**UNKNOWN",(long) sizeof(zn->u)); + STORE_BYTES(&zn->u, sizeof(zn->u)); + break; } } @@ -2070,11 +2270,12 @@ * otherwise, it is too case-dependent (or inscrutable), so we do * a bitwise copy. */ - switch(zn->op_type) { - case IS_CONST: + switch(zn->op_type) + { + case IS_CONST: apc_deserialize_zval(&zn->u.constant TSRMLS_CC); break; - default: + default: LOAD_BYTES(&zn->u, sizeof(zn->u)); break; } @@ -2092,13 +2293,13 @@ SERIALIZE_SCALAR(zo->opcode, zend_uchar); BCOMPILERG(callback_key)="RESULT"; - apc_serialize_znode(&zo->result TSRMLS_CC); + apc_serialize_znode(&zo->result TSRMLS_CC); BCOMPILERG(callback_key)="OP1"; - apc_serialize_znode(&zo->op1 TSRMLS_CC); + apc_serialize_znode(&zo->op1 TSRMLS_CC); BCOMPILERG(callback_key)="OP2"; - apc_serialize_znode(&zo->op2 TSRMLS_CC); + apc_serialize_znode(&zo->op2 TSRMLS_CC); SERIALIZE_SCALAR(zo->extended_value, ulong); if (zo->extended_value != 0) { @@ -2106,18 +2307,19 @@ } CALLBACK_ADD_LONG("LINE",(long) id); /* add some useful info! */ - switch (zo->opcode) { - case ZEND_JMP: - CALLBACK_ADD_LONG("JMP",zo->op1.u.opline_num); - break; - case ZEND_JMPZ: - CALLBACK_ADD_LONG("JMP",zo->op2.u.opline_num); - break; + switch (zo->opcode) + { + case ZEND_JMP: + CALLBACK_ADD_LONG("JMP",zo->op1.u.opline_num); + break; + case ZEND_JMPZ: + CALLBACK_ADD_LONG("JMP",zo->op2.u.opline_num); + break; } CALLBACK_END(); - SERIALIZE_SCALAR(zo->lineno, uint); + SERIALIZE_SCALAR(zo->lineno, uint); } void apc_deserialize_zend_op(zend_op* zo TSRMLS_DC) @@ -2171,7 +2373,7 @@ CALLBACK_ADD_LONG("USES_GLOBALS",(long) zoa->uses_globals); CALLBACK_END(); for (i = 0; i < (int) zoa->last; i++) { - + apc_serialize_zend_op(i, &zoa->opcodes[i] TSRMLS_CC); } @@ -2223,134 +2425,140 @@ apc_deserialize_zend_op(&zoa->opcodes[i] TSRMLS_CC); if(zoa->opcodes[i].opcode == ZEND_DECLARE_FUNCTION_OR_CLASS) { HashTable* table; - char* op2str; /* op2str and op2len are for convenience */ + char* op2str; /* op2str and op2len are for convenience */ int op2len; - + exists = 1; - + op2str = zoa->opcodes[i].op2.u.constant.value.str.val; op2len = zoa->opcodes[i].op2.u.constant.value.str.len; - - switch(zoa->opcodes[i].extended_value) { - /* a run-time function declaration */ - case ZEND_DECLARE_FUNCTION: { - zend_function* function; - - table = CG(function_table); - - if (zend_hash_find(table, op2str, op2len + 1, - (void**) &function) == SUCCESS) + + switch(zoa->opcodes[i].extended_value) + { + /* a run-time function declaration */ + case ZEND_DECLARE_FUNCTION: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - } break; - - /* a run-time class declaration */ - case ZEND_DECLARE_CLASS: { - zend_class_entry *ce; - - table = CG(class_table); - - if (zend_hash_find(table, op2str, op2len + 1, - (void**) &ce) == SUCCESS) + zend_function* function; + + table = CG(function_table); + + if (zend_hash_find(table, op2str, op2len + 1, + (void**) &function) == SUCCESS) + { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + } + break; + + /* a run-time class declaration */ + case ZEND_DECLARE_CLASS: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - } break; + zend_class_entry *ce; + + table = CG(class_table); + + if (zend_hash_find(table, op2str, op2len + 1, + (void**) &ce) == SUCCESS) { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + } + break; /* a run-time derived class declaration */ - case ZEND_DECLARE_INHERITED_CLASS: { - zend_class_entry *ce; - char* class_name; - char* parent_name; - int class_name_length; - namearray_t* children = 0; - int minSize; - - table = CG(class_table); - - /* op2str is a class name of the form "base:derived", - * where derived is the class being declared and base - * is its parent in the class hierarchy. Extract the - * two names into parent_name and class_name. */ - - parent_name = apclib_estrdup(op2str); - if ((class_name = strchr(parent_name, ':')) == 0) { - zend_error(E_CORE_ERROR,"Invalid runtime class entry"); - } - *class_name++ = '\0'; /* advance past ':' */ - - if (zend_hash_find(table, class_name, strlen(class_name)+1, - (void**) &ce) == SUCCESS) + case ZEND_DECLARE_INHERITED_CLASS: { - zval_dtor(&zoa->opcodes[i].op1.u.constant); - zval_dtor(&zoa->opcodes[i].op2.u.constant); - zoa->opcodes[i].opcode = ZEND_NOP; - memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); - memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); - zoa->opcodes[i].op1.op_type = IS_UNUSED; - zoa->opcodes[i].op2.op_type = IS_UNUSED; - } - - /* The parent class hasn't been compiled yet, most likely - * because it is defined in an included file. We must defer - * this inheritance until the parent class is created. Add - * a tuple for this class and its parent to the deferred - * inheritance table */ - - class_name_length = strlen(class_name); - //children = apc_nametable_retrieve(deferred_inheritance, - // parent_name TSRMLS_CC); - - if (children == 0) { - /* Create and initialize a new namearray_t for this - * class's parent and insert into the deferred - * inheritance table. */ - - children = (namearray_t*) malloc(sizeof(namearray_t)); - children->length = 0; - children->size = class_name_length + 1; - children->strings = (char*) malloc(children->size); - - //apc_nametable_insert(deferred_inheritance, - // parent_name, children TSRMLS_CC); - } - - /* a deferred class can't be a top-level parent */ - //apc_nametable_insert(non_inheritors, class_name, NULL TSRMLS_CC); - - - minSize = children->length + class_name_length + 1; - - if (minSize > children->size) { - /* The strings array (children->strings) is not big - * enough to store this class name. Expand the array - * using an exponential resize. */ - - while (minSize > children->size) { - children->size *= 2; + zend_class_entry *ce; + char* class_name; + char* parent_name; + int class_name_length; + namearray_t* children = 0; + int minSize; + + table = CG(class_table); + + /* op2str is a class name of the form "base:derived", + * where derived is the class being declared and base + * is its parent in the class hierarchy. Extract the + * two names into parent_name and class_name. */ + + parent_name = apclib_estrdup(op2str); + if ((class_name = strchr(parent_name, ':')) == 0) { + zend_error(E_CORE_ERROR,"Invalid runtime class entry"); } - children->strings = apclib_erealloc(children->strings, - children->size); - } - memcpy(children->strings + children->length, + *class_name++ = '\0'; /* advance past ':' */ + + if (zend_hash_find(table, class_name, strlen(class_name)+1, + (void**) &ce) == SUCCESS) + { + zval_dtor(&zoa->opcodes[i].op1.u.constant); + zval_dtor(&zoa->opcodes[i].op2.u.constant); + zoa->opcodes[i].opcode = ZEND_NOP; + memset(&zoa->opcodes[i].op1, 0, sizeof(znode)); + memset(&zoa->opcodes[i].op2, 0, sizeof(znode)); + zoa->opcodes[i].op1.op_type = IS_UNUSED; + zoa->opcodes[i].op2.op_type = IS_UNUSED; + } + + /* The parent class hasn't been compiled yet, most likely + * because it is defined in an included file. We must defer + * this inheritance until the parent class is created. Add + * a tuple for this class and its parent to the deferred + * inheritance table */ + + class_name_length = strlen(class_name); + //children = apc_nametable_retrieve(deferred_inheritance, + // parent_name TSRMLS_CC); + + if (children == 0) { + /* Create and initialize a new namearray_t for this + * class's parent and insert into the deferred + * inheritance table. */ + + children = (namearray_t*) malloc(sizeof(namearray_t)); + children->length = 0; + children->size = class_name_length + 1; + children->strings = (char*) malloc(children->size); + + //apc_nametable_insert(deferred_inheritance, + // parent_name, children TSRMLS_CC); + } + + /* a deferred class can't be a top-level parent */ + //apc_nametable_insert(non_inheritors, class_name, NULL TSRMLS_CC); + + + minSize = children->length + class_name_length + 1; + + if (minSize > children->size) { + /* The strings array (children->strings) is not big + * enough to store this class name. Expand the array + * using an exponential resize. */ + + while (minSize > children->size) { + children->size *= 2; + } + children->strings = apclib_erealloc(children->strings, + children->size); + } + memcpy(children->strings + children->length, class_name, class_name_length + 1); - children->length += class_name_length + 1; - } break; - - default: + children->length += class_name_length + 1; + } + break; + + default: break; } } @@ -2365,9 +2573,9 @@ zoa->brk_cont_array = NULL; if (exists) { zoa->brk_cont_array = (zend_brk_cont_element*) - emalloc(zoa->last_brk_cont * sizeof(zend_brk_cont_element)); + emalloc(zoa->last_brk_cont * sizeof(zend_brk_cont_element)); LOAD_BYTES(zoa->brk_cont_array, zoa->last_brk_cont * - sizeof(zend_brk_cont_element)); + sizeof(zend_brk_cont_element)); } apc_create_hashtable(&zoa->static_variables, apc_create_zval, NULL, sizeof(zval *) TSRMLS_CC); @@ -2400,7 +2608,7 @@ SERIALIZE_SCALAR(zif->type, zend_uchar); apc_serialize_arg_types(zif->arg_types TSRMLS_CC); apc_serialize_string(zif->function_name TSRMLS_CC); - SERIALIZE_SCALAR(zif->handler, void*); + SERIALIZE_SCALAR(zif->handler, void*); } void apc_deserialize_zend_internal_function(zend_internal_function* zif TSRMLS_DC) @@ -2420,7 +2628,7 @@ SERIALIZE_SCALAR(zof->type, zend_uchar); apc_serialize_arg_types(zof->arg_types TSRMLS_CC); apc_serialize_string(zof->function_name TSRMLS_CC); - SERIALIZE_SCALAR(zof->var, zend_uint); + SERIALIZE_SCALAR(zof->var, zend_uint); } void apc_deserialize_zend_overloaded_function(zend_overloaded_function* zof TSRMLS_DC) @@ -2444,19 +2652,20 @@ SERIALIZE_SCALAR(zf->type, zend_uchar); BCOMPILER_DEBUG(("start serialize zend function\n")); - switch(zf->type) { - case ZEND_INTERNAL_FUNCTION: + switch(zf->type) + { + case ZEND_INTERNAL_FUNCTION: apc_serialize_zend_internal_function(&zf->internal_function TSRMLS_CC); break; - case ZEND_OVERLOADED_FUNCTION: + case ZEND_OVERLOADED_FUNCTION: apc_serialize_zend_overloaded_function(&zf->overloaded_function TSRMLS_CC); break; - case ZEND_USER_FUNCTION: - case ZEND_EVAL_CODE: + case ZEND_USER_FUNCTION: + case ZEND_EVAL_CODE: BCOMPILER_DEBUG(("start serialize op array\n")); apc_serialize_zend_op_array(&zf->op_array TSRMLS_CC); break; - default: + default: /* the above are all valid zend_function types. If we hit this * case something has gone very very wrong. */ assert(0); @@ -2467,20 +2676,21 @@ { /* PEEK_SCALAR(&zf->type, zend_uchar); */ DESERIALIZE_SCALAR(&zf->type, zend_uchar); - switch(zf->type) { - case ZEND_INTERNAL_FUNCTION: + switch(zf->type) + { + case ZEND_INTERNAL_FUNCTION: apc_deserialize_zend_internal_function(&zf->internal_function TSRMLS_CC); break; - case ZEND_OVERLOADED_FUNCTION: + case ZEND_OVERLOADED_FUNCTION: apc_deserialize_zend_overloaded_function(&zf->overloaded_function TSRMLS_CC); break; - case ZEND_USER_FUNCTION: - case ZEND_EVAL_CODE: + case ZEND_USER_FUNCTION: + case ZEND_EVAL_CODE: apc_deserialize_zend_op_array(&zf->op_array, 0 TSRMLS_CC); break; - default: + default: /* the above are all valid zend_function types. If we hit this - * case something has gone very very wrong. */ + * case something has gone very very wrong. */ assert(0); } } @@ -2492,9 +2702,10 @@ } void apc_free_zend_function(zend_function** zf TSRMLS_DC) { - if (*zf != NULL) - efree(*zf); - *zf = NULL; + if (*zf != NULL) { + efree(*zf); + } + *zf = NULL; }