summaryrefslogtreecommitdiff
path: root/lib/libcrypto/bio/b_print.c
blob: f6943ea3f3819c19fe28bd894190a7d47993729e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* $OpenBSD: b_print.c,v 1.28 2024/03/02 09:18:28 tb Exp $ */

/* Theo de Raadt places this file in the public domain. */

#include <openssl/bio.h>

#include "bio_local.h"

#ifdef HAVE_FUNOPEN
static int
_BIO_write(void *cookie, const char *buf, int nbytes)
{
	return BIO_write(cookie, buf, nbytes);
}

int
BIO_vprintf(BIO *bio, const char *format, va_list args)
{
	int ret;
	FILE *fp;

	fp = funopen(bio, NULL, &_BIO_write, NULL, NULL);
	if (fp == NULL) {
		ret = -1;
		goto fail;
	}
	ret = vfprintf(fp, format, args);
	fclose(fp);
fail:
	return (ret);
}

#else /* !HAVE_FUNOPEN */

int
BIO_vprintf(BIO *bio, const char *format, va_list args)
{
	int ret;
	char *buf = NULL;

	ret = vasprintf(&buf, format, args);
	if (ret == -1)
		return (ret);
	BIO_write(bio, buf, ret);
	free(buf);
	return (ret);
}

#endif /* HAVE_FUNOPEN */

int
BIO_printf(BIO *bio, const char *format, ...)
{
	va_list args;
	int ret;

	va_start(args, format);
	ret = BIO_vprintf(bio, format, args);
	va_end(args);
	return (ret);
}
LCRYPTO_ALIAS(BIO_printf);