diff --git a/mxml.c b/mxml.c index cba124f..c51bf06 100755 --- a/mxml.c +++ b/mxml.c @@ -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 len; @@ -117,7 +157,7 @@ int mxml_write_line(MXML_WRITER *writer, const char *line) if (writer->buffer) { if (writer->buffer_len + len >= writer->buffer_size) { 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); writer->buffer_len += len; @@ -140,12 +180,12 @@ MXML_WRITER *mxml_open_buffer(void) time_t now; MXML_WRITER *writer; - writer = (MXML_WRITER *)malloc(sizeof(MXML_WRITER)); + writer = (MXML_WRITER *)mxml_malloc(sizeof(MXML_WRITER)); memset(writer, 0, sizeof(MXML_WRITER)); writer->translate = 1; writer->buffer_size = 10000; - writer->buffer = (char *)malloc(10000); + writer->buffer = (char *)mxml_malloc(10000); writer->buffer[0] = 0; writer->buffer_len = 0; @@ -187,7 +227,7 @@ MXML_WRITER *mxml_open_file(const char *file_name) time_t now; MXML_WRITER *writer; - writer = (MXML_WRITER *)malloc(sizeof(MXML_WRITER)); + writer = (MXML_WRITER *)mxml_malloc(sizeof(MXML_WRITER)); memset(writer, 0, sizeof(MXML_WRITER)); writer->translate = 1; @@ -196,7 +236,7 @@ MXML_WRITER *mxml_open_file(const char *file_name) if (writer->fh == -1) { sprintf(line, "Unable to open file \"%s\": ", file_name); perror(line); - free(writer); + mxml_free(writer); return NULL; } @@ -225,22 +265,23 @@ MXML_WRITER *mxml_open_file(const char *file_name) void mxml_encode(char *src, int size, int translate) { char *ps, *pd; - static char *buffer = NULL; static int buffer_size = 1000; assert(size); - if (buffer == NULL) - buffer = (char *) malloc(buffer_size); + if (_encode_buffer == NULL) { + _encode_buffer = (char *) mxml_malloc(buffer_size); + atexit(mxml_deallocate); + } if (size > buffer_size) { - buffer = (char *) realloc(buffer, size*2); + _encode_buffer = (char *) mxml_realloc(_encode_buffer, size*2); buffer_size = size; } ps = src; - pd = buffer; - for (ps = src ; *ps && (size_t)pd - (size_t)buffer < (size_t)(size-10) ; ps++) { + pd = _encode_buffer; + for (ps = src ; *ps && (size_t)pd - (size_t)_encode_buffer < (size_t)(size-10) ; ps++) { if (translate) { /* tranlate "<", ">", "&", """, "'" */ switch (*ps) { @@ -284,7 +325,7 @@ void mxml_encode(char *src, int size, int translate) } *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 */ if (writer->level == 0) - writer->stack = (char **)malloc(sizeof(char *)); + writer->stack = (char **)mxml_malloc(sizeof(char *)); 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); writer->level++; writer->element_is_open = TRUE; @@ -411,9 +452,9 @@ int mxml_end_element(MXML_WRITER *writer) if (writer->element_is_open) { writer->element_is_open = FALSE; - free(writer->stack[writer->level]); + mxml_free(writer->stack[writer->level]); if (writer->level == 0) - free(writer->stack); + mxml_free(writer->stack); strcpy(line, "/>\n"); return mxml_write_line(writer, line) == (int)strlen(line); } @@ -426,9 +467,9 @@ int mxml_end_element(MXML_WRITER *writer) strlcat(line, "stack[writer->level], sizeof(line)); - free(writer->stack[writer->level]); + mxml_free(writer->stack[writer->level]); if (writer->level == 0) - free(writer->stack); + mxml_free(writer->stack); strlcat(line, ">\n", sizeof(line)); 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) { - static char *data_enc; static int data_size = 0; if (!writer->element_is_open) @@ -476,16 +516,16 @@ int mxml_write_value(MXML_WRITER *writer, const char *data) writer->data_was_written = TRUE; if (data_size == 0) { - data_enc = (char *)malloc(1000); + _data_enc = (char *)mxml_malloc(1000); data_size = 1000; } else if ((int)strlen(data)*2+1000 > data_size) { 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); - mxml_encode(data_enc, data_size, writer->translate); - return mxml_write_line(writer, data_enc) == (int)strlen(data_enc); + strcpy(_data_enc, data); + mxml_encode(_data_enc, data_size, writer->translate); + 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); p = writer->buffer; - free(writer); + mxml_free(writer); return p; } @@ -594,7 +634,7 @@ int mxml_close_file(MXML_WRITER *writer) mxml_end_element(writer); close(writer->fh); - free(writer); + mxml_free(writer); return 1; } @@ -626,10 +666,10 @@ PMXML_NODE mxml_add_special_node_at(PMXML_NODE parent, int node_type, const char assert(parent); if (parent->n_children == 0) - parent->child = (PMXML_NODE)malloc(sizeof(MXML_NODE)); + parent->child = (PMXML_NODE)mxml_malloc(sizeof(MXML_NODE)); else { 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); @@ -656,7 +696,7 @@ PMXML_NODE mxml_add_special_node_at(PMXML_NODE parent, int node_type, const char parent->n_children++; if (value && *value) { - pnode->value = (char *)malloc(strlen(value)+1); + pnode->value = (char *)mxml_malloc(strlen(value)+1); assert(pnode->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(tree); if (parent->n_children == 0) - parent->child = (PMXML_NODE)malloc(sizeof(MXML_NODE)); + parent->child = (PMXML_NODE)mxml_malloc(sizeof(MXML_NODE)); else { 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) { /* 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) { if (pnode->n_attributes == 0) { - pnode->attribute_name = (char*)malloc(MXML_NAME_LENGTH); - pnode->attribute_value = (char**)malloc(sizeof(char *)); + pnode->attribute_name = (char*)mxml_malloc(MXML_NAME_LENGTH); + pnode->attribute_value = (char**)mxml_malloc(sizeof(char *)); } else { - pnode->attribute_name = (char*)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_name = (char*)mxml_realloc(pnode->attribute_name, MXML_NAME_LENGTH*(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); - 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); 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 (*xml_path == 0) { if (*found == 0) - *nodelist = (PMXML_NODE *)malloc(sizeof(PMXML_NODE)); + *nodelist = (PMXML_NODE *)mxml_malloc(sizeof(PMXML_NODE)); 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; (*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); if (n > 0) { pnode = node[0]; - free(node); + mxml_free(node); } else 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) { 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) - pnode->value = (char *)malloc(strlen(value)+1); + pnode->value = (char *)mxml_malloc(strlen(value)+1); else 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) 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); return TRUE; } @@ -1162,9 +1202,9 @@ int mxml_delete_node(PMXML_NODE pnode) memcpy(&parent->child[j], &parent->child[j+1], sizeof(MXML_NODE)); 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 - free(parent->child); + mxml_free(parent->child); } } else mxml_free_tree(pnode); @@ -1185,18 +1225,18 @@ int mxml_delete_attribute(PMXML_NODE pnode, const char *attrib_name) if (i == pnode->n_attributes) return FALSE; - free(pnode->attribute_value[i]); + mxml_free(pnode->attribute_value[i]); for (j=i ; jn_attributes-1 ; j++) { 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]; } if (pnode->n_attributes > 0) { - pnode->attribute_name = (char *)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_name = (char *)mxml_realloc(pnode->attribute_name, MXML_NAME_LENGTH*(pnode->n_attributes-1)); + pnode->attribute_value = (char **)mxml_realloc(pnode->attribute_value, sizeof(char *)*(pnode->n_attributes-1)); } else { - free(pnode->attribute_name); - free(pnode->attribute_value); + mxml_free(pnode->attribute_name); + mxml_free(pnode->attribute_value); } 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); else 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); 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); strlcat(error, str, error_size); - free(msg); + mxml_free(msg); mxml_free_tree(root); 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; - pnew->value = (char *)malloc(len+1); + pnew->value = (char *)mxml_malloc(len+1); memcpy(pnew->value, pv, len); pnew->value[len] = 0; 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; - pnew->value = (char *)malloc(len+1); + pnew->value = (char *)mxml_malloc(len+1); memcpy(pnew->value, pv, len); pnew->value[len] = 0; 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"); 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); pnew->value[len] = 0; 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 *pv; char delimiter; - int i, j, k, line_number; + int i, j, k, line_number, status; char *replacement; char entity_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 fh, length, len; char *buffer; - PMXML_NODE root = mxml_create_root_node(); /* dummy for 'HERE' */ int ip; /* counter for entity value */ char directoryname[FILENAME_MAX]; char filename[FILENAME_MAX]; int entity_value_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++) entity_value[ip] = NULL; line_number = 1; nentity = -1; + status = 0; if (!buf || !(*buf) || !strlen(*buf)) return 0; @@ -1584,32 +1626,24 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ mxml_dirname(directoryname); /* copy string to temporary space */ - buffer = (char *) malloc(strlen(*buf) + 1); + buffer = (char *) mxml_malloc(strlen(*buf) + 1); if (buffer == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } strcpy(buffer, *buf); p = strstr(buffer, "!DOCTYPE"); if (p == NULL) { /* no entities */ - mxml_free_tree(root); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 0; + status = 0; + goto error; } pv = strstr(p, "["); if (pv == NULL) { /* no entities */ - mxml_free_tree(root); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 0; + status = 1; + goto error; } p = pv + 1; @@ -1630,10 +1664,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } if (!*p) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } 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; if (strstr(p, "-->") == NULL) { read_error(HERE, "Unterminated comment"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } while (strncmp(p, "-->", 3) != 0) { @@ -1660,10 +1690,8 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ nentity++; if (nentity >= MXML_MAX_ENTITY) { read_error(HERE, "Too much entities"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } pv = p + 7; @@ -1680,17 +1708,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } if (!*p) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*p == '<' || *p == '>') { read_error(HERE, "Unexpected \'%c\' inside !ENTITY", *p); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } pv = p; @@ -1699,17 +1723,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ if (!*pv) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*pv == '<' || *pv == '>') { read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } 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) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*p == '<') { read_error(HERE, "Unexpected \'<\' inside entity \"%s\"", &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } /* extract replacement or SYSTEM */ @@ -1745,17 +1761,13 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } if (!*p) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*p == '>') { read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } /* check if SYSTEM */ @@ -1774,34 +1786,26 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } if (!*p) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*p == '>') { read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*p != '\"' && *p != '\'') { read_error(HERE, "Replacement was not found for entity \"%s\"", &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } delimiter = *p; p++; if (!*p) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } pv = p; while (*pv && *pv != delimiter) @@ -1809,27 +1813,21 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ if (!*pv) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } if (*pv == '<') { read_error(HERE, "Unexpected \'%c\' inside entity \"%s\"", *pv, &entity_name[nentity][1]); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } len = (size_t) pv - (size_t) p; - replacement = (char *) malloc(len + 1); + replacement = (char *) mxml_malloc(len + 1); if (replacement == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } 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) { strcpy(entity_reference_name[nentity], replacement); } else { - entity_value[nentity] = (char *) malloc(strlen(replacement)); + entity_value[nentity] = (char *) mxml_malloc(strlen(replacement)); if (entity_value[nentity] == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } strcpy(entity_value[nentity], replacement); } - free(replacement); + mxml_free(replacement); p = pv; 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) { read_error(HERE, "Unexpected end of file"); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } } } @@ -1887,36 +1881,32 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ if (fh == -1) { entity_value[i] = - (char *) malloc(strlen(entity_reference_name[i]) + strlen("") + 1); + (char *) mxml_malloc(strlen(entity_reference_name[i]) + strlen("") + 1); if (entity_value[i] == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } sprintf(entity_value[i], "", entity_reference_name[i]); } else { length = lseek(fh, 0, SEEK_END); lseek(fh, 0, SEEK_SET); if (length == 0) { - entity_value[i] = (char *) malloc(1); + entity_value[i] = (char *) mxml_malloc(1); if (entity_value[i] == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + close(fh); + status = 1; + goto error; } entity_value[i][0] = 0; } else { - entity_value[i] = (char *) malloc(length); + entity_value[i] = (char *) mxml_malloc(length); if (entity_value[i] == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + close(fh); + status = 1; + goto error; } /* 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 */ if (mxml_parse_entity(&entity_value[i], filename, error, error_size) != 0) { - mxml_free_tree(root); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } } } @@ -1955,14 +1942,11 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } /* re-allocate memory */ - free(*buf); - *buf = (char *) malloc(length + 1); + *buf = (char *) mxml_realloc(*buf, length + 1); if (*buf == NULL) { read_error(HERE, "Cannot allocate memory."); - free(buffer); - for (ip = 0; ip < MXML_MAX_ENTITY; ip++) - free(entity_value[ip]); - return 1; + status = 1; + goto error; } /* replace entities */ @@ -1984,12 +1968,17 @@ int mxml_parse_entity(char **buf, const char *file_name, char *error, int error_ } while (*p); *pv = 0; - free(buffer); +error: + + if (buffer != NULL) + mxml_free(buffer); 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); - 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); lseek(fh, 0, SEEK_SET); - buf = (char *)malloc(length+1); + buf = (char *)mxml_malloc(length+1); if (buf == NULL) { close(fh); 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); if (mxml_parse_entity(&buf, file_name, error, error_size) != 0) { - free(buf); + mxml_free(buf); return NULL; } root = mxml_parse_buffer(buf, error, error_size); - free(buf); + mxml_free(buf); return root; } @@ -2180,23 +2169,23 @@ void mxml_free_tree(PMXML_NODE tree) for (i=0 ; in_children ; i++) mxml_free_tree(&tree->child[i]); if (tree->n_children) - free(tree->child); + mxml_free(tree->child); /* now free dynamic data */ for (i=0 ; in_attributes ; i++) - free(tree->attribute_value[i]); + mxml_free(tree->attribute_value[i]); if (tree->n_attributes) { - free(tree->attribute_name); - free(tree->attribute_value); + mxml_free(tree->attribute_name); + mxml_free(tree->attribute_value); } if (tree->value) - free(tree->value); + mxml_free(tree->value); /* if we are the root node, free it */ if (tree->parent == NULL) - free(tree); + mxml_free(tree); } /*------------------------------------------------------------------*/