text
stringlengths
0
357
}
memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
}
else if ((*s == '&' && (type == '&' || type == ' ' /*|| type == '*'*/))
/*|| (*s == '%' && type == '%')*/)
{
/* entity reference */
for (b = 0; s_ent[b] && strncmp(s + 1, s_ent[b], strlen(s_ent[b])); b += 2)
; /* find entity in entity list */
if (s_ent[b++]) { /* found a match */
if ((c = strlen(s_ent[b])) - 1 > (e = strchr(s, ';')) - s) {
/* the replacement is larger than the entity: enlarge buffer */
l = (d = (s - r)) + c + strlen(e); /* new length */
if (r == original_buf) {
char* new_ret = malloc(l); if (new_ret == NULL) { return r; }
strcpy(new_ret, r);
r = new_ret;
}
else {
char* new_ret = realloc(r, l); if (new_ret == NULL) { return r; }
r = new_ret;
}
e = strchr((s = r + d), ';'); /* fix up pointers */
}
memmove(s + c, e + 1, strlen(e)); /* shift rest of string */
strncpy(s, s_ent[b], c); /* copy in replacement text */
}
else s++; /* not a known entity */
}
else if ((type == ' ' /*|| type == '*'*/) && isspace(*s))
{
*(s++) = ' ';
}
else s++; /* no decoding needed */
}
/* normalize spaces for non-cdata attributes
if (type == '*') {
for (s = r; *s; s++) {
if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
while (*s && *s != ' ') s++;
}
if (--s >= r && *s == ' ') *s = '\0';
}*/
return r;
}
/*******************************************************************************
* Tools
******************************************************************************/
#define XML_WS "\t\r\n "
static void def_starttag_cb (void* userdata, const char* tag, char** attr) { }
static void def_endtag_cb (void* userdata, const char* tag) { }
static void def_text_cb (void* userdata, const char* text, int len) { }
static void call_text_cb(dc_saxparser_t* saxparser, char* text, size_t len, char type)
{
if (text && len)
{
char bak = text[len], *text_new;
text[len] = '\0';
text_new = xml_decode(text, type);
saxparser->text_cb(saxparser->userdata, text_new, len);
if (text != text_new) { free(text_new); }
text[len] = bak;
}
}
static void do_free_attr(char** attr, int* free_attr)
{
/* "attr" are key/value pairs; the function frees the data if the corresponding bit in "free_attr" is set.
(we need this as we try to use the strings from the "main" document instead of allocating small strings) */
#define FREE_KEY 0x01
#define FREE_VALUE 0x02
int i = 0;
while (attr[i]) {
if (free_attr[i>>1]&FREE_KEY && attr[i]) { free(attr[i]); }
if (free_attr[i>>1]&FREE_VALUE && attr[i+1]) { free(attr[i+1]); }
i += 2;
}
attr[0] = NULL; /* set list to zero-length */
}
/*******************************************************************************
* Main interface
******************************************************************************/