1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if defined(LIBC_SCCS) && !defined(lint) 34 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 35 #endif /* LIBC_SCCS and not lint */ 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * This is the code responsible for handling positional arguments 41 * (%m$ and %m$.n$) for vfprintf() and vfwprintf(). 42 */ 43 44 #include "namespace.h" 45 #include <sys/types.h> 46 47 #include <limits.h> 48 #include <stdarg.h> 49 #include <stddef.h> 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <wchar.h> 55 56 #include "un-namespace.h" 57 #include "printflocal.h" 58 59 #ifdef NL_ARGMAX 60 #define MAX_POSARG NL_ARGMAX 61 #else 62 #define MAX_POSARG 65536 63 #endif 64 65 /* 66 * Type ids for argument type table. 67 */ 68 enum typeid { 69 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT, 70 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 71 T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SSIZET, 72 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 73 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 74 }; 75 76 /* An expandable array of types. */ 77 struct typetable { 78 enum typeid *table; /* table of types */ 79 enum typeid stattable[STATIC_ARG_TBL_SIZE]; 80 u_int tablesize; /* current size of type table */ 81 u_int tablemax; /* largest used index in table */ 82 u_int nextarg; /* 1-based argument index */ 83 }; 84 85 static int __grow_type_table(struct typetable *); 86 static void build_arg_table (struct typetable *, va_list, union arg **); 87 88 /* 89 * Initialize a struct typetable. 90 */ 91 static inline void 92 inittypes(struct typetable *types) 93 { 94 u_int n; 95 96 types->table = types->stattable; 97 types->tablesize = STATIC_ARG_TBL_SIZE; 98 types->tablemax = 0; 99 types->nextarg = 1; 100 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++) 101 types->table[n] = T_UNUSED; 102 } 103 104 /* 105 * struct typetable destructor. 106 */ 107 static inline void 108 freetypes(struct typetable *types) 109 { 110 111 if (types->table != types->stattable) 112 free (types->table); 113 } 114 115 /* 116 * Ensure that there is space to add a new argument type to the type table. 117 * Expand the table if necessary. Returns 0 on success. 118 */ 119 static inline int 120 _ensurespace(struct typetable *types) 121 { 122 123 if (types->nextarg >= types->tablesize) { 124 if (__grow_type_table(types)) 125 return (-1); 126 } 127 if (types->nextarg > types->tablemax) 128 types->tablemax = types->nextarg; 129 return (0); 130 } 131 132 /* 133 * Add an argument type to the table, expanding if necessary. 134 * Returns 0 on success. 135 */ 136 static inline int 137 addtype(struct typetable *types, enum typeid type) 138 { 139 140 if (_ensurespace(types)) 141 return (-1); 142 types->table[types->nextarg++] = type; 143 return (0); 144 } 145 146 static inline int 147 addsarg(struct typetable *types, int flags) 148 { 149 150 if (_ensurespace(types)) 151 return (-1); 152 if (flags & INTMAXT) 153 types->table[types->nextarg++] = T_INTMAXT; 154 else if (flags & SIZET) 155 types->table[types->nextarg++] = T_SSIZET; 156 else if (flags & PTRDIFFT) 157 types->table[types->nextarg++] = T_PTRDIFFT; 158 else if (flags & LLONGINT) 159 types->table[types->nextarg++] = T_LLONG; 160 else if (flags & LONGINT) 161 types->table[types->nextarg++] = T_LONG; 162 else 163 types->table[types->nextarg++] = T_INT; 164 return (0); 165 } 166 167 static inline int 168 adduarg(struct typetable *types, int flags) 169 { 170 171 if (_ensurespace(types)) 172 return (-1); 173 if (flags & INTMAXT) 174 types->table[types->nextarg++] = T_UINTMAXT; 175 else if (flags & SIZET) 176 types->table[types->nextarg++] = T_SIZET; 177 else if (flags & PTRDIFFT) 178 types->table[types->nextarg++] = T_SIZET; 179 else if (flags & LLONGINT) 180 types->table[types->nextarg++] = T_U_LLONG; 181 else if (flags & LONGINT) 182 types->table[types->nextarg++] = T_U_LONG; 183 else 184 types->table[types->nextarg++] = T_U_INT; 185 return (0); 186 } 187 188 /* 189 * Add * arguments to the type array. 190 */ 191 static inline int 192 addaster(struct typetable *types, char **fmtp) 193 { 194 char *cp; 195 u_int n2; 196 197 n2 = 0; 198 cp = *fmtp; 199 while (is_digit(*cp)) { 200 n2 = 10 * n2 + to_digit(*cp); 201 cp++; 202 } 203 if (*cp == '$') { 204 u_int hold = types->nextarg; 205 types->nextarg = n2; 206 if (addtype(types, T_INT)) 207 return (-1); 208 types->nextarg = hold; 209 *fmtp = ++cp; 210 } else { 211 if (addtype(types, T_INT)) 212 return (-1); 213 } 214 return (0); 215 } 216 217 static inline int 218 addwaster(struct typetable *types, wchar_t **fmtp) 219 { 220 wchar_t *cp; 221 u_int n2; 222 223 n2 = 0; 224 cp = *fmtp; 225 while (is_digit(*cp)) { 226 n2 = 10 * n2 + to_digit(*cp); 227 cp++; 228 } 229 if (*cp == '$') { 230 u_int hold = types->nextarg; 231 types->nextarg = n2; 232 if (addtype(types, T_INT)) 233 return (-1); 234 types->nextarg = hold; 235 *fmtp = ++cp; 236 } else { 237 if (addtype(types, T_INT)) 238 return (-1); 239 } 240 return (0); 241 } 242 243 /* 244 * Find all arguments when a positional parameter is encountered. Returns a 245 * table, indexed by argument number, of pointers to each arguments. The 246 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 247 * It will be replaces with a malloc-ed one if it overflows. 248 * Returns 0 on success. On failure, returns nonzero and sets errno. 249 */ 250 int 251 __find_arguments (const char *fmt0, va_list ap, union arg **argtable) 252 { 253 char *fmt; /* format string */ 254 int ch; /* character from fmt */ 255 u_int n; /* handy integer (short term usage) */ 256 int error; 257 int flags; /* flags as above */ 258 struct typetable types; /* table of types */ 259 260 fmt = (char *)fmt0; 261 inittypes(&types); 262 error = 0; 263 264 /* 265 * Scan the format for conversions (`%' character). 266 */ 267 for (;;) { 268 while ((ch = *fmt) != '\0' && ch != '%') 269 fmt++; 270 if (ch == '\0') 271 goto done; 272 fmt++; /* skip over '%' */ 273 274 flags = 0; 275 276 rflag: ch = *fmt++; 277 reswitch: switch (ch) { 278 case ' ': 279 case '#': 280 goto rflag; 281 case '*': 282 if ((error = addaster(&types, &fmt))) 283 goto error; 284 goto rflag; 285 case '-': 286 case '+': 287 case '\'': 288 goto rflag; 289 case '.': 290 if ((ch = *fmt++) == '*') { 291 if ((error = addaster(&types, &fmt))) 292 goto error; 293 goto rflag; 294 } 295 while (is_digit(ch)) { 296 ch = *fmt++; 297 } 298 goto reswitch; 299 case '0': 300 goto rflag; 301 case '1': case '2': case '3': case '4': 302 case '5': case '6': case '7': case '8': case '9': 303 n = 0; 304 do { 305 n = 10 * n + to_digit(ch); 306 /* Detect overflow */ 307 if (n > MAX_POSARG) { 308 error = -1; 309 goto error; 310 } 311 ch = *fmt++; 312 } while (is_digit(ch)); 313 if (ch == '$') { 314 types.nextarg = n; 315 goto rflag; 316 } 317 goto reswitch; 318 #ifndef NO_FLOATING_POINT 319 case 'L': 320 flags |= LONGDBL; 321 goto rflag; 322 #endif 323 case 'h': 324 if (flags & SHORTINT) { 325 flags &= ~SHORTINT; 326 flags |= CHARINT; 327 } else 328 flags |= SHORTINT; 329 goto rflag; 330 case 'j': 331 flags |= INTMAXT; 332 goto rflag; 333 case 'l': 334 if (flags & LONGINT) { 335 flags &= ~LONGINT; 336 flags |= LLONGINT; 337 } else 338 flags |= LONGINT; 339 goto rflag; 340 case 'q': 341 flags |= LLONGINT; /* not necessarily */ 342 goto rflag; 343 case 't': 344 flags |= PTRDIFFT; 345 goto rflag; 346 case 'z': 347 flags |= SIZET; 348 goto rflag; 349 case 'C': 350 flags |= LONGINT; 351 /*FALLTHROUGH*/ 352 case 'c': 353 error = addtype(&types, 354 (flags & LONGINT) ? T_WINT : T_INT); 355 if (error) 356 goto error; 357 break; 358 case 'D': 359 flags |= LONGINT; 360 /*FALLTHROUGH*/ 361 case 'd': 362 case 'i': 363 if ((error = addsarg(&types, flags))) 364 goto error; 365 break; 366 #ifndef NO_FLOATING_POINT 367 case 'a': 368 case 'A': 369 case 'e': 370 case 'E': 371 case 'f': 372 case 'g': 373 case 'G': 374 error = addtype(&types, 375 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE); 376 if (error) 377 goto error; 378 break; 379 #endif /* !NO_FLOATING_POINT */ 380 case 'n': 381 if (flags & INTMAXT) 382 error = addtype(&types, TP_INTMAXT); 383 else if (flags & PTRDIFFT) 384 error = addtype(&types, TP_PTRDIFFT); 385 else if (flags & SIZET) 386 error = addtype(&types, TP_SSIZET); 387 else if (flags & LLONGINT) 388 error = addtype(&types, TP_LLONG); 389 else if (flags & LONGINT) 390 error = addtype(&types, TP_LONG); 391 else if (flags & SHORTINT) 392 error = addtype(&types, TP_SHORT); 393 else if (flags & CHARINT) 394 error = addtype(&types, TP_SCHAR); 395 else 396 error = addtype(&types, TP_INT); 397 if (error) 398 goto error; 399 continue; /* no output */ 400 case 'O': 401 flags |= LONGINT; 402 /*FALLTHROUGH*/ 403 case 'o': 404 if ((error = adduarg(&types, flags))) 405 goto error; 406 break; 407 case 'p': 408 if ((error = addtype(&types, TP_VOID))) 409 goto error; 410 break; 411 case 'S': 412 flags |= LONGINT; 413 /*FALLTHROUGH*/ 414 case 's': 415 error = addtype(&types, 416 (flags & LONGINT) ? TP_WCHAR : TP_CHAR); 417 if (error) 418 goto error; 419 break; 420 case 'U': 421 flags |= LONGINT; 422 /*FALLTHROUGH*/ 423 case 'u': 424 case 'X': 425 case 'x': 426 if ((error = adduarg(&types, flags))) 427 goto error; 428 break; 429 default: /* "%?" prints ?, unless ? is NUL */ 430 if (ch == '\0') 431 goto done; 432 break; 433 } 434 } 435 done: 436 build_arg_table(&types, ap, argtable); 437 error: 438 freetypes(&types); 439 return (error || *argtable == NULL); 440 } 441 442 /* wchar version of __find_arguments. */ 443 int 444 __find_warguments (const wchar_t *fmt0, va_list ap, union arg **argtable) 445 { 446 wchar_t *fmt; /* format string */ 447 wchar_t ch; /* character from fmt */ 448 u_int n; /* handy integer (short term usage) */ 449 int error; 450 int flags; /* flags as above */ 451 struct typetable types; /* table of types */ 452 453 fmt = (wchar_t *)fmt0; 454 inittypes(&types); 455 error = 0; 456 457 /* 458 * Scan the format for conversions (`%' character). 459 */ 460 for (;;) { 461 while ((ch = *fmt) != '\0' && ch != '%') 462 fmt++; 463 if (ch == '\0') 464 goto done; 465 fmt++; /* skip over '%' */ 466 467 flags = 0; 468 469 rflag: ch = *fmt++; 470 reswitch: switch (ch) { 471 case ' ': 472 case '#': 473 goto rflag; 474 case '*': 475 if ((error = addwaster(&types, &fmt))) 476 goto error; 477 goto rflag; 478 case '-': 479 case '+': 480 case '\'': 481 goto rflag; 482 case '.': 483 if ((ch = *fmt++) == '*') { 484 if ((error = addwaster(&types, &fmt))) 485 goto error; 486 goto rflag; 487 } 488 while (is_digit(ch)) { 489 ch = *fmt++; 490 } 491 goto reswitch; 492 case '0': 493 goto rflag; 494 case '1': case '2': case '3': case '4': 495 case '5': case '6': case '7': case '8': case '9': 496 n = 0; 497 do { 498 n = 10 * n + to_digit(ch); 499 /* Detect overflow */ 500 if (n > MAX_POSARG) { 501 error = -1; 502 goto error; 503 } 504 ch = *fmt++; 505 } while (is_digit(ch)); 506 if (ch == '$') { 507 types.nextarg = n; 508 goto rflag; 509 } 510 goto reswitch; 511 #ifndef NO_FLOATING_POINT 512 case 'L': 513 flags |= LONGDBL; 514 goto rflag; 515 #endif 516 case 'h': 517 if (flags & SHORTINT) { 518 flags &= ~SHORTINT; 519 flags |= CHARINT; 520 } else 521 flags |= SHORTINT; 522 goto rflag; 523 case 'j': 524 flags |= INTMAXT; 525 goto rflag; 526 case 'l': 527 if (flags & LONGINT) { 528 flags &= ~LONGINT; 529 flags |= LLONGINT; 530 } else 531 flags |= LONGINT; 532 goto rflag; 533 case 'q': 534 flags |= LLONGINT; /* not necessarily */ 535 goto rflag; 536 case 't': 537 flags |= PTRDIFFT; 538 goto rflag; 539 case 'z': 540 flags |= SIZET; 541 goto rflag; 542 case 'C': 543 flags |= LONGINT; 544 /*FALLTHROUGH*/ 545 case 'c': 546 error = addtype(&types, 547 (flags & LONGINT) ? T_WINT : T_INT); 548 if (error) 549 goto error; 550 break; 551 case 'D': 552 flags |= LONGINT; 553 /*FALLTHROUGH*/ 554 case 'd': 555 case 'i': 556 if ((error = addsarg(&types, flags))) 557 goto error; 558 break; 559 #ifndef NO_FLOATING_POINT 560 case 'a': 561 case 'A': 562 case 'e': 563 case 'E': 564 case 'f': 565 case 'g': 566 case 'G': 567 error = addtype(&types, 568 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE); 569 if (error) 570 goto error; 571 break; 572 #endif /* !NO_FLOATING_POINT */ 573 case 'n': 574 if (flags & INTMAXT) 575 error = addtype(&types, TP_INTMAXT); 576 else if (flags & PTRDIFFT) 577 error = addtype(&types, TP_PTRDIFFT); 578 else if (flags & SIZET) 579 error = addtype(&types, TP_SSIZET); 580 else if (flags & LLONGINT) 581 error = addtype(&types, TP_LLONG); 582 else if (flags & LONGINT) 583 error = addtype(&types, TP_LONG); 584 else if (flags & SHORTINT) 585 error = addtype(&types, TP_SHORT); 586 else if (flags & CHARINT) 587 error = addtype(&types, TP_SCHAR); 588 else 589 error = addtype(&types, TP_INT); 590 if (error) 591 goto error; 592 continue; /* no output */ 593 case 'O': 594 flags |= LONGINT; 595 /*FALLTHROUGH*/ 596 case 'o': 597 if ((error = adduarg(&types, flags))) 598 goto error; 599 break; 600 case 'p': 601 if ((error = addtype(&types, TP_VOID))) 602 goto error; 603 break; 604 case 'S': 605 flags |= LONGINT; 606 /*FALLTHROUGH*/ 607 case 's': 608 error = addtype(&types, 609 (flags & LONGINT) ? TP_WCHAR : TP_CHAR); 610 if (error) 611 goto error; 612 break; 613 case 'U': 614 flags |= LONGINT; 615 /*FALLTHROUGH*/ 616 case 'u': 617 case 'X': 618 case 'x': 619 if ((error = adduarg(&types, flags))) 620 goto error; 621 break; 622 default: /* "%?" prints ?, unless ? is NUL */ 623 if (ch == '\0') 624 goto done; 625 break; 626 } 627 } 628 done: 629 build_arg_table(&types, ap, argtable); 630 error: 631 freetypes(&types); 632 return (error || *argtable == NULL); 633 } 634 635 /* 636 * Increase the size of the type table. Returns 0 on success. 637 */ 638 static int 639 __grow_type_table(struct typetable *types) 640 { 641 enum typeid *const oldtable = types->table; 642 const int oldsize = types->tablesize; 643 enum typeid *newtable; 644 u_int n, newsize; 645 646 /* Detect overflow */ 647 if (types->nextarg > NL_ARGMAX) 648 return (-1); 649 650 newsize = oldsize * 2; 651 if (newsize < types->nextarg + 1) 652 newsize = types->nextarg + 1; 653 if (oldsize == STATIC_ARG_TBL_SIZE) { 654 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL) 655 return (-1); 656 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid)); 657 } else { 658 newtable = reallocarray(oldtable, newsize, sizeof(enum typeid)); 659 if (newtable == NULL) 660 return (-1); 661 } 662 for (n = oldsize; n < newsize; n++) 663 newtable[n] = T_UNUSED; 664 665 types->table = newtable; 666 types->tablesize = newsize; 667 668 return (0); 669 } 670 671 /* 672 * Build the argument table from the completed type table. 673 * On malloc failure, *argtable is set to NULL. 674 */ 675 static void 676 build_arg_table(struct typetable *types, va_list ap, union arg **argtable) 677 { 678 u_int n; 679 680 if (types->tablemax >= STATIC_ARG_TBL_SIZE) { 681 *argtable = (union arg *) 682 malloc (sizeof (union arg) * (types->tablemax + 1)); 683 if (*argtable == NULL) 684 return; 685 } 686 687 (*argtable) [0].intarg = 0; 688 for (n = 1; n <= types->tablemax; n++) { 689 switch (types->table[n]) { 690 case T_UNUSED: /* whoops! */ 691 (*argtable) [n].intarg = va_arg (ap, int); 692 break; 693 case TP_SCHAR: 694 (*argtable) [n].pschararg = va_arg (ap, signed char *); 695 break; 696 case TP_SHORT: 697 (*argtable) [n].pshortarg = va_arg (ap, short *); 698 break; 699 case T_INT: 700 (*argtable) [n].intarg = va_arg (ap, int); 701 break; 702 case T_U_INT: 703 (*argtable) [n].uintarg = va_arg (ap, unsigned int); 704 break; 705 case TP_INT: 706 (*argtable) [n].pintarg = va_arg (ap, int *); 707 break; 708 case T_LONG: 709 (*argtable) [n].longarg = va_arg (ap, long); 710 break; 711 case T_U_LONG: 712 (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 713 break; 714 case TP_LONG: 715 (*argtable) [n].plongarg = va_arg (ap, long *); 716 break; 717 case T_LLONG: 718 (*argtable) [n].longlongarg = va_arg (ap, long long); 719 break; 720 case T_U_LLONG: 721 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 722 break; 723 case TP_LLONG: 724 (*argtable) [n].plonglongarg = va_arg (ap, long long *); 725 break; 726 case T_PTRDIFFT: 727 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 728 break; 729 case TP_PTRDIFFT: 730 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 731 break; 732 case T_SIZET: 733 (*argtable) [n].sizearg = va_arg (ap, size_t); 734 break; 735 case T_SSIZET: 736 (*argtable) [n].sizearg = va_arg (ap, ssize_t); 737 break; 738 case TP_SSIZET: 739 (*argtable) [n].pssizearg = va_arg (ap, ssize_t *); 740 break; 741 case T_INTMAXT: 742 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 743 break; 744 case T_UINTMAXT: 745 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 746 break; 747 case TP_INTMAXT: 748 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 749 break; 750 case T_DOUBLE: 751 #ifndef NO_FLOATING_POINT 752 (*argtable) [n].doublearg = va_arg (ap, double); 753 #endif 754 break; 755 case T_LONG_DOUBLE: 756 #ifndef NO_FLOATING_POINT 757 (*argtable) [n].longdoublearg = va_arg (ap, long double); 758 #endif 759 break; 760 case TP_CHAR: 761 (*argtable) [n].pchararg = va_arg (ap, char *); 762 break; 763 case TP_VOID: 764 (*argtable) [n].pvoidarg = va_arg (ap, void *); 765 break; 766 case T_WINT: 767 (*argtable) [n].wintarg = va_arg (ap, wint_t); 768 break; 769 case TP_WCHAR: 770 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 771 break; 772 } 773 } 774 } 775