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 int fmtstr(char **, char **, size_t *, size_t *,
128 const char *, int, int, int);
129 static int fmtint(char **, char **, size_t *, size_t *,
130 LLONG, int, int, int, int);
131 static int fmtfp(char **, char **, size_t *, size_t *,
132 LDOUBLE, int, int, int);
133 static int doapr_outch(char **, char **, size_t *, size_t *, int);
134 static int _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 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
230 if (isdigit((unsigned char)ch)) {
231 min = 10 * min + char_to_int(ch);
233 } else if (ch == '*') {
234 min = va_arg(args, int);
248 if (isdigit((unsigned char)ch)) {
251 max = 10 * max + char_to_int(ch);
253 } else if (ch == '*') {
254 max = va_arg(args, int);
267 if (*format == 'l') {
279 cflags = DP_C_LDOUBLE;
293 value = (short int)va_arg(args, int);
296 value = va_arg(args, long int);
299 value = va_arg(args, LLONG);
302 value = va_arg(args, int);
305 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
315 flags |= DP_F_UNSIGNED;
318 value = (unsigned short int)va_arg(args, unsigned int);
321 value = (LLONG) va_arg(args, unsigned long int);
324 value = va_arg(args, unsigned LLONG);
327 value = (LLONG) va_arg(args, unsigned int);
330 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
331 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
336 if (cflags == DP_C_LDOUBLE)
337 fvalue = va_arg(args, LDOUBLE);
339 fvalue = va_arg(args, double);
340 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
347 if (cflags == DP_C_LDOUBLE)
348 fvalue = va_arg(args, LDOUBLE);
350 fvalue = va_arg(args, double);
355 if (cflags == DP_C_LDOUBLE)
356 fvalue = va_arg(args, LDOUBLE);
358 fvalue = va_arg(args, double);
361 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
366 strvalue = va_arg(args, char *);
373 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
378 value = (size_t)va_arg(args, void *);
379 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
380 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 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
407 /* not supported yet, treat as next char */
415 state = DP_S_DEFAULT;
416 flags = cflags = min = 0;
425 *truncated = (currlen > *maxlen - 1);
427 currlen = *maxlen - 1;
428 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
430 *retlen = currlen - 1;
435 fmtstr(char **sbuffer,
438 size_t *maxlen, const char *value, int flags, int min, int max)
447 strln = strlen(value);
451 padlen = min - strln;
452 if (min < 0 || padlen < 0)
454 if (flags & DP_F_MINUS)
457 while ((padlen > 0) && (cnt < max)) {
458 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
463 while (*value && (cnt < max)) {
464 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
468 while ((padlen < 0) && (cnt < max)) {
469 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
478 fmtint(char **sbuffer,
481 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
484 const char *prefix = "";
485 unsigned LLONG uvalue;
486 char convert[DECIMAL_SIZE(value) + 3];
495 if (!(flags & DP_F_UNSIGNED)) {
499 } else if (flags & DP_F_PLUS)
501 else if (flags & DP_F_SPACE)
504 if (flags & DP_F_NUM) {
513 convert[place++] = (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;
523 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
528 if (flags & DP_F_ZERO) {
529 zpadlen = OSSL_MAX(zpadlen, spadlen);
532 if (flags & DP_F_MINUS)
536 while (spadlen > 0) {
537 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
544 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
549 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
556 while (zpadlen > 0) {
557 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
564 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
568 /* left justified spaces */
569 while (spadlen < 0) {
570 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
577 static LDOUBLE abs_val(LDOUBLE value)
579 LDOUBLE result = value;
585 static LDOUBLE pow_10(int in_exp)
595 static long roundv(LDOUBLE value)
598 intpart = (long)value;
599 value = value - intpart;
606 fmtfp(char **sbuffer,
609 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
625 ufvalue = abs_val(fvalue);
628 else if (flags & DP_F_PLUS)
630 else if (flags & DP_F_SPACE)
633 intpart = (long)ufvalue;
636 * sorry, we only support 9 digits past the decimal because of our
643 * we "cheat" by converting the fractional part to integer by multiplying
646 max10 = roundv(pow_10(max));
647 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
649 if (fracpart >= max10) {
654 /* convert integer part */
656 iconvert[iplace++] = "0123456789"[intpart % 10];
657 intpart = (intpart / 10);
658 } while (intpart && (iplace < (int)sizeof(iconvert)));
659 if (iplace == sizeof iconvert)
661 iconvert[iplace] = 0;
663 /* convert fractional part */
665 fconvert[fplace++] = "0123456789"[fracpart % 10];
666 fracpart = (fracpart / 10);
667 } while (fplace < max);
668 if (fplace == sizeof fconvert)
670 fconvert[fplace] = 0;
672 /* -1 for decimal point, another -1 if we are printing a sign */
673 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
674 zpadlen = max - fplace;
679 if (flags & DP_F_MINUS)
682 if ((flags & DP_F_ZERO) && (padlen > 0)) {
684 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
690 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
696 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
700 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
704 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
709 * Decimal point. This should probably use locale to find the correct
712 if (max > 0 || (flags & DP_F_NUM)) {
713 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
717 if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
722 while (zpadlen > 0) {
723 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
729 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
736 #define BUFFER_INC 1024
739 doapr_outch(char **sbuffer,
740 char **buffer, size_t *currlen, size_t *maxlen, int c)
742 /* If we haven't at least one buffer, someone has doe a big booboo */
743 assert(*sbuffer != NULL || buffer != NULL);
745 /* |currlen| must always be <= |*maxlen| */
746 assert(*currlen <= *maxlen);
748 if (buffer && *currlen == *maxlen) {
749 if (*maxlen > INT_MAX - BUFFER_INC)
752 *maxlen += BUFFER_INC;
753 if (*buffer == NULL) {
754 *buffer = OPENSSL_malloc(*maxlen);
758 assert(*sbuffer != NULL);
759 memcpy(*buffer, *sbuffer, *currlen);
764 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
771 if (*currlen < *maxlen) {
773 (*sbuffer)[(*currlen)++] = (char)c;
775 (*buffer)[(*currlen)++] = (char)c;
781 /***************************************************************************/
783 int BIO_printf(BIO *bio, const char *format, ...)
788 va_start(args, format);
790 ret = BIO_vprintf(bio, format, args);
796 int BIO_vprintf(BIO *bio, const char *format, va_list args)
800 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
801 * in small-stack environments, like threads
802 * or DOS programs. */
803 char *hugebufp = hugebuf;
804 size_t hugebufsize = sizeof(hugebuf);
809 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
811 OPENSSL_free(dynbuf);
815 ret = BIO_write(bio, dynbuf, (int)retlen);
816 OPENSSL_free(dynbuf);
818 ret = BIO_write(bio, hugebuf, (int)retlen);
824 * As snprintf is not available everywhere, we provide our own
825 * implementation. This function has nothing to do with BIOs, but it's
826 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
827 * function should be renamed, but to what?)
829 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
834 va_start(args, format);
836 ret = BIO_vsnprintf(buf, n, format, args);
842 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
847 if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
852 * In case of truncation, return -1 like traditional snprintf.
853 * (Current drafts for ISO/IEC 9899 say snprintf should return the
854 * number of characters that would have been written, had the buffer
855 * been large enough.)
859 return (retlen <= INT_MAX) ? (int)retlen : -1;