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.
70 #ifndef NO_SYS_TYPES_H
71 #include <sys/types.h>
73 #include <openssl/bio.h>
76 # ifndef HAVE_LONG_LONG
77 # define HAVE_LONG_LONG 1
81 static void dopr (char *buffer, size_t maxlen, size_t *retlen,
82 const char *format, va_list args);
83 static void doapr (char **buffer, size_t *retlen,
84 const char *format, va_list args);
86 int BIO_printf (BIO *bio, ...)
92 #ifdef USE_ALLOCATING_PRINT
95 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
99 format=va_arg(args, char *);
101 #ifndef USE_ALLOCATING_PRINT
103 dopr(hugebuf, sizeof(hugebuf), &retlen, format, args);
106 CRYPTO_push_info("doapr()");
107 doapr(&hugebuf, &retlen, format, args);
111 ret=BIO_write(bio, hugebuf, (int)retlen);
113 #ifdef USE_ALLOCATING_PRINT
123 * Copyright Patrick Powell 1995
124 * This code is based on code written by Patrick Powell <papowell@astart.com>
125 * It may be used for any purpose as long as this notice remains intact
126 * on all source code distributions.
130 * This code contains numerious changes and enhancements which were
131 * made by lots of contributors over the last years to Patrick Powell's
134 * o Patrick Powell <papowell@astart.com> (1995)
135 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
136 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
137 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
138 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
139 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
140 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
144 #define LDOUBLE long double
146 #define LDOUBLE double
150 #define LLONG long long
155 static void fmtstr (void (*)(char **, size_t *, size_t *, int),
156 char **, size_t *, size_t *, char *, int, int, int);
157 static void fmtint (void (*)(char **, size_t *, size_t *, int),
158 char **, size_t *, size_t *, LLONG, int, int, int, int);
159 static void fmtfp (void (*)(char **, size_t *, size_t *, int),
160 char **, size_t *, size_t *, LDOUBLE, int, int, int);
161 static int dopr_isbig (size_t, size_t);
162 static int doapr_isbig (size_t, size_t);
163 static int dopr_copy (size_t);
164 static int doapr_copy (size_t);
165 static void dopr_outch (char **, size_t *, size_t *, int);
166 static void doapr_outch (char **, size_t *, size_t *, int);
167 static void _dopr(void (*)(char **, size_t *, size_t *, int),
168 int (*)(size_t, size_t), int (*)(size_t),
169 char **buffer, size_t *maxlen, size_t *retlen,
170 const char *format, va_list args);
172 /* format read states */
173 #define DP_S_DEFAULT 0
182 /* format flags - Bits */
183 #define DP_F_MINUS (1 << 0)
184 #define DP_F_PLUS (1 << 1)
185 #define DP_F_SPACE (1 << 2)
186 #define DP_F_NUM (1 << 3)
187 #define DP_F_ZERO (1 << 4)
188 #define DP_F_UP (1 << 5)
189 #define DP_F_UNSIGNED (1 << 6)
191 /* conversion flags */
194 #define DP_C_LDOUBLE 3
197 /* some handy macros */
198 #define char_to_int(p) (p - '0')
199 #define MAX(p,q) ((p >= q) ? p : q)
209 _dopr(dopr_outch, dopr_isbig, dopr_copy,
210 &buffer, &maxlen, retlen, format, args);
220 size_t dummy_maxlen = 0;
221 _dopr(doapr_outch, doapr_isbig, doapr_copy,
222 buffer, &dummy_maxlen, retlen, format, args);
227 void (*outch_fn)(char **, size_t *, size_t *, int),
228 int (*isbig_fn)(size_t, size_t),
229 int (*copy_fn)(size_t),
247 state = DP_S_DEFAULT;
248 flags = currlen = cflags = min = 0;
252 while (state != DP_S_DONE) {
253 if ((ch == '\0') || (*isbig_fn)(currlen, *maxlen))
261 (*outch_fn)(buffer, &currlen, maxlen, ch);
292 if (isdigit((unsigned char)ch)) {
293 min = 10 * min + char_to_int(ch);
295 } else if (ch == '*') {
296 min = va_arg(args, int);
310 if (isdigit((unsigned char)ch)) {
313 max = 10 * max + char_to_int(ch);
315 } else if (ch == '*') {
316 max = va_arg(args, int);
329 if (*format == 'l') {
341 cflags = DP_C_LDOUBLE;
355 value = (short int)va_arg(args, int);
358 value = va_arg(args, long int);
361 value = va_arg(args, LLONG);
364 value = va_arg(args, int);
367 fmtint(outch_fn, buffer, &currlen, maxlen,
368 value, 10, min, max, flags);
376 flags |= DP_F_UNSIGNED;
379 value = (unsigned short int)va_arg(args, unsigned int);
382 value = (LLONG) va_arg(args,
386 value = va_arg(args, unsigned LLONG);
389 value = (LLONG) va_arg(args,
393 fmtint(outch_fn, buffer, &currlen, maxlen, value,
394 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
398 if (cflags == DP_C_LDOUBLE)
399 fvalue = va_arg(args, LDOUBLE);
401 fvalue = va_arg(args, double);
402 fmtfp(outch_fn, buffer, &currlen, maxlen,
403 fvalue, min, max, flags);
408 if (cflags == DP_C_LDOUBLE)
409 fvalue = va_arg(args, LDOUBLE);
411 fvalue = va_arg(args, double);
416 if (cflags == DP_C_LDOUBLE)
417 fvalue = va_arg(args, LDOUBLE);
419 fvalue = va_arg(args, double);
422 (*outch_fn)(buffer, &currlen, maxlen,
426 strvalue = va_arg(args, char *);
428 max = (*copy_fn)(*maxlen);
429 fmtstr(outch_fn, buffer, &currlen, maxlen, strvalue,
433 value = (long)va_arg(args, void *);
434 fmtint(outch_fn, buffer, &currlen, maxlen,
435 value, 16, min, max, flags);
438 if (cflags == DP_C_SHORT) {
440 num = va_arg(args, short int *);
442 } else if (cflags == DP_C_LONG) { /* XXX */
444 num = va_arg(args, long int *);
445 *num = (long int) currlen;
446 } else if (cflags == DP_C_LLONG) { /* XXX */
448 num = va_arg(args, LLONG *);
449 *num = (LLONG) currlen;
452 num = va_arg(args, int *);
457 (*outch_fn)(buffer, &currlen, maxlen, ch);
460 /* not supported yet, treat as next char */
468 state = DP_S_DEFAULT;
469 flags = cflags = min = 0;
478 if (currlen >= *maxlen - 1)
479 currlen = *maxlen - 1;
480 (*buffer)[currlen] = '\0';
487 void (*outch_fn)(char **, size_t *, size_t *, int),
501 for (strln = 0; value[strln]; ++strln)
503 padlen = min - strln;
506 if (flags & DP_F_MINUS)
509 while ((padlen > 0) && (cnt < max)) {
510 (*outch_fn)(buffer, currlen, maxlen, ' ');
514 while (*value && (cnt < max)) {
515 (*outch_fn)(buffer, currlen, maxlen, *value++);
518 while ((padlen < 0) && (cnt < max)) {
519 (*outch_fn)(buffer, currlen, maxlen, ' ');
527 void (*outch_fn)(char **, size_t *, size_t *, int),
538 unsigned LLONG uvalue;
548 if (!(flags & DP_F_UNSIGNED)) {
552 } else if (flags & DP_F_PLUS)
554 else if (flags & DP_F_SPACE)
561 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
562 [uvalue % (unsigned) base];
563 uvalue = (uvalue / (unsigned) base);
564 } while (uvalue && (place < 20));
569 zpadlen = max - place;
570 spadlen = min - MAX(max, place) - (signvalue ? 1 : 0);
575 if (flags & DP_F_ZERO) {
576 zpadlen = MAX(zpadlen, spadlen);
579 if (flags & DP_F_MINUS)
583 while (spadlen > 0) {
584 (*outch_fn)(buffer, currlen, maxlen, ' ');
590 (*outch_fn)(buffer, currlen, maxlen, signvalue);
594 while (zpadlen > 0) {
595 (*outch_fn)(buffer, currlen, maxlen, '0');
601 (*outch_fn)(buffer, currlen, maxlen, convert[--place]);
603 /* left justified spaces */
604 while (spadlen < 0) {
605 (*outch_fn)(buffer, currlen, maxlen, ' ');
612 abs_val(LDOUBLE value)
614 LDOUBLE result = value;
635 intpart = (long) value;
636 value = value - intpart;
644 void (*outch_fn)(char **, size_t *, size_t *, int),
667 ufvalue = abs_val(fvalue);
670 else if (flags & DP_F_PLUS)
672 else if (flags & DP_F_SPACE)
675 intpart = (long)ufvalue;
677 /* sorry, we only support 9 digits past the decimal because of our
682 /* we "cheat" by converting the fractional part to integer by
683 multiplying by a factor of 10 */
684 fracpart = round((pow10(max)) * (ufvalue - intpart));
686 if (fracpart >= pow10(max)) {
688 fracpart -= (long)pow10(max);
691 /* convert integer part */
694 (caps ? "0123456789ABCDEF"
695 : "0123456789abcdef")[intpart % 10];
696 intpart = (intpart / 10);
697 } while (intpart && (iplace < 20));
700 iconvert[iplace] = 0;
702 /* convert fractional part */
705 (caps ? "0123456789ABCDEF"
706 : "0123456789abcdef")[fracpart % 10];
707 fracpart = (fracpart / 10);
708 } while (fracpart && (fplace < 20));
711 fconvert[fplace] = 0;
713 /* -1 for decimal point, another -1 if we are printing a sign */
714 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
715 zpadlen = max - fplace;
720 if (flags & DP_F_MINUS)
723 if ((flags & DP_F_ZERO) && (padlen > 0)) {
725 (*outch_fn)(buffer, currlen, maxlen, signvalue);
730 (*outch_fn)(buffer, currlen, maxlen, '0');
735 (*outch_fn)(buffer, currlen, maxlen, ' ');
739 (*outch_fn)(buffer, currlen, maxlen, signvalue);
742 (*outch_fn)(buffer, currlen, maxlen, iconvert[--iplace]);
745 * Decimal point. This should probably use locale to find the correct
749 (*outch_fn)(buffer, currlen, maxlen, '.');
752 (*outch_fn)(buffer, currlen, maxlen, fconvert[--fplace]);
754 while (zpadlen > 0) {
755 (*outch_fn)(buffer, currlen, maxlen, '0');
760 (*outch_fn)(buffer, currlen, maxlen, ' ');
776 /* Return as high an integer as possible */
785 return currlen > maxlen;
803 if (*currlen < *maxlen)
804 (*buffer)[(*currlen)++] = (char)c;
815 if (*buffer == NULL) {
818 *buffer = Malloc(*maxlen);
820 while (*currlen >= *maxlen) {
822 *buffer = Realloc(*buffer, *maxlen);
824 /* What to do if *buffer is NULL? */
825 assert(*buffer != NULL);
827 (*buffer)[(*currlen)++] = (char)c;