updates.
[silc.git] / lib / silcutil / silcutil.c
index 9d1ab991b5311cdb6157913aafa8da8b4a57b660..c37ec4e55df810e5c16206f1b94e3254016cc384 100644 (file)
@@ -32,7 +32,7 @@ int silc_file_open(const char *filename, int flags)
 {
   int fd;
 
-  fd = open(filename, flags);
+  fd = open(filename, flags, 0600);
 
   return fd;
 }
@@ -158,6 +158,24 @@ char *silc_file_readfile(const char *filename, uint32 *return_len)
   return buffer;
 }
 
+/* Returns files size. Returns 0 on error. */
+
+uint64 silc_file_size(const char *filename)
+{
+  int ret;
+  struct stat stats;
+
+#ifndef SILC_WIN32\r
+  ret = lstat(filename, &stats);
+#else\r
+  ret = stat(filename, &stats);\r
+#endif\r
+  if (ret < 0)\r
+    return 0;\r
+
+  return (uint64)stats.st_size;
+}
+
 /* Gets line from a buffer. Stops reading when a newline or EOF occurs.
    This doesn't remove the newline sign from the destination buffer. The
    argument begin is returned and should be passed again for the function. */
@@ -526,7 +544,7 @@ char *silc_id_render(void *id, uint16 type)
       if (server_id->ip.data_len > 4) {
 
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, server_id->ip.data);
+       memcpy(&ipv4.s_addr, server_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -547,7 +565,7 @@ char *silc_id_render(void *id, uint16 type)
       if (client_id->ip.data_len > 4) {
 
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, client_id->ip.data);
+       memcpy(&ipv4.s_addr, client_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -569,7 +587,7 @@ char *silc_id_render(void *id, uint16 type)
       if (channel_id->ip.data_len > 4) {
 
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, channel_id->ip.data);
+       memcpy(&ipv4.s_addr, channel_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -886,3 +904,31 @@ char *silc_client_chumode_char(uint32 mode)
 
   return strdup(string);
 }
+
+/* Creates fingerprint from data, usually used with SHA1 digests */
+
+char *silc_fingerprint(const unsigned char *data, uint32 data_len)
+{
+  char fingerprint[64], *cp;
+  int i;
+
+  memset(fingerprint, 0, sizeof(fingerprint));
+  cp = fingerprint;
+  for (i = 0; i < data_len; i++) {
+    snprintf(cp, sizeof(fingerprint), "%02X", data[i]);
+    cp += 2;
+    
+    if ((i + 1) % 2 == 0)
+      snprintf(cp++, sizeof(fingerprint), " ");
+
+    if ((i + 1) % 10 == 0)
+      snprintf(cp++, sizeof(fingerprint), " ");
+  }
+  i--;
+  if ((i + 1) % 2 == 0)
+    cp[-2] = 0;
+  if ((i + 1) % 10 == 0)
+    cp[-1] = 0;
+  
+  return strdup(fingerprint);
+}