2 * Copyright 1995-2022 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 "crypto/ctype.h"
14 #include "internal/numbers.h"
15 #include <openssl/bio.h>
16 #include <openssl/configuration.h>
19 * Copyright Patrick Powell 1995
20 * This code is based on code written by Patrick Powell <papowell@astart.com>
21 * It may be used for any purpose as long as this notice remains intact
22 * on all source code distributions.
25 #ifdef HAVE_LONG_DOUBLE
26 # define LDOUBLE long double
28 # define LDOUBLE double
31 static int fmtstr(char **, char **, size_t *, size_t *,
32 const char *, int, int, int);
33 static int fmtint(char **, char **, size_t *, size_t *,
34 int64_t, int, int, int, int);
35 #ifndef OPENSSL_SYS_UEFI
36 static int fmtfp(char **, char **, size_t *, size_t *,
37 LDOUBLE, int, int, int, int);
39 static int doapr_outch(char **, char **, size_t *, size_t *, int);
40 static int _dopr(char **sbuffer, char **buffer,
41 size_t *maxlen, size_t *retlen, int *truncated,
42 const char *format, va_list args);
44 /* format read states */
45 #define DP_S_DEFAULT 0
54 /* format flags - Bits */
55 /* left-aligned padding */
56 #define DP_F_MINUS (1 << 0)
57 /* print an explicit '+' for a value with positive sign */
58 #define DP_F_PLUS (1 << 1)
59 /* print an explicit ' ' for a value with positive sign */
60 #define DP_F_SPACE (1 << 2)
61 /* print 0/0x prefix for octal/hex and decimal point for floating point */
62 #define DP_F_NUM (1 << 3)
63 /* print leading zeroes */
64 #define DP_F_ZERO (1 << 4)
65 /* print HEX in UPPPERcase */
66 #define DP_F_UP (1 << 5)
67 /* treat value as unsigned */
68 #define DP_F_UNSIGNED (1 << 6)
70 /* conversion flags */
73 #define DP_C_LDOUBLE 3
77 /* Floating point formats */
82 /* some handy macros */
83 #define char_to_int(p) (p - '0')
84 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
90 size_t *retlen, int *truncated, const char *format, va_list args)
94 #ifndef OPENSSL_SYS_UEFI
105 state = DP_S_DEFAULT;
106 flags = currlen = cflags = min = 0;
110 while (state != DP_S_DONE) {
111 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
119 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
151 if (ossl_isdigit(ch)) {
152 min = 10 * min + char_to_int(ch);
154 } else if (ch == '*') {
155 min = va_arg(args, int);
169 if (ossl_isdigit(ch)) {
172 max = 10 * max + char_to_int(ch);
174 } else if (ch == '*') {
175 max = va_arg(args, int);
188 if (*format == 'l') {
201 cflags = DP_C_LDOUBLE;
219 value = (short int)va_arg(args, int);
222 value = va_arg(args, long int);
225 value = va_arg(args, int64_t);
228 value = va_arg(args, ossl_ssize_t);
231 value = va_arg(args, int);
234 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
244 flags |= DP_F_UNSIGNED;
247 value = (unsigned short int)va_arg(args, unsigned int);
250 value = va_arg(args, unsigned long int);
253 value = va_arg(args, uint64_t);
256 value = va_arg(args, size_t);
259 value = va_arg(args, unsigned int);
262 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
263 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
267 #ifndef OPENSSL_SYS_UEFI
269 if (cflags == DP_C_LDOUBLE)
270 fvalue = va_arg(args, LDOUBLE);
272 fvalue = va_arg(args, double);
273 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
281 if (cflags == DP_C_LDOUBLE)
282 fvalue = va_arg(args, LDOUBLE);
284 fvalue = va_arg(args, double);
285 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
293 if (cflags == DP_C_LDOUBLE)
294 fvalue = va_arg(args, LDOUBLE);
296 fvalue = va_arg(args, double);
297 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
307 /* not implemented for UEFI */
308 ERR_raise(ERR_LIB_BIO, ERR_R_UNSUPPORTED);
312 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
317 strvalue = va_arg(args, char *);
324 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
329 value = (size_t)va_arg(args, void *);
330 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
331 value, 16, min, max, flags | DP_F_NUM))
337 num = va_arg(args, int *);
342 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
346 /* not supported yet, treat as next char */
354 state = DP_S_DEFAULT;
355 flags = cflags = min = 0;
365 * We have to truncate if there is no dynamic buffer and we have filled the
368 if (buffer == NULL) {
369 *truncated = (currlen > *maxlen - 1);
371 currlen = *maxlen - 1;
373 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
375 *retlen = currlen - 1;
380 fmtstr(char **sbuffer,
383 size_t *maxlen, const char *value, int flags, int min, int max)
392 strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
394 padlen = min - strln;
395 if (min < 0 || padlen < 0)
399 * Calculate the maximum output including padding.
400 * Make sure max doesn't overflow into negativity
402 if (max < INT_MAX - padlen)
407 if (flags & DP_F_MINUS)
410 while ((padlen > 0) && (max < 0 || cnt < max)) {
411 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
416 while (strln > 0 && (max < 0 || cnt < max)) {
417 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
422 while ((padlen < 0) && (max < 0 || cnt < max)) {
423 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
432 fmtint(char **sbuffer,
435 size_t *maxlen, int64_t value, int base, int min, int max, int flags)
438 const char *prefix = "";
440 char convert[DECIMAL_SIZE(value) + 3];
449 if (!(flags & DP_F_UNSIGNED)) {
452 uvalue = 0 - (uint64_t)value;
453 } else if (flags & DP_F_PLUS)
455 else if (flags & DP_F_SPACE)
458 if (flags & DP_F_NUM) {
467 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
468 [uvalue % (unsigned)base];
469 uvalue = (uvalue / (unsigned)base);
470 } while (uvalue && (place < (int)sizeof(convert)));
471 if (place == sizeof(convert))
475 zpadlen = max - place;
477 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
482 if (flags & DP_F_ZERO) {
483 zpadlen = OSSL_MAX(zpadlen, spadlen);
486 if (flags & DP_F_MINUS)
490 while (spadlen > 0) {
491 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
498 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
503 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
510 while (zpadlen > 0) {
511 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
518 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
522 /* left justified spaces */
523 while (spadlen < 0) {
524 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
531 #ifndef OPENSSL_SYS_UEFI
533 static LDOUBLE abs_val(LDOUBLE value)
535 LDOUBLE result = value;
541 static LDOUBLE pow_10(int in_exp)
551 static long roundv(LDOUBLE value)
554 intpart = (long)value;
555 value = value - intpart;
562 fmtfp(char **sbuffer,
565 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
579 unsigned long intpart;
580 unsigned long fracpart;
589 else if (flags & DP_F_PLUS)
591 else if (flags & DP_F_SPACE)
595 * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
596 * depending on the number to be printed. Work out which one it is and use
599 if (style == G_FORMAT) {
601 realstyle = F_FORMAT;
602 } else if (fvalue < 0.0001) {
603 realstyle = E_FORMAT;
604 } else if ((max == 0 && fvalue >= 10)
605 || (max > 0 && fvalue >= pow_10(max))) {
606 realstyle = E_FORMAT;
608 realstyle = F_FORMAT;
614 if (style != F_FORMAT) {
616 /* Calculate the exponent */
618 while (tmpvalue < 1) {
622 while (tmpvalue > 10) {
627 if (style == G_FORMAT) {
629 * In G_FORMAT the "precision" represents significant digits. We
630 * always have at least 1 significant digit.
634 /* Now convert significant digits to decimal places */
635 if (realstyle == F_FORMAT) {
639 * Should not happen. If we're in F_FORMAT then exp < max?
641 (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
646 * In E_FORMAT there is always one significant digit in front
647 * of the decimal point, so:
648 * significant digits == 1 + decimal places
653 if (realstyle == E_FORMAT)
656 ufvalue = abs_val(fvalue);
658 * By subtracting 65535 (2^16-1) we cancel the low order 15 bits
659 * of ULONG_MAX to avoid using imprecise floating point values.
661 if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
663 (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
666 intpart = (unsigned long)ufvalue;
669 * sorry, we only support 9 digits past the decimal because of our
676 * we "cheat" by converting the fractional part to integer by multiplying
679 max10 = roundv(pow_10(max));
680 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
682 if (fracpart >= max10) {
687 /* convert integer part */
689 iconvert[iplace++] = "0123456789"[intpart % 10];
690 intpart = (intpart / 10);
691 } while (intpart && (iplace < (int)sizeof(iconvert)));
692 if (iplace == sizeof(iconvert))
694 iconvert[iplace] = 0;
696 /* convert fractional part */
697 while (fplace < max) {
698 if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
699 /* We strip trailing zeros in G_FORMAT */
701 fracpart = fracpart / 10;
706 fconvert[fplace++] = "0123456789"[fracpart % 10];
707 fracpart = (fracpart / 10);
710 if (fplace == sizeof(fconvert))
712 fconvert[fplace] = 0;
714 /* convert exponent part */
715 if (realstyle == E_FORMAT) {
723 econvert[eplace++] = "0123456789"[tmpexp % 10];
724 tmpexp = (tmpexp / 10);
725 } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
726 /* Exponent is huge!! Too big to print */
728 (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
731 /* Add a leading 0 for single digit exponents */
733 econvert[eplace++] = '0';
737 * -1 for decimal point (if we have one, i.e. max > 0),
738 * another -1 if we are printing a sign
740 padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
741 /* Take some off for exponent prefix "+e" and exponent */
742 if (realstyle == E_FORMAT)
743 padlen -= 2 + eplace;
744 zpadlen = max - fplace;
749 if (flags & DP_F_MINUS)
752 if ((flags & DP_F_ZERO) && (padlen > 0)) {
754 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
760 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
766 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
770 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
774 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
779 * Decimal point. This should probably use locale to find the correct
782 if (max > 0 || (flags & DP_F_NUM)) {
783 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
787 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
792 while (zpadlen > 0) {
793 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
797 if (realstyle == E_FORMAT) {
800 if ((flags & DP_F_UP) == 0)
804 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
807 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
810 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
814 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
821 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
828 #endif /* OPENSSL_SYS_UEFI */
830 #define BUFFER_INC 1024
833 doapr_outch(char **sbuffer,
834 char **buffer, size_t *currlen, size_t *maxlen, int c)
836 /* If we haven't at least one buffer, someone has done a big booboo */
837 if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
840 /* |currlen| must always be <= |*maxlen| */
841 if (!ossl_assert(*currlen <= *maxlen))
844 if (buffer && *currlen == *maxlen) {
845 if (*maxlen > INT_MAX - BUFFER_INC)
848 *maxlen += BUFFER_INC;
849 if (*buffer == NULL) {
850 if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
851 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
855 if (!ossl_assert(*sbuffer != NULL))
857 memcpy(*buffer, *sbuffer, *currlen);
863 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
864 if (tmpbuf == NULL) {
865 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
872 if (*currlen < *maxlen) {
874 (*sbuffer)[(*currlen)++] = (char)c;
876 (*buffer)[(*currlen)++] = (char)c;
882 /***************************************************************************/
884 int BIO_printf(BIO *bio, const char *format, ...)
889 va_start(args, format);
891 ret = BIO_vprintf(bio, format, args);
897 int BIO_vprintf(BIO *bio, const char *format, va_list args)
901 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
902 * in small-stack environments, like threads
903 * or DOS programs. */
904 char *hugebufp = hugebuf;
905 size_t hugebufsize = sizeof(hugebuf);
910 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
912 OPENSSL_free(dynbuf);
916 ret = BIO_write(bio, dynbuf, (int)retlen);
917 OPENSSL_free(dynbuf);
919 ret = BIO_write(bio, hugebuf, (int)retlen);
925 * As snprintf is not available everywhere, we provide our own
926 * implementation. This function has nothing to do with BIOs, but it's
927 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
928 * function should be renamed, but to what?)
930 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
935 va_start(args, format);
937 ret = BIO_vsnprintf(buf, n, format, args);
943 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
948 if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
953 * In case of truncation, return -1 like traditional snprintf.
954 * (Current drafts for ISO/IEC 9899 say snprintf should return the
955 * number of characters that would have been written, had the buffer
956 * been large enough.)
959 return (retlen <= INT_MAX) ? (int)retlen : -1;