Skip to content

Commit

Permalink
Add alternate string management support.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Mar 7, 2024
1 parent 1dfe8e7 commit 9e22b3e
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 104 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
`MXML_TYPE_DIRECTIVE` node types (Issue #250)
- Added `mxmlLoadFilename` and `mxmlSaveFilename` functions (Issue #291)
- Added AFL fuzzing support (Issue #306)
- Added string copy/free callbacks to support alternate memory management of
strings.
- Renamed `mxml_type_t` enumerations to `MXML_TYPE_xxx` (Issue #251)
- Updated APIs to use bool type instead of an int representing a boolean value.
- Updated the SAX callback to return a `bool` value to control processing
Expand Down
16 changes: 8 additions & 8 deletions mxml-attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ mxmlElementDeleteAttr(mxml_node_t *node,// I - Element
if (!strcmp(attr->name, name))
{
// Delete this attribute...
free(attr->name);
free(attr->value);
_mxml_strfree(attr->name);
_mxml_strfree(attr->value);

i --;
if (i > 0)
Expand Down Expand Up @@ -165,7 +165,7 @@ mxmlElementSetAttr(mxml_node_t *node, // I - Element node

if (value)
{
if ((valuec = strdup(value)) == NULL)
if ((valuec = _mxml_strcopy(value)) == NULL)
{
_mxml_error("Unable to allocate memory for attribute '%s' in element %s.", name, node->value.element.name);
return;
Expand All @@ -177,7 +177,7 @@ mxmlElementSetAttr(mxml_node_t *node, // I - Element node
}

if (!mxml_set_attr(node, name, valuec))
free(valuec);
_mxml_strfree(valuec);
}


Expand Down Expand Up @@ -212,10 +212,10 @@ mxmlElementSetAttrf(mxml_node_t *node, // I - Element node
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

if ((value = strdup(buffer)) == NULL)
if ((value = _mxml_strcopy(buffer)) == NULL)
_mxml_error("Unable to allocate memory for attribute '%s' in element %s.", name, node->value.element.name);
else if (!mxml_set_attr(node, name, value))
free(value);
_mxml_strfree(value);
}


Expand All @@ -238,7 +238,7 @@ mxml_set_attr(mxml_node_t *node, // I - Element node
if (!strcmp(attr->name, name))
{
// Free the old value as needed...
free(attr->value);
_mxml_strfree(attr->value);
attr->value = value;

return (true);
Expand All @@ -255,7 +255,7 @@ mxml_set_attr(mxml_node_t *node, // I - Element node
node->value.element.attrs = attr;
attr += node->value.element.num_attrs;

if ((attr->name = strdup(name)) == NULL)
if ((attr->name = _mxml_strcopy(name)) == NULL)
{
_mxml_error("Unable to allocate memory for attribute '%s' in element %s.", name, node->value.element.name);
return (false);
Expand Down
44 changes: 0 additions & 44 deletions mxml-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,50 +527,6 @@ mxmlSaveString(
}


//
// 'mxmlSetCustomHandlers()' - Set the handling functions for custom data.
//
// The load function accepts a node pointer and a data string and must
// return 0 on success and non-zero on error.
//
// The save function accepts a node pointer and must return a malloc'd
// string on success and `NULL` on error.
//

void
mxmlSetCustomCallbacks(
mxml_custom_load_cb_t load_cb, // I - Load callback function
mxml_custom_save_cb_t save_cb, // I - Save callback function
void *cbdata) // I - Callback data
{
_mxml_global_t *global = _mxml_global();
// Global data


global->custom_load_cb = load_cb;
global->custom_save_cb = save_cb;
global->custom_cbdata = cbdata;
}


//
// 'mxmlSetErrorCallback()' - Set the error message callback.
//

void
mxmlSetErrorCallback(
mxml_error_cb_t cb, // I - Error callback function
void *cbdata) // I - Error callback data
{
_mxml_global_t *global = _mxml_global();
// Global data


global->error_cb = cb;
global->error_cbdata = cbdata;
}


//
// 'mxmlSetWrapMargin()' - Set the wrap margin when saving XML data.
//
Expand Down
4 changes: 2 additions & 2 deletions mxml-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mxmlIndexDelete(mxml_index_t *ind) // I - Index to delete
return;

// Free memory...
free(ind->attr);
_mxml_strfree(ind->attr);
free(ind->nodes);
free(ind);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ mxmlIndexNew(mxml_node_t *node, // I - XML node tree

if (attr)
{
if ((ind->attr = strdup(attr)) == NULL)
if ((ind->attr = _mxml_strcopy(attr)) == NULL)
{
_mxml_error("Unable to allocate memory for index attribute.");
free(ind);
Expand Down
44 changes: 22 additions & 22 deletions mxml-node.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ mxmlNewCDATA(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
// Create the node and set the name value...
if ((node = mxml_new(parent, MXML_TYPE_CDATA)) != NULL)
{
if ((node->value.cdata = strdup(data)) == NULL)
if ((node->value.cdata = _mxml_strcopy(data)) == NULL)
{
_mxml_error("Unable to allocate memory for CDATA.");
mxmlDelete(node);
Expand Down Expand Up @@ -251,7 +251,7 @@ mxmlNewCDATAf(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

node->value.cdata = strdup(buffer);
node->value.cdata = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -283,7 +283,7 @@ mxmlNewComment(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
// Create the node and set the name value...
if ((node = mxml_new(parent, MXML_TYPE_COMMENT)) != NULL)
{
if ((node->value.comment = strdup(comment)) == NULL)
if ((node->value.comment = _mxml_strcopy(comment)) == NULL)
{
_mxml_error("Unable to allocate memory for comment.");
mxmlDelete(node);
Expand Down Expand Up @@ -327,7 +327,7 @@ mxmlNewCommentf(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

node->value.comment = strdup(buffer);
node->value.comment = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -391,7 +391,7 @@ mxmlNewDeclaration(
// Create the node and set the name value...
if ((node = mxml_new(parent, MXML_TYPE_DECLARATION)) != NULL)
{
if ((node->value.declaration = strdup(declaration)) == NULL)
if ((node->value.declaration = _mxml_strcopy(declaration)) == NULL)
{
_mxml_error("Unable to allocate memory for declaration.");
mxmlDelete(node);
Expand Down Expand Up @@ -436,7 +436,7 @@ mxmlNewDeclarationf(
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

node->value.declaration = strdup(buffer);
node->value.declaration = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -468,7 +468,7 @@ mxmlNewDirective(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
// Create the node and set the name value...
if ((node = mxml_new(parent, MXML_TYPE_DIRECTIVE)) != NULL)
{
if ((node->value.directive = strdup(directive)) == NULL)
if ((node->value.directive = _mxml_strcopy(directive)) == NULL)
{
_mxml_error("Unable to allocate memory for processing instruction.");
mxmlDelete(node);
Expand Down Expand Up @@ -512,7 +512,7 @@ mxmlNewDirectivef(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

node->value.directive = strdup(buffer);
node->value.directive = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -542,7 +542,7 @@ mxmlNewElement(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`

// Create the node and set the element name...
if ((node = mxml_new(parent, MXML_TYPE_ELEMENT)) != NULL)
node->value.element.name = strdup(name);
node->value.element.name = _mxml_strcopy(name);

return (node);
}
Expand Down Expand Up @@ -597,7 +597,7 @@ mxmlNewOpaque(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`

// Create the node and set the element name...
if ((node = mxml_new(parent, MXML_TYPE_OPAQUE)) != NULL)
node->value.opaque = strdup(opaque);
node->value.opaque = _mxml_strcopy(opaque);

return (node);
}
Expand Down Expand Up @@ -635,7 +635,7 @@ mxmlNewOpaquef(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);

node->value.opaque = strdup(buffer);
node->value.opaque = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -695,7 +695,7 @@ mxmlNewText(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
if ((node = mxml_new(parent, MXML_TYPE_TEXT)) != NULL)
{
node->value.text.whitespace = whitespace;
node->value.text.string = strdup(string);
node->value.text.string = _mxml_strcopy(string);
}

return (node);
Expand Down Expand Up @@ -737,7 +737,7 @@ mxmlNewTextf(mxml_node_t *parent, // I - Parent node or `MXML_NO_PARENT`
va_end(ap);

node->value.text.whitespace = whitespace;
node->value.text.string = strdup(buffer);
node->value.text.string = _mxml_strcopy(buffer);
}

return (node);
Expand Down Expand Up @@ -854,26 +854,26 @@ mxml_free(mxml_node_t *node) // I - Node
switch (node->type)
{
case MXML_TYPE_CDATA :
free(node->value.cdata);
_mxml_strfree(node->value.cdata);
break;
case MXML_TYPE_COMMENT :
free(node->value.comment);
_mxml_strfree(node->value.comment);
break;
case MXML_TYPE_DECLARATION :
free(node->value.declaration);
_mxml_strfree(node->value.declaration);
break;
case MXML_TYPE_DIRECTIVE :
free(node->value.directive);
_mxml_strfree(node->value.directive);
break;
case MXML_TYPE_ELEMENT :
free(node->value.element.name);
_mxml_strfree(node->value.element.name);

if (node->value.element.num_attrs)
{
for (i = 0; i < node->value.element.num_attrs; i ++)
{
free(node->value.element.attrs[i].name);
free(node->value.element.attrs[i].value);
_mxml_strfree(node->value.element.attrs[i].name);
_mxml_strfree(node->value.element.attrs[i].value);
}

free(node->value.element.attrs);
Expand All @@ -883,13 +883,13 @@ mxml_free(mxml_node_t *node) // I - Node
// Nothing to do
break;
case MXML_TYPE_OPAQUE :
free(node->value.opaque);
_mxml_strfree(node->value.opaque);
break;
case MXML_TYPE_REAL :
// Nothing to do
break;
case MXML_TYPE_TEXT :
free(node->value.text.string);
_mxml_strfree(node->value.text.string);
break;
case MXML_TYPE_CUSTOM :
if (node->value.custom.data && node->value.custom.destroy)
Expand Down
Loading

0 comments on commit 9e22b3e

Please sign in to comment.