Fix Typos
[openssl.git] / crypto / err / err_prn.c
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/crypto.h>
13 #include <openssl/buffer.h>
14 #include <openssl/err.h>
15
16 void ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
17                          void *u)
18 {
19     CRYPTO_THREAD_ID tid = CRYPTO_THREAD_get_current_id();
20     unsigned long l;
21     char buf[256];
22     char buf2[4096], *hex;
23     const char *file, *data;
24     int line, flags;
25
26     while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
27         ERR_error_string_n(l, buf, sizeof(buf));
28         hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
29         BIO_snprintf(buf2, sizeof(buf2), "%s:%s:%s:%d:%s\n", hex, buf, file,
30                      line, (flags & ERR_TXT_STRING) ? data : "");
31         OPENSSL_free(hex);
32         if (cb(buf2, strlen(buf2), u) <= 0)
33             break;              /* abort outputting the error report */
34     }
35 }
36
37 static int print_bio(const char *str, size_t len, void *bp)
38 {
39     return BIO_write((BIO *)bp, str, len);
40 }
41
42 void ERR_print_errors(BIO *bp)
43 {
44     ERR_print_errors_cb(print_bio, bp);
45 }
46
47 #ifndef OPENSSL_NO_STDIO
48 void ERR_print_errors_fp(FILE *fp)
49 {
50     BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
51     if (bio == NULL)
52         return;
53
54     ERR_print_errors_cb(print_bio, bio);
55     BIO_free(bio);
56 }
57 #endif