xmlrole.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
  10. Copyright (c) 2002 Greg Stein <gstein@users.sourceforge.net>
  11. Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
  12. Copyright (c) 2002-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
  13. Copyright (c) 2005-2009 Steven Solie <steven@solie.ca>
  14. Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org>
  15. Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
  16. Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
  17. Copyright (c) 2021 Donghee Na <donghee.na@python.org>
  18. Licensed under the MIT license:
  19. Permission is hereby granted, free of charge, to any person obtaining
  20. a copy of this software and associated documentation files (the
  21. "Software"), to deal in the Software without restriction, including
  22. without limitation the rights to use, copy, modify, merge, publish,
  23. distribute, sublicense, and/or sell copies of the Software, and to permit
  24. persons to whom the Software is furnished to do so, subject to the
  25. following conditions:
  26. The above copyright notice and this permission notice shall be included
  27. in all copies or substantial portions of the Software.
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  31. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  32. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  33. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  34. USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. */
  36. #include "expat_config.h"
  37. #include <stddef.h>
  38. #ifdef _WIN32
  39. # include "winconfig.h"
  40. #endif
  41. #include "expat_external.h"
  42. #include "internal.h"
  43. #include "xmlrole.h"
  44. #include "ascii.h"
  45. /* Doesn't check:
  46. that ,| are not mixed in a model group
  47. content of literals
  48. */
  49. static const char KW_ANY[] = {ASCII_A, ASCII_N, ASCII_Y, '\0'};
  50. static const char KW_ATTLIST[]
  51. = {ASCII_A, ASCII_T, ASCII_T, ASCII_L, ASCII_I, ASCII_S, ASCII_T, '\0'};
  52. static const char KW_CDATA[]
  53. = {ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0'};
  54. static const char KW_DOCTYPE[]
  55. = {ASCII_D, ASCII_O, ASCII_C, ASCII_T, ASCII_Y, ASCII_P, ASCII_E, '\0'};
  56. static const char KW_ELEMENT[]
  57. = {ASCII_E, ASCII_L, ASCII_E, ASCII_M, ASCII_E, ASCII_N, ASCII_T, '\0'};
  58. static const char KW_EMPTY[]
  59. = {ASCII_E, ASCII_M, ASCII_P, ASCII_T, ASCII_Y, '\0'};
  60. static const char KW_ENTITIES[] = {ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T,
  61. ASCII_I, ASCII_E, ASCII_S, '\0'};
  62. static const char KW_ENTITY[]
  63. = {ASCII_E, ASCII_N, ASCII_T, ASCII_I, ASCII_T, ASCII_Y, '\0'};
  64. static const char KW_FIXED[]
  65. = {ASCII_F, ASCII_I, ASCII_X, ASCII_E, ASCII_D, '\0'};
  66. static const char KW_ID[] = {ASCII_I, ASCII_D, '\0'};
  67. static const char KW_IDREF[]
  68. = {ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, '\0'};
  69. static const char KW_IDREFS[]
  70. = {ASCII_I, ASCII_D, ASCII_R, ASCII_E, ASCII_F, ASCII_S, '\0'};
  71. #ifdef XML_DTD
  72. static const char KW_IGNORE[]
  73. = {ASCII_I, ASCII_G, ASCII_N, ASCII_O, ASCII_R, ASCII_E, '\0'};
  74. #endif
  75. static const char KW_IMPLIED[]
  76. = {ASCII_I, ASCII_M, ASCII_P, ASCII_L, ASCII_I, ASCII_E, ASCII_D, '\0'};
  77. #ifdef XML_DTD
  78. static const char KW_INCLUDE[]
  79. = {ASCII_I, ASCII_N, ASCII_C, ASCII_L, ASCII_U, ASCII_D, ASCII_E, '\0'};
  80. #endif
  81. static const char KW_NDATA[]
  82. = {ASCII_N, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0'};
  83. static const char KW_NMTOKEN[]
  84. = {ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K, ASCII_E, ASCII_N, '\0'};
  85. static const char KW_NMTOKENS[] = {ASCII_N, ASCII_M, ASCII_T, ASCII_O, ASCII_K,
  86. ASCII_E, ASCII_N, ASCII_S, '\0'};
  87. static const char KW_NOTATION[] = {ASCII_N, ASCII_O, ASCII_T, ASCII_A, ASCII_T,
  88. ASCII_I, ASCII_O, ASCII_N, '\0'};
  89. static const char KW_PCDATA[]
  90. = {ASCII_P, ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, '\0'};
  91. static const char KW_PUBLIC[]
  92. = {ASCII_P, ASCII_U, ASCII_B, ASCII_L, ASCII_I, ASCII_C, '\0'};
  93. static const char KW_REQUIRED[] = {ASCII_R, ASCII_E, ASCII_Q, ASCII_U, ASCII_I,
  94. ASCII_R, ASCII_E, ASCII_D, '\0'};
  95. static const char KW_SYSTEM[]
  96. = {ASCII_S, ASCII_Y, ASCII_S, ASCII_T, ASCII_E, ASCII_M, '\0'};
  97. #ifndef MIN_BYTES_PER_CHAR
  98. # define MIN_BYTES_PER_CHAR(enc) ((enc)->minBytesPerChar)
  99. #endif
  100. #ifdef XML_DTD
  101. # define setTopLevel(state) \
  102. ((state)->handler \
  103. = ((state)->documentEntity ? internalSubset : externalSubset1))
  104. #else /* not XML_DTD */
  105. # define setTopLevel(state) ((state)->handler = internalSubset)
  106. #endif /* not XML_DTD */
  107. typedef int PTRCALL PROLOG_HANDLER(PROLOG_STATE *state, int tok,
  108. const char *ptr, const char *end,
  109. const ENCODING *enc);
  110. static PROLOG_HANDLER prolog0, prolog1, prolog2, doctype0, doctype1, doctype2,
  111. doctype3, doctype4, doctype5, internalSubset, entity0, entity1, entity2,
  112. entity3, entity4, entity5, entity6, entity7, entity8, entity9, entity10,
  113. notation0, notation1, notation2, notation3, notation4, attlist0, attlist1,
  114. attlist2, attlist3, attlist4, attlist5, attlist6, attlist7, attlist8,
  115. attlist9, element0, element1, element2, element3, element4, element5,
  116. element6, element7,
  117. #ifdef XML_DTD
  118. externalSubset0, externalSubset1, condSect0, condSect1, condSect2,
  119. #endif /* XML_DTD */
  120. declClose, error;
  121. static int FASTCALL common(PROLOG_STATE *state, int tok);
  122. static int PTRCALL
  123. prolog0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  124. const ENCODING *enc) {
  125. switch (tok) {
  126. case XML_TOK_PROLOG_S:
  127. state->handler = prolog1;
  128. return XML_ROLE_NONE;
  129. case XML_TOK_XML_DECL:
  130. state->handler = prolog1;
  131. return XML_ROLE_XML_DECL;
  132. case XML_TOK_PI:
  133. state->handler = prolog1;
  134. return XML_ROLE_PI;
  135. case XML_TOK_COMMENT:
  136. state->handler = prolog1;
  137. return XML_ROLE_COMMENT;
  138. case XML_TOK_BOM:
  139. return XML_ROLE_NONE;
  140. case XML_TOK_DECL_OPEN:
  141. if (! XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  142. KW_DOCTYPE))
  143. break;
  144. state->handler = doctype0;
  145. return XML_ROLE_DOCTYPE_NONE;
  146. case XML_TOK_INSTANCE_START:
  147. state->handler = error;
  148. return XML_ROLE_INSTANCE_START;
  149. }
  150. return common(state, tok);
  151. }
  152. static int PTRCALL
  153. prolog1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  154. const ENCODING *enc) {
  155. switch (tok) {
  156. case XML_TOK_PROLOG_S:
  157. return XML_ROLE_NONE;
  158. case XML_TOK_PI:
  159. return XML_ROLE_PI;
  160. case XML_TOK_COMMENT:
  161. return XML_ROLE_COMMENT;
  162. case XML_TOK_BOM:
  163. /* This case can never arise. To reach this role function, the
  164. * parse must have passed through prolog0 and therefore have had
  165. * some form of input, even if only a space. At that point, a
  166. * byte order mark is no longer a valid character (though
  167. * technically it should be interpreted as a non-breaking space),
  168. * so will be rejected by the tokenizing stages.
  169. */
  170. return XML_ROLE_NONE; /* LCOV_EXCL_LINE */
  171. case XML_TOK_DECL_OPEN:
  172. if (! XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  173. KW_DOCTYPE))
  174. break;
  175. state->handler = doctype0;
  176. return XML_ROLE_DOCTYPE_NONE;
  177. case XML_TOK_INSTANCE_START:
  178. state->handler = error;
  179. return XML_ROLE_INSTANCE_START;
  180. }
  181. return common(state, tok);
  182. }
  183. static int PTRCALL
  184. prolog2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  185. const ENCODING *enc) {
  186. UNUSED_P(ptr);
  187. UNUSED_P(end);
  188. UNUSED_P(enc);
  189. switch (tok) {
  190. case XML_TOK_PROLOG_S:
  191. return XML_ROLE_NONE;
  192. case XML_TOK_PI:
  193. return XML_ROLE_PI;
  194. case XML_TOK_COMMENT:
  195. return XML_ROLE_COMMENT;
  196. case XML_TOK_INSTANCE_START:
  197. state->handler = error;
  198. return XML_ROLE_INSTANCE_START;
  199. }
  200. return common(state, tok);
  201. }
  202. static int PTRCALL
  203. doctype0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  204. const ENCODING *enc) {
  205. UNUSED_P(ptr);
  206. UNUSED_P(end);
  207. UNUSED_P(enc);
  208. switch (tok) {
  209. case XML_TOK_PROLOG_S:
  210. return XML_ROLE_DOCTYPE_NONE;
  211. case XML_TOK_NAME:
  212. case XML_TOK_PREFIXED_NAME:
  213. state->handler = doctype1;
  214. return XML_ROLE_DOCTYPE_NAME;
  215. }
  216. return common(state, tok);
  217. }
  218. static int PTRCALL
  219. doctype1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  220. const ENCODING *enc) {
  221. switch (tok) {
  222. case XML_TOK_PROLOG_S:
  223. return XML_ROLE_DOCTYPE_NONE;
  224. case XML_TOK_OPEN_BRACKET:
  225. state->handler = internalSubset;
  226. return XML_ROLE_DOCTYPE_INTERNAL_SUBSET;
  227. case XML_TOK_DECL_CLOSE:
  228. state->handler = prolog2;
  229. return XML_ROLE_DOCTYPE_CLOSE;
  230. case XML_TOK_NAME:
  231. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  232. state->handler = doctype3;
  233. return XML_ROLE_DOCTYPE_NONE;
  234. }
  235. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  236. state->handler = doctype2;
  237. return XML_ROLE_DOCTYPE_NONE;
  238. }
  239. break;
  240. }
  241. return common(state, tok);
  242. }
  243. static int PTRCALL
  244. doctype2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  245. const ENCODING *enc) {
  246. UNUSED_P(ptr);
  247. UNUSED_P(end);
  248. UNUSED_P(enc);
  249. switch (tok) {
  250. case XML_TOK_PROLOG_S:
  251. return XML_ROLE_DOCTYPE_NONE;
  252. case XML_TOK_LITERAL:
  253. state->handler = doctype3;
  254. return XML_ROLE_DOCTYPE_PUBLIC_ID;
  255. }
  256. return common(state, tok);
  257. }
  258. static int PTRCALL
  259. doctype3(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  260. const ENCODING *enc) {
  261. UNUSED_P(ptr);
  262. UNUSED_P(end);
  263. UNUSED_P(enc);
  264. switch (tok) {
  265. case XML_TOK_PROLOG_S:
  266. return XML_ROLE_DOCTYPE_NONE;
  267. case XML_TOK_LITERAL:
  268. state->handler = doctype4;
  269. return XML_ROLE_DOCTYPE_SYSTEM_ID;
  270. }
  271. return common(state, tok);
  272. }
  273. static int PTRCALL
  274. doctype4(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  275. const ENCODING *enc) {
  276. UNUSED_P(ptr);
  277. UNUSED_P(end);
  278. UNUSED_P(enc);
  279. switch (tok) {
  280. case XML_TOK_PROLOG_S:
  281. return XML_ROLE_DOCTYPE_NONE;
  282. case XML_TOK_OPEN_BRACKET:
  283. state->handler = internalSubset;
  284. return XML_ROLE_DOCTYPE_INTERNAL_SUBSET;
  285. case XML_TOK_DECL_CLOSE:
  286. state->handler = prolog2;
  287. return XML_ROLE_DOCTYPE_CLOSE;
  288. }
  289. return common(state, tok);
  290. }
  291. static int PTRCALL
  292. doctype5(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  293. const ENCODING *enc) {
  294. UNUSED_P(ptr);
  295. UNUSED_P(end);
  296. UNUSED_P(enc);
  297. switch (tok) {
  298. case XML_TOK_PROLOG_S:
  299. return XML_ROLE_DOCTYPE_NONE;
  300. case XML_TOK_DECL_CLOSE:
  301. state->handler = prolog2;
  302. return XML_ROLE_DOCTYPE_CLOSE;
  303. }
  304. return common(state, tok);
  305. }
  306. static int PTRCALL
  307. internalSubset(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  308. const ENCODING *enc) {
  309. switch (tok) {
  310. case XML_TOK_PROLOG_S:
  311. return XML_ROLE_NONE;
  312. case XML_TOK_DECL_OPEN:
  313. if (XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  314. KW_ENTITY)) {
  315. state->handler = entity0;
  316. return XML_ROLE_ENTITY_NONE;
  317. }
  318. if (XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  319. KW_ATTLIST)) {
  320. state->handler = attlist0;
  321. return XML_ROLE_ATTLIST_NONE;
  322. }
  323. if (XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  324. KW_ELEMENT)) {
  325. state->handler = element0;
  326. return XML_ROLE_ELEMENT_NONE;
  327. }
  328. if (XmlNameMatchesAscii(enc, ptr + 2 * MIN_BYTES_PER_CHAR(enc), end,
  329. KW_NOTATION)) {
  330. state->handler = notation0;
  331. return XML_ROLE_NOTATION_NONE;
  332. }
  333. break;
  334. case XML_TOK_PI:
  335. return XML_ROLE_PI;
  336. case XML_TOK_COMMENT:
  337. return XML_ROLE_COMMENT;
  338. case XML_TOK_PARAM_ENTITY_REF:
  339. return XML_ROLE_PARAM_ENTITY_REF;
  340. case XML_TOK_CLOSE_BRACKET:
  341. state->handler = doctype5;
  342. return XML_ROLE_DOCTYPE_NONE;
  343. case XML_TOK_NONE:
  344. return XML_ROLE_NONE;
  345. }
  346. return common(state, tok);
  347. }
  348. #ifdef XML_DTD
  349. static int PTRCALL
  350. externalSubset0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  351. const ENCODING *enc) {
  352. state->handler = externalSubset1;
  353. if (tok == XML_TOK_XML_DECL)
  354. return XML_ROLE_TEXT_DECL;
  355. return externalSubset1(state, tok, ptr, end, enc);
  356. }
  357. static int PTRCALL
  358. externalSubset1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  359. const ENCODING *enc) {
  360. switch (tok) {
  361. case XML_TOK_COND_SECT_OPEN:
  362. state->handler = condSect0;
  363. return XML_ROLE_NONE;
  364. case XML_TOK_COND_SECT_CLOSE:
  365. if (state->includeLevel == 0)
  366. break;
  367. state->includeLevel -= 1;
  368. return XML_ROLE_NONE;
  369. case XML_TOK_PROLOG_S:
  370. return XML_ROLE_NONE;
  371. case XML_TOK_CLOSE_BRACKET:
  372. break;
  373. case XML_TOK_NONE:
  374. if (state->includeLevel)
  375. break;
  376. return XML_ROLE_NONE;
  377. default:
  378. return internalSubset(state, tok, ptr, end, enc);
  379. }
  380. return common(state, tok);
  381. }
  382. #endif /* XML_DTD */
  383. static int PTRCALL
  384. entity0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  385. const ENCODING *enc) {
  386. UNUSED_P(ptr);
  387. UNUSED_P(end);
  388. UNUSED_P(enc);
  389. switch (tok) {
  390. case XML_TOK_PROLOG_S:
  391. return XML_ROLE_ENTITY_NONE;
  392. case XML_TOK_PERCENT:
  393. state->handler = entity1;
  394. return XML_ROLE_ENTITY_NONE;
  395. case XML_TOK_NAME:
  396. state->handler = entity2;
  397. return XML_ROLE_GENERAL_ENTITY_NAME;
  398. }
  399. return common(state, tok);
  400. }
  401. static int PTRCALL
  402. entity1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  403. const ENCODING *enc) {
  404. UNUSED_P(ptr);
  405. UNUSED_P(end);
  406. UNUSED_P(enc);
  407. switch (tok) {
  408. case XML_TOK_PROLOG_S:
  409. return XML_ROLE_ENTITY_NONE;
  410. case XML_TOK_NAME:
  411. state->handler = entity7;
  412. return XML_ROLE_PARAM_ENTITY_NAME;
  413. }
  414. return common(state, tok);
  415. }
  416. static int PTRCALL
  417. entity2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  418. const ENCODING *enc) {
  419. switch (tok) {
  420. case XML_TOK_PROLOG_S:
  421. return XML_ROLE_ENTITY_NONE;
  422. case XML_TOK_NAME:
  423. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  424. state->handler = entity4;
  425. return XML_ROLE_ENTITY_NONE;
  426. }
  427. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  428. state->handler = entity3;
  429. return XML_ROLE_ENTITY_NONE;
  430. }
  431. break;
  432. case XML_TOK_LITERAL:
  433. state->handler = declClose;
  434. state->role_none = XML_ROLE_ENTITY_NONE;
  435. return XML_ROLE_ENTITY_VALUE;
  436. }
  437. return common(state, tok);
  438. }
  439. static int PTRCALL
  440. entity3(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  441. const ENCODING *enc) {
  442. UNUSED_P(ptr);
  443. UNUSED_P(end);
  444. UNUSED_P(enc);
  445. switch (tok) {
  446. case XML_TOK_PROLOG_S:
  447. return XML_ROLE_ENTITY_NONE;
  448. case XML_TOK_LITERAL:
  449. state->handler = entity4;
  450. return XML_ROLE_ENTITY_PUBLIC_ID;
  451. }
  452. return common(state, tok);
  453. }
  454. static int PTRCALL
  455. entity4(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  456. const ENCODING *enc) {
  457. UNUSED_P(ptr);
  458. UNUSED_P(end);
  459. UNUSED_P(enc);
  460. switch (tok) {
  461. case XML_TOK_PROLOG_S:
  462. return XML_ROLE_ENTITY_NONE;
  463. case XML_TOK_LITERAL:
  464. state->handler = entity5;
  465. return XML_ROLE_ENTITY_SYSTEM_ID;
  466. }
  467. return common(state, tok);
  468. }
  469. static int PTRCALL
  470. entity5(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  471. const ENCODING *enc) {
  472. switch (tok) {
  473. case XML_TOK_PROLOG_S:
  474. return XML_ROLE_ENTITY_NONE;
  475. case XML_TOK_DECL_CLOSE:
  476. setTopLevel(state);
  477. return XML_ROLE_ENTITY_COMPLETE;
  478. case XML_TOK_NAME:
  479. if (XmlNameMatchesAscii(enc, ptr, end, KW_NDATA)) {
  480. state->handler = entity6;
  481. return XML_ROLE_ENTITY_NONE;
  482. }
  483. break;
  484. }
  485. return common(state, tok);
  486. }
  487. static int PTRCALL
  488. entity6(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  489. const ENCODING *enc) {
  490. UNUSED_P(ptr);
  491. UNUSED_P(end);
  492. UNUSED_P(enc);
  493. switch (tok) {
  494. case XML_TOK_PROLOG_S:
  495. return XML_ROLE_ENTITY_NONE;
  496. case XML_TOK_NAME:
  497. state->handler = declClose;
  498. state->role_none = XML_ROLE_ENTITY_NONE;
  499. return XML_ROLE_ENTITY_NOTATION_NAME;
  500. }
  501. return common(state, tok);
  502. }
  503. static int PTRCALL
  504. entity7(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  505. const ENCODING *enc) {
  506. switch (tok) {
  507. case XML_TOK_PROLOG_S:
  508. return XML_ROLE_ENTITY_NONE;
  509. case XML_TOK_NAME:
  510. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  511. state->handler = entity9;
  512. return XML_ROLE_ENTITY_NONE;
  513. }
  514. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  515. state->handler = entity8;
  516. return XML_ROLE_ENTITY_NONE;
  517. }
  518. break;
  519. case XML_TOK_LITERAL:
  520. state->handler = declClose;
  521. state->role_none = XML_ROLE_ENTITY_NONE;
  522. return XML_ROLE_ENTITY_VALUE;
  523. }
  524. return common(state, tok);
  525. }
  526. static int PTRCALL
  527. entity8(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  528. const ENCODING *enc) {
  529. UNUSED_P(ptr);
  530. UNUSED_P(end);
  531. UNUSED_P(enc);
  532. switch (tok) {
  533. case XML_TOK_PROLOG_S:
  534. return XML_ROLE_ENTITY_NONE;
  535. case XML_TOK_LITERAL:
  536. state->handler = entity9;
  537. return XML_ROLE_ENTITY_PUBLIC_ID;
  538. }
  539. return common(state, tok);
  540. }
  541. static int PTRCALL
  542. entity9(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  543. const ENCODING *enc) {
  544. UNUSED_P(ptr);
  545. UNUSED_P(end);
  546. UNUSED_P(enc);
  547. switch (tok) {
  548. case XML_TOK_PROLOG_S:
  549. return XML_ROLE_ENTITY_NONE;
  550. case XML_TOK_LITERAL:
  551. state->handler = entity10;
  552. return XML_ROLE_ENTITY_SYSTEM_ID;
  553. }
  554. return common(state, tok);
  555. }
  556. static int PTRCALL
  557. entity10(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  558. const ENCODING *enc) {
  559. UNUSED_P(ptr);
  560. UNUSED_P(end);
  561. UNUSED_P(enc);
  562. switch (tok) {
  563. case XML_TOK_PROLOG_S:
  564. return XML_ROLE_ENTITY_NONE;
  565. case XML_TOK_DECL_CLOSE:
  566. setTopLevel(state);
  567. return XML_ROLE_ENTITY_COMPLETE;
  568. }
  569. return common(state, tok);
  570. }
  571. static int PTRCALL
  572. notation0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  573. const ENCODING *enc) {
  574. UNUSED_P(ptr);
  575. UNUSED_P(end);
  576. UNUSED_P(enc);
  577. switch (tok) {
  578. case XML_TOK_PROLOG_S:
  579. return XML_ROLE_NOTATION_NONE;
  580. case XML_TOK_NAME:
  581. state->handler = notation1;
  582. return XML_ROLE_NOTATION_NAME;
  583. }
  584. return common(state, tok);
  585. }
  586. static int PTRCALL
  587. notation1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  588. const ENCODING *enc) {
  589. switch (tok) {
  590. case XML_TOK_PROLOG_S:
  591. return XML_ROLE_NOTATION_NONE;
  592. case XML_TOK_NAME:
  593. if (XmlNameMatchesAscii(enc, ptr, end, KW_SYSTEM)) {
  594. state->handler = notation3;
  595. return XML_ROLE_NOTATION_NONE;
  596. }
  597. if (XmlNameMatchesAscii(enc, ptr, end, KW_PUBLIC)) {
  598. state->handler = notation2;
  599. return XML_ROLE_NOTATION_NONE;
  600. }
  601. break;
  602. }
  603. return common(state, tok);
  604. }
  605. static int PTRCALL
  606. notation2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  607. const ENCODING *enc) {
  608. UNUSED_P(ptr);
  609. UNUSED_P(end);
  610. UNUSED_P(enc);
  611. switch (tok) {
  612. case XML_TOK_PROLOG_S:
  613. return XML_ROLE_NOTATION_NONE;
  614. case XML_TOK_LITERAL:
  615. state->handler = notation4;
  616. return XML_ROLE_NOTATION_PUBLIC_ID;
  617. }
  618. return common(state, tok);
  619. }
  620. static int PTRCALL
  621. notation3(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  622. const ENCODING *enc) {
  623. UNUSED_P(ptr);
  624. UNUSED_P(end);
  625. UNUSED_P(enc);
  626. switch (tok) {
  627. case XML_TOK_PROLOG_S:
  628. return XML_ROLE_NOTATION_NONE;
  629. case XML_TOK_LITERAL:
  630. state->handler = declClose;
  631. state->role_none = XML_ROLE_NOTATION_NONE;
  632. return XML_ROLE_NOTATION_SYSTEM_ID;
  633. }
  634. return common(state, tok);
  635. }
  636. static int PTRCALL
  637. notation4(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  638. const ENCODING *enc) {
  639. UNUSED_P(ptr);
  640. UNUSED_P(end);
  641. UNUSED_P(enc);
  642. switch (tok) {
  643. case XML_TOK_PROLOG_S:
  644. return XML_ROLE_NOTATION_NONE;
  645. case XML_TOK_LITERAL:
  646. state->handler = declClose;
  647. state->role_none = XML_ROLE_NOTATION_NONE;
  648. return XML_ROLE_NOTATION_SYSTEM_ID;
  649. case XML_TOK_DECL_CLOSE:
  650. setTopLevel(state);
  651. return XML_ROLE_NOTATION_NO_SYSTEM_ID;
  652. }
  653. return common(state, tok);
  654. }
  655. static int PTRCALL
  656. attlist0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  657. const ENCODING *enc) {
  658. UNUSED_P(ptr);
  659. UNUSED_P(end);
  660. UNUSED_P(enc);
  661. switch (tok) {
  662. case XML_TOK_PROLOG_S:
  663. return XML_ROLE_ATTLIST_NONE;
  664. case XML_TOK_NAME:
  665. case XML_TOK_PREFIXED_NAME:
  666. state->handler = attlist1;
  667. return XML_ROLE_ATTLIST_ELEMENT_NAME;
  668. }
  669. return common(state, tok);
  670. }
  671. static int PTRCALL
  672. attlist1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  673. const ENCODING *enc) {
  674. UNUSED_P(ptr);
  675. UNUSED_P(end);
  676. UNUSED_P(enc);
  677. switch (tok) {
  678. case XML_TOK_PROLOG_S:
  679. return XML_ROLE_ATTLIST_NONE;
  680. case XML_TOK_DECL_CLOSE:
  681. setTopLevel(state);
  682. return XML_ROLE_ATTLIST_NONE;
  683. case XML_TOK_NAME:
  684. case XML_TOK_PREFIXED_NAME:
  685. state->handler = attlist2;
  686. return XML_ROLE_ATTRIBUTE_NAME;
  687. }
  688. return common(state, tok);
  689. }
  690. static int PTRCALL
  691. attlist2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  692. const ENCODING *enc) {
  693. switch (tok) {
  694. case XML_TOK_PROLOG_S:
  695. return XML_ROLE_ATTLIST_NONE;
  696. case XML_TOK_NAME: {
  697. static const char *const types[] = {
  698. KW_CDATA, KW_ID, KW_IDREF, KW_IDREFS,
  699. KW_ENTITY, KW_ENTITIES, KW_NMTOKEN, KW_NMTOKENS,
  700. };
  701. int i;
  702. for (i = 0; i < (int)(sizeof(types) / sizeof(types[0])); i++)
  703. if (XmlNameMatchesAscii(enc, ptr, end, types[i])) {
  704. state->handler = attlist8;
  705. return XML_ROLE_ATTRIBUTE_TYPE_CDATA + i;
  706. }
  707. }
  708. if (XmlNameMatchesAscii(enc, ptr, end, KW_NOTATION)) {
  709. state->handler = attlist5;
  710. return XML_ROLE_ATTLIST_NONE;
  711. }
  712. break;
  713. case XML_TOK_OPEN_PAREN:
  714. state->handler = attlist3;
  715. return XML_ROLE_ATTLIST_NONE;
  716. }
  717. return common(state, tok);
  718. }
  719. static int PTRCALL
  720. attlist3(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  721. const ENCODING *enc) {
  722. UNUSED_P(ptr);
  723. UNUSED_P(end);
  724. UNUSED_P(enc);
  725. switch (tok) {
  726. case XML_TOK_PROLOG_S:
  727. return XML_ROLE_ATTLIST_NONE;
  728. case XML_TOK_NMTOKEN:
  729. case XML_TOK_NAME:
  730. case XML_TOK_PREFIXED_NAME:
  731. state->handler = attlist4;
  732. return XML_ROLE_ATTRIBUTE_ENUM_VALUE;
  733. }
  734. return common(state, tok);
  735. }
  736. static int PTRCALL
  737. attlist4(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  738. const ENCODING *enc) {
  739. UNUSED_P(ptr);
  740. UNUSED_P(end);
  741. UNUSED_P(enc);
  742. switch (tok) {
  743. case XML_TOK_PROLOG_S:
  744. return XML_ROLE_ATTLIST_NONE;
  745. case XML_TOK_CLOSE_PAREN:
  746. state->handler = attlist8;
  747. return XML_ROLE_ATTLIST_NONE;
  748. case XML_TOK_OR:
  749. state->handler = attlist3;
  750. return XML_ROLE_ATTLIST_NONE;
  751. }
  752. return common(state, tok);
  753. }
  754. static int PTRCALL
  755. attlist5(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  756. const ENCODING *enc) {
  757. UNUSED_P(ptr);
  758. UNUSED_P(end);
  759. UNUSED_P(enc);
  760. switch (tok) {
  761. case XML_TOK_PROLOG_S:
  762. return XML_ROLE_ATTLIST_NONE;
  763. case XML_TOK_OPEN_PAREN:
  764. state->handler = attlist6;
  765. return XML_ROLE_ATTLIST_NONE;
  766. }
  767. return common(state, tok);
  768. }
  769. static int PTRCALL
  770. attlist6(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  771. const ENCODING *enc) {
  772. UNUSED_P(ptr);
  773. UNUSED_P(end);
  774. UNUSED_P(enc);
  775. switch (tok) {
  776. case XML_TOK_PROLOG_S:
  777. return XML_ROLE_ATTLIST_NONE;
  778. case XML_TOK_NAME:
  779. state->handler = attlist7;
  780. return XML_ROLE_ATTRIBUTE_NOTATION_VALUE;
  781. }
  782. return common(state, tok);
  783. }
  784. static int PTRCALL
  785. attlist7(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  786. const ENCODING *enc) {
  787. UNUSED_P(ptr);
  788. UNUSED_P(end);
  789. UNUSED_P(enc);
  790. switch (tok) {
  791. case XML_TOK_PROLOG_S:
  792. return XML_ROLE_ATTLIST_NONE;
  793. case XML_TOK_CLOSE_PAREN:
  794. state->handler = attlist8;
  795. return XML_ROLE_ATTLIST_NONE;
  796. case XML_TOK_OR:
  797. state->handler = attlist6;
  798. return XML_ROLE_ATTLIST_NONE;
  799. }
  800. return common(state, tok);
  801. }
  802. /* default value */
  803. static int PTRCALL
  804. attlist8(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  805. const ENCODING *enc) {
  806. switch (tok) {
  807. case XML_TOK_PROLOG_S:
  808. return XML_ROLE_ATTLIST_NONE;
  809. case XML_TOK_POUND_NAME:
  810. if (XmlNameMatchesAscii(enc, ptr + MIN_BYTES_PER_CHAR(enc), end,
  811. KW_IMPLIED)) {
  812. state->handler = attlist1;
  813. return XML_ROLE_IMPLIED_ATTRIBUTE_VALUE;
  814. }
  815. if (XmlNameMatchesAscii(enc, ptr + MIN_BYTES_PER_CHAR(enc), end,
  816. KW_REQUIRED)) {
  817. state->handler = attlist1;
  818. return XML_ROLE_REQUIRED_ATTRIBUTE_VALUE;
  819. }
  820. if (XmlNameMatchesAscii(enc, ptr + MIN_BYTES_PER_CHAR(enc), end,
  821. KW_FIXED)) {
  822. state->handler = attlist9;
  823. return XML_ROLE_ATTLIST_NONE;
  824. }
  825. break;
  826. case XML_TOK_LITERAL:
  827. state->handler = attlist1;
  828. return XML_ROLE_DEFAULT_ATTRIBUTE_VALUE;
  829. }
  830. return common(state, tok);
  831. }
  832. static int PTRCALL
  833. attlist9(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  834. const ENCODING *enc) {
  835. UNUSED_P(ptr);
  836. UNUSED_P(end);
  837. UNUSED_P(enc);
  838. switch (tok) {
  839. case XML_TOK_PROLOG_S:
  840. return XML_ROLE_ATTLIST_NONE;
  841. case XML_TOK_LITERAL:
  842. state->handler = attlist1;
  843. return XML_ROLE_FIXED_ATTRIBUTE_VALUE;
  844. }
  845. return common(state, tok);
  846. }
  847. static int PTRCALL
  848. element0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  849. const ENCODING *enc) {
  850. UNUSED_P(ptr);
  851. UNUSED_P(end);
  852. UNUSED_P(enc);
  853. switch (tok) {
  854. case XML_TOK_PROLOG_S:
  855. return XML_ROLE_ELEMENT_NONE;
  856. case XML_TOK_NAME:
  857. case XML_TOK_PREFIXED_NAME:
  858. state->handler = element1;
  859. return XML_ROLE_ELEMENT_NAME;
  860. }
  861. return common(state, tok);
  862. }
  863. static int PTRCALL
  864. element1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  865. const ENCODING *enc) {
  866. switch (tok) {
  867. case XML_TOK_PROLOG_S:
  868. return XML_ROLE_ELEMENT_NONE;
  869. case XML_TOK_NAME:
  870. if (XmlNameMatchesAscii(enc, ptr, end, KW_EMPTY)) {
  871. state->handler = declClose;
  872. state->role_none = XML_ROLE_ELEMENT_NONE;
  873. return XML_ROLE_CONTENT_EMPTY;
  874. }
  875. if (XmlNameMatchesAscii(enc, ptr, end, KW_ANY)) {
  876. state->handler = declClose;
  877. state->role_none = XML_ROLE_ELEMENT_NONE;
  878. return XML_ROLE_CONTENT_ANY;
  879. }
  880. break;
  881. case XML_TOK_OPEN_PAREN:
  882. state->handler = element2;
  883. state->level = 1;
  884. return XML_ROLE_GROUP_OPEN;
  885. }
  886. return common(state, tok);
  887. }
  888. static int PTRCALL
  889. element2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  890. const ENCODING *enc) {
  891. switch (tok) {
  892. case XML_TOK_PROLOG_S:
  893. return XML_ROLE_ELEMENT_NONE;
  894. case XML_TOK_POUND_NAME:
  895. if (XmlNameMatchesAscii(enc, ptr + MIN_BYTES_PER_CHAR(enc), end,
  896. KW_PCDATA)) {
  897. state->handler = element3;
  898. return XML_ROLE_CONTENT_PCDATA;
  899. }
  900. break;
  901. case XML_TOK_OPEN_PAREN:
  902. state->level = 2;
  903. state->handler = element6;
  904. return XML_ROLE_GROUP_OPEN;
  905. case XML_TOK_NAME:
  906. case XML_TOK_PREFIXED_NAME:
  907. state->handler = element7;
  908. return XML_ROLE_CONTENT_ELEMENT;
  909. case XML_TOK_NAME_QUESTION:
  910. state->handler = element7;
  911. return XML_ROLE_CONTENT_ELEMENT_OPT;
  912. case XML_TOK_NAME_ASTERISK:
  913. state->handler = element7;
  914. return XML_ROLE_CONTENT_ELEMENT_REP;
  915. case XML_TOK_NAME_PLUS:
  916. state->handler = element7;
  917. return XML_ROLE_CONTENT_ELEMENT_PLUS;
  918. }
  919. return common(state, tok);
  920. }
  921. static int PTRCALL
  922. element3(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  923. const ENCODING *enc) {
  924. UNUSED_P(ptr);
  925. UNUSED_P(end);
  926. UNUSED_P(enc);
  927. switch (tok) {
  928. case XML_TOK_PROLOG_S:
  929. return XML_ROLE_ELEMENT_NONE;
  930. case XML_TOK_CLOSE_PAREN:
  931. state->handler = declClose;
  932. state->role_none = XML_ROLE_ELEMENT_NONE;
  933. return XML_ROLE_GROUP_CLOSE;
  934. case XML_TOK_CLOSE_PAREN_ASTERISK:
  935. state->handler = declClose;
  936. state->role_none = XML_ROLE_ELEMENT_NONE;
  937. return XML_ROLE_GROUP_CLOSE_REP;
  938. case XML_TOK_OR:
  939. state->handler = element4;
  940. return XML_ROLE_ELEMENT_NONE;
  941. }
  942. return common(state, tok);
  943. }
  944. static int PTRCALL
  945. element4(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  946. const ENCODING *enc) {
  947. UNUSED_P(ptr);
  948. UNUSED_P(end);
  949. UNUSED_P(enc);
  950. switch (tok) {
  951. case XML_TOK_PROLOG_S:
  952. return XML_ROLE_ELEMENT_NONE;
  953. case XML_TOK_NAME:
  954. case XML_TOK_PREFIXED_NAME:
  955. state->handler = element5;
  956. return XML_ROLE_CONTENT_ELEMENT;
  957. }
  958. return common(state, tok);
  959. }
  960. static int PTRCALL
  961. element5(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  962. const ENCODING *enc) {
  963. UNUSED_P(ptr);
  964. UNUSED_P(end);
  965. UNUSED_P(enc);
  966. switch (tok) {
  967. case XML_TOK_PROLOG_S:
  968. return XML_ROLE_ELEMENT_NONE;
  969. case XML_TOK_CLOSE_PAREN_ASTERISK:
  970. state->handler = declClose;
  971. state->role_none = XML_ROLE_ELEMENT_NONE;
  972. return XML_ROLE_GROUP_CLOSE_REP;
  973. case XML_TOK_OR:
  974. state->handler = element4;
  975. return XML_ROLE_ELEMENT_NONE;
  976. }
  977. return common(state, tok);
  978. }
  979. static int PTRCALL
  980. element6(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  981. const ENCODING *enc) {
  982. UNUSED_P(ptr);
  983. UNUSED_P(end);
  984. UNUSED_P(enc);
  985. switch (tok) {
  986. case XML_TOK_PROLOG_S:
  987. return XML_ROLE_ELEMENT_NONE;
  988. case XML_TOK_OPEN_PAREN:
  989. state->level += 1;
  990. return XML_ROLE_GROUP_OPEN;
  991. case XML_TOK_NAME:
  992. case XML_TOK_PREFIXED_NAME:
  993. state->handler = element7;
  994. return XML_ROLE_CONTENT_ELEMENT;
  995. case XML_TOK_NAME_QUESTION:
  996. state->handler = element7;
  997. return XML_ROLE_CONTENT_ELEMENT_OPT;
  998. case XML_TOK_NAME_ASTERISK:
  999. state->handler = element7;
  1000. return XML_ROLE_CONTENT_ELEMENT_REP;
  1001. case XML_TOK_NAME_PLUS:
  1002. state->handler = element7;
  1003. return XML_ROLE_CONTENT_ELEMENT_PLUS;
  1004. }
  1005. return common(state, tok);
  1006. }
  1007. static int PTRCALL
  1008. element7(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1009. const ENCODING *enc) {
  1010. UNUSED_P(ptr);
  1011. UNUSED_P(end);
  1012. UNUSED_P(enc);
  1013. switch (tok) {
  1014. case XML_TOK_PROLOG_S:
  1015. return XML_ROLE_ELEMENT_NONE;
  1016. case XML_TOK_CLOSE_PAREN:
  1017. state->level -= 1;
  1018. if (state->level == 0) {
  1019. state->handler = declClose;
  1020. state->role_none = XML_ROLE_ELEMENT_NONE;
  1021. }
  1022. return XML_ROLE_GROUP_CLOSE;
  1023. case XML_TOK_CLOSE_PAREN_ASTERISK:
  1024. state->level -= 1;
  1025. if (state->level == 0) {
  1026. state->handler = declClose;
  1027. state->role_none = XML_ROLE_ELEMENT_NONE;
  1028. }
  1029. return XML_ROLE_GROUP_CLOSE_REP;
  1030. case XML_TOK_CLOSE_PAREN_QUESTION:
  1031. state->level -= 1;
  1032. if (state->level == 0) {
  1033. state->handler = declClose;
  1034. state->role_none = XML_ROLE_ELEMENT_NONE;
  1035. }
  1036. return XML_ROLE_GROUP_CLOSE_OPT;
  1037. case XML_TOK_CLOSE_PAREN_PLUS:
  1038. state->level -= 1;
  1039. if (state->level == 0) {
  1040. state->handler = declClose;
  1041. state->role_none = XML_ROLE_ELEMENT_NONE;
  1042. }
  1043. return XML_ROLE_GROUP_CLOSE_PLUS;
  1044. case XML_TOK_COMMA:
  1045. state->handler = element6;
  1046. return XML_ROLE_GROUP_SEQUENCE;
  1047. case XML_TOK_OR:
  1048. state->handler = element6;
  1049. return XML_ROLE_GROUP_CHOICE;
  1050. }
  1051. return common(state, tok);
  1052. }
  1053. #ifdef XML_DTD
  1054. static int PTRCALL
  1055. condSect0(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1056. const ENCODING *enc) {
  1057. switch (tok) {
  1058. case XML_TOK_PROLOG_S:
  1059. return XML_ROLE_NONE;
  1060. case XML_TOK_NAME:
  1061. if (XmlNameMatchesAscii(enc, ptr, end, KW_INCLUDE)) {
  1062. state->handler = condSect1;
  1063. return XML_ROLE_NONE;
  1064. }
  1065. if (XmlNameMatchesAscii(enc, ptr, end, KW_IGNORE)) {
  1066. state->handler = condSect2;
  1067. return XML_ROLE_NONE;
  1068. }
  1069. break;
  1070. }
  1071. return common(state, tok);
  1072. }
  1073. static int PTRCALL
  1074. condSect1(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1075. const ENCODING *enc) {
  1076. UNUSED_P(ptr);
  1077. UNUSED_P(end);
  1078. UNUSED_P(enc);
  1079. switch (tok) {
  1080. case XML_TOK_PROLOG_S:
  1081. return XML_ROLE_NONE;
  1082. case XML_TOK_OPEN_BRACKET:
  1083. state->handler = externalSubset1;
  1084. state->includeLevel += 1;
  1085. return XML_ROLE_NONE;
  1086. }
  1087. return common(state, tok);
  1088. }
  1089. static int PTRCALL
  1090. condSect2(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1091. const ENCODING *enc) {
  1092. UNUSED_P(ptr);
  1093. UNUSED_P(end);
  1094. UNUSED_P(enc);
  1095. switch (tok) {
  1096. case XML_TOK_PROLOG_S:
  1097. return XML_ROLE_NONE;
  1098. case XML_TOK_OPEN_BRACKET:
  1099. state->handler = externalSubset1;
  1100. return XML_ROLE_IGNORE_SECT;
  1101. }
  1102. return common(state, tok);
  1103. }
  1104. #endif /* XML_DTD */
  1105. static int PTRCALL
  1106. declClose(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1107. const ENCODING *enc) {
  1108. UNUSED_P(ptr);
  1109. UNUSED_P(end);
  1110. UNUSED_P(enc);
  1111. switch (tok) {
  1112. case XML_TOK_PROLOG_S:
  1113. return state->role_none;
  1114. case XML_TOK_DECL_CLOSE:
  1115. setTopLevel(state);
  1116. return state->role_none;
  1117. }
  1118. return common(state, tok);
  1119. }
  1120. /* This function will only be invoked if the internal logic of the
  1121. * parser has broken down. It is used in two cases:
  1122. *
  1123. * 1: When the XML prolog has been finished. At this point the
  1124. * processor (the parser level above these role handlers) should
  1125. * switch from prologProcessor to contentProcessor and reinitialise
  1126. * the handler function.
  1127. *
  1128. * 2: When an error has been detected (via common() below). At this
  1129. * point again the processor should be switched to errorProcessor,
  1130. * which will never call a handler.
  1131. *
  1132. * The result of this is that error() can only be called if the
  1133. * processor switch failed to happen, which is an internal error and
  1134. * therefore we shouldn't be able to provoke it simply by using the
  1135. * library. It is a necessary backstop, however, so we merely exclude
  1136. * it from the coverage statistics.
  1137. *
  1138. * LCOV_EXCL_START
  1139. */
  1140. static int PTRCALL
  1141. error(PROLOG_STATE *state, int tok, const char *ptr, const char *end,
  1142. const ENCODING *enc) {
  1143. UNUSED_P(state);
  1144. UNUSED_P(tok);
  1145. UNUSED_P(ptr);
  1146. UNUSED_P(end);
  1147. UNUSED_P(enc);
  1148. return XML_ROLE_NONE;
  1149. }
  1150. /* LCOV_EXCL_STOP */
  1151. static int FASTCALL
  1152. common(PROLOG_STATE *state, int tok) {
  1153. #ifdef XML_DTD
  1154. if (! state->documentEntity && tok == XML_TOK_PARAM_ENTITY_REF)
  1155. return XML_ROLE_INNER_PARAM_ENTITY_REF;
  1156. #else
  1157. UNUSED_P(tok);
  1158. #endif
  1159. state->handler = error;
  1160. return XML_ROLE_ERROR;
  1161. }
  1162. void
  1163. XmlPrologStateInit(PROLOG_STATE *state) {
  1164. state->handler = prolog0;
  1165. #ifdef XML_DTD
  1166. state->documentEntity = 1;
  1167. state->includeLevel = 0;
  1168. state->inEntityValue = 0;
  1169. #endif /* XML_DTD */
  1170. }
  1171. #ifdef XML_DTD
  1172. void
  1173. XmlPrologStateInitExternalEntity(PROLOG_STATE *state) {
  1174. state->handler = externalSubset0;
  1175. state->documentEntity = 0;
  1176. state->includeLevel = 0;
  1177. }
  1178. #endif /* XML_DTD */