libs/web: fix sanitize_utf8(), passes all testcases now
This commit is contained in:
parent
09e71acf6b
commit
8ac568acb0
1 changed files with 67 additions and 57 deletions
|
@ -181,8 +181,6 @@ static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
|
||||||
unsigned char *ptr = *s;
|
unsigned char *ptr = *s;
|
||||||
unsigned int o = 0, v, n;
|
unsigned int o = 0, v, n;
|
||||||
|
|
||||||
//for (o = 0; o < l; o++)
|
|
||||||
{
|
|
||||||
/* ascii byte without null */
|
/* ascii byte without null */
|
||||||
if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
|
if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F))
|
||||||
{
|
{
|
||||||
|
@ -245,7 +243,6 @@ static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
|
||||||
o = 1;
|
o = 1;
|
||||||
ptr++;
|
ptr++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
*s = ptr;
|
*s = ptr;
|
||||||
return o;
|
return o;
|
||||||
|
@ -256,15 +253,28 @@ char * sanitize_utf8(const char *s, unsigned int l)
|
||||||
{
|
{
|
||||||
struct template_buffer *buf = buf_init();
|
struct template_buffer *buf = buf_init();
|
||||||
unsigned char *ptr = (unsigned char *)s;
|
unsigned char *ptr = (unsigned char *)s;
|
||||||
|
unsigned int v, o;
|
||||||
|
|
||||||
if (!buf)
|
if (!buf)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!_validate_utf8(&ptr, l, buf))
|
for (o = 0; o < l; o++)
|
||||||
{
|
{
|
||||||
free(buf->data);
|
/* ascii char */
|
||||||
free(buf);
|
if ((*ptr >= 0x01) && (*ptr <= 0x7F))
|
||||||
return NULL;
|
{
|
||||||
|
if (!buf_putchar(buf, *ptr++))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* invalid byte or multi byte sequence */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!(v = _validate_utf8(&ptr, l - o, buf)))
|
||||||
|
break;
|
||||||
|
|
||||||
|
o += (v - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf_destroy(buf);
|
return buf_destroy(buf);
|
||||||
|
|
Loading…
Reference in a new issue