Index: trunk/src/sh_utils.c
===================================================================
--- trunk/src/sh_utils.c	(revision 584)
+++ trunk/src/sh_utils.c	(revision 1)
@@ -27,8 +27,15 @@
 #include <unistd.h>
 
+#if TIME_WITH_SYS_TIME
+#include <sys/time.h>
+#include <time.h>
+#else
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>
-#endif
+#else
 #include <time.h>
+#endif
+#endif
+
 
 #include "samhain.h"
@@ -38,5 +45,4 @@
 #include "sh_tiger.h"
 #include "sh_entropy.h"
-#include "sh_pthread.h"
 
 #undef  FIL__
@@ -45,5 +51,5 @@
 UINT32 ErrFlag[2];
 
-int sh_util_flagval(const char * c, int * fval)
+int sh_util_flagval(char * c, int * fval)
 {
   SL_ENTER(_("sh_util_flagval"));
@@ -95,45 +101,15 @@
 static int sh_ask_update = S_FALSE;
 
-int sh_util_set_interactive(const char * str)
-{
-  (void) str;
-
-  sh_ask_update = S_TRUE;
+int sh_util_set_interactive(char * str)
+{
+  if (str == NULL)
+    sh_ask_update = S_TRUE;
+  else
+    sh_ask_update = S_TRUE;
+
   sh_unix_setnodeamon(NULL);
 
   return 0;
 }
-
-static char * sh_update_file = NULL;
-
-int sh_util_update_file (const char * str)
-{
-  if (str)
-    {
-      if (0 == access(str, R_OK)) /* flawfinder: ignore */
-	{
-	  if (NULL != sh_update_file)
-	    SH_FREE(sh_update_file);
-	  sh_update_file = sh_util_strdup(str);
-	  sh_ask_update = S_TRUE;
-	  sh_unix_setnodeamon(NULL);
-	  return 0;
-	}
-      else
-	{
-	  char ebuf[SH_ERRBUF_SIZE];
-	  int  errnum = errno;
-
-	  sh_error_message(errnum, ebuf, sizeof(ebuf));
-	  sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
-			   ebuf, _("sh_util_update_file") );
-	  
-	  return -1;
-	}
-    }
-
-  return -1;
-}
-
 
 #if !defined(STDIN_FILENO)
@@ -144,54 +120,5 @@
 #endif
 
-/* Returns S_FALSE if no update desired 
- */
-int sh_util_update_checkfile(const char * path)
-{
-  FILE * fd = fopen(sh_update_file, "r");
-  char * line;
-
-  if (!fd)
-    {
-      uid_t  euid;
-      int errnum = errno;
-      sl_get_euid(&euid);
-      sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_NOACCESS,
-		       (long) euid, sh_update_file);
-      aud_exit (FIL__, __LINE__, EXIT_FAILURE);
-      return S_FALSE;
-    }
-
-  line = SH_ALLOC(8192);
-
-  while (NULL != fgets(line, 8192, fd))
-    {
-      char * nl = strrchr(line, '\n');
-
-      if (nl)
-	{
-	  *nl = '\0';
-
-	  /* Check for MS Windows line terminator 
-	   */
-	  if (nl > line) --nl;
-	  if (*nl == '\r')
-	    *nl = '\0';
-	}
-
-      if (0 == sl_strcmp(line, path))
-	{
-	  SH_FREE(line);
-	  fclose(fd);
-	  return S_TRUE;
-	}
-    }
-  SH_FREE(line);
-  fclose(fd);
-  return S_FALSE;
-}
-
-/* Returns S_FALSE if no update desired 
- */
-int sh_util_ask_update(const char * path)
+int sh_util_ask_update(char * path)
 {
   int    inchar, c;
@@ -206,10 +133,4 @@
     }
 
-  if (sh_update_file)
-    {
-      i = sh_util_update_checkfile(path);
-      SL_RETURN(i, _("sh_util_ask_update"));
-    }
-
 #ifdef HAVE_TTYNAME
   if (!ttyname(STDIN_FILENO))
