Use own malloc/free, added freeing of some local buffers at exit

This commit is contained in:
Stefan Ritt 2009-06-10 14:37:16 +00:00
parent 344c6a6667
commit 11888c6aa7

375
mxml.c
View File

@ -108,6 +108,46 @@ static int mxml_find_nodes1(PMXML_NODE tree, const char *xml_path, PMXML_NODE **
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
static char *_encode_buffer = NULL;
static char *_data_enc = NULL;
/*------------------------------------------------------------------*/
void *mxml_malloc(size_t size)
{
return malloc(size);
}
/*------------------------------------------------------------------*/
void *mxml_realloc(void *p, size_t size)
{
return realloc(p, size);
}
/*------------------------------------------------------------------*/
void mxml_free(void *p)
{
free(p);
}
/*------------------------------------------------------------------*/
void mxml_deallocate(void)
{
if (_encode_buffer != NULL) {
mxml_free(_encode_buffer);
_encode_buffer = NULL;
}
if (_data_enc != NULL) {
mxml_free(_data_enc);
_data_enc = NULL;
}
}
/*------------------------------------------------------------------*/
int mxml_write_line(MXML_WRITER *writer, const char *line) int mxml_write_line(MXML_WRITER *writer, const char *line)
{ {
int len; int len;
@ -117,7 +157,7 @@ int mxml_write_line(MXML_WRITER *writer, const char *line)
if (writer->buffer) { if (writer->buffer) {
if (writer->buffer_len + len >= writer->buffer_size) { if (writer->buffer_len + len >= writer->buffer_size) {
writer->buffer_size += 10000; writer->buffer_size += 10000;
writer->buffer = (char *)realloc(writer->buffer, writer->buffer_size); writer->buffer = (char *)mxml_realloc(writer->buffer, writer->buffer_size);
} }
strcpy(writer->buffer + writer->buffer_len, line); strcpy(writer->buffer + writer->buffer_len, line);
writer->buffer_len += len; writer->buffer_len += len;
@ -140,12 +180,12 @@ MXML_WRITER *mxml_open_buffer(void)
time_t now; time_t now;
MXML_WRITER *writer; MXML_WRITER *writer;
writer = (MXML_WRITER *)malloc(sizeof(MXML_WRITER)); writer = (MXML_WRITER *)mxml_malloc(sizeof(MXML_WRITER));
memset(writer, 0, sizeof(MXML_WRITER)); memset(writer, 0, sizeof(MXML_WRITER));
writer->translate = 1; writer->translate = 1;
writer->buffer_size = 10000; writer->buffer_size = 10000;
writer->buffer = (char *)malloc(10000); writer->buffer = (char *)mxml_malloc(10000);
writer->buffer[0] = 0; writer->buffer[0] = 0;
writer->buffer_len = 0; writer->buffer_len = 0;
@ -187,7 +227,7 @@ MXML_WRITER *mxml_open_file(const char *file_name)
time_t now; time_t now;
MXML_WRITER *writer; MXML_WRITER *writer;
writer = (MXML_WRITER *)malloc(sizeof(MXML_WRITER)); writer = (MXML_WRITER *)mxml_malloc(sizeof(MXML_WRITER));
memset(writer, 0, sizeof(MXML_WRITER)); memset(writer, 0, sizeof(MXML_WRITER));
writer->translate = 1; writer->translate = 1;
@ -196,7 +236,7 @@ MXML_WRITER *mxml_open_file(const char *file_name)
if (writer->fh == -1) { if (writer->fh == -1) {
sprintf(line, "Unable to open file \"%s\": ", file_name); sprintf(line, "Unable to open file \"%s\": ", file_name);
perror(line); perror(line);
free(writer); mxml_free(writer);
return NULL; return NULL;
} }
@ -225,22 +265,23 @@ MXML_WRITER *mxml_open_file(const char *file_name)
void mxml_encode(char *src, int size, int translate) void mxml_encode(char *src, int size, int translate)
{ {
char *ps, *pd; char *ps, *pd;
static char *buffer = NULL;
static int buffer_size = 1000; static int buffer_size = 1000;
assert(size); assert(size);
if (buffer == NULL) if (_encode_buffer == NULL) {
buffer = (char *) malloc(buffer_size); _encode_buffer = (char *) mxml_malloc(buffer_size);
atexit(mxml_deallocate);
}
if (size > buffer_size) { if (size > buffer_size) {
buffer = (char *) realloc(buffer, size*2); _encode_buffer = (char *) mxml_realloc(_encode_buffer, size*2);
buffer_size = size; buffer_size = size;
} }
ps = src; ps = src;
pd = buffer; pd = _encode_buffer;
for (ps = src ; *ps && (size_t)pd - (size_t)buffer < (size_t)(size-10) ; ps++) { for (ps = src ; *ps && (size_t)pd - (size_t)_encode_buffer < (size_t)(size-10) ; ps++) {
if (translate) { /* tranlate "<", ">", "&", """, "'" */ if (translate) { /* tranlate "<", ">", "&", """, "'" */
switch (*ps) { switch (*ps) {
@ -284,7 +325,7 @@ void mxml_encode(char *src, int size, int translate)
} }
*pd = 0; *pd = 0;
strlcpy(src, buffer, size); strlcpy(src, _encode_buffer, size);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
@ -367,11 +408,11 @@ int mxml_start_element1(MXML_WRITER *writer, const char *name, int indent)
/* put element on stack */ /* put element on stack */
if (writer->level == 0) if (writer->level == 0)
writer->stack = (char **)malloc(sizeof(char *)); writer->stack = (char **)mxml_malloc(sizeof(char *));
else else
writer->stack = (char **)realloc(writer->stack, sizeof(char *)*(writer->level+1)); writer->stack = (char **)mxml_realloc(writer->stack, sizeof(char *)*(writer->level+1));
writer->stack[writer->level] = (char *) malloc(strlen(name_enc)+1); writer->stack[writer->level] = (char *) mxml_malloc(strlen(name_enc)+1);
strcpy(writer->stack[writer->level], name_enc); strcpy(writer->stack[writer->level], name_enc);
writer->level++; writer->level++;
writer->element_is_open = TRUE; writer->element_is_open = TRUE;
@ -411,9 +452,9 @@ int mxml_end_element(MXML_WRITER *writer)
if (writer->element_is_open) { if (writer->element_is_open) {
writer->element_is_open = FALSE; writer->element_is_open = FALSE;
free(writer->stack[writer->level]); mxml_free(writer->stack[writer->level]);
if (writer->level == 0) if (writer->level == 0)
free(writer->stack); mxml_free(writer->stack);
strcpy(line, "/>\n"); strcpy(line, "/>\n");
return mxml_write_line(writer, line) == (int)strlen(line); return mxml_write_line(writer, line) == (int)strlen(line);
} }
@ -426,9 +467,9 @@ int mxml_end_element(MXML_WRITER *writer)
strlcat(line, "</", sizeof(line)); strlcat(line, "</", sizeof(line));
strlcat(line, writer->stack[writer->level], sizeof(line)); strlcat(line, writer->stack[writer->level], sizeof(line));
free(writer->stack[writer->level]); mxml_free(writer->stack[writer->level]);
if (writer->level == 0) if (writer->level == 0)
free(writer->stack); mxml_free(writer->stack);
strlcat(line, ">\n", sizeof(line)); strlcat(line, ">\n", sizeof(line));
writer->data_was_written = FALSE; writer->data_was_written = FALSE;
@ -464,7 +505,6 @@ int mxml_write_attribute(MXML_WRITER *writer, const char *name, const char *valu
*/ */
int mxml_write_value(MXML_WRITER *writer, const char *data) int mxml_write_value(MXML_WRITER *writer, const char *data)
{ {
static char *data_enc;
static int data_size = 0; static int data_size = 0;
if (!writer->element_is_open) if (!writer->element_is_open)
@ -476,16 +516,16 @@ int mxml_write_value(MXML_WRITER *writer, const char *data)
writer->data_was_written = TRUE; writer->data_was_written = TRUE;
if (data_size == 0) { if (data_size == 0) {
data_enc = (char *)malloc(1000); _data_enc = (char *)mxml_malloc(1000);
data_size = 1000; data_size = 1000;
} else if ((int)strlen(data)*2+1000 > data_size) { } else if ((int)strlen(data)*2+1000 > data_size) {
data_size = 1000+strlen(data)*2; data_size = 1000+strlen(data)*2;
data_enc = (char *)realloc(data_enc, data_size); _data_enc = (char *)mxml_realloc(_data_enc, data_size);
} }
strcpy(data_enc, data); strcpy(_data_enc, data);
mxml_encode(data_enc, data_size, writer->translate); mxml_encode(_data_enc, data_size, writer->translate);
return mxml_write_line(writer, data_enc) == (int)strlen(data_enc); return mxml_write_line(writer, _data_enc) == (int)strlen(_data_enc);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
@ -570,7 +610,7 @@ char *mxml_close_buffer(MXML_WRITER *writer)
mxml_end_element(writer); mxml_end_element(writer);
p = writer->buffer; p = writer->buffer;
free(writer); mxml_free(writer);
return p; return p;
} }
@ -594,7 +634,7 @@ int mxml_close_file(MXML_WRITER *writer)
mxml_end_element(writer); mxml_end_element(writer);
close(writer->fh); close(writer->fh);
free(writer); mxml_free(writer);
return 1; return 1;
} }
@ -626,10 +666,10 @@ PMXML_NODE mxml_add_special_node_at(PMXML_NODE parent, int node_type, const char
assert(parent); assert(parent);
if (parent->n_children == 0) if (parent->n_children == 0)
parent->child = (PMXML_NODE)malloc(sizeof(MXML_NODE)); parent->child = (PMXML_NODE)mxml_malloc(sizeof(MXML_NODE));
else { else {
pchild = parent->child; pchild = parent->child;
parent->child = (PMXML_NODE)realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children+1)); parent->child = (PMXML_NODE)mxml_realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children+1));
} }
assert(parent->child); assert(parent->child);
@ -656,7 +696,7 @@ PMXML_NODE mxml_add_special_node_at(PMXML_NODE parent, int node_type, const char
parent->n_children++; parent->n_children++;
if (value && *value) { if (value && *value) {
pnode->value = (char *)malloc(strlen(value)+1); pnode->value = (char *)mxml_malloc(strlen(value)+1);
assert(pnode->value); assert(pnode->value);
strcpy(pnode->value, value); strcpy(pnode->value, value);
} }
@ -707,10 +747,10 @@ int mxml_add_tree_at(PMXML_NODE parent, PMXML_NODE tree, int idx)
assert(parent); assert(parent);
assert(tree); assert(tree);
if (parent->n_children == 0) if (parent->n_children == 0)
parent->child = (PMXML_NODE)malloc(sizeof(MXML_NODE)); parent->child = (PMXML_NODE)mxml_malloc(sizeof(MXML_NODE));
else { else {
pchild = parent->child; pchild = parent->child;
parent->child = (PMXML_NODE)realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children+1)); parent->child = (PMXML_NODE)mxml_realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children+1));
if (parent->child != pchild) { if (parent->child != pchild) {
/* correct parent pointer for children */ /* correct parent pointer for children */
@ -769,15 +809,15 @@ int mxml_add_tree(PMXML_NODE parent, PMXML_NODE tree)
int mxml_add_attribute(PMXML_NODE pnode, const char *attrib_name, const char *attrib_value) int mxml_add_attribute(PMXML_NODE pnode, const char *attrib_name, const char *attrib_value)
{ {
if (pnode->n_attributes == 0) { if (pnode->n_attributes == 0) {
pnode->attribute_name = (char*)malloc(MXML_NAME_LENGTH); pnode->attribute_name = (char*)mxml_malloc(MXML_NAME_LENGTH);
pnode->attribute_value = (char**)malloc(sizeof(char *)); pnode->attribute_value = (char**)mxml_malloc(sizeof(char *));
} else { } else {
pnode->attribute_name = (char*)realloc(pnode->attribute_name, MXML_NAME_LENGTH*(pnode->n_attributes+1)); pnode->attribute_name = (char*)mxml_realloc(pnode->attribute_name, MXML_NAME_LENGTH*(pnode->n_attributes+1));
pnode->attribute_value = (char**)realloc(pnode->attribute_value, sizeof(char *)*(pnode->n_attributes+1)); pnode->attribute_value = (char**)mxml_realloc(pnode->attribute_value, sizeof(char *)*(pnode->n_attributes+1));
} }
strlcpy(pnode->attribute_name+pnode->n_attributes*MXML_NAME_LENGTH, attrib_name, MXML_NAME_LENGTH); strlcpy(pnode->attribute_name+pnode->n_attributes*MXML_NAME_LENGTH, attrib_name, MXML_NAME_LENGTH);
pnode->attribute_value[pnode->n_attributes] = (char *)malloc(strlen(attrib_value)+1); pnode->attribute_value[pnode->n_attributes] = (char *)mxml_malloc(strlen(attrib_value)+1);
strcpy(pnode->attribute_value[pnode->n_attributes], attrib_value); strcpy(pnode->attribute_value[pnode->n_attributes], attrib_value);
pnode->n_attributes++; pnode->n_attributes++;
@ -818,9 +858,9 @@ int mxml_add_resultnode(PMXML_NODE node, const char *xml_path, PMXML_NODE **node
/* if at end of path, add this node */ /* if at end of path, add this node */
if (*xml_path == 0) { if (*xml_path == 0) {
if (*found == 0) if (*found == 0)
*nodelist = (PMXML_NODE *)malloc(sizeof(PMXML_NODE)); *nodelist = (PMXML_NODE *)mxml_malloc(sizeof(PMXML_NODE));
else else
*nodelist = (PMXML_NODE *)realloc(*nodelist, sizeof(PMXML_NODE)*(*found + 1)); *nodelist = (PMXML_NODE *)mxml_realloc(*nodelist, sizeof(PMXML_NODE)*(*found + 1));
(*nodelist)[*found] = node; (*nodelist)[*found] = node;
(*found)++; (*found)++;
@ -1008,7 +1048,7 @@ PMXML_NODE mxml_find_node(PMXML_NODE tree, const char *xml_path)
n = mxml_find_nodes(tree, xml_path, &node); n = mxml_find_nodes(tree, xml_path, &node);
if (n > 0) { if (n > 0) {
pnode = node[0]; pnode = node[0];
free(node); mxml_free(node);
} else } else
pnode = NULL; pnode = NULL;
@ -1058,9 +1098,9 @@ int mxml_replace_node_name(PMXML_NODE pnode, const char *name)
int mxml_replace_node_value(PMXML_NODE pnode, const char *value) int mxml_replace_node_value(PMXML_NODE pnode, const char *value)
{ {
if (pnode->value) if (pnode->value)
pnode->value = (char *)realloc(pnode->value, strlen(value)+1); pnode->value = (char *)mxml_realloc(pnode->value, strlen(value)+1);
else if (value) else if (value)
pnode->value = (char *)malloc(strlen(value)+1); pnode->value = (char *)mxml_malloc(strlen(value)+1);
else else
pnode->value = NULL; pnode->value = NULL;
@ -1131,7 +1171,7 @@ int mxml_replace_attribute_value(PMXML_NODE pnode, const char *attrib_name, cons
if (i == pnode->n_attributes) if (i == pnode->n_attributes)
return FALSE; return FALSE;
pnode->attribute_value[i] = (char *)realloc(pnode->attribute_value[i], strlen(attrib_value)+1); pnode->attribute_value[i] = (char *)mxml_realloc(pnode->attribute_value[i], strlen(attrib_value)+1);
strcpy(pnode->attribute_value[i], attrib_value); strcpy(pnode->attribute_value[i], attrib_value);
return TRUE; return TRUE;
} }
@ -1162,9 +1202,9 @@ int mxml_delete_node(PMXML_NODE pnode)
memcpy(&parent->child[j], &parent->child[j+1], sizeof(MXML_NODE)); memcpy(&parent->child[j], &parent->child[j+1], sizeof(MXML_NODE));
parent->n_children--; parent->n_children--;
if (parent->n_children) if (parent->n_children)
parent->child = (PMXML_NODE)realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children)); parent->child = (PMXML_NODE)mxml_realloc(parent->child, sizeof(MXML_NODE)*(parent->n_children));
else else
free(parent->child); mxml_free(parent->child);
} }
} else } else
mxml_free_tree(pnode); mxml_free_tree(pnode);
@ -1185,18 +1225,18 @@ int mxml_delete_attribute(PMXML_NODE pnode, const char *attrib_name)
if (i == pnode->n_attributes) if (i == pnode->n_attributes)
return FALSE; return FALSE;
free(pnode->attribute_value[i]); mxml_free(pnode->attribute_value[i]);
for (j=i ; j<pnode->n_attributes-1 ; j++) { for (j=i ; j<pnode->n_attributes-1 ; j++) {
strcpy(pnode->attribute_name+j*MXML_NAME_LENGTH, pnode->attribute_name+(j+1)*MXML_NAME_LENGTH); strcpy(pnode->attribute_name+j*MXML_NAME_LENGTH, pnode->attribute_name+(j+1)*MXML_NAME_LENGTH);
pnode->attribute_value[j] = pnode->attribute_value[j+1]; pnode->attribute_value[j] = pnode->attribute_value[j+1];
} }
if (pnode->n_attributes > 0) { if (pnode->n_attributes > 0) {
pnode->attribute_name = (char *)realloc(pnode->attribute_name, MXML_NAME_LENGTH*(pnode->n_attributes-1)); pnode->attribute_name = (char *)mxml_realloc(pnode->attribute_name, MXML_NAME_LENGTH*(pnode->n_attributes-1));
pnode->attribute_value = (char **)realloc(pnode->attribute_value, sizeof(char *)*(pnode->n_attributes-1)); pnode->attribute_value = (char **)mxml_realloc(pnode->attribute_value, sizeof(char *)*(pnode->n_attributes-1));
} else { } else {
free(pnode->attribute_name); mxml_free(pnode->attribute_name);
free(pnode->attribute_value); mxml_free(pnode->attribute_value);
} }
return TRUE; return TRUE;
@ -1218,7 +1258,7 @@ PMXML_NODE read_error(PMXML_NODE root, const char *file_name, int line_number, c
sprintf(str, "XML read error in file \"%s\", line %d: ", file_name, line_number); sprintf(str, "XML read error in file \"%s\", line %d: ", file_name, line_number);
else else
sprintf(str, "XML read error, line %d: ", line_number); sprintf(str, "XML read error, line %d: ", line_number);
msg = (char *)malloc(error_size); msg = (char *)mxml_malloc(error_size);
strlcpy(error, str, error_size); strlcpy(error, str, error_size);
va_start(argptr, format); va_start(argptr, format);
@ -1226,7 +1266,7 @@ PMXML_NODE read_error(PMXML_NODE root, const char *file_name, int line_number, c
va_end(argptr); va_end(argptr);
strlcat(error, str, error_size); strlcat(error, str, error_size);
free(msg); mxml_free(msg);
mxml_free_tree(root); mxml_free_tree(root);
return NULL; return NULL;
@ -1291,7 +1331,7 @@ PMXML_NODE mxml_parse_buffer(const char *buf, char *error, int error_size)
} }
len = (size_t)p - (size_t)pv; len = (size_t)p - (size_t)pv;
pnew->value = (char *)malloc(len+1); pnew->value = (char *)mxml_malloc(len+1);
memcpy(pnew->value, pv, len); memcpy(pnew->value, pv, len);
pnew->value[len] = 0; pnew->value[len] = 0;
mxml_decode(pnew->value); mxml_decode(pnew->value);
@ -1315,7 +1355,7 @@ PMXML_NODE mxml_parse_buffer(const char *buf, char *error, int error_size)
} }
len = (size_t)p - (size_t)pv; len = (size_t)p - (size_t)pv;
pnew->value = (char *)malloc(len+1); pnew->value = (char *)mxml_malloc(len+1);
memcpy(pnew->value, pv, len); memcpy(pnew->value, pv, len);
pnew->value[len] = 0; pnew->value[len] = 0;
mxml_decode(pnew->value); mxml_decode(pnew->value);
@ -1519,7 +1559,7 @@ PMXML_NODE mxml_parse_buffer(const char *buf, char *error, int error_size)
return read_error(HERE, "Unexpected end of file"); return read_error(HERE, "Unexpected end of file");
len = (size_t)pv - (size_t)p; len = (size_t)pv - (size_t)p;
pnew->value = (char *)malloc(len+1); pnew->value = (char *)mxml_malloc(len+1);
memcpy(pnew->value, p, len); memcpy(pnew->value, p, len);
pnew->value[len] = 0; pnew->value[len] = 0;
mxml_decode(pnew->value); mxml_decode(pnew->value);
@ -1555,7 +1595,7 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
char *p; char *p;
char *pv; char *pv;
char delimiter; char delimiter;
int i, j, k, line_number; int i, j, k, line_number, status;
char *replacement; char *replacement;
char entity_name[MXML_MAX_ENTITY][256]; char entity_name[MXML_MAX_ENTITY][256];
char entity_reference_name[MXML_MAX_ENTITY][256]; char entity_reference_name[MXML_MAX_ENTITY][256];
@ -1564,18 +1604,20 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
int nentity; int nentity;
int fh, length, len; int fh, length, len;
char *buffer; char *buffer;
PMXML_NODE root = mxml_create_root_node(); /* dummy for 'HERE' */
int ip; /* counter for entity value */ int ip; /* counter for entity value */
char directoryname[FILENAME_MAX]; char directoryname[FILENAME_MAX];
char filename[FILENAME_MAX]; char filename[FILENAME_MAX];
int entity_value_length[MXML_MAX_ENTITY]; int entity_value_length[MXML_MAX_ENTITY];
int entity_name_length[MXML_MAX_ENTITY]; int entity_name_length[MXML_MAX_ENTITY];
PMXML_NODE root = mxml_create_root_node(); /* dummy for 'HERE' */
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) for (ip = 0; ip < MXML_MAX_ENTITY; ip++)
entity_value[ip] = NULL; entity_value[ip] = NULL;
line_number = 1; line_number = 1;
nentity = -1; nentity = -1;
status = 0;
if (!buf || !(*buf) || !strlen(*buf)) if (!buf || !(*buf) || !strlen(*buf))
return 0; return 0;
@ -1584,32 +1626,24 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
mxml_dirname(directoryname); mxml_dirname(directoryname);
/* copy string to temporary space */ /* copy string to temporary space */
buffer = (char *) malloc(strlen(*buf) + 1); buffer = (char *) mxml_malloc(strlen(*buf) + 1);
if (buffer == NULL) { if (buffer == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
strcpy(buffer, *buf); strcpy(buffer, *buf);
p = strstr(buffer, "!DOCTYPE"); p = strstr(buffer, "!DOCTYPE");
if (p == NULL) { /* no entities */ if (p == NULL) { /* no entities */
mxml_free_tree(root); status = 0;
free(buffer); goto error;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++)
free(entity_value[ip]);
return 0;
} }
pv = strstr(p, "["); pv = strstr(p, "[");
if (pv == NULL) { /* no entities */ if (pv == NULL) { /* no entities */
mxml_free_tree(root); status = 1;
free(buffer); goto error;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++)
free(entity_value[ip]);
return 0;
} }
p = pv + 1; p = pv + 1;
@ -1630,10 +1664,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (strncmp(p, "!--", 3) == 0) { if (strncmp(p, "!--", 3) == 0) {
@ -1641,10 +1673,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
p += 3; p += 3;
if (strstr(p, "-->") == NULL) { if (strstr(p, "-->") == NULL) {
read_error(HERE, "Unterminated comment"); read_error(HERE, "Unterminated comment");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
while (strncmp(p, "-->", 3) != 0) { while (strncmp(p, "-->", 3) != 0) {
@ -1660,10 +1690,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
nentity++; nentity++;
if (nentity >= MXML_MAX_ENTITY) { if (nentity >= MXML_MAX_ENTITY) {
read_error(HERE, "Too much entities"); read_error(HERE, "Too much entities");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
pv = p + 7; pv = p + 7;
@ -1680,17 +1708,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*p == '<' || *p == '>') { if (*p == '<' || *p == '>') {
read_error(HERE, "Unexpected \'%c\' inside !ENTITY", *p); read_error(HERE, "Unexpected \'%c\' inside !ENTITY", *p);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
pv = p; pv = p;
@ -1699,17 +1723,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if (!*pv) { if (!*pv) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*pv == '<' || *pv == '>') { if (*pv == '<' || *pv == '>') {
read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]); read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
len = (size_t) pv - (size_t) p; len = (size_t) pv - (size_t) p;
@ -1724,17 +1744,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*p == '<') { if (*p == '<') {
read_error(HERE, "Unexpected \'<\' inside entity \"%s\"", &entity_name[nentity][1]); read_error(HERE, "Unexpected \'<\' inside entity \"%s\"", &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
/* extract replacement or SYSTEM */ /* extract replacement or SYSTEM */
@ -1745,17 +1761,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*p == '>') { if (*p == '>') {
read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
/* check if SYSTEM */ /* check if SYSTEM */
@ -1774,34 +1786,26 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*p == '>') { if (*p == '>') {
read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*p != '\"' && *p != '\'') { if (*p != '\"' && *p != '\'') {
read_error(HERE, "Replacement was not found for entity \"%s\"", &entity_name[nentity][1]); read_error(HERE, "Replacement was not found for entity \"%s\"", &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
delimiter = *p; delimiter = *p;
p++; p++;
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
pv = p; pv = p;
while (*pv && *pv != delimiter) while (*pv && *pv != delimiter)
@ -1809,27 +1813,21 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if (!*pv) { if (!*pv) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
if (*pv == '<') { if (*pv == '<') {
read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]); read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]);
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
len = (size_t) pv - (size_t) p; len = (size_t) pv - (size_t) p;
replacement = (char *) malloc(len + 1); replacement = (char *) mxml_malloc(len + 1);
if (replacement == NULL) { if (replacement == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
memcpy(replacement, p, len); memcpy(replacement, p, len);
@ -1839,17 +1837,15 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if (entity_type[nentity] == EXTERNAL_ENTITY) { if (entity_type[nentity] == EXTERNAL_ENTITY) {
strcpy(entity_reference_name[nentity], replacement); strcpy(entity_reference_name[nentity], replacement);
} else { } else {
entity_value[nentity] = (char *) malloc(strlen(replacement)); entity_value[nentity] = (char *) mxml_malloc(strlen(replacement));
if (entity_value[nentity] == NULL) { if (entity_value[nentity] == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
strcpy(entity_value[nentity], replacement); strcpy(entity_value[nentity], replacement);
} }
free(replacement); mxml_free(replacement);
p = pv; p = pv;
while (*p && isspace((unsigned char)*p)) { while (*p && isspace((unsigned char)*p)) {
@ -1859,10 +1855,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
if (!*p) { if (!*p) {
read_error(HERE, "Unexpected end of file"); read_error(HERE, "Unexpected end of file");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
} }
} }
@ -1887,36 +1881,32 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
if (fh == -1) { if (fh == -1) {
entity_value[i] = entity_value[i] =
(char *) malloc(strlen(entity_reference_name[i]) + strlen("<!-- is missing -->") + 1); (char *) mxml_malloc(strlen(entity_reference_name[i]) + strlen("<!-- is missing -->") + 1);
if (entity_value[i] == NULL) { if (entity_value[i] == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
sprintf(entity_value[i], "<!-- %s is missing -->", entity_reference_name[i]); sprintf(entity_value[i], "<!-- %s is missing -->", entity_reference_name[i]);
} else { } else {
length = lseek(fh, 0, SEEK_END); length = lseek(fh, 0, SEEK_END);
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
if (length == 0) { if (length == 0) {
entity_value[i] = (char *) malloc(1); entity_value[i] = (char *) mxml_malloc(1);
if (entity_value[i] == NULL) { if (entity_value[i] == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); close(fh);
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) status = 1;
free(entity_value[ip]); goto error;
return 1;
} }
entity_value[i][0] = 0; entity_value[i][0] = 0;
} else { } else {
entity_value[i] = (char *) malloc(length); entity_value[i] = (char *) mxml_malloc(length);
if (entity_value[i] == NULL) { if (entity_value[i] == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); close(fh);
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) status = 1;
free(entity_value[ip]); goto error;
return 1;
} }
/* read complete file at once */ /* read complete file at once */
@ -1926,11 +1916,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
/* recursive parse */ /* recursive parse */
if (mxml_parse_entity(&entity_value[i], filename, error, error_size) != 0) { if (mxml_parse_entity(&entity_value[i], filename, error, error_size) != 0) {
mxml_free_tree(root); status = 1;
free(buffer); goto error;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++)
free(entity_value[ip]);
return 1;
} }
} }
} }
@ -1955,14 +1942,11 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} }
/* re-allocate memory */ /* re-allocate memory */
free(*buf); *buf = (char *) mxml_realloc(*buf, length + 1);
*buf = (char *) malloc(length + 1);
if (*buf == NULL) { if (*buf == NULL) {
read_error(HERE, "Cannot allocate memory."); read_error(HERE, "Cannot allocate memory.");
free(buffer); status = 1;
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) goto error;
free(entity_value[ip]);
return 1;
} }
/* replace entities */ /* replace entities */
@ -1984,12 +1968,17 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_
} while (*p); } while (*p);
*pv = 0; *pv = 0;
free(buffer); error:
if (buffer != NULL)
mxml_free(buffer);
for (ip = 0; ip < MXML_MAX_ENTITY; ip++) for (ip = 0; ip < MXML_MAX_ENTITY; ip++)
free(entity_value[ip]); if (entity_value[ip] != NULL)
mxml_free(entity_value[ip]);
mxml_free_tree(root); mxml_free_tree(root);
return 0;
return status;
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/
@ -2018,7 +2007,7 @@ PMXML_NODE mxml_parse_file(const char *file_name, char *error, int error_size)
length = lseek(fh, 0, SEEK_END); length = lseek(fh, 0, SEEK_END);
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
buf = (char *)malloc(length+1); buf = (char *)mxml_malloc(length+1);
if (buf == NULL) { if (buf == NULL) {
close(fh); close(fh);
sprintf(line, "Cannot allocate buffer: "); sprintf(line, "Cannot allocate buffer: ");
@ -2033,13 +2022,13 @@ PMXML_NODE mxml_parse_file(const char *file_name, char *error, int error_size)
close(fh); close(fh);
if (mxml_parse_entity(&buf, file_name, error, error_size) != 0) { if (mxml_parse_entity(&buf, file_name, error, error_size) != 0) {
free(buf); mxml_free(buf);
return NULL; return NULL;
} }
root = mxml_parse_buffer(buf, error, error_size); root = mxml_parse_buffer(buf, error, error_size);
free(buf); mxml_free(buf);
return root; return root;
} }
@ -2180,23 +2169,23 @@ void mxml_free_tree(PMXML_NODE tree)
for (i=0 ; i<tree->n_children ; i++) for (i=0 ; i<tree->n_children ; i++)
mxml_free_tree(&tree->child[i]); mxml_free_tree(&tree->child[i]);
if (tree->n_children) if (tree->n_children)
free(tree->child); mxml_free(tree->child);
/* now free dynamic data */ /* now free dynamic data */
for (i=0 ; i<tree->n_attributes ; i++) for (i=0 ; i<tree->n_attributes ; i++)
free(tree->attribute_value[i]); mxml_free(tree->attribute_value[i]);
if (tree->n_attributes) { if (tree->n_attributes) {
free(tree->attribute_name); mxml_free(tree->attribute_name);
free(tree->attribute_value); mxml_free(tree->attribute_value);
} }
if (tree->value) if (tree->value)
free(tree->value); mxml_free(tree->value);
/* if we are the root node, free it */ /* if we are the root node, free it */
if (tree->parent == NULL) if (tree->parent == NULL)
free(tree); mxml_free(tree);
} }
/*------------------------------------------------------------------*/ /*------------------------------------------------------------------*/