handlers.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. /* XML handler functions for the Expat test suite
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2001-2006 Fred L. Drake, Jr. <[email protected]>
  9. Copyright (c) 2003 Greg Stein <[email protected]>
  10. Copyright (c) 2005-2007 Steven Solie <[email protected]>
  11. Copyright (c) 2005-2012 Karl Waclawek <[email protected]>
  12. Copyright (c) 2016-2024 Sebastian Pipping <[email protected]>
  13. Copyright (c) 2017-2022 Rhodri James <[email protected]>
  14. Copyright (c) 2017 Joe Orton <[email protected]>
  15. Copyright (c) 2017 José Gutiérrez de la Concha <[email protected]>
  16. Copyright (c) 2018 Marco Maggi <[email protected]>
  17. Copyright (c) 2019 David Loffredo <[email protected]>
  18. Copyright (c) 2020 Tim Gates <[email protected]>
  19. Copyright (c) 2021 Donghee Na <[email protected]>
  20. Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <[email protected]>
  21. Licensed under the MIT license:
  22. Permission is hereby granted, free of charge, to any person obtaining
  23. a copy of this software and associated documentation files (the
  24. "Software"), to deal in the Software without restriction, including
  25. without limitation the rights to use, copy, modify, merge, publish,
  26. distribute, sublicense, and/or sell copies of the Software, and to permit
  27. persons to whom the Software is furnished to do so, subject to the
  28. following conditions:
  29. The above copyright notice and this permission notice shall be included
  30. in all copies or substantial portions of the Software.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  34. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  35. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  36. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  37. USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39. #if defined(NDEBUG)
  40. # undef NDEBUG /* because test suite relies on assert(...) at the moment */
  41. #endif
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <assert.h>
  45. #include "expat_config.h"
  46. #include "expat.h"
  47. #include "internal.h"
  48. #include "chardata.h"
  49. #include "structdata.h"
  50. #include "common.h"
  51. #include "handlers.h"
  52. /* Global variables for user parameter settings tests */
  53. /* Variable holding the expected handler userData */
  54. const void *g_handler_data = NULL;
  55. /* Count of the number of times the comment handler has been invoked */
  56. int g_comment_count = 0;
  57. /* Count of the number of skipped entities */
  58. int g_skip_count = 0;
  59. /* Count of the number of times the XML declaration handler is invoked */
  60. int g_xdecl_count = 0;
  61. /* Start/End Element Handlers */
  62. void XMLCALL
  63. start_element_event_handler(void *userData, const XML_Char *name,
  64. const XML_Char **atts) {
  65. UNUSED_P(atts);
  66. CharData_AppendXMLChars((CharData *)userData, name, -1);
  67. }
  68. void XMLCALL
  69. end_element_event_handler(void *userData, const XML_Char *name) {
  70. CharData *storage = (CharData *)userData;
  71. CharData_AppendXMLChars(storage, XCS("/"), 1);
  72. CharData_AppendXMLChars(storage, name, -1);
  73. }
  74. void XMLCALL
  75. start_element_event_handler2(void *userData, const XML_Char *name,
  76. const XML_Char **attr) {
  77. StructData *storage = (StructData *)userData;
  78. UNUSED_P(attr);
  79. StructData_AddItem(storage, name, XML_GetCurrentColumnNumber(g_parser),
  80. XML_GetCurrentLineNumber(g_parser), STRUCT_START_TAG);
  81. }
  82. void XMLCALL
  83. end_element_event_handler2(void *userData, const XML_Char *name) {
  84. StructData *storage = (StructData *)userData;
  85. StructData_AddItem(storage, name, XML_GetCurrentColumnNumber(g_parser),
  86. XML_GetCurrentLineNumber(g_parser), STRUCT_END_TAG);
  87. }
  88. void XMLCALL
  89. counting_start_element_handler(void *userData, const XML_Char *name,
  90. const XML_Char **atts) {
  91. ElementInfo *info = (ElementInfo *)userData;
  92. AttrInfo *attr;
  93. int count, id, i;
  94. while (info->name != NULL) {
  95. if (! xcstrcmp(name, info->name))
  96. break;
  97. info++;
  98. }
  99. if (info->name == NULL)
  100. fail("Element not recognised");
  101. /* The attribute count is twice what you might expect. It is a
  102. * count of items in atts, an array which contains alternating
  103. * attribute names and attribute values. For the naive user this
  104. * is possibly a little unexpected, but it is what the
  105. * documentation in expat.h tells us to expect.
  106. */
  107. count = XML_GetSpecifiedAttributeCount(g_parser);
  108. if (info->attr_count * 2 != count) {
  109. fail("Not got expected attribute count");
  110. return;
  111. }
  112. id = XML_GetIdAttributeIndex(g_parser);
  113. if (id == -1 && info->id_name != NULL) {
  114. fail("ID not present");
  115. return;
  116. }
  117. if (id != -1 && xcstrcmp(atts[id], info->id_name)) {
  118. fail("ID does not have the correct name");
  119. return;
  120. }
  121. for (i = 0; i < info->attr_count; i++) {
  122. attr = info->attributes;
  123. while (attr->name != NULL) {
  124. if (! xcstrcmp(atts[0], attr->name))
  125. break;
  126. attr++;
  127. }
  128. if (attr->name == NULL) {
  129. fail("Attribute not recognised");
  130. return;
  131. }
  132. if (xcstrcmp(atts[1], attr->value)) {
  133. fail("Attribute has wrong value");
  134. return;
  135. }
  136. /* Remember, two entries in atts per attribute (see above) */
  137. atts += 2;
  138. }
  139. }
  140. void XMLCALL
  141. suspending_end_handler(void *userData, const XML_Char *s) {
  142. UNUSED_P(s);
  143. XML_StopParser((XML_Parser)userData, 1);
  144. }
  145. void XMLCALL
  146. start_element_suspender(void *userData, const XML_Char *name,
  147. const XML_Char **atts) {
  148. UNUSED_P(userData);
  149. UNUSED_P(atts);
  150. if (! xcstrcmp(name, XCS("suspend")))
  151. XML_StopParser(g_parser, XML_TRUE);
  152. if (! xcstrcmp(name, XCS("abort")))
  153. XML_StopParser(g_parser, XML_FALSE);
  154. }
  155. /* Check that an element name and attribute name match the expected values.
  156. The expected values are passed as an array reference of string pointers
  157. provided as the userData argument; the first is the expected
  158. element name, and the second is the expected attribute name.
  159. */
  160. int g_triplet_start_flag = XML_FALSE;
  161. int g_triplet_end_flag = XML_FALSE;
  162. void XMLCALL
  163. triplet_start_checker(void *userData, const XML_Char *name,
  164. const XML_Char **atts) {
  165. XML_Char **elemstr = (XML_Char **)userData;
  166. char buffer[1024];
  167. if (xcstrcmp(elemstr[0], name) != 0) {
  168. snprintf(buffer, sizeof(buffer),
  169. "unexpected start string: '%" XML_FMT_STR "'", name);
  170. fail(buffer);
  171. }
  172. if (xcstrcmp(elemstr[1], atts[0]) != 0) {
  173. snprintf(buffer, sizeof(buffer),
  174. "unexpected attribute string: '%" XML_FMT_STR "'", atts[0]);
  175. fail(buffer);
  176. }
  177. g_triplet_start_flag = XML_TRUE;
  178. }
  179. /* Check that the element name passed to the end-element handler matches
  180. the expected value. The expected value is passed as the first element
  181. in an array of strings passed as the userData argument.
  182. */
  183. void XMLCALL
  184. triplet_end_checker(void *userData, const XML_Char *name) {
  185. XML_Char **elemstr = (XML_Char **)userData;
  186. if (xcstrcmp(elemstr[0], name) != 0) {
  187. char buffer[1024];
  188. snprintf(buffer, sizeof(buffer),
  189. "unexpected end string: '%" XML_FMT_STR "'", name);
  190. fail(buffer);
  191. }
  192. g_triplet_end_flag = XML_TRUE;
  193. }
  194. void XMLCALL
  195. overwrite_start_checker(void *userData, const XML_Char *name,
  196. const XML_Char **atts) {
  197. CharData *storage = (CharData *)userData;
  198. CharData_AppendXMLChars(storage, XCS("start "), 6);
  199. CharData_AppendXMLChars(storage, name, -1);
  200. while (*atts != NULL) {
  201. CharData_AppendXMLChars(storage, XCS("\nattribute "), 11);
  202. CharData_AppendXMLChars(storage, *atts, -1);
  203. atts += 2;
  204. }
  205. CharData_AppendXMLChars(storage, XCS("\n"), 1);
  206. }
  207. void XMLCALL
  208. overwrite_end_checker(void *userData, const XML_Char *name) {
  209. CharData *storage = (CharData *)userData;
  210. CharData_AppendXMLChars(storage, XCS("end "), 4);
  211. CharData_AppendXMLChars(storage, name, -1);
  212. CharData_AppendXMLChars(storage, XCS("\n"), 1);
  213. }
  214. void XMLCALL
  215. start_element_fail(void *userData, const XML_Char *name,
  216. const XML_Char **atts) {
  217. UNUSED_P(userData);
  218. UNUSED_P(name);
  219. UNUSED_P(atts);
  220. /* We should never get here. */
  221. fail("should never reach start_element_fail()");
  222. }
  223. void XMLCALL
  224. start_ns_clearing_start_element(void *userData, const XML_Char *prefix,
  225. const XML_Char *uri) {
  226. UNUSED_P(prefix);
  227. UNUSED_P(uri);
  228. XML_SetStartElementHandler((XML_Parser)userData, NULL);
  229. }
  230. void XMLCALL
  231. start_element_issue_240(void *userData, const XML_Char *name,
  232. const XML_Char **atts) {
  233. DataIssue240 *mydata = (DataIssue240 *)userData;
  234. UNUSED_P(name);
  235. UNUSED_P(atts);
  236. mydata->deep++;
  237. }
  238. void XMLCALL
  239. end_element_issue_240(void *userData, const XML_Char *name) {
  240. DataIssue240 *mydata = (DataIssue240 *)userData;
  241. UNUSED_P(name);
  242. mydata->deep--;
  243. if (mydata->deep == 0) {
  244. XML_StopParser(mydata->parser, 0);
  245. }
  246. }
  247. /* Text encoding handlers */
  248. int XMLCALL
  249. UnknownEncodingHandler(void *data, const XML_Char *encoding,
  250. XML_Encoding *info) {
  251. UNUSED_P(data);
  252. if (xcstrcmp(encoding, XCS("unsupported-encoding")) == 0) {
  253. int i;
  254. for (i = 0; i < 256; ++i)
  255. info->map[i] = i;
  256. info->data = NULL;
  257. info->convert = NULL;
  258. info->release = NULL;
  259. return XML_STATUS_OK;
  260. }
  261. return XML_STATUS_ERROR;
  262. }
  263. static void
  264. dummy_release(void *data) {
  265. UNUSED_P(data);
  266. }
  267. int XMLCALL
  268. UnrecognisedEncodingHandler(void *data, const XML_Char *encoding,
  269. XML_Encoding *info) {
  270. UNUSED_P(data);
  271. UNUSED_P(encoding);
  272. info->data = NULL;
  273. info->convert = NULL;
  274. info->release = dummy_release;
  275. return XML_STATUS_ERROR;
  276. }
  277. int XMLCALL
  278. unknown_released_encoding_handler(void *data, const XML_Char *encoding,
  279. XML_Encoding *info) {
  280. UNUSED_P(data);
  281. if (! xcstrcmp(encoding, XCS("unsupported-encoding"))) {
  282. int i;
  283. for (i = 0; i < 256; i++)
  284. info->map[i] = i;
  285. info->data = NULL;
  286. info->convert = NULL;
  287. info->release = dummy_release;
  288. return XML_STATUS_OK;
  289. }
  290. return XML_STATUS_ERROR;
  291. }
  292. static int XMLCALL
  293. failing_converter(void *data, const char *s) {
  294. UNUSED_P(data);
  295. UNUSED_P(s);
  296. /* Always claim to have failed */
  297. return -1;
  298. }
  299. static int XMLCALL
  300. prefix_converter(void *data, const char *s) {
  301. UNUSED_P(data);
  302. /* If the first byte is 0xff, raise an error */
  303. if (s[0] == (char)-1)
  304. return -1;
  305. /* Just add the low bits of the first byte to the second */
  306. return (s[1] + (s[0] & 0x7f)) & 0x01ff;
  307. }
  308. int XMLCALL
  309. MiscEncodingHandler(void *data, const XML_Char *encoding, XML_Encoding *info) {
  310. int i;
  311. int high_map = -2; /* Assume a 2-byte sequence */
  312. if (! xcstrcmp(encoding, XCS("invalid-9"))
  313. || ! xcstrcmp(encoding, XCS("ascii-like"))
  314. || ! xcstrcmp(encoding, XCS("invalid-len"))
  315. || ! xcstrcmp(encoding, XCS("invalid-a"))
  316. || ! xcstrcmp(encoding, XCS("invalid-surrogate"))
  317. || ! xcstrcmp(encoding, XCS("invalid-high")))
  318. high_map = -1;
  319. for (i = 0; i < 128; ++i)
  320. info->map[i] = i;
  321. for (; i < 256; ++i)
  322. info->map[i] = high_map;
  323. /* If required, put an invalid value in the ASCII entries */
  324. if (! xcstrcmp(encoding, XCS("invalid-9")))
  325. info->map[9] = 5;
  326. /* If required, have a top-bit set character starts a 5-byte sequence */
  327. if (! xcstrcmp(encoding, XCS("invalid-len")))
  328. info->map[0x81] = -5;
  329. /* If required, make a top-bit set character a valid ASCII character */
  330. if (! xcstrcmp(encoding, XCS("invalid-a")))
  331. info->map[0x82] = 'a';
  332. /* If required, give a top-bit set character a forbidden value,
  333. * what would otherwise be the first of a surrogate pair.
  334. */
  335. if (! xcstrcmp(encoding, XCS("invalid-surrogate")))
  336. info->map[0x83] = 0xd801;
  337. /* If required, give a top-bit set character too high a value */
  338. if (! xcstrcmp(encoding, XCS("invalid-high")))
  339. info->map[0x84] = 0x010101;
  340. info->data = data;
  341. info->release = NULL;
  342. if (! xcstrcmp(encoding, XCS("failing-conv")))
  343. info->convert = failing_converter;
  344. else if (! xcstrcmp(encoding, XCS("prefix-conv")))
  345. info->convert = prefix_converter;
  346. else
  347. info->convert = NULL;
  348. return XML_STATUS_OK;
  349. }
  350. int XMLCALL
  351. long_encoding_handler(void *userData, const XML_Char *encoding,
  352. XML_Encoding *info) {
  353. int i;
  354. UNUSED_P(userData);
  355. UNUSED_P(encoding);
  356. for (i = 0; i < 256; i++)
  357. info->map[i] = i;
  358. info->data = NULL;
  359. info->convert = NULL;
  360. info->release = NULL;
  361. return XML_STATUS_OK;
  362. }
  363. /* External Entity Handlers */
  364. int XMLCALL
  365. external_entity_optioner(XML_Parser parser, const XML_Char *context,
  366. const XML_Char *base, const XML_Char *systemId,
  367. const XML_Char *publicId) {
  368. ExtOption *options = (ExtOption *)XML_GetUserData(parser);
  369. XML_Parser ext_parser;
  370. UNUSED_P(base);
  371. UNUSED_P(publicId);
  372. while (options->parse_text != NULL) {
  373. if (! xcstrcmp(systemId, options->system_id)) {
  374. enum XML_Status rc;
  375. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  376. if (ext_parser == NULL)
  377. return XML_STATUS_ERROR;
  378. rc = _XML_Parse_SINGLE_BYTES(ext_parser, options->parse_text,
  379. (int)strlen(options->parse_text), XML_TRUE);
  380. XML_ParserFree(ext_parser);
  381. return rc;
  382. }
  383. options++;
  384. }
  385. fail("No suitable option found");
  386. return XML_STATUS_ERROR;
  387. }
  388. int XMLCALL
  389. external_entity_loader(XML_Parser parser, const XML_Char *context,
  390. const XML_Char *base, const XML_Char *systemId,
  391. const XML_Char *publicId) {
  392. ExtTest *test_data = (ExtTest *)XML_GetUserData(parser);
  393. XML_Parser extparser;
  394. UNUSED_P(base);
  395. UNUSED_P(systemId);
  396. UNUSED_P(publicId);
  397. extparser = XML_ExternalEntityParserCreate(parser, context, NULL);
  398. if (extparser == NULL)
  399. fail("Could not create external entity parser.");
  400. if (test_data->encoding != NULL) {
  401. if (! XML_SetEncoding(extparser, test_data->encoding))
  402. fail("XML_SetEncoding() ignored for external entity");
  403. }
  404. if (_XML_Parse_SINGLE_BYTES(extparser, test_data->parse_text,
  405. (int)strlen(test_data->parse_text), XML_TRUE)
  406. == XML_STATUS_ERROR) {
  407. xml_failure(extparser);
  408. return XML_STATUS_ERROR;
  409. }
  410. XML_ParserFree(extparser);
  411. return XML_STATUS_OK;
  412. }
  413. int XMLCALL
  414. external_entity_faulter(XML_Parser parser, const XML_Char *context,
  415. const XML_Char *base, const XML_Char *systemId,
  416. const XML_Char *publicId) {
  417. XML_Parser ext_parser;
  418. ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
  419. UNUSED_P(base);
  420. UNUSED_P(systemId);
  421. UNUSED_P(publicId);
  422. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  423. if (ext_parser == NULL)
  424. fail("Could not create external entity parser");
  425. if (fault->encoding != NULL) {
  426. if (! XML_SetEncoding(ext_parser, fault->encoding))
  427. fail("XML_SetEncoding failed");
  428. }
  429. if (_XML_Parse_SINGLE_BYTES(ext_parser, fault->parse_text,
  430. (int)strlen(fault->parse_text), XML_TRUE)
  431. != XML_STATUS_ERROR)
  432. fail(fault->fail_text);
  433. if (XML_GetErrorCode(ext_parser) != fault->error)
  434. xml_failure(ext_parser);
  435. XML_ParserFree(ext_parser);
  436. return XML_STATUS_ERROR;
  437. }
  438. int XMLCALL
  439. external_entity_null_loader(XML_Parser parser, const XML_Char *context,
  440. const XML_Char *base, const XML_Char *systemId,
  441. const XML_Char *publicId) {
  442. UNUSED_P(parser);
  443. UNUSED_P(context);
  444. UNUSED_P(base);
  445. UNUSED_P(systemId);
  446. UNUSED_P(publicId);
  447. return XML_STATUS_OK;
  448. }
  449. int XMLCALL
  450. external_entity_resetter(XML_Parser parser, const XML_Char *context,
  451. const XML_Char *base, const XML_Char *systemId,
  452. const XML_Char *publicId) {
  453. const char *text = "<!ELEMENT doc (#PCDATA)*>";
  454. XML_Parser ext_parser;
  455. XML_ParsingStatus status;
  456. UNUSED_P(base);
  457. UNUSED_P(systemId);
  458. UNUSED_P(publicId);
  459. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  460. if (ext_parser == NULL)
  461. fail("Could not create external entity parser");
  462. XML_GetParsingStatus(ext_parser, &status);
  463. if (status.parsing != XML_INITIALIZED) {
  464. fail("Parsing status is not INITIALIZED");
  465. return XML_STATUS_ERROR;
  466. }
  467. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  468. == XML_STATUS_ERROR) {
  469. xml_failure(parser);
  470. return XML_STATUS_ERROR;
  471. }
  472. XML_GetParsingStatus(ext_parser, &status);
  473. if (status.parsing != XML_FINISHED) {
  474. fail("Parsing status is not FINISHED");
  475. return XML_STATUS_ERROR;
  476. }
  477. /* Check we can't parse here */
  478. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  479. != XML_STATUS_ERROR)
  480. fail("Parsing when finished not faulted");
  481. if (XML_GetErrorCode(ext_parser) != XML_ERROR_FINISHED)
  482. fail("Parsing when finished faulted with wrong code");
  483. XML_ParserReset(ext_parser, NULL);
  484. XML_GetParsingStatus(ext_parser, &status);
  485. if (status.parsing != XML_FINISHED) {
  486. fail("Parsing status not still FINISHED");
  487. return XML_STATUS_ERROR;
  488. }
  489. XML_ParserFree(ext_parser);
  490. return XML_STATUS_OK;
  491. }
  492. void XMLCALL
  493. entity_suspending_decl_handler(void *userData, const XML_Char *name,
  494. XML_Content *model) {
  495. XML_Parser ext_parser = (XML_Parser)userData;
  496. UNUSED_P(name);
  497. if (XML_StopParser(ext_parser, XML_TRUE) != XML_STATUS_ERROR)
  498. fail("Attempting to suspend a subordinate parser not faulted");
  499. if (XML_GetErrorCode(ext_parser) != XML_ERROR_SUSPEND_PE)
  500. fail("Suspending subordinate parser get wrong code");
  501. XML_SetElementDeclHandler(ext_parser, NULL);
  502. XML_FreeContentModel(g_parser, model);
  503. }
  504. int XMLCALL
  505. external_entity_suspender(XML_Parser parser, const XML_Char *context,
  506. const XML_Char *base, const XML_Char *systemId,
  507. const XML_Char *publicId) {
  508. const char *text = "<!ELEMENT doc (#PCDATA)*>";
  509. XML_Parser ext_parser;
  510. UNUSED_P(base);
  511. UNUSED_P(systemId);
  512. UNUSED_P(publicId);
  513. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  514. if (ext_parser == NULL)
  515. fail("Could not create external entity parser");
  516. XML_SetElementDeclHandler(ext_parser, entity_suspending_decl_handler);
  517. XML_SetUserData(ext_parser, ext_parser);
  518. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  519. == XML_STATUS_ERROR) {
  520. xml_failure(ext_parser);
  521. return XML_STATUS_ERROR;
  522. }
  523. XML_ParserFree(ext_parser);
  524. return XML_STATUS_OK;
  525. }
  526. void XMLCALL
  527. entity_suspending_xdecl_handler(void *userData, const XML_Char *version,
  528. const XML_Char *encoding, int standalone) {
  529. XML_Parser ext_parser = (XML_Parser)userData;
  530. UNUSED_P(version);
  531. UNUSED_P(encoding);
  532. UNUSED_P(standalone);
  533. XML_StopParser(ext_parser, g_resumable);
  534. XML_SetXmlDeclHandler(ext_parser, NULL);
  535. }
  536. int XMLCALL
  537. external_entity_suspend_xmldecl(XML_Parser parser, const XML_Char *context,
  538. const XML_Char *base, const XML_Char *systemId,
  539. const XML_Char *publicId) {
  540. const char *text = "<?xml version='1.0' encoding='us-ascii'?>";
  541. XML_Parser ext_parser;
  542. XML_ParsingStatus status;
  543. enum XML_Status rc;
  544. UNUSED_P(base);
  545. UNUSED_P(systemId);
  546. UNUSED_P(publicId);
  547. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  548. if (ext_parser == NULL)
  549. fail("Could not create external entity parser");
  550. XML_SetXmlDeclHandler(ext_parser, entity_suspending_xdecl_handler);
  551. XML_SetUserData(ext_parser, ext_parser);
  552. rc = _XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE);
  553. XML_GetParsingStatus(ext_parser, &status);
  554. if (g_resumable) {
  555. if (rc == XML_STATUS_ERROR)
  556. xml_failure(ext_parser);
  557. if (status.parsing != XML_SUSPENDED)
  558. fail("Ext Parsing status not SUSPENDED");
  559. } else {
  560. if (rc != XML_STATUS_ERROR)
  561. fail("Ext parsing not aborted");
  562. if (XML_GetErrorCode(ext_parser) != XML_ERROR_ABORTED)
  563. xml_failure(ext_parser);
  564. if (status.parsing != XML_FINISHED)
  565. fail("Ext Parsing status not FINISHED");
  566. }
  567. XML_ParserFree(ext_parser);
  568. return XML_STATUS_OK;
  569. }
  570. int XMLCALL
  571. external_entity_suspending_faulter(XML_Parser parser, const XML_Char *context,
  572. const XML_Char *base,
  573. const XML_Char *systemId,
  574. const XML_Char *publicId) {
  575. XML_Parser ext_parser;
  576. ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
  577. void *buffer;
  578. int parse_len = (int)strlen(fault->parse_text);
  579. UNUSED_P(base);
  580. UNUSED_P(systemId);
  581. UNUSED_P(publicId);
  582. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  583. if (ext_parser == NULL)
  584. fail("Could not create external entity parser");
  585. XML_SetXmlDeclHandler(ext_parser, entity_suspending_xdecl_handler);
  586. XML_SetUserData(ext_parser, ext_parser);
  587. g_resumable = XML_TRUE;
  588. buffer = XML_GetBuffer(ext_parser, parse_len);
  589. if (buffer == NULL)
  590. fail("Could not allocate parse buffer");
  591. assert(buffer != NULL);
  592. memcpy(buffer, fault->parse_text, parse_len);
  593. if (XML_ParseBuffer(ext_parser, parse_len, XML_FALSE) != XML_STATUS_SUSPENDED)
  594. fail("XML declaration did not suspend");
  595. if (XML_ResumeParser(ext_parser) != XML_STATUS_OK)
  596. xml_failure(ext_parser);
  597. if (XML_ParseBuffer(ext_parser, 0, XML_TRUE) != XML_STATUS_ERROR)
  598. fail(fault->fail_text);
  599. if (XML_GetErrorCode(ext_parser) != fault->error)
  600. xml_failure(ext_parser);
  601. XML_ParserFree(ext_parser);
  602. return XML_STATUS_ERROR;
  603. }
  604. int XMLCALL
  605. external_entity_failer__if_not_xml_ge(XML_Parser parser,
  606. const XML_Char *context,
  607. const XML_Char *base,
  608. const XML_Char *systemId,
  609. const XML_Char *publicId) {
  610. UNUSED_P(parser);
  611. UNUSED_P(context);
  612. UNUSED_P(base);
  613. UNUSED_P(systemId);
  614. UNUSED_P(publicId);
  615. #if XML_GE == 0
  616. fail(
  617. "Function external_entity_suspending_failer was called despite XML_GE==0.");
  618. #endif
  619. return XML_STATUS_OK;
  620. }
  621. int XMLCALL
  622. external_entity_cr_catcher(XML_Parser parser, const XML_Char *context,
  623. const XML_Char *base, const XML_Char *systemId,
  624. const XML_Char *publicId) {
  625. const char *text = "\r";
  626. XML_Parser ext_parser;
  627. UNUSED_P(base);
  628. UNUSED_P(systemId);
  629. UNUSED_P(publicId);
  630. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  631. if (ext_parser == NULL)
  632. fail("Could not create external entity parser");
  633. XML_SetCharacterDataHandler(ext_parser, cr_cdata_handler);
  634. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  635. == XML_STATUS_ERROR)
  636. xml_failure(ext_parser);
  637. XML_ParserFree(ext_parser);
  638. return XML_STATUS_OK;
  639. }
  640. int XMLCALL
  641. external_entity_bad_cr_catcher(XML_Parser parser, const XML_Char *context,
  642. const XML_Char *base, const XML_Char *systemId,
  643. const XML_Char *publicId) {
  644. const char *text = "<tag>\r";
  645. XML_Parser ext_parser;
  646. UNUSED_P(base);
  647. UNUSED_P(systemId);
  648. UNUSED_P(publicId);
  649. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  650. if (ext_parser == NULL)
  651. fail("Could not create external entity parser");
  652. XML_SetCharacterDataHandler(ext_parser, cr_cdata_handler);
  653. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  654. == XML_STATUS_OK)
  655. fail("Async entity error not caught");
  656. if (XML_GetErrorCode(ext_parser) != XML_ERROR_ASYNC_ENTITY)
  657. xml_failure(ext_parser);
  658. XML_ParserFree(ext_parser);
  659. return XML_STATUS_OK;
  660. }
  661. int XMLCALL
  662. external_entity_rsqb_catcher(XML_Parser parser, const XML_Char *context,
  663. const XML_Char *base, const XML_Char *systemId,
  664. const XML_Char *publicId) {
  665. const char *text = "<tag>]";
  666. XML_Parser ext_parser;
  667. UNUSED_P(base);
  668. UNUSED_P(systemId);
  669. UNUSED_P(publicId);
  670. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  671. if (ext_parser == NULL)
  672. fail("Could not create external entity parser");
  673. XML_SetCharacterDataHandler(ext_parser, rsqb_handler);
  674. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  675. != XML_STATUS_ERROR)
  676. fail("Async entity error not caught");
  677. if (XML_GetErrorCode(ext_parser) != XML_ERROR_ASYNC_ENTITY)
  678. xml_failure(ext_parser);
  679. XML_ParserFree(ext_parser);
  680. return XML_STATUS_OK;
  681. }
  682. int XMLCALL
  683. external_entity_good_cdata_ascii(XML_Parser parser, const XML_Char *context,
  684. const XML_Char *base, const XML_Char *systemId,
  685. const XML_Char *publicId) {
  686. const char *text = "<a><![CDATA[<greeting>Hello, world!</greeting>]]></a>";
  687. const XML_Char *expected = XCS("<greeting>Hello, world!</greeting>");
  688. CharData storage;
  689. XML_Parser ext_parser;
  690. UNUSED_P(base);
  691. UNUSED_P(systemId);
  692. UNUSED_P(publicId);
  693. CharData_Init(&storage);
  694. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  695. if (ext_parser == NULL)
  696. fail("Could not create external entity parser");
  697. XML_SetUserData(ext_parser, &storage);
  698. XML_SetCharacterDataHandler(ext_parser, accumulate_characters);
  699. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  700. == XML_STATUS_ERROR)
  701. xml_failure(ext_parser);
  702. CharData_CheckXMLChars(&storage, expected);
  703. XML_ParserFree(ext_parser);
  704. return XML_STATUS_OK;
  705. }
  706. int XMLCALL
  707. external_entity_param_checker(XML_Parser parser, const XML_Char *context,
  708. const XML_Char *base, const XML_Char *systemId,
  709. const XML_Char *publicId) {
  710. const char *text = "<!-- Subordinate parser -->\n"
  711. "<!ELEMENT doc (#PCDATA)*>";
  712. XML_Parser ext_parser;
  713. UNUSED_P(base);
  714. UNUSED_P(systemId);
  715. UNUSED_P(publicId);
  716. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  717. if (ext_parser == NULL)
  718. fail("Could not create external entity parser");
  719. g_handler_data = ext_parser;
  720. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  721. == XML_STATUS_ERROR) {
  722. xml_failure(parser);
  723. return XML_STATUS_ERROR;
  724. }
  725. g_handler_data = parser;
  726. XML_ParserFree(ext_parser);
  727. return XML_STATUS_OK;
  728. }
  729. int XMLCALL
  730. external_entity_ref_param_checker(XML_Parser parameter, const XML_Char *context,
  731. const XML_Char *base,
  732. const XML_Char *systemId,
  733. const XML_Char *publicId) {
  734. const char *text = "<!ELEMENT doc (#PCDATA)*>";
  735. XML_Parser ext_parser;
  736. UNUSED_P(base);
  737. UNUSED_P(systemId);
  738. UNUSED_P(publicId);
  739. if ((void *)parameter != g_handler_data)
  740. fail("External entity ref handler parameter not correct");
  741. /* Here we use the global 'parser' variable */
  742. ext_parser = XML_ExternalEntityParserCreate(g_parser, context, NULL);
  743. if (ext_parser == NULL)
  744. fail("Could not create external entity parser");
  745. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  746. == XML_STATUS_ERROR)
  747. xml_failure(ext_parser);
  748. XML_ParserFree(ext_parser);
  749. return XML_STATUS_OK;
  750. }
  751. int XMLCALL
  752. external_entity_param(XML_Parser parser, const XML_Char *context,
  753. const XML_Char *base, const XML_Char *systemId,
  754. const XML_Char *publicId) {
  755. const char *text1 = "<!ELEMENT doc EMPTY>\n"
  756. "<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
  757. "<!ENTITY % e2 '%e1;'>\n"
  758. "%e1;\n";
  759. const char *text2 = "<!ELEMENT el EMPTY>\n"
  760. "<el/>\n";
  761. XML_Parser ext_parser;
  762. UNUSED_P(base);
  763. UNUSED_P(publicId);
  764. if (systemId == NULL)
  765. return XML_STATUS_OK;
  766. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  767. if (ext_parser == NULL)
  768. fail("Could not create external entity parser");
  769. if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
  770. if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
  771. != XML_STATUS_ERROR)
  772. fail("Inner DTD with invalid tag not rejected");
  773. if (XML_GetErrorCode(ext_parser) != XML_ERROR_EXTERNAL_ENTITY_HANDLING)
  774. xml_failure(ext_parser);
  775. } else if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
  776. if (_XML_Parse_SINGLE_BYTES(ext_parser, text2, (int)strlen(text2), XML_TRUE)
  777. != XML_STATUS_ERROR)
  778. fail("Invalid tag in external param not rejected");
  779. if (XML_GetErrorCode(ext_parser) != XML_ERROR_SYNTAX)
  780. xml_failure(ext_parser);
  781. } else {
  782. fail("Unknown system ID");
  783. }
  784. XML_ParserFree(ext_parser);
  785. return XML_STATUS_ERROR;
  786. }
  787. int XMLCALL
  788. external_entity_load_ignore(XML_Parser parser, const XML_Char *context,
  789. const XML_Char *base, const XML_Char *systemId,
  790. const XML_Char *publicId) {
  791. const char *text = "<![IGNORE[<!ELEMENT e (#PCDATA)*>]]>";
  792. XML_Parser ext_parser;
  793. UNUSED_P(base);
  794. UNUSED_P(systemId);
  795. UNUSED_P(publicId);
  796. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  797. if (ext_parser == NULL)
  798. fail("Could not create external entity parser");
  799. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  800. == XML_STATUS_ERROR)
  801. xml_failure(parser);
  802. XML_ParserFree(ext_parser);
  803. return XML_STATUS_OK;
  804. }
  805. int XMLCALL
  806. external_entity_load_ignore_utf16(XML_Parser parser, const XML_Char *context,
  807. const XML_Char *base,
  808. const XML_Char *systemId,
  809. const XML_Char *publicId) {
  810. const char text[] =
  811. /* <![IGNORE[<!ELEMENT e (#PCDATA)*>]]> */
  812. "<\0!\0[\0I\0G\0N\0O\0R\0E\0[\0"
  813. "<\0!\0E\0L\0E\0M\0E\0N\0T\0 \0e\0 \0"
  814. "(\0#\0P\0C\0D\0A\0T\0A\0)\0*\0>\0]\0]\0>\0";
  815. XML_Parser ext_parser;
  816. UNUSED_P(base);
  817. UNUSED_P(systemId);
  818. UNUSED_P(publicId);
  819. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  820. if (ext_parser == NULL)
  821. fail("Could not create external entity parser");
  822. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)sizeof(text) - 1, XML_TRUE)
  823. == XML_STATUS_ERROR)
  824. xml_failure(parser);
  825. XML_ParserFree(ext_parser);
  826. return XML_STATUS_OK;
  827. }
  828. int XMLCALL
  829. external_entity_load_ignore_utf16_be(XML_Parser parser, const XML_Char *context,
  830. const XML_Char *base,
  831. const XML_Char *systemId,
  832. const XML_Char *publicId) {
  833. const char text[] =
  834. /* <![IGNORE[<!ELEMENT e (#PCDATA)*>]]> */
  835. "\0<\0!\0[\0I\0G\0N\0O\0R\0E\0["
  836. "\0<\0!\0E\0L\0E\0M\0E\0N\0T\0 \0e\0 "
  837. "\0(\0#\0P\0C\0D\0A\0T\0A\0)\0*\0>\0]\0]\0>";
  838. XML_Parser ext_parser;
  839. UNUSED_P(base);
  840. UNUSED_P(systemId);
  841. UNUSED_P(publicId);
  842. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  843. if (ext_parser == NULL)
  844. fail("Could not create external entity parser");
  845. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)sizeof(text) - 1, XML_TRUE)
  846. == XML_STATUS_ERROR)
  847. xml_failure(parser);
  848. XML_ParserFree(ext_parser);
  849. return XML_STATUS_OK;
  850. }
  851. int XMLCALL
  852. external_entity_valuer(XML_Parser parser, const XML_Char *context,
  853. const XML_Char *base, const XML_Char *systemId,
  854. const XML_Char *publicId) {
  855. const char *text1 = "<!ELEMENT doc EMPTY>\n"
  856. "<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
  857. "<!ENTITY % e2 '%e1;'>\n"
  858. "%e1;\n";
  859. XML_Parser ext_parser;
  860. UNUSED_P(base);
  861. UNUSED_P(publicId);
  862. if (systemId == NULL)
  863. return XML_STATUS_OK;
  864. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  865. if (ext_parser == NULL)
  866. fail("Could not create external entity parser");
  867. if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
  868. if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
  869. == XML_STATUS_ERROR)
  870. xml_failure(ext_parser);
  871. } else if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
  872. ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
  873. enum XML_Status status;
  874. enum XML_Error error;
  875. status = _XML_Parse_SINGLE_BYTES(ext_parser, fault->parse_text,
  876. (int)strlen(fault->parse_text), XML_TRUE);
  877. if (fault->error == XML_ERROR_NONE) {
  878. if (status == XML_STATUS_ERROR)
  879. xml_failure(ext_parser);
  880. } else {
  881. if (status != XML_STATUS_ERROR)
  882. fail(fault->fail_text);
  883. error = XML_GetErrorCode(ext_parser);
  884. if (error != fault->error
  885. && (fault->error != XML_ERROR_XML_DECL
  886. || error != XML_ERROR_TEXT_DECL))
  887. xml_failure(ext_parser);
  888. }
  889. }
  890. XML_ParserFree(ext_parser);
  891. return XML_STATUS_OK;
  892. }
  893. int XMLCALL
  894. external_entity_not_standalone(XML_Parser parser, const XML_Char *context,
  895. const XML_Char *base, const XML_Char *systemId,
  896. const XML_Char *publicId) {
  897. const char *text1 = "<!ELEMENT doc EMPTY>\n"
  898. "<!ENTITY % e1 SYSTEM 'bar'>\n"
  899. "%e1;\n";
  900. const char *text2 = "<!ATTLIST doc a1 CDATA 'value'>";
  901. XML_Parser ext_parser;
  902. UNUSED_P(base);
  903. UNUSED_P(publicId);
  904. if (systemId == NULL)
  905. return XML_STATUS_OK;
  906. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  907. if (ext_parser == NULL)
  908. fail("Could not create external entity parser");
  909. if (! xcstrcmp(systemId, XCS("foo"))) {
  910. XML_SetNotStandaloneHandler(ext_parser, reject_not_standalone_handler);
  911. if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
  912. != XML_STATUS_ERROR)
  913. fail("Expected not standalone rejection");
  914. if (XML_GetErrorCode(ext_parser) != XML_ERROR_NOT_STANDALONE)
  915. xml_failure(ext_parser);
  916. XML_SetNotStandaloneHandler(ext_parser, NULL);
  917. XML_ParserFree(ext_parser);
  918. return XML_STATUS_ERROR;
  919. } else if (! xcstrcmp(systemId, XCS("bar"))) {
  920. if (_XML_Parse_SINGLE_BYTES(ext_parser, text2, (int)strlen(text2), XML_TRUE)
  921. == XML_STATUS_ERROR)
  922. xml_failure(ext_parser);
  923. }
  924. XML_ParserFree(ext_parser);
  925. return XML_STATUS_OK;
  926. }
  927. int XMLCALL
  928. external_entity_value_aborter(XML_Parser parser, const XML_Char *context,
  929. const XML_Char *base, const XML_Char *systemId,
  930. const XML_Char *publicId) {
  931. const char *text1 = "<!ELEMENT doc EMPTY>\n"
  932. "<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
  933. "<!ENTITY % e2 '%e1;'>\n"
  934. "%e1;\n";
  935. const char *text2 = "<?xml version='1.0' encoding='utf-8'?>";
  936. XML_Parser ext_parser;
  937. UNUSED_P(base);
  938. UNUSED_P(publicId);
  939. if (systemId == NULL)
  940. return XML_STATUS_OK;
  941. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  942. if (ext_parser == NULL)
  943. fail("Could not create external entity parser");
  944. if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
  945. if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
  946. == XML_STATUS_ERROR)
  947. xml_failure(ext_parser);
  948. }
  949. if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
  950. XML_SetXmlDeclHandler(ext_parser, entity_suspending_xdecl_handler);
  951. XML_SetUserData(ext_parser, ext_parser);
  952. if (_XML_Parse_SINGLE_BYTES(ext_parser, text2, (int)strlen(text2), XML_TRUE)
  953. != XML_STATUS_ERROR)
  954. fail("Aborted parse not faulted");
  955. if (XML_GetErrorCode(ext_parser) != XML_ERROR_ABORTED)
  956. xml_failure(ext_parser);
  957. }
  958. XML_ParserFree(ext_parser);
  959. return XML_STATUS_OK;
  960. }
  961. int XMLCALL
  962. external_entity_public(XML_Parser parser, const XML_Char *context,
  963. const XML_Char *base, const XML_Char *systemId,
  964. const XML_Char *publicId) {
  965. const char *text1 = (const char *)XML_GetUserData(parser);
  966. const char *text2 = "<!ATTLIST doc a CDATA 'value'>";
  967. const char *text = NULL;
  968. XML_Parser ext_parser;
  969. int parse_res;
  970. UNUSED_P(base);
  971. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  972. if (ext_parser == NULL)
  973. return XML_STATUS_ERROR;
  974. if (systemId != NULL && ! xcstrcmp(systemId, XCS("http://example.org/"))) {
  975. text = text1;
  976. } else if (publicId != NULL && ! xcstrcmp(publicId, XCS("foo"))) {
  977. text = text2;
  978. } else
  979. fail("Unexpected parameters to external entity parser");
  980. assert(text != NULL);
  981. parse_res
  982. = _XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE);
  983. XML_ParserFree(ext_parser);
  984. return parse_res;
  985. }
  986. int XMLCALL
  987. external_entity_devaluer(XML_Parser parser, const XML_Char *context,
  988. const XML_Char *base, const XML_Char *systemId,
  989. const XML_Char *publicId) {
  990. const char *text = "<!ELEMENT doc EMPTY>\n"
  991. "<!ENTITY % e1 SYSTEM 'bar'>\n"
  992. "%e1;\n";
  993. XML_Parser ext_parser;
  994. int clear_handler_flag = (XML_GetUserData(parser) != NULL);
  995. UNUSED_P(base);
  996. UNUSED_P(publicId);
  997. if (systemId == NULL || ! xcstrcmp(systemId, XCS("bar")))
  998. return XML_STATUS_OK;
  999. if (xcstrcmp(systemId, XCS("foo")))
  1000. fail("Unexpected system ID");
  1001. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1002. if (ext_parser == NULL)
  1003. fail("Could note create external entity parser");
  1004. if (clear_handler_flag)
  1005. XML_SetExternalEntityRefHandler(ext_parser, NULL);
  1006. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  1007. == XML_STATUS_ERROR)
  1008. xml_failure(ext_parser);
  1009. XML_ParserFree(ext_parser);
  1010. return XML_STATUS_OK;
  1011. }
  1012. int XMLCALL
  1013. external_entity_oneshot_loader(XML_Parser parser, const XML_Char *context,
  1014. const XML_Char *base, const XML_Char *systemId,
  1015. const XML_Char *publicId) {
  1016. ExtHdlrData *test_data = (ExtHdlrData *)XML_GetUserData(parser);
  1017. XML_Parser ext_parser;
  1018. UNUSED_P(base);
  1019. UNUSED_P(systemId);
  1020. UNUSED_P(publicId);
  1021. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1022. if (ext_parser == NULL)
  1023. fail("Could not create external entity parser.");
  1024. /* Use the requested entity parser for further externals */
  1025. XML_SetExternalEntityRefHandler(ext_parser, test_data->handler);
  1026. if (_XML_Parse_SINGLE_BYTES(ext_parser, test_data->parse_text,
  1027. (int)strlen(test_data->parse_text), XML_TRUE)
  1028. == XML_STATUS_ERROR) {
  1029. xml_failure(ext_parser);
  1030. }
  1031. XML_ParserFree(ext_parser);
  1032. return XML_STATUS_OK;
  1033. }
  1034. int XMLCALL
  1035. external_entity_loader2(XML_Parser parser, const XML_Char *context,
  1036. const XML_Char *base, const XML_Char *systemId,
  1037. const XML_Char *publicId) {
  1038. ExtTest2 *test_data = (ExtTest2 *)XML_GetUserData(parser);
  1039. XML_Parser extparser;
  1040. UNUSED_P(base);
  1041. UNUSED_P(systemId);
  1042. UNUSED_P(publicId);
  1043. extparser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1044. if (extparser == NULL)
  1045. fail("Coulr not create external entity parser");
  1046. if (test_data->encoding != NULL) {
  1047. if (! XML_SetEncoding(extparser, test_data->encoding))
  1048. fail("XML_SetEncoding() ignored for external entity");
  1049. }
  1050. if (_XML_Parse_SINGLE_BYTES(extparser, test_data->parse_text,
  1051. test_data->parse_len, XML_TRUE)
  1052. == XML_STATUS_ERROR) {
  1053. xml_failure(extparser);
  1054. }
  1055. XML_ParserFree(extparser);
  1056. return XML_STATUS_OK;
  1057. }
  1058. int XMLCALL
  1059. external_entity_faulter2(XML_Parser parser, const XML_Char *context,
  1060. const XML_Char *base, const XML_Char *systemId,
  1061. const XML_Char *publicId) {
  1062. ExtFaults2 *test_data = (ExtFaults2 *)XML_GetUserData(parser);
  1063. XML_Parser extparser;
  1064. UNUSED_P(base);
  1065. UNUSED_P(systemId);
  1066. UNUSED_P(publicId);
  1067. extparser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1068. if (extparser == NULL)
  1069. fail("Could not create external entity parser");
  1070. if (test_data->encoding != NULL) {
  1071. if (! XML_SetEncoding(extparser, test_data->encoding))
  1072. fail("XML_SetEncoding() ignored for external entity");
  1073. }
  1074. if (_XML_Parse_SINGLE_BYTES(extparser, test_data->parse_text,
  1075. test_data->parse_len, XML_TRUE)
  1076. != XML_STATUS_ERROR)
  1077. fail(test_data->fail_text);
  1078. if (XML_GetErrorCode(extparser) != test_data->error)
  1079. xml_failure(extparser);
  1080. XML_ParserFree(extparser);
  1081. return XML_STATUS_ERROR;
  1082. }
  1083. int XMLCALL
  1084. external_entity_unfinished_attlist(XML_Parser parser, const XML_Char *context,
  1085. const XML_Char *base,
  1086. const XML_Char *systemId,
  1087. const XML_Char *publicId) {
  1088. const char *text = "<!ELEMENT barf ANY>\n"
  1089. "<!ATTLIST barf my_attr (blah|%blah;a|foo) #REQUIRED>\n"
  1090. "<!--COMMENT-->\n";
  1091. XML_Parser ext_parser;
  1092. UNUSED_P(base);
  1093. UNUSED_P(publicId);
  1094. if (systemId == NULL)
  1095. return XML_STATUS_OK;
  1096. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1097. if (ext_parser == NULL)
  1098. fail("Could not create external entity parser");
  1099. if (_XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE)
  1100. == XML_STATUS_ERROR)
  1101. xml_failure(ext_parser);
  1102. XML_ParserFree(ext_parser);
  1103. return XML_STATUS_OK;
  1104. }
  1105. int XMLCALL
  1106. external_entity_handler(XML_Parser parser, const XML_Char *context,
  1107. const XML_Char *base, const XML_Char *systemId,
  1108. const XML_Char *publicId) {
  1109. void *user_data = XML_GetUserData(parser);
  1110. const char *text;
  1111. XML_Parser p2;
  1112. UNUSED_P(base);
  1113. UNUSED_P(systemId);
  1114. UNUSED_P(publicId);
  1115. if (user_data == NULL)
  1116. text = ("<!ELEMENT doc (e+)>\n"
  1117. "<!ATTLIST doc xmlns CDATA #IMPLIED>\n"
  1118. "<!ELEMENT e EMPTY>\n");
  1119. else
  1120. text = ("<?xml version='1.0' encoding='us-ascii'?>"
  1121. "<e/>");
  1122. /* Set user data to any non-NULL value */
  1123. XML_SetUserData(parser, parser);
  1124. p2 = XML_ExternalEntityParserCreate(parser, context, NULL);
  1125. if (_XML_Parse_SINGLE_BYTES(p2, text, (int)strlen(text), XML_TRUE)
  1126. == XML_STATUS_ERROR) {
  1127. xml_failure(p2);
  1128. return XML_STATUS_ERROR;
  1129. }
  1130. XML_ParserFree(p2);
  1131. return XML_STATUS_OK;
  1132. }
  1133. int XMLCALL
  1134. external_entity_duff_loader(XML_Parser parser, const XML_Char *context,
  1135. const XML_Char *base, const XML_Char *systemId,
  1136. const XML_Char *publicId) {
  1137. XML_Parser new_parser;
  1138. unsigned int i;
  1139. const unsigned int max_alloc_count = 10;
  1140. UNUSED_P(base);
  1141. UNUSED_P(systemId);
  1142. UNUSED_P(publicId);
  1143. /* Try a few different allocation levels */
  1144. for (i = 0; i < max_alloc_count; i++) {
  1145. g_allocation_count = i;
  1146. new_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1147. if (new_parser != NULL) {
  1148. XML_ParserFree(new_parser);
  1149. break;
  1150. }
  1151. }
  1152. if (i == 0)
  1153. fail("External parser creation ignored failing allocator");
  1154. else if (i == max_alloc_count)
  1155. fail("Extern parser not created with max allocation count");
  1156. /* Make sure other random allocation doesn't now fail */
  1157. g_allocation_count = ALLOC_ALWAYS_SUCCEED;
  1158. /* Make sure the failure code path is executed too */
  1159. return XML_STATUS_ERROR;
  1160. }
  1161. int XMLCALL
  1162. external_entity_dbl_handler(XML_Parser parser, const XML_Char *context,
  1163. const XML_Char *base, const XML_Char *systemId,
  1164. const XML_Char *publicId) {
  1165. int *pcallno = (int *)XML_GetUserData(parser);
  1166. int callno = *pcallno;
  1167. const char *text;
  1168. XML_Parser new_parser = NULL;
  1169. int i;
  1170. const int max_alloc_count = 20;
  1171. UNUSED_P(base);
  1172. UNUSED_P(systemId);
  1173. UNUSED_P(publicId);
  1174. if (callno == 0) {
  1175. /* First time through, check how many calls to malloc occur */
  1176. text = ("<!ELEMENT doc (e+)>\n"
  1177. "<!ATTLIST doc xmlns CDATA #IMPLIED>\n"
  1178. "<!ELEMENT e EMPTY>\n");
  1179. g_allocation_count = 10000;
  1180. new_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1181. if (new_parser == NULL) {
  1182. fail("Unable to allocate first external parser");
  1183. return XML_STATUS_ERROR;
  1184. }
  1185. /* Stash the number of calls in the user data */
  1186. *pcallno = 10000 - g_allocation_count;
  1187. } else {
  1188. text = ("<?xml version='1.0' encoding='us-ascii'?>"
  1189. "<e/>");
  1190. /* Try at varying levels to exercise more code paths */
  1191. for (i = 0; i < max_alloc_count; i++) {
  1192. g_allocation_count = callno + i;
  1193. new_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1194. if (new_parser != NULL)
  1195. break;
  1196. }
  1197. if (i == 0) {
  1198. fail("Second external parser unexpectedly created");
  1199. XML_ParserFree(new_parser);
  1200. return XML_STATUS_ERROR;
  1201. } else if (i == max_alloc_count) {
  1202. fail("Second external parser not created");
  1203. return XML_STATUS_ERROR;
  1204. }
  1205. }
  1206. g_allocation_count = ALLOC_ALWAYS_SUCCEED;
  1207. if (_XML_Parse_SINGLE_BYTES(new_parser, text, (int)strlen(text), XML_TRUE)
  1208. == XML_STATUS_ERROR) {
  1209. xml_failure(new_parser);
  1210. return XML_STATUS_ERROR;
  1211. }
  1212. XML_ParserFree(new_parser);
  1213. return XML_STATUS_OK;
  1214. }
  1215. int XMLCALL
  1216. external_entity_dbl_handler_2(XML_Parser parser, const XML_Char *context,
  1217. const XML_Char *base, const XML_Char *systemId,
  1218. const XML_Char *publicId) {
  1219. int *pcallno = (int *)XML_GetUserData(parser);
  1220. int callno = *pcallno;
  1221. const char *text;
  1222. XML_Parser new_parser;
  1223. enum XML_Status rv;
  1224. UNUSED_P(base);
  1225. UNUSED_P(systemId);
  1226. UNUSED_P(publicId);
  1227. if (callno == 0) {
  1228. /* Try different allocation levels for whole exercise */
  1229. text = ("<!ELEMENT doc (e+)>\n"
  1230. "<!ATTLIST doc xmlns CDATA #IMPLIED>\n"
  1231. "<!ELEMENT e EMPTY>\n");
  1232. *pcallno = 1;
  1233. new_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1234. if (new_parser == NULL)
  1235. return XML_STATUS_ERROR;
  1236. rv = _XML_Parse_SINGLE_BYTES(new_parser, text, (int)strlen(text), XML_TRUE);
  1237. } else {
  1238. /* Just run through once */
  1239. text = ("<?xml version='1.0' encoding='us-ascii'?>"
  1240. "<e/>");
  1241. new_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1242. if (new_parser == NULL)
  1243. return XML_STATUS_ERROR;
  1244. rv = _XML_Parse_SINGLE_BYTES(new_parser, text, (int)strlen(text), XML_TRUE);
  1245. }
  1246. XML_ParserFree(new_parser);
  1247. if (rv == XML_STATUS_ERROR)
  1248. return XML_STATUS_ERROR;
  1249. return XML_STATUS_OK;
  1250. }
  1251. int XMLCALL
  1252. external_entity_alloc_set_encoding(XML_Parser parser, const XML_Char *context,
  1253. const XML_Char *base,
  1254. const XML_Char *systemId,
  1255. const XML_Char *publicId) {
  1256. /* As for external_entity_loader() */
  1257. const char *text = "<?xml encoding='iso-8859-3'?>"
  1258. "\xC3\xA9";
  1259. XML_Parser ext_parser;
  1260. enum XML_Status status;
  1261. UNUSED_P(base);
  1262. UNUSED_P(systemId);
  1263. UNUSED_P(publicId);
  1264. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1265. if (ext_parser == NULL)
  1266. return XML_STATUS_ERROR;
  1267. if (! XML_SetEncoding(ext_parser, XCS("utf-8"))) {
  1268. XML_ParserFree(ext_parser);
  1269. return XML_STATUS_ERROR;
  1270. }
  1271. status
  1272. = _XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE);
  1273. XML_ParserFree(ext_parser);
  1274. if (status == XML_STATUS_ERROR)
  1275. return XML_STATUS_ERROR;
  1276. return XML_STATUS_OK;
  1277. }
  1278. int XMLCALL
  1279. external_entity_reallocator(XML_Parser parser, const XML_Char *context,
  1280. const XML_Char *base, const XML_Char *systemId,
  1281. const XML_Char *publicId) {
  1282. const char *text = get_buffer_test_text;
  1283. XML_Parser ext_parser;
  1284. void *buffer;
  1285. enum XML_Status status;
  1286. UNUSED_P(base);
  1287. UNUSED_P(systemId);
  1288. UNUSED_P(publicId);
  1289. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1290. if (ext_parser == NULL)
  1291. fail("Could not create external entity parser");
  1292. g_reallocation_count = *(int *)XML_GetUserData(parser);
  1293. buffer = XML_GetBuffer(ext_parser, 1536);
  1294. if (buffer == NULL)
  1295. fail("Buffer allocation failed");
  1296. assert(buffer != NULL);
  1297. memcpy(buffer, text, strlen(text));
  1298. status = XML_ParseBuffer(ext_parser, (int)strlen(text), XML_FALSE);
  1299. g_reallocation_count = -1;
  1300. XML_ParserFree(ext_parser);
  1301. return (status == XML_STATUS_OK) ? XML_STATUS_OK : XML_STATUS_ERROR;
  1302. }
  1303. int XMLCALL
  1304. external_entity_alloc(XML_Parser parser, const XML_Char *context,
  1305. const XML_Char *base, const XML_Char *systemId,
  1306. const XML_Char *publicId) {
  1307. const char *text = (const char *)XML_GetUserData(parser);
  1308. XML_Parser ext_parser;
  1309. int parse_res;
  1310. UNUSED_P(base);
  1311. UNUSED_P(systemId);
  1312. UNUSED_P(publicId);
  1313. ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
  1314. if (ext_parser == NULL)
  1315. return XML_STATUS_ERROR;
  1316. parse_res
  1317. = _XML_Parse_SINGLE_BYTES(ext_parser, text, (int)strlen(text), XML_TRUE);
  1318. XML_ParserFree(ext_parser);
  1319. return parse_res;
  1320. }
  1321. int XMLCALL
  1322. external_entity_parser_create_alloc_fail_handler(XML_Parser parser,
  1323. const XML_Char *context,
  1324. const XML_Char *base,
  1325. const XML_Char *systemId,
  1326. const XML_Char *publicId) {
  1327. UNUSED_P(base);
  1328. UNUSED_P(systemId);
  1329. UNUSED_P(publicId);
  1330. if (context != NULL)
  1331. fail("Unexpected non-NULL context");
  1332. // The following number intends to fail the upcoming allocation in line
  1333. // "parser->m_protocolEncodingName = copyString(encodingName,
  1334. // &(parser->m_mem));" in function parserInit.
  1335. g_allocation_count = 3;
  1336. const XML_Char *const encodingName = XCS("UTF-8"); // needs something non-NULL
  1337. const XML_Parser ext_parser
  1338. = XML_ExternalEntityParserCreate(parser, context, encodingName);
  1339. if (ext_parser != NULL)
  1340. fail(
  1341. "Call to XML_ExternalEntityParserCreate was expected to fail out-of-memory");
  1342. g_allocation_count = ALLOC_ALWAYS_SUCCEED;
  1343. return XML_STATUS_ERROR;
  1344. }
  1345. #if XML_GE == 1
  1346. int
  1347. accounting_external_entity_ref_handler(XML_Parser parser,
  1348. const XML_Char *context,
  1349. const XML_Char *base,
  1350. const XML_Char *systemId,
  1351. const XML_Char *publicId) {
  1352. UNUSED_P(base);
  1353. UNUSED_P(publicId);
  1354. const struct AccountingTestCase *const testCase
  1355. = (const struct AccountingTestCase *)XML_GetUserData(parser);
  1356. const char *externalText = NULL;
  1357. if (xcstrcmp(systemId, XCS("first.ent")) == 0) {
  1358. externalText = testCase->firstExternalText;
  1359. } else if (xcstrcmp(systemId, XCS("second.ent")) == 0) {
  1360. externalText = testCase->secondExternalText;
  1361. } else {
  1362. assert(! "systemId is neither \"first.ent\" nor \"second.ent\"");
  1363. }
  1364. assert(externalText);
  1365. XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0);
  1366. assert(entParser);
  1367. const enum XML_Status status = _XML_Parse_SINGLE_BYTES(
  1368. entParser, externalText, (int)strlen(externalText), XML_TRUE);
  1369. XML_ParserFree(entParser);
  1370. return status;
  1371. }
  1372. #endif /* XML_GE == 1 */
  1373. /* NotStandalone handlers */
  1374. int XMLCALL
  1375. reject_not_standalone_handler(void *userData) {
  1376. UNUSED_P(userData);
  1377. return XML_STATUS_ERROR;
  1378. }
  1379. int XMLCALL
  1380. accept_not_standalone_handler(void *userData) {
  1381. UNUSED_P(userData);
  1382. return XML_STATUS_OK;
  1383. }
  1384. /* Attribute List handlers */
  1385. void XMLCALL
  1386. verify_attlist_decl_handler(void *userData, const XML_Char *element_name,
  1387. const XML_Char *attr_name,
  1388. const XML_Char *attr_type,
  1389. const XML_Char *default_value, int is_required) {
  1390. AttTest *at = (AttTest *)userData;
  1391. if (xcstrcmp(element_name, at->element_name))
  1392. fail("Unexpected element name in attribute declaration");
  1393. if (xcstrcmp(attr_name, at->attr_name))
  1394. fail("Unexpected attribute name in attribute declaration");
  1395. if (xcstrcmp(attr_type, at->attr_type))
  1396. fail("Unexpected attribute type in attribute declaration");
  1397. if ((default_value == NULL && at->default_value != NULL)
  1398. || (default_value != NULL && at->default_value == NULL)
  1399. || (default_value != NULL && xcstrcmp(default_value, at->default_value)))
  1400. fail("Unexpected default value in attribute declaration");
  1401. if (is_required != at->is_required)
  1402. fail("Requirement mismatch in attribute declaration");
  1403. }
  1404. /* Character Data handlers */
  1405. void XMLCALL
  1406. clearing_aborting_character_handler(void *userData, const XML_Char *s,
  1407. int len) {
  1408. UNUSED_P(userData);
  1409. UNUSED_P(s);
  1410. UNUSED_P(len);
  1411. XML_StopParser(g_parser, g_resumable);
  1412. XML_SetCharacterDataHandler(g_parser, NULL);
  1413. }
  1414. void XMLCALL
  1415. parser_stop_character_handler(void *userData, const XML_Char *s, int len) {
  1416. UNUSED_P(userData);
  1417. UNUSED_P(s);
  1418. UNUSED_P(len);
  1419. XML_ParsingStatus status;
  1420. XML_GetParsingStatus(g_parser, &status);
  1421. if (status.parsing == XML_FINISHED) {
  1422. return; // the parser was stopped by a previous call to this handler.
  1423. }
  1424. XML_StopParser(g_parser, g_resumable);
  1425. XML_SetCharacterDataHandler(g_parser, NULL);
  1426. if (! g_resumable) {
  1427. /* Check that aborting an aborted parser is faulted */
  1428. if (XML_StopParser(g_parser, XML_FALSE) != XML_STATUS_ERROR)
  1429. fail("Aborting aborted parser not faulted");
  1430. if (XML_GetErrorCode(g_parser) != XML_ERROR_FINISHED)
  1431. xml_failure(g_parser);
  1432. } else if (g_abortable) {
  1433. /* Check that aborting a suspended parser works */
  1434. if (XML_StopParser(g_parser, XML_FALSE) == XML_STATUS_ERROR)
  1435. xml_failure(g_parser);
  1436. } else {
  1437. /* Check that suspending a suspended parser works */
  1438. if (XML_StopParser(g_parser, XML_TRUE) != XML_STATUS_ERROR)
  1439. fail("Suspending suspended parser not faulted");
  1440. if (XML_GetErrorCode(g_parser) != XML_ERROR_SUSPENDED)
  1441. xml_failure(g_parser);
  1442. }
  1443. }
  1444. void XMLCALL
  1445. cr_cdata_handler(void *userData, const XML_Char *s, int len) {
  1446. int *pfound = (int *)userData;
  1447. /* Internal processing turns the CR into a newline for the
  1448. * character data handler, but not for the default handler
  1449. */
  1450. if (len == 1 && (*s == XCS('\n') || *s == XCS('\r')))
  1451. *pfound = 1;
  1452. }
  1453. void XMLCALL
  1454. rsqb_handler(void *userData, const XML_Char *s, int len) {
  1455. int *pfound = (int *)userData;
  1456. if (len == 1 && *s == XCS(']'))
  1457. *pfound = 1;
  1458. }
  1459. void XMLCALL
  1460. byte_character_handler(void *userData, const XML_Char *s, int len) {
  1461. #if XML_CONTEXT_BYTES > 0
  1462. int offset, size;
  1463. const char *buffer;
  1464. ByteTestData *data = (ByteTestData *)userData;
  1465. UNUSED_P(s);
  1466. buffer = XML_GetInputContext(g_parser, &offset, &size);
  1467. if (buffer == NULL)
  1468. fail("Failed to get context buffer");
  1469. if (offset != data->start_element_len)
  1470. fail("Context offset in unexpected position");
  1471. if (len != data->cdata_len)
  1472. fail("CDATA length reported incorrectly");
  1473. if (size != data->total_string_len)
  1474. fail("Context size is not full buffer");
  1475. if (XML_GetCurrentByteIndex(g_parser) != offset)
  1476. fail("Character byte index incorrect");
  1477. if (XML_GetCurrentByteCount(g_parser) != len)
  1478. fail("Character byte count incorrect");
  1479. #else
  1480. UNUSED_P(s);
  1481. UNUSED_P(userData);
  1482. UNUSED_P(len);
  1483. #endif
  1484. }
  1485. void XMLCALL
  1486. ext2_accumulate_characters(void *userData, const XML_Char *s, int len) {
  1487. ExtTest2 *test_data = (ExtTest2 *)userData;
  1488. accumulate_characters(test_data->storage, s, len);
  1489. }
  1490. /* Handlers that record their function name and int arg. */
  1491. static void
  1492. record_call(struct handler_record_list *const rec, const char *funcname,
  1493. const int arg) {
  1494. const int max_entries = sizeof(rec->entries) / sizeof(rec->entries[0]);
  1495. assert_true(rec->count < max_entries);
  1496. struct handler_record_entry *const e = &rec->entries[rec->count++];
  1497. e->name = funcname;
  1498. e->arg = arg;
  1499. }
  1500. void XMLCALL
  1501. record_default_handler(void *userData, const XML_Char *s, int len) {
  1502. UNUSED_P(s);
  1503. record_call((struct handler_record_list *)userData, __func__, len);
  1504. }
  1505. void XMLCALL
  1506. record_cdata_handler(void *userData, const XML_Char *s, int len) {
  1507. UNUSED_P(s);
  1508. record_call((struct handler_record_list *)userData, __func__, len);
  1509. XML_DefaultCurrent(g_parser);
  1510. }
  1511. void XMLCALL
  1512. record_cdata_nodefault_handler(void *userData, const XML_Char *s, int len) {
  1513. UNUSED_P(s);
  1514. record_call((struct handler_record_list *)userData, __func__, len);
  1515. }
  1516. void XMLCALL
  1517. record_skip_handler(void *userData, const XML_Char *entityName,
  1518. int is_parameter_entity) {
  1519. UNUSED_P(entityName);
  1520. record_call((struct handler_record_list *)userData, __func__,
  1521. is_parameter_entity);
  1522. }
  1523. void XMLCALL
  1524. record_element_start_handler(void *userData, const XML_Char *name,
  1525. const XML_Char **atts) {
  1526. UNUSED_P(atts);
  1527. CharData_AppendXMLChars((CharData *)userData, name, (int)xcstrlen(name));
  1528. }
  1529. void XMLCALL
  1530. record_element_end_handler(void *userData, const XML_Char *name) {
  1531. CharData *storage = (CharData *)userData;
  1532. CharData_AppendXMLChars(storage, XCS("/"), 1);
  1533. CharData_AppendXMLChars(storage, name, -1);
  1534. }
  1535. const struct handler_record_entry *
  1536. _handler_record_get(const struct handler_record_list *storage, int index,
  1537. const char *file, int line) {
  1538. if (storage->count <= index) {
  1539. _fail(file, line, "too few handler calls");
  1540. }
  1541. return &storage->entries[index];
  1542. }
  1543. /* Entity Declaration Handlers */
  1544. static const XML_Char *entity_name_to_match = NULL;
  1545. static const XML_Char *entity_value_to_match = NULL;
  1546. static int entity_match_flag = ENTITY_MATCH_NOT_FOUND;
  1547. void XMLCALL
  1548. param_entity_match_handler(void *userData, const XML_Char *entityName,
  1549. int is_parameter_entity, const XML_Char *value,
  1550. int value_length, const XML_Char *base,
  1551. const XML_Char *systemId, const XML_Char *publicId,
  1552. const XML_Char *notationName) {
  1553. UNUSED_P(userData);
  1554. UNUSED_P(base);
  1555. UNUSED_P(systemId);
  1556. UNUSED_P(publicId);
  1557. UNUSED_P(notationName);
  1558. if (! is_parameter_entity || entity_name_to_match == NULL
  1559. || entity_value_to_match == NULL) {
  1560. return;
  1561. }
  1562. if (! xcstrcmp(entityName, entity_name_to_match)) {
  1563. /* The cast here is safe because we control the horizontal and
  1564. * the vertical, and we therefore know our strings are never
  1565. * going to overflow an int.
  1566. */
  1567. if (value_length != (int)xcstrlen(entity_value_to_match)
  1568. || xcstrncmp(value, entity_value_to_match, value_length)) {
  1569. entity_match_flag = ENTITY_MATCH_FAIL;
  1570. } else {
  1571. entity_match_flag = ENTITY_MATCH_SUCCESS;
  1572. }
  1573. }
  1574. /* Else leave the match flag alone */
  1575. }
  1576. void
  1577. param_entity_match_init(const XML_Char *name, const XML_Char *value) {
  1578. entity_name_to_match = name;
  1579. entity_value_to_match = value;
  1580. entity_match_flag = ENTITY_MATCH_NOT_FOUND;
  1581. }
  1582. int
  1583. get_param_entity_match_flag(void) {
  1584. return entity_match_flag;
  1585. }
  1586. /* Misc handlers */
  1587. void XMLCALL
  1588. xml_decl_handler(void *userData, const XML_Char *version,
  1589. const XML_Char *encoding, int standalone) {
  1590. UNUSED_P(version);
  1591. UNUSED_P(encoding);
  1592. if (userData != g_handler_data)
  1593. fail("User data (xml decl) not correctly set");
  1594. if (standalone != -1)
  1595. fail("Standalone not flagged as not present in XML decl");
  1596. g_xdecl_count++;
  1597. }
  1598. void XMLCALL
  1599. param_check_skip_handler(void *userData, const XML_Char *entityName,
  1600. int is_parameter_entity) {
  1601. UNUSED_P(entityName);
  1602. UNUSED_P(is_parameter_entity);
  1603. if (userData != g_handler_data)
  1604. fail("User data (skip) not correctly set");
  1605. g_skip_count++;
  1606. }
  1607. void XMLCALL
  1608. data_check_comment_handler(void *userData, const XML_Char *data) {
  1609. UNUSED_P(data);
  1610. /* Check that the userData passed through is what we expect */
  1611. if (userData != g_handler_data)
  1612. fail("User data (parser) not correctly set");
  1613. /* Check that the user data in the parser is appropriate */
  1614. if (XML_GetUserData(userData) != (void *)1)
  1615. fail("User data in parser not correctly set");
  1616. g_comment_count++;
  1617. }
  1618. void XMLCALL
  1619. selective_aborting_default_handler(void *userData, const XML_Char *s, int len) {
  1620. const XML_Char trigger_char = *(const XML_Char *)userData;
  1621. int found = 0;
  1622. for (int i = 0; i < len; ++i) {
  1623. if (s[i] == trigger_char) {
  1624. found = 1;
  1625. break;
  1626. }
  1627. }
  1628. if (found) {
  1629. XML_StopParser(g_parser, g_resumable);
  1630. XML_SetDefaultHandler(g_parser, NULL);
  1631. }
  1632. }
  1633. void XMLCALL
  1634. suspending_comment_handler(void *userData, const XML_Char *data) {
  1635. UNUSED_P(data);
  1636. XML_Parser parser = (XML_Parser)userData;
  1637. XML_StopParser(parser, XML_TRUE);
  1638. }
  1639. void XMLCALL
  1640. element_decl_suspender(void *userData, const XML_Char *name,
  1641. XML_Content *model) {
  1642. UNUSED_P(userData);
  1643. UNUSED_P(name);
  1644. XML_StopParser(g_parser, XML_TRUE);
  1645. XML_FreeContentModel(g_parser, model);
  1646. }
  1647. void XMLCALL
  1648. accumulate_pi_characters(void *userData, const XML_Char *target,
  1649. const XML_Char *data) {
  1650. CharData *storage = (CharData *)userData;
  1651. CharData_AppendXMLChars(storage, target, -1);
  1652. CharData_AppendXMLChars(storage, XCS(": "), 2);
  1653. CharData_AppendXMLChars(storage, data, -1);
  1654. CharData_AppendXMLChars(storage, XCS("\n"), 1);
  1655. }
  1656. void XMLCALL
  1657. accumulate_comment(void *userData, const XML_Char *data) {
  1658. CharData *storage = (CharData *)userData;
  1659. CharData_AppendXMLChars(storage, data, -1);
  1660. }
  1661. void XMLCALL
  1662. accumulate_entity_decl(void *userData, const XML_Char *entityName,
  1663. int is_parameter_entity, const XML_Char *value,
  1664. int value_length, const XML_Char *base,
  1665. const XML_Char *systemId, const XML_Char *publicId,
  1666. const XML_Char *notationName) {
  1667. CharData *storage = (CharData *)userData;
  1668. UNUSED_P(is_parameter_entity);
  1669. UNUSED_P(base);
  1670. UNUSED_P(systemId);
  1671. UNUSED_P(publicId);
  1672. UNUSED_P(notationName);
  1673. CharData_AppendXMLChars(storage, entityName, -1);
  1674. CharData_AppendXMLChars(storage, XCS("="), 1);
  1675. if (value == NULL)
  1676. CharData_AppendXMLChars(storage, XCS("(null)"), -1);
  1677. else
  1678. CharData_AppendXMLChars(storage, value, value_length);
  1679. CharData_AppendXMLChars(storage, XCS("\n"), 1);
  1680. }
  1681. void XMLCALL
  1682. accumulate_char_data(void *userData, const XML_Char *s, int len) {
  1683. CharData *const storage = (CharData *)userData;
  1684. CharData_AppendXMLChars(storage, s, len);
  1685. }
  1686. void XMLCALL
  1687. accumulate_start_element(void *userData, const XML_Char *name,
  1688. const XML_Char **atts) {
  1689. CharData *const storage = (CharData *)userData;
  1690. CharData_AppendXMLChars(storage, XCS("("), 1);
  1691. CharData_AppendXMLChars(storage, name, -1);
  1692. if ((atts != NULL) && (atts[0] != NULL)) {
  1693. CharData_AppendXMLChars(storage, XCS("("), 1);
  1694. while (atts[0] != NULL) {
  1695. CharData_AppendXMLChars(storage, atts[0], -1);
  1696. CharData_AppendXMLChars(storage, XCS("="), 1);
  1697. CharData_AppendXMLChars(storage, atts[1], -1);
  1698. atts += 2;
  1699. if (atts[0] != NULL) {
  1700. CharData_AppendXMLChars(storage, XCS(","), 1);
  1701. }
  1702. }
  1703. CharData_AppendXMLChars(storage, XCS(")"), 1);
  1704. }
  1705. CharData_AppendXMLChars(storage, XCS(")\n"), 2);
  1706. }
  1707. void XMLCALL
  1708. checking_default_handler(void *userData, const XML_Char *s, int len) {
  1709. DefaultCheck *data = (DefaultCheck *)userData;
  1710. int i;
  1711. for (i = 0; data[i].expected != NULL; i++) {
  1712. if (data[i].expectedLen == len
  1713. && ! memcmp(data[i].expected, s, len * sizeof(XML_Char))) {
  1714. data[i].seen = XML_TRUE;
  1715. break;
  1716. }
  1717. }
  1718. }
  1719. void XMLCALL
  1720. accumulate_and_suspend_comment_handler(void *userData, const XML_Char *data) {
  1721. ParserPlusStorage *const parserPlusStorage = (ParserPlusStorage *)userData;
  1722. accumulate_comment(parserPlusStorage->storage, data);
  1723. XML_StopParser(parserPlusStorage->parser, XML_TRUE);
  1724. }