@@ -217,5 +138,4 @@
       if (NULL != ttyname(STDERR_FILENO))
         {
-	  /* cppcheck-suppress leakReturnValNotUsed */
           if (NULL == freopen(ttyname(STDERR_FILENO), "r", stdin))
             {
@@ -261,5 +181,5 @@
 	  else
 	    {
-	      fprintf(stderr, "%s", _("Please answer y(es) or n(o)\n"));
+	      fprintf(stderr, _("Please answer y(es) or n(o)\n"));
 	    }
 	  /*@-charintliteral@*/
@@ -270,5 +190,5 @@
 }
 
-int sh_util_hidesetup(const char * c)
+int sh_util_hidesetup(char * c)
 {
   int i;
@@ -278,98 +198,5 @@
   SL_RETURN(i, _("sh_util_hidesetup"));
 }
-
-char * sh_util_acl_compact(char * buf, ssize_t len)
-{
-  unsigned char  * p = (unsigned char *) buf;
-  int       state = 0;
-  ssize_t   rem = 0;
-  char    * out;
-  
-  SH_VALIDATE_NE(buf, NULL);
-  SH_VALIDATE_GE(len, 0);
-
-  out = SH_ALLOC(len + 1);
-
-  while (*p != '\0')  {
-
-    /* -- not at start or after newline
-     */
-    if (state == 1) {
-      if (*p == '\n' || *p == ' ' || *p == '\t' || *p == '#') {
-	while (*p != '\n') {
-	  ++p;
-	  if (*p == '\0') {
-	    goto exit_it;
-	  }
-	}
-	out[rem] = ','; ++rem;    /* <-- ensures (rem > 0) is true    */ 
-	while (p[1] == '\n') ++p; /* scan over consecutive newlines   */
-	state = 0;
-	if (p[1] == '\0') {
-	  out[rem-1] = '\0';      /* rem > 0 because of 4 lines above */
-	  break;
-	}
-      }
-      else {
-	if (*p <= 0x7F && isgraph((int) *p)) {
-	  out[rem] = (char) *p; ++rem;
-	}
-      }
-    }
-
-    /* -- at start or after newline
-     */
-    else /* if (state == 0) */ {
-      if        (0 == strncmp((char *) p, "user", 4)) {
-	out[rem] = 'u'; ++rem;
-	p += 3;
-      } else if (0 == strncmp((char *) p, "group", 5)) {
-	out[rem] = 'g'; ++rem;
-	p += 4; 
-      } else if (0 == strncmp((char *) p, "mask", 4)) {
-	out[rem] = 'm'; ++rem;
-	p += 3;
-      } else if (0 == strncmp((char *) p, "other", 5)) {
-	out[rem] = 'o';
-	p += 4; ++rem;
-      } else if (*p == '\0') {
-	if (rem > 0) { out[rem-1] = '\0'; }
-	break;
-      } else {
-	if (*p <= 0x7F && isprint((int) *p)) {
-	  out[rem] = (char) *p; ++rem;
-	}
-      }
-      state = 1;
-    }
-    ++p;
-  }
- exit_it:
-  out[rem] = '\0';
-  return out;
-}
-
-
-char * sh_util_strdup_l (const char * str, size_t len)
-{
-  char * p = NULL;
-
-  SL_ENTER(_("sh_util_strdup_l"));
-
-  SH_VALIDATE_NE(str, NULL);
-  SH_VALIDATE_NE(len, 0);
-
-  if (str && sl_ok_adds (len, 1))
-    {
-      p   = SH_ALLOC (len + 1);
-      (void) memcpy (p, str, len+1);
-    }
-  else
-    {
-      safe_fatal(_("integer overflow in sh_util_strdup_l"), FIL__, __LINE__);
-    }
-  SL_RETURN( p, _("sh_util_strdup_l"));
-}
-
+    
 char * sh_util_strdup (const char * str) 
 {
@@ -379,31 +206,11 @@
   SL_ENTER(_("sh_util_strdup"));
 
-  SH_VALIDATE_NE(str, NULL);
-
-  if (str)
+  if (str != NULL)
     {
       len = sl_strlen(str);
       p   = SH_ALLOC (len + 1);
-      (void) memcpy (p, str, len+1);
+      (void) sl_strlcpy (p, str, len+1);
     }
   SL_RETURN( p, _("sh_util_strdup"));
-}
-
-char * sh_util_strdup_track (const char * str, char * file, int line) 
-{
-  char * p = NULL;
-  size_t len;
-
-  SL_ENTER(_("sh_util_strdup_track"));
-
-  SH_VALIDATE_NE(str, NULL);
-
-  if (str)
-    {
-      len = sl_strlen(str);
-      p   = SH_OALLOC (len + 1, file, line);
-      (void) memcpy (p, str, len+1);
-    }
-  SL_RETURN( p, _("sh_util_strdup_track"));
 }
 
@@ -413,24 +220,22 @@
 char * sh_util_strsep (char **str, const char *delim) 
 {
-  char *ret, *c;
-  const char *d;
+  char *ret, *c, *d;
 
   SL_ENTER(_("sh_util_strsep"));
   ret = *str;
 
-  SH_VALIDATE_NE(ret, NULL);
-
-  if (*str)
-    {
-      for (c = *str; *c != '\0'; c++) {
-	for (d = delim; *d != '\0'; d++) {
-	  if (*c == *d) {
-	    *c = '\0';
-	    *str = c + 1;
-	    SL_RETURN(ret, _("sh_util_strsep"));
-	  }
-	}
+  if (ret == NULL) {
+    SL_RETURN(ret, _("sh_util_strsep"));
+  }
+
+  for (c = *str; *c != '\0'; c++) {
+    for (d = (char *) delim; *d != '\0'; d++) {
+      if (*c == *d) {
+        *c = '\0';
+        *str = c + 1;
+        SL_RETURN(ret, _("sh_util_strsep"));
       }
     }
+  }
 
   /* If we get to here, there's no delimiters in the string */
@@ -440,5 +245,5 @@
 
 
-/* returned string must be free'd by caller.
+/* returned string must be free'd by caller
  */
 char * sh_util_formatted (const char * formatt, st_format * ftab)
@@ -455,11 +260,10 @@
   time_t inpp;
 
-  char * clist[16] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-		       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
+  char * clist[16];
   int    nn = 0;
 
   SL_ENTER(_("sh_util_formatted"));
 
-  if (formatt == NULL || ftab == NULL || *formatt == '\0')
+  if (formatt == NULL || ftab == NULL)
     SL_RETURN(NULL, _("sh_util_formatted"));
 
@@ -467,21 +271,25 @@
    */
   size = sl_strlen(formatt);
-
-  if (!sl_ok_adds(size, 1))
+  if (size > 0)
+    {
+      fmt = (char *) SH_ALLOC(size + 1);
+      (void) sl_strlcpy(fmt, formatt, size + 1);
+    }
+  else
     SL_RETURN(NULL, _("sh_util_formatted"));
 
-  ++size;
-  fmt = SH_ALLOC(size);
-  (void) sl_strlcpy(fmt, formatt, size);
-
   p = fmt;
 
   j = 0;
-  while (ftab[j].fchar != '\0') {
-    if (ftab[j].type != S_FMT_STRING)
-      ftab[j].data_str = NULL;
-    ++j;
-  }
- 
+  while (ftab[j].fchar != '\0')
+    {
+      if (ftab[j].type != S_FMT_STRING)
+	ftab[j].data_str = NULL;
+      ++j;
+    }
+
+  for (j = 0; j < 16; ++j)
+    clist[j] = NULL;
+
   while (p != NULL && *p != '\0' && NULL != (q = strchr(p, '%')))
     {
@@ -516,10 +324,8 @@
 	      i  = 1;
 	      
-	      switch(ftab[j].type) {
-
-	      case S_FMT_STRING:
+	      if (ftab[j].type == S_FMT_STRING)
 		{
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -529,9 +335,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_ULONG:
+	      else if (ftab[j].type == S_FMT_ULONG)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -541,5 +345,5 @@
 		  /*@+bufferoverflowhigh@*/
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -549,9 +353,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_LONG:
+	      else if (ftab[j].type == S_FMT_LONG)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -561,5 +363,5 @@
 		  /*@+bufferoverflowhigh@*/
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -569,9 +371,7 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      case S_FMT_TIME:
+	      else if (ftab[j].type == S_FMT_TIME)
 		{
 		  ftab[j].data_str = (char *) SH_ALLOC(64);
@@ -593,5 +393,5 @@
 		    }
 		  isiz = sl_strlen(ftab[j].data_str);
-		  if (isiz > 0 && sl_ok_adds(size, isiz))
+		  if (isiz > 0)
 		    {
 		      size += isiz;
@@ -601,19 +401,11 @@
 		  else
 		    *q = '%';
-		  goto endsrch;
+		  break;
 		}
-		break;
-
-	      default:
-		/* do nothing */;
-	      }
 
 	    }
-	  ++j;
-	}
-
-    endsrch:
-
-      p = q;
+	  else
+	    ++j;
+	}
 
       /* -- not found -- */
@@ -621,7 +413,11 @@
 	{
 	  *q = '%';
+	  p = q;
 	  ++p;
 	}
-
+      else
+	{
+	  p = q;
+	}
     }
 
@@ -633,6 +429,5 @@
   /* -- closing '\0' --
    */
-  if (sl_ok_adds(size, 1))
-    size++;
+  size++;
   outstr = (char *) SH_ALLOC(size);
 
@@ -644,14 +439,14 @@
 		      clist[8],  clist[9], clist[10], clist[11], 
 		      clist[12], clist[13], clist[14], clist[15]); 
-  outstr[size-1] = '\0';
-
+  
   /* -- cleanup --
    */
   j = 0;
-  while (ftab[j].fchar != '\0') {
-    if (ftab[j].type != S_FMT_STRING && ftab[j].data_str != NULL)
-      SH_FREE(ftab[j].data_str);
-    ++j;
-  }
+  while (ftab[j].fchar != '\0')
+    {
+      if (ftab[j].type != S_FMT_STRING && ftab[j].data_str != NULL)
+	SH_FREE(ftab[j].data_str);
+      ++j;
+    }
   SH_FREE(fmt);
 
@@ -659,8 +454,7 @@
 }
 
-/* read a hexchar, return int value (0-15)
- * can't inline (AIX)
- */
-int sh_util_hexchar( const char c )
+/* can't inline (AIX)
+ */
+int sh_util_hexchar( char c )
 {
   /*@+charint@*/
@@ -675,51 +469,24 @@
 }
 
-char * sh_util_charhex( unsigned char i , char * i2h)
-{
-  int j, k;
-
-  j = i / 16;
-  k = i - (j*16);
-
-  if (j < 10) i2h[0] = '0'+j;
-  else        i2h[0] = 'A'+(j-10);
-  
-  if (k < 10) i2h[1] = '0'+k;
-  else        i2h[1] = 'A'+(k-10);
-
-  return i2h;
-}
-
 /* read a hexadecimal key, convert to binary
  */
