Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bio/bio_print.c

raw
1 /*
2 * Copyright 1995-2025 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 <[email protected]>
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 struct pr_desc {
32 /** Static buffer */
33 char *sbuffer;
34 /** Dynamic buffer */
35 char **buffer;
36 /** Current writing position */
37 size_t currlen;
38 /** Buffer size */
39 size_t maxlen;
40 /** "Write position", for proper %n support */
41 long long pos;
42 };
43
44 static int fmtstr(struct pr_desc *, const char *, int, int, int);
45 static int fmtint(struct pr_desc *, int64_t, int, int, int, int);
46 #ifndef OPENSSL_SYS_UEFI
47 static int fmtfp(struct pr_desc *, LDOUBLE, int, int, int, int);
48 #endif
49 static int doapr_outch(struct pr_desc *, int);
50 static int eob_ok(struct pr_desc *desc, long long left);
51 static int _dopr(char **sbuffer, char **buffer,
52 size_t *maxlen, size_t *retlen, int *truncated,
53 const char *format, va_list args);
54
55 /* format read states */
56 #define DP_S_DEFAULT 0
57 #define DP_S_FLAGS 1
58 #define DP_S_MIN 2
59 #define DP_S_DOT 3
60 #define DP_S_MAX 4
61 #define DP_S_MOD 5
62 #define DP_S_CONV 6
63 #define DP_S_DONE 7
64
65 /* format flags - Bits */
66 /* left-aligned padding */
67 #define DP_F_MINUS (1 << 0)
68 /* print an explicit '+' for a value with positive sign */
69 #define DP_F_PLUS (1 << 1)
70 /* print an explicit ' ' for a value with positive sign */
71 #define DP_F_SPACE (1 << 2)
72 /* print 0/0x prefix for octal/hex and decimal point for floating point */
73 #define DP_F_NUM (1 << 3)
74 /* print leading zeroes */
75 #define DP_F_ZERO (1 << 4)
76 /* print HEX in UPPERcase */
77 #define DP_F_UP (1 << 5)
78 /* treat value as unsigned */
79 #define DP_F_UNSIGNED (1 << 6)
80
81 /* conversion flags */
82 #define DP_C_CHAR 1
83 #define DP_C_SHORT 2
84 #define DP_C_LONG 3
85 #define DP_C_LDOUBLE 4
86 #define DP_C_LLONG 5
87 #define DP_C_SIZE 6
88 #define DP_C_PTRDIFF 7
89
90 /* Floating point formats */
91 #define F_FORMAT 0
92 #define E_FORMAT 1
93 #define G_FORMAT 2
94
95 /* some handy macros */
96 #define char_to_int(p) (p - '0')
97 #define OSSL_MAX(p, q) ((p >= q) ? p : q)
98
99 static int
100 _dopr(char **sbuffer,
101 char **buffer,
102 size_t *maxlen,
103 size_t *retlen, int *truncated, const char *format, va_list args)
104 {
105 char ch;
106 int64_t value;
107 #ifndef OPENSSL_SYS_UEFI
108 LDOUBLE fvalue;
109 #endif
110 char *strvalue;
111 int min;
112 int max;
113 int state;
114 int flags;
115 int cflags;
116 struct pr_desc desc = { *sbuffer, buffer, 0, *maxlen, 0 };
117 int ret = 0;
118
119 state = DP_S_DEFAULT;
120 flags = cflags = min = 0;
121 max = -1;
122 ch = *format++;
123
124 while (state != DP_S_DONE) {
125 if (ch == '\0')
126 state = DP_S_DONE;
127
128 switch (state) {
129 case DP_S_DEFAULT:
130 if (ch == '%')
131 state = DP_S_FLAGS;
132 else if (!doapr_outch(&desc, ch))
133 goto out;
134 ch = *format++;
135 break;
136 case DP_S_FLAGS:
137 switch (ch) {
138 case '-':
139 flags |= DP_F_MINUS;
140 ch = *format++;
141 break;
142 case '+':
143 flags |= DP_F_PLUS;
144 ch = *format++;
145 break;
146 case ' ':
147 flags |= DP_F_SPACE;
148 ch = *format++;
149 break;
150 case '#':
151 flags |= DP_F_NUM;
152 ch = *format++;
153 break;
154 case '0':
155 flags |= DP_F_ZERO;
156 ch = *format++;
157 break;
158 default:
159 state = DP_S_MIN;
160 break;
161 }
162 break;
163 case DP_S_MIN: /* width */
164 if (ossl_isdigit(ch)) {
165 /*
166 * Most implementations cap the possible explicitly specified
167 * width by (INT_MAX / 10) * 10 - 1 or so (the standard gives
168 * no clear limit on this), we can do the same.
169 */
170 if (min < INT_MAX / 10)
171 min = 10 * min + char_to_int(ch);
172 else
173 goto out;
174 ch = *format++;
175 } else if (ch == '*') {
176 min = va_arg(args, int);
177 if (min < 0) {
178 flags |= DP_F_MINUS;
179 min = -min;
180 }
181 ch = *format++;
182 state = DP_S_DOT;
183 } else
184 state = DP_S_DOT;
185 break;
186 case DP_S_DOT:
187 if (ch == '.') {
188 state = DP_S_MAX;
189 ch = *format++;
190 } else
191 state = DP_S_MOD;
192 break;
193 case DP_S_MAX: /* precision */
194 if (ossl_isdigit(ch)) {
195 if (max < 0)
196 max = 0;
197 /*
198 * Most implementations cap the possible explicitly specified
199 * width by (INT_MAX / 10) * 10 - 1 or so (the standard gives
200 * no clear limit on this), we can do the same.
201 */
202 if (max < INT_MAX / 10)
203 max = 10 * max + char_to_int(ch);
204 else
205 goto out;
206 ch = *format++;
207 } else if (ch == '*') {
208 max = va_arg(args, int);
209 ch = *format++;
210 state = DP_S_MOD;
211 } else {
212 if (max < 0)
213 max = 0;
214 state = DP_S_MOD;
215 }
216 break;
217 case DP_S_MOD:
218 switch (ch) {
219 case 'h':
220 if (*format == 'h') {
221 cflags = DP_C_CHAR;
222 format++;
223 } else {
224 cflags = DP_C_SHORT;
225 }
226 ch = *format++;
227 break;
228 case 'l':
229 if (*format == 'l') {
230 cflags = DP_C_LLONG;
231 format++;
232 } else
233 cflags = DP_C_LONG;
234 ch = *format++;
235 break;
236 case 'q':
237 case 'j':
238 cflags = DP_C_LLONG;
239 ch = *format++;
240 break;
241 case 'L':
242 cflags = DP_C_LDOUBLE;
243 ch = *format++;
244 break;
245 case 'z':
246 cflags = DP_C_SIZE;
247 ch = *format++;
248 break;
249 case 't':
250 cflags = DP_C_PTRDIFF;
251 ch = *format++;
252 break;
253 default:
254 break;
255 }
256 state = DP_S_CONV;
257 break;
258 case DP_S_CONV:
259 switch (ch) {
260 case 'd':
261 case 'i':
262 switch (cflags) {
263 case DP_C_CHAR:
264 value = (signed char)va_arg(args, int);
265 break;
266 case DP_C_SHORT:
267 value = (short int)va_arg(args, int);
268 break;
269 case DP_C_LONG:
270 value = va_arg(args, long int);
271 break;
272 case DP_C_LLONG:
273 value = va_arg(args, int64_t);
274 break;
275 case DP_C_SIZE:
276 value = va_arg(args, ossl_ssize_t);
277 break;
278 case DP_C_PTRDIFF:
279 value = va_arg(args, ptrdiff_t);
280 break;
281 default:
282 value = va_arg(args, int);
283 break;
284 }
285 if (!fmtint(&desc, value, 10, min, max, flags))
286 goto out;
287 break;
288 case 'X':
289 flags |= DP_F_UP;
290 /* FALLTHROUGH */
291 case 'x':
292 case 'o':
293 case 'u':
294 flags |= DP_F_UNSIGNED;
295 switch (cflags) {
296 case DP_C_CHAR:
297 value = (unsigned char)va_arg(args, unsigned int);
298 break;
299 case DP_C_SHORT:
300 value = (unsigned short int)va_arg(args, unsigned int);
301 break;
302 case DP_C_LONG:
303 value = va_arg(args, unsigned long int);
304 break;
305 case DP_C_LLONG:
306 value = va_arg(args, uint64_t);
307 break;
308 case DP_C_SIZE:
309 value = va_arg(args, size_t);
310 break;
311 case DP_C_PTRDIFF:
312 /*
313 * There is no unsigned variant of ptrdiff_t, and POSIX
314 * requires using a "corresponding unsigned type argument".
315 * Assuming it is power of two in size, at least.
316 */
317 if (sizeof(ptrdiff_t) == sizeof(uint64_t))
318 value = va_arg(args, uint64_t);
319 else
320 value = va_arg(args, unsigned int);
321 break;
322 default:
323 value = va_arg(args, unsigned int);
324 break;
325 }
326 if (!fmtint(&desc, value,
327 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
328 min, max, flags))
329 goto out;
330 break;
331 #ifndef OPENSSL_SYS_UEFI
332 case 'f':
333 if (cflags == DP_C_LDOUBLE)
334 fvalue = va_arg(args, LDOUBLE);
335 else
336 fvalue = va_arg(args, double);
337 if (!fmtfp(&desc, fvalue, min, max, flags, F_FORMAT))
338 goto out;
339 break;
340 case 'E':
341 flags |= DP_F_UP;
342 /* fall through */
343 case 'e':
344 if (cflags == DP_C_LDOUBLE)
345 fvalue = va_arg(args, LDOUBLE);
346 else
347 fvalue = va_arg(args, double);
348 if (!fmtfp(&desc, fvalue, min, max, flags, E_FORMAT))
349 goto out;
350 break;
351 case 'G':
352 flags |= DP_F_UP;
353 /* fall through */
354 case 'g':
355 if (cflags == DP_C_LDOUBLE)
356 fvalue = va_arg(args, LDOUBLE);
357 else
358 fvalue = va_arg(args, double);
359 if (!fmtfp(&desc, fvalue, min, max, flags, G_FORMAT))
360 goto out;
361 break;
362 #else
363 case 'f':
364 case 'E':
365 case 'e':
366 case 'G':
367 case 'g':
368 /* not implemented for UEFI */
369 ERR_raise(ERR_LIB_BIO, ERR_R_UNSUPPORTED);
370 goto out;
371 #endif
372 case 'c':
373 if (!doapr_outch(&desc, va_arg(args, int)))
374 goto out;
375 break;
376 case 's':
377 strvalue = va_arg(args, char *);
378 if (max < 0)
379 max = INT_MAX;
380 if (!fmtstr(&desc, strvalue, flags, min, max))
381 goto out;
382 break;
383 case 'p':
384 value = (size_t)va_arg(args, void *);
385 if (!fmtint(&desc, value, 16, min, max, flags | DP_F_NUM))
386 goto out;
387 break;
388 case 'n':
389 switch (cflags) {
390 #define HANDLE_N(type) \
391 do { \
392 type *num; \
393 \
394 num = va_arg(args, type *); \
395 *num = (type)desc.pos; \
396 } while (0)
397 case DP_C_CHAR:
398 HANDLE_N(signed char);
399 break;
400 case DP_C_SHORT:
401 HANDLE_N(short);
402 break;
403 case DP_C_LONG:
404 HANDLE_N(long);
405 break;
406 case DP_C_LLONG:
407 HANDLE_N(long long);
408 break;
409 case DP_C_SIZE:
410 HANDLE_N(ossl_ssize_t);
411 break;
412 case DP_C_PTRDIFF:
413 HANDLE_N(ptrdiff_t);
414 break;
415 default:
416 HANDLE_N(int);
417 break;
418 #undef HANDLE_N
419 }
420 break;
421 case '%':
422 if (!doapr_outch(&desc, ch))
423 goto out;
424 break;
425 case 'w':
426 /* not supported yet, treat as next char */
427 format++;
428 break;
429 default:
430 /* unknown, skip */
431 break;
432 }
433 ch = *format++;
434 state = DP_S_DEFAULT;
435 flags = cflags = min = 0;
436 max = -1;
437 break;
438 case DP_S_DONE:
439 break;
440 default:
441 break;
442 }
443 }
444 ret = 1;
445
446 out:
447 /*
448 * We have to truncate if there is no dynamic buffer and we have filled the
449 * static buffer.
450 */
451 if (buffer == NULL) {
452 *truncated = (desc.currlen > desc.maxlen - 1);
453 if (*truncated)
454 desc.currlen = desc.maxlen - 1;
455 }
456
457 if (!doapr_outch(&desc, '\0'))
458 ret = 0;
459
460 *retlen = desc.currlen - 1;
461 *sbuffer = desc.sbuffer;
462 *maxlen = desc.maxlen;
463
464 return ret;
465 }
466
467 static int
468 fmtstr(struct pr_desc *desc, const char *value, int flags, int min, int max)
469 {
470 int padlen = 0;
471 size_t strln;
472 int cnt = 0;
473
474 if (value == 0)
475 value = "<NULL>";
476
477 strln = OPENSSL_strnlen(value, max < 0 ? SIZE_MAX : (size_t)max);
478
479 if (min >= 0 && strln < INT_MAX) {
480 padlen = min - (int)strln;
481 if (padlen < 0)
482 padlen = 0;
483 }
484 if (max >= 0) {
485 /*
486 * Calculate the maximum output including padding.
487 * Make sure max doesn't overflow into negativity
488 */
489 if (max < INT_MAX - padlen)
490 max += padlen;
491 else
492 max = INT_MAX;
493 }
494
495 if (!(flags & DP_F_MINUS) && padlen > 0) {
496 /* cap padlen to max so we can pass it as-is to eob_ok() */
497 if (max >= 0) {
498 if (padlen > max)
499 padlen = max;
500 cnt = padlen;
501 }
502 while (padlen > 0 && eob_ok(desc, padlen)) {
503 if (!doapr_outch(desc, ' '))
504 return 0;
505 --padlen;
506 }
507 }
508 if (max >= 0) {
509 /* cap strln to (max - cnt) so we can pass it as-is to eob_ok() */
510 if (strln > INT_MAX || (int)strln > max - cnt)
511 strln = max - cnt;
512 cnt += (int)strln;
513 }
514 while (strln > 0 && eob_ok(desc, strln)) {
515 if (!doapr_outch(desc, *value++))
516 return 0;
517 --strln;
518 }
519 if ((flags & DP_F_MINUS) && padlen > 0) {
520 /* cap padlen to (max - cnt) so we can pass it as-is to eob_ok() */
521 if (max >= 0) {
522 if (padlen > max - cnt)
523 padlen = max - cnt;
524 }
525 while (padlen > 0 && eob_ok(desc, padlen)) {
526 if (!doapr_outch(desc, ' '))
527 return 0;
528 --padlen;
529 }
530 }
531 return 1;
532 }
533
534 static int
535 fmtint(struct pr_desc *desc,
536 int64_t value, int base, int min, int max, int flags)
537 {
538 static const char oct_prefix[] = "0";
539
540 int signvalue = 0;
541 const char *prefix = "";
542 uint64_t uvalue;
543 char convert[DECIMAL_SIZE(value) + 3];
544 int place = 0;
545 int spadlen = 0;
546 int zpadlen = 0;
547 int caps = 0;
548
549 if (max < 0) {
550 /* A negative precision is taken as if the precision were omitted. */
551 max = 1;
552 } else {
553 /*
554 * If a precision is given with an integer conversion,
555 * the 0 flag is ignored.
556 */
557 flags &= ~DP_F_ZERO;
558 }
559 uvalue = value;
560 if (!(flags & DP_F_UNSIGNED)) {
561 if (value < 0) {
562 signvalue = '-';
563 uvalue = 0 - (uint64_t)value;
564 } else if (flags & DP_F_PLUS)
565 signvalue = '+';
566 else if (flags & DP_F_SPACE)
567 signvalue = ' ';
568 }
569 if (flags & DP_F_NUM) {
570 if (base == 8)
571 prefix = oct_prefix;
572 if (value != 0) {
573 if (base == 16)
574 prefix = flags & DP_F_UP ? "0X" : "0x";
575 }
576 }
577 if (flags & DP_F_UP)
578 caps = 1;
579 /* When 0 is printed with an explicit precision 0, the output is empty. */
580 while (uvalue && (place < (int)sizeof(convert))) {
581 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
582 [uvalue % (unsigned)base];
583 uvalue = (uvalue / (unsigned)base);
584 }
585 if (place == sizeof(convert))
586 place--;
587 convert[place] = 0;
588
589 /*
590 * "#" (alternative form):
591 * - For o conversion, it shall increase the precision, if and only
592 * if necessary, to force the first digit of the result to be a zero
593 */
594 zpadlen = max - place - (prefix == oct_prefix);
595 if (zpadlen < 0)
596 zpadlen = 0;
597 spadlen = min - OSSL_MAX(max, place + zpadlen + (signvalue ? 1 : 0) + (int)strlen(prefix));
598 if (spadlen < 0)
599 spadlen = 0;
600 if (flags & DP_F_MINUS) {
601 spadlen = -spadlen;
602 } else if (flags & DP_F_ZERO) {
603 zpadlen = zpadlen + spadlen;
604 spadlen = 0;
605 }
606
607 /* spaces */
608 while (spadlen > 0 && eob_ok(desc, spadlen)) {
609 if (!doapr_outch(desc, ' '))
610 return 0;
611 --spadlen;
612 }
613
614 /* sign */
615 if (signvalue)
616 if (!doapr_outch(desc, signvalue))
617 return 0;
618
619 /* prefix */
620 while (*prefix) {
621 if (!doapr_outch(desc, *prefix))
622 return 0;
623 prefix++;
624 }
625
626 /* zeros */
627 if (zpadlen > 0) {
628 while (zpadlen > 0 && eob_ok(desc, zpadlen)) {
629 if (!doapr_outch(desc, '0'))
630 return 0;
631 --zpadlen;
632 }
633 }
634 /* digits */
635 while (place > 0) {
636 if (!doapr_outch(desc, convert[--place]))
637 return 0;
638 }
639
640 /* left justified spaces */
641 if (spadlen < 0) {
642 spadlen = -spadlen;
643
644 while (spadlen > 0 && eob_ok(desc, spadlen)) {
645 if (!doapr_outch(desc, ' '))
646 return 0;
647 --spadlen;
648 }
649 }
650 return 1;
651 }
652
653 #ifndef OPENSSL_SYS_UEFI
654
655 static LDOUBLE abs_val(LDOUBLE value)
656 {
657 LDOUBLE result = value;
658 if (value < 0)
659 result = -value;
660 if (result > 0 && result / 2 == result) /* INF */
661 result = 0;
662 else if (result != result) /* NAN */
663 result = 0;
664 return result;
665 }
666
667 static LDOUBLE pow_10(int in_exp)
668 {
669 LDOUBLE result = 1;
670 while (in_exp) {
671 result *= 10;
672 in_exp--;
673 }
674 return result;
675 }
676
677 static long roundv(LDOUBLE value)
678 {
679 long intpart;
680 intpart = (long)value;
681 value = value - intpart;
682 if (value >= 0.5)
683 intpart++;
684 return intpart;
685 }
686
687 static int
688 fmtfp(struct pr_desc *desc,
689 LDOUBLE fvalue, int min, int max, int flags, int style)
690 {
691 int signvalue = 0;
692 LDOUBLE ufvalue;
693 LDOUBLE tmpvalue;
694 char iconvert[20];
695 char fconvert[20];
696 char econvert[20];
697 int iplace = 0;
698 int fplace = 0;
699 int eplace = 0;
700 int padlen = 0;
701 int zpadlen = 0;
702 long exp = 0;
703 unsigned long intpart;
704 unsigned long fracpart;
705 unsigned long max10;
706 int realstyle;
707
708 if (max < 0)
709 max = 6;
710
711 if (fvalue < 0)
712 signvalue = '-';
713 else if (flags & DP_F_PLUS)
714 signvalue = '+';
715 else if (flags & DP_F_SPACE)
716 signvalue = ' ';
717 ufvalue = abs_val(fvalue);
718 if (ufvalue == 0 && fvalue != 0) /* INF or NAN? */
719 signvalue = '?';
720
721 /*
722 * G_FORMAT sometimes prints like E_FORMAT and sometimes like F_FORMAT
723 * depending on the number to be printed. Work out which one it is and use
724 * that from here on.
725 */
726 if (style == G_FORMAT) {
727 if (ufvalue == 0.0) {
728 realstyle = F_FORMAT;
729 } else if (ufvalue < 0.0001) {
730 realstyle = E_FORMAT;
731 } else if ((max == 0 && ufvalue >= 10)
732 || (max > 0 && ufvalue >= pow_10(max))) {
733 realstyle = E_FORMAT;
734 } else {
735 realstyle = F_FORMAT;
736 }
737 } else {
738 realstyle = style;
739 }
740
741 if (style != F_FORMAT) {
742 tmpvalue = ufvalue;
743 /* Calculate the exponent */
744 if (ufvalue != 0.0) {
745 while (tmpvalue < 1) {
746 tmpvalue *= 10;
747 exp--;
748 }
749 while (tmpvalue > 10) {
750 tmpvalue /= 10;
751 exp++;
752 }
753 }
754 if (style == G_FORMAT) {
755 /*
756 * In G_FORMAT the "precision" represents significant digits. We
757 * always have at least 1 significant digit.
758 */
759 if (max == 0)
760 max = 1;
761 /* Now convert significant digits to decimal places */
762 if (realstyle == F_FORMAT) {
763 max -= (exp + 1);
764 if (max < 0) {
765 /*
766 * Should not happen. If we're in F_FORMAT then exp < max?
767 */
768 (void)doapr_outch(desc, '\0');
769 return 0;
770 }
771 } else {
772 /*
773 * In E_FORMAT there is always one significant digit in front
774 * of the decimal point, so:
775 * significant digits == 1 + decimal places
776 */
777 max--;
778 }
779 }
780 if (realstyle == E_FORMAT)
781 ufvalue = tmpvalue;
782 }
783
784 /*
785 * By subtracting 65535 (2^16-1) we cancel the low order 15 bits
786 * of ULONG_MAX to avoid using imprecise floating point values.
787 */
788 if (ufvalue >= (double)(ULONG_MAX - 65535) + 65536.0) {
789 /* Number too big */
790 (void)doapr_outch(desc, '\0');
791 return 0;
792 }
793 intpart = (unsigned long)ufvalue;
794
795 /*
796 * sorry, we only support 9 digits past the decimal because of our
797 * conversion method
798 */
799 if (max > 9)
800 max = 9;
801
802 /*
803 * we "cheat" by converting the fractional part to integer by multiplying
804 * by a factor of 10
805 */
806 max10 = roundv(pow_10(max));
807 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
808
809 if (fracpart >= max10) {
810 intpart++;
811 fracpart -= max10;
812 }
813
814 /* convert integer part */
815 do {
816 iconvert[iplace++] = "0123456789"[intpart % 10];
817 intpart = (intpart / 10);
818 } while (intpart && (iplace < (int)sizeof(iconvert)));
819 if (iplace == sizeof(iconvert))
820 iplace--;
821 iconvert[iplace] = 0;
822
823 /* convert fractional part */
824 while (fplace < max) {
825 if (style == G_FORMAT && fplace == 0 && (fracpart % 10) == 0) {
826 /* We strip trailing zeros in G_FORMAT */
827 max--;
828 fracpart = fracpart / 10;
829 if (fplace < max)
830 continue;
831 break;
832 }
833 fconvert[fplace++] = "0123456789"[fracpart % 10];
834 fracpart = (fracpart / 10);
835 }
836
837 fconvert[fplace] = 0;
838
839 /* convert exponent part */
840 if (realstyle == E_FORMAT) {
841 int tmpexp;
842 if (exp < 0)
843 tmpexp = -exp;
844 else
845 tmpexp = exp;
846
847 do {
848 econvert[eplace++] = "0123456789"[tmpexp % 10];
849 tmpexp = (tmpexp / 10);
850 } while (tmpexp > 0 && eplace < (int)sizeof(econvert));
851 /* Exponent is huge!! Too big to print */
852 if (tmpexp > 0) {
853 (void)doapr_outch(desc, '\0');
854 return 0;
855 }
856 /* Add a leading 0 for single digit exponents */
857 if (eplace == 1)
858 econvert[eplace++] = '0';
859 }
860
861 /*
862 * -1 for decimal point (if we have one, i.e. max > 0),
863 * another -1 if we are printing a sign
864 */
865 padlen = min - iplace - max - (max > 0 ? 1 : 0) - ((signvalue) ? 1 : 0);
866 /* Take some off for exponent prefix "+e" and exponent */
867 if (realstyle == E_FORMAT)
868 padlen -= 2 + eplace;
869 zpadlen = max - fplace;
870 if (zpadlen < 0)
871 zpadlen = 0;
872 if (padlen < 0)
873 padlen = 0;
874 if (flags & DP_F_MINUS)
875 padlen = -padlen;
876
877 if ((flags & DP_F_ZERO) && (padlen > 0)) {
878 if (signvalue) {
879 if (!doapr_outch(desc, signvalue))
880 return 0;
881 --padlen;
882 signvalue = 0;
883 }
884 while (padlen > 0 && eob_ok(desc, padlen)) {
885 if (!doapr_outch(desc, '0'))
886 return 0;
887 --padlen;
888 }
889 padlen = 0;
890 }
891 while (padlen > 0 && eob_ok(desc, padlen)) {
892 if (!doapr_outch(desc, ' '))
893 return 0;
894 --padlen;
895 }
896 padlen = 0;
897 if (signvalue && !doapr_outch(desc, signvalue))
898 return 0;
899
900 while (iplace > 0) {
901 if (!doapr_outch(desc, iconvert[--iplace]))
902 return 0;
903 }
904
905 /*
906 * Decimal point. This should probably use locale to find the correct
907 * char to print out.
908 */
909 if (max > 0 || (flags & DP_F_NUM)) {
910 if (!doapr_outch(desc, '.'))
911 return 0;
912
913 while (fplace > 0) {
914 if (!doapr_outch(desc, fconvert[--fplace]))
915 return 0;
916 }
917 }
918 while (zpadlen > 0 && eob_ok(desc, zpadlen)) {
919 if (!doapr_outch(desc, '0'))
920 return 0;
921 --zpadlen;
922 }
923 if (realstyle == E_FORMAT) {
924 char ech;
925
926 if ((flags & DP_F_UP) == 0)
927 ech = 'e';
928 else
929 ech = 'E';
930 if (!doapr_outch(desc, ech))
931 return 0;
932 if (exp < 0) {
933 if (!doapr_outch(desc, '-'))
934 return 0;
935 } else {
936 if (!doapr_outch(desc, '+'))
937 return 0;
938 }
939 while (eplace > 0) {
940 if (!doapr_outch(desc, econvert[--eplace]))
941 return 0;
942 }
943 }
944
945 if (padlen < 0) {
946 padlen = -padlen;
947
948 while (padlen > 0 && eob_ok(desc, padlen)) {
949 if (!doapr_outch(desc, ' '))
950 return 0;
951 --padlen;
952 }
953 }
954 return 1;
955 }
956
957 #endif /* OPENSSL_SYS_UEFI */
958
959 #define BUFFER_INC 1024
960
961 static int
962 doapr_outch(struct pr_desc *desc, int c)
963 {
964 /* If we haven't at least one buffer, someone has done a big booboo */
965 if (!ossl_assert(desc->sbuffer != NULL || desc->buffer != NULL))
966 return 0;
967
968 /* |currlen| must always be <= |*maxlen| */
969 if (!ossl_assert(desc->currlen <= desc->maxlen))
970 return 0;
971
972 if (desc->buffer != NULL && desc->currlen == desc->maxlen) {
973 if (desc->maxlen > INT_MAX - BUFFER_INC)
974 return 0;
975
976 desc->maxlen += BUFFER_INC;
977 if (*(desc->buffer) == NULL) {
978 if ((*(desc->buffer) = OPENSSL_malloc(desc->maxlen)) == NULL)
979 return 0;
980 if (desc->currlen > 0) {
981 if (!ossl_assert(desc->sbuffer != NULL))
982 return 0;
983 memcpy(*(desc->buffer), desc->sbuffer, desc->currlen);
984 }
985 desc->sbuffer = NULL;
986 } else {
987 char *tmpbuf;
988
989 tmpbuf = OPENSSL_realloc(*(desc->buffer), desc->maxlen);
990 if (tmpbuf == NULL)
991 return 0;
992 *(desc->buffer) = tmpbuf;
993 }
994 }
995
996 if (desc->currlen < desc->maxlen) {
997 if (desc->sbuffer)
998 (desc->sbuffer)[(desc->currlen)++] = (char)c;
999 else
1000 (*(desc->buffer))[(desc->currlen)++] = (char)c;
1001 }
1002
1003 if (desc->pos < LLONG_MAX)
1004 desc->pos++;
1005
1006 return 1;
1007 }
1008
1009 /**
1010 * Checks if we reached an end of a non-extandable buffer (sbuffer) and updates
1011 * desc->pos by adding the amount of bytes left if the end of buffer is reached.
1012 * That allow quickly short-circuiting long loops with unreasonably long paddings
1013 * and widths.
1014 *
1015 * @param desc Pointer to a print descriptor.
1016 * @param left Bytes left in the chunk caller is currently processing.
1017 * @return 0 - end of buffer is reached, no need to continue;
1018 * 1 - there is still space in the buffer, the caller may continue to
1019 * try to output bytes in the buffer with doapr_outch().
1020 */
1021 static int eob_ok(struct pr_desc *desc, long long left)
1022 {
1023 /*
1024 * desc->buffer is auto-sizeable, so we are never supposed to reach the end
1025 * of it.
1026 */
1027 if (desc->buffer != NULL)
1028 return 1;
1029
1030 if (desc->currlen >= desc->maxlen) {
1031 if (left > 0) {
1032 if (desc->pos < LLONG_MAX - left) {
1033 desc->pos += left;
1034 } else {
1035 desc->pos = LLONG_MAX;
1036 }
1037 }
1038
1039 return 0;
1040 }
1041
1042 return 1;
1043 }
1044
1045 /***************************************************************************/
1046
1047 int BIO_printf(BIO *bio, const char *format, ...)
1048 {
1049 va_list args;
1050 int ret;
1051
1052 va_start(args, format);
1053
1054 ret = BIO_vprintf(bio, format, args);
1055
1056 va_end(args);
1057 return ret;
1058 }
1059
1060 int BIO_vprintf(BIO *bio, const char *format, va_list args)
1061 {
1062 int ret;
1063 size_t retlen;
1064 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
1065 * in small-stack environments, like threads
1066 * or DOS programs. */
1067 char *hugebufp = hugebuf;
1068 size_t hugebufsize = sizeof(hugebuf);
1069 char *dynbuf = NULL;
1070 int ignored;
1071
1072 dynbuf = NULL;
1073 if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
1074 args)) {
1075 OPENSSL_free(dynbuf);
1076 return -1;
1077 }
1078 if (dynbuf) {
1079 ret = BIO_write(bio, dynbuf, (int)retlen);
1080 OPENSSL_free(dynbuf);
1081 } else {
1082 ret = BIO_write(bio, hugebuf, (int)retlen);
1083 }
1084 return ret;
1085 }
1086
1087 /*
1088 * As snprintf is not available everywhere, we provide our own
1089 * implementation. This function has nothing to do with BIOs, but it's
1090 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
1091 * function should be renamed, but to what?)
1092 */
1093 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
1094 {
1095 va_list args;
1096 int ret;
1097
1098 va_start(args, format);
1099
1100 ret = BIO_vsnprintf(buf, n, format, args);
1101
1102 va_end(args);
1103 return ret;
1104 }
1105
1106 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
1107 {
1108 size_t retlen;
1109 int truncated;
1110
1111 if (!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
1112 return -1;
1113
1114 if (truncated)
1115 /*
1116 * In case of truncation, return -1 like traditional snprintf.
1117 * (Current drafts for ISO/IEC 9899 say snprintf should return the
1118 * number of characters that would have been written, had the buffer
1119 * been large enough.)
1120 */
1121 return -1;
1122 return (retlen <= INT_MAX) ? (int)retlen : -1;
1123 }