diff --git a/mxml.c b/mxml.c index a610934..7c6bf08 100755 --- a/mxml.c +++ b/mxml.c @@ -1397,9 +1397,9 @@ PMXML_NODE mxml_parse_buffer(char *buf, char *error, int error_size) /*------------------------------------------------------------------*/ -PMXML_NODE mxml_parse_entity(char **buf, char *file_name, char *error, int error_size) -/* parse !ENTYTY entries of XML files and replace with references. Return NULL - in case of error, return error description. Optional file_name is used +int mxml_parse_entity(char **buf, char *file_name, char *error, int error_size) +/* parse !ENTYTY entries of XML files and replace with references. Return 0 + in case of no errors, return error description. Optional file_name is used for error reporting if called from mxml_parse_file() */ { char *p; @@ -1425,22 +1425,37 @@ PMXML_NODE mxml_parse_entity(char **buf, char *file_name, char *error, int error line_number = 1; nentity = -1; - strcpy(directoryname, mxml_dirname(file_name)); + if (!buf || !(*buf) || !strlen(*buf)) + return 0; + + strcpy(directoryname, file_name); + mxml_dirname(directoryname); /* copy string to temporary space */ buffer = (char *) malloc(strlen(*buf) + 1); if (buffer == NULL) { - return read_error(HERE, "Cannot allocate memory."); + read_error(HERE, "Cannot allocate memory."); + return 1; } strcpy(buffer, *buf); p = strstr(buffer, "!DOCTYPE"); - if (p == NULL) /* no entities */ - return root; + 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; + } pv = strstr(p, "["); - if (pv == NULL) /* no entities */ - return root; + 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; + } free(*buf); p = pv + 1; @@ -1460,25 +1475,32 @@ PMXML_NODE mxml_parse_entity(char **buf, char *file_name, char *error, int error p++; } if (!*p) { -/* - free(buffer); - for(ip=0;ip") == NULL) { + read_error(HERE, "Unterminated comment"); + return 1; + } + while (strncmp(p, "-->", 3) != 0) { + if (*p == '\n') + line_number++; + p++; + } + p += 3; + } + + else if (strncmp(p, "!ENTITY", 7) == 0) { /* found entity */ nentity++; if (nentity >= MXML_MAX_ENTITY) { -/* - free(buffer); - for(ip=0;ip') { -/* - free(buffer); - for(ip=0;ip') { -/* - free(buffer); - for(ip=0;ip') { -/* - free(buffer); - for(ip=0;ip\' inside entity \"%s\"", &entity_name[nentity][1]); + read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); + return 1; } /* check if SYSTEM */ @@ -1597,72 +1586,42 @@ PMXML_NODE mxml_parse_entity(char **buf, char *file_name, char *error, int error p++; } if (!*p) { -/* - free(buffer); - for(ip=0;ip') { -/* - free(buffer); - for(ip=0;ip\' inside entity \"%s\"", &entity_name[nentity][1]); + read_error(HERE, "Unexpected \'>\' inside entity \"%s\"", &entity_name[nentity][1]); + return 1; } if (*p != '\"' && *p != '\'') { -/* - free(buffer); - for(ip=0;ip") + 1); if (entity_value[i] == NULL) { -/* - free(buffer); - for(ip=0;ip", entity_reference_name[i]); } else { @@ -1735,23 +1682,15 @@ PMXML_NODE mxml_parse_entity(char **buf, char *file_name, char *error, int error if (length == 0) { entity_value[i] = (char *) malloc(1); if (entity_value[i] == NULL) { -/* - free(buffer); - for(ip=0;ip p ) + pv = strrchr(path, ':'); + if (pv > p) p = pv; - pv = strrchr (path, '\\'); - if( pv > p ) + pv = strrchr(path, '\\'); + if (pv > p) p = pv; #endif - if (p == 0) - { - newpath = (char *) malloc(2); - if (newpath == 0) - return NULL; - newpath[0] = '.'; - newpath[1] = 0; - } - else if(p == path){ /* root directory */ - newpath = (char *) malloc(2); - if (newpath == 0) - return NULL; - newpath[0] = *p; - newpath[1] = 0; - } + if (p == 0) /* current directory */ + strcpy(path, "."); + else if (p == path) /* root directory */ + sprintf(path, "%c", *p); else - { - p--; - length = p - path + 1; - newpath = (char *) malloc(length + 1); - if (newpath == 0) - return NULL; - strncpy (newpath, path, length); - newpath[length] = 0; - } - return newpath; + *p = 0; + + return; } /*------------------------------------------------------------------*/ diff --git a/mxml.h b/mxml.h index fe0c41b..3705507 100755 --- a/mxml.h +++ b/mxml.h @@ -94,11 +94,11 @@ int mxml_delete_attribute(PMXML_NODE, char *attrib_name); PMXML_NODE mxml_create_root_node(); PMXML_NODE mxml_parse_file(char *file_name, char *error, int error_size); PMXML_NODE mxml_parse_buffer(char *buffer, char *error, int error_size); -PMXML_NODE mxml_parse_entity(char **buf, char* file_name, char *error, int error_size); +int mxml_parse_entity(char **buf, char* file_name, char *error, int error_size); int mxml_write_tree(char *file_name, PMXML_NODE tree); void mxml_debug_tree(PMXML_NODE tree, int level); void mxml_free_tree(PMXML_NODE tree); -char* mxml_dirname(char* path); -char* mxml_basename(char *path); +void mxml_dirname(char* path); +void mxml_basename(char *path); /*------------------------------------------------------------------*/