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