From ce8c9bf266578f5e8e510220334091aa5bf90a9a Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sat, 22 Feb 2014 07:36:01 +0000 Subject: [PATCH] forgot to add msg module --- include/re_msg.h | 21 +++++++++++++++ src/msg/ctype.c | 62 ++++++++++++++++++++++++++++++++++++++++++ src/msg/mod.mk | 8 ++++++ src/msg/param.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 include/re_msg.h create mode 100644 src/msg/ctype.c create mode 100644 src/msg/mod.mk create mode 100644 src/msg/param.c diff --git a/include/re_msg.h b/include/re_msg.h new file mode 100644 index 0000000..9c52ee9 --- /dev/null +++ b/include/re_msg.h @@ -0,0 +1,21 @@ +/** + * @file re_msg.h Interface to generic message components + * + * Copyright (C) 2010 Creytiv.com + */ + + +/** Content-Type */ +struct msg_ctype { + struct pl type; + struct pl subtype; + struct pl params; +}; + + +int msg_ctype_decode(struct msg_ctype *ctype, const struct pl *pl); +bool msg_ctype_cmp(const struct msg_ctype *ctype, + const char *type, const char *subtype); + +int msg_param_decode(const struct pl *pl, const char *name, struct pl *val); +int msg_param_exists(const struct pl *pl, const char *name, struct pl *end); diff --git a/src/msg/ctype.c b/src/msg/ctype.c new file mode 100644 index 0000000..710f2c7 --- /dev/null +++ b/src/msg/ctype.c @@ -0,0 +1,62 @@ +/** + * @file ctype.c Content-Type decode + * + * Copyright (C) 2010 Creytiv.com + */ +#include +#include +#include + + +/** + * Decode a pointer-length string into Content-Type header + * + * @param ctype Content-Type header + * @param pl Pointer-length string + * + * @return 0 for success, otherwise errorcode + */ +int msg_ctype_decode(struct msg_ctype *ctype, const struct pl *pl) +{ + struct pl ws; + + if (!ctype || !pl) + return EINVAL; + + if (re_regex(pl->p, pl->l, + "[ \t\r\n]*[^ \t\r\n;/]+[ \t\r\n]*/[ \t\r\n]*[^ \t\r\n;]+" + "[^]*", + &ws, &ctype->type, NULL, NULL, &ctype->subtype, + &ctype->params)) + return EBADMSG; + + if (ws.p != pl->p) + return EBADMSG; + + return 0; +} + + +/** + * Compare Content-Type + * + * @param ctype Content-Type header + * @param type Media type + * @param subtype Media sub-type + * + * @return true if match, false if no match + */ +bool msg_ctype_cmp(const struct msg_ctype *ctype, + const char *type, const char *subtype) +{ + if (!ctype || !type || !subtype) + return false; + + if (pl_strcasecmp(&ctype->type, type)) + return false; + + if (pl_strcasecmp(&ctype->subtype, subtype)) + return false; + + return true; +} diff --git a/src/msg/mod.mk b/src/msg/mod.mk new file mode 100644 index 0000000..8f81b73 --- /dev/null +++ b/src/msg/mod.mk @@ -0,0 +1,8 @@ +# +# mod.mk +# +# Copyright (C) 2010 Creytiv.com +# + +SRCS += msg/ctype.c +SRCS += msg/param.c diff --git a/src/msg/param.c b/src/msg/param.c new file mode 100644 index 0000000..1974b82 --- /dev/null +++ b/src/msg/param.c @@ -0,0 +1,70 @@ +/** + * @file param.c SIP Parameter decode + * + * Copyright (C) 2010 Creytiv.com + */ +#include +#include +#include + + +/** + * Check if a parameter exists + * + * @param pl Pointer-length string + * @param name Parameter name + * @param val Returned parameter value + * + * @return 0 for success, otherwise errorcode + */ +int msg_param_exists(const struct pl *pl, const char *name, struct pl *val) +{ + struct pl v1, v2; + char xpr[128]; + + if (!pl || !name || !val) + return EINVAL; + + (void)re_snprintf(xpr, sizeof(xpr), ";[ \t\r\n]*%s[ \t\r\n;=]*", name); + + if (re_regex(pl->p, pl->l, xpr, &v1, &v2)) + return ENOENT; + + if (!v2.l && v2.p < pl->p + pl->l) + return ENOENT; + + val->p = v1.p - 1; + val->l = v2.p - val->p; + + return 0; +} + + +/** + * Decode a Parameter + * + * @param pl Pointer-length string + * @param name Parameter name + * @param val Returned parameter value + * + * @return 0 for success, otherwise errorcode + */ +int msg_param_decode(const struct pl *pl, const char *name, struct pl *val) +{ + char expr[128]; + struct pl v; + + if (!pl || !name || !val) + return EINVAL; + + (void)re_snprintf(expr, sizeof(expr), + ";[ \t\r\n]*%s[ \t\r\n]*=[ \t\r\n]*[~ \t\r\n;]+", + name); + + if (re_regex(pl->p, pl->l, expr, NULL, NULL, NULL, &v)) + return ENOENT; + + *val = v; + + return 0; +}