-f/--format: support percent decoding

Convert percent hexcodes when format mode is enabled:

    m:chat:to@example.com:from@example.com:01234=%30%31%32%33%34
master
Michael Santos 6 years ago
parent 5127b271ba
commit 231bee7c74

@ -1282,11 +1282,27 @@ xmppipe_send_stanza(xmppipe_state_t *state, char *buf0, size_t len)
return;
}
type = tok[1];
to = tok[2];
body = tok[4];
type = xmppipe_fmt_decode(tok[1]);
if (type == NULL)
goto XMPPIPE_ERR;
to = xmppipe_fmt_decode(tok[2]);
if (to == NULL)
goto XMPPIPE_ERR;
body = xmppipe_fmt_decode(tok[4]);
if (body == NULL)
goto XMPPIPE_ERR;
xmppipe_send_message(state, to, type, body, strlen(body));
XMPPIPE_ERR:
free(type);
free(to);
free(body);
}
void

@ -104,6 +104,8 @@ typedef struct {
int xmppipe_fmt_init();
char *xmppipe_fmt(const char *);
char *xmppipe_nfmt(const char *, size_t);
char *xmppipe_fmt_decode(const char *);
char *xmppipe_nfmt_decode(const char *, size_t);
int xmppipe_set_nonblock(int fd);
char *xmppipe_servername(char *);

@ -13,6 +13,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xmppipe.h"
#include "errno.h"
static unsigned char rfc3986[256];
@ -56,3 +57,51 @@ xmppipe_fmt(const char *s)
{
return xmppipe_nfmt(s, strlen(s));
}
char *
xmppipe_nfmt_decode(const char *s, size_t len)
{
char *buf = xmppipe_calloc(len+1, 1);
char *p = buf;
size_t i = 0;
char fmt[3] = {0};
char *endptr;
for (i = 0; i < len; i++) {
unsigned char c = s[i];
if (c == '%') {
unsigned char n = 0;
if (i + 2 > len) goto XMPPIPE_ERR;
(void)memcpy(fmt, s+i+1, 2);
errno = 0;
n = strtol(fmt, &endptr, 16);
if ( (errno != 0) || (endptr == fmt))
goto XMPPIPE_ERR;
*p++ = n;
i += 2;
}
else {
*p++ = c;
}
}
return buf;
XMPPIPE_ERR:
free(buf);
return NULL;
}
char *
xmppipe_fmt_decode(const char *s)
{
if (s == NULL)
return NULL;
return xmppipe_nfmt_decode(s, strlen(s));
}

Loading…
Cancel
Save