def read(self, apc_file, offset, args, const_args): class_struct = apc_file.read_struct(offset, self._fmt_str) from objects import MutableString self.name = MutableString( apc_file.read_len_string(class_struct[0].int_value, class_struct[1].int_value)) self.name_len = class_struct[1].int_value self.parent_name = MutableString( apc_file.read_asciiz_string(class_struct[2].int_value)) self.class_entry = apc_file.read_ptr( class_struct[3].int_value, APCFile.build_ZEND_class_entry)
def php_array_element_dump(zv, num_args, args, hash_key): level = args[0] if isinstance(hash_key, happy_hash.HashTableIntKey): zend_write( MutableString((level + 1) * ' ' + ('[%d]' % hash_key.intval) + '=>\n'), 0) elif isinstance(hash_key, happy_hash.HashTableStringKey): zend_write( MutableString((level + 1) * ' ' + ('["%s"]' % hash_key.strval.to_str()) + '=>\n'), 0) else: assert False php_var_dump(zv.deref(), level + 2) return 0
def read(self, apc_file, offset, args, const_args): cv_struct = apc_file.read_struct(offset, self._fmt_str) from objects import MutableString self.name = MutableString( apc_file.read_len_string(cv_struct[0].int_value, cv_struct[1].int_value)) self.name_len = cv_struct[1].int_value self.hash_value = cv_struct[2].int_value
def read(self, apc_file, offset, args, const_args): function_struct = apc_file.read_struct(offset, self._fmt_str) from objects import MutableString self.name = MutableString( apc_file.read_len_string(function_struct[0].int_value, function_struct[1].int_value)) self.name_len = function_struct[1].int_value self.function = apc_file.read_ptr(function_struct[2].int_value, APCFile.build_ZEND_function)
def php_var_dump(struc, level): zval_element_dump_func = None if level > 1: zend_write(MutableString((level - 1) * ' '), 0) if zval_utils.Z_TYPE_P(struc) == IS_LONG: zend_write( MutableString('%sint(%d)\n' % (COMMON(struc), zval_utils.Z_LVAL_P(struc))), 0) elif zval_utils.Z_TYPE_P(struc) == IS_BOOL: zend_write( MutableString('%sbool(%s)\n' % (COMMON(struc), 'true' if zval_utils.Z_LVAL_P(struc) else 'false')), 0) elif zval_utils.Z_TYPE_P(struc) == IS_DOUBLE: zend_write( MutableString('%sfloat(%s)\n' % (COMMON(struc), formatted_as_float(zval_utils.Z_DVAL_P(struc)))), 0) elif zval_utils.Z_TYPE_P(struc) == IS_STRING: zend_write( MutableString('%sstring(%d) "%s"\n' % (COMMON(struc), zval_utils.Z_STRLEN_P(struc), zval_utils.Z_STRVAL_P(struc).to_str())), 0) elif zval_utils.Z_TYPE_P(struc) == IS_ARRAY: myht = happy_operators.Z_ARRVAL_P(struc) # TODO: check nApplyCount zend_write( MutableString('%sarray(%d) {\n' % (COMMON(struc), happy_hash.zend_hash_num_elements( happy_operators.Z_ARRVAL_P(struc)))), 0) php_element_dump_func = php_array_element_dump if myht: happy_hash.zend_hash_apply_with_arguments(myht, php_element_dump_func, 1, level) # TODO: dec nApplyCount if level > 1: zend_write(MutableString((level - 1) * ' '), 0) zend_write(MutableString('}\n'), 0) elif zval_utils.Z_TYPE_P(struc) == IS_NULL: zend_write(MutableString('NULL\n'), 0) else: raise Exception('Not implemented yet')
def read(self, apc_file, offset, args, const_args): pi_struct = apc_file.read_struct(offset, self._fmt_str) self.flags = pi_struct[0].int_value from objects import MutableString self.name = MutableString( apc_file.read_len_string(pi_struct[1].int_value, pi_struct[2].int_value)) self.name_len = pi_struct[2].int_value self.h = pi_struct[3] self.doc_comment = apc_file.read_len_string( pi_struct[4].int_value, pi_struct[5].int_value) self.doc_comment_len = pi_struct[5].int_value self.ce = apc_file.read_ptr(pi_struct[6].int_value, APCFile.build_ZEND_class_entry)
def read(self, apc_file, offset, args, const_args): arg_struct = apc_file.read_struct(offset, self._fmt_str) from objects import MutableString self.name = MutableString( apc_file.read_len_string(arg_struct[0].int_value, arg_struct[1].int_value)) self.name_len = arg_struct[1].int_value self.class_name = apc_file.read_len_string(arg_struct[2].int_value, arg_struct[3].int_value) self.class_name_len = arg_struct[3].int_value self.array_type_hint = arg_struct[4].int_value self.allow_null = arg_struct[5].int_value self.pass_by_reference = arg_struct[6].int_value self.return_reference = arg_struct[7].int_value self.required_num_args = arg_struct[8].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
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