Serialized PHP value: C++98/STL parsing library

A serialized PHP value, in the format used by PHP's built-in serialize and unserialize functions. This format closely mirrors PHP's data model: it supports all of PHP's scalar types (NULL, booleans, numbers, strings), associative arrays, objects, and recursive data structures using references. The only PHP values not supported by this format are resources, which usually correspond to native file or connection handles and cannot be meaningfully serialized.

There is no official documentation for this data format; this spec was created based on the PHP source code and the behavior of serialize/unserialize. PHP makes no guarantees about compatibility of serialized data between PHP versions, but in practice, the format has remained fully backwards-compatible - values serialized by an older PHP version can be unserialized on any newer PHP version. This spec supports serialized values from PHP 7.3 or any earlier version.

Application

PHP

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of Serialized PHP value using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    php_serialized_value_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.type() // => A single-character code indicating the type of the serialized value.

C++98/STL source code to parse Serialized PHP value

php_serialized_value.h

#ifndef PHP_SERIALIZED_VALUE_H_
#define PHP_SERIALIZED_VALUE_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * A serialized PHP value, in the format used by PHP's built-in `serialize` and
 * `unserialize` functions. This format closely mirrors PHP's data model:
 * it supports all of PHP's scalar types (`NULL`, booleans, numbers, strings),
 * associative arrays, objects, and recursive data structures using references.
 * The only PHP values not supported by this format are *resources*,
 * which usually correspond to native file or connection handles and cannot be
 * meaningfully serialized.
 * 
 * There is no official documentation for this data format;
 * this spec was created based on the PHP source code and the behavior of
 * `serialize`/`unserialize`. PHP makes no guarantees about compatibility of
 * serialized data between PHP versions, but in practice, the format has
 * remained fully backwards-compatible - values serialized by an older
 * PHP version can be unserialized on any newer PHP version.
 * This spec supports serialized values from PHP 7.3 or any earlier version.
 * \sa https://www.php.net/manual/en/function.serialize.php Source
 * \sa https://www.php.net/manual/en/function.serialize.php#66147 Source
 * \sa https://www.php.net/manual/en/function.unserialize.php Source
 * \sa https://github.com/php/php-src/blob/php-7.3.5/ext/standard/var_unserializer.re Source
 * \sa https://github.com/php/php-src/blob/php-7.3.5/ext/standard/var.c#L822 Source
 */

class php_serialized_value_t : public kaitai::kstruct {

public:
    class count_prefixed_mapping_t;
    class float_contents_t;
    class length_prefixed_quoted_string_t;
    class object_contents_t;
    class array_contents_t;
    class custom_serialized_object_contents_t;
    class null_contents_t;
    class php_3_object_contents_t;
    class bool_contents_t;
    class string_contents_t;
    class int_contents_t;
    class mapping_entry_t;

    enum value_type_t {
        VALUE_TYPE_CUSTOM_SERIALIZED_OBJECT = 67,
        VALUE_TYPE_NULL = 78,
        VALUE_TYPE_OBJECT = 79,
        VALUE_TYPE_VARIABLE_REFERENCE = 82,
        VALUE_TYPE_PHP_6_STRING = 83,
        VALUE_TYPE_ARRAY = 97,
        VALUE_TYPE_BOOL = 98,
        VALUE_TYPE_FLOAT = 100,
        VALUE_TYPE_INT = 105,
        VALUE_TYPE_PHP_3_OBJECT = 111,
        VALUE_TYPE_OBJECT_REFERENCE = 114,
        VALUE_TYPE_STRING = 115
    };

    enum bool_value_t {
        BOOL_VALUE_FALSE = 48,
        BOOL_VALUE_TRUE = 49
    };

    php_serialized_value_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, php_serialized_value_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~php_serialized_value_t();

    /**
     * A mapping (a sequence of key-value pairs) prefixed with its size.
     */

    class count_prefixed_mapping_t : public kaitai::kstruct {

    public:

        count_prefixed_mapping_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~count_prefixed_mapping_t();

    private:
        bool f_num_entries;
        int32_t m_num_entries;

    public:

        /**
         * The number of key-value pairs in the mapping, parsed as an integer.
         */
        int32_t num_entries();