-int sh_util_hextobinary (char * binary, const char * hex, int bytes)
+int sh_util_hextobinary (char * binary, char * hex, int bytes)
 {
   int i = 0, j, k, l = 0;
-  char c;
-
-#define SH_HEXCHAR(x, y) \
-    c = (x); \
-    if ( c >= '0' && c <= '9' ) \
-      y = c - '0'; \
-    else if ( c >= 'a' && c <= 'f' ) \
-      y = c - 'a' + 10; \
-    else if ( c >= 'A' && c <= 'F' ) \
-      y = c - 'A' + 10; \
-    else \
-      SL_RETURN((-1), _("sh_util_hextobinary"))
-
 
   SL_ENTER(_("sh_util_hextobinary"));
 
-  if (bytes < 2)
-    SL_RETURN((-1), _("sh_util_hextobinary"));
-
-  while (i < (bytes-1))
-    {
-      SH_HEXCHAR(hex[i],   k);
-      SH_HEXCHAR(hex[i+1], j);
-      
-      binary[l] = (char)(k * 16 + j);
-      ++l; i+= 2;
+  while (i < bytes)
+    {
+      k = sh_util_hexchar(hex[i]); j = sh_util_hexchar(hex[i+1]); 
+      if (k != -1 && j != -1) 
+        {
+          binary[l] = (char)(k * 16 + j);
+          ++l; i+= 2;
+        }
+      else
+        {
+	  SL_RETURN((-1), _("sh_util_hextobinary"));
+        }
     }
   
@@ -746,6 +513,5 @@
  */
 static char * sh_util_hmac_tiger (char * hexkey,  
-				  char * text, size_t textlen,
-				  char * res, size_t len)
+				  char * text, size_t textlen)
 {
   static char opad[KEY_BLOCK] = { 
@@ -761,10 +527,4 @@
     (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36
   };
-  static char  zap[KEY_BLOCK] = { 
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,  
-    (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00
-  };
   char        K[KEY_BLOCK];
   char        outer[KEY_BLOCK];
@@ -773,90 +533,78 @@
   UINT32    * h2;
   UINT32      cc[KEY_LEN/4];
-  UINT32      kbuf[KEY_BYT/sizeof(UINT32)];
-  char hashbuf[KEYBUF_SIZE];
-
-  int         result;
+  char      * res;
+
   size_t      i;
 
   SL_ENTER(_("sh_util_hmac_tiger"));
-
-  ASSERT((KEY_BLOCK <= (KEY_LEN/2)), _("KEY_BLOCK <= (KEY_LEN/2)"));
-
-  memcpy (K, zap, KEY_BLOCK);
-
-  result = sh_util_hextobinary (K, hexkey, KEY_LEN);
-
-  ASSERT((result >= 0), _("result >= 0"));
-
-  if ((result >= 0) && sl_ok_adds(textlen, KEY_BLOCK))
-    {
-      inner = (char *) SH_ALLOC (textlen + KEY_BLOCK); 
-
-      for (i = 0; i < KEY_BLOCK; ++i)
-	{
-	  outer[i]  = K[i] ^ opad[i];
-	  inner[i]  = K[i] ^ ipad[i];
-	}
-      for (i = KEY_BLOCK; i < (KEY_BLOCK+textlen); ++i)
-	{
-	  inner[i] = text[i - KEY_BLOCK];
-	}
-    }
-  else
-    {
-      sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
-		      _("integer overflow"), 
-		      _("sh_util_hmac_tiger"));
-      (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
-      sl_strlcpy(res, hashbuf, len);
+  ASSERT((KEY_BLOCK <= (KEY_LEN/2)), _("KEY_BLOCK <= (KEY_LEN/2)"))
+
+  if (KEY_BLOCK > (KEY_LEN/2))
+    {
+      res = sh_tiger_hash (NULL, TIGER_DATA, 0);
       SL_RETURN(res, _("sh_util_hmac_tiger"));
+    }
+
+  memset (K, 0x00, KEY_BLOCK);
+
+  if (sh_util_hextobinary (K, hexkey, KEY_LEN) < 0)
+    {
+      res = sh_tiger_hash (NULL, TIGER_DATA, 0);
+      SL_RETURN(res, _("sh_util_hmac_tiger"));
+    }
+
+  inner = (char *) SH_ALLOC (textlen + KEY_BLOCK); 
+
+  for (i = 0; i < KEY_BLOCK; ++i)
+    {
+      outer[i]  = K[i] ^ opad[i];
+      inner[i]  = K[i] ^ ipad[i];
+    }
+  for (i = KEY_BLOCK; i < (KEY_BLOCK+textlen); ++i)
+    {
+      inner[i] = text[i - KEY_BLOCK];
     }
 
   /* now compute the hash 
    */
-  h1 = sh_tiger_hash_uint32 ( outer, TIGER_DATA, KEY_BLOCK,
-			      kbuf, KEY_BYT/sizeof(UINT32));
+  h1 = sh_tiger_hash_uint32 ( outer,
+			      TIGER_DATA,
+			      KEY_BLOCK);
   for (i = 0; i < (KEY_LEN/8); ++i)
-    copy_four ( (unsigned char *) &(cc[i]), h1[i]);
-
-  /* cppcheck-suppress uninitvar */
-  h2 = sh_tiger_hash_uint32 ( inner, TIGER_DATA,
-			      (unsigned long) KEY_BLOCK+textlen,
-			      kbuf, KEY_BYT/sizeof(UINT32));
+    {
+      /* cc[i] = h1[i]; */
+      copy_four ( (unsigned char *) &(cc[i]), h1[i]);
+    }
+
+  h2 = sh_tiger_hash_uint32 ( inner,
+			      TIGER_DATA,
+			      (unsigned long) KEY_BLOCK+textlen);
   for (i = KEY_LEN/8; i < (KEY_LEN/4); ++i)
-    copy_four ( (unsigned char *) &(cc[i]), h2[i - (KEY_LEN/8)]);
-
-  /* cppcheck-suppress uninitvar */
+    {
+      copy_four ( (unsigned char *) &(cc[i]), h2[i - (KEY_LEN/8)]);
+      /* cc[i] = h2[i - (KEY_LEN/8)]; */
+    }
   SH_FREE(inner);
   
-  (void) sh_tiger_hash ((char *) &cc[0],
-			TIGER_DATA,
-			(unsigned long) (KEY_LEN/4 * sizeof(UINT32)),
-			hashbuf, sizeof(hashbuf));
-
-  sl_strlcpy(res, hashbuf, len);
+  res = sh_tiger_hash ((char *) &cc[0],
+		       TIGER_DATA,
+		       (unsigned long) (KEY_LEN/4 * sizeof(UINT32)));
+
   SL_RETURN(res, _("sh_util_hmac_tiger"));
 }
 
 static char * sh_util_hash_tiger ( char * hexkey,  
-				   char * text, size_t textlen,
-				   char * res, size_t len)
-{
+				   char * text, size_t textlen)
+{
+  char         * res;
   char           h2[2*KEY_LEN+1];
-  char hashbuf[KEYBUF_SIZE];
-
   SL_ENTER(_("sh_util_hash_tiger"));
 
   (void) sl_strlcpy(h2, hexkey, KEY_LEN+1); 
-  (void) sl_strlcat(h2, 
-		    sh_tiger_hash(text, TIGER_DATA, 
-				  (unsigned long) textlen,
-				  hashbuf, sizeof(hashbuf)), 
-		    2*KEY_LEN+1
-		    );
-
-  (void) sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN, hashbuf, sizeof(hashbuf));
-
-  sl_strlcpy(res, hashbuf, len);
+  (void) sl_strlcat(h2, sh_tiger_hash(text, TIGER_DATA, 
+				      (unsigned long) textlen), 2*KEY_LEN+1);
+
+  res = sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN);
+
   SL_RETURN(res, _("sh_util_hash_tiger"));
 }
@@ -869,5 +617,5 @@
 static int sigtype = TYPE_HMAC;
 
-int sh_util_sigtype (const char * c)
+int sh_util_sigtype (char * c)
 {
   SL_ENTER(_("sh_util_sigtype"));
@@ -886,6 +634,5 @@
 
 char * sh_util_siggen (char * hexkey,  
-		       char * text, size_t textlen,
-		       char * res, size_t len)  
+		       char * text, size_t textlen)  
 {
   char * p;
@@ -894,15 +641,16 @@
   if (sigtype == TYPE_HMAC)
     p = sh_util_hmac_tiger (hexkey,  
-			    text, textlen, res, len);
+			    text, textlen);
   else
     p = sh_util_hash_tiger (hexkey,  
-			    text, textlen, res, len);
+			    text, textlen);
   SL_RETURN(p, _("sh_util_siggen"));
 }    
 
