blob: 1974b8252f53895279e2b667a96476c4452d4816 [file] [log] [blame]
James Kuszmaul82f6c042021-01-17 11:30:16 -08001/**
2 * @file param.c SIP Parameter decode
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6#include <re_types.h>
7#include <re_fmt.h>
8#include <re_msg.h>
9
10
11/**
12 * Check if a parameter exists
13 *
14 * @param pl Pointer-length string
15 * @param name Parameter name
16 * @param val Returned parameter value
17 *
18 * @return 0 for success, otherwise errorcode
19 */
20int msg_param_exists(const struct pl *pl, const char *name, struct pl *val)
21{
22 struct pl v1, v2;
23 char xpr[128];
24
25 if (!pl || !name || !val)
26 return EINVAL;
27
28 (void)re_snprintf(xpr, sizeof(xpr), ";[ \t\r\n]*%s[ \t\r\n;=]*", name);
29
30 if (re_regex(pl->p, pl->l, xpr, &v1, &v2))
31 return ENOENT;
32
33 if (!v2.l && v2.p < pl->p + pl->l)
34 return ENOENT;
35
36 val->p = v1.p - 1;
37 val->l = v2.p - val->p;
38
39 return 0;
40}
41
42
43/**
44 * Decode a Parameter
45 *
46 * @param pl Pointer-length string
47 * @param name Parameter name
48 * @param val Returned parameter value
49 *
50 * @return 0 for success, otherwise errorcode
51 */
52int msg_param_decode(const struct pl *pl, const char *name, struct pl *val)
53{
54 char expr[128];
55 struct pl v;
56
57 if (!pl || !name || !val)
58 return EINVAL;
59
60 (void)re_snprintf(expr, sizeof(expr),
61 ";[ \t\r\n]*%s[ \t\r\n]*=[ \t\r\n]*[~ \t\r\n;]+",
62 name);
63
64 if (re_regex(pl->p, pl->l, expr, NULL, NULL, NULL, &v))
65 return ENOENT;
66
67 *val = v;
68
69 return 0;
70}