xref: /onnv-gate/usr/src/common/openssl/crypto/bio/b_print.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/b_print.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* disable assert() unless BIO_DEBUG has been defined */
600Sstevel@tonic-gate #ifndef BIO_DEBUG
610Sstevel@tonic-gate # ifndef NDEBUG
620Sstevel@tonic-gate #  define NDEBUG
630Sstevel@tonic-gate # endif
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Stolen from tjh's ssl/ssl_trc.c stuff.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <stdio.h>
710Sstevel@tonic-gate #include <string.h>
720Sstevel@tonic-gate #include <ctype.h>
730Sstevel@tonic-gate #include <assert.h>
740Sstevel@tonic-gate #include <limits.h>
750Sstevel@tonic-gate #include "cryptlib.h"
760Sstevel@tonic-gate #ifndef NO_SYS_TYPES_H
770Sstevel@tonic-gate #include <sys/types.h>
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate #include <openssl/bn.h>         /* To get BN_LLONG properly defined */
800Sstevel@tonic-gate #include <openssl/bio.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #ifdef BN_LLONG
830Sstevel@tonic-gate # ifndef HAVE_LONG_LONG
840Sstevel@tonic-gate #  define HAVE_LONG_LONG 1
850Sstevel@tonic-gate # endif
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /***************************************************************************/
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Copyright Patrick Powell 1995
920Sstevel@tonic-gate  * This code is based on code written by Patrick Powell <papowell@astart.com>
930Sstevel@tonic-gate  * It may be used for any purpose as long as this notice remains intact
940Sstevel@tonic-gate  * on all source code distributions.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * This code contains numerious changes and enhancements which were
990Sstevel@tonic-gate  * made by lots of contributors over the last years to Patrick Powell's
1000Sstevel@tonic-gate  * original code:
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * o Patrick Powell <papowell@astart.com>      (1995)
1030Sstevel@tonic-gate  * o Brandon Long <blong@fiction.net>          (1996, for Mutt)
1040Sstevel@tonic-gate  * o Thomas Roessler <roessler@guug.de>        (1998, for Mutt)
1050Sstevel@tonic-gate  * o Michael Elkins <me@cs.hmc.edu>            (1998, for Mutt)
1060Sstevel@tonic-gate  * o Andrew Tridgell <tridge@samba.org>        (1998, for Samba)
1070Sstevel@tonic-gate  * o Luke Mewburn <lukem@netbsd.org>           (1999, for LukemFTP)
1080Sstevel@tonic-gate  * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
1090Sstevel@tonic-gate  * o ...                                       (for OpenSSL)
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #ifdef HAVE_LONG_DOUBLE
1130Sstevel@tonic-gate #define LDOUBLE long double
1140Sstevel@tonic-gate #else
1150Sstevel@tonic-gate #define LDOUBLE double
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #if HAVE_LONG_LONG
1190Sstevel@tonic-gate # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
1200Sstevel@tonic-gate # define LLONG _int64
1210Sstevel@tonic-gate # else
1220Sstevel@tonic-gate # define LLONG long long
1230Sstevel@tonic-gate # endif
1240Sstevel@tonic-gate #else
1250Sstevel@tonic-gate #define LLONG long
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static void fmtstr     (char **, char **, size_t *, size_t *,
1290Sstevel@tonic-gate 			const char *, int, int, int);
1300Sstevel@tonic-gate static void fmtint     (char **, char **, size_t *, size_t *,
1310Sstevel@tonic-gate 			LLONG, int, int, int, int);
1320Sstevel@tonic-gate static void fmtfp      (char **, char **, size_t *, size_t *,
1330Sstevel@tonic-gate 			LDOUBLE, int, int, int);
1340Sstevel@tonic-gate static void doapr_outch (char **, char **, size_t *, size_t *, int);
1350Sstevel@tonic-gate static void _dopr(char **sbuffer, char **buffer,
1360Sstevel@tonic-gate 		  size_t *maxlen, size_t *retlen, int *truncated,
1370Sstevel@tonic-gate 		  const char *format, va_list args);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* format read states */
1400Sstevel@tonic-gate #define DP_S_DEFAULT    0
1410Sstevel@tonic-gate #define DP_S_FLAGS      1
1420Sstevel@tonic-gate #define DP_S_MIN        2
1430Sstevel@tonic-gate #define DP_S_DOT        3
1440Sstevel@tonic-gate #define DP_S_MAX        4
1450Sstevel@tonic-gate #define DP_S_MOD        5
1460Sstevel@tonic-gate #define DP_S_CONV       6
1470Sstevel@tonic-gate #define DP_S_DONE       7
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* format flags - Bits */
1500Sstevel@tonic-gate #define DP_F_MINUS      (1 << 0)
1510Sstevel@tonic-gate #define DP_F_PLUS       (1 << 1)
1520Sstevel@tonic-gate #define DP_F_SPACE      (1 << 2)
1530Sstevel@tonic-gate #define DP_F_NUM        (1 << 3)
1540Sstevel@tonic-gate #define DP_F_ZERO       (1 << 4)
1550Sstevel@tonic-gate #define DP_F_UP         (1 << 5)
1560Sstevel@tonic-gate #define DP_F_UNSIGNED   (1 << 6)
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /* conversion flags */
1590Sstevel@tonic-gate #define DP_C_SHORT      1
1600Sstevel@tonic-gate #define DP_C_LONG       2
1610Sstevel@tonic-gate #define DP_C_LDOUBLE    3
1620Sstevel@tonic-gate #define DP_C_LLONG      4
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /* some handy macros */
1650Sstevel@tonic-gate #define char_to_int(p) (p - '0')
1660Sstevel@tonic-gate #define OSSL_MAX(p,q) ((p >= q) ? p : q)
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static void
_dopr(char ** sbuffer,char ** buffer,size_t * maxlen,size_t * retlen,int * truncated,const char * format,va_list args)1690Sstevel@tonic-gate _dopr(
1700Sstevel@tonic-gate     char **sbuffer,
1710Sstevel@tonic-gate     char **buffer,
1720Sstevel@tonic-gate     size_t *maxlen,
1730Sstevel@tonic-gate     size_t *retlen,
1740Sstevel@tonic-gate     int *truncated,
1750Sstevel@tonic-gate     const char *format,
1760Sstevel@tonic-gate     va_list args)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate     char ch;
1790Sstevel@tonic-gate     LLONG value;
1800Sstevel@tonic-gate     LDOUBLE fvalue;
1810Sstevel@tonic-gate     char *strvalue;
1820Sstevel@tonic-gate     int min;
1830Sstevel@tonic-gate     int max;
1840Sstevel@tonic-gate     int state;
1850Sstevel@tonic-gate     int flags;
1860Sstevel@tonic-gate     int cflags;
1870Sstevel@tonic-gate     size_t currlen;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate     state = DP_S_DEFAULT;
1900Sstevel@tonic-gate     flags = currlen = cflags = min = 0;
1910Sstevel@tonic-gate     max = -1;
1920Sstevel@tonic-gate     ch = *format++;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate     while (state != DP_S_DONE) {
1950Sstevel@tonic-gate         if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
1960Sstevel@tonic-gate             state = DP_S_DONE;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate         switch (state) {
1990Sstevel@tonic-gate         case DP_S_DEFAULT:
2000Sstevel@tonic-gate             if (ch == '%')
2010Sstevel@tonic-gate                 state = DP_S_FLAGS;
2020Sstevel@tonic-gate             else
2030Sstevel@tonic-gate                 doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
2040Sstevel@tonic-gate             ch = *format++;
2050Sstevel@tonic-gate             break;
2060Sstevel@tonic-gate         case DP_S_FLAGS:
2070Sstevel@tonic-gate             switch (ch) {
2080Sstevel@tonic-gate             case '-':
2090Sstevel@tonic-gate                 flags |= DP_F_MINUS;
2100Sstevel@tonic-gate                 ch = *format++;
2110Sstevel@tonic-gate                 break;
2120Sstevel@tonic-gate             case '+':
2130Sstevel@tonic-gate                 flags |= DP_F_PLUS;
2140Sstevel@tonic-gate                 ch = *format++;
2150Sstevel@tonic-gate                 break;
2160Sstevel@tonic-gate             case ' ':
2170Sstevel@tonic-gate                 flags |= DP_F_SPACE;
2180Sstevel@tonic-gate                 ch = *format++;
2190Sstevel@tonic-gate                 break;
2200Sstevel@tonic-gate             case '#':
2210Sstevel@tonic-gate                 flags |= DP_F_NUM;
2220Sstevel@tonic-gate                 ch = *format++;
2230Sstevel@tonic-gate                 break;
2240Sstevel@tonic-gate             case '0':
2250Sstevel@tonic-gate                 flags |= DP_F_ZERO;
2260Sstevel@tonic-gate                 ch = *format++;
2270Sstevel@tonic-gate                 break;
2280Sstevel@tonic-gate             default:
2290Sstevel@tonic-gate                 state = DP_S_MIN;
2300Sstevel@tonic-gate                 break;
2310Sstevel@tonic-gate             }
2320Sstevel@tonic-gate             break;
2330Sstevel@tonic-gate         case DP_S_MIN:
2340Sstevel@tonic-gate             if (isdigit((unsigned char)ch)) {
2350Sstevel@tonic-gate                 min = 10 * min + char_to_int(ch);
2360Sstevel@tonic-gate                 ch = *format++;
2370Sstevel@tonic-gate             } else if (ch == '*') {
2380Sstevel@tonic-gate                 min = va_arg(args, int);
2390Sstevel@tonic-gate                 ch = *format++;
2400Sstevel@tonic-gate                 state = DP_S_DOT;
2410Sstevel@tonic-gate             } else
2420Sstevel@tonic-gate                 state = DP_S_DOT;
2430Sstevel@tonic-gate             break;
2440Sstevel@tonic-gate         case DP_S_DOT:
2450Sstevel@tonic-gate             if (ch == '.') {
2460Sstevel@tonic-gate                 state = DP_S_MAX;
2470Sstevel@tonic-gate                 ch = *format++;
2480Sstevel@tonic-gate             } else
2490Sstevel@tonic-gate                 state = DP_S_MOD;
2500Sstevel@tonic-gate             break;
2510Sstevel@tonic-gate         case DP_S_MAX:
2520Sstevel@tonic-gate             if (isdigit((unsigned char)ch)) {
2530Sstevel@tonic-gate                 if (max < 0)
2540Sstevel@tonic-gate                     max = 0;
2550Sstevel@tonic-gate                 max = 10 * max + char_to_int(ch);
2560Sstevel@tonic-gate                 ch = *format++;
2570Sstevel@tonic-gate             } else if (ch == '*') {
2580Sstevel@tonic-gate                 max = va_arg(args, int);
2590Sstevel@tonic-gate                 ch = *format++;
2600Sstevel@tonic-gate                 state = DP_S_MOD;
2610Sstevel@tonic-gate             } else
2620Sstevel@tonic-gate                 state = DP_S_MOD;
2630Sstevel@tonic-gate             break;
2640Sstevel@tonic-gate         case DP_S_MOD:
2650Sstevel@tonic-gate             switch (ch) {
2660Sstevel@tonic-gate             case 'h':
2670Sstevel@tonic-gate                 cflags = DP_C_SHORT;
2680Sstevel@tonic-gate                 ch = *format++;
2690Sstevel@tonic-gate                 break;
2700Sstevel@tonic-gate             case 'l':
2710Sstevel@tonic-gate                 if (*format == 'l') {
2720Sstevel@tonic-gate                     cflags = DP_C_LLONG;
2730Sstevel@tonic-gate                     format++;
2740Sstevel@tonic-gate                 } else
2750Sstevel@tonic-gate                     cflags = DP_C_LONG;
2760Sstevel@tonic-gate                 ch = *format++;
2770Sstevel@tonic-gate                 break;
2780Sstevel@tonic-gate             case 'q':
2790Sstevel@tonic-gate                 cflags = DP_C_LLONG;
2800Sstevel@tonic-gate                 ch = *format++;
2810Sstevel@tonic-gate                 break;
2820Sstevel@tonic-gate             case 'L':
2830Sstevel@tonic-gate                 cflags = DP_C_LDOUBLE;
2840Sstevel@tonic-gate                 ch = *format++;
2850Sstevel@tonic-gate                 break;
2860Sstevel@tonic-gate             default:
2870Sstevel@tonic-gate                 break;
2880Sstevel@tonic-gate             }
2890Sstevel@tonic-gate             state = DP_S_CONV;
2900Sstevel@tonic-gate             break;
2910Sstevel@tonic-gate         case DP_S_CONV:
2920Sstevel@tonic-gate             switch (ch) {
2930Sstevel@tonic-gate             case 'd':
2940Sstevel@tonic-gate             case 'i':
2950Sstevel@tonic-gate                 switch (cflags) {
2960Sstevel@tonic-gate                 case DP_C_SHORT:
2970Sstevel@tonic-gate                     value = (short int)va_arg(args, int);
2980Sstevel@tonic-gate                     break;
2990Sstevel@tonic-gate                 case DP_C_LONG:
3000Sstevel@tonic-gate                     value = va_arg(args, long int);
3010Sstevel@tonic-gate                     break;
3020Sstevel@tonic-gate                 case DP_C_LLONG:
3030Sstevel@tonic-gate                     value = va_arg(args, LLONG);
3040Sstevel@tonic-gate                     break;
3050Sstevel@tonic-gate                 default:
3060Sstevel@tonic-gate                     value = va_arg(args, int);
3070Sstevel@tonic-gate                     break;
3080Sstevel@tonic-gate                 }
3090Sstevel@tonic-gate                 fmtint(sbuffer, buffer, &currlen, maxlen,
3100Sstevel@tonic-gate                        value, 10, min, max, flags);
3110Sstevel@tonic-gate                 break;
3120Sstevel@tonic-gate             case 'X':
3130Sstevel@tonic-gate                 flags |= DP_F_UP;
3140Sstevel@tonic-gate                 /* FALLTHROUGH */
3150Sstevel@tonic-gate             case 'x':
3160Sstevel@tonic-gate             case 'o':
3170Sstevel@tonic-gate             case 'u':
3180Sstevel@tonic-gate                 flags |= DP_F_UNSIGNED;
3190Sstevel@tonic-gate                 switch (cflags) {
3200Sstevel@tonic-gate                 case DP_C_SHORT:
3210Sstevel@tonic-gate                     value = (unsigned short int)va_arg(args, unsigned int);
3220Sstevel@tonic-gate                     break;
3230Sstevel@tonic-gate                 case DP_C_LONG:
3240Sstevel@tonic-gate                     value = (LLONG) va_arg(args,
3250Sstevel@tonic-gate                         unsigned long int);
3260Sstevel@tonic-gate                     break;
3270Sstevel@tonic-gate                 case DP_C_LLONG:
3280Sstevel@tonic-gate                     value = va_arg(args, unsigned LLONG);
3290Sstevel@tonic-gate                     break;
3300Sstevel@tonic-gate                 default:
3310Sstevel@tonic-gate                     value = (LLONG) va_arg(args,
3320Sstevel@tonic-gate                         unsigned int);
3330Sstevel@tonic-gate                     break;
3340Sstevel@tonic-gate                 }
3350Sstevel@tonic-gate                 fmtint(sbuffer, buffer, &currlen, maxlen, value,
3360Sstevel@tonic-gate                        ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
3370Sstevel@tonic-gate                        min, max, flags);
3380Sstevel@tonic-gate                 break;
3390Sstevel@tonic-gate             case 'f':
3400Sstevel@tonic-gate                 if (cflags == DP_C_LDOUBLE)
3410Sstevel@tonic-gate                     fvalue = va_arg(args, LDOUBLE);
3420Sstevel@tonic-gate                 else
3430Sstevel@tonic-gate                     fvalue = va_arg(args, double);
3440Sstevel@tonic-gate                 fmtfp(sbuffer, buffer, &currlen, maxlen,
3450Sstevel@tonic-gate                       fvalue, min, max, flags);
3460Sstevel@tonic-gate                 break;
3470Sstevel@tonic-gate             case 'E':
3480Sstevel@tonic-gate                 flags |= DP_F_UP;
3490Sstevel@tonic-gate             case 'e':
3500Sstevel@tonic-gate                 if (cflags == DP_C_LDOUBLE)
3510Sstevel@tonic-gate                     fvalue = va_arg(args, LDOUBLE);
3520Sstevel@tonic-gate                 else
3530Sstevel@tonic-gate                     fvalue = va_arg(args, double);
3540Sstevel@tonic-gate                 break;
3550Sstevel@tonic-gate             case 'G':
3560Sstevel@tonic-gate                 flags |= DP_F_UP;
3570Sstevel@tonic-gate             case 'g':
3580Sstevel@tonic-gate                 if (cflags == DP_C_LDOUBLE)
3590Sstevel@tonic-gate                     fvalue = va_arg(args, LDOUBLE);
3600Sstevel@tonic-gate                 else
3610Sstevel@tonic-gate                     fvalue = va_arg(args, double);
3620Sstevel@tonic-gate                 break;
3630Sstevel@tonic-gate             case 'c':
3640Sstevel@tonic-gate                 doapr_outch(sbuffer, buffer, &currlen, maxlen,
3650Sstevel@tonic-gate                     va_arg(args, int));
3660Sstevel@tonic-gate                 break;
3670Sstevel@tonic-gate             case 's':
3680Sstevel@tonic-gate                 strvalue = va_arg(args, char *);
3690Sstevel@tonic-gate                 if (max < 0) {
3700Sstevel@tonic-gate 		    if (buffer)
3710Sstevel@tonic-gate 			max = INT_MAX;
3720Sstevel@tonic-gate 		    else
3730Sstevel@tonic-gate 			max = *maxlen;
3740Sstevel@tonic-gate 		}
3750Sstevel@tonic-gate                 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
3760Sstevel@tonic-gate                        flags, min, max);
3770Sstevel@tonic-gate                 break;
3780Sstevel@tonic-gate             case 'p':
3790Sstevel@tonic-gate                 value = (long)va_arg(args, void *);
3800Sstevel@tonic-gate                 fmtint(sbuffer, buffer, &currlen, maxlen,
3810Sstevel@tonic-gate                     value, 16, min, max, flags|DP_F_NUM);
3820Sstevel@tonic-gate                 break;
3830Sstevel@tonic-gate             case 'n': /* XXX */
3840Sstevel@tonic-gate                 if (cflags == DP_C_SHORT) {
3850Sstevel@tonic-gate                     short int *num;
3860Sstevel@tonic-gate                     num = va_arg(args, short int *);
3870Sstevel@tonic-gate                     *num = currlen;
3880Sstevel@tonic-gate                 } else if (cflags == DP_C_LONG) { /* XXX */
3890Sstevel@tonic-gate                     long int *num;
3900Sstevel@tonic-gate                     num = va_arg(args, long int *);
3910Sstevel@tonic-gate                     *num = (long int) currlen;
3920Sstevel@tonic-gate                 } else if (cflags == DP_C_LLONG) { /* XXX */
3930Sstevel@tonic-gate                     LLONG *num;
3940Sstevel@tonic-gate                     num = va_arg(args, LLONG *);
3950Sstevel@tonic-gate                     *num = (LLONG) currlen;
3960Sstevel@tonic-gate                 } else {
3970Sstevel@tonic-gate                     int    *num;
3980Sstevel@tonic-gate                     num = va_arg(args, int *);
3990Sstevel@tonic-gate                     *num = currlen;
4000Sstevel@tonic-gate                 }
4010Sstevel@tonic-gate                 break;
4020Sstevel@tonic-gate             case '%':
4030Sstevel@tonic-gate                 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
4040Sstevel@tonic-gate                 break;
4050Sstevel@tonic-gate             case 'w':
4060Sstevel@tonic-gate                 /* not supported yet, treat as next char */
4070Sstevel@tonic-gate                 ch = *format++;
4080Sstevel@tonic-gate                 break;
4090Sstevel@tonic-gate             default:
4100Sstevel@tonic-gate                 /* unknown, skip */
4110Sstevel@tonic-gate                 break;
4120Sstevel@tonic-gate             }
4130Sstevel@tonic-gate             ch = *format++;
4140Sstevel@tonic-gate             state = DP_S_DEFAULT;
4150Sstevel@tonic-gate             flags = cflags = min = 0;
4160Sstevel@tonic-gate             max = -1;
4170Sstevel@tonic-gate             break;
4180Sstevel@tonic-gate         case DP_S_DONE:
4190Sstevel@tonic-gate             break;
4200Sstevel@tonic-gate         default:
4210Sstevel@tonic-gate             break;
4220Sstevel@tonic-gate         }
4230Sstevel@tonic-gate     }
4240Sstevel@tonic-gate     *truncated = (currlen > *maxlen - 1);
4250Sstevel@tonic-gate     if (*truncated)
4260Sstevel@tonic-gate         currlen = *maxlen - 1;
4270Sstevel@tonic-gate     doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
4280Sstevel@tonic-gate     *retlen = currlen - 1;
4290Sstevel@tonic-gate     return;
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate static void
fmtstr(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,const char * value,int flags,int min,int max)4330Sstevel@tonic-gate fmtstr(
4340Sstevel@tonic-gate     char **sbuffer,
4350Sstevel@tonic-gate     char **buffer,
4360Sstevel@tonic-gate     size_t *currlen,
4370Sstevel@tonic-gate     size_t *maxlen,
4380Sstevel@tonic-gate     const char *value,
4390Sstevel@tonic-gate     int flags,
4400Sstevel@tonic-gate     int min,
4410Sstevel@tonic-gate     int max)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate     int padlen, strln;
4440Sstevel@tonic-gate     int cnt = 0;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate     if (value == 0)
4470Sstevel@tonic-gate         value = "<NULL>";
4480Sstevel@tonic-gate     for (strln = 0; value[strln]; ++strln)
4490Sstevel@tonic-gate         ;
4500Sstevel@tonic-gate     padlen = min - strln;
4510Sstevel@tonic-gate     if (padlen < 0)
4520Sstevel@tonic-gate         padlen = 0;
4530Sstevel@tonic-gate     if (flags & DP_F_MINUS)
4540Sstevel@tonic-gate         padlen = -padlen;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate     while ((padlen > 0) && (cnt < max)) {
4570Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
4580Sstevel@tonic-gate         --padlen;
4590Sstevel@tonic-gate         ++cnt;
4600Sstevel@tonic-gate     }
4610Sstevel@tonic-gate     while (*value && (cnt < max)) {
4620Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
4630Sstevel@tonic-gate         ++cnt;
4640Sstevel@tonic-gate     }
4650Sstevel@tonic-gate     while ((padlen < 0) && (cnt < max)) {
4660Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
4670Sstevel@tonic-gate         ++padlen;
4680Sstevel@tonic-gate         ++cnt;
4690Sstevel@tonic-gate     }
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate static void
fmtint(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,LLONG value,int base,int min,int max,int flags)4730Sstevel@tonic-gate fmtint(
4740Sstevel@tonic-gate     char **sbuffer,
4750Sstevel@tonic-gate     char **buffer,
4760Sstevel@tonic-gate     size_t *currlen,
4770Sstevel@tonic-gate     size_t *maxlen,
4780Sstevel@tonic-gate     LLONG value,
4790Sstevel@tonic-gate     int base,
4800Sstevel@tonic-gate     int min,
4810Sstevel@tonic-gate     int max,
4820Sstevel@tonic-gate     int flags)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate     int signvalue = 0;
485*2139Sjp161948     const char *prefix = "";
4860Sstevel@tonic-gate     unsigned LLONG uvalue;
4870Sstevel@tonic-gate     char convert[DECIMAL_SIZE(value)+3];
4880Sstevel@tonic-gate     int place = 0;
4890Sstevel@tonic-gate     int spadlen = 0;
4900Sstevel@tonic-gate     int zpadlen = 0;
4910Sstevel@tonic-gate     int caps = 0;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate     if (max < 0)
4940Sstevel@tonic-gate         max = 0;
4950Sstevel@tonic-gate     uvalue = value;
4960Sstevel@tonic-gate     if (!(flags & DP_F_UNSIGNED)) {
4970Sstevel@tonic-gate         if (value < 0) {
4980Sstevel@tonic-gate             signvalue = '-';
4990Sstevel@tonic-gate             uvalue = -value;
5000Sstevel@tonic-gate         } else if (flags & DP_F_PLUS)
5010Sstevel@tonic-gate             signvalue = '+';
5020Sstevel@tonic-gate         else if (flags & DP_F_SPACE)
5030Sstevel@tonic-gate             signvalue = ' ';
5040Sstevel@tonic-gate     }
5050Sstevel@tonic-gate     if (flags & DP_F_NUM) {
5060Sstevel@tonic-gate 	if (base == 8) prefix = "0";
5070Sstevel@tonic-gate 	if (base == 16) prefix = "0x";
5080Sstevel@tonic-gate     }
5090Sstevel@tonic-gate     if (flags & DP_F_UP)
5100Sstevel@tonic-gate         caps = 1;
5110Sstevel@tonic-gate     do {
5120Sstevel@tonic-gate         convert[place++] =
5130Sstevel@tonic-gate             (caps ? "0123456789ABCDEF" : "0123456789abcdef")
5140Sstevel@tonic-gate             [uvalue % (unsigned) base];
5150Sstevel@tonic-gate         uvalue = (uvalue / (unsigned) base);
516*2139Sjp161948     } while (uvalue && (place < (int)sizeof(convert)));
517*2139Sjp161948     if (place == sizeof(convert))
5180Sstevel@tonic-gate         place--;
5190Sstevel@tonic-gate     convert[place] = 0;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate     zpadlen = max - place;
5220Sstevel@tonic-gate     spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
5230Sstevel@tonic-gate     if (zpadlen < 0)
5240Sstevel@tonic-gate         zpadlen = 0;
5250Sstevel@tonic-gate     if (spadlen < 0)
5260Sstevel@tonic-gate         spadlen = 0;
5270Sstevel@tonic-gate     if (flags & DP_F_ZERO) {
5280Sstevel@tonic-gate         zpadlen = OSSL_MAX(zpadlen, spadlen);
5290Sstevel@tonic-gate         spadlen = 0;
5300Sstevel@tonic-gate     }
5310Sstevel@tonic-gate     if (flags & DP_F_MINUS)
5320Sstevel@tonic-gate         spadlen = -spadlen;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate     /* spaces */
5350Sstevel@tonic-gate     while (spadlen > 0) {
5360Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5370Sstevel@tonic-gate         --spadlen;
5380Sstevel@tonic-gate     }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate     /* sign */
5410Sstevel@tonic-gate     if (signvalue)
5420Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate     /* prefix */
5450Sstevel@tonic-gate     while (*prefix) {
5460Sstevel@tonic-gate 	doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
5470Sstevel@tonic-gate 	prefix++;
5480Sstevel@tonic-gate     }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate     /* zeros */
5510Sstevel@tonic-gate     if (zpadlen > 0) {
5520Sstevel@tonic-gate         while (zpadlen > 0) {
5530Sstevel@tonic-gate             doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
5540Sstevel@tonic-gate             --zpadlen;
5550Sstevel@tonic-gate         }
5560Sstevel@tonic-gate     }
5570Sstevel@tonic-gate     /* digits */
5580Sstevel@tonic-gate     while (place > 0)
5590Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate     /* left justified spaces */
5620Sstevel@tonic-gate     while (spadlen < 0) {
5630Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
5640Sstevel@tonic-gate         ++spadlen;
5650Sstevel@tonic-gate     }
5660Sstevel@tonic-gate     return;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate static LDOUBLE
abs_val(LDOUBLE value)5700Sstevel@tonic-gate abs_val(LDOUBLE value)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate     LDOUBLE result = value;
5730Sstevel@tonic-gate     if (value < 0)
5740Sstevel@tonic-gate         result = -value;
5750Sstevel@tonic-gate     return result;
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate static LDOUBLE
pow_10(int in_exp)579*2139Sjp161948 pow_10(int in_exp)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate     LDOUBLE result = 1;
5820Sstevel@tonic-gate     while (in_exp) {
5830Sstevel@tonic-gate         result *= 10;
5840Sstevel@tonic-gate         in_exp--;
5850Sstevel@tonic-gate     }
5860Sstevel@tonic-gate     return result;
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate static long
roundv(LDOUBLE value)5900Sstevel@tonic-gate roundv(LDOUBLE value)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate     long intpart;
5930Sstevel@tonic-gate     intpart = (long) value;
5940Sstevel@tonic-gate     value = value - intpart;
5950Sstevel@tonic-gate     if (value >= 0.5)
5960Sstevel@tonic-gate         intpart++;
5970Sstevel@tonic-gate     return intpart;
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate static void
fmtfp(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,LDOUBLE fvalue,int min,int max,int flags)6010Sstevel@tonic-gate fmtfp(
6020Sstevel@tonic-gate     char **sbuffer,
6030Sstevel@tonic-gate     char **buffer,
6040Sstevel@tonic-gate     size_t *currlen,
6050Sstevel@tonic-gate     size_t *maxlen,
6060Sstevel@tonic-gate     LDOUBLE fvalue,
6070Sstevel@tonic-gate     int min,
6080Sstevel@tonic-gate     int max,
6090Sstevel@tonic-gate     int flags)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate     int signvalue = 0;
6120Sstevel@tonic-gate     LDOUBLE ufvalue;
6130Sstevel@tonic-gate     char iconvert[20];
6140Sstevel@tonic-gate     char fconvert[20];
6150Sstevel@tonic-gate     int iplace = 0;
6160Sstevel@tonic-gate     int fplace = 0;
6170Sstevel@tonic-gate     int padlen = 0;
6180Sstevel@tonic-gate     int zpadlen = 0;
6190Sstevel@tonic-gate     int caps = 0;
6200Sstevel@tonic-gate     long intpart;
6210Sstevel@tonic-gate     long fracpart;
622*2139Sjp161948     long max10;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate     if (max < 0)
6250Sstevel@tonic-gate         max = 6;
6260Sstevel@tonic-gate     ufvalue = abs_val(fvalue);
6270Sstevel@tonic-gate     if (fvalue < 0)
6280Sstevel@tonic-gate         signvalue = '-';
6290Sstevel@tonic-gate     else if (flags & DP_F_PLUS)
6300Sstevel@tonic-gate         signvalue = '+';
6310Sstevel@tonic-gate     else if (flags & DP_F_SPACE)
6320Sstevel@tonic-gate         signvalue = ' ';
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate     intpart = (long)ufvalue;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate     /* sorry, we only support 9 digits past the decimal because of our
6370Sstevel@tonic-gate        conversion method */
6380Sstevel@tonic-gate     if (max > 9)
6390Sstevel@tonic-gate         max = 9;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate     /* we "cheat" by converting the fractional part to integer by
6420Sstevel@tonic-gate        multiplying by a factor of 10 */
643*2139Sjp161948     max10 = roundv(pow_10(max));
644*2139Sjp161948     fracpart = roundv(pow_10(max) * (ufvalue - intpart));
6450Sstevel@tonic-gate 
646*2139Sjp161948     if (fracpart >= max10) {
6470Sstevel@tonic-gate         intpart++;
648*2139Sjp161948         fracpart -= max10;
6490Sstevel@tonic-gate     }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate     /* convert integer part */
6520Sstevel@tonic-gate     do {
6530Sstevel@tonic-gate         iconvert[iplace++] =
6540Sstevel@tonic-gate             (caps ? "0123456789ABCDEF"
6550Sstevel@tonic-gate               : "0123456789abcdef")[intpart % 10];
6560Sstevel@tonic-gate         intpart = (intpart / 10);
657*2139Sjp161948     } while (intpart && (iplace < (int)sizeof(iconvert)));
6580Sstevel@tonic-gate     if (iplace == sizeof iconvert)
6590Sstevel@tonic-gate         iplace--;
6600Sstevel@tonic-gate     iconvert[iplace] = 0;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate     /* convert fractional part */
6630Sstevel@tonic-gate     do {
6640Sstevel@tonic-gate         fconvert[fplace++] =
6650Sstevel@tonic-gate             (caps ? "0123456789ABCDEF"
6660Sstevel@tonic-gate               : "0123456789abcdef")[fracpart % 10];
6670Sstevel@tonic-gate         fracpart = (fracpart / 10);
6680Sstevel@tonic-gate     } while (fplace < max);
6690Sstevel@tonic-gate     if (fplace == sizeof fconvert)
6700Sstevel@tonic-gate         fplace--;
6710Sstevel@tonic-gate     fconvert[fplace] = 0;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate     /* -1 for decimal point, another -1 if we are printing a sign */
6740Sstevel@tonic-gate     padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
6750Sstevel@tonic-gate     zpadlen = max - fplace;
6760Sstevel@tonic-gate     if (zpadlen < 0)
6770Sstevel@tonic-gate         zpadlen = 0;
6780Sstevel@tonic-gate     if (padlen < 0)
6790Sstevel@tonic-gate         padlen = 0;
6800Sstevel@tonic-gate     if (flags & DP_F_MINUS)
6810Sstevel@tonic-gate         padlen = -padlen;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate     if ((flags & DP_F_ZERO) && (padlen > 0)) {
6840Sstevel@tonic-gate         if (signvalue) {
6850Sstevel@tonic-gate             doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
6860Sstevel@tonic-gate             --padlen;
6870Sstevel@tonic-gate             signvalue = 0;
6880Sstevel@tonic-gate         }
6890Sstevel@tonic-gate         while (padlen > 0) {
6900Sstevel@tonic-gate             doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
6910Sstevel@tonic-gate             --padlen;
6920Sstevel@tonic-gate         }
6930Sstevel@tonic-gate     }
6940Sstevel@tonic-gate     while (padlen > 0) {
6950Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
6960Sstevel@tonic-gate         --padlen;
6970Sstevel@tonic-gate     }
6980Sstevel@tonic-gate     if (signvalue)
6990Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate     while (iplace > 0)
7020Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate     /*
7050Sstevel@tonic-gate      * Decimal point. This should probably use locale to find the correct
7060Sstevel@tonic-gate      * char to print out.
7070Sstevel@tonic-gate      */
7080Sstevel@tonic-gate     if (max > 0 || (flags & DP_F_NUM)) {
7090Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate         while (fplace > 0)
7120Sstevel@tonic-gate             doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
7130Sstevel@tonic-gate     }
7140Sstevel@tonic-gate     while (zpadlen > 0) {
7150Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
7160Sstevel@tonic-gate         --zpadlen;
7170Sstevel@tonic-gate     }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate     while (padlen < 0) {
7200Sstevel@tonic-gate         doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
7210Sstevel@tonic-gate         ++padlen;
7220Sstevel@tonic-gate     }
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate static void
doapr_outch(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,int c)7260Sstevel@tonic-gate doapr_outch(
7270Sstevel@tonic-gate     char **sbuffer,
7280Sstevel@tonic-gate     char **buffer,
7290Sstevel@tonic-gate     size_t *currlen,
7300Sstevel@tonic-gate     size_t *maxlen,
7310Sstevel@tonic-gate     int c)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate     /* If we haven't at least one buffer, someone has doe a big booboo */
7340Sstevel@tonic-gate     assert(*sbuffer != NULL || buffer != NULL);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate     if (buffer) {
7370Sstevel@tonic-gate 	while (*currlen >= *maxlen) {
7380Sstevel@tonic-gate 	    if (*buffer == NULL) {
7390Sstevel@tonic-gate 		if (*maxlen == 0)
7400Sstevel@tonic-gate 		    *maxlen = 1024;
7410Sstevel@tonic-gate 		*buffer = OPENSSL_malloc(*maxlen);
7420Sstevel@tonic-gate 		if (*currlen > 0) {
7430Sstevel@tonic-gate 		    assert(*sbuffer != NULL);
7440Sstevel@tonic-gate 		    memcpy(*buffer, *sbuffer, *currlen);
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 		*sbuffer = NULL;
7470Sstevel@tonic-gate 	    } else {
7480Sstevel@tonic-gate 		*maxlen += 1024;
7490Sstevel@tonic-gate 		*buffer = OPENSSL_realloc(*buffer, *maxlen);
7500Sstevel@tonic-gate 	    }
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 	/* What to do if *buffer is NULL? */
7530Sstevel@tonic-gate 	assert(*sbuffer != NULL || *buffer != NULL);
7540Sstevel@tonic-gate     }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate     if (*currlen < *maxlen) {
7570Sstevel@tonic-gate 	if (*sbuffer)
7580Sstevel@tonic-gate 	    (*sbuffer)[(*currlen)++] = (char)c;
7590Sstevel@tonic-gate 	else
7600Sstevel@tonic-gate 	    (*buffer)[(*currlen)++] = (char)c;
7610Sstevel@tonic-gate     }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate     return;
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate /***************************************************************************/
7670Sstevel@tonic-gate 
BIO_printf(BIO * bio,const char * format,...)7680Sstevel@tonic-gate int BIO_printf (BIO *bio, const char *format, ...)
7690Sstevel@tonic-gate 	{
7700Sstevel@tonic-gate 	va_list args;
7710Sstevel@tonic-gate 	int ret;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	va_start(args, format);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	ret = BIO_vprintf(bio, format, args);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	va_end(args);
7780Sstevel@tonic-gate 	return(ret);
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 
BIO_vprintf(BIO * bio,const char * format,va_list args)7810Sstevel@tonic-gate int BIO_vprintf (BIO *bio, const char *format, va_list args)
7820Sstevel@tonic-gate 	{
7830Sstevel@tonic-gate 	int ret;
7840Sstevel@tonic-gate 	size_t retlen;
7850Sstevel@tonic-gate 	char hugebuf[1024*2];	/* Was previously 10k, which is unreasonable
7860Sstevel@tonic-gate 				   in small-stack environments, like threads
7870Sstevel@tonic-gate 				   or DOS programs. */
7880Sstevel@tonic-gate 	char *hugebufp = hugebuf;
7890Sstevel@tonic-gate 	size_t hugebufsize = sizeof(hugebuf);
7900Sstevel@tonic-gate 	char *dynbuf = NULL;
7910Sstevel@tonic-gate 	int ignored;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	dynbuf = NULL;
7940Sstevel@tonic-gate 	CRYPTO_push_info("doapr()");
7950Sstevel@tonic-gate 	_dopr(&hugebufp, &dynbuf, &hugebufsize,
7960Sstevel@tonic-gate 		&retlen, &ignored, format, args);
7970Sstevel@tonic-gate 	if (dynbuf)
7980Sstevel@tonic-gate 		{
7990Sstevel@tonic-gate 		ret=BIO_write(bio, dynbuf, (int)retlen);
8000Sstevel@tonic-gate 		OPENSSL_free(dynbuf);
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	else
8030Sstevel@tonic-gate 		{
8040Sstevel@tonic-gate 		ret=BIO_write(bio, hugebuf, (int)retlen);
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 	CRYPTO_pop_info();
8070Sstevel@tonic-gate 	return(ret);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate /* As snprintf is not available everywhere, we provide our own implementation.
8110Sstevel@tonic-gate  * This function has nothing to do with BIOs, but it's closely related
8120Sstevel@tonic-gate  * to BIO_printf, and we need *some* name prefix ...
8130Sstevel@tonic-gate  * (XXX  the function should be renamed, but to what?) */
BIO_snprintf(char * buf,size_t n,const char * format,...)8140Sstevel@tonic-gate int BIO_snprintf(char *buf, size_t n, const char *format, ...)
8150Sstevel@tonic-gate 	{
8160Sstevel@tonic-gate 	va_list args;
8170Sstevel@tonic-gate 	int ret;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	va_start(args, format);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	ret = BIO_vsnprintf(buf, n, format, args);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	va_end(args);
8240Sstevel@tonic-gate 	return(ret);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
BIO_vsnprintf(char * buf,size_t n,const char * format,va_list args)8270Sstevel@tonic-gate int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
8280Sstevel@tonic-gate 	{
8290Sstevel@tonic-gate 	size_t retlen;
8300Sstevel@tonic-gate 	int truncated;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	_dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (truncated)
8350Sstevel@tonic-gate 		/* In case of truncation, return -1 like traditional snprintf.
8360Sstevel@tonic-gate 		 * (Current drafts for ISO/IEC 9899 say snprintf should return
8370Sstevel@tonic-gate 		 * the number of characters that would have been written,
8380Sstevel@tonic-gate 		 * had the buffer been large enough.) */
8390Sstevel@tonic-gate 		return -1;
8400Sstevel@tonic-gate 	else
8410Sstevel@tonic-gate 		return (retlen <= INT_MAX) ? (int)retlen : -1;
8420Sstevel@tonic-gate 	}
843