Skip to content

Commit ce8c9bf

Browse files
committed
forgot to add msg module
1 parent a2e9dbd commit ce8c9bf

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

include/re_msg.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @file re_msg.h Interface to generic message components
3+
*
4+
* Copyright (C) 2010 Creytiv.com
5+
*/
6+
7+
8+
/** Content-Type */
9+
struct msg_ctype {
10+
struct pl type;
11+
struct pl subtype;
12+
struct pl params;
13+
};
14+
15+
16+
int msg_ctype_decode(struct msg_ctype *ctype, const struct pl *pl);
17+
bool msg_ctype_cmp(const struct msg_ctype *ctype,
18+
const char *type, const char *subtype);
19+
20+
int msg_param_decode(const struct pl *pl, const char *name, struct pl *val);
21+
int msg_param_exists(const struct pl *pl, const char *name, struct pl *end);

src/msg/ctype.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @file ctype.c Content-Type 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+
* Decode a pointer-length string into Content-Type header
13+
*
14+
* @param ctype Content-Type header
15+
* @param pl Pointer-length string
16+
*
17+
* @return 0 for success, otherwise errorcode
18+
*/
19+
int msg_ctype_decode(struct msg_ctype *ctype, const struct pl *pl)
20+
{
21+
struct pl ws;
22+
23+
if (!ctype || !pl)
24+
return EINVAL;
25+
26+
if (re_regex(pl->p, pl->l,
27+
"[ \t\r\n]*[^ \t\r\n;/]+[ \t\r\n]*/[ \t\r\n]*[^ \t\r\n;]+"
28+
"[^]*",
29+
&ws, &ctype->type, NULL, NULL, &ctype->subtype,
30+
&ctype->params))
31+
return EBADMSG;
32+
33+
if (ws.p != pl->p)
34+
return EBADMSG;
35+
36+
return 0;
37+
}
38+
39+
40+
/**
41+
* Compare Content-Type
42+
*
43+
* @param ctype Content-Type header
44+
* @param type Media type
45+
* @param subtype Media sub-type
46+
*
47+
* @return true if match, false if no match
48+
*/
49+
bool msg_ctype_cmp(const struct msg_ctype *ctype,
50+
const char *type, const char *subtype)
51+
{
52+
if (!ctype || !type || !subtype)
53+
return false;
54+
55+
if (pl_strcasecmp(&ctype->type, type))
56+
return false;
57+
58+
if (pl_strcasecmp(&ctype->subtype, subtype))
59+
return false;
60+
61+
return true;
62+
}

src/msg/mod.mk

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# mod.mk
3+
#
4+
# Copyright (C) 2010 Creytiv.com
5+
#
6+
7+
SRCS += msg/ctype.c
8+
SRCS += msg/param.c

src/msg/param.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
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+
*/
20+
int 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+
*/
52+
int 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+
}

0 commit comments

Comments
 (0)