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    | https://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 #if U_ICU_VERSION_MAJOR_NUM < 65
312 	/* There is a bug in ICU < 64.1 (ICU-12584) which prevents MessageFormatter::getFormats()
313 	   to handle more than 10 formats correctly. The enumerator could be
314 	   used to walk through the present formatters using getFormat(), which
315 	   however seems to provide just a readonly access. This workaround
316 	   prevents crash when there are > 10 formats but doesn't set any error.
317 	   As a result, only DateFormatters with > 10 subformats are affected. */
318 	icu::StringEnumeration* fnames = mf->getFormatNames(err.code);
319 	if (!fnames || U_FAILURE(err.code)) {
320 		return;
321 	}
322 	count = fnames->count(err.code);
323 	delete fnames;
324 	if (count > 10) {
325 		return;
326 	}
327 #endif
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 		ZVAL_DEREF(elem);
396 		Formattable& formattable = fargs[argNum];
397 		UnicodeString& key = farg_names[argNum];
398 		Formattable::Type argType = Formattable::kObject, //unknown
399 						  *storedArgType = NULL;
400 		if (!U_SUCCESS(err.code)) {
401 			break;
402 		}
403 		/* Process key and retrieve type */
404 		if (str_index == NULL) {
405 			/* includes case where index < 0 because it's exposed as unsigned */
406 			if (num_index > (zend_ulong)INT32_MAX) {
407 				intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
408 					"Found negative or too large array key", 0);
409 				continue;
410 			}
411 
412 		   UChar temp[16];
413 		   int32_t len = u_sprintf(temp, "%u", (uint32_t)num_index);
414 		   key.append(temp, len);
415 
416 		   storedArgType = (Formattable::Type*)zend_hash_index_find_ptr(types, (zend_ulong)num_index);
417 		} else { //string; assumed to be in UTF-8
418 			intl_stringFromChar(key, ZSTR_VAL(str_index), ZSTR_LEN(str_index), &err.code);
419 
420 			if (U_FAILURE(err.code)) {
421 				char *message;
422 				spprintf(&message, 0,
423 					"Invalid UTF-8 data in argument key: '%s'", ZSTR_VAL(str_index));
424 				intl_errors_set(&err, err.code,	message, 1);
425 				efree(message);
426 				continue;
427 			}
428 
429 			storedArgType = (Formattable::Type*)zend_hash_str_find_ptr(types, (char*)key.getBuffer(), key.length() * sizeof(UChar));
430 		}
431 
432 		if (storedArgType != NULL) {
433 			argType = *storedArgType;
434 		}
435 
436 		/* Convert zval to formattable according to message format type
437 		 * or (as a fallback) the zval type */
438 		if (argType != Formattable::kObject) {
439 			switch (argType) {
440 			case Formattable::kString:
441 				{
442 					zend_string *str, *tmp_str;
443 
444 	string_arg:
445 					/* This implicitly converts objects
446 					 * Note that our vectors will leak if object conversion fails
447 					 * and PHP ends up with a fatal error and calls longjmp
448 					 * as a result of that.
449 					 */
450 					str = zval_get_tmp_string(elem, &tmp_str);
451 
452 					UnicodeString *text = new UnicodeString();
453 					intl_stringFromChar(*text,
454 						ZSTR_VAL(str), ZSTR_LEN(str), &err.code);
455 
456 					if (U_FAILURE(err.code)) {
457 						char *message;
458 						spprintf(&message, 0, "Invalid UTF-8 data in string argument: "
459 							"'%s'", ZSTR_VAL(str));
460 						intl_errors_set(&err, err.code, message, 1);
461 						efree(message);
462 						delete text;
463 						continue;
464 					}
465 					formattable.adoptString(text);
466 					zend_tmp_string_release(tmp_str);
467 					break;
468 				}
469 			case Formattable::kDouble:
470 				{
471 					double d = zval_get_double(elem);
472 					formattable.setDouble(d);
473 					break;
474 				}
475 			case Formattable::kLong:
476 				{
477 					int32_t tInt32 = 0;
478 
479 					if (Z_TYPE_P(elem) == IS_DOUBLE) {
480 						if (Z_DVAL_P(elem) > (double)INT32_MAX ||
481 								Z_DVAL_P(elem) < (double)INT32_MIN) {
482 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
483 								"Found PHP float with absolute value too large for "
484 								"32 bit integer argument", 0);
485 						} else {
486 							tInt32 = (int32_t)Z_DVAL_P(elem);
487 						}
488 					} else if (Z_TYPE_P(elem) == IS_LONG) {
489 						if (Z_LVAL_P(elem) > INT32_MAX ||
490 								Z_LVAL_P(elem) < INT32_MIN) {
491 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
492 								"Found PHP integer with absolute value too large "
493 								"for 32 bit integer argument", 0);
494 						} else {
495 							tInt32 = (int32_t)Z_LVAL_P(elem);
496 						}
497 					} else {
498 						tInt32 = (int32_t)zval_get_long(elem);
499 					}
500 					formattable.setLong(tInt32);
501 					break;
502 				}
503 			case Formattable::kInt64:
504 				{
505 					int64_t tInt64 = 0;
506 
507 					if (Z_TYPE_P(elem) == IS_DOUBLE) {
508 						if (Z_DVAL_P(elem) > (double)U_INT64_MAX ||
509 								Z_DVAL_P(elem) < (double)U_INT64_MIN) {
510 							intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
511 								"Found PHP float with absolute value too large for "
512 								"64 bit integer argument", 0);
513 						} else {
514 							tInt64 = (int64_t)Z_DVAL_P(elem);
515 						}
516 					} else if (Z_TYPE_P(elem) == IS_LONG) {
517 						/* assume long is not wider than 64 bits */
518 						tInt64 = (int64_t)Z_LVAL_P(elem);
519 					} else {
520 						tInt64 = (int64_t)zval_get_long(elem);
521 					}
522 					formattable.setInt64(tInt64);
523 					break;
524 				}
525 			case Formattable::kDate:
526 				{
527 					double dd = intl_zval_to_millis(elem, &err, "msgfmt_format");
528 					if (U_FAILURE(err.code)) {
529 						char *message;
530 						zend_string *u8key;
531 						UErrorCode status = UErrorCode();
532 						u8key = intl_charFromString(key, &status);
533 						if (u8key) {
534 							spprintf(&message, 0, "The argument for key '%s' "
535 								"cannot be used as a date or time", ZSTR_VAL(u8key));
536 							intl_errors_set(&err, err.code, message, 1);
537 							zend_string_release_ex(u8key, 0);
538 							efree(message);
539 						}
540 						continue;
541 					}
542 					formattable.setDate(dd);
543 					break;
544 				}
545 			default:
546 				intl_errors_set(&err, U_ILLEGAL_ARGUMENT_ERROR,
547 					"Found unsupported argument type", 0);
548 				break;
549 			}
550 		} else {
551 			/* We couldn't find any information about the argument in the pattern, this
552 			 * means it's an extra argument. So convert it to a number if it's a number or
553 			 * bool or null and to a string if it's anything else except arrays . */
554 			switch (Z_TYPE_P(elem)) {
555 			case IS_DOUBLE:
556 				formattable.setDouble(Z_DVAL_P(elem));
557 				break;
558 			case IS_LONG:
559 				formattable.setInt64((int64_t)Z_LVAL_P(elem));
560 				break;
561 			case IS_NULL:
562 			case IS_FALSE:
563 				formattable.setInt64((int64_t)0);
564 				break;
565 			case IS_TRUE:
566 				formattable.setInt64((int64_t)1);
567 				break;
568 			case IS_STRING:
569 			case IS_OBJECT:
570 				goto string_arg;
571 			default:
572 				{
573 					char *message;
574 					zend_string *u8key;
575 					UErrorCode status = UErrorCode();
576 					u8key = intl_charFromString(key, &status);
577 					if (u8key) {
578 						spprintf(&message, 0, "No strategy to convert the "
579 							"value given for the argument with key '%s' "
580 							"is available", ZSTR_VAL(u8key));
581 						intl_errors_set(&err,
582 							U_ILLEGAL_ARGUMENT_ERROR, message, 1);
583 						zend_string_release_ex(u8key, 0);
584 						efree(message);
585 					}
586 				}
587 			}
588 		}
589 		argNum++;
590 	} ZEND_HASH_FOREACH_END(); // visiting each argument
591 
592 	if (U_FAILURE(err.code)) {
593 		return;
594 	}
595 
596 	UnicodeString resultStr;
597 	FieldPosition fieldPosition(0);
598 
599 	/* format the message */
600 	mf->format(farg_names.empty() ? NULL : &farg_names[0],
601 		fargs.empty() ? NULL : &fargs[0], arg_count, resultStr, err.code);
602 
603 	if (U_FAILURE(err.code)) {
604 		intl_errors_set(&err, err.code,
605 			"Call to ICU MessageFormat::format() has failed", 0);
606 		return;
607 	}
608 
609 	*formatted_len = resultStr.length();
610 	*formatted = eumalloc(*formatted_len+1);
611 	resultStr.extract(*formatted, *formatted_len+1, err.code);
612 	if (U_FAILURE(err.code)) {
613 		intl_errors_set(&err, err.code,
614 			"Error copying format() result", 0);
615 		return;
616 	}
617 }
618 
619 #define cleanup_zvals() for(int j=i;j>=0;j--) { zval_ptr_dtor((*args)+i); }
620 
umsg_parse_helper(UMessageFormat * fmt,int * count,zval ** args,UChar * source,int32_t source_len,UErrorCode * status)621 U_CFUNC void umsg_parse_helper(UMessageFormat *fmt, int *count, zval **args, UChar *source, int32_t source_len, UErrorCode *status)
622 {
623     UnicodeString srcString(source, source_len);
624     Formattable *fargs = ((const MessageFormat*)fmt)->parse(srcString, *count, *status);
625 
626 	if(U_FAILURE(*status)) {
627 		return;
628 	}
629 
630 	*args = (zval *)safe_emalloc(*count, sizeof(zval), 0);
631 
632     // assign formattables to varargs
633     for(int32_t i = 0; i < *count; i++) {
634 	    int64_t aInt64;
635 		double aDate;
636 		UnicodeString temp;
637 		zend_string *u8str;
638 
639 		switch(fargs[i].getType()) {
640         case Formattable::kDate:
641 			aDate = ((double)fargs[i].getDate())/U_MILLIS_PER_SECOND;
642 			ZVAL_DOUBLE(&(*args)[i], aDate);
643             break;
644 
645         case Formattable::kDouble:
646 			ZVAL_DOUBLE(&(*args)[i], (double)fargs[i].getDouble());
647             break;
648 
649         case Formattable::kLong:
650 			ZVAL_LONG(&(*args)[i], fargs[i].getLong());
651             break;
652 
653         case Formattable::kInt64:
654             aInt64 = fargs[i].getInt64();
655 			if(aInt64 > ZEND_LONG_MAX || aInt64 < -ZEND_LONG_MAX) {
656 				ZVAL_DOUBLE(&(*args)[i], (double)aInt64);
657 			} else {
658 				ZVAL_LONG(&(*args)[i], (zend_long)aInt64);
659 			}
660             break;
661 
662         case Formattable::kString:
663             fargs[i].getString(temp);
664 			u8str = intl_convert_utf16_to_utf8(temp.getBuffer(), temp.length(), status);
665 			if(!u8str) {
666 				cleanup_zvals();
667 				return;
668 			}
669 			ZVAL_NEW_STR(&(*args)[i], u8str);
670             break;
671 
672         case Formattable::kObject:
673         case Formattable::kArray:
674             *status = U_ILLEGAL_ARGUMENT_ERROR;
675 			cleanup_zvals();
676             break;
677         }
678     }
679 	delete[] fargs;
680 }
681