    private:
        std::string m_num_entries_dec;
        std::string m_opening_brace;
        std::vector<mapping_entry_t*>* m_entries;
        std::string m_closing_brace;
        php_serialized_value_t* m__root;
        kaitai::kstruct* m__parent;

    public:

        /**
         * The number of key-value pairs in the mapping, in ASCII decimal.
         */
        std::string num_entries_dec() const { return m_num_entries_dec; }
        std::string opening_brace() const { return m_opening_brace; }

        /**
         * The key-value pairs contained in the mapping.
         */
        std::vector<mapping_entry_t*>* entries() const { return m_entries; }
        std::string closing_brace() const { return m_closing_brace; }
        php_serialized_value_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    /**
     * The contents of a floating-point value.
     */

    class float_contents_t : public kaitai::kstruct {

    public:

        float_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~float_contents_t();

    private:
        std::string m_colon;
        std::string m_value_dec;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }

        /**
         * The value of the `float`, in ASCII decimal, as generated by PHP's
         * usual double-to-string conversion. In particular, this means that:
         * 
         * * A decimal point may not be included (for integral numbers)
         * * The number may use exponent notation (e. g. `1.0E+16`)
         * * Positive and negative infinity are represented as `INF`
         *   and `-INF`, respectively
         * * Not-a-number is represented as `NAN`
         */
        std::string value_dec() const { return m_value_dec; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * A quoted string prefixed with its length.
     * 
     * Despite the quotes surrounding the string data, it can contain
     * arbitrary bytes, which are never escaped in any way.
     * This does not cause any ambiguities when parsing - the bounds of
     * the string are determined only by the length field, not by the quotes.
     */

    class length_prefixed_quoted_string_t : public kaitai::kstruct {

    public:

        length_prefixed_quoted_string_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~length_prefixed_quoted_string_t();

    private:
        bool f_len_data;
        int32_t m_len_data;

    public:

        /**
         * The length of the string's contents in bytes, parsed as an integer.
         * The quotes are not counted in this size number.
         */
        int32_t len_data();

    private:
        std::string m_len_data_dec;
        std::string m_opening_quote;
        std::string m_data;
        std::string m_closing_quote;
        php_serialized_value_t* m__root;
        kaitai::kstruct* m__parent;

    public:

        /**
         * The length of the string's data in bytes, in ASCII decimal.
         * The quotes are not counted in this length number.
         */
        std::string len_data_dec() const { return m_len_data_dec; }
        std::string opening_quote() const { return m_opening_quote; }

        /**
         * The data contained in the string. The quotes are not included.
         */
        std::string data() const { return m_data; }
        std::string closing_quote() const { return m_closing_quote; }
        php_serialized_value_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    /**
     * The contents of an object value serialized in the default format.
     * Unlike its PHP 3 counterpart, it contains a class name.
     */

    class object_contents_t : public kaitai::kstruct {

    public:

        object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~object_contents_t();

    private:
        std::string m_colon1;
        length_prefixed_quoted_string_t* m_class_name;
        std::string m_colon2;
        count_prefixed_mapping_t* m_properties;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon1() const { return m_colon1; }

        /**
         * The name of the object's class.
         */
        length_prefixed_quoted_string_t* class_name() const { return m_class_name; }
        std::string colon2() const { return m_colon2; }

        /**
         * The object's properties. Keys ust be of type `string`,
         * values may have any type.
         */
        count_prefixed_mapping_t* properties() const { return m_properties; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of an array value.
     */

    class array_contents_t : public kaitai::kstruct {

    public:

        array_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~array_contents_t();

    private:
        std::string m_colon;
        count_prefixed_mapping_t* m_elements;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }

        /**
         * The array's elements. Keys must be of type `int` or `string`,
         * values may have any type.
         */
        count_prefixed_mapping_t* elements() const { return m_elements; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of an object value that implements a custom
     * serialized format using `Serializable`.
     */

    class custom_serialized_object_contents_t : public kaitai::kstruct {

    public:

        custom_serialized_object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~custom_serialized_object_contents_t();

    private:
        bool f_len_data;
        int32_t m_len_data;

    public:

        /**
         * The length of the serialized data in bytes, parsed as an integer.
         * The braces are not counted in this length number.
         */
        int32_t len_data();

    private:
        std::string m_colon1;
        length_prefixed_quoted_string_t* m_class_name;
        std::string m_colon2;
        std::string m_len_data_dec;
        std::string m_opening_brace;
        std::string m_data;
        std::string m_closing_quote;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon1() const { return m_colon1; }

        /**
         * The name of the object's class.
         */
        length_prefixed_quoted_string_t* class_name() const { return m_class_name; }
        std::string colon2() const { return m_colon2; }

        /**
         * The length of the serialized data in bytes, in ASCII decimal.
         * The braces are not counted in this size number.
         */
        std::string len_data_dec() const { return m_len_data_dec; }
        std::string opening_brace() const { return m_opening_brace; }

        /**
         * The custom serialized data. The braces are not included.
         * 
         * Although the surrounding braces make it look like a regular
         * serialized object, this field is actually more similar to a string:
         * it can contain arbitrary data that is not required to follow
         * any common structure.
         */
        std::string data() const { return m_data; }
        std::string closing_quote() const { return m_closing_quote; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of a null value (`value_type::null`). This structure
     * contains no actual data, since there is only a single `NULL` value.
     */

    class null_contents_t : public kaitai::kstruct {

    public:

        null_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~null_contents_t();

    private:
        std::string m_semicolon;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string semicolon() const { return m_semicolon; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of a PHP 3 object value. Unlike its counterpart in PHP 4
     * and above, it does not contain a class name.
     */

    class php_3_object_contents_t : public kaitai::kstruct {

    public:

        php_3_object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~php_3_object_contents_t();

    private:
        std::string m_colon;
        count_prefixed_mapping_t* m_properties;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }

        /**
         * The object's properties. Keys must be of type `string`,
         * values may have any type.
         */
        count_prefixed_mapping_t* properties() const { return m_properties; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of a boolean value (`value_type::bool`).
     */

    class bool_contents_t : public kaitai::kstruct {

    public:

        bool_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~bool_contents_t();

    private:
        bool f_value;
        bool m_value;

    public:

        /**
         * The value of the `bool`, parsed as a boolean.
         */
        bool value();

    private:
        std::string m_colon;
        bool_value_t m_value_dec;
        std::string m_semicolon;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }

        /**
         * The value of the `bool`: `0` for `false` or `1` for `true`.
         */
        bool_value_t value_dec() const { return m_value_dec; }
        std::string semicolon() const { return m_semicolon; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of a string value.
     * 
     * Note: PHP strings can contain arbitrary byte sequences.
     * They are not necessarily valid text in any specific encoding.
     */

    class string_contents_t : public kaitai::kstruct {

    public:

        string_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~string_contents_t();

    private:
        bool f_value;
        std::string m_value;

    public:

        /**
         * The value of the string, as a byte array.
         */
        std::string value();

    private:
        std::string m_colon;
        length_prefixed_quoted_string_t* m_string;
        std::string m_semicolon;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }
        length_prefixed_quoted_string_t* string() const { return m_string; }
        std::string semicolon() const { return m_semicolon; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * The contents of an integer-like value:
     * either an actual integer (`value_type::int`) or a reference
     * (`value_type::variable_reference`, `value_type::object_reference`).
     */

    class int_contents_t : public kaitai::kstruct {

    public:

        int_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~int_contents_t();

    private:
        bool f_value;
        int32_t m_value;

    public:

        /**
         * The value of the `int`, parsed as an integer.
         */
        int32_t value();

    private:
        std::string m_colon;
        std::string m_value_dec;
        php_serialized_value_t* m__root;
        php_serialized_value_t* m__parent;

    public:
        std::string colon() const { return m_colon; }

        /**
         * The value of the `int`, in ASCII decimal.
         */
        std::string value_dec() const { return m_value_dec; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t* _parent() const { return m__parent; }
    };

    /**
     * A mapping entry consisting of a key and a value.
     */

    class mapping_entry_t : public kaitai::kstruct {

    public:

        mapping_entry_t(kaitai::kstream* p__io, php_serialized_value_t::count_prefixed_mapping_t* p__parent = 0, php_serialized_value_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~mapping_entry_t();

    private:
        php_serialized_value_t* m_key;
        php_serialized_value_t* m_value;
        php_serialized_value_t* m__root;
        php_serialized_value_t::count_prefixed_mapping_t* m__parent;

    public:

        /**
         * The key of the entry.
         */
        php_serialized_value_t* key() const { return m_key; }

        /**
         * The value of the entry.
         */
        php_serialized_value_t* value() const { return m_value; }
        php_serialized_value_t* _root() const { return m__root; }
        php_serialized_value_t::count_prefixed_mapping_t* _parent() const { return m__parent; }
    };

private:
    value_type_t m_type;
    kaitai::kstruct* m_contents;
    bool n_contents;

public:
    bool _is_null_contents() { contents(); return n_contents; };

private:
    php_serialized_value_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * A single-character code indicating the type of the serialized value.
     */
    value_type_t type() const { return m_type; }

    /**
     * The contents of the serialized value, which vary depending on the type.
     */
    kaitai::kstruct* contents() const { return m_contents; }
    php_serialized_value_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // PHP_SERIALIZED_VALUE_H_

php_serialized_value.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "php_serialized_value.h"
#include "kaitai/exceptions.h"

php_serialized_value_t::php_serialized_value_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::_read() {
    m_type = static_cast<php_serialized_value_t::value_type_t>(m__io->read_u1());
    n_contents = true;
    switch (type()) {
    case php_serialized_value_t::VALUE_TYPE_CUSTOM_SERIALIZED_OBJECT: {
        n_contents = false;
        m_contents = new custom_serialized_object_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_PHP_3_OBJECT: {
        n_contents = false;
        m_contents = new php_3_object_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_OBJECT: {
        n_contents = false;
        m_contents = new object_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_VARIABLE_REFERENCE: {
        n_contents = false;
        m_contents = new int_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_PHP_6_STRING: {
        n_contents = false;
        m_contents = new string_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_FLOAT: {
        n_contents = false;
        m_contents = new float_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_OBJECT_REFERENCE: {
        n_contents = false;
        m_contents = new int_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_NULL: {
        n_contents = false;
        m_contents = new null_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_BOOL: {
        n_contents = false;
        m_contents = new bool_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_INT: {
        n_contents = false;
        m_contents = new int_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_ARRAY: {
        n_contents = false;
        m_contents = new array_contents_t(m__io, this, m__root);
        break;
    }
    case php_serialized_value_t::VALUE_TYPE_STRING: {
        n_contents = false;
        m_contents = new string_contents_t(m__io, this, m__root);
        break;
    }
    }
}

php_serialized_value_t::~php_serialized_value_t() {
    _clean_up();
}

void php_serialized_value_t::_clean_up() {
    if (!n_contents) {
        if (m_contents) {
            delete m_contents; m_contents = 0;
        }
    }
}

php_serialized_value_t::count_prefixed_mapping_t::count_prefixed_mapping_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_entries = 0;
    f_num_entries = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::count_prefixed_mapping_t::_read() {
    m_num_entries_dec = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(58, false, true, true), std::string("ASCII"));
    m_opening_brace = m__io->read_bytes(1);
    if (!(opening_brace() == std::string("\x7B", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x7B", 1), opening_brace(), _io(), std::string("/types/count_prefixed_mapping/seq/1"));
    }
    m_entries = new std::vector<mapping_entry_t*>();
    const int l_entries = num_entries();
    for (int i = 0; i < l_entries; i++) {
        m_entries->push_back(new mapping_entry_t(m__io, this, m__root));
    }
    m_closing_brace = m__io->read_bytes(1);
    if (!(closing_brace() == std::string("\x7D", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x7D", 1), closing_brace(), _io(), std::string("/types/count_prefixed_mapping/seq/3"));
    }
}

php_serialized_value_t::count_prefixed_mapping_t::~count_prefixed_mapping_t() {
    _clean_up();
}

void php_serialized_value_t::count_prefixed_mapping_t::_clean_up() {
    if (m_entries) {
        for (std::vector<mapping_entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
            delete *it;
        }
        delete m_entries; m_entries = 0;
    }
}

int32_t php_serialized_value_t::count_prefixed_mapping_t::num_entries() {
    if (f_num_entries)
        return m_num_entries;
    m_num_entries = std::stoi(num_entries_dec());
    f_num_entries = true;
    return m_num_entries;
}

php_serialized_value_t::float_contents_t::float_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::float_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/float_contents/seq/0"));
    }
    m_value_dec = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(59, false, true, true), std::string("ASCII"));
}

php_serialized_value_t::float_contents_t::~float_contents_t() {
    _clean_up();
}

void php_serialized_value_t::float_contents_t::_clean_up() {
}

php_serialized_value_t::length_prefixed_quoted_string_t::length_prefixed_quoted_string_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_len_data = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::length_prefixed_quoted_string_t::_read() {
    m_len_data_dec = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(58, false, true, true), std::string("ASCII"));
    m_opening_quote = m__io->read_bytes(1);
    if (!(opening_quote() == std::string("\x22", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x22", 1), opening_quote(), _io(), std::string("/types/length_prefixed_quoted_string/seq/1"));
    }
    m_data = m__io->read_bytes(len_data());
    m_closing_quote = m__io->read_bytes(1);
    if (!(closing_quote() == std::string("\x22", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x22", 1), closing_quote(), _io(), std::string("/types/length_prefixed_quoted_string/seq/3"));
    }
}

php_serialized_value_t::length_prefixed_quoted_string_t::~length_prefixed_quoted_string_t() {
    _clean_up();
}

void php_serialized_value_t::length_prefixed_quoted_string_t::_clean_up() {
}

int32_t php_serialized_value_t::length_prefixed_quoted_string_t::len_data() {
    if (f_len_data)
        return m_len_data;
    m_len_data = std::stoi(len_data_dec());
    f_len_data = true;
    return m_len_data;
}

php_serialized_value_t::object_contents_t::object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_class_name = 0;
    m_properties = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::object_contents_t::_read() {
    m_colon1 = m__io->read_bytes(1);
    if (!(colon1() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon1(), _io(), std::string("/types/object_contents/seq/0"));
    }
    m_class_name = new length_prefixed_quoted_string_t(m__io, this, m__root);
    m_colon2 = m__io->read_bytes(1);
    if (!(colon2() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon2(), _io(), std::string("/types/object_contents/seq/2"));
    }
    m_properties = new count_prefixed_mapping_t(m__io, this, m__root);
}

php_serialized_value_t::object_contents_t::~object_contents_t() {
    _clean_up();
}

void php_serialized_value_t::object_contents_t::_clean_up() {
    if (m_class_name) {
        delete m_class_name; m_class_name = 0;
    }
    if (m_properties) {
        delete m_properties; m_properties = 0;
    }
}

php_serialized_value_t::array_contents_t::array_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_elements = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::array_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/array_contents/seq/0"));
    }
    m_elements = new count_prefixed_mapping_t(m__io, this, m__root);
}

php_serialized_value_t::array_contents_t::~array_contents_t() {
    _clean_up();
}

void php_serialized_value_t::array_contents_t::_clean_up() {
    if (m_elements) {
        delete m_elements; m_elements = 0;
    }
}

php_serialized_value_t::custom_serialized_object_contents_t::custom_serialized_object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_class_name = 0;
    f_len_data = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::custom_serialized_object_contents_t::_read() {
    m_colon1 = m__io->read_bytes(1);
    if (!(colon1() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon1(), _io(), std::string("/types/custom_serialized_object_contents/seq/0"));
    }
    m_class_name = new length_prefixed_quoted_string_t(m__io, this, m__root);
    m_colon2 = m__io->read_bytes(1);
    if (!(colon2() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon2(), _io(), std::string("/types/custom_serialized_object_contents/seq/2"));
    }
    m_len_data_dec = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(58, false, true, true), std::string("ASCII"));
    m_opening_brace = m__io->read_bytes(1);
    if (!(opening_brace() == std::string("\x7B", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x7B", 1), opening_brace(), _io(), std::string("/types/custom_serialized_object_contents/seq/4"));
    }
    m_data = m__io->read_bytes(len_data());
    m_closing_quote = m__io->read_bytes(1);
    if (!(closing_quote() == std::string("\x7D", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x7D", 1), closing_quote(), _io(), std::string("/types/custom_serialized_object_contents/seq/6"));
    }
}

php_serialized_value_t::custom_serialized_object_contents_t::~custom_serialized_object_contents_t() {
    _clean_up();
}

void php_serialized_value_t::custom_serialized_object_contents_t::_clean_up() {
    if (m_class_name) {
        delete m_class_name; m_class_name = 0;
    }
}

int32_t php_serialized_value_t::custom_serialized_object_contents_t::len_data() {
    if (f_len_data)
        return m_len_data;
    m_len_data = std::stoi(len_data_dec());
    f_len_data = true;
    return m_len_data;
}

php_serialized_value_t::null_contents_t::null_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::null_contents_t::_read() {
    m_semicolon = m__io->read_bytes(1);
    if (!(semicolon() == std::string("\x3B", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3B", 1), semicolon(), _io(), std::string("/types/null_contents/seq/0"));
    }
}

php_serialized_value_t::null_contents_t::~null_contents_t() {
    _clean_up();
}

void php_serialized_value_t::null_contents_t::_clean_up() {
}

php_serialized_value_t::php_3_object_contents_t::php_3_object_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_properties = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::php_3_object_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/php_3_object_contents/seq/0"));
    }
    m_properties = new count_prefixed_mapping_t(m__io, this, m__root);
}

php_serialized_value_t::php_3_object_contents_t::~php_3_object_contents_t() {
    _clean_up();
}

void php_serialized_value_t::php_3_object_contents_t::_clean_up() {
    if (m_properties) {
        delete m_properties; m_properties = 0;
    }
}

php_serialized_value_t::bool_contents_t::bool_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_value = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::bool_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/bool_contents/seq/0"));
    }
    m_value_dec = static_cast<php_serialized_value_t::bool_value_t>(m__io->read_u1());
    m_semicolon = m__io->read_bytes(1);
    if (!(semicolon() == std::string("\x3B", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3B", 1), semicolon(), _io(), std::string("/types/bool_contents/seq/2"));
    }
}

php_serialized_value_t::bool_contents_t::~bool_contents_t() {
    _clean_up();
}

void php_serialized_value_t::bool_contents_t::_clean_up() {
}

bool php_serialized_value_t::bool_contents_t::value() {
    if (f_value)
        return m_value;
    m_value = value_dec() == php_serialized_value_t::BOOL_VALUE_TRUE;
    f_value = true;
    return m_value;
}

php_serialized_value_t::string_contents_t::string_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_string = 0;
    f_value = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::string_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/string_contents/seq/0"));
    }
    m_string = new length_prefixed_quoted_string_t(m__io, this, m__root);
    m_semicolon = m__io->read_bytes(1);
    if (!(semicolon() == std::string("\x3B", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3B", 1), semicolon(), _io(), std::string("/types/string_contents/seq/2"));
    }
}

php_serialized_value_t::string_contents_t::~string_contents_t() {
    _clean_up();
}

void php_serialized_value_t::string_contents_t::_clean_up() {
    if (m_string) {
        delete m_string; m_string = 0;
    }
}

std::string php_serialized_value_t::string_contents_t::value() {
    if (f_value)
        return m_value;
    m_value = string()->data();
    f_value = true;
    return m_value;
}

php_serialized_value_t::int_contents_t::int_contents_t(kaitai::kstream* p__io, php_serialized_value_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_value = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::int_contents_t::_read() {
    m_colon = m__io->read_bytes(1);
    if (!(colon() == std::string("\x3A", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x3A", 1), colon(), _io(), std::string("/types/int_contents/seq/0"));
    }
    m_value_dec = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(59, false, true, true), std::string("ASCII"));
}

php_serialized_value_t::int_contents_t::~int_contents_t() {
    _clean_up();
}

void php_serialized_value_t::int_contents_t::_clean_up() {
}

int32_t php_serialized_value_t::int_contents_t::value() {
    if (f_value)
        return m_value;
    m_value = std::stoi(value_dec());
    f_value = true;
    return m_value;
}

php_serialized_value_t::mapping_entry_t::mapping_entry_t(kaitai::kstream* p__io, php_serialized_value_t::count_prefixed_mapping_t* p__parent, php_serialized_value_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_key = 0;
    m_value = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void php_serialized_value_t::mapping_entry_t::_read() {
    m_key = new php_serialized_value_t(m__io);
    m_value = new php_serialized_value_t(m__io);
}

php_serialized_value_t::mapping_entry_t::~mapping_entry_t() {
    _clean_up();
}

void php_serialized_value_t::mapping_entry_t::_clean_up() {
    if (m_key) {
        delete m_key; m_key = 0;
    }
    if (m_value) {
        delete m_value; m_value = 0;
    }
}