+  
  
 /* a simple compressor
  */
-size_t sh_util_compress (char * dest, char * src, size_t dest_size)
+long sh_util_compress (char * dest, char * src, size_t dest_size)
 {
   char * add;
@@ -945,5 +693,5 @@
 
   dest[dest_size-1] = '\0'; /* paranoia       */
-  SL_RETURN((count), _("sh_util_compress")); /* no of chars copied */    
+  SL_RETURN(((long)count), _("sh_util_compress")); /* no of chars copied */    
 }
 
@@ -959,8 +707,4 @@
     char c[sizeof(long)];
   } u;
-#ifdef WORDS_BIGENDIAN
-  unsigned char swap;
-  unsigned char * ii = (unsigned char *) dest;
-#endif
 
   SL_ENTER(_("sh_util_cpylong"));    
@@ -985,8 +729,4 @@
       ++i;
     }
-#ifdef WORDS_BIGENDIAN
-  swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
-  swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
-#endif
   SL_RET0(_("sh_util_cpylong"));
 }
@@ -1012,5 +752,5 @@
 
 
-/* interval [0, 4294967295]
+/* interval [0, 4294967296]
  */
 static UINT32 taus_get_long (void *vstate)
@@ -1018,8 +758,6 @@
   UINT32 * state = (UINT32 *) vstate;
 
-  /*
   if (skey->rngI == BAD)
     (void)taus_seed();
-  */
 
 #define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
@@ -1035,53 +773,28 @@
  * input for a one-way hash function.
  */
