-removing 2nd argument from GNUNET_CLIENT_disconnect as it was virtually always GNUNE...
[oweals/gnunet.git] / src / util / strings.c
index 2ed1a0da3655d165f1639215276e6a4c063aa4c2..f9e2d8da67ecd2522d820700cda4d96d67cd42c1 100644 (file)
@@ -31,6 +31,7 @@
 #endif
 #include "gnunet_common.h"
 #include "gnunet_strings_lib.h"
+#include <unicase.h>
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
@@ -293,7 +294,7 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
     { "'", 60 * 1000},
     { "h", 60 * 60 * 1000},
     { "d", 24 * 60 * 60 * 1000},
-    { "a", 31557600 /* year */ },
+    { "a", 31536000000LL /* year */ },
     { NULL, 0}
   };
   int ret;
@@ -401,6 +402,45 @@ GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
   return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
 }
 
+/**
+ * Convert the utf-8 input string to lowercase
+ * Output needs to be allocated appropriately
+ *
+ * @param input input string
+ * @param output output buffer
+ */
+void
+GNUNET_STRINGS_utf8_tolower(const char* input, char** output)
+{
+  uint8_t *tmp_in;
+  size_t len;
+
+  tmp_in = u8_tolower ((uint8_t*)input, strlen ((char *) input),
+                       NULL, UNINORM_NFD, NULL, &len);
+  memcpy(*output, tmp_in, len);
+  (*output)[len] = '\0';
+  free(tmp_in);
+}
+
+/**
+ * Convert the utf-8 input string to uppercase
+ * Output needs to be allocated appropriately
+ *
+ * @param input input string
+ * @param output output buffer
+ */
+void
+GNUNET_STRINGS_utf8_toupper(const char* input, char** output)
+{
+  uint8_t *tmp_in;
+  size_t len;
+
+  tmp_in = u8_toupper ((uint8_t*)input, strlen ((char *) input),
+                       NULL, UNINORM_NFD, NULL, &len);
+  memcpy(*output, tmp_in, len);
+  (*output)[len] = '\0';
+  free(tmp_in);
+}
 
 
 /**
@@ -643,10 +683,10 @@ getValue__ (unsigned char a)
  * @return pointer to the next byte in 'out' or NULL on error.
  */
 char *
-GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size_t out_size)
+GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out, size_t out_size)
 {
   /**
-   * 32 characters for encoding (GNUNET_CRYPTO_hash => 32 characters)
+   * 32 characters for encoding 
    */
   static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
   unsigned int wpos;
@@ -656,7 +696,11 @@ GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size
 
   GNUNET_assert (data != NULL);
   GNUNET_assert (out != NULL);
-  GNUNET_assert (out_size >= (((size*8) + ((size*8) % 5)) % 5));
+  if (out_size < (((size*8) + ((size*8) % 5)) % 5))
+  {
+    GNUNET_break (0);
+    return NULL;
+  }
   vbit = 0;
   wpos = 0;
   rpos = 0;
@@ -675,12 +719,18 @@ GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size, char *out, size
       vbit = 5;
     }
     if (wpos >= out_size)
+    {
+      GNUNET_break (0);
       return NULL;
+    }
     out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
     vbit -= 5;
   }
   if (wpos != out_size)
+  {
+    GNUNET_break (0);
     return NULL;
+  }
   GNUNET_assert (vbit == 0);
   return &out[wpos];
 }
@@ -967,7 +1017,7 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
   ret = inet_pton (AF_INET6, zt_addr, &r_buf->sin6_addr);
   if (ret <= 0)
     return GNUNET_SYSERR;
-  r_buf->sin6_port = htonl (port);
+  r_buf->sin6_port = htons (port);
   r_buf->sin6_family = AF_INET6;
   return GNUNET_OK;
 }
@@ -1003,8 +1053,10 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
   if (port > 65535)
     return GNUNET_SYSERR;
 
+
+
   r_buf->sin_family = AF_INET;
-  r_buf->sin_port = htonl (port);
+  r_buf->sin_port = htons (port);
   r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
       (temps[2] << 8) + temps[3]);
   return GNUNET_OK;