[Openvas-commits] r5764 - in trunk/gsa: . src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Oct 31 00:47:46 CET 2009


Author: jan
Date: 2009-10-31 00:47:45 +0100 (Sat, 31 Oct 2009)
New Revision: 5764

Added:
   trunk/gsa/src/validator.c
Modified:
   trunk/gsa/ChangeLog
Log:
* src/validator.c: New.



Modified: trunk/gsa/ChangeLog
===================================================================
--- trunk/gsa/ChangeLog	2009-10-30 15:47:00 UTC (rev 5763)
+++ trunk/gsa/ChangeLog	2009-10-30 23:47:45 UTC (rev 5764)
@@ -1,3 +1,7 @@
+2009-10-31  Jan-Oliver Wagner <jan-oliver.wagner at greenbone.net>
+
+	* src/validator.c: New.
+
 2009-10-30  Jan-Oliver Wagner <jan-oliver.wagner at greenbone.net>
 
 	* src/html/src/img/download.png, src/html/src/img/bullet2.png,

Added: trunk/gsa/src/validator.c
===================================================================
--- trunk/gsa/src/validator.c	2009-10-30 15:47:00 UTC (rev 5763)
+++ trunk/gsa/src/validator.c	2009-10-30 23:47:45 UTC (rev 5764)
@@ -0,0 +1,135 @@
+/* Greenbone Security Assistant (set for openvas-libraries/base)
+ * $Id$
+ * Description: String validator.
+ *
+ * Authors:
+ * Matthew Mundell <matthew.mundell at intevation.de>
+ * Jan-Oliver Wagner <jan-oliver.wagner at greenbone.net>
+ *
+ * Copyright:
+ * Copyright (C) 2009 Greenbone Networks GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * or, at your option, any later version as published by the Free
+ * Software Foundation
+ *
+ * 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
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "tracef.h"
+#include "validator.h"
+
+#undef G_LOG_DOMAIN
+/**
+ * @brief GLib log domain.
+ */
+#define G_LOG_DOMAIN "gsad vali"
+
+/**
+ * @file validator.c
+ * @brief Validation mechanism.
+ *
+ * Defines a mechanism to validate strings according to named rules.
+ *
+ * \ref openvas_validator_new creates a new validator which must be freed
+ * with \ref openvas_validator_free.  \ref openvas_validator_add adds a regular
+ * expression to a validator as a rule.  \ref openvas_validate checks that a
+ * given string matches a given rule.
+ */
+
+/**
+ * @brief Create a new validator.
+ *
+ * The validator must be freed with \ref openvas_validator_free.
+ *
+ * @return A newly allocated validator.
+ */
+validator_t
+openvas_validator_new ()
+{
+  return g_hash_table_new_full (g_str_hash,
+                                g_str_equal,
+                                g_free,
+                                g_free);
+}
+
+/**
+ * @brief Add or overwrite a validation rule.
+ *
+ * @param  validator  Validator to add rule to.
+ * @param  name       Name of the rule.
+ * @param  regex      Validation rule as a regular expression.
+ */
+void
+openvas_validator_add (validator_t validator,
+                       const char *name,
+                       const char *regex)
+{
+  g_hash_table_insert (validator,
+                       (gpointer) g_strdup (name),
+                       (gpointer) g_strdup (regex));
+}
+
+/**
+ * @brief Validate a string for a given rule.
+ *
+ * @param  validator  Validator to validate from.
+ * @param  name       Name of rule.
+ * @param  value      Value to validate.
+ *
+ * @return 0 if valid \arg value is valid, 1 if failed to find \arg name in
+ *         validator, 2 if value failed to match the regexp.
+ */
+int
+openvas_validate (validator_t validator, const char *name, const char *value)
+{
+  gpointer key, regex;
+
+  if (g_hash_table_lookup_extended (validator, name, &key, &regex))
+    {
+      if (regex == NULL)
+        {
+          if (value == NULL)
+            {
+              tracef ("%s: matched, regex NULL", __FUNCTION__);
+              return 0;
+            }
+          tracef ("%s: failed to match, regex NULL", __FUNCTION__);
+          return 2;
+        }
+
+      tracef ("matching <%s> against <%s>: ", (char *) regex, value);
+      if (g_regex_match_simple ((const gchar *) regex,
+                                (const gchar *) value,
+                                0,
+                                0))
+        {
+          tracef ("%s: matched", __FUNCTION__);
+          return 0;
+        }
+      tracef ("%s: failed to match\n", __FUNCTION__);
+      return 2;
+    }
+
+  tracef ("%s: failed to find name", __FUNCTION__);
+  return 1;
+}
+
+/**
+ * @brief Free a validator.
+ *
+ * @param  validator  Validator.
+ */
+void
+openvas_validator_free (validator_t validator)
+{
+  if (validator) g_hash_table_destroy (validator);
+}



More information about the Openvas-commits mailing list