xmlwf.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <[email protected]>
  10. Copyright (c) 2001-2003 Fred L. Drake, Jr. <[email protected]>
  11. Copyright (c) 2004-2009 Karl Waclawek <[email protected]>
  12. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  13. Copyright (c) 2016-2021 Sebastian Pipping <[email protected]>
  14. Copyright (c) 2017 Rhodri James <[email protected]>
  15. Copyright (c) 2019 David Loffredo <[email protected]>
  16. Copyright (c) 2020 Joe Orton <[email protected]>
  17. Copyright (c) 2020 Kleber Tarcísio <[email protected]>
  18. Copyright (c) 2021 Tim Bray <[email protected]>
  19. Licensed under the MIT license:
  20. Permission is hereby granted, free of charge, to any person obtaining
  21. a copy of this software and associated documentation files (the
  22. "Software"), to deal in the Software without restriction, including
  23. without limitation the rights to use, copy, modify, merge, publish,
  24. distribute, sublicense, and/or sell copies of the Software, and to permit
  25. persons to whom the Software is furnished to do so, subject to the
  26. following conditions:
  27. The above copyright notice and this permission notice shall be included
  28. in all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  32. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  33. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  34. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  35. USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. #include <expat_config.h>
  38. #include <assert.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <stddef.h>
  42. #include <string.h>
  43. #include <math.h> /* for isnan */
  44. #include <errno.h>
  45. #include "expat.h"
  46. #include "codepage.h"
  47. #include "internal.h" /* for UNUSED_P only */
  48. #include "xmlfile.h"
  49. #include "xmltchar.h"
  50. #ifdef _MSC_VER
  51. # include <crtdbg.h>
  52. #endif
  53. #ifdef XML_UNICODE
  54. # include <wchar.h>
  55. #endif
  56. enum ExitCode {
  57. XMLWF_EXIT_SUCCESS = 0,
  58. XMLWF_EXIT_INTERNAL_ERROR = 1,
  59. XMLWF_EXIT_NOT_WELLFORMED = 2,
  60. XMLWF_EXIT_OUTPUT_ERROR = 3,
  61. XMLWF_EXIT_USAGE_ERROR = 4,
  62. };
  63. /* Structures for handler user data */
  64. typedef struct NotationList {
  65. struct NotationList *next;
  66. const XML_Char *notationName;
  67. const XML_Char *systemId;
  68. const XML_Char *publicId;
  69. } NotationList;
  70. typedef struct xmlwfUserData {
  71. FILE *fp;
  72. NotationList *notationListHead;
  73. const XML_Char *currentDoctypeName;
  74. } XmlwfUserData;
  75. /* This ensures proper sorting. */
  76. #define NSSEP T('\001')
  77. static void XMLCALL
  78. characterData(void *userData, const XML_Char *s, int len) {
  79. FILE *fp = ((XmlwfUserData *)userData)->fp;
  80. for (; len > 0; --len, ++s) {
  81. switch (*s) {
  82. case T('&'):
  83. fputts(T("&amp;"), fp);
  84. break;
  85. case T('<'):
  86. fputts(T("&lt;"), fp);
  87. break;
  88. case T('>'):
  89. fputts(T("&gt;"), fp);
  90. break;
  91. #ifdef W3C14N
  92. case 13:
  93. fputts(T("&#xD;"), fp);
  94. break;
  95. #else
  96. case T('"'):
  97. fputts(T("&quot;"), fp);
  98. break;
  99. case 9:
  100. case 10:
  101. case 13:
  102. ftprintf(fp, T("&#%d;"), *s);
  103. break;
  104. #endif
  105. default:
  106. puttc(*s, fp);
  107. break;
  108. }
  109. }
  110. }
  111. static void
  112. attributeValue(FILE *fp, const XML_Char *s) {
  113. puttc(T('='), fp);
  114. puttc(T('"'), fp);
  115. assert(s);
  116. for (;;) {
  117. switch (*s) {
  118. case 0:
  119. case NSSEP:
  120. puttc(T('"'), fp);
  121. return;
  122. case T('&'):
  123. fputts(T("&amp;"), fp);
  124. break;
  125. case T('<'):
  126. fputts(T("&lt;"), fp);
  127. break;
  128. case T('"'):
  129. fputts(T("&quot;"), fp);
  130. break;
  131. #ifdef W3C14N
  132. case 9:
  133. fputts(T("&#x9;"), fp);
  134. break;
  135. case 10:
  136. fputts(T("&#xA;"), fp);
  137. break;
  138. case 13:
  139. fputts(T("&#xD;"), fp);
  140. break;
  141. #else
  142. case T('>'):
  143. fputts(T("&gt;"), fp);
  144. break;
  145. case 9:
  146. case 10:
  147. case 13:
  148. ftprintf(fp, T("&#%d;"), *s);
  149. break;
  150. #endif
  151. default:
  152. puttc(*s, fp);
  153. break;
  154. }
  155. s++;
  156. }
  157. }
  158. /* Lexicographically comparing UTF-8 encoded attribute values,
  159. is equivalent to lexicographically comparing based on the character number. */
  160. static int
  161. attcmp(const void *att1, const void *att2) {
  162. return tcscmp(*(const XML_Char **)att1, *(const XML_Char **)att2);
  163. }
  164. static void XMLCALL
  165. startElement(void *userData, const XML_Char *name, const XML_Char **atts) {
  166. int nAtts;
  167. const XML_Char **p;
  168. FILE *fp = ((XmlwfUserData *)userData)->fp;
  169. puttc(T('<'), fp);
  170. fputts(name, fp);
  171. p = atts;
  172. while (*p)
  173. ++p;
  174. nAtts = (int)((p - atts) >> 1);
  175. if (nAtts > 1)
  176. qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, attcmp);
  177. while (*atts) {
  178. puttc(T(' '), fp);
  179. fputts(*atts++, fp);
  180. attributeValue(fp, *atts);
  181. atts++;
  182. }
  183. puttc(T('>'), fp);
  184. }
  185. static void XMLCALL
  186. endElement(void *userData, const XML_Char *name) {
  187. FILE *fp = ((XmlwfUserData *)userData)->fp;
  188. puttc(T('<'), fp);
  189. puttc(T('/'), fp);
  190. fputts(name, fp);
  191. puttc(T('>'), fp);
  192. }
  193. static int
  194. nsattcmp(const void *p1, const void *p2) {
  195. const XML_Char *att1 = *(const XML_Char **)p1;
  196. const XML_Char *att2 = *(const XML_Char **)p2;
  197. int sep1 = (tcsrchr(att1, NSSEP) != 0);
  198. int sep2 = (tcsrchr(att1, NSSEP) != 0);
  199. if (sep1 != sep2)
  200. return sep1 - sep2;
  201. return tcscmp(att1, att2);
  202. }
  203. static void XMLCALL
  204. startElementNS(void *userData, const XML_Char *name, const XML_Char **atts) {
  205. int nAtts;
  206. int nsi;
  207. const XML_Char **p;
  208. FILE *fp = ((XmlwfUserData *)userData)->fp;
  209. const XML_Char *sep;
  210. puttc(T('<'), fp);
  211. sep = tcsrchr(name, NSSEP);
  212. if (sep) {
  213. fputts(T("n1:"), fp);
  214. fputts(sep + 1, fp);
  215. fputts(T(" xmlns:n1"), fp);
  216. attributeValue(fp, name);
  217. nsi = 2;
  218. } else {
  219. fputts(name, fp);
  220. nsi = 1;
  221. }
  222. p = atts;
  223. while (*p)
  224. ++p;
  225. nAtts = (int)((p - atts) >> 1);
  226. if (nAtts > 1)
  227. qsort((void *)atts, nAtts, sizeof(XML_Char *) * 2, nsattcmp);
  228. while (*atts) {
  229. name = *atts++;
  230. sep = tcsrchr(name, NSSEP);
  231. puttc(T(' '), fp);
  232. if (sep) {
  233. ftprintf(fp, T("n%d:"), nsi);
  234. fputts(sep + 1, fp);
  235. } else
  236. fputts(name, fp);
  237. attributeValue(fp, *atts);
  238. if (sep) {
  239. ftprintf(fp, T(" xmlns:n%d"), nsi++);
  240. attributeValue(fp, name);
  241. }
  242. atts++;
  243. }
  244. puttc(T('>'), fp);
  245. }
  246. static void XMLCALL
  247. endElementNS(void *userData, const XML_Char *name) {
  248. FILE *fp = ((XmlwfUserData *)userData)->fp;
  249. const XML_Char *sep;
  250. puttc(T('<'), fp);
  251. puttc(T('/'), fp);
  252. sep = tcsrchr(name, NSSEP);
  253. if (sep) {
  254. fputts(T("n1:"), fp);
  255. fputts(sep + 1, fp);
  256. } else
  257. fputts(name, fp);
  258. puttc(T('>'), fp);
  259. }
  260. #ifndef W3C14N
  261. static void XMLCALL
  262. processingInstruction(void *userData, const XML_Char *target,
  263. const XML_Char *data) {
  264. FILE *fp = ((XmlwfUserData *)userData)->fp;
  265. puttc(T('<'), fp);
  266. puttc(T('?'), fp);
  267. fputts(target, fp);
  268. puttc(T(' '), fp);
  269. fputts(data, fp);
  270. puttc(T('?'), fp);
  271. puttc(T('>'), fp);
  272. }
  273. static XML_Char *
  274. xcsdup(const XML_Char *s) {
  275. XML_Char *result;
  276. int count = 0;
  277. int numBytes;
  278. /* Get the length of the string, including terminator */
  279. while (s[count++] != 0) {
  280. /* Do nothing */
  281. }
  282. numBytes = count * sizeof(XML_Char);
  283. result = malloc(numBytes);
  284. if (result == NULL)
  285. return NULL;
  286. memcpy(result, s, numBytes);
  287. return result;
  288. }
  289. static void XMLCALL
  290. startDoctypeDecl(void *userData, const XML_Char *doctypeName,
  291. const XML_Char *sysid, const XML_Char *publid,
  292. int has_internal_subset) {
  293. XmlwfUserData *data = (XmlwfUserData *)userData;
  294. UNUSED_P(sysid);
  295. UNUSED_P(publid);
  296. UNUSED_P(has_internal_subset);
  297. data->currentDoctypeName = xcsdup(doctypeName);
  298. }
  299. static void
  300. freeNotations(XmlwfUserData *data) {
  301. NotationList *notationListHead = data->notationListHead;
  302. while (notationListHead != NULL) {
  303. NotationList *next = notationListHead->next;
  304. free((void *)notationListHead->notationName);
  305. free((void *)notationListHead->systemId);
  306. free((void *)notationListHead->publicId);
  307. free(notationListHead);
  308. notationListHead = next;
  309. }
  310. data->notationListHead = NULL;
  311. }
  312. static void
  313. cleanupUserData(XmlwfUserData *userData) {
  314. free((void *)userData->currentDoctypeName);
  315. userData->currentDoctypeName = NULL;
  316. freeNotations(userData);
  317. }
  318. static int
  319. xcscmp(const XML_Char *xs, const XML_Char *xt) {
  320. while (*xs != 0 && *xt != 0) {
  321. if (*xs < *xt)
  322. return -1;
  323. if (*xs > *xt)
  324. return 1;
  325. xs++;
  326. xt++;
  327. }
  328. if (*xs < *xt)
  329. return -1;
  330. if (*xs > *xt)
  331. return 1;
  332. return 0;
  333. }
  334. static int
  335. notationCmp(const void *a, const void *b) {
  336. const NotationList *const n1 = *(NotationList **)a;
  337. const NotationList *const n2 = *(NotationList **)b;
  338. return xcscmp(n1->notationName, n2->notationName);
  339. }
  340. static void XMLCALL
  341. endDoctypeDecl(void *userData) {
  342. XmlwfUserData *data = (XmlwfUserData *)userData;
  343. NotationList **notations;
  344. int notationCount = 0;
  345. NotationList *p;
  346. int i;
  347. /* How many notations do we have? */
  348. for (p = data->notationListHead; p != NULL; p = p->next)
  349. notationCount++;
  350. if (notationCount == 0) {
  351. /* Nothing to report */
  352. free((void *)data->currentDoctypeName);
  353. data->currentDoctypeName = NULL;
  354. return;
  355. }
  356. notations = malloc(notationCount * sizeof(NotationList *));
  357. if (notations == NULL) {
  358. fprintf(stderr, "Unable to sort notations");
  359. freeNotations(data);
  360. return;
  361. }
  362. for (p = data->notationListHead, i = 0; i < notationCount; p = p->next, i++) {
  363. notations[i] = p;
  364. }
  365. qsort(notations, notationCount, sizeof(NotationList *), notationCmp);
  366. /* Output the DOCTYPE header */
  367. fputts(T("<!DOCTYPE "), data->fp);
  368. fputts(data->currentDoctypeName, data->fp);
  369. fputts(T(" [\n"), data->fp);
  370. /* Now the NOTATIONs */
  371. for (i = 0; i < notationCount; i++) {
  372. fputts(T("<!NOTATION "), data->fp);
  373. fputts(notations[i]->notationName, data->fp);
  374. if (notations[i]->publicId != NULL) {
  375. fputts(T(" PUBLIC '"), data->fp);
  376. fputts(notations[i]->publicId, data->fp);
  377. puttc(T('\''), data->fp);
  378. if (notations[i]->systemId != NULL) {
  379. puttc(T(' '), data->fp);
  380. puttc(T('\''), data->fp);
  381. fputts(notations[i]->systemId, data->fp);
  382. puttc(T('\''), data->fp);
  383. }
  384. } else if (notations[i]->systemId != NULL) {
  385. fputts(T(" SYSTEM '"), data->fp);
  386. fputts(notations[i]->systemId, data->fp);
  387. puttc(T('\''), data->fp);
  388. }
  389. puttc(T('>'), data->fp);
  390. puttc(T('\n'), data->fp);
  391. }
  392. /* Finally end the DOCTYPE */
  393. fputts(T("]>\n"), data->fp);
  394. free(notations);
  395. freeNotations(data);
  396. free((void *)data->currentDoctypeName);
  397. data->currentDoctypeName = NULL;
  398. }
  399. static void XMLCALL
  400. notationDecl(void *userData, const XML_Char *notationName, const XML_Char *base,
  401. const XML_Char *systemId, const XML_Char *publicId) {
  402. XmlwfUserData *data = (XmlwfUserData *)userData;
  403. NotationList *entry = malloc(sizeof(NotationList));
  404. const char *errorMessage = "Unable to store NOTATION for output\n";
  405. UNUSED_P(base);
  406. if (entry == NULL) {
  407. fputs(errorMessage, stderr);
  408. return; /* Nothing we can really do about this */
  409. }
  410. entry->notationName = xcsdup(notationName);
  411. if (entry->notationName == NULL) {
  412. fputs(errorMessage, stderr);
  413. free(entry);
  414. return;
  415. }
  416. if (systemId != NULL) {
  417. entry->systemId = xcsdup(systemId);
  418. if (entry->systemId == NULL) {
  419. fputs(errorMessage, stderr);
  420. free((void *)entry->notationName);
  421. free(entry);
  422. return;
  423. }
  424. } else {
  425. entry->systemId = NULL;
  426. }
  427. if (publicId != NULL) {
  428. entry->publicId = xcsdup(publicId);
  429. if (entry->publicId == NULL) {
  430. fputs(errorMessage, stderr);
  431. free((void *)entry->systemId); /* Safe if it's NULL */
  432. free((void *)entry->notationName);
  433. free(entry);
  434. return;
  435. }
  436. } else {
  437. entry->publicId = NULL;
  438. }
  439. entry->next = data->notationListHead;
  440. data->notationListHead = entry;
  441. }
  442. #endif /* not W3C14N */
  443. static void XMLCALL
  444. defaultCharacterData(void *userData, const XML_Char *s, int len) {
  445. UNUSED_P(s);
  446. UNUSED_P(len);
  447. XML_DefaultCurrent((XML_Parser)userData);
  448. }
  449. static void XMLCALL
  450. defaultStartElement(void *userData, const XML_Char *name,
  451. const XML_Char **atts) {
  452. UNUSED_P(name);
  453. UNUSED_P(atts);
  454. XML_DefaultCurrent((XML_Parser)userData);
  455. }
  456. static void XMLCALL
  457. defaultEndElement(void *userData, const XML_Char *name) {
  458. UNUSED_P(name);
  459. XML_DefaultCurrent((XML_Parser)userData);
  460. }
  461. static void XMLCALL
  462. defaultProcessingInstruction(void *userData, const XML_Char *target,
  463. const XML_Char *data) {
  464. UNUSED_P(target);
  465. UNUSED_P(data);
  466. XML_DefaultCurrent((XML_Parser)userData);
  467. }
  468. static void XMLCALL
  469. nopCharacterData(void *userData, const XML_Char *s, int len) {
  470. UNUSED_P(userData);
  471. UNUSED_P(s);
  472. UNUSED_P(len);
  473. }
  474. static void XMLCALL
  475. nopStartElement(void *userData, const XML_Char *name, const XML_Char **atts) {
  476. UNUSED_P(userData);
  477. UNUSED_P(name);
  478. UNUSED_P(atts);
  479. }
  480. static void XMLCALL
  481. nopEndElement(void *userData, const XML_Char *name) {
  482. UNUSED_P(userData);
  483. UNUSED_P(name);
  484. }
  485. static void XMLCALL
  486. nopProcessingInstruction(void *userData, const XML_Char *target,
  487. const XML_Char *data) {
  488. UNUSED_P(userData);
  489. UNUSED_P(target);
  490. UNUSED_P(data);
  491. }
  492. static void XMLCALL
  493. markup(void *userData, const XML_Char *s, int len) {
  494. FILE *fp = ((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp;
  495. for (; len > 0; --len, ++s)
  496. puttc(*s, fp);
  497. }
  498. static void
  499. metaLocation(XML_Parser parser) {
  500. const XML_Char *uri = XML_GetBase(parser);
  501. FILE *fp = ((XmlwfUserData *)XML_GetUserData(parser))->fp;
  502. if (uri)
  503. ftprintf(fp, T(" uri=\"%s\""), uri);
  504. ftprintf(fp,
  505. T(" byte=\"%") T(XML_FMT_INT_MOD) T("d\"") T(" nbytes=\"%d\"")
  506. T(" line=\"%") T(XML_FMT_INT_MOD) T("u\"") T(" col=\"%")
  507. T(XML_FMT_INT_MOD) T("u\""),
  508. XML_GetCurrentByteIndex(parser), XML_GetCurrentByteCount(parser),
  509. XML_GetCurrentLineNumber(parser),
  510. XML_GetCurrentColumnNumber(parser));
  511. }
  512. static void
  513. metaStartDocument(void *userData) {
  514. fputts(T("<document>\n"),
  515. ((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp);
  516. }
  517. static void
  518. metaEndDocument(void *userData) {
  519. fputts(T("</document>\n"),
  520. ((XmlwfUserData *)XML_GetUserData((XML_Parser)userData))->fp);
  521. }
  522. static void XMLCALL
  523. metaStartElement(void *userData, const XML_Char *name, const XML_Char **atts) {
  524. XML_Parser parser = (XML_Parser)userData;
  525. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  526. FILE *fp = data->fp;
  527. const XML_Char **specifiedAttsEnd
  528. = atts + XML_GetSpecifiedAttributeCount(parser);
  529. const XML_Char **idAttPtr;
  530. int idAttIndex = XML_GetIdAttributeIndex(parser);
  531. if (idAttIndex < 0)
  532. idAttPtr = 0;
  533. else
  534. idAttPtr = atts + idAttIndex;
  535. ftprintf(fp, T("<starttag name=\"%s\""), name);
  536. metaLocation(parser);
  537. if (*atts) {
  538. fputts(T(">\n"), fp);
  539. do {
  540. ftprintf(fp, T("<attribute name=\"%s\" value=\""), atts[0]);
  541. characterData(data, atts[1], (int)tcslen(atts[1]));
  542. if (atts >= specifiedAttsEnd)
  543. fputts(T("\" defaulted=\"yes\"/>\n"), fp);
  544. else if (atts == idAttPtr)
  545. fputts(T("\" id=\"yes\"/>\n"), fp);
  546. else
  547. fputts(T("\"/>\n"), fp);
  548. } while (*(atts += 2));
  549. fputts(T("</starttag>\n"), fp);
  550. } else
  551. fputts(T("/>\n"), fp);
  552. }
  553. static void XMLCALL
  554. metaEndElement(void *userData, const XML_Char *name) {
  555. XML_Parser parser = (XML_Parser)userData;
  556. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  557. FILE *fp = data->fp;
  558. ftprintf(fp, T("<endtag name=\"%s\""), name);
  559. metaLocation(parser);
  560. fputts(T("/>\n"), fp);
  561. }
  562. static void XMLCALL
  563. metaProcessingInstruction(void *userData, const XML_Char *target,
  564. const XML_Char *data) {
  565. XML_Parser parser = (XML_Parser)userData;
  566. XmlwfUserData *usrData = (XmlwfUserData *)XML_GetUserData(parser);
  567. FILE *fp = usrData->fp;
  568. ftprintf(fp, T("<pi target=\"%s\" data=\""), target);
  569. characterData(usrData, data, (int)tcslen(data));
  570. puttc(T('"'), fp);
  571. metaLocation(parser);
  572. fputts(T("/>\n"), fp);
  573. }
  574. static void XMLCALL
  575. metaComment(void *userData, const XML_Char *data) {
  576. XML_Parser parser = (XML_Parser)userData;
  577. XmlwfUserData *usrData = (XmlwfUserData *)XML_GetUserData(parser);
  578. FILE *fp = usrData->fp;
  579. fputts(T("<comment data=\""), fp);
  580. characterData(usrData, data, (int)tcslen(data));
  581. puttc(T('"'), fp);
  582. metaLocation(parser);
  583. fputts(T("/>\n"), fp);
  584. }
  585. static void XMLCALL
  586. metaStartCdataSection(void *userData) {
  587. XML_Parser parser = (XML_Parser)userData;
  588. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  589. FILE *fp = data->fp;
  590. fputts(T("<startcdata"), fp);
  591. metaLocation(parser);
  592. fputts(T("/>\n"), fp);
  593. }
  594. static void XMLCALL
  595. metaEndCdataSection(void *userData) {
  596. XML_Parser parser = (XML_Parser)userData;
  597. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  598. FILE *fp = data->fp;
  599. fputts(T("<endcdata"), fp);
  600. metaLocation(parser);
  601. fputts(T("/>\n"), fp);
  602. }
  603. static void XMLCALL
  604. metaCharacterData(void *userData, const XML_Char *s, int len) {
  605. XML_Parser parser = (XML_Parser)userData;
  606. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  607. FILE *fp = data->fp;
  608. fputts(T("<chars str=\""), fp);
  609. characterData(data, s, len);
  610. puttc(T('"'), fp);
  611. metaLocation(parser);
  612. fputts(T("/>\n"), fp);
  613. }
  614. static void XMLCALL
  615. metaStartDoctypeDecl(void *userData, const XML_Char *doctypeName,
  616. const XML_Char *sysid, const XML_Char *pubid,
  617. int has_internal_subset) {
  618. XML_Parser parser = (XML_Parser)userData;
  619. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  620. FILE *fp = data->fp;
  621. UNUSED_P(sysid);
  622. UNUSED_P(pubid);
  623. UNUSED_P(has_internal_subset);
  624. ftprintf(fp, T("<startdoctype name=\"%s\""), doctypeName);
  625. metaLocation(parser);
  626. fputts(T("/>\n"), fp);
  627. }
  628. static void XMLCALL
  629. metaEndDoctypeDecl(void *userData) {
  630. XML_Parser parser = (XML_Parser)userData;
  631. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  632. FILE *fp = data->fp;
  633. fputts(T("<enddoctype"), fp);
  634. metaLocation(parser);
  635. fputts(T("/>\n"), fp);
  636. }
  637. static void XMLCALL
  638. metaNotationDecl(void *userData, const XML_Char *notationName,
  639. const XML_Char *base, const XML_Char *systemId,
  640. const XML_Char *publicId) {
  641. XML_Parser parser = (XML_Parser)userData;
  642. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  643. FILE *fp = data->fp;
  644. UNUSED_P(base);
  645. ftprintf(fp, T("<notation name=\"%s\""), notationName);
  646. if (publicId)
  647. ftprintf(fp, T(" public=\"%s\""), publicId);
  648. if (systemId) {
  649. fputts(T(" system=\""), fp);
  650. characterData(data, systemId, (int)tcslen(systemId));
  651. puttc(T('"'), fp);
  652. }
  653. metaLocation(parser);
  654. fputts(T("/>\n"), fp);
  655. }
  656. static void XMLCALL
  657. metaEntityDecl(void *userData, const XML_Char *entityName, int is_param,
  658. const XML_Char *value, int value_length, const XML_Char *base,
  659. const XML_Char *systemId, const XML_Char *publicId,
  660. const XML_Char *notationName) {
  661. XML_Parser parser = (XML_Parser)userData;
  662. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  663. FILE *fp = data->fp;
  664. UNUSED_P(is_param);
  665. UNUSED_P(base);
  666. if (value) {
  667. ftprintf(fp, T("<entity name=\"%s\""), entityName);
  668. metaLocation(parser);
  669. puttc(T('>'), fp);
  670. characterData(data, value, value_length);
  671. fputts(T("</entity/>\n"), fp);
  672. } else if (notationName) {
  673. ftprintf(fp, T("<entity name=\"%s\""), entityName);
  674. if (publicId)
  675. ftprintf(fp, T(" public=\"%s\""), publicId);
  676. fputts(T(" system=\""), fp);
  677. characterData(data, systemId, (int)tcslen(systemId));
  678. puttc(T('"'), fp);
  679. ftprintf(fp, T(" notation=\"%s\""), notationName);
  680. metaLocation(parser);
  681. fputts(T("/>\n"), fp);
  682. } else {
  683. ftprintf(fp, T("<entity name=\"%s\""), entityName);
  684. if (publicId)
  685. ftprintf(fp, T(" public=\"%s\""), publicId);
  686. fputts(T(" system=\""), fp);
  687. characterData(data, systemId, (int)tcslen(systemId));
  688. puttc(T('"'), fp);
  689. metaLocation(parser);
  690. fputts(T("/>\n"), fp);
  691. }
  692. }
  693. static void XMLCALL
  694. metaStartNamespaceDecl(void *userData, const XML_Char *prefix,
  695. const XML_Char *uri) {
  696. XML_Parser parser = (XML_Parser)userData;
  697. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  698. FILE *fp = data->fp;
  699. fputts(T("<startns"), fp);
  700. if (prefix)
  701. ftprintf(fp, T(" prefix=\"%s\""), prefix);
  702. if (uri) {
  703. fputts(T(" ns=\""), fp);
  704. characterData(data, uri, (int)tcslen(uri));
  705. fputts(T("\"/>\n"), fp);
  706. } else
  707. fputts(T("/>\n"), fp);
  708. }
  709. static void XMLCALL
  710. metaEndNamespaceDecl(void *userData, const XML_Char *prefix) {
  711. XML_Parser parser = (XML_Parser)userData;
  712. XmlwfUserData *data = (XmlwfUserData *)XML_GetUserData(parser);
  713. FILE *fp = data->fp;
  714. if (! prefix)
  715. fputts(T("<endns/>\n"), fp);
  716. else
  717. ftprintf(fp, T("<endns prefix=\"%s\"/>\n"), prefix);
  718. }
  719. static int XMLCALL
  720. unknownEncodingConvert(void *data, const char *p) {
  721. return codepageConvert(*(int *)data, p);
  722. }
  723. static int XMLCALL
  724. unknownEncoding(void *userData, const XML_Char *name, XML_Encoding *info) {
  725. int cp;
  726. static const XML_Char prefixL[] = T("windows-");
  727. static const XML_Char prefixU[] = T("WINDOWS-");
  728. int i;
  729. UNUSED_P(userData);
  730. for (i = 0; prefixU[i]; i++)
  731. if (name[i] != prefixU[i] && name[i] != prefixL[i])
  732. return 0;
  733. cp = 0;
  734. for (; name[i]; i++) {
  735. static const XML_Char digits[] = T("0123456789");
  736. const XML_Char *s = tcschr(digits, name[i]);
  737. if (! s)
  738. return 0;
  739. cp *= 10;
  740. cp += (int)(s - digits);
  741. if (cp >= 0x10000)
  742. return 0;
  743. }
  744. if (! codepageMap(cp, info->map))
  745. return 0;
  746. info->convert = unknownEncodingConvert;
  747. /* We could just cast the code page integer to a void *,
  748. and avoid the use of release. */
  749. info->release = free;
  750. info->data = malloc(sizeof(int));
  751. if (! info->data)
  752. return 0;
  753. *(int *)info->data = cp;
  754. return 1;
  755. }
  756. static int XMLCALL
  757. notStandalone(void *userData) {
  758. UNUSED_P(userData);
  759. return 0;
  760. }
  761. static void
  762. showVersion(XML_Char *prog) {
  763. XML_Char *s = prog;
  764. XML_Char ch;
  765. const XML_Feature *features = XML_GetFeatureList();
  766. while ((ch = *s) != 0) {
  767. if (ch == '/'
  768. #if defined(_WIN32)
  769. || ch == '\\'
  770. #endif
  771. )
  772. prog = s + 1;
  773. ++s;
  774. }
  775. ftprintf(stdout, T("%s using %s\n"), prog, XML_ExpatVersion());
  776. if (features != NULL && features[0].feature != XML_FEATURE_END) {
  777. int i = 1;
  778. ftprintf(stdout, T("%s"), features[0].name);
  779. if (features[0].value)
  780. ftprintf(stdout, T("=%ld"), features[0].value);
  781. while (features[i].feature != XML_FEATURE_END) {
  782. ftprintf(stdout, T(", %s"), features[i].name);
  783. if (features[i].value)
  784. ftprintf(stdout, T("=%ld"), features[i].value);
  785. ++i;
  786. }
  787. ftprintf(stdout, T("\n"));
  788. }
  789. }
  790. static void
  791. usage(const XML_Char *prog, int rc) {
  792. ftprintf(
  793. stderr,
  794. /* Generated with:
  795. * $ xmlwf/xmlwf_helpgen.sh
  796. * To update, change xmlwf/xmlwf_helpgen.py, then paste the output of
  797. * xmlwf/xmlwf_helpgen.sh in here.
  798. */
  799. /* clang-format off */
  800. T("usage:\n")
  801. T(" %s [OPTIONS] [FILE ...]\n")
  802. T(" %s -h\n")
  803. T(" %s -v\n")
  804. T("\n")
  805. T("xmlwf - Determines if an XML document is well-formed\n")
  806. T("\n")
  807. T("positional arguments:\n")
  808. T(" FILE file to process (default: STDIN)\n")
  809. T("\n")
  810. T("input control arguments:\n")
  811. T(" -s print an error if the document is not [s]tandalone\n")
  812. T(" -n enable [n]amespace processing\n")
  813. T(" -p enable processing external DTDs and [p]arameter entities\n")
  814. T(" -x enable processing of e[x]ternal entities\n")
  815. T(" -e ENCODING override any in-document [e]ncoding declaration\n")
  816. T(" -w enable support for [W]indows code pages\n")
  817. T(" -r disable memory-mapping and use normal file [r]ead IO calls instead\n")
  818. T(" -k when processing multiple files, [k]eep processing after first file with error\n")
  819. T("\n")
  820. T("output control arguments:\n")
  821. T(" -d DIRECTORY output [d]estination directory\n")
  822. T(" -c write a [c]opy of input XML, not canonical XML\n")
  823. T(" -m write [m]eta XML, not canonical XML\n")
  824. T(" -t write no XML output for [t]iming of plain parsing\n")
  825. T(" -N enable adding doctype and [n]otation declarations\n")
  826. T("\n")
  827. T("billion laughs attack protection:\n")
  828. T(" NOTE: If you ever need to increase these values for non-attack payload, please file a bug report.\n")
  829. T("\n")
  830. T(" -a FACTOR set maximum tolerated [a]mplification factor (default: 100.0)\n")
  831. T(" -b BYTES set number of output [b]ytes needed to activate (default: 8 MiB)\n")
  832. T("\n")
  833. T("info arguments:\n")
  834. T(" -h show this [h]elp message and exit\n")
  835. T(" -v show program's [v]ersion number and exit\n")
  836. T("\n")
  837. T("exit status:\n")
  838. T(" 0 the input files are well-formed and the output (if requested) was written successfully\n")
  839. T(" 1 could not allocate data structures, signals a serious problem with execution environment\n")
  840. T(" 2 one or more input files were not well-formed\n")
  841. T(" 3 could not create an output file\n")
  842. T(" 4 command-line argument error\n")
  843. T("\n")
  844. T("xmlwf of libexpat is software libre, licensed under the MIT license.\n")
  845. T("Please report bugs at https://github.com/libexpat/libexpat/issues. Thank you!\n")
  846. , /* clang-format on */
  847. prog, prog, prog);
  848. exit(rc);
  849. }
  850. #if defined(__MINGW32__) && defined(XML_UNICODE)
  851. /* Silence warning about missing prototype */
  852. int wmain(int argc, XML_Char **argv);
  853. #endif
  854. #define XMLWF_SHIFT_ARG_INTO(constCharStarTarget, argc, argv, i, j) \
  855. { \
  856. if (argv[i][j + 1] == T('\0')) { \
  857. if (++i == argc) \
  858. usage(argv[0], XMLWF_EXIT_USAGE_ERROR); \
  859. constCharStarTarget = argv[i]; \
  860. } else { \
  861. constCharStarTarget = argv[i] + j + 1; \
  862. } \
  863. i++; \
  864. j = 0; \
  865. }
  866. int
  867. tmain(int argc, XML_Char **argv) {
  868. int i, j;
  869. const XML_Char *outputDir = NULL;
  870. const XML_Char *encoding = NULL;
  871. unsigned processFlags = XML_MAP_FILE;
  872. int windowsCodePages = 0;
  873. int outputType = 0;
  874. int useNamespaces = 0;
  875. int requireStandalone = 0;
  876. int requiresNotations = 0;
  877. int continueOnError = 0;
  878. float attackMaximumAmplification = -1.0f; /* signaling "not set" */
  879. unsigned long long attackThresholdBytes;
  880. XML_Bool attackThresholdGiven = XML_FALSE;
  881. int exitCode = XMLWF_EXIT_SUCCESS;
  882. enum XML_ParamEntityParsing paramEntityParsing
  883. = XML_PARAM_ENTITY_PARSING_NEVER;
  884. int useStdin = 0;
  885. XmlwfUserData userData = {NULL, NULL, NULL};
  886. #ifdef _MSC_VER
  887. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  888. #endif
  889. i = 1;
  890. j = 0;
  891. while (i < argc) {
  892. if (j == 0) {
  893. if (argv[i][0] != T('-'))
  894. break;
  895. if (argv[i][1] == T('-') && argv[i][2] == T('\0')) {
  896. i++;
  897. break;
  898. }
  899. j++;
  900. }
  901. switch (argv[i][j]) {
  902. case T('r'):
  903. processFlags &= ~XML_MAP_FILE;
  904. j++;
  905. break;
  906. case T('s'):
  907. requireStandalone = 1;
  908. j++;
  909. break;
  910. case T('n'):
  911. useNamespaces = 1;
  912. j++;
  913. break;
  914. case T('p'):
  915. paramEntityParsing = XML_PARAM_ENTITY_PARSING_ALWAYS;
  916. /* fall through */
  917. case T('x'):
  918. processFlags |= XML_EXTERNAL_ENTITIES;
  919. j++;
  920. break;
  921. case T('w'):
  922. windowsCodePages = 1;
  923. j++;
  924. break;
  925. case T('m'):
  926. outputType = 'm';
  927. j++;
  928. break;
  929. case T('c'):
  930. outputType = 'c';
  931. useNamespaces = 0;
  932. j++;
  933. break;
  934. case T('t'):
  935. outputType = 't';
  936. j++;
  937. break;
  938. case T('N'):
  939. requiresNotations = 1;
  940. j++;
  941. break;
  942. case T('d'):
  943. XMLWF_SHIFT_ARG_INTO(outputDir, argc, argv, i, j);
  944. break;
  945. case T('e'):
  946. XMLWF_SHIFT_ARG_INTO(encoding, argc, argv, i, j);
  947. break;
  948. case T('h'):
  949. usage(argv[0], XMLWF_EXIT_SUCCESS);
  950. return 0;
  951. case T('v'):
  952. showVersion(argv[0]);
  953. return 0;
  954. case T('k'):
  955. continueOnError = 1;
  956. j++;
  957. break;
  958. case T('a'): {
  959. const XML_Char *valueText = NULL;
  960. XMLWF_SHIFT_ARG_INTO(valueText, argc, argv, i, j);
  961. errno = 0;
  962. XML_Char *afterValueText = (XML_Char *)valueText;
  963. attackMaximumAmplification = tcstof(valueText, &afterValueText);
  964. if ((errno != 0) || (afterValueText[0] != T('\0'))
  965. || isnan(attackMaximumAmplification)
  966. || (attackMaximumAmplification < 1.0f)) {
  967. // This prevents tperror(..) from reporting misleading "[..]: Success"
  968. errno = ERANGE;
  969. tperror(T("invalid amplification limit") T(
  970. " (needs a floating point number greater or equal than 1.0)"));
  971. exit(XMLWF_EXIT_USAGE_ERROR);
  972. }
  973. #ifndef XML_DTD
  974. ftprintf(stderr, T("Warning: Given amplification limit ignored") T(
  975. ", xmlwf has been compiled without DTD support.\n"));
  976. #endif
  977. break;
  978. }
  979. case T('b'): {
  980. const XML_Char *valueText = NULL;
  981. XMLWF_SHIFT_ARG_INTO(valueText, argc, argv, i, j);
  982. errno = 0;
  983. XML_Char *afterValueText = (XML_Char *)valueText;
  984. attackThresholdBytes = tcstoull(valueText, &afterValueText, 10);
  985. if ((errno != 0) || (afterValueText[0] != T('\0'))) {
  986. // This prevents tperror(..) from reporting misleading "[..]: Success"
  987. errno = ERANGE;
  988. tperror(T("invalid ignore threshold")
  989. T(" (needs an integer from 0 to 2^64-1)"));
  990. exit(XMLWF_EXIT_USAGE_ERROR);
  991. }
  992. attackThresholdGiven = XML_TRUE;
  993. #ifndef XML_DTD
  994. ftprintf(stderr, T("Warning: Given attack threshold ignored") T(
  995. ", xmlwf has been compiled without DTD support.\n"));
  996. #endif
  997. break;
  998. }
  999. case T('\0'):
  1000. if (j > 1) {
  1001. i++;
  1002. j = 0;
  1003. break;
  1004. }
  1005. /* fall through */
  1006. default:
  1007. usage(argv[0], XMLWF_EXIT_USAGE_ERROR);
  1008. }
  1009. }
  1010. if (i == argc) {
  1011. useStdin = 1;
  1012. processFlags &= ~XML_MAP_FILE;
  1013. i--;
  1014. }
  1015. for (; i < argc; i++) {
  1016. XML_Char *outName = 0;
  1017. int result;
  1018. XML_Parser parser;
  1019. if (useNamespaces)
  1020. parser = XML_ParserCreateNS(encoding, NSSEP);
  1021. else
  1022. parser = XML_ParserCreate(encoding);
  1023. if (! parser) {
  1024. tperror(T("Could not instantiate parser"));
  1025. exit(XMLWF_EXIT_INTERNAL_ERROR);
  1026. }
  1027. if (attackMaximumAmplification != -1.0f) {
  1028. #ifdef XML_DTD
  1029. XML_SetBillionLaughsAttackProtectionMaximumAmplification(
  1030. parser, attackMaximumAmplification);
  1031. #endif
  1032. }
  1033. if (attackThresholdGiven) {
  1034. #ifdef XML_DTD
  1035. XML_SetBillionLaughsAttackProtectionActivationThreshold(
  1036. parser, attackThresholdBytes);
  1037. #else
  1038. (void)attackThresholdBytes; // silence -Wunused-but-set-variable
  1039. #endif
  1040. }
  1041. if (requireStandalone)
  1042. XML_SetNotStandaloneHandler(parser, notStandalone);
  1043. XML_SetParamEntityParsing(parser, paramEntityParsing);
  1044. if (outputType == 't') {
  1045. /* This is for doing timings; this gives a more realistic estimate of
  1046. the parsing time. */
  1047. outputDir = 0;
  1048. XML_SetElementHandler(parser, nopStartElement, nopEndElement);
  1049. XML_SetCharacterDataHandler(parser, nopCharacterData);
  1050. XML_SetProcessingInstructionHandler(parser, nopProcessingInstruction);
  1051. } else if (outputDir) {
  1052. const XML_Char *delim = T("/");
  1053. const XML_Char *file = useStdin ? T("STDIN") : argv[i];
  1054. if (! useStdin) {
  1055. /* Jump after last (back)slash */
  1056. const XML_Char *lastDelim = tcsrchr(file, delim[0]);
  1057. if (lastDelim)
  1058. file = lastDelim + 1;
  1059. #if defined(_WIN32)
  1060. else {
  1061. const XML_Char *winDelim = T("\\");
  1062. lastDelim = tcsrchr(file, winDelim[0]);
  1063. if (lastDelim) {
  1064. file = lastDelim + 1;
  1065. delim = winDelim;
  1066. }
  1067. }
  1068. #endif
  1069. }
  1070. outName = (XML_Char *)malloc((tcslen(outputDir) + tcslen(file) + 2)
  1071. * sizeof(XML_Char));
  1072. if (! outName) {
  1073. tperror(T("Could not allocate memory"));
  1074. exit(XMLWF_EXIT_INTERNAL_ERROR);
  1075. }
  1076. tcscpy(outName, outputDir);
  1077. tcscat(outName, delim);
  1078. tcscat(outName, file);
  1079. userData.fp = tfopen(outName, T("wb"));
  1080. if (! userData.fp) {
  1081. tperror(outName);
  1082. exitCode = XMLWF_EXIT_OUTPUT_ERROR;
  1083. if (continueOnError) {
  1084. free(outName);
  1085. cleanupUserData(&userData);
  1086. continue;
  1087. } else {
  1088. break;
  1089. }
  1090. }
  1091. setvbuf(userData.fp, NULL, _IOFBF, 16384);
  1092. #ifdef XML_UNICODE
  1093. puttc(0xFEFF, userData.fp);
  1094. #endif
  1095. XML_SetUserData(parser, &userData);
  1096. switch (outputType) {
  1097. case 'm':
  1098. XML_UseParserAsHandlerArg(parser);
  1099. XML_SetElementHandler(parser, metaStartElement, metaEndElement);
  1100. XML_SetProcessingInstructionHandler(parser, metaProcessingInstruction);
  1101. XML_SetCommentHandler(parser, metaComment);
  1102. XML_SetCdataSectionHandler(parser, metaStartCdataSection,
  1103. metaEndCdataSection);
  1104. XML_SetCharacterDataHandler(parser, metaCharacterData);
  1105. XML_SetDoctypeDeclHandler(parser, metaStartDoctypeDecl,
  1106. metaEndDoctypeDecl);
  1107. XML_SetEntityDeclHandler(parser, metaEntityDecl);
  1108. XML_SetNotationDeclHandler(parser, metaNotationDecl);
  1109. XML_SetNamespaceDeclHandler(parser, metaStartNamespaceDecl,
  1110. metaEndNamespaceDecl);
  1111. metaStartDocument(parser);
  1112. break;
  1113. case 'c':
  1114. XML_UseParserAsHandlerArg(parser);
  1115. XML_SetDefaultHandler(parser, markup);
  1116. XML_SetElementHandler(parser, defaultStartElement, defaultEndElement);
  1117. XML_SetCharacterDataHandler(parser, defaultCharacterData);
  1118. XML_SetProcessingInstructionHandler(parser,
  1119. defaultProcessingInstruction);
  1120. break;
  1121. default:
  1122. if (useNamespaces)
  1123. XML_SetElementHandler(parser, startElementNS, endElementNS);
  1124. else
  1125. XML_SetElementHandler(parser, startElement, endElement);
  1126. XML_SetCharacterDataHandler(parser, characterData);
  1127. #ifndef W3C14N
  1128. XML_SetProcessingInstructionHandler(parser, processingInstruction);
  1129. if (requiresNotations) {
  1130. XML_SetDoctypeDeclHandler(parser, startDoctypeDecl, endDoctypeDecl);
  1131. XML_SetNotationDeclHandler(parser, notationDecl);
  1132. }
  1133. #endif /* not W3C14N */
  1134. break;
  1135. }
  1136. }
  1137. if (windowsCodePages)
  1138. XML_SetUnknownEncodingHandler(parser, unknownEncoding, 0);
  1139. result = XML_ProcessFile(parser, useStdin ? NULL : argv[i], processFlags);
  1140. if (outputDir) {
  1141. if (outputType == 'm')
  1142. metaEndDocument(parser);
  1143. fclose(userData.fp);
  1144. if (! result) {
  1145. tremove(outName);
  1146. }
  1147. free(outName);
  1148. }
  1149. XML_ParserFree(parser);
  1150. if (! result) {
  1151. exitCode = XMLWF_EXIT_NOT_WELLFORMED;
  1152. cleanupUserData(&userData);
  1153. if (! continueOnError) {
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. return exitCode;
  1159. }