def zend_register_constant(c): # TODO: make it correct name = c.name import global_state return happy_hash.zend_hash_add(global_state.EG.zend_constants, name, name.length(), zend_constant_ptr(c, False), 0, const_null_ptr_ptr)
def hash_add(ptr, ikey, skey, key_is_str, ptr2): ht = _cast_ptr_to_ht(ptr) zval = _cast_ptr_to_zval(ptr2) if not zval: zval = zval_utils.make_empty_zval() zval_ptr = zval_utils.zp_stack(zval) zval_ptr_ptr = zval_utils.zpp_stack(zval_ptr) retval_ptr = zval_utils.zppp_stack(global_state.null_zval_ptr_ptr) # TODO: ADDREF on zval??? if key_is_str: ms = objects.MutableString.from_string_data(skey) res = happy_hash.zend_hash_add(ht, ms, 1, zval_ptr_ptr, 0, retval_ptr) else: res = happy_hash.zend_hash_index_update(ht, ikey, zval_ptr_ptr, 0, retval_ptr) if res == happy_hash.FAILURE: return zval_null return _return_retval(retval_ptr)
def read(self, apc_file, offset, args, const_args): info_ht = APCFile.ZEND_HashTable('') header_struct = apc_file.read_struct( offset, self._header_str + info_ht.get_padding_suffix()) offset += calcsize(self._header_str + info_ht.get_padding_suffix()) import happy_hash import global_state import zval_utils from objects import MutableString function_table = apc_file.read_ptr( offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args=(APCFile.build_ZEND_function, False)) self.function_table = None if not isinstance(function_table, APCFile.NullStruct): self.function_table = happy_hash.zend_hash_init( None, 0, None, None, False) for class_method in function_table.members(): methodname = class_method.arKey[:-1] happy_hash.zend_hash_add( self.function_table, MutableString(methodname), len(methodname), zval_utils.zp_stack(class_method.pData), 0, global_state.null_zval_ptr_ptr) offset += function_table.calc_size() import happy_hash import global_state import zval_utils from objects import MutableString default_properties = apc_file.read_ptr( offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args=(APCFile.build_ZEND_zval, True)) offset += default_properties.calc_size() import happy_hash import global_state import zval_utils from objects import MutableString properties_info = apc_file.read_ptr( offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args=(APCFile.build_ZEND_property_info, False)) offset += properties_info.calc_size() self.default_static_members = apc_file.read_ptr( offset, APCFile.build_ZEND_HashTable, '0P', const_args=(APCFile.build_ZEND_zval, True)) offset += self.default_static_members.calc_size() static_members_struct = apc_file.read_struct( offset, '@P' + info_ht.get_padding_suffix()) static_members = apc_file.read_ptr( static_members_struct[0].int_value, APCFile.build_ZEND_HashTable, const_args=(APCFile.build_ZEND_zval, True)) self.static_members = None if not isinstance(static_members, APCFile.NullStruct): self.static_members = happy_hash.zend_hash_init( None, 0, None, None, False) for static_member in static_members.members(): member_name = static_member.arKey[:-1].replace('\0', '#') happy_hash.zend_hash_add( self.static_members, MutableString(member_name), len(member_name), zval_utils.zpp_stack( zval_utils.zp_stack(static_member.pData)), 0, global_state.null_zval_ptr_ptr_ptr) offset += calcsize('@P' + info_ht.get_padding_suffix()) self.constants_table = apc_file.read_ptr( offset, APCFile.build_ZEND_HashTable, '0P', const_args=(APCFile.build_ZEND_zval, True)) offset += self.constants_table.calc_size() # Header info self.class_type = header_struct[0].int_value from objects import MutableString self.name = MutableString( apc_file.read_len_string(header_struct[1].int_value, header_struct[2].int_value)) self.name_length = header_struct[2].int_value self.parent = apc_file.read_ptr(header_struct[3].int_value, APCFile.build_ZEND_class_entry) self.refcount = header_struct[4].int_value self.constants_updated = header_struct[5].int_value self.ce_flags = header_struct[6].int_value self.properties_info = None if not isinstance(properties_info, APCFile.NullStruct): self.properties_info = happy_hash.zend_hash_init( None, 0, None, None, False) for property_info in properties_info.members(): propname = property_info.arKey[:-1] if property_info.pData.flags & ZEND_ACC_PRIVATE: property_info.pData.name = MutableString( '#' + self.name.to_str() + '#' + propname) elif property_info.pData.flags & ZEND_ACC_PROTECTED: # TODO: implement zend_declare_property_ex properly in zend_API property_info.pData.name = MutableString('#' + '*' + '#' + propname) else: property_info.pData.name = MutableString(propname) happy_hash.zend_hash_add( self.properties_info, MutableString(propname), len(propname), zval_utils.zp_stack(property_info.pData), 0, global_state.null_zval_ptr_ptr) self.default_properties = None if not isinstance(default_properties, APCFile.NullStruct): self.default_properties = happy_hash.zend_hash_init( None, 0, None, None, False) for default_prop in default_properties.members(): propname = default_prop.arKey[:-1].replace('\0', '#') happy_hash.zend_hash_add( self.default_properties, MutableString(propname), len(propname), zval_utils.zpp_stack( zval_utils.zp_stack(default_prop.pData)), 0, global_state.null_zval_ptr_ptr_ptr) # TODO: read builtins self.create_object = None # Class methods ptrs_struct = apc_file.read_struct(offset, self._ptrs_str) self.constructor = apc_file.read_ptr(ptrs_struct[1].int_value, APCFile.build_ZEND_function) self.destructor = apc_file.read_ptr(ptrs_struct[2].int_value, APCFile.build_ZEND_function) self.clone = apc_file.read_ptr(ptrs_struct[3].int_value, APCFile.build_ZEND_function) self.uuget = apc_file.read_ptr(ptrs_struct[4].int_value, APCFile.build_ZEND_function) self.uuset = apc_file.read_ptr(ptrs_struct[5].int_value, APCFile.build_ZEND_function) self.uuunset = apc_file.read_ptr(ptrs_struct[6].int_value, APCFile.build_ZEND_function) self.uuisset = apc_file.read_ptr(ptrs_struct[7].int_value, APCFile.build_ZEND_function) self.uucall = apc_file.read_ptr(ptrs_struct[8].int_value, APCFile.build_ZEND_function) self.uucallstatic = apc_file.read_ptr(ptrs_struct[9].int_value, APCFile.build_ZEND_function) self.uutostring = apc_file.read_ptr(ptrs_struct[10].int_value, APCFile.build_ZEND_function) self.serialize_func = apc_file.read_ptr( ptrs_struct[11].int_value, APCFile.build_ZEND_function) self.unserialize_func = apc_file.read_ptr( ptrs_struct[12].int_value, APCFile.build_ZEND_function) # Iterator funcs self.zf_new_iterator = apc_file.read_ptr( ptrs_struct[14].int_value, APCFile.build_ZEND_function) self.zf_valid = apc_file.read_ptr(ptrs_struct[15].int_value, APCFile.build_ZEND_function) self.zf_current = apc_file.read_ptr(ptrs_struct[16].int_value, APCFile.build_ZEND_function) self.zf_key = apc_file.read_ptr(ptrs_struct[17].int_value, APCFile.build_ZEND_function) self.zf_next = apc_file.read_ptr(ptrs_struct[18].int_value, APCFile.build_ZEND_function) self.zf_rewind = apc_file.read_ptr(ptrs_struct[19].int_value, APCFile.build_ZEND_function) self.get_static_method = APCFile.NullStruct() # Interfaces self.num_interfaces = ptrs_struct[27].int_value iface_array_fmt_str = '@' + ('P' * self.num_interfaces) iface_array = apc_file.read_struct(ptrs_struct[26].int_value, iface_array_fmt_str) self.interfaces = [ apc_file.read_ptr(x.int_value, APCFile.build_ZEND_class_entry) for x in iface_array ] # Rest of stuff self.filename = apc_file.read_asciiz_string( ptrs_struct[28].int_value) self.line_start = ptrs_struct[29].int_value self.line_end = ptrs_struct[30].int_value self.doc_comment = apc_file.read_len_string( ptrs_struct[31].int_value, ptrs_struct[32].int_value) self.doc_comment_len = ptrs_struct[32].int_value
def read(self, apc_file, offset, args, const_args): op_array_struct = apc_file.read_struct(offset, self._fmt_str) self.type = op_array_struct[0].int_value self.function_name = apc_file.read_asciiz_string( op_array_struct[1].int_value) self.scope = apc_file.read_ptr(op_array_struct[2].int_value, APCFile.build_ZEND_class_entry) self.fn_flags = op_array_struct[3].int_value self.prototype = apc_file.read_ptr(op_array_struct[4].int_value, APCFile.build_ZEND_function) self.num_args = op_array_struct[5].int_value self.required_num_args = op_array_struct[6].int_value self.arg_info = apc_file.read_ptr(op_array_struct[7].int_value, APCFile.build_ZEND_arg_info) self.pass_rest_by_reference = op_array_struct[8].int_value self.return_reference = op_array_struct[9].int_value self.done_pass_two = op_array_struct[10].int_value self.refcount = apc_file.read_uint(op_array_struct[11].int_value) self.opcodes = apc_file.read_count_elements( op_array_struct[12].int_value, op_array_struct[13].int_value, APCFile.build_ZEND_op) self.last = op_array_struct[13].int_value self.size = op_array_struct[14].int_value self.vars = apc_file.read_count_elements( op_array_struct[15].int_value, op_array_struct[16].int_value, APCFile.build_ZEND_compiled_variable) self.last_var = op_array_struct[16].int_value self.size_var = op_array_struct[17].int_value self.T = op_array_struct[18].int_value self.brk_cont_array = apc_file.read_count_elements( op_array_struct[19].int_value, op_array_struct[20].int_value, APCFile.build_ZEND_brk_cont_element) self.last_brk_cont = op_array_struct[20].int_value self.current_brk_cont = op_array_struct[21].int_value self.try_catch_array = apc_file.read_count_elements( op_array_struct[22].int_value, op_array_struct[23].int_value, APCFile.build_ZEND_try_catch_element) self.last_try_catch = op_array_struct[23].int_value import happy_hash import global_state import zval_utils from objects import MutableString static_variables = apc_file.read_ptr( op_array_struct[24].int_value, APCFile.build_ZEND_HashTable, const_args=(APCFile.build_ZEND_zval, True)) self.static_variables = None if not isinstance(static_variables, APCFile.NullStruct): self.static_variables = happy_hash.zend_hash_init( None, 0, None, None, False) for static_var in static_variables.members(): varname = static_var.arKey[:-1] happy_hash.zend_hash_add( self.static_variables, MutableString(varname), len(varname), zval_utils.zpp_stack( zval_utils.zp_stack(static_var.pData)), 0, global_state.null_zval_ptr_ptr_ptr) self.start_op = apc_file.read_ptr(op_array_struct[25].int_value, APCFile.build_ZEND_op) self.backpatch_count = op_array_struct[26].int_value self.this_var = op_array_struct[27].int_value self.filename = apc_file.read_asciiz_string( op_array_struct[28].int_value) self.line_start = op_array_struct[29].int_value self.line_end = op_array_struct[30].int_value self.doc_comment = apc_file.read_len_string( op_array_struct[31].int_value, op_array_struct[32].int_value) self.doc_comment_len = op_array_struct[32].int_value self.early_binding = op_array_struct[33].int_value
def read(self, apc_file, offset, args, const_args): info_ht = APCFile.ZEND_HashTable('') header_struct = apc_file.read_struct(offset, self._header_str + info_ht.get_padding_suffix()) offset += calcsize(self._header_str + info_ht.get_padding_suffix()) import happy_hash import global_state import zval_utils from objects import MutableString function_table = apc_file.read_ptr(offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args = (APCFile.build_ZEND_function, False)) self.function_table = None if not isinstance(function_table, APCFile.NullStruct): self.function_table = happy_hash.zend_hash_init(None, 0, None, None, False) for class_method in function_table.members(): methodname = class_method.arKey[:-1] happy_hash.zend_hash_add(self.function_table, MutableString(methodname), len(methodname), zval_utils.zp_stack(class_method.pData), 0, global_state.null_zval_ptr_ptr) offset += function_table.calc_size() import happy_hash import global_state import zval_utils from objects import MutableString default_properties = apc_file.read_ptr(offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args = (APCFile.build_ZEND_zval, True)) offset += default_properties.calc_size() import happy_hash import global_state import zval_utils from objects import MutableString properties_info = apc_file.read_ptr(offset, APCFile.build_ZEND_HashTable, info_ht.get_padding_suffix(), const_args = (APCFile.build_ZEND_property_info, False)) offset += properties_info.calc_size() self.default_static_members = apc_file.read_ptr(offset, APCFile.build_ZEND_HashTable, '0P', const_args = (APCFile.build_ZEND_zval, True)) offset += self.default_static_members.calc_size() static_members_struct = apc_file.read_struct(offset, '@P' + info_ht.get_padding_suffix()) static_members = apc_file.read_ptr( static_members_struct[0].int_value, APCFile.build_ZEND_HashTable, const_args = (APCFile.build_ZEND_zval, True)) self.static_members = None if not isinstance(static_members, APCFile.NullStruct): self.static_members = happy_hash.zend_hash_init(None, 0, None, None, False) for static_member in static_members.members(): member_name = static_member.arKey[:-1].replace('\0', '#') happy_hash.zend_hash_add(self.static_members, MutableString(member_name), len(member_name), zval_utils.zpp_stack(zval_utils.zp_stack(static_member.pData)), 0, global_state.null_zval_ptr_ptr_ptr) offset += calcsize('@P' + info_ht.get_padding_suffix()) self.constants_table = apc_file.read_ptr(offset, APCFile.build_ZEND_HashTable, '0P', const_args = (APCFile.build_ZEND_zval, True)) offset += self.constants_table.calc_size() # Header info self.class_type = header_struct[0].int_value from objects import MutableString self.name = MutableString(apc_file.read_len_string(header_struct[1].int_value, header_struct[2].int_value)) self.name_length = header_struct[2].int_value self.parent = apc_file.read_ptr(header_struct[3].int_value, APCFile.build_ZEND_class_entry) self.refcount = header_struct[4].int_value self.constants_updated = header_struct[5].int_value self.ce_flags = header_struct[6].int_value self.properties_info = None if not isinstance(properties_info, APCFile.NullStruct): self.properties_info = happy_hash.zend_hash_init(None, 0, None, None, False) for property_info in properties_info.members(): propname = property_info.arKey[:-1] if property_info.pData.flags & ZEND_ACC_PRIVATE: property_info.pData.name = MutableString('#' + self.name.to_str() + '#' + propname) elif property_info.pData.flags & ZEND_ACC_PROTECTED: # TODO: implement zend_declare_property_ex properly in zend_API property_info.pData.name = MutableString('#' + '*' + '#' + propname) else: property_info.pData.name = MutableString(propname) happy_hash.zend_hash_add(self.properties_info, MutableString(propname), len(propname), zval_utils.zp_stack(property_info.pData), 0, global_state.null_zval_ptr_ptr) self.default_properties = None if not isinstance(default_properties, APCFile.NullStruct): self.default_properties = happy_hash.zend_hash_init(None, 0, None, None, False) for default_prop in default_properties.members(): propname = default_prop.arKey[:-1].replace('\0', '#') happy_hash.zend_hash_add(self.default_properties, MutableString(propname), len(propname), zval_utils.zpp_stack(zval_utils.zp_stack(default_prop.pData)), 0, global_state.null_zval_ptr_ptr_ptr) # TODO: read builtins self.create_object = None # Class methods ptrs_struct = apc_file.read_struct(offset, self._ptrs_str) self.constructor = apc_file.read_ptr(ptrs_struct[1].int_value, APCFile.build_ZEND_function) self.destructor = apc_file.read_ptr(ptrs_struct[2].int_value, APCFile.build_ZEND_function) self.clone = apc_file.read_ptr(ptrs_struct[3].int_value, APCFile.build_ZEND_function) self.uuget = apc_file.read_ptr(ptrs_struct[4].int_value, APCFile.build_ZEND_function) self.uuset = apc_file.read_ptr(ptrs_struct[5].int_value, APCFile.build_ZEND_function) self.uuunset = apc_file.read_ptr(ptrs_struct[6].int_value, APCFile.build_ZEND_function) self.uuisset = apc_file.read_ptr(ptrs_struct[7].int_value, APCFile.build_ZEND_function) self.uucall = apc_file.read_ptr(ptrs_struct[8].int_value, APCFile.build_ZEND_function) self.uucallstatic = apc_file.read_ptr(ptrs_struct[9].int_value, APCFile.build_ZEND_function) self.uutostring = apc_file.read_ptr(ptrs_struct[10].int_value, APCFile.build_ZEND_function) self.serialize_func = apc_file.read_ptr(ptrs_struct[11].int_value, APCFile.build_ZEND_function) self.unserialize_func = apc_file.read_ptr(ptrs_struct[12].int_value, APCFile.build_ZEND_function) # Iterator funcs self.zf_new_iterator = apc_file.read_ptr(ptrs_struct[14].int_value, APCFile.build_ZEND_function) self.zf_valid = apc_file.read_ptr(ptrs_struct[15].int_value, APCFile.build_ZEND_function) self.zf_current = apc_file.read_ptr(ptrs_struct[16].int_value, APCFile.build_ZEND_function) self.zf_key = apc_file.read_ptr(ptrs_struct[17].int_value, APCFile.build_ZEND_function) self.zf_next = apc_file.read_ptr(ptrs_struct[18].int_value, APCFile.build_ZEND_function) self.zf_rewind = apc_file.read_ptr(ptrs_struct[19].int_value, APCFile.build_ZEND_function) self.get_static_method = APCFile.NullStruct() # Interfaces self.num_interfaces = ptrs_struct[27].int_value iface_array_fmt_str = '@' + ('P' * self.num_interfaces) iface_array = apc_file.read_struct(ptrs_struct[26].int_value, iface_array_fmt_str) self.interfaces = [apc_file.read_ptr(x.int_value, APCFile.build_ZEND_class_entry) for x in iface_array] # Rest of stuff self.filename = apc_file.read_asciiz_string(ptrs_struct[28].int_value) self.line_start = ptrs_struct[29].int_value self.line_end = ptrs_struct[30].int_value self.doc_comment = apc_file.read_len_string(ptrs_struct[31].int_value, ptrs_struct[32].int_value) self.doc_comment_len = ptrs_struct[32].int_value
def read(self, apc_file, offset, args, const_args): op_array_struct = apc_file.read_struct(offset, self._fmt_str) self.type = op_array_struct[0].int_value self.function_name = apc_file.read_asciiz_string( op_array_struct[1].int_value) self.scope = apc_file.read_ptr( op_array_struct[2].int_value, APCFile.build_ZEND_class_entry) self.fn_flags = op_array_struct[3].int_value self.prototype = apc_file.read_ptr( op_array_struct[4].int_value, APCFile.build_ZEND_function) self.num_args = op_array_struct[5].int_value self.required_num_args = op_array_struct[6].int_value self.arg_info = apc_file.read_ptr( op_array_struct[7].int_value, APCFile.build_ZEND_arg_info) self.pass_rest_by_reference = op_array_struct[8].int_value self.return_reference = op_array_struct[9].int_value self.done_pass_two = op_array_struct[10].int_value self.refcount = apc_file.read_uint(op_array_struct[11].int_value) self.opcodes = apc_file.read_count_elements( op_array_struct[12].int_value, op_array_struct[13].int_value, APCFile.build_ZEND_op) self.last = op_array_struct[13].int_value self.size = op_array_struct[14].int_value self.vars = apc_file.read_count_elements( op_array_struct[15].int_value, op_array_struct[16].int_value, APCFile.build_ZEND_compiled_variable) self.last_var = op_array_struct[16].int_value self.size_var = op_array_struct[17].int_value self.T = op_array_struct[18].int_value self.brk_cont_array = apc_file.read_count_elements( op_array_struct[19].int_value, op_array_struct[20].int_value, APCFile.build_ZEND_brk_cont_element) self.last_brk_cont = op_array_struct[20].int_value self.current_brk_cont = op_array_struct[21].int_value self.try_catch_array = apc_file.read_count_elements( op_array_struct[22].int_value, op_array_struct[23].int_value, APCFile.build_ZEND_try_catch_element) self.last_try_catch = op_array_struct[23].int_value import happy_hash import global_state import zval_utils from objects import MutableString static_variables = apc_file.read_ptr( op_array_struct[24].int_value, APCFile.build_ZEND_HashTable, const_args = (APCFile.build_ZEND_zval, True)) self.static_variables = None if not isinstance(static_variables, APCFile.NullStruct): self.static_variables = happy_hash.zend_hash_init(None, 0, None, None, False) for static_var in static_variables.members(): varname = static_var.arKey[:-1] happy_hash.zend_hash_add(self.static_variables, MutableString(varname), len(varname), zval_utils.zpp_stack(zval_utils.zp_stack(static_var.pData)), 0, global_state.null_zval_ptr_ptr_ptr) self.start_op = apc_file.read_ptr( op_array_struct[25].int_value, APCFile.build_ZEND_op) self.backpatch_count = op_array_struct[26].int_value self.this_var = op_array_struct[27].int_value self.filename = apc_file.read_asciiz_string(op_array_struct[28].int_value) self.line_start = op_array_struct[29].int_value self.line_end = op_array_struct[30].int_value self.doc_comment = apc_file.read_len_string( op_array_struct[31].int_value, op_array_struct[32].int_value) self.doc_comment_len = op_array_struct[32].int_value self.early_binding = op_array_struct[33].int_value