xref: /PHP-7.4/main/rfc1867.c (revision 5d31ee30)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
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    +----------------------------------------------------------------------+
15    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
16    |          Jani Taskinen <jani@php.net>                                |
17    +----------------------------------------------------------------------+
18  */
19 
20 /*
21  *  This product includes software developed by the Apache Group
22  *  for use in the Apache HTTP server project (http://www.apache.org/).
23  *
24  */
25 
26 #include <stdio.h>
27 #include "php.h"
28 #include "php_open_temporary_file.h"
29 #include "zend_globals.h"
30 #include "php_globals.h"
31 #include "php_variables.h"
32 #include "rfc1867.h"
33 #include "ext/standard/php_string.h"
34 #include "zend_smart_string.h"
35 
36 #if defined(PHP_WIN32) && !defined(HAVE_ATOLL)
37 # define atoll(s) _atoi64(s)
38 # define HAVE_ATOLL 1
39 #endif
40 
41 #ifndef DEBUG_FILE_UPLOAD
42 # define DEBUG_FILE_UPLOAD 0
43 #endif
44 
dummy_encoding_translation(void)45 static int dummy_encoding_translation(void)
46 {
47 	return 0;
48 }
49 
50 static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop);
51 static char *php_ap_getword_conf(const zend_encoding *encoding, char *str);
52 
53 static php_rfc1867_encoding_translation_t php_rfc1867_encoding_translation = dummy_encoding_translation;
54 static php_rfc1867_get_detect_order_t php_rfc1867_get_detect_order = NULL;
55 static php_rfc1867_set_input_encoding_t php_rfc1867_set_input_encoding = NULL;
56 static php_rfc1867_getword_t php_rfc1867_getword = php_ap_getword;
57 static php_rfc1867_getword_conf_t php_rfc1867_getword_conf = php_ap_getword_conf;
58 static php_rfc1867_basename_t php_rfc1867_basename = NULL;
59 
60 PHPAPI int (*php_rfc1867_callback)(unsigned int event, void *event_data, void **extra) = NULL;
61 
62 static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, zend_bool override_protection);
63 
64 /* The longest property name we use in an uploaded file array */
65 #define MAX_SIZE_OF_INDEX sizeof("[tmp_name]")
66 
67 /* The longest anonymous name */
68 #define MAX_SIZE_ANONNAME 33
69 
70 /* Errors */
71 #define UPLOAD_ERROR_OK   0  /* File upload successful */
72 #define UPLOAD_ERROR_A    1  /* Uploaded file exceeded upload_max_filesize */
73 #define UPLOAD_ERROR_B    2  /* Uploaded file exceeded MAX_FILE_SIZE */
74 #define UPLOAD_ERROR_C    3  /* Partially uploaded */
75 #define UPLOAD_ERROR_D    4  /* No file uploaded */
76 #define UPLOAD_ERROR_E    6  /* Missing /tmp or similar directory */
77 #define UPLOAD_ERROR_F    7  /* Failed to write file to disk */
78 #define UPLOAD_ERROR_X    8  /* File upload stopped by extension */
79 
php_rfc1867_register_constants(void)80 void php_rfc1867_register_constants(void) /* {{{ */
81 {
82 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_OK",         UPLOAD_ERROR_OK, CONST_CS | CONST_PERSISTENT);
83 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_INI_SIZE",   UPLOAD_ERROR_A,  CONST_CS | CONST_PERSISTENT);
84 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_FORM_SIZE",  UPLOAD_ERROR_B,  CONST_CS | CONST_PERSISTENT);
85 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_PARTIAL",    UPLOAD_ERROR_C,  CONST_CS | CONST_PERSISTENT);
86 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_NO_FILE",    UPLOAD_ERROR_D,  CONST_CS | CONST_PERSISTENT);
87 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_NO_TMP_DIR", UPLOAD_ERROR_E,  CONST_CS | CONST_PERSISTENT);
88 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_CANT_WRITE", UPLOAD_ERROR_F,  CONST_CS | CONST_PERSISTENT);
89 	REGISTER_MAIN_LONG_CONSTANT("UPLOAD_ERR_EXTENSION",  UPLOAD_ERROR_X,  CONST_CS | CONST_PERSISTENT);
90 }
91 /* }}} */
92 
normalize_protected_variable(char * varname)93 static void normalize_protected_variable(char *varname) /* {{{ */
94 {
95 	char *s = varname, *index = NULL, *indexend = NULL, *p;
96 
97 	/* overjump leading space */
98 	while (*s == ' ') {
99 		s++;
100 	}
101 
102 	/* and remove it */
103 	if (s != varname) {
104 		memmove(varname, s, strlen(s)+1);
105 	}
106 
107 	for (p = varname; *p && *p != '['; p++) {
108 		switch(*p) {
109 			case ' ':
110 			case '.':
111 				*p = '_';
112 				break;
113 		}
114 	}
115 
116 	/* find index */
117 	index = strchr(varname, '[');
118 	if (index) {
119 		index++;
120 		s = index;
121 	} else {
122 		return;
123 	}
124 
125 	/* done? */
126 	while (index) {
127 		while (*index == ' ' || *index == '\r' || *index == '\n' || *index=='\t') {
128 			index++;
129 		}
130 		indexend = strchr(index, ']');
131 		indexend = indexend ? indexend + 1 : index + strlen(index);
132 
133 		if (s != index) {
134 			memmove(s, index, strlen(index)+1);
135 			s += indexend-index;
136 		} else {
137 			s = indexend;
138 		}
139 
140 		if (*s == '[') {
141 			s++;
142 			index = s;
143 		} else {
144 			index = NULL;
145 		}
146 	}
147 	*s = '\0';
148 }
149 /* }}} */
150 
add_protected_variable(char * varname)151 static void add_protected_variable(char *varname) /* {{{ */
152 {
153 	normalize_protected_variable(varname);
154 	zend_hash_str_add_empty_element(&PG(rfc1867_protected_variables), varname, strlen(varname));
155 }
156 /* }}} */
157 
is_protected_variable(char * varname)158 static zend_bool is_protected_variable(char *varname) /* {{{ */
159 {
160 	normalize_protected_variable(varname);
161 	return zend_hash_str_exists(&PG(rfc1867_protected_variables), varname, strlen(varname));
162 }
163 /* }}} */
164 
safe_php_register_variable(char * var,char * strval,size_t val_len,zval * track_vars_array,zend_bool override_protection)165 static void safe_php_register_variable(char *var, char *strval, size_t val_len, zval *track_vars_array, zend_bool override_protection) /* {{{ */
166 {
167 	if (override_protection || !is_protected_variable(var)) {
168 		php_register_variable_safe(var, strval, val_len, track_vars_array);
169 	}
170 }
171 /* }}} */
172 
safe_php_register_variable_ex(char * var,zval * val,zval * track_vars_array,zend_bool override_protection)173 static void safe_php_register_variable_ex(char *var, zval *val, zval *track_vars_array, zend_bool override_protection) /* {{{ */
174 {
175 	if (override_protection || !is_protected_variable(var)) {
176 		php_register_variable_ex(var, val, track_vars_array);
177 	}
178 }
179 /* }}} */
180 
register_http_post_files_variable(char * strvar,char * val,zval * http_post_files,zend_bool override_protection)181 static void register_http_post_files_variable(char *strvar, char *val, zval *http_post_files, zend_bool override_protection) /* {{{ */
182 {
183 	safe_php_register_variable(strvar, val, strlen(val), http_post_files, override_protection);
184 }
185 /* }}} */
186 
register_http_post_files_variable_ex(char * var,zval * val,zval * http_post_files,zend_bool override_protection)187 static void register_http_post_files_variable_ex(char *var, zval *val, zval *http_post_files, zend_bool override_protection) /* {{{ */
188 {
189 	safe_php_register_variable_ex(var, val, http_post_files, override_protection);
190 }
191 /* }}} */
192 
free_filename(zval * el)193 static void free_filename(zval *el) {
194 	zend_string *filename = Z_STR_P(el);
195 	zend_string_release_ex(filename, 0);
196 }
197 
destroy_uploaded_files_hash(void)198 PHPAPI void destroy_uploaded_files_hash(void) /* {{{ */
199 {
200 	zval *el;
201 
202 	ZEND_HASH_FOREACH_VAL(SG(rfc1867_uploaded_files), el) {
203 		zend_string *filename = Z_STR_P(el);
204 		VCWD_UNLINK(ZSTR_VAL(filename));
205 	} ZEND_HASH_FOREACH_END();
206 	zend_hash_destroy(SG(rfc1867_uploaded_files));
207 	FREE_HASHTABLE(SG(rfc1867_uploaded_files));
208 }
209 /* }}} */
210 
211 /* {{{ Following code is based on apache_multipart_buffer.c from libapreq-0.33 package. */
212 
213 #define FILLUNIT (1024 * 5)
214 
215 typedef struct {
216 
217 	/* read buffer */
218 	char *buffer;
219 	char *buf_begin;
220 	int  bufsize;
221 	int  bytes_in_buffer;
222 
223 	/* boundary info */
224 	char *boundary;
225 	char *boundary_next;
226 	int  boundary_next_len;
227 
228 	const zend_encoding *input_encoding;
229 	const zend_encoding **detect_order;
230 	size_t detect_order_size;
231 } multipart_buffer;
232 
233 typedef struct {
234 	char *key;
235 	char *value;
236 } mime_header_entry;
237 
238 /*
239  * Fill up the buffer with client data.
240  * Returns number of bytes added to buffer.
241  */
fill_buffer(multipart_buffer * self)242 static int fill_buffer(multipart_buffer *self)
243 {
244 	int bytes_to_read, total_read = 0, actual_read = 0;
245 
246 	/* shift the existing data if necessary */
247 	if (self->bytes_in_buffer > 0 && self->buf_begin != self->buffer) {
248 		memmove(self->buffer, self->buf_begin, self->bytes_in_buffer);
249 	}
250 
251 	self->buf_begin = self->buffer;
252 
253 	/* calculate the free space in the buffer */
254 	bytes_to_read = self->bufsize - self->bytes_in_buffer;
255 
256 	/* read the required number of bytes */
257 	while (bytes_to_read > 0) {
258 
259 		char *buf = self->buffer + self->bytes_in_buffer;
260 
261 		actual_read = (int)sapi_module.read_post(buf, bytes_to_read);
262 
263 		/* update the buffer length */
264 		if (actual_read > 0) {
265 			self->bytes_in_buffer += actual_read;
266 			SG(read_post_bytes) += actual_read;
267 			total_read += actual_read;
268 			bytes_to_read -= actual_read;
269 		} else {
270 			break;
271 		}
272 	}
273 
274 	return total_read;
275 }
276 
277 /* eof if we are out of bytes, or if we hit the final boundary */
multipart_buffer_eof(multipart_buffer * self)278 static int multipart_buffer_eof(multipart_buffer *self)
279 {
280 	return self->bytes_in_buffer == 0 && fill_buffer(self) < 1;
281 }
282 
283 /* create new multipart_buffer structure */
multipart_buffer_new(char * boundary,int boundary_len)284 static multipart_buffer *multipart_buffer_new(char *boundary, int boundary_len)
285 {
286 	multipart_buffer *self = (multipart_buffer *) ecalloc(1, sizeof(multipart_buffer));
287 
288 	int minsize = boundary_len + 6;
289 	if (minsize < FILLUNIT) minsize = FILLUNIT;
290 
291 	self->buffer = (char *) ecalloc(1, minsize + 1);
292 	self->bufsize = minsize;
293 
294 	spprintf(&self->boundary, 0, "--%s", boundary);
295 
296 	self->boundary_next_len = (int)spprintf(&self->boundary_next, 0, "\n--%s", boundary);
297 
298 	self->buf_begin = self->buffer;
299 	self->bytes_in_buffer = 0;
300 
301 	if (php_rfc1867_encoding_translation()) {
302 		php_rfc1867_get_detect_order(&self->detect_order, &self->detect_order_size);
303 	} else {
304 		self->detect_order = NULL;
305 		self->detect_order_size = 0;
306 	}
307 
308 	self->input_encoding = NULL;
309 
310 	return self;
311 }
312 
313 /*
314  * Gets the next CRLF terminated line from the input buffer.
315  * If it doesn't find a CRLF, and the buffer isn't completely full, returns
316  * NULL; otherwise, returns the beginning of the null-terminated line,
317  * minus the CRLF.
318  *
319  * Note that we really just look for LF terminated lines. This works
320  * around a bug in internet explorer for the macintosh which sends mime
321  * boundaries that are only LF terminated when you use an image submit
322  * button in a multipart/form-data form.
323  */
next_line(multipart_buffer * self)324 static char *next_line(multipart_buffer *self)
325 {
326 	/* look for LF in the data */
327 	char* line = self->buf_begin;
328 	char* ptr = memchr(self->buf_begin, '\n', self->bytes_in_buffer);
329 
330 	if (ptr) {	/* LF found */
331 
332 		/* terminate the string, remove CRLF */
333 		if ((ptr - line) > 0 && *(ptr-1) == '\r') {
334 			*(ptr-1) = 0;
335 		} else {
336 			*ptr = 0;
337 		}
338 
339 		/* bump the pointer */
340 		self->buf_begin = ptr + 1;
341 		self->bytes_in_buffer -= (self->buf_begin - line);
342 
343 	} else {	/* no LF found */
344 
345 		/* buffer isn't completely full, fail */
346 		if (self->bytes_in_buffer < self->bufsize) {
347 			return NULL;
348 		}
349 		/* return entire buffer as a partial line */
350 		line[self->bufsize] = 0;
351 		self->buf_begin = ptr;
352 		self->bytes_in_buffer = 0;
353 	}
354 
355 	return line;
356 }
357 
358 /* Returns the next CRLF terminated line from the client */
get_line(multipart_buffer * self)359 static char *get_line(multipart_buffer *self)
360 {
361 	char* ptr = next_line(self);
362 
363 	if (!ptr) {
364 		fill_buffer(self);
365 		ptr = next_line(self);
366 	}
367 
368 	return ptr;
369 }
370 
371 /* Free header entry */
php_free_hdr_entry(mime_header_entry * h)372 static void php_free_hdr_entry(mime_header_entry *h)
373 {
374 	if (h->key) {
375 		efree(h->key);
376 	}
377 	if (h->value) {
378 		efree(h->value);
379 	}
380 }
381 
382 /* finds a boundary */
find_boundary(multipart_buffer * self,char * boundary)383 static int find_boundary(multipart_buffer *self, char *boundary)
384 {
385 	char *line;
386 
387 	/* loop through lines */
388 	while( (line = get_line(self)) )
389 	{
390 		/* finished if we found the boundary */
391 		if (!strcmp(line, boundary)) {
392 			return 1;
393 		}
394 	}
395 
396 	/* didn't find the boundary */
397 	return 0;
398 }
399 
400 /* parse headers */
multipart_buffer_headers(multipart_buffer * self,zend_llist * header)401 static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header)
402 {
403 	char *line;
404 	mime_header_entry entry = {0};
405 	smart_string buf_value = {0};
406 	char *key = NULL;
407 
408 	/* didn't find boundary, abort */
409 	if (!find_boundary(self, self->boundary)) {
410 		return 0;
411 	}
412 
413 	/* get lines of text, or CRLF_CRLF */
414 
415 	while ((line = get_line(self)) && line[0] != '\0') {
416 		/* add header to table */
417 		char *value = NULL;
418 
419 		if (php_rfc1867_encoding_translation()) {
420 			self->input_encoding = zend_multibyte_encoding_detector((const unsigned char *) line, strlen(line), self->detect_order, self->detect_order_size);
421 		}
422 
423 		/* space in the beginning means same header */
424 		if (!isspace(line[0])) {
425 			value = strchr(line, ':');
426 		}
427 
428 		if (value) {
429 			if (buf_value.c && key) {
430 				/* new entry, add the old one to the list */
431 				smart_string_0(&buf_value);
432 				entry.key = key;
433 				entry.value = buf_value.c;
434 				zend_llist_add_element(header, &entry);
435 				buf_value.c = NULL;
436 				key = NULL;
437 			}
438 
439 			*value = '\0';
440 			do { value++; } while (isspace(*value));
441 
442 			key = estrdup(line);
443 			smart_string_appends(&buf_value, value);
444 		} else if (buf_value.c) { /* If no ':' on the line, add to previous line */
445 			smart_string_appends(&buf_value, line);
446 		} else {
447 			continue;
448 		}
449 	}
450 
451 	if (buf_value.c && key) {
452 		/* add the last one to the list */
453 		smart_string_0(&buf_value);
454 		entry.key = key;
455 		entry.value = buf_value.c;
456 		zend_llist_add_element(header, &entry);
457 	}
458 
459 	return 1;
460 }
461 
php_mime_get_hdr_value(zend_llist header,char * key)462 static char *php_mime_get_hdr_value(zend_llist header, char *key)
463 {
464 	mime_header_entry *entry;
465 
466 	if (key == NULL) {
467 		return NULL;
468 	}
469 
470 	entry = zend_llist_get_first(&header);
471 	while (entry) {
472 		if (!strcasecmp(entry->key, key)) {
473 			return entry->value;
474 		}
475 		entry = zend_llist_get_next(&header);
476 	}
477 
478 	return NULL;
479 }
480 
php_ap_getword(const zend_encoding * encoding,char ** line,char stop)481 static char *php_ap_getword(const zend_encoding *encoding, char **line, char stop)
482 {
483 	char *pos = *line, quote;
484 	char *res;
485 
486 	while (*pos && *pos != stop) {
487 		if ((quote = *pos) == '"' || quote == '\'') {
488 			++pos;
489 			while (*pos && *pos != quote) {
490 				if (*pos == '\\' && pos[1] && pos[1] == quote) {
491 					pos += 2;
492 				} else {
493 					++pos;
494 				}
495 			}
496 			if (*pos) {
497 				++pos;
498 			}
499 		} else ++pos;
500 	}
501 	if (*pos == '\0') {
502 		res = estrdup(*line);
503 		*line += strlen(*line);
504 		return res;
505 	}
506 
507 	res = estrndup(*line, pos - *line);
508 
509 	while (*pos == stop) {
510 		++pos;
511 	}
512 
513 	*line = pos;
514 	return res;
515 }
516 
substring_conf(char * start,int len,char quote)517 static char *substring_conf(char *start, int len, char quote)
518 {
519 	char *result = emalloc(len + 1);
520 	char *resp = result;
521 	int i;
522 
523 	for (i = 0; i < len && start[i] != quote; ++i) {
524 		if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) {
525 			*resp++ = start[++i];
526 		} else {
527 			*resp++ = start[i];
528 		}
529 	}
530 
531 	*resp = '\0';
532 	return result;
533 }
534 
php_ap_getword_conf(const zend_encoding * encoding,char * str)535 static char *php_ap_getword_conf(const zend_encoding *encoding, char *str)
536 {
537 	while (*str && isspace(*str)) {
538 		++str;
539 	}
540 
541 	if (!*str) {
542 		return estrdup("");
543 	}
544 
545 	if (*str == '"' || *str == '\'') {
546 		char quote = *str;
547 
548 		str++;
549 		return substring_conf(str, (int)strlen(str), quote);
550 	} else {
551 		char *strend = str;
552 
553 		while (*strend && !isspace(*strend)) {
554 			++strend;
555 		}
556 		return substring_conf(str, strend - str, 0);
557 	}
558 }
559 
php_ap_basename(const zend_encoding * encoding,char * path)560 static char *php_ap_basename(const zend_encoding *encoding, char *path)
561 {
562 	char *s = strrchr(path, '\\');
563 	char *s2 = strrchr(path, '/');
564 
565 	if (s && s2) {
566 		if (s > s2) {
567 			++s;
568 		} else {
569 			s = ++s2;
570 		}
571 		return s;
572 	} else if (s) {
573 		return ++s;
574 	} else if (s2) {
575 		return ++s2;
576 	}
577 	return path;
578 }
579 
580 /*
581  * Search for a string in a fixed-length byte string.
582  * If partial is true, partial matches are allowed at the end of the buffer.
583  * Returns NULL if not found, or a pointer to the start of the first match.
584  */
php_ap_memstr(char * haystack,int haystacklen,char * needle,int needlen,int partial)585 static void *php_ap_memstr(char *haystack, int haystacklen, char *needle, int needlen, int partial)
586 {
587 	int len = haystacklen;
588 	char *ptr = haystack;
589 
590 	/* iterate through first character matches */
591 	while( (ptr = memchr(ptr, needle[0], len)) ) {
592 
593 		/* calculate length after match */
594 		len = haystacklen - (ptr - (char *)haystack);
595 
596 		/* done if matches up to capacity of buffer */
597 		if (memcmp(needle, ptr, needlen < len ? needlen : len) == 0 && (partial || len >= needlen)) {
598 			break;
599 		}
600 
601 		/* next character */
602 		ptr++; len--;
603 	}
604 
605 	return ptr;
606 }
607 
608 /* read until a boundary condition */
multipart_buffer_read(multipart_buffer * self,char * buf,size_t bytes,int * end)609 static size_t multipart_buffer_read(multipart_buffer *self, char *buf, size_t bytes, int *end)
610 {
611 	size_t len, max;
612 	char *bound;
613 
614 	/* fill buffer if needed */
615 	if (bytes > (size_t)self->bytes_in_buffer) {
616 		fill_buffer(self);
617 	}
618 
619 	/* look for a potential boundary match, only read data up to that point */
620 	if ((bound = php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 1))) {
621 		max = bound - self->buf_begin;
622 		if (end && php_ap_memstr(self->buf_begin, self->bytes_in_buffer, self->boundary_next, self->boundary_next_len, 0)) {
623 			*end = 1;
624 		}
625 	} else {
626 		max = self->bytes_in_buffer;
627 	}
628 
629 	/* maximum number of bytes we are reading */
630 	len = max < bytes-1 ? max : bytes-1;
631 
632 	/* if we read any data... */
633 	if (len > 0) {
634 
635 		/* copy the data */
636 		memcpy(buf, self->buf_begin, len);
637 		buf[len] = 0;
638 
639 		if (bound && len > 0 && buf[len-1] == '\r') {
640 			buf[--len] = 0;
641 		}
642 
643 		/* update the buffer */
644 		self->bytes_in_buffer -= (int)len;
645 		self->buf_begin += len;
646 	}
647 
648 	return len;
649 }
650 
651 /*
652   XXX: this is horrible memory-usage-wise, but we only expect
653   to do this on small pieces of form data.
654 */
multipart_buffer_read_body(multipart_buffer * self,size_t * len)655 static char *multipart_buffer_read_body(multipart_buffer *self, size_t *len)
656 {
657 	char buf[FILLUNIT], *out=NULL;
658 	size_t total_bytes=0, read_bytes=0;
659 
660 	while((read_bytes = multipart_buffer_read(self, buf, sizeof(buf), NULL))) {
661 		out = erealloc(out, total_bytes + read_bytes + 1);
662 		memcpy(out + total_bytes, buf, read_bytes);
663 		total_bytes += read_bytes;
664 	}
665 
666 	if (out) {
667 		out[total_bytes] = '\0';
668 	}
669 	*len = total_bytes;
670 
671 	return out;
672 }
673 /* }}} */
674 
675 /*
676  * The combined READER/HANDLER
677  *
678  */
679 
SAPI_POST_HANDLER_FUNC(rfc1867_post_handler)680 SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
681 {
682 	char *boundary, *s = NULL, *boundary_end = NULL, *start_arr = NULL, *array_index = NULL;
683 	char *lbuf = NULL, *abuf = NULL;
684 	zend_string *temp_filename = NULL;
685 	int boundary_len = 0, cancel_upload = 0, is_arr_upload = 0;
686 	size_t array_len = 0;
687 	int64_t total_bytes = 0, max_file_size = 0;
688 	int skip_upload = 0, anonindex = 0, is_anonymous;
689 	HashTable *uploaded_files = NULL;
690 	multipart_buffer *mbuff;
691 	zval *array_ptr = (zval *) arg;
692 	int fd = -1;
693 	zend_llist header;
694 	void *event_extra_data = NULL;
695 	unsigned int llen = 0;
696 	int upload_cnt = INI_INT("max_file_uploads");
697 	const zend_encoding *internal_encoding = zend_multibyte_get_internal_encoding();
698 	php_rfc1867_getword_t getword;
699 	php_rfc1867_getword_conf_t getword_conf;
700 	php_rfc1867_basename_t _basename;
701 	zend_long count = 0;
702 
703 	if (php_rfc1867_encoding_translation() && internal_encoding) {
704 		getword = php_rfc1867_getword;
705 		getword_conf = php_rfc1867_getword_conf;
706 		_basename = php_rfc1867_basename;
707 	} else {
708 		getword = php_ap_getword;
709 		getword_conf = php_ap_getword_conf;
710 		_basename = php_ap_basename;
711 	}
712 
713 	if (SG(post_max_size) > 0 && SG(request_info).content_length > SG(post_max_size)) {
714 		sapi_module.sapi_error(E_WARNING, "POST Content-Length of " ZEND_LONG_FMT " bytes exceeds the limit of " ZEND_LONG_FMT " bytes", SG(request_info).content_length, SG(post_max_size));
715 		return;
716 	}
717 
718 	/* Get the boundary */
719 	boundary = strstr(content_type_dup, "boundary");
720 	if (!boundary) {
721 		int content_type_len = (int)strlen(content_type_dup);
722 		char *content_type_lcase = estrndup(content_type_dup, content_type_len);
723 
724 		php_strtolower(content_type_lcase, content_type_len);
725 		boundary = strstr(content_type_lcase, "boundary");
726 		if (boundary) {
727 			boundary = content_type_dup + (boundary - content_type_lcase);
728 		}
729 		efree(content_type_lcase);
730 	}
731 
732 	if (!boundary || !(boundary = strchr(boundary, '='))) {
733 		sapi_module.sapi_error(E_WARNING, "Missing boundary in multipart/form-data POST data");
734 		return;
735 	}
736 
737 	boundary++;
738 	boundary_len = (int)strlen(boundary);
739 
740 	if (boundary[0] == '"') {
741 		boundary++;
742 		boundary_end = strchr(boundary, '"');
743 		if (!boundary_end) {
744 			sapi_module.sapi_error(E_WARNING, "Invalid boundary in multipart/form-data POST data");
745 			return;
746 		}
747 	} else {
748 		/* search for the end of the boundary */
749 		boundary_end = strpbrk(boundary, ",;");
750 	}
751 	if (boundary_end) {
752 		boundary_end[0] = '\0';
753 		boundary_len = boundary_end-boundary;
754 	}
755 
756 	/* Initialize the buffer */
757 	if (!(mbuff = multipart_buffer_new(boundary, boundary_len))) {
758 		sapi_module.sapi_error(E_WARNING, "Unable to initialize the input buffer");
759 		return;
760 	}
761 
762 	/* Initialize $_FILES[] */
763 	zend_hash_init(&PG(rfc1867_protected_variables), 8, NULL, NULL, 0);
764 
765 	ALLOC_HASHTABLE(uploaded_files);
766 	zend_hash_init(uploaded_files, 8, NULL, free_filename, 0);
767 	SG(rfc1867_uploaded_files) = uploaded_files;
768 
769 	if (Z_TYPE(PG(http_globals)[TRACK_VARS_FILES]) != IS_ARRAY) {
770 		/* php_auto_globals_create_files() might have already done that */
771 		array_init(&PG(http_globals)[TRACK_VARS_FILES]);
772 	}
773 
774 	zend_llist_init(&header, sizeof(mime_header_entry), (llist_dtor_func_t) php_free_hdr_entry, 0);
775 
776 	if (php_rfc1867_callback != NULL) {
777 		multipart_event_start event_start;
778 
779 		event_start.content_length = SG(request_info).content_length;
780 		if (php_rfc1867_callback(MULTIPART_EVENT_START, &event_start, &event_extra_data) == FAILURE) {
781 			goto fileupload_done;
782 		}
783 	}
784 
785 	while (!multipart_buffer_eof(mbuff))
786 	{
787 		char buff[FILLUNIT];
788 		char *cd = NULL, *param = NULL, *filename = NULL, *tmp = NULL;
789 		size_t blen = 0, wlen = 0;
790 		zend_off_t offset;
791 
792 		zend_llist_clean(&header);
793 
794 		if (!multipart_buffer_headers(mbuff, &header)) {
795 			goto fileupload_done;
796 		}
797 
798 		if ((cd = php_mime_get_hdr_value(header, "Content-Disposition"))) {
799 			char *pair = NULL;
800 			int end = 0;
801 
802 			while (isspace(*cd)) {
803 				++cd;
804 			}
805 
806 			while (*cd && (pair = getword(mbuff->input_encoding, &cd, ';')))
807 			{
808 				char *key = NULL, *word = pair;
809 
810 				while (isspace(*cd)) {
811 					++cd;
812 				}
813 
814 				if (strchr(pair, '=')) {
815 					key = getword(mbuff->input_encoding, &pair, '=');
816 
817 					if (!strcasecmp(key, "name")) {
818 						if (param) {
819 							efree(param);
820 						}
821 						param = getword_conf(mbuff->input_encoding, pair);
822 						if (mbuff->input_encoding && internal_encoding) {
823 							unsigned char *new_param;
824 							size_t new_param_len;
825 							if ((size_t)-1 != zend_multibyte_encoding_converter(&new_param, &new_param_len, (unsigned char *)param, strlen(param), internal_encoding, mbuff->input_encoding)) {
826 								efree(param);
827 								param = (char *)new_param;
828 							}
829 						}
830 					} else if (!strcasecmp(key, "filename")) {
831 						if (filename) {
832 							efree(filename);
833 						}
834 						filename = getword_conf(mbuff->input_encoding, pair);
835 						if (mbuff->input_encoding && internal_encoding) {
836 							unsigned char *new_filename;
837 							size_t new_filename_len;
838 							if ((size_t)-1 != zend_multibyte_encoding_converter(&new_filename, &new_filename_len, (unsigned char *)filename, strlen(filename), internal_encoding, mbuff->input_encoding)) {
839 								efree(filename);
840 								filename = (char *)new_filename;
841 							}
842 						}
843 					}
844 				}
845 				if (key) {
846 					efree(key);
847 				}
848 				efree(word);
849 			}
850 
851 			/* Normal form variable, safe to read all data into memory */
852 			if (!filename && param) {
853 				size_t value_len;
854 				char *value = multipart_buffer_read_body(mbuff, &value_len);
855 				size_t new_val_len; /* Dummy variable */
856 
857 				if (!value) {
858 					value = estrdup("");
859 					value_len = 0;
860 				}
861 
862 				if (mbuff->input_encoding && internal_encoding) {
863 					unsigned char *new_value;
864 					size_t new_value_len;
865 					if ((size_t)-1 != zend_multibyte_encoding_converter(&new_value, &new_value_len, (unsigned char *)value, value_len, internal_encoding, mbuff->input_encoding)) {
866 						efree(value);
867 						value = (char *)new_value;
868 						value_len = new_value_len;
869 					}
870 				}
871 
872 				if (++count <= PG(max_input_vars) && sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len)) {
873 					if (php_rfc1867_callback != NULL) {
874 						multipart_event_formdata event_formdata;
875 						size_t newlength = new_val_len;
876 
877 						event_formdata.post_bytes_processed = SG(read_post_bytes);
878 						event_formdata.name = param;
879 						event_formdata.value = &value;
880 						event_formdata.length = new_val_len;
881 						event_formdata.newlength = &newlength;
882 						if (php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data) == FAILURE) {
883 							efree(param);
884 							efree(value);
885 							continue;
886 						}
887 						new_val_len = newlength;
888 					}
889 					safe_php_register_variable(param, value, new_val_len, array_ptr, 0);
890 				} else {
891 					if (count == PG(max_input_vars) + 1) {
892 						php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
893 					}
894 
895 					if (php_rfc1867_callback != NULL) {
896 						multipart_event_formdata event_formdata;
897 
898 						event_formdata.post_bytes_processed = SG(read_post_bytes);
899 						event_formdata.name = param;
900 						event_formdata.value = &value;
901 						event_formdata.length = value_len;
902 						event_formdata.newlength = NULL;
903 						php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data);
904 					}
905 				}
906 
907 				if (!strcasecmp(param, "MAX_FILE_SIZE")) {
908 #ifdef HAVE_ATOLL
909 					max_file_size = atoll(value);
910 #else
911 					max_file_size = strtoll(value, NULL, 10);
912 #endif
913 				}
914 
915 				efree(param);
916 				efree(value);
917 				continue;
918 			}
919 
920 			/* If file_uploads=off, skip the file part */
921 			if (!PG(file_uploads)) {
922 				skip_upload = 1;
923 			} else if (upload_cnt <= 0) {
924 				skip_upload = 1;
925 				sapi_module.sapi_error(E_WARNING, "Maximum number of allowable file uploads has been exceeded");
926 			}
927 
928 			/* Return with an error if the posted data is garbled */
929 			if (!param && !filename) {
930 				sapi_module.sapi_error(E_WARNING, "File Upload Mime headers garbled");
931 				goto fileupload_done;
932 			}
933 
934 			if (!param) {
935 				is_anonymous = 1;
936 				param = emalloc(MAX_SIZE_ANONNAME);
937 				snprintf(param, MAX_SIZE_ANONNAME, "%u", anonindex++);
938 			} else {
939 				is_anonymous = 0;
940 			}
941 
942 			/* New Rule: never repair potential malicious user input */
943 			if (!skip_upload) {
944 				long c = 0;
945 				tmp = param;
946 
947 				while (*tmp) {
948 					if (*tmp == '[') {
949 						c++;
950 					} else if (*tmp == ']') {
951 						c--;
952 						if (tmp[1] && tmp[1] != '[') {
953 							skip_upload = 1;
954 							break;
955 						}
956 					}
957 					if (c < 0) {
958 						skip_upload = 1;
959 						break;
960 					}
961 					tmp++;
962 				}
963 				/* Brackets should always be closed */
964 				if(c != 0) {
965 					skip_upload = 1;
966 				}
967 			}
968 
969 			total_bytes = cancel_upload = 0;
970 			temp_filename = NULL;
971 			fd = -1;
972 
973 			if (!skip_upload && php_rfc1867_callback != NULL) {
974 				multipart_event_file_start event_file_start;
975 
976 				event_file_start.post_bytes_processed = SG(read_post_bytes);
977 				event_file_start.name = param;
978 				event_file_start.filename = &filename;
979 				if (php_rfc1867_callback(MULTIPART_EVENT_FILE_START, &event_file_start, &event_extra_data) == FAILURE) {
980 					temp_filename = NULL;
981 					efree(param);
982 					efree(filename);
983 					continue;
984 				}
985 			}
986 
987 			if (skip_upload) {
988 				efree(param);
989 				efree(filename);
990 				continue;
991 			}
992 
993 			if (filename[0] == '\0') {
994 #if DEBUG_FILE_UPLOAD
995 				sapi_module.sapi_error(E_NOTICE, "No file uploaded");
996 #endif
997 				cancel_upload = UPLOAD_ERROR_D;
998 			}
999 
1000 			offset = 0;
1001 			end = 0;
1002 
1003 			if (!cancel_upload) {
1004 				/* only bother to open temp file if we have data */
1005 				blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end);
1006 #if DEBUG_FILE_UPLOAD
1007 				if (blen > 0) {
1008 #else
1009 				/* in non-debug mode we have no problem with 0-length files */
1010 				{
1011 #endif
1012 					fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK);
1013 					upload_cnt--;
1014 					if (fd == -1) {
1015 						sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");
1016 						cancel_upload = UPLOAD_ERROR_E;
1017 					}
1018 				}
1019 			}
1020 
1021 			while (!cancel_upload && (blen > 0))
1022 			{
1023 				if (php_rfc1867_callback != NULL) {
1024 					multipart_event_file_data event_file_data;
1025 
1026 					event_file_data.post_bytes_processed = SG(read_post_bytes);
1027 					event_file_data.offset = offset;
1028 					event_file_data.data = buff;
1029 					event_file_data.length = blen;
1030 					event_file_data.newlength = &blen;
1031 					if (php_rfc1867_callback(MULTIPART_EVENT_FILE_DATA, &event_file_data, &event_extra_data) == FAILURE) {
1032 						cancel_upload = UPLOAD_ERROR_X;
1033 						continue;
1034 					}
1035 				}
1036 
1037 				if (PG(upload_max_filesize) > 0 && (zend_long)(total_bytes+blen) > PG(upload_max_filesize)) {
1038 #if DEBUG_FILE_UPLOAD
1039 					sapi_module.sapi_error(E_NOTICE, "upload_max_filesize of " ZEND_LONG_FMT " bytes exceeded - file [%s=%s] not saved", PG(upload_max_filesize), param, filename);
1040 #endif
1041 					cancel_upload = UPLOAD_ERROR_A;
1042 				} else if (max_file_size && ((zend_long)(total_bytes+blen) > max_file_size)) {
1043 #if DEBUG_FILE_UPLOAD
1044 					sapi_module.sapi_error(E_NOTICE, "MAX_FILE_SIZE of %" PRId64 " bytes exceeded - file [%s=%s] not saved", max_file_size, param, filename);
1045 #endif
1046 					cancel_upload = UPLOAD_ERROR_B;
1047 				} else if (blen > 0) {
1048 #ifdef PHP_WIN32
1049 					wlen = write(fd, buff, (unsigned int)blen);
1050 #else
1051 					wlen = write(fd, buff, blen);
1052 #endif
1053 
1054 					if (wlen == (size_t)-1) {
1055 						/* write failed */
1056 #if DEBUG_FILE_UPLOAD
1057 						sapi_module.sapi_error(E_NOTICE, "write() failed - %s", strerror(errno));
1058 #endif
1059 						cancel_upload = UPLOAD_ERROR_F;
1060 					} else if (wlen < blen) {
1061 #if DEBUG_FILE_UPLOAD
1062 						sapi_module.sapi_error(E_NOTICE, "Only %zd bytes were written, expected to write %zd", wlen, blen);
1063 #endif
1064 						cancel_upload = UPLOAD_ERROR_F;
1065 					} else {
1066 						total_bytes += wlen;
1067 					}
1068 					offset += wlen;
1069 				}
1070 
1071 				/* read data for next iteration */
1072 				blen = multipart_buffer_read(mbuff, buff, sizeof(buff), &end);
1073 			}
1074 
1075 			if (fd != -1) { /* may not be initialized if file could not be created */
1076 				close(fd);
1077 			}
1078 
1079 			if (!cancel_upload && !end) {
1080 #if DEBUG_FILE_UPLOAD
1081 				sapi_module.sapi_error(E_NOTICE, "Missing mime boundary at the end of the data for file %s", filename[0] != '\0' ? filename : "");
1082 #endif
1083 				cancel_upload = UPLOAD_ERROR_C;
1084 			}
1085 #if DEBUG_FILE_UPLOAD
1086 			if (filename[0] != '\0' && total_bytes == 0 && !cancel_upload) {
1087 				sapi_module.sapi_error(E_WARNING, "Uploaded file size 0 - file [%s=%s] not saved", param, filename);
1088 				cancel_upload = 5;
1089 			}
1090 #endif
1091 			if (php_rfc1867_callback != NULL) {
1092 				multipart_event_file_end event_file_end;
1093 
1094 				event_file_end.post_bytes_processed = SG(read_post_bytes);
1095 				event_file_end.temp_filename = temp_filename ? ZSTR_VAL(temp_filename) : NULL;
1096 				event_file_end.cancel_upload = cancel_upload;
1097 				if (php_rfc1867_callback(MULTIPART_EVENT_FILE_END, &event_file_end, &event_extra_data) == FAILURE) {
1098 					cancel_upload = UPLOAD_ERROR_X;
1099 				}
1100 			}
1101 
1102 			if (cancel_upload) {
1103 				if (temp_filename) {
1104 					if (cancel_upload != UPLOAD_ERROR_E) { /* file creation failed */
1105 						unlink(ZSTR_VAL(temp_filename));
1106 					}
1107 					zend_string_release_ex(temp_filename, 0);
1108 				}
1109 				temp_filename = NULL;
1110 			} else {
1111 				zend_hash_add_ptr(SG(rfc1867_uploaded_files), temp_filename, temp_filename);
1112 			}
1113 
1114 			/* is_arr_upload is true when name of file upload field
1115 			 * ends in [.*]
1116 			 * start_arr is set to point to 1st [ */
1117 			is_arr_upload =	(start_arr = strchr(param,'[')) && (param[strlen(param)-1] == ']');
1118 
1119 			if (is_arr_upload) {
1120 				array_len = strlen(start_arr);
1121 				if (array_index) {
1122 					efree(array_index);
1123 				}
1124 				array_index = estrndup(start_arr + 1, array_len - 2);
1125 			}
1126 
1127 			/* Add $foo_name */
1128 			if (llen < strlen(param) + MAX_SIZE_OF_INDEX + 1) {
1129 				llen = (int)strlen(param);
1130 				lbuf = (char *) safe_erealloc(lbuf, llen, 1, MAX_SIZE_OF_INDEX + 1);
1131 				llen += MAX_SIZE_OF_INDEX + 1;
1132 			}
1133 
1134 			if (is_arr_upload) {
1135 				if (abuf) efree(abuf);
1136 				abuf = estrndup(param, strlen(param)-array_len);
1137 				snprintf(lbuf, llen, "%s_name[%s]", abuf, array_index);
1138 			} else {
1139 				snprintf(lbuf, llen, "%s_name", param);
1140 			}
1141 
1142 			/* The \ check should technically be needed for win32 systems only where
1143 			 * it is a valid path separator. However, IE in all it's wisdom always sends
1144 			 * the full path of the file on the user's filesystem, which means that unless
1145 			 * the user does basename() they get a bogus file name. Until IE's user base drops
1146 			 * to nill or problem is fixed this code must remain enabled for all systems. */
1147 			s = _basename(internal_encoding, filename);
1148 			if (!s) {
1149 				s = filename;
1150 			}
1151 
1152 			if (!is_anonymous) {
1153 				safe_php_register_variable(lbuf, s, strlen(s), NULL, 0);
1154 			}
1155 
1156 			/* Add $foo[name] */
1157 			if (is_arr_upload) {
1158 				snprintf(lbuf, llen, "%s[name][%s]", abuf, array_index);
1159 			} else {
1160 				snprintf(lbuf, llen, "%s[name]", param);
1161 			}
1162 			register_http_post_files_variable(lbuf, s, &PG(http_globals)[TRACK_VARS_FILES], 0);
1163 			efree(filename);
1164 			s = NULL;
1165 
1166 			/* Possible Content-Type: */
1167 			if (cancel_upload || !(cd = php_mime_get_hdr_value(header, "Content-Type"))) {
1168 				cd = "";
1169 			} else {
1170 				/* fix for Opera 6.01 */
1171 				s = strchr(cd, ';');
1172 				if (s != NULL) {
1173 					*s = '\0';
1174 				}
1175 			}
1176 
1177 			/* Add $foo_type */
1178 			if (is_arr_upload) {
1179 				snprintf(lbuf, llen, "%s_type[%s]", abuf, array_index);
1180 			} else {
1181 				snprintf(lbuf, llen, "%s_type", param);
1182 			}
1183 			if (!is_anonymous) {
1184 				safe_php_register_variable(lbuf, cd, strlen(cd), NULL, 0);
1185 			}
1186 
1187 			/* Add $foo[type] */
1188 			if (is_arr_upload) {
1189 				snprintf(lbuf, llen, "%s[type][%s]", abuf, array_index);
1190 			} else {
1191 				snprintf(lbuf, llen, "%s[type]", param);
1192 			}
1193 			register_http_post_files_variable(lbuf, cd, &PG(http_globals)[TRACK_VARS_FILES], 0);
1194 
1195 			/* Restore Content-Type Header */
1196 			if (s != NULL) {
1197 				*s = ';';
1198 			}
1199 			s = "";
1200 
1201 			{
1202 				/* store temp_filename as-is (in case upload_tmp_dir
1203 				 * contains escapeable characters. escape only the variable name.) */
1204 				zval zfilename;
1205 
1206 				/* Initialize variables */
1207 				add_protected_variable(param);
1208 
1209 				/* if param is of form xxx[.*] this will cut it to xxx */
1210 				if (!is_anonymous) {
1211 					if (temp_filename) {
1212 						ZVAL_STR_COPY(&zfilename, temp_filename);
1213 					} else {
1214 						ZVAL_EMPTY_STRING(&zfilename);
1215 					}
1216 					safe_php_register_variable_ex(param, &zfilename, NULL, 1);
1217 				}
1218 
1219 				/* Add $foo[tmp_name] */
1220 				if (is_arr_upload) {
1221 					snprintf(lbuf, llen, "%s[tmp_name][%s]", abuf, array_index);
1222 				} else {
1223 					snprintf(lbuf, llen, "%s[tmp_name]", param);
1224 				}
1225 				add_protected_variable(lbuf);
1226 				if (temp_filename) {
1227 					ZVAL_STR_COPY(&zfilename, temp_filename);
1228 				} else {
1229 					ZVAL_EMPTY_STRING(&zfilename);
1230 				}
1231 				register_http_post_files_variable_ex(lbuf, &zfilename, &PG(http_globals)[TRACK_VARS_FILES], 1);
1232 			}
1233 
1234 			{
1235 				zval file_size, error_type;
1236 				int size_overflow = 0;
1237 				char file_size_buf[65];
1238 
1239 				ZVAL_LONG(&error_type, cancel_upload);
1240 
1241 				/* Add $foo[error] */
1242 				if (cancel_upload) {
1243 					ZVAL_LONG(&file_size, 0);
1244 				} else {
1245 					if (total_bytes > ZEND_LONG_MAX) {
1246 #ifdef PHP_WIN32
1247 						if (_i64toa_s(total_bytes, file_size_buf, 65, 10)) {
1248 							file_size_buf[0] = '0';
1249 							file_size_buf[1] = '\0';
1250 						}
1251 #else
1252 						{
1253 							int __len = snprintf(file_size_buf, 65, "%" PRId64, total_bytes);
1254 							file_size_buf[__len] = '\0';
1255 						}
1256 #endif
1257 						size_overflow = 1;
1258 
1259 					} else {
1260 						ZVAL_LONG(&file_size, total_bytes);
1261 					}
1262 				}
1263 
1264 				if (is_arr_upload) {
1265 					snprintf(lbuf, llen, "%s[error][%s]", abuf, array_index);
1266 				} else {
1267 					snprintf(lbuf, llen, "%s[error]", param);
1268 				}
1269 				register_http_post_files_variable_ex(lbuf, &error_type, &PG(http_globals)[TRACK_VARS_FILES], 0);
1270 
1271 				/* Add $foo_size */
1272 				if (is_arr_upload) {
1273 					snprintf(lbuf, llen, "%s_size[%s]", abuf, array_index);
1274 				} else {
1275 					snprintf(lbuf, llen, "%s_size", param);
1276 				}
1277 				if (!is_anonymous) {
1278 					if (size_overflow) {
1279 						ZVAL_STRING(&file_size, file_size_buf);
1280 					}
1281 					safe_php_register_variable_ex(lbuf, &file_size, NULL, size_overflow);
1282 				}
1283 
1284 				/* Add $foo[size] */
1285 				if (is_arr_upload) {
1286 					snprintf(lbuf, llen, "%s[size][%s]", abuf, array_index);
1287 				} else {
1288 					snprintf(lbuf, llen, "%s[size]", param);
1289 				}
1290 				if (size_overflow) {
1291 					ZVAL_STRING(&file_size, file_size_buf);
1292 				}
1293 				register_http_post_files_variable_ex(lbuf, &file_size, &PG(http_globals)[TRACK_VARS_FILES], size_overflow);
1294 			}
1295 			efree(param);
1296 		}
1297 	}
1298 
1299 fileupload_done:
1300 	if (php_rfc1867_callback != NULL) {
1301 		multipart_event_end event_end;
1302 
1303 		event_end.post_bytes_processed = SG(read_post_bytes);
1304 		php_rfc1867_callback(MULTIPART_EVENT_END, &event_end, &event_extra_data);
1305 	}
1306 
1307 	if (lbuf) efree(lbuf);
1308 	if (abuf) efree(abuf);
1309 	if (array_index) efree(array_index);
1310 	zend_hash_destroy(&PG(rfc1867_protected_variables));
1311 	zend_llist_destroy(&header);
1312 	if (mbuff->boundary_next) efree(mbuff->boundary_next);
1313 	if (mbuff->boundary) efree(mbuff->boundary);
1314 	if (mbuff->buffer) efree(mbuff->buffer);
1315 	if (mbuff) efree(mbuff);
1316 }
1317 /* }}} */
1318 
1319 SAPI_API void php_rfc1867_set_multibyte_callbacks(
1320 					php_rfc1867_encoding_translation_t encoding_translation,
1321 					php_rfc1867_get_detect_order_t get_detect_order,
1322 					php_rfc1867_set_input_encoding_t set_input_encoding,
1323 					php_rfc1867_getword_t getword,
1324 					php_rfc1867_getword_conf_t getword_conf,
1325 					php_rfc1867_basename_t basename) /* {{{ */
1326 {
1327 	php_rfc1867_encoding_translation = encoding_translation;
1328 	php_rfc1867_get_detect_order = get_detect_order;
1329 	php_rfc1867_set_input_encoding = set_input_encoding;
1330 	php_rfc1867_getword = getword;
1331 	php_rfc1867_getword_conf = getword_conf;
1332 	php_rfc1867_basename = basename;
1333 }
1334 /* }}} */
1335