2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/strlst.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/domain.h>
33 struct _sw_text_record {
34 AvahiStringList *strlst;
42 static size_t strlcpy(char *dest, const char *src, size_t n) {
47 strncpy(dest, src, n-1);
56 sw_result sw_text_record_init(sw_text_record *self) {
61 if (!(*self = avahi_new(struct _sw_text_record, 1))) {
66 (*self)->strlst = NULL;
67 (*self)->buffer = NULL;
68 (*self)->buffer_size = 0;
69 (*self)->buffer_valid = 0;
74 sw_result sw_text_record_fina(sw_text_record self) {
79 avahi_string_list_free(self->strlst);
80 avahi_free(self->buffer);
85 sw_result sw_text_record_add_string(
87 sw_const_string string) {
96 if (!(n = avahi_string_list_add(self->strlst, string)))
100 self->buffer_valid = 0;
104 sw_result sw_text_record_add_key_and_string_value(
107 sw_const_string val) {
116 if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
120 self->buffer_valid = 0;
124 sw_result sw_text_record_add_key_and_binary_value(
138 if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
142 self->buffer_valid = 0;
146 static int rebuild(sw_text_record self) {
149 if (self->buffer_valid)
152 self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
154 if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
157 avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
158 self->buffer_valid = 1;
163 sw_octets sw_text_record_bytes(sw_text_record self) {
168 if (rebuild(self) < 0)
174 sw_uint32 sw_text_record_len(sw_text_record self) {
179 if (rebuild(self) < 0)
180 return (uint32_t) -1;
182 return self->buffer_size;
185 struct _sw_text_record_iterator {
186 AvahiStringList *strlst, *index;
190 sw_result sw_text_record_iterator_init(
191 sw_text_record_iterator * self,
192 sw_octets text_record,
193 sw_uint32 text_record_len) {
195 AvahiStringList *txt;
200 if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
205 if (avahi_string_list_parse(text_record, text_record_len, &txt) < 0) {
211 (*self)->index = (*self)->strlst = avahi_string_list_reverse(txt);
216 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
221 avahi_string_list_free(self->strlst);
227 sw_result sw_text_record_iterator_next(
228 sw_text_record_iterator self,
229 char key[SW_TEXT_RECORD_MAX_LEN],
230 sw_uint8 val[SW_TEXT_RECORD_MAX_LEN],
231 sw_uint32 * val_len) {
233 char *mkey = NULL, *mvalue = NULL;
244 if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
247 strlcpy(key, mkey, SW_TEXT_RECORD_MAX_LEN);
248 memset(val, 0, SW_TEXT_RECORD_MAX_LEN);
249 memcpy(val, mvalue, msize);
255 self->index = self->index->next;