xref: /openssl/crypto/bio/bio_print.c (revision 23def9d3)
1 /*
2  * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
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>
17 
18 /*
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.
23  */
24 
25 #ifdef HAVE_LONG_DOUBLE
26 # define LDOUBLE long double
27 #else
28 # define LDOUBLE double
29 #endif
30 
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);
38 #endif
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);
43 
44 /* format read states */
45 #define DP_S_DEFAULT    0
46 #define DP_S_FLAGS      1
47 #define DP_S_MIN        2
48 #define DP_S_DOT        3
49 #define DP_S_MAX        4
50 #define DP_S_MOD        5
51 #define DP_S_CONV       6
52 #define DP_S_DONE       7
53 
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 UPPERcase */
66 #define DP_F_UP         (1 << 5)
67 /* treat value as unsigned */
68 #define DP_F_UNSIGNED   (1 << 6)
69 
70 /* conversion flags */
71 #define DP_C_SHORT      1
72 #define DP_C_LONG       2
73 #define DP_C_LDOUBLE    3
74 #define DP_C_LLONG      4
75 #define DP_C_SIZE       5
76 
77 /* Floating point formats */
78 #define F_FORMAT        0
79 #define E_FORMAT        1
80 #define G_FORMAT        2
81 
82 /* some handy macros */
83 #define char_to_int(p) (p - '0')
84 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
85 
86 static int
_dopr(char ** sbuffer,char ** buffer,size_t * maxlen,size_t * retlen,int * truncated,const char * format,va_list args)87 _dopr(char **sbuffer,
88       char **buffer,
89       size_t *maxlen,
90       size_t *retlen, int *truncated, const char *format, va_list args)
91 {
92     char ch;
93     int64_t value;
94 #ifndef OPENSSL_SYS_UEFI
95     LDOUBLE fvalue;
96 #endif
97     char *strvalue;
98     int min;
99     int max;
100     int state;
101     int flags;
102     int cflags;
103     size_t currlen;
104 
105     state = DP_S_DEFAULT;
106     flags = currlen = cflags = min = 0;
107     max = -1;
108     ch = *format++;
109 
110     while (state != DP_S_DONE) {
111         if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
112             state = DP_S_DONE;
113 
114         switch (state) {
115         case DP_S_DEFAULT:
116             if (ch == '%')
117                 state = DP_S_FLAGS;
118             else
119                 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
120                     return 0;
121             ch = *format++;
122             break;
123         case DP_S_FLAGS:
124             switch (ch) {
125             case '-':
126                 flags |= DP_F_MINUS;
127                 ch = *format++;
128                 break;
129             case '+':
130                 flags |= DP_F_PLUS;
131                 ch = *format++;
132                 break;
133             case ' ':
134                 flags |= DP_F_SPACE;
135                 ch = *format++;
136                 break;
137             case '#':
138                 flags |= DP_F_NUM;
139                 ch = *format++;
140                 break;
141             case '0':
142                 flags |= DP_F_ZERO;
143                 ch = *format++;
144                 break;
145             default:
146                 state = DP_S_MIN;
147                 break;
148             }
149             break;
150         case DP_S_MIN:
151             if (ossl_isdigit(ch)) {
152                 min = 10 * min + char_to_int(ch);
153                 ch = *format++;
154             } else if (ch == '*') {
155                 min = va_arg(args, int);
156                 ch = *format++;
157                 state = DP_S_DOT;
158             } else
159                 state = DP_S_DOT;
160             break;
161         case DP_S_DOT:
162             if (ch == '.') {
163                 state = DP_S_MAX;
164                 ch = *format++;
165             } else
166                 state = DP_S_MOD;
167             break;
168         case DP_S_MAX:
169             if (ossl_isdigit(ch)) {
170                 if (max < 0)
171                     max = 0;
172                 max = 10 * max + char_to_int(ch);
173                 ch = *format++;
174             } else if (ch == '*') {
175                 max = va_arg(args, int);
176                 ch = *format++;
177                 state = DP_S_MOD;
178             } else
179                 state = DP_S_MOD;
180             break;
181         case DP_S_MOD:
182             switch (ch) {
183             case 'h':
184                 cflags = DP_C_SHORT;
185                 ch = *format++;
186                 break;
187             case 'l':
188                 if (*format == 'l') {
189                     cflags = DP_C_LLONG;
190                     format++;
191                 } else
192                     cflags = DP_C_LONG;
193                 ch = *format++;
194                 break;
195             case 'q':
196             case 'j':
197                 cflags = DP_C_LLONG;
198                 ch = *format++;
199                 break;
200             case 'L':
201                 cflags = DP_C_LDOUBLE;
202                 ch = *format++;
203                 break;
204             case 'z':
205                 cflags = DP_C_SIZE;
206                 ch = *format++;
207                 break;
208             default:
209                 break;
210             }
211             state = DP_S_CONV;
212             break;
213         case DP_S_CONV:
214             switch (ch) {
215             case 'd':
216             case 'i':
217                 switch (cflags) {
218                 case DP_C_SHORT:
219                     value = (short int)va_arg(args, int);
220                     break;
221                 case DP_C_LONG:
222                     value = va_arg(args, long int);
223                     break;
224                 case DP_C_LLONG:
225                     value = va_arg(args, int64_t);
226                     break;
227                 case DP_C_SIZE:
228                     value = va_arg(args, ossl_ssize_t);
229                     break;
230                 default:
231                     value = va_arg(args, int);
232                     break;
233                 }
234                 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
235                             max, flags))
236                     return 0;
237                 break;
238             case 'X':
239                 flags |= DP_F_UP;
240                 /* FALLTHROUGH */
241             case 'x':
242             case 'o':
243             case 'u':
244                 flags |= DP_F_UNSIGNED;
245                 switch (cflags) {
246                 case DP_C_SHORT:
247                     value = (unsigned short int)va_arg(args, unsigned int);
248                     break;
249                 case DP_C_LONG:
250                     value = va_arg(args, unsigned long int);
251                     break;
252                 case DP_C_LLONG:
253                     value = va_arg(args, uint64_t);
254                     break;
255                 case DP_C_SIZE:
256                     value = va_arg(args, size_t);
257                     break;
258                 default:
259                     value = va_arg(args, unsigned int);
260                     break;
261                 }
262                 if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
263                             ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
264                             min, max, flags))
265                     return 0;
266                 break;
267 #ifndef OPENSSL_SYS_UEFI
268             case 'f':
269                 if (cflags == DP_C_LDOUBLE)
270                     fvalue = va_arg(args, LDOUBLE);
271                 else
272                     fvalue = va_arg(args, double);
273                 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
274                            flags, F_FORMAT))
275                     return 0;
276                 break;
277             case 'E':
278                 flags |= DP_F_UP;
279                 /* fall through */
280             case 'e':
281                 if (cflags == DP_C_LDOUBLE)
282                     fvalue = va_arg(args, LDOUBLE);
283                 else
284                     fvalue = va_arg(args, double);
285                 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
286                            flags, E_FORMAT))
287                     return 0;
288                 break;
289             case 'G':
290                 flags |= DP_F_UP;
291                 /* fall through */
292             case 'g':
293                 if (cflags == DP_C_LDOUBLE)
294                     fvalue = va_arg(args, LDOUBLE);
295                 else
296                     fvalue = va_arg(args, double);
297                 if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
298                            flags, G_FORMAT))
299                     return 0;
300                 break;
301 #else
302             case 'f':
303             case 'E':
304             case 'e':
305             case 'G':
306             case 'g':
307                 /* not implemented for UEFI */
308                 ERR_raise(ERR_LIB_BIO, ERR_R_UNSUPPORTED);
309                 return 0;
310 #endif
311             case 'c':
312                 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen,
313                                  va_arg(args, int)))
314                     return 0;
315                 break;
316             case 's':
317                 strvalue = va_arg(args, char *);
318                 if (max < 0) {
319                     if (buffer)
320                         max = INT_MAX;
321                     else
322                         max = *maxlen;
323                 }
324                 if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
325                             flags, min, max))
326                     return 0;
327                 break;
328             case 'p':
329                 value = (size_t)va_arg(args, void *);
330                 if (!fmtint(sbuffer, buffer, &currlen, maxlen,
331                             value, 16, min, max, flags | DP_F_NUM))
332                     return 0;
333                 break;
334             case 'n':
335                 {
336                     int *num;
337                     num = va_arg(args, int *);
338                     *num = currlen;
339                 }
340                 break;
341             case '%':
342                 if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
343                     return 0;
344                 break;
345             case 'w':
346                 /* not supported yet, treat as next char */
347                 format++;
348                 break;
349             default:
350                 /* unknown, skip */
351                 break;
352             }
353             ch = *format++;
354             state = DP_S_DEFAULT;
355             flags = cflags = min = 0;
356             max = -1;
357             break;
358         case DP_S_DONE:
359             break;
360         default:
361             break;
362         }
363     }
364     /*
365      * We have to truncate if there is no dynamic buffer and we have filled the
366      * static buffer.
367      */
368     if (buffer == NULL) {
369         *truncated = (currlen > *maxlen - 1);
370         if (*truncated)
371             currlen = *maxlen - 1;
372     }
373     if (!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
374         return 0;
375     *retlen = currlen - 1;
376     return 1;
377 }
378 
379 static int
fmtstr(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,const char * value,int flags,int min,int max)380 fmtstr(char **sbuffer,
381        char **buffer,
382        size_t *currlen,
383        size_t *maxlen, const char *value, int flags, int min, int max)
384 {
385     int padlen;
386     size_t strln;
387     int cnt = 0;
388 
389     if (value == 0)
390         value = "<NULL>";
391 
392     strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
393 
394     padlen = min - strln;
395     if (min < 0 || padlen < 0)
396         padlen = 0;
397     if (max >= 0) {
398         /*
399          * Calculate the maximum output including padding.
400          * Make sure max doesn't overflow into negativity
401          */
402         if (max < INT_MAX - padlen)
403             max += padlen;
404         else
405             max = INT_MAX;
406     }
407     if (flags & DP_F_MINUS)
408         padlen = -padlen;
409 
410     while ((padlen > 0) && (max < 0 || cnt < max)) {
411         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
412             return 0;
413         --padlen;
414         ++cnt;
415     }
416     while (strln > 0 && (max < 0 || cnt < max)) {
417         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
418             return 0;
419         --strln;
420         ++cnt;
421     }
422     while ((padlen < 0) && (max < 0 || cnt < max)) {
423         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
424             return 0;
425         ++padlen;
426         ++cnt;
427     }
428     return 1;
429 }
430 
431 static int
fmtint(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,int64_t value,int base,int min,int max,int flags)432 fmtint(char **sbuffer,
433        char **buffer,
434        size_t *currlen,
435        size_t *maxlen, int64_t value, int base, int min, int max, int flags)
436 {
437     int signvalue = 0;
438     const char *prefix = "";
439     uint64_t uvalue;
440     char convert[DECIMAL_SIZE(value) + 3];
441     int place = 0;
442     int spadlen = 0;
443     int zpadlen = 0;
444     int caps = 0;
445 
446     if (max < 0)
447         max = 0;
448     uvalue = value;
449     if (!(flags & DP_F_UNSIGNED)) {
450         if (value < 0) {
451             signvalue = '-';
452             uvalue = 0 - (uint64_t)value;
453         } else if (flags & DP_F_PLUS)
454             signvalue = '+';
455         else if (flags & DP_F_SPACE)
456             signvalue = ' ';
457     }
458     if (flags & DP_F_NUM) {
459         if (base == 8)
460             prefix = "0";
461         if (base == 16)
462             prefix = "0x";
463     }
464     if (flags & DP_F_UP)
465         caps = 1;
466     do {
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))
472         place--;
473     convert[place] = 0;
474 
475     zpadlen = max - place;
476     spadlen =
477         min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
478     if (zpadlen < 0)
479         zpadlen = 0;
480     if (spadlen < 0)
481         spadlen = 0;
482     if (flags & DP_F_ZERO) {
483         zpadlen = OSSL_MAX(zpadlen, spadlen);
484         spadlen = 0;
485     }
486     if (flags & DP_F_MINUS)
487         spadlen = -spadlen;
488 
489     /* spaces */
490     while (spadlen > 0) {
491         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
492             return 0;
493         --spadlen;
494     }
495 
496     /* sign */
497     if (signvalue)
498         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
499             return 0;
500 
501     /* prefix */
502     while (*prefix) {
503         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
504             return 0;
505         prefix++;
506     }
507 
508     /* zeros */
509     if (zpadlen > 0) {
510         while (zpadlen > 0) {
511             if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
512                 return 0;
513             --zpadlen;
514         }
515     }
516     /* digits */
517     while (place > 0) {
518         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
519             return 0;
520     }
521 
522     /* left justified spaces */
523     while (spadlen < 0) {
524         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
525             return 0;
526         ++spadlen;
527     }
528     return 1;
529 }
530 
531 #ifndef OPENSSL_SYS_UEFI
532 
abs_val(LDOUBLE value)533 static LDOUBLE abs_val(LDOUBLE value)
534 {
535     LDOUBLE result = value;
536     if (value < 0)
537         result = -value;
538     return result;
539 }
540 
pow_10(int in_exp)541 static LDOUBLE pow_10(int in_exp)
542 {
543     LDOUBLE result = 1;
544     while (in_exp) {
545         result *= 10;
546         in_exp--;
547     }
548     return result;
549 }
550 
roundv(LDOUBLE value)551 static long roundv(LDOUBLE value)
552 {
553     long intpart;
554     intpart = (long)value;
555     value = value - intpart;
556     if (value >= 0.5)
557         intpart++;
558     return intpart;
559 }
560 
561 static int
fmtfp(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,LDOUBLE fvalue,int min,int max,int flags,int style)562 fmtfp(char **sbuffer,
563       char **buffer,
564       size_t *currlen,
565       size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags, int style)
566 {
567     int signvalue = 0;
568     LDOUBLE ufvalue;
569     LDOUBLE tmpvalue;
570     char iconvert[20];
571     char fconvert[20];
572     char econvert[20];
573     int iplace = 0;
574     int fplace = 0;
575     int eplace = 0;
576     int padlen = 0;
577     int zpadlen = 0;
578     long exp = 0;
579     unsigned long intpart;
580     unsigned long fracpart;
581     unsigned long max10;
582     int realstyle;
583 
584     if (max < 0)
585         max = 6;
586 
587     if (fvalue < 0)
588         signvalue = '-';
589     else if (flags & DP_F_PLUS)
590         signvalue = '+';
591     else if (flags & DP_F_SPACE)
592         signvalue = ' ';
593 
594     /*
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
597      * that from here on.
598      */
599     if (style == G_FORMAT) {
600         if (fvalue == 0.0) {
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;
607         } else {
608             realstyle = F_FORMAT;
609         }
610     } else {
611         realstyle = style;
612     }
613 
614     if (style != F_FORMAT) {
615         tmpvalue = fvalue;
616         /* Calculate the exponent */
617         if (fvalue != 0.0) {
618             while (tmpvalue < 1) {
619                 tmpvalue *= 10;
620                 exp--;
621             }
622             while (tmpvalue > 10) {
623                 tmpvalue /= 10;
624                 exp++;
625             }
626         }
627         if (style == G_FORMAT) {
628             /*
629              * In G_FORMAT the "precision" represents significant digits. We
630              * always have at least 1 significant digit.
631              */
632             if (max == 0)
633                 max = 1;
634             /* Now convert significant digits to decimal places */
635             if (realstyle == F_FORMAT) {
636                 max -= (exp + 1);
637                 if (max < 0) {
638                     /*
639                      * Should not happen. If we're in F_FORMAT then exp < max?
640                      */
641                     (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
642                     return 0;
643                 }
644             } else {
645                 /*
646                  * In E_FORMAT there is always one significant digit in front
647                  * of the decimal point, so:
648                  * significant digits == 1 + decimal places
649                  */
650                 max--;
651             }
652         }
653         if (realstyle == E_FORMAT)
654             fvalue = tmpvalue;
655     }
656     ufvalue = abs_val(fvalue);
657     /*
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.
660      */
661     if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
662         /* Number too big */
663         (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
664         return 0;
665     }
666     intpart = (unsigned long)ufvalue;
667 
668     /*
669      * sorry, we only support 9 digits past the decimal because of our
670      * conversion method
671      */
672     if (max > 9)
673         max = 9;
674 
675     /*
676      * we "cheat" by converting the fractional part to integer by multiplying
677      * by a factor of 10
678      */
679     max10 = roundv(pow_10(max));
680     fracpart = roundv(pow_10(max) * (ufvalue - intpart));
681 
682     if (fracpart >= max10) {
683         intpart++;
684         fracpart -= max10;
685     }
686 
687     /* convert integer part */
688     do {
689         iconvert[iplace++] = "0123456789"[intpart % 10];
690         intpart = (intpart / 10);
691     } while (intpart && (iplace < (int)sizeof(iconvert)));
692     if (iplace == sizeof(iconvert))
693         iplace--;
694     iconvert[iplace] = 0;
695 
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 */
700             max--;
701             fracpart = fracpart / 10;
702             if (fplace < max)
703                 continue;
704             break;
705         }
706         fconvert[fplace++] = "0123456789"[fracpart % 10];
707         fracpart = (fracpart / 10);
708     }
709 
710     fconvert[fplace] = 0;
711 
712     /* convert exponent part */
713     if (realstyle == E_FORMAT) {
714         int tmpexp;
715         if (exp < 0)
716             tmpexp = -exp;
717         else
718             tmpexp = exp;
719 
720         do {
721             econvert[eplace++] = "0123456789"[tmpexp % 10];
722             tmpexp = (tmpexp / 10);
723         } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
724         /* Exponent is huge!! Too big to print */
725         if (tmpexp > 0) {
726             (void)doapr_outch(sbuffer, buffer, currlen, maxlen, '\0');
727             return 0;
728         }
729         /* Add a leading 0 for single digit exponents */
730         if (eplace == 1)
731             econvert[eplace++] = '0';
732     }
733 
734     /*
735      * -1 for decimal point (if we have one, i.e. max > 0),
736      * another -1 if we are printing a sign
737      */
738     padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
739     /* Take some off for exponent prefix "+e" and exponent */
740     if (realstyle == E_FORMAT)
741         padlen -= 2 + eplace;
742     zpadlen = max - fplace;
743     if (zpadlen < 0)
744         zpadlen = 0;
745     if (padlen < 0)
746         padlen = 0;
747     if (flags & DP_F_MINUS)
748         padlen = -padlen;
749 
750     if ((flags & DP_F_ZERO) && (padlen > 0)) {
751         if (signvalue) {
752             if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
753                 return 0;
754             --padlen;
755             signvalue = 0;
756         }
757         while (padlen > 0) {
758             if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
759                 return 0;
760             --padlen;
761         }
762     }
763     while (padlen > 0) {
764         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
765             return 0;
766         --padlen;
767     }
768     if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
769         return 0;
770 
771     while (iplace > 0) {
772         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
773             return 0;
774     }
775 
776     /*
777      * Decimal point. This should probably use locale to find the correct
778      * char to print out.
779      */
780     if (max > 0 || (flags & DP_F_NUM)) {
781         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
782             return 0;
783 
784         while (fplace > 0) {
785             if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
786                              fconvert[--fplace]))
787                 return 0;
788         }
789     }
790     while (zpadlen > 0) {
791         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
792             return 0;
793         --zpadlen;
794     }
795     if (realstyle == E_FORMAT) {
796         char ech;
797 
798         if ((flags & DP_F_UP) == 0)
799             ech = 'e';
800         else
801             ech = 'E';
802         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ech))
803                 return 0;
804         if (exp < 0) {
805             if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '-'))
806                     return 0;
807         } else {
808             if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '+'))
809                     return 0;
810         }
811         while (eplace > 0) {
812             if (!doapr_outch(sbuffer, buffer, currlen, maxlen,
813                              econvert[--eplace]))
814                 return 0;
815         }
816     }
817 
818     while (padlen < 0) {
819         if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
820             return 0;
821         ++padlen;
822     }
823     return 1;
824 }
825 
826 #endif /* OPENSSL_SYS_UEFI */
827 
828 #define BUFFER_INC  1024
829 
830 static int
doapr_outch(char ** sbuffer,char ** buffer,size_t * currlen,size_t * maxlen,int c)831 doapr_outch(char **sbuffer,
832             char **buffer, size_t *currlen, size_t *maxlen, int c)
833 {
834     /* If we haven't at least one buffer, someone has done a big booboo */
835     if (!ossl_assert(*sbuffer != NULL || buffer != NULL))
836         return 0;
837 
838     /* |currlen| must always be <= |*maxlen| */
839     if (!ossl_assert(*currlen <= *maxlen))
840         return 0;
841 
842     if (buffer && *currlen == *maxlen) {
843         if (*maxlen > INT_MAX - BUFFER_INC)
844             return 0;
845 
846         *maxlen += BUFFER_INC;
847         if (*buffer == NULL) {
848             if ((*buffer = OPENSSL_malloc(*maxlen)) == NULL)
849                 return 0;
850             if (*currlen > 0) {
851                 if (!ossl_assert(*sbuffer != NULL))
852                     return 0;
853                 memcpy(*buffer, *sbuffer, *currlen);
854             }
855             *sbuffer = NULL;
856         } else {
857             char *tmpbuf;
858 
859             tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
860             if (tmpbuf == NULL)
861                 return 0;
862             *buffer = tmpbuf;
863         }
864     }
865 
866     if (*currlen < *maxlen) {
867         if (*sbuffer)
868             (*sbuffer)[(*currlen)++] = (char)c;
869         else
870             (*buffer)[(*currlen)++] = (char)c;
871     }
872 
873     return 1;
874 }
875 
876 /***************************************************************************/
877 
BIO_printf(BIO * bio,const char * format,...)878 int BIO_printf(BIO *bio, const char *format, ...)
879 {
880     va_list args;
881     int ret;
882 
883     va_start(args, format);
884 
885     ret = BIO_vprintf(bio, format, args);
886 
887     va_end(args);
888     return ret;
889 }
890 
BIO_vprintf(BIO * bio,const char * format,va_list args)891 int BIO_vprintf(BIO *bio, const char *format, va_list args)
892 {
893     int ret;
894     size_t retlen;
895     char hugebuf[1024 * 2];     /* Was previously 10k, which is unreasonable
896                                  * in small-stack environments, like threads
897                                  * or DOS programs. */
898     char *hugebufp = hugebuf;
899     size_t hugebufsize = sizeof(hugebuf);
900     char *dynbuf = NULL;
901     int ignored;
902 
903     dynbuf = NULL;
904     if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
905                 args)) {
906         OPENSSL_free(dynbuf);
907         return -1;
908     }
909     if (dynbuf) {
910         ret = BIO_write(bio, dynbuf, (int)retlen);
911         OPENSSL_free(dynbuf);
912     } else {
913         ret = BIO_write(bio, hugebuf, (int)retlen);
914     }
915     return ret;
916 }
917 
918 /*
919  * As snprintf is not available everywhere, we provide our own
920  * implementation. This function has nothing to do with BIOs, but it's
921  * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
922  * function should be renamed, but to what?)
923  */
BIO_snprintf(char * buf,size_t n,const char * format,...)924 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
925 {
926     va_list args;
927     int ret;
928 
929     va_start(args, format);
930 
931     ret = BIO_vsnprintf(buf, n, format, args);
932 
933     va_end(args);
934     return ret;
935 }
936 
BIO_vsnprintf(char * buf,size_t n,const char * format,va_list args)937 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
938 {
939     size_t retlen;
940     int truncated;
941 
942     if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
943         return -1;
944 
945     if (truncated)
946         /*
947          * In case of truncation, return -1 like traditional snprintf.
948          * (Current drafts for ISO/IEC 9899 say snprintf should return the
949          * number of characters that would have been written, had the buffer
950          * been large enough.)
951          */
952         return -1;
953     return (retlen <= INT_MAX) ? (int)retlen : -1;
954 }
955