Commit 70b83a56 authored by Tollef Fog Heen's avatar Tollef Fog Heen

Merge r4176: 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/branches/2.0@4301 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 61eb7011
......@@ -366,7 +366,7 @@ BAN_AddTest(struct cli *cli, struct ban *b, const char *a1, const char *a2, cons
sb = vsb_newauto();
XXXAN(sb);
vsb_printf(sb, "%s %s ", a1, a2);
vsb_quote(sb, a3, 0);
vsb_quote(sb, a3, -1, 0);
vsb_finish(sb);
AZ(vsb_overflowed(sb));
bt->test = strdup(vsb_data(sb));
......
......@@ -77,7 +77,8 @@ char *vsb_data(struct vsb *);
int vsb_len(struct vsb *);
int vsb_done(const 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
};
#endif
......
......@@ -73,7 +73,7 @@ void
cli_quote(struct cli *cli, const char *s)
{
vsb_quote(cli->sb, s, 0);
vsb_quote(cli->sb, s, -1, 0);
}
void
......
......@@ -481,25 +481,27 @@ vsb_done(const struct vsb *s)
* Quote a string
*/
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;
int quote = 0;
(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 == '"') {
quote++;
break;
}
}
if (!quote) {
(void)vsb_cat(s, p);
(void)vsb_bcat(s, p, len);
return;
}
(void)vsb_putc(s, '"');
for (q = p; *q != '\0'; q++) {
for (q = p; q < p + len; q++) {
switch (*q) {
case ' ':
(void)vsb_putc(s, *q);
......@@ -522,9 +524,66 @@ vsb_quote(struct vsb *s, const char *p, int how)
if (isgraph(*q))
(void)vsb_putc(s, *q);
else
(void)vsb_printf(s, "\\%o", *q);
(void)vsb_printf(s, "\\%o", *q & 0xff);
break;
}
}
(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