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.]
60 * Stolen from tjh's ssl/ssl_trc.c stuff.
69 #ifndef NO_SYS_TYPES_H
70 #include <sys/types.h>
72 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
73 #include <openssl/bio.h>
76 # ifndef HAVE_LONG_LONG
77 # define HAVE_LONG_LONG 1
81 /***************************************************************************/
84 * Copyright Patrick Powell 1995
85 * This code is based on code written by Patrick Powell <papowell@astart.com>
86 * It may be used for any purpose as long as this notice remains intact
87 * on all source code distributions.
91 * This code contains numerious changes and enhancements which were
92 * made by lots of contributors over the last years to Patrick Powell's
95 * o Patrick Powell <papowell@astart.com> (1995)
96 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
97 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
98 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
99 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
100 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
101 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
102 * o ... (for OpenSSL)
106 #define LDOUBLE long double
108 #define LDOUBLE double
112 # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
113 # define LLONG _int64
115 # define LLONG long long
121 static void fmtstr (char **, char **, size_t *, size_t *,
122 const char *, int, int, int);
123 static void fmtint (char **, char **, size_t *, size_t *,
124 LLONG, int, int, int, int);
125 static void fmtfp (char **, char **, size_t *, size_t *,
126 LDOUBLE, int, int, int);
127 static void doapr_outch (char **, char **, size_t *, size_t *, int);
128 static void _dopr(char **sbuffer, char **buffer,
129 size_t *maxlen, size_t *retlen, int *truncated,
130 const char *format, va_list args);
132 /* format read states */
133 #define DP_S_DEFAULT 0
142 /* format flags - Bits */
143 #define DP_F_MINUS (1 << 0)
144 #define DP_F_PLUS (1 << 1)
145 #define DP_F_SPACE (1 << 2)
146 #define DP_F_NUM (1 << 3)
147 #define DP_F_ZERO (1 << 4)
148 #define DP_F_UP (1 << 5)
149 #define DP_F_UNSIGNED (1 << 6)
151 /* conversion flags */
154 #define DP_C_LDOUBLE 3
157 /* some handy macros */
158 #define char_to_int(p) (p - '0')
159 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
182 state = DP_S_DEFAULT;
183 flags = currlen = cflags = min = 0;
187 while (state != DP_S_DONE) {
188 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
196 doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
227 if (isdigit((unsigned char)ch)) {
228 min = 10 * min + char_to_int(ch);
230 } else if (ch == '*') {
231 min = va_arg(args, int);
245 if (isdigit((unsigned char)ch)) {
248 max = 10 * max + char_to_int(ch);
250 } else if (ch == '*') {
251 max = va_arg(args, int);
264 if (*format == 'l') {
276 cflags = DP_C_LDOUBLE;
290 value = (short int)va_arg(args, int);
293 value = va_arg(args, long int);
296 value = va_arg(args, LLONG);
299 value = va_arg(args, int);
302 fmtint(sbuffer, buffer, &currlen, maxlen,
303 value, 10, min, max, flags);
311 flags |= DP_F_UNSIGNED;
314 value = (unsigned short int)va_arg(args, unsigned int);
317 value = (LLONG) va_arg(args,
321 value = va_arg(args, unsigned LLONG);
324 value = (LLONG) va_arg(args,
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 = (long)va_arg(args, void *);
373 fmtint(sbuffer, buffer, &currlen, maxlen,
374 value, 16, min, max, flags);
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;
441 for (strln = 0; value[strln]; ++strln)
443 padlen = min - strln;
446 if (flags & DP_F_MINUS)
449 while ((padlen > 0) && (cnt < max)) {
450 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
454 while (*value && (cnt < max)) {
455 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
458 while ((padlen < 0) && (cnt < max)) {
459 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
478 unsigned LLONG uvalue;
488 if (!(flags & DP_F_UNSIGNED)) {
492 } else if (flags & DP_F_PLUS)
494 else if (flags & DP_F_SPACE)
501 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
502 [uvalue % (unsigned) base];
503 uvalue = (uvalue / (unsigned) base);
504 } while (uvalue && (place < 20));
509 zpadlen = max - place;
510 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0);
515 if (flags & DP_F_ZERO) {
516 zpadlen = OSSL_MAX(zpadlen, spadlen);
519 if (flags & DP_F_MINUS)
523 while (spadlen > 0) {
524 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
530 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
534 while (zpadlen > 0) {
535 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
541 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
543 /* left justified spaces */
544 while (spadlen < 0) {
545 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
552 abs_val(LDOUBLE value)
554 LDOUBLE result = value;
572 roundv(LDOUBLE value)
575 intpart = (long) value;
576 value = value - intpart;
607 ufvalue = abs_val(fvalue);
610 else if (flags & DP_F_PLUS)
612 else if (flags & DP_F_SPACE)
615 intpart = (long)ufvalue;
617 /* sorry, we only support 9 digits past the decimal because of our
622 /* we "cheat" by converting the fractional part to integer by
623 multiplying by a factor of 10 */
624 fracpart = roundv((pow10(max)) * (ufvalue - intpart));
626 if (fracpart >= pow10(max)) {
628 fracpart -= (long)pow10(max);
631 /* convert integer part */
634 (caps ? "0123456789ABCDEF"
635 : "0123456789abcdef")[intpart % 10];
636 intpart = (intpart / 10);
637 } while (intpart && (iplace < 20));
640 iconvert[iplace] = 0;
642 /* convert fractional part */
645 (caps ? "0123456789ABCDEF"
646 : "0123456789abcdef")[fracpart % 10];
647 fracpart = (fracpart / 10);
648 } while (fplace < max);
651 fconvert[fplace] = 0;
653 /* -1 for decimal point, another -1 if we are printing a sign */
654 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
655 zpadlen = max - fplace;
660 if (flags & DP_F_MINUS)
663 if ((flags & DP_F_ZERO) && (padlen > 0)) {
665 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
670 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
675 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
679 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
682 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
685 * Decimal point. This should probably use locale to find the correct
689 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
692 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
694 while (zpadlen > 0) {
695 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
700 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
713 /* If we haven't at least one buffer, someone has doe a big booboo */
714 assert(*sbuffer != NULL || buffer != NULL);
717 while (*currlen >= *maxlen) {
718 if (*buffer == NULL) {
721 *buffer = OPENSSL_malloc(*maxlen);
723 assert(*sbuffer != NULL);
724 memcpy(*buffer, *sbuffer, *currlen);
729 *buffer = OPENSSL_realloc(*buffer, *maxlen);
732 /* What to do if *buffer is NULL? */
733 assert(*sbuffer != NULL || *buffer != NULL);
736 if (*currlen < *maxlen) {
738 (*sbuffer)[(*currlen)++] = (char)c;
740 (*buffer)[(*currlen)++] = (char)c;
746 /***************************************************************************/
748 int BIO_printf (BIO *bio, const char *format, ...)
753 va_start(args, format);
755 ret = BIO_vprintf(bio, format, args);
761 int BIO_vprintf (BIO *bio, const char *format, va_list args)
765 MS_STATIC char hugebuf[1024*10];
766 char *hugebufp = hugebuf;
767 size_t hugebufsize = sizeof(hugebuf);
772 CRYPTO_push_info("doapr()");
773 _dopr(&hugebufp, &dynbuf, &hugebufsize,
774 &retlen, &ignored, format, args);
777 ret=BIO_write(bio, dynbuf, (int)retlen);
778 OPENSSL_free(dynbuf);
782 ret=BIO_write(bio, hugebuf, (int)retlen);
788 /* As snprintf is not available everywhere, we provide our own implementation.
789 * This function has nothing to do with BIOs, but it's closely related
790 * to BIO_printf, and we need *some* name prefix ...
791 * (XXX the function should be renamed, but to what?) */
792 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
797 va_start(args, format);
799 ret = BIO_vsnprintf(buf, n, format, args);
805 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
810 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
813 /* In case of truncation, return -1 like traditional snprintf.
814 * (Current drafts for ISO/IEC 9899 say snprintf should return
815 * the number of characters that would have been written,
816 * had the buffer been large enough.) */
819 return (retlen <= INT_MAX) ? retlen : -1;