2013-08-03 20:11:58 +02:00
|
|
|
#include "validatexml.h"
|
2013-07-21 17:00:12 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
void skipspaces(const char xmldata[], int *pos, int *linenr)
|
|
|
|
{
|
|
|
|
const char *p = &xmldata[*pos];
|
|
|
|
while (isspace(*p) || *p == '\r' || *p == '\n') {
|
|
|
|
if (strncmp(p,"\r\n",2)==0)
|
|
|
|
++p;
|
|
|
|
if (*p == '\r' || *p == '\n')
|
|
|
|
++(*linenr);
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
*pos = p - xmldata;
|
|
|
|
}
|
|
|
|
|
|
|
|
int validatexml(const char xmldata[])
|
|
|
|
{
|
|
|
|
if (strncmp(xmldata,"<?xml version=\"1.0\"?>",21)!=0)
|
|
|
|
return 1;
|
|
|
|
int linenr = 1;
|
|
|
|
char elementNames[10][64]; // only 10 element levels handled
|
|
|
|
int level = 0;
|
|
|
|
for (int pos = 21; xmldata[pos]; pos++) {
|
|
|
|
if (strncmp(&xmldata[pos], "\r\n", 2)==0) {
|
|
|
|
++linenr;
|
|
|
|
++pos;
|
|
|
|
} else if (xmldata[pos]=='\r' || xmldata[pos]=='\n') {
|
|
|
|
++linenr;
|
|
|
|
} else if (xmldata[pos] == '<') {
|
2013-07-29 10:21:10 +02:00
|
|
|
// found a element, validate it
|
2013-07-21 17:00:12 +02:00
|
|
|
++pos;
|
|
|
|
skipspaces(xmldata,&pos,&linenr);
|
2013-07-29 10:21:10 +02:00
|
|
|
|
|
|
|
// is this a end-element?
|
2013-07-21 17:00:12 +02:00
|
|
|
if (xmldata[pos] == '/') {
|
2013-07-29 10:21:10 +02:00
|
|
|
// end element without any previous start element
|
2013-07-21 17:00:12 +02:00
|
|
|
if (level <= 0) {
|
|
|
|
return linenr;
|
|
|
|
}
|
|
|
|
--level;
|
2013-07-29 10:21:10 +02:00
|
|
|
// compare name of end element with name of start element
|
2013-07-21 17:00:12 +02:00
|
|
|
int len = strlen(elementNames[level]);
|
|
|
|
if (strncmp(&xmldata[pos+1],elementNames[level],len)!=0 || xmldata[pos+1+len]!='>')
|
|
|
|
return linenr;
|
|
|
|
pos += 1 + len;
|
|
|
|
} else {
|
2013-07-29 10:21:10 +02:00
|
|
|
// this validator allows max 8 element levels
|
2013-07-21 17:00:12 +02:00
|
|
|
if (level > 8)
|
|
|
|
return linenr;
|
|
|
|
if (!isalpha(xmldata[pos]))
|
|
|
|
return linenr;
|
2013-07-29 10:21:10 +02:00
|
|
|
// add element name to elementNames so it can be compared later against the end element
|
2013-07-21 17:00:12 +02:00
|
|
|
memset(elementNames[level], 0, 64);
|
|
|
|
for (int i = 0; i < 64; i++) {
|
|
|
|
if ((xmldata[pos+i]>='a' && xmldata[pos+i]<='z') || xmldata[pos+i] == '-')
|
|
|
|
elementNames[level][i] = xmldata[pos+i];
|
|
|
|
else {
|
|
|
|
pos += i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 10:21:10 +02:00
|
|
|
if (!strchr("/> \r\n", xmldata[pos]))
|
2013-07-21 17:00:12 +02:00
|
|
|
return linenr;
|
|
|
|
|
|
|
|
level++;
|
|
|
|
|
2013-07-29 10:21:10 +02:00
|
|
|
// validate all attributes
|
|
|
|
while (xmldata[pos] != '/' && xmldata[pos] != '>') {
|
|
|
|
// validate one attribute
|
2013-07-21 17:00:12 +02:00
|
|
|
skipspaces(xmldata,&pos,&linenr);
|
|
|
|
if ((xmldata[pos] >= 'a') && xmldata[pos] <= 'z') {
|
2013-07-29 10:21:10 +02:00
|
|
|
// attribute name
|
2013-07-21 17:00:12 +02:00
|
|
|
while (((xmldata[pos] >= 'a') && xmldata[pos] <= 'z') || xmldata[pos] == '-')
|
|
|
|
++pos;
|
|
|
|
if (xmldata[pos++] != '=')
|
|
|
|
return linenr;
|
|
|
|
if (xmldata[pos++] != '\"')
|
|
|
|
return linenr;
|
2013-07-29 10:21:10 +02:00
|
|
|
// attribute value
|
|
|
|
while (isalnum(xmldata[pos]) || strchr(":-.,_",xmldata[pos]))
|
2013-07-21 17:00:12 +02:00
|
|
|
++pos;
|
|
|
|
if (xmldata[pos++] != '\"')
|
|
|
|
return linenr;
|
|
|
|
if (!strchr("> \r\n", xmldata[pos]))
|
|
|
|
return linenr;
|
2013-07-29 10:21:10 +02:00
|
|
|
} else if (xmldata[pos] != '/' && xmldata[pos] != '>') {
|
2013-07-21 17:00:12 +02:00
|
|
|
return linenr;
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 10:21:10 +02:00
|
|
|
|
|
|
|
// no end element.. <abc/>
|
|
|
|
if (xmldata[pos] == '/') {
|
|
|
|
--level;
|
|
|
|
++pos;
|
|
|
|
skipspaces(xmldata,&pos,&linenr);
|
|
|
|
if (xmldata[pos] != '>')
|
|
|
|
return linenr;
|
|
|
|
}
|
2013-07-21 17:00:12 +02:00
|
|
|
}
|
|
|
|
} else if (xmldata[pos] == '>') {
|
|
|
|
return linenr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level != 0)
|
|
|
|
return linenr;
|
2013-07-22 07:26:15 +02:00
|
|
|
|
2013-07-21 17:00:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|