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.
66 #include <openssl/bio.h>
68 static void dopr (char *buffer, size_t maxlen, const char *format,
71 int BIO_printf (BIO *bio, ...)
76 MS_STATIC char hugebuf[1024*2]; /* 10k in one chunk is the limit */
79 format=va_arg(args, char *);
83 dopr(hugebuf, sizeof(hugebuf), format, args);
85 ret=BIO_write(bio,hugebuf,strlen(hugebuf));
92 * Copyright Patrick Powell 1995
93 * This code is based on code written by Patrick Powell (papowell@astart.com)
94 * It may be used for any purpose as long as this notice remains intact
95 * on all source code distributions
100 #include <sys/types.h>
103 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
104 char *value, int flags, int min, int max);
105 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
106 long value, int base, int min, int max, int flags);
107 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
108 long double fvalue, int min, int max, int flags);
109 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c );
112 * dopr(): poor man's version of doprintf
115 /* format read states */
116 #define DP_S_DEFAULT 0
125 /* format flags - Bits */
126 #define DP_F_MINUS (1 << 0)
127 #define DP_F_PLUS (1 << 1)
128 #define DP_F_SPACE (1 << 2)
129 #define DP_F_NUM (1 << 3)
130 #define DP_F_ZERO (1 << 4)
131 #define DP_F_UP (1 << 5)
132 #define DP_F_UNSIGNED (1 << 6)
134 /* Conversion Flags */
137 #define DP_C_LDOUBLE 3
139 #define char_to_int(p) (p - '0')
140 #define MAX(p,q) ((p >= q) ? p : q)
142 static void dopr (char *buffer, size_t maxlen, const char *format, va_list args)
155 state = DP_S_DEFAULT;
156 currlen = flags = cflags = min = 0;
160 while (state != DP_S_DONE)
162 if ((ch == '\0') || (currlen >= maxlen))
171 dopr_outch (buffer, &currlen, maxlen, ch);
203 if (isdigit((unsigned char)ch))
205 min = 10*min + char_to_int (ch);
210 min = va_arg (args, int);
227 if (isdigit((unsigned char)ch))
231 max = 10*max + char_to_int (ch);
236 max = va_arg (args, int);
244 /* Currently, we don't support Long Long, bummer */
256 cflags = DP_C_LDOUBLE;
269 if (cflags == DP_C_SHORT)
270 value = va_arg (args, short int);
271 else if (cflags == DP_C_LONG)
272 value = va_arg (args, long int);
274 value = va_arg (args, int);
275 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
278 flags |= DP_F_UNSIGNED;
279 if (cflags == DP_C_SHORT)
280 value = va_arg (args, unsigned short int);
281 else if (cflags == DP_C_LONG)
282 value = va_arg (args, unsigned long int);
284 value = va_arg (args, unsigned int);
285 fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags);
288 flags |= DP_F_UNSIGNED;
289 if (cflags == DP_C_SHORT)
290 value = va_arg (args, unsigned short int);
291 else if (cflags == DP_C_LONG)
292 value = va_arg (args, unsigned long int);
294 value = va_arg (args, unsigned int);
295 fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags);
300 flags |= DP_F_UNSIGNED;
301 if (cflags == DP_C_SHORT)
302 value = va_arg (args, unsigned short int);
303 else if (cflags == DP_C_LONG)
304 value = va_arg (args, unsigned long int);
306 value = va_arg (args, unsigned int);
307 fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags);
310 if (cflags == DP_C_LDOUBLE)
311 fvalue = va_arg (args, long double);
313 fvalue = va_arg (args, double);
314 /* um, floating point? */
315 fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags);
320 if (cflags == DP_C_LDOUBLE)
321 fvalue = va_arg (args, long double);
323 fvalue = va_arg (args, double);
328 if (cflags == DP_C_LDOUBLE)
329 fvalue = va_arg (args, long double);
331 fvalue = va_arg (args, double);
334 dopr_outch (buffer, &currlen, maxlen, va_arg (args, int));
337 strvalue = va_arg (args, char *);
339 max = maxlen; /* ie, no max */
340 fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max);
343 strvalue = va_arg (args, void *);
344 fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags);
347 if (cflags == DP_C_SHORT)
350 num = va_arg (args, short int *);
353 else if (cflags == DP_C_LONG)
356 num = va_arg (args, long int *);
362 num = va_arg (args, int *);
367 dopr_outch (buffer, &currlen, maxlen, ch);
370 /* not supported yet, treat as next char */
378 state = DP_S_DEFAULT;
379 flags = cflags = min = 0;
386 break; /* some picky compilers need this */
389 if (currlen < maxlen - 1)
390 buffer[currlen] = '\0';
392 buffer[maxlen - 1] = '\0';
395 static void fmtstr (char *buffer, size_t *currlen, size_t maxlen,
396 char *value, int flags, int min, int max)
398 int padlen, strln; /* amount to pad */
406 for (strln = 0; value[strln]; ++strln); /* strlen */
407 padlen = min - strln;
410 if (flags & DP_F_MINUS)
411 padlen = -padlen; /* Left Justify */
413 while ((padlen > 0) && (cnt < max))
415 dopr_outch (buffer, currlen, maxlen, ' ');
419 while (*value && (cnt < max))
421 dopr_outch (buffer, currlen, maxlen, *value++);
424 while ((padlen < 0) && (cnt < max))
426 dopr_outch (buffer, currlen, maxlen, ' ');
432 /* Have to handle DP_F_NUM (ie 0x and 0 alternates) */
434 static void fmtint (char *buffer, size_t *currlen, size_t maxlen,
435 long value, int base, int min, int max, int flags)
438 unsigned long uvalue;
441 int spadlen = 0; /* amount to space pad */
442 int zpadlen = 0; /* amount to zero pad */
450 if(!(flags & DP_F_UNSIGNED))
457 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
460 if (flags & DP_F_SPACE)
464 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
468 (caps? "0123456789ABCDEF":"0123456789abcdef")
469 [uvalue % (unsigned)base ];
470 uvalue = (uvalue / (unsigned)base );
471 } while(uvalue && (place < 20));
472 if (place == 20) place--;
475 zpadlen = max - place;
476 spadlen = min - MAX (max, place) - (signvalue ? 1 : 0);
477 if (zpadlen < 0) zpadlen = 0;
478 if (spadlen < 0) spadlen = 0;
479 if (flags & DP_F_ZERO)
481 zpadlen = MAX(zpadlen, spadlen);
484 if (flags & DP_F_MINUS)
485 spadlen = -spadlen; /* Left Justifty */
487 #ifdef DEBUG_SNPRINTF
488 dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n",
489 zpadlen, spadlen, min, max, place));
495 dopr_outch (buffer, currlen, maxlen, ' ');
501 dopr_outch (buffer, currlen, maxlen, signvalue);
508 dopr_outch (buffer, currlen, maxlen, '0');
515 dopr_outch (buffer, currlen, maxlen, convert[--place]);
517 /* Left Justified spaces */
518 while (spadlen < 0) {
519 dopr_outch (buffer, currlen, maxlen, ' ');
524 static long double abs_val (long double value)
526 long double result = value;
534 static long double pow10 (int exp)
536 long double result = 1;
547 static long round (long double value)
552 value = value - intpart;
559 static void fmtfp (char *buffer, size_t *currlen, size_t maxlen,
560 long double fvalue, int min, int max, int flags)
568 int padlen = 0; /* amount to pad */
575 * AIX manpage says the default is 0, but Solaris says the default
576 * is 6, and sprintf on AIX defaults to 6
581 ufvalue = abs_val (fvalue);
586 if (flags & DP_F_PLUS) /* Do a sign (+/i) */
589 if (flags & DP_F_SPACE)
593 if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */
599 * Sorry, we only support 9 digits past the decimal because of our
605 /* We "cheat" by converting the fractional part to integer by
606 * multiplying by a factor of 10
608 fracpart = round ((pow10 (max)) * (ufvalue - intpart));
610 if (fracpart >= pow10 (max))
613 fracpart -= pow10 (max);
616 #ifdef DEBUG_SNPRINTF
617 dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart));
620 /* Convert integer part */
623 (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10];
624 intpart = (intpart / 10);
625 } while(intpart && (iplace < 20));
626 if (iplace == 20) iplace--;
627 iconvert[iplace] = 0;
629 /* Convert fractional part */
632 (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10];
633 fracpart = (fracpart / 10);
634 } while(fracpart && (fplace < 20));
635 if (fplace == 20) fplace--;
636 fconvert[fplace] = 0;
638 /* -1 for decimal point, another -1 if we are printing a sign */
639 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
640 zpadlen = max - fplace;
645 if (flags & DP_F_MINUS)
646 padlen = -padlen; /* Left Justifty */
648 if ((flags & DP_F_ZERO) && (padlen > 0))
652 dopr_outch (buffer, currlen, maxlen, signvalue);
658 dopr_outch (buffer, currlen, maxlen, '0');
664 dopr_outch (buffer, currlen, maxlen, ' ');
668 dopr_outch (buffer, currlen, maxlen, signvalue);
671 dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]);
674 * Decimal point. This should probably use locale to find the correct
677 dopr_outch (buffer, currlen, maxlen, '.');
680 dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]);
684 dopr_outch (buffer, currlen, maxlen, '0');
690 dopr_outch (buffer, currlen, maxlen, ' ');
695 static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c)
697 if (*currlen < maxlen)
698 buffer[(*currlen)++] = c;