X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-compat-howl%2Ftext.c;h=7ef4df33f89bf87c47a7e731843e344b3c8d8a73;hb=7a5b2f69af7d36d6cd4153142f125fa011784e03;hp=25428d814ac8aec68ceb96f4f29ae8d115fe2031;hpb=0433169ea9c6906f31b1a78e9a118858dd776dad;p=catta diff --git a/avahi-compat-howl/text.c b/avahi-compat-howl/text.c index 25428d8..7ef4df3 100644 --- a/avahi-compat-howl/text.c +++ b/avahi-compat-howl/text.c @@ -1,18 +1,16 @@ -/* $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 @@ -25,12 +23,11 @@ #include -#include - #include #include #include +#include "howl.h" #include "warn.h" struct _sw_text_record { @@ -40,21 +37,37 @@ struct _sw_text_record { int buffer_valid; }; +#ifndef HAVE_STRLCPY + +static size_t strlcpy(char *dest, const char *src, size_t n) { + assert(dest); + assert(src); + + if (n > 0) { + strncpy(dest, src, n-1); + dest[n-1] = 0; + } + + return strlen(src); +} + +#endif + sw_result sw_text_record_init(sw_text_record *self) { assert(self); AVAHI_WARN_LINKAGE; - + if (!(*self = avahi_new(struct _sw_text_record, 1))) { *self = NULL; return SW_E_UNKNOWN; } - + (*self)->strlst = NULL; (*self)->buffer = NULL; (*self)->buffer_size = 0; (*self)->buffer_valid = 0; - + return SW_OKAY; } @@ -74,12 +87,12 @@ sw_result sw_text_record_add_string( sw_const_string string) { AvahiStringList *n; - + assert(self); assert(string); AVAHI_WARN_LINKAGE; - + if (!(n = avahi_string_list_add(self->strlst, string))) return SW_E_UNKNOWN; @@ -97,7 +110,7 @@ sw_result sw_text_record_add_key_and_string_value( assert(self); assert(key); - + AVAHI_WARN_LINKAGE; if (!(n = avahi_string_list_add_pair(self->strlst, key, val))) @@ -119,7 +132,7 @@ sw_result sw_text_record_add_key_and_binary_value( assert(self); assert(key); assert(len || !val); - + AVAHI_WARN_LINKAGE; if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len))) @@ -137,10 +150,10 @@ static int rebuild(sw_text_record self) { return 0; self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0); - + if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1))) return -1; - + avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size); self->buffer_valid = 1; @@ -162,7 +175,7 @@ sw_uint32 sw_text_record_len(sw_text_record self) { assert(self); AVAHI_WARN_LINKAGE; - + if (rebuild(self) < 0) return (uint32_t) -1; @@ -171,7 +184,7 @@ sw_uint32 sw_text_record_len(sw_text_record self) { struct _sw_text_record_iterator { AvahiStringList *strlst, *index; - + }; sw_result sw_text_record_iterator_init( @@ -179,17 +192,24 @@ sw_result sw_text_record_iterator_init( sw_octets text_record, sw_uint32 text_record_len) { + AvahiStringList *txt; assert(self); AVAHI_WARN_LINKAGE; - + if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) { *self = NULL; return SW_E_UNKNOWN; } - (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len)); - + if (avahi_string_list_parse(text_record, text_record_len, &txt) < 0) { + avahi_free(*self); + *self = NULL; + return SW_E_UNKNOWN; + } + + (*self)->index = (*self)->strlst = avahi_string_list_reverse(txt); + return SW_OKAY; } @@ -200,22 +220,22 @@ sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) { avahi_string_list_free(self->strlst); avahi_free(self); - + return SW_OKAY; } sw_result sw_text_record_iterator_next( sw_text_record_iterator self, - char key[255], - sw_uint8 val[255], + char key[SW_TEXT_RECORD_MAX_LEN], + sw_uint8 val[SW_TEXT_RECORD_MAX_LEN], sw_uint32 * val_len) { char *mkey = NULL, *mvalue = NULL; size_t msize = 0; - + assert(self); assert(key); - + AVAHI_WARN_LINKAGE; if (!self->index) @@ -224,10 +244,11 @@ sw_result sw_text_record_iterator_next( if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0) return SW_E_UNKNOWN; - avahi_strlcpy(key, mkey, 255); + strlcpy(key, mkey, SW_TEXT_RECORD_MAX_LEN); + memset(val, 0, SW_TEXT_RECORD_MAX_LEN); memcpy(val, mvalue, msize); *val_len = msize; - + avahi_free(mkey); avahi_free(mvalue);