1 /*
2 * ntdll printf functions
3 *
4 * Copyright 1999, 2009 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "windef.h"
33 #include "winternl.h"
34 #include "ntdll_misc.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39
40 static const SIZE_T size_max = ~(SIZE_T)0 >> 1;
41
42 /* FIXME: convert sizes to SIZE_T etc. */
43
44 typedef struct pf_output_t
45 {
46 int used;
47 int len;
48 BOOL unicode;
49 union {
50 LPWSTR W;
51 LPSTR A;
52 } buf;
53 } pf_output;
54
55 typedef struct pf_flags_t
56 {
57 char Sign, LeftAlign, Alternate, PadZero;
58 int FieldLength, Precision;
59 char IntegerLength, IntegerDouble;
60 char WideString;
61 char Format;
62 } pf_flags;
63
64 /*
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
68 */
69 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
70 {
71 int space = out->len - out->used;
72
73 if( len < 0 )
74 len = strlenW( str );
75 if( out->unicode )
76 {
77 LPWSTR p = out->buf.W + out->used;
78
79 if( space >= len )
80 {
81 memcpy( p, str, len*sizeof(WCHAR) );
82 out->used += len;
83 return len;
84 }
85 if( space > 0 )
86 memcpy( p, str, space*sizeof(WCHAR) );
87 out->used += len;
88 }
89 else
90 {
91 LPSTR p = out->buf.A + out->used;
92 ULONG n;
93
94 RtlUnicodeToMultiByteSize( &n, str, len * sizeof(WCHAR) );
95 if( space >= n )
96 {
97 RtlUnicodeToMultiByteN( p, n, NULL, str, len * sizeof(WCHAR) );
98 out->used += n;
99 return len;
100 }
101 if (space > 0) RtlUnicodeToMultiByteN( p, space, NULL, str, len * sizeof(WCHAR) );
102 out->used += n;
103 }
104 return -1;
105 }
106
107 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
108 {
109 int space = out->len - out->used;
110
111 if( len < 0 )
112 len = strlen( str );
113 if( !out->unicode )
114 {
115 LPSTR p = out->buf.A + out->used;
116
117 if( space >= len )
118 {
119 memcpy( p, str, len );
120 out->used += len;
121 return len;
122 }
123 if( space > 0 )
124 memcpy( p, str, space );
125 out->used += len;
126 }
127 else
128 {
129 LPWSTR p = out->buf.W + out->used;
130 ULONG n;
131
132 RtlMultiByteToUnicodeSize( &n, str, len );
133 if (space >= n / sizeof(WCHAR))
134 {
135 RtlMultiByteToUnicodeN( p, n, NULL, str, len );
136 out->used += n / sizeof(WCHAR);
137 return len;
138 }
139 if (space > 0) RtlMultiByteToUnicodeN( p, space * sizeof(WCHAR), NULL, str, len );
140 out->used += n;
141 }
142 return -1;
143 }
144
145 /* pf_fill: takes care of signs, alignment, zero and field padding */
146 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
147 {
148 int i, r = 0;
149
150 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
151 flags->Sign = 0;
152
153 if( left && flags->Sign )
154 {
155 flags->FieldLength--;
156 if( flags->PadZero )
157 r = pf_output_stringA( out, &flags->Sign, 1 );
158 }
159
160 if( ( !left && flags->LeftAlign ) ||
161 ( left && !flags->LeftAlign ))
162 {
163 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
164 {
165 if( left && flags->PadZero )
166 r = pf_output_stringA( out, "", 1 );
167 else
168 r = pf_output_stringA( out, " ", 1 );
169 }
170 }
171
172 if( left && flags->Sign && !flags->PadZero )
173 r = pf_output_stringA( out, &flags->Sign, 1 );
174
175 return r;
176 }
177
178 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
179 int len, pf_flags *flags )
180 {
181 int r = 0;
182
183 if( len < 0 )
184 len = strlenW( str );
185
186 if (flags->Precision >= 0 && flags->Precision < len)
187 len = flags->Precision;
188
189 r = pf_fill( out, len, flags, 1 );
190
191 if( r>=0 )
192 r = pf_output_stringW( out, str, len );
193
194 if( r>=0 )
195 r = pf_fill( out, len, flags, 0 );
196
197 return r;
198 }
199
200 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
201 int len, pf_flags *flags )
202 {
203 int r = 0;
204
205 if( len < 0 )
206 len = strlen( str );
207
208 if (flags->Precision >= 0 && flags->Precision < len)
209 len = flags->Precision;
210
211 r = pf_fill( out, len, flags, 1 );
212
213 if( r>=0 )
214 r = pf_output_stringA( out, str, len );
215
216 if( r>=0 )
217 r = pf_fill( out, len, flags, 0 );
218
219 return r;
220 }
221
222 static int pf_handle_string_format( pf_output *out, const void* str, int len,
223 pf_flags *flags, BOOL capital_letter)
224 {
225 if(str == NULL) /* catch NULL pointer */
226 return pf_output_format_A( out, "(null)", -1, flags);
227
228 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
229 if(flags->WideString || flags->IntegerLength == 'l')
230 return pf_output_format_W( out, str, len, flags);
231 if(flags->IntegerLength == 'h')
232 return pf_output_format_A( out, str, len, flags);
233
234 /* %s,%c -> chars in ansi functions & wchars in unicode
235 * %S,%C -> wchars in ansi functions & chars in unicode */
236 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
237 return pf_output_format_A( out, str, len, flags);
238 else
239 return pf_output_format_W( out, str, len, flags);
240 }
241
242 static inline BOOL pf_is_integer_format( char fmt )
243 {
244 static const char float_fmts[] = "diouxX";
245 if (!fmt)
246 return FALSE;
247 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
248 }
249
250 static inline BOOL pf_is_double_format( char fmt )
251 {
252 static const char float_fmts[] = "aeEfgG";
253 if (!fmt)
254 return FALSE;
255 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
256 }
257
258 static inline BOOL pf_is_valid_format( char fmt )
259 {
260 static const char float_fmts[] = "acCdeEfgGinouxX";
261 if (!fmt)
262 return FALSE;
263 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
264 }
265
266 static void pf_rebuild_format_string( char *p, pf_flags *flags )
267 {
268 *p++ = '%';
269 if( flags->Sign )
270 *p++ = flags->Sign;
271 if( flags->LeftAlign )
272 *p++ = flags->LeftAlign;
273 if( flags->Alternate )
274 *p++ = flags->Alternate;
275 if( flags->PadZero )
276 *p++ = flags->PadZero;
277 if( flags->FieldLength )
278 {
279 sprintf(p, "%d", flags->FieldLength);
280 p += strlen(p);
281 }
282 if( flags->Precision >= 0 )
283 {
284 sprintf(p, ".%d", flags->Precision);
285 p += strlen(p);
286 }
287 *p++ = flags->Format;
288 *p++ = 0;
289 }
290
291 /* pf_integer_conv: prints x to buf, including alternate formats and
292 additional precision digits, but not field characters or the sign */
293 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
294 LONGLONG x )
295 {
296 unsigned int base;
297 const char *digits;
298
299 int i, j, k;
300 char number[40], *tmp = number;
301
302 if( buf_len > sizeof number )
303 tmp = RtlAllocateHeap( GetProcessHeap(), 0, buf_len );
304
305 base = 10;
306 if( flags->Format == 'o' )
307 base = 8;
308 else if( flags->Format == 'x' || flags->Format == 'X' )
309 base = 16;
310
311 if( flags->Format == 'X' )
312 digits = "0123456789ABCDEFX";
313 else
314 digits = "0123456789abcdefx";
315
316 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
317 {
318 x = -x;
319 flags->Sign = '-';
320 }
321
322 /* Do conversion (backwards) */
323 i = 0;
324 if( x == 0 && flags->Precision )
325 tmp[i++] = '';
326 else
327 while( x != 0 )
328 {
329 j = (ULONGLONG) x % base;
330 x = (ULONGLONG) x / base;
331 tmp[i++] = digits[j];
332 }
333 k = flags->Precision - i;
334 while( k-- > 0 )
335 tmp[i++] = '';
336 if( flags->Alternate )
337 {
338 if( base == 16 )
339 {
340 tmp[i++] = digits[16];
341 tmp[i++] = '';
342 }
343 else if( base == 8 && tmp[i-1] != '' )
344 tmp[i++] = '';
345 }
346
347 /* Reverse for buf */
348 j = 0;
349 while( i-- > 0 )
350 buf[j++] = tmp[i];
351 buf[j] = '\0';
352
353 /* Adjust precision so pf_fill won't truncate the number later */
354 flags->Precision = strlen( buf );
355
356 if( tmp != number )
357 RtlFreeHeap( GetProcessHeap(), 0, tmp );
358
359 return;
360 }
361
362 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
363 to 3 digits, accounting for padding, in place. Needed to match
364 the native printf's which always use 3 digits. */
365 static void pf_fixup_exponent( char *buf )
366 {
367 char* tmp = buf;
368
369 while (tmp[0] && toupper(tmp[0]) != 'E')
370 tmp++;
371
372 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
373 isdigit(tmp[2]) && isdigit(tmp[3]))
374 {
375 char final;
376
377 if (isdigit(tmp[4]))
378 return; /* Exponent already 3 digits */
379
380 /* We have a 2 digit exponent. Prepend '' to make it 3 */
381 tmp += 2;
382 final = tmp[2];
383 tmp[2] = tmp[1];
384 tmp[1] = tmp[0];
385 tmp[0] = '';
386 if (final == '\0')
387 {
388 /* We didn't expand into trailing space, so this string isn't left
389 * justified. Terminate the string and strip a ' ' at the start of
390 * the string if there is one (as there may be if the string is
391 * right justified).
392 */
393 tmp[3] = '\0';
394 if (buf[0] == ' ')
395 memmove(buf, buf + 1, (tmp - buf) + 3);
396 }
397 /* Otherwise, we expanded into trailing space -> nothing to do */
398 }
399 }
400
401 /*********************************************************************
402 * pf_vsnprintf (INTERNAL)
403 *
404 * implements both A and W vsnprintf functions
405 */
406 static int pf_vsnprintf( pf_output *out, const WCHAR *format, __ms_va_list valist )
407 {
408 int r;
409 LPCWSTR q, p = format;
410 pf_flags flags;
411
412 TRACE("format is %s\n",debugstr_w(format));
413 while (*p)
414 {
415 q = strchrW( p, '%' );
416
417 /* there's no % characters left, output the rest of the string */
418 if( !q )
419 {
420 r = pf_output_stringW(out, p, -1);
421 if( r<0 )
422 return r;
423 p += r;
424 continue;
425 }
426
427 /* there's characters before the %, output them */
428 if( q != p )
429 {
430 r = pf_output_stringW(out, p, q - p);
431 if( r<0 )
432 return r;
433 p = q;
434 }
435
436 /* we must be at a % now, skip over it */
437 assert( *p == '%' );
438 p++;
439
440 /* output a single % character */
441 if( *p == '%' )
442 {
443 r = pf_output_stringW(out, p++, 1);
444 if( r<0 )
445 return r;
446 continue;
447 }
448
449 /* parse the flags */
450 memset( &flags, 0, sizeof flags );
451 while (*p)
452 {
453 if( *p == '+' || *p == ' ' )
454 {
455 if ( flags.Sign != '+' )
456 flags.Sign = *p;
457 }
458 else if( *p == '-' )
459 flags.LeftAlign = *p;
460 else if( *p == '' )
461 flags.PadZero = *p;
462 else if( *p == '#' )
463 flags.Alternate = *p;
464 else
465 break;
466 p++;
467 }
468
469 /* deal with the field width specifier */
470 flags.FieldLength = 0;
471 if( *p == '*' )
472 {
473 flags.FieldLength = va_arg( valist, int );
474 if (flags.FieldLength < 0)
475 {
476 flags.LeftAlign = '-';
477 flags.FieldLength = -flags.FieldLength;
478 }
479 p++;
480 }
481 else while( isdigit(*p) )
482 {
483 flags.FieldLength *= 10;
484 flags.FieldLength += *p++ - '';
485 }
486
487 /* deal with precision */
488 flags.Precision = -1;
489 if( *p == '.' )
490 {
491 flags.Precision = 0;
492 p++;
493 if( *p == '*' )
494 {
495 flags.Precision = va_arg( valist, int );
496 p++;
497 }
498 else while( isdigit(*p) )
499 {
500 flags.Precision *= 10;
501 flags.Precision += *p++ - '';
502 }
503 }
504
505 /* deal with integer width modifier */
506 while( *p )
507 {
508 if( *p == 'h' || *p == 'l' || *p == 'L' )
509 {
510 flags.IntegerLength = *p;
511 p++;
512 }
513 else if( *p == 'I' )
514 {
515 if( *(p+1) == '6' && *(p+2) == '4' )
516 {
517 flags.IntegerDouble++;
518 p += 3;
519 }
520 else if( *(p+1) == '3' && *(p+2) == '2' )
521 p += 3;
522 else if( isdigit(*(p+1)) || *(p+1) == 0 )
523 break;
524 else
525 p++;
526 }
527 else if( *p == 'w' )
528 flags.WideString = *p++;
529 else if( *p == 'F' )
530 p++; /* ignore */
531 else
532 break;
533 }
534
535 flags.Format = *p;
536 r = 0;
537
538 if (flags.Format == '$')
539 {
540 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
541 return -1;
542 }
543 /* output a string */
544 if( flags.Format == 's' || flags.Format == 'S' )
545 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
546 &flags, (flags.Format == 'S') );
547
548 /* output a single character */
549 else if( flags.Format == 'c' || flags.Format == 'C' )
550 {
551 INT ch = va_arg( valist, int );
552
553 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
554 }
555
556 /* output a pointer */
557 else if( flags.Format == 'p' )
558 {
559 char pointer[32];
560 void *ptr = va_arg( valist, void * );
561
562 flags.PadZero = 0;
563 if( flags.Alternate )
564 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
565 else
566 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
567 r = pf_output_format_A( out, pointer, -1, &flags );
568 }
569
570 /* deal with %n */
571 else if( flags.Format == 'n' )
572 {
573 int *x = va_arg(valist, int *);
574 *x = out->used;
575 }
576
577 /* deal with 64-bit integers */
578 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
579 {
580 char number[40], *x = number;
581
582 /* Estimate largest possible required buffer size:
583 * Chooses the larger of the field or precision
584 * Includes extra bytes: 1 byte for null, 1 byte for sign,
585 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
586 for a decimal, and 1 byte for an additional float digit. */
587 int x_len = ((flags.FieldLength > flags.Precision) ?
588 flags.FieldLength : flags.Precision) + 10;
589
590 if( x_len >= sizeof number)
591 x = RtlAllocateHeap( GetProcessHeap(), 0, x_len );
592
593 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
594
595 r = pf_output_format_A( out, x, -1, &flags );
596 if( x != number )
597 RtlFreeHeap( GetProcessHeap(), 0, x );
598 }
599
600 /* deal with integers and floats using libc's printf */
601 else if( pf_is_valid_format( flags.Format ) )
602 {
603 char fmt[20], number[40], *x = number;
604
605 /* Estimate largest possible required buffer size:
606 * Chooses the larger of the field or precision
607 * Includes extra bytes: 1 byte for null, 1 byte for sign,
608 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
609 for a decimal, and 1 byte for an additional float digit. */
610 int x_len = ((flags.FieldLength > flags.Precision) ?
611 flags.FieldLength : flags.Precision) + 10;
612
613 if( x_len >= sizeof number)
614 x = RtlAllocateHeap( GetProcessHeap(), 0, x_len );
615
616 pf_rebuild_format_string( fmt, &flags );
617
618 if( pf_is_double_format( flags.Format ) )
619 {
620 sprintf( x, fmt, va_arg(valist, double) );
621 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
622 pf_fixup_exponent( x );
623 }
624 else
625 sprintf( x, fmt, va_arg(valist, int) );
626
627 r = pf_output_stringA( out, x, -1 );
628 if( x != number )
629 RtlFreeHeap( GetProcessHeap(), 0, x );
630 }
631 else
632 continue;
633
634 if( r<0 )
635 return r;
636 p++;
637 }
638
639 /* check we reached the end, and null terminate the string */
640 assert( *p == 0 );
641 pf_output_stringW( out, p, 1 );
642
643 return out->used - 1;
644 }
645
646
647 /*********************************************************************
648 * _vsnprintf (NTDLL.@)
649 */
650 int CDECL NTDLL__vsnprintf( char *str, SIZE_T len, const char *format, __ms_va_list args )
651 {
652 DWORD sz;
653 LPWSTR formatW = NULL;
654 pf_output out;
655 int r;
656
657 out.unicode = FALSE;
658 out.buf.A = str;
659 out.used = 0;
660 out.len = len;
661
662 if (format)
663 {
664 RtlMultiByteToUnicodeSize( &sz, format, strlen(format) + 1 );
665 if (!(formatW = RtlAllocateHeap( GetProcessHeap(), 0, sz ))) return -1;
666 RtlMultiByteToUnicodeN( formatW, sz, NULL, format, strlen(format) + 1 );
667 }
668 r = pf_vsnprintf( &out, formatW, args );
669 RtlFreeHeap( GetProcessHeap(), 0, formatW );
670 return r;
671 }
672
673
674 /***********************************************************************
675 * _vsnwprintf (NTDLL.@)
676 */
677 int CDECL NTDLL__vsnwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, __ms_va_list args )
678 {
679 pf_output out;
680
681 out.unicode = TRUE;
682 out.buf.W = str;
683 out.used = 0;
684 out.len = len;
685
686 return pf_vsnprintf( &out, format, args );
687 }
688
689
690 /*********************************************************************
691 * _snprintf (NTDLL.@)
692 */
693 int CDECL NTDLL__snprintf( char *str, SIZE_T len, const char *format, ... )
694 {
695 int ret;
696 __ms_va_list valist;
697
698 __ms_va_start( valist, format );
699 ret = NTDLL__vsnprintf( str, len, format, valist );
700 __ms_va_end( valist );
701 return ret;
702 }
703
704
705 /***********************************************************************
706 * _snwprintf (NTDLL.@)
707 */
708 int CDECL NTDLL__snwprintf( WCHAR *str, SIZE_T len, const WCHAR *format, ... )
709 {
710 int ret;
711 __ms_va_list valist;
712
713 __ms_va_start(valist, format);
714 ret = NTDLL__vsnwprintf( str, len, format, valist );
715 __ms_va_end(valist);
716 return ret;
717 }
718
719
720 /*********************************************************************
721 * vsprintf (NTDLL.@)
722 */
723 int CDECL NTDLL_vsprintf( char *str, const char *format, __ms_va_list args )
724 {
725 return NTDLL__vsnprintf( str, size_max, format, args );
726 }
727
728
729 /*********************************************************************
730 * sprintf (NTDLL.@)
731 */
732 int CDECL NTDLL_sprintf( char *str, const char *format, ... )
733 {
734 int ret;
735 __ms_va_list valist;
736
737 __ms_va_start( valist, format );
738 ret = NTDLL__vsnprintf( str, size_max, format, valist );
739 __ms_va_end( valist );
740 return ret;
741 }
742
743
744 /***********************************************************************
745 * swprintf (NTDLL.@)
746 */
747 int CDECL NTDLL_swprintf( WCHAR *str, const WCHAR *format, ... )
748 {
749 int ret;
750 __ms_va_list valist;
751
752 __ms_va_start(valist, format);
753 ret = NTDLL__vsnwprintf( str, size_max, format, valist );
754 __ms_va_end(valist);
755 return ret;
756 }
757
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.