Rename smart:: namespace to req::
[hiphop-php.git] / hphp / runtime / base / variable-unserializer.h
blob1b80139b0a4abbb9941e9b60b68576b0e6b72308
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_VARIABLE_UNSERIALIZER_H_
18 #define incl_HPHP_VARIABLE_UNSERIALIZER_H_
20 #include "hphp/runtime/base/req-containers.h"
21 #include "hphp/runtime/base/type-variant.h"
22 #include "hphp/runtime/base/types.h"
24 namespace HPHP {
26 ///////////////////////////////////////////////////////////////////////////////
28 enum class UnserializeMode {
29 Value = 0,
30 Key = 1,
31 ColValue = 2,
32 ColKey = 3,
35 struct VariableUnserializer {
38 * Supported unserialization formats.
40 enum class Type {
41 Serialize,
42 APCSerialize,
43 DebuggerSerialize
47 * Construct an unserializer, with an optional whitelist of classes to
48 * allow. In the whitelist, empty_array means "allow no classes", while
49 * null_array means allow any classes. We default to null_array since
50 * serialization is not limited inside the VM.
52 VariableUnserializer(
53 const char* str,
54 size_t len,
55 Type type,
56 bool allowUnknownSerializableClass = false,
57 const Array& classWhitelist = null_array);
60 * Main API; unserialize the buffer and return as a Variant.
62 Variant unserialize();
65 * Read the appropriate data type from buffer.
67 int64_t readInt();
68 double readDouble();
69 char readChar();
70 void read(char* buf, unsigned n);
73 * Read a character and throw if it differs from expected.
75 void expectChar(char expected);
76 void throwUnexpected(char expected, char got);
79 * Accessors.
81 Type type() const;
82 bool allowUnknownSerializableClass() const;
83 const char* head() const;
84 char peek() const;
85 char peekBack() const;
86 bool endOfBuffer() const;
89 * True if clsName is allowed to be unserialized.
91 bool isWhitelistedClass(const String& clsName) const;
94 * Set the beginning and end of internal buffer.
96 void set(const char* buf, const char* end);
99 * Push v onto the vector of refs for future reference.
101 void add(Variant* v, UnserializeMode mode);
104 * Used by the 'r' encoding to get a reference.
106 Variant* getByVal(int id);
109 * Used by the 'R' encoding to get a reference.
111 Variant* getByRef(int id);
114 * Store properties/array elements that get overwritten incase they are
115 * referenced later during unserialization
117 void putInOverwrittenList(const Variant& v);
119 private:
121 * Hold references to previously-unserialized data, along with bits telling
122 * whether it is legal to reference them later.
124 struct RefInfo {
125 explicit RefInfo(Variant* v);
126 static RefInfo makeNonRefable(Variant* v);
127 Variant* var() const;
128 bool canBeReferenced() const;
129 private:
130 uintptr_t m_data;
133 Array m_overwrittenList;
135 void check() const;
137 Type m_type;
138 const char* m_buf;
139 const char* m_end;
140 req::vector<RefInfo> m_refs;
141 bool m_unknownSerializable;
142 const Array& m_classWhiteList; // classes allowed to be unserialized
145 ///////////////////////////////////////////////////////////////////////////////
147 void unserializeVariant(Variant&, VariableUnserializer *unserializer,
148 UnserializeMode mode = UnserializeMode::Value);
152 #include "hphp/runtime/base/variable-unserializer-inl.h"
154 #endif // incl_HPHP_VARIABLE_UNSERIALIZER_H_