err_prn.c revision 280304
1175702Smarius/* crypto/err/err_prn.c */
2175702Smarius/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3175702Smarius * All rights reserved.
4175702Smarius *
5175702Smarius * This package is an SSL implementation written
6175702Smarius * by Eric Young (eay@cryptsoft.com).
7175702Smarius * The implementation was written so as to conform with Netscapes SSL.
8175702Smarius *
9175702Smarius * This library is free for commercial and non-commercial use as long as
10175702Smarius * the following conditions are aheared to.  The following conditions
11175702Smarius * apply to all code found in this distribution, be it the RC4, RSA,
12175702Smarius * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13175702Smarius * included with this distribution is covered by the same copyright terms
14175702Smarius * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15175702Smarius *
16175702Smarius * Copyright remains Eric Young's, and as such any Copyright notices in
17175702Smarius * the code are not to be removed.
18175702Smarius * If this package is used in a product, Eric Young should be given attribution
19175702Smarius * as the author of the parts of the library used.
20175702Smarius * This can be in the form of a textual message at program startup or
21175702Smarius * in documentation (online or textual) provided with the package.
22175702Smarius *
23175702Smarius * Redistribution and use in source and binary forms, with or without
24175702Smarius * modification, are permitted provided that the following conditions
25175702Smarius * are met:
26175702Smarius * 1. Redistributions of source code must retain the copyright
27175702Smarius *    notice, this list of conditions and the following disclaimer.
28175702Smarius * 2. Redistributions in binary form must reproduce the above copyright
29175702Smarius *    notice, this list of conditions and the following disclaimer in the
30175702Smarius *    documentation and/or other materials provided with the distribution.
31175702Smarius * 3. All advertising materials mentioning features or use of this software
32175702Smarius *    must display the following acknowledgement:
33175702Smarius *    "This product includes cryptographic software written by
34175702Smarius *     Eric Young (eay@cryptsoft.com)"
35175702Smarius *    The word 'cryptographic' can be left out if the rouines from the library
36175702Smarius *    being used are not cryptographic related :-).
37175702Smarius * 4. If you include any Windows specific code (or a derivative thereof) from
38175702Smarius *    the apps directory (application code) you must include an acknowledgement:
39175702Smarius *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40175702Smarius *
41175702Smarius * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42175702Smarius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43175702Smarius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44175702Smarius * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45175702Smarius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46175702Smarius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47175702Smarius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48175702Smarius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49175702Smarius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50175702Smarius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51175702Smarius * SUCH DAMAGE.
52175702Smarius *
53175702Smarius * The licence and distribution terms for any publically available version or
54175702Smarius * derivative of this code cannot be changed.  i.e. this code cannot simply be
55175702Smarius * copied and put under another distribution licence
56175702Smarius * [including the GNU Public Licence.]
57175702Smarius */
58175702Smarius
59175702Smarius#include <stdio.h>
60175702Smarius#include "cryptlib.h"
61231914Smarius#include <openssl/lhash.h>
62231914Smarius#include <openssl/crypto.h>
63231914Smarius#include <openssl/buffer.h>
64175702Smarius#include <openssl/err.h>
65175702Smarius
66175702Smariusvoid ERR_print_errors_cb(int (*cb) (const char *str, size_t len, void *u),
67175702Smarius                         void *u)
68175702Smarius{
69175702Smarius    unsigned long l;
70175702Smarius    char buf[256];
71175702Smarius    char buf2[4096];
72175702Smarius    const char *file, *data;
73175702Smarius    int line, flags;
74175702Smarius    unsigned long es;
75175702Smarius    CRYPTO_THREADID cur;
76175702Smarius
77175702Smarius    CRYPTO_THREADID_current(&cur);
78175702Smarius    es = CRYPTO_THREADID_hash(&cur);
79175702Smarius    while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0) {
80175702Smarius        ERR_error_string_n(l, buf, sizeof buf);
81175702Smarius        BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", es, buf,
82175702Smarius                     file, line, (flags & ERR_TXT_STRING) ? data : "");
83175702Smarius        if (cb(buf2, strlen(buf2), u) <= 0)
84175702Smarius            break;              /* abort outputting the error report */
85175702Smarius    }
86175702Smarius}
87175702Smarius
88175702Smarius#ifndef OPENSSL_NO_FP_API
89175702Smariusstatic int print_fp(const char *str, size_t len, void *fp)
90175702Smarius{
91175702Smarius    BIO bio;
92175702Smarius
93175702Smarius    BIO_set(&bio, BIO_s_file());
94175702Smarius    BIO_set_fp(&bio, fp, BIO_NOCLOSE);
95175702Smarius
96175702Smarius    return BIO_printf(&bio, "%s", str);
97227908Smarius}
98175702Smarius
99175702Smariusvoid ERR_print_errors_fp(FILE *fp)
100175702Smarius{
101175702Smarius    ERR_print_errors_cb(print_fp, fp);
102175702Smarius}
103175702Smarius#endif
104175702Smarius
105175702Smariusstatic int print_bio(const char *str, size_t len, void *bp)
106175702Smarius{
107175702Smarius    return BIO_write((BIO *)bp, str, len);
108175702Smarius}
109175702Smarius
110175702Smariusvoid ERR_print_errors(BIO *bp)
111175702Smarius{
112175702Smarius    ERR_print_errors_cb(print_bio, bp);
113175702Smarius}
114221407Smarius