import libcrypto (LibreSSL 2.5.2)
[unleashed.git] / lib / libcrypto / bio / b_print.c
blob09747767ddeec04f641635a81ede26d82cdfefa1
1 /* $OpenBSD: b_print.c,v 1.25 2014/06/12 15:49:28 deraadt Exp $ */
3 /* Theo de Raadt places this file in the public domain. */
5 #include <openssl/bio.h>
7 int
8 BIO_printf(BIO *bio, const char *format, ...)
10 va_list args;
11 int ret;
13 va_start(args, format);
14 ret = BIO_vprintf(bio, format, args);
15 va_end(args);
16 return (ret);
19 #ifdef HAVE_FUNOPEN
20 static int
21 _BIO_write(void *cookie, const char *buf, int nbytes)
23 return BIO_write(cookie, buf, nbytes);
26 int
27 BIO_vprintf(BIO *bio, const char *format, va_list args)
29 int ret;
30 FILE *fp;
32 fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
33 if (fp == NULL) {
34 ret = -1;
35 goto fail;
37 ret = vfprintf(fp, format, args);
38 fclose(fp);
39 fail:
40 return (ret);
43 #else /* !HAVE_FUNOPEN */
45 int
46 BIO_vprintf(BIO *bio, const char *format, va_list args)
48 int ret;
49 char *buf = NULL;
51 ret = vasprintf(&buf, format, args);
52 if (buf == NULL) {
53 ret = -1;
54 goto fail;
56 BIO_write(bio, buf, ret);
57 free(buf);
58 fail:
59 return (ret);
62 #endif /* HAVE_FUNOPEN */
65 * BIO_snprintf and BIO_vsnprintf return -1 for overflow,
66 * due to the history of this API. Justification:
68 * Traditional snprintf surfaced in 4.4BSD, and returned
69 * "number of bytes wanted". Solaris and Windows opted to
70 * return -1. A draft standard was written which returned -1.
71 * Due to the large volume of code already using the first
72 * semantics, the draft was repaired before standardization to
73 * specify "number of bytes wanted" plus "-1 for character conversion
74 * style errors". Solaris adapted to this rule, but Windows stuck
75 * with -1.
77 * Original OpenSSL comment which is full of lies:
79 * "In case of truncation, return -1 like traditional snprintf.
80 * (Current drafts for ISO/IEC 9899 say snprintf should return
81 * the number of characters that would have been written,
82 * had the buffer been large enough.)"
84 int
85 BIO_snprintf(char *buf, size_t n, const char *format, ...)
87 va_list args;
88 int ret;
90 va_start(args, format);
91 ret = vsnprintf(buf, n, format, args);
92 va_end(args);
94 if (ret >= n || ret == -1)
95 return (-1);
96 return (ret);
99 int
100 BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
102 int ret;
104 ret = vsnprintf(buf, n, format, args);
106 if (ret >= n || ret == -1)
107 return (-1);
108 return (ret);