lws_json_escape

Signed-off-by: Andy Green <andy@warmcat.com>
This commit is contained in:
Andy Green 2016-06-17 10:05:23 +08:00
parent 4bd5b96735
commit 920daf10a1
2 changed files with 38 additions and 1 deletions

View file

@ -1799,6 +1799,42 @@ lws_sql_purify(char *escaped, const char *string, int len)
return escaped;
}
/**
* lws_json_purify() - like strncpy but with escaping for json chars
*
* @escaped: output buffer
* @string: input buffer ('/0' terminated)
* @len: output buffer max length
*
* Because escaping expands the output string, it's not
* possible to do it in-place, ie, with escaped == string
*/
LWS_VISIBLE LWS_EXTERN const char *
lws_json_purify(char *escaped, const char *string, int len)
{
const char *p = string;
char *q = escaped;
while (*p && len-- > 6) {
if (*p == '\"' || *p == '\\' || *p < 0x20) {
*q++ = '\\';
*q++ = 'u';
*q++ = '0';
*q++ = '0';
*q++ = hex[((*p) >> 4) & 15];
*q++ = hex[(*p) & 15];
len -= 5;
p++;
} else
*q++ = *p++;
}
*q = '\0';
return escaped;
}
/**
* lws_urlencode() - like strncpy but with urlencoding
*

View file

@ -1810,7 +1810,8 @@ lws_urlencode(char *escaped, const char *string, int len);
LWS_VISIBLE LWS_EXTERN const char *
lws_sql_purify(char *escaped, const char *string, int len);
LWS_VISIBLE LWS_EXTERN const char *
lws_json_purify(char *escaped, const char *string, int len);
/*
* URLDECODE 1 / 2