| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bio.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <stdarg.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
int BIO_printf(BIO *bio, const char *format, ...) { |
| 14 |
va_list args; |
| 15 |
char buf[256], *out, out_malloced = 0; |
| 16 |
int out_len, ret; |
| 17 |
|
| 18 |
va_start(args, format); |
| 19 |
out_len = vsnprintf(buf, sizeof(buf), format, args); |
| 20 |
va_end(args); |
| 21 |
if (out_len < 0) { |
| 22 |
return -1; |
| 23 |
} |
| 24 |
|
| 25 |
if ((size_t) out_len >= sizeof(buf)) { |
| 26 |
const size_t requested_len = (size_t)out_len; |
| 27 |
// The output was truncated. Note that vsnprintf's return value does not |
| 28 |
// include a trailing NUL, but the buffer must be sized for it. |
| 29 |
out = OPENSSL_malloc(requested_len + 1); |
| 30 |
out_malloced = 1; |
| 31 |
if (out == NULL) { |
| 32 |
return -1; |
| 33 |
} |
| 34 |
va_start(args, format); |
| 35 |
out_len = vsnprintf(out, requested_len + 1, format, args); |
| 36 |
va_end(args); |
| 37 |
assert(out_len == (int)requested_len); |
| 38 |
} else { |
| 39 |
out = buf; |
| 40 |
} |
| 41 |
|
| 42 |
ret = BIO_write(bio, out, out_len); |
| 43 |
if (out_malloced) { |
| 44 |
OPENSSL_free(out); |
| 45 |
} |
| 46 |
|
| 47 |
return ret; |
| 48 |
} |