1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stanislav Malyshev <stas@zend.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "../intl_cppshims.h"
22 
23 #include <limits.h>
24 #include <unicode/msgfmt.h>
25 #include <unicode/chariter.h>
26 #include <unicode/ustdio.h>
27 #include <unicode/timezone.h>
28 #include <unicode/datefmt.h>
29 #include <unicode/calendar.h>
30 #include <unicode/strenum.h>
31 
32 #include <vector>
33 
34 #include "../intl_convertcpp.h"
35 #include "../common/common_date.h"
36 
37 extern "C" {
38 #include "php_intl.h"
39 #include "msgformat_class.h"
40 #include "msgformat_format.h"
41 #include "msgformat_helpers.h"
42 #include "intl_convert.h"
43 #define USE_TIMEZONE_POINTER
44 #include "../timezone/timezone_class.h"
45 }
46 
47 U_NAMESPACE_BEGIN
48 /**
49  * ICU declares MessageFormatAdapter as a friend class of MessageFormat,
50  * to use as a backdoor for accessing private MessageFormat members.
51  * We use it for the same purpose here. Prefix the methods with php to
52  * avoid clashes with any definitions in ICU.
53  */
54 class MessageFormatAdapter {
55 public:
56     static const Formattable::Type* phpGetArgTypeList(const MessageFormat& m,
57                                                    int32_t& count);
58     static const MessagePattern phpGetMessagePattern(MessageFormat* m);
59 };
60 
61 const Formattable::Type*
phpGetArgTypeList(const MessageFormat & m,int32_t & count)62 MessageFormatAdapter::phpGetArgTypeList(const MessageFormat& m,
63                                      int32_t& count) {
64     return m.getArgTypeList(count);
65 }
66 
67 const MessagePattern
phpGetMessagePattern(MessageFormat * m)68 MessageFormatAdapter::phpGetMessagePattern(MessageFormat* m) {
69     return m->msgPattern;
70 }
71 U_NAMESPACE_END
72 
73 using icu::Formattable;
74 using icu::Format;
75 using icu::DateFormat;
76 using icu::MessageFormat;
77 using icu::MessagePattern;
78 using icu::MessageFormatAdapter;
79 using icu::FieldPosition;
80 
umsg_format_arg_count(UMessageFormat * fmt)81 U_CFUNC int32_t umsg_format_arg_count(UMessageFormat *fmt)
82 {
83 	int32_t fmt_count = 0;
84 	MessageFormatAdapter::phpGetArgTypeList(*(const MessageFormat*)fmt, fmt_count);
85 	return fmt_count;
86 }
87 
arg_types_dtor(zval * el)88 static void arg_types_dtor(zval *el) {
89 	efree(Z_PTR_P(el));
90 }
91 
umsg_get_numeric_types(MessageFormatter_object * mfo,intl_error & err)92 static HashTable *umsg_get_numeric_types(MessageFormatter_object *mfo,
93 										 intl_error& err)
94 {
95 	HashTable *ret;
96 	int32_t parts_count;
97 
98 	if (U_FAILURE(err.code)) {
99 		return NULL;
100 	}
101 
102 	if (mfo->mf_data.arg_types) {
103 		/* already cached */
104 		return mfo->mf_data.arg_types;
105 	}
106 
107 	const Formattable::Type *types = MessageFormatAdapter::phpGetArgTypeList(
108 		*(MessageFormat*)mfo->mf_data.umsgf, parts_count);
109 
110 	/* Hash table will store Formattable::Type objects directly,
111 	 * so no need for destructor */
112 	ALLOC_HASHTABLE(ret);
113 	zend_hash_init(ret, parts_count, NULL, arg_types_dtor, 0);
114 
115 	for (int i = 0; i < parts_count; i++) {
116 		const Formattable::Type t = types[i];
117 		zend_hash_index_update_mem(ret, (zend_ulong)i, (void*)&t, sizeof(t));
118 	}
119 
120 	if (U_FAILURE(err.code)) {
121 		zend_hash_destroy(ret);
122 		efree(ret);
123 
124 		return NULL;
125 	}
126 
127 	mfo->mf_data.arg_types = ret;
128 
129 	return ret;
130 }
131 
umsg_parse_format(MessageFormatter_object * mfo,const MessagePattern & mp,intl_error & err)132 static HashTable *umsg_parse_format(MessageFormatter_object *mfo,
133 									const MessagePattern& mp,
134 									intl_error& err)
135 {
136 	HashTable *ret;
137 	int32_t parts_count;
138 
139 	if (U_FAILURE(err.code)) {
140 		return NULL;
141 	}
142 
143 	if (!((MessageFormat *)mfo->mf_data.umsgf)->usesNamedArguments()) {
144 		return umsg_get_numeric_types(mfo, err);
145 	}
146 
147 	if (mfo->mf_data.arg_types) {
148 		/* already cached */
149 		return mfo->mf_data.arg_types;
150 	}
151 
152 	/* Hash table will store Formattable::Type objects directly,
153 	 * so no need for destructor */
154 	ALLOC_HASHTABLE(ret);
155 	zend_hash_init(ret, 32, NULL, arg_types_dtor, 0);
156 
157 	parts_count = mp.countParts();
158 
159 	// See MessageFormat::cacheExplicitFormats()
160 	/*
161 	 * Looking through the pattern, go to each arg_start part type.
162 	 * The arg-typeof that tells us the argument type (simple, complicated)
163 	 * then the next part is either the arg_name or arg number
164 	 * and then if it's simple after that there could be a part-type=arg-type
165 	 * while substring will tell us number, spellout, etc.
166 	 * If the next thing isn't an arg-type then assume string.
167 	*/
168 	/* The last two "parts" can at most be ARG_LIMIT and MSG_LIMIT
169 	 * which we need not examine. */
170 	for (int32_t i = 0; i < parts_count - 2 && U_SUCCESS(err.code); i++) {
171 		MessagePattern::Part p = mp.getPart(i);
172 
173 		if (p.getType() != UMSGPAT_PART_TYPE_ARG_START) {
174 			continue;
175 		}
176 
177 		MessagePattern::Part name_part = mp.getPart(++i); /* Getting name, advancing i */
178 		Formattable::Type type,
179 						  *storedType;
180 
181 		if (name_part.getType() == UMSGPAT_PART_TYPE_ARG_NAME) {
182 			UnicodeString argName = mp.getSubstring(name_part);
183 			if ((storedType = (Formattable::Type*)zend_hash_str_find_ptr(ret, (char*)argName.getBuffer(), argName.length() * sizeof(UChar))) == NULL) {
184 				/* not found already; create new entry in HT */
185 				Formattable::Type bogusType = Formattable::kObject;
186 				storedType = (Formattable::Type*)zend_hash_str_update_mem(ret, (char*)argName.getBuffer(), argName.length() * sizeof(UChar),
187 						(void*)&bogusType, sizeof(bogusType));
188 			}
189 		} else if (name_part.getType() == UMSGPAT_PART_TYPE_ARG_NUMBER) {
190 			int32_t argNumber = name_part.getValue();
191 			if (argNumber < 0) {
192 				intl_errors_set(&err, U_INVALID_FORMAT_ERROR,
193 					"Found part with negative number", 0);
194 				continue;
195 			}
196 			if ((storedType = (Formattable::Type*)zend_hash_index_find_ptr(ret, (zend_ulong)argNumber)) == NULL) {
197 				/* not found already; create new entry in HT */
198 				Formattable::Type bogusType = Formattable::kObject;
199 				storedType = (Formattable::Type*)zend_hash_index_update_mem(ret, (zend_ulong)argNumber, (void*)&bogusType, sizeof(bogusType));
200 			}
201 		} else {
202 			intl_errors_set(&err, U_INVALID_FORMAT_ERROR, "Invalid part type encountered", 0);
203 			continue;
204 		}
205 
206 		UMessagePatternArgType argType = p.getArgType();
207 		/* No type specified, treat it as a string */
208 		if (argType == UMSGPAT_ARG_TYPE_NONE) {
209 			type = Formattable::kString;
210 		} else { /* Some type was specified, might be simple or complicated */
211 			if (argType == UMSGPAT_ARG_TYPE_SIMPLE) {
212 				/* For a SIMPLE arg, after the name part, there should be
213 				 * an ARG_TYPE part whose string value tells us what to do */
214 				MessagePattern::Part type_part = mp.getPart(++i); /* Getting type, advancing i */
215 				if (type_part.getType() == UMSGPAT_PART_TYPE_ARG_TYPE) {
216 					UnicodeString typeString = mp.getSubstring(type_part);
217 					/* This is all based on the rules in the docs for MessageFormat
218 					 * @see http://icu-project.org/apiref/icu4c/classMessageFormat.html */
219 #define ASCII_LITERAL(s) UNICODE_STRING(s, sizeof(s)-1)
220 					if (typeString == ASCII_LITERAL("number")) {
221 						MessagePattern::Part style_part = mp.getPart(i + 1); /* Not advancing i */
222 						if (style_part.getType() == UMSGPAT_PART_TYPE_ARG_STYLE) {
223 							UnicodeString styleString = mp.getSubstring(style_part);
224 							if (styleString == ASCII_LITERAL("integer")) {
225 								type = Formattable::kInt64;
226 							} else if (styleString == ASCII_LITERAL("currency")) {
227 								type = Formattable::kDouble;
228 							} else if (styleString == ASCII_LITERAL("percent")) {
229 								type = Formattable::kDouble;
230 							} else { /* some style invalid/unknown to us */
231 								type = Formattable::kDouble;
232 							}
233 						} else { // if missing style, part, make it a double
234 							type = Formattable::kDouble;
235 						}
236 					} else if ((typeString == ASCII_LITERAL("date")) || (typeString == ASCII_LITERAL("time"))) {
237 						type = Formattable::kDate;
238 					} else if ((typeString == ASCII_LITERAL("spellout")) || (typeString == ASCII_LITERAL("ordinal"))
239 							|| (typeString == ASCII_LITERAL("duration"))) {
240 						type = Formattable::kDouble;
241 					}
242 #undef ASCII_LITERAL
243 				} else {
244 					/* If there's no UMSGPAT_PART_TYPE_ARG_TYPE right after a
245 					 * UMSGPAT_ARG_TYPE_SIMPLE argument, then the pattern
246 					 * is broken. */
247 					intl_errors_set(&err, U_PARSE_ERROR,
248 						"Expected UMSGPAT_PART_TYPE_ARG_TYPE part following "
249 						"UMSGPAT_ARG_TYPE_SIMPLE part", 0);
250 					continue;
251 				}
252 			} else if (argType == UMSGPAT_ARG_TYPE_PLURAL) {
253 				type = Formattable::kDouble;
254 			} else if (argType == UMSGPAT_ARG_TYPE_CHOICE) {
255 				type = Formattable::kDouble;
256 			} else if (argType == UMSGPAT_ARG_TYPE_SELECT) {
257 				type = Formattable::kString;
258 			} else if (argType == UMSGPAT_ARG_TYPE_SELECTORDINAL) {
259 				type = Formattable::kDouble;
260 			} else {
261 				type = Formattable::kString;
262 			}
263 		} /* was type specified? */
264 
265 		/* We found a different type for the same arg! */
266 		if (*storedType != Formattable::kObject && *storedType != type) {
267 			intl_errors_set(&err, U_ARGUMENT_TYPE_MISMATCH,
268 				"Inconsistent types declared for an argument", 0);
269 			continue;
270 		}
271 
272 		*storedType = type;
273 	} /* visiting each part */
274 
275 	if (U_FAILURE(err.code)) {
276 		zend_hash_destroy(ret);
277 		efree(ret);
278 
279 		return NULL;
280 	}
281 
282 	mfo->mf_data.arg_types = ret;
283 
284 	return ret;
285 }
286 
umsg_get_types(MessageFormatter_object * mfo,intl_error & err)287 static HashTable *umsg_get_types(MessageFormatter_object *mfo,
288 								 intl_error& err)
289 {
290 	MessageFormat *mf = (MessageFormat *)mfo->mf_data.umsgf;
291 
292 	const MessagePattern mp = MessageFormatAdapter::phpGetMessagePattern(mf);
293 
294 	return umsg_parse_format(mfo, mp, err);
295 }
296 
umsg_set_timezone(MessageFormatter_object * mfo,intl_error & err)297 static void umsg_set_timezone(MessageFormatter_object *mfo,
298 							  intl_error& err)
299 {
300 	MessageFormat *mf = (MessageFormat *)mfo->mf_data.umsgf;
301 	TimeZone	  *used_tz = NULL;
302 	const Format  **formats;
303 	int32_t		  count;
304 
305 	/* Unfortanely, this cannot change the time zone for arguments that
306 	 * appear inside complex formats because ::getFormats() returns NULL
307 	 * for all uncached formats, which is the case for complex formats
308 	 * unless they were set via one of the ::setFormat() methods */
309 
310 	if (mfo->mf_data.tz_set) {
311 		return; /* already done */
312 	}
313 
314 	/* There is a bug in ICU which prevents MessageFormatter::getFormats()
315 	   to handle more than 10 formats correctly. The enumerator could be
316 	   used to walk through the present formatters using getFormat(), which
317 	   however seems to provide just a readonly access. This workaround
318 	   prevents crash when there are > 10 formats but doesn't set any error.
319 	   As a result, only DateFormatters with > 10 subformats are affected.
320 	   This workaround should be ifdef'd out, when the bug has been fixed
321 	   in ICU. */
322 	icu::StringEnumeration* fnames = mf->getFormatNames(err.code);
323 	if (!fnames || U_FAILURE(err.code)) {
324 		return;
325 	}
326 	count = fnames->count(err.code);
327 	delete fnames;
328 	if (count > 10) {
329 		return;
330 	}
331 
332 	formats = mf->getFormats(count);
333 
334 	if (formats == NULL) {
335 		intl_errors_set(&err, U_MEMORY_ALLOCATION_ERROR,
336 			"Out of memory retrieving subformats", 0);
337 	}
338 
339 	for (int i = 0; U_SUCCESS(err.code) && i < count; i++) {
340 		DateFormat* df = dynamic_cast<DateFormat*>(
341 			const_cast<Format *>(formats[i]));
342 		if (df == NULL) {
343 			continue;
344 		}
345 
346 		if (used_tz == NULL) {
347 			zval nullzv;
348 			ZVAL_NULL(&nullzv);
349 			used_tz = timezone_process_timezone_argument(&nullzv, &err, "msgfmt_format");
350 			if (used_tz == NULL) {
351 				continue;
352 			}
353 		}
354 
355 		df->adoptTimeZone(used_tz->clone());
356 	}
357 
358 	if (U_SUCCESS(err.code)) {
359 		mfo->mf_data.tz_set = 1;
360 	}
361 
362   if (used_tz) {
363     delete used_tz;
364   }
365 }
366 
umsg_format_helper(MessageFormatter_object * mfo,HashTable * args,UChar ** formatted,int32_t * formatted_len)367 U_CFUNC void umsg_format_helper(MessageFormatter_object *mfo,
368 								HashTable *args,
369 								UChar **formatted,
370 								int32_t *formatted_len)
371 {
372 	int arg_count = zend_hash_num_elements(args);
373 	std::vector<Formattable> fargs;
374 	std::vector<UnicodeString> farg_names;
375 	MessageFormat *mf = (MessageFormat *)mfo->mf_data.umsgf;
376 	HashTable *types;
377 	intl_error& err = INTL_DATA_ERROR(mfo);
378 
379 	if (U_FAILURE(err.code)) {
380 		return;
381 	}
382 
383 	types = umsg_get_types(mfo, err);
384 
385 	umsg_set_timezone(mfo, err);
386 
387 	fargs.resize(arg_count);
388 	farg_names.resize(arg_count);
389 
390 	int				argNum = 0;
391 	zval			*elem;
392 
393 	// Key related variables
394 	zend_string		*str_index;
395 	zend_ulong		 num_index;
396 
397 	ZEND_HASH_FOREACH_KEY_VAL(args, num_index, str_index, elem) {
398 		Formattable& formattable = fargs[argNum];
399 		UnicodeString& key = farg_names[argNum];
400 		Formattable::Type argType = Formattable::kObject, //unknown
401 						  *storedArgType = NULL;
402 		if (!U_SUCCESS(err.code)) {
403 			break;
404 		}
405 		/* Process key and retrieve type */
406 		if (str_index == NULL) {
407 			/* includes case where index < 0 because it's exposed as unsigned */
408 			if (num_index > (zend_ulong)INT32_MAX) {
409 				intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
410 					"Found negative or too large array key", 0);
411 				continue;
412 			}
413 
414 		   UChar temp[16];
415 		   int32_t len = u_sprintf(temp, "%u", (uint32_t)num_index);
416 		   key.append(temp, len);
417 
418 		   storedArgType = (Formattable::Type*)zend_hash_index_find_ptr(types, (zend_ulong)num_index);
419 		} else { //string; assumed to be in UTF-8
420 			intl_stringFromChar(key, ZSTR_VAL(str_index), ZSTR_LEN(str_index), &err.code);
421 
422 			if (U_FAILURE(err.code)) {
423 				char *message;
424 				spprintf(&message, 0,
425 					"Invalid UTF-8 data in argument key: '%s'", ZSTR_VAL(str_index));
426 				intl_errors_set(&err, err.code,	message, 1);
427 				efree(message);
428 				continue;
429 			}
430 
431 			storedArgType = (Formattable::Type*)zend_hash_str_find_ptr(types, (char*)key.getBuffer(), key.length() * sizeof(UChar));
432 		}
433 
434 		if (storedArgType != NULL) {
435 			argType = *storedArgType;
436 		}
437 
438 		/* Convert zval to formattable according to message format type
439 		 * or (as a fallback) the zval type */
440 		if (argType != Formattable::kObject) {
441 			switch (argType) {
442 			case Formattable::kString:
443 				{
444 					zend_string *str, *tmp_str;
445 
446 	string_arg:
447 					/* This implicitly converts objects
448 					 * Note that our vectors will leak if object conversion fails
449 					 * and PHP ends up with a fatal error and calls longjmp
450 					 * as a result of that.
451 					 */
452 					str = zval_get_tmp_string(elem, &tmp_str);
453 
454 					UnicodeString *text = new UnicodeString();
455 					intl_stringFromChar(*text,
456 						ZSTR_VAL(str), ZSTR_LEN(str), &err.code);
457 
458 					if (U_FAILURE(err.code)) {
459 						char *message;
460 						spprintf(&message, 0, "Invalid UTF-8 data in string argument: "
461 							"'%s'", ZSTR_VAL(str));
462 						intl_errors_set(&err, err.code, message, 1);
463 						efree(message);
464 						delete text;
465 						continue;
466 					}
467 					formattable.adoptString(text);
468 					zend_tmp_string_release(tmp_str);
469 					break;
470 				}
471 			case Formattable::kDouble:
472 				{
473 					double d = zval_get_double(elem);
474 					formattable.setDouble(d);
475 					break;
476 				}
477 			case Formattable::kLong:
478 				{
479 					int32_t tInt32 = 0;
480 
481 					if (Z_TYPE_P(elem) == IS_DOUBLE) {
482 						if (Z_DVAL_P(elem) > (double)INT32_MAX ||
483 								Z_DVAL_P(elem) < (double)INT32_MIN) {
484 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
485 								"Found PHP float with absolute value too large for "
486 								"32 bit integer argument", 0);
487 						} else {
488 							tInt32 = (int32_t)Z_DVAL_P(elem);
489 						}
490 					} else if (Z_TYPE_P(elem) == IS_LONG) {
491 						if (Z_LVAL_P(elem) > INT32_MAX ||
492 								Z_LVAL_P(elem) < INT32_MIN) {
493 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
494 								"Found PHP integer with absolute value too large "
495 								"for 32 bit integer argument", 0);
496 						} else {
497 							tInt32 = (int32_t)Z_LVAL_P(elem);
498 						}
499 					} else {
500 						tInt32 = (int32_t)zval_get_long(elem);
501 					}
502 					formattable.setLong(tInt32);
503 					break;
504 				}
505 			case Formattable::kInt64:
506 				{
507 					int64_t tInt64 = 0;
508 
509 					if (Z_TYPE_P(elem) == IS_DOUBLE) {
510 						if (Z_DVAL_P(elem) > (double)U_INT64_MAX ||
511 								Z_DVAL_P(elem) < (double)U_INT64_MIN) {
512 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
513 								"Found PHP float with absolute value too large for "
514 								"64 bit integer argument", 0);
515 						} else {
516 							tInt64 = (int64_t)Z_DVAL_P(elem);
517 						}
518 					} else if (Z_TYPE_P(elem) == IS_LONG) {
519 						/* assume long is not wider than 64 bits */
520 						tInt64 = (int64_t)Z_LVAL_P(elem);
521 					} else {
522 						tInt64 = (int64_t)zval_get_long(elem);
523 					}
524 					formattable.setInt64(tInt64);
525 					break;
526 				}
527 			case Formattable::kDate:
528 				{
529 					double dd = intl_zval_to_millis(elem, &err, "msgfmt_format");
530 					if (U_FAILURE(err.code)) {
531 						char *message;
532 						zend_string *u8key;
533 						UErrorCode status = UErrorCode();
534 						u8key = intl_charFromString(key, &status);
535 						if (u8key) {
536 							spprintf(&message, 0, "The argument for key '%s' "
537 								"cannot be used as a date or time", ZSTR_VAL(u8key));
538 							intl_errors_set(&err, err.code, message, 1);
539 							zend_string_release_ex(u8key, 0);
540 							efree(message);
541 						}
542 						continue;
543 					}
544 					formattable.setDate(dd);
545 					break;
546 				}
547 			default:
548 				intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
549 					"Found unsupported argument type", 0);
550 				break;
551 			}
552 		} else {
553 			/* We couldn't find any information about the argument in the pattern, this
554 			 * means it's an extra argument. So convert it to a number if it's a number or
555 			 * bool or null and to a string if it's anything else except arrays . */
556 			switch (Z_TYPE_P(elem)) {
557 			case IS_DOUBLE:
558 				formattable.setDouble(Z_DVAL_P(elem));
559 				break;
560 			case IS_LONG:
561 				formattable.setInt64((int64_t)Z_LVAL_P(elem));
562 				break;
563 			case IS_NULL:
564 			case IS_FALSE:
565 				formattable.setInt64((int64_t)0);
566 				break;
567 			case IS_TRUE:
568 				formattable.setInt64((int64_t)1);
569 				break;
570 			case IS_STRING:
571 			case IS_OBJECT:
572 				goto string_arg;
573 			default:
574 				{
575 					char *message;
576 					zend_string *u8key;
577 					UErrorCode status = UErrorCode();
578 					u8key = intl_charFromString(key, &status);
579 					if (u8key) {
580 						spprintf(&message, 0, "No strategy to convert the "
581 							"value given for the argument with key '%s' "
582 							"is available", ZSTR_VAL(u8key));
583 						intl_errors_set(&err,
584 							U_ILLEGAL_ARGUMENT_ERROR, message, 1);
585 						zend_string_release_ex(u8key, 0);
586 						efree(message);
587 					}
588 				}
589 			}
590 		}
591 		argNum++;
592 	} ZEND_HASH_FOREACH_END(); // visiting each argument
593 
594 	if (U_FAILURE(err.code)) {
595 		return;
596 	}
597 
598 	UnicodeString resultStr;
599 	FieldPosition fieldPosition(0);
600 
601 	/* format the message */
602 	mf->format(farg_names.empty() ? NULL : &farg_names[0],
603 		fargs.empty() ? NULL : &fargs[0], arg_count, resultStr, err.code);
604 
605 	if (U_FAILURE(err.code)) {
606 		intl_errors_set(&err, err.code,
607 			"Call to ICU MessageFormat::format() has failed", 0);
608 		return;
609 	}
610 
611 	*formatted_len = resultStr.length();
612 	*formatted = eumalloc(*formatted_len+1);
613 	resultStr.extract(*formatted, *formatted_len+1, err.code);
614 	if (U_FAILURE(err.code)) {
615 		intl_errors_set(&err, err.code,
616 			"Error copying format() result", 0);
617 		return;
618 	}
619 }
620 
621 #define cleanup_zvals() for(int j=i;j>=0;j--) { zval_ptr_dtor((*args)+i); }
622 
umsg_parse_helper(UMessageFormat * fmt,int * count,zval ** args,UChar * source,int32_t source_len,UErrorCode * status)623 U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval **args, UChar *source, int32_t source_len, UErrorCode *status)
624 {
625     UnicodeString srcString(source, source_len);
626     Formattable *fargs = ((const MessageFormat*)fmt)->parse(srcString, *count, *status);
627 
628 	if(U_FAILURE(*status)) {
629 		return;
630 	}
631 
632 	*args = (zval *)safe_emalloc(*count, sizeof(zval), 0);
633 
634     // assign formattables to varargs
635     for(int32_t i = 0; i < *count; i++) {
636 	    int64_t aInt64;
637 		double aDate;
638 		UnicodeString temp;
639 		zend_string *u8str;
640 
641 		switch(fargs[i].getType()) {
642         case Formattable::kDate:
643 			aDate = ((double)fargs[i].getDate())/U_MILLIS_PER_SECOND;
644 			ZVAL_DOUBLE(&(*args)[i], aDate);
645             break;
646 
647         case Formattable::kDouble:
648 			ZVAL_DOUBLE(&(*args)[i], (double)fargs[i].getDouble());
649             break;
650 
651         case Formattable::kLong:
652 			ZVAL_LONG(&(*args)[i], fargs[i].getLong());
653             break;
654 
655         case Formattable::kInt64:
656             aInt64 = fargs[i].getInt64();
657 			if(aInt64 > ZEND_LONG_MAX || aInt64 < -ZEND_LONG_MAX) {
658 				ZVAL_DOUBLE(&(*args)[i], (double)aInt64);
659 			} else {
660 				ZVAL_LONG(&(*args)[i], (zend_long)aInt64);
661 			}
662             break;
663 
664         case Formattable::kString:
665             fargs[i].getString(temp);
666 			u8str = intl_convert_utf16_to_utf8(temp.getBuffer(), temp.length(), status);
667 			if(!u8str) {
668 				cleanup_zvals();
669 				return;
670 			}
671 			ZVAL_NEW_STR(&(*args)[i], u8str);
672             break;
673 
674         case Formattable::kObject:
675         case Formattable::kArray:
676             *status = U_ILLEGAL_ARGUMENT_ERROR;
677 			cleanup_zvals();
678             break;
679         }
680     }
681 	delete[] fargs;
682 }
683