summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--MirOTR/entities.cpp19
-rw-r--r--MirOTR/entities.h2
2 files changed, 13 insertions, 8 deletions
diff --git a/MirOTR/entities.cpp b/MirOTR/entities.cpp
index 892681b..ce11fe3 100644
--- a/MirOTR/entities.cpp
+++ b/MirOTR/entities.cpp
@@ -324,9 +324,9 @@ static size_t putc_utf8(unsigned long cp, char *buffer)
}
static _Bool parse_entity(const char *current, char **to,
- const char **from, size_t len)
+ const char **from, size_t maxlen)
{
- const char *end = (const char *)memchr(current, ';', len);
+ const char *end = (const char *)memchr(current, ';', maxlen);
if(!end) return 0;
if(current[1] == '#')
@@ -373,24 +373,27 @@ size_t decode_html_entities_utf8(char *dest, const char *src, size_t len)
const char *from = src;
const char *current;
- while((current = (const char*)memchr(from, '&', len)))
+ if (!len) len = strlen(src);
+ size_t remain = len;
+ while((current = (const char*)memchr(from, '&', len-(from-src))))
{
memcpy(to, from, (size_t)(current - from));
to += current - from;
+ //remain = len-(current-src);
- if(parse_entity(current, &to, &from, len))
+ if(parse_entity(current, &to, &from, len-(current-src)))
continue;
from = current;
*to++ = *from++;
}
- size_t remaining = strnlen(from, len);
+ remain = strnlen(from, len-(from-src));
- memcpy(to, from, remaining);
- to += remaining;
+ memcpy(to, from, remain);
+ to += remain;
- *to = 0;
+ if (src!=dest || (size_t)(to-dest) < len ) *to = 0;
return (size_t)(to - dest);
}
diff --git a/MirOTR/entities.h b/MirOTR/entities.h
index bf4c8eb..e4121dc 100644
--- a/MirOTR/entities.h
+++ b/MirOTR/entities.h
@@ -15,6 +15,8 @@ extern size_t decode_html_entities_utf8(char *dest, const char *src, size_t len)
otherwise, the output will be placed in `dest`, which should point
to a buffer big enough to hold `strlen(src) + 1` characters, while
`src` remains unchanged
+ if `len` is given, `dest` must be at least big enough
+ to hold `len + 1` characters.
the function returns the length of the decoded string
*/