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.
67 #include <sys/types.h>
69 #include <openssl/bio.h>
72 # ifndef HAVE_LONG_LONG
73 # define HAVE_LONG_LONG
77 static void dopr (char *buffer, size_t maxlen, size_t *retlen,
78 const char *format, va_list args);
80 int BIO_printf (BIO *bio, ...)
85 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
88 format=va_arg(args, char *);
91 dopr(hugebuf, sizeof(hugebuf), &ret, format, args);
92 ret=BIO_write(bio, hugebuf, ret);
99 * Copyright Patrick Powell 1995
100 * This code is based on code written by Patrick Powell <papowell@astart.com>
101 * It may be used for any purpose as long as this notice remains intact
102 * on all source code distributions.
106 * This code contains numerious changes and enhancements which were
107 * made by lots of contributors over the last years to Patrick Powell's
110 * o Patrick Powell <papowell@astart.com> (1995)
111 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
112 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
113 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
114 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
115 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
116 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
120 #define LDOUBLE long double
122 #define LDOUBLE double
126 #define LLONG long long
131 static void fmtstr (char *, size_t *, size_t, char *, int, int, int);
132 static void fmtint (char *, size_t *, size_t, LLONG, int, int, int, int);
133 static void fmtfp (char *, size_t *, size_t, LDOUBLE, int, int, int);
134 static void dopr_outch (char *, size_t *, size_t, int);
136 /* format read states */
137 #define DP_S_DEFAULT 0
146 /* format flags - Bits */
147 #define DP_F_MINUS (1 << 0)
148 #define DP_F_PLUS (1 << 1)
149 #define DP_F_SPACE (1 << 2)
150 #define DP_F_NUM (1 << 3)
151 #define DP_F_ZERO (1 << 4)
152 #define DP_F_UP (1 << 5)
153 #define DP_F_UNSIGNED (1 << 6)
155 /* conversion flags */
158 #define DP_C_LDOUBLE 3
161 /* some handy macros */
162 #define char_to_int(p) (p - '0')
163 #define MAX(p,q) ((p >= q) ? p : q)
184 state = DP_S_DEFAULT;
185 flags = currlen = cflags = min = 0;
189 while (state != DP_S_DONE) {
190 if ((ch == '\0') || (currlen >= maxlen))
198 dopr_outch(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 = va_arg(args, short int);
295 value = va_arg(args, long int);
298 value = va_arg(args, LLONG);
301 value = va_arg(args, int);
304 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
312 flags |= DP_F_UNSIGNED;
319 value = (LLONG) va_arg(args,
323 value = va_arg(args, unsigned LLONG);
326 value = (LLONG) va_arg(args,
330 fmtint(buffer, &currlen, maxlen, value,
331 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
335 if (cflags == DP_C_LDOUBLE)
336 fvalue = va_arg(args, LDOUBLE);
338 fvalue = va_arg(args, double);
339 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
344 if (cflags == DP_C_LDOUBLE)
345 fvalue = va_arg(args, LDOUBLE);
347 fvalue = va_arg(args, double);
352 if (cflags == DP_C_LDOUBLE)
353 fvalue = va_arg(args, LDOUBLE);
355 fvalue = va_arg(args, double);
358 dopr_outch(buffer, &currlen, maxlen,
362 strvalue = va_arg(args, char *);
365 fmtstr(buffer, &currlen, maxlen, strvalue,
369 value = (long)va_arg(args, void *);
370 fmtint(buffer, &currlen, maxlen,
371 value, 16, min, max, flags);
374 if (cflags == DP_C_SHORT) {
376 num = va_arg(args, short int *);
378 } else if (cflags == DP_C_LONG) { /* XXX */
380 num = va_arg(args, long int *);
381 *num = (long int) currlen;
382 } else if (cflags == DP_C_LLONG) { /* XXX */
384 num = va_arg(args, LLONG *);
385 *num = (LLONG) currlen;
388 num = va_arg(args, int *);
393 dopr_outch(buffer, &currlen, maxlen, ch);
396 /* not supported yet, treat as next char */
404 state = DP_S_DEFAULT;
405 flags = cflags = min = 0;
414 if (currlen >= maxlen - 1)
415 currlen = maxlen - 1;
416 buffer[currlen] = '\0';
436 for (strln = 0; value[strln]; ++strln)
438 padlen = min - strln;
441 if (flags & DP_F_MINUS)
444 while ((padlen > 0) && (cnt < max)) {
445 dopr_outch(buffer, currlen, maxlen, ' ');
449 while (*value && (cnt < max)) {
450 dopr_outch(buffer, currlen, maxlen, *value++);
453 while ((padlen < 0) && (cnt < max)) {
454 dopr_outch(buffer, currlen, maxlen, ' ');
472 unsigned LLONG uvalue;
482 if (!(flags & DP_F_UNSIGNED)) {
486 } else if (flags & DP_F_PLUS)
488 else if (flags & DP_F_SPACE)
495 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
496 [uvalue % (unsigned) base];
497 uvalue = (uvalue / (unsigned) base);
498 } while (uvalue && (place < 20));
503 zpadlen = max - place;
504 spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
509 if (flags & DP_F_ZERO) {
510 zpadlen = MAX(zpadlen, spadlen);
513 if (flags & DP_F_MINUS)
517 while (spadlen > 0) {
518 dopr_outch(buffer, currlen, maxlen, ' ');
524 dopr_outch(buffer, currlen, maxlen, signvalue);
528 while (zpadlen > 0) {
529 dopr_outch(buffer, currlen, maxlen, '0');
535 dopr_outch(buffer, currlen, maxlen, convert[--place]);
537 /* left justified spaces */
538 while (spadlen < 0) {
539 dopr_outch(buffer, currlen, maxlen, ' ');
546 abs_val(LDOUBLE value)
548 LDOUBLE result = value;
569 intpart = (long) value;
570 value = value - intpart;
600 ufvalue = abs_val(fvalue);
603 else if (flags & DP_F_PLUS)
605 else if (flags & DP_F_SPACE)
608 intpart = (long)ufvalue;
610 /* sorry, we only support 9 digits past the decimal because of our
615 /* we "cheat" by converting the fractional part to integer by
616 multiplying by a factor of 10 */
617 fracpart = round((pow10(max)) * (ufvalue - intpart));
619 if (fracpart >= pow10(max)) {
621 fracpart -= (long)pow10(max);
624 /* convert integer part */
627 (caps ? "0123456789ABCDEF"
628 : "0123456789abcdef")[intpart % 10];
629 intpart = (intpart / 10);
630 } while (intpart && (iplace < 20));
633 iconvert[iplace] = 0;
635 /* convert fractional part */
638 (caps ? "0123456789ABCDEF"
639 : "0123456789abcdef")[fracpart % 10];
640 fracpart = (fracpart / 10);
641 } while (fracpart && (fplace < 20));
644 fconvert[fplace] = 0;
646 /* -1 for decimal point, another -1 if we are printing a sign */
647 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
648 zpadlen = max - fplace;
653 if (flags & DP_F_MINUS)
656 if ((flags & DP_F_ZERO) && (padlen > 0)) {
658 dopr_outch(buffer, currlen, maxlen, signvalue);
663 dopr_outch(buffer, currlen, maxlen, '0');
668 dopr_outch(buffer, currlen, maxlen, ' ');
672 dopr_outch(buffer, currlen, maxlen, signvalue);
675 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
678 * Decimal point. This should probably use locale to find the correct
682 dopr_outch(buffer, currlen, maxlen, '.');
685 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
687 while (zpadlen > 0) {
688 dopr_outch(buffer, currlen, maxlen, '0');
693 dopr_outch(buffer, currlen, maxlen, ' ');
705 if (*currlen < maxlen)
706 buffer[(*currlen)++] = (char)c;