Bug Summary

File:libs/esl/src/esl_event.c
Location:line 833, column 5
Description:Value stored to 'buf' is never read

Annotated Source Code

1/*
2 * Copyright (c) 2007-2014, Anthony Minessale II
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of the original author; nor the names of any contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35#include <esl.h>
36#include <esl_event.h>
37
38static char *my_dup(const char *s)
39{
40 size_t len = strlen(s) + 1;
41 void *new = malloc(len);
42 esl_assert(new)((new) ? (void) (0) : __assert_fail ("new", "libs/esl/src/esl_event.c"
, 42, __PRETTY_FUNCTION__))
;
43
44 return (char *) memcpy(new, s, len);
45}
46
47#ifndef ALLOC
48#define ALLOC(size)malloc(size) malloc(size)
49#endif
50#ifndef DUP
51#define DUP(str)my_dup(str) my_dup(str)
52#endif
53#ifndef FREE
54#define FREE(ptr)if (ptr) free(ptr); ptr = ((void*)0) esl_safe_free(ptr)if (ptr) free(ptr); ptr = ((void*)0)
55#endif
56
57/* make sure this is synced with the esl_event_types_t enum in esl_types.h
58 also never put any new ones before EVENT_ALL
59*/
60static const char *EVENT_NAMES[] = {
61 "CUSTOM",
62 "CLONE",
63 "CHANNEL_CREATE",
64 "CHANNEL_DESTROY",
65 "CHANNEL_STATE",
66 "CHANNEL_CALLSTATE",
67 "CHANNEL_ANSWER",
68 "CHANNEL_HANGUP",
69 "CHANNEL_HANGUP_COMPLETE",
70 "CHANNEL_EXECUTE",
71 "CHANNEL_EXECUTE_COMPLETE",
72 "CHANNEL_HOLD",
73 "CHANNEL_UNHOLD",
74 "CHANNEL_BRIDGE",
75 "CHANNEL_UNBRIDGE",
76 "CHANNEL_PROGRESS",
77 "CHANNEL_PROGRESS_MEDIA",
78 "CHANNEL_OUTGOING",
79 "CHANNEL_PARK",
80 "CHANNEL_UNPARK",
81 "CHANNEL_APPLICATION",
82 "CHANNEL_ORIGINATE",
83 "CHANNEL_UUID",
84 "API",
85 "LOG",
86 "INBOUND_CHAN",
87 "OUTBOUND_CHAN",
88 "STARTUP",
89 "SHUTDOWN",
90 "PUBLISH",
91 "UNPUBLISH",
92 "TALK",
93 "NOTALK",
94 "SESSION_CRASH",
95 "MODULE_LOAD",
96 "MODULE_UNLOAD",
97 "DTMF",
98 "MESSAGE",
99 "PRESENCE_IN",
100 "NOTIFY_IN",
101 "PRESENCE_OUT",
102 "PRESENCE_PROBE",
103 "MESSAGE_WAITING",
104 "MESSAGE_QUERY",
105 "ROSTER",
106 "CODEC",
107 "BACKGROUND_JOB",
108 "DETECTED_SPEECH",
109 "DETECTED_TONE",
110 "PRIVATE_COMMAND",
111 "HEARTBEAT",
112 "TRAP",
113 "ADD_SCHEDULE",
114 "DEL_SCHEDULE",
115 "EXE_SCHEDULE",
116 "RE_SCHEDULE",
117 "RELOADXML",
118 "NOTIFY",
119 "PHONE_FEATURE",
120 "PHONE_FEATURE_SUBSCRIBE",
121 "SEND_MESSAGE",
122 "RECV_MESSAGE",
123 "REQUEST_PARAMS",
124 "CHANNEL_DATA",
125 "GENERAL",
126 "COMMAND",
127 "SESSION_HEARTBEAT",
128 "CLIENT_DISCONNECTED",
129 "SERVER_DISCONNECTED",
130 "SEND_INFO",
131 "RECV_INFO",
132 "RECV_RTCP_MESSAGE",
133 "CALL_SECURE",
134 "NAT",
135 "RECORD_START",
136 "RECORD_STOP",
137 "PLAYBACK_START",
138 "PLAYBACK_STOP",
139 "CALL_UPDATE",
140 "FAILURE",
141 "SOCKET_DATA",
142 "MEDIA_BUG_START",
143 "MEDIA_BUG_STOP",
144 "CONFERENCE_DATA_QUERY",
145 "CONFERENCE_DATA",
146 "CALL_SETUP_REQ",
147 "CALL_SETUP_RESULT",
148 "CALL_DETAIL",
149 "DEVICE_STATE",
150 "ALL"
151};
152
153ESL_DECLARE(const char *)const char *esl_event_name(esl_event_types_t event)
154{
155 return EVENT_NAMES[event];
156}
157
158ESL_DECLARE(esl_status_t)esl_status_t esl_name_event(const char *name, esl_event_types_t *type)
159{
160 esl_event_types_t x;
161
162 for (x = 0; x <= ESL_EVENT_ALL; x++) {
163 if ((strlen(name) > 13 && !strcasecmp(name + 13, EVENT_NAMES[x])) || !strcasecmp(name, EVENT_NAMES[x])) {
164 *type = x;
165 return ESL_SUCCESS;
166 }
167 }
168
169 return ESL_FAIL;
170}
171
172
173ESL_DECLARE(esl_status_t)esl_status_t esl_event_create_subclass(esl_event_t **event, esl_event_types_t event_id, const char *subclass_name)
174{
175 *event = NULL((void*)0);
176
177 if ((event_id != ESL_EVENT_CLONE && event_id != ESL_EVENT_CUSTOM) && subclass_name) {
178 return ESL_FAIL;
179 }
180
181 *event = ALLOC(sizeof(esl_event_t))malloc(sizeof(esl_event_t));
182 esl_assert(*event)((*event) ? (void) (0) : __assert_fail ("*event", "libs/esl/src/esl_event.c"
, 182, __PRETTY_FUNCTION__))
;
183
184
185 memset(*event, 0, sizeof(esl_event_t));
186
187 if (event_id != ESL_EVENT_CLONE) {
188 (*event)->event_id = event_id;
189 esl_event_add_header_string(*event, ESL_STACK_BOTTOM, "Event-Name", esl_event_name((*event)->event_id));
190 }
191
192 if (subclass_name) {
193 (*event)->subclass_name = DUP(subclass_name)my_dup(subclass_name);
194 esl_event_add_header_string(*event, ESL_STACK_BOTTOM, "Event-Subclass", subclass_name);
195 }
196
197 return ESL_SUCCESS;
198}
199
200
201ESL_DECLARE(const char *)const char *esl_priority_name(esl_priority_t priority)
202{
203 switch (priority) { /*lol */
204 case ESL_PRIORITY_NORMAL:
205 return "NORMAL";
206 case ESL_PRIORITY_LOW:
207 return "LOW";
208 case ESL_PRIORITY_HIGH:
209 return "HIGH";
210 default:
211 return "INVALID";
212 }
213}
214
215ESL_DECLARE(esl_status_t)esl_status_t esl_event_set_priority(esl_event_t *event, esl_priority_t priority)
216{
217 event->priority = priority;
218 esl_event_add_header_string(event, ESL_STACK_TOP, "priority", esl_priority_name(priority));
219 return ESL_SUCCESS;
220}
221
222#define ESL_HASH_KEY_STRING-1 -1
223
224static unsigned int esl_ci_hashfunc_default(const char *char_key, esl_ssize_t *klen)
225
226{
227 unsigned int hash = 0;
228 const unsigned char *key = (const unsigned char *)char_key;
229 const unsigned char *p;
230 esl_ssize_t i;
231
232 if (*klen == ESL_HASH_KEY_STRING-1) {
233 for (p = key; *p; p++) {
234 hash = hash * 33 + tolower(*p)(__extension__ ({ int __res; if (sizeof (*p) > 1) { if (__builtin_constant_p
(*p)) { int __c = (*p); __res = __c < -128 || __c > 255
? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower
(*p); } else __res = (*__ctype_tolower_loc ())[(int) (*p)]; __res
; }))
;
235 }
236 *klen = p - key;
237 }
238 else {
239 for (p = key, i = *klen; i; i--, p++) {
240 hash = hash * 33 + tolower(*p)(__extension__ ({ int __res; if (sizeof (*p) > 1) { if (__builtin_constant_p
(*p)) { int __c = (*p); __res = __c < -128 || __c > 255
? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower
(*p); } else __res = (*__ctype_tolower_loc ())[(int) (*p)]; __res
; }))
;
241 }
242 }
243
244 return hash;
245}
246
247ESL_DECLARE(esl_event_header_t *)esl_event_header_t * esl_event_get_header_ptr(esl_event_t *event, const char *header_name)
248{
249 esl_event_header_t *hp;
250 esl_ssize_t hlen = -1;
251 unsigned long hash = 0;
252
253 esl_assert(event)((event) ? (void) (0) : __assert_fail ("event", "libs/esl/src/esl_event.c"
, 253, __PRETTY_FUNCTION__))
;
254
255 if (!header_name)
256 return NULL((void*)0);
257
258 hash = esl_ci_hashfunc_default(header_name, &hlen);
259
260 for (hp = event->headers; hp; hp = hp->next) {
261 if ((!hp->hash || hash == hp->hash) && !strcasecmp(hp->name, header_name)) {
262 return hp;
263 }
264 }
265 return NULL((void*)0);
266}
267
268ESL_DECLARE(char *)char * esl_event_get_header_idx(esl_event_t *event, const char *header_name, int idx)
269{
270 esl_event_header_t *hp;
271
272 if ((hp = esl_event_get_header_ptr(event, header_name))) {
273 if (idx > -1) {
274 if (idx < hp->idx) {
275 return hp->array[idx];
276 } else {
277 return NULL((void*)0);
278 }
279 }
280
281 return hp->value;
282 } else if (header_name && !strcmp(header_name, "_body")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(header_name) && __builtin_constant_p ("_body") &&
(__s1_len = __builtin_strlen (header_name), __s2_len = __builtin_strlen
("_body"), (!((size_t)(const void *)((header_name) + 1) - (size_t
)(const void *)(header_name) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)(("_body") + 1) - (size_t)(const void
*)("_body") == 1) || __s2_len >= 4)) ? __builtin_strcmp (
header_name, "_body") : (__builtin_constant_p (header_name) &&
((size_t)(const void *)((header_name) + 1) - (size_t)(const void
*)(header_name) == 1) && (__s1_len = __builtin_strlen
(header_name), __s1_len < 4) ? (__builtin_constant_p ("_body"
) && ((size_t)(const void *)(("_body") + 1) - (size_t
)(const void *)("_body") == 1) ? __builtin_strcmp (header_name
, "_body") : (__extension__ ({ const unsigned char *__s2 = (const
unsigned char *) (const char *) ("_body"); int __result = ((
(const unsigned char *) (const char *) (header_name))[0] - __s2
[0]); if (__s1_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) (header_name))[1]
- __s2[1]); if (__s1_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) (header_name))[2]
- __s2[2]); if (__s1_len > 2 && __result == 0) __result
= (((const unsigned char *) (const char *) (header_name))[3]
- __s2[3]); } } __result; }))) : (__builtin_constant_p ("_body"
) && ((size_t)(const void *)(("_body") + 1) - (size_t
)(const void *)("_body") == 1) && (__s2_len = __builtin_strlen
("_body"), __s2_len < 4) ? (__builtin_constant_p (header_name
) && ((size_t)(const void *)((header_name) + 1) - (size_t
)(const void *)(header_name) == 1) ? __builtin_strcmp (header_name
, "_body") : (- (__extension__ ({ const unsigned char *__s2 =
(const unsigned char *) (const char *) (header_name); int __result
= (((const unsigned char *) (const char *) ("_body"))[0] - __s2
[0]); if (__s2_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) ("_body"))[1] - __s2
[1]); if (__s2_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) ("_body"))[2] - __s2
[2]); if (__s2_len > 2 && __result == 0) __result =
(((const unsigned char *) (const char *) ("_body"))[3] - __s2
[3]); } } __result; })))) : __builtin_strcmp (header_name, "_body"
)))); })
) {
283 return event->body;
284 }
285
286 return NULL((void*)0);
287}
288
289ESL_DECLARE(char *)char *esl_event_get_body(esl_event_t *event)
290{
291 return (event ? event->body : NULL((void*)0));
292}
293
294ESL_DECLARE(esl_status_t)esl_status_t esl_event_del_header_val(esl_event_t *event, const char *header_name, const char *val)
295{
296 esl_event_header_t *hp, *lp = NULL((void*)0), *tp;
297 esl_status_t status = (esl_status_t) ESL_FALSE;
298 int x = 0;
299 esl_ssize_t hlen = -1;
300 unsigned long hash = 0;
301
302 tp = event->headers;
303 while (tp) {
304 hp = tp;
305 tp = tp->next;
306
307 x++;
308 esl_assert(x < 1000000)((x < 1000000) ? (void) (0) : __assert_fail ("x < 1000000"
, "libs/esl/src/esl_event.c", 308, __PRETTY_FUNCTION__))
;
309 hash = esl_ci_hashfunc_default(header_name, &hlen);
310
311 if ((!hp->hash || hash == hp->hash) && (hp->name && !strcasecmp(header_name, hp->name)) && (esl_strlen_zero(val)(!val || *(val) == '\0') || (hp->value && !strcmp(hp->value, val)__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(hp->value) && __builtin_constant_p (val) &&
(__s1_len = __builtin_strlen (hp->value), __s2_len = __builtin_strlen
(val), (!((size_t)(const void *)((hp->value) + 1) - (size_t
)(const void *)(hp->value) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)((val) + 1) - (size_t)(const void *
)(val) == 1) || __s2_len >= 4)) ? __builtin_strcmp (hp->
value, val) : (__builtin_constant_p (hp->value) &&
((size_t)(const void *)((hp->value) + 1) - (size_t)(const
void *)(hp->value) == 1) && (__s1_len = __builtin_strlen
(hp->value), __s1_len < 4) ? (__builtin_constant_p (val
) && ((size_t)(const void *)((val) + 1) - (size_t)(const
void *)(val) == 1) ? __builtin_strcmp (hp->value, val) : (
__extension__ ({ const unsigned char *__s2 = (const unsigned char
*) (const char *) (val); int __result = (((const unsigned char
*) (const char *) (hp->value))[0] - __s2[0]); if (__s1_len
> 0 && __result == 0) { __result = (((const unsigned
char *) (const char *) (hp->value))[1] - __s2[1]); if (__s1_len
> 1 && __result == 0) { __result = (((const unsigned
char *) (const char *) (hp->value))[2] - __s2[2]); if (__s1_len
> 2 && __result == 0) __result = (((const unsigned
char *) (const char *) (hp->value))[3] - __s2[3]); } } __result
; }))) : (__builtin_constant_p (val) && ((size_t)(const
void *)((val) + 1) - (size_t)(const void *)(val) == 1) &&
(__s2_len = __builtin_strlen (val), __s2_len < 4) ? (__builtin_constant_p
(hp->value) && ((size_t)(const void *)((hp->value
) + 1) - (size_t)(const void *)(hp->value) == 1) ? __builtin_strcmp
(hp->value, val) : (- (__extension__ ({ const unsigned char
*__s2 = (const unsigned char *) (const char *) (hp->value
); int __result = (((const unsigned char *) (const char *) (val
))[0] - __s2[0]); if (__s2_len > 0 && __result == 0
) { __result = (((const unsigned char *) (const char *) (val)
)[1] - __s2[1]); if (__s2_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) (val)
)[2] - __s2[2]); if (__s2_len > 2 && __result == 0
) __result = (((const unsigned char *) (const char *) (val))[
3] - __s2[3]); } } __result; })))) : __builtin_strcmp (hp->
value, val)))); })
))) {
312 if (lp) {
313 lp->next = hp->next;
314 } else {
315 event->headers = hp->next;
316 }
317 if (hp == event->last_header || !hp->next) {
318 event->last_header = lp;
319 }
320 FREE(hp->name)if (hp->name) free(hp->name); hp->name = ((void*)0);
321
322 if (hp->idx) {
323 int i = 0;
324
325 for (i = 0; i < hp->idx; i++) {
326 FREE(hp->array[i])if (hp->array[i]) free(hp->array[i]); hp->array[i] =
((void*)0)
;
327 }
328 FREE(hp->array)if (hp->array) free(hp->array); hp->array = ((void*)
0)
;
329 }
330
331 FREE(hp->value)if (hp->value) free(hp->value); hp->value = ((void*)
0)
;
332
333 memset(hp, 0, sizeof(*hp));
334#ifdef ESL_EVENT_RECYCLE
335 if (esl_queue_trypush(EVENT_HEADER_RECYCLE_QUEUE, hp) != ESL_SUCCESS) {
336 FREE(hp)if (hp) free(hp); hp = ((void*)0);
337 }
338#else
339 FREE(hp)if (hp) free(hp); hp = ((void*)0);
340#endif
341 status = ESL_SUCCESS;
342 } else {
343 lp = hp;
344 }
345 }
346
347 return status;
348}
349
350static esl_event_header_t *new_header(const char *header_name)
351{
352 esl_event_header_t *header;
353
354#ifdef ESL_EVENT_RECYCLE
355 void *pop;
356 if (esl_queue_trypop(EVENT_HEADER_RECYCLE_QUEUE, &pop) == ESL_SUCCESS) {
357 header = (esl_event_header_t *) pop;
358 } else {
359#endif
360 header = ALLOC(sizeof(*header))malloc(sizeof(*header));
361 esl_assert(header)((header) ? (void) (0) : __assert_fail ("header", "libs/esl/src/esl_event.c"
, 361, __PRETTY_FUNCTION__))
;
362#ifdef ESL_EVENT_RECYCLE
363 }
364#endif
365
366 memset(header, 0, sizeof(*header));
367 header->name = DUP(header_name)my_dup(header_name);
368
369 return header;
370
371}
372
373ESL_DECLARE(int)int esl_event_add_array(esl_event_t *event, const char *var, const char *val)
374{
375 char *data;
376 char **array;
377 int max = 0;
378 int len;
379 const char *p;
380 int i;
381
382 if (strlen(val) < 8) {
383 return -1;
384 }
385
386 p = val + 7;
387
388 max = 1;
389
390 while((p = strstr(p, "|:"))) {
391 max++;
392 p += 2;
393 }
394
395 data = strdup(val + 7);
396
397 len = (sizeof(char *) * max) + 1;
398 array = malloc(len);
399 esl_assert(array)((array) ? (void) (0) : __assert_fail ("array", "libs/esl/src/esl_event.c"
, 399, __PRETTY_FUNCTION__))
;
400 memset(array, 0, len);
401
402 esl_separate_string_string(data, "|:", array, max);
403
404 for(i = 0; i < max; i++) {
405 esl_event_add_header_string(event, ESL_STACK_PUSH, var, array[i]);
406 }
407
408 free(array);
409 free(data);
410
411 return 0;
412}
413
414static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data)
415{
416 esl_event_header_t *header = NULL((void*)0);
417 esl_ssize_t hlen = -1;
418 int exists = 0, fly = 0;
419 char *index_ptr;
420 int index = 0;
421 char *real_header_name = NULL((void*)0);
422
423 if (!strcmp(header_name, "_body")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(header_name) && __builtin_constant_p ("_body") &&
(__s1_len = __builtin_strlen (header_name), __s2_len = __builtin_strlen
("_body"), (!((size_t)(const void *)((header_name) + 1) - (size_t
)(const void *)(header_name) == 1) || __s1_len >= 4) &&
(!((size_t)(const void *)(("_body") + 1) - (size_t)(const void
*)("_body") == 1) || __s2_len >= 4)) ? __builtin_strcmp (
header_name, "_body") : (__builtin_constant_p (header_name) &&
((size_t)(const void *)((header_name) + 1) - (size_t)(const void
*)(header_name) == 1) && (__s1_len = __builtin_strlen
(header_name), __s1_len < 4) ? (__builtin_constant_p ("_body"
) && ((size_t)(const void *)(("_body") + 1) - (size_t
)(const void *)("_body") == 1) ? __builtin_strcmp (header_name
, "_body") : (__extension__ ({ const unsigned char *__s2 = (const
unsigned char *) (const char *) ("_body"); int __result = ((
(const unsigned char *) (const char *) (header_name))[0] - __s2
[0]); if (__s1_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) (header_name))[1]
- __s2[1]); if (__s1_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) (header_name))[2]
- __s2[2]); if (__s1_len > 2 && __result == 0) __result
= (((const unsigned char *) (const char *) (header_name))[3]
- __s2[3]); } } __result; }))) : (__builtin_constant_p ("_body"
) && ((size_t)(const void *)(("_body") + 1) - (size_t
)(const void *)("_body") == 1) && (__s2_len = __builtin_strlen
("_body"), __s2_len < 4) ? (__builtin_constant_p (header_name
) && ((size_t)(const void *)((header_name) + 1) - (size_t
)(const void *)(header_name) == 1) ? __builtin_strcmp (header_name
, "_body") : (- (__extension__ ({ const unsigned char *__s2 =
(const unsigned char *) (const char *) (header_name); int __result
= (((const unsigned char *) (const char *) ("_body"))[0] - __s2
[0]); if (__s2_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) ("_body"))[1] - __s2
[1]); if (__s2_len > 1 && __result == 0) { __result
= (((const unsigned char *) (const char *) ("_body"))[2] - __s2
[2]); if (__s2_len > 2 && __result == 0) __result =
(((const unsigned char *) (const char *) ("_body"))[3] - __s2
[3]); } } __result; })))) : __builtin_strcmp (header_name, "_body"
)))); })
) {
424 esl_event_set_body(event, data);
425 }
426
427 if ((index_ptr = strchr(header_name, '[')(__extension__ (__builtin_constant_p ('[') && !__builtin_constant_p
(header_name) && ('[') == '\0' ? (char *) __rawmemchr
(header_name, '[') : __builtin_strchr (header_name, '[')))
)) {
428 index_ptr++;
429 index = atoi(index_ptr);
430 real_header_name = DUP(header_name)my_dup(header_name);
431 if ((index_ptr = strchr(real_header_name, '[')(__extension__ (__builtin_constant_p ('[') && !__builtin_constant_p
(real_header_name) && ('[') == '\0' ? (char *) __rawmemchr
(real_header_name, '[') : __builtin_strchr (real_header_name
, '[')))
)) {
432 *index_ptr++ = '\0';
433 }
434 header_name = real_header_name;
435 }
436
437 if (index_ptr || (stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
438
439 if (!(header = esl_event_get_header_ptr(event, header_name)) && index_ptr) {
440
441 header = new_header(header_name);
442
443 if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)((event)->flags & ESL_EF_UNIQ_HEADERS)) {
444 esl_event_del_header(event, header_name)esl_event_del_header_val(event, header_name, ((void*)0));
445 }
446
447 fly++;
448 }
449
450 if (header || (header = esl_event_get_header_ptr(event, header_name))) {
451
452 if (index_ptr) {
453 if (index > -1 && index <= 4000) {
454 if (index < header->idx) {
455 FREE(header->array[index])if (header->array[index]) free(header->array[index]); header
->array[index] = ((void*)0)
;
456 header->array[index] = DUP(data)my_dup(data);
457 } else {
458 int i;
459 char **m;
460
461 m = realloc(header->array, sizeof(char *) * (index + 1));
462 esl_assert(m)((m) ? (void) (0) : __assert_fail ("m", "libs/esl/src/esl_event.c"
, 462, __PRETTY_FUNCTION__))
;
463 header->array = m;
464 for (i = header->idx; i < index; i++) {
465 m[i] = DUP("")my_dup("");
466 }
467 m[index] = DUP(data)my_dup(data);
468 header->idx = index + 1;
469 if (!fly) {
470 exists = 1;
471 }
472
473 goto redraw;
474 }
475 }
476 goto end;
477 } else {
478 if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
479 exists++;
480 stack &= ~(ESL_STACK_TOP | ESL_STACK_BOTTOM);
481 } else {
482 header = NULL((void*)0);
483 }
484 }
485 }
486 }
487
488
489 if (!header) {
490
491 if (esl_strlen_zero(data)(!data || *(data) == '\0')) {
492 esl_event_del_header(event, header_name)esl_event_del_header_val(event, header_name, ((void*)0));
493 FREE(data)if (data) free(data); data = ((void*)0);
494 goto end;
495 }
496
497 if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)((event)->flags & ESL_EF_UNIQ_HEADERS)) {
498 esl_event_del_header(event, header_name)esl_event_del_header_val(event, header_name, ((void*)0));
499 }
500
501 if (strstr(data, "ARRAY::")) {
502 esl_event_add_array(event, header_name, data);
503 FREE(data)if (data) free(data); data = ((void*)0);
504 goto end;
505 }
506
507
508 header = new_header(header_name);
509 }
510
511 if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
512 char **m = NULL((void*)0);
513 esl_size_t len = 0;
514 char *hv;
515 int i = 0, j = 0;
516
517 if (header->value && !header->idx) {
518 m = malloc(sizeof(char *));
519 esl_assert(m)((m) ? (void) (0) : __assert_fail ("m", "libs/esl/src/esl_event.c"
, 519, __PRETTY_FUNCTION__))
;
520 m[0] = header->value;
521 header->value = NULL((void*)0);
522 header->array = m;
523 header->idx++;
524 m = NULL((void*)0);
525 }
526
527 i = header->idx + 1;
528 m = realloc(header->array, sizeof(char *) * i);
529 esl_assert(m)((m) ? (void) (0) : __assert_fail ("m", "libs/esl/src/esl_event.c"
, 529, __PRETTY_FUNCTION__))
;
530
531 if ((stack & ESL_STACK_PUSH)) {
532 m[header->idx] = data;
533 } else if ((stack & ESL_STACK_UNSHIFT)) {
534 for (j = header->idx; j > 0; j--) {
535 m[j] = m[j-1];
536 }
537 m[0] = data;
538 }
539
540 header->idx++;
541 header->array = m;
542
543 redraw:
544 len = 0;
545 for(j = 0; j < header->idx; j++) {
546 len += strlen(header->array[j]) + 2;
547 }
548
549 if (len) {
550 len += 8;
551 hv = realloc(header->value, len);
552 esl_assert(hv)((hv) ? (void) (0) : __assert_fail ("hv", "libs/esl/src/esl_event.c"
, 552, __PRETTY_FUNCTION__))
;
553 header->value = hv;
554
555 if (header->idx > 1) {
556 esl_snprintf(header->value, len, "ARRAY::");
557 } else {
558 *header->value = '\0';
559 }
560
561 for(j = 0; j < header->idx; j++) {
562 esl_snprintf(header->value + strlen(header->value), len - strlen(header->value), "%s%s", j == 0 ? "" : "|:", header->array[j]);
563 }
564 }
565
566 } else {
567 header->value = data;
568 }
569
570 if (!exists) {
571 header->hash = esl_ci_hashfunc_default(header->name, &hlen);
572
573 if ((stack & ESL_STACK_TOP)) {
574 header->next = event->headers;
575 event->headers = header;
576 if (!event->last_header) {
577 event->last_header = header;
578 }
579 } else {
580 if (event->last_header) {
581 event->last_header->next = header;
582 } else {
583 event->headers = header;
584 header->next = NULL((void*)0);
585 }
586 event->last_header = header;
587 }
588 }
589
590 end:
591
592 esl_safe_free(real_header_name)if (real_header_name) free(real_header_name); real_header_name
= ((void*)0)
;
593
594 return ESL_SUCCESS;
595}
596
597ESL_DECLARE(esl_status_t)esl_status_t esl_event_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *fmt, ...)
598{
599 int ret = 0;
600 char *data;
601 va_list ap;
602
603 va_start(ap, fmt)__builtin_va_start(ap, fmt);
604 ret = esl_vasprintf(&data, fmt, ap);
605 va_end(ap)__builtin_va_end(ap);
606
607 if (ret == -1) {
608 return ESL_FAIL;
609 }
610
611 return esl_event_base_add_header(event, stack, header_name, data);
612}
613
614ESL_DECLARE(esl_status_t)esl_status_t esl_event_add_header_string(esl_event_t *event, esl_stack_t stack, const char *header_name, const char *data)
615{
616 if (data) {
617 return esl_event_base_add_header(event, stack, header_name, DUP(data)my_dup(data));
618 }
619 return ESL_FAIL;
620}
621
622ESL_DECLARE(esl_status_t)esl_status_t esl_event_set_body(esl_event_t *event, const char *body)
623{
624 esl_safe_free(event->body)if (event->body) free(event->body); event->body = ((
void*)0)
;
625
626 if (body) {
627 event->body = DUP(body)my_dup(body);
628 }
629
630 return ESL_SUCCESS;
631}
632
633ESL_DECLARE(esl_status_t)esl_status_t esl_event_add_body(esl_event_t *event, const char *fmt, ...)
634{
635 int ret = 0;
636 char *data;
637
638 va_list ap;
639 if (fmt) {
640 va_start(ap, fmt)__builtin_va_start(ap, fmt);
641 ret = esl_vasprintf(&data, fmt, ap);
642 va_end(ap)__builtin_va_end(ap);
643
644 if (ret == -1) {
645 return ESL_FAIL;
646 } else {
647 esl_safe_free(event->body)if (event->body) free(event->body); event->body = ((
void*)0)
;
648 event->body = data;
649 return ESL_SUCCESS;
650 }
651 } else {
652 return ESL_FAIL;
653 }
654}
655
656
657ESL_DECLARE(void)void esl_event_destroy(esl_event_t **event)
658{
659 esl_event_t *ep = *event;
660 esl_event_header_t *hp, *this;
661
662 if (ep) {
663 for (hp = ep->headers; hp;) {
664 this = hp;
665 hp = hp->next;
666 FREE(this->name)if (this->name) free(this->name); this->name = ((void
*)0)
;
667
668 if (this->idx) {
669 int i = 0;
670
671 for (i = 0; i < this->idx; i++) {
672 FREE(this->array[i])if (this->array[i]) free(this->array[i]); this->array
[i] = ((void*)0)
;
673 }
674 FREE(this->array)if (this->array) free(this->array); this->array = ((
void*)0)
;
675 }
676
677 FREE(this->value)if (this->value) free(this->value); this->value = ((
void*)0)
;
678
679
680#ifdef ESL_EVENT_RECYCLE
681 if (esl_queue_trypush(EVENT_HEADER_RECYCLE_QUEUE, this) != ESL_SUCCESS) {
682 FREE(this)if (this) free(this); this = ((void*)0);
683 }
684#else
685 FREE(this)if (this) free(this); this = ((void*)0);
686#endif
687
688
689 }
690 FREE(ep->body)if (ep->body) free(ep->body); ep->body = ((void*)0);
691 FREE(ep->subclass_name)if (ep->subclass_name) free(ep->subclass_name); ep->
subclass_name = ((void*)0)
;
692#ifdef ESL_EVENT_RECYCLE
693 if (esl_queue_trypush(EVENT_RECYCLE_QUEUE, ep) != ESL_SUCCESS) {
694 FREE(ep)if (ep) free(ep); ep = ((void*)0);
695 }
696#else
697 FREE(ep)if (ep) free(ep); ep = ((void*)0);
698#endif
699
700 }
701 *event = NULL((void*)0);
702}
703
704ESL_DECLARE(void)void esl_event_merge(esl_event_t *event, esl_event_t *tomerge)
705{
706 esl_event_header_t *hp;
707
708 esl_assert(tomerge && event)((tomerge && event) ? (void) (0) : __assert_fail ("tomerge && event"
, "libs/esl/src/esl_event.c", 708, __PRETTY_FUNCTION__))
;
709
710 for (hp = tomerge->headers; hp; hp = hp->next) {
711 if (hp->idx) {
712 int i;
713
714 for(i = 0; i < hp->idx; i++) {
715 esl_event_add_header_string(event, ESL_STACK_PUSH, hp->name, hp->array[i]);
716 }
717 } else {
718 esl_event_add_header_string(event, ESL_STACK_BOTTOM, hp->name, hp->value);
719 }
720 }
721}
722
723ESL_DECLARE(esl_status_t)esl_status_t esl_event_dup(esl_event_t **event, esl_event_t *todup)
724{
725 esl_event_header_t *hp;
726
727 if (esl_event_create_subclass(event, ESL_EVENT_CLONE, todup->subclass_name) != ESL_SUCCESS) {
728 return ESL_GENERR;
729 }
730
731 (*event)->event_id = todup->event_id;
732 (*event)->event_user_data = todup->event_user_data;
733 (*event)->bind_user_data = todup->bind_user_data;
734 (*event)->flags = todup->flags;
735 for (hp = todup->headers; hp; hp = hp->next) {
736 if (todup->subclass_name && !strcmp(hp->name, "Event-Subclass")__extension__ ({ size_t __s1_len, __s2_len; (__builtin_constant_p
(hp->name) && __builtin_constant_p ("Event-Subclass"
) && (__s1_len = __builtin_strlen (hp->name), __s2_len
= __builtin_strlen ("Event-Subclass"), (!((size_t)(const void
*)((hp->name) + 1) - (size_t)(const void *)(hp->name) ==
1) || __s1_len >= 4) && (!((size_t)(const void *)
(("Event-Subclass") + 1) - (size_t)(const void *)("Event-Subclass"
) == 1) || __s2_len >= 4)) ? __builtin_strcmp (hp->name
, "Event-Subclass") : (__builtin_constant_p (hp->name) &&
((size_t)(const void *)((hp->name) + 1) - (size_t)(const void
*)(hp->name) == 1) && (__s1_len = __builtin_strlen
(hp->name), __s1_len < 4) ? (__builtin_constant_p ("Event-Subclass"
) && ((size_t)(const void *)(("Event-Subclass") + 1) -
(size_t)(const void *)("Event-Subclass") == 1) ? __builtin_strcmp
(hp->name, "Event-Subclass") : (__extension__ ({ const unsigned
char *__s2 = (const unsigned char *) (const char *) ("Event-Subclass"
); int __result = (((const unsigned char *) (const char *) (hp
->name))[0] - __s2[0]); if (__s1_len > 0 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
hp->name))[1] - __s2[1]); if (__s1_len > 1 && __result
== 0) { __result = (((const unsigned char *) (const char *) (
hp->name))[2] - __s2[2]); if (__s1_len > 2 && __result
== 0) __result = (((const unsigned char *) (const char *) (hp
->name))[3] - __s2[3]); } } __result; }))) : (__builtin_constant_p
("Event-Subclass") && ((size_t)(const void *)(("Event-Subclass"
) + 1) - (size_t)(const void *)("Event-Subclass") == 1) &&
(__s2_len = __builtin_strlen ("Event-Subclass"), __s2_len <
4) ? (__builtin_constant_p (hp->name) && ((size_t
)(const void *)((hp->name) + 1) - (size_t)(const void *)(hp
->name) == 1) ? __builtin_strcmp (hp->name, "Event-Subclass"
) : (- (__extension__ ({ const unsigned char *__s2 = (const unsigned
char *) (const char *) (hp->name); int __result = (((const
unsigned char *) (const char *) ("Event-Subclass"))[0] - __s2
[0]); if (__s2_len > 0 && __result == 0) { __result
= (((const unsigned char *) (const char *) ("Event-Subclass"
))[1] - __s2[1]); if (__s2_len > 1 && __result == 0
) { __result = (((const unsigned char *) (const char *) ("Event-Subclass"
))[2] - __s2[2]); if (__s2_len > 2 && __result == 0
) __result = (((const unsigned char *) (const char *) ("Event-Subclass"
))[3] - __s2[3]); } } __result; })))) : __builtin_strcmp (hp->
name, "Event-Subclass")))); })
) {
737 continue;
738 }
739
740 if (hp->idx) {
741 int i;
742 for (i = 0; i < hp->idx; i++) {
743 esl_event_add_header_string(*event, ESL_STACK_PUSH, hp->name, hp->array[i]);
744 }
745 } else {
746 esl_event_add_header_string(*event, ESL_STACK_BOTTOM, hp->name, hp->value);
747 }
748 }
749
750 if (todup->body) {
751 (*event)->body = DUP(todup->body)my_dup(todup->body);
752 }
753
754 (*event)->key = todup->key;
755
756 return ESL_SUCCESS;
757}
758
759
760ESL_DECLARE(esl_status_t)esl_status_t esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode)
761{
762 esl_size_t len = 0;
763 esl_event_header_t *hp;
764 esl_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0;
765 char *buf;
766 char *encode_buf = NULL((void*)0); /* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */
767
768 *str = NULL((void*)0);
769
770 dlen = blocksize * 2;
771
772 if (!(buf = malloc(dlen))) {
773 abort();
774 }
775
776 /* go ahead and give ourselves some space to work with, should save a few reallocs */
777 if (!(encode_buf = malloc(encode_len))) {
778 abort();
779 }
780
781 /* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */
782 for (hp = event->headers; hp; hp = hp->next) {
783 /*
784 * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX)
785 * so we could end up with a string that is 3 times the originals length, unlikely but rather
786 * be safe than destroy the string, also add one for the null. And try to be smart about using
787 * the memory, allocate and only reallocate if we need more. This avoids an alloc, free CPU
788 * destroying loop.
789 */
790
791 if (hp->idx) {
792 int i;
793 new_len = 0;
794 for(i = 0; i < hp->idx; i++) {
795 new_len += (strlen(hp->array[i]) * 3) + 1;
796 }
797 } else {
798 new_len = (strlen(hp->value) * 3) + 1;
799 }
800
801 if (encode_len < new_len) {
802 char *tmp;
803
804 /* keep track of the size of our allocation */
805 encode_len = new_len;
806
807 if (!(tmp = realloc(encode_buf, encode_len))) {
808 abort();
809 }
810
811 encode_buf = tmp;
812 }
813
814 /* handle any bad things in the string like newlines : etc that screw up the serialized format */
815
816
817 if (encode) {
818 esl_url_encode(hp->value, encode_buf, encode_len);
819 } else {
820 esl_snprintf(encode_buf, encode_len, "%s", hp->value);
821 }
822
823
824 llen = strlen(hp->name) + strlen(encode_buf) + 8;
825
826 if ((len + llen) > dlen) {
827 char *m;
828 char *old = buf;
829 dlen += (blocksize + (len + llen));
830 if ((m = realloc(buf, dlen))) {
831 buf = m;
832 } else {
833 buf = old;
Value stored to 'buf' is never read
834 abort();
835 }
836 }
837
838 esl_snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf);
839 len = strlen(buf);
840 }
841
842 /* we are done with the memory we used for encoding, give it back */
843 esl_safe_free(encode_buf)if (encode_buf) free(encode_buf); encode_buf = ((void*)0);
844
845 if (event->body) {
846 int blen = (int) strlen(event->body);
847 llen = blen;
848
849 if (blen) {
850 llen += 25;
851 } else {
852 llen += 5;
853 }
854
855 if ((len + llen) > dlen) {
856 char *m;
857 char *old = buf;
858 dlen += (blocksize + (len + llen));
859 if ((m = realloc(buf, dlen))) {
860 buf = m;
861 } else {
862 buf = old;
863 abort();
864 }
865 }
866
867 if (blen) {
868 esl_snprintf(buf + len, dlen - len, "Content-Length: %d\n\n%s", blen, event->body);
869 } else {
870 esl_snprintf(buf + len, dlen - len, "\n");
871 }
872 } else {
873 esl_snprintf(buf + len, dlen - len, "\n");
874 }
875
876 *str = buf;
877
878 return ESL_SUCCESS;
879}
880
881ESL_DECLARE(esl_status_t)esl_status_t esl_event_create_json(esl_event_t **event, const char *json)
882{
883 esl_event_t *new_event;
884 cJSON *cj, *cjp;
885
886
887 if (!(cj = cJSON_Parse(json))) {
888 return (esl_status_t) ESL_FALSE;
889 }
890
891 if (esl_event_create(&new_event, ESL_EVENT_CLONE)esl_event_create_subclass(&new_event, ESL_EVENT_CLONE, ((
void*)0))
!= ESL_SUCCESS) {
892 cJSON_Delete(cj);
893 return (esl_status_t) ESL_FALSE;
894 }
895
896 for (cjp = cj->child; cjp; cjp = cjp->next) {
897 char *name = cjp->string;
898 char *value = cjp->valuestring;
899
900 if (name && value) {
901 if (!strcasecmp(name, "_body")) {
902 esl_event_add_body(new_event, value, ESL_VA_NONE"%s", "");
903 } else {
904 if (!strcasecmp(name, "event-name")) {
905 esl_event_del_header(new_event, "event-name")esl_event_del_header_val(new_event, "event-name", ((void*)0));
906 esl_name_event(value, &new_event->event_id);
907 }
908
909 esl_event_add_header_string(new_event, ESL_STACK_BOTTOM, name, value);
910 }
911
912 } else if (name) {
913 if (cjp->type == cJSON_Array5) {
914 int i, x = cJSON_GetArraySize(cjp);
915
916 for (i = 0; i < x; i++) {
917 cJSON *item = cJSON_GetArrayItem(cjp, i);
918
919 if (item != NULL((void*)0) && item->type == cJSON_String4 && item->valuestring) {
920 esl_event_add_header_string(new_event, ESL_STACK_PUSH, name, item->valuestring);
921 }
922 }
923 }
924 }
925 }
926
927 cJSON_Delete(cj);
928 *event = new_event;
929 return ESL_SUCCESS;
930}
931
932ESL_DECLARE(esl_status_t)esl_status_t esl_event_serialize_json(esl_event_t *event, char **str)
933{
934 esl_event_header_t *hp;
935 cJSON *cj;
936
937 *str = NULL((void*)0);
938
939 cj = cJSON_CreateObject();
940
941 for (hp = event->headers; hp; hp = hp->next) {
942 if (hp->idx) {
943 cJSON *a = cJSON_CreateArray();
944 int i;
945
946 for(i = 0; i < hp->idx; i++) {
947 cJSON_AddItemToArray(a, cJSON_CreateString(hp->array[i]));
948 }
949
950 cJSON_AddItemToObject(cj, hp->name, a);
951
952 } else {
953 cJSON_AddItemToObject(cj, hp->name, cJSON_CreateString(hp->value));
954 }
955 }
956
957 if (event->body) {
958 int blen = (int) strlen(event->body);
959 char tmp[25];
960
961 esl_snprintf(tmp, sizeof(tmp), "%d", blen);
962
963 cJSON_AddItemToObject(cj, "Content-Length", cJSON_CreateString(tmp));
964 cJSON_AddItemToObject(cj, "_body", cJSON_CreateString(event->body));
965 }
966
967 *str = cJSON_Print(cj);
968 cJSON_Delete(cj);
969
970 return ESL_SUCCESS;
971}
972
973/* For Emacs:
974 * Local Variables:
975 * mode:c
976 * indent-tabs-mode:t
977 * tab-width:4
978 * c-basic-offset:4
979 * End:
980 * For VIM:
981 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
982 */