]> git.meshlink.io Git - catta/blobdiff - avahi-common/util.c
* replace guchar and gint by AvahiProtocol, AvahiIfIndex at many places where it...
[catta] / avahi-common / util.c
index a41475a3b7242d11ef9eb7fa67423fd01cad8bad..8be7b8830b90e4c856f2ab93878c77cadd7d5e22 100644 (file)
@@ -79,25 +79,17 @@ static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
 
 gchar *avahi_normalize_name(const gchar *s) {
     gchar tmp[256];
-    gchar *n, *t;
     guint l;
     g_assert(s);
 
     unescape_uneeded(s, tmp, sizeof(tmp));
 
-    n = g_utf8_normalize(tmp, -1, G_NORMALIZE_DEFAULT);
+    l = strlen(tmp);
 
-    if ((l = strlen(n)) == 0) {
-        g_free(n);
-        return g_strdup(".");
-    }
-
-    if (n[l-1] == '.')
-        return n;
+    while (l > 0 && tmp[l-1] == '.')
+        tmp[--l] = 0;
 
-    t = g_strdup_printf("%s.", n);
-    g_free(n);
-    return t;
+    return g_strdup(tmp);
 }
 
 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
@@ -119,16 +111,56 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
     return 0;
 }
 
-glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
+AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
     g_assert(a);
     g_assert(b);
 
     if (avahi_timeval_compare(a, b) < 0)
-        return avahi_timeval_diff(b, a);
+        return - avahi_timeval_diff(b, a);
+
+    return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
+}
+
+GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
+    AvahiUsec u;
+    g_assert(a);
+
+    u = usec + a->tv_usec;
+
+    if (u < 0) {
+        a->tv_usec = (glong) (1000000 + (u % 1000000));
+        a->tv_sec += (glong) (-1 + (u / 1000000));
+    } else {
+        a->tv_usec = (glong) (u % 1000000);
+        a->tv_sec += (glong) (u / 1000000);
+    }
 
-    return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
+    return a;
 }
 
+AvahiUsec avahi_age(const GTimeVal *a) {
+    GTimeVal now;
+    
+    g_assert(a);
+
+    g_get_current_time(&now);
+
+    return avahi_timeval_diff(&now, a);
+}
+
+GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
+    g_assert(tv);
+
+    g_get_current_time(tv);
+
+    if (msec)
+        avahi_timeval_add(tv, (AvahiUsec) msec*1000);
+
+    if (jitter)
+        avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
+        
+    return tv;
+}
 
 gint avahi_set_cloexec(gint fd) {
     gint n;
@@ -176,30 +208,6 @@ gint avahi_wait_for_write(gint fd) {
     return 0;
 }
 
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
-    g_assert(tv);
-
-    g_get_current_time(tv);
-
-    if (msec)
-        g_time_val_add(tv, msec*1000);
-
-    if (jitter)
-        g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
-        
-    return tv;
-}
-
-glong avahi_age(const GTimeVal *a) {
-    GTimeVal now;
-    
-    g_assert(a);
-
-    g_get_current_time(&now);
-
-    return avahi_timeval_diff(&now, a);
-}
-
 /* Read the first label from string *name, unescape "\" and write it to dest */
 gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
     guint i = 0;
@@ -280,22 +288,6 @@ gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name,
     return r;
 }
 
-static gint utf8_strcasecmp(const gchar *a, const gchar *b) {
-    gchar *ta, *tb;
-    gint r;
-    
-    g_assert(a);
-    g_assert(b);
-
-    ta = g_utf8_casefold(a, -1);
-    tb = g_utf8_casefold(b, -1);
-    r = g_utf8_collate(ta, tb);
-    g_free(ta);
-    g_free(tb);
-
-    return r;
-}
-
 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
     g_assert(a);
     g_assert(b);
@@ -314,7 +306,7 @@ gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
         else if ((pa && !pb) || (!pa && pb))
             return FALSE;
 
-        if (utf8_strcasecmp(pa, pb))
+        if (g_ascii_strcasecmp(pa, pb))
             return FALSE;
     }
 
@@ -385,7 +377,7 @@ guint avahi_domain_hash(const gchar *s) {
     guint hash = 0;
     
     for (;;) {
-        gchar c[65], *n, *m;
+        gchar c[65], *m;
 
         if (!avahi_unescape_label(&s, c, sizeof(c)))
             return hash;
@@ -393,13 +385,9 @@ guint avahi_domain_hash(const gchar *s) {
         if (!c[0])
             continue;
         
-        n = g_utf8_normalize(c, -1, G_NORMALIZE_DEFAULT);
-        m = g_utf8_strdown(n, -1);
-
+        m = g_ascii_strdown(c, -1);
         hash += g_str_hash(m);
-
         g_free(m);
-        g_free(n);
     }
 }