2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Helper functions to skip white spaces, find tokens, find separators and free
2N/A * memory.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <assert.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <ctype.h>
2N/A#include <sdp.h>
2N/A
2N/A#include "sdp_parse.h"
2N/A#include "commp_util.h"
2N/A
2N/Avoid
2N/Asdp_free_origin(sdp_origin_t *origin)
2N/A{
2N/A if (origin != NULL) {
2N/A if (origin->o_username != NULL)
2N/A free(origin->o_username);
2N/A if (origin->o_nettype != NULL)
2N/A free(origin->o_nettype);
2N/A if (origin->o_addrtype != NULL)
2N/A free(origin->o_addrtype);
2N/A if (origin->o_address != NULL)
2N/A free(origin->o_address);
2N/A free(origin);
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_key(sdp_key_t *key)
2N/A{
2N/A if (key != NULL) {
2N/A if (key->k_method != NULL)
2N/A free(key->k_method);
2N/A if (key->k_enckey != NULL)
2N/A free(key->k_enckey);
2N/A free(key);
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_zone(sdp_zone_t *zone)
2N/A{
2N/A sdp_zone_t *next_zone;
2N/A
2N/A while (zone != NULL) {
2N/A next_zone = zone->z_next;
2N/A if (zone->z_offset != NULL)
2N/A free(zone->z_offset);
2N/A free(zone);
2N/A zone = next_zone;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_list(sdp_list_t *list)
2N/A{
2N/A sdp_list_t *next_list;
2N/A
2N/A while (list != NULL) {
2N/A next_list = list->next;
2N/A if (list->value != NULL)
2N/A free(list->value);
2N/A free(list);
2N/A list = next_list;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_media(sdp_media_t *media)
2N/A{
2N/A sdp_media_t *next_media;
2N/A
2N/A while (media != NULL) {
2N/A next_media = media->m_next;
2N/A if (media->m_name != NULL)
2N/A free(media->m_name);
2N/A if (media->m_proto != NULL)
2N/A free(media->m_proto);
2N/A if (media->m_format != NULL)
2N/A sdp_free_list(media->m_format);
2N/A if (media->m_info != NULL)
2N/A free(media->m_info);
2N/A if (media->m_conn != NULL)
2N/A sdp_free_connection(media->m_conn);
2N/A if (media->m_bw != NULL)
2N/A sdp_free_bandwidth(media->m_bw);
2N/A if (media->m_key != NULL)
2N/A sdp_free_key(media->m_key);
2N/A if (media->m_attr != NULL)
2N/A sdp_free_attribute(media->m_attr);
2N/A free(media);
2N/A media = next_media;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_attribute(sdp_attr_t *attr)
2N/A{
2N/A sdp_attr_t *next_attr;
2N/A
2N/A while (attr != NULL) {
2N/A next_attr = attr->a_next;
2N/A if (attr->a_name != NULL)
2N/A free(attr->a_name);
2N/A if (attr->a_value != NULL)
2N/A free(attr->a_value);
2N/A free(attr);
2N/A attr = next_attr;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_connection(sdp_conn_t *conn)
2N/A{
2N/A sdp_conn_t *next_conn;
2N/A
2N/A while (conn != NULL) {
2N/A next_conn = conn->c_next;
2N/A if (conn->c_nettype != NULL)
2N/A free(conn->c_nettype);
2N/A if (conn->c_addrtype != NULL)
2N/A free(conn->c_addrtype);
2N/A if (conn->c_address != NULL)
2N/A free(conn->c_address);
2N/A free(conn);
2N/A conn = next_conn;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_bandwidth(sdp_bandwidth_t *bw)
2N/A{
2N/A sdp_bandwidth_t *next_bw;
2N/A
2N/A while (bw != NULL) {
2N/A next_bw = bw->b_next;
2N/A if (bw->b_type != NULL)
2N/A free(bw->b_type);
2N/A free(bw);
2N/A bw = next_bw;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_repeat(sdp_repeat_t *repeat)
2N/A{
2N/A sdp_repeat_t *next_repeat;
2N/A
2N/A while (repeat != NULL) {
2N/A next_repeat = repeat->r_next;
2N/A sdp_free_list(repeat->r_offset);
2N/A free(repeat);
2N/A repeat = next_repeat;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_time(sdp_time_t *time)
2N/A{
2N/A sdp_time_t *next_time;
2N/A
2N/A while (time != NULL) {
2N/A next_time = time->t_next;
2N/A sdp_free_repeat(time->t_repeat);
2N/A free(time);
2N/A time = next_time;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Asdp_free_session(sdp_session_t *session)
2N/A{
2N/A if (session == NULL)
2N/A return;
2N/A if (session->s_origin != NULL)
2N/A sdp_free_origin(session->s_origin);
2N/A if (session->s_name != NULL)
2N/A free(session->s_name);
2N/A if (session->s_info != NULL)
2N/A free(session->s_info);
2N/A if (session->s_uri != NULL)
2N/A free(session->s_uri);
2N/A if (session->s_email != NULL)
2N/A sdp_free_list(session->s_email);
2N/A if (session->s_phone != NULL)
2N/A sdp_free_list(session->s_phone);
2N/A if (session->s_conn != NULL)
2N/A sdp_free_connection(session->s_conn);
2N/A if (session->s_bw != NULL)
2N/A sdp_free_bandwidth(session->s_bw);
2N/A if (session->s_time != NULL)
2N/A sdp_free_time(session->s_time);
2N/A if (session->s_zone != NULL)
2N/A sdp_free_zone(session->s_zone);
2N/A if (session->s_key != NULL)
2N/A sdp_free_key(session->s_key);
2N/A if (session->s_attr != NULL)
2N/A sdp_free_attribute(session->s_attr);
2N/A if (session->s_media != NULL)
2N/A sdp_free_media(session->s_media);
2N/A free(session);
2N/A}
2N/A
2N/A/*
2N/A * Adds text of a given length to a linked list. If the list is NULL to
2N/A * start with it builds the new list
2N/A */
2N/Aint
2N/Aadd_value_to_list(sdp_list_t **list, const char *value, int len, boolean_t text)
2N/A{
2N/A sdp_list_t *new = NULL;
2N/A sdp_list_t *tmp = NULL;
2N/A
2N/A new = malloc(sizeof (sdp_list_t));
2N/A if (new == NULL)
2N/A return (ENOMEM);
2N/A new->next = NULL;
2N/A if (text)
2N/A new->value = (char *)calloc(1, len + 1);
2N/A else
2N/A new->value = (uint64_t *)calloc(1, sizeof (uint64_t));
2N/A if (new->value == NULL) {
2N/A free(new);
2N/A return (ENOMEM);
2N/A }
2N/A if (text) {
2N/A (void) strncpy(new->value, value, len);
2N/A } else {
2N/A if (commp_time_to_secs((char *)value, (char *)(value +
2N/A len), new->value) != 0) {
2N/A sdp_free_list(new);
2N/A return (EINVAL);
2N/A }
2N/A }
2N/A if (*list == NULL) {
2N/A *list = new;
2N/A } else {
2N/A tmp = *list;
2N/A while (tmp->next != NULL)
2N/A tmp = tmp->next;
2N/A tmp->next = new;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Given a linked list converts it to space separated string.
2N/A */
2N/Aint
2N/Asdp_list_to_str(sdp_list_t *list, char **buf, boolean_t text)
2N/A{
2N/A int size = 0;
2N/A int wrote = 0;
2N/A sdp_list_t *tmp;
2N/A char *ret;
2N/A char c[1];
2N/A
2N/A if (list == NULL) {
2N/A *buf = NULL;
2N/A return (EINVAL);
2N/A }
2N/A tmp = list;
2N/A while (list != NULL) {
2N/A if (text)
2N/A size += strlen((char *)list->value);
2N/A else
2N/A size += snprintf(c, 1, "%lld",
2N/A *(uint64_t *)list->value);
2N/A size++;
2N/A list = list->next;
2N/A }
2N/A list = tmp;
2N/A if (size > 0) {
2N/A *buf = calloc(1, size + 1);
2N/A if (*buf == NULL)
2N/A return (ENOMEM);
2N/A ret = *buf;
2N/A while (list != NULL) {
2N/A if (text) {
2N/A wrote = snprintf(ret, size, "%s ",
2N/A (char *)list->value);
2N/A } else {
2N/A wrote = snprintf(ret, size, "%lld ",
2N/A *(uint64_t *)list->value);
2N/A }
2N/A ret = ret + wrote;
2N/A size = size - wrote;
2N/A list = list->next;
2N/A }
2N/A } else {
2N/A return (EINVAL);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Given a space separated string, converts it into linked list. SDP field
2N/A * repeat and media can have undefined number of offsets or formats
2N/A * respectively. We need to capture it in a linked list.
2N/A */
2N/Aint
2N/Asdp_str_to_list(sdp_list_t **list, const char *buf, int len, boolean_t text)
2N/A{
2N/A const char *begin;
2N/A const char *current;
2N/A const char *end;
2N/A int ret = 0;
2N/A
2N/A if (len == 0)
2N/A return (EINVAL);
2N/A current = buf;
2N/A end = current + len;
2N/A /* takes care of strings with just spaces */
2N/A if (commp_skip_white_space(&current, end) != 0)
2N/A return (EINVAL);
2N/A while (current < end) {
2N/A (void) commp_skip_white_space(&current, end);
2N/A begin = current;
2N/A while (current < end) {
2N/A if (isspace(*current))
2N/A break;
2N/A ++current;
2N/A }
2N/A if (current != begin) {
2N/A if ((ret = add_value_to_list(list, begin,
2N/A current - begin, text)) != 0) {
2N/A sdp_free_list(*list);
2N/A *list = NULL;
2N/A return (ret);
2N/A }
2N/A }
2N/A }
2N/A return (0);
2N/A}