xmlwf.c 31 KB

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