Path: blob/main/crypto/heimdal/lib/com_err/com_err.c
34878 views
/*1* Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/323334#include <config.h>3536#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <roken.h>40#include "com_err.h"4142struct et_list *_et_list = NULL;434445KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL46error_message (long code)47{48static char msg[128];49const char *p = com_right(_et_list, code);50if (p == NULL) {51if (code < 0)52snprintf(msg, sizeof(msg), "Unknown error %ld", code);53else54p = strerror(code);55}56if (p != NULL && *p != '\0') {57strlcpy(msg, p, sizeof(msg));58} else59snprintf(msg, sizeof(msg), "Unknown error %ld", code);60return msg;61}6263KRB5_LIB_FUNCTION int KRB5_LIB_CALL64init_error_table(const char **msgs, long base, int count)65{66initialize_error_table_r(&_et_list, msgs, count, base);67return 0;68}6970static void KRB5_CALLCONV71default_proc (const char *whoami, long code, const char *fmt, va_list args)72__attribute__((__format__(__printf__, 3, 0)));7374static void KRB5_CALLCONV75default_proc (const char *whoami, long code, const char *fmt, va_list args)76{77if (whoami)78fprintf(stderr, "%s: ", whoami);79if (code)80fprintf(stderr, "%s ", error_message(code));81if (fmt)82vfprintf(stderr, fmt, args);83fprintf(stderr, "\r\n"); /* ??? */84}8586static errf com_err_hook = default_proc;8788KRB5_LIB_FUNCTION void KRB5_LIB_CALL89com_err_va (const char *whoami,90long code,91const char *fmt,92va_list args)93{94(*com_err_hook) (whoami, code, fmt, args);95}9697KRB5_LIB_FUNCTION void KRB5_LIB_CALL98com_err (const char *whoami,99long code,100const char *fmt,101...)102{103va_list ap;104va_start(ap, fmt);105com_err_va (whoami, code, fmt, ap);106va_end(ap);107}108109KRB5_LIB_FUNCTION errf KRB5_LIB_CALL110set_com_err_hook (errf new)111{112errf old = com_err_hook;113114if (new)115com_err_hook = new;116else117com_err_hook = default_proc;118119return old;120}121122KRB5_LIB_FUNCTION errf KRB5_LIB_CALL123reset_com_err_hook (void)124{125return set_com_err_hook(NULL);126}127128#define ERRCODE_RANGE 8 /* # of bits to shift table number */129#define BITS_PER_CHAR 6 /* # bits to shift per character in name */130131static const char char_set[] =132"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";133134static char buf[6];135136KRB5_LIB_FUNCTION const char * KRB5_LIB_CALL137error_table_name(int num)138{139int ch;140int i;141char *p;142143/* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */144p = buf;145num >>= ERRCODE_RANGE;146/* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */147num &= 077777777;148/* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */149for (i = 4; i >= 0; i--) {150ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);151if (ch != 0)152*p++ = char_set[ch-1];153}154*p = '\0';155return(buf);156}157158KRB5_LIB_FUNCTION void KRB5_LIB_CALL159add_to_error_table(struct et_list *new_table)160{161struct et_list *et;162163for (et = _et_list; et; et = et->next) {164if (et->table->base == new_table->table->base)165return;166}167168new_table->next = _et_list;169_et_list = new_table;170}171172173