dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libcommputil / common / sdp_parse_helper.c
blobfd18b8318670f313f6dff9ac1c5f67fc13e9de64
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Helper functions to skip white spaces, find tokens, find separators and free
29 * memory.
32 #include <stdio.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <sdp.h>
40 #include "sdp_parse.h"
41 #include "commp_util.h"
43 void
44 sdp_free_origin(sdp_origin_t *origin)
46 if (origin != NULL) {
47 free(origin->o_username);
48 free(origin->o_nettype);
49 free(origin->o_addrtype);
50 free(origin->o_address);
51 free(origin);
55 void
56 sdp_free_key(sdp_key_t *key)
58 if (key != NULL) {
59 free(key->k_method);
60 free(key->k_enckey);
61 free(key);
65 void
66 sdp_free_zone(sdp_zone_t *zone)
68 sdp_zone_t *next_zone;
70 while (zone != NULL) {
71 next_zone = zone->z_next;
72 free(zone->z_offset);
73 free(zone);
74 zone = next_zone;
78 void
79 sdp_free_list(sdp_list_t *list)
81 sdp_list_t *next_list;
83 while (list != NULL) {
84 next_list = list->next;
85 free(list->value);
86 free(list);
87 list = next_list;
91 void
92 sdp_free_media(sdp_media_t *media)
94 sdp_media_t *next_media;
96 while (media != NULL) {
97 next_media = media->m_next;
98 free(media->m_name);
99 free(media->m_proto);
100 if (media->m_format != NULL)
101 sdp_free_list(media->m_format);
102 free(media->m_info);
103 if (media->m_conn != NULL)
104 sdp_free_connection(media->m_conn);
105 if (media->m_bw != NULL)
106 sdp_free_bandwidth(media->m_bw);
107 if (media->m_key != NULL)
108 sdp_free_key(media->m_key);
109 if (media->m_attr != NULL)
110 sdp_free_attribute(media->m_attr);
111 free(media);
112 media = next_media;
116 void
117 sdp_free_attribute(sdp_attr_t *attr)
119 sdp_attr_t *next_attr;
121 while (attr != NULL) {
122 next_attr = attr->a_next;
123 free(attr->a_name);
124 free(attr->a_value);
125 free(attr);
126 attr = next_attr;
130 void
131 sdp_free_connection(sdp_conn_t *conn)
133 sdp_conn_t *next_conn;
135 while (conn != NULL) {
136 next_conn = conn->c_next;
137 free(conn->c_nettype);
138 free(conn->c_addrtype);
139 free(conn->c_address);
140 free(conn);
141 conn = next_conn;
145 void
146 sdp_free_bandwidth(sdp_bandwidth_t *bw)
148 sdp_bandwidth_t *next_bw;
150 while (bw != NULL) {
151 next_bw = bw->b_next;
152 free(bw->b_type);
153 free(bw);
154 bw = next_bw;
158 void
159 sdp_free_repeat(sdp_repeat_t *repeat)
161 sdp_repeat_t *next_repeat;
163 while (repeat != NULL) {
164 next_repeat = repeat->r_next;
165 sdp_free_list(repeat->r_offset);
166 free(repeat);
167 repeat = next_repeat;
171 void
172 sdp_free_time(sdp_time_t *time)
174 sdp_time_t *next_time;
176 while (time != NULL) {
177 next_time = time->t_next;
178 sdp_free_repeat(time->t_repeat);
179 free(time);
180 time = next_time;
184 void
185 sdp_free_session(sdp_session_t *session)
187 if (session == NULL)
188 return;
189 if (session->s_origin != NULL)
190 sdp_free_origin(session->s_origin);
191 free(session->s_name);
192 free(session->s_info);
193 free(session->s_uri);
194 if (session->s_email != NULL)
195 sdp_free_list(session->s_email);
196 if (session->s_phone != NULL)
197 sdp_free_list(session->s_phone);
198 if (session->s_conn != NULL)
199 sdp_free_connection(session->s_conn);
200 if (session->s_bw != NULL)
201 sdp_free_bandwidth(session->s_bw);
202 if (session->s_time != NULL)
203 sdp_free_time(session->s_time);
204 if (session->s_zone != NULL)
205 sdp_free_zone(session->s_zone);
206 if (session->s_key != NULL)
207 sdp_free_key(session->s_key);
208 if (session->s_attr != NULL)
209 sdp_free_attribute(session->s_attr);
210 if (session->s_media != NULL)
211 sdp_free_media(session->s_media);
212 free(session);
216 * Adds text of a given length to a linked list. If the list is NULL to
217 * start with it builds the new list
220 add_value_to_list(sdp_list_t **list, const char *value, int len, boolean_t text)
222 sdp_list_t *new = NULL;
223 sdp_list_t *tmp = NULL;
225 new = malloc(sizeof (sdp_list_t));
226 if (new == NULL)
227 return (ENOMEM);
228 new->next = NULL;
229 if (text)
230 new->value = (char *)calloc(1, len + 1);
231 else
232 new->value = (uint64_t *)calloc(1, sizeof (uint64_t));
233 if (new->value == NULL) {
234 free(new);
235 return (ENOMEM);
237 if (text) {
238 (void) strncpy(new->value, value, len);
239 } else {
240 if (commp_time_to_secs((char *)value, (char *)(value +
241 len), new->value) != 0) {
242 sdp_free_list(new);
243 return (EINVAL);
246 if (*list == NULL) {
247 *list = new;
248 } else {
249 tmp = *list;
250 while (tmp->next != NULL)
251 tmp = tmp->next;
252 tmp->next = new;
254 return (0);
258 * Given a linked list converts it to space separated string.
261 sdp_list_to_str(sdp_list_t *list, char **buf, boolean_t text)
263 int size = 0;
264 int wrote = 0;
265 sdp_list_t *tmp;
266 char *ret;
267 char c[1];
269 if (list == NULL) {
270 *buf = NULL;
271 return (EINVAL);
273 tmp = list;
274 while (list != NULL) {
275 if (text)
276 size += strlen((char *)list->value);
277 else
278 size += snprintf(c, 1, "%lld",
279 *(uint64_t *)list->value);
280 size++;
281 list = list->next;
283 list = tmp;
284 if (size > 0) {
285 *buf = calloc(1, size + 1);
286 if (*buf == NULL)
287 return (ENOMEM);
288 ret = *buf;
289 while (list != NULL) {
290 if (text) {
291 wrote = snprintf(ret, size, "%s ",
292 (char *)list->value);
293 } else {
294 wrote = snprintf(ret, size, "%lld ",
295 *(uint64_t *)list->value);
297 ret = ret + wrote;
298 size = size - wrote;
299 list = list->next;
301 } else {
302 return (EINVAL);
304 return (0);
308 * Given a space separated string, converts it into linked list. SDP field
309 * repeat and media can have undefined number of offsets or formats
310 * respectively. We need to capture it in a linked list.
313 sdp_str_to_list(sdp_list_t **list, const char *buf, int len, boolean_t text)
315 const char *begin;
316 const char *current;
317 const char *end;
318 int ret = 0;
320 if (len == 0)
321 return (EINVAL);
322 current = buf;
323 end = current + len;
324 /* takes care of strings with just spaces */
325 if (commp_skip_white_space(&current, end) != 0)
326 return (EINVAL);
327 while (current < end) {
328 (void) commp_skip_white_space(&current, end);
329 begin = current;
330 while (current < end) {
331 if (isspace(*current))
332 break;
333 ++current;
335 if (current != begin) {
336 if ((ret = add_value_to_list(list, begin,
337 current - begin, text)) != 0) {
338 sdp_free_list(*list);
339 *list = NULL;
340 return (ret);
344 return (0);