Index: trunk/src/sh_database.c
===================================================================
--- trunk/src/sh_database.c	(revision 68)
+++ trunk/src/sh_database.c	(revision 76)
@@ -1197,29 +1197,25 @@
   long len;
 
-  if (!end    || !val   || !size)
-    return end;
-
-  if (val[0] == '\0')
-    {
-      return end;
-    }
-  else
-    {
-      if (*size > 1)
+  if (!((end == NULL) || (val == NULL) || (size == NULL)))
+    {
+      if (val[0] != '\0')
 	{
-	  *end = ','; ++end; (*size) -= 1;
-	  if (flag == 1) { *end = '\''; ++end; (*size) -= 1; }
-	  *end = '\0';
+	  if (*size > 1)
+	    {
+	      *end = ','; ++end; (*size) -= 1;
+	      if (flag == 1) { *end = '\''; ++end; (*size) -= 1; }
+	      *end = '\0';
+	    }
+	  len = (long) strlen(val);
+	  if ((long) *size > (len+1))
+	    {
+	      (void) sl_strlcat(end, val, (size_t) *size);
+	      end   += len; (*size) -= len;
+	      if (flag == 1) { *end = '\''; ++end;  (*size) -= 1; }
+	      *end = '\0'; 
+	    }
 	}
-      len = (long) strlen(val);
-      if ((long) *size > (len+1))
-	{
-	  (void) sl_strlcat(end, val, (size_t) *size);
-	  end   += len; (*size) -= len;
-	  if (flag == 1) { *end = '\''; ++end;  (*size) -= 1; }
-	  *end = '\0'; 
-	}
-    }
-
+    }
+  
   return end;
 }
@@ -1250,4 +1246,7 @@
   char md5out[33];
   int  cnt;
+
+  size_t len_val;
+  size_t len_col;
 
   SL_ENTER(_("sh_database_entry"));
@@ -1331,8 +1330,11 @@
   /*@+type@*/
 
-  size   =  (int) (SH_QUERY_MAX - strlen(values));
-  end    =  values + strlen(values);
-  c_size =  1023   - (int) strlen(columns); /* sizeof(colums) == 1024 */
-  c_end  =  columns + strlen(columns);
+  len_val = strlen(values);
+  size    =  (int) (SH_QUERY_MAX - len_val);
+  end     =  values + len_val;
+
+  len_col = strlen(columns);
+  c_size  =  1023   - (int) len_col; /* sizeof(colums) == 1024 */
+  c_end   =  columns + len_col;
 
   i = 4;
@@ -1354,16 +1356,22 @@
 	  if (p != end)
 	    {
-	      /* 
-	       * 'host' is a reserved word in SQL
-	       */
-	      if (attr_tab[i].val == SH_SLOT_HOST)
-		c_end = null_or_val (c_end, _("fromhost"), &c_size,0);
-	      /* 
-	       * 'group' is a reserved word in SQL
-	       */
-	      else if (attr_tab[i].val == SH_SLOT_GROUP)
-		c_end = null_or_val (c_end, _("grp"), &c_size,0);
+	      if ((attr_tab[i].val != SH_SLOT_HOST) &&
+		  (attr_tab[i].val != SH_SLOT_GROUP))
+		{
+		  c_end = null_or_val (c_end, attr_tab[i].attr, &c_size,0);
+		}
 	      else
-		c_end = null_or_val (c_end, attr_tab[i].attr, &c_size,0);
+		{
+		  /* 
+		   * 'host' is a reserved word in SQL
+		   */
+		  if (attr_tab[i].val == SH_SLOT_HOST)
+		    c_end = null_or_val (c_end, _("fromhost"), &c_size,0);
+		  /* 
+		   * 'group' is a reserved word in SQL
+		   */
+		  else /* if (attr_tab[i].val == SH_SLOT_GROUP) */
+		    c_end = null_or_val (c_end, _("grp"), &c_size,0);
+		}
 	    }
 	  /*@-type@*//* byte* versus char[..] */
@@ -1523,27 +1531,37 @@
   unsigned char * p = (unsigned char *) p_in;
 
