From Alexis La Goutte:
[obnox/wireshark/wip.git] / epan / exceptions.h
1 /* exceptions.h
2  * Wireshark's exceptions.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __EXCEPTIONS_H__
26 #define __EXCEPTIONS_H__
27
28 #ifndef XCEPT_H
29 #include "except.h"
30 #endif
31
32 /* Wireshark has only one exception group, to make these macros simple */
33 #define XCEPT_GROUP_WIRESHARK 1
34
35 /**
36     Index is out of range.
37     An attempt was made to read past the end of a buffer.
38     This generally means that the capture was done with a "slice"
39     length or "snapshot" length less than the maximum packet size,
40     and a link-layer packet was cut short by that, so not all of the
41     data in the link-layer packet was available.
42 **/
43 #define BoundsError             1
44
45 /**
46     Index is beyond reported length (not cap_len)
47     An attempt was made to read past the logical end of a buffer. This
48     differs from a BoundsError in that the parent protocol established a
49     limit past which this dissector should not process in the buffer and that
50     limit was exceeded.
51     This generally means that the packet is invalid, i.e. whatever
52     code constructed the packet and put it on the wire didn't put enough
53     data into it.  It is therefore currently reported as a "Malformed
54     packet".
55     However, it also happens in some cases where the packet was fragmented
56     and the fragments weren't reassembled.  We need to add another length
57     field to a tvbuff, so that "length of the packet from the link layer"
58     and "length of the packet were it fully reassembled" are different,
59     and going past the first of those without going past the second would
60     throw a different exception, which would be reported as an "Unreassembled
61     packet" rather than a "Malformed packet".
62 **/
63 #define ReportedBoundsError     2
64
65 /**
66     During dfilter parsing
67 **/
68 #define TypeError               3
69
70 /**
71     A bug was detected in a dissector.
72
73     DO NOT throw this with THROW(); that means that no details about
74     the dissector error will be reported.  (Instead, the message will
75     blame you for not providing details.)
76
77     Instead, use the DISSECTOR_ASSERT(), etc. macros in epan/proto.h.
78 **/
79 #define DissectorError          4
80
81 /**
82     Index is out of range.
83     An attempt was made to read past the end of a buffer.
84     This error is specific to SCSI data transfers where for some CDBs
85     it is normal that the data PDU might be short.
86     I.e. ReportLuns initially called with allocation_length=8, just enough
87     to get the "size" of lun list back after which the initiator will
88     reissue the command with an allocation_length that is big enough.
89 **/
90 #define ScsiBoundsError         5
91
92 /**
93     Running out of memory.
94     A dissector tried to allocate memory but that failed.
95 **/
96 #define OutOfMemoryError        6
97
98
99 /* Usage:
100  *
101  * TRY {
102  *      code;
103  * }
104  *
105  * CATCH(exception) {
106  *      code;
107  * }
108  *
109  * CATCH2(exception1, exception2) {
110  *      code;
111  * }
112  *
113  * CATCH_ALL {
114  *      code;
115  * }
116  *
117  * FINALLY {
118  *      code;
119  * }
120  *
121  * ENDTRY;
122  *
123  * ********* Never use 'goto' or 'return' inside the TRY, CATCH, CATCH_ALL,
124  * ********* or FINALLY blocks. Execution must proceed through ENDTRY before
125  * ********* branching out.
126  *
127  * This is really something like:
128  *
129  * {
130  *      caught = FALSE:
131  *      x = setjmp();
132  *      if (x == 0) {
133  *              <TRY code>
134  *      }
135  *      if (!caught && x == 1) {
136  *              caught = TRUE;
137  *              <CATCH(1) code>
138  *      }
139  *      if (!caught && x == 2) {
140  *              caught = TRUE;
141  *              <CATCH(2) code>
142  *      }
143  *      if (!caught && (x == 3 || x == 4)) {
144  *              caught = TRUE;
145  *              <CATCH2(3,4) code>
146  *      }
147  *      if (!caught && x != 0) {
148  *              caught = TRUE;
149  *              <CATCH_ALL code>
150  *      }
151  *      <FINALLY code>
152  *      if(!caught) {
153  *              RETHROW(x)
154  *      }
155  * }<ENDTRY tag>
156  *
157  * All CATCH's must precede a CATCH_ALL.
158  * FINALLY must occur after any CATCH or CATCH_ALL.
159  * ENDTRY marks the end of the TRY code.
160  * TRY and ENDTRY are the mandatory parts of a TRY block.
161  * CATCH, CATCH_ALL, and FINALLY are all optional (although
162  * you'll probably use at least one, otherwise why "TRY"?)
163  *
164  * GET_MESSAGE  returns string ptr to exception message
165  *              when exception is thrown via THROW_MESSAGE()
166  *
167  * To throw/raise an exception.
168  *
169  * THROW(exception)
170  * RETHROW                              rethrow the caught exception
171  *
172  * A cleanup callback is a function called in case an exception occurs
173  * and is not caught. It should be used to free any dynamically-allocated data.
174  * A pop or call_and_pop should occur at the same statement-nesting level
175  * as the push.
176  *
177  * CLEANUP_CB_PUSH(func, data)
178  * CLEANUP_CB_POP
179  * CLEANUP_CB_CALL_AND_POP
180  */
181
182 /* we do up to three passes through the bit of code after except_try_push(),
183  * and except_state is used to keep track of where we are.
184  */
185 #define EXCEPT_CAUGHT   1 /* exception has been caught, no need to rethrow at
186                            * END_TRY */
187
188 #define EXCEPT_RETHROWN 2 /* the exception was rethrown from a CATCH
189                            * block. Don't reenter the CATCH blocks, but do
190                            * execute FINALLY and rethrow at END_TRY */
191
192 #define EXCEPT_FINALLY  4 /* we've entered the FINALLY block - don't allow
193                            * RETHROW, and don't reenter FINALLY if a
194                            * different exception is thrown */
195
196 #define TRY \
197 {\
198         except_t *exc; \
199         volatile int except_state = 0; \
200         static const except_id_t catch_spec[] = { \
201                 { XCEPT_GROUP_WIRESHARK, XCEPT_CODE_ANY } }; \
202         except_try_push(catch_spec, 1, &exc); \
203                                                        \
204         if(except_state & EXCEPT_CAUGHT)               \
205             except_state |= EXCEPT_RETHROWN;           \
206         except_state &= ~EXCEPT_CAUGHT;                \
207                                                        \
208         if (except_state == 0 && exc == 0)             \
209                 /* user's code goes here */
210
211 #define ENDTRY \
212         /* rethrow the exception if necessary */ \
213         if(!(except_state&EXCEPT_CAUGHT) && exc != 0)  \
214             except_rethrow(exc);                 \
215         except_try_pop();\
216 }
217
218 /* the (except_state |= EXCEPT_CAUGHT) in the below is a way of setting
219  * except_state before the user's code, without disrupting the user's code if
220  * it's a one-liner.
221  */
222 #define CATCH(x) \
223         if (except_state == 0 && exc != 0 && exc->except_id.except_code == (x) && \
224             (except_state |= EXCEPT_CAUGHT))                                      \
225                 /* user's code goes here */
226
227 #define CATCH2(x,y) \
228         if (except_state == 0 && exc != 0 && \
229             (exc->except_id.except_code == (x) || exc->except_id.except_code == (y)) && \
230             (except_state|=EXCEPT_CAUGHT))                                             \
231                 /* user's code goes here */
232
233 #define CATCH_ALL \
234         if (except_state == 0 && exc != 0 && \
235             (except_state|=EXCEPT_CAUGHT))                                             \
236                 /* user's code goes here */
237
238
239 #define FINALLY \
240         if( !(except_state & EXCEPT_FINALLY) && (except_state|=EXCEPT_FINALLY)) \
241                 /* user's code goes here */
242
243 #define THROW(x) \
244         except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL)
245
246 #define THROW_ON(cond, x) G_STMT_START { \
247         if ((cond)) \
248                 except_throw(XCEPT_GROUP_WIRESHARK, (x), NULL); \
249 } G_STMT_END
250
251 #define THROW_MESSAGE(x, y) \
252         except_throw(XCEPT_GROUP_WIRESHARK, (x), (y))
253
254 #define GET_MESSAGE                     except_message(exc)
255
256 #define RETHROW                                     \
257     {                                               \
258         /* check we're in a catch block */          \
259         g_assert(except_state == EXCEPT_CAUGHT);    \
260         /* we can't use except_rethrow here, as that pops a catch block \
261          * off the stack, and we don't want to do that, because we want to \
262          * excecute the FINALLY {} block first.     \
263          * except_throw doesn't provide an interface to rethrow an existing \
264          * exception; however, longjmping back to except_try_push() has the \
265          * desired effect.                          \
266          *                                          \
267          * Note also that THROW and RETHROW should provide much the same \
268          * functionality in terms of which blocks to enter, so any messing \
269          * about with except_state in here would indicate that THROW is \
270          * doing the wrong thing.                   \
271          */                                         \
272         longjmp(except_ch.except_jmp,1);            \
273     }
274
275 #define EXCEPT_CODE                     except_code(exc)
276
277 /* Register cleanup functions in case an exception is thrown and not caught.
278  * From the Kazlib documentation, with modifications for use with the
279  * Wireshark-specific macros:
280  *
281  * CLEANUP_PUSH(func, arg)
282  *
283  *  The call to CLEANUP_PUSH shall be matched with a call to
284  *  CLEANUP_CALL_AND_POP or CLEANUP_POP which must occur in the same
285  *  statement block at the same level of nesting. This requirement allows
286  *  an implementation to provide a CLEANUP_PUSH macro which opens up a
287  *  statement block and a CLEANUP_POP which closes the statement block.
288  *  The space for the registered pointers can then be efficiently
289  *  allocated from automatic storage.
290  *
291  *  The CLEANUP_PUSH macro registers a cleanup handler that will be
292  *  called if an exception subsequently occurs before the matching
293  *  CLEANUP_[CALL_AND_]POP is executed, and is not intercepted and
294  *  handled by a try-catch region that is nested between the two.
295  *
296  *  The first argument to CLEANUP_PUSH is a pointer to the cleanup
297  *  handler, a function that returns nothing and takes a single
298  *  argument of type void*. The second argument is a void* value that
299  *  is registered along with the handler.  This value is what is passed
300  *  to the registered handler, should it be called.
301  *
302  *  Cleanup handlers are called in the reverse order of their nesting:
303  *  inner handlers are called before outer handlers.
304  *
305  *  The program shall not leave the cleanup region between
306  *  the call to the macro CLEANUP_PUSH and the matching call to
307  *  CLEANUP_[CALL_AND_]POP by means other than throwing an exception,
308  *  or calling CLEANUP_[CALL_AND_]POP.
309  *
310  *  Within the call to the cleanup handler, it is possible that new
311  *  exceptions may happen.  Such exceptions must be handled before the
312  *  cleanup handler terminates. If the call to the cleanup handler is
313  *  terminated by an exception, the behavior is undefined. The exception
314  *  which triggered the cleanup is not yet caught; thus the program
315  *  would be effectively trying to replace an exception with one that
316  *  isn't in a well-defined state.
317  *
318  *
319  * CLEANUP_POP and CLEANUP_CALL_AND_POP
320  *
321  *  A call to the CLEANUP_POP or CLEANUP_CALL_AND_POP macro shall match
322  *  each call to CLEANUP_PUSH which shall be in the same statement block
323  *  at the same nesting level.  It shall match the most recent such a
324  *  call that is not matched by a previous CLEANUP_[CALL_AND_]POP at
325  *  the same level.
326  *
327  *  These macros causes the registered cleanup handler to be removed. If
328  *  CLEANUP_CALL_AND_POP is called, the cleanup handler is called.
329  *  In that case, the registered context pointer is passed to the cleanup
330  *  handler. If CLEANUP_POP is called, the cleanup handler is not called.
331  *
332  *  The program shall not leave the region between the call to the
333  *  macro CLEANUP_PUSH and the matching call to CLEANUP_[CALL_AND_]POP
334  *  other than by throwing an exception, or by executing the
335  *  CLEANUP_CALL_AND_POP.
336  *
337  */
338
339
340 #define CLEANUP_PUSH(f,a)               except_cleanup_push((f),(a))
341 #define CLEANUP_POP                     except_cleanup_pop(0)
342 #define CLEANUP_CALL_AND_POP            except_cleanup_pop(1)
343
344 #endif /* __EXCEPTIONS_H__ */