Commit d90f9ad3 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Give vsb_quote() an optional length paramter (Pass -1 for strlen).

Add a complementary vsb_unquote() function.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4176 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 31484475
...@@ -291,7 +291,7 @@ BAN_AddTest(struct cli *cli, struct ban *b, const char *a1, const char *a2, cons ...@@ -291,7 +291,7 @@ BAN_AddTest(struct cli *cli, struct ban *b, const char *a1, const char *a2, cons
} }
vsb_printf(b->vsb, "%s %s ", a1, a2); vsb_printf(b->vsb, "%s %s ", a1, a2);
vsb_quote(b->vsb, a3, 0); vsb_quote(b->vsb, a3, -1, 0);
return (0); return (0);
} }
......
...@@ -77,7 +77,8 @@ char *vsb_data(struct vsb *); ...@@ -77,7 +77,8 @@ char *vsb_data(struct vsb *);
int vsb_len(struct vsb *); int vsb_len(struct vsb *);
int vsb_done(const struct vsb *); int vsb_done(const struct vsb *);
void vsb_delete(struct vsb *); void vsb_delete(struct vsb *);
void vsb_quote(struct vsb *s, const char *p, int how); void vsb_quote(struct vsb *s, const char *p, int len, int how);
const char *vsb_unquote(struct vsb *s, const char *p, int len, int how);
#ifdef __cplusplus #ifdef __cplusplus
}; };
#endif #endif
......
...@@ -74,7 +74,7 @@ void ...@@ -74,7 +74,7 @@ void
cli_quote(struct cli *cli, const char *s) cli_quote(struct cli *cli, const char *s)
{ {
vsb_quote(cli->sb, s, 0); vsb_quote(cli->sb, s, -1, 0);
} }
void void
......
...@@ -482,25 +482,27 @@ vsb_done(const struct vsb *s) ...@@ -482,25 +482,27 @@ vsb_done(const struct vsb *s)
* Quote a string * Quote a string
*/ */
void void
vsb_quote(struct vsb *s, const char *p, int how) vsb_quote(struct vsb *s, const char *p, int len, int how)
{ {
const char *q; const char *q;
int quote = 0; int quote = 0;
(void)how; /* For future enhancements */ (void)how; /* For future enhancements */
if (len == -1)
len = strlen(p);
for (q = p; *q != '\0'; q++) { for (q = p; q < p + len; q++) {
if (!isgraph(*q) || *q == '"') { if (!isgraph(*q) || *q == '"') {
quote++; quote++;
break; break;
} }
} }
if (!quote) { if (!quote) {
(void)vsb_cat(s, p); (void)vsb_bcat(s, p, len);
return; return;
} }
(void)vsb_putc(s, '"'); (void)vsb_putc(s, '"');
for (q = p; *q != '\0'; q++) { for (q = p; q < p + len; q++) {
switch (*q) { switch (*q) {
case ' ': case ' ':
(void)vsb_putc(s, *q); (void)vsb_putc(s, *q);
...@@ -523,9 +525,66 @@ vsb_quote(struct vsb *s, const char *p, int how) ...@@ -523,9 +525,66 @@ vsb_quote(struct vsb *s, const char *p, int how)
if (isgraph(*q)) if (isgraph(*q))
(void)vsb_putc(s, *q); (void)vsb_putc(s, *q);
else else
(void)vsb_printf(s, "\\%o", *q); (void)vsb_printf(s, "\\%o", *q & 0xff);
break; break;
} }
} }
(void)vsb_putc(s, '"'); (void)vsb_putc(s, '"');
} }
/*
* Unquote a string
*/
const char *
vsb_unquote(struct vsb *s, const char *p, int len, int how)
{
const char *q;
char *r;
unsigned long u;
char c;
(void)how; /* For future enhancements */
if (len == -1)
len = strlen(p);
for (q = p; q < p + len; q++) {
if (*q != '\\') {
(void)vsb_bcat(s, q, 1);
continue;
}
if (++q >= p + len)
return ("Incomplete '\\'-sequence at end of string");
switch(*q) {
case 'n':
(void)vsb_bcat(s, "\n", 1);
continue;
case 'r':
(void)vsb_bcat(s, "\r", 1);
continue;
case 't':
(void)vsb_bcat(s, "\t", 1);
continue;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
errno = 0;
u = strtoul(q, &r, 8);
if (errno != 0 || (u & ~0xff))
return ("\\ooo sequence out of range");
c = (char)u;
(void)vsb_bcat(s, &c, 1);
q = r - 1;
continue;
default:
(void)vsb_bcat(s, q, 1);
}
}
return (NULL);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment