2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include "internal/ctype.h"
14 #include "internal/numbers.h"
15 #include <openssl/bio.h>
18 * Copyright Patrick Powell 1995
19 * This code is based on code written by Patrick Powell <papowell@astart.com>
20 * It may be used for any purpose as long as this notice remains intact
21 * on all source code distributions.
24 #ifdef HAVE_LONG_DOUBLE
25 # define LDOUBLE long double
27 # define LDOUBLE double
30 static int fmtstr(char **, char **, size_t *, size_t *,
31 const char *, int, int, int);
32 static int fmtint(char **, char **, size_t *, size_t *,
33 int64_t, int, int, int, int);
34 static int fmtfp(char **, char **, size_t *, size_t *,
35 LDOUBLE, int, int, int, int);
36 static int doapr_outch(char **, char **, size_t *, size_t *, int);
37 static int _dopr(char **sbuffer, char **buffer,
38 size_t *maxlen, size_t *retlen, int *truncated,
39 const char *format, va_list args);
41 /* format read states */
42 #define DP_S_DEFAULT 0
51 /* format flags - Bits */
52 /* left-aligned padding */
53 #define DP_F_MINUS (1 << 0)
54 /* print an explicit '+' for a value with positive sign */
55 #define DP_F_PLUS (1 << 1)
56 /* print an explicit ' ' for a value with positive sign */
57 #define DP_F_SPACE (1 << 2)
58 /* print 0/0x prefix for octal/hex and decimal point for floating point */
59 #define DP_F_NUM (1 << 3)
60 /* print leading zeroes */
61 #define DP_F_ZERO (1 << 4)
62 /* print HEX in UPPPERcase */
63 #define DP_F_UP (1 << 5)
64 /* treat value as unsigned */
65 #define DP_F_UNSIGNED (1 << 6)
67 /* conversion flags */
70 #define DP_C_LDOUBLE 3
74 /* Floating point formats */
79 /* some handy macros */
80 #define char_to_int(p) (p - '0')
81 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
87 size_t *retlen, int *truncated, const char *format, va_list args)
100 state = DP_S_DEFAULT;
101 flags = currlen = cflags = min = 0;
105 while (state != DP_S_DONE) {
106 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
114 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
146 if (ossl_isdigit(ch)) {
147 min = 10 * min + char_to_int(ch);
149 } else if (ch == '*') {
150 min = va_arg(args, int);
164 if (ossl_isdigit(ch)) {
167 max = 10 * max + char_to_int(ch);
169 } else if (ch == '*') {
170 max = va_arg(args, int);
183 if (*format == 'l') {
196 cflags = DP_C_LDOUBLE;
214 value = (short int)va_arg(args, int);
217 value = va_arg(args, long int);
220 value = va_arg(args, int64_t);
223 value = va_arg(args, ossl_ssize_t);
226 value = va_arg(args, int);
229 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
239 flags |= DP_F_UNSIGNED;
242 value = (unsigned short int)va_arg(args, unsigned int);
245 value = va_arg(args, unsigned long int);
248 value = va_arg(args, uint64_t);
251 value = va_arg(args, size_t);
254 value = va_arg(args, unsigned int);
257 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
258 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
263 if (cflags == DP_C_LDOUBLE)
264 fvalue = va_arg(args, LDOUBLE);
266 fvalue = va_arg(args, double);
267 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
275 if (cflags == DP_C_LDOUBLE)
276 fvalue = va_arg(args, LDOUBLE);
278 fvalue = va_arg(args, double);
279 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
287 if (cflags == DP_C_LDOUBLE)
288 fvalue = va_arg(args, LDOUBLE);
290 fvalue = va_arg(args, double);
291 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
296 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
301 strvalue = va_arg(args, char *);
308 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
313 value = (size_t)va_arg(args, void *);
314 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
315 value, 16, min, max, flags | DP_F_NUM))
321 num = va_arg(args, int *);
326 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
330 /* not supported yet, treat as next char */
338 state = DP_S_DEFAULT;
339 flags = cflags = min = 0;
349 * We have to truncate if there is no dynamic buffer and we have filled the
352 if (buffer == NULL) {
353 *truncated = (currlen > *maxlen - 1);
355 currlen = *maxlen - 1;
357 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
359 *retlen = currlen - 1;
364 fmtstr(char **sbuffer,
367 size_t *maxlen, const char *value, int flags, int min, int max)
376 strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
378 padlen = min - strln;
379 if (min < 0 || padlen < 0)
383 * Calculate the maximum output including padding.
384 * Make sure max doesn't overflow into negativity
386 if (max < INT_MAX - padlen)
391 if (flags & DP_F_MINUS)
394 while ((padlen > 0) && (max < 0 || cnt < max)) {
395 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
400 while (strln > 0 && (max < 0 || cnt < max)) {
401 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
406 while ((padlen < 0) && (max < 0 || cnt < max)) {
407 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
416 fmtint(char **sbuffer,
419 size_t *maxlen, int64_t value, int base, int min, int max, int flags)
422 const char *prefix = "";
424 char convert[DECIMAL_SIZE(value) + 3];
433 if (!(flags & DP_F_UNSIGNED)) {
436 uvalue = 0 - (uint64_t)value;
437 } else if (flags & DP_F_PLUS)
439 else if (flags & DP_F_SPACE)
442 if (flags & DP_F_NUM) {
451 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
452 [uvalue % (unsigned)base];
453 uvalue = (uvalue / (unsigned)base);
454 } while (uvalue && (place < (int)sizeof(convert)));
455 if (place == sizeof(convert))
459 zpadlen = max - place;
461 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
466 if (flags & DP_F_ZERO) {
467 zpadlen = OSSL_MAX(zpadlen, spadlen);
470 if (flags & DP_F_MINUS)
474 while (spadlen > 0) {
475 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
482 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
487 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
494 while (zpadlen > 0) {
495 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
502 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
506 /* left justified spaces */
507 while (spadlen < 0) {
508 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
515 static LDOUBLE abs_val(LDOUBLE value)
517 LDOUBLE result = value;
523 static LDOUBLE pow_10(int in_exp)
533 static long roundv(LDOUBLE value)
536 intpart = (long)value;
537 value = value - intpart;
544 fmtfp(char **sbuffer,
547 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
561 unsigned long intpart;
562 unsigned long fracpart;
571 else if (flags & DP_F_PLUS)
573 else if (flags & DP_F_SPACE)
577 * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
578 * depending on the number to be printed. Work out which one it is and use
581 if (style == G_FORMAT) {
583 realstyle = F_FORMAT;
584 } else if (fvalue < 0.0001) {
585 realstyle = E_FORMAT;
586 } else if ((max == 0 && fvalue >= 10)
587 || (max > 0 && fvalue >= pow_10(max))) {
588 realstyle = E_FORMAT;
590 realstyle = F_FORMAT;
596 if (style != F_FORMAT) {
598 /* Calculate the exponent */
600 while (tmpvalue < 1) {
604 while (tmpvalue > 10) {
609 if (style == G_FORMAT) {
611 * In G_FORMAT the "precision" represents significant digits. We
612 * always have at least 1 significant digit.
616 /* Now convert significant digits to decimal places */
617 if (realstyle == F_FORMAT) {
621 * Should not happen. If we're in F_FORMAT then exp < max?
627 * In E_FORMAT there is always one significant digit in front
628 * of the decimal point, so:
629 * significant digits == 1 + decimal places
634 if (realstyle == E_FORMAT)
637 ufvalue = abs_val(fvalue);
638 if (ufvalue > ULONG_MAX) {
642 intpart = (unsigned long)ufvalue;
645 * sorry, we only support 9 digits past the decimal because of our
652 * we "cheat" by converting the fractional part to integer by multiplying
655 max10 = roundv(pow_10(max));
656 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
658 if (fracpart >= max10) {
663 /* convert integer part */
665 iconvert[iplace++] = "0123456789"[intpart % 10];
666 intpart = (intpart / 10);
667 } while (intpart && (iplace < (int)sizeof(iconvert)));
668 if (iplace == sizeof(iconvert))
670 iconvert[iplace] = 0;
672 /* convert fractional part */
673 while (fplace < max) {
674 if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
675 /* We strip trailing zeros in G_FORMAT */
677 fracpart = fracpart / 10;
682 fconvert[fplace++] = "0123456789"[fracpart % 10];
683 fracpart = (fracpart / 10);
686 if (fplace == sizeof(fconvert))
688 fconvert[fplace] = 0;
690 /* convert exponent part */
691 if (realstyle == E_FORMAT) {
699 econvert[eplace++] = "0123456789"[tmpexp % 10];
700 tmpexp = (tmpexp / 10);
701 } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
702 /* Exponent is huge!! Too big to print */
705 /* Add a leading 0 for single digit exponents */
707 econvert[eplace++] = '0';
711 * -1 for decimal point (if we have one, i.e. max > 0),
712 * another -1 if we are printing a sign
714 padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
715 /* Take some off for exponent prefix "+e" and exponent */
716 if (realstyle == E_FORMAT)
717 padlen -= 2 + eplace;
718 zpadlen = max - fplace;
723 if (flags & DP_F_MINUS)
726 if ((flags & DP_F_ZERO) && (padlen > 0)) {
728 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
734 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
740 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
744 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
748 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
753 * Decimal point. This should probably use locale to find the correct
756 if (max > 0 || (flags & DP_F_NUM)) {
757 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
761 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
766 while (zpadlen > 0) {
767 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
771 if (realstyle == E_FORMAT) {
774 if ((flags & DP_F_UP) == 0)
778 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
781 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
784 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
788 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
795 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
802 #define BUFFER_INC 1024
805 doapr_outch(char **sbuffer,
806 char **buffer, size_t *currlen, size_t *maxlen, int c)
808 /* If we haven't at least one buffer, someone has done a big booboo */
809 if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
812 /* |currlen| must always be <= |*maxlen| */
813 if (!ossl_assert(*currlen <= *maxlen))
816 if (buffer && *currlen == *maxlen) {
817 if (*maxlen > INT_MAX - BUFFER_INC)
820 *maxlen += BUFFER_INC;
821 if (*buffer == NULL) {
822 if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
823 BIOerr(BIO_F_DOAPR_OUTCH, ERR_R_MALLOC_FAILURE);
827 if (!ossl_assert(*sbuffer != NULL))
829 memcpy(*buffer, *sbuffer, *currlen);
834 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
841 if (*currlen < *maxlen) {
843 (*sbuffer)[(*currlen)++] = (char)c;
845 (*buffer)[(*currlen)++] = (char)c;
851 /***************************************************************************/
853 int BIO_printf(BIO *bio, const char *format, ...)
858 va_start(args, format);
860 ret = BIO_vprintf(bio, format, args);
866 int BIO_vprintf(BIO *bio, const char *format, va_list args)
870 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
871 * in small-stack environments, like threads
872 * or DOS programs. */
873 char *hugebufp = hugebuf;
874 size_t hugebufsize = sizeof(hugebuf);
879 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
881 OPENSSL_free(dynbuf);
885 ret = BIO_write(bio, dynbuf, (int)retlen);
886 OPENSSL_free(dynbuf);
888 ret = BIO_write(bio, hugebuf, (int)retlen);
894 * As snprintf is not available everywhere, we provide our own
895 * implementation. This function has nothing to do with BIOs, but it's
896 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
897 * function should be renamed, but to what?)
899 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
904 va_start(args, format);
906 ret = BIO_vsnprintf(buf, n, format, args);
912 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
917 if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
922 * In case of truncation, return -1 like traditional snprintf.
923 * (Current drafts for ISO/IEC 9899 say snprintf should return the
924 * number of characters that would have been written, had the buffer
925 * been large enough.)
929 return (retlen <= INT_MAX) ? (int)retlen : -1;