1 /*- 2 * Copyright (c) 2009 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Alistair Crooks (agc@NetBSD.org) 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 /* 30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk) 31 * All rights reserved. 32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted 33 * their moral rights under the UK Copyright Design and Patents Act 1988 to 34 * be recorded as the authors of this copyright work. 35 * 36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 37 * use this file except in compliance with the License. 38 * 39 * You may obtain a copy of the License at 40 * http://www.apache.org/licenses/LICENSE-2.0 41 * 42 * Unless required by applicable law or agreed to in writing, software 43 * distributed under the License is distributed on an "AS IS" BASIS, 44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 45 * 46 * See the License for the specific language governing permissions and 47 * limitations under the License. 48 */ 49 50 /** \file 51 */ 52 53 #ifndef ERRORS_H_ 54 #define ERRORS_H_ 55 56 #include <errno.h> 57 58 /** error codes */ 59 /* Remember to add names to map in errors.c */ 60 typedef enum { 61 PGP_E_OK = 0x0000, /* no error */ 62 PGP_E_FAIL = 0x0001, /* general error */ 63 PGP_E_SYSTEM_ERROR = 0x0002, /* system error, look at errno for 64 * details */ 65 PGP_E_UNIMPLEMENTED = 0x0003, /* feature not yet implemented */ 66 67 /* reader errors */ 68 PGP_E_R = 0x1000, /* general reader error */ 69 PGP_E_R_READ_FAILED = PGP_E_R + 1, 70 PGP_E_R_EARLY_EOF = PGP_E_R + 2, 71 PGP_E_R_BAD_FORMAT = PGP_E_R + 3, /* For example, malformed 72 * armour */ 73 PGP_E_R_UNSUPPORTED = PGP_E_R + 4, 74 PGP_E_R_UNCONSUMED_DATA = PGP_E_R + 5, 75 76 /* writer errors */ 77 PGP_E_W = 0x2000, /* general writer error */ 78 PGP_E_W_WRITE_FAILED = PGP_E_W + 1, 79 PGP_E_W_WRITE_TOO_SHORT = PGP_E_W + 2, 80 81 /* parser errors */ 82 PGP_E_P = 0x3000, /* general parser error */ 83 PGP_E_P_NOT_ENOUGH_DATA = PGP_E_P + 1, 84 PGP_E_P_UNKNOWN_TAG = PGP_E_P + 2, 85 PGP_E_P_PACKET_CONSUMED = PGP_E_P + 3, 86 PGP_E_P_MPI_FORMAT_ERROR = PGP_E_P + 4, 87 PGP_E_P_PACKET_NOT_CONSUMED = PGP_E_P + 5, 88 PGP_E_P_DECOMPRESSION_ERROR = PGP_E_P + 6, 89 PGP_E_P_NO_USERID = PGP_E_P + 7, 90 91 /* creator errors */ 92 PGP_E_C = 0x4000, /* general creator error */ 93 94 /* validation errors */ 95 PGP_E_V = 0x5000, /* general validation error */ 96 PGP_E_V_BAD_SIGNATURE = PGP_E_V + 1, 97 PGP_E_V_NO_SIGNATURE = PGP_E_V + 2, 98 PGP_E_V_UNKNOWN_SIGNER = PGP_E_V + 3, 99 PGP_E_V_BAD_HASH = PGP_E_V + 4, 100 101 /* Algorithm support errors */ 102 PGP_E_ALG = 0x6000, /* general algorithm error */ 103 PGP_E_ALG_UNSUPPORTED_SYMMETRIC_ALG = PGP_E_ALG + 1, 104 PGP_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG = PGP_E_ALG + 2, 105 PGP_E_ALG_UNSUPPORTED_SIGNATURE_ALG = PGP_E_ALG + 3, 106 PGP_E_ALG_UNSUPPORTED_HASH_ALG = PGP_E_ALG + 4, 107 PGP_E_ALG_UNSUPPORTED_COMPRESS_ALG = PGP_E_ALG + 5, 108 109 /* Protocol errors */ 110 PGP_E_PROTO = 0x7000, /* general protocol error */ 111 PGP_E_PROTO_BAD_SYMMETRIC_DECRYPT = PGP_E_PROTO + 2, 112 PGP_E_PROTO_UNKNOWN_SS = PGP_E_PROTO + 3, 113 PGP_E_PROTO_CRITICAL_SS_IGNORED = PGP_E_PROTO + 4, 114 PGP_E_PROTO_BAD_PUBLIC_KEY_VRSN = PGP_E_PROTO + 5, 115 PGP_E_PROTO_BAD_SIGNATURE_VRSN = PGP_E_PROTO + 6, 116 PGP_E_PROTO_BAD_ONE_PASS_SIG_VRSN = PGP_E_PROTO + 7, 117 PGP_E_PROTO_BAD_PKSK_VRSN = PGP_E_PROTO + 8, 118 PGP_E_PROTO_DECRYPTED_MSG_WRONG_LEN = PGP_E_PROTO + 9, 119 PGP_E_PROTO_BAD_SK_CHECKSUM = PGP_E_PROTO + 10 120 } pgp_errcode_t; 121 122 /** one entry in a linked list of errors */ 123 typedef struct pgp_error { 124 pgp_errcode_t errcode; 125 int sys_errno; /* irrelevent unless errcode == 126 * PGP_E_SYSTEM_ERROR */ 127 char *comment; 128 const char *file; 129 int line; 130 struct pgp_error *next; 131 } pgp_error_t; 132 133 const char *pgp_errcode(const pgp_errcode_t); 134 135 void 136 pgp_push_error(pgp_error_t **, pgp_errcode_t, 137 int, 138 const char *, int, const char *,...); 139 void pgp_print_error(pgp_error_t *); 140 void pgp_print_errors(pgp_error_t *); 141 void pgp_free_errors(pgp_error_t *); 142 int pgp_has_error(pgp_error_t *, pgp_errcode_t); 143 144 #define PGP_SYSTEM_ERROR_1(err,code,sys,fmt,arg) do { \ 145 pgp_push_error(err,PGP_E_SYSTEM_ERROR,errno,__FILE__,__LINE__,sys);\ 146 pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg); \ 147 } while(/*CONSTCOND*/0) 148 149 #define PGP_MEMORY_ERROR(err) { \ 150 fprintf(stderr, "Memory error\n"); \ 151 } /* \todo placeholder for better error 152 * handling */ 153 #define PGP_ERROR_1(err,code,fmt,arg) do { \ 154 pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg); \ 155 } while(/*CONSTCOND*/0) 156 #define PGP_ERROR_2(err,code,fmt,arg,arg2) do { \ 157 pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2); \ 158 } while(/*CONSTCOND*/0) 159 #define PGP_ERROR_3(err,code,fmt,arg,arg2,arg3) do { \ 160 pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3); \ 161 } while(/*CONSTCOND*/0) 162 #define PGP_ERROR_4(err,code,fmt,arg,arg2,arg3,arg4) do { \ 163 pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3,arg4); \ 164 } while(/*CONSTCOND*/0) 165 166 #endif /* ERRORS_H_ */ 167