and added files
[bcm963xx.git] / userapps / opensource / libosip2 / src / osipparser2 / osip_content_length.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include <osipparser2/osip_port.h>
24 #include <osipparser2/osip_message.h>
25 #include <osipparser2/osip_parser.h>
26
27 int
28 osip_content_length_init (osip_content_length_t ** cl)
29 {
30   *cl =
31     (osip_content_length_t *) osip_malloc (sizeof (osip_content_length_t));
32   if (*cl == NULL)
33     return -1;
34   (*cl)->value = NULL;
35   return 0;
36 }
37
38 /* adds the content_length header to message.       */
39 /* INPUT : const char *hvalue | value of header.    */
40 /* OUTPUT: osip_message_t *sip | structure to save results.  */
41 /* returns -1 on error. */
42 int
43 osip_message_set_content_length (osip_message_t * sip, const char *hvalue)
44 {
45   int i;
46
47   if (hvalue == NULL || hvalue[0] == '\0')
48     return 0;
49
50   if (sip->content_length != NULL)
51     return -1;
52   i = osip_content_length_init (&(sip->content_length));
53   if (i != 0)
54     return -1;
55   sip->message_property = 2;
56   i = osip_content_length_parse (sip->content_length, hvalue);
57   if (i != 0)
58     {
59       osip_content_length_free (sip->content_length);
60       sip->content_length = NULL;
61       return -1;
62     }
63
64   return 0;
65 }
66
67 int
68 osip_content_length_parse (osip_content_length_t * content_length,
69                            const char *hvalue)
70 {
71   if (strlen (hvalue) + 1 < 2)
72     return -1;
73   content_length->value = (char *) osip_malloc (strlen (hvalue) + 1);
74   if (content_length->value == NULL)
75     return -1;
76   osip_strncpy (content_length->value, hvalue, strlen (hvalue));
77   return 0;
78 }
79
80 /* returns the content_length header.            */
81 /* INPUT : osip_message_t *sip | sip message.   */
82 /* returns null on error. */
83 osip_content_length_t *
84 osip_message_get_content_length (const osip_message_t * sip)
85 {
86   return sip->content_length;
87 }
88
89 /* returns the content_length header as a string.          */
90 /* INPUT : osip_content_length_t *content_length | content_length header.  */
91 /* returns null on error. */
92 int
93 osip_content_length_to_str (const osip_content_length_t * cl, char **dest)
94 {
95   if (cl == NULL)
96     return -1;
97   *dest = osip_strdup (cl->value);
98   return 0;
99 }
100
101 /* deallocates a osip_content_length_t strcture.  */
102 /* INPUT : osip_content_length_t *content_length | content_length header. */
103 void
104 osip_content_length_free (osip_content_length_t * content_length)
105 {
106   if (content_length == NULL)
107     return;
108   osip_free (content_length->value);
109   osip_free (content_length);
110 }
111
112 int
113 osip_content_length_clone (const osip_content_length_t * ctl,
114                            osip_content_length_t ** dest)
115 {
116   int i;
117   osip_content_length_t *cl;
118
119   *dest = NULL;
120   if (ctl == NULL)
121     return -1;
122   /*
123      empty headers are allowed:
124      if (ctl->value==NULL) return -1;
125    */
126   i = osip_content_length_init (&cl);
127   if (i == -1)                  /* allocation failed */
128     return -1;
129   if (ctl->value != NULL)
130     cl->value = osip_strdup (ctl->value);
131
132   *dest = cl;
133   return 0;
134 }