Update contrib.
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.]
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
61 /* disable assert() unless BIO_DEBUG has been defined */
69 * Stolen from tjh's ssl/ssl_trc.c stuff.
78 #ifndef NO_SYS_TYPES_H
79 #include <sys/types.h>
81 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
82 #include <openssl/bio.h>
84 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
85 # ifndef HAVE_LONG_LONG
86 # define HAVE_LONG_LONG 1
90 /***************************************************************************/
93 * Copyright Patrick Powell 1995
94 * This code is based on code written by Patrick Powell <papowell@astart.com>
95 * It may be used for any purpose as long as this notice remains intact
96 * on all source code distributions.
100 * This code contains numerious changes and enhancements which were
101 * made by lots of contributors over the last years to Patrick Powell's
104 * o Patrick Powell <papowell@astart.com> (1995)
105 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
106 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
107 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
108 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
109 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
110 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
111 * o ... (for OpenSSL)
114 #ifdef HAVE_LONG_DOUBLE
115 #define LDOUBLE long double
117 #define LDOUBLE double
121 # if defined(OPENSSL_SYS_WIN32) && !defined(__GNUC__)
122 # define LLONG __int64
124 # define LLONG long long
130 static void fmtstr (char **, char **, size_t *, size_t *,
131 const char *, int, int, int);
132 static void fmtint (char **, char **, size_t *, size_t *,
133 LLONG, int, int, int, int);
134 static void fmtfp (char **, char **, size_t *, size_t *,
135 LDOUBLE, int, int, int);
136 static void doapr_outch (char **, char **, size_t *, size_t *, int);
137 static void _dopr(char **sbuffer, char **buffer,
138 size_t *maxlen, size_t *retlen, int *truncated,
139 const char *format, va_list args);
141 /* format read states */
142 #define DP_S_DEFAULT 0
151 /* format flags - Bits */
152 #define DP_F_MINUS (1 << 0)
153 #define DP_F_PLUS (1 << 1)
154 #define DP_F_SPACE (1 << 2)
155 #define DP_F_NUM (1 << 3)
156 #define DP_F_ZERO (1 << 4)
157 #define DP_F_UP (1 << 5)
158 #define DP_F_UNSIGNED (1 << 6)
160 /* conversion flags */
163 #define DP_C_LDOUBLE 3
166 /* some handy macros */
167 #define char_to_int(p) (p - '0')
168 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
191 state = DP_S_DEFAULT;
192 flags = currlen = cflags = min = 0;
196 while (state != DP_S_DONE) {
197 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
205 doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
236 if (isdigit((unsigned char)ch)) {
237 min = 10 * min + char_to_int(ch);
239 } else if (ch == '*') {
240 min = va_arg(args, int);
254 if (isdigit((unsigned char)ch)) {
257 max = 10 * max + char_to_int(ch);
259 } else if (ch == '*') {
260 max = va_arg(args, int);
273 if (*format == 'l') {
285 cflags = DP_C_LDOUBLE;
299 value = (short int)va_arg(args, int);
302 value = va_arg(args, long int);
305 value = va_arg(args, LLONG);
308 value = va_arg(args, int);
311 fmtint(sbuffer, buffer, &currlen, maxlen,
312 value, 10, min, max, flags);
320 flags |= DP_F_UNSIGNED;
323 value = (unsigned short int)va_arg(args, unsigned int);
326 value = (LLONG) va_arg(args,
330 value = va_arg(args, unsigned LLONG);
333 value = (LLONG) va_arg(args,
337 fmtint(sbuffer, buffer, &currlen, maxlen, value,
338 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
342 if (cflags == DP_C_LDOUBLE)
343 fvalue = va_arg(args, LDOUBLE);
345 fvalue = va_arg(args, double);
346 fmtfp(sbuffer, buffer, &currlen, maxlen,
347 fvalue, min, max, flags);
352 if (cflags == DP_C_LDOUBLE)
353 fvalue = va_arg(args, LDOUBLE);
355 fvalue = va_arg(args, double);
360 if (cflags == DP_C_LDOUBLE)
361 fvalue = va_arg(args, LDOUBLE);
363 fvalue = va_arg(args, double);
366 doapr_outch(sbuffer, buffer, &currlen, maxlen,
370 strvalue = va_arg(args, char *);
377 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
381 value = (long)va_arg(args, void *);
382 fmtint(sbuffer, buffer, &currlen, maxlen,
383 value, 16, min, max, flags|DP_F_NUM);
386 if (cflags == DP_C_SHORT) {
388 num = va_arg(args, short int *);
390 } else if (cflags == DP_C_LONG) { /* XXX */
392 num = va_arg(args, long int *);
393 *num = (long int) currlen;
394 } else if (cflags == DP_C_LLONG) { /* XXX */
396 num = va_arg(args, LLONG *);
397 *num = (LLONG) currlen;
400 num = va_arg(args, int *);
405 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
408 /* not supported yet, treat as next char */
416 state = DP_S_DEFAULT;
417 flags = cflags = min = 0;
426 *truncated = (currlen > *maxlen - 1);
428 currlen = *maxlen - 1;
429 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
430 *retlen = currlen - 1;
450 for (strln = 0; value[strln]; ++strln)
452 padlen = min - strln;
455 if (flags & DP_F_MINUS)
458 while ((padlen > 0) && (cnt < max)) {
459 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
463 while (*value && (cnt < max)) {
464 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
467 while ((padlen < 0) && (cnt < max)) {
468 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
487 const char *prefix = "";
488 unsigned LLONG uvalue;
489 char convert[DECIMAL_SIZE(value)+3];
498 if (!(flags & DP_F_UNSIGNED)) {
502 } else if (flags & DP_F_PLUS)
504 else if (flags & DP_F_SPACE)
507 if (flags & DP_F_NUM) {
508 if (base == 8) prefix = "0";
509 if (base == 16) prefix = "0x";
515 (caps ? "0123456789ABCDEF" : "0123456789abcdef")
516 [uvalue % (unsigned) base];
517 uvalue = (uvalue / (unsigned) base);
518 } while (uvalue && (place < (int)sizeof(convert)));
519 if (place == sizeof(convert))
523 zpadlen = max - place;
524 spadlen = min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
529 if (flags & DP_F_ZERO) {
530 zpadlen = OSSL_MAX(zpadlen, spadlen);
533 if (flags & DP_F_MINUS)
537 while (spadlen > 0) {
538 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
544 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
548 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
554 while (zpadlen > 0) {
555 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
561 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
563 /* left justified spaces */
564 while (spadlen < 0) {
565 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
572 abs_val(LDOUBLE value)
574 LDOUBLE result = value;
592 roundv(LDOUBLE value)
595 intpart = (long) value;
596 value = value - intpart;
628 ufvalue = abs_val(fvalue);
631 else if (flags & DP_F_PLUS)
633 else if (flags & DP_F_SPACE)
636 intpart = (long)ufvalue;
638 /* sorry, we only support 9 digits past the decimal because of our
643 /* we "cheat" by converting the fractional part to integer by
644 multiplying by a factor of 10 */
645 max10 = roundv(pow_10(max));
646 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
648 if (fracpart >= max10) {
653 /* convert integer part */
656 (caps ? "0123456789ABCDEF"
657 : "0123456789abcdef")[intpart % 10];
658 intpart = (intpart / 10);
659 } while (intpart && (iplace < (int)sizeof(iconvert)));
660 if (iplace == sizeof iconvert)
662 iconvert[iplace] = 0;
664 /* convert fractional part */
667 (caps ? "0123456789ABCDEF"
668 : "0123456789abcdef")[fracpart % 10];
669 fracpart = (fracpart / 10);
670 } while (fplace < max);
671 if (fplace == sizeof fconvert)
673 fconvert[fplace] = 0;
675 /* -1 for decimal point, another -1 if we are printing a sign */
676 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
677 zpadlen = max - fplace;
682 if (flags & DP_F_MINUS)
685 if ((flags & DP_F_ZERO) && (padlen > 0)) {
687 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
692 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
697 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
701 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
704 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
707 * Decimal point. This should probably use locale to find the correct
710 if (max > 0 || (flags & DP_F_NUM)) {
711 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
714 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
716 while (zpadlen > 0) {
717 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
722 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
735 /* If we haven't at least one buffer, someone has doe a big booboo */
736 assert(*sbuffer != NULL || buffer != NULL);
739 while (*currlen >= *maxlen) {
740 if (*buffer == NULL) {
743 *buffer = OPENSSL_malloc(*maxlen);
745 assert(*sbuffer != NULL);
746 memcpy(*buffer, *sbuffer, *currlen);
751 *buffer = OPENSSL_realloc(*buffer, *maxlen);
754 /* What to do if *buffer is NULL? */
755 assert(*sbuffer != NULL || *buffer != NULL);
758 if (*currlen < *maxlen) {
760 (*sbuffer)[(*currlen)++] = (char)c;
762 (*buffer)[(*currlen)++] = (char)c;
768 /***************************************************************************/
770 EXPORT_C int BIO_printf (BIO *bio, const char *format, ...)
775 va_start(args, format);
777 ret = BIO_vprintf(bio, format, args);
783 EXPORT_C int BIO_vprintf (BIO *bio, const char *format, va_list args)
788 char hugebuf[1024*2]; /* Was previously 10k, which is unreasonable
789 in small-stack environments, like threads
794 char *hugebufp = hugebuf;
795 size_t hugebufsize = sizeof(hugebuf);
800 CRYPTO_push_info("doapr()");
801 _dopr(&hugebufp, &dynbuf, &hugebufsize,
802 &retlen, &ignored, format, args);
805 ret=BIO_write(bio, dynbuf, (int)retlen);
806 OPENSSL_free(dynbuf);
810 ret=BIO_write(bio, hugebuf, (int)retlen);
816 /* As snprintf is not available everywhere, we provide our own implementation.
817 * This function has nothing to do with BIOs, but it's closely related
818 * to BIO_printf, and we need *some* name prefix ...
819 * (XXX the function should be renamed, but to what?) */
820 EXPORT_C int BIO_snprintf(char *buf, size_t n, const char *format, ...)
825 va_start(args, format);
827 ret = BIO_vsnprintf(buf, n, format, args);
833 EXPORT_C int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
838 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
841 /* In case of truncation, return -1 like traditional snprintf.
842 * (Current drafts for ISO/IEC 9899 say snprintf should return
843 * the number of characters that would have been written,
844 * had the buffer been large enough.) */
847 return (retlen <= INT_MAX) ? (int)retlen : -1;