err_prn.c revision 215697
11573Srgrimes/* crypto/err/err_prn.c */
21573Srgrimes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
31573Srgrimes * All rights reserved.
41573Srgrimes *
51573Srgrimes * This package is an SSL implementation written
61573Srgrimes * by Eric Young (eay@cryptsoft.com).
71573Srgrimes * The implementation was written so as to conform with Netscapes SSL.
81573Srgrimes *
91573Srgrimes * This library is free for commercial and non-commercial use as long as
101573Srgrimes * the following conditions are aheared to.  The following conditions
111573Srgrimes * apply to all code found in this distribution, be it the RC4, RSA,
121573Srgrimes * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
131573Srgrimes * included with this distribution is covered by the same copyright terms
141573Srgrimes * except that the holder is Tim Hudson (tjh@cryptsoft.com).
151573Srgrimes *
161573Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in
171573Srgrimes * the code are not to be removed.
181573Srgrimes * If this package is used in a product, Eric Young should be given attribution
191573Srgrimes * as the author of the parts of the library used.
201573Srgrimes * This can be in the form of a textual message at program startup or
211573Srgrimes * in documentation (online or textual) provided with the package.
221573Srgrimes *
231573Srgrimes * Redistribution and use in source and binary forms, with or without
241573Srgrimes * modification, are permitted provided that the following conditions
251573Srgrimes * are met:
261573Srgrimes * 1. Redistributions of source code must retain the copyright
271573Srgrimes *    notice, this list of conditions and the following disclaimer.
281573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
2950476Speter *    notice, this list of conditions and the following disclaimer in the
301573Srgrimes *    documentation and/or other materials provided with the distribution.
31251203Sjilles * 3. All advertising materials mentioning features or use of this software
321573Srgrimes *    must display the following acknowledgement:
3379531Sru *    "This product includes cryptographic software written by
341573Srgrimes *     Eric Young (eay@cryptsoft.com)"
351573Srgrimes *    The word 'cryptographic' can be left out if the rouines from the library
361573Srgrimes *    being used are not cryptographic related :-).
371573Srgrimes * 4. If you include any Windows specific code (or a derivative thereof) from
3859460Sphantom *    the apps directory (application code) you must include an acknowledgement:
3959460Sphantom *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
401573Srgrimes *
4184306Sru * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
421573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
431573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
441573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
451573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
461573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47100144Skeramida * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48100144Skeramida * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49108028Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
501573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
511573Srgrimes * SUCH DAMAGE.
521573Srgrimes *
531573Srgrimes * The licence and distribution terms for any publically available version or
541573Srgrimes * derivative of this code cannot be changed.  i.e. this code cannot simply be
551573Srgrimes * copied and put under another distribution licence
561573Srgrimes * [including the GNU Public Licence.]
571573Srgrimes */
58131504Sru
591573Srgrimes#include <stdio.h>
601573Srgrimes#include "cryptlib.h"
611573Srgrimes#include <openssl/lhash.h>
621573Srgrimes#include <openssl/crypto.h>
631573Srgrimes#include <openssl/buffer.h>
641573Srgrimes#include <openssl/err.h>
651573Srgrimes
661573Srgrimesvoid ERR_print_errors_cb(int (*cb)(const char *str, size_t len, void *u),
671573Srgrimes			 void *u)
681573Srgrimes	{
691573Srgrimes	unsigned long l;
701573Srgrimes	char buf[256];
711573Srgrimes	char buf2[4096];
721573Srgrimes	const char *file,*data;
731573Srgrimes	int line,flags;
741573Srgrimes	unsigned long es;
751573Srgrimes
761573Srgrimes	es=CRYPTO_thread_id();
771573Srgrimes	while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
781573Srgrimes		{
791573Srgrimes		ERR_error_string_n(l, buf, sizeof buf);
801573Srgrimes		BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", es, buf,
811573Srgrimes			file, line, (flags & ERR_TXT_STRING) ? data : "");
821573Srgrimes		if (cb(buf2, strlen(buf2), u) <= 0)
831573Srgrimes			break; /* abort outputting the error report */
841573Srgrimes		}
851573Srgrimes	}
86108028Sru
871573Srgrimes#ifndef OPENSSL_NO_FP_API
881573Srgrimesstatic int print_fp(const char *str, size_t len, void *fp)
8979754Sdd	{
901573Srgrimes	BIO bio;
911573Srgrimes
921573Srgrimes	BIO_set(&bio,BIO_s_file());
93131504Sru	BIO_set_fp(&bio,fp,BIO_NOCLOSE);
94131504Sru
9523574Smckay	return BIO_printf(&bio, "%s", str);
9681462Sru	}
9723574Smckayvoid ERR_print_errors_fp(FILE *fp)
98108028Sru	{
991573Srgrimes	ERR_print_errors_cb(print_fp, fp);
100108028Sru	}
10123574Smckay#endif
10223574Smckay
10323574Smckayvoid ERR_error_string_n(unsigned long e, char *buf, size_t len)
10423574Smckay	{
10523574Smckay	char lsbuf[64], fsbuf[64], rsbuf[64];
10623574Smckay	const char *ls,*fs,*rs;
10723574Smckay	unsigned long l,f,r;
10823574Smckay
10923574Smckay	l=ERR_GET_LIB(e);
11023574Smckay	f=ERR_GET_FUNC(e);
11123574Smckay	r=ERR_GET_REASON(e);
11223574Smckay
11323574Smckay	ls=ERR_lib_error_string(e);
11423574Smckay	fs=ERR_func_error_string(e);
1151573Srgrimes	rs=ERR_reason_error_string(e);
116251203Sjilles
117251203Sjilles	if (ls == NULL)
118251203Sjilles		BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
1191573Srgrimes	if (fs == NULL)
120251203Sjilles		BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
1211573Srgrimes	if (rs == NULL)
122100144Skeramida		BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
123100144Skeramida
124234131Seadler	BIO_snprintf(buf, len,"error:%08lX:%s:%s:%s", e, ls?ls:lsbuf,
1251573Srgrimes		fs?fs:fsbuf, rs?rs:rsbuf);
1261573Srgrimes	if (strlen(buf) == len-1)
127108087Sru		{
128108087Sru		/* output may be truncated; make sure we always have 5
129108087Sru		 * colon-separated fields, i.e. 4 colons ... */
1301573Srgrimes#define NUM_COLONS 4
1311573Srgrimes		if (len > NUM_COLONS) /* ... if possible */
1321573Srgrimes			{
1331573Srgrimes			int i;
134239503Szeising			char *s = buf;
135234131Seadler
136234131Seadler			for (i = 0; i < NUM_COLONS; i++)
137234131Seadler				{
138234131Seadler				char *colon = strchr(s, ':');
139234131Seadler				if (colon == NULL || colon > &buf[len-1] - NUM_COLONS + i)
140234131Seadler					{
141234131Seadler					/* set colon no. i at last possible position
142234131Seadler					 * (buf[len-1] is the terminating 0)*/
143234131Seadler					colon = &buf[len-1] - NUM_COLONS + i;
144234131Seadler					*colon = ':';
145234131Seadler					}
1461573Srgrimes				s = colon + 1;
1471573Srgrimes				}
1481573Srgrimes			}
1491573Srgrimes		}
15021907Swosch	}
15121907Swosch
1521573Srgrimes/* BAD for multi-threading: uses a local buffer if ret == NULL */
1531573Srgrimes/* ERR_error_string_n should be used instead for ret != NULL
154254410Sjilles * as ERR_error_string cannot know how large the buffer is */
155254410Sjilleschar *ERR_error_string(unsigned long e, char *ret)
1561573Srgrimes	{
15720097Swosch	static char buf[256];
15820097Swosch
1591573Srgrimes	if (ret == NULL) ret=buf;
1601573Srgrimes	ERR_error_string_n(e, ret, 256);
161108028Sru
16220097Swosch	return ret;
16317906Swosch	}
164108028Sru