-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 'xml' keyword in liblognorm.
- Loading branch information
Showing
7 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,9 @@ | |
#include <errno.h> | ||
#endif | ||
|
||
#include <libxml/xmlmemory.h> | ||
#include <libxml/parser.h> | ||
|
||
|
||
/* how should output values be formatted? */ | ||
enum FMT_MODE { | ||
|
@@ -75,6 +78,38 @@ hParseInt(const unsigned char **buf, size_t *lenBuf) | |
return i; | ||
} | ||
|
||
/* Credits to https://github.com/katie-snow/xml2json-c | ||
This code is under GPL-3.0 License | ||
*/ | ||
static inline void | ||
xml2jsonc_convert_elements(xmlNode *anode, json_object *jobj) | ||
{ | ||
xmlNode *cur_node = NULL; | ||
json_object *cur_jobj = NULL; | ||
json_object *cur_jstr = NULL; | ||
|
||
for (cur_node = anode; cur_node; cur_node = cur_node->next) | ||
{ | ||
if (cur_node->type == XML_ELEMENT_NODE) | ||
{ | ||
if (xmlChildElementCount(cur_node) == 0) | ||
{ | ||
/* JSON string object */ | ||
cur_jobj = json_object_new_object(); | ||
cur_jstr = json_object_new_string((const char *)xmlNodeGetContent(cur_node)); | ||
json_object_object_add(jobj, (const char *)cur_node->name, cur_jstr); | ||
} | ||
else | ||
{ | ||
/* JSON object */ | ||
cur_jobj = json_object_new_object(); | ||
json_object_object_add(jobj, (const char *)cur_node->name, json_object_get(cur_jobj)); | ||
} | ||
} | ||
xml2jsonc_convert_elements(cur_node->children, cur_jobj); | ||
} | ||
} | ||
|
||
/* parser _parse interface | ||
* | ||
* All parsers receive | ||
|
@@ -2325,6 +2360,69 @@ PARSER_Parse(v2IPTables) | |
return r; | ||
} | ||
|
||
/** | ||
* Parse XML. This parser tries to find XML data inside a message. | ||
* If it finds valid XML, it will extract it. | ||
* | ||
* Note: The XML Parser expects a string that begins with '<' and | ||
* ends with '>'. whitespace or any other character at the | ||
* beginning or at the end of the string will cause a parse failure | ||
* | ||
* Note: Is there is extra content after the XML content | ||
* the parser will fail. A hack consist of finding the | ||
* last '>' in the string and ignore the rest. | ||
* | ||
* added 2021-02-01 by [email protected] | ||
*/ | ||
PARSER_Parse(XML) | ||
xmlDocPtr doc = NULL; | ||
xmlNodePtr root_element = NULL; | ||
|
||
/* Find the last occurence of '>' in the string */ | ||
char * pch; | ||
pch=strrchr((const char *) npb->str + *offs, '>'); | ||
|
||
/* Truncate the string after the last occurence of '>' */ | ||
int newLen = pch - (npb->str + *offs) + 1; | ||
char *cstr = strndup(npb->str + *offs, newLen); | ||
CHKN(cstr); | ||
|
||
doc=xmlParseDoc((xmlChar*) cstr); | ||
free(cstr); | ||
|
||
/* Invalid XML string */ | ||
if (doc == NULL) { | ||
goto done; | ||
} | ||
|
||
/* Now convert XML document into JSON document */ | ||
root_element = xmlDocGetRootElement(doc); | ||
json_object *json = NULL; | ||
json = json_object_new_object(); | ||
xml2jsonc_convert_elements(root_element, json); | ||
|
||
if(json == NULL) | ||
goto done; | ||
|
||
/* parsing OK */ | ||
*parsed = newLen ; | ||
r = 0; | ||
|
||
if(value == NULL) { | ||
json_object_put(json); | ||
} else { | ||
*value = json; | ||
} | ||
|
||
done: | ||
if(doc != NULL) | ||
xmlFreeDoc(doc); | ||
xmlCleanupParser(); | ||
return r; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Parse JSON. This parser tries to find JSON data inside a message. | ||
* If it finds valid JSON, it will extract it. Extra data after the | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters