4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <avahi-common/strlst.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/domain.h>
35 struct _sw_text_record {
36 AvahiStringList *strlst;
44 static size_t strlcpy(char *dest, const char *src, size_t n) {
49 strncpy(dest, src, n-1);
58 sw_result sw_text_record_init(sw_text_record *self) {
63 if (!(*self = avahi_new(struct _sw_text_record, 1))) {
68 (*self)->strlst = NULL;
69 (*self)->buffer = NULL;
70 (*self)->buffer_size = 0;
71 (*self)->buffer_valid = 0;
76 sw_result sw_text_record_fina(sw_text_record self) {
81 avahi_string_list_free(self->strlst);
82 avahi_free(self->buffer);
87 sw_result sw_text_record_add_string(
89 sw_const_string string) {
98 if (!(n = avahi_string_list_add(self->strlst, string)))
102 self->buffer_valid = 0;
106 sw_result sw_text_record_add_key_and_string_value(
109 sw_const_string val) {
118 if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
122 self->buffer_valid = 0;
126 sw_result sw_text_record_add_key_and_binary_value(
140 if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
144 self->buffer_valid = 0;
148 static int rebuild(sw_text_record self) {
151 if (self->buffer_valid)
154 self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
156 if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
159 avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
160 self->buffer_valid = 1;
165 sw_octets sw_text_record_bytes(sw_text_record self) {
170 if (rebuild(self) < 0)
176 sw_uint32 sw_text_record_len(sw_text_record self) {
181 if (rebuild(self) < 0)
182 return (uint32_t) -1;
184 return self->buffer_size;
187 struct _sw_text_record_iterator {
188 AvahiStringList *strlst, *index;
192 sw_result sw_text_record_iterator_init(
193 sw_text_record_iterator * self,
194 sw_octets text_record,
195 sw_uint32 text_record_len) {
197 AvahiStringList *txt;
202 if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
207 if (avahi_string_list_parse(text_record, text_record_len, &txt) < 0) {
213 (*self)->index = (*self)->strlst = avahi_string_list_reverse(txt);
218 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
223 avahi_string_list_free(self->strlst);
229 sw_result sw_text_record_iterator_next(
230 sw_text_record_iterator self,
231 char key[SW_TEXT_RECORD_MAX_LEN],
232 sw_uint8 val[SW_TEXT_RECORD_MAX_LEN],
233 sw_uint32 * val_len) {
235 char *mkey = NULL, *mvalue = NULL;
246 if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
249 strlcpy(key, mkey, SW_TEXT_RECORD_MAX_LEN);
250 memset(val, 0, SW_TEXT_RECORD_MAX_LEN);
251 memcpy(val, mvalue, msize);
257 self->index = self->index->next;