Add a new WTAP_ERR_DECOMPRESS error, and use that for errors discovered
[obnox/wireshark/wip.git] / packet-range.c
1 /* packet-range.c
2  * Packet range routines (save, print, ...)
3  *
4  * $Id$
5  *
6  * Dick Gooris <gooris@lucent.com>
7  * Ulf Lamping <ulf.lamping@web.de>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <glib.h>
37
38 #include <epan/frame_data.h>
39
40 #include "globals.h"
41
42 #include "packet-range.h"
43
44 /* (re-)calculate the packet counts (except the user specified range) */
45 static void packet_range_calc(packet_range_t *range) {
46   guint32       current_count;
47   guint32       mark_low;
48   guint32       mark_high;
49   guint32       displayed_mark_low;
50   guint32       displayed_mark_high;
51   frame_data    *packet;
52
53
54   range->selected_packet        = 0L;
55
56   mark_low                      = 0L;
57   mark_high                     = 0L;
58   range->mark_range_cnt         = 0L;
59   range->ignored_cnt            = 0L;
60   range->ignored_marked_cnt     = 0L;
61   range->ignored_mark_range_cnt = 0L;
62   range->ignored_user_range_cnt = 0L;
63
64   displayed_mark_low            = 0L;
65   displayed_mark_high           = 0L;
66   range->displayed_cnt          = 0L;
67   range->displayed_marked_cnt   = 0L;
68   range->displayed_mark_range_cnt=0L;
69   range->displayed_ignored_cnt            = 0L;
70   range->displayed_ignored_marked_cnt     = 0L;
71   range->displayed_ignored_mark_range_cnt = 0L;
72   range->displayed_ignored_user_range_cnt = 0L;
73
74   /* The next for-loop is used to obtain the amount of packets to be processed
75    * and is used to present the information in the Save/Print As widget.
76    * We have different types of ranges: All the packets, the number
77    * of packets of a marked range, a single packet, and a user specified
78    * packet range. The last one is not calculated here since this
79    * data must be entered in the widget by the user.
80    */
81
82   current_count = 0;
83   for(packet = cfile.plist_start; packet != NULL; packet = packet->next) {
84       current_count++;
85       if (cfile.current_frame == packet) {
86           range->selected_packet = current_count;
87       }
88       if (packet->flags.passed_dfilter) {
89           range->displayed_cnt++;
90       }
91       if (packet->flags.marked) {
92             if (packet->flags.ignored) {
93                 range->ignored_marked_cnt++;
94             }
95             if (packet->flags.passed_dfilter) {
96                 range->displayed_marked_cnt++;
97                 if (packet->flags.ignored) {
98                     range->displayed_ignored_marked_cnt++;
99                 }
100                 if (displayed_mark_low == 0) {
101                    displayed_mark_low = current_count;
102                 }
103                 if (current_count > displayed_mark_high) {
104                    displayed_mark_high = current_count;
105                 }
106             }
107
108             if (mark_low == 0) {
109                mark_low = current_count;
110             }
111             if (current_count > mark_high) {
112                mark_high = current_count;
113             }
114       }
115       if (packet->flags.ignored) {
116           range->ignored_cnt++;
117           if (packet->flags.passed_dfilter) {
118               range->displayed_ignored_cnt++;
119           }
120       }
121   }
122
123   current_count = 0;
124   for(packet = cfile.plist_start; packet != NULL; packet = packet->next) {
125       current_count++;
126
127       if (current_count >= mark_low &&
128           current_count <= mark_high)
129       {
130           range->mark_range_cnt++;
131           if (packet->flags.ignored) {
132               range->ignored_mark_range_cnt++;
133           }
134       }
135
136       if (current_count >= displayed_mark_low &&
137           current_count <= displayed_mark_high)
138       {
139           if (packet->flags.passed_dfilter) {
140             range->displayed_mark_range_cnt++;
141             if (packet->flags.ignored) {
142                 range->displayed_ignored_mark_range_cnt++;
143             }
144           }
145       }
146   }
147
148   /* in case we marked just one packet, we add 1. */
149   /*if (cfile.marked_count != 0) {
150     range->mark_range = mark_high - mark_low + 1;
151   }*/
152
153   /* in case we marked just one packet, we add 1. */
154   /*if (range->displayed_marked_cnt != 0) {
155     range->displayed_mark_range = displayed_mark_high - displayed_mark_low + 1;
156   }*/
157 }
158
159
160 /* (re-)calculate the user specified packet range counts */
161 static void packet_range_calc_user(packet_range_t *range) {
162   guint32       current_count;
163   frame_data    *packet;
164
165   range->user_range_cnt             = 0L;
166   range->ignored_user_range_cnt     = 0L;
167   range->displayed_user_range_cnt   = 0L;
168   range->displayed_ignored_user_range_cnt = 0L;
169
170   current_count = 0;
171   for(packet = cfile.plist_start; packet != NULL; packet = packet->next) {
172       current_count++;
173
174       if (value_is_in_range(range->user_range, current_count)) {
175           range->user_range_cnt++;
176           if (packet->flags.ignored) {
177               range->ignored_user_range_cnt++;
178           }
179           if (packet->flags.passed_dfilter) {
180             range->displayed_user_range_cnt++;
181             if (packet->flags.ignored) {
182                 range->displayed_ignored_user_range_cnt++;
183             }
184           }
185       }
186   }
187 }
188
189
190 /* init the range struct */
191 void packet_range_init(packet_range_t *range) {
192
193   range->process            = range_process_all;
194   range->process_filtered   = FALSE;
195   range->remove_ignored     = FALSE;
196   range->user_range         = range_empty();
197
198   /* calculate all packet range counters */
199   packet_range_calc(range);
200   packet_range_calc_user(range);
201 }
202
203 /* check whether the packet range is OK */
204 convert_ret_t packet_range_check(packet_range_t *range) {
205   if (range->process == range_process_user_range && range->user_range == NULL) {
206     /* Not valid - return the error. */
207     return range->user_range_status;
208   }
209   return CVT_NO_ERROR;
210 }
211
212 /* init the processing run */
213 void packet_range_process_init(packet_range_t *range) {
214   /* Check that, if an explicit range was selected, it's valid. */
215   /* "enumeration" values */
216   range->marked_range_active    = FALSE;
217   range->selected_done          = FALSE;
218
219   if (range->process_filtered == FALSE) {
220     range->marked_range_left = range->mark_range_cnt;
221   } else {
222     range->marked_range_left = range->displayed_mark_range_cnt;
223   }
224 }
225
226 /* do we have to process all packets? */
227 gboolean packet_range_process_all(packet_range_t *range) {
228     return range->process == range_process_all && !range->process_filtered && !range->remove_ignored;
229 }
230
231 /* do we have to process this packet? */
232 range_process_e packet_range_process_packet(packet_range_t *range, frame_data *fdata) {
233
234     if (range->remove_ignored && fdata->flags.ignored) {
235         return range_process_next;
236     }
237
238     switch(range->process) {
239     case(range_process_all):
240         break;
241     case(range_process_selected):
242         if (range->selected_done) {
243           return range_processing_finished;
244         }
245         if (fdata->num != cfile.current_frame->num) {
246           return range_process_next;
247         }
248         range->selected_done = TRUE;
249         break;
250     case(range_process_marked):
251         if (fdata->flags.marked == FALSE) {
252           return range_process_next;
253         }
254         break;
255     case(range_process_marked_range):
256         if (range->marked_range_left == 0) {
257           return range_processing_finished;
258         }
259         if (fdata->flags.marked == TRUE) {
260           range->marked_range_active = TRUE;
261         }
262         if (range->marked_range_active == FALSE ) {
263           return range_process_next;
264         }
265         if (!range->process_filtered ||
266           (range->process_filtered && fdata->flags.passed_dfilter == TRUE))
267         {
268           range->marked_range_left--;
269         }
270         break;
271     case(range_process_user_range):
272         if (value_is_in_range(range->user_range, fdata->num) == FALSE) {
273           return range_process_next;
274         }
275         break;
276     default:
277         g_assert_not_reached();
278     }
279
280     /* this packet has to pass the display filter but didn't? -> try next */
281     if (range->process_filtered && fdata->flags.passed_dfilter == FALSE) {
282         return range_process_next;
283     }
284
285     /* We fell through the conditions above, so we accept this packet */
286     return range_process_this;
287 }
288
289
290 /******************** Range Entry Parser *********************************/
291
292 /* Converts a range string to a user range.
293  * The parameter 'es' points to the string to be converted, and is defined in
294  * the Save/Print-As widget.
295  */
296
297 void packet_range_convert_str(packet_range_t *range, const gchar *es)
298 {
299     range_t *new_range;
300     convert_ret_t ret;
301
302     if (range->user_range != NULL)
303         g_free(range->user_range);
304     ret = range_convert_str(&new_range, es, cfile.count);
305     if (ret != CVT_NO_ERROR) {
306         /* range isn't valid */
307         range->user_range                 = NULL;
308         range->user_range_status          = ret;
309         range->user_range_cnt             = 0L;
310         range->ignored_user_range_cnt     = 0L;
311         range->displayed_user_range_cnt   = 0L;
312         range->displayed_ignored_user_range_cnt = 0L;
313         return;
314     }
315     range->user_range = new_range;
316
317     /* calculate new user specified packet range counts */
318     packet_range_calc_user(range);
319 } /* packet_range_convert_str */