[Openvas-commits] r3189 - in trunk/openvas-client: . nessus

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Apr 24 14:59:07 CEST 2009


Author: felix
Date: 2009-04-24 14:59:07 +0200 (Fri, 24 Apr 2009)
New Revision: 3189

Modified:
   trunk/openvas-client/ChangeLog
   trunk/openvas-client/nessus/data_mining.c
   trunk/openvas-client/nessus/data_mining.h
Log:
Removed double code, added include ('new' subset module).

* nessus/data_minig.c, nessus/data_mining.h: Removed code that is now
in subset module, minor reformatting, added include.


Modified: trunk/openvas-client/ChangeLog
===================================================================
--- trunk/openvas-client/ChangeLog	2009-04-24 12:55:04 UTC (rev 3188)
+++ trunk/openvas-client/ChangeLog	2009-04-24 12:59:07 UTC (rev 3189)
@@ -1,5 +1,12 @@
 2009-04-22  Felix Wolfsteller <felix.wolfsteller at intevation.de>
 
+	Removed double code, added include ('new' subset module).
+
+	* nessus/data_minig.c, nessus/data_mining.h: Removed code that is now
+	in subset module, minor reformatting, added include.
+
+2009-04-22  Felix Wolfsteller <felix.wolfsteller at intevation.de>
+
 	Extracted subset module.
 	
 	* nessus/subset.h, nessus/subset.c: New module. Code from data_mining.

Modified: trunk/openvas-client/nessus/data_mining.c
===================================================================
--- trunk/openvas-client/nessus/data_mining.c	2009-04-24 12:55:04 UTC (rev 3188)
+++ trunk/openvas-client/nessus/data_mining.c	2009-04-24 12:59:07 UTC (rev 3189)
@@ -73,6 +73,7 @@
  * 
  * @section subsetmanagement Subset Management Interface
  *
+ * @ref subset
  * A subset contains the result of a query. It is made up of rows
  * and fields (that we call values).
  * To go from the current row to the next one, use the function
@@ -88,8 +89,8 @@
 #include "context.h"
 #include "data_mining.h"
 #include "severity_filter.h"
+#include "subset.h"
 
-
 #ifndef MIN
 #define MIN(x,y) ((x<y)?(x):(y))
 #endif
@@ -170,11 +171,10 @@
    bufsz = 1024 * 1024;
    buf   = emalloc ( bufsz );
   }
-  
-  
+
   if(backends[be].fields)
-  	return 0;
-	
+    return 0;
+
   backends[be].fields = emalloc(BE_NUM_FIELDS * sizeof(struct hfield*));
   for ( i = 0 ; i < BE_NUM_FIELDS ; i ++ )
   {
@@ -262,7 +262,7 @@
     f = f->next;
    }
   }
- } 
+ }
  return 0;
 }
 
@@ -314,15 +314,14 @@
 // GLib >= 2.8: GMappedFile
 #ifdef HAVE_MMAP
 static int
