handlers.c 65 KB

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