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, ...)
86 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
89 format=va_arg(args, char *);
92 dopr(hugebuf, sizeof(hugebuf), &retlen, format, args);
93 ret=BIO_write(bio, hugebuf, (int)retlen);
100 * Copyright Patrick Powell 1995
101 * This code is based on code written by Patrick Powell <papowell@astart.com>
102 * It may be used for any purpose as long as this notice remains intact
103 * on all source code distributions.
107 * This code contains numerious changes and enhancements which were
108 * made by lots of contributors over the last years to Patrick Powell's
111 * o Patrick Powell <papowell@astart.com> (1995)
112 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
113 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
114 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
115 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
116 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
117 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
121 #define LDOUBLE long double
123 #define LDOUBLE double
127 #define LLONG long long
132 static void fmtstr (char *, size_t *, size_t, char *, int, int, int);
133 static void fmtint (char *, size_t *, size_t, LLONG, int, int, int, int);
134 static void fmtfp (char *, size_t *, size_t, LDOUBLE, int, int, int);
135 static void dopr_outch (char *, size_t *, size_t, int);
137 /* format read states */
138 #define DP_S_DEFAULT 0
147 /* format flags - Bits */
148 #define DP_F_MINUS (1 << 0)
149 #define DP_F_PLUS (1 << 1)
150 #define DP_F_SPACE (1 << 2)
151 #define DP_F_NUM (1 << 3)
152 #define DP_F_ZERO (1 << 4)
153 #define DP_F_UP (1 << 5)
154 #define DP_F_UNSIGNED (1 << 6)
156 /* conversion flags */
159 #define DP_C_LDOUBLE 3
162 /* some handy macros */
163 #define char_to_int(p) (p - '0')
164 #define MAX(p,q) ((p >= q) ? p : q)
185 state = DP_S_DEFAULT;
186 flags = currlen = cflags = min = 0;
190 while (state != DP_S_DONE) {
191 if ((ch == '\0') || (currlen >= maxlen))
199 dopr_outch(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 = va_arg(args, short int);
296 value = va_arg(args, long int);
299 value = va_arg(args, LLONG);
302 value = va_arg(args, int);
305 fmtint(buffer, &currlen, maxlen, value, 10, min, max, flags);
313 flags |= DP_F_UNSIGNED;
320 value = (LLONG) va_arg(args,
324 value = va_arg(args, unsigned LLONG);
327 value = (LLONG) va_arg(args,
331 fmtint(buffer, &currlen, maxlen, value,
332 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 fmtfp(buffer, &currlen, maxlen, fvalue, min, max, flags);
345 if (cflags == DP_C_LDOUBLE)
346 fvalue = va_arg(args, LDOUBLE);
348 fvalue = va_arg(args, double);
353 if (cflags == DP_C_LDOUBLE)
354 fvalue = va_arg(args, LDOUBLE);
356 fvalue = va_arg(args, double);
359 dopr_outch(buffer, &currlen, maxlen,
363 strvalue = va_arg(args, char *);
366 fmtstr(buffer, &currlen, maxlen, strvalue,
370 value = (long)va_arg(args, void *);
371 fmtint(buffer, &currlen, maxlen,
372 value, 16, min, max, flags);
375 if (cflags == DP_C_SHORT) {
377 num = va_arg(args, short int *);
379 } else if (cflags == DP_C_LONG) { /* XXX */
381 num = va_arg(args, long int *);
382 *num = (long int) currlen;
383 } else if (cflags == DP_C_LLONG) { /* XXX */
385 num = va_arg(args, LLONG *);
386 *num = (LLONG) currlen;
389 num = va_arg(args, int *);
394 dopr_outch(buffer, &currlen, maxlen, ch);
397 /* not supported yet, treat as next char */
405 state = DP_S_DEFAULT;
406 flags = cflags = min = 0;
415 if (currlen >= maxlen - 1)
416 currlen = maxlen - 1;
417 buffer[currlen] = '\0';
437 for (strln = 0; value[strln]; ++strln)
439 padlen = min - strln;
442 if (flags & DP_F_MINUS)
445 while ((padlen > 0) && (cnt < max)) {
446 dopr_outch(buffer, currlen, maxlen, ' ');
450 while (*value && (cnt < max)) {
451 dopr_outch(buffer, currlen, maxlen, *value++);
454 while ((padlen < 0) && (cnt < max)) {
455 dopr_outch(buffer, currlen, maxlen, ' ');
473 unsigned LLONG uvalue;
483 if (!(flags & DP_F_UNSIGNED)) {
487 } else if (flags & DP_F_PLUS)
489 else if (flags & DP_F_SPACE)
496 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
497 [uvalue % (unsigned) base];
498 uvalue = (uvalue / (unsigned) base);
499 } while (uvalue && (place < 20));
504 zpadlen = max - place;
505 spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
510 if (flags & DP_F_ZERO) {
511 zpadlen = MAX(zpadlen, spadlen);
514 if (flags & DP_F_MINUS)
518 while (spadlen > 0) {
519 dopr_outch(buffer, currlen, maxlen, ' ');
525 dopr_outch(buffer, currlen, maxlen, signvalue);
529 while (zpadlen > 0) {
530 dopr_outch(buffer, currlen, maxlen, '0');
536 dopr_outch(buffer, currlen, maxlen, convert[--place]);
538 /* left justified spaces */
539 while (spadlen < 0) {
540 dopr_outch(buffer, currlen, maxlen, ' ');
547 abs_val(LDOUBLE value)
549 LDOUBLE result = value;
570 intpart = (long) value;
571 value = value - intpart;
601 ufvalue = abs_val(fvalue);
604 else if (flags & DP_F_PLUS)
606 else if (flags & DP_F_SPACE)
609 intpart = (long)ufvalue;
611 /* sorry, we only support 9 digits past the decimal because of our
616 /* we "cheat" by converting the fractional part to integer by
617 multiplying by a factor of 10 */
618 fracpart = round((pow10(max)) * (ufvalue - intpart));
620 if (fracpart >= pow10(max)) {
622 fracpart -= (long)pow10(max);
625 /* convert integer part */
628 (caps ? "0123456789ABCDEF"
629 : "0123456789abcdef")[intpart % 10];
630 intpart = (intpart / 10);
631 } while (intpart && (iplace < 20));
634 iconvert[iplace] = 0;
636 /* convert fractional part */
639 (caps ? "0123456789ABCDEF"
640 : "0123456789abcdef")[fracpart % 10];
641 fracpart = (fracpart / 10);
642 } while (fracpart && (fplace < 20));
645 fconvert[fplace] = 0;
647 /* -1 for decimal point, another -1 if we are printing a sign */
648 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
649 zpadlen = max - fplace;
654 if (flags & DP_F_MINUS)
657 if ((flags & DP_F_ZERO) && (padlen > 0)) {
659 dopr_outch(buffer, currlen, maxlen, signvalue);
664 dopr_outch(buffer, currlen, maxlen, '0');
669 dopr_outch(buffer, currlen, maxlen, ' ');
673 dopr_outch(buffer, currlen, maxlen, signvalue);
676 dopr_outch(buffer, currlen, maxlen, iconvert[--iplace]);
679 * Decimal point. This should probably use locale to find the correct
683 dopr_outch(buffer, currlen, maxlen, '.');
686 dopr_outch(buffer, currlen, maxlen, fconvert[--fplace]);
688 while (zpadlen > 0) {
689 dopr_outch(buffer, currlen, maxlen, '0');
694 dopr_outch(buffer, currlen, maxlen, ' ');
706 if (*currlen < maxlen)
707 buffer[(*currlen)++] = (char)c;