-
-UINT32 taus_get ()
-{
-#define TAUS_SAMPLE 12
-
-  UINT32   taus_svec[TAUS_SAMPLE];
+UINT32 taus_get (void *state1, void *state2, void *state3)
+{
+  UINT32   svec[6];
   UINT32   retval;
   UINT32 * res;
-  UINT32 * res_vec = &(skey->res_vec[0]);
-  static   int      res_num = 0;
   register int i;
-  UINT32       kbuf[KEY_BYT/sizeof(UINT32)];
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  if (res_num > 0)
-    {
-      retval  = res_vec[res_num];
-      res_num = (res_num == 5) ? 0 : (res_num + 1);
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
-      return  retval;
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  (void)taus_seed();
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  for (i = 0; i < (TAUS_SAMPLE/3); ++i)
-    {
-      taus_svec[i*3]   = taus_get_long (&(skey->rng0[0]));
-      taus_svec[i*3+1] = taus_get_long (&(skey->rng1[0]));
-      taus_svec[i*3+2] = taus_get_long (&(skey->rng2[0]));
-    }
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  res     = sh_tiger_hash_uint32 ( (char *) &taus_svec[0], 
+
+  svec[0] = taus_get_long (state1);
+  svec[1] = taus_get_long (state2);
+  svec[2] = taus_get_long (state3);
+  svec[3] = taus_get_long (state1);
+  svec[4] = taus_get_long (state2);
+  svec[5] = taus_get_long (state3);
+
+  res     = sh_tiger_hash_uint32 ( (char *) &svec[0], 
 				   TIGER_DATA, 
-				   (unsigned long)(TAUS_SAMPLE * sizeof(UINT32)),
-				   kbuf, KEY_BYT/sizeof(UINT32));
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  for (i = 1; i < 6; ++i)
-    { 
-      res_vec[i] = res[i];
-    }
-  retval  = res[0];
-  res_num = 1;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
-
-  memset(taus_svec, 0, TAUS_SAMPLE * sizeof(UINT32));
+				   (unsigned long)(6 * sizeof(UINT32)));
+
+  for (i = 1; i < KEY_BYT/4; ++i)
+    res[0] ^= res[i];
+  retval = res[0];
+
+  memset (res,  0,            KEY_BYT);
+  memset (svec, 0, 6 * sizeof(UINT32));
 
   return retval;
@@ -1139,7 +852,5 @@
   char                 bufx[9 * sizeof(UINT32) + 1];
   int                  status;
-  static unsigned long seed_time    = 0;
-  static unsigned long seed_counter = 3000;
-  unsigned long        gtime;
+  static unsigned long seed_time = 0;
 
   SL_ENTER(_("taus_seed"));
@@ -1147,13 +858,9 @@
   if (skey->rngI == GOOD)
     {
-      ++seed_counter;
-
-      if ( ((sh_unix_longtime () - seed_time) < 1800) &&
-	   ( seed_counter                     < 3000))
+      if ( (sh_unix_longtime () - seed_time) < 3600)
 	SL_RETURN( (0), _("taus_seed"));
     }
   
-  seed_time    = sh_unix_longtime ();
-  seed_counter = 0;
+  seed_time = sh_unix_longtime ();
 
   status = sh_entropy (24, bufx);
@@ -1161,5 +868,5 @@
   if (!SL_ISERROR(status))
     {
-      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
+      skey->rngI = GOOD;
       memcpy (&skey->rng0[0], &bufx[0],                  2*sizeof(UINT32));
       memcpy (&skey->rng1[0], &bufx[2*sizeof(UINT32)],   2*sizeof(UINT32));
@@ -1175,6 +882,4 @@
       taus_set_from_state( &(skey->rng2[0]), &(skey->rng2[0]));
 
-      skey->rngI = GOOD;
-      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
       SL_RETURN( (0), _("taus_seed"));
     }
@@ -1185,16 +890,13 @@
   /* emergency backup - unsafe !
    */
+  skey->rngI = GOOD;
 #ifdef HAVE_GETTIMEOFDAY
-  gtime = sh_unix_notime();
+  taus_set_from_ulong ( &(skey->rng0[0]), LCG (sh_unix_notime())      );
 #else
-  gtime = seed_time;
-#endif
-
-  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
-  taus_set_from_ulong ( &(skey->rng0[0]), LCG (gtime)          );
+  taus_set_from_ulong ( &(skey->rng0[0]), LCG (seed_time)      );
+#endif
   taus_set_from_ulong ( &(skey->rng1[0]), LCG (skey->rng0[0])  );
   taus_set_from_ulong ( &(skey->rng2[0]), LCG (skey->rng1[0])  );
   skey->rngI = BAD;
-  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
   SL_RETURN( (-1), _("taus_seed"));
@@ -1206,5 +908,5 @@
 static void copy_four (unsigned char * dest, UINT32 in);
 
-int sh_util_set_newkey (const char * new_in)
+int sh_util_set_newkey (char * new)
 {
   size_t i, j = 0;
@@ -1213,6 +915,6 @@
   SL_TICKET fout;
   char * key;
-  char * path = NULL;
-  char * outpath = NULL;
+  char * path;
+  char * outpath;
   unsigned char * image = NULL;
   long s = 0;
@@ -1220,9 +922,8 @@
   long ii, k = 0;
   UINT32    * h1;
-  char * new = NULL;
 
   if (0 != sl_is_suid())
     {
-      fprintf(stderr, "%s", _("ERROR: insufficient privilege\n"));
+      fprintf(stderr, _("ERROR: insufficient privilege\n"));
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1230,7 +931,7 @@
     }
         
-  if (new_in == NULL || new_in[0] == '\0')
-    {
-      fprintf(stderr, "%s", 
+  if (new == NULL || new[0] == '\0')
+    {
+      fprintf(stderr, 
 	      _("ERROR: no key given\n Argument must be 'key@path'\n"));
       _exit (EXIT_FAILURE);
@@ -1238,10 +939,4 @@
       return -1;
     }
-
-  len = strlen(new_in) + 1;
-  if (NULL == (new = calloc(1, len)))
-    goto bail_mem;
-  memcpy(new, new_in, len);
-
   key = new;
   len = strlen(new);
@@ -1255,7 +950,6 @@
   if (j == 0)
     {
-      fprintf(stderr, "%s",
+      fprintf(stderr, 
 	      _("ERROR: no path to executable given\n Argument must be 'key@path'\n"));
-      free(new);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1264,18 +958,17 @@
   else
     path = &new[j];
-
-  len = strlen(path) + 1 + 4;
   /*@-usedef@*/
-  if (NULL == (outpath = calloc(1,len)))
+  if (NULL == (outpath = malloc(strlen(path) + 1 + 4)))
     goto bail_mem;
   /*@-usedef@*/
-  sl_snprintf (outpath, len, _("%s.out"), path);
-
-  fp = sl_open_read(FIL__, __LINE__, path, SL_NOPRIV);
+  /*@-bufferoverflowhigh@*/
+  sprintf (outpath, _("%s.out"), path);               /* known to fit  */ 
+  /*@+bufferoverflowhigh@*/
+
+  fp = sl_open_read(path, SL_NOPRIV);
   if (SL_ISERROR(fp))
     {
       fprintf(stderr, 
 	      _("ERROR: cannot open %s for read (errnum = %ld)\n"), path, fp);
-      free(new); free (outpath);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1283,10 +976,9 @@
     }
   
-  fout = sl_open_write(FIL__, __LINE__, outpath, SL_NOPRIV);
+  fout = sl_open_write(outpath, SL_NOPRIV);
   if (SL_ISERROR(fout))
     {
       fprintf(stderr, 
 	      _("ERROR: cannot open %s (errnum = %ld)\n"), outpath, fout);
-      free(new); free (outpath);
       _exit (EXIT_FAILURE);
       /*@notreached@*/
@@ -1295,17 +987,12 @@
 
 
-  image = calloc(1,4096);
+  image = malloc (4096);
   if (!image)
     goto bail_mem;
   while (0 < (ii = sl_read (fp, &image[s], 4096)))
     {
-      unsigned char * ptr;
       ilen += ii;
       s    += 4096;
-      ptr = realloc (image, (size_t) (4096 + s));
-      if (ptr)
-	image = ptr;
-      else
-	{ free(image); image = NULL; }
+      image = realloc (image, (size_t) (4096 + s));
       if (!image)
 	goto bail_mem;
@@ -1326,10 +1013,7 @@
 	  image[k+7] == new_key[7])
 	{
-	  UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
-
-	  printf("%s", _("old key found\n")); 
+	  printf(_("old key found\n")); 
 	  h1 = sh_tiger_hash_uint32 (key, TIGER_DATA, 
-				     (unsigned long)strlen(key),
-				     kbuf, KEY_BYT/sizeof(UINT32));
+				     (unsigned long)strlen(key));
 	  copy_four( (unsigned char *) &(image[k]),   h1[0]);
 	  copy_four( (unsigned char *) &(image[k+4]), h1[1]);
@@ -1337,5 +1021,4 @@
 	  (void) sl_close (fout);
 	  printf(_("new file %s written\n"), outpath);
-	  free(new); free (outpath); free(image);
 	  _exit (EXIT_SUCCESS);
 	  /*@notreached@*/
@@ -1344,7 +1027,6 @@
     }
 
-  fprintf(stderr, "%s",
+  fprintf(stderr, 
 	  _("ERROR: old key not found\n"));
-  free(new); free (outpath); free(image);
   _exit (EXIT_FAILURE);
   /*@notreached@*/
@@ -1353,9 +1035,6 @@
 
  bail_mem:
-  fprintf(stderr, "%s",
+  fprintf(stderr, 
 	  _("ERROR: out of memory\n"));
-  if (new) free(new); 
-  if (outpath) free (outpath);
-  if (image) free (image);
   _exit (EXIT_FAILURE);
   /*@notreached@*/
@@ -1378,5 +1057,4 @@
   register int    i, j, j1 = 0, j2 = 0, j3;
   char          * dez; 
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_util_encode"));
@@ -1398,16 +1076,13 @@
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN,
-				  hashbuf, sizeof(hashbuf)), 
+		    sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN), 
 		    KEY_LEN+1);
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_util_hmac_tiger (skey->vernam, salt, strlen(salt),
-					hashbuf, sizeof(hashbuf)),
-		    KEY_LEN+1);
+		    sh_util_hmac_tiger (skey->vernam, salt, strlen(salt)),
+	     KEY_LEN+1);
 
   (void) sl_strlcpy(skey->vernam, 
-		    sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8,
-					hashbuf, sizeof(hashbuf)),
+		    sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8),
 		    KEY_LEN+1);
 
@@ -1441,15 +1116,17 @@
 /* server mode 
  */
-int sh_util_setserver (const char * dummy)
+int sh_util_setserver (char * dummy)
 {
   SL_ENTER(_("sh_util_setserver"));
 
-  (void) dummy;
-  sh.flag.isserver = GOOD;
+  if (dummy)
+    sh.flag.isserver = GOOD;
+  else
+    sh.flag.isserver = GOOD;
   SL_RETURN((0),_("sh_util_setserver"));
 }
 
 
-int sh_util_setlooptime (const char * str)
+int sh_util_setlooptime (char * str)
 {
   int i = atoi (str);
@@ -1468,5 +1145,5 @@
 
 #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
-int  sh_util_setchecksum (const char * str)
+int  sh_util_setchecksum (char * str)
 {
   static int reject = 0;
@@ -1566,5 +1243,4 @@
   int          status = 0;
   char       * p;
-  char hashbuf[KEYBUF_SIZE];
 
   SL_ENTER(_("sh_util_keyinit"));
@@ -1584,9 +1260,9 @@
 
   for (i = 0; i < 6; ++i)
-    bufy[i] = taus_get();
+    bufy[i] = taus_get(&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
 
   p = sh_tiger_hash ((char *) bufy, TIGER_DATA, 
-		     (unsigned long)(6*sizeof(UINT32)),
-		     hashbuf, sizeof(hashbuf));
+		     (unsigned long)(6*sizeof(UINT32)));
+  p[size-1] = '\0';
 
   i = sl_strlcpy(buf, p, (size_t)size);
@@ -1607,160 +1283,11 @@
 
 static unsigned char sh_obscure_index[256];
-static int sh_obscure_no_check = S_FALSE;
-
-int sh_util_valid_utf8 (const unsigned char * str) 
-{
-  const int     sh_val_utf8_1 = 1;
-  const int     sh_val_utf8_2 = 2;
-  const int     sh_val_utf8_3 = 3;
-  const int     sh_val_utf8_4 = 4;
-
-  size_t        len = strlen((const char *)str);
-  size_t        l   = 0;
-  int           typ = 0;
-  unsigned char c     = '\0';
-  unsigned char c2[2] = { 0x00, 0x00 };
-  unsigned char c3[3] = { 0x00, 0x00, 0x00 };
-
-
-#define SH_VAL_UTF8_1 ((c != '\0') && ((c & 0x80) == 0x00))
-#define SH_VAL_UTF8_2 ((c != '\0') && ((c & 0xE0) == 0xC0)) /* 110x xxxx */
-#define SH_VAL_UTF8_3 ((c != '\0') && ((c & 0xF0) == 0xE0)) /* 1110 xxxx */
-#define SH_VAL_UTF8_4 ((c != '\0') && ((c & 0xF8) == 0xF0)) /* 1111 0xxx */
-#define SH_VAL_UTF8_N ((c != '\0') && ((c & 0xC0) == 0x80)) /* 10xx xxxx */
-#define SH_VAL_BAD    ((c == '"')  || (c == '\t') || (c == '\b') || \
-                       (c == '\f') || (c == '\n') || \
-                       (c == '\r') || (c == '\v') || iscntrl((int) c) || \
-                       (c != ' ' && !isgraph ((int) c)))
-   
-  while(l < len) 
-    {
-      c = str[l];
-
-      if      (SH_VAL_UTF8_1) 
-	{
-	  if (!(SH_VAL_BAD && (sh_obscure_index[c] != 1)))
-	    {
-	      typ = sh_val_utf8_1;
-	      ++l; continue;
-	    }
-	  else
-	    {
-	      return S_FALSE;
-	    }
-	} 
-      else if (SH_VAL_UTF8_2) 
-	{ 
-	  typ = sh_val_utf8_2;
-	  c2[0] = c;
-	  if ((c & 0x3e) != 0x00) /* !(overlong 2-byte seq.) */
-	    {
-	      ++l; 
-	      if (l != len) {
-		c = str[l];
-		if(SH_VAL_UTF8_N) {
-		  c2[1] = c;
-		  ++l; continue;
-		} 
-		else {
-		  return S_FALSE;
-		} 
-	      } 
-	      else {
-		return S_FALSE; 
-	      }
-	    }
-	  else
-	    {
-	      return S_FALSE; /* overlong 2-byte seq. */
-	    }
-	} 
-      else if (SH_VAL_UTF8_3) 
-	{
-	  typ = sh_val_utf8_3;
-	  c3[0] = c;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  if (((str[l-1] & 0x1F) == 0x00) && ((c & 0x60) == 0x00))
-	    return S_FALSE; /* overlong 3-byte seq. */
-	  c3[1] = c;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  c3[2] = c;
-	  ++l; continue;
-	} 
-      else if (SH_VAL_UTF8_4) 
-	{
-	  typ = sh_val_utf8_4;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  if (((str[l-1] & 0x0F) == 0x00) && ((c & 0x70) == 0x00))
-	    return S_FALSE; /* overlong 4-byte seq. */
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  ++l; if (l == len) return S_FALSE; c = str[l];
-	  if(!SH_VAL_UTF8_N) return S_FALSE;
-	  ++l; continue;
-	}
-      return S_FALSE;
-    }
-
-  /* last character is invisible (space or else)
-   */
-  if (typ == sh_val_utf8_1)
-    { 
-      if (c != ' ')
-	return S_TRUE;
-      else
-	return S_FALSE;
-    }
-  else if (typ == sh_val_utf8_2)
-    {
-      if (c2[0] == 0xC2 && c2[1] == 0xA0) /* nbsp */
-	return S_FALSE;
-      else
-	return S_TRUE;
-    }
-  else if (typ == sh_val_utf8_3)
-    {
-      if (c3[0] == 0xE2) 
-	{
-	  if (c3[1] == 0x80 && c3[2] >= 0x80 && c3[2] <= 0x8F)
-	    return S_FALSE; /* various spaces, left-to-right, right-to-left */
-	  else if (c3[1] == 0x80 && (c3[2] == 0xA8 || c3[2] == 0xA9 || 
-				     c3[2] == 0xAD || c3[2] == 0xAF))
-	    return S_FALSE; /* line sep, para sep, zw word joiner, nnbsp */
-	  else if (c3[1] == 0x81 && (c3[2] == 0xA0 || c3[2] == 0xA1 || 
-				     c3[2] == 0x9F))
-	    return S_FALSE; /* word joiner, function app, math space */
-	  else
-	    return S_TRUE;
-	}
-      else if (c3[0] == 0xE3 && c3[1] == 0x80 && c3[2] == 0x80)
-	{
-	  return S_FALSE; /* ideographic space */
-	}
-      else if (c3[0] == 0xEF && c3[1] == 0xBB && c3[2] == 0xBF)
-	{
-	  return S_FALSE; /* zwnbsp */
-	}
-      else
-	{
-	  return S_TRUE;
-	}
-    }
-  else
-    {
-      return S_TRUE;
-    }
-}
-
-
-int sh_util_obscure_ok (const char * str)
+
+int sh_util_obscure_ok (char * str)
 {
   unsigned long   i;
-  char * endptr = NULL;
-
-  SL_ENTER(_("sh_util_obscure_ok"));
+  char * endptr = str;
+
+  SL_ENTER(_("sh_util_obscure_ex"));
 
   if (0 == sl_strncmp("all", str, 3))
@@ -1770,9 +1297,6 @@
 	  sh_obscure_index[i] = (unsigned char)1;
 	}
-      sh_obscure_no_check = S_TRUE;
-      SL_RETURN(0, _("sh_util_obscure_ok"));
-    }
-
-  sh_obscure_no_check = S_FALSE;
+      SL_RETURN(0, _("sh_util_obscure_ex"));
+    }
 
   for (i = 0; i < 255; ++i)
@@ -1780,13 +1304,4 @@
       sh_obscure_index[i] = (unsigned char)0;
     }
-
-  i = strtoul (str, &endptr, 0);
-  if (i > 255)
-    {
-      SL_RETURN(-1, _("sh_util_obscure_ok"));
-    }
-  sh_obscure_index[i] = (unsigned char)1;
-  if (*endptr == ',')
-    ++endptr;
 
   while (*endptr != '\0')
@@ -1795,5 +1310,5 @@
       if (i > 255)
 	{
-	  SL_RETURN(-1, _("sh_util_obscure_ok"));
+	  SL_RETURN(-1, _("sh_util_obscure_ex"));
 	}
       sh_obscure_index[i] = (unsigned char)1;
@@ -1801,26 +1316,12 @@
 	++endptr;
     }
