xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/errors.h (revision 9ee9e0d7de4c59c936a17df52be682915dc66f43)
1 /*
2  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
3  * All rights reserved.
4  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
5  * their moral rights under the UK Copyright Design and Patents Act 1988 to
6  * be recorded as the authors of this copyright work.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License.
10  *
11  * You may obtain a copy of the License at
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 /** \file
23  */
24 
25 #ifndef OPS_ERRORS
26 #define OPS_ERRORS
27 
28 #include <errno.h>
29 
30 /** error codes */
31 /* Remember to add names to map in errors.c */
32 typedef enum {
33 	OPS_E_OK = 0x0000,	/* no error */
34 	OPS_E_FAIL = 0x0001,	/* general error */
35 	OPS_E_SYSTEM_ERROR = 0x0002,	/* system error, look at errno for
36 					 * details */
37 	OPS_E_UNIMPLEMENTED = 0x0003,	/* feature not yet implemented */
38 
39 	/* reader errors */
40 	OPS_E_R = 0x1000,	/* general reader error */
41 	OPS_E_R_READ_FAILED = OPS_E_R + 1,
42 	OPS_E_R_EARLY_EOF = OPS_E_R + 2,
43 	OPS_E_R_BAD_FORMAT = OPS_E_R + 3,	/* For example, malformed
44 						 * armour */
45 	OPS_E_R_UNSUPPORTED = OPS_E_R + 4,
46 	OPS_E_R_UNCONSUMED_DATA = OPS_E_R + 5,
47 
48 	/* writer errors */
49 	OPS_E_W = 0x2000,	/* general writer error */
50 	OPS_E_W_WRITE_FAILED = OPS_E_W + 1,
51 	OPS_E_W_WRITE_TOO_SHORT = OPS_E_W + 2,
52 
53 	/* parser errors */
54 	OPS_E_P = 0x3000,	/* general parser error */
55 	OPS_E_P_NOT_ENOUGH_DATA = OPS_E_P + 1,
56 	OPS_E_P_UNKNOWN_TAG = OPS_E_P + 2,
57 	OPS_E_P_PACKET_CONSUMED = OPS_E_P + 3,
58 	OPS_E_P_MPI_FORMAT_ERROR = OPS_E_P + 4,
59 	OPS_E_P_PACKET_NOT_CONSUMED = OPS_E_P + 5,
60 	OPS_E_P_DECOMPRESSION_ERROR = OPS_E_P + 6,
61 	OPS_E_P_NO_USERID = OPS_E_P + 7,
62 
63 	/* creator errors */
64 	OPS_E_C = 0x4000,	/* general creator error */
65 
66 	/* validation errors */
67 	OPS_E_V = 0x5000,	/* general validation error */
68 	OPS_E_V_BAD_SIGNATURE = OPS_E_V + 1,
69 	OPS_E_V_NO_SIGNATURE = OPS_E_V + 2,
70 	OPS_E_V_UNKNOWN_SIGNER = OPS_E_V + 3,
71 	OPS_E_V_BAD_HASH = OPS_E_V + 4,
72 
73 	/* Algorithm support errors */
74 	OPS_E_ALG = 0x6000,	/* general algorithm error */
75 	OPS_E_ALG_UNSUPPORTED_SYMMETRIC_ALG = OPS_E_ALG + 1,
76 	OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG = OPS_E_ALG + 2,
77 	OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG = OPS_E_ALG + 3,
78 	OPS_E_ALG_UNSUPPORTED_HASH_ALG = OPS_E_ALG + 4,
79 	OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG = OPS_E_ALG + 5,
80 
81 	/* Protocol errors */
82 	OPS_E_PROTO = 0x7000,	/* general protocol error */
83 	OPS_E_PROTO_BAD_SYMMETRIC_DECRYPT = OPS_E_PROTO + 2,
84 	OPS_E_PROTO_UNKNOWN_SS = OPS_E_PROTO + 3,
85 	OPS_E_PROTO_CRITICAL_SS_IGNORED = OPS_E_PROTO + 4,
86 	OPS_E_PROTO_BAD_PUBLIC_KEY_VRSN = OPS_E_PROTO + 5,
87 	OPS_E_PROTO_BAD_SIGNATURE_VRSN = OPS_E_PROTO + 6,
88 	OPS_E_PROTO_BAD_ONE_PASS_SIG_VRSN = OPS_E_PROTO + 7,
89 	OPS_E_PROTO_BAD_PKSK_VRSN = OPS_E_PROTO + 8,
90 	OPS_E_PROTO_DECRYPTED_MSG_WRONG_LEN = OPS_E_PROTO + 9,
91 	OPS_E_PROTO_BAD_SK_CHECKSUM = OPS_E_PROTO + 10
92 } __ops_errcode_t;
93 
94 /** one entry in a linked list of errors */
95 typedef struct __ops_error {
96 	__ops_errcode_t   errcode;
97 	int             sys_errno;	/* !< irrelevent unless errcode ==
98 					 * OPS_E_SYSTEM_ERROR */
99 	char           *comment;
100 	const char     *file;
101 	int             line;
102 	struct __ops_error *next;
103 }               __ops_error_t;
104 
105 const char     *__ops_errcode(const __ops_errcode_t errcode);
106 
107 void
108 __ops_push_error(__ops_error_t ** errstack, __ops_errcode_t errcode, int sys_errno,
109 	       const char *file, int line, const char *comment,...);
110 void            __ops_print_error(__ops_error_t * err);
111 void            __ops_print_errors(__ops_error_t * errstack);
112 void            __ops_free_errors(__ops_error_t * errstack);
113 int             __ops_has_error(__ops_error_t * errstack, __ops_errcode_t errcode);
114 
115 #define OPS_SYSTEM_ERROR_1(err,code,syscall,fmt,arg)	do {		\
116 	__ops_push_error(err,OPS_E_SYSTEM_ERROR,errno,__FILE__,__LINE__,syscall);\
117 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
118 } while(/*CONSTCOND*/0)
119 #define OPS_MEMORY_ERROR(err) {						\
120 	fprintf(stderr, "Memory error\n");				\
121 }				/* \todo placeholder for better error
122 				 * handling */
123 #define OPS_ERROR(err,code,fmt)	do {					\
124 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt);		\
125 } while(/*CONSTCOND*/0)
126 #define OPS_ERROR_1(err,code,fmt,arg)	do {				\
127 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
128 } while(/*CONSTCOND*/0)
129 #define OPS_ERROR_2(err,code,fmt,arg,arg2)	do {			\
130 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2);	\
131 } while(/*CONSTCOND*/0)
132 #define OPS_ERROR_3(err,code,fmt,arg,arg2,arg3)	do {			\
133 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3);	\
134 } while(/*CONSTCOND*/0)
135 #define OPS_ERROR_4(err,code,fmt,arg,arg2,arg3,arg4)	do {		\
136 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3,arg4); \
137 } while(/*CONSTCOND*/0)
138 
139 #endif				/* OPS_ERRORS */
140