gluon-web: make pcdata() prototype match lmo_translate()

This commit is contained in:
Matthias Schiffer 2018-02-23 02:08:25 +01:00
parent 93d3393993
commit 1a426c3bb9
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
3 changed files with 17 additions and 24 deletions

View File

@ -75,21 +75,18 @@ static int template_L_parse_string(lua_State *L)
static int template_L_pcdata(lua_State *L) static int template_L_pcdata(lua_State *L)
{ {
size_t len = 0, outlen; size_t inlen, outlen;
const char *str = luaL_checklstring(L, 1, &len); char *out;
char *res = pcdata(str, len, &outlen); const char *in = luaL_checklstring(L, 1, &inlen);
if (!pcdata(in, inlen, &out, &outlen))
return 0;
if (res != NULL) lua_pushlstring(L, out, outlen);
{ free(out);
lua_pushlstring(L, res, outlen);
free(res);
return 1; return 1;
} }
return 0;
}
static int template_L_load_catalog(lua_State *L) { static int template_L_load_catalog(lua_State *L) {
const char *lang = luaL_optstring(L, 1, "en"); const char *lang = luaL_optstring(L, 1, "en");
const char *dir = luaL_checkstring(L, 2); const char *dir = luaL_checkstring(L, 2);

View File

@ -256,7 +256,7 @@ static size_t validate_utf8(const unsigned char **s, size_t l, struct template_b
/* Sanitize given string and strip all invalid XML bytes /* Sanitize given string and strip all invalid XML bytes
* Validate UTF-8 sequences * Validate UTF-8 sequences
* Escape XML control chars */ * Escape XML control chars */
char * pcdata(const char *s, size_t l, size_t *outl) bool pcdata(const char *s, size_t l, char **out, size_t *outl)
{ {
struct template_buffer *buf = buf_init(l); struct template_buffer *buf = buf_init(l);
const unsigned char *ptr = (const unsigned char *)s; const unsigned char *ptr = (const unsigned char *)s;
@ -265,15 +265,14 @@ char * pcdata(const char *s, size_t l, size_t *outl)
int esl; int esl;
if (!buf) if (!buf)
return NULL; return false;
for (o = 0; o < l; o++) { for (o = 0; o < l; o++) {
/* Invalid XML bytes */ /* Invalid XML bytes */
if ((*ptr <= 0x08) || if ((*ptr <= 0x08) ||
((*ptr >= 0x0B) && (*ptr <= 0x0C)) || ((*ptr >= 0x0B) && (*ptr <= 0x0C)) ||
((*ptr >= 0x0E) && (*ptr <= 0x1F)) || ((*ptr >= 0x0E) && (*ptr <= 0x1F)) ||
(*ptr == 0x7F)) (*ptr == 0x7F)) {
{
ptr++; ptr++;
} }
@ -282,8 +281,7 @@ char * pcdata(const char *s, size_t l, size_t *outl)
(*ptr == '"') || (*ptr == '"') ||
(*ptr == '&') || (*ptr == '&') ||
(*ptr == '<') || (*ptr == '<') ||
(*ptr == '>')) (*ptr == '>')) {
{
esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr); esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
if (!buf_append(buf, esq, esl)) if (!buf_append(buf, esq, esl))
@ -293,14 +291,12 @@ char * pcdata(const char *s, size_t l, size_t *outl)
} }
/* ascii char */ /* ascii char */
else if (*ptr <= 0x7F) else if (*ptr <= 0x7F) {
{
buf_putchar(buf, (char)*ptr++); buf_putchar(buf, (char)*ptr++);
} }
/* multi byte sequence */ /* multi byte sequence */
else else {
{
if (!(v = validate_utf8(&ptr, l - o, buf))) if (!(v = validate_utf8(&ptr, l - o, buf)))
break; break;
@ -309,5 +305,6 @@ char * pcdata(const char *s, size_t l, size_t *outl)
} }
*outl = buf_length(buf); *outl = buf_length(buf);
return buf_destroy(buf); *out = buf_destroy(buf);
return true;
} }

View File

@ -42,7 +42,6 @@ static inline size_t buf_length(const struct template_buffer *buf)
return buf->dptr - buf->data; return buf->dptr - buf->data;
} }
bool pcdata(const char *s, size_t l, char **out, size_t *outl);
char * pcdata(const char *s, size_t l, size_t *outl);
#endif #endif