-  SL_RETURN(0, _("sh_util_obscure_ok"));
-}
-
-static int sh_obscure_check_utf8 = S_FALSE;
-
-int sh_util_obscure_utf8 (const char * c)
-{
-  int i;
-  SL_ENTER(_("sh_util_obscure_utf8"));
-  i = sh_util_flagval(c, &(sh_obscure_check_utf8));
-  if (sh_obscure_check_utf8 == S_TRUE)
-    sh_obscure_no_check = S_FALSE;
-  SL_RETURN(i, _("sh_util_obscure_utf8"));
-}
-
-
-int sh_util_obscurename (ShErrLevel level, const char * name_orig, int flag)
-{
-  const unsigned char * name = (const unsigned char *) name_orig;
+  SL_RETURN(0, _("sh_util_obscure_ex"));
+}
+
+int sh_util_obscurename (ShErrLevel level, char * name_orig, int flag)
+{
+  char * name = name_orig;
   char * safe;
   unsigned int i;
-  size_t len = 0;
 
   SL_ENTER(_("sh_util_obscurename"));
@@ -1828,184 +1329,93 @@
   ASSERT_RET((name != NULL), _("name != NULL"), (0))
 
-  if (sh_obscure_no_check == S_FALSE)
-    {
-      if (sh_obscure_check_utf8 != S_TRUE)
-	{
-	  /* -- Check name. --
-	   */
-	  while (*name != '\0') 
+  /* -- Check name. --
+   */
+  while (*name != '\0') 
+    {
+      if ( (*name) == '"'  || (*name) == '\t' ||
+	   (*name) == '\b' || (*name) == '\f' || 
+	   (*name) == '\n' || (*name) == '\r' ||
+	   (*name) == '\v' || iscntrl((int) *name) || 
+	   ((*name) != ' ' && !isgraph ((int) *name)) ) 
+	{
+	  i = (unsigned char) *name;
+	  if (sh_obscure_index[i] != (unsigned char)1)
 	    {
-	      if ( (*name) >  0x7F || (*name) == '"'  || (*name) == '\t' ||
-		   (*name) == '\b' || (*name) == '\f' || 
-		   (*name) == '\n' || (*name) == '\r' ||
-		   (*name) == '\v' || iscntrl((int) *name) || 
-		   ((*name) != ' ' && !isgraph ((int) *name)) ) 
+	      if (flag == S_TRUE)
 		{
-		  i = (unsigned char) *name;
-		  if (sh_obscure_index[i] != (unsigned char)1)
-		    {
-		      goto err;
-		    }
+		  safe = sh_util_safe_name (name_orig);  
+		  sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 
+				   safe);
+		  SH_FREE(safe);
 		}
-	      name++; ++len;
+	      SL_RETURN((-1),_("sh_util_obscurename"));
 	    }
-
-	  /* Check for blank at end of name
-	   */
-	  if ((len > 0) && (name_orig[len-1] == ' '))
-	    {
-	      goto err;
-	    }
-	}
-      else
-	{
-	  if (S_FALSE == sh_util_valid_utf8(name))
-	    {
-	      goto err;
-	    }
-	  SL_RETURN((0),_("sh_util_obscurename"));
-	}
-    }
-      
+	}
+      name++;
+    }
+
   SL_RETURN((0),_("sh_util_obscurename"));
