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.]
59 * Stolen from tjh's ssl/ssl_trc.c stuff.
67 #include "internal/cryptlib.h"
68 #ifndef NO_SYS_TYPES_H
69 # include <sys/types.h>
71 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
72 #include <openssl/bio.h>
74 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
75 # ifndef HAVE_LONG_LONG
76 # define HAVE_LONG_LONG 1
80 /***************************************************************************/
83 * Copyright Patrick Powell 1995
84 * This code is based on code written by Patrick Powell <papowell@astart.com>
85 * It may be used for any purpose as long as this notice remains intact
86 * on all source code distributions.
90 * This code contains numerious changes and enhancements which were
91 * made by lots of contributors over the last years to Patrick Powell's
94 * o Patrick Powell <papowell@astart.com> (1995)
95 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
96 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
97 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
98 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
99 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
100 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
101 * o ... (for OpenSSL)
104 #ifdef HAVE_LONG_DOUBLE
105 # define LDOUBLE long double
107 # define LDOUBLE double
110 #ifdef HAVE_LONG_LONG
111 # if defined(_WIN32) && !defined(__GNUC__)
112 # define LLONG __int64
114 # define LLONG long long
120 static int fmtstr(char **, char **, size_t *, size_t *,
121 const char *, int, int, int);
122 static int fmtint(char **, char **, size_t *, size_t *,
123 LLONG, int, int, int, int);
124 static int fmtfp(char **, char **, size_t *, size_t *,
125 LDOUBLE, int, int, int);
126 static int doapr_outch(char **, char **, size_t *, size_t *, int);
127 static int _dopr(char **sbuffer, char **buffer,
128 size_t *maxlen, size_t *retlen, int *truncated,
129 const char *format, va_list args);
131 /* format read states */
132 #define DP_S_DEFAULT 0
141 /* format flags - Bits */
142 #define DP_F_MINUS (1 << 0)
143 #define DP_F_PLUS (1 << 1)
144 #define DP_F_SPACE (1 << 2)
145 #define DP_F_NUM (1 << 3)
146 #define DP_F_ZERO (1 << 4)
147 #define DP_F_UP (1 << 5)
148 #define DP_F_UNSIGNED (1 << 6)
150 /* conversion flags */
153 #define DP_C_LDOUBLE 3
156 /* some handy macros */
157 #define char_to_int(p) (p - '0')
158 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
161 _dopr(char **sbuffer,
164 size_t *retlen, int *truncated, const char *format, va_list args)
177 state = DP_S_DEFAULT;
178 flags = currlen = cflags = min = 0;
182 while (state != DP_S_DONE) {
183 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
191 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
223 if (isdigit((unsigned char)ch)) {
224 min = 10 * min + char_to_int(ch);
226 } else if (ch == '*') {
227 min = va_arg(args, int);
241 if (isdigit((unsigned char)ch)) {
244 max = 10 * max + char_to_int(ch);
246 } else if (ch == '*') {
247 max = va_arg(args, int);
260 if (*format == 'l') {
272 cflags = DP_C_LDOUBLE;
286 value = (short int)va_arg(args, int);
289 value = va_arg(args, long int);
292 value = va_arg(args, LLONG);
295 value = va_arg(args, int);
298 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
308 flags |= DP_F_UNSIGNED;
311 value = (unsigned short int)va_arg(args, unsigned int);
314 value = (LLONG) va_arg(args, unsigned long int);
317 value = va_arg(args, unsigned LLONG);
320 value = (LLONG) va_arg(args, unsigned int);
323 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
324 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
329 if (cflags == DP_C_LDOUBLE)
330 fvalue = va_arg(args, LDOUBLE);
332 fvalue = va_arg(args, double);
333 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
340 if (cflags == DP_C_LDOUBLE)
341 fvalue = va_arg(args, LDOUBLE);
343 fvalue = va_arg(args, double);
348 if (cflags == DP_C_LDOUBLE)
349 fvalue = va_arg(args, LDOUBLE);
351 fvalue = va_arg(args, double);
354 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
359 strvalue = va_arg(args, char *);
366 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
371 value = (size_t)va_arg(args, void *);
372 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
373 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 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
400 /* not supported yet, treat as next char */
408 state = DP_S_DEFAULT;
409 flags = cflags = min = 0;
418 *truncated = (currlen > *maxlen - 1);
420 currlen = *maxlen - 1;
421 if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
423 *retlen = currlen - 1;
428 fmtstr(char **sbuffer,
431 size_t *maxlen, const char *value, int flags, int min, int max)
440 strln = strlen(value);
444 padlen = min - strln;
445 if (min < 0 || padlen < 0)
447 if (flags & DP_F_MINUS)
450 while ((padlen > 0) && (cnt < max)) {
451 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
456 while (*value && (cnt < max)) {
457 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
461 while ((padlen < 0) && (cnt < max)) {
462 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
471 fmtint(char **sbuffer,
474 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
477 const char *prefix = "";
478 unsigned LLONG uvalue;
479 char convert[DECIMAL_SIZE(value) + 3];
488 if (!(flags & DP_F_UNSIGNED)) {
492 } else if (flags & DP_F_PLUS)
494 else if (flags & DP_F_SPACE)
497 if (flags & DP_F_NUM) {
506 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
507 [uvalue % (unsigned)base];
508 uvalue = (uvalue / (unsigned)base);
509 } while (uvalue && (place < (int)sizeof(convert)));
510 if (place == sizeof(convert))
514 zpadlen = max - place;
516 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
521 if (flags & DP_F_ZERO) {
522 zpadlen = OSSL_MAX(zpadlen, spadlen);
525 if (flags & DP_F_MINUS)
529 while (spadlen > 0) {
530 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
537 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
542 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
549 while (zpadlen > 0) {
550 if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
557 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
561 /* left justified spaces */
562 while (spadlen < 0) {
563 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
570 static LDOUBLE abs_val(LDOUBLE value)
572 LDOUBLE result = value;
578 static LDOUBLE pow_10(int in_exp)
588 static long roundv(LDOUBLE value)
591 intpart = (long)value;
592 value = value - intpart;
599 fmtfp(char **sbuffer,
602 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
618 ufvalue = abs_val(fvalue);
621 else if (flags & DP_F_PLUS)
623 else if (flags & DP_F_SPACE)
626 intpart = (long)ufvalue;
629 * sorry, we only support 9 digits past the decimal because of our
636 * we "cheat" by converting the fractional part to integer by multiplying
639 max10 = roundv(pow_10(max));
640 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
642 if (fracpart >= max10) {
647 /* convert integer part */
649 iconvert[iplace++] = "0123456789"[intpart % 10];
650 intpart = (intpart / 10);
651 } while (intpart && (iplace < (int)sizeof(iconvert)));
652 if (iplace == sizeof iconvert)
654 iconvert[iplace] = 0;
656 /* convert fractional part */
658 fconvert[fplace++] = "0123456789"[fracpart % 10];
659 fracpart = (fracpart / 10);
660 } while (fplace < max);
661 if (fplace == sizeof fconvert)
663 fconvert[fplace] = 0;
665 /* -1 for decimal point, another -1 if we are printing a sign */
666 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
667 zpadlen = max - fplace;
672 if (flags & DP_F_MINUS)
675 if ((flags & DP_F_ZERO) && (padlen > 0)) {
677 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
683 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
689 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
693 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
697 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
702 * Decimal point. This should probably use locale to find the correct
705 if (max > 0 || (flags & DP_F_NUM)) {
706 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
710 if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
715 while (zpadlen > 0) {
716 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
722 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
729 #define BUFFER_INC 1024
732 doapr_outch(char **sbuffer,
733 char **buffer, size_t *currlen, size_t *maxlen, int c)
735 /* If we haven't at least one buffer, someone has doe a big booboo */
736 assert(*sbuffer != NULL || buffer != NULL);
738 /* |currlen| must always be <= |*maxlen| */
739 assert(*currlen <= *maxlen);
741 if (buffer && *currlen == *maxlen) {
742 if (*maxlen > INT_MAX - BUFFER_INC)
745 *maxlen += BUFFER_INC;
746 if (*buffer == NULL) {
747 *buffer = OPENSSL_malloc(*maxlen);
751 assert(*sbuffer != NULL);
752 memcpy(*buffer, *sbuffer, *currlen);
757 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
764 if (*currlen < *maxlen) {
766 (*sbuffer)[(*currlen)++] = (char)c;
768 (*buffer)[(*currlen)++] = (char)c;
774 /***************************************************************************/
776 int BIO_printf(BIO *bio, const char *format, ...)
781 va_start(args, format);
783 ret = BIO_vprintf(bio, format, args);
789 int BIO_vprintf(BIO *bio, const char *format, va_list args)
793 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
794 * in small-stack environments, like threads
795 * or DOS programs. */
796 char *hugebufp = hugebuf;
797 size_t hugebufsize = sizeof(hugebuf);
802 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
804 OPENSSL_free(dynbuf);
808 ret = BIO_write(bio, dynbuf, (int)retlen);
809 OPENSSL_free(dynbuf);
811 ret = BIO_write(bio, hugebuf, (int)retlen);
817 * As snprintf is not available everywhere, we provide our own
818 * implementation. This function has nothing to do with BIOs, but it's
819 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
820 * function should be renamed, but to what?)
822 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
827 va_start(args, format);
829 ret = BIO_vsnprintf(buf, n, format, args);
835 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
840 if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
845 * In case of truncation, return -1 like traditional snprintf.
846 * (Current drafts for ISO/IEC 9899 say snprintf should return the
847 * number of characters that would have been written, had the buffer
848 * been large enough.)
852 return (retlen <= INT_MAX) ? (int)retlen : -1;