Code auditing weekend results and fixes committing.
[silc.git] / lib / silcutil / silcutil.c
index 39b9ab1ad92eb7a79c5f297f8a70324833a9fad4..5511cd06c215216956542df49bdb1ce0c464526f 100644 (file)
  * These are general utility functions that doesn't belong to any specific
  * group of routines.
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.1  2000/09/13 17:45:16  priikone
- *     Splitted SILC core library. Core library includes now only
- *     SILC protocol specific stuff. New utility library includes the
- *     old stuff from core library that is more generic purpose stuff.
- *
- * Revision 1.4  2000/07/19 07:04:04  priikone
- *     Minor bug fix in silc_encode_pem
- *
- * Revision 1.3  2000/07/10 05:34:40  priikone
- *     Added PEM encoding/decoding functions.
- *
- * Revision 1.2  2000/07/05 06:06:12  priikone
- *     Added file saving with specific mode.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 
@@ -84,8 +63,10 @@ char *silc_file_read(const char *filename, int *return_len)
 
   close(fd);
   buffer[filelen] = EOF;
-  
-  *return_len = filelen;
+
+  if (return_len)
+    *return_len = filelen;
+
   return buffer;
 }
 
@@ -289,7 +270,7 @@ int silc_string_compare(char *string1, char *string2)
   return FALSE;
 }
 
-unsigned char pem_enc[64] =
+static unsigned char pem_enc[64] =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
 /* Encodes data into PEM encoding. Returns NULL terminated PEM encoded
@@ -476,7 +457,7 @@ int silc_parse_nickname(char *string, char **nickname, char **server,
     }
     
     if (server) {
-      *server = silc_calloc(strlen(string) - tlen, sizeof(char));
+      *server = silc_calloc((strlen(string) - tlen) + 1, sizeof(char));
       memcpy(*server, string + tlen + 1, strlen(string) - tlen - 1);
     }
   } else {
@@ -547,3 +528,75 @@ void silc_parse_command_line(unsigned char *buffer,
 
   *parsed_num = argc;
 }
+
+/* Formats arguments to a string and returns it after allocating memory
+   for it. It must be remembered to free it later. */
+
+char *silc_format(char *fmt, ...)
+{
+  va_list args;
+  static char buf[8192];
+
+  memset(buf, 0, sizeof(buf));
+  va_start(args, fmt);
+  vsnprintf(buf, sizeof(buf) - 1, fmt, args);
+  va_end(args);
+
+  return strdup(buf);
+}
+
+/* Renders ID to suitable to print for example to log file. */
+
+static char rid[256];
+
+char *silc_id_render(void *id, unsigned short type)
+{
+  char tmp[100];
+  unsigned char tmps[2];
+
+  memset(rid, 0, sizeof(rid));
+  switch(type) {
+  case SILC_ID_SERVER:
+    {
+      SilcServerID *server_id = (SilcServerID *)id;
+      strcat(rid, inet_ntoa(server_id->ip));
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), ",%d,", ntohs(server_id->port));
+      strcat(rid, tmp);
+      SILC_PUT16_MSB(server_id->rnd, tmps);
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);
+      strcat(rid, tmp);
+    }
+    break;
+  case SILC_ID_CLIENT:
+    {
+      SilcClientID *client_id = (SilcClientID *)id;
+      strcat(rid, inet_ntoa(client_id->ip));
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), ",%02x,", client_id->rnd);
+      strcat(rid, tmp);
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), "[%02x %02x %02x %02x...]", 
+              client_id->hash[0], client_id->hash[1],
+              client_id->hash[2], client_id->hash[3]);
+      strcat(rid, tmp);
+    }
+    break;
+  case SILC_ID_CHANNEL:
+    {
+      SilcChannelID *channel_id = (SilcChannelID *)id;
+      strcat(rid, inet_ntoa(channel_id->ip));
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), ",%d,", ntohs(channel_id->port));
+      strcat(rid, tmp);
+      SILC_PUT16_MSB(channel_id->rnd, tmps);
+      memset(tmp, 0, sizeof(tmp));
+      snprintf(tmp, sizeof(tmp), "[%02x %02x]", tmps[0], tmps[1]);
+      strcat(rid, tmp);
+    }
+    break;
+  }
+
+  return rid;
+}