2017-08-12 14:56:14 -04:00
|
|
|
/*
|
|
|
|
__ __ _
|
|
|
|
___\ \/ /_ __ __ _| |_
|
|
|
|
/ _ \\ /| '_ \ / _` | __|
|
|
|
|
| __// \| |_) | (_| | |_
|
|
|
|
\___/_/\_\ .__/ \__,_|\__|
|
|
|
|
|_| XML parser
|
|
|
|
|
|
|
|
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
|
2021-05-01 19:49:02 -04:00
|
|
|
Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
|
|
|
|
Copyright (c) 2001-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
|
|
|
|
Copyright (c) 2004-2009 Karl Waclawek <karl@waclawek.net>
|
|
|
|
Copyright (c) 2005-2007 Steven Solie <ssolie@users.sourceforge.net>
|
|
|
|
Copyright (c) 2016-2021 Sebastian Pipping <sebastian@pipping.org>
|
|
|
|
Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
|
|
|
|
Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
|
|
|
|
Copyright (c) 2020 Joe Orton <jorton@redhat.com>
|
|
|
|
Copyright (c) 2020 Kleber Tarcísio <klebertarcisio@yahoo.com.br>
|
|
|
|
Copyright (c) 2021 Tim Bray <tbray@textuality.com>
|
2017-08-12 14:56:14 -04:00
|
|
|
Licensed under the MIT license:
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
persons to whom the Software is furnished to do so, subject to the
|
|
|
|
following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2000-04-21 00:20:31 -04:00
|
|
|
*/
|
1998-04-05 11:11:29 -04:00
|
|
|
|
2021-04-18 14:17:43 -04:00
|
|
|
#include <expat_config.h>
|
|
|
|
|
2017-08-28 15:38:44 -04:00
|
|
|
#include <assert.h>
|
1997-11-11 00:52:10 -05:00
|
|
|
#include <stdio.h>
|
1998-02-04 00:25:27 -05:00
|
|
|
#include <stdlib.h>
|
1998-02-04 02:20:46 -05:00
|
|
|
#include <stddef.h>
|
1997-12-10 02:44:19 -05:00
|
|
|
#include <string.h>
|
2021-04-17 11:26:17 -04:00
|
|
|
#include <math.h> /* for isnan */
|
|
|
|
#include <errno.h>
|
1998-02-04 00:25:27 -05:00
|
|
|
|
2000-09-18 12:26:23 -04:00
|
|
|
#include "expat.h"
|
1998-11-25 03:31:50 -05:00
|
|
|
#include "codepage.h"
|
2019-08-03 14:34:54 -04:00
|
|
|
#include "internal.h" /* for UNUSED_P only */
|
1998-11-25 03:31:50 -05:00
|
|
|
#include "xmlfile.h"
|
|
|
|
#include "xmltchar.h"
|
1998-02-04 00:25:27 -05:00
|
|
|
|
1997-12-13 04:11:15 -05:00
|
|
|
#ifdef _MSC_VER
|
2019-08-03 14:34:54 -04:00
|
|
|
# include <crtdbg.h>
|
1997-12-13 04:11:15 -05:00
|
|
|
#endif
|
1997-11-11 00:52:10 -05:00
|
|
|
|
2017-08-25 12:26:57 -04:00
|
|
|
#ifdef XML_UNICODE
|
2019-08-03 14:34:54 -04:00
|
|
|
# include <wchar.h>
|
2017-08-25 12:26:57 -04:00
|
|
|
#endif
|
|
|
|
|
2021-04-19 12:19:44 -04:00
|
|
|
enum ExitCode {
|
|
|
|
XMLWF_EXIT_SUCCESS = 0,
|
|
|
|
XMLWF_EXIT_INTERNAL_ERROR = 1,
|
|
|
|
XMLWF_EXIT_NOT_WELLFORMED = 2,
|
|
|
|
XMLWF_EXIT_OUTPUT_ERROR = 3,
|
|
|
|
XMLWF_EXIT_USAGE_ERROR = 4,
|
|
|
|
};
|
|
|
|
|
2017-08-21 12:21:22 -04:00
|
|
|
/* Structures for handler user data */
|
|
|
|
typedef struct NotationList {
|
|
|
|
struct NotationList *next;
|
|
|
|
const XML_Char *notationName;
|
|
|
|
const XML_Char *systemId;
|
|
|
|
const XML_Char *publicId;
|
|
|
|
} NotationList;
|
|
|
|
|
|
|
|
typedef struct xmlwfUserData {
|
|
|
|
FILE *fp;
|
|
|
|
NotationList *notationListHead;
|
|
|
|
const XML_Char *currentDoctypeName;
|
|
|
|
} XmlwfUserData;
|
|
|
|
|
2000-04-21 00:20:31 -04:00
|
|
|
/* This ensures proper sorting. */
|
1999-07-09 08:17:40 -04:00
|
|
|
|
|
|
|
#define NSSEP T('\001')
|
1998-08-26 05:10:50 -04:00
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
characterData(void *userData, const XML_Char *s, int len) {
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-02-04 00:25:27 -05:00
|
|
|
for (; len > 0; --len, ++s) {
|
|
|
|
switch (*s) {
|
1998-05-31 07:53:28 -04:00
|
|
|
case T('&'):
|
|
|
|
fputts(T("&"), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
1998-05-31 07:53:28 -04:00
|
|
|
case T('<'):
|
|
|
|
fputts(T("<"), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
1998-05-31 07:53:28 -04:00
|
|
|
case T('>'):
|
|
|
|
fputts(T(">"), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
1999-07-09 08:17:40 -04:00
|
|
|
#ifdef W3C14N
|
|
|
|
case 13:
|
|
|
|
fputts(T("
"), fp);
|
|
|
|
break;
|
|
|
|
#else
|
1998-05-31 07:53:28 -04:00
|
|
|
case T('"'):
|
|
|
|
fputts(T("""), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
1998-02-09 20:44:29 -05:00
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
case 13:
|
1998-05-31 07:53:28 -04:00
|
|
|
ftprintf(fp, T("&#%d;"), *s);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
1999-07-09 08:17:40 -04:00
|
|
|
#endif
|
1998-02-04 00:25:27 -05:00
|
|
|
default:
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(*s, fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
attributeValue(FILE *fp, const XML_Char *s) {
|
1999-07-09 08:17:40 -04:00
|
|
|
puttc(T('='), fp);
|
|
|
|
puttc(T('"'), fp);
|
2017-08-28 15:38:44 -04:00
|
|
|
assert(s);
|
1999-07-09 08:17:40 -04:00
|
|
|
for (;;) {
|
|
|
|
switch (*s) {
|
|
|
|
case 0:
|
|
|
|
case NSSEP:
|
|
|
|
puttc(T('"'), fp);
|
|
|
|
return;
|
|
|
|
case T('&'):
|
|
|
|
fputts(T("&"), fp);
|
|
|
|
break;
|
|
|
|
case T('<'):
|
|
|
|
fputts(T("<"), fp);
|
|
|
|
break;
|
|
|
|
case T('"'):
|
|
|
|
fputts(T("""), fp);
|
|
|
|
break;
|
|
|
|
#ifdef W3C14N
|
|
|
|
case 9:
|
|
|
|
fputts(T("	"), fp);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
fputts(T("
"), fp);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
fputts(T("
"), fp);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
case T('>'):
|
|
|
|
fputts(T(">"), fp);
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
case 13:
|
|
|
|
ftprintf(fp, T("&#%d;"), *s);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
puttc(*s, fp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-21 00:20:31 -04:00
|
|
|
/* Lexicographically comparing UTF-8 encoded attribute values,
|
1998-02-04 00:25:27 -05:00
|
|
|
is equivalent to lexicographically comparing based on the character number. */
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
2019-08-03 14:34:54 -04:00
|
|
|
attcmp(const void *att1, const void *att2) {
|
1998-05-31 07:53:28 -04:00
|
|
|
return tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2);
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
|
1998-02-04 00:25:27 -05:00
|
|
|
int nAtts;
|
1998-05-31 07:53:28 -04:00
|
|
|
const XML_Char **p;
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(T('<'), fp);
|
|
|
|
fputts(name, fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
|
|
|
|
p = atts;
|
|
|
|
while (*p)
|
|
|
|
++p;
|
2006-04-14 13:53:03 -04:00
|
|
|
nAtts = (int)((p - atts) >> 1);
|
1998-02-04 00:25:27 -05:00
|
|
|
if (nAtts > 1)
|
1998-05-31 07:53:28 -04:00
|
|
|
qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, attcmp);
|
1998-02-04 00:25:27 -05:00
|
|
|
while (*atts) {
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(T(' '), fp);
|
|
|
|
fputts(*atts++, fp);
|
1999-07-09 08:17:40 -04:00
|
|
|
attributeValue(fp, *atts);
|
1998-02-04 00:25:27 -05:00
|
|
|
atts++;
|
|
|
|
}
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(T('>'), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
endElement(void *userData, const XML_Char *name) {
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(T('<'), fp);
|
|
|
|
puttc(T('/'), fp);
|
|
|
|
fputts(name, fp);
|
|
|
|
puttc(T('>'), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static int
|
2019-08-03 14:34:54 -04:00
|
|
|
nsattcmp(const void *p1, const void *p2) {
|
1999-07-09 08:17:40 -04:00
|
|
|
const XML_Char *att1 = *(const XML_Char **)p1;
|
|
|
|
const XML_Char *att2 = *(const XML_Char **)p2;
|
|
|
|
int sep1 = (tcsrchr(att1, NSSEP) != 0);
|
|
|
|
int sep2 = (tcsrchr(att1, NSSEP) != 0);
|
|
|
|
if (sep1 != sep2)
|
|
|
|
return sep1 - sep2;
|
|
|
|
return tcscmp(att1, att2);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
startElementNS(void *userData, const XML_Char *name, const XML_Char **atts) {
|
1998-08-26 05:10:50 -04:00
|
|
|
int nAtts;
|
|
|
|
int nsi;
|
|
|
|
const XML_Char **p;
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-08-26 05:10:50 -04:00
|
|
|
const XML_Char *sep;
|
|
|
|
puttc(T('<'), fp);
|
|
|
|
|
|
|
|
sep = tcsrchr(name, NSSEP);
|
|
|
|
if (sep) {
|
1999-07-09 08:17:40 -04:00
|
|
|
fputts(T("n1:"), fp);
|
1998-08-26 05:10:50 -04:00
|
|
|
fputts(sep + 1, fp);
|
1999-07-09 08:17:40 -04:00
|
|
|
fputts(T(" xmlns:n1"), fp);
|
|
|
|
attributeValue(fp, name);
|
|
|
|
nsi = 2;
|
2019-08-03 14:34:54 -04:00
|
|
|
} else {
|
1998-08-26 05:10:50 -04:00
|
|
|
fputts(name, fp);
|
1999-07-09 08:17:40 -04:00
|
|
|
nsi = 1;
|
1998-08-26 05:10:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p = atts;
|
|
|
|
while (*p)
|
|
|
|
++p;
|
2006-04-14 13:53:03 -04:00
|
|
|
nAtts = (int)((p - atts) >> 1);
|
1998-08-26 05:10:50 -04:00
|
|
|
if (nAtts > 1)
|
1999-07-09 08:17:40 -04:00
|
|
|
qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, nsattcmp);
|
1998-08-26 05:10:50 -04:00
|
|
|
while (*atts) {
|
|
|
|
name = *atts++;
|
|
|
|
sep = tcsrchr(name, NSSEP);
|
1999-07-09 08:17:40 -04:00
|
|
|
puttc(T(' '), fp);
|
1998-08-26 05:10:50 -04:00
|
|
|
if (sep) {
|
1999-07-09 08:17:40 -04:00
|
|
|
ftprintf(fp, T("n%d:"), nsi);
|
|
|
|
fputts(sep + 1, fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else
|
1999-07-09 08:17:40 -04:00
|
|
|
fputts(name, fp);
|
|
|
|
attributeValue(fp, *atts);
|
|
|
|
if (sep) {
|
|
|
|
ftprintf(fp, T(" xmlns:n%d"), nsi++);
|
|
|
|
attributeValue(fp, name);
|
|
|
|
}
|
1998-08-26 05:10:50 -04:00
|
|
|
atts++;
|
|
|
|
}
|
|
|
|
puttc(T('>'), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
endElementNS(void *userData, const XML_Char *name) {
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-08-26 05:10:50 -04:00
|
|
|
const XML_Char *sep;
|
|
|
|
puttc(T('<'), fp);
|
|
|
|
puttc(T('/'), fp);
|
|
|
|
sep = tcsrchr(name, NSSEP);
|
|
|
|
if (sep) {
|
1999-07-09 08:17:40 -04:00
|
|
|
fputts(T("n1:"), fp);
|
1998-08-26 05:10:50 -04:00
|
|
|
fputts(sep + 1, fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else
|
1998-08-26 05:10:50 -04:00
|
|
|
fputts(name, fp);
|
|
|
|
puttc(T('>'), fp);
|
|
|
|
}
|
|
|
|
|
1999-07-09 08:17:40 -04:00
|
|
|
#ifndef W3C14N
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2002-04-19 17:41:54 -04:00
|
|
|
processingInstruction(void *userData, const XML_Char *target,
|
2019-08-03 14:34:54 -04:00
|
|
|
const XML_Char *data) {
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)userData)->fp;
|
1998-05-31 07:53:28 -04:00
|
|
|
puttc(T('<'), fp);
|
|
|
|
puttc(T('?'), fp);
|
|
|
|
fputts(target, fp);
|
|
|
|
puttc(T(' '), fp);
|
|
|
|
fputts(data, fp);
|
|
|
|
puttc(T('?'), fp);
|
|
|
|
puttc(T('>'), fp);
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
static XML_Char *
|
|
|
|
xcsdup(const XML_Char *s) {
|
2017-07-05 09:18:00 -04:00
|
|
|
XML_Char *result;
|
2017-08-21 12:53:10 -04:00
|
|
|
int count = 0;
|
|
|
|
int numBytes;
|
2017-07-05 09:18:00 -04:00
|
|
|
|
|
|
|
/* Get the length of the string, including terminator */
|
2017-08-21 12:53:10 -04:00
|
|
|
while (s[count++] != 0) {
|
2017-07-05 09:18:00 -04:00
|
|
|
/* Do nothing */
|
|
|
|
}
|
2017-08-21 12:53:10 -04:00
|
|
|
numBytes = count * sizeof(XML_Char);
|
|
|
|
result = malloc(numBytes);
|
2017-07-05 09:18:00 -04:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
2017-08-21 12:53:10 -04:00
|
|
|
memcpy(result, s, numBytes);
|
2017-07-05 09:18:00 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
startDoctypeDecl(void *userData, const XML_Char *doctypeName,
|
2019-08-16 12:20:00 -04:00
|
|
|
const XML_Char *sysid, const XML_Char *publid,
|
|
|
|
int has_internal_subset) {
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)userData;
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(sysid);
|
|
|
|
UNUSED_P(publid);
|
|
|
|
UNUSED_P(has_internal_subset);
|
2017-08-21 12:53:10 -04:00
|
|
|
data->currentDoctypeName = xcsdup(doctypeName);
|
2017-07-05 09:18:00 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 10:56:10 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
freeNotations(XmlwfUserData *data) {
|
2017-08-21 12:21:22 -04:00
|
|
|
NotationList *notationListHead = data->notationListHead;
|
|
|
|
|
2017-07-05 10:56:10 -04:00
|
|
|
while (notationListHead != NULL) {
|
|
|
|
NotationList *next = notationListHead->next;
|
|
|
|
free((void *)notationListHead->notationName);
|
|
|
|
free((void *)notationListHead->systemId);
|
|
|
|
free((void *)notationListHead->publicId);
|
|
|
|
free(notationListHead);
|
|
|
|
notationListHead = next;
|
|
|
|
}
|
2017-08-21 12:21:22 -04:00
|
|
|
data->notationListHead = NULL;
|
2017-07-05 10:56:10 -04:00
|
|
|
}
|
|
|
|
|
2021-03-16 23:40:26 -04:00
|
|
|
static void
|
|
|
|
cleanupUserData(XmlwfUserData *userData) {
|
|
|
|
free((void *)userData->currentDoctypeName);
|
|
|
|
userData->currentDoctypeName = NULL;
|
|
|
|
freeNotations(userData);
|
|
|
|
}
|
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
static int
|
|
|
|
xcscmp(const XML_Char *xs, const XML_Char *xt) {
|
2017-07-05 10:56:10 -04:00
|
|
|
while (*xs != 0 && *xt != 0) {
|
|
|
|
if (*xs < *xt)
|
|
|
|
return -1;
|
|
|
|
if (*xs > *xt)
|
|
|
|
return 1;
|
|
|
|
xs++;
|
|
|
|
xt++;
|
|
|
|
}
|
|
|
|
if (*xs < *xt)
|
|
|
|
return -1;
|
|
|
|
if (*xs > *xt)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-08-03 14:34:54 -04:00
|
|
|
notationCmp(const void *a, const void *b) {
|
|
|
|
const NotationList *const n1 = *(NotationList **)a;
|
|
|
|
const NotationList *const n2 = *(NotationList **)b;
|
2017-07-05 10:56:10 -04:00
|
|
|
|
|
|
|
return xcscmp(n1->notationName, n2->notationName);
|
|
|
|
}
|
|
|
|
|
2017-07-05 09:18:00 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
endDoctypeDecl(void *userData) {
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)userData;
|
2017-07-05 10:56:10 -04:00
|
|
|
NotationList **notations;
|
|
|
|
int notationCount = 0;
|
|
|
|
NotationList *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* How many notations do we have? */
|
2017-08-21 12:21:22 -04:00
|
|
|
for (p = data->notationListHead; p != NULL; p = p->next)
|
2017-07-05 10:56:10 -04:00
|
|
|
notationCount++;
|
2017-08-22 06:54:52 -04:00
|
|
|
if (notationCount == 0) {
|
|
|
|
/* Nothing to report */
|
|
|
|
free((void *)data->currentDoctypeName);
|
|
|
|
data->currentDoctypeName = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2017-07-05 10:56:10 -04:00
|
|
|
|
|
|
|
notations = malloc(notationCount * sizeof(NotationList *));
|
|
|
|
if (notations == NULL) {
|
|
|
|
fprintf(stderr, "Unable to sort notations");
|
2017-08-21 12:21:22 -04:00
|
|
|
freeNotations(data);
|
2017-07-05 10:56:10 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-07-05 09:18:00 -04:00
|
|
|
|
2019-08-03 14:34:54 -04:00
|
|
|
for (p = data->notationListHead, i = 0; i < notationCount; p = p->next, i++) {
|
2017-07-05 10:56:10 -04:00
|
|
|
notations[i] = p;
|
|
|
|
}
|
|
|
|
qsort(notations, notationCount, sizeof(NotationList *), notationCmp);
|
|
|
|
|
|
|
|
/* Output the DOCTYPE header */
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T("<!DOCTYPE "), data->fp);
|
|
|
|
fputts(data->currentDoctypeName, data->fp);
|
|
|
|
fputts(T(" [\n"), data->fp);
|
2017-07-05 10:56:10 -04:00
|
|
|
|
|
|
|
/* Now the NOTATIONs */
|
|
|
|
for (i = 0; i < notationCount; i++) {
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T("<!NOTATION "), data->fp);
|
|
|
|
fputts(notations[i]->notationName, data->fp);
|
2017-07-05 10:56:10 -04:00
|
|
|
if (notations[i]->publicId != NULL) {
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T(" PUBLIC '"), data->fp);
|
|
|
|
fputts(notations[i]->publicId, data->fp);
|
|
|
|
puttc(T('\''), data->fp);
|
2017-07-05 10:56:10 -04:00
|
|
|
if (notations[i]->systemId != NULL) {
|
2017-08-21 12:21:22 -04:00
|
|
|
puttc(T(' '), data->fp);
|
|
|
|
puttc(T('\''), data->fp);
|
|
|
|
fputts(notations[i]->systemId, data->fp);
|
|
|
|
puttc(T('\''), data->fp);
|
2017-07-05 09:18:00 -04:00
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
} else if (notations[i]->systemId != NULL) {
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T(" SYSTEM '"), data->fp);
|
|
|
|
fputts(notations[i]->systemId, data->fp);
|
|
|
|
puttc(T('\''), data->fp);
|
2017-07-05 10:56:10 -04:00
|
|
|
}
|
2017-08-21 12:21:22 -04:00
|
|
|
puttc(T('>'), data->fp);
|
|
|
|
puttc(T('\n'), data->fp);
|
2017-07-05 09:18:00 -04:00
|
|
|
}
|
2017-07-05 10:56:10 -04:00
|
|
|
|
|
|
|
/* Finally end the DOCTYPE */
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T("]>\n"), data->fp);
|
2017-07-05 10:56:10 -04:00
|
|
|
|
|
|
|
free(notations);
|
2017-08-21 12:21:22 -04:00
|
|
|
freeNotations(data);
|
|
|
|
free((void *)data->currentDoctypeName);
|
|
|
|
data->currentDoctypeName = NULL;
|
2017-07-05 09:18:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
notationDecl(void *userData, const XML_Char *notationName, const XML_Char *base,
|
|
|
|
const XML_Char *systemId, const XML_Char *publicId) {
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)userData;
|
2017-07-05 09:18:00 -04:00
|
|
|
NotationList *entry = malloc(sizeof(NotationList));
|
2017-08-21 13:16:41 -04:00
|
|
|
const char *errorMessage = "Unable to store NOTATION for output\n";
|
2017-07-05 09:18:00 -04:00
|
|
|
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(base);
|
2017-07-05 09:18:00 -04:00
|
|
|
if (entry == NULL) {
|
2017-08-21 13:16:41 -04:00
|
|
|
fputs(errorMessage, stderr);
|
2017-07-05 09:18:00 -04:00
|
|
|
return; /* Nothing we can really do about this */
|
|
|
|
}
|
2017-08-21 12:53:10 -04:00
|
|
|
entry->notationName = xcsdup(notationName);
|
2017-07-05 09:18:00 -04:00
|
|
|
if (entry->notationName == NULL) {
|
2017-08-21 13:16:41 -04:00
|
|
|
fputs(errorMessage, stderr);
|
2017-07-05 09:18:00 -04:00
|
|
|
free(entry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (systemId != NULL) {
|
2017-08-21 12:53:10 -04:00
|
|
|
entry->systemId = xcsdup(systemId);
|
2017-07-05 09:18:00 -04:00
|
|
|
if (entry->systemId == NULL) {
|
2017-08-21 13:16:41 -04:00
|
|
|
fputs(errorMessage, stderr);
|
2017-07-05 09:18:00 -04:00
|
|
|
free((void *)entry->notationName);
|
|
|
|
free(entry);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
} else {
|
2017-07-05 09:18:00 -04:00
|
|
|
entry->systemId = NULL;
|
|
|
|
}
|
|
|
|
if (publicId != NULL) {
|
2017-08-21 12:53:10 -04:00
|
|
|
entry->publicId = xcsdup(publicId);
|
2017-07-05 09:18:00 -04:00
|
|
|
if (entry->publicId == NULL) {
|
2017-08-21 13:16:41 -04:00
|
|
|
fputs(errorMessage, stderr);
|
2017-07-05 09:18:00 -04:00
|
|
|
free((void *)entry->systemId); /* Safe if it's NULL */
|
|
|
|
free((void *)entry->notationName);
|
|
|
|
free(entry);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
} else {
|
2017-08-02 11:09:13 -04:00
|
|
|
entry->publicId = NULL;
|
|
|
|
}
|
2017-07-05 09:18:00 -04:00
|
|
|
|
2017-08-21 12:21:22 -04:00
|
|
|
entry->next = data->notationListHead;
|
|
|
|
data->notationListHead = entry;
|
2017-07-05 09:18:00 -04:00
|
|
|
}
|
|
|
|
|
1999-07-09 08:17:40 -04:00
|
|
|
#endif /* not W3C14N */
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
defaultCharacterData(void *userData, const XML_Char *s, int len) {
|
|
|
|
UNUSED_P(s);
|
|
|
|
UNUSED_P(len);
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_DefaultCurrent((XML_Parser)userData);
|
1998-06-21 02:53:13 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
defaultStartElement(void *userData, const XML_Char *name,
|
|
|
|
const XML_Char **atts) {
|
|
|
|
UNUSED_P(name);
|
|
|
|
UNUSED_P(atts);
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_DefaultCurrent((XML_Parser)userData);
|
1998-06-21 02:53:13 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
defaultEndElement(void *userData, const XML_Char *name) {
|
|
|
|
UNUSED_P(name);
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_DefaultCurrent((XML_Parser)userData);
|
1998-06-21 02:53:13 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
defaultProcessingInstruction(void *userData, const XML_Char *target,
|
|
|
|
const XML_Char *data) {
|
|
|
|
UNUSED_P(target);
|
|
|
|
UNUSED_P(data);
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_DefaultCurrent((XML_Parser)userData);
|
1998-06-21 02:53:13 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
nopCharacterData(void *userData, const XML_Char *s, int len) {
|
|
|
|
UNUSED_P(userData);
|
|
|
|
UNUSED_P(s);
|
|
|
|
UNUSED_P(len);
|
1998-11-24 10:24:59 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
nopStartElement(void *userData, const XML_Char *name, const XML_Char **atts) {
|
|
|
|
UNUSED_P(userData);
|
|
|
|
UNUSED_P(name);
|
|
|
|
UNUSED_P(atts);
|
1998-11-24 10:24:59 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
nopEndElement(void *userData, const XML_Char *name) {
|
|
|
|
UNUSED_P(userData);
|
|
|
|
UNUSED_P(name);
|
1998-11-24 10:24:59 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
nopProcessingInstruction(void *userData, const XML_Char *target,
|
|
|
|
const XML_Char *data) {
|
|
|
|
UNUSED_P(userData);
|
|
|
|
UNUSED_P(target);
|
|
|
|
UNUSED_P(data);
|
1998-11-24 10:24:59 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
markup(void *userData, const XML_Char *s, int len) {
|
|
|
|
FILE *fp = ((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp;
|
1998-06-17 06:04:31 -04:00
|
|
|
for (; len > 0; --len, ++s)
|
1998-06-21 02:53:13 -04:00
|
|
|
puttc(*s, fp);
|
1998-06-17 06:04:31 -04:00
|
|
|
}
|
1998-06-01 00:33:10 -04:00
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
metaLocation(XML_Parser parser) {
|
1998-06-03 06:16:20 -04:00
|
|
|
const XML_Char *uri = XML_GetBase(parser);
|
2017-08-21 12:21:22 -04:00
|
|
|
FILE *fp = ((XmlwfUserData *)XML_GetUserData(parser))->fp;
|
1998-06-03 06:16:20 -04:00
|
|
|
if (uri)
|
2017-08-21 12:21:22 -04:00
|
|
|
ftprintf(fp, T(" uri=\"%s\""), uri);
|
|
|
|
ftprintf(fp,
|
2019-08-03 14:34:54 -04:00
|
|
|
T(" byte=\"%") T(XML_FMT_INT_MOD) T("d\"") T(" nbytes=\"%d\"")
|
|
|
|
T(" line=\"%") T(XML_FMT_INT_MOD) T("u\"") T(" col=\"%")
|
|
|
|
T(XML_FMT_INT_MOD) T("u\""),
|
|
|
|
XML_GetCurrentByteIndex(parser), XML_GetCurrentByteCount(parser),
|
2002-07-01 11:13:02 -04:00
|
|
|
XML_GetCurrentLineNumber(parser),
|
|
|
|
XML_GetCurrentColumnNumber(parser));
|
1998-06-01 00:33:10 -04:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
metaStartDocument(void *userData) {
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T("<document>\n"),
|
2019-08-03 14:34:54 -04:00
|
|
|
((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp);
|
1998-11-25 04:26:02 -05:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
metaEndDocument(void *userData) {
|
2017-08-21 12:21:22 -04:00
|
|
|
fputts(T("</document>\n"),
|
2019-08-03 14:34:54 -04:00
|
|
|
((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp);
|
1998-11-25 04:26:02 -05:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaStartElement(void *userData, const XML_Char *name, const XML_Char **atts) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1999-03-25 22:53:05 -05:00
|
|
|
const XML_Char **specifiedAttsEnd
|
2019-08-03 14:34:54 -04:00
|
|
|
= atts + XML_GetSpecifiedAttributeCount(parser);
|
2000-04-14 06:53:33 -04:00
|
|
|
const XML_Char **idAttPtr;
|
|
|
|
int idAttIndex = XML_GetIdAttributeIndex(parser);
|
|
|
|
if (idAttIndex < 0)
|
|
|
|
idAttPtr = 0;
|
|
|
|
else
|
|
|
|
idAttPtr = atts + idAttIndex;
|
2017-08-21 12:21:22 -04:00
|
|
|
|
1998-06-03 06:16:20 -04:00
|
|
|
ftprintf(fp, T("<starttag name=\"%s\""), name);
|
|
|
|
metaLocation(parser);
|
|
|
|
if (*atts) {
|
|
|
|
fputts(T(">\n"), fp);
|
|
|
|
do {
|
1998-06-03 06:55:52 -04:00
|
|
|
ftprintf(fp, T("<attribute name=\"%s\" value=\""), atts[0]);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, atts[1], (int)tcslen(atts[1]));
|
1999-03-25 22:53:05 -05:00
|
|
|
if (atts >= specifiedAttsEnd)
|
2002-07-01 11:13:02 -04:00
|
|
|
fputts(T("\" defaulted=\"yes\"/>\n"), fp);
|
2000-04-14 06:53:33 -04:00
|
|
|
else if (atts == idAttPtr)
|
2002-07-01 11:13:02 -04:00
|
|
|
fputts(T("\" id=\"yes\"/>\n"), fp);
|
1999-03-25 22:53:05 -05:00
|
|
|
else
|
2002-07-01 11:13:02 -04:00
|
|
|
fputts(T("\"/>\n"), fp);
|
1998-06-03 06:16:20 -04:00
|
|
|
} while (*(atts += 2));
|
|
|
|
fputts(T("</starttag>\n"), fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else
|
1998-06-03 06:16:20 -04:00
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaEndElement(void *userData, const XML_Char *name) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1998-06-03 06:16:20 -04:00
|
|
|
ftprintf(fp, T("<endtag name=\"%s\""), name);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
1998-06-01 00:33:10 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2002-08-27 01:49:52 -04:00
|
|
|
metaProcessingInstruction(void *userData, const XML_Char *target,
|
2019-08-03 14:34:54 -04:00
|
|
|
const XML_Char *data) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *usrData = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = usrData->fp;
|
1998-06-03 06:16:20 -04:00
|
|
|
ftprintf(fp, T("<pi target=\"%s\" data=\""), target);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(usrData, data, (int)tcslen(data));
|
1998-06-03 06:16:20 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
1998-06-01 00:33:10 -04:00
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaComment(void *userData, const XML_Char *data) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *usrData = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = usrData->fp;
|
1998-09-24 00:12:12 -04:00
|
|
|
fputts(T("<comment data=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(usrData, data, (int)tcslen(data));
|
1998-09-24 00:12:12 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaStartCdataSection(void *userData) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1998-11-24 00:42:17 -05:00
|
|
|
fputts(T("<startcdata"), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaEndCdataSection(void *userData) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1998-11-24 00:42:17 -05:00
|
|
|
fputts(T("<endcdata"), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaCharacterData(void *userData, const XML_Char *s, int len) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1998-06-03 06:16:20 -04:00
|
|
|
fputts(T("<chars str=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, s, len);
|
1998-06-03 06:16:20 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
1998-06-03 03:04:44 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaStartDoctypeDecl(void *userData, const XML_Char *doctypeName,
|
2019-08-16 12:20:00 -04:00
|
|
|
const XML_Char *sysid, const XML_Char *pubid,
|
|
|
|
int has_internal_subset) {
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(sysid);
|
|
|
|
UNUSED_P(pubid);
|
|
|
|
UNUSED_P(has_internal_subset);
|
1999-07-28 05:34:18 -04:00
|
|
|
ftprintf(fp, T("<startdoctype name=\"%s\""), doctypeName);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaEndDoctypeDecl(void *userData) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1999-07-28 05:34:18 -04:00
|
|
|
fputts(T("<enddoctype"), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaNotationDecl(void *userData, const XML_Char *notationName,
|
2019-08-16 12:20:00 -04:00
|
|
|
const XML_Char *base, const XML_Char *systemId,
|
2019-08-03 14:34:54 -04:00
|
|
|
const XML_Char *publicId) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(base);
|
1998-06-03 06:16:20 -04:00
|
|
|
ftprintf(fp, T("<notation name=\"%s\""), notationName);
|
|
|
|
if (publicId)
|
|
|
|
ftprintf(fp, T(" public=\"%s\""), publicId);
|
|
|
|
if (systemId) {
|
|
|
|
fputts(T(" system=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, systemId, (int)tcslen(systemId));
|
1998-06-03 06:16:20 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
}
|
1998-06-03 03:04:44 -04:00
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
metaEntityDecl(void *userData, const XML_Char *entityName, int is_param,
|
|
|
|
const XML_Char *value, int value_length, const XML_Char *base,
|
|
|
|
const XML_Char *systemId, const XML_Char *publicId,
|
|
|
|
const XML_Char *notationName) {
|
2019-08-03 14:34:54 -04:00
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
2000-05-12 01:56:27 -04:00
|
|
|
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(is_param);
|
|
|
|
UNUSED_P(base);
|
2000-09-28 15:47:35 -04:00
|
|
|
if (value) {
|
|
|
|
ftprintf(fp, T("<entity name=\"%s\""), entityName);
|
|
|
|
metaLocation(parser);
|
|
|
|
puttc(T('>'), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, value, value_length);
|
2000-09-28 15:47:35 -04:00
|
|
|
fputts(T("</entity/>\n"), fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else if (notationName) {
|
2000-09-28 15:47:35 -04:00
|
|
|
ftprintf(fp, T("<entity name=\"%s\""), entityName);
|
|
|
|
if (publicId)
|
|
|
|
ftprintf(fp, T(" public=\"%s\""), publicId);
|
|
|
|
fputts(T(" system=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, systemId, (int)tcslen(systemId));
|
2000-09-28 15:47:35 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
ftprintf(fp, T(" notation=\"%s\""), notationName);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else {
|
2000-09-28 15:47:35 -04:00
|
|
|
ftprintf(fp, T("<entity name=\"%s\""), entityName);
|
|
|
|
if (publicId)
|
|
|
|
ftprintf(fp, T(" public=\"%s\""), publicId);
|
|
|
|
fputts(T(" system=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, systemId, (int)tcslen(systemId));
|
2000-09-28 15:47:35 -04:00
|
|
|
puttc(T('"'), fp);
|
|
|
|
metaLocation(parser);
|
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
2000-05-12 01:56:27 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaStartNamespaceDecl(void *userData, const XML_Char *prefix,
|
|
|
|
const XML_Char *uri) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
1999-03-06 22:33:24 -05:00
|
|
|
fputts(T("<startns"), fp);
|
|
|
|
if (prefix)
|
|
|
|
ftprintf(fp, T(" prefix=\"%s\""), prefix);
|
|
|
|
if (uri) {
|
|
|
|
fputts(T(" ns=\""), fp);
|
2017-08-21 12:21:22 -04:00
|
|
|
characterData(data, uri, (int)tcslen(uri));
|
1999-03-06 22:33:24 -05:00
|
|
|
fputts(T("\"/>\n"), fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else
|
1999-03-06 22:33:24 -05:00
|
|
|
fputts(T("/>\n"), fp);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static void XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
metaEndNamespaceDecl(void *userData, const XML_Char *prefix) {
|
|
|
|
XML_Parser parser = (XML_Parser)userData;
|
2017-08-21 12:21:22 -04:00
|
|
|
XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
|
|
|
|
FILE *fp = data->fp;
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! prefix)
|
1999-03-06 22:33:24 -05:00
|
|
|
fputts(T("<endns/>\n"), fp);
|
|
|
|
else
|
|
|
|
ftprintf(fp, T("<endns prefix=\"%s\"/>\n"), prefix);
|
|
|
|
}
|
1998-05-10 06:18:15 -04:00
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static int XMLCALL
|
2019-08-03 14:34:54 -04:00
|
|
|
unknownEncodingConvert(void *data, const char *p) {
|
1998-06-02 04:57:14 -04:00
|
|
|
return codepageConvert(*(int *)data, p);
|
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static int XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
unknownEncoding(void *userData, const XML_Char *name, XML_Encoding *info) {
|
1998-06-01 08:12:35 -04:00
|
|
|
int cp;
|
|
|
|
static const XML_Char prefixL[] = T("windows-");
|
|
|
|
static const XML_Char prefixU[] = T("WINDOWS-");
|
|
|
|
int i;
|
|
|
|
|
2019-08-16 12:20:00 -04:00
|
|
|
UNUSED_P(userData);
|
1998-06-01 08:12:35 -04:00
|
|
|
for (i = 0; prefixU[i]; i++)
|
1998-06-02 04:57:14 -04:00
|
|
|
if (name[i] != prefixU[i] && name[i] != prefixL[i])
|
1998-06-01 08:12:35 -04:00
|
|
|
return 0;
|
2019-08-03 14:34:54 -04:00
|
|
|
|
1998-06-01 08:12:35 -04:00
|
|
|
cp = 0;
|
1998-06-02 04:57:14 -04:00
|
|
|
for (; name[i]; i++) {
|
1998-06-01 08:12:35 -04:00
|
|
|
static const XML_Char digits[] = T("0123456789");
|
1998-06-02 04:57:14 -04:00
|
|
|
const XML_Char *s = tcschr(digits, name[i]);
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! s)
|
1998-06-01 08:12:35 -04:00
|
|
|
return 0;
|
|
|
|
cp *= 10;
|
2006-04-14 13:53:03 -04:00
|
|
|
cp += (int)(s - digits);
|
1998-06-01 08:12:35 -04:00
|
|
|
if (cp >= 0x10000)
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! codepageMap(cp, info->map))
|
1998-06-02 04:57:14 -04:00
|
|
|
return 0;
|
|
|
|
info->convert = unknownEncodingConvert;
|
|
|
|
/* We could just cast the code page integer to a void *,
|
|
|
|
and avoid the use of release. */
|
|
|
|
info->release = free;
|
|
|
|
info->data = malloc(sizeof(int));
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! info->data)
|
1998-06-02 04:57:14 -04:00
|
|
|
return 0;
|
|
|
|
*(int *)info->data = cp;
|
|
|
|
return 1;
|
1998-06-01 08:12:35 -04:00
|
|
|
}
|
|
|
|
|
2003-10-15 12:25:44 -04:00
|
|
|
static int XMLCALL
|
2019-08-16 12:20:00 -04:00
|
|
|
notStandalone(void *userData) {
|
|
|
|
UNUSED_P(userData);
|
1999-04-19 22:52:43 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
showVersion(XML_Char *prog) {
|
2001-12-05 11:30:31 -05:00
|
|
|
XML_Char *s = prog;
|
|
|
|
XML_Char ch;
|
2002-08-29 00:54:04 -04:00
|
|
|
const XML_Feature *features = XML_GetFeatureList();
|
2001-12-05 11:30:31 -05:00
|
|
|
while ((ch = *s) != 0) {
|
2002-08-22 17:55:25 -04:00
|
|
|
if (ch == '/'
|
2017-05-25 09:06:28 -04:00
|
|
|
#if defined(_WIN32)
|
2002-08-22 17:55:25 -04:00
|
|
|
|| ch == '\\'
|
|
|
|
#endif
|
2019-08-03 14:34:54 -04:00
|
|
|
)
|
2001-12-05 11:30:31 -05:00
|
|
|
prog = s + 1;
|
|
|
|
++s;
|
|
|
|
}
|
2002-09-04 21:48:26 -04:00
|
|
|
ftprintf(stdout, T("%s using %s\n"), prog, XML_ExpatVersion());
|
|
|
|
if (features != NULL && features[0].feature != XML_FEATURE_END) {
|
2002-08-26 13:20:48 -04:00
|
|
|
int i = 1;
|
2002-09-04 21:48:26 -04:00
|
|
|
ftprintf(stdout, T("%s"), features[0].name);
|
|
|
|
if (features[0].value)
|
|
|
|
ftprintf(stdout, T("=%ld"), features[0].value);
|
2002-08-29 00:54:04 -04:00
|
|
|
while (features[i].feature != XML_FEATURE_END) {
|
|
|
|
ftprintf(stdout, T(", %s"), features[i].name);
|
2002-09-04 21:48:26 -04:00
|
|
|
if (features[i].value)
|
|
|
|
ftprintf(stdout, T("=%ld"), features[i].value);
|
2002-08-26 13:20:48 -04:00
|
|
|
++i;
|
|
|
|
}
|
2002-09-04 21:48:26 -04:00
|
|
|
ftprintf(stdout, T("\n"));
|
2002-08-26 13:20:48 -04:00
|
|
|
}
|
2001-12-05 11:30:31 -05:00
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
static void
|
2019-08-03 14:34:54 -04:00
|
|
|
usage(const XML_Char *prog, int rc) {
|
|
|
|
ftprintf(
|
|
|
|
stderr,
|
2019-09-04 17:23:29 -04:00
|
|
|
/* Generated with:
|
|
|
|
* $ xmlwf/xmlwf_helpgen.sh
|
2021-03-16 23:40:26 -04:00
|
|
|
* To update, change xmlwf/xmlwf_helpgen.py, then paste the output of
|
|
|
|
* xmlwf/xmlwf_helpgen.sh in here.
|
2019-09-04 17:23:29 -04:00
|
|
|
*/
|
|
|
|
/* clang-format off */
|
2021-04-25 11:14:54 -04:00
|
|
|
T("usage:\n")
|
|
|
|
T(" %s [OPTIONS] [FILE ...]\n")
|
|
|
|
T(" %s -h\n")
|
|
|
|
T(" %s -v\n")
|
2019-09-04 17:23:29 -04:00
|
|
|
T("\n")
|
|
|
|
T("xmlwf - Determines if an XML document is well-formed\n")
|
|
|
|
T("\n")
|
|
|
|
T("positional arguments:\n")
|
2019-11-26 10:17:44 -05:00
|
|
|
T(" FILE file to process (default: STDIN)\n")
|
2019-09-04 17:23:29 -04:00
|
|
|
T("\n")
|
|
|
|
T("input control arguments:\n")
|
|
|
|
T(" -s print an error if the document is not [s]tandalone\n")
|
|
|
|
T(" -n enable [n]amespace processing\n")
|
|
|
|
T(" -p enable processing external DTDs and [p]arameter entities\n")
|
|
|
|
T(" -x enable processing of e[x]ternal entities\n")
|
|
|
|
T(" -e ENCODING override any in-document [e]ncoding declaration\n")
|
|
|
|
T(" -w enable support for [W]indows code pages\n")
|
|
|
|
T(" -r disable memory-mapping and use normal file [r]ead IO calls instead\n")
|
2021-03-16 23:40:26 -04:00
|
|
|
T(" -k when processing multiple files, [k]eep processing after first file with error\n")
|
2019-09-04 17:23:29 -04:00
|
|
|
T("\n")
|
|
|
|
T("output control arguments:\n")
|
|
|
|
T(" -d DIRECTORY output [d]estination directory\n")
|
|
|
|
T(" -c write a [c]opy of input XML, not canonical XML\n")
|
|
|
|
T(" -m write [m]eta XML, not canonical XML\n")
|
|
|
|
T(" -t write no XML output for [t]iming of plain parsing\n")
|
|
|
|
T(" -N enable adding doctype and [n]otation declarations\n")
|
|
|
|
T("\n")
|
2021-04-17 11:26:17 -04:00
|
|
|
T("billion laughs attack protection:\n")
|
|
|
|
T(" NOTE: If you ever need to increase these values for non-attack payload, please file a bug report.\n")
|
|
|
|
T("\n")
|
|
|
|
T(" -a FACTOR set maximum tolerated [a]mplification factor (default: 100.0)\n")
|
|
|
|
T(" -b BYTES set number of output [b]ytes needed to activate (default: 8 MiB)\n")
|
|
|
|
T("\n")
|
2019-09-04 17:23:29 -04:00
|
|
|
T("info arguments:\n")
|
|
|
|
T(" -h show this [h]elp message and exit\n")
|
|
|
|
T(" -v show program's [v]ersion number and exit\n")
|
|
|
|
T("\n")
|
2021-03-16 23:40:26 -04:00
|
|
|
T("exit status:\n")
|
|
|
|
T(" 0 the input files are well-formed and the output (if requested) was written successfully\n")
|
|
|
|
T(" 1 could not allocate data structures, signals a serious problem with execution environment\n")
|
2021-03-19 16:51:16 -04:00
|
|
|
T(" 2 one or more input files were not well-formed\n")
|
2021-03-16 23:40:26 -04:00
|
|
|
T(" 3 could not create an output file\n")
|
2021-03-19 16:51:16 -04:00
|
|
|
T(" 4 command-line argument error\n")
|
2021-03-16 23:40:26 -04:00
|
|
|
T("\n")
|
2019-11-26 10:17:44 -05:00
|
|
|
T("xmlwf of libexpat is software libre, licensed under the MIT license.\n")
|
2019-09-04 17:23:29 -04:00
|
|
|
T("Please report bugs at https://github.com/libexpat/libexpat/issues. Thank you!\n")
|
|
|
|
, /* clang-format on */
|
2021-04-25 11:14:54 -04:00
|
|
|
prog, prog, prog);
|
2002-04-19 17:41:54 -04:00
|
|
|
exit(rc);
|
1997-11-11 00:52:10 -05:00
|
|
|
}
|
|
|
|
|
2017-09-09 12:42:41 -04:00
|
|
|
#if defined(__MINGW32__) && defined(XML_UNICODE)
|
|
|
|
/* Silence warning about missing prototype */
|
|
|
|
int wmain(int argc, XML_Char **argv);
|
|
|
|
#endif
|
|
|
|
|
2021-04-17 12:35:51 -04:00
|
|
|
#define XMLWF_SHIFT_ARG_INTO(constCharStarTarget, argc, argv, i, j) \
|
|
|
|
{ \
|
|
|
|
if (argv[i][j + 1] == T('\0')) { \
|
|
|
|
if (++i == argc) \
|
2021-04-19 12:19:44 -04:00
|
|
|
usage(argv[0], XMLWF_EXIT_USAGE_ERROR); \
|
2021-04-17 12:35:51 -04:00
|
|
|
constCharStarTarget = argv[i]; \
|
|
|
|
} else { \
|
|
|
|
constCharStarTarget = argv[i] + j + 1; \
|
|
|
|
} \
|
|
|
|
i++; \
|
|
|
|
j = 0; \
|
|
|
|
}
|
|
|
|
|
2002-04-19 17:41:54 -04:00
|
|
|
int
|
2019-08-03 14:34:54 -04:00
|
|
|
tmain(int argc, XML_Char **argv) {
|
1999-04-19 22:52:43 -04:00
|
|
|
int i, j;
|
2002-05-17 13:21:51 -04:00
|
|
|
const XML_Char *outputDir = NULL;
|
|
|
|
const XML_Char *encoding = NULL;
|
1998-11-25 03:31:50 -05:00
|
|
|
unsigned processFlags = XML_MAP_FILE;
|
1998-06-01 08:12:35 -04:00
|
|
|
int windowsCodePages = 0;
|
1998-06-17 06:04:31 -04:00
|
|
|
int outputType = 0;
|
1998-08-26 05:10:50 -04:00
|
|
|
int useNamespaces = 0;
|
1999-04-19 22:52:43 -04:00
|
|
|
int requireStandalone = 0;
|
2017-08-02 11:09:13 -04:00
|
|
|
int requiresNotations = 0;
|
2021-03-16 23:40:26 -04:00
|
|
|
int continueOnError = 0;
|
2021-04-17 11:26:17 -04:00
|
|
|
|
|
|
|
float attackMaximumAmplification = -1.0f; /* signaling "not set" */
|
|
|
|
unsigned long long attackThresholdBytes;
|
|
|
|
XML_Bool attackThresholdGiven = XML_FALSE;
|
|
|
|
|
2021-04-19 12:19:44 -04:00
|
|
|
int exitCode = XMLWF_EXIT_SUCCESS;
|
2019-08-03 14:34:54 -04:00
|
|
|
enum XML_ParamEntityParsing paramEntityParsing
|
|
|
|
= XML_PARAM_ENTITY_PARSING_NEVER;
|
2002-04-19 17:41:54 -04:00
|
|
|
int useStdin = 0;
|
2019-08-03 14:34:54 -04:00
|
|
|
XmlwfUserData userData = {NULL, NULL, NULL};
|
1997-12-10 02:44:19 -05:00
|
|
|
|
1997-12-13 04:11:15 -05:00
|
|
|
#ifdef _MSC_VER
|
2019-08-03 14:34:54 -04:00
|
|
|
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
|
1997-12-13 04:11:15 -05:00
|
|
|
#endif
|
1997-12-10 02:44:19 -05:00
|
|
|
|
1998-02-04 00:25:27 -05:00
|
|
|
i = 1;
|
1999-04-19 22:52:43 -04:00
|
|
|
j = 0;
|
|
|
|
while (i < argc) {
|
|
|
|
if (j == 0) {
|
|
|
|
if (argv[i][0] != T('-'))
|
2002-07-01 11:13:02 -04:00
|
|
|
break;
|
1999-04-19 22:52:43 -04:00
|
|
|
if (argv[i][1] == T('-') && argv[i][2] == T('\0')) {
|
2002-07-01 11:13:02 -04:00
|
|
|
i++;
|
|
|
|
break;
|
1999-04-19 22:52:43 -04:00
|
|
|
}
|
|
|
|
j++;
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
1999-04-19 22:52:43 -04:00
|
|
|
switch (argv[i][j]) {
|
|
|
|
case T('r'):
|
1998-11-25 03:31:50 -05:00
|
|
|
processFlags &= ~XML_MAP_FILE;
|
1998-02-04 00:25:27 -05:00
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('s'):
|
|
|
|
requireStandalone = 1;
|
|
|
|
j++;
|
|
|
|
break;
|
|
|
|
case T('n'):
|
1998-08-26 05:10:50 -04:00
|
|
|
useNamespaces = 1;
|
1998-08-22 18:40:45 -04:00
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
1999-06-26 07:29:49 -04:00
|
|
|
case T('p'):
|
|
|
|
paramEntityParsing = XML_PARAM_ENTITY_PARSING_ALWAYS;
|
|
|
|
/* fall through */
|
1999-04-19 22:52:43 -04:00
|
|
|
case T('x'):
|
1998-11-25 03:31:50 -05:00
|
|
|
processFlags |= XML_EXTERNAL_ENTITIES;
|
1998-05-10 06:18:15 -04:00
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('w'):
|
1998-06-01 08:12:35 -04:00
|
|
|
windowsCodePages = 1;
|
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('m'):
|
1998-06-17 06:04:31 -04:00
|
|
|
outputType = 'm';
|
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('c'):
|
1998-06-17 06:04:31 -04:00
|
|
|
outputType = 'c';
|
1998-08-26 05:10:50 -04:00
|
|
|
useNamespaces = 0;
|
1998-06-03 03:04:44 -04:00
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('t'):
|
1998-11-24 10:24:59 -05:00
|
|
|
outputType = 't';
|
|
|
|
j++;
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
2017-08-02 11:09:13 -04:00
|
|
|
case T('N'):
|
|
|
|
requiresNotations = 1;
|
|
|
|
j++;
|
|
|
|
break;
|
1999-04-19 22:52:43 -04:00
|
|
|
case T('d'):
|
2021-04-17 12:35:51 -04:00
|
|
|
XMLWF_SHIFT_ARG_INTO(outputDir, argc, argv, i, j);
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
|
|
|
case T('e'):
|
2021-04-17 12:35:51 -04:00
|
|
|
XMLWF_SHIFT_ARG_INTO(encoding, argc, argv, i, j);
|
1999-04-19 22:52:43 -04:00
|
|
|
break;
|
2002-04-19 17:41:54 -04:00
|
|
|
case T('h'):
|
2021-04-19 12:19:44 -04:00
|
|
|
usage(argv[0], XMLWF_EXIT_SUCCESS);
|
2002-04-19 17:41:54 -04:00
|
|
|
return 0;
|
2001-12-05 11:30:31 -05:00
|
|
|
case T('v'):
|
|
|
|
showVersion(argv[0]);
|
|
|
|
return 0;
|
2021-03-16 23:40:26 -04:00
|
|
|
case T('k'):
|
|
|
|
continueOnError = 1;
|
|
|
|
j++;
|
|
|
|
break;
|
2021-04-17 11:26:17 -04:00
|
|
|
case T('a'): {
|
|
|
|
const XML_Char *valueText = NULL;
|
|
|
|
XMLWF_SHIFT_ARG_INTO(valueText, argc, argv, i, j);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
XML_Char *afterValueText = (XML_Char *)valueText;
|
|
|
|
attackMaximumAmplification = tcstof(valueText, &afterValueText);
|
|
|
|
if ((errno != 0) || (afterValueText[0] != T('\0'))
|
|
|
|
|| isnan(attackMaximumAmplification)
|
|
|
|
|| (attackMaximumAmplification < 1.0f)) {
|
|
|
|
// This prevents tperror(..) from reporting misleading "[..]: Success"
|
|
|
|
errno = ERANGE;
|
|
|
|
tperror(T("invalid amplification limit") T(
|
|
|
|
" (needs a floating point number greater or equal than 1.0)"));
|
|
|
|
exit(XMLWF_EXIT_USAGE_ERROR);
|
|
|
|
}
|
|
|
|
#ifndef XML_DTD
|
|
|
|
ftprintf(stderr, T("Warning: Given amplification limit ignored") T(
|
|
|
|
", xmlwf has been compiled without DTD support.\n"));
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T('b'): {
|
|
|
|
const XML_Char *valueText = NULL;
|
|
|
|
XMLWF_SHIFT_ARG_INTO(valueText, argc, argv, i, j);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
XML_Char *afterValueText = (XML_Char *)valueText;
|
|
|
|
attackThresholdBytes = tcstoull(valueText, &afterValueText, 10);
|
|
|
|
if ((errno != 0) || (afterValueText[0] != T('\0'))) {
|
|
|
|
// This prevents tperror(..) from reporting misleading "[..]: Success"
|
|
|
|
errno = ERANGE;
|
|
|
|
tperror(T("invalid ignore threshold")
|
|
|
|
T(" (needs an integer from 0 to 2^64-1)"));
|
|
|
|
exit(XMLWF_EXIT_USAGE_ERROR);
|
|
|
|
}
|
|
|
|
attackThresholdGiven = XML_TRUE;
|
|
|
|
#ifndef XML_DTD
|
|
|
|
ftprintf(stderr, T("Warning: Given attack threshold ignored") T(
|
|
|
|
", xmlwf has been compiled without DTD support.\n"));
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
1999-04-19 22:52:43 -04:00
|
|
|
case T('\0'):
|
|
|
|
if (j > 1) {
|
2002-07-01 11:13:02 -04:00
|
|
|
i++;
|
|
|
|
j = 0;
|
|
|
|
break;
|
1999-04-19 22:52:43 -04:00
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
default:
|
2021-04-19 12:19:44 -04:00
|
|
|
usage(argv[0], XMLWF_EXIT_USAGE_ERROR);
|
1999-04-19 22:52:43 -04:00
|
|
|
}
|
1997-11-11 00:52:10 -05:00
|
|
|
}
|
2002-04-19 17:41:54 -04:00
|
|
|
if (i == argc) {
|
|
|
|
useStdin = 1;
|
|
|
|
processFlags &= ~XML_MAP_FILE;
|
|
|
|
i--;
|
|
|
|
}
|
1997-12-10 02:44:19 -05:00
|
|
|
for (; i < argc; i++) {
|
1998-05-31 07:53:28 -04:00
|
|
|
XML_Char *outName = 0;
|
1998-02-04 00:25:27 -05:00
|
|
|
int result;
|
1998-08-26 05:10:50 -04:00
|
|
|
XML_Parser parser;
|
|
|
|
if (useNamespaces)
|
|
|
|
parser = XML_ParserCreateNS(encoding, NSSEP);
|
|
|
|
else
|
|
|
|
parser = XML_ParserCreate(encoding);
|
2016-06-18 17:09:00 -04:00
|
|
|
|
|
|
|
if (! parser) {
|
2017-08-25 12:26:57 -04:00
|
|
|
tperror(T("Could not instantiate parser"));
|
2021-04-19 12:19:44 -04:00
|
|
|
exit(XMLWF_EXIT_INTERNAL_ERROR);
|
2016-06-18 17:09:00 -04:00
|
|
|
}
|
|
|
|
|
2021-04-17 11:26:17 -04:00
|
|
|
if (attackMaximumAmplification != -1.0f) {
|
|
|
|
#ifdef XML_DTD
|
|
|
|
XML_SetBillionLaughsAttackProtectionMaximumAmplification(
|
|
|
|
parser, attackMaximumAmplification);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (attackThresholdGiven) {
|
|
|
|
#ifdef XML_DTD
|
|
|
|
XML_SetBillionLaughsAttackProtectionActivationThreshold(
|
|
|
|
parser, attackThresholdBytes);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-04-19 22:52:43 -04:00
|
|
|
if (requireStandalone)
|
|
|
|
XML_SetNotStandaloneHandler(parser, notStandalone);
|
1999-06-26 07:29:49 -04:00
|
|
|
XML_SetParamEntityParsing(parser, paramEntityParsing);
|
1999-03-07 03:44:14 -05:00
|
|
|
if (outputType == 't') {
|
|
|
|
/* This is for doing timings; this gives a more realistic estimate of
|
2002-07-01 11:13:02 -04:00
|
|
|
the parsing time. */
|
1999-03-07 03:44:14 -05:00
|
|
|
outputDir = 0;
|
|
|
|
XML_SetElementHandler(parser, nopStartElement, nopEndElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, nopCharacterData);
|
|
|
|
XML_SetProcessingInstructionHandler(parser, nopProcessingInstruction);
|
2019-08-03 14:34:54 -04:00
|
|
|
} else if (outputDir) {
|
|
|
|
const XML_Char *delim = T("/");
|
2002-04-19 17:41:54 -04:00
|
|
|
const XML_Char *file = useStdin ? T("STDIN") : argv[i];
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! useStdin) {
|
2007-04-29 20:50:22 -04:00
|
|
|
/* Jump after last (back)slash */
|
2019-08-03 14:34:54 -04:00
|
|
|
const XML_Char *lastDelim = tcsrchr(file, delim[0]);
|
2007-04-29 20:50:22 -04:00
|
|
|
if (lastDelim)
|
|
|
|
file = lastDelim + 1;
|
2017-05-25 09:06:28 -04:00
|
|
|
#if defined(_WIN32)
|
2007-04-29 20:50:22 -04:00
|
|
|
else {
|
2019-08-03 14:34:54 -04:00
|
|
|
const XML_Char *winDelim = T("\\");
|
2007-04-29 20:50:22 -04:00
|
|
|
lastDelim = tcsrchr(file, winDelim[0]);
|
|
|
|
if (lastDelim) {
|
|
|
|
file = lastDelim + 1;
|
|
|
|
delim = winDelim;
|
|
|
|
}
|
|
|
|
}
|
1998-02-09 05:33:07 -05:00
|
|
|
#endif
|
2007-04-29 20:50:22 -04:00
|
|
|
}
|
2004-08-10 14:18:25 -04:00
|
|
|
outName = (XML_Char *)malloc((tcslen(outputDir) + tcslen(file) + 2)
|
2019-08-03 14:34:54 -04:00
|
|
|
* sizeof(XML_Char));
|
2020-06-22 10:15:12 -04:00
|
|
|
if (! outName) {
|
|
|
|
tperror(T("Could not allocate memory"));
|
2021-04-19 12:19:44 -04:00
|
|
|
exit(XMLWF_EXIT_INTERNAL_ERROR);
|
2020-06-22 10:15:12 -04:00
|
|
|
}
|
1998-05-31 07:53:28 -04:00
|
|
|
tcscpy(outName, outputDir);
|
2007-04-29 20:50:22 -04:00
|
|
|
tcscat(outName, delim);
|
1998-05-31 07:53:28 -04:00
|
|
|
tcscat(outName, file);
|
2017-08-21 12:21:22 -04:00
|
|
|
userData.fp = tfopen(outName, T("wb"));
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! userData.fp) {
|
2002-07-01 11:13:02 -04:00
|
|
|
tperror(outName);
|
2021-04-19 12:19:44 -04:00
|
|
|
exitCode = XMLWF_EXIT_OUTPUT_ERROR;
|
2021-03-16 23:40:26 -04:00
|
|
|
if (continueOnError) {
|
|
|
|
free(outName);
|
|
|
|
cleanupUserData(&userData);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
1998-02-04 00:25:27 -05:00
|
|
|
}
|
2017-08-21 12:21:22 -04:00
|
|
|
setvbuf(userData.fp, NULL, _IOFBF, 16384);
|
1998-05-31 07:53:28 -04:00
|
|
|
#ifdef XML_UNICODE
|
2017-08-21 12:21:22 -04:00
|
|
|
puttc(0xFEFF, userData.fp);
|
1998-05-31 07:53:28 -04:00
|
|
|
#endif
|
2017-08-21 12:21:22 -04:00
|
|
|
XML_SetUserData(parser, &userData);
|
1998-06-17 06:04:31 -04:00
|
|
|
switch (outputType) {
|
|
|
|
case 'm':
|
2002-07-01 11:13:02 -04:00
|
|
|
XML_UseParserAsHandlerArg(parser);
|
|
|
|
XML_SetElementHandler(parser, metaStartElement, metaEndElement);
|
|
|
|
XML_SetProcessingInstructionHandler(parser, metaProcessingInstruction);
|
|
|
|
XML_SetCommentHandler(parser, metaComment);
|
|
|
|
XML_SetCdataSectionHandler(parser, metaStartCdataSection,
|
|
|
|
metaEndCdataSection);
|
|
|
|
XML_SetCharacterDataHandler(parser, metaCharacterData);
|
|
|
|
XML_SetDoctypeDeclHandler(parser, metaStartDoctypeDecl,
|
|
|
|
metaEndDoctypeDecl);
|
|
|
|
XML_SetEntityDeclHandler(parser, metaEntityDecl);
|
|
|
|
XML_SetNotationDeclHandler(parser, metaNotationDecl);
|
|
|
|
XML_SetNamespaceDeclHandler(parser, metaStartNamespaceDecl,
|
|
|
|
metaEndNamespaceDecl);
|
|
|
|
metaStartDocument(parser);
|
|
|
|
break;
|
1998-06-17 06:04:31 -04:00
|
|
|
case 'c':
|
2002-07-01 11:13:02 -04:00
|
|
|
XML_UseParserAsHandlerArg(parser);
|
|
|
|
XML_SetDefaultHandler(parser, markup);
|
|
|
|
XML_SetElementHandler(parser, defaultStartElement, defaultEndElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, defaultCharacterData);
|
|
|
|
XML_SetProcessingInstructionHandler(parser,
|
|
|
|
defaultProcessingInstruction);
|
|
|
|
break;
|
1998-06-17 06:04:31 -04:00
|
|
|
default:
|
2002-07-01 11:13:02 -04:00
|
|
|
if (useNamespaces)
|
|
|
|
XML_SetElementHandler(parser, startElementNS, endElementNS);
|
|
|
|
else
|
|
|
|
XML_SetElementHandler(parser, startElement, endElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, characterData);
|
1999-07-09 08:17:40 -04:00
|
|
|
#ifndef W3C14N
|
2002-07-01 11:13:02 -04:00
|
|
|
XML_SetProcessingInstructionHandler(parser, processingInstruction);
|
2017-08-02 11:09:13 -04:00
|
|
|
if (requiresNotations) {
|
|
|
|
XML_SetDoctypeDeclHandler(parser, startDoctypeDecl, endDoctypeDecl);
|
|
|
|
XML_SetNotationDeclHandler(parser, notationDecl);
|
|
|
|
}
|
1999-07-09 08:17:40 -04:00
|
|
|
#endif /* not W3C14N */
|
2002-07-01 11:13:02 -04:00
|
|
|
break;
|
1998-06-03 03:04:44 -04:00
|
|
|
}
|
1998-06-01 08:12:35 -04:00
|
|
|
}
|
|
|
|
if (windowsCodePages)
|
1998-06-02 04:57:14 -04:00
|
|
|
XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0);
|
2002-04-19 17:41:54 -04:00
|
|
|
result = XML_ProcessFile(parser, useStdin ? NULL : argv[i], processFlags);
|
1998-02-04 00:25:27 -05:00
|
|
|
if (outputDir) {
|
1998-06-17 06:04:31 -04:00
|
|
|
if (outputType == 'm')
|
2002-07-01 11:13:02 -04:00
|
|
|
metaEndDocument(parser);
|
2017-08-21 12:21:22 -04:00
|
|
|
fclose(userData.fp);
|
2019-08-03 14:34:54 -04:00
|
|
|
if (! result) {
|
2002-07-01 11:13:02 -04:00
|
|
|
tremove(outName);
|
2009-01-18 19:19:47 -05:00
|
|
|
}
|
1998-02-04 00:25:27 -05:00
|
|
|
free(outName);
|
|
|
|
}
|
|
|
|
XML_ParserFree(parser);
|
2019-09-05 12:29:59 -04:00
|
|
|
if (! result) {
|
2021-04-19 12:19:44 -04:00
|
|
|
exitCode = XMLWF_EXIT_NOT_WELLFORMED;
|
2021-03-16 23:40:26 -04:00
|
|
|
cleanupUserData(&userData);
|
|
|
|
if (! continueOnError) {
|
|
|
|
break;
|
|
|
|
}
|
2019-09-05 12:29:59 -04:00
|
|
|
}
|
1997-11-11 00:52:10 -05:00
|
|
|
}
|
2021-03-16 23:40:26 -04:00
|
|
|
return exitCode;
|
1997-11-11 00:52:10 -05:00
|
|
|
}
|