2 * ntdll printf functions
4 * Copyright 1999, 2009 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
34 #include "ntdll_misc.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
40 static const SIZE_T size_max
= ~(SIZE_T
)0 >> 1;
42 /* FIXME: convert sizes to SIZE_T etc. */
44 typedef struct pf_output_t
55 typedef struct pf_flags_t
57 char Sign
, LeftAlign
, Alternate
, PadZero
;
58 int FieldLength
, Precision
;
59 char IntegerLength
, IntegerDouble
;
65 * writes a string of characters to the output
66 * returns -1 if the string doesn't fit in the output buffer
67 * return the length of the string if all characters were written
69 static inline int pf_output_stringW( pf_output
*out
, LPCWSTR str
, int len
)
71 int space
= out
->len
- out
->used
;
77 LPWSTR p
= out
->buf
.W
+ out
->used
;
86 memcpy( p
, str
, len
*sizeof(WCHAR
) );
92 memcpy( p
, str
, space
*sizeof(WCHAR
) );
98 LPSTR p
= out
->buf
.A
+ out
->used
;
101 RtlUnicodeToMultiByteSize( &n
, str
, len
* sizeof(WCHAR
) );
110 RtlUnicodeToMultiByteN( p
, n
, NULL
, str
, len
* sizeof(WCHAR
) );
116 RtlUnicodeToMultiByteN( p
, space
, NULL
, str
, len
* sizeof(WCHAR
) );
123 static inline int pf_output_stringA( pf_output
*out
, LPCSTR str
, int len
)
125 int space
= out
->len
- out
->used
;
131 LPSTR p
= out
->buf
.A
+ out
->used
;
140 memcpy( p
, str
, len
);
146 memcpy( p
, str
, space
);
152 LPWSTR p
= out
->buf
.W
+ out
->used
;
155 RtlMultiByteToUnicodeSize( &n
, str
, len
);
159 out
->used
+= n
/ sizeof(WCHAR
);
162 if (space
>= n
/ sizeof(WCHAR
))
164 RtlMultiByteToUnicodeN( p
, n
, NULL
, str
, len
);
165 out
->used
+= n
/ sizeof(WCHAR
);
170 RtlMultiByteToUnicodeN( p
, space
* sizeof(WCHAR
), NULL
, str
, len
);
177 /* pf_fill: takes care of signs, alignment, zero and field padding */
178 static inline int pf_fill( pf_output
*out
, int len
, pf_flags
*flags
, char left
)
182 if( flags
->Sign
&& !( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
185 if( left
&& flags
->Sign
)
187 flags
->FieldLength
--;
189 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
192 if( ( !left
&& flags
->LeftAlign
) ||
193 ( left
&& !flags
->LeftAlign
))
195 for( i
=0; (i
<(flags
->FieldLength
-len
)) && (r
>=0); i
++ )
197 if( left
&& flags
->PadZero
)
198 r
= pf_output_stringA( out
, "0", 1 );
200 r
= pf_output_stringA( out
, " ", 1 );
204 if (left
&& flags
->Sign
&& !flags
->PadZero
&& r
>= 0)
205 r
= pf_output_stringA( out
, &flags
->Sign
, 1 );
210 static inline int pf_output_format_W( pf_output
*out
, LPCWSTR str
,
211 int len
, pf_flags
*flags
)
216 len
= strlenW( str
);
218 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
219 len
= flags
->Precision
;
221 r
= pf_fill( out
, len
, flags
, 1 );
224 r
= pf_output_stringW( out
, str
, len
);
227 r
= pf_fill( out
, len
, flags
, 0 );
232 static inline int pf_output_format_A( pf_output
*out
, LPCSTR str
,
233 int len
, pf_flags
*flags
)
240 if (flags
->Precision
>= 0 && flags
->Precision
< len
)
241 len
= flags
->Precision
;
243 r
= pf_fill( out
, len
, flags
, 1 );
246 r
= pf_output_stringA( out
, str
, len
);
249 r
= pf_fill( out
, len
, flags
, 0 );
254 static int pf_handle_string_format( pf_output
*out
, const void* str
, int len
,
255 pf_flags
*flags
, BOOL capital_letter
)
257 if(str
== NULL
) /* catch NULL pointer */
258 return pf_output_format_A( out
, "(null)", -1, flags
);
260 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
261 if(flags
->WideString
|| flags
->IntegerLength
== 'l')
262 return pf_output_format_W( out
, str
, len
, flags
);
263 if(flags
->IntegerLength
== 'h')
264 return pf_output_format_A( out
, str
, len
, flags
);
266 /* %s,%c -> chars in ansi functions & wchars in unicode
267 * %S,%C -> wchars in ansi functions & chars in unicode */
268 if( capital_letter
== out
->unicode
) /* either both TRUE or both FALSE */
269 return pf_output_format_A( out
, str
, len
, flags
);
271 return pf_output_format_W( out
, str
, len
, flags
);
274 static inline BOOL
pf_is_integer_format( char fmt
)
276 static const char float_fmts
[] = "diouxX";
279 return strchr( float_fmts
, fmt
) != 0;
282 static inline BOOL
pf_is_double_format( char fmt
)
284 static const char float_fmts
[] = "aeEfgG";
287 return strchr( float_fmts
, fmt
) != 0;
290 static inline BOOL
pf_is_valid_format( char fmt
)
292 static const char float_fmts
[] = "acCdeEfgGinouxX";
295 return strchr( float_fmts
, fmt
) != 0;
298 static void pf_rebuild_format_string( char *p
, pf_flags
*flags
)
303 if( flags
->LeftAlign
)
304 *p
++ = flags
->LeftAlign
;
305 if( flags
->Alternate
)
306 *p
++ = flags
->Alternate
;
308 *p
++ = flags
->PadZero
;
309 if( flags
->FieldLength
)
311 sprintf(p
, "%d", flags
->FieldLength
);
314 if( flags
->Precision
>= 0 )
316 sprintf(p
, ".%d", flags
->Precision
);
319 *p
++ = flags
->Format
;
323 /* pf_integer_conv: prints x to buf, including alternate formats and
324 additional precision digits, but not field characters or the sign */
325 static void pf_integer_conv( char *buf
, int buf_len
, pf_flags
*flags
,
332 char number
[40], *tmp
= number
;
334 if( buf_len
> sizeof number
)
336 if (!(tmp
= RtlAllocateHeap( GetProcessHeap(), 0, buf_len
)))
344 if( flags
->Format
== 'o' )
346 else if( flags
->Format
== 'x' || flags
->Format
== 'X' )
349 if( flags
->Format
== 'X' )
350 digits
= "0123456789ABCDEFX";
352 digits
= "0123456789abcdefx";
354 if( x
< 0 && ( flags
->Format
== 'd' || flags
->Format
== 'i' ) )
360 /* Do conversion (backwards) */
362 if( x
== 0 && flags
->Precision
)
367 j
= (ULONGLONG
) x
% base
;
368 x
= (ULONGLONG
) x
/ base
;
369 tmp
[i
++] = digits
[j
];
371 k
= flags
->Precision
- i
;
374 if( flags
->Alternate
)
378 tmp
[i
++] = digits
[16];
381 else if( base
== 8 && tmp
[i
-1] != '0' )
385 /* Reverse for buf */
391 /* Adjust precision so pf_fill won't truncate the number later */
392 flags
->Precision
= strlen( buf
);
395 RtlFreeHeap( GetProcessHeap(), 0, tmp
);
400 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
401 to 3 digits, accounting for padding, in place. Needed to match
402 the native printf's which always use 3 digits. */
403 static void pf_fixup_exponent( char *buf
)
407 while (tmp
[0] && NTDLL_tolower(tmp
[0]) != 'e')
410 if (tmp
[0] && (tmp
[1] == '+' || tmp
[1] == '-') &&
411 isdigit(tmp
[2]) && isdigit(tmp
[3]))
416 return; /* Exponent already 3 digits */
418 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
426 /* We didn't expand into trailing space, so this string isn't left
427 * justified. Terminate the string and strip a ' ' at the start of
428 * the string if there is one (as there may be if the string is
433 memmove(buf
, buf
+ 1, (tmp
- buf
) + 3);
435 /* Otherwise, we expanded into trailing space -> nothing to do */
439 static inline BOOL
isDigit(WCHAR c
)
441 return c
>= '0' && c
<= '9';
444 /*********************************************************************
445 * pf_vsnprintf (INTERNAL)
447 * implements both A and W vsnprintf functions
449 static int pf_vsnprintf( pf_output
*out
, const WCHAR
*format
, __ms_va_list valist
)
452 LPCWSTR q
, p
= format
;
455 TRACE("format is %s\n",debugstr_w(format
));
458 q
= strchrW( p
, '%' );
460 /* there are no % characters left: output the rest of the string */
463 r
= pf_output_stringW(out
, p
, -1);
470 /* there are characters before the %: output them */
473 r
= pf_output_stringW(out
, p
, q
- p
);
479 /* we must be at a % now, skip over it */
483 /* output a single % character */
486 r
= pf_output_stringW(out
, p
++, 1);
492 /* parse the flags */
493 memset( &flags
, 0, sizeof flags
);
496 if( *p
== '+' || *p
== ' ' )
498 if ( flags
.Sign
!= '+' )
502 flags
.LeftAlign
= *p
;
506 flags
.Alternate
= *p
;
512 /* deal with the field width specifier */
513 flags
.FieldLength
= 0;
516 flags
.FieldLength
= va_arg( valist
, int );
517 if (flags
.FieldLength
< 0)
519 flags
.LeftAlign
= '-';
520 flags
.FieldLength
= -flags
.FieldLength
;
524 else while( isDigit(*p
) )
526 flags
.FieldLength
*= 10;
527 flags
.FieldLength
+= *p
++ - '0';
530 /* deal with precision */
531 flags
.Precision
= -1;
538 flags
.Precision
= va_arg( valist
, int );
541 else while( isDigit(*p
) )
543 flags
.Precision
*= 10;
544 flags
.Precision
+= *p
++ - '0';
548 /* deal with integer width modifier */
551 if( *p
== 'h' || *p
== 'l' || *p
== 'L' )
553 flags
.IntegerLength
= *p
;
558 if( *(p
+1) == '6' && *(p
+2) == '4' )
560 flags
.IntegerDouble
++;
563 else if( *(p
+1) == '3' && *(p
+2) == '2' )
565 else if( isDigit(*(p
+1)) || *(p
+1) == 0 )
571 flags
.WideString
= *p
++;
581 if (flags
.Format
== '$')
583 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format
));
586 /* output a string */
587 if( flags
.Format
== 's' || flags
.Format
== 'S' )
588 r
= pf_handle_string_format( out
, va_arg(valist
, const void*), -1,
589 &flags
, (flags
.Format
== 'S') );
591 /* output a single character */
592 else if( flags
.Format
== 'c' || flags
.Format
== 'C' )
594 INT ch
= va_arg( valist
, int );
596 r
= pf_handle_string_format( out
, &ch
, 1, &flags
, (flags
.Format
== 'C') );
599 /* output a pointer */
600 else if( flags
.Format
== 'p' )
603 void *ptr
= va_arg( valist
, void * );
606 if( flags
.Alternate
)
607 sprintf(pointer
, "0X%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
609 sprintf(pointer
, "%0*lX", 2 * (int)sizeof(ptr
), (ULONG_PTR
)ptr
);
610 r
= pf_output_format_A( out
, pointer
, -1, &flags
);
614 else if( flags
.Format
== 'n' )
616 int *x
= va_arg(valist
, int *);
620 /* deal with 64-bit integers */
621 else if( pf_is_integer_format( flags
.Format
) && flags
.IntegerDouble
)
623 char number
[40], *x
= number
;
625 /* Estimate largest possible required buffer size:
626 * Chooses the larger of the field or precision
627 * Includes extra bytes: 1 byte for null, 1 byte for sign,
628 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
629 for a decimal, and 1 byte for an additional float digit. */
630 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
631 flags
.FieldLength
: flags
.Precision
) + 10;
633 if( x_len
>= sizeof number
)
634 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
637 pf_integer_conv( x
, x_len
, &flags
, va_arg(valist
, LONGLONG
) );
639 r
= pf_output_format_A( out
, x
, -1, &flags
);
641 RtlFreeHeap( GetProcessHeap(), 0, x
);
644 /* deal with integers and floats using libc's printf */
645 else if( pf_is_valid_format( flags
.Format
) )
647 char fmt
[20], number
[40], *x
= number
;
649 /* Estimate largest possible required buffer size:
650 * Chooses the larger of the field or precision
651 * Includes extra bytes: 1 byte for null, 1 byte for sign,
652 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
653 for a decimal, and 1 byte for an additional float digit. */
654 int x_len
= ((flags
.FieldLength
> flags
.Precision
) ?
655 flags
.FieldLength
: flags
.Precision
) + 10;
657 if( x_len
>= sizeof number
)
658 if (!(x
= RtlAllocateHeap( GetProcessHeap(), 0, x_len
)))
661 pf_rebuild_format_string( fmt
, &flags
);
663 if( pf_is_double_format( flags
.Format
) )
665 sprintf( x
, fmt
, va_arg(valist
, double) );
666 if (NTDLL_tolower(flags
.Format
) == 'e' || NTDLL_tolower(flags
.Format
) == 'g')
667 pf_fixup_exponent( x
);
670 sprintf( x
, fmt
, va_arg(valist
, int) );
672 r
= pf_output_stringA( out
, x
, -1 );
674 RtlFreeHeap( GetProcessHeap(), 0, x
);
684 /* check we reached the end, and null terminate the string */
690 /*********************************************************************
691 * _vsnprintf (NTDLL.@)
693 int CDECL
NTDLL__vsnprintf( char *str
, SIZE_T len
, const char *format
, __ms_va_list args
)
696 LPWSTR formatW
= NULL
;
707 RtlMultiByteToUnicodeSize( &sz
, format
, strlen(format
) + 1 );
708 if (!(formatW
= RtlAllocateHeap( GetProcessHeap(), 0, sz
))) return -1;
709 RtlMultiByteToUnicodeN( formatW
, sz
, NULL
, format
, strlen(format
) + 1 );
711 r
= pf_vsnprintf( &out
, formatW
, args
);
712 RtlFreeHeap( GetProcessHeap(), 0, formatW
);
713 if (out
.used
< len
) str
[out
.used
] = 0;
718 /***********************************************************************
719 * _vsnwprintf (NTDLL.@)
721 int CDECL
NTDLL__vsnwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, __ms_va_list args
)
731 r
= pf_vsnprintf( &out
, format
, args
);
732 if (out
.used
< len
) str
[out
.used
] = 0;
737 /*********************************************************************
738 * _snprintf (NTDLL.@)
740 int WINAPIV
NTDLL__snprintf( char *str
, SIZE_T len
, const char *format
, ... )
745 __ms_va_start( valist
, format
);
746 ret
= NTDLL__vsnprintf( str
, len
, format
, valist
);
747 __ms_va_end( valist
);
752 /***********************************************************************
753 * _snwprintf (NTDLL.@)
755 int WINAPIV
NTDLL__snwprintf( WCHAR
*str
, SIZE_T len
, const WCHAR
*format
, ... )
760 __ms_va_start(valist
, format
);
761 ret
= NTDLL__vsnwprintf( str
, len
, format
, valist
);
767 /*********************************************************************
768 * _vsnprintf_s (NTDLL.@)
770 int CDECL
_vsnprintf_s( char *str
, SIZE_T size
, SIZE_T len
, const char *format
, __ms_va_list args
)
773 LPWSTR formatW
= NULL
;
780 out
.len
= min( size
, len
);
784 RtlMultiByteToUnicodeSize( &sz
, format
, strlen(format
) + 1 );
785 if (!(formatW
= RtlAllocateHeap( GetProcessHeap(), 0, sz
))) return -1;
786 RtlMultiByteToUnicodeN( formatW
, sz
, NULL
, format
, strlen(format
) + 1 );
788 r
= pf_vsnprintf( &out
, formatW
, args
);
789 RtlFreeHeap( GetProcessHeap(), 0, formatW
);
790 if (out
.used
< size
) str
[out
.used
] = 0;
792 if (r
== size
) r
= -1;
797 /*********************************************************************
798 * _snprintf_s (NTDLL.@)
800 int WINAPIV
_snprintf_s( char *str
, SIZE_T size
, SIZE_T len
, const char *format
, ... )
805 __ms_va_start( valist
, format
);
806 ret
= _vsnprintf_s( str
, size
, len
, format
, valist
);
807 __ms_va_end( valist
);
812 /*********************************************************************
815 int CDECL
NTDLL_vsprintf( char *str
, const char *format
, __ms_va_list args
)
817 return NTDLL__vsnprintf( str
, size_max
, format
, args
);
821 /*********************************************************************
824 int WINAPIV
NTDLL_sprintf( char *str
, const char *format
, ... )
829 __ms_va_start( valist
, format
);
830 ret
= NTDLL__vsnprintf( str
, size_max
, format
, valist
);
831 __ms_va_end( valist
);
836 /***********************************************************************
839 int WINAPIV
NTDLL_swprintf( WCHAR
*str
, const WCHAR
*format
, ... )
844 __ms_va_start(valist
, format
);
845 ret
= NTDLL__vsnwprintf( str
, size_max
, format
, valist
);