Blame view

3rdparty/libosip2-5.3.0/src/osipparser2/osip_header.c 7.98 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  /*
    The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
    Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
  
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
  
  #include <osipparser2/internal.h>
  
  #include <osipparser2/osip_port.h>
  #include <osipparser2/osip_message.h>
  #include <osipparser2/osip_parser.h>
  
  /* Add a header to a SIP message.                           */
  /* INPUT :  char *hname | pointer to a header name.         */
  /* INPUT :  char *hvalue | pointer to a header value.       */
  /* OUTPUT: osip_message_t *sip | structure to save results.          */
  /* returns -1 on error. */
  int osip_message_set_header(osip_message_t *sip, const char *hname, const char *hvalue) {
    osip_header_t *h;
    int i;
  
    if (sip == NULL || hname == NULL)
      return OSIP_BADPARAMETER;
  
    i = osip_header_init(&h);
  
    if (i != 0)
      return i;
  
    h->hname = (char *) osip_malloc(strlen(hname) + 1);
  
    if (h->hname == NULL) {
      osip_header_free(h);
      return OSIP_NOMEM;
    }
  
    osip_clrncpy(h->hname, hname, strlen(hname));
  
    if (hvalue != NULL) { /* some headers can be null ("subject:") */
      h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  
      if (h->hvalue == NULL) {
        osip_header_free(h);
        return OSIP_NOMEM;
      }
  
      osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  
    } else
      h->hvalue = NULL;
  
    sip->message_property = 2;
    osip_list_add(&sip->headers, h, -1);
    return OSIP_SUCCESS; /* ok */
  }
  
  /* Add a or replace exising header  to a SIP message.       */
  /* INPUT :  char *hname | pointer to a header name.         */
  /* INPUT :  char *hvalue | pointer to a header value.       */
  /* OUTPUT: osip_message_t *sip | structure to save results. */
  /* returns -1 on error. */
  int osip_message_replace_header(osip_message_t *sip, const char *hname, const char *hvalue) {
    osip_header_t *h, *oldh;
    int i, oldpos = -1;
  
    if (sip == NULL || hname == NULL)
      return OSIP_BADPARAMETER;
  
    oldpos = osip_message_header_get_byname(sip, hname, 0, &oldh);
  
    i = osip_header_init(&h);
  
    if (i != 0)
      return i;
  
    h->hname = (char *) osip_malloc(strlen(hname) + 1);
  
    if (h->hname == NULL) {
      osip_header_free(h);
      return OSIP_NOMEM;
    }
  
    osip_clrncpy(h->hname, hname, strlen(hname));
  
    if (hvalue != NULL) { /* some headers can be null ("subject:") */
      h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  
      if (h->hvalue == NULL) {
        osip_header_free(h);
        return OSIP_NOMEM;
      }
  
      osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  
    } else
      h->hvalue = NULL;
  
    if (oldpos != -1) {
      osip_list_remove(&sip->headers, oldpos);
      osip_header_free(oldh);
    }
  
    sip->message_property = 2;
    osip_list_add(&sip->headers, h, -1);
    return OSIP_SUCCESS; /* ok */
  }
  
  #ifndef MINISIZE
  /* Add a header to a SIP message at the top of the list.    */
  /* INPUT :  char *hname | pointer to a header name.         */
  /* INPUT :  char *hvalue | pointer to a header value.       */
  /* OUTPUT: osip_message_t *sip | structure to save results.          */
  /* returns -1 on error. */
  int osip_message_set_topheader(osip_message_t *sip, const char *hname, const char *hvalue) {
    osip_header_t *h;
    int i;
  
    if (sip == NULL || hname == NULL)
      return OSIP_BADPARAMETER;
  
    i = osip_header_init(&h);
  
    if (i != 0)
      return i;
  
    h->hname = (char *) osip_malloc(strlen(hname) + 1);
  
    if (h->hname == NULL) {
      osip_header_free(h);
      return OSIP_NOMEM;
    }
  
    osip_clrncpy(h->hname, hname, strlen(hname));
  
    if (hvalue != NULL) { /* some headers can be null ("subject:") */
      h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  
      if (h->hvalue == NULL) {
        osip_header_free(h);
        return OSIP_NOMEM;
      }
  
      osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  
    } else
      h->hvalue = NULL;
  
    sip->message_property = 2;
    osip_list_add(&sip->headers, h, 0);
    return OSIP_SUCCESS; /* ok */
  }
  
  /* Get a header in a SIP message.                       */
  /* INPUT : int pos | position of number in message.     */
  /* OUTPUT: osip_message_t *sip | structure to scan for a header .*/
  /* return null on error. */
  int osip_message_get_header(const osip_message_t *sip, int pos, osip_header_t **dest) {
    *dest = NULL;
  
    if (osip_list_size(&sip->headers) <= pos)
      return OSIP_UNDEFINED_ERROR; /* NULL */
  
    *dest = (osip_header_t *) osip_list_get(&sip->headers, pos);
    return pos;
  }
  #endif
  
  /* Get a header in a SIP message.                       */
  /* INPUT : int pos | position where we start the search */
  /* OUTPUT: osip_message_t *sip | structure to look for header.   */
  /* return the current position of the header found      */
  /* and -1 on error. */
  int osip_message_header_get_byname(const osip_message_t *sip, const char *hname, int pos, osip_header_t **dest) {
    int i;
    osip_header_t *tmp;
  
    *dest = NULL;
    i = pos;
  
    if (osip_list_size(&sip->headers) <= pos)
      return OSIP_UNDEFINED_ERROR; /* NULL */
  
    while (osip_list_size(&sip->headers) > i) {
      tmp = (osip_header_t *) osip_list_get(&sip->headers, i);
  
      if (osip_strcasecmp(tmp->hname, hname) == 0) {
        *dest = tmp;
        return i;
      }
  
      i++;
    }
  
    return OSIP_UNDEFINED_ERROR; /* not found */
  }
  
  int osip_header_init(osip_header_t **header) {
    *header = (osip_header_t *) osip_malloc(sizeof(osip_header_t));
  
    if (*header == NULL)
      return OSIP_NOMEM;
  
    (*header)->hname = NULL;
    (*header)->hvalue = NULL;
    return OSIP_SUCCESS;
  }
  
  void osip_header_free(osip_header_t *header) {
    if (header == NULL)
      return;
  
    osip_free(header->hname);
    osip_free(header->hvalue);
    header->hname = NULL;
    header->hvalue = NULL;
  
    osip_free(header);
  }
  
  /* returns the header as a string.    */
  /* INPUT : osip_header_t *header | header. */
  /* returns null on error. */
  int osip_header_to_str(const osip_header_t *header, char **dest) {
    size_t len, hlen;
  
    *dest = NULL;
  
    if ((header == NULL) || (header->hname == NULL))
      return OSIP_BADPARAMETER;
  
    len = 0;
    hlen = strlen(header->hname);
  
    if (header->hvalue != NULL)
      len = strlen(header->hvalue);
  
    *dest = (char *) osip_malloc(hlen + len + 3);
  
    if (*dest == NULL)
      return OSIP_NOMEM;
  
    if (header->hvalue != NULL)
      snprintf(*dest, hlen + len + 3, "%s: %s", header->hname, header->hvalue);
  
    else
      snprintf(*dest, hlen + len + 3, "%s: ", header->hname);
  
    if (*dest[0] >= 'a' && *dest[0] <= 'z')
      *dest[0] = (*dest[0] - 32);
  
    return OSIP_SUCCESS;
  }
  
  char *osip_header_get_name(const osip_header_t *header) {
    if (header == NULL)
      return NULL;
  
    return header->hname;
  }
  
  void osip_header_set_name(osip_header_t *header, char *name) {
    header->hname = name;
  }
  
  char *osip_header_get_value(const osip_header_t *header) {
    if (header == NULL)
      return NULL;
  
    return header->hvalue;
  }
  
  void osip_header_set_value(osip_header_t *header, char *value) {
    header->hvalue = value;
  }
  
  int osip_header_clone(const osip_header_t *header, osip_header_t **dest) {
    int i;
    osip_header_t *he;
  
    *dest = NULL;
  
    if (header == NULL)
      return OSIP_BADPARAMETER;
  
    if (header->hname == NULL)
      return OSIP_BADPARAMETER;
  
    i = osip_header_init(&he);
  
    if (i != 0)
      return i;
  
    he->hname = osip_strdup(header->hname);
  
    if (he->hname == NULL) {
      osip_header_free(he);
      return OSIP_NOMEM;
    }
  
    if (header->hvalue != NULL) {
      he->hvalue = osip_strdup(header->hvalue);
  
      if (he->hvalue == NULL) {
        osip_header_free(he);
        return OSIP_NOMEM;
      }
    }
  
    *dest = he;
    return OSIP_SUCCESS;
  }