xmlrole.c 35 KB

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