1 /*
2 The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3 Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
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 <osipparser2/internal.h>
21
22 #include <osipparser2/osip_port.h>
23 #include <osipparser2/osip_message.h>
24 #include <osipparser2/osip_parser.h>
25
osip_content_length_init(osip_content_length_t ** cl)26 int osip_content_length_init(osip_content_length_t **cl) {
27 *cl = (osip_content_length_t *) osip_malloc(sizeof(osip_content_length_t));
28
29 if (*cl == NULL)
30 return OSIP_NOMEM;
31
32 (*cl)->value = NULL;
33 return OSIP_SUCCESS;
34 }
35
36 /* adds the content_length header to message. */
37 /* INPUT : const char *hvalue | value of header. */
38 /* OUTPUT: osip_message_t *sip | structure to save results. */
39 /* returns -1 on error. */
osip_message_set_content_length(osip_message_t * sip,const char * hvalue)40 int osip_message_set_content_length(osip_message_t *sip, const char *hvalue) {
41 int i;
42
43 if (hvalue == NULL || hvalue[0] == '\0')
44 return OSIP_SUCCESS;
45
46 if (sip->content_length != NULL)
47 return OSIP_SYNTAXERROR;
48
49 i = osip_content_length_init(&(sip->content_length));
50
51 if (i != 0)
52 return i;
53
54 sip->message_property = 2;
55 i = osip_content_length_parse(sip->content_length, hvalue);
56
57 if (i != 0) {
58 osip_content_length_free(sip->content_length);
59 sip->content_length = NULL;
60 return i;
61 }
62
63 return OSIP_SUCCESS;
64 }
65
osip_content_length_parse(osip_content_length_t * content_length,const char * hvalue)66 int osip_content_length_parse(osip_content_length_t *content_length, const char *hvalue) {
67 size_t len;
68
69 if (hvalue == NULL)
70 return OSIP_BADPARAMETER;
71
72 len = strlen(hvalue);
73
74 if (len + 1 < 2)
75 return OSIP_SYNTAXERROR;
76
77 content_length->value = (char *) osip_malloc(len + 1);
78
79 if (content_length->value == NULL)
80 return OSIP_NOMEM;
81
82 osip_strncpy(content_length->value, hvalue, len);
83 return OSIP_SUCCESS;
84 }
85
86 #ifndef MINISIZE
87 /* returns the content_length header. */
88 /* INPUT : osip_message_t *sip | sip message. */
89 /* returns null on error. */
osip_message_get_content_length(const osip_message_t * sip)90 osip_content_length_t *osip_message_get_content_length(const osip_message_t *sip) {
91 return sip->content_length;
92 }
93 #endif
94
95 /* returns the content_length header as a string. */
96 /* INPUT : osip_content_length_t *content_length | content_length header. */
97 /* returns null on error. */
osip_content_length_to_str(const osip_content_length_t * cl,char ** dest)98 int osip_content_length_to_str(const osip_content_length_t *cl, char **dest) {
99 if (cl == NULL)
100 return OSIP_BADPARAMETER;
101
102 *dest = osip_strdup(cl->value);
103
104 if (*dest == NULL)
105 return OSIP_NOMEM;
106
107 return OSIP_SUCCESS;
108 }
109
110 /* deallocates a osip_content_length_t strcture. */
111 /* INPUT : osip_content_length_t *content_length | content_length header. */
osip_content_length_free(osip_content_length_t * content_length)112 void osip_content_length_free(osip_content_length_t *content_length) {
113 if (content_length == NULL)
114 return;
115
116 osip_free(content_length->value);
117 osip_free(content_length);
118 }
119
osip_content_length_clone(const osip_content_length_t * ctl,osip_content_length_t ** dest)120 int osip_content_length_clone(const osip_content_length_t *ctl, osip_content_length_t **dest) {
121 int i;
122 osip_content_length_t *cl;
123
124 *dest = NULL;
125
126 if (ctl == NULL)
127 return OSIP_BADPARAMETER;
128
129 /*
130 empty headers are allowed:
131 if (ctl->value==NULL) return -1;
132 */
133 i = osip_content_length_init(&cl);
134
135 if (i != 0) /* allocation failed */
136 return i;
137
138 if (ctl->value != NULL) {
139 cl->value = osip_strdup(ctl->value);
140
141 if (cl->value == NULL) {
142 osip_content_length_free(cl);
143 return OSIP_NOMEM;
144 }
145 }
146
147 *dest = cl;
148 return OSIP_SUCCESS;
149 }
150