summaryrefslogtreecommitdiffstats
path: root/avahi-common
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-08-11 23:45:42 +0000
committerLennart Poettering <lennart@poettering.net>2005-08-11 23:45:42 +0000
commite63a65b3955b173a3e8d6b78c6377a518a9922d6 (patch)
tree9665358bbdd3c228cfeae6b3cbe27e29ac53e506 /avahi-common
parentc3d36ee186b5fb24480590080215f7e14ee0f6a6 (diff)
* drop glib from avahi-common
* add new module with avahi_malloc() and friends git-svn-id: file:///home/lennart/svn/public/avahi/trunk@298 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-common')
-rw-r--r--avahi-common/Makefile.am15
-rw-r--r--avahi-common/address.c70
-rw-r--r--avahi-common/address.h31
-rw-r--r--avahi-common/alternative-test.c13
-rw-r--r--avahi-common/alternative.c56
-rw-r--r--avahi-common/alternative.h6
-rw-r--r--avahi-common/domain-test.c25
-rw-r--r--avahi-common/llist.h12
-rw-r--r--avahi-common/malloc.c171
-rw-r--r--avahi-common/malloc.h75
-rw-r--r--avahi-common/strlst-test.c23
-rw-r--r--avahi-common/strlst.c86
-rw-r--r--avahi-common/strlst.h30
-rw-r--r--avahi-common/timeval-test.c34
-rw-r--r--avahi-common/util.c282
-rw-r--r--avahi-common/util.h52
16 files changed, 665 insertions, 316 deletions
diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am
index ad8d1a7..3b025be 100644
--- a/avahi-common/Makefile.am
+++ b/avahi-common/Makefile.am
@@ -34,7 +34,8 @@ avahi_commoninclude_HEADERS = \
alternative.h \
util.h \
cdecl.h \
- defs.h
+ defs.h \
+ malloc.h
if ENABLE_DBUS
avahi_commoninclude_HEADERS += dbus.h
@@ -52,34 +53,40 @@ lib_LTLIBRARIES = \
libavahi-common.la
libavahi_common_la_SOURCES = \
+ malloc.c malloc.h \
address.c address.h \
- strlst.c strlst.h \
alternative.c alternative.h \
- util.c util.h \
- error.c error.h
+ error.c error.h \
+ strlst.c strlst.h \
+ util.c util.h
libavahi_common_la_CFLAGS = $(AM_CFLAGS)
libavahi_common_la_LIBADD = $(AM_LDADD)
strlst_test_SOURCES = \
strlst.c strlst.h \
+ malloc.c malloc.h \
strlst-test.c
strlst_test_CFLAGS = $(AM_CFLAGS)
strlst_test_LDADD = $(AM_LDADD)
alternative_test_SOURCES = \
alternative.c alternative.h \
+ malloc.c malloc.h \
+ util.c util.h \
alternative-test.c
alternative_test_CFLAGS = $(AM_CFLAGS)
alternative_test_LDADD = $(AM_LDADD)
domain_test_SOURCES = \
util.c util.h \
+ malloc.c malloc.h \
domain-test.c
domain_test_CFLAGS = $(AM_CFLAGS)
domain_test_LDADD = $(AM_LDADD)
timeval_test_SOURCES = \
util.c util.h \
+ malloc.c malloc.h \
timeval-test.c
timeval_test_CFLAGS = $(AM_CFLAGS)
timeval_test_LDADD = $(AM_LDADD)
diff --git a/avahi-common/address.c b/avahi-common/address.c
index 8f1848e..c638b41 100644
--- a/avahi-common/address.c
+++ b/avahi-common/address.c
@@ -27,11 +27,14 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
+#include <assert.h>
#include "address.h"
+#include "util.h"
+#include "malloc.h"
-guint avahi_address_get_size(const AvahiAddress *a) {
- g_assert(a);
+size_t avahi_address_get_size(const AvahiAddress *a) {
+ assert(a);
if (a->family == AVAHI_PROTO_INET)
return 4;
@@ -41,9 +44,9 @@ guint avahi_address_get_size(const AvahiAddress *a) {
return 0;
}
-gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
- g_assert(a);
- g_assert(b);
+int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
+ assert(a);
+ assert(b);
if (a->family != b->family)
return -1;
@@ -51,23 +54,24 @@ gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b) {
return memcmp(a->data.data, b->data.data, avahi_address_get_size(a));
}
-gchar *avahi_address_snprint(char *s, guint length, const AvahiAddress *a) {
- g_assert(s);
- g_assert(length);
- g_assert(a);
- return (gchar*) inet_ntop(a->family, a->data.data, s, length);
+char *avahi_address_snprint(char *s, size_t length, const AvahiAddress *a) {
+ assert(s);
+ assert(length);
+ assert(a);
+
+ return (char*) inet_ntop(a->family, a->data.data, s, length);
}
-gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
- guint32 n = ntohl(a->address);
- g_assert(a);
+char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a) {
+ uint32_t n = ntohl(a->address);
+ assert(a);
- return g_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
+ return avahi_strdup_printf("%u.%u.%u.%u.in-addr.arpa", n & 0xFF, (n >> 8) & 0xFF, (n >> 16) & 0xFF, n >> 24);
}
-static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *suffix) {
+static char *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const char *suffix) {
- return g_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s",
+ return avahi_strdup_printf("%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%s",
a->address[15] & 0xF,
a->address[15] >> 4,
a->address[14] & 0xF,
@@ -103,17 +107,17 @@ static gchar *reverse_lookup_name_ipv6(const AvahiIPv6Address *a, const gchar *s
suffix);
}
-gchar *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
+char *avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a) {
return reverse_lookup_name_ipv6(a, "ip6.arpa");
}
-gchar *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
+char *avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a) {
return reverse_lookup_name_ipv6(a, "ip6.int");
}
-AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAddress *ret_addr) {
- g_assert(ret_addr);
- g_assert(s);
+AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol family, AvahiAddress *ret_addr) {
+ assert(ret_addr);
+ assert(s);
if (family == AVAHI_PROTO_UNSPEC) {
if (inet_pton(AF_INET, s, ret_addr->data.data) <= 0) {
@@ -134,10 +138,10 @@ AvahiAddress *avahi_address_parse(const gchar *s, AvahiProtocol family, AvahiAdd
}
AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr) {
- g_assert(sa);
- g_assert(ret_addr);
+ assert(sa);
+ assert(ret_addr);
- g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
+ assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
ret_addr->family = sa->sa_family;
@@ -149,10 +153,10 @@ AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddres
return ret_addr;
}
-guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
- g_assert(sa);
+uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa) {
+ assert(sa);
- g_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
+ assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
if (sa->sa_family == AF_INET)
return ntohs(((const struct sockaddr_in*) sa)->sin_port);
@@ -160,16 +164,18 @@ guint16 avahi_port_from_sockaddr(const struct sockaddr* sa) {
return ntohs(((const struct sockaddr_in6*) sa)->sin6_port);
}
-gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
- static const guint8 ipv4_in_ipv6[] = {
+int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a) {
+
+ static const uint8_t ipv4_in_ipv6[] = {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
- 0xFF, 0xFF, 0xFF, 0xFF };
+ 0xFF, 0xFF, 0xFF, 0xFF
+ };
- g_assert(a);
+ assert(a);
if (a->family != AVAHI_PROTO_INET6)
- return FALSE;
+ return 0;
return memcmp(a->data.ipv6.address, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0;
}
diff --git a/avahi-common/address.h b/avahi-common/address.h
index 3b34551..8056ac9 100644
--- a/avahi-common/address.h
+++ b/avahi-common/address.h
@@ -23,7 +23,8 @@
***/
#include <sys/socket.h>
-#include <glib.h>
+#include <inttypes.h>
+
#include <avahi-common/cdecl.h>
/** \file address.h Defintions and functions to manipulate IP addresses. */
@@ -31,10 +32,10 @@
AVAHI_C_DECL_BEGIN
/** Protocol family specification, takes the values AVAHI_INET, AVAHI_INET6, AVAHI_UNSPEC */
-typedef guchar AvahiProtocol;
+typedef unsigned char AvahiProtocol;
/** Numeric network interface index. Takes OS dependent values and the special constant AVAHI_IF_UNSPEC */
-typedef gint AvahiIfIndex;
+typedef int AvahiIfIndex;
/** Values for AvahiProtocol */
enum {
@@ -50,12 +51,12 @@ enum {
/** An IPv4 address */
typedef struct {
- guint32 address; /**< Address data in network byte order. */
+ uint32_t address; /**< Address data in network byte order. */
} AvahiIPv4Address;
/** An IPv6 address */
typedef struct {
- guint8 address[16]; /**< Address data */
+ uint8_t address[16]; /**< Address data */
} AvahiIPv6Address;
/** Protocol (address family) independent address structure */
@@ -65,18 +66,18 @@ typedef struct {
union {
AvahiIPv6Address ipv6; /** Address when IPv6 */
AvahiIPv4Address ipv4; /** Address when IPv4 */
- guint8 data[1]; /** Type independant data field */
+ uint8_t data[1]; /** Type independant data field */
} data;
} AvahiAddress;
/** Return the address data size of the specified address. (4 for IPv4, 16 for IPv6) */
-guint avahi_address_get_size(const AvahiAddress *a);
+size_t avahi_address_get_size(const AvahiAddress *a);
/** Compare two addresses. Returns 0 when equal, a negative value when a < b, a positive value when a > b. */
-gint avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b);
+int avahi_address_cmp(const AvahiAddress *a, const AvahiAddress *b);
/** Convert the specified address *a to a human readable character string */
-gchar *avahi_address_snprint(char *ret_s, guint length, const AvahiAddress *a);
+char *avahi_address_snprint(char *ret_s, size_t length, const AvahiAddress *a);
/** Convert the specifeid human readable character string to an
* address structure. Set af to AVAHI_UNSPEC for automatic address
@@ -87,20 +88,20 @@ AvahiAddress *avahi_address_parse(const char *s, AvahiProtocol af, AvahiAddress
AvahiAddress *avahi_address_from_sockaddr(const struct sockaddr* sa, AvahiAddress *ret_addr);
/** Return the port number of a sockaddr structure (either IPv4 or IPv6) */
-guint16 avahi_port_from_sockaddr(const struct sockaddr* sa);
+uint16_t avahi_port_from_sockaddr(const struct sockaddr* sa);
/** Generate the DNS reverse lookup name for an IPv4 address. g_free() the result! */
-gchar* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a);
+char* avahi_reverse_lookup_name_ipv4(const AvahiIPv4Address *a);
/** Generate the modern DNS reverse lookup name for an IPv6 address, ending in ipv6.arpa. g_free() the result! */
-gchar* avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a);
+char* avahi_reverse_lookup_name_ipv6_arpa(const AvahiIPv6Address *a);
/** Generate the historic DNS reverse lookup name for an IPv6 address, ending in ipv6.int. g_free() the result! */
-gchar* avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a);
+char* avahi_reverse_lookup_name_ipv6_int(const AvahiIPv6Address *a);
/** Check whether the specified IPv6 address is in fact an
- * encapsulated IPv4 address */
-gboolean avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a);
+ * encapsulated IPv4 address, returns 1 if yes, 0 otherwise */
+int avahi_address_is_ipv4_in_ipv6(const AvahiAddress *a);
AVAHI_C_DECL_END
diff --git a/avahi-common/alternative-test.c b/avahi-common/alternative-test.c
index 9e26d24..7f0223a 100644
--- a/avahi-common/alternative-test.c
+++ b/avahi-common/alternative-test.c
@@ -26,23 +26,24 @@
#include <stdio.h>
#include "alternative.h"
+#include "malloc.h"
int main(int argc, char *argv[]) {
- gchar *r = NULL;
- gint i, k;
+ char *r = NULL;
+ int i, k;
for (k = 0; k < 2; k++) {
for (i = 0; i < 20; i++) {
- gchar *n;
+ char *n;
- n = i == 0 ? g_strdup("gurke") : (k ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r));
- g_free(r);
+ n = i == 0 ? avahi_strdup("gurke") : (k ? avahi_alternative_service_name(r) : avahi_alternative_host_name(r));
+ avahi_free(r);
r = n;
printf("%s\n", r);
}
}
- g_free(r);
+ avahi_free(r);
}
diff --git a/avahi-common/alternative.c b/avahi-common/alternative.c
index b1b52de..53a1e6c 100644
--- a/avahi-common/alternative.c
+++ b/avahi-common/alternative.c
@@ -26,38 +26,47 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
+#include <assert.h>
#include "alternative.h"
+#include "malloc.h"
+#include "util.h"
-gchar * avahi_alternative_host_name(const gchar *s) {
- const gchar *p, *e = NULL;
- gchar *c, *r;
- gint n;
+char * avahi_alternative_host_name(const char *s) {
+ const char *p, *e;
+ char *r;
- g_assert(s);
+ assert(s);
+
+ e = s;
for (p = s; *p; p++)
if (!isdigit(*p))
e = p+1;
- if (e && *e)
- n = atoi(e)+1;
- else
- n = 2;
+ if (*e) {
+ char *c;
+
+ if (!(c = avahi_strndup(s, e-s)))
+ return NULL;
- c = e ? g_strndup(s, e-s) : g_strdup(s);
- r = g_strdup_printf("%s%i", c, n);
- g_free(c);
+ r = avahi_strdup_printf("%s%i", c, atoi(e)+1);
+ avahi_free(c);
+
+ } else
+ r = avahi_strdup_printf("%s2", s);
return r;
}
-gchar *avahi_alternative_service_name(const gchar *s) {
- const gchar *e;
- g_assert(s);
+char *avahi_alternative_service_name(const char *s) {
+ const char *e;
+ char *r;
+
+ assert(s);
if ((e = strstr(s, " #"))) {
- const gchar *n, *p;
+ const char *n, *p;
e += 2;
while ((n = strstr(e, " #")))
@@ -71,10 +80,15 @@ gchar *avahi_alternative_service_name(const gchar *s) {
}
if (e) {
- gchar *r, *c = g_strndup(s, e-s);
- r = g_strdup_printf("%s%i", c, atoi(e)+1);
- g_free(c);
- return r;
+ char *c;
+
+ if (!(c = avahi_strndup(s, e-s)))
+ return NULL;
+
+ r = avahi_strdup_printf("%s%i", c, atoi(e)+1);
+ avahi_free(c);
} else
- return g_strdup_printf("%s #2", s);
+ r = avahi_strdup_printf("%s #2", s);
+
+ return r;
}
diff --git a/avahi-common/alternative.h b/avahi-common/alternative.h
index c1bf8cb..3d7b2fe 100644
--- a/avahi-common/alternative.h
+++ b/avahi-common/alternative.h
@@ -22,8 +22,6 @@
USA.
***/
-#include <glib.h>
-
#include <avahi-common/cdecl.h>
/** \file alternative.h Functions to find alternative names for hosts and services in the case of name collision */
@@ -34,13 +32,13 @@ AVAHI_C_DECL_BEGIN
* original host name, "2" is appended, Afterwards the number is
* increased on each call. (i.e. "foo" becomes "foo2" becomes "foo3"
* and so on.) g_free() the result. */
-gchar *avahi_alternative_host_name(const gchar *s);
+char *avahi_alternative_host_name(const char *s);
/** Find an alternative for the specified service name. If called with
an original service name, " #2" is appended. Afterwards the number
is increased on each call (i.e. "foo" becomes "foo #2" becomes
"foo #3" and so on.) g_free() the result. */
-gchar *avahi_alternative_service_name(const gchar *s);
+char *avahi_alternative_service_name(const char *s);
AVAHI_C_DECL_END
diff --git a/avahi-common/domain-test.c b/avahi-common/domain-test.c
index 240f960..a9ad0b6 100644
--- a/avahi-common/domain-test.c
+++ b/avahi-common/domain-test.c
@@ -23,26 +23,29 @@
#include <config.h>
#endif
+#include <stdio.h>
+
#include "util.h"
+#include "malloc.h"
int main(int argc, char *argv[]) {
- gchar *s;
+ char *s;
- g_message("host name: %s", s = avahi_get_host_name());
- g_free(s);
+ printf("host name: %s\n", s = avahi_get_host_name());
+ avahi_free(s);
- g_message("%s", s = avahi_normalize_name("foo.foo."));
- g_free(s);
+ printf("%s\n", s = avahi_normalize_name("foo.foo."));
+ avahi_free(s);
- g_message("%s", s = avahi_normalize_name("\\f\\o\\\\o\\..\\f\\ \\o\\o."));
- g_free(s);
+ printf("%s\n", s = avahi_normalize_name("\\f\\o\\\\o\\..\\f\\ \\o\\o."));
+ avahi_free(s);
- g_message("%i", avahi_domain_equal("\\aaa bbb\\.cccc\\\\.dee.fff.", "aaa\\ bbb\\.cccc\\\\.dee.fff"));
- g_message("%i", avahi_domain_equal("\\A", "a"));
+ printf("%i\n", avahi_domain_equal("\\aaa bbb\\.cccc\\\\.dee.fff.", "aaa\\ bbb\\.cccc\\\\.dee.fff"));
+ printf("%i\n", avahi_domain_equal("\\A", "a"));
- g_message("%i", avahi_domain_equal("a", "aaa"));
+ printf("%i\n", avahi_domain_equal("a", "aaa"));
- g_message("%u = %u", avahi_domain_hash("\\Aaaab\\\\."), avahi_domain_hash("aaaa\\b\\\\"));
+ printf("%u = %u\n", avahi_domain_hash("\\Aaaab\\\\."), avahi_domain_hash("aaaa\\b\\\\"));
return 0;
}
diff --git a/avahi-common/llist.h b/avahi-common/llist.h
index 7c28f31..4f48b55 100644
--- a/avahi-common/llist.h
+++ b/avahi-common/llist.h
@@ -22,10 +22,10 @@
USA.
***/
-#include <glib.h>
-
/* Some macros for maintaining doubly linked lists */
+#include <assert.h>
+
/* The head of the linked list. Use this in the structure that shall
* contain the head of the linked list */
#define AVAHI_LLIST_HEAD(t,name) t *name
@@ -39,14 +39,14 @@
/* Initialize a list item */
#define AVAHI_LLIST_INIT(t,name,item) do { \
t *_item = (item); \
- g_assert(_item); \
+ assert(_item); \
_item->name##_prev = _item->name##_next = NULL; \
} while(0)
/* Prepend an item to the list */
#define AVAHI_LLIST_PREPEND(t,name,head,item) do { \
t **_head = &(head), *_item = (item); \
- g_assert(_item); \
+ assert(_item); \
if ((_item->name##_next = *_head)) \
_item->name##_next->name##_prev = _item; \
_item->name##_prev = NULL; \
@@ -56,13 +56,13 @@
/* Remove an item from the list */
#define AVAHI_LLIST_REMOVE(t,name,head,item) do { \
t **_head = &(head), *_item = (item); \
- g_assert(_item); \
+ assert(_item); \
if (_item->name##_next) \
_item->name##_next->name##_prev = _item->name##_prev; \
if (_item->name##_prev) \
_item->name##_prev->name##_next = _item->name##_next; \
else {\
- g_assert(*_head == _item); \
+ assert(*_head == _item); \
*_head = _item->name##_next; \
} \
_item->name##_next = _item->name##_prev = NULL; \
diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c
new file mode 100644
index 0000000..19e1e59
--- /dev/null
+++ b/avahi-common/malloc.c
@@ -0,0 +1,171 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <stdio.h>
+
+#include "malloc.h"
+
+static const AvahiAllocator *allocator = NULL;
+
+void *avahi_malloc(size_t size) {
+
+ if (size <= 0)
+ return NULL;
+
+ if (!allocator)
+ return malloc(size);
+
+ assert(allocator->malloc);
+ return allocator->malloc(size);
+}
+
+void *avahi_malloc0(size_t size) {
+ void *p;
+
+ if (size <= 0)
+ return NULL;
+
+ if (!allocator)
+ return calloc(1, size);
+
+ if (allocator->calloc)
+ return allocator->calloc(1, size);
+
+ assert(allocator->malloc);
+ if ((p = allocator->malloc(size)))
+ memset(p, 0, size);
+
+ return p;
+}
+
+void avahi_free(void *p) {
+
+ if (!p)
+ return;
+
+ if (!allocator) {
+ free(p);
+ return;
+ }
+
+ assert(allocator->free);
+ allocator->free(p);
+}
+
+void *avahi_realloc(void *p, size_t size) {
+
+ if (!allocator)
+ return realloc(p, size);
+
+ assert(allocator->realloc);
+ return allocator->realloc(p, size);
+}
+
+char *avahi_strdup(const char *s) {
+ char *r;
+ size_t size;
+
+ if (!s)
+ return NULL;
+
+ size = strlen(s);
+ if (!(r = avahi_malloc(size+1)))
+ return NULL;
+
+ memcpy(r, s, size+1);
+ return r;
+}
+
+char *avahi_strndup(const char *s, size_t max) {
+ char *r;
+ size_t size;
+
+ if (!s)
+ return NULL;
+
+ size = strlen(s);
+
+ if (size > max)
+ size = max;
+
+ if (!(r = avahi_malloc(size+1)))
+ return NULL;
+
+ memcpy(r, s, size);
+ r[size] = 0;
+ return r;
+}
+
+/* Change the allocator */
+void avahi_set_allocator(const AvahiAllocator *a) {
+ allocator = a;
+}
+
+char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
+ size_t len = 100;
+ char *buf;
+
+ assert(fmt);
+
+ if (!(buf = avahi_malloc(len)))
+ return NULL;
+
+ for (;;) {
+ int n;
+ char *nbuf;
+
+ n = vsnprintf(buf, len, fmt, ap);
+
+ if (n >= 0 && n < (int) len)
+ return buf;
+
+ if (n >= 0)
+ len = n+1;
+ else
+ len *= 2;
+
+ if (!(nbuf = avahi_realloc(buf, len))) {
+ avahi_free(buf);
+ return NULL;
+ }
+
+ buf = nbuf;
+ }
+}
+
+char *avahi_strdup_printf(const char *fmt, ... ) {
+ char *s;
+ va_list ap;
+
+ va_start(ap, fmt);
+ s = avahi_strdup_vprintf(fmt, ap);
+ va_end(ap);
+
+ return s;
+}
+
diff --git a/avahi-common/malloc.h b/avahi-common/malloc.h
new file mode 100644
index 0000000..11ac9cf
--- /dev/null
+++ b/avahi-common/malloc.h
@@ -0,0 +1,75 @@
+#ifndef foomallochfoo
+#define foomallochfoo
+
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <sys/types.h>
+#include <stdarg.h>
+
+/** Allocate some memory, just like the libc malloc() */
+void *avahi_malloc(size_t size);
+
+/** Similar to avahi_malloc() but set the memory to zero */
+void *avahi_malloc0(size_t size);
+
+/** Free some memory */
+void avahi_free(void *p);
+
+/** Similar to libc's realloc() */
+void *avahi_realloc(void *p, size_t size);
+
+/** Allocate n new structures of the specified type. */
+#define avahi_new(type, n) (type*) avahi_malloc(n*sizeof(type))
+
+/** Same as avahi_new() but set the memory to zero */
+#define avahi_new0(type, n) (type*) avahi_malloc0(n*sizeof(type))
+
+/* Just like libc's strdup() */
+char *avahi_strdup(const char *s);
+
+/* Just like libc's strndup() */
+char *avahi_strndup(const char *s, size_t l);
+
+/** Wraps allocator functions */
+typedef struct AvahiAllocator AvahiAllocator;
+struct AvahiAllocator {
+ void* (*malloc)(size_t size);
+ void (*free)(void *p);
+ void* (*realloc)(void *p, size_t size);
+ void* (*calloc)(size_t nmemb, size_t size); /**< May be NULL */
+};
+
+/* Change the allocator. May be NULL to return to default (libc)
+ * allocators. The structure is not copied! */
+void avahi_set_allocator(const AvahiAllocator *a);
+
+
+char *avahi_strdup_vprintf(const char *fmt, va_list ap);
+
+#ifdef __GNUC__
+char *avahi_strdup_printf(const char *fmt, ... ) __attribute__ ((format(printf, 1, 2)));
+#else
+char *avahi_strdup_printf(const char *fmt, ... );
+#endif
+
+
+#endif
diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c
index 58ca60d..12aa623 100644
--- a/avahi-common/strlst-test.c
+++ b/avahi-common/strlst-test.c
@@ -23,16 +23,17 @@
#include <config.h>
#endif
-#include <glib.h>
#include <stdio.h>
+#include <assert.h>
#include "strlst.h"
+#include "malloc.h"
int main(int argc, char *argv[]) {
- gchar *t;
- guint8 data[1024];
+ char *t;
+ uint8_t data[1024];
AvahiStringList *a = NULL, *b;
- guint size, n;
+ size_t size, n;
a = avahi_string_list_new("prefix", "a", "b", NULL);
@@ -40,18 +41,18 @@ int main(int argc, char *argv[]) {
a = avahi_string_list_add(a, "foo");
a = avahi_string_list_add(a, "bar");
a = avahi_string_list_add(a, "quux");
- a = avahi_string_list_add_arbitrary(a, (const guint8*) "null\0null", 9);
+ a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9);
a = avahi_string_list_add(a, "end");
t = avahi_string_list_to_string(a);
printf("--%s--\n", t);
- g_free(t);
+ avahi_free(t);
size = avahi_string_list_serialize(a, data, sizeof(data));
printf("%u\n", size);
- for (t = (gchar*) data, n = 0; n < size; n++, t++) {
+ for (t = (char*) data, n = 0; n < size; n++, t++) {
if (*t <= 32)
printf("(%u)", *t);
else
@@ -62,21 +63,21 @@ int main(int argc, char *argv[]) {
b = avahi_string_list_parse(data, size);
- g_assert(avahi_string_list_equal(a, b));
+ assert(avahi_string_list_equal(a, b));
t = avahi_string_list_to_string(b);
printf("--%s--\n", t);
- g_free(t);
+ avahi_free(t);
avahi_string_list_free(b);
b = avahi_string_list_copy(a);
- g_assert(avahi_string_list_equal(a, b));
+ assert(avahi_string_list_equal(a, b));
t = avahi_string_list_to_string(b);
printf("--%s--\n", t);
- g_free(t);
+ avahi_free(t);
avahi_string_list_free(a);
avahi_string_list_free(b);
diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c
index 46ed852..8a05e02 100644
--- a/avahi-common/strlst.c
+++ b/avahi-common/strlst.c
@@ -25,25 +25,30 @@
#include <string.h>
#include <stdarg.h>
+#include <assert.h>
#include "strlst.h"
+#include "malloc.h"
-AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, guint size) {
+AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
AvahiStringList *n;
- n = g_malloc(sizeof(AvahiStringList) + size);
+ if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
+ return NULL;
+
n->next = l;
n->size = size;
return n;
}
-AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint8*text, guint size) {
+AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
AvahiStringList *n;
- g_assert(text);
+ assert(text);
- n = avahi_string_list_add_anonymous(l, size);
+ if (!(n = avahi_string_list_add_anonymous(l, size)))
+ return NULL;
if (size > 0)
memcpy(n->text, text, size);
@@ -51,20 +56,21 @@ AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint
return n;
}
-AvahiStringList *avahi_string_list_add(AvahiStringList *l, const gchar *text) {
- g_assert(text);
+AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
+ assert(text);
- return avahi_string_list_add_arbitrary(l, (const guint8*) text, strlen(text));
+ return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
}
-AvahiStringList *avahi_string_list_parse(gconstpointer data, guint size) {
+AvahiStringList *avahi_string_list_parse(const void* data, size_t size) {
AvahiStringList *r = NULL;
- const guint8 *c;
- g_assert(data);
+ const uint8_t *c;
+
+ assert(data);
c = data;
for (;;) {
- guint k;
+ size_t k;
if (size < 1)
break;
@@ -84,7 +90,7 @@ void avahi_string_list_free(AvahiStringList *l) {
while (l) {
n = l->next;
- g_free(l);
+ avahi_free(l);
l = n;
}
}
@@ -102,10 +108,10 @@ AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
return r;
}
-gchar* avahi_string_list_to_string(AvahiStringList *l) {
+char* avahi_string_list_to_string(AvahiStringList *l) {
AvahiStringList *n;
- guint s = 0;
- gchar *t, *e;
+ size_t s = 0;
+ char *t, *e;
l = avahi_string_list_reverse(l);
@@ -116,19 +122,22 @@ gchar* avahi_string_list_to_string(AvahiStringList *l) {
s += n->size+2;
}
- t = e = g_new(gchar, s+1);
+ if (!(t = e = avahi_new(char, s+1))) {
+ l = avahi_string_list_reverse(l);
+ return NULL;
+ }
for (n = l; n; n = n->next) {
if (n != l)
*(e++) = ' ';
*(e++) = '"';
- strncpy(e, (gchar*) n->text, n->size);
+ strncpy(e, (char*) n->text, n->size);
e[n->size] = 0;
e = strchr(e, 0);
*(e++) = '"';
- g_assert(e);
+ assert(e);
}
l = avahi_string_list_reverse(l);
@@ -138,20 +147,20 @@ gchar* avahi_string_list_to_string(AvahiStringList *l) {
return t;
}
-guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size) {
- guint used = 0;
+size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
+ size_t used = 0;
if (data) {
- guint8 *c;
+ uint8_t *c;
AvahiStringList *n;
- g_assert(data);
+ assert(data);
l = avahi_string_list_reverse(l);
c = data;
for (n = l; n; n = n->next) {
- guint k;
+ size_t k;
if (size < 1)
break;
@@ -174,7 +183,7 @@ guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size)
AvahiStringList *n;
for (n = l; n; n = n->next) {
- guint k;
+ size_t k;
k = n->size;
if (k > 255)
@@ -187,20 +196,20 @@ guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size)
return used;
}
-gboolean avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
+int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
for (;;) {
if (!a && !b)
- return TRUE;
+ return 1;
if (!a || !b)
- return FALSE;
+ return 0;
if (a->size != b->size)
- return FALSE;
+ return 0;
if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
- return FALSE;
+ return 0;
a = a->next;
b = b->next;
@@ -218,16 +227,15 @@ AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
}
AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
- const gchar *txt;
+ const char *txt;
- while ((txt = va_arg(va, const gchar*)))
+ while ((txt = va_arg(va, const char*)))
r = avahi_string_list_add(r, txt);
return r;
}
-
-AvahiStringList *avahi_string_list_new(const gchar *txt, ...) {
+AvahiStringList *avahi_string_list_new(const char *txt, ...) {
va_list va;
AvahiStringList *r = NULL;
@@ -255,11 +263,11 @@ AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
return avahi_string_list_reverse(r);
}
-AvahiStringList *avahi_string_list_new_from_array(const gchar *array[], gint length) {
+AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
AvahiStringList *r = NULL;
- gint i;
+ int i;
- g_assert(array);
+ assert(array);
for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
r = avahi_string_list_add(r, array[i]);
@@ -267,8 +275,8 @@ AvahiStringList *avahi_string_list_new_from_array(const gchar *array[], gint len
return r;
}
-guint avahi_string_list_length(const AvahiStringList *l) {
- guint n = 0;
+unsigned avahi_string_list_length(const AvahiStringList *l) {
+ unsigned n = 0;
for (; l; l = l->next)
n++;
diff --git a/avahi-common/strlst.h b/avahi-common/strlst.h
index a4a4202..78cffb4 100644
--- a/avahi-common/strlst.h
+++ b/avahi-common/strlst.h
@@ -22,7 +22,10 @@
USA.
***/
-#include <glib.h>
+#include <sys/types.h>
+#include <inttypes.h>
+#include <stdarg.h>
+
#include <avahi-common/cdecl.h>
/** \file strlst.h Implementation of a data type to store lists of strings */
@@ -37,14 +40,14 @@ AVAHI_C_DECL_BEGIN
* primarily for storing DNS TXT record data. */
typedef struct AvahiStringList {
struct AvahiStringList *next; /**< Pointe to the next linked list element */
- guint size; /**< Size of text[] */
- guint8 text[1]; /**< Character data */
+ size_t size; /**< Size of text[] */
+ uint8_t text[1]; /**< Character data */
} AvahiStringList;
/** Create a new string list by taking a variable list of NUL
* terminated strings. The strings are copied using g_strdup(). The
* argument list must be terminated by a NULL pointer. */
-AvahiStringList *avahi_string_list_new(const gchar *txt, ...);
+AvahiStringList *avahi_string_list_new(const char *txt, ...);
/** Same as avahi_string_list_new() but pass a va_list structure */
AvahiStringList *avahi_string_list_new_va(va_list va);
@@ -52,7 +55,7 @@ AvahiStringList *avahi_string_list_new_va(va_list va);
/** Create a new string list from a string array. The strings are
* copied using g_strdup(). length should contain the length of the
* array, or -1 if the array is NULL terminated*/
-AvahiStringList *avahi_string_list_new_from_array(const gchar **array, gint length);
+AvahiStringList *avahi_string_list_new_from_array(const char **array, int length);
/** Free a string list */
void avahi_string_list_free(AvahiStringList *l);
@@ -60,18 +63,18 @@ void avahi_string_list_free(AvahiStringList *l);
/** Append a NUL terminated string to the specified string list. The
* passed string is copied using g_strdup(). Returns the new list
* start. */
-AvahiStringList *avahi_string_list_add(AvahiStringList *l, const gchar *text);
+AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text);
/** Append an arbitrary length byte string to the list. Returns the
* new list start. */
-AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const guint8 *text, guint size);
+AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t *text, size_t size);
/** Append a new entry to the string list. The string is not filled
with data. The caller should fill in string data afterwards by writing
it to l->text, where l is the pointer returned by this function. This
function exists solely to optimize a few operations where otherwise
superfluous string copying would be necessary. */
-AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, guint size);
+AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size);
/** Same as avahi_string_list_add(), but takes a variable number of
* NUL terminated strings. The argument list must be terminated by a
@@ -85,17 +88,17 @@ AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va);
/** Convert the string list object to a single character string,
* seperated by spaces and enclosed in "". g_free() the result! This
* function doesn't work well with string that contain NUL bytes. */
-gchar* avahi_string_list_to_string(AvahiStringList *l);
+char* avahi_string_list_to_string(AvahiStringList *l);
/** Serialize the string list object in a way that is compatible with
* the storing of DNS TXT records. Strings longer than 255 bytes are truncated. */
-guint avahi_string_list_serialize(AvahiStringList *l, gpointer data, guint size);
+size_t avahi_string_list_serialize(AvahiStringList *l, void * data, size_t size);
/** Inverse of avahi_string_list_serialize() */
-AvahiStringList *avahi_string_list_parse(gconstpointer data, guint size);
+AvahiStringList *avahi_string_list_parse(const void *data, size_t size);
/** Compare to string lists */
-gboolean avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b);
+int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b);
/** Copy a string list */
AvahiStringList *avahi_string_list_copy(const AvahiStringList *l);
@@ -104,8 +107,7 @@ AvahiStringList *avahi_string_list_copy(const AvahiStringList *l);
AvahiStringList* avahi_string_list_reverse(AvahiStringList *l);
/** Return the number of elements in the string list */
-guint avahi_string_list_length(const AvahiStringList *l);
-
+unsigned avahi_string_list_length(const AvahiStringList *l);
AVAHI_C_DECL_END
diff --git a/avahi-common/timeval-test.c b/avahi-common/timeval-test.c
index a0392eb..6f44034 100644
--- a/avahi-common/timeval-test.c
+++ b/avahi-common/timeval-test.c
@@ -1,15 +1,41 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
#include "util.h"
int main(int argc, char *argv[]) {
- GTimeVal a = { 5, 5 }, b;
+ struct timeval a = { 5, 5 }, b;
b = a;
- g_message("%li.%li", a.tv_sec, a.tv_usec);
+ printf("%li.%li\n", a.tv_sec, a.tv_usec);
avahi_timeval_add(&a, -50);
- g_message("%li.%li", a.tv_sec, a.tv_usec);
+ printf("%li.%li\n", a.tv_sec, a.tv_usec);
- g_message("%lli", avahi_timeval_diff(&a, &b));
+ printf("%lli\n", avahi_timeval_diff(&a, &b));
}
diff --git a/avahi-common/util.c b/avahi-common/util.c
index a977bb2..cc80fe5 100644
--- a/avahi-common/util.c
+++ b/avahi-common/util.c
@@ -31,10 +31,12 @@
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
+#include <assert.h>
#include "util.h"
+#include "malloc.h"
-gchar *avahi_get_host_name(void) {
+char *avahi_get_host_name(void) {
#ifdef HOST_NAME_MAX
char t[HOST_NAME_MAX];
#else
@@ -45,29 +47,29 @@ gchar *avahi_get_host_name(void) {
return avahi_normalize_name(t);
}
-static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
- gboolean escaped = FALSE;
+static char *unescape_uneeded(const char *src, char *ret_dest, size_t size) {
+ int escaped = 0;
- g_assert(src);
- g_assert(ret_dest);
- g_assert(size > 0);
+ assert(src);
+ assert(ret_dest);
+ assert(size > 0);
for (; *src; src++) {
if (!escaped && *src == '\\')
- escaped = TRUE;
+ escaped = 1;
else if (escaped && (*src == '.' || *src == '\\')) {
if ((size -= 2) <= 1) break;
*(ret_dest++) = '\\';
*(ret_dest++) = *src;
- escaped = FALSE;
+ escaped = 0;
} else {
if (--size <= 1) break;
*(ret_dest++) = *src;
- escaped = FALSE;
+ escaped = 0;
}
}
@@ -77,10 +79,11 @@ static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
return ret_dest;
}
-gchar *avahi_normalize_name(const gchar *s) {
- gchar tmp[256];
- guint l;
- g_assert(s);
+char *avahi_normalize_name(const char *s) {
+ char tmp[256];
+ size_t l;
+
+ assert(s);
unescape_uneeded(s, tmp, sizeof(tmp));
@@ -89,12 +92,12 @@ gchar *avahi_normalize_name(const gchar *s) {
while (l > 0 && tmp[l-1] == '.')
tmp[--l] = 0;
- return g_strdup(tmp);
+ return avahi_strdup(tmp);
}
-gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
- g_assert(a);
- g_assert(b);
+int avahi_timeval_compare(const struct timeval *a, const struct timeval *b) {
+ assert(a);
+ assert(b);
if (a->tv_sec < b->tv_sec)
return -1;
@@ -111,9 +114,9 @@ gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
return 0;
}
-AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
- g_assert(a);
- g_assert(b);
+AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b) {
+ assert(a);
+ assert(b);
if (avahi_timeval_compare(a, b) < 0)
return - avahi_timeval_diff(b, a);
@@ -121,51 +124,52 @@ AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
}
-GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec) {
AvahiUsec u;
- g_assert(a);
+ assert(a);
u = usec + a->tv_usec;
if (u < 0) {
- a->tv_usec = (glong) (1000000 + (u % 1000000));
- a->tv_sec += (glong) (-1 + (u / 1000000));
+ a->tv_usec = (long) (1000000 + (u % 1000000));
+ a->tv_sec += (long) (-1 + (u / 1000000));
} else {
- a->tv_usec = (glong) (u % 1000000);
- a->tv_sec += (glong) (u / 1000000);
+ a->tv_usec = (long) (u % 1000000);
+ a->tv_sec += (long) (u / 1000000);
}
return a;
}
-AvahiUsec avahi_age(const GTimeVal *a) {
- GTimeVal now;
+AvahiUsec avahi_age(const struct timeval *a) {
+ struct timeval now;
- g_assert(a);
+ assert(a);
- g_get_current_time(&now);
+ gettimeofday(&now, NULL);
return avahi_timeval_diff(&now, a);
}
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
- g_assert(tv);
- g_get_current_time(tv);
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter) {
+ assert(tv);
+
+ gettimeofday(tv, NULL);
if (msec)
avahi_timeval_add(tv, (AvahiUsec) msec*1000);
if (jitter)
- avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
+ avahi_timeval_add(tv, (AvahiUsec) (jitter*1000.0*rand()/(RAND_MAX+1.0)));
return tv;
}
-gint avahi_set_cloexec(gint fd) {
- gint n;
+int avahi_set_cloexec(int fd) {
+ int n;
- g_assert(fd >= 0);
+ assert(fd >= 0);
if ((n = fcntl(fd, F_GETFD)) < 0)
return -1;
@@ -176,10 +180,10 @@ gint avahi_set_cloexec(gint fd) {
return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
}
-gint avahi_set_nonblock(gint fd) {
- gint n;
-
- g_assert(fd >= 0);
+int avahi_set_nonblock(int fd) {
+ int n;
+
+ assert(fd >= 0);
if ((n = fcntl(fd, F_GETFL)) < 0)
return -1;
@@ -190,32 +194,29 @@ gint avahi_set_nonblock(gint fd) {
return fcntl(fd, F_SETFL, n|O_NONBLOCK);
}
-gint avahi_wait_for_write(gint fd) {
+int avahi_wait_for_write(int fd) {
fd_set fds;
- gint r;
+ int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
- if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
- g_message("select() failed: %s", strerror(errno));
-
+ if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0)
return -1;
- }
- g_assert(r > 0);
+ assert(r > 0);
return 0;
}
/* 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;
- gchar *d;
+char *avahi_unescape_label(const char **name, char *dest, size_t size) {
+ unsigned i = 0;
+ char *d;
- g_assert(dest);
- g_assert(size > 0);
- g_assert(name);
+ assert(dest);
+ assert(size > 0);
+ assert(name);
if (!**name)
return NULL;
@@ -245,7 +246,7 @@ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
i++;
}
- g_assert(i < size);
+ assert(i < size);
*d = 0;
@@ -253,14 +254,14 @@ gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
}
/* Escape "\" and ".", append \0 */
-gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
- gchar *r;
+char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size) {
+ char *r;
- g_assert(src);
- g_assert(ret_name);
- g_assert(*ret_name);
- g_assert(ret_size);
- g_assert(*ret_size > 0);
+ assert(src);
+ assert(ret_name);
+ assert(*ret_name);
+ assert(ret_size);
+ assert(*ret_size > 0);
r = *ret_name;
@@ -288,41 +289,41 @@ gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name,
return r;
}
-gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
- g_assert(a);
- g_assert(b);
+int avahi_domain_equal(const char *a, const char *b) {
+ assert(a);
+ assert(b);
if (a == b)
- return TRUE;
+ return 1;
for (;;) {
- gchar ca[65], cb[65], *pa, *pb;
+ char ca[65], cb[65], *pa, *pb;
pa = avahi_unescape_label(&a, ca, sizeof(ca));
pb = avahi_unescape_label(&b, cb, sizeof(cb));
if (!pa && !pb)
- return TRUE;
+ return 1;
else if ((pa && !pb) || (!pa && pb))
- return FALSE;
+ return 0;
- if (g_ascii_strcasecmp(pa, pb))
- return FALSE;
+ if (strcasecmp(pa, pb))
+ return 0;
}
- return TRUE;
+ return 1;
}
-gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
- g_assert(a);
- g_assert(b);
+int avahi_binary_domain_cmp(const char *a, const char *b) {
+ assert(a);
+ assert(b);
if (a == b)
return 0;
for (;;) {
- gchar ca[65], cb[65], *pa, *pb;
- gint r;
+ char ca[65], cb[65], *pa, *pb;
+ int r;
pa = avahi_unescape_label(&a, ca, sizeof(ca));
pb = avahi_unescape_label(&b, cb, sizeof(cb));
@@ -339,14 +340,14 @@ gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
}
}
-void avahi_hexdump(gconstpointer p, guint size) {
- const guint8 *c = p;
- g_assert(p);
+void avahi_hexdump(const void* p, size_t size) {
+ const uint8_t *c = p;
+ assert(p);
printf("Dumping %u bytes from %p:\n", size, p);
while (size > 0) {
- guint i;
+ unsigned i;
for (i = 0; i < 16; i++) {
if (i < size)
@@ -373,11 +374,20 @@ void avahi_hexdump(gconstpointer p, guint size) {
}
}
-guint avahi_domain_hash(const gchar *s) {
- guint hash = 0;
+unsigned avahi_strhash(const char *p) {
+ unsigned hash = 0;
+
+ for (; *p; p++)
+ hash = 31 * hash + *p;
+
+ return hash;
+}
+
+unsigned avahi_domain_hash(const char *s) {
+ unsigned hash = 0;
for (;;) {
- gchar c[65], *m;
+ char c[65];
if (!avahi_unescape_label(&s, c, sizeof(c)))
return hash;
@@ -385,18 +395,16 @@ guint avahi_domain_hash(const gchar *s) {
if (!c[0])
continue;
- m = g_ascii_strdown(c, -1);
- hash += g_str_hash(m);
- g_free(m);
+ hash += avahi_strhash(avahi_strdown(c));
}
}
-gchar *avahi_format_mac_address(const guint8* mac, guint size) {
- gchar *r, *t;
- guint i;
- static const gchar hex[] = "0123456789abcdef";
+char *avahi_format_mac_address(const uint8_t* mac, size_t size) {
+ char *r, *t;
+ unsigned i;
+ static const char hex[] = "0123456789abcdef";
- t = r = g_new(gchar, size > 0 ? size*3 : 1);
+ t = r = avahi_new(char, size > 0 ? size*3 : 1);
if (size <= 0) {
*r = 0;
@@ -415,46 +423,46 @@ gchar *avahi_format_mac_address(const guint8* mac, guint size) {
return r;
}
-gboolean avahi_valid_service_type(const gchar *t) {
- const gchar *p;
- g_assert(t);
+int avahi_valid_service_type(const char *t) {
+ const char *p;
+ assert(t);
if (strlen(t) < 5)
- return FALSE;
+ return 0;
if (*t != '_')
- return FALSE;
+ return 0;
if (!(p = strchr(t, '.')))
- return FALSE;
+ return 0;
if (p - t > 63 || p - t < 2)
- return FALSE;
+ return 0;
if (*(++p) != '_')
- return FALSE;
+ return 0;
if (strchr(p, '.'))
- return FALSE;
+ return 0;
if (strlen(p) > 63 || strlen(p) < 2)
- return FALSE;
+ return 0;
- return TRUE;
+ return 1;
}
-gboolean avahi_valid_domain_name(const gchar *t) {
- const gchar *p, *dp;
- gboolean dot = FALSE;
+int avahi_valid_domain_name(const char *t) {
+ const char *p, *dp;
+ int dot = 0;
- g_assert(t);
+ assert(t);
if (*t == 0)
- return FALSE;
+ return 0;
/* Domains may not start with a dot */
if (*t == '.')
- return FALSE;
+ return 0;
dp = t;
@@ -462,49 +470,71 @@ gboolean avahi_valid_domain_name(const gchar *t) {
if (*p == '.') {
if (dot) /* Two subsequent dots */
- return FALSE;
+ return 0;
if (p - dp > 63)
- return FALSE;
+ return 0;
- dot = TRUE;
+ dot = 1;
dp = p + 1;
} else
- dot = FALSE;
+ dot = 0;
}
if (p - dp > 63)
- return FALSE;
+ return 0;
/* A trailing dot IS allowed */
- return TRUE;
+ return 1;
}
-gboolean avahi_valid_service_name(const gchar *t) {
- g_assert(t);
+int avahi_valid_service_name(const char *t) {
+ assert(t);
if (*t == 0)
- return FALSE;
+ return 0;
if (strlen(t) > 63)
- return FALSE;
+ return 0;
- return TRUE;
+ return 1;
}
-gboolean avahi_valid_host_name(const gchar *t) {
- g_assert(t);
+int avahi_valid_host_name(const char *t) {
+ assert(t);
if (*t == 0)
- return FALSE;
+ return 0;
if (strlen(t) > 63)
- return FALSE;
+ return 0;
if (strchr(t, '.'))
- return FALSE;
+ return 0;
- return TRUE;
+ return 1;
}
+
+char *avahi_strdown(char *s) {
+ char *c;
+
+ assert(s);
+
+ for (c = s; *c; c++)
+ *c = (char) tolower(*c);
+
+ return s;
+}
+
+char *avahi_strup(char *s) {
+ char *c;
+ assert(s);
+
+ for (c = s; *c; c++)
+ *c = (char) toupper(*c);
+
+ return s;
+}
+
diff --git a/avahi-common/util.h b/avahi-common/util.h
index 0d4fb4b..65252e3 100644
--- a/avahi-common/util.h
+++ b/avahi-common/util.h
@@ -22,48 +22,54 @@
USA.
***/
-#include <glib.h>
+#include <inttypes.h>
+#include <stdarg.h>
+#include <sys/time.h>
#include <avahi-common/cdecl.h>
AVAHI_C_DECL_BEGIN
-typedef gint64 AvahiUsec;
+typedef int64_t AvahiUsec;
-gchar *avahi_normalize_name(const gchar *s); /* g_free() the result! */
-gchar *avahi_get_host_name(void); /* g_free() the result! */
+char *avahi_normalize_name(const char *s); /* avahi_free() the result! */
+char *avahi_get_host_name(void); /* avahi_free() the result! */
-gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b);
-AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b);
-GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec);
+int avahi_timeval_compare(const struct timeval *a, const struct timeval *b);
+AvahiUsec avahi_timeval_diff(const struct timeval *a, const struct timeval *b);
+struct timeval* avahi_timeval_add(struct timeval *a, AvahiUsec usec);
-AvahiUsec avahi_age(const GTimeVal *a);
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter);
+AvahiUsec avahi_age(const struct timeval *a);
+struct timeval *avahi_elapse_time(struct timeval *tv, unsigned msec, unsigned jitter);
-gint avahi_set_cloexec(gint fd);
-gint avahi_set_nonblock(gint fd);
-gint avahi_wait_for_write(gint fd);
+int avahi_set_cloexec(int fd);
+int avahi_set_nonblock(int fd);
+int avahi_wait_for_write(int fd);
-gboolean avahi_domain_equal(const gchar *a, const gchar *b);
-gint avahi_binary_domain_cmp(const gchar *a, const gchar *b);
+int avahi_domain_equal(const char *a, const char *b);
+int avahi_binary_domain_cmp(const char *a, const char *b);
-void avahi_hexdump(gconstpointer p, guint size);
+void avahi_hexdump(const void *p, size_t size);
/* Read the first label from the textual domain name *name, unescape
* it and write it to dest, *name is changed to point to the next label*/
-gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size);
+char *avahi_unescape_label(const char **name, char *dest, size_t size);
/* Escape the domain name in *src and write it to *ret_name */
-gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size);
+char *avahi_escape_label(const uint8_t* src, size_t src_length, char **ret_name, size_t *ret_size);
-guint avahi_domain_hash(const gchar *s);
+unsigned avahi_strhash(const char *p);
+unsigned avahi_domain_hash(const char *s);
-gchar *avahi_format_mac_address(const guint8* mac, guint size);
+char *avahi_format_mac_address(const uint8_t* mac, size_t size);
-gboolean avahi_valid_service_type(const gchar *t);
-gboolean avahi_valid_domain_name(const gchar *t);
-gboolean avahi_valid_service_name(const gchar *t);
-gboolean avahi_valid_host_name(const gchar *t);
+int avahi_valid_service_type(const char *t);
+int avahi_valid_domain_name(const char *t);
+int avahi_valid_service_name(const char *t);
+int avahi_valid_host_name(const char *t);
+
+char *avahi_strdown(char *s);
+char *avahi_strup(char *s);
AVAHI_C_DECL_END