Skip to content

Instantly share code, notes, and snippets.

@slayerlab
Last active April 16, 2018 02:31
Show Gist options
  • Save slayerlab/5c87e191fd409b23bed52743f2bd176e to your computer and use it in GitHub Desktop.
Save slayerlab/5c87e191fd409b23bed52743f2bd176e to your computer and use it in GitHub Desktop.
[C] Parsing and Validate XML file for dump all content contained on nodes. (see below about dependencies and exec)
/***
* toolname: RSS.xml Reader
* synopsis: Parsing and Validate all content contained on RSS.xml
* using libXML2
* purpose: Show how use the function xmlReaderforFile() -, just
* for education coding
* author: sl4y3r_0wn3r | slayer owner
* compile: gcc -o <output> -Werror -Wall <source.c> $(xml2-config --libs --cflags) -Winline
* preview: http://i.imgur.com/EleRdUI.png
***/
#include <stdio.h>
#include <stdlib.h>
#include <libxml/xmlreader.h>
#ifndef LIBXML_READER_ENABLED
static inline void /* the function not will be "inline" because GCC doesn't.
* unless 'always_inline' attribute is specified */
print_usage(const char *argv0) __attribute__((always_inline));
static void /* rss.xml handling */
streamXML(const char *filename); /* filename to parse */
static void
procNode(xmlTextReaderPtr reader);
int main(int argc, char *argv[])
{
int ReturnToExit = EXIT_SUCCESS;
if (argc != 2){
print_usage(argv[0]);
goto b_ReturnToExit;
}
LIBXML_TEST_VERSION /* (Macro) for testing API */
streamXML(argv[1]);
xmlCleanupParser();
xmlMemoryDump();
return ReturnToExit; /* EXIT_SUCCESS; */
b_ReturnToExit:
ReturnToExit = EXIT_FAILURE;
return ReturnToExit;
}
#else
void main(void)
{
fprintf(stderr,
"XInclude support not compiled in\n");
exit(EXIT_FAILURE);
}
#endif
static void
procNode(xmlTextReaderPtr reader)
{
const xmlChar *value;
value = xmlTextReaderConstValue(reader);
do{
if (value == NULL){ break; }
if (xmlStrlen(value) > 0)
printf(" %s", (char *)value);
}while(0);
}
static void
streamXML(const char *filename)
{
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename,NULL, /* filename,encoding,int option */
// XML_PARSE_DTDATTR | /* DTD attr defaults */
// XML_PARSE_DTDVALID | /* Valida with the DTD */
XML_PARSE_NOENT);/* No Entities */
if (reader != NULL){
ret = xmlTextReaderRead(reader);
while (!(ret == 0)){
procNode(reader);
ret = xmlTextReaderRead(reader);
}
if (!!(xmlTextReaderIsValid(reader))){ /* edit !! for ! to enable xml validate */
fprintf(stderr,
"[!] Document %s does not validate\n", filename);
}
xmlFreeTextReader(reader);
if (ret != 0){
fprintf(stderr,
"[!] %s: failed to parse\n", filename);
}
}else{
fprintf(stderr,"[!] unable to open %s\n", filename);
}
}
static inline void
print_usage(const char *argv0)
{
fprintf(stderr,
"[!] Usage: %s <rss.xml>\n",argv0);
}
@slayerlab
Copy link
Author

slayerlab commented May 16, 2016

installing LibXML2:

$ apt-get install libxml2 libxml2-dev libxslt1-dev 

compiling:

$ gcc -o <output> -Werror -Wall <source.c> $(xml2-config --libs --cflags)

execute:

root@slayer:~# ./rssxml_reader.c
[!] Usage: ./rssxml_reader.c <rss.xml>

Preview: http://i.imgur.com/EleRdUI.png

@slayerlab
Copy link
Author

Dropping this as public 2 years later.
This is a simple PoC code when I was studying 'XML parsing' in C.

Note: Do not use LibXML in your project because it has so many lack of security, such as denial of service. I recommend "LibXMLSec".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment