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 #define LLONG long long
117 static void fmtstr (char **, char **, size_t *, size_t *,
118 const char *, int, int, int);
119 static void fmtint (char **, char **, size_t *, size_t *,
120 LLONG, int, int, int, int);
121 static void fmtfp (char **, char **, size_t *, size_t *,
122 LDOUBLE, int, int, int);
123 static void doapr_outch (char **, char **, size_t *, size_t *, int);
124 static void _dopr(char **sbuffer, char **buffer,
125 size_t *maxlen, size_t *retlen, int *truncated,
126 const char *format, va_list args);
128 /* format read states */
129 #define DP_S_DEFAULT 0
138 /* format flags - Bits */
139 #define DP_F_MINUS (1 << 0)
140 #define DP_F_PLUS (1 << 1)
141 #define DP_F_SPACE (1 << 2)
142 #define DP_F_NUM (1 << 3)
143 #define DP_F_ZERO (1 << 4)
144 #define DP_F_UP (1 << 5)
145 #define DP_F_UNSIGNED (1 << 6)
147 /* conversion flags */
150 #define DP_C_LDOUBLE 3
153 /* some handy macros */
154 #define char_to_int(p) (p - '0')
155 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
178 state = DP_S_DEFAULT;
179 flags = currlen = cflags = min = 0;
183 while (state != DP_S_DONE) {
184 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
192 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 fmtint(sbuffer, buffer, &currlen, maxlen,
299 value, 10, min, max, flags);
307 flags |= DP_F_UNSIGNED;
310 value = (unsigned short int)va_arg(args, unsigned int);
313 value = (LLONG) va_arg(args,
317 value = va_arg(args, unsigned LLONG);
320 value = (LLONG) va_arg(args,
324 fmtint(sbuffer, buffer, &currlen, maxlen, value,
325 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 fmtfp(sbuffer, buffer, &currlen, maxlen,
334 fvalue, min, max, flags);
339 if (cflags == DP_C_LDOUBLE)
340 fvalue = va_arg(args, LDOUBLE);
342 fvalue = va_arg(args, double);
347 if (cflags == DP_C_LDOUBLE)
348 fvalue = va_arg(args, LDOUBLE);
350 fvalue = va_arg(args, double);
353 doapr_outch(sbuffer, buffer, &currlen, maxlen,
357 strvalue = va_arg(args, char *);
364 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
368 value = (long)va_arg(args, void *);
369 fmtint(sbuffer, buffer, &currlen, maxlen,
370 value, 16, min, max, flags);
373 if (cflags == DP_C_SHORT) {
375 num = va_arg(args, short int *);
377 } else if (cflags == DP_C_LONG) { /* XXX */
379 num = va_arg(args, long int *);
380 *num = (long int) currlen;
381 } else if (cflags == DP_C_LLONG) { /* XXX */
383 num = va_arg(args, LLONG *);
384 *num = (LLONG) currlen;
387 num = va_arg(args, int *);
392 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
395 /* not supported yet, treat as next char */
403 state = DP_S_DEFAULT;
404 flags = cflags = min = 0;
413 *truncated = (currlen > *maxlen - 1);
415 currlen = *maxlen - 1;
416 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
417 *retlen = currlen - 1;
437 for (strln = 0; value[strln]; ++strln)
439 padlen = min - strln;
442 if (flags & DP_F_MINUS)
445 while ((padlen > 0) && (cnt < max)) {
446 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
450 while (*value && (cnt < max)) {
451 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
454 while ((padlen < 0) && (cnt < max)) {
455 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
474 unsigned LLONG uvalue;
484 if (!(flags & DP_F_UNSIGNED)) {
488 } else if (flags & DP_F_PLUS)
490 else if (flags & DP_F_SPACE)
497 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
498 [uvalue % (unsigned) base];
499 uvalue = (uvalue / (unsigned) base);
500 } while (uvalue && (place < 20));
505 zpadlen = max - place;
506 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0);
511 if (flags & DP_F_ZERO) {
512 zpadlen = OSSL_MAX(zpadlen, spadlen);
515 if (flags & DP_F_MINUS)
519 while (spadlen > 0) {
520 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
526 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
530 while (zpadlen > 0) {
531 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
537 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
539 /* left justified spaces */
540 while (spadlen < 0) {
541 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
548 abs_val(LDOUBLE value)
550 LDOUBLE result = value;
571 intpart = (long) value;
572 value = value - intpart;
603 ufvalue = abs_val(fvalue);
606 else if (flags & DP_F_PLUS)
608 else if (flags & DP_F_SPACE)
611 intpart = (long)ufvalue;
613 /* sorry, we only support 9 digits past the decimal because of our
618 /* we "cheat" by converting the fractional part to integer by
619 multiplying by a factor of 10 */
620 fracpart = round((pow10(max)) * (ufvalue - intpart));
622 if (fracpart >= pow10(max)) {
624 fracpart -= (long)pow10(max);
627 /* convert integer part */
630 (caps ? "0123456789ABCDEF"
631 : "0123456789abcdef")[intpart % 10];
632 intpart = (intpart / 10);
633 } while (intpart && (iplace < 20));
636 iconvert[iplace] = 0;
638 /* convert fractional part */
641 (caps ? "0123456789ABCDEF"
642 : "0123456789abcdef")[fracpart % 10];
643 fracpart = (fracpart / 10);
644 } while (fplace < max);
647 fconvert[fplace] = 0;
649 /* -1 for decimal point, another -1 if we are printing a sign */
650 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
651 zpadlen = max - fplace;
656 if (flags & DP_F_MINUS)
659 if ((flags & DP_F_ZERO) && (padlen > 0)) {
661 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
666 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
671 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
675 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
678 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
681 * Decimal point. This should probably use locale to find the correct
685 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
688 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
690 while (zpadlen > 0) {
691 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
696 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
709 /* If we haven't at least one buffer, someone has doe a big booboo */
710 assert(*sbuffer != NULL || buffer != NULL);
713 while (*currlen >= *maxlen) {
714 if (*buffer == NULL) {
715 assert(*sbuffer != NULL);
718 *buffer = OPENSSL_malloc(*maxlen);
720 memcpy(*buffer, *sbuffer, *currlen);
724 *buffer = OPENSSL_realloc(*buffer, *maxlen);
727 /* What to do if *buffer is NULL? */
728 assert(*sbuffer != NULL || *buffer != NULL);
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 MS_STATIC char hugebuf[1024*10];
761 char *hugebufp = hugebuf;
762 size_t hugebufsize = sizeof(hugebuf);
767 CRYPTO_push_info("doapr()");
768 _dopr(&hugebufp, &dynbuf, &hugebufsize,
769 &retlen, &ignored, format, args);
772 ret=BIO_write(bio, dynbuf, (int)retlen);
773 OPENSSL_free(dynbuf);
777 ret=BIO_write(bio, hugebuf, (int)retlen);
783 /* As snprintf is not available everywhere, we provide our own implementation.
784 * This function has nothing to do with BIOs, but it's closely related
785 * to BIO_printf, and we need *some* name prefix ...
786 * (XXX the function should be renamed, but to what?) */
787 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
792 va_start(args, format);
794 ret = BIO_vsnprintf(buf, n, format, args);
800 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
805 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
808 /* In case of truncation, return -1 like traditional snprintf.
809 * (Current drafts for ISO/IEC 9899 say snprintf should return
810 * the number of characters that would have been written,
811 * had the buffer been large enough.) */
814 return (retlen <= INT_MAX) ? retlen : -1;