xref: /openbsd/lib/libcrypto/bio/b_print.c (revision 8bb5c4c3)
1 /* $OpenBSD: b_print.c,v 1.28 2024/03/02 09:18:28 tb Exp $ */
2 
3 /* Theo de Raadt places this file in the public domain. */
4 
5 #include <openssl/bio.h>
6 
7 #include "bio_local.h"
8 
9 #ifdef HAVE_FUNOPEN
10 static int
_BIO_write(void * cookie,const char * buf,int nbytes)11 _BIO_write(void *cookie, const char *buf, int nbytes)
12 {
13 	return BIO_write(cookie, buf, nbytes);
14 }
15 
16 int
BIO_vprintf(BIO * bio,const char * format,va_list args)17 BIO_vprintf(BIO *bio, const char *format, va_list args)
18 {
19 	int ret;
20 	FILE *fp;
21 
22 	fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
23 	if (fp == NULL) {
24 		ret = -1;
25 		goto fail;
26 	}
27 	ret = vfprintf(fp, format, args);
28 	fclose(fp);
29 fail:
30 	return (ret);
31 }
32 
33 #else /* !HAVE_FUNOPEN */
34 
35 int
BIO_vprintf(BIO * bio,const char * format,va_list args)36 BIO_vprintf(BIO *bio, const char *format, va_list args)
37 {
38 	int ret;
39 	char *buf = NULL;
40 
41 	ret = vasprintf(&buf, format, args);
42 	if (ret == -1)
43 		return (ret);
44 	BIO_write(bio, buf, ret);
45 	free(buf);
46 	return (ret);
47 }
48 
49 #endif /* HAVE_FUNOPEN */
50 
51 int
BIO_printf(BIO * bio,const char * format,...)52 BIO_printf(BIO *bio, const char *format, ...)
53 {
54 	va_list args;
55 	int ret;
56 
57 	va_start(args, format);
58 	ret = BIO_vprintf(bio, format, args);
59 	va_end(args);
60 	return (ret);
61 }
62 LCRYPTO_ALIAS(BIO_printf);
63