1 /*
2  * This is the vsnprintf for scummvm/symbian implementation from the original
3  * snprintf.c, all support functions has been removed and vsnprintf renamed to
4  * symbian_vsnprintf
5  * According to the homepage this function may be licensed under either the
6  * Frontier Artistic License or the GPL.
7  *
8  * snprintf.c - a portable implementation of snprintf
9  *
10  * AUTHOR
11  *   Mark Martinec <mark.martinec@ijs.si>, April 1999.
12  *
13  *   Copyright 1999, Mark Martinec. All rights reserved.
14  *
15  * TERMS AND CONDITIONS
16  *   This program is free software; you can redistribute it and/or modify
17  *   it under the terms of the "Frontier Artistic License" which comes
18  *   with this Kit.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23  *   See the Frontier Artistic License for more details.
24  *
25  *   You should have received a copy of the Frontier Artistic License
26  *   with this Kit in the file named LICENSE.txt .
27  *   If not, I'll be glad to provide one.
28  *
29  * FEATURES
30  * - careful adherence to specs regarding flags, field width and precision;
31  * - good performance for large string handling (large format, large
32  *   argument or large paddings). Performance is similar to system's sprintf
33  *   and in several cases significantly better (make sure you compile with
34  *   optimizations turned on, tell the compiler the code is strict ANSI
35  *   if necessary to give it more freedom for optimizations);
36  * - return value semantics per ISO/IEC 9899:1999 ("ISO C99");
37  * - written in standard ISO/ANSI C - requires an ANSI C compiler.
38  *
39  * SUPPORTED CONVERSION SPECIFIERS AND DATA TYPES
40  *
41  * This snprintf only supports the following conversion specifiers:
42  * s, c, d, u, o, x, X, p  (and synonyms: i, D, U, O - see below)
43  * with flags: '-', '+', ' ', '0' and '#'.
44  * An asterisk is supported for field width as well as precision.
45  *
46  * Length modifiers 'h' (short int), 'l' (long int),
47  * and 'll' (long long int) are supported.
48  * NOTE:
49  *   If macro SNPRINTF_LONGLONG_SUPPORT is not defined (default) the
50  *   length modifier 'll' is recognized but treated the same as 'l',
51  *   which may cause argument value truncation! Defining
52  *   SNPRINTF_LONGLONG_SUPPORT requires that your system's sprintf also
53  *   handles length modifier 'll'.  long long int is a language extension
54  *   which may not be portable.
55  *
56  * Conversion of numeric data (conversion specifiers d, u, o, x, X, p)
57  * with length modifiers (none or h, l, ll) is left to the system routine
58  * sprintf, but all handling of flags, field width and precision as well as
59  * c and s conversions is done very carefully by this portable routine.
60  * If a string precision (truncation) is specified (e.g. %.8s) it is
61  * guaranteed the string beyond the specified precision will not be referenced.
62  *
63  * Length modifiers h, l and ll are ignored for c and s conversions (data
64  * types wint_t and wchar_t are not supported).
65  *
66  * The following common synonyms for conversion characters are supported:
67  *   - i is a synonym for d
68  *   - D is a synonym for ld, explicit length modifiers are ignored
69  *   - U is a synonym for lu, explicit length modifiers are ignored
70  *   - O is a synonym for lo, explicit length modifiers are ignored
71  * The D, O and U conversion characters are nonstandard, they are supported
72  * for backward compatibility only, and should not be used for new code.
73  *
74  * The following is specifically NOT supported:
75  *   - flag ' (thousands' grouping character) is recognized but ignored
76  *   - numeric conversion specifiers: f, e, E, g, G and synonym F,
77  *     as well as the new a and A conversion specifiers
78  *   - length modifier 'L' (long double) and 'q' (quad - use 'll' instead)
79  *   - wide character/string conversions: lc, ls, and nonstandard
80  *     synonyms C and S
81  *   - writeback of converted string length: conversion character n
82  *   - the n$ specification for direct reference to n-th argument
83  *   - locales
84  *
85  * It is permitted for str_m to be zero, and it is permitted to specify NULL
86  * pointer for resulting string argument if str_m is zero (as per ISO C99).
87  *
88  * The return value is the number of characters which would be generated
89  * for the given input, excluding the trailing null. If this value
90  * is greater or equal to str_m, not all characters from the result
91  * have been stored in str, output bytes beyond the (str_m-1) -th character
92  * are discarded. If str_m is greater than zero it is guaranteed
93  * the resulting string will be null-terminated.
94  *
95  * NOTE that this matches the ISO C99, OpenBSD, and GNU C library 2.1,
96  * but is different from some older and vendor implementations,
97  * and is also different from XPG, XSH5, SUSv2 specifications.
98  * For historical discussion on changes in the semantics and standards
99  * of snprintf see printf(3) man page in the Linux programmers manual.
100  *
101  * Routines asprintf and vasprintf return a pointer (in the ptr argument)
102  * to a buffer sufficiently large to hold the resulting string. This pointer
103  * should be passed to free(3) to release the allocated storage when it is
104  * no longer needed. If sufficient space cannot be allocated, these functions
105  * will return -1 and set ptr to be a NULL pointer. These two routines are a
106  * GNU C library extensions (glibc).
107  *
108  * Routines asnprintf and vasnprintf are similar to asprintf and vasprintf,
109  * yet, like snprintf and vsnprintf counterparts, will write at most str_m-1
110  * characters into the allocated output string, the last character in the
111  * allocated buffer then gets the terminating null. If the formatted string
112  * length (the return value) is greater than or equal to the str_m argument,
113  * the resulting string was truncated and some of the formatted characters
114  * were discarded. These routines present a handy way to limit the amount
115  * of allocated memory to some sane value.
116  *
117  * AVAILABILITY
118  *   http://www.ijs.si/software/snprintf/
119  *
120  * REVISION HISTORY
121  * 1999-04	V0.9  Mark Martinec
122  *		- initial version, some modifications after comparing printf
123  *		  man pages for Digital Unix 4.0, Solaris 2.6 and HPUX 10,
124  *		  and checking how Perl handles sprintf (differently!);
125  * 1999-04-09	V1.0  Mark Martinec <mark.martinec@ijs.si>
126  *		- added main test program, fixed remaining inconsistencies,
127  *		  added optional (long long int) support;
128  * 1999-04-12	V1.1  Mark Martinec <mark.martinec@ijs.si>
129  *		- support the 'p' conversion (pointer to void);
130  *		- if a string precision is specified
131  *		  make sure the string beyond the specified precision
132  *		  will not be referenced (e.g. by strlen);
133  * 1999-04-13	V1.2  Mark Martinec <mark.martinec@ijs.si>
134  *		- support synonyms %D=%ld, %U=%lu, %O=%lo;
135  *		- speed up the case of long format string with few conversions;
136  * 1999-06-30	V1.3  Mark Martinec <mark.martinec@ijs.si>
137  *		- fixed runaway loop (eventually crashing when str_l wraps
138  *		  beyond 2^31) while copying format string without
139  *		  conversion specifiers to a buffer that is too short
140  *		  (thanks to Edwin Young <edwiny@autonomy.com> for
141  *		  spotting the problem);
142  *		- added macros PORTABLE_SNPRINTF_VERSION_(MAJOR|MINOR)
143  *		  to snprintf.h
144  * 2000-02-14	V2.0 (never released) Mark Martinec <mark.martinec@ijs.si>
145  *		- relaxed license terms: The Artistic License now applies.
146  *		  You may still apply the GNU GENERAL PUBLIC LICENSE
147  *		  as was distributed with previous versions, if you prefer;
148  *		- changed REVISION HISTORY dates to use ISO 8601 date format;
149  *		- added vsnprintf (patch also independently proposed by
150  *		  Caolan McNamara 2000-05-04, and Keith M Willenson 2000-06-01)
151  * 2000-06-27	V2.1  Mark Martinec <mark.martinec@ijs.si>
152  *		- removed POSIX check for str_m<1; value 0 for str_m is
153  *		  allowed by ISO C99 (and GNU C library 2.1) - (pointed out
154  *		  on 2000-05-04 by Caolan McNamara, caolan@ csn dot ul dot ie).
155  *		  Besides relaxed license this change in standards adherence
156  *		  is the main reason to bump up the major version number;
157  *		- added nonstandard routines asnprintf, vasnprintf, asprintf,
158  *		  vasprintf that dynamically allocate storage for the
159  *		  resulting string; these routines are not compiled by default,
160  *		  see comments where NEED_V?ASN?PRINTF macros are defined;
161  *		- autoconf contributed by Caolan McNamara
162  * 2000-10-06	V2.2  Mark Martinec <mark.martinec@ijs.si>
163  *		- BUG FIX: the %c conversion used a temporary variable
164  *		  that was no longer in scope when referenced,
165  *		  possibly causing incorrect resulting character;
166  *		- BUG FIX: make precision and minimal field width unsigned
167  *		  to handle huge values (2^31 <= n < 2^32) correctly;
168  *		  also be more careful in the use of signed/unsigned/size_t
169  *		  internal variables - probably more careful than many
170  *		  vendor implementations, but there may still be a case
171  *		  where huge values of str_m, precision or minimal field
172  *		  could cause incorrect behaviour;
173  *		- use separate variables for signed/unsigned arguments,
174  *		  and for short/int, long, and long long argument lengths
175  *		  to avoid possible incompatibilities on certain
176  *		  computer architectures. Also use separate variable
177  *		  arg_sign to hold sign of a numeric argument,
178  *		  to make code more transparent;
179  *		- some fiddling with zero padding and "0x" to make it
180  *		  Linux compatible;
181  *		- systematically use macros fast_memcpy and fast_memset
182  *		  instead of case-by-case hand optimization; determine some
183  *		  breakeven string lengths for different architectures;
184  *		- terminology change: 'format' -> 'conversion specifier',
185  *		  'C9x' -> 'ISO/IEC 9899:1999 ("ISO C99")',
186  *		  'alternative form' -> 'alternate form',
187  *		  'data type modifier' -> 'length modifier';
188  *		- several comments rephrased and new ones added;
189  *		- make compiler not complain about 'credits' defined but
190  *		  not used;
191  */
192 /* ============================================= */
193 /* NO USER SERVICABLE PARTS FOLLOWING THIS POINT */
194 /* ============================================= */
195 
196 #define PORTABLE_SNPRINTF_VERSION_MAJOR 2
197 #define PORTABLE_SNPRINTF_VERSION_MINOR 2
198 #include <sys/types.h>
199 #include <string.h>
200 #include <stdlib.h>
201 #include <stdio.h>
202 #include <stdarg.h>
203 #include <assert.h>
204 #include <errno.h>
205 #ifdef isdigit
206 #undef isdigit
207 #endif
208 #define isdigit(c) ((c) >= '0' && (c) <= '9')
209 
210 #ifndef breakeven_point
211 #  define breakeven_point   6	/* some reasonable one-size-fits-all value */
212 #endif
213 
214 #define fast_memcpy(d,s,n) \
215 { size_t nn = (size_t)(n); \
216 	if (nn >= breakeven_point) memcpy((d), (s), nn); \
217 	else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
218 	char *dd; const char *ss; \
219 for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } }
220 
221 #define fast_memset(d,c,n) \
222 { size_t nn = (size_t)(n); \
223 	if (nn >= breakeven_point) memset((d), (int)(c), nn); \
224 	else if (nn > 0) { /* proc call overhead is worth only for large strings*/\
225 	char *dd; const int cc=(int)(c); \
226 for (dd=(d); nn>0; nn--) *dd++ = cc; } }
227 
228 
229 /* declarations */
230 
231 static char credits[] = "\n\
232 @(#)snprintf.c, v2.2: Mark Martinec, <mark.martinec@ijs.si>\n\
233 @(#)snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies.\n\
234 @(#)snprintf.c, v2.2: http://www.ijs.si/software/snprintf/\n";
symbian_vsnprintf(char * str,size_t str_m,const char * fmt,va_list ap)235 int symbian_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) {
236 
237 	size_t str_l = 0;
238 	const char *p = fmt;
239 
240 	/* In contrast with POSIX, the ISO C99 now says
241 	* that str can be NULL and str_m can be 0.
242 	* This is more useful than the old:  if (str_m < 1) return -1; */
243 
244 	if (!p) p = "";
245 	while (*p) {
246 		if (*p != '%') {
247 			/* if (str_l < str_m) str[str_l++] = *p++;    -- this would be sufficient */
248 			/* but the following code achieves better performance for cases
249 			* where format string is long and contains few conversions */
250 			const char *q = strchr(p + 1, '%');
251 			size_t n = !q ? strlen(p) : (q - p);
252 			if (str_l < str_m) {
253 				size_t avail = str_m - str_l;
254 				fast_memcpy(str + str_l, p, (n > avail ? avail : n));
255 			}
256 			p += n;
257 			str_l += n;
258 		} else {
259 			const char *starting_p;
260 			size_t min_field_width = 0, precision = 0;
261 			int zero_padding = 0, precision_specified = 0, justify_left = 0;
262 			int alternate_form = 0, force_sign = 0;
263 			int space_for_positive = 1; /* If both the ' ' and '+' flags appear,
264 			the ' ' flag should be ignored. */
265 			char length_modifier = '\0';            /* allowed values: \0, h, l, L */
266 			char tmp[32];/* temporary buffer for simple numeric->string conversion */
267 
268 			const char *str_arg;      /* string address in case of string argument */
269 			size_t str_arg_l;         /* natural field width of arg without padding
270 									  and sign */
271 			unsigned char uchar_arg;
272 			/* unsigned char argument value - only defined for c conversion.
273 			N.B. standard explicitly states the char argument for
274 			the c conversion is unsigned */
275 
276 			size_t number_of_zeros_to_pad = 0;
277 			/* number of zeros to be inserted for numeric conversions
278 			as required by the precision or minimal field width */
279 
280 			size_t zero_padding_insertion_ind = 0;
281 			/* index into tmp where zero padding is to be inserted */
282 
283 			char fmt_spec = '\0';
284 			/* current conversion specifier character */
285 
286 			str_arg = credits;/* just to make compiler happy (defined but not used)*/
287 			str_arg = NULL;
288 			starting_p = p;
289 			p++;  /* skip '%' */
290 			/* parse flags */
291 			while (*p == '0' || *p == '-' || *p == '+' ||
292 			       *p == ' ' || *p == '#' || *p == '\'') {
293 				switch (*p) {
294 				case '0':
295 					zero_padding = 1;
296 					break;
297 				case '-':
298 					justify_left = 1;
299 					break;
300 				case '+':
301 					force_sign = 1;
302 					space_for_positive = 0;
303 					break;
304 				case ' ':
305 					force_sign = 1;
306 					/* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */
307 					break;
308 				case '#':
309 					alternate_form = 1;
310 					break;
311 				case '\'':
312 					break;
313 				}
314 				p++;
315 			}
316 			/* If the '0' and '-' flags both appear, the '0' flag should be ignored. */
317 
318 			/* parse field width */
319 			if (*p == '*') {
320 				int j;
321 				p++;
322 				j = va_arg(ap, int);
323 				if (j >= 0) min_field_width = j;
324 				else { min_field_width = -j; justify_left = 1; }
325 			} else if (isdigit((int)(*p))) {
326 				/* size_t could be wider than unsigned int;
327 				    make sure we treat argument like common implementations do */
328 				unsigned int uj = *p++ - '0';
329 				while (isdigit((int)(*p))) uj = 10 * uj + (unsigned int)(*p++ - '0');
330 				min_field_width = uj;
331 			}
332 			/* parse precision */
333 			if (*p == '.') {
334 				p++;
335 				precision_specified = 1;
336 				if (*p == '*') {
337 					int j = va_arg(ap, int);
338 					p++;
339 					if (j >= 0) precision = j;
340 					else {
341 						precision_specified = 0;
342 						precision = 0;
343 						/* NOTE:
344 						*   Solaris 2.6 man page claims that in this case the precision
345 						*   should be set to 0.  Digital Unix 4.0, HPUX 10 and BSD man page
346 						*   claim that this case should be treated as unspecified precision,
347 						*   which is what we do here.
348 						*/
349 					}
350 				} else if (isdigit((int)(*p))) {
351 					/* size_t could be wider than unsigned int;
352 					    make sure we treat argument like common implementations do */
353 					unsigned int uj = *p++ - '0';
354 					while (isdigit((int)(*p))) uj = 10 * uj + (unsigned int)(*p++ - '0');
355 					precision = uj;
356 				}
357 			}
358 			/* parse 'h', 'l' and 'll' length modifiers */
359 			if (*p == 'h' || *p == 'l') {
360 				length_modifier = *p;
361 				p++;
362 				if (length_modifier == 'l' && *p == 'l') {   /* double l = long long */
363 #ifdef SNPRINTF_LONGLONG_SUPPORT
364 					length_modifier = '2';                  /* double l encoded as '2' */
365 #else
366 					length_modifier = 'l';                 /* treat it as a single 'l' */
367 #endif
368 					p++;
369 				}
370 			}
371 			fmt_spec = *p;
372 			/* common synonyms: */
373 			switch (fmt_spec) {
374 			case 'i':
375 				fmt_spec = 'd';
376 				break;
377 			case 'D':
378 				fmt_spec = 'd';
379 				length_modifier = 'l';
380 				break;
381 			case 'U':
382 				fmt_spec = 'u';
383 				length_modifier = 'l';
384 				break;
385 			case 'O':
386 				fmt_spec = 'o';
387 				length_modifier = 'l';
388 				break;
389 			default:
390 				break;
391 			}
392 			/* get parameter value, do initial processing */
393 			switch (fmt_spec) {
394 			case '%': /* % behaves similar to 's' regarding flags and field widths */
395 			case 'c': /* c behaves similar to 's' regarding flags and field widths */
396 			case 's':
397 				length_modifier = '\0';          /* wint_t and wchar_t not supported */
398 				/* the result of zero padding flag with non-numeric conversion specifier*/
399 				/* is undefined. Solaris and HPUX 10 does zero padding in this case,    */
400 				/* Digital Unix and Linux does not. */
401 				zero_padding = 0;    /* turn zero padding off for string conversions */
402 				str_arg_l = 1;
403 				switch (fmt_spec) {
404 				case '%':
405 					str_arg = p;
406 					break;
407 				case 'c': {
408 					int j = va_arg(ap, int);
409 					uchar_arg = (unsigned char) j;   /* standard demands unsigned char */
410 					str_arg = (const char *) & uchar_arg;
411 					break;
412 				}
413 				case 's':
414 					str_arg = va_arg(ap, const char *);
415 					if (!str_arg) str_arg_l = 0;
416 					/* make sure not to address string beyond the specified precision !!! */
417 					else if (!precision_specified) str_arg_l = strlen(str_arg);
418 					/* truncate string if necessary as requested by precision */
419 					else if (precision == 0) str_arg_l = 0;
420 					else {
421 						/* memchr on HP does not like n > 2^31  !!! */
422 						const char *q = (const char*) memchr(str_arg, '\0',
423 						                                     precision <= 0x7fffffff ? precision : 0x7fffffff);
424 						str_arg_l = !q ? precision : (q - str_arg);
425 					}
426 					break;
427 				default:
428 					break;
429 				}
430 				break;
431 			case 'd':
432 			case 'u':
433 			case 'o':
434 			case 'x':
435 			case 'X':
436 			case 'p': {
437 				/* NOTE: the u, o, x, X and p conversion specifiers imply
438 				    the value is unsigned;  d implies a signed value */
439 
440 				int arg_sign = 0;
441 				/* 0 if numeric argument is zero (or if pointer is NULL for 'p'),
442 				+1 if greater than zero (or nonzero for unsigned arguments),
443 				-1 if negative (unsigned argument is never negative) */
444 
445 				int int_arg = 0;
446 				unsigned int uint_arg = 0;
447 				/* only defined for length modifier h, or for no length modifiers */
448 
449 				long int long_arg = 0;
450 				unsigned long int ulong_arg = 0;
451 				/* only defined for length modifier l */
452 
453 				void *ptr_arg = NULL;
454 				/* pointer argument value -only defined for p conversion */
455 
456 #ifdef SNPRINTF_LONGLONG_SUPPORT
457 				long long int long_long_arg = 0;
458 				unsigned long long int ulong_long_arg = 0;
459 				/* only defined for length modifier ll */
460 #endif
461 				if (fmt_spec == 'p') {
462 					/* HPUX 10: An l, h, ll or L before any other conversion character
463 					*   (other than d, i, u, o, x, or X) is ignored.
464 					* Digital Unix:
465 					*   not specified, but seems to behave as HPUX does.
466 					* Solaris: If an h, l, or L appears before any other conversion
467 					*   specifier (other than d, i, u, o, x, or X), the behavior
468 					*   is undefined. (Actually %hp converts only 16-bits of address
469 					*   and %llp treats address as 64-bit data which is incompatible
470 					*   with (void *) argument on a 32-bit system).
471 					    */
472 					length_modifier = '\0';
473 					ptr_arg = va_arg(ap, void *);
474 					if (ptr_arg != NULL) arg_sign = 1;
475 				} else if (fmt_spec == 'd') {  /* signed */
476 					switch (length_modifier) {
477 					case '\0':
478 					case 'h':
479 						/* It is non-portable to specify a second argument of char or short
480 						* to va_arg, because arguments seen by the called function
481 						* are not char or short.  C converts char and short arguments
482 						* to int before passing them to a function.
483 						    */
484 						int_arg = va_arg(ap, int);
485 						if (int_arg > 0) arg_sign =  1;
486 						else if (int_arg < 0) arg_sign = -1;
487 						break;
488 					case 'l':
489 						long_arg = va_arg(ap, long int);
490 						if (long_arg > 0) arg_sign =  1;
491 						else if (long_arg < 0) arg_sign = -1;
492 						break;
493 #ifdef SNPRINTF_LONGLONG_SUPPORT
494 					case '2':
495 						long_long_arg = va_arg(ap, long long int);
496 						if (long_long_arg > 0) arg_sign =  1;
497 						else if (long_long_arg < 0) arg_sign = -1;
498 						break;
499 #endif
500 					}
501 				} else {  /* unsigned */
502 					switch (length_modifier) {
503 					case '\0':
504 					case 'h':
505 						uint_arg = va_arg(ap, unsigned int);
506 						if (uint_arg) arg_sign = 1;
507 						break;
508 					case 'l':
509 						ulong_arg = va_arg(ap, unsigned long int);
510 						if (ulong_arg) arg_sign = 1;
511 						break;
512 #ifdef SNPRINTF_LONGLONG_SUPPORT
513 					case '2':
514 						ulong_long_arg = va_arg(ap, unsigned long long int);
515 						if (ulong_long_arg) arg_sign = 1;
516 						break;
517 #endif
518 					}
519 				}
520 				str_arg = tmp;
521 				str_arg_l = 0;
522 				/* NOTE:
523 				*   For d, i, u, o, x, and X conversions, if precision is specified,
524 				*   the '0' flag should be ignored. This is so with Solaris 2.6,
525 				*   Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl.
526 				*/
527 				if (precision_specified) zero_padding = 0;
528 				if (fmt_spec == 'd') {
529 					if (force_sign && arg_sign >= 0)
530 						tmp[str_arg_l++] = space_for_positive ? ' ' : '+';
531 					/* leave negative numbers for sprintf to handle,
532 					to avoid handling tricky cases like (short int)(-32768) */
533 				} else if (alternate_form) {
534 					if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X'))
535 						{ tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; }
536 					/* alternate form should have no effect for p conversion, but ... */
537 				}
538 				zero_padding_insertion_ind = str_arg_l;
539 				if (!precision_specified) precision = 1;   /* default precision is 1 */
540 				if (precision == 0 && arg_sign == 0
541 				   ) {
542 					/* converted to null string */
543 					/* When zero value is formatted with an explicit precision 0,
544 					the resulting formatted string is empty (d, i, u, o, x, X, p).   */
545 				} else {
546 					char f[5];
547 					int f_l = 0;
548 					f[f_l++] = '%';    /* construct a simple format string for sprintf */
549 					if (!length_modifier) { } else if (length_modifier == '2') { f[f_l++] = 'l'; f[f_l++] = 'l'; } else f[f_l++] = length_modifier;
550 					f[f_l++] = fmt_spec;
551 					f[f_l++] = '\0';
552 					if (fmt_spec == 'p') str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg);
553 					else if (fmt_spec == 'd') {  /* signed */
554 						switch (length_modifier) {
555 						case '\0':
556 						case 'h':
557 							str_arg_l += sprintf(tmp + str_arg_l, f, int_arg);
558 							break;
559 						case 'l':
560 							str_arg_l += sprintf(tmp + str_arg_l, f, long_arg);
561 							break;
562 #ifdef SNPRINTF_LONGLONG_SUPPORT
563 						case '2':
564 							str_arg_l += sprintf(tmp + str_arg_l, f, long_long_arg);
565 							break;
566 #endif
567 						}
568 					} else {  /* unsigned */
569 						switch (length_modifier) {
570 						case '\0':
571 						case 'h':
572 							str_arg_l += sprintf(tmp + str_arg_l, f, uint_arg);
573 							break;
574 						case 'l':
575 							str_arg_l += sprintf(tmp + str_arg_l, f, ulong_arg);
576 							break;
577 #ifdef SNPRINTF_LONGLONG_SUPPORT
578 						case '2':
579 							str_arg_l += sprintf(tmp + str_arg_l, f, ulong_long_arg);
580 							break;
581 #endif
582 						}
583 					}
584 					/* include the optional minus sign and possible "0x"
585 					in the region before the zero padding insertion point */
586 					if (zero_padding_insertion_ind < str_arg_l &&
587 					        tmp[zero_padding_insertion_ind] == '-') {
588 						zero_padding_insertion_ind++;
589 					}
590 					if (zero_padding_insertion_ind + 1 < str_arg_l &&
591 					        tmp[zero_padding_insertion_ind]   == '0' &&
592 					        (tmp[zero_padding_insertion_ind+1] == 'x' ||
593 					         tmp[zero_padding_insertion_ind+1] == 'X')) {
594 						zero_padding_insertion_ind += 2;
595 					}
596 				}
597 				{
598 					size_t num_of_digits = str_arg_l - zero_padding_insertion_ind;
599 					if (alternate_form && fmt_spec == 'o'
600 					        /* unless zero is already the first character */
601 					        && !(zero_padding_insertion_ind < str_arg_l
602 					             && tmp[zero_padding_insertion_ind] == '0')
603 					   ) {        /* assure leading zero for alternate-form octal numbers */
604 						if (!precision_specified || precision < num_of_digits + 1) {
605 							/* precision is increased to force the first character to be zero,
606 							except if a zero value is formatted with an explicit precision
607 							    of zero */
608 							precision = num_of_digits + 1;
609 							precision_specified = 1;
610 						}
611 					}
612 					/* zero padding to specified precision? */
613 					if (num_of_digits < precision)
614 						number_of_zeros_to_pad = precision - num_of_digits;
615 				}
616 				/* zero padding to specified minimal field width? */
617 				if (!justify_left && zero_padding) {
618 					int n = min_field_width - (str_arg_l + number_of_zeros_to_pad);
619 					if (n > 0) number_of_zeros_to_pad += n;
620 				}
621 				break;
622 			}
623 			default: /* unrecognized conversion specifier, keep format string as-is*/
624 				zero_padding = 0;  /* turn zero padding off for non-numeric convers. */
625 				justify_left = 1;
626 				min_field_width = 0;                /* reset flags */
627 				/* discard the unrecognized conversion, just keep *
628 				* the unrecognized conversion character          */
629 				str_arg = p;
630 				str_arg_l = 0;
631 				if (*p) str_arg_l++;  /* include invalid conversion specifier unchanged
632 		  if not at end-of-string */
633 				break;
634 			}
635 			if (*p) p++;      /* step over the just processed conversion specifier */
636 			/* insert padding to the left as requested by min_field_width;
637 			this does not include the zero padding in case of numerical conversions*/
638 			if (!justify_left) {                /* left padding with blank or zero */
639 				int n = min_field_width - (str_arg_l + number_of_zeros_to_pad);
640 				if (n > 0) {
641 					if (str_l < str_m) {
642 						size_t avail = str_m - str_l;
643 						fast_memset(str + str_l, (zero_padding ? '0' : ' '), (n > avail ? avail : n));
644 					}
645 					str_l += n;
646 				}
647 			}
648 			/* zero padding as requested by the precision or by the minimal field width
649 			* for numeric conversions required? */
650 			if (number_of_zeros_to_pad <= 0) {
651 				/* will not copy first part of numeric right now, *
652 				    * force it to be copied later in its entirety    */
653 				zero_padding_insertion_ind = 0;
654 			} else {
655 				/* insert first part of numerics (sign or '0x') before zero padding */
656 				int n = zero_padding_insertion_ind;
657 				if (n > 0) {
658 					if (str_l < str_m) {
659 						size_t avail = str_m - str_l;
660 						fast_memcpy(str + str_l, str_arg, (n > avail ? avail : n));
661 					}
662 					str_l += n;
663 				}
664 				/* insert zero padding as requested by the precision or min field width */
665 				n = number_of_zeros_to_pad;
666 				if (n > 0) {
667 					if (str_l < str_m) {
668 						size_t avail = str_m - str_l;
669 						fast_memset(str + str_l, '0', (n > avail ? avail : n));
670 					}
671 					str_l += n;
672 				}
673 			}
674 			/* insert formatted string
675 			* (or as-is conversion specifier for unknown conversions) */
676 			{
677 				int n = str_arg_l - zero_padding_insertion_ind;
678 				if (n > 0) {
679 					if (str_l < str_m) {
680 						size_t avail = str_m - str_l;
681 						fast_memcpy(str + str_l, str_arg + zero_padding_insertion_ind,
682 						            (n > avail ? avail : n));
683 					}
684 					str_l += n;
685 				}
686 			}
687 			/* insert right padding */
688 			if (justify_left) {          /* right blank padding to the field width */
689 				int n = min_field_width - (str_arg_l + number_of_zeros_to_pad);
690 				if (n > 0) {
691 					if (str_l < str_m) {
692 						size_t avail = str_m - str_l;
693 						fast_memset(str + str_l, ' ', (n > avail ? avail : n));
694 					}
695 					str_l += n;
696 				}
697 			}
698 		}
699 	}
700 	if (str_m > 0) { /* make sure the string is null-terminated
701 				   even at the expense of overwriting the last character
702 	  (shouldn't happen, but just in case) */
703 		str[str_l <= str_m-1 ? str_l : str_m-1] = '\0';
704 	}
705 	/* Return the number of characters formatted (excluding trailing null
706 	 * character), that is, the number of characters that would have been
707 	 * written to the buffer if it were large enough.
708 	 *
709 	 * The value of str_l should be returned, but str_l is of unsigned type
710 	 * size_t, and snprintf is int, possibly leading to an undetected
711 	 * integer overflow, resulting in a negative return value, which is illegal.
712 	 * Both XSH5 and ISO C99 (at least the draft) are silent on this issue.
713 	 * Should errno be set to EOVERFLOW and EOF returned in this case???
714 	 */
715 	return (int) str_l;
716 }
717 
symbian_snprintf(char * text,size_t maxlen,const char * fmt,...)718 int symbian_snprintf(char *text, size_t maxlen, const char *fmt, ...) {
719 	va_list ap;
720 	int retval;
721 
722 	va_start(ap, fmt);
723 	retval = symbian_vsnprintf(text, maxlen, fmt, ap);
724 	va_end(ap);
725 
726 	return retval;
727 }
728