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 int fmtstr(char **, char **, size_t *, size_t *,
129 const char *, int, int, int);
130 static int fmtint(char **, char **, size_t *, size_t *,
131 LLONG, int, int, int, int);
132 static int fmtfp(char **, char **, size_t *, size_t *,
133 LDOUBLE, int, int, int);
134 static int doapr_outch(char **, char **, size_t *, size_t *, int);
135 static int _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)
169 _dopr(char **sbuffer,
172 size_t *retlen, int *truncated, const char *format, va_list args)
185 state = DP_S_DEFAULT;
186 flags = currlen = cflags = min = 0;
190 while (state != DP_S_DONE) {
191 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
199 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
231 if (isdigit((unsigned char)ch)) {
232 min = 10 * min + char_to_int(ch);
234 } else if (ch == '*') {
235 min = va_arg(args, int);
249 if (isdigit((unsigned char)ch)) {
252 max = 10 * max + char_to_int(ch);
254 } else if (ch == '*') {
255 max = va_arg(args, int);
268 if (*format == 'l') {
280 cflags = DP_C_LDOUBLE;
294 value = (short int)va_arg(args, int);
297 value = va_arg(args, long int);
300 value = va_arg(args, LLONG);
303 value = va_arg(args, int);
306 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
316 flags |= DP_F_UNSIGNED;
319 value = (unsigned short int)va_arg(args, unsigned int);
322 value = (LLONG) va_arg(args, unsigned long int);
325 value = va_arg(args, unsigned LLONG);
328 value = (LLONG) va_arg(args, unsigned int);
331 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
332 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
337 if (cflags == DP_C_LDOUBLE)
338 fvalue = va_arg(args, LDOUBLE);
340 fvalue = va_arg(args, double);
341 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
348 if (cflags == DP_C_LDOUBLE)
349 fvalue = va_arg(args, LDOUBLE);
351 fvalue = va_arg(args, double);
356 if (cflags == DP_C_LDOUBLE)
357 fvalue = va_arg(args, LDOUBLE);
359 fvalue = va_arg(args, double);
362 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
367 strvalue = va_arg(args, char *);
374 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
379 value = (long)va_arg(args, void *);
380 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
381 value, 16, min, max, flags | DP_F_NUM))
385 if (cflags == DP_C_SHORT) {
387 num = va_arg(args, short int *);
389 } else if (cflags == DP_C_LONG) { /* XXX */
391 num = va_arg(args, long int *);
392 *num = (long int)currlen;
393 } else if (cflags == DP_C_LLONG) { /* XXX */
395 num = va_arg(args, LLONG *);
396 *num = (LLONG) currlen;
399 num = va_arg(args, int *);
404 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
408 /* not supported yet, treat as next char */
416 state = DP_S_DEFAULT;
417 flags = cflags = min = 0;
427 * We have to truncate if there is no dynamic buffer and we have filled the
430 if (buffer == NULL) {
431 *truncated = (currlen > *maxlen - 1);
433 currlen = *maxlen - 1;
435 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
437 *retlen = currlen - 1;
442 fmtstr(char **sbuffer,
445 size_t *maxlen, const char *value, int flags, int min, int max)
454 strln = strlen(value);
458 padlen = min - strln;
459 if (min < 0 || padlen < 0)
461 if (flags & DP_F_MINUS)
464 while ((padlen > 0) && (cnt < max)) {
465 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
470 while (*value && (cnt < max)) {
471 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
475 while ((padlen < 0) && (cnt < max)) {
476 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
485 fmtint(char **sbuffer,
488 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
491 const char *prefix = "";
492 unsigned LLONG uvalue;
493 char convert[DECIMAL_SIZE(value) + 3];
502 if (!(flags & DP_F_UNSIGNED)) {
506 } else if (flags & DP_F_PLUS)
508 else if (flags & DP_F_SPACE)
511 if (flags & DP_F_NUM) {
520 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
521 [uvalue % (unsigned)base];
522 uvalue = (uvalue / (unsigned)base);
523 } while (uvalue && (place < (int)sizeof(convert)));
524 if (place == sizeof(convert))
528 zpadlen = max - place;
530 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
535 if (flags & DP_F_ZERO) {
536 zpadlen = OSSL_MAX(zpadlen, spadlen);
539 if (flags & DP_F_MINUS)
543 while (spadlen > 0) {
544 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
551 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
556 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
563 while (zpadlen > 0) {
564 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
571 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
575 /* left justified spaces */
576 while (spadlen < 0) {
577 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
584 static LDOUBLE abs_val(LDOUBLE value)
586 LDOUBLE result = value;
592 static LDOUBLE pow_10(int in_exp)
602 static long roundv(LDOUBLE value)
605 intpart = (long)value;
606 value = value - intpart;
613 fmtfp(char **sbuffer,
616 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
632 ufvalue = abs_val(fvalue);
635 else if (flags & DP_F_PLUS)
637 else if (flags & DP_F_SPACE)
640 intpart = (long)ufvalue;
643 * sorry, we only support 9 digits past the decimal because of our
650 * we "cheat" by converting the fractional part to integer by multiplying
653 max10 = roundv(pow_10(max));
654 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
656 if (fracpart >= max10) {
661 /* convert integer part */
663 iconvert[iplace++] = "0123456789"[intpart % 10];
664 intpart = (intpart / 10);
665 } while (intpart && (iplace < (int)sizeof(iconvert)));
666 if (iplace == sizeof iconvert)
668 iconvert[iplace] = 0;
670 /* convert fractional part */
672 fconvert[fplace++] = "0123456789"[fracpart % 10];
673 fracpart = (fracpart / 10);
674 } while (fplace < max);
675 if (fplace == sizeof fconvert)
677 fconvert[fplace] = 0;
679 /* -1 for decimal point, another -1 if we are printing a sign */
680 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
681 zpadlen = max - fplace;
686 if (flags & DP_F_MINUS)
689 if ((flags & DP_F_ZERO) && (padlen > 0)) {
691 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
697 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
703 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
707 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
711 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
716 * Decimal point. This should probably use locale to find the correct
719 if (max > 0 || (flags & DP_F_NUM)) {
720 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
724 if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
729 while (zpadlen > 0) {
730 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
736 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
743 #define BUFFER_INC 1024
746 doapr_outch(char **sbuffer,
747 char **buffer, size_t *currlen, size_t *maxlen, int c)
749 /* If we haven't at least one buffer, someone has doe a big booboo */
750 assert(*sbuffer != NULL || buffer != NULL);
752 /* |currlen| must always be <= |*maxlen| */
753 assert(*currlen <= *maxlen);
755 if (buffer && *currlen == *maxlen) {
756 if (*maxlen > INT_MAX - BUFFER_INC)
759 *maxlen += BUFFER_INC;
760 if (*buffer == NULL) {
761 *buffer = OPENSSL_malloc(*maxlen);
765 assert(*sbuffer != NULL);
766 memcpy(*buffer, *sbuffer, *currlen);
771 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
778 if (*currlen < *maxlen) {
780 (*sbuffer)[(*currlen)++] = (char)c;
782 (*buffer)[(*currlen)++] = (char)c;
788 /***************************************************************************/
790 int BIO_printf(BIO *bio, const char *format, ...)
795 va_start(args, format);
797 ret = BIO_vprintf(bio, format, args);
803 int BIO_vprintf(BIO *bio, const char *format, va_list args)
807 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
808 * in small-stack environments, like threads
809 * or DOS programs. */
810 char *hugebufp = hugebuf;
811 size_t hugebufsize = sizeof(hugebuf);
816 CRYPTO_push_info("doapr()");
817 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
819 OPENSSL_free(dynbuf);
823 ret = BIO_write(bio, dynbuf, (int)retlen);
824 OPENSSL_free(dynbuf);
826 ret = BIO_write(bio, hugebuf, (int)retlen);
833 * As snprintf is not available everywhere, we provide our own
834 * implementation. This function has nothing to do with BIOs, but it's
835 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
836 * function should be renamed, but to what?)
838 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
843 va_start(args, format);
845 ret = BIO_vsnprintf(buf, n, format, args);
851 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
856 if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
861 * In case of truncation, return -1 like traditional snprintf.
862 * (Current drafts for ISO/IEC 9899 say snprintf should return the
863 * number of characters that would have been written, had the buffer
864 * been large enough.)
868 return (retlen <= INT_MAX) ? (int)retlen : -1;