3 class haxe_Unserializer {
4 public function __construct($buf) {
5 if(!php_Boot::$skip_constructor) {
7 $this->length = strlen($buf);
9 $this->scache = new _hx_array(array());
10 $this->cache = new _hx_array(array());
11 $r = haxe_Unserializer::$DEFAULT_RESOLVER;
13 $r = _hx_qtype("Type");
14 haxe_Unserializer::$DEFAULT_RESOLVER = $r;
16 $this->setResolver($r);
18 public function unserialize() {
19 switch(ord(substr($this->buf,$this->pos++,1))) {
33 return $this->readDigits();
38 $c = ord(substr($this->buf,$this->pos,1));
39 if($c >= 43 && $c < 58 || $c === 101 || $c === 69) {
46 return Std::parseFloat(_hx_substr($this->buf, $p1, $this->pos - $p1));
49 $len = $this->readDigits();
50 if(ord(substr($this->buf,$this->pos++,1)) !== 58 || $this->length - $this->pos < $len) {
51 throw new HException("Invalid string length");
53 $s = _hx_substr($this->buf, $this->pos, $len);
56 $this->scache->push($s);
63 return Math::$NEGATIVE_INFINITY;
66 return Math::$POSITIVE_INFINITY;
70 $a = new _hx_array(array());
71 $this->cache->push($a);
73 $c = ord(substr($this->buf,$this->pos,1));
80 $n = $this->readDigits();
81 $a[$a->length + $n - 1] = null;
84 $a->push($this->unserialize());
91 $o = _hx_anonymous(array());
92 $this->cache->push($o);
93 $this->unserializeObject($o);
97 $n = $this->readDigits();
98 if($n < 0 || $n >= $this->cache->length) {
99 throw new HException("Invalid reference");
101 return $this->cache[$n];
104 $n = $this->readDigits();
105 if($n < 0 || $n >= $this->scache->length) {
106 throw new HException("Invalid string reference");
108 return $this->scache[$n];
111 throw new HException($this->unserialize());
114 $name = $this->unserialize();
115 $cl = $this->resolver->resolveClass($name);
117 throw new HException("Class not found " . $name);
119 $o = Type::createEmptyInstance($cl);
120 $this->cache->push($o);
121 $this->unserializeObject($o);
125 $name = $this->unserialize();
126 $edecl = $this->resolver->resolveEnum($name);
127 if($edecl === null) {
128 throw new HException("Enum not found " . $name);
130 $e = $this->unserializeEnum($edecl, $this->unserialize());
131 $this->cache->push($e);
135 $name = $this->unserialize();
136 $edecl = $this->resolver->resolveEnum($name);
137 if($edecl === null) {
138 throw new HException("Enum not found " . $name);
141 $index = $this->readDigits();
142 $tag = _hx_array_get(Type::getEnumConstructs($edecl), $index);
144 throw new HException("Unknown enum index " . $name . "@" . _hx_string_rec($index, ""));
146 $e = $this->unserializeEnum($edecl, $tag);
147 $this->cache->push($e);
152 $this->cache->push($l);
154 while(ord(substr($this->buf,$this->pos,1)) !== 104) {
155 $l->add($this->unserialize());
162 $this->cache->push($h);
164 while(ord(substr($this->buf,$this->pos,1)) !== 104) {
165 $s = $this->unserialize();
166 $h->set($s, $this->unserialize());
174 $this->cache->push($h);
176 $c = ord(substr($this->buf,$this->pos++,1));
178 $i = $this->readDigits();
179 $h->set($i, $this->unserialize());
180 $c = ord(substr($this->buf,$this->pos++,1));
184 throw new HException("Invalid IntHash format");
189 $d = Date::fromString(_hx_substr($this->buf, $this->pos, 19));
190 $this->cache->push($d);
195 $len = $this->readDigits();
197 if(ord(substr($this->buf,$this->pos++,1)) !== 58 || $this->length - $this->pos < $len) {
198 throw new HException("Invalid bytes length");
200 $codes = haxe_Unserializer::$CODES;
201 if($codes === null) {
202 $codes = haxe_Unserializer::initCodes();
203 haxe_Unserializer::$CODES = $codes;
207 $size = ($len >> 2) * 3 + (haxe_Unserializer_0($this, $buf, $codes, $i, $len, $rest));
208 $max = $i + ($len - $rest);
209 $bytes = haxe_io_Bytes::alloc($size);
212 $c1 = $codes[ord(substr($buf,$i++,1))];
213 $c2 = $codes[ord(substr($buf,$i++,1))];
214 $bytes->b[$bpos++] = chr($c1 << 2 | $c2 >> 4);
215 $c3 = $codes[ord(substr($buf,$i++,1))];
216 $bytes->b[$bpos++] = chr($c2 << 4 | $c3 >> 2);
217 $c4 = $codes[ord(substr($buf,$i++,1))];
218 $bytes->b[$bpos++] = chr($c3 << 6 | $c4);
219 unset($c4,$c3,$c2,$c1);
222 $c1 = $codes[ord(substr($buf,$i++,1))];
223 $c2 = $codes[ord(substr($buf,$i++,1))];
224 $bytes->b[$bpos++] = chr($c1 << 2 | $c2 >> 4);
226 $c3 = $codes[ord(substr($buf,$i++,1))];
227 $bytes->b[$bpos++] = chr($c2 << 4 | $c3 >> 2);
231 $this->cache->push($bytes);
235 $name = $this->unserialize();
236 $cl = $this->resolver->resolveClass($name);
238 throw new HException("Class not found " . $name);
240 $o = Type::createEmptyInstance($cl);
241 $this->cache->push($o);
242 $o->hxUnserialize($this);
243 if(ord(substr($this->buf,$this->pos++,1)) !== 103) {
244 throw new HException("Invalid custom data");
252 throw new HException("Invalid char " . _hx_char_at($this->buf, $this->pos) . " at position " . _hx_string_rec($this->pos, ""));
254 public function unserializeEnum($edecl, $tag) {
255 if(ord(substr($this->buf,$this->pos++,1)) !== 58) {
256 throw new HException("Invalid enum format");
258 $nargs = $this->readDigits();
260 return Type::createEnum($edecl, $tag, null);
262 $args = new _hx_array(array());
263 while($nargs-- > 0) {
264 $args->push($this->unserialize());
266 return Type::createEnum($edecl, $tag, $args);
268 public function unserializeObject($o) {
270 if($this->pos >= $this->length) {
271 throw new HException("Invalid object");
273 if(ord(substr($this->buf,$this->pos,1)) === 103) {
276 $k = $this->unserialize();
277 if(!Std::is($k, _hx_qtype("String"))) {
278 throw new HException("Invalid object key");
280 $v = $this->unserialize();
286 public function readDigits() {
291 $c = ord(substr($this->buf,$this->pos,1));
296 if($this->pos !== $fpos) {
303 if($c < 48 || $c > 57) {
306 $k = $k * 10 + ($c - 48);
315 public function get($p) {
316 return ord(substr($this->buf,$p,1));
318 public function getResolver() {
319 return $this->resolver;
321 public function setResolver($r) {
323 $this->resolver = _hx_anonymous(array("resolveClass" => array(new _hx_lambda(array(&$r), "haxe_Unserializer_1"), 'execute'), "resolveEnum" => array(new _hx_lambda(array(&$r), "haxe_Unserializer_2"), 'execute')));
325 $this->resolver = $r;
334 public function __call($m, $a) {
335 if(isset($this->$m) && is_callable($this->$m))
336 return call_user_func_array($this->$m, $a);
337 else if(isset($this->»dynamics[$m]) && is_callable($this->»dynamics[$m]))
338 return call_user_func_array($this->»dynamics[$m], $a);
339 else if('toString' == $m)
340 return $this->__toString();
342 throw new HException('Unable to call «'.$m.'»');
344 static $DEFAULT_RESOLVER;
345 static $BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:";
346 static $CODES = null;
347 static function initCodes() {
348 $codes = new _hx_array(array());
350 $_g1 = 0; $_g = strlen(haxe_Unserializer::$BASE64);
353 $codes[ord(substr(haxe_Unserializer::$BASE64,$i,1))] = $i;
359 static function run($v) {
360 return _hx_deref(new haxe_Unserializer($v))->unserialize();
362 function __toString() { return 'haxe.Unserializer'; }
364 haxe_Unserializer::$DEFAULT_RESOLVER = _hx_qtype("Type");
365 function haxe_Unserializer_0(&$»this, &$buf, &$codes, &$i, &$len, &$rest) {
372 function haxe_Unserializer_1(&$r, $_) {
377 function haxe_Unserializer_2(&$r, $_) {