-mmap_read_line_n (be, buf, size, n)
- int be;
- char * buf;
- size_t size;
- int n;
+mmap_read_line_n (int be, char * buf, size_t size, int n)
 {
- char* sol, * eol;
- if ( size <= 0 )
-	return -1;
+ char* sol;
+ char* eol;
+
+ if (size <= 0)
+  return -1;
+
  size --;
  sol = backends[be].lines[n];
  if(!sol)
@@ -424,10 +423,10 @@
   * reset the position where needed. */
  {
  int tot = 1;
- bzero(buf, size);
+ bzero (buf, size);
 
 
- if(read(backends[be].fd, buf, 1) <= 0)
+ if (read(backends[be].fd, buf, 1) <= 0)
   return -1;
  while(buf[0] != '\n')
  {
@@ -442,335 +441,6 @@
  }
 }
 
-
-/*-----------------------------------------------------------------*
- 		    SUBSET MANAGEMENT INTERFACE
- *-----------------------------------------------------------------*
-
-  A subset contains the result of a query. It is made up of rows
-  and fields (that we call values).
-  To go from the current row to the next one, use the function
-  subset_next(). To extract value of the values (fields),
-  use subset_nth_value(). subset_value() is an alias for
-  subset_nth_value(subset, 0), ie: it returns the first field
-  (the only one which can not be NULL).
- 
- 
--------------------------------------------------------------------*/
-
-struct subset *
-subset_next (struct subset * subset)
-{
- return subset->next;
-}
-
-char *
-subset_nth_value (struct subset * subset, int n)
-{
- if(n >= subset->num_fields)
-  return NULL;
- return subset->data[n];
-}
-
-char *
-subset_value (struct subset * subset)
-{
- return subset_nth_value(subset, 0);
-}
-
-
-
-/**
- * @brief Prepends a new subset to a given one and adds the value \<value\>
- * @brief to it (to the new one).
- * 
- * @return New subset (whose next is the given subset).
- */
-static struct subset *
-subset_add (struct subset * subset, char * value)
-{
- struct subset * ret;
-
- ret = emalloc(sizeof(*ret));
- ret->next = subset;
- ret->data = emalloc(sizeof(char*)*2);
- ret->num_fields = 1;
- ret->data[0] = rmslashes(value);
- ret->data[1] = NULL;
- return ret;
-}
-
-
-/**
- * @brief Add another value (field) in the same row.
- * 
- * In contrast to subset_add, does not add a new row (if subset != NULL).
- * 
- * @param[in,out] subset The subset to add a value to (will be returned). Can be
- *                        NULL, then a new subset will be created.
- * 
- * @return Param subset with value added, or new one if subset == NULL.
- */
-static struct subset *
-subset_add_again (struct subset * subset, char * value)
-{
- struct subset * ret = subset;
- if(!subset)
-  return subset_add(subset, value);
- ret->data = realloc(ret->data, (ret->num_fields+1)*sizeof(char*));
- ret->data[ret->num_fields] = rmslashes(value);
- ret->num_fields++;
- return ret;
-}
-
-
-/*-------------------------------------------------------------------------*
-				Subset sorting
- --------------------------------------------------------------------------*/
-
-/**
- * @brief (Merge-)Sort a subset list.
- *
- * @param list      The subset we want to sort.
- * @param n         Starting number of fields to sort.
- * @param m         Last number of the fields of the subset to sort.
- * @param cmp       Array of comparison functions. Each field can have its
- *                  own comparison function.
- *
- * @return Sorted subset.
- */
-static struct subset *
-merge_sort (struct subset * list, int n, int m, cmp_func_t* cmp)
-{
- struct subset * p, * q, * e, *tail;
- int insize = 1, nmerges, i;
- register int psize = 0, qsize = 0;
-
- for(;;)
- {
-  p = list;
-  list = tail = NULL;
-  nmerges = 0;
-
-  while  (p)
-  {
-   nmerges++; 
-   q = p;
-   for(i=0;i<insize;i++)
-    {
-      psize++;
-      q = q->next;
-      if(!q)break;
-    }
-
-   qsize = insize;
-   while((psize > 0) || ((qsize > 0) && q))
-    {
-      if(psize == 0)
-        {
-          e = q;
-          q = q->next;
-          qsize--;
-        }
-      else if(qsize == 0 || !q)
-        {
-          e = p;
-          p = p->next;
-          psize--;
-        }
-      else
-        {
-          int k;
-          int p_smaller = 0;
-
-          for (k=n; (k<=m) && (p_smaller==0); k++)
-            {
-              char * a = subset_nth_value (p, k);
-              char * b = subset_nth_value (q, k);
-              p_smaller = cmp[k-n] (a, b);
-            }
-
-          if (p_smaller >= 0)
-            {
-              e = p; p = p->next;psize--;
-            }
-          else
-            {
-              e = q; q = q->next;
-              qsize --;
-            }
-        }
-
-      if(tail)
-        {
-          tail->next = e;
-        }
-      else
-        {
-          list = e;
-        }
-      tail = e;
-    } /* while((psize > 0) || ((qsize > 0) && q)) */
-   p = q;
-  }
-
-   if (tail) tail->next = NULL;
-   if (nmerges <= 1)
-    {
-      return list;
-    }
-   insize *= 2;
-  }
-}
-
-
-/**
- * @brief (Merge-)Sort a subset list.
- *
- * @param subset      The subset we want to sort.
- * @param field_start Starting number of fields to sort.
- * @param field_end   Last number of the fields of the subset to sort.
- * @param cmp         Array of comparison functions. Each field can have its
- *                    own comparison function.
- *
- * @return Sorted subset.
- */
-struct subset*
-subset_sort (struct subset * subset, int field_start, int field_end,
-             cmp_func_t* cmp)
-{
- return merge_sort (subset, field_start, field_end, cmp);
-}
-
-
-/*------------------------------------------------------------------------*
- * Other subset-related utilities					  *
- *------------------------------------------------------------------------*/
- 
-/**
- * @brief Act as the uniq(1) unix utility -> two entries with the same
- * @brief fields are removed.
- *
- * This function compares the \<n\> first fields.
- * (hence, subset_uniq(s, 0) will remove all duplicates in a list).
- */
-struct subset *
-subset_uniq (struct subset * subset, int n)
-{
- struct subset * s = subset;
- if(!s)
-  return NULL;
-
- while(subset->next)
- {
-  int i;
-  int removed = 0;
-  for(i=0;i<=n;i++)
-  {
-  if(subset->data[i]       &&
-     subset->next->data[i] &&
-     !strcmp(subset->data[i], subset->next->data[i])
-    )
-   {
-   struct subset * old = subset->next;
-   subset->next = subset->next->next;
-   old->next = NULL;
-   subset_free(old);
-   removed++;
-   break;
-   }
-  }
-
-  if(!removed)subset = subset->next;
-  }
-  return s;
-}
-
-
-
-/**
- * @brief Tells us if the value \<value\> is already in the \<nth\> field of
- * @brief the current row.
- */
-static char *
-subset_in_nth (struct subset * subset, char * value, int n)
-{
- while(subset)
- {
-  if(subset->data[n] && !strcmp(subset->data[n], value))
-   return subset->data[n];
- subset = subset->next;
- }
- return NULL;
-}
-
-
-/**
- * @brief An alias for the above function, for subset_in_nth (0).
- */
-char *
-subset_in (struct subset * subset, char * value)
-{
- return subset_in_nth(subset, value, 0);
-}
-
-
-/**
- * @brief Frees a subset and associate fields from memory.
- */
-void
-subset_free (struct subset * subset)
-{
- while(subset)
- {
-  struct subset * next = subset->next;
-  while(subset->num_fields > 0)
-  {
-   efree(&subset->data[subset->num_fields-1]);
-   subset->num_fields --;
-  }
-  efree(&subset->data);
-  efree(&subset);
-  subset = next;
- }
-}
-
-
-/**
- * @brief Dumps the content of a subset on screen (for debugging purposes only).
- */
-int
-subset_dump (struct subset * subset)
-{
- int i;
- if(!subset)
-  return(0);
- 
- for(i=0;i<subset->num_fields;i++)
- {
- printf("%s,", subset->data[i]);
- }
- printf("\n");
- return subset_dump(subset->next);
-}
-
-
-/**
- * @brief Returns the number of items ("rows") in a subset.
- */
-int
-subset_size (struct subset * subset)
-{
-  int sz = 0;
-  while (subset)
-    {
-      sz++;
-      subset = subset->next;
-    }
-  return sz;
-}
-
-
 /*---------------------------------------------------------------------*
  * Data mining functions                                               *
  *---------------------------------------------------------------------*/
@@ -1542,6 +1212,7 @@
       ret = subset_add (ret, hostname);
       ret->id_line = curr_id_line;
 
+      // NULL guard these!
       ret = subset_add_again (ret, port);
       ret = subset_add_again (ret, plugin_id);
       ret = subset_add_again (ret, severity);

Modified: trunk/openvas-client/nessus/data_mining.h
===================================================================
--- trunk/openvas-client/nessus/data_mining.h	2009-04-24 12:55:04 UTC (rev 3188)
+++ trunk/openvas-client/nessus/data_mining.h	2009-04-24 12:59:07 UTC (rev 3189)
@@ -33,39 +33,12 @@
 #define __DATA_MINING_H__
 
 #include <glib.h>
+#include "subset.h"
 
-/*---------------------------------------------------------------*
- * Subset management  						 *
- *---------------------------------------------------------------*/
-
-/**
- * @brief Result ("rows") of a query as list.
- */
-struct subset
-{
-   struct subset * next;
-   int num_fields;
-   char ** data;
-   unsigned int id_line; /**< Workaround to a primary key field, hold line number in nbe file for this row. */
-};
-
 /* Comparison function for subset_sort() */
-typedef int(*cmp_func_t)(char*, char*);
+//typedef int(*cmp_func_t)(char*, char*);
 
 
-
-
-struct subset * subset_next(struct subset*);
-char * subset_value(struct subset*);
-char * subset_nth_value(struct subset *, int);
-int subset_size(struct subset*);
-void subset_free(struct subset *);
-
-
-
-struct subset * subset_sort(struct subset *, int, int, cmp_func_t*);
-struct subset * subset_uniq(struct subset * , int);
-
 struct subset * report_query_all_by_line    (int be, GSList* line_nr_list);
 struct subset * report_query_single_by_line (int be, unsigned int line_nr);
 



More information about the Openvas-commits mailing list