From c3e01fdd4361709ffd0939e99b37447937e313e2 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 22 Jun 2008 16:02:13 +0200 Subject: [PATCH] Add buffer_add_xmlescaped, and malloc-- Signed-off-by: Pierre Habouzit --- common/buffer.c | 30 ++++++++++++++++++++++++++++++ common/buffer.h | 5 ++++- common/markup.c | 6 ++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/common/buffer.c b/common/buffer.c index 1e9f79041..ebb31a220 100644 --- a/common/buffer.c +++ b/common/buffer.c @@ -104,3 +104,33 @@ char *buffer_detach(buffer_t *buf) buffer_init(buf); return res; } + +void buffer_add_xmlescaped(buffer_t *buf, const char *s) +{ + for (;;) { + int len = strcspn(s, "&<>'\""); + buffer_add(buf, s, len); + s += len; + + switch (*s++) { + case '\0': + return; + case '&': + buffer_adds(buf, "&"); + break; + case '<': + buffer_adds(buf, "<"); + break; + case '>': + buffer_adds(buf, ">"); + break; + case '\'': + /* OG: why not use default? */ + buffer_adds(buf, "'"); + break; + case '"': + buffer_adds(buf, """); + break; + } + } +} diff --git a/common/buffer.h b/common/buffer.h index 7c79525da..17a57305f 100644 --- a/common/buffer.h +++ b/common/buffer.h @@ -109,7 +109,7 @@ buffer_splice(buffer_t *buf, int pos, int len, const void *data, int dlen) } -static inline void buffer_add(buffer_t *buf, void *data, int len) { +static inline void buffer_add(buffer_t *buf, const void *data, int len) { buffer_splice(buf, buf->len, 0, data, len); } static inline void buffer_adds(buffer_t *buf, const char *s) { @@ -127,4 +127,7 @@ void buffer_addvf(buffer_t *buf, const char *fmt, va_list) void buffer_addf(buffer_t *buf, const char *fmt, ...) __attribute__((format(printf, 2, 3))); + +void buffer_add_xmlescaped(buffer_t *buf, const char *s); + #endif /* MC_BASE_BUFFER_H */ diff --git a/common/markup.c b/common/markup.c index 6906d393f..094513b25 100644 --- a/common/markup.c +++ b/common/markup.c @@ -126,11 +126,9 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)), GError **error __attribute__ ((unused))) { markup_parser_data_t *p = (markup_parser_data_t *) user_data; - char *esc; - esc = g_markup_escape_text(text, text_len); - buffer_adds(&p->text, esc); - p_delete(&esc); + buffer_grow(&p->text, text_len); + buffer_add_xmlescaped(&p->text, text); } /** Create a markup_parser_data_t structure with elements list.