0

I have nested struct data in c. How to save _zend_op_array to file?

struct _zend_op {
    const void *handler;
    znode_op op1;
    znode_op op2;
    znode_op result;
    uint32_t extended_value;
    uint32_t lineno;
    zend_uchar opcode;
    zend_uchar op1_type;
    zend_uchar op2_type;
    zend_uchar result_type;
};

struct _zend_op_array {
    /* Common elements */
    zend_uchar type;
    zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */
    uint32_t fn_flags;
    zend_string *function_name;
    zend_class_entry *scope;
    zend_function *prototype;
    uint32_t num_args;
    uint32_t required_num_args;
    zend_arg_info *arg_info;
    /* END of common elements */

    int cache_size;     /* number of run_time_cache_slots * sizeof(void*) */
    int last_var;       /* number of CV variables */
    uint32_t T;         /* number of temporary variables */
    uint32_t last;      /* number of opcodes */

    zend_op *opcodes;
    ZEND_MAP_PTR_DEF(void **, run_time_cache);
    ZEND_MAP_PTR_DEF(HashTable *, static_variables_ptr);
    HashTable *static_variables;
    zend_string **vars; /* names of CV variables */

    uint32_t *refcount;

    int last_live_range;
    int last_try_catch;
    zend_live_range *live_range;
    zend_try_catch_element *try_catch_array;

    zend_string *filename;
    uint32_t line_start;
    uint32_t line_end;
    zend_string *doc_comment;

    int last_literal;
    zval *literals;

    void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};
Saucy Goat
  • 1,587
  • 1
  • 11
  • 32
Arshid KV
  • 9,631
  • 3
  • 35
  • 36
  • 2
    Can you please be more specific? What exactly do you not know how to do? Writing a structure to file is called serialisation. The C language itself does not deal with serialisation. You need to define your own file format (can be text or binary) and write/read the struct fields yourself. There are also many libraries that implement different forms of serialisation. – kaylum Dec 24 '19 at 04:33
  • 1
    See this answer on [how to dump memory to file](https://stackoverflow.com/questions/3913119/dumping-memory-to-file) – thuyein Dec 24 '19 at 04:35
  • 1
    @ThuYeinTun - It is not as simple as dumping the memory to the file. e.g. `arg_info` is presumably an array which is dynamically allocated and is stored in a separate memory location. – Rishikesh Raje Dec 24 '19 at 04:49
  • You will need to *"Serialze"* your data and write the data to file. There appear to be quite a number of allocated objects, and you will have to write all data out -- you cannot simply write the pointer address. Some look nebulous like `HashTable *static_variables;` (if you don't own the static variables and they are provided by some PHP data structure, then you will have to copy and write the values out and have some way to set those again when read back in) The could conceivably be the entire hash table. All doable, but the code my be longer than the program itself. – David C. Rankin Dec 24 '19 at 07:08

1 Answers1

1

You have a complicated structure with many pointers inside. The method to store depends on the data elements. I will give some general suggestions here.

  • If you can ensure that that the number of fields are always constant (e.g. arg_info has a known number of elements) and that you will not have a semicolon in your data (or if present it is properly escaped) then you can define some sort of CSV format with semicolons separating the fields.

    • You will also need to write the number of elements in array first and then the array elements.
    • You will need to write your own functions for encoding and parsing.
  • Another approach is to go for a structured format like JSON or XML. Maybe you can use some open source encoders and parsers for this.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • 1
    @ArshidKV That's not a reasonable question to ask without providing a detailed description of your requirements. Each serialisation technique has their pros and cons and which is the most suitable depends on your exact requirements. – kaylum Dec 24 '19 at 04:43
  • I want save PHP (zend engine) opcode to file and retrieve back. – Arshid KV Dec 24 '19 at 04:50