2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/opensslconf.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 */
311 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
316 strvalue = va_arg(args, char *);
323 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
328 value = (size_t)va_arg(args, void *);
329 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
330 value, 16, min, max, flags | DP_F_NUM))
336 num = va_arg(args, int *);
341 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
345 /* not supported yet, treat as next char */
353 state = DP_S_DEFAULT;
354 flags = cflags = min = 0;
364 * We have to truncate if there is no dynamic buffer and we have filled the
367 if (buffer == NULL) {
368 *truncated = (currlen > *maxlen - 1);
370 currlen = *maxlen - 1;
372 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
374 *retlen = currlen - 1;
379 fmtstr(char **sbuffer,
382 size_t *maxlen, const char *value, int flags, int min, int max)
391 strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
393 padlen = min - strln;
394 if (min < 0 || padlen < 0)
398 * Calculate the maximum output including padding.
399 * Make sure max doesn't overflow into negativity
401 if (max < INT_MAX - padlen)
406 if (flags & DP_F_MINUS)
409 while ((padlen > 0) && (max < 0 || cnt < max)) {
410 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
415 while (strln > 0 && (max < 0 || cnt < max)) {
416 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
421 while ((padlen < 0) && (max < 0 || cnt < max)) {
422 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
431 fmtint(char **sbuffer,
434 size_t *maxlen, int64_t value, int base, int min, int max, int flags)
437 const char *prefix = "";
439 char convert[DECIMAL_SIZE(value) + 3];
448 if (!(flags & DP_F_UNSIGNED)) {
451 uvalue = 0 - (uint64_t)value;
452 } else if (flags & DP_F_PLUS)
454 else if (flags & DP_F_SPACE)
457 if (flags & DP_F_NUM) {
466 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
467 [uvalue % (unsigned)base];
468 uvalue = (uvalue / (unsigned)base);
469 } while (uvalue && (place < (int)sizeof(convert)));
470 if (place == sizeof(convert))
474 zpadlen = max - place;
476 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
481 if (flags & DP_F_ZERO) {
482 zpadlen = OSSL_MAX(zpadlen, spadlen);
485 if (flags & DP_F_MINUS)
489 while (spadlen > 0) {
490 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
497 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
502 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
509 while (zpadlen > 0) {
510 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
517 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
521 /* left justified spaces */
522 while (spadlen < 0) {
523 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
530 #ifndef OPENSSL_SYS_UEFI
532 static LDOUBLE abs_val(LDOUBLE value)
534 LDOUBLE result = value;
540 static LDOUBLE pow_10(int in_exp)
550 static long roundv(LDOUBLE value)
553 intpart = (long)value;
554 value = value - intpart;
561 fmtfp(char **sbuffer,
564 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
578 unsigned long intpart;
579 unsigned long fracpart;
588 else if (flags & DP_F_PLUS)
590 else if (flags & DP_F_SPACE)
594 * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
595 * depending on the number to be printed. Work out which one it is and use
598 if (style == G_FORMAT) {
600 realstyle = F_FORMAT;
601 } else if (fvalue < 0.0001) {
602 realstyle = E_FORMAT;
603 } else if ((max == 0 && fvalue >= 10)
604 || (max > 0 && fvalue >= pow_10(max))) {
605 realstyle = E_FORMAT;
607 realstyle = F_FORMAT;
613 if (style != F_FORMAT) {
615 /* Calculate the exponent */
617 while (tmpvalue < 1) {
621 while (tmpvalue > 10) {
626 if (style == G_FORMAT) {
628 * In G_FORMAT the "precision" represents significant digits. We
629 * always have at least 1 significant digit.
633 /* Now convert significant digits to decimal places */
634 if (realstyle == F_FORMAT) {
638 * Should not happen. If we're in F_FORMAT then exp < max?
644 * In E_FORMAT there is always one significant digit in front
645 * of the decimal point, so:
646 * significant digits == 1 + decimal places
651 if (realstyle == E_FORMAT)
654 ufvalue = abs_val(fvalue);
656 * By subtracting 65535 (2^16-1) we cancel the low order 15 bits
657 * of ULONG_MAX to avoid using imprecise floating point values.
659 if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
663 intpart = (unsigned long)ufvalue;
666 * sorry, we only support 9 digits past the decimal because of our
673 * we "cheat" by converting the fractional part to integer by multiplying
676 max10 = roundv(pow_10(max));
677 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
679 if (fracpart >= max10) {
684 /* convert integer part */
686 iconvert[iplace++] = "0123456789"[intpart % 10];
687 intpart = (intpart / 10);
688 } while (intpart && (iplace < (int)sizeof(iconvert)));
689 if (iplace == sizeof(iconvert))
691 iconvert[iplace] = 0;
693 /* convert fractional part */
694 while (fplace < max) {
695 if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
696 /* We strip trailing zeros in G_FORMAT */
698 fracpart = fracpart / 10;
703 fconvert[fplace++] = "0123456789"[fracpart % 10];
704 fracpart = (fracpart / 10);
707 if (fplace == sizeof(fconvert))
709 fconvert[fplace] = 0;
711 /* convert exponent part */
712 if (realstyle == E_FORMAT) {
720 econvert[eplace++] = "0123456789"[tmpexp % 10];
721 tmpexp = (tmpexp / 10);
722 } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
723 /* Exponent is huge!! Too big to print */
726 /* Add a leading 0 for single digit exponents */
728 econvert[eplace++] = '0';
732 * -1 for decimal point (if we have one, i.e. max > 0),
733 * another -1 if we are printing a sign
735 padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
736 /* Take some off for exponent prefix "+e" and exponent */
737 if (realstyle == E_FORMAT)
738 padlen -= 2 + eplace;
739 zpadlen = max - fplace;
744 if (flags & DP_F_MINUS)
747 if ((flags & DP_F_ZERO) && (padlen > 0)) {
749 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
755 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
761 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
765 if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
769 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
774 * Decimal point. This should probably use locale to find the correct
777 if (max > 0 || (flags & DP_F_NUM)) {
778 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
782 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
787 while (zpadlen > 0) {
788 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
792 if (realstyle == E_FORMAT) {
795 if ((flags & DP_F_UP) == 0)
799 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
802 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
805 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
809 if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
816 if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
823 #endif /* OPENSSL_SYS_UEFI */
825 #define BUFFER_INC 1024
828 doapr_outch(char **sbuffer,
829 char **buffer, size_t *currlen, size_t *maxlen, int c)
831 /* If we haven't at least one buffer, someone has done a big booboo */
832 if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
835 /* |currlen| must always be <= |*maxlen| */
836 if (!ossl_assert(*currlen <= *maxlen))
839 if (buffer && *currlen == *maxlen) {
840 if (*maxlen > INT_MAX - BUFFER_INC)
843 *maxlen += BUFFER_INC;
844 if (*buffer == NULL) {
845 if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL) {
846 BIOerr(BIO_F_DOAPR_OUTCH, ERR_R_MALLOC_FAILURE);
850 if (!ossl_assert(*sbuffer != NULL))
852 memcpy(*buffer, *sbuffer, *currlen);
857 tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
864 if (*currlen < *maxlen) {
866 (*sbuffer)[(*currlen)++] = (char)c;
868 (*buffer)[(*currlen)++] = (char)c;
874 /***************************************************************************/
876 int BIO_printf(BIO *bio, const char *format, ...)
881 va_start(args, format);
883 ret = BIO_vprintf(bio, format, args);
889 int BIO_vprintf(BIO *bio, const char *format, va_list args)
893 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
894 * in small-stack environments, like threads
895 * or DOS programs. */
896 char *hugebufp = hugebuf;
897 size_t hugebufsize = sizeof(hugebuf);
902 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
904 OPENSSL_free(dynbuf);
908 ret = BIO_write(bio, dynbuf, (int)retlen);
909 OPENSSL_free(dynbuf);
911 ret = BIO_write(bio, hugebuf, (int)retlen);
917 * As snprintf is not available everywhere, we provide our own
918 * implementation. This function has nothing to do with BIOs, but it's
919 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
920 * function should be renamed, but to what?)
922 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
927 va_start(args, format);
929 ret = BIO_vsnprintf(buf, n, format, args);
935 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
940 if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
945 * In case of truncation, return -1 like traditional snprintf.
946 * (Current drafts for ISO/IEC 9899 say snprintf should return the
947 * number of characters that would have been written, had the buffer
948 * been large enough.)
952 return (retlen <= INT_MAX) ? (int)retlen : -1;