1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
58 /* disable assert() unless BIO_DEBUG has been defined */
66 * Stolen from tjh's ssl/ssl_trc.c stuff.
74 #include "internal/cryptlib.h"
75 #ifndef NO_SYS_TYPES_H
76 # include <sys/types.h>
78 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
79 #include <openssl/bio.h>
81 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
82 # ifndef HAVE_LONG_LONG
83 # define HAVE_LONG_LONG 1
87 /***************************************************************************/
90 * Copyright Patrick Powell 1995
91 * This code is based on code written by Patrick Powell <papowell@astart.com>
92 * It may be used for any purpose as long as this notice remains intact
93 * on all source code distributions.
97 * This code contains numerious changes and enhancements which were
98 * made by lots of contributors over the last years to Patrick Powell's
101 * o Patrick Powell <papowell@astart.com> (1995)
102 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
103 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
104 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
105 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
106 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
107 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
108 * o ... (for OpenSSL)
111 #ifdef HAVE_LONG_DOUBLE
112 # define LDOUBLE long double
114 # define LDOUBLE double
117 #ifdef HAVE_LONG_LONG
118 # if defined(_WIN32) && !defined(__GNUC__)
119 # define LLONG __int64
121 # define LLONG long long
127 static void fmtstr(char **, char **, size_t *, size_t *,
128 const char *, int, int, int);
129 static void fmtint(char **, char **, size_t *, size_t *,
130 LLONG, int, int, int, int);
131 static void fmtfp(char **, char **, size_t *, size_t *,
132 LDOUBLE, int, int, int);
133 static void doapr_outch(char **, char **, size_t *, size_t *, int);
134 static void _dopr(char **sbuffer, char **buffer,
135 size_t *maxlen, size_t *retlen, int *truncated,
136 const char *format, va_list args);
138 /* format read states */
139 #define DP_S_DEFAULT 0
148 /* format flags - Bits */
149 #define DP_F_MINUS (1 << 0)
150 #define DP_F_PLUS (1 << 1)
151 #define DP_F_SPACE (1 << 2)
152 #define DP_F_NUM (1 << 3)
153 #define DP_F_ZERO (1 << 4)
154 #define DP_F_UP (1 << 5)
155 #define DP_F_UNSIGNED (1 << 6)
157 /* conversion flags */
160 #define DP_C_LDOUBLE 3
163 /* some handy macros */
164 #define char_to_int(p) (p - '0')
165 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
168 _dopr(char **sbuffer,
171 size_t *retlen, int *truncated, const char *format, va_list args)
184 state = DP_S_DEFAULT;
185 flags = currlen = cflags = min = 0;
189 while (state != DP_S_DONE) {
190 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
198 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
229 if (isdigit((unsigned char)ch)) {
230 min = 10 * min + char_to_int(ch);
232 } else if (ch == '*') {
233 min = va_arg(args, int);
247 if (isdigit((unsigned char)ch)) {
250 max = 10 * max + char_to_int(ch);
252 } else if (ch == '*') {
253 max = va_arg(args, int);
266 if (*format == 'l') {
278 cflags = DP_C_LDOUBLE;
292 value = (short int)va_arg(args, int);
295 value = va_arg(args, long int);
298 value = va_arg(args, LLONG);
301 value = va_arg(args, int);
304 fmtint(sbuffer, buffer, &currlen, maxlen,
305 value, 10, min, max, flags);
313 flags |= DP_F_UNSIGNED;
316 value = (unsigned short int)va_arg(args, unsigned int);
319 value = (LLONG) va_arg(args, unsigned long int);
322 value = va_arg(args, unsigned LLONG);
325 value = (LLONG) va_arg(args, unsigned int);
328 fmtint(sbuffer, buffer, &currlen, maxlen, value,
329 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
333 if (cflags == DP_C_LDOUBLE)
334 fvalue = va_arg(args, LDOUBLE);
336 fvalue = va_arg(args, double);
337 fmtfp(sbuffer, buffer, &currlen, maxlen,
338 fvalue, min, max, flags);
343 if (cflags == DP_C_LDOUBLE)
344 fvalue = va_arg(args, LDOUBLE);
346 fvalue = va_arg(args, double);
351 if (cflags == DP_C_LDOUBLE)
352 fvalue = va_arg(args, LDOUBLE);
354 fvalue = va_arg(args, double);
357 doapr_outch(sbuffer, buffer, &currlen, maxlen,
361 strvalue = va_arg(args, char *);
368 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
372 value = (size_t)va_arg(args, void *);
373 fmtint(sbuffer, buffer, &currlen, maxlen,
374 value, 16, min, max, flags | DP_F_NUM);
377 if (cflags == DP_C_SHORT) {
379 num = va_arg(args, short int *);
381 } else if (cflags == DP_C_LONG) { /* XXX */
383 num = va_arg(args, long int *);
384 *num = (long int)currlen;
385 } else if (cflags == DP_C_LLONG) { /* XXX */
387 num = va_arg(args, LLONG *);
388 *num = (LLONG) currlen;
391 num = va_arg(args, int *);
396 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
399 /* not supported yet, treat as next char */
407 state = DP_S_DEFAULT;
408 flags = cflags = min = 0;
417 *truncated = (currlen > *maxlen - 1);
419 currlen = *maxlen - 1;
420 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
421 *retlen = currlen - 1;
426 fmtstr(char **sbuffer,
429 size_t *maxlen, const char *value, int flags, int min, int max)
436 for (strln = 0; value[strln]; ++strln) ;
437 padlen = min - strln;
440 if (flags & DP_F_MINUS)
443 while ((padlen > 0) && (cnt < max)) {
444 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
448 while (*value && (cnt < max)) {
449 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
452 while ((padlen < 0) && (cnt < max)) {
453 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
460 fmtint(char **sbuffer,
463 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
466 const char *prefix = "";
467 unsigned LLONG uvalue;
468 char convert[DECIMAL_SIZE(value) + 3];
477 if (!(flags & DP_F_UNSIGNED)) {
481 } else if (flags & DP_F_PLUS)
483 else if (flags & DP_F_SPACE)
486 if (flags & DP_F_NUM) {
495 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
496 [uvalue % (unsigned)base];
497 uvalue = (uvalue / (unsigned)base);
498 } while (uvalue && (place < (int)sizeof(convert)));
499 if (place == sizeof(convert))
503 zpadlen = max - place;
505 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
510 if (flags & DP_F_ZERO) {
511 zpadlen = OSSL_MAX(zpadlen, spadlen);
514 if (flags & DP_F_MINUS)
518 while (spadlen > 0) {
519 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
525 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
529 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
535 while (zpadlen > 0) {
536 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
542 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
544 /* left justified spaces */
545 while (spadlen < 0) {
546 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
552 static LDOUBLE abs_val(LDOUBLE value)
554 LDOUBLE result = value;
560 static LDOUBLE pow_10(int in_exp)
570 static long roundv(LDOUBLE value)
573 intpart = (long)value;
574 value = value - intpart;
581 fmtfp(char **sbuffer,
584 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
600 ufvalue = abs_val(fvalue);
603 else if (flags & DP_F_PLUS)
605 else if (flags & DP_F_SPACE)
608 intpart = (long)ufvalue;
611 * sorry, we only support 9 digits past the decimal because of our
618 * we "cheat" by converting the fractional part to integer by multiplying
621 max10 = roundv(pow_10(max));
622 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
624 if (fracpart >= max10) {
629 /* convert integer part */
631 iconvert[iplace++] = "0123456789"[intpart % 10];
632 intpart = (intpart / 10);
633 } while (intpart && (iplace < (int)sizeof(iconvert)));
634 if (iplace == sizeof iconvert)
636 iconvert[iplace] = 0;
638 /* convert fractional part */
640 fconvert[fplace++] = "0123456789"[fracpart % 10];
641 fracpart = (fracpart / 10);
642 } while (fplace < max);
643 if (fplace == sizeof fconvert)
645 fconvert[fplace] = 0;
647 /* -1 for decimal point, another -1 if we are printing a sign */
648 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
649 zpadlen = max - fplace;
654 if (flags & DP_F_MINUS)
657 if ((flags & DP_F_ZERO) && (padlen > 0)) {
659 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
664 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
669 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
673 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
676 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
679 * Decimal point. This should probably use locale to find the correct
682 if (max > 0 || (flags & DP_F_NUM)) {
683 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
686 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
688 while (zpadlen > 0) {
689 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
694 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
700 doapr_outch(char **sbuffer,
701 char **buffer, size_t *currlen, size_t *maxlen, int c)
703 /* If we haven't at least one buffer, someone has doe a big booboo */
704 assert(*sbuffer != NULL || buffer != NULL);
706 /* |currlen| must always be <= |*maxlen| */
707 assert(*currlen <= *maxlen);
709 if (buffer && *currlen == *maxlen) {
711 if (*buffer == NULL) {
712 *buffer = OPENSSL_malloc(*maxlen);
713 if (*buffer == NULL) {
714 /* Panic! Can't really do anything sensible. Just return */
718 assert(*sbuffer != NULL);
719 memcpy(*buffer, *sbuffer, *currlen);
723 *buffer = OPENSSL_realloc(*buffer, *maxlen);
725 /* Panic! Can't really do anything sensible. Just return */
731 if (*currlen < *maxlen) {
733 (*sbuffer)[(*currlen)++] = (char)c;
735 (*buffer)[(*currlen)++] = (char)c;
741 /***************************************************************************/
743 int BIO_printf(BIO *bio, const char *format, ...)
748 va_start(args, format);
750 ret = BIO_vprintf(bio, format, args);
756 int BIO_vprintf(BIO *bio, const char *format, va_list args)
760 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
761 * in small-stack environments, like threads
762 * or DOS programs. */
763 char *hugebufp = hugebuf;
764 size_t hugebufsize = sizeof(hugebuf);
769 _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, args);
771 ret = BIO_write(bio, dynbuf, (int)retlen);
772 OPENSSL_free(dynbuf);
774 ret = BIO_write(bio, hugebuf, (int)retlen);
780 * As snprintf is not available everywhere, we provide our own
781 * implementation. This function has nothing to do with BIOs, but it's
782 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
783 * function should be renamed, but to what?)
785 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
790 va_start(args, format);
792 ret = BIO_vsnprintf(buf, n, format, args);
798 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
803 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
807 * In case of truncation, return -1 like traditional snprintf.
808 * (Current drafts for ISO/IEC 9899 say snprintf should return the
809 * number of characters that would have been written, had the buffer
810 * been large enough.)
814 return (retlen <= INT_MAX) ? (int)retlen : -1;