var_unserializer.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. /* Generated by re2c 1.0.1 */
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 7 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2018 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Author: Sascha Schumann <sascha@schumann.cx> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "ext/standard/php_var.h"
  21. #include "php_incomplete_class.h"
  22. #include "zend_portability.h"
  23. struct php_unserialize_data {
  24. void *first;
  25. void *last;
  26. void *first_dtor;
  27. void *last_dtor;
  28. HashTable *allowed_classes;
  29. };
  30. PHPAPI php_unserialize_data_t php_var_unserialize_init() {
  31. php_unserialize_data_t d;
  32. /* fprintf(stderr, "UNSERIALIZE_INIT == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */
  33. if (BG(serialize_lock) || !BG(unserialize).level) {
  34. d = ecalloc(1, sizeof(struct php_unserialize_data));
  35. if (!BG(serialize_lock)) {
  36. BG(unserialize).data = d;
  37. BG(unserialize).level = 1;
  38. }
  39. } else {
  40. d = BG(unserialize).data;
  41. ++BG(unserialize).level;
  42. }
  43. return d;
  44. }
  45. PHPAPI void php_var_unserialize_destroy(php_unserialize_data_t d) {
  46. /* fprintf(stderr, "UNSERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */
  47. if (BG(serialize_lock) || BG(unserialize).level == 1) {
  48. var_destroy(&d);
  49. efree(d);
  50. }
  51. if (!BG(serialize_lock) && !--BG(unserialize).level) {
  52. BG(unserialize).data = NULL;
  53. }
  54. }
  55. PHPAPI HashTable *php_var_unserialize_get_allowed_classes(php_unserialize_data_t d) {
  56. return d->allowed_classes;
  57. }
  58. PHPAPI void php_var_unserialize_set_allowed_classes(php_unserialize_data_t d, HashTable *classes) {
  59. d->allowed_classes = classes;
  60. }
  61. /* {{{ reference-handling for unserializer: var_* */
  62. #define VAR_ENTRIES_MAX 1024
  63. #define VAR_ENTRIES_DBG 0
  64. /* VAR_FLAG used in var_dtor entries to signify an entry on which __wakeup should be called */
  65. #define VAR_WAKEUP_FLAG 1
  66. typedef struct {
  67. zval *data[VAR_ENTRIES_MAX];
  68. zend_long used_slots;
  69. void *next;
  70. } var_entries;
  71. typedef struct {
  72. zval data[VAR_ENTRIES_MAX];
  73. zend_long used_slots;
  74. void *next;
  75. } var_dtor_entries;
  76. static inline void var_push(php_unserialize_data_t *var_hashx, zval *rval)
  77. {
  78. var_entries *var_hash = (*var_hashx)->last;
  79. #if VAR_ENTRIES_DBG
  80. fprintf(stderr, "var_push(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_P(rval));
  81. #endif
  82. if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
  83. var_hash = emalloc(sizeof(var_entries));
  84. var_hash->used_slots = 0;
  85. var_hash->next = 0;
  86. if (!(*var_hashx)->first) {
  87. (*var_hashx)->first = var_hash;
  88. } else {
  89. ((var_entries *) (*var_hashx)->last)->next = var_hash;
  90. }
  91. (*var_hashx)->last = var_hash;
  92. }
  93. var_hash->data[var_hash->used_slots++] = rval;
  94. }
  95. PHPAPI void var_push_dtor(php_unserialize_data_t *var_hashx, zval *rval)
  96. {
  97. zval *tmp_var = var_tmp_var(var_hashx);
  98. if (!tmp_var) {
  99. return;
  100. }
  101. ZVAL_COPY(tmp_var, rval);
  102. }
  103. PHPAPI zval *var_tmp_var(php_unserialize_data_t *var_hashx)
  104. {
  105. var_dtor_entries *var_hash;
  106. if (!var_hashx || !*var_hashx) {
  107. return NULL;
  108. }
  109. var_hash = (*var_hashx)->last_dtor;
  110. if (!var_hash || var_hash->used_slots == VAR_ENTRIES_MAX) {
  111. var_hash = emalloc(sizeof(var_dtor_entries));
  112. var_hash->used_slots = 0;
  113. var_hash->next = 0;
  114. if (!(*var_hashx)->first_dtor) {
  115. (*var_hashx)->first_dtor = var_hash;
  116. } else {
  117. ((var_dtor_entries *) (*var_hashx)->last_dtor)->next = var_hash;
  118. }
  119. (*var_hashx)->last_dtor = var_hash;
  120. }
  121. ZVAL_UNDEF(&var_hash->data[var_hash->used_slots]);
  122. Z_EXTRA(var_hash->data[var_hash->used_slots]) = 0;
  123. return &var_hash->data[var_hash->used_slots++];
  124. }
  125. PHPAPI void var_replace(php_unserialize_data_t *var_hashx, zval *ozval, zval *nzval)
  126. {
  127. zend_long i;
  128. var_entries *var_hash = (*var_hashx)->first;
  129. #if VAR_ENTRIES_DBG
  130. fprintf(stderr, "var_replace(%ld): %d\n", var_hash?var_hash->used_slots:-1L, Z_TYPE_P(nzval));
  131. #endif
  132. while (var_hash) {
  133. for (i = 0; i < var_hash->used_slots; i++) {
  134. if (var_hash->data[i] == ozval) {
  135. var_hash->data[i] = nzval;
  136. /* do not break here */
  137. }
  138. }
  139. var_hash = var_hash->next;
  140. }
  141. }
  142. static zval *var_access(php_unserialize_data_t *var_hashx, zend_long id)
  143. {
  144. var_entries *var_hash = (*var_hashx)->first;
  145. #if VAR_ENTRIES_DBG
  146. fprintf(stderr, "var_access(%ld): %ld\n", var_hash?var_hash->used_slots:-1L, id);
  147. #endif
  148. while (id >= VAR_ENTRIES_MAX && var_hash && var_hash->used_slots == VAR_ENTRIES_MAX) {
  149. var_hash = var_hash->next;
  150. id -= VAR_ENTRIES_MAX;
  151. }
  152. if (!var_hash) return NULL;
  153. if (id < 0 || id >= var_hash->used_slots) return NULL;
  154. return var_hash->data[id];
  155. }
  156. PHPAPI void var_destroy(php_unserialize_data_t *var_hashx)
  157. {
  158. void *next;
  159. zend_long i;
  160. var_entries *var_hash = (*var_hashx)->first;
  161. var_dtor_entries *var_dtor_hash = (*var_hashx)->first_dtor;
  162. zend_bool wakeup_failed = 0;
  163. zval wakeup_name;
  164. ZVAL_UNDEF(&wakeup_name);
  165. #if VAR_ENTRIES_DBG
  166. fprintf(stderr, "var_destroy(%ld)\n", var_hash?var_hash->used_slots:-1L);
  167. #endif
  168. while (var_hash) {
  169. next = var_hash->next;
  170. efree_size(var_hash, sizeof(var_entries));
  171. var_hash = next;
  172. }
  173. while (var_dtor_hash) {
  174. for (i = 0; i < var_dtor_hash->used_slots; i++) {
  175. zval *zv = &var_dtor_hash->data[i];
  176. #if VAR_ENTRIES_DBG
  177. fprintf(stderr, "var_destroy dtor(%p, %ld)\n", var_dtor_hash->data[i], Z_REFCOUNT_P(var_dtor_hash->data[i]));
  178. #endif
  179. /* Perform delayed __wakeup calls */
  180. if (Z_EXTRA_P(zv) == VAR_WAKEUP_FLAG) {
  181. if (!wakeup_failed) {
  182. zval retval;
  183. if (Z_ISUNDEF(wakeup_name)) {
  184. ZVAL_STRINGL(&wakeup_name, "__wakeup", sizeof("__wakeup") - 1);
  185. }
  186. BG(serialize_lock)++;
  187. if (call_user_function(CG(function_table), zv, &wakeup_name, &retval, 0, 0) == FAILURE || Z_ISUNDEF(retval)) {
  188. wakeup_failed = 1;
  189. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  190. }
  191. BG(serialize_lock)--;
  192. zval_ptr_dtor(&retval);
  193. } else {
  194. GC_ADD_FLAGS(Z_OBJ_P(zv), IS_OBJ_DESTRUCTOR_CALLED);
  195. }
  196. }
  197. i_zval_ptr_dtor(zv ZEND_FILE_LINE_CC);
  198. }
  199. next = var_dtor_hash->next;
  200. efree_size(var_dtor_hash, sizeof(var_dtor_entries));
  201. var_dtor_hash = next;
  202. }
  203. zval_ptr_dtor_nogc(&wakeup_name);
  204. }
  205. /* }}} */
  206. static zend_string *unserialize_str(const unsigned char **p, size_t len, size_t maxlen)
  207. {
  208. size_t i, j;
  209. zend_string *str = zend_string_safe_alloc(1, len, 0, 0);
  210. unsigned char *end = *(unsigned char **)p+maxlen;
  211. if (end < *p) {
  212. zend_string_efree(str);
  213. return NULL;
  214. }
  215. for (i = 0; i < len; i++) {
  216. if (*p >= end) {
  217. zend_string_efree(str);
  218. return NULL;
  219. }
  220. if (**p != '\\') {
  221. ZSTR_VAL(str)[i] = (char)**p;
  222. } else {
  223. unsigned char ch = 0;
  224. for (j = 0; j < 2; j++) {
  225. (*p)++;
  226. if (**p >= '0' && **p <= '9') {
  227. ch = (ch << 4) + (**p -'0');
  228. } else if (**p >= 'a' && **p <= 'f') {
  229. ch = (ch << 4) + (**p -'a'+10);
  230. } else if (**p >= 'A' && **p <= 'F') {
  231. ch = (ch << 4) + (**p -'A'+10);
  232. } else {
  233. zend_string_efree(str);
  234. return NULL;
  235. }
  236. }
  237. ZSTR_VAL(str)[i] = (char)ch;
  238. }
  239. (*p)++;
  240. }
  241. ZSTR_VAL(str)[i] = 0;
  242. ZSTR_LEN(str) = i;
  243. return str;
  244. }
  245. static inline int unserialize_allowed_class(
  246. zend_string *class_name, php_unserialize_data_t *var_hashx)
  247. {
  248. HashTable *classes = (*var_hashx)->allowed_classes;
  249. zend_string *lcname;
  250. int res;
  251. ALLOCA_FLAG(use_heap)
  252. if(classes == NULL) {
  253. return 1;
  254. }
  255. if(!zend_hash_num_elements(classes)) {
  256. return 0;
  257. }
  258. ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(class_name), use_heap);
  259. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
  260. res = zend_hash_exists(classes, lcname);
  261. ZSTR_ALLOCA_FREE(lcname, use_heap);
  262. return res;
  263. }
  264. #define YYFILL(n) do { } while (0)
  265. #define YYCTYPE unsigned char
  266. #define YYCURSOR cursor
  267. #define YYLIMIT limit
  268. #define YYMARKER marker
  269. static inline zend_long parse_iv2(const unsigned char *p, const unsigned char **q)
  270. {
  271. zend_ulong result = 0;
  272. zend_ulong neg = 0;
  273. const unsigned char *start;
  274. if (*p == '-') {
  275. neg = 1;
  276. p++;
  277. } else if (UNEXPECTED(*p == '+')) {
  278. p++;
  279. }
  280. while (UNEXPECTED(*p == '0')) {
  281. p++;
  282. }
  283. start = p;
  284. while (*p >= '0' && *p <= '9') {
  285. result = result * 10 + ((zend_ulong)(*p) - '0');
  286. p++;
  287. }
  288. if (q) {
  289. *q = p;
  290. }
  291. /* number too long or overflow */
  292. if (UNEXPECTED(p - start > MAX_LENGTH_OF_LONG - 1)
  293. || (SIZEOF_ZEND_LONG == 4
  294. && UNEXPECTED(p - start == MAX_LENGTH_OF_LONG - 1)
  295. && UNEXPECTED(*start > '2'))
  296. || UNEXPECTED(result > ZEND_LONG_MAX + neg)) {
  297. php_error_docref(NULL, E_WARNING, "Numerical result out of range");
  298. return (!neg) ? ZEND_LONG_MAX : ZEND_LONG_MIN;
  299. }
  300. return (zend_long) ((!neg) ? result : -result);
  301. }
  302. static inline zend_long parse_iv(const unsigned char *p)
  303. {
  304. return parse_iv2(p, NULL);
  305. }
  306. /* no need to check for length - re2c already did */
  307. static inline size_t parse_uiv(const unsigned char *p)
  308. {
  309. unsigned char cursor;
  310. size_t result = 0;
  311. while (1) {
  312. cursor = *p;
  313. if (cursor >= '0' && cursor <= '9') {
  314. result = result * 10 + (size_t)(cursor - (unsigned char)'0');
  315. } else {
  316. break;
  317. }
  318. p++;
  319. }
  320. return result;
  321. }
  322. #define UNSERIALIZE_PARAMETER zval *rval, const unsigned char **p, const unsigned char *max, php_unserialize_data_t *var_hash
  323. #define UNSERIALIZE_PASSTHRU rval, p, max, var_hash
  324. static int php_var_unserialize_internal(UNSERIALIZE_PARAMETER, int as_key);
  325. static zend_always_inline int process_nested_data(UNSERIALIZE_PARAMETER, HashTable *ht, zend_long elements, int objprops)
  326. {
  327. while (elements-- > 0) {
  328. zval key, *data, d, *old_data;
  329. zend_ulong idx;
  330. ZVAL_UNDEF(&key);
  331. if (!php_var_unserialize_internal(&key, p, max, NULL, 1)) {
  332. zval_ptr_dtor(&key);
  333. return 0;
  334. }
  335. data = NULL;
  336. ZVAL_UNDEF(&d);
  337. if (!objprops) {
  338. if (Z_TYPE(key) == IS_LONG) {
  339. idx = Z_LVAL(key);
  340. numeric_key:
  341. if (UNEXPECTED((old_data = zend_hash_index_find(ht, idx)) != NULL)) {
  342. //??? update hash
  343. var_push_dtor(var_hash, old_data);
  344. data = zend_hash_index_update(ht, idx, &d);
  345. } else {
  346. data = zend_hash_index_add_new(ht, idx, &d);
  347. }
  348. } else if (Z_TYPE(key) == IS_STRING) {
  349. if (UNEXPECTED(ZEND_HANDLE_NUMERIC(Z_STR(key), idx))) {
  350. goto numeric_key;
  351. }
  352. if (UNEXPECTED((old_data = zend_hash_find(ht, Z_STR(key))) != NULL)) {
  353. //??? update hash
  354. var_push_dtor(var_hash, old_data);
  355. data = zend_hash_update(ht, Z_STR(key), &d);
  356. } else {
  357. data = zend_hash_add_new(ht, Z_STR(key), &d);
  358. }
  359. } else {
  360. zval_ptr_dtor(&key);
  361. return 0;
  362. }
  363. } else {
  364. if (EXPECTED(Z_TYPE(key) == IS_STRING)) {
  365. string_key:
  366. if (Z_TYPE_P(rval) == IS_OBJECT
  367. && zend_hash_num_elements(&Z_OBJCE_P(rval)->properties_info) > 0) {
  368. zend_property_info *existing_propinfo;
  369. zend_string *new_key;
  370. const char *unmangled_class = NULL;
  371. const char *unmangled_prop;
  372. size_t unmangled_prop_len;
  373. zend_string *unmangled;
  374. if (UNEXPECTED(zend_unmangle_property_name_ex(Z_STR(key), &unmangled_class, &unmangled_prop, &unmangled_prop_len) == FAILURE)) {
  375. zval_ptr_dtor(&key);
  376. return 0;
  377. }
  378. unmangled = zend_string_init(unmangled_prop, unmangled_prop_len, 0);
  379. existing_propinfo = zend_hash_find_ptr(&Z_OBJCE_P(rval)->properties_info, unmangled);
  380. if ((unmangled_class == NULL || !strcmp(unmangled_class, "*") || !strcasecmp(unmangled_class, ZSTR_VAL(Z_OBJCE_P(rval)->name)))
  381. && (existing_propinfo != NULL)
  382. && (existing_propinfo->flags & ZEND_ACC_PPP_MASK)) {
  383. if (existing_propinfo->flags & ZEND_ACC_PROTECTED) {
  384. new_key = zend_mangle_property_name(
  385. "*", 1, ZSTR_VAL(unmangled), ZSTR_LEN(unmangled), 0);
  386. zend_string_release_ex(unmangled, 0);
  387. } else if (existing_propinfo->flags & ZEND_ACC_PRIVATE) {
  388. if (unmangled_class != NULL && strcmp(unmangled_class, "*") != 0) {
  389. new_key = zend_mangle_property_name(
  390. unmangled_class, strlen(unmangled_class),
  391. ZSTR_VAL(unmangled), ZSTR_LEN(unmangled),
  392. 0);
  393. } else {
  394. new_key = zend_mangle_property_name(
  395. ZSTR_VAL(existing_propinfo->ce->name), ZSTR_LEN(existing_propinfo->ce->name),
  396. ZSTR_VAL(unmangled), ZSTR_LEN(unmangled),
  397. 0);
  398. }
  399. zend_string_release_ex(unmangled, 0);
  400. } else {
  401. ZEND_ASSERT(existing_propinfo->flags & ZEND_ACC_PUBLIC);
  402. new_key = unmangled;
  403. }
  404. zval_ptr_dtor_str(&key);
  405. ZVAL_STR(&key, new_key);
  406. } else {
  407. zend_string_release_ex(unmangled, 0);
  408. }
  409. }
  410. if ((old_data = zend_hash_find(ht, Z_STR(key))) != NULL) {
  411. if (Z_TYPE_P(old_data) == IS_INDIRECT) {
  412. old_data = Z_INDIRECT_P(old_data);
  413. }
  414. var_push_dtor(var_hash, old_data);
  415. data = zend_hash_update_ind(ht, Z_STR(key), &d);
  416. } else {
  417. data = zend_hash_add_new(ht, Z_STR(key), &d);
  418. }
  419. } else if (Z_TYPE(key) == IS_LONG) {
  420. /* object properties should include no integers */
  421. convert_to_string(&key);
  422. goto string_key;
  423. } else {
  424. zval_ptr_dtor(&key);
  425. return 0;
  426. }
  427. }
  428. if (!php_var_unserialize_internal(data, p, max, var_hash, 0)) {
  429. zval_ptr_dtor(&key);
  430. return 0;
  431. }
  432. var_push_dtor(var_hash, data);
  433. zval_ptr_dtor_str(&key);
  434. if (elements && *(*p-1) != ';' && *(*p-1) != '}') {
  435. (*p)--;
  436. return 0;
  437. }
  438. }
  439. return 1;
  440. }
  441. static inline int finish_nested_data(UNSERIALIZE_PARAMETER)
  442. {
  443. if (*p >= max || **p != '}') {
  444. return 0;
  445. }
  446. (*p)++;
  447. return 1;
  448. }
  449. static inline int object_custom(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
  450. {
  451. zend_long datalen;
  452. datalen = parse_iv2((*p) + 2, p);
  453. (*p) += 2;
  454. if (datalen < 0 || (max - (*p)) <= datalen) {
  455. zend_error(E_WARNING, "Insufficient data for unserializing - " ZEND_LONG_FMT " required, " ZEND_LONG_FMT " present", datalen, (zend_long)(max - (*p)));
  456. return 0;
  457. }
  458. /* Check that '}' is present before calling ce->unserialize() to mitigate issues
  459. * with unserialize reading past the end of the passed buffer if the string is not
  460. * appropriately terminated (usually NUL terminated, but '}' is also sufficient.) */
  461. if ((*p)[datalen] != '}') {
  462. return 0;
  463. }
  464. if (ce->unserialize == NULL) {
  465. zend_error(E_WARNING, "Class %s has no unserializer", ZSTR_VAL(ce->name));
  466. object_init_ex(rval, ce);
  467. } else if (ce->unserialize(rval, ce, (const unsigned char*)*p, datalen, (zend_unserialize_data *)var_hash) != SUCCESS) {
  468. return 0;
  469. }
  470. (*p) += datalen + 1; /* +1 for '}' */
  471. return 1;
  472. }
  473. static inline zend_long object_common1(UNSERIALIZE_PARAMETER, zend_class_entry *ce)
  474. {
  475. zend_long elements;
  476. if( *p >= max - 2) {
  477. zend_error(E_WARNING, "Bad unserialize data");
  478. return -1;
  479. }
  480. elements = parse_iv2((*p) + 2, p);
  481. (*p) += 2;
  482. if (ce->serialize == NULL) {
  483. object_init_ex(rval, ce);
  484. } else {
  485. /* If this class implements Serializable, it should not land here but in object_custom(). The passed string
  486. obviously doesn't descend from the regular serializer. */
  487. zend_error(E_WARNING, "Erroneous data format for unserializing '%s'", ZSTR_VAL(ce->name));
  488. return -1;
  489. }
  490. return elements;
  491. }
  492. #ifdef PHP_WIN32
  493. # pragma optimize("", off)
  494. #endif
  495. static inline int object_common2(UNSERIALIZE_PARAMETER, zend_long elements)
  496. {
  497. HashTable *ht;
  498. zend_bool has_wakeup;
  499. if (Z_TYPE_P(rval) != IS_OBJECT) {
  500. return 0;
  501. }
  502. has_wakeup = Z_OBJCE_P(rval) != PHP_IC_ENTRY
  503. && zend_hash_str_exists(&Z_OBJCE_P(rval)->function_table, "__wakeup", sizeof("__wakeup")-1);
  504. ht = Z_OBJPROP_P(rval);
  505. if (elements >= (zend_long)(HT_MAX_SIZE - zend_hash_num_elements(ht))) {
  506. return 0;
  507. }
  508. zend_hash_extend(ht, zend_hash_num_elements(ht) + elements, HT_FLAGS(ht) & HASH_FLAG_PACKED);
  509. if (!process_nested_data(UNSERIALIZE_PASSTHRU, ht, elements, 1)) {
  510. if (has_wakeup) {
  511. ZVAL_DEREF(rval);
  512. GC_ADD_FLAGS(Z_OBJ_P(rval), IS_OBJ_DESTRUCTOR_CALLED);
  513. }
  514. return 0;
  515. }
  516. ZVAL_DEREF(rval);
  517. if (has_wakeup) {
  518. /* Delay __wakeup call until end of serialization */
  519. zval *wakeup_var = var_tmp_var(var_hash);
  520. ZVAL_COPY(wakeup_var, rval);
  521. Z_EXTRA_P(wakeup_var) = VAR_WAKEUP_FLAG;
  522. }
  523. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  524. }
  525. #ifdef PHP_WIN32
  526. # pragma optimize("", on)
  527. #endif
  528. PHPAPI int php_var_unserialize(UNSERIALIZE_PARAMETER)
  529. {
  530. var_entries *orig_var_entries = (*var_hash)->last;
  531. zend_long orig_used_slots = orig_var_entries ? orig_var_entries->used_slots : 0;
  532. int result;
  533. result = php_var_unserialize_internal(UNSERIALIZE_PASSTHRU, 0);
  534. if (!result) {
  535. /* If the unserialization failed, mark all elements that have been added to var_hash
  536. * as NULL. This will forbid their use by other unserialize() calls in the same
  537. * unserialization context. */
  538. var_entries *e = orig_var_entries;
  539. zend_long s = orig_used_slots;
  540. while (e) {
  541. for (; s < e->used_slots; s++) {
  542. e->data[s] = NULL;
  543. }
  544. e = e->next;
  545. s = 0;
  546. }
  547. }
  548. return result;
  549. }
  550. static int php_var_unserialize_internal(UNSERIALIZE_PARAMETER, int as_key)
  551. {
  552. const unsigned char *cursor, *limit, *marker, *start;
  553. zval *rval_ref;
  554. limit = max;
  555. cursor = *p;
  556. if (YYCURSOR >= YYLIMIT) {
  557. return 0;
  558. }
  559. if (var_hash && (*p)[0] != 'R') {
  560. var_push(var_hash, rval);
  561. }
  562. start = cursor;
  563. {
  564. YYCTYPE yych;
  565. static const unsigned char yybm[] = {
  566. 0, 0, 0, 0, 0, 0, 0, 0,
  567. 0, 0, 0, 0, 0, 0, 0, 0,
  568. 0, 0, 0, 0, 0, 0, 0, 0,
  569. 0, 0, 0, 0, 0, 0, 0, 0,
  570. 0, 0, 0, 0, 0, 0, 0, 0,
  571. 0, 0, 0, 0, 0, 0, 0, 0,
  572. 128, 128, 128, 128, 128, 128, 128, 128,
  573. 128, 128, 0, 0, 0, 0, 0, 0,
  574. 0, 0, 0, 0, 0, 0, 0, 0,
  575. 0, 0, 0, 0, 0, 0, 0, 0,
  576. 0, 0, 0, 0, 0, 0, 0, 0,
  577. 0, 0, 0, 0, 0, 0, 0, 0,
  578. 0, 0, 0, 0, 0, 0, 0, 0,
  579. 0, 0, 0, 0, 0, 0, 0, 0,
  580. 0, 0, 0, 0, 0, 0, 0, 0,
  581. 0, 0, 0, 0, 0, 0, 0, 0,
  582. 0, 0, 0, 0, 0, 0, 0, 0,
  583. 0, 0, 0, 0, 0, 0, 0, 0,
  584. 0, 0, 0, 0, 0, 0, 0, 0,
  585. 0, 0, 0, 0, 0, 0, 0, 0,
  586. 0, 0, 0, 0, 0, 0, 0, 0,
  587. 0, 0, 0, 0, 0, 0, 0, 0,
  588. 0, 0, 0, 0, 0, 0, 0, 0,
  589. 0, 0, 0, 0, 0, 0, 0, 0,
  590. 0, 0, 0, 0, 0, 0, 0, 0,
  591. 0, 0, 0, 0, 0, 0, 0, 0,
  592. 0, 0, 0, 0, 0, 0, 0, 0,
  593. 0, 0, 0, 0, 0, 0, 0, 0,
  594. 0, 0, 0, 0, 0, 0, 0, 0,
  595. 0, 0, 0, 0, 0, 0, 0, 0,
  596. 0, 0, 0, 0, 0, 0, 0, 0,
  597. 0, 0, 0, 0, 0, 0, 0, 0,
  598. };
  599. if ((YYLIMIT - YYCURSOR) < 7) YYFILL(7);
  600. yych = *YYCURSOR;
  601. switch (yych) {
  602. case 'C':
  603. case 'O': goto yy4;
  604. case 'N': goto yy5;
  605. case 'R': goto yy6;
  606. case 'S': goto yy7;
  607. case 'a': goto yy8;
  608. case 'b': goto yy9;
  609. case 'd': goto yy10;
  610. case 'i': goto yy11;
  611. case 'o': goto yy12;
  612. case 'r': goto yy13;
  613. case 's': goto yy14;
  614. case '}': goto yy15;
  615. default: goto yy2;
  616. }
  617. yy2:
  618. ++YYCURSOR;
  619. yy3:
  620. { return 0; }
  621. yy4:
  622. yych = *(YYMARKER = ++YYCURSOR);
  623. if (yych == ':') goto yy17;
  624. goto yy3;
  625. yy5:
  626. yych = *++YYCURSOR;
  627. if (yych == ';') goto yy19;
  628. goto yy3;
  629. yy6:
  630. yych = *(YYMARKER = ++YYCURSOR);
  631. if (yych == ':') goto yy21;
  632. goto yy3;
  633. yy7:
  634. yych = *(YYMARKER = ++YYCURSOR);
  635. if (yych == ':') goto yy22;
  636. goto yy3;
  637. yy8:
  638. yych = *(YYMARKER = ++YYCURSOR);
  639. if (yych == ':') goto yy23;
  640. goto yy3;
  641. yy9:
  642. yych = *(YYMARKER = ++YYCURSOR);
  643. if (yych == ':') goto yy24;
  644. goto yy3;
  645. yy10:
  646. yych = *(YYMARKER = ++YYCURSOR);
  647. if (yych == ':') goto yy25;
  648. goto yy3;
  649. yy11:
  650. yych = *(YYMARKER = ++YYCURSOR);
  651. if (yych == ':') goto yy26;
  652. goto yy3;
  653. yy12:
  654. yych = *(YYMARKER = ++YYCURSOR);
  655. if (yych == ':') goto yy27;
  656. goto yy3;
  657. yy13:
  658. yych = *(YYMARKER = ++YYCURSOR);
  659. if (yych == ':') goto yy28;
  660. goto yy3;
  661. yy14:
  662. yych = *(YYMARKER = ++YYCURSOR);
  663. if (yych == ':') goto yy29;
  664. goto yy3;
  665. yy15:
  666. ++YYCURSOR;
  667. {
  668. /* this is the case where we have less data than planned */
  669. php_error_docref(NULL, E_NOTICE, "Unexpected end of serialized data");
  670. return 0; /* not sure if it should be 0 or 1 here? */
  671. }
  672. yy17:
  673. yych = *++YYCURSOR;
  674. if (yybm[0+yych] & 128) {
  675. goto yy30;
  676. }
  677. yy18:
  678. YYCURSOR = YYMARKER;
  679. goto yy3;
  680. yy19:
  681. ++YYCURSOR;
  682. {
  683. *p = YYCURSOR;
  684. ZVAL_NULL(rval);
  685. return 1;
  686. }
  687. yy21:
  688. yych = *++YYCURSOR;
  689. if (yych <= '/') goto yy18;
  690. if (yych <= '9') goto yy32;
  691. goto yy18;
  692. yy22:
  693. yych = *++YYCURSOR;
  694. if (yych <= '/') goto yy18;
  695. if (yych <= '9') goto yy34;
  696. goto yy18;
  697. yy23:
  698. yych = *++YYCURSOR;
  699. if (yych <= '/') goto yy18;
  700. if (yych <= '9') goto yy36;
  701. goto yy18;
  702. yy24:
  703. yych = *++YYCURSOR;
  704. if (yych <= '/') goto yy18;
  705. if (yych <= '0') goto yy38;
  706. if (yych <= '1') goto yy39;
  707. goto yy18;
  708. yy25:
  709. yych = *++YYCURSOR;
  710. if (yych <= '/') {
  711. if (yych <= ',') {
  712. if (yych == '+') goto yy40;
  713. goto yy18;
  714. } else {
  715. if (yych <= '-') goto yy41;
  716. if (yych <= '.') goto yy42;
  717. goto yy18;
  718. }
  719. } else {
  720. if (yych <= 'I') {
  721. if (yych <= '9') goto yy43;
  722. if (yych <= 'H') goto yy18;
  723. goto yy45;
  724. } else {
  725. if (yych == 'N') goto yy46;
  726. goto yy18;
  727. }
  728. }
  729. yy26:
  730. yych = *++YYCURSOR;
  731. if (yych <= ',') {
  732. if (yych == '+') goto yy47;
  733. goto yy18;
  734. } else {
  735. if (yych <= '-') goto yy47;
  736. if (yych <= '/') goto yy18;
  737. if (yych <= '9') goto yy48;
  738. goto yy18;
  739. }
  740. yy27:
  741. yych = *++YYCURSOR;
  742. if (yych <= '/') goto yy18;
  743. if (yych <= '9') goto yy50;
  744. goto yy18;
  745. yy28:
  746. yych = *++YYCURSOR;
  747. if (yych <= '/') goto yy18;
  748. if (yych <= '9') goto yy52;
  749. goto yy18;
  750. yy29:
  751. yych = *++YYCURSOR;
  752. if (yych <= '/') goto yy18;
  753. if (yych <= '9') goto yy54;
  754. goto yy18;
  755. yy30:
  756. ++YYCURSOR;
  757. if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
  758. yych = *YYCURSOR;
  759. if (yybm[0+yych] & 128) {
  760. goto yy30;
  761. }
  762. if (yych <= '/') goto yy18;
  763. if (yych <= ':') goto yy56;
  764. goto yy18;
  765. yy32:
  766. ++YYCURSOR;
  767. if (YYLIMIT <= YYCURSOR) YYFILL(1);
  768. yych = *YYCURSOR;
  769. if (yych <= '/') goto yy18;
  770. if (yych <= '9') goto yy32;
  771. if (yych == ';') goto yy57;
  772. goto yy18;
  773. yy34:
  774. ++YYCURSOR;
  775. if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
  776. yych = *YYCURSOR;
  777. if (yych <= '/') goto yy18;
  778. if (yych <= '9') goto yy34;
  779. if (yych <= ':') goto yy59;
  780. goto yy18;
  781. yy36:
  782. ++YYCURSOR;
  783. if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
  784. yych = *YYCURSOR;
  785. if (yych <= '/') goto yy18;
  786. if (yych <= '9') goto yy36;
  787. if (yych <= ':') goto yy60;
  788. goto yy18;
  789. yy38:
  790. yych = *++YYCURSOR;
  791. if (yych == ';') goto yy61;
  792. goto yy18;
  793. yy39:
  794. yych = *++YYCURSOR;
  795. if (yych == ';') goto yy63;
  796. goto yy18;
  797. yy40:
  798. yych = *++YYCURSOR;
  799. if (yych == '.') goto yy42;
  800. if (yych <= '/') goto yy18;
  801. if (yych <= '9') goto yy43;
  802. goto yy18;
  803. yy41:
  804. yych = *++YYCURSOR;
  805. if (yych <= '/') {
  806. if (yych != '.') goto yy18;
  807. } else {
  808. if (yych <= '9') goto yy43;
  809. if (yych == 'I') goto yy45;
  810. goto yy18;
  811. }
  812. yy42:
  813. yych = *++YYCURSOR;
  814. if (yych <= '/') goto yy18;
  815. if (yych <= '9') goto yy65;
  816. goto yy18;
  817. yy43:
  818. ++YYCURSOR;
  819. if ((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
  820. yych = *YYCURSOR;
  821. if (yych <= ':') {
  822. if (yych <= '.') {
  823. if (yych <= '-') goto yy18;
  824. goto yy65;
  825. } else {
  826. if (yych <= '/') goto yy18;
  827. if (yych <= '9') goto yy43;
  828. goto yy18;
  829. }
  830. } else {
  831. if (yych <= 'E') {
  832. if (yych <= ';') goto yy67;
  833. if (yych <= 'D') goto yy18;
  834. goto yy69;
  835. } else {
  836. if (yych == 'e') goto yy69;
  837. goto yy18;
  838. }
  839. }
  840. yy45:
  841. yych = *++YYCURSOR;
  842. if (yych == 'N') goto yy70;
  843. goto yy18;
  844. yy46:
  845. yych = *++YYCURSOR;
  846. if (yych == 'A') goto yy71;
  847. goto yy18;
  848. yy47:
  849. yych = *++YYCURSOR;
  850. if (yych <= '/') goto yy18;
  851. if (yych >= ':') goto yy18;
  852. yy48:
  853. ++YYCURSOR;
  854. if (YYLIMIT <= YYCURSOR) YYFILL(1);
  855. yych = *YYCURSOR;
  856. if (yych <= '/') goto yy18;
  857. if (yych <= '9') goto yy48;
  858. if (yych == ';') goto yy72;
  859. goto yy18;
  860. yy50:
  861. ++YYCURSOR;
  862. if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
  863. yych = *YYCURSOR;
  864. if (yych <= '/') goto yy18;
  865. if (yych <= '9') goto yy50;
  866. if (yych <= ':') goto yy74;
  867. goto yy18;
  868. yy52:
  869. ++YYCURSOR;
  870. if (YYLIMIT <= YYCURSOR) YYFILL(1);
  871. yych = *YYCURSOR;
  872. if (yych <= '/') goto yy18;
  873. if (yych <= '9') goto yy52;
  874. if (yych == ';') goto yy75;
  875. goto yy18;
  876. yy54:
  877. ++YYCURSOR;
  878. if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
  879. yych = *YYCURSOR;
  880. if (yych <= '/') goto yy18;
  881. if (yych <= '9') goto yy54;
  882. if (yych <= ':') goto yy77;
  883. goto yy18;
  884. yy56:
  885. yych = *++YYCURSOR;
  886. if (yych == '"') goto yy78;
  887. goto yy18;
  888. yy57:
  889. ++YYCURSOR;
  890. {
  891. zend_long id;
  892. *p = YYCURSOR;
  893. if (!var_hash) return 0;
  894. id = parse_uiv(start + 2) - 1;
  895. if (id == -1 || (rval_ref = var_access(var_hash, id)) == NULL) {
  896. return 0;
  897. }
  898. if (Z_ISUNDEF_P(rval_ref) || (Z_ISREF_P(rval_ref) && Z_ISUNDEF_P(Z_REFVAL_P(rval_ref)))) {
  899. return 0;
  900. }
  901. if (Z_ISREF_P(rval_ref)) {
  902. ZVAL_COPY(rval, rval_ref);
  903. } else {
  904. ZVAL_NEW_REF(rval_ref, rval_ref);
  905. ZVAL_COPY(rval, rval_ref);
  906. }
  907. return 1;
  908. }
  909. yy59:
  910. yych = *++YYCURSOR;
  911. if (yych == '"') goto yy80;
  912. goto yy18;
  913. yy60:
  914. yych = *++YYCURSOR;
  915. if (yych == '{') goto yy82;
  916. goto yy18;
  917. yy61:
  918. ++YYCURSOR;
  919. {
  920. *p = YYCURSOR;
  921. ZVAL_FALSE(rval);
  922. return 1;
  923. }
  924. yy63:
  925. ++YYCURSOR;
  926. {
  927. *p = YYCURSOR;
  928. ZVAL_TRUE(rval);
  929. return 1;
  930. }
  931. yy65:
  932. ++YYCURSOR;
  933. if ((YYLIMIT - YYCURSOR) < 3) YYFILL(3);
  934. yych = *YYCURSOR;
  935. if (yych <= ';') {
  936. if (yych <= '/') goto yy18;
  937. if (yych <= '9') goto yy65;
  938. if (yych <= ':') goto yy18;
  939. } else {
  940. if (yych <= 'E') {
  941. if (yych <= 'D') goto yy18;
  942. goto yy69;
  943. } else {
  944. if (yych == 'e') goto yy69;
  945. goto yy18;
  946. }
  947. }
  948. yy67:
  949. ++YYCURSOR;
  950. {
  951. #if SIZEOF_ZEND_LONG == 4
  952. use_double:
  953. #endif
  954. *p = YYCURSOR;
  955. ZVAL_DOUBLE(rval, zend_strtod((const char *)start + 2, NULL));
  956. return 1;
  957. }
  958. yy69:
  959. yych = *++YYCURSOR;
  960. if (yych <= ',') {
  961. if (yych == '+') goto yy84;
  962. goto yy18;
  963. } else {
  964. if (yych <= '-') goto yy84;
  965. if (yych <= '/') goto yy18;
  966. if (yych <= '9') goto yy85;
  967. goto yy18;
  968. }
  969. yy70:
  970. yych = *++YYCURSOR;
  971. if (yych == 'F') goto yy87;
  972. goto yy18;
  973. yy71:
  974. yych = *++YYCURSOR;
  975. if (yych == 'N') goto yy87;
  976. goto yy18;
  977. yy72:
  978. ++YYCURSOR;
  979. {
  980. #if SIZEOF_ZEND_LONG == 4
  981. int digits = YYCURSOR - start - 3;
  982. if (start[2] == '-' || start[2] == '+') {
  983. digits--;
  984. }
  985. /* Use double for large zend_long values that were serialized on a 64-bit system */
  986. if (digits >= MAX_LENGTH_OF_LONG - 1) {
  987. if (digits == MAX_LENGTH_OF_LONG - 1) {
  988. int cmp = strncmp((char*)YYCURSOR - MAX_LENGTH_OF_LONG, long_min_digits, MAX_LENGTH_OF_LONG - 1);
  989. if (!(cmp < 0 || (cmp == 0 && start[2] == '-'))) {
  990. goto use_double;
  991. }
  992. } else {
  993. goto use_double;
  994. }
  995. }
  996. #endif
  997. *p = YYCURSOR;
  998. ZVAL_LONG(rval, parse_iv(start + 2));
  999. return 1;
  1000. }
  1001. yy74:
  1002. yych = *++YYCURSOR;
  1003. if (yych == '"') goto yy88;
  1004. goto yy18;
  1005. yy75:
  1006. ++YYCURSOR;
  1007. {
  1008. zend_long id;
  1009. *p = YYCURSOR;
  1010. if (!var_hash) return 0;
  1011. id = parse_uiv(start + 2) - 1;
  1012. if (id == -1 || (rval_ref = var_access(var_hash, id)) == NULL) {
  1013. return 0;
  1014. }
  1015. if (rval_ref == rval) {
  1016. return 0;
  1017. }
  1018. ZVAL_DEREF(rval_ref);
  1019. if (Z_TYPE_P(rval_ref) != IS_OBJECT) {
  1020. return 0;
  1021. }
  1022. ZVAL_COPY(rval, rval_ref);
  1023. return 1;
  1024. }
  1025. yy77:
  1026. yych = *++YYCURSOR;
  1027. if (yych == '"') goto yy90;
  1028. goto yy18;
  1029. yy78:
  1030. ++YYCURSOR;
  1031. {
  1032. size_t len, len2, len3, maxlen;
  1033. zend_long elements;
  1034. char *str;
  1035. zend_string *class_name;
  1036. zend_class_entry *ce;
  1037. int incomplete_class = 0;
  1038. int custom_object = 0;
  1039. zval user_func;
  1040. zval retval;
  1041. zval args[1];
  1042. if (!var_hash) return 0;
  1043. if (*start == 'C') {
  1044. custom_object = 1;
  1045. }
  1046. len2 = len = parse_uiv(start + 2);
  1047. maxlen = max - YYCURSOR;
  1048. if (maxlen < len || len == 0) {
  1049. *p = start + 2;
  1050. return 0;
  1051. }
  1052. str = (char*)YYCURSOR;
  1053. YYCURSOR += len;
  1054. if (*(YYCURSOR) != '"') {
  1055. *p = YYCURSOR;
  1056. return 0;
  1057. }
  1058. if (*(YYCURSOR+1) != ':') {
  1059. *p = YYCURSOR+1;
  1060. return 0;
  1061. }
  1062. len3 = strspn(str, "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\");
  1063. if (len3 != len)
  1064. {
  1065. *p = YYCURSOR + len3 - len;
  1066. return 0;
  1067. }
  1068. class_name = zend_string_init(str, len, 0);
  1069. do {
  1070. if(!unserialize_allowed_class(class_name, var_hash)) {
  1071. incomplete_class = 1;
  1072. ce = PHP_IC_ENTRY;
  1073. break;
  1074. }
  1075. /* Try to find class directly */
  1076. BG(serialize_lock)++;
  1077. ce = zend_lookup_class(class_name);
  1078. if (ce) {
  1079. BG(serialize_lock)--;
  1080. if (EG(exception)) {
  1081. zend_string_release_ex(class_name, 0);
  1082. return 0;
  1083. }
  1084. break;
  1085. }
  1086. BG(serialize_lock)--;
  1087. if (EG(exception)) {
  1088. zend_string_release_ex(class_name, 0);
  1089. return 0;
  1090. }
  1091. /* Check for unserialize callback */
  1092. if ((PG(unserialize_callback_func) == NULL) || (PG(unserialize_callback_func)[0] == '\0')) {
  1093. incomplete_class = 1;
  1094. ce = PHP_IC_ENTRY;
  1095. break;
  1096. }
  1097. /* Call unserialize callback */
  1098. ZVAL_STRING(&user_func, PG(unserialize_callback_func));
  1099. ZVAL_STR_COPY(&args[0], class_name);
  1100. BG(serialize_lock)++;
  1101. if (call_user_function_ex(CG(function_table), NULL, &user_func, &retval, 1, args, 0, NULL) != SUCCESS) {
  1102. BG(serialize_lock)--;
  1103. if (EG(exception)) {
  1104. zend_string_release_ex(class_name, 0);
  1105. zval_ptr_dtor(&user_func);
  1106. zval_ptr_dtor(&args[0]);
  1107. return 0;
  1108. }
  1109. php_error_docref(NULL, E_WARNING, "defined (%s) but not found", Z_STRVAL(user_func));
  1110. incomplete_class = 1;
  1111. ce = PHP_IC_ENTRY;
  1112. zval_ptr_dtor(&user_func);
  1113. zval_ptr_dtor(&args[0]);
  1114. break;
  1115. }
  1116. BG(serialize_lock)--;
  1117. zval_ptr_dtor(&retval);
  1118. if (EG(exception)) {
  1119. zend_string_release_ex(class_name, 0);
  1120. zval_ptr_dtor(&user_func);
  1121. zval_ptr_dtor(&args[0]);
  1122. return 0;
  1123. }
  1124. /* The callback function may have defined the class */
  1125. BG(serialize_lock)++;
  1126. if ((ce = zend_lookup_class(class_name)) == NULL) {
  1127. php_error_docref(NULL, E_WARNING, "Function %s() hasn't defined the class it was called for", Z_STRVAL(user_func));
  1128. incomplete_class = 1;
  1129. ce = PHP_IC_ENTRY;
  1130. }
  1131. BG(serialize_lock)--;
  1132. zval_ptr_dtor(&user_func);
  1133. zval_ptr_dtor(&args[0]);
  1134. break;
  1135. } while (1);
  1136. *p = YYCURSOR;
  1137. if (custom_object) {
  1138. int ret;
  1139. ret = object_custom(UNSERIALIZE_PASSTHRU, ce);
  1140. if (ret && incomplete_class) {
  1141. php_store_class_name(rval, ZSTR_VAL(class_name), len2);
  1142. }
  1143. zend_string_release_ex(class_name, 0);
  1144. return ret;
  1145. }
  1146. elements = object_common1(UNSERIALIZE_PASSTHRU, ce);
  1147. if (elements < 0) {
  1148. zend_string_release_ex(class_name, 0);
  1149. return 0;
  1150. }
  1151. if (incomplete_class) {
  1152. php_store_class_name(rval, ZSTR_VAL(class_name), len2);
  1153. }
  1154. zend_string_release_ex(class_name, 0);
  1155. return object_common2(UNSERIALIZE_PASSTHRU, elements);
  1156. }
  1157. yy80:
  1158. ++YYCURSOR;
  1159. {
  1160. size_t len, maxlen;
  1161. zend_string *str;
  1162. len = parse_uiv(start + 2);
  1163. maxlen = max - YYCURSOR;
  1164. if (maxlen < len) {
  1165. *p = start + 2;
  1166. return 0;
  1167. }
  1168. if ((str = unserialize_str(&YYCURSOR, len, maxlen)) == NULL) {
  1169. return 0;
  1170. }
  1171. if (*(YYCURSOR) != '"') {
  1172. zend_string_efree(str);
  1173. *p = YYCURSOR;
  1174. return 0;
  1175. }
  1176. if (*(YYCURSOR + 1) != ';') {
  1177. efree(str);
  1178. *p = YYCURSOR + 1;
  1179. return 0;
  1180. }
  1181. YYCURSOR += 2;
  1182. *p = YYCURSOR;
  1183. ZVAL_STR(rval, str);
  1184. return 1;
  1185. }
  1186. yy82:
  1187. ++YYCURSOR;
  1188. {
  1189. zend_long elements = parse_iv(start + 2);
  1190. /* use iv() not uiv() in order to check data range */
  1191. *p = YYCURSOR;
  1192. if (!var_hash) return 0;
  1193. if (elements < 0 || elements >= HT_MAX_SIZE) {
  1194. return 0;
  1195. }
  1196. if (elements) {
  1197. array_init_size(rval, elements);
  1198. /* we can't convert from packed to hash during unserialization, because
  1199. reference to some zvals might be keept in var_hash (to support references) */
  1200. zend_hash_real_init_mixed(Z_ARRVAL_P(rval));
  1201. } else {
  1202. ZVAL_EMPTY_ARRAY(rval);
  1203. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  1204. }
  1205. /* The array may contain references to itself, in which case we'll be modifying an
  1206. * rc>1 array. This is okay, since the array is, ostensibly, only visible to
  1207. * unserialize (in practice unserialization handlers also see it). Ideally we should
  1208. * prohibit "r:" references to non-objects, as we only generate them for objects. */
  1209. HT_ALLOW_COW_VIOLATION(Z_ARRVAL_P(rval));
  1210. if (!process_nested_data(UNSERIALIZE_PASSTHRU, Z_ARRVAL_P(rval), elements, 0)) {
  1211. return 0;
  1212. }
  1213. return finish_nested_data(UNSERIALIZE_PASSTHRU);
  1214. }
  1215. yy84:
  1216. yych = *++YYCURSOR;
  1217. if (yych <= '/') goto yy18;
  1218. if (yych >= ':') goto yy18;
  1219. yy85:
  1220. ++YYCURSOR;
  1221. if (YYLIMIT <= YYCURSOR) YYFILL(1);
  1222. yych = *YYCURSOR;
  1223. if (yych <= '/') goto yy18;
  1224. if (yych <= '9') goto yy85;
  1225. if (yych == ';') goto yy67;
  1226. goto yy18;
  1227. yy87:
  1228. yych = *++YYCURSOR;
  1229. if (yych == ';') goto yy92;
  1230. goto yy18;
  1231. yy88:
  1232. ++YYCURSOR;
  1233. {
  1234. zend_long elements;
  1235. if (!var_hash) return 0;
  1236. elements = object_common1(UNSERIALIZE_PASSTHRU, ZEND_STANDARD_CLASS_DEF_PTR);
  1237. if (elements < 0 || elements >= HT_MAX_SIZE) {
  1238. return 0;
  1239. }
  1240. return object_common2(UNSERIALIZE_PASSTHRU, elements);
  1241. }
  1242. yy90:
  1243. ++YYCURSOR;
  1244. {
  1245. size_t len, maxlen;
  1246. char *str;
  1247. len = parse_uiv(start + 2);
  1248. maxlen = max - YYCURSOR;
  1249. if (maxlen < len) {
  1250. *p = start + 2;
  1251. return 0;
  1252. }
  1253. str = (char*)YYCURSOR;
  1254. YYCURSOR += len;
  1255. if (*(YYCURSOR) != '"') {
  1256. *p = YYCURSOR;
  1257. return 0;
  1258. }
  1259. if (*(YYCURSOR + 1) != ';') {
  1260. *p = YYCURSOR + 1;
  1261. return 0;
  1262. }
  1263. YYCURSOR += 2;
  1264. *p = YYCURSOR;
  1265. if (len == 0) {
  1266. ZVAL_EMPTY_STRING(rval);
  1267. } else if (len == 1) {
  1268. ZVAL_INTERNED_STR(rval, ZSTR_CHAR((zend_uchar)*str));
  1269. } else if (as_key) {
  1270. ZVAL_STR(rval, zend_string_init_interned(str, len, 0));
  1271. } else {
  1272. ZVAL_STRINGL(rval, str, len);
  1273. }
  1274. return 1;
  1275. }
  1276. yy92:
  1277. ++YYCURSOR;
  1278. {
  1279. *p = YYCURSOR;
  1280. if (!strncmp((char*)start + 2, "NAN", 3)) {
  1281. ZVAL_DOUBLE(rval, ZEND_NAN);
  1282. } else if (!strncmp((char*)start + 2, "INF", 3)) {
  1283. ZVAL_DOUBLE(rval, ZEND_INFINITY);
  1284. } else if (!strncmp((char*)start + 2, "-INF", 4)) {
  1285. ZVAL_DOUBLE(rval, -ZEND_INFINITY);
  1286. } else {
  1287. ZVAL_NULL(rval);
  1288. }
  1289. return 1;
  1290. }
  1291. }
  1292. return 0;
  1293. }