]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/b_print.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / crypto / bio / b_print.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58 /* disable assert() unless BIO_DEBUG has been defined */
59 #ifndef BIO_DEBUG
60 # ifndef NDEBUG
61 # define NDEBUG
62 # endif
63 #endif
64
65 /*
66 * Stolen from tjh's ssl/ssl_trc.c stuff.
67 */
68
69 #include <stdio.h>
70 #include <string.h>
71 #include <ctype.h>
72 #include <assert.h>
73 #include <limits.h>
74 #include "internal/cryptlib.h"
75 #ifndef NO_SYS_TYPES_H
76 # include <sys/types.h>
77 #endif
78 #include <openssl/bn.h> /* To get BN_LLONG properly defined */
79 #include <openssl/bio.h>
80
81 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
82 # ifndef HAVE_LONG_LONG
83 # define HAVE_LONG_LONG 1
84 # endif
85 #endif
86
87 /***************************************************************************/
88
89 /*
90 * Copyright Patrick Powell 1995
91 * This code is based on code written by Patrick Powell <papowell@astart.com>
92 * It may be used for any purpose as long as this notice remains intact
93 * on all source code distributions.
94 */
95
96 /*-
97 * This code contains numerious changes and enhancements which were
98 * made by lots of contributors over the last years to Patrick Powell's
99 * original code:
100 *
101 * o Patrick Powell <papowell@astart.com> (1995)
102 * o Brandon Long <blong@fiction.net> (1996, for Mutt)
103 * o Thomas Roessler <roessler@guug.de> (1998, for Mutt)
104 * o Michael Elkins <me@cs.hmc.edu> (1998, for Mutt)
105 * o Andrew Tridgell <tridge@samba.org> (1998, for Samba)
106 * o Luke Mewburn <lukem@netbsd.org> (1999, for LukemFTP)
107 * o Ralf S. Engelschall <rse@engelschall.com> (1999, for Pth)
108 * o ... (for OpenSSL)
109 */
110
111 #ifdef HAVE_LONG_DOUBLE
112 # define LDOUBLE long double
113 #else
114 # define LDOUBLE double
115 #endif
116
117 #ifdef HAVE_LONG_LONG
118 # if defined(_WIN32) && !defined(__GNUC__)
119 # define LLONG __int64
120 # else
121 # define LLONG long long
122 # endif
123 #else
124 # define LLONG long
125 #endif
126
127 static void fmtstr(char **, char **, size_t *, size_t *,
128 const char *, int, int, int);
129 static void fmtint(char **, char **, size_t *, size_t *,
130 LLONG, int, int, int, int);
131 static void fmtfp(char **, char **, size_t *, size_t *,
132 LDOUBLE, int, int, int);
133 static void doapr_outch(char **, char **, size_t *, size_t *, int);
134 static void _dopr(char **sbuffer, char **buffer,
135 size_t *maxlen, size_t *retlen, int *truncated,
136 const char *format, va_list args);
137
138 /* format read states */
139 #define DP_S_DEFAULT 0
140 #define DP_S_FLAGS 1
141 #define DP_S_MIN 2
142 #define DP_S_DOT 3
143 #define DP_S_MAX 4
144 #define DP_S_MOD 5
145 #define DP_S_CONV 6
146 #define DP_S_DONE 7
147
148 /* format flags - Bits */
149 #define DP_F_MINUS (1 << 0)
150 #define DP_F_PLUS (1 << 1)
151 #define DP_F_SPACE (1 << 2)
152 #define DP_F_NUM (1 << 3)
153 #define DP_F_ZERO (1 << 4)
154 #define DP_F_UP (1 << 5)
155 #define DP_F_UNSIGNED (1 << 6)
156
157 /* conversion flags */
158 #define DP_C_SHORT 1
159 #define DP_C_LONG 2
160 #define DP_C_LDOUBLE 3
161 #define DP_C_LLONG 4
162
163 /* some handy macros */
164 #define char_to_int(p) (p - '0')
165 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
166
167 static void
168 _dopr(char **sbuffer,
169 char **buffer,
170 size_t *maxlen,
171 size_t *retlen, int *truncated, const char *format, va_list args)
172 {
173 char ch;
174 LLONG value;
175 LDOUBLE fvalue;
176 char *strvalue;
177 int min;
178 int max;
179 int state;
180 int flags;
181 int cflags;
182 size_t currlen;
183
184 state = DP_S_DEFAULT;
185 flags = currlen = cflags = min = 0;
186 max = -1;
187 ch = *format++;
188
189 while (state != DP_S_DONE) {
190 if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
191 state = DP_S_DONE;
192
193 switch (state) {
194 case DP_S_DEFAULT:
195 if (ch == '%')
196 state = DP_S_FLAGS;
197 else
198 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
199 ch = *format++;
200 break;
201 case DP_S_FLAGS:
202 switch (ch) {
203 case '-':
204 flags |= DP_F_MINUS;
205 ch = *format++;
206 break;
207 case '+':
208 flags |= DP_F_PLUS;
209 ch = *format++;
210 break;
211 case ' ':
212 flags |= DP_F_SPACE;
213 ch = *format++;
214 break;
215 case '#':
216 flags |= DP_F_NUM;
217 ch = *format++;
218 break;
219 case '0':
220 flags |= DP_F_ZERO;
221 ch = *format++;
222 break;
223 default:
224 state = DP_S_MIN;
225 break;
226 }
227 break;
228 case DP_S_MIN:
229 if (isdigit((unsigned char)ch)) {
230 min = 10 * min + char_to_int(ch);
231 ch = *format++;
232 } else if (ch == '*') {
233 min = va_arg(args, int);
234 ch = *format++;
235 state = DP_S_DOT;
236 } else
237 state = DP_S_DOT;
238 break;
239 case DP_S_DOT:
240 if (ch == '.') {
241 state = DP_S_MAX;
242 ch = *format++;
243 } else
244 state = DP_S_MOD;
245 break;
246 case DP_S_MAX:
247 if (isdigit((unsigned char)ch)) {
248 if (max < 0)
249 max = 0;
250 max = 10 * max + char_to_int(ch);
251 ch = *format++;
252 } else if (ch == '*') {
253 max = va_arg(args, int);
254 ch = *format++;
255 state = DP_S_MOD;
256 } else
257 state = DP_S_MOD;
258 break;
259 case DP_S_MOD:
260 switch (ch) {
261 case 'h':
262 cflags = DP_C_SHORT;
263 ch = *format++;
264 break;
265 case 'l':
266 if (*format == 'l') {
267 cflags = DP_C_LLONG;
268 format++;
269 } else
270 cflags = DP_C_LONG;
271 ch = *format++;
272 break;
273 case 'q':
274 cflags = DP_C_LLONG;
275 ch = *format++;
276 break;
277 case 'L':
278 cflags = DP_C_LDOUBLE;
279 ch = *format++;
280 break;
281 default:
282 break;
283 }
284 state = DP_S_CONV;
285 break;
286 case DP_S_CONV:
287 switch (ch) {
288 case 'd':
289 case 'i':
290 switch (cflags) {
291 case DP_C_SHORT:
292 value = (short int)va_arg(args, int);
293 break;
294 case DP_C_LONG:
295 value = va_arg(args, long int);
296 break;
297 case DP_C_LLONG:
298 value = va_arg(args, LLONG);
299 break;
300 default:
301 value = va_arg(args, int);
302 break;
303 }
304 fmtint(sbuffer, buffer, &currlen, maxlen,
305 value, 10, min, max, flags);
306 break;
307 case 'X':
308 flags |= DP_F_UP;
309 /* FALLTHROUGH */
310 case 'x':
311 case 'o':
312 case 'u':
313 flags |= DP_F_UNSIGNED;
314 switch (cflags) {
315 case DP_C_SHORT:
316 value = (unsigned short int)va_arg(args, unsigned int);
317 break;
318 case DP_C_LONG:
319 value = (LLONG) va_arg(args, unsigned long int);
320 break;
321 case DP_C_LLONG:
322 value = va_arg(args, unsigned LLONG);
323 break;
324 default:
325 value = (LLONG) va_arg(args, unsigned int);
326 break;
327 }
328 fmtint(sbuffer, buffer, &currlen, maxlen, value,
329 ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
330 min, max, flags);
331 break;
332 case 'f':
333 if (cflags == DP_C_LDOUBLE)
334 fvalue = va_arg(args, LDOUBLE);
335 else
336 fvalue = va_arg(args, double);
337 fmtfp(sbuffer, buffer, &currlen, maxlen,
338 fvalue, min, max, flags);
339 break;
340 case 'E':
341 flags |= DP_F_UP;
342 case 'e':
343 if (cflags == DP_C_LDOUBLE)
344 fvalue = va_arg(args, LDOUBLE);
345 else
346 fvalue = va_arg(args, double);
347 break;
348 case 'G':
349 flags |= DP_F_UP;
350 case 'g':
351 if (cflags == DP_C_LDOUBLE)
352 fvalue = va_arg(args, LDOUBLE);
353 else
354 fvalue = va_arg(args, double);
355 break;
356 case 'c':
357 doapr_outch(sbuffer, buffer, &currlen, maxlen,
358 va_arg(args, int));
359 break;
360 case 's':
361 strvalue = va_arg(args, char *);
362 if (max < 0) {
363 if (buffer)
364 max = INT_MAX;
365 else
366 max = *maxlen;
367 }
368 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
369 flags, min, max);
370 break;
371 case 'p':
372 value = (size_t)va_arg(args, void *);
373 fmtint(sbuffer, buffer, &currlen, maxlen,
374 value, 16, min, max, flags | DP_F_NUM);
375 break;
376 case 'n': /* XXX */
377 if (cflags == DP_C_SHORT) {
378 short int *num;
379 num = va_arg(args, short int *);
380 *num = currlen;
381 } else if (cflags == DP_C_LONG) { /* XXX */
382 long int *num;
383 num = va_arg(args, long int *);
384 *num = (long int)currlen;
385 } else if (cflags == DP_C_LLONG) { /* XXX */
386 LLONG *num;
387 num = va_arg(args, LLONG *);
388 *num = (LLONG) currlen;
389 } else {
390 int *num;
391 num = va_arg(args, int *);
392 *num = currlen;
393 }
394 break;
395 case '%':
396 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
397 break;
398 case 'w':
399 /* not supported yet, treat as next char */
400 ch = *format++;
401 break;
402 default:
403 /* unknown, skip */
404 break;
405 }
406 ch = *format++;
407 state = DP_S_DEFAULT;
408 flags = cflags = min = 0;
409 max = -1;
410 break;
411 case DP_S_DONE:
412 break;
413 default:
414 break;
415 }
416 }
417 *truncated = (currlen > *maxlen - 1);
418 if (*truncated)
419 currlen = *maxlen - 1;
420 doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
421 *retlen = currlen - 1;
422 return;
423 }
424
425 static void
426 fmtstr(char **sbuffer,
427 char **buffer,
428 size_t *currlen,
429 size_t *maxlen, const char *value, int flags, int min, int max)
430 {
431 int padlen, strln;
432 int cnt = 0;
433
434 if (value == 0)
435 value = "<NULL>";
436 for (strln = 0; value[strln]; ++strln) ;
437 padlen = min - strln;
438 if (padlen < 0)
439 padlen = 0;
440 if (flags & DP_F_MINUS)
441 padlen = -padlen;
442
443 while ((padlen > 0) && (cnt < max)) {
444 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
445 --padlen;
446 ++cnt;
447 }
448 while (*value && (cnt < max)) {
449 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
450 ++cnt;
451 }
452 while ((padlen < 0) && (cnt < max)) {
453 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
454 ++padlen;
455 ++cnt;
456 }
457 }
458
459 static void
460 fmtint(char **sbuffer,
461 char **buffer,
462 size_t *currlen,
463 size_t *maxlen, LLONG value, int base, int min, int max, int flags)
464 {
465 int signvalue = 0;
466 const char *prefix = "";
467 unsigned LLONG uvalue;
468 char convert[DECIMAL_SIZE(value) + 3];
469 int place = 0;
470 int spadlen = 0;
471 int zpadlen = 0;
472 int caps = 0;
473
474 if (max < 0)
475 max = 0;
476 uvalue = value;
477 if (!(flags & DP_F_UNSIGNED)) {
478 if (value < 0) {
479 signvalue = '-';
480 uvalue = -value;
481 } else if (flags & DP_F_PLUS)
482 signvalue = '+';
483 else if (flags & DP_F_SPACE)
484 signvalue = ' ';
485 }
486 if (flags & DP_F_NUM) {
487 if (base == 8)
488 prefix = "0";
489 if (base == 16)
490 prefix = "0x";
491 }
492 if (flags & DP_F_UP)
493 caps = 1;
494 do {
495 convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
496 [uvalue % (unsigned)base];
497 uvalue = (uvalue / (unsigned)base);
498 } while (uvalue && (place < (int)sizeof(convert)));
499 if (place == sizeof(convert))
500 place--;
501 convert[place] = 0;
502
503 zpadlen = max - place;
504 spadlen =
505 min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
506 if (zpadlen < 0)
507 zpadlen = 0;
508 if (spadlen < 0)
509 spadlen = 0;
510 if (flags & DP_F_ZERO) {
511 zpadlen = OSSL_MAX(zpadlen, spadlen);
512 spadlen = 0;
513 }
514 if (flags & DP_F_MINUS)
515 spadlen = -spadlen;
516
517 /* spaces */
518 while (spadlen > 0) {
519 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
520 --spadlen;
521 }
522
523 /* sign */
524 if (signvalue)
525 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
526
527 /* prefix */
528 while (*prefix) {
529 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
530 prefix++;
531 }
532
533 /* zeros */
534 if (zpadlen > 0) {
535 while (zpadlen > 0) {
536 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
537 --zpadlen;
538 }
539 }
540 /* digits */
541 while (place > 0)
542 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
543
544 /* left justified spaces */
545 while (spadlen < 0) {
546 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
547 ++spadlen;
548 }
549 return;
550 }
551
552 static LDOUBLE abs_val(LDOUBLE value)
553 {
554 LDOUBLE result = value;
555 if (value < 0)
556 result = -value;
557 return result;
558 }
559
560 static LDOUBLE pow_10(int in_exp)
561 {
562 LDOUBLE result = 1;
563 while (in_exp) {
564 result *= 10;
565 in_exp--;
566 }
567 return result;
568 }
569
570 static long roundv(LDOUBLE value)
571 {
572 long intpart;
573 intpart = (long)value;
574 value = value - intpart;
575 if (value >= 0.5)
576 intpart++;
577 return intpart;
578 }
579
580 static void
581 fmtfp(char **sbuffer,
582 char **buffer,
583 size_t *currlen,
584 size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
585 {
586 int signvalue = 0;
587 LDOUBLE ufvalue;
588 char iconvert[20];
589 char fconvert[20];
590 int iplace = 0;
591 int fplace = 0;
592 int padlen = 0;
593 int zpadlen = 0;
594 long intpart;
595 long fracpart;
596 long max10;
597
598 if (max < 0)
599 max = 6;
600 ufvalue = abs_val(fvalue);
601 if (fvalue < 0)
602 signvalue = '-';
603 else if (flags & DP_F_PLUS)
604 signvalue = '+';
605 else if (flags & DP_F_SPACE)
606 signvalue = ' ';
607
608 intpart = (long)ufvalue;
609
610 /*
611 * sorry, we only support 9 digits past the decimal because of our
612 * conversion method
613 */
614 if (max > 9)
615 max = 9;
616
617 /*
618 * we "cheat" by converting the fractional part to integer by multiplying
619 * by a factor of 10
620 */
621 max10 = roundv(pow_10(max));
622 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
623
624 if (fracpart >= max10) {
625 intpart++;
626 fracpart -= max10;
627 }
628
629 /* convert integer part */
630 do {
631 iconvert[iplace++] = "0123456789"[intpart % 10];
632 intpart = (intpart / 10);
633 } while (intpart && (iplace < (int)sizeof(iconvert)));
634 if (iplace == sizeof iconvert)
635 iplace--;
636 iconvert[iplace] = 0;
637
638 /* convert fractional part */
639 do {
640 fconvert[fplace++] = "0123456789"[fracpart % 10];
641 fracpart = (fracpart / 10);
642 } while (fplace < max);
643 if (fplace == sizeof fconvert)
644 fplace--;
645 fconvert[fplace] = 0;
646
647 /* -1 for decimal point, another -1 if we are printing a sign */
648 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
649 zpadlen = max - fplace;
650 if (zpadlen < 0)
651 zpadlen = 0;
652 if (padlen < 0)
653 padlen = 0;
654 if (flags & DP_F_MINUS)
655 padlen = -padlen;
656
657 if ((flags & DP_F_ZERO) && (padlen > 0)) {
658 if (signvalue) {
659 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
660 --padlen;
661 signvalue = 0;
662 }
663 while (padlen > 0) {
664 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
665 --padlen;
666 }
667 }
668 while (padlen > 0) {
669 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
670 --padlen;
671 }
672 if (signvalue)
673 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
674
675 while (iplace > 0)
676 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
677
678 /*
679 * Decimal point. This should probably use locale to find the correct
680 * char to print out.
681 */
682 if (max > 0 || (flags & DP_F_NUM)) {
683 doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
684
685 while (fplace > 0)
686 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
687 }
688 while (zpadlen > 0) {
689 doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
690 --zpadlen;
691 }
692
693 while (padlen < 0) {
694 doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
695 ++padlen;
696 }
697 }
698
699 static void
700 doapr_outch(char **sbuffer,
701 char **buffer, size_t *currlen, size_t *maxlen, int c)
702 {
703 /* If we haven't at least one buffer, someone has doe a big booboo */
704 assert(*sbuffer != NULL || buffer != NULL);
705
706 /* |currlen| must always be <= |*maxlen| */
707 assert(*currlen <= *maxlen);
708
709 if (buffer && *currlen == *maxlen) {
710 *maxlen += 1024;
711 if (*buffer == NULL) {
712 *buffer = OPENSSL_malloc(*maxlen);
713 if (*buffer == NULL) {
714 /* Panic! Can't really do anything sensible. Just return */
715 return;
716 }
717 if (*currlen > 0) {
718 assert(*sbuffer != NULL);
719 memcpy(*buffer, *sbuffer, *currlen);
720 }
721 *sbuffer = NULL;
722 } else {
723 *buffer = OPENSSL_realloc(*buffer, *maxlen);
724 if (!*buffer) {
725 /* Panic! Can't really do anything sensible. Just return */
726 return;
727 }
728 }
729 }
730
731 if (*currlen < *maxlen) {
732 if (*sbuffer)
733 (*sbuffer)[(*currlen)++] = (char)c;
734 else
735 (*buffer)[(*currlen)++] = (char)c;
736 }
737
738 return;
739 }
740
741 /***************************************************************************/
742
743 int BIO_printf(BIO *bio, const char *format, ...)
744 {
745 va_list args;
746 int ret;
747
748 va_start(args, format);
749
750 ret = BIO_vprintf(bio, format, args);
751
752 va_end(args);
753 return (ret);
754 }
755
756 int BIO_vprintf(BIO *bio, const char *format, va_list args)
757 {
758 int ret;
759 size_t retlen;
760 char hugebuf[1024 * 2]; /* Was previously 10k, which is unreasonable
761 * in small-stack environments, like threads
762 * or DOS programs. */
763 char *hugebufp = hugebuf;
764 size_t hugebufsize = sizeof(hugebuf);
765 char *dynbuf = NULL;
766 int ignored;
767
768 dynbuf = NULL;
769 _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, args);
770 if (dynbuf) {
771 ret = BIO_write(bio, dynbuf, (int)retlen);
772 OPENSSL_free(dynbuf);
773 } else {
774 ret = BIO_write(bio, hugebuf, (int)retlen);
775 }
776 return (ret);
777 }
778
779 /*
780 * As snprintf is not available everywhere, we provide our own
781 * implementation. This function has nothing to do with BIOs, but it's
782 * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
783 * function should be renamed, but to what?)
784 */
785 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
786 {
787 va_list args;
788 int ret;
789
790 va_start(args, format);
791
792 ret = BIO_vsnprintf(buf, n, format, args);
793
794 va_end(args);
795 return (ret);
796 }
797
798 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
799 {
800 size_t retlen;
801 int truncated;
802
803 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
804
805 if (truncated)
806 /*
807 * In case of truncation, return -1 like traditional snprintf.
808 * (Current drafts for ISO/IEC 9899 say snprintf should return the
809 * number of characters that would have been written, had the buffer
810 * been large enough.)
811 */
812 return -1;
813 else
814 return (retlen <= INT_MAX) ? (int)retlen : -1;
815 }