]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/b_print.c
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 /***************************************************************************/
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 (void (*)(char **, size_t *, size_t *, int),
118 char **, size_t *, size_t *, const char *, int, int,
120 static void fmtint (void (*)(char **, size_t *, size_t *, int),
121 char **, size_t *, size_t *, LLONG
, int, int, int, int);
122 static void fmtfp (void (*)(char **, size_t *, size_t *, int),
123 char **, size_t *, size_t *, LDOUBLE
, int, int, int);
124 static int dopr_isbig (size_t, size_t);
125 static int dopr_copy (size_t);
126 static void dopr_outch (char **, size_t *, size_t *, int);
127 #ifdef USE_ALLOCATING_PRINT
128 static int doapr_isbig (size_t, size_t);
129 static int doapr_copy (size_t);
130 static void doapr_outch (char **, size_t *, size_t *, int);
132 static void _dopr(void (*)(char **, size_t *, size_t *, int),
133 int (*)(size_t, size_t), int (*)(size_t),
134 char **buffer
, size_t *maxlen
, size_t *retlen
, int *truncated
,
135 const char *format
, va_list args
);
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)
166 #ifndef USE_ALLOCATING_PRINT
176 _dopr(dopr_outch
, dopr_isbig
, dopr_copy
,
177 &buffer
, &maxlen
, retlen
, &ignored
, format
, args
);
188 size_t dummy_maxlen
= 0;
190 _dopr(doapr_outch
, doapr_isbig
, doapr_copy
,
191 buffer
, &dummy_maxlen
, retlen
, &ignored
, format
, args
);
197 void (*outch_fn
)(char **, size_t *, size_t *, int),
198 int (*isbig_fn
)(size_t, size_t),
199 int (*copy_fn
)(size_t),
218 state
= DP_S_DEFAULT
;
219 flags
= currlen
= cflags
= min
= 0;
223 while (state
!= DP_S_DONE
) {
224 if ((ch
== '\0') || (*isbig_fn
)(currlen
, *maxlen
))
232 (*outch_fn
)(buffer
, &currlen
, maxlen
, ch
);
263 if (isdigit((unsigned char)ch
)) {
264 min
= 10 * min
+ char_to_int(ch
);
266 } else if (ch
== '*') {
267 min
= va_arg(args
, int);
281 if (isdigit((unsigned char)ch
)) {
284 max
= 10 * max
+ char_to_int(ch
);
286 } else if (ch
== '*') {
287 max
= va_arg(args
, int);
300 if (*format
== 'l') {
312 cflags
= DP_C_LDOUBLE
;
326 value
= (short int)va_arg(args
, int);
329 value
= va_arg(args
, long int);
332 value
= va_arg(args
, LLONG
);
335 value
= va_arg(args
, int);
338 fmtint(outch_fn
, buffer
, &currlen
, maxlen
,
339 value
, 10, min
, max
, flags
);
347 flags
|= DP_F_UNSIGNED
;
350 value
= (unsigned short int)va_arg(args
, unsigned int);
353 value
= (LLONG
) va_arg(args
,
357 value
= va_arg(args
, unsigned LLONG
);
360 value
= (LLONG
) va_arg(args
,
364 fmtint(outch_fn
, buffer
, &currlen
, maxlen
, value
,
365 ch
== 'o' ? 8 : (ch
== 'u' ? 10 : 16),
369 if (cflags
== DP_C_LDOUBLE
)
370 fvalue
= va_arg(args
, LDOUBLE
);
372 fvalue
= va_arg(args
, double);
373 fmtfp(outch_fn
, buffer
, &currlen
, maxlen
,
374 fvalue
, min
, max
, flags
);
379 if (cflags
== DP_C_LDOUBLE
)
380 fvalue
= va_arg(args
, LDOUBLE
);
382 fvalue
= va_arg(args
, double);
387 if (cflags
== DP_C_LDOUBLE
)
388 fvalue
= va_arg(args
, LDOUBLE
);
390 fvalue
= va_arg(args
, double);
393 (*outch_fn
)(buffer
, &currlen
, maxlen
,
397 strvalue
= va_arg(args
, char *);
399 max
= (*copy_fn
)(*maxlen
);
400 fmtstr(outch_fn
, buffer
, &currlen
, maxlen
, strvalue
,
404 value
= (long)va_arg(args
, void *);
405 fmtint(outch_fn
, buffer
, &currlen
, maxlen
,
406 value
, 16, min
, max
, flags
);
409 if (cflags
== DP_C_SHORT
) {
411 num
= va_arg(args
, short int *);
413 } else if (cflags
== DP_C_LONG
) { /* XXX */
415 num
= va_arg(args
, long int *);
416 *num
= (long int) currlen
;
417 } else if (cflags
== DP_C_LLONG
) { /* XXX */
419 num
= va_arg(args
, LLONG
*);
420 *num
= (LLONG
) currlen
;
423 num
= va_arg(args
, int *);
428 (*outch_fn
)(buffer
, &currlen
, maxlen
, ch
);
431 /* not supported yet, treat as next char */
439 state
= DP_S_DEFAULT
;
440 flags
= cflags
= min
= 0;
449 *truncated
= (currlen
> *maxlen
- 1);
451 currlen
= *maxlen
- 1;
452 (*buffer
)[currlen
] = '\0';
459 void (*outch_fn
)(char **, size_t *, size_t *, int),
473 for (strln
= 0; value
[strln
]; ++strln
)
475 padlen
= min
- strln
;
478 if (flags
& DP_F_MINUS
)
481 while ((padlen
> 0) && (cnt
< max
)) {
482 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
486 while (*value
&& (cnt
< max
)) {
487 (*outch_fn
)(buffer
, currlen
, maxlen
, *value
++);
490 while ((padlen
< 0) && (cnt
< max
)) {
491 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
499 void (*outch_fn
)(char **, size_t *, size_t *, int),
510 unsigned LLONG uvalue
;
520 if (!(flags
& DP_F_UNSIGNED
)) {
524 } else if (flags
& DP_F_PLUS
)
526 else if (flags
& DP_F_SPACE
)
533 (caps
? "0123456789ABCDEF" : "0123456789abcdef")
534 [uvalue
% (unsigned) base
];
535 uvalue
= (uvalue
/ (unsigned) base
);
536 } while (uvalue
&& (place
< 20));
541 zpadlen
= max
- place
;
542 spadlen
= min
- MAX(max
, place
) - (signvalue
? 1 : 0);
547 if (flags
& DP_F_ZERO
) {
548 zpadlen
= MAX(zpadlen
, spadlen
);
551 if (flags
& DP_F_MINUS
)
555 while (spadlen
> 0) {
556 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
562 (*outch_fn
)(buffer
, currlen
, maxlen
, signvalue
);
566 while (zpadlen
> 0) {
567 (*outch_fn
)(buffer
, currlen
, maxlen
, '0');
573 (*outch_fn
)(buffer
, currlen
, maxlen
, convert
[--place
]);
575 /* left justified spaces */
576 while (spadlen
< 0) {
577 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
584 abs_val(LDOUBLE value
)
586 LDOUBLE result
= value
;
607 intpart
= (long) value
;
608 value
= value
- intpart
;
616 void (*outch_fn
)(char **, size_t *, size_t *, int),
639 ufvalue
= abs_val(fvalue
);
642 else if (flags
& DP_F_PLUS
)
644 else if (flags
& DP_F_SPACE
)
647 intpart
= (long)ufvalue
;
649 /* sorry, we only support 9 digits past the decimal because of our
654 /* we "cheat" by converting the fractional part to integer by
655 multiplying by a factor of 10 */
656 fracpart
= round((pow10(max
)) * (ufvalue
- intpart
));
658 if (fracpart
>= pow10(max
)) {
660 fracpart
-= (long)pow10(max
);
663 /* convert integer part */
666 (caps
? "0123456789ABCDEF"
667 : "0123456789abcdef")[intpart
% 10];
668 intpart
= (intpart
/ 10);
669 } while (intpart
&& (iplace
< 20));
672 iconvert
[iplace
] = 0;
674 /* convert fractional part */
677 (caps
? "0123456789ABCDEF"
678 : "0123456789abcdef")[fracpart
% 10];
679 fracpart
= (fracpart
/ 10);
680 } while (fracpart
&& (fplace
< 20));
683 fconvert
[fplace
] = 0;
685 /* -1 for decimal point, another -1 if we are printing a sign */
686 padlen
= min
- iplace
- max
- 1 - ((signvalue
) ? 1 : 0);
687 zpadlen
= max
- fplace
;
692 if (flags
& DP_F_MINUS
)
695 if ((flags
& DP_F_ZERO
) && (padlen
> 0)) {
697 (*outch_fn
)(buffer
, currlen
, maxlen
, signvalue
);
702 (*outch_fn
)(buffer
, currlen
, maxlen
, '0');
707 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
711 (*outch_fn
)(buffer
, currlen
, maxlen
, signvalue
);
714 (*outch_fn
)(buffer
, currlen
, maxlen
, iconvert
[--iplace
]);
717 * Decimal point. This should probably use locale to find the correct
721 (*outch_fn
)(buffer
, currlen
, maxlen
, '.');
724 (*outch_fn
)(buffer
, currlen
, maxlen
, fconvert
[--fplace
]);
726 while (zpadlen
> 0) {
727 (*outch_fn
)(buffer
, currlen
, maxlen
, '0');
732 (*outch_fn
)(buffer
, currlen
, maxlen
, ' ');
744 #ifdef USE_ALLOCATING_PRINT
749 /* Return as high an integer as possible */
759 return currlen
> maxlen
;
762 #ifdef USE_ALLOCATING_PRINT
779 if (*currlen
< *maxlen
)
780 (*buffer
)[(*currlen
)++] = (char)c
;
784 #ifdef USE_ALLOCATING_PRINT
792 if (*buffer
== NULL
) {
795 *buffer
= Malloc(*maxlen
);
797 while (*currlen
>= *maxlen
) {
799 *buffer
= Realloc(*buffer
, *maxlen
);
801 /* What to do if *buffer is NULL? */
802 assert(*buffer
!= NULL
);
804 (*buffer
)[(*currlen
)++] = (char)c
;
809 /***************************************************************************/
811 int BIO_printf (BIO
*bio
, const char *format
, ...)
816 #ifdef USE_ALLOCATING_PRINT
819 MS_STATIC
char hugebuf
[1024*2]; /* 10k in one chunk is the limit */
822 va_start(args
, format
);
824 #ifndef USE_ALLOCATING_PRINT
826 dopr(hugebuf
, sizeof(hugebuf
), &retlen
, format
, args
);
829 CRYPTO_push_info("doapr()");
830 doapr(&hugebuf
, &retlen
, format
, args
);
834 ret
=BIO_write(bio
, hugebuf
, (int)retlen
);
836 #ifdef USE_ALLOCATING_PRINT
845 /* As snprintf is not available everywhere, we provide our own implementation.
846 * This function has nothing to do with BIOs, but it's closely related
847 * to BIO_printf, and we need *some* name prefix ...
848 * (XXX the function should be renamed, but to what?) */
849 int BIO_snprintf(char *buf
, size_t n
, const char *format
, ...)
855 va_start(args
, format
);
856 _dopr(dopr_outch
, dopr_isbig
, dopr_copy
,
857 &buf
, &n
, &retlen
, &truncated
, format
, args
);
859 /* In case of truncation, return -1 like traditional snprintf.
860 * (Current drafts for ISO/IEC 9899 say snprintf should return
861 * the number of characters that would have been written,
862 * had the buffer been large enough.) */
865 return (retlen
<= INT_MAX
) ? retlen
: -1;