From 7147e42bfff421358d512a587f9087d11d7367f1 Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 11 Aug 2006 10:47:40 +0000 Subject: [PATCH] Don't recognize '\' as magic in CSTR tokens, use %xx escapes instead. Add decoded string element to struct token. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@809 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/lib/libvcl/vcc_compile.h | 1 + varnish-cache/lib/libvcl/vcc_token.c | 68 ++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/varnish-cache/lib/libvcl/vcc_compile.h b/varnish-cache/lib/libvcl/vcc_compile.h index c13f92ff..1486fa78 100644 --- a/varnish-cache/lib/libvcl/vcc_compile.h +++ b/varnish-cache/lib/libvcl/vcc_compile.h @@ -13,6 +13,7 @@ struct token { const char *e; TAILQ_ENTRY(token) list; unsigned cnt; + char *dec; }; struct tokenlist { diff --git a/varnish-cache/lib/libvcl/vcc_token.c b/varnish-cache/lib/libvcl/vcc_token.c index e4c4c049..b6ac5e6b 100644 --- a/varnish-cache/lib/libvcl/vcc_token.c +++ b/varnish-cache/lib/libvcl/vcc_token.c @@ -163,6 +163,59 @@ vcc_IdIs(struct token *t, const char *p) return (1); } +/*-------------------------------------------------------------------- + * Decode %xx in a string + */ + +static int +vcc_xdig(const char c) +{ + static const char *xdigit = + "0123456789abcdef" + "0123456789ABCDEF"; + const char *p; + + p = strchr(xdigit, c); + assert(p != NULL); + return ((p - xdigit) % 16); +} + +static int +vcc_decstr(struct tokenlist *tl) +{ + const char *p; + char *q; + + assert(tl->t->tok == CSTR); + tl->t->dec = malloc((tl->t->e - tl->t->b) - 1); + assert(tl->t->dec != NULL); + q = tl->t->dec; + for (p = tl->t->b + 1; p < tl->t->e - 1; ) { + if (*p != '%') { + *q++ = *p++; + continue; + } + if (p + 4 > tl->t->e) { + vcc_AddToken(tl, CSTR, p, tl->t->e); + vsb_printf(tl->sb, + "Incomplete %%xx escape\n"); + vcc_ErrWhere(tl, tl->t); + return(1); + } + if (!isxdigit(p[1]) || !isxdigit(p[2])) { + vcc_AddToken(tl, CSTR, p, p + 3); + vsb_printf(tl->sb, + "Illegal hex char in %%xx escape\n"); + vcc_ErrWhere(tl, tl->t); + return(1); + } + *q++ = vcc_xdig(p[1]) * 16 + vcc_xdig(p[2]); + p += 3; + } + *q++ = '\0'; + return (0); +} + /*-------------------------------------------------------------------- * Add a token to the token list. */ @@ -235,16 +288,21 @@ vcc_Lexer(struct tokenlist *tl, const char *b, const char *e) /* Match strings, with \\ and \" escapes */ if (*p == '"') { for (q = p + 1; q < e; q++) { - if (*q == '\\' && q[1] == '\\') - q++; - else if (*q == '\\' && q[1] == '"') - q++; - else if (*q == '"') { + if (*q == '"') { q++; break; } + if (*q == '\r' || *q == '\n') { + vcc_AddToken(tl, EOI, p, q); + vsb_printf(tl->sb, + "Unterminated string at\n"); + vcc_ErrWhere(tl, tl->t); + return; + } } vcc_AddToken(tl, CSTR, p, q); + if (vcc_decstr(tl)) + return; p = q; continue; } -- 2.39.5