From d5f4c6b3b3d0ba7fa0365ea83746530539737a92 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 3 May 2005 23:38:17 +0000 Subject: [PATCH] allow NULL bytes in strlsts git-svn-id: file:///home/lennart/svn/public/avahi/trunk@39 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- strlst-test.c | 14 +++++++++++++- strlst.c | 31 +++++++++++++++++-------------- strlst.h | 4 +++- todo | 5 +---- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/strlst-test.c b/strlst-test.c index c4cef31..e77eae8 100644 --- a/strlst-test.c +++ b/strlst-test.c @@ -9,9 +9,12 @@ int main(int argc, char *argv[]) { flxStringList *a = NULL, *b; guint size, n; + a = flx_string_list_add(a, "start"); a = flx_string_list_add(a, "foo"); a = flx_string_list_add(a, "bar"); - a = flx_string_list_add(a, "baz"); + a = flx_string_list_add(a, "quux"); + a = flx_string_list_add_arbitrary(a, "null\0null", 9); + a = flx_string_list_add(a, "end"); t = flx_string_list_to_string(a); printf("--%s--\n", t); @@ -38,7 +41,16 @@ int main(int argc, char *argv[]) { printf("--%s--\n", t); g_free(t); + flx_string_list_free(b); + + b = flx_string_list_copy(a); + + g_assert(flx_string_list_equal(a, b)); + t = flx_string_list_to_string(b); + printf("--%s--\n", t); + g_free(t); + flx_string_list_free(a); flx_string_list_free(b); diff --git a/strlst.c b/strlst.c index d0507e0..417d423 100644 --- a/strlst.c +++ b/strlst.c @@ -3,15 +3,14 @@ #include "strlst.h" -static flxStringList *string_list_add_internal(flxStringList *l, const gchar *text, guint size) { +flxStringList *flx_string_list_add_arbitrary(flxStringList *l, const guint8*text, guint size) { flxStringList *n; g_assert(text); n = g_malloc(sizeof(flxStringList) + size); n->next = l; - strncpy(n->text, text, size); - n->text[size] = 0; + memcpy(n->text, text, n->size = size); return n; } @@ -19,7 +18,7 @@ static flxStringList *string_list_add_internal(flxStringList *l, const gchar *te flxStringList *flx_string_list_add(flxStringList *l, const gchar *text) { g_assert(text); - return string_list_add_internal(l, text, strlen(text)); + return flx_string_list_add_arbitrary(l, (const guint8*) text, strlen(text)); } flxStringList *flx_string_list_parse(gconstpointer data, guint size) { @@ -35,7 +34,7 @@ flxStringList *flx_string_list_parse(gconstpointer data, guint size) { break; k = *(c++); - r = string_list_add_internal(r, (const gchar*) c, k); + r = flx_string_list_add_arbitrary(r, c, k); c += k; size -= 1 + k; @@ -54,7 +53,6 @@ void flx_string_list_free(flxStringList *l) { } } - static flxStringList* string_list_reverse(flxStringList *l) { flxStringList *r = NULL, *n; @@ -79,18 +77,19 @@ gchar* flx_string_list_to_string(flxStringList *l) { if (n != l) s ++; - s += strlen(n->text)+2; + s += n->size+3; } - t = e = g_new(gchar, s+1); + t = e = g_new(gchar, s); for (n = l; n; n = n->next) { if (n != l) *(e++) = ' '; *(e++) = '"'; - strcpy(e, n->text); - e += strlen(n->text); + strncpy(e, n->text, n->size); + e[n->size] = 0; + e = strchr(e, 0); *(e++) = '"'; } @@ -118,7 +117,7 @@ guint flx_string_list_serialize(flxStringList *l, gpointer data, guint size) { if (size < 1) break; - k = strlen(n->text); + k = n->size; if (k > 255) k = 255; @@ -139,7 +138,7 @@ guint flx_string_list_serialize(flxStringList *l, gpointer data, guint size) { for (n = l; n; n = n->next) { guint k; - k = strlen(n->text); + k = n->size; if (k > 255) k = 255; @@ -159,7 +158,10 @@ gboolean flx_string_list_equal(flxStringList *a, flxStringList *b) { if (!a || !b) return FALSE; - if (strcmp(a->text, b->text) != 0) + if (a->size != b->size) + return FALSE; + + if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0) return FALSE; a = a->next; @@ -186,6 +188,7 @@ flxStringList *flx_string_list_add_many_va(flxStringList *r, va_list va) { return r; } + flxStringList *flx_string_list_new(const gchar *txt, ...) { va_list va; flxStringList *r = NULL; @@ -209,7 +212,7 @@ flxStringList *flx_string_list_copy(flxStringList *l) { flxStringList *r = NULL; for (; l; l = l->next) - r = flx_string_list_add(r, l->text); + r = flx_string_list_add_arbitrary(r, l->text, l->size); return string_list_reverse(r); } diff --git a/strlst.h b/strlst.h index 94fb563..ca09fba 100644 --- a/strlst.h +++ b/strlst.h @@ -7,7 +7,8 @@ typedef struct _flxStringList flxStringList; struct _flxStringList { flxStringList *next; - gchar text[1]; + guint size; + guint8 text[1]; }; flxStringList *flx_string_list_new(const gchar *txt, ...); @@ -16,6 +17,7 @@ flxStringList *flx_string_list_new_va(va_list va); void flx_string_list_free(flxStringList *l); flxStringList *flx_string_list_add(flxStringList *l, const gchar *text); +flxStringList *flx_string_list_add_arbitrary(flxStringList *l, const guint8 *text, guint size); flxStringList *flx_string_list_add_many(flxStringList *r, ...); flxStringList *flx_string_list_add_many_va(flxStringList *r, va_list va); diff --git a/todo b/todo index 3e24640..ef861c7 100644 --- a/todo +++ b/todo @@ -5,8 +5,6 @@ todo: * add SRV and TXT records referenced from PTR records automatically to packet * add A and AAAA records referenced from SRV records automatically to packet -* allow NULL bytes in TXT records - done: * Probing/Conflict resolution * uniqueness @@ -20,5 +18,4 @@ done: * name compression * remove expression "rrset" from source files * defend our entries on incoming goodbye - - +* allow NULL bytes in TXT records -- 2.39.5