-  while (*p != '\0') 
-    {
-      if (*p == '\\')
+  if (*p != '\0')
+    {
+      do 
 	{
-	  escp = (escp == 1) ? 0 : 1;
-	}
-      else if ((*p == '\'' || *p == '\"') && escp == 0)
-	{
-	  retv = S_FALSE;
-	}
-      else if (*p > 0x7F)
-	{
-	  retv = S_FALSE;
-	}
-      else 
-	{
-	  escp = 0;
-	}
-      ++p;
-    }
-  if (escp == 1) 
-    retv = S_FALSE;
-  return retv;
+	  if (*p <=  0x7F)
+	    {
+	      if (escp == 0)
+		{
+		  if      (!((*p == '\'') || (*p == '\"') || (*p != '\\'))) 
+		    /* do nothing */;
+		  else if (*p == '\\') escp = 1;
+		  else    retv = S_FALSE; /* (*p == '\'' || *p == '\"') */
+		}
+	      else /* escp == 1 */
+		{
+		  escp = 0;
+		}
+	    }
+	  else /* *p > 0x7F */
+	    {
+	      retv = S_FALSE;
+	    }
+	  
+	  ++p;
+	  
+	} 
+      while (*p != '\0');
+    }
+
+  if (escp == 0)
+    return retv;
+  else
+    return S_FALSE;
 }
 
@@ -1580,5 +1598,5 @@
     SL_RETURN (NULL, _("sh_database_parse"));
 
-  while ((p != NULL) && (*p != '\0') && (*p != '>'))
+  while ((*p != '\0') && (*p != '>'))
     {
       if (p[0] == 'l' && p[1] == 'o' && p[2] == 'g' &&
@@ -1631,9 +1649,11 @@
   /* non-whitespace 
    */
-  i = 0;
   for (i=0; i < 64; ++i)
     {
-      key_str[i] = p[i];
-      if (p[i] == '=')
+      if (p[i] != '=')
+	{
+	  key_str[i] = p[i];
+	}
+      else
 	{
 	  key_str[i] = '\0';
@@ -1653,13 +1673,44 @@
 	{
 	  q = strchr(&p[j+2], '"');
-	  if (!q)
-	    {
-	      SL_RETURN(NULL, _("sh_database_parse"));
-	    }
-	  else
+	  if (q)
 	    {
 	      *q = '\0';
 
-	      if (S_FALSE == is_escaped(&p[j+2])) {
+	      if (S_TRUE == is_escaped(&p[j+2])) {
+
+		if      (res->val == 1)
+		  (void) sl_strlcpy(db_entry->sev, &p[j+2], 
+				    (size_t)res->size);
+		else if (res->val == 2)
+		  {
+		    z = strchr(&p[j+2], 'T');
+		    if (z) *z = ' ';
+		    (void) sl_strlcpy(db_entry->time, &p[j+2],  20);
+		  }
+		else if (res->val == 3)
+		  (void) sl_strlcpy(db_entry->host, &p[j+2], 
+				    (size_t) res->size);
+		else if (res->val == 4)
+		  (void) sl_strlcpy(db_entry->msg,  &p[j+2], 
+				    (size_t) res->size);
+		else if (res->size != 0)
+		  {
+		    (void) sl_strlcpy( (((char *)(db_entry))+ res->off),
+				       &p[j+2], 
+				       (size_t) res->size);
+		  }
+		else if (res->val >= START_SEC_LONGS)
+		  {
+		    db_entry->long_data[res->val-START_SEC_LONGS]
+		      = atol(&p[j+2]);
+		  }
+
+		*q = '"';
+		p  = q; 
+		++p;
+
+		goto parse;
+	      }
+	      else { /* S_FALSE == is_escaped(&p[j+2]) */
 		sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
 				_("Message not properly escaped"), 
@@ -1667,37 +1718,8 @@
 		SL_RETURN(NULL, _("sh_database_parse"));
 	      }
-
-	      if      (res->val == 1)
-		(void) sl_strlcpy(db_entry->sev, &p[j+2], 
-				  (size_t)res->size);
-	      else if (res->val == 2)
-		{
-		  z = strchr(&p[j+2], 'T');
-		  if (z) *z = ' ';
-		  (void) sl_strlcpy(db_entry->time, &p[j+2],  20);
-		}
-	      else if (res->val == 3)
-		(void) sl_strlcpy(db_entry->host, &p[j+2], 
-				  (size_t) res->size);
-	      else if (res->val == 4)
-		(void) sl_strlcpy(db_entry->msg,  &p[j+2], 
-				  (size_t) res->size);
-	      else if (res->size != 0)
-		{
-		  (void) sl_strlcpy( (((char *)(db_entry))+ res->off),
-				     &p[j+2], 
-				     (size_t) res->size);
-		}
-	      else if (res->val >= START_SEC_LONGS)
-		{
-		  db_entry->long_data[res->val-START_SEC_LONGS]
-		    = atol(&p[j+2]);
-		}
-
-	      *q = '"';
-	      p  = q; 
-	      ++p;
-
-	      goto parse;
+	    }
+	  else /* q == NULL */
+	    {
+	      SL_RETURN(NULL, _("sh_database_parse"));
 	    }
 	}