-
- err:
-  
-  if (flag == S_TRUE)
-    {
-      safe = sh_util_safe_name (name_orig);  
-      sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 
-		       safe);
-      SH_FREE(safe);
-    }
-  SL_RETURN((-1),_("sh_util_obscurename"));
-}
-
+}
 #endif
 
 /* returns freshly allocated memory, return value should be free'd
  */
-char * sh_util_dirname(const char * fullpath)
+char * sh_util_basename(char * fullpath)
 {
   char * retval;
-  size_t len;
-  char * tmp;
-
-  SL_ENTER(_("sh_util_dirname"));
+  size_t i;
+
+  SL_ENTER(_("sh_util_basename"));
 
   ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
-  ASSERT_RET ((*fullpath == '/'), _("*fullpath == '/'"), (NULL))
-
-  retval = sh_util_strdup(fullpath);
-
-  tmp    = retval;
-  while (*tmp == '/') ++tmp;
-
-  /* (1) only leading slashes -- return exact copy 
+
+  i = sl_strlen (fullpath);  /* fullpath[i] is terminating '\0' */
+
+  while (i > 0) {
+    --i;
+    if (fullpath[i] == '/') break;
+  }
+
+  /* -- Not a valid path. --
    */
-  if (*tmp == '\0')
-    {
-      SL_RETURN(retval, _("sh_util_dirname"));
-    }
-
-  /* (2) there are non-slash characters, so delete trailing slashes
-   */
-  len    = sl_strlen (retval);     /* retval[len] is terminating '\0' */
-
-  while (len > 1 && retval[len-1] == '/')    /* delete trailing slash */
-    {
-      retval[len-1] = '\0';
-      --len;
-    }
-
-  /* (3) now delete all non-slash characters up to the preceding slash
-   */
-  while (len > 1 && retval[len-1] != '/') {
-    retval[len-1] = '\0';
-    --len;
-  }
-
-  /* (4a) only leading slashes left, so return this
-   */
-  if (&(retval[len]) == tmp)
-    {
-      SL_RETURN(retval, _("sh_util_dirname"));
-    }
-
-  /* (4b) strip trailing slash(es) of parent directory
-   */
-  while (len > 1 && retval[len-1] == '/') {
-    retval[len-1] = '\0';
-    --len;
-  }
-  SL_RETURN(retval, _("sh_util_dirname"));
-
+  if ((fullpath[i] != '/') && (i == 0) ) 
+    SL_RETURN(NULL, _("sh_util_basename"));
+
+  retval = SH_ALLOC(i + 1);
+
+  (void) sl_strlcpy (retval, fullpath, i+1);
+
+  SL_RETURN(retval, _("sh_util_basename"));
 }
 
 /* returns freshly allocated memory, return value should be free'd
  */
-char * sh_util_basename(const char * fullpath)
-{
-  char       * retval = NULL;
-  const char * tmp;
-  char       * tmp2;
-  char       * c;
-  size_t       len;
-
-  SL_ENTER(_("sh_util_basename"));
+char * sh_util_filename(char * fullpath)
+{
+  char * retval;
+  char * c;
+  size_t i;
+
+  SL_ENTER(_("sh_util_filename"));
 
   ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
 
-  tmp = fullpath; while (*tmp == '/') ++tmp;
-  if (*tmp == '\0')
-    {
-      retval = sh_util_strdup(fullpath);
-    }
-  else
-    {
-      tmp2 = sh_util_strdup(tmp);
-      len  = sl_strlen (tmp2);
-
-      while (len > 1 && tmp2[len-1] == '/')
-	{
-	  tmp2[len-1] = '\0';
-	  --len;
-	}
-
-      if (tmp2) /* for llvm/clang analyzer */
-	{
-	  c = strrchr(tmp2, '/');
-	  if (c)
-	    {
-	      retval = sh_util_strdup(++c);
-	      SH_FREE(tmp2);
-	    }
-	  else
-	    {
-	      retval = tmp2;
-	    }
-	}
-    }
-
-  SL_RETURN(retval, _("sh_util_basename"));
-}
-
-#define SH_ESCAPE_SPACE      1
-#define SH_DONT_ESCAPE_SPACE 0    
-char * sh_util_safe_name_int (const char * name, int escape_space);
-
+  c = strrchr(fullpath, '/');
+  i = sl_strlen (c);
+  if (i <= 1) SL_RETURN(NULL, _("sh_util_filename")); /* ends in '/' */
+  ++c;
+  --i;
+
+  retval = SH_ALLOC(i + 1);
+
+  (void) sl_strlcpy (retval, c, i+1);
+
+  SL_RETURN(retval, _("sh_util_filename"));
+}
+
+    
+/* returns freshly allocated memory, return value should be free'd
+ */
 char * sh_util_safe_name (const char * name)
-{
-  return sh_util_safe_name_int (name, SH_ESCAPE_SPACE); 
-}
-
-char * sh_util_safe_name_keepspace (const char * name)
-{
-  return sh_util_safe_name_int (name, SH_DONT_ESCAPE_SPACE); 
-}
-
-/* returns freshly allocated memory, return value should be free'd
- */
-char * sh_util_safe_name_int (const char * name, int escape_space)
 {
   register int  i = 0;
@@ -2014,5 +1424,4 @@
   char          oct[32];
   char          format[16];
-  size_t        len;
 
   SL_ENTER(_("sh_util_safe_name"));
@@ -2031,32 +1440,13 @@
   */
 
-  len = sl_strlen(name);
-  p   = name;
-
 #ifdef SH_USE_XML
-  if (sl_ok_muls (6, len) && sl_ok_adds ((6*len), 2))
-    { retval = SH_ALLOC(6 * len + 2); }
-  else
-    {
-      /* return an allocated array
-       */
-      retval = SH_ALLOC(11);
-      (void) sl_strlcpy(retval, _("(overflow)"), 11);
-      SL_RETURN(retval, _("sh_util_safe_name"));
-    }
+  retval = SH_ALLOC(6 * sl_strlen(name) + 2);
 #else
-  if (sl_ok_muls (4, len) && sl_ok_adds ((4*len), 2))
-    { retval = SH_ALLOC(4 * len + 2); }
-  else
-    {
-      /* return an allocated array
-       */
-      retval = SH_ALLOC(11);
-      (void) sl_strlcpy(retval, _("(overflow)"), 11);
-      SL_RETURN(retval, _("sh_util_safe_name"));
-    }
+  retval = SH_ALLOC(4 * sl_strlen(name) + 2);
 #endif 
 
   (void) sl_strncpy(format, _("%c%03o"), 16);
+
+  p = name;
 
   while (*p != '\0') {
@@ -2094,11 +1484,6 @@
 #endif
     } else if ( (*p) == ' ') {     /* space            */
-      if (escape_space) {
-	retval[i] = '\\'; ++i; 
-	retval[i] = ' ';
-      }
-      else {
-	retval[i] = *p;
-      }
+      retval[i] = '\\'; ++i; 
+      retval[i] = ' ';
 #ifdef SH_USE_XML
     } else if ( (*p) == '"') {     /* double quote     */
@@ -2132,5 +1517,4 @@
     } else if (!isgraph ((int) *p)) {    /* not printable    */
       /*@-bufferoverflowhigh -formatconst@*/
-      /* flawfinder: ignore */
       sprintf(oct, format, '\\',                 /* known to fit  */
 	      (unsigned char) *p);
@@ -2150,7 +1534,7 @@
 }
 
-int sh_util_isnum (const char *str)
-{
-  const char *p = str;
+int sh_util_isnum (char *str)
+{
+  char *p = str;
 
   SL_ENTER(_("sh_util_isnum"));
@@ -2168,5 +1552,5 @@
 char * sh_util_strconcat (const char * arg1, ...)
 {
-  size_t    length, l2;
+  size_t    length;
   char    * s;
   char    * strnew;
@@ -2183,19 +1567,10 @@
   while (s != NULL)
     {
-      l2 = sl_strlen (s);
-      if (sl_ok_adds(length, l2))
-	length += l2;
-      else
-	SL_RETURN(NULL, _("sh_util_strconcat"));
+      length = length + sl_strlen (s);
       s = va_arg (vl, char * );
     }
   va_end (vl);
 
-  if (sl_ok_adds(length, 2))
-    strnew = SH_ALLOC( length + 2 );
-  else
-    SL_RETURN(NULL, _("sh_util_strconcat"));
-
-  /* cppcheck-suppress uninitvar */
+  strnew = SH_ALLOC( length + 2 ); 
   strnew[0] = '\0';
 
@@ -2212,172 +1587,4 @@
 
   SL_RETURN(strnew, _("sh_util_strconcat"));
-}
-
-static const char bto64_0[] = N_("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()");
-static char bto64[65] = { '\0' };
-
-  
-size_t sh_util_base64_enc (unsigned char * out, 
-			   const unsigned char * instr, 
-			   size_t lin)
-{
-  int             ll;
-  unsigned char   a, b, c;
-  size_t          len  = 0;
-  size_t          j    = 0;
-
- start:
-  if (bto64[0] != '\0')
-    {
-      if (instr /* && *instr *//* need to handle binary data */)
-	{
-	  if (lin == 0)
-	    lin = strlen((const char *)instr);
-
-	  if (lin > 0)
-	    {
-	      do {
-		ll = 0;
-		
-		if (len < lin) 
-		  { a = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { a = 0; }
-		if (len < lin) 
-		  { b = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { b = 0; }
-		if (len < lin) 
-		  { c = *instr; ++instr; ++len; ++ll; }
-		else 
-		  { c = 0; }
-		
-		*out = bto64[ a >> 2 ];
-		++j; ++out;
-		*out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
-		++j; ++out;
-		*out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
-		++j; ++out;
-		*out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
-		++j; ++out;
-	      } while (len < lin);
-	    }
-	}
-      *out = '\0';
-      return j;
-    }
-
-  memcpy(bto64, _(bto64_0), 65);
-  goto start;
-}
-
-size_t sh_util_base64_enc_alloc (char **out, const char *in, size_t inlen)
-{
-  size_t outlen = SH_B64_SIZ(inlen);
-
-  if (inlen > outlen) /* overflow */
-    {
-      *out = NULL;
-      return 0;
-    }
-
-  *out = SH_ALLOC(outlen);
-  return sh_util_base64_enc((unsigned char *)*out, (const unsigned char *)in, inlen);
-}
-
-size_t sh_util_base64_dec (unsigned char *out, 
-			   const unsigned char *in, 
-			   size_t lin)
-{
-  size_t i;
-  unsigned char c;
-  unsigned char b;
-  size_t lout = 0;
-  unsigned int  w = 0;
-
-  if (out && in)
-    {
-      if (lin == 0)
-	lin = strlen((const char *)in);
-
-      for (i = 0; i < lin; i++)
-	{
-	  c = *in; ++in;
-	  b = 0;
-	  
-	  if ((c >= 'A') && (c <= 'Z'))
-	    {
-	      b = (c - 'A');
-	    }
-	  else if ((c >= 'a') && (c <= 'z'))
-	    {
-	      b = (c - 'a' + 26);
-	    }
-	  else if ((c >= '0') && (c <= '9'))
-	    {
-	      b = (c - '0' + 52);
-	    }
-	  else if (c == '(' || c == '+')
-	    {
-	      b = 62;
-	    }
-	  else if (c == ')' || c == '/')
-	    {
-	      b = 63;
-	    }
-	  else if (c == '?' || c == '=')
-	    {
-	      /* last byte was written to, but will now get
-	       * truncated
-	       */
-	      if (lout > 0) --lout;
-	      break;
-	    }
-	  
-	  if (w == 0)
-	    {
-	      *out = (b << 2) & 0xfc;
-	      ++lout;
-	    }
-	  else if (w == 1)
-	    {
-	      *out |= (b >> 4) & 0x03;
-	      ++out;
-	      *out = (b << 4) & 0xf0;
-	      ++lout;
-	    }
-	  else if (w == 2)
-	    {
-	      *out |= (b >> 2) & 0x0f;
-	      ++out;
-	      *out = (b << 6) & 0xc0;
-	      ++lout;
-	    }
-	  else if (w == 3)
-	    {
-	      *out |= b & 0x3f;
-	      ++out;
-	    }
-	  
-	  ++w;
-	  
-	  if (w == 4)
-	    {
-	      w = 0;
-	    }
-	}
-      *out = '\0';
-    }
-  return lout;
-}
-
-size_t sh_util_base64_dec_alloc (unsigned char **out, const unsigned char *in, 
-				 size_t lin)
-{
-  size_t lout = 3 * (lin / 4) + 2;
-
-  *out = SH_ALLOC(lout);
-
-  return sh_util_base64_dec (*out, in, lin);
 }
 
@@ -2412,7 +1619,7 @@
   if (status != 0 && status != REG_NOMATCH) 
     {
-      errbuf = SH_ALLOC(BUFSIZ);
+      errbuf = SH_ALLOC(BUFSIZ+2);
       (void) regerror(status, &preg, errbuf, BUFSIZ); 
-      errbuf[BUFSIZ-1] = '\0';
+      errbuf[BUFSIZ] = '\0';
       sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
 		       errbuf, regex_str);
