From ab6d1b3f79ee148ad7b8f68b057213a25583720b Mon Sep 17 00:00:00 2001 From: Michael Santos Date: Sat, 8 Dec 2018 08:46:29 -0500 Subject: [PATCH] format: clean up input parsing --- src/xmppipe.c | 132 ++++++++++++++++++++++++--------------------- src/xmppipe_fmt.c | 7 ++- src/xmppipe_util.c | 24 --------- 3 files changed, 76 insertions(+), 87 deletions(-) diff --git a/src/xmppipe.c b/src/xmppipe.c index ed1a160..1121ec6 100644 --- a/src/xmppipe.c +++ b/src/xmppipe.c @@ -1203,22 +1203,25 @@ xmppipe_muc_subject(xmppipe_state_t *state, char *buf) } void -xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len) +xmppipe_send_stanza(xmppipe_state_t *state, char *buf, size_t len) { - int i = 0; - int j = 0; char *to = NULL; char *type = NULL; - char *buf = NULL; + char *deftype; + + int i; + size_t n; + char *tmp = NULL; + char *start = NULL; + char *end = NULL; char *body = NULL; + int valid = 0; - char *tok[6] = {0}; + deftype = (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat"; switch (state->format) { case XMPPIPE_FMT_STDIN: - xmppipe_send_message(state, state->out, - (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat", - buf0, len); + xmppipe_send_message(state, state->out, deftype, buf, len); return; case XMPPIPE_FMT_COLON: @@ -1231,79 +1234,86 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len) return; } - buf = xmppipe_strdup(buf0); + tmp = xmppipe_strdup(buf); + start = tmp; - tok[0] = xmppipe_strtok(buf, ":"); - switch (tok[0][0]) { - case 'm': - j = 5; - break; - case 'p': - j = 4; - /* unsupported: fall through */ - default: - if (state->verbose) - (void)fprintf(stderr, "unsupported stanza: %s\n", - tok[0] == NULL ? "NULL" : tok[0]); + /* trailing newline */ + end = strchr(start, '\n'); + if (end != NULL) + *end = '\0'; - return; - } + for (i = 0; start != NULL; i++) { + end = strchr(start, ':'); + if (end != NULL) + *end++ = '\0'; - while (tok[i] != NULL && i < j) { - i++; - tok[i] = xmppipe_strtok(NULL, ":"); + n = strlen(start); if (state->verbose) (void)fprintf(stderr, "message:%d:%s\n", i, - tok[i] == NULL ? "NULL" : tok[i]); - } - - if (tok[1] == NULL) { - if (state->verbose) - (void)fprintf(stderr, "type required\n"); + n == 0 ? "" : start); - return; - } - - if (strlen(tok[1]) == 0) - tok[1] = (state->opt & XMPPIPE_OPT_GROUPCHAT) ? "groupchat" : "chat"; + switch (i) { + case 0: + if (n != 1) { + if (state->verbose) + (void)fprintf(stderr, "stanza required\n"); - if (tok[2] == NULL) { - if (state->verbose) - (void)fprintf(stderr, "to address required\n"); + goto XMPPIPE_DONE; + } - return; - } + switch (start[0]) { + case 'm': + break; + case 'p': + /* unsupported: fall through */ + default: + if (state->verbose) + (void)fprintf(stderr, "unsupported stanza: %c\n", start[0]); - if (strlen(tok[2]) == 0) - tok[2] = state->out; + goto XMPPIPE_DONE; + } + break; - if (tok[4] == NULL) { - if (state->verbose) - (void)fprintf(stderr, "body required\n"); + case 1: + type = xmppipe_fmt_decode((n == 0) ? deftype : start); + if (type == NULL) + goto XMPPIPE_DONE; + break; - return; - } + case 2: + to = xmppipe_fmt_decode((n == 0) ? state->out : start); + if (to == NULL) + goto XMPPIPE_DONE; - type = xmppipe_fmt_decode(tok[1]); + break; - if (type == NULL) - goto XMPPIPE_ERR; + case 3: + break; - to = xmppipe_fmt_decode(tok[2]); + case 4: + body = xmppipe_fmt_decode(start); + if (body == NULL) + goto XMPPIPE_DONE; - if (to == NULL) - goto XMPPIPE_ERR; + valid = 1; + break; - body = xmppipe_fmt_decode(tok[4]); + default: + goto XMPPIPE_DONE; + } - if (body == NULL) - goto XMPPIPE_ERR; + start = end; + } - xmppipe_send_message(state, to, type, body, strlen(body)); +XMPPIPE_DONE: + if (valid == 1) + xmppipe_send_message(state, to, type, body, strlen(body)); + else + if (state->verbose) + (void)fprintf(stderr, "invalid input\n"); -XMPPIPE_ERR: - free(buf); + free(tmp); free(type); free(to); free(body); diff --git a/src/xmppipe_fmt.c b/src/xmppipe_fmt.c index 84fbd14..3845e20 100644 --- a/src/xmppipe_fmt.c +++ b/src/xmppipe_fmt.c @@ -61,12 +61,15 @@ xmppipe_fmt_encode(const char *s) char * xmppipe_nfmt_decode(const char *s, size_t len) { - char *buf = xmppipe_calloc(len+1, 1); - char *p = buf; + char *buf; + char *p; size_t i = 0; char fmt[3] = {0}; char *endptr; + buf = xmppipe_calloc(len+1, 1); + p = buf; + for (i = 0; i < len; i++) { unsigned char c = s[i]; if (c == '%') { diff --git a/src/xmppipe_util.c b/src/xmppipe_util.c index 9b92e66..6503051 100644 --- a/src/xmppipe_util.c +++ b/src/xmppipe_util.c @@ -92,30 +92,6 @@ xmppipe_calloc(size_t nmemb, size_t size) return buf; } -// https://stackoverflow.com/a/30295426 - char -*xmppipe_strtok(char *str, char const *delims) -{ - static char *src = NULL; - char *p, *ret = 0; - - if (str != NULL) src = str; - - if (src == NULL || *src == '\0') - return NULL; - - ret = src; - if ((p = strpbrk(src, delims)) != NULL) - { - *p = 0; - src = ++p; - } - else - src += strlen(src); - - return ret; -} - xmpp_stanza_t * xmppipe_stanza_new(xmpp_ctx_t *ctx) {