Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcutil / silcutil.c
index 40892e8ae552130c01794a8e49673014dbf4ca08..0cb176e7739ba60576e78c86edd08030a0e33f63 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2006 Pekka Riikonen
+  Copyright (C) 1997 - 2007 Pekka Riikonen
 
   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
@@ -88,7 +88,7 @@ SilcBool silc_to_upper(const char *string, char *dest, SilcUInt32 dest_size)
     return FALSE;
 
   for (i = 0; i < strlen(string); i++)
-    dest[i] = toupper(string[i]);
+    dest[i] = (char)toupper((int)string[i]);
 
   return TRUE;
 }
@@ -103,7 +103,7 @@ SilcBool silc_to_lower(const char *string, char *dest, SilcUInt32 dest_size)
     return FALSE;
 
   for (i = 0; i < strlen(string); i++)
-    dest[i] = tolower(string[i]);
+    dest[i] = (char)tolower((int)string[i]);
 
   return TRUE;
 }
@@ -119,8 +119,10 @@ int silc_parse_userfqdn(const char *string,
   if (!user && !fqdn)
     return 0;
 
-  memset(user, 0, user_size);
-  memset(fqdn, 0, fqdn_size);
+  if (user)
+    memset(user, 0, user_size);
+  if (user)
+    memset(fqdn, 0, fqdn_size);
 
   if (!string)
     return 0;
@@ -236,7 +238,7 @@ char *silc_format(char *fmt, ...)
 
   memset(buf, 0, sizeof(buf));
   va_start(args, fmt);
-  vsnprintf(buf, sizeof(buf) - 1, fmt, args);
+  silc_vsnprintf(buf, sizeof(buf) - 1, fmt, args);
   va_end(args);
 
   return strdup(buf);
@@ -253,7 +255,7 @@ SilcUInt32 silc_hash_string(void *key, void *user_context)
   SilcUInt32 h = 0, g;
 
   while (*s != '\0') {
-    h = (h << 4) + tolower(*s);
+    h = (h << 4) + tolower((int)*s);
     if ((g = h & 0xf0000000)) {
       h = h ^ (g >> 24);
       h = h ^ g;
@@ -467,28 +469,41 @@ SilcBool silc_hash_public_key_compare(void *key1, void *key2,
 
 char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len)
 {
-  char fingerprint[64], *cp;
-  int i;
+  unsigned char *fingerprint, *cp;
+  unsigned int len, blocks, i;
+
+  if (!data || !data_len)
+    return NULL;
+
+  if (data_len >= 256)
+    data_len = 255;
+
+  /* Align and calculate total length */
+  len = ((data_len + 19) / 20) * 20;
+  blocks = (len / 10);
+  len = (len * 2) + ((blocks - 1) * 2) + (4 * blocks) + 2 + 1;
+
+  cp = fingerprint = silc_calloc(len, sizeof(*fingerprint));
+  if (!cp)
+    return NULL;
 
-  memset(fingerprint, 0, sizeof(fingerprint));
-  cp = fingerprint;
   for (i = 0; i < data_len; i++) {
-    snprintf(cp, sizeof(fingerprint), "%02X", data[i]);
+    silc_snprintf(cp, len, "%02X", data[i]);
     cp += 2;
+    len -= 2;
 
     if ((i + 1) % 2 == 0)
-      snprintf(cp++, sizeof(fingerprint), " ");
-
+      silc_snprintf(cp++, len--, " ");
     if ((i + 1) % 10 == 0)
-      snprintf(cp++, sizeof(fingerprint), " ");
+      silc_snprintf(cp++, len--, " ");
   }
   i--;
-  if ((i + 1) % 2 == 0)
-    cp[-2] = 0;
   if ((i + 1) % 10 == 0)
-    cp[-1] = 0;
+    *(--cp) = '\0';
+  if ((i + 1) % 2 == 0)
+    *(--cp) = '\0';
 
-  return strdup(fingerprint);
+  return fingerprint;
 }
 
 /* Return TRUE if the `data' is ASCII string. */
@@ -542,13 +557,18 @@ char *silc_get_input(const char *prompt, SilcBool echo_off)
     printf("%s", prompt);
     fflush(stdout);
 
+  read_again1:
     if ((read(fd, input, sizeof(input))) < 0) {
+      if (errno == EAGAIN || errno == EINTR)
+       goto read_again1;
       fprintf(stderr, "silc: %s\n", strerror(errno));
+      signal(SIGINT, SIG_DFL);
       tcsetattr(fd, TCSANOW, &to_old);
       return NULL;
     }
 
     if (strlen(input) <= 1) {
+      signal(SIGINT, SIG_DFL);
       tcsetattr(fd, TCSANOW, &to_old);
       return NULL;
     }
@@ -576,11 +596,19 @@ char *silc_get_input(const char *prompt, SilcBool echo_off)
     printf("%s", prompt);
     fflush(stdout);
 
+    signal(SIGINT, SIG_IGN);
+
+  read_again2:
     if ((read(fd, input, sizeof(input))) < 0) {
+      if (errno == EAGAIN || errno == EINTR)
+       goto read_again2;
       fprintf(stderr, "silc: %s\n", strerror(errno));
+      signal(SIGINT, SIG_DFL);
       return NULL;
     }
 
+    signal(SIGINT, SIG_DFL);
+
     if (strlen(input) <= 1)
       return NULL;
 
@@ -593,18 +621,3 @@ char *silc_get_input(const char *prompt, SilcBool echo_off)
   return NULL;
 #endif /* SILC_UNIX */
 }
-
-/* Copies va_list */
-
-void silc_va_copy(va_list dest, va_list src)
-{
-#if defined(HAVE_VA_COPY)
-  va_copy(dest, src);
-#elif defined(HAVE___VA_COPY)
-  __va_copy(dest, src);
-#elif defined(SILC_VA_COPY_ARRAY)
-  memmove(dest, src, sizeof(va_list));
-#else
-  dest = src;
-#endif
-}