2017-02-08 21:14:30 +00:00
|
|
|
/*
|
2018-02-22 13:23:33 +00:00
|
|
|
* gluon-web Template - Utility functions
|
2017-02-08 21:14:30 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Jo-Philipp Wich <jow@openwrt.org>
|
2018-02-22 13:23:33 +00:00
|
|
|
* Copyright (C) 2018 Matthias Schiffer <mschiffer@universe-factory.net>
|
2017-02-08 21:14:30 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "template_utils.h"
|
|
|
|
#include "template_lmo.h"
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-02-08 21:14:30 +00:00
|
|
|
/* initialize a buffer object */
|
2018-02-22 16:41:38 +00:00
|
|
|
struct template_buffer * buf_init(size_t size)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 16:41:38 +00:00
|
|
|
struct template_buffer *buf = malloc(sizeof(*buf));
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
if (buf != NULL) {
|
2017-02-08 21:14:30 +00:00
|
|
|
buf->size = size;
|
|
|
|
buf->data = malloc(buf->size);
|
2018-02-22 20:09:35 +00:00
|
|
|
buf->dptr = buf->data;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 20:09:35 +00:00
|
|
|
if (buf->data != NULL || size == 0)
|
2017-02-08 21:14:30 +00:00
|
|
|
return buf;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grow buffer */
|
2018-02-22 16:41:38 +00:00
|
|
|
static bool buf_grow(struct template_buffer *buf, size_t len)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 16:41:38 +00:00
|
|
|
size_t off = buf->dptr - buf->data, left = buf->size - off;
|
|
|
|
if (len <= left)
|
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
size_t diff = len - left;
|
|
|
|
if (diff < 1024)
|
|
|
|
diff = 1024;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
char *data = realloc(buf->data, buf->size + diff);
|
|
|
|
if (data == NULL)
|
|
|
|
return false;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
buf->data = data;
|
|
|
|
buf->dptr = data + off;
|
|
|
|
buf->size += diff;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* put one char into buffer object */
|
2018-02-22 16:41:38 +00:00
|
|
|
static bool buf_putchar(struct template_buffer *buf, char c)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 16:41:38 +00:00
|
|
|
if (!buf_grow(buf, 1))
|
|
|
|
return false;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
|
|
|
*(buf->dptr++) = c;
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* append data to buffer */
|
2018-02-22 16:41:38 +00:00
|
|
|
bool buf_append(struct template_buffer *buf, const char *s, size_t len)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 16:41:38 +00:00
|
|
|
if (!buf_grow(buf, len))
|
|
|
|
return false;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
|
|
|
memcpy(buf->dptr, s, len);
|
|
|
|
buf->dptr += len;
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy buffer object and return pointer to data */
|
|
|
|
char * buf_destroy(struct template_buffer *buf)
|
|
|
|
{
|
|
|
|
char *data = buf->data;
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* calculate the number of expected continuation chars */
|
2018-02-22 16:41:38 +00:00
|
|
|
static inline size_t mb_num_chars(unsigned char c)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
if ((c & 0xE0) == 0xC0)
|
|
|
|
return 2;
|
|
|
|
else if ((c & 0xF0) == 0xE0)
|
|
|
|
return 3;
|
|
|
|
else if ((c & 0xF8) == 0xF0)
|
|
|
|
return 4;
|
|
|
|
else if ((c & 0xFC) == 0xF8)
|
|
|
|
return 5;
|
|
|
|
else if ((c & 0xFE) == 0xFC)
|
|
|
|
return 6;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test whether the given byte is a valid continuation char */
|
2018-02-22 16:41:38 +00:00
|
|
|
static inline bool mb_is_cont(unsigned char c)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
return ((c >= 0x80) && (c <= 0xBF));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test whether the byte sequence at the given pointer with the given
|
|
|
|
* length is the shortest possible representation of the code point */
|
2018-02-22 16:41:38 +00:00
|
|
|
static inline bool mb_is_shortest(const unsigned char *s, size_t n)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
switch (n)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
/* 1100000x (10xxxxxx) */
|
|
|
|
return !(((*s >> 1) == 0x60) &&
|
|
|
|
((*(s+1) >> 6) == 0x02));
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
/* 11100000 100xxxxx (10xxxxxx) */
|
|
|
|
return !((*s == 0xE0) &&
|
|
|
|
((*(s+1) >> 5) == 0x04) &&
|
|
|
|
((*(s+2) >> 6) == 0x02));
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
/* 11110000 1000xxxx (10xxxxxx 10xxxxxx) */
|
|
|
|
return !((*s == 0xF0) &&
|
|
|
|
((*(s+1) >> 4) == 0x08) &&
|
|
|
|
((*(s+2) >> 6) == 0x02) &&
|
|
|
|
((*(s+3) >> 6) == 0x02));
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
/* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) */
|
|
|
|
return !((*s == 0xF8) &&
|
|
|
|
((*(s+1) >> 3) == 0x10) &&
|
|
|
|
((*(s+2) >> 6) == 0x02) &&
|
|
|
|
((*(s+3) >> 6) == 0x02) &&
|
|
|
|
((*(s+4) >> 6) == 0x02));
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
/* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) */
|
|
|
|
return !((*s == 0xF8) &&
|
|
|
|
((*(s+1) >> 2) == 0x20) &&
|
|
|
|
((*(s+2) >> 6) == 0x02) &&
|
|
|
|
((*(s+3) >> 6) == 0x02) &&
|
|
|
|
((*(s+4) >> 6) == 0x02) &&
|
|
|
|
((*(s+5) >> 6) == 0x02));
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
return true;
|
2017-02-08 21:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test whether the byte sequence at the given pointer with the given
|
|
|
|
* length is an UTF-16 surrogate */
|
2018-02-22 16:41:38 +00:00
|
|
|
static inline bool mb_is_surrogate(const unsigned char *s, size_t n)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
return ((n == 3) && (*s == 0xED) && (*(s+1) >= 0xA0) && (*(s+1) <= 0xBF));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test whether the byte sequence at the given pointer with the given
|
|
|
|
* length is an illegal UTF-8 code point */
|
2018-02-22 16:41:38 +00:00
|
|
|
static inline bool mb_is_illegal(const unsigned char *s, size_t n)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
return ((n == 3) && (*s == 0xEF) && (*(s+1) == 0xBF) &&
|
|
|
|
(*(s+2) >= 0xBE) && (*(s+2) <= 0xBF));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* scan given source string, validate UTF-8 sequence and store result
|
|
|
|
* in given buffer object */
|
2018-02-22 16:41:38 +00:00
|
|
|
static size_t validate_utf8(const unsigned char **s, size_t l, struct template_buffer *buf)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
2018-02-22 16:41:38 +00:00
|
|
|
const unsigned char *ptr = *s;
|
|
|
|
size_t o = 0, v, n;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
|
|
|
/* ascii byte without null */
|
2018-02-22 16:41:38 +00:00
|
|
|
if ((*(ptr+0) >= 0x01) && (*(ptr+0) <= 0x7F)) {
|
2017-02-08 21:14:30 +00:00
|
|
|
if (!buf_putchar(buf, *ptr++))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
o = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* multi byte sequence */
|
2018-02-22 16:41:38 +00:00
|
|
|
else if ((n = mb_num_chars(*ptr)) > 1) {
|
2017-02-08 21:14:30 +00:00
|
|
|
/* count valid chars */
|
|
|
|
for (v = 1; (v <= n) && ((o+v) < l) && mb_is_cont(*(ptr+v)); v++);
|
|
|
|
|
|
|
|
switch (n)
|
|
|
|
{
|
|
|
|
case 6:
|
|
|
|
case 5:
|
|
|
|
/* five and six byte sequences are always invalid */
|
|
|
|
if (!buf_putchar(buf, '?'))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* if the number of valid continuation bytes matches the
|
|
|
|
* expected number and if the sequence is legal, copy
|
|
|
|
* the bytes to the destination buffer */
|
|
|
|
if ((v == n) && mb_is_shortest(ptr, n) &&
|
|
|
|
!mb_is_surrogate(ptr, n) && !mb_is_illegal(ptr, n))
|
|
|
|
{
|
|
|
|
/* copy sequence */
|
2018-02-22 16:41:38 +00:00
|
|
|
if (!buf_append(buf, (const char *)ptr, n))
|
2017-02-08 21:14:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the found sequence is illegal, skip it */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (!buf_putchar(buf, '?'))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* advance beyound the last found valid continuation char */
|
|
|
|
o = v;
|
|
|
|
ptr += v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalid byte (0x00) */
|
2018-02-22 16:41:38 +00:00
|
|
|
else {
|
2017-02-08 21:14:30 +00:00
|
|
|
if (!buf_putchar(buf, '?')) /* or 0xEF, 0xBF, 0xBD */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
o = 1;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = ptr;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanitize given string and strip all invalid XML bytes
|
|
|
|
* Validate UTF-8 sequences
|
|
|
|
* Escape XML control chars */
|
2018-02-22 16:41:38 +00:00
|
|
|
char * pcdata(const char *s, size_t l, size_t *outl)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
struct template_buffer *buf = buf_init(l);
|
2018-02-22 16:41:38 +00:00
|
|
|
const unsigned char *ptr = (const unsigned char *)s;
|
|
|
|
size_t o, v;
|
2017-02-08 21:14:30 +00:00
|
|
|
char esq[8];
|
|
|
|
int esl;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return NULL;
|
|
|
|
|
2018-02-22 23:34:06 +00:00
|
|
|
for (o = 0; o < l; o++) {
|
2017-02-08 21:14:30 +00:00
|
|
|
/* Invalid XML bytes */
|
2018-02-22 13:33:47 +00:00
|
|
|
if ((*ptr <= 0x08) ||
|
2017-02-08 21:14:30 +00:00
|
|
|
((*ptr >= 0x0B) && (*ptr <= 0x0C)) ||
|
|
|
|
((*ptr >= 0x0E) && (*ptr <= 0x1F)) ||
|
|
|
|
(*ptr == 0x7F))
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Escapes */
|
2018-02-22 23:34:06 +00:00
|
|
|
else if ((*ptr == '\'') ||
|
|
|
|
(*ptr == '"') ||
|
|
|
|
(*ptr == '&') ||
|
|
|
|
(*ptr == '<') ||
|
|
|
|
(*ptr == '>'))
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
|
|
|
|
|
|
|
|
if (!buf_append(buf, esq, esl))
|
|
|
|
break;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ascii char */
|
|
|
|
else if (*ptr <= 0x7F)
|
|
|
|
{
|
|
|
|
buf_putchar(buf, (char)*ptr++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* multi byte sequence */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(v = validate_utf8(&ptr, l - o, buf)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
o += (v - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
*outl = buf_length(buf);
|
2017-02-08 21:14:30 +00:00
|
|
|
return buf_destroy(buf);
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
void luastr_escape(struct template_buffer *out, const char *s, size_t l, bool escape_xml)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
int esl;
|
|
|
|
char esq[8];
|
2018-02-22 16:41:38 +00:00
|
|
|
const char *ptr;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 23:34:06 +00:00
|
|
|
for (ptr = s; ptr < (s + l); ptr++) {
|
|
|
|
switch (*ptr) {
|
2017-02-08 21:14:30 +00:00
|
|
|
case '\\':
|
|
|
|
buf_append(out, "\\\\", 2);
|
|
|
|
break;
|
|
|
|
|
2018-02-22 23:34:06 +00:00
|
|
|
case '\'':
|
2017-02-08 21:14:30 +00:00
|
|
|
if (escape_xml)
|
2018-02-22 23:34:06 +00:00
|
|
|
buf_append(out, "'", 5);
|
2017-02-08 21:14:30 +00:00
|
|
|
else
|
2018-02-22 23:34:06 +00:00
|
|
|
buf_append(out, "\\\'", 2);
|
2017-02-08 21:14:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
buf_append(out, "\\n", 2);
|
|
|
|
break;
|
|
|
|
|
2018-02-22 23:34:06 +00:00
|
|
|
case '"':
|
2017-02-08 21:14:30 +00:00
|
|
|
case '&':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
2018-02-22 16:41:38 +00:00
|
|
|
if (escape_xml) {
|
2017-02-08 21:14:30 +00:00
|
|
|
esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
|
|
|
|
buf_append(out, esq, esl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
buf_putchar(out, *ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:41:38 +00:00
|
|
|
void luastr_translate(struct template_buffer *out, const char *s, size_t l, bool escape_xml)
|
2017-02-08 21:14:30 +00:00
|
|
|
{
|
|
|
|
char *tr;
|
2018-02-22 16:41:38 +00:00
|
|
|
size_t trlen;
|
2017-02-08 21:14:30 +00:00
|
|
|
|
2018-02-22 21:47:27 +00:00
|
|
|
if (lmo_translate(s, l, &tr, &trlen))
|
2017-02-08 21:14:30 +00:00
|
|
|
luastr_escape(out, tr, trlen, escape_xml);
|
|
|
|
else
|
|
|
|
luastr_escape(out, s, l, escape_xml);
|
|
|
|
}
|