2001-02-27 19:23:30 +00:00
|
|
|
/* drange.c
|
|
|
|
* Routines for providing general range support to the dfilter library
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-27 19:23:30 +00:00
|
|
|
* Copyright (c) 2000 by Ed Warnicke <hagbard@physics.rutgers.edu>
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
2001-02-27 19:23:30 +00:00
|
|
|
* By Gerald Combs
|
|
|
|
* Copyright 1999 Gerald Combs
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-02-27 19:23:30 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
2012-02-02 11:54:33 +00:00
|
|
|
|
2001-02-27 19:23:30 +00:00
|
|
|
#include "drange.h"
|
|
|
|
|
|
|
|
/* drange_node constructor */
|
|
|
|
drange_node*
|
|
|
|
drange_node_new(void)
|
|
|
|
{
|
|
|
|
drange_node* new_range_node;
|
|
|
|
|
2012-06-03 09:26:15 +00:00
|
|
|
new_range_node = g_new(drange_node,1);
|
2001-03-02 17:04:25 +00:00
|
|
|
new_range_node->start_offset = 0;
|
2001-02-27 19:23:30 +00:00
|
|
|
new_range_node->length = 0;
|
2001-03-02 17:04:25 +00:00
|
|
|
new_range_node->end_offset = 0;
|
2010-01-19 19:37:03 +00:00
|
|
|
new_range_node->ending = DRANGE_NODE_END_T_UNINITIALIZED;
|
2001-02-27 19:23:30 +00:00
|
|
|
return new_range_node;
|
|
|
|
}
|
|
|
|
|
2012-06-19 12:12:41 +00:00
|
|
|
static drange_node*
|
|
|
|
drange_node_dup(drange_node *org)
|
|
|
|
{
|
|
|
|
drange_node *new_range_node;
|
|
|
|
|
|
|
|
if (!org)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new_range_node = g_new(drange_node,1);
|
|
|
|
new_range_node->start_offset = org->start_offset;
|
|
|
|
new_range_node->length = org->length;
|
|
|
|
new_range_node->end_offset = org->end_offset;
|
|
|
|
new_range_node->ending = org->ending;
|
|
|
|
return new_range_node;
|
|
|
|
}
|
|
|
|
|
2001-02-27 19:23:30 +00:00
|
|
|
/* drange_node destructor */
|
|
|
|
void
|
|
|
|
drange_node_free(drange_node* drnode)
|
|
|
|
{
|
|
|
|
g_free(drnode);
|
|
|
|
}
|
|
|
|
|
2002-08-28 20:41:00 +00:00
|
|
|
/* drange_node accessors */
|
2001-02-27 19:23:30 +00:00
|
|
|
gint
|
2001-03-02 17:04:25 +00:00
|
|
|
drange_node_get_start_offset(drange_node* drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
g_assert(drnode->ending != DRANGE_NODE_END_T_UNINITIALIZED);
|
2001-03-02 17:04:25 +00:00
|
|
|
return drnode->start_offset;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
drange_node_get_length(drange_node* drnode)
|
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
g_assert(drnode->ending == DRANGE_NODE_END_T_LENGTH);
|
2001-02-27 19:23:30 +00:00
|
|
|
return drnode->length;
|
|
|
|
}
|
|
|
|
|
2001-03-02 17:04:25 +00:00
|
|
|
gint
|
|
|
|
drange_node_get_end_offset(drange_node* drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
g_assert(drnode->ending == DRANGE_NODE_END_T_OFFSET);
|
2001-03-02 17:04:25 +00:00
|
|
|
return drnode->end_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
drange_node_end_t
|
|
|
|
drange_node_get_ending(drange_node* drnode)
|
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
g_assert(drnode->ending != DRANGE_NODE_END_T_UNINITIALIZED);
|
2001-03-02 17:04:25 +00:00
|
|
|
return drnode->ending;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* drange_node mutators */
|
|
|
|
void
|
2001-03-02 17:04:25 +00:00
|
|
|
drange_node_set_start_offset(drange_node* drnode, gint offset)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
2001-03-02 17:04:25 +00:00
|
|
|
drnode->start_offset = offset;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
drange_node_set_length(drange_node* drnode, gint length)
|
|
|
|
{
|
|
|
|
drnode->length = length;
|
2010-01-19 19:37:03 +00:00
|
|
|
drnode->ending = DRANGE_NODE_END_T_LENGTH;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-03-02 17:04:25 +00:00
|
|
|
drange_node_set_end_offset(drange_node* drnode, gint offset)
|
|
|
|
{
|
|
|
|
drnode->end_offset = offset;
|
2010-01-19 19:37:03 +00:00
|
|
|
drnode->ending = DRANGE_NODE_END_T_OFFSET;
|
2001-03-02 17:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
drange_node_set_to_the_end(drange_node* drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
drnode->ending = DRANGE_NODE_END_T_TO_THE_END;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* drange constructor */
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *
|
2001-02-27 19:23:30 +00:00
|
|
|
drange_new(void)
|
|
|
|
{
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t * new_drange;
|
|
|
|
new_drange = g_new(drange_t,1);
|
2001-02-27 19:23:30 +00:00
|
|
|
new_drange->range_list = NULL;
|
|
|
|
new_drange->has_total_length = TRUE;
|
|
|
|
new_drange->total_length = 0;
|
2001-03-02 17:04:25 +00:00
|
|
|
new_drange->min_start_offset = G_MAXINT;
|
|
|
|
new_drange->max_start_offset = G_MININT;
|
2001-02-27 19:23:30 +00:00
|
|
|
return new_drange;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drange_append_wrapper(gpointer data, gpointer user_data)
|
|
|
|
{
|
2012-06-03 09:26:15 +00:00
|
|
|
drange_node *drnode = (drange_node *)data;
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *dr = (drange_t *)user_data;
|
2001-02-27 19:23:30 +00:00
|
|
|
|
|
|
|
drange_append_drange_node(dr, drnode);
|
|
|
|
}
|
|
|
|
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *
|
2001-02-27 19:23:30 +00:00
|
|
|
drange_new_from_list(GSList *list)
|
|
|
|
{
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *new_drange;
|
2001-02-27 19:23:30 +00:00
|
|
|
|
|
|
|
new_drange = drange_new();
|
|
|
|
g_slist_foreach(list, drange_append_wrapper, new_drange);
|
|
|
|
return new_drange;
|
|
|
|
}
|
|
|
|
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *
|
|
|
|
drange_dup(drange_t *org)
|
2012-06-19 12:12:41 +00:00
|
|
|
{
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_t *new_drange;
|
2012-06-19 12:12:41 +00:00
|
|
|
GSList *p;
|
|
|
|
|
|
|
|
if (!org)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new_drange = drange_new();
|
|
|
|
for (p = org->range_list; p; p = p->next) {
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_node *drnode = (drange_node *)p->data;
|
2012-06-19 12:12:41 +00:00
|
|
|
drange_append_drange_node(new_drange, drange_node_dup(drnode));
|
|
|
|
}
|
|
|
|
return new_drange;
|
|
|
|
}
|
|
|
|
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2001-02-27 19:23:30 +00:00
|
|
|
static void
|
2002-03-02 20:51:46 +00:00
|
|
|
drange_node_free_wrapper(gpointer data, gpointer userdata _U_)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drange destructor */
|
|
|
|
void
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_free(drange_t * dr)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
|
|
|
drange_node_free_list(dr->range_list);
|
|
|
|
g_free(dr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call drange_node destructor on all list items */
|
|
|
|
void
|
|
|
|
drange_node_free_list(GSList* list)
|
|
|
|
{
|
|
|
|
g_slist_foreach(list, drange_node_free_wrapper, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drange accessors */
|
2013-03-05 06:16:45 +00:00
|
|
|
gboolean drange_has_total_length(drange_t * dr){ return dr->has_total_length; }
|
|
|
|
gint drange_get_total_length(drange_t * dr) { return dr->total_length; }
|
|
|
|
gint drange_get_min_start_offset(drange_t * dr) { return dr->min_start_offset; }
|
|
|
|
gint drange_get_max_start_offset(drange_t * dr) { return dr->max_start_offset; }
|
2002-08-28 20:41:00 +00:00
|
|
|
|
2001-02-27 19:23:30 +00:00
|
|
|
static void
|
2013-03-05 06:16:45 +00:00
|
|
|
update_drange_with_node(drange_t *dr, drange_node *drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
2010-01-19 19:37:03 +00:00
|
|
|
if(drnode->ending == DRANGE_NODE_END_T_TO_THE_END){
|
2001-02-27 19:23:30 +00:00
|
|
|
dr->has_total_length = FALSE;
|
|
|
|
}
|
|
|
|
else if(dr->has_total_length){
|
|
|
|
dr->total_length += drnode->length;
|
|
|
|
}
|
2001-03-02 17:04:25 +00:00
|
|
|
if(drnode->start_offset < dr->min_start_offset){
|
|
|
|
dr->min_start_offset = drnode->start_offset;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
2001-03-02 17:04:25 +00:00
|
|
|
if(drnode->start_offset > dr->max_start_offset){
|
|
|
|
dr->max_start_offset = drnode->start_offset;
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drange mutators */
|
|
|
|
void
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_prepend_drange_node(drange_t * dr, drange_node* drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
|
|
|
if(drnode != NULL){
|
|
|
|
dr->range_list = g_slist_prepend(dr->range_list,drnode);
|
|
|
|
update_drange_with_node(dr, drnode);
|
2002-08-28 20:41:00 +00:00
|
|
|
}
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_append_drange_node(drange_t * dr, drange_node* drnode)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
|
|
|
if(drnode != NULL){
|
|
|
|
dr->range_list = g_slist_append(dr->range_list,drnode);
|
|
|
|
update_drange_with_node(dr, drnode);
|
2002-08-28 20:41:00 +00:00
|
|
|
}
|
2001-02-27 19:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-03-05 06:16:45 +00:00
|
|
|
drange_foreach_drange_node(drange_t * dr, GFunc func, gpointer funcdata)
|
2001-02-27 19:23:30 +00:00
|
|
|
{
|
|
|
|
g_slist_foreach(dr->range_list,func,funcdata);
|
|
|
|
}
|