1 /* crypto/bio/b_print.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 /* disable assert() unless BIO_DEBUG has been defined */
67 * Stolen from tjh's ssl/ssl_trc.c stuff.
76 #ifndef NO_SYS_TYPES_H
77 #include <sys/types.h>
79 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
80 #include <openssl/bio.h>
82 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
83 # ifndef HAVE_LONG_LONG
84 # define HAVE_LONG_LONG 1
88 /***************************************************************************/
91 * Copyright Patrick Powell 1995
92 * This code is based on code written by Patrick Powell <papowell@astart.com>
93 * It may be used for any purpose as long as this notice remains intact
94 * on all source code distributions.
98 * This code contains numerious changes and enhancements which were
99 * made by lots of contributors over the last years to Patrick Powell's
102 * o Patrick Powell <papowell@astart.com> (1995)
103 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
104 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
105 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
106 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
107 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
108 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
109 * o ... (for OpenSSL)
112 #ifdef HAVE_LONG_DOUBLE
113 #define LDOUBLE long double
115 #define LDOUBLE double
118 #ifdef HAVE_LONG_LONG
119 # if defined(_WIN32) && !defined(__GNUC__)
120 # define LLONG __int64
122 # define LLONG long long
128 static void fmtstr (char **, char **, size_t *, size_t *,
129 const char *, int, int, int);
130 static void fmtint (char **, char **, size_t *, size_t *,
131 LLONG
, int, int, int, int);
132 static void fmtfp (char **, char **, size_t *, size_t *,
133 LDOUBLE
, int, int, int);
134 static void doapr_outch (char **, char **, size_t *, size_t *, int);
135 static void _dopr(char **sbuffer
, char **buffer
,
136 size_t *maxlen
, size_t *retlen
, int *truncated
,
137 const char *format
, va_list args
);
139 /* format read states */
140 #define DP_S_DEFAULT 0
149 /* format flags - Bits */
150 #define DP_F_MINUS (1 << 0)
151 #define DP_F_PLUS (1 << 1)
152 #define DP_F_SPACE (1 << 2)
153 #define DP_F_NUM (1 << 3)
154 #define DP_F_ZERO (1 << 4)
155 #define DP_F_UP (1 << 5)
156 #define DP_F_UNSIGNED (1 << 6)
158 /* conversion flags */
161 #define DP_C_LDOUBLE 3
164 /* some handy macros */
165 #define char_to_int(p) (p - '0')
166 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
189 state
= DP_S_DEFAULT
;
190 flags
= currlen
= cflags
= min
= 0;
194 while (state
!= DP_S_DONE
) {
195 if (ch
== '\0' || (buffer
== NULL
&& currlen
>= *maxlen
))
203 doapr_outch(sbuffer
,buffer
, &currlen
, maxlen
, ch
);
234 if (isdigit((unsigned char)ch
)) {
235 min
= 10 * min
+ char_to_int(ch
);
237 } else if (ch
== '*') {
238 min
= va_arg(args
, int);
252 if (isdigit((unsigned char)ch
)) {
255 max
= 10 * max
+ char_to_int(ch
);
257 } else if (ch
== '*') {
258 max
= va_arg(args
, int);
271 if (*format
== 'l') {
283 cflags
= DP_C_LDOUBLE
;
297 value
= (short int)va_arg(args
, int);
300 value
= va_arg(args
, long int);
303 value
= va_arg(args
, LLONG
);
306 value
= va_arg(args
, int);
309 fmtint(sbuffer
, buffer
, &currlen
, maxlen
,
310 value
, 10, min
, max
, flags
);
318 flags
|= DP_F_UNSIGNED
;
321 value
= (unsigned short int)va_arg(args
, unsigned int);
324 value
= (LLONG
) va_arg(args
,
328 value
= va_arg(args
, unsigned LLONG
);
331 value
= (LLONG
) va_arg(args
,
335 fmtint(sbuffer
, buffer
, &currlen
, maxlen
, value
,
336 ch
== 'o' ? 8 : (ch
== 'u' ? 10 : 16),
340 if (cflags
== DP_C_LDOUBLE
)
341 fvalue
= va_arg(args
, LDOUBLE
);
343 fvalue
= va_arg(args
, double);
344 fmtfp(sbuffer
, buffer
, &currlen
, maxlen
,
345 fvalue
, min
, max
, flags
);
350 if (cflags
== DP_C_LDOUBLE
)
351 fvalue
= va_arg(args
, LDOUBLE
);
353 fvalue
= va_arg(args
, double);
358 if (cflags
== DP_C_LDOUBLE
)
359 fvalue
= va_arg(args
, LDOUBLE
);
361 fvalue
= va_arg(args
, double);
364 doapr_outch(sbuffer
, buffer
, &currlen
, maxlen
,
368 strvalue
= va_arg(args
, char *);
375 fmtstr(sbuffer
, buffer
, &currlen
, maxlen
, strvalue
,
379 value
= (long)va_arg(args
, void *);
380 fmtint(sbuffer
, buffer
, &currlen
, maxlen
,
381 value
, 16, min
, max
, flags
|DP_F_NUM
);
384 if (cflags
== DP_C_SHORT
) {
386 num
= va_arg(args
, short int *);
388 } else if (cflags
== DP_C_LONG
) { /* XXX */
390 num
= va_arg(args
, long int *);
391 *num
= (long int) currlen
;
392 } else if (cflags
== DP_C_LLONG
) { /* XXX */
394 num
= va_arg(args
, LLONG
*);
395 *num
= (LLONG
) currlen
;
398 num
= va_arg(args
, int *);
403 doapr_outch(sbuffer
, buffer
, &currlen
, maxlen
, ch
);
406 /* not supported yet, treat as next char */
414 state
= DP_S_DEFAULT
;
415 flags
= cflags
= min
= 0;
424 *truncated
= (currlen
> *maxlen
- 1);
426 currlen
= *maxlen
- 1;
427 doapr_outch(sbuffer
, buffer
, &currlen
, maxlen
, '\0');
428 *retlen
= currlen
- 1;
448 for (strln
= 0; value
[strln
]; ++strln
)
450 padlen
= min
- strln
;
453 if (flags
& DP_F_MINUS
)
456 while ((padlen
> 0) && (cnt
< max
)) {
457 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
461 while (*value
&& (cnt
< max
)) {
462 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, *value
++);
465 while ((padlen
< 0) && (cnt
< max
)) {
466 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
485 const char *prefix
= "";
486 unsigned LLONG uvalue
;
487 char convert
[DECIMAL_SIZE(value
)+3];
496 if (!(flags
& DP_F_UNSIGNED
)) {
500 } else if (flags
& DP_F_PLUS
)
502 else if (flags
& DP_F_SPACE
)
505 if (flags
& DP_F_NUM
) {
506 if (base
== 8) prefix
= "0";
507 if (base
== 16) prefix
= "0x";
513 (caps
? "0123456789ABCDEF" : "0123456789abcdef")
514 [uvalue
% (unsigned) base
];
515 uvalue
= (uvalue
/ (unsigned) base
);
516 } while (uvalue
&& (place
< (int)sizeof(convert
)));
517 if (place
== sizeof(convert
))
521 zpadlen
= max
- place
;
522 spadlen
= min
- OSSL_MAX(max
, place
) - (signvalue
? 1 : 0) - strlen(prefix
);
527 if (flags
& DP_F_ZERO
) {
528 zpadlen
= OSSL_MAX(zpadlen
, spadlen
);
531 if (flags
& DP_F_MINUS
)
535 while (spadlen
> 0) {
536 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
542 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, signvalue
);
546 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, *prefix
);
552 while (zpadlen
> 0) {
553 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, '0');
559 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, convert
[--place
]);
561 /* left justified spaces */
562 while (spadlen
< 0) {
563 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
570 abs_val(LDOUBLE value
)
572 LDOUBLE result
= value
;
590 roundv(LDOUBLE value
)
593 intpart
= (long) value
;
594 value
= value
- intpart
;
626 ufvalue
= abs_val(fvalue
);
629 else if (flags
& DP_F_PLUS
)
631 else if (flags
& DP_F_SPACE
)
634 intpart
= (long)ufvalue
;
636 /* sorry, we only support 9 digits past the decimal because of our
641 /* we "cheat" by converting the fractional part to integer by
642 multiplying by a factor of 10 */
643 max10
= roundv(pow_10(max
));
644 fracpart
= roundv(pow_10(max
) * (ufvalue
- intpart
));
646 if (fracpart
>= max10
) {
651 /* convert integer part */
654 (caps
? "0123456789ABCDEF"
655 : "0123456789abcdef")[intpart
% 10];
656 intpart
= (intpart
/ 10);
657 } while (intpart
&& (iplace
< (int)sizeof(iconvert
)));
658 if (iplace
== sizeof iconvert
)
660 iconvert
[iplace
] = 0;
662 /* convert fractional part */
665 (caps
? "0123456789ABCDEF"
666 : "0123456789abcdef")[fracpart
% 10];
667 fracpart
= (fracpart
/ 10);
668 } while (fplace
< max
);
669 if (fplace
== sizeof fconvert
)
671 fconvert
[fplace
] = 0;
673 /* -1 for decimal point, another -1 if we are printing a sign */
674 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
675 zpadlen
= max
- fplace
;
680 if (flags
& DP_F_MINUS
)
683 if ((flags
& DP_F_ZERO
) && (padlen
> 0)) {
685 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, signvalue
);
690 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, '0');
695 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
699 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, signvalue
);
702 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
705 * Decimal point. This should probably use locale to find the correct
708 if (max
> 0 || (flags
& DP_F_NUM
)) {
709 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, '.');
712 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
714 while (zpadlen
> 0) {
715 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, '0');
720 doapr_outch(sbuffer
, buffer
, currlen
, maxlen
, ' ');
733 /* If we haven't at least one buffer, someone has doe a big booboo */
734 assert(*sbuffer
!= NULL
|| buffer
!= NULL
);
737 while (*currlen
>= *maxlen
) {
738 if (*buffer
== NULL
) {
741 *buffer
= OPENSSL_malloc(*maxlen
);
743 assert(*sbuffer
!= NULL
);
744 memcpy(*buffer
, *sbuffer
, *currlen
);
749 *buffer
= OPENSSL_realloc(*buffer
, *maxlen
);
752 /* What to do if *buffer is NULL? */
753 assert(*sbuffer
!= NULL
|| *buffer
!= NULL
);
756 if (*currlen
< *maxlen
) {
758 (*sbuffer
)[(*currlen
)++] = (char)c
;
760 (*buffer
)[(*currlen
)++] = (char)c
;
766 /***************************************************************************/
768 int BIO_printf (BIO
*bio
, const char *format
, ...)
773 va_start(args
, format
);
775 ret
= BIO_vprintf(bio
, format
, args
);
781 int BIO_vprintf (BIO
*bio
, const char *format
, va_list args
)
785 char hugebuf
[1024*2]; /* Was previously 10k, which is unreasonable
786 in small-stack environments, like threads
788 char *hugebufp
= hugebuf
;
789 size_t hugebufsize
= sizeof(hugebuf
);
794 CRYPTO_push_info("doapr()");
795 _dopr(&hugebufp
, &dynbuf
, &hugebufsize
,
796 &retlen
, &ignored
, format
, args
);
799 ret
=BIO_write(bio
, dynbuf
, (int)retlen
);
800 OPENSSL_free(dynbuf
);
804 ret
=BIO_write(bio
, hugebuf
, (int)retlen
);
810 /* As snprintf is not available everywhere, we provide our own implementation.
811 * This function has nothing to do with BIOs, but it's closely related
812 * to BIO_printf, and we need *some* name prefix ...
813 * (XXX the function should be renamed, but to what?) */
814 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
819 va_start(args
, format
);
821 ret
= BIO_vsnprintf(buf
, n
, format
, args
);
827 int BIO_vsnprintf(char *buf
, size_t n
, const char *format
, va_list args
)
832 _dopr(&buf
, NULL
, &n
, &retlen
, &truncated
, format
, args
);
835 /* In case of truncation, return -1 like traditional snprintf.
836 * (Current drafts for ISO/IEC 9899 say snprintf should return
837 * the number of characters that would have been written,
838 * had the buffer been large enough.) */
841 return (retlen
<= INT_MAX
) ? (int)retlen
: -1;