]> git.meshlink.io Git - catta/blob - avahi-compat-howl/text.c
1cd34fe5fc7f446d27ddd47191cd9286e103f443
[catta] / avahi-compat-howl / text.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27
28 #include <avahi-common/strlst.h>
29 #include <avahi-common/malloc.h>
30 #include <avahi-common/domain.h>
31
32 #include "howl.h"
33 #include "warn.h"
34
35 struct _sw_text_record {
36     AvahiStringList *strlst;
37     uint8_t *buffer;
38     size_t buffer_size;
39     int buffer_valid;
40 };
41
42 sw_result sw_text_record_init(sw_text_record *self) {
43     assert(self);
44
45     AVAHI_WARN_LINKAGE;
46     
47     if (!(*self = avahi_new(struct _sw_text_record, 1))) {
48         *self = NULL;
49         return SW_E_UNKNOWN;
50     }
51     
52     (*self)->strlst = NULL;
53     (*self)->buffer = NULL;
54     (*self)->buffer_size = 0;
55     (*self)->buffer_valid = 0;
56     
57     return SW_OKAY;
58 }
59
60 sw_result sw_text_record_fina(sw_text_record self) {
61     assert(self);
62
63     AVAHI_WARN_LINKAGE;
64
65     avahi_string_list_free(self->strlst);
66     avahi_free(self->buffer);
67     avahi_free(self);
68     return SW_OKAY;
69 }
70
71 sw_result sw_text_record_add_string(
72     sw_text_record self,
73     sw_const_string string) {
74
75     AvahiStringList *n;
76     
77     assert(self);
78     assert(string);
79
80     AVAHI_WARN_LINKAGE;
81     
82     if (!(n = avahi_string_list_add(self->strlst, string)))
83         return SW_E_UNKNOWN;
84
85     self->strlst = n;
86     self->buffer_valid = 0;
87     return SW_OKAY;
88 }
89
90 sw_result sw_text_record_add_key_and_string_value(
91     sw_text_record self,
92     sw_const_string key,
93     sw_const_string val) {
94
95     AvahiStringList *n;
96
97     assert(self);
98     assert(key);
99     
100     AVAHI_WARN_LINKAGE;
101
102     if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
103         return SW_E_UNKNOWN;
104
105     self->strlst = n;
106     self->buffer_valid = 0;
107     return SW_OKAY;
108 }
109
110 sw_result sw_text_record_add_key_and_binary_value(
111     sw_text_record self,
112     sw_const_string key,
113     sw_octets val,
114     sw_uint32 len) {
115
116     AvahiStringList *n;
117
118     assert(self);
119     assert(key);
120     assert(len || !val);
121     
122     AVAHI_WARN_LINKAGE;
123
124     if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
125         return SW_E_UNKNOWN;
126
127     self->strlst = n;
128     self->buffer_valid = 0;
129     return SW_OKAY;
130 }
131
132 static int rebuild(sw_text_record self) {
133     assert(self);
134
135     if (self->buffer_valid)
136         return 0;
137
138     self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
139     
140     if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
141         return -1;
142         
143     avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
144     self->buffer_valid = 1;
145
146     return 0;
147 }
148
149 sw_octets sw_text_record_bytes(sw_text_record self) {
150     assert(self);
151
152     AVAHI_WARN_LINKAGE;
153
154     if (rebuild(self) < 0)
155         return NULL;
156
157     return self->buffer;
158 }
159
160 sw_uint32 sw_text_record_len(sw_text_record self) {
161     assert(self);
162
163     AVAHI_WARN_LINKAGE;
164     
165     if (rebuild(self) < 0)
166         return (uint32_t) -1;
167
168     return self->buffer_size;
169 }
170
171 struct _sw_text_record_iterator {
172     AvahiStringList *strlst, *index;
173     
174 };
175
176 sw_result sw_text_record_iterator_init(
177     sw_text_record_iterator * self,
178     sw_octets text_record,
179     sw_uint32 text_record_len) {
180
181     assert(self);
182
183     AVAHI_WARN_LINKAGE;
184     
185     if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
186         *self = NULL;
187         return SW_E_UNKNOWN;
188     }
189
190     (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len));
191     
192     return SW_OKAY;
193 }
194
195 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
196     assert(self);
197
198     AVAHI_WARN_LINKAGE;
199
200     avahi_string_list_free(self->strlst);
201     avahi_free(self);
202     
203     return SW_OKAY;
204 }
205
206 sw_result sw_text_record_iterator_next(
207     sw_text_record_iterator self,
208     char key[SW_TEXT_RECORD_MAX_LEN],
209     sw_uint8 val[SW_TEXT_RECORD_MAX_LEN],
210     sw_uint32 * val_len) {
211
212     char *mkey = NULL, *mvalue = NULL;
213     size_t msize = 0;
214     
215     assert(self);
216     assert(key);
217     
218     AVAHI_WARN_LINKAGE;
219
220     if (!self->index)
221         return SW_E_UNKNOWN;
222
223     if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
224         return SW_E_UNKNOWN;
225
226     avahi_strlcpy(key, mkey, SW_TEXT_RECORD_MAX_LEN);
227     memset(val, 0, SW_TEXT_RECORD_MAX_LEN);
228     memcpy(val, mvalue, msize);
229     *val_len = msize;
230     
231     avahi_free(mkey);
232     avahi_free(mvalue);
233
234     self->index = self->index->next;
235
236     return SW_OKAY;
237 }
238