xmltok_impl.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /* This file is included (from xmltok.c, 1-3 times depending on XML_MIN_SIZE)!
  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 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
  11. Copyright (c) 2002-2016 Karl Waclawek <karl@waclawek.net>
  12. Copyright (c) 2016-2022 Sebastian Pipping <sebastian@pipping.org>
  13. Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
  14. Copyright (c) 2018 Benjamin Peterson <benjamin@python.org>
  15. Copyright (c) 2018 Anton Maklakov <antmak.pub@gmail.com>
  16. Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
  17. Copyright (c) 2020 Boris Kolpackov <boris@codesynthesis.com>
  18. Copyright (c) 2022 Martin Ettl <ettl.martin78@googlemail.com>
  19. Licensed under the MIT license:
  20. Permission is hereby granted, free of charge, to any person obtaining
  21. a copy of this software and associated documentation files (the
  22. "Software"), to deal in the Software without restriction, including
  23. without limitation the rights to use, copy, modify, merge, publish,
  24. distribute, sublicense, and/or sell copies of the Software, and to permit
  25. persons to whom the Software is furnished to do so, subject to the
  26. following conditions:
  27. The above copyright notice and this permission notice shall be included
  28. in all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  32. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  33. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  34. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  35. USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. #ifdef XML_TOK_IMPL_C
  38. # ifndef IS_INVALID_CHAR // i.e. for UTF-16 and XML_MIN_SIZE not defined
  39. # define IS_INVALID_CHAR(enc, ptr, n) (0)
  40. # endif
  41. # define INVALID_LEAD_CASE(n, ptr, nextTokPtr) \
  42. case BT_LEAD##n: \
  43. if (end - ptr < n) \
  44. return XML_TOK_PARTIAL_CHAR; \
  45. if (IS_INVALID_CHAR(enc, ptr, n)) { \
  46. *(nextTokPtr) = (ptr); \
  47. return XML_TOK_INVALID; \
  48. } \
  49. ptr += n; \
  50. break;
  51. # define INVALID_CASES(ptr, nextTokPtr) \
  52. INVALID_LEAD_CASE(2, ptr, nextTokPtr) \
  53. INVALID_LEAD_CASE(3, ptr, nextTokPtr) \
  54. INVALID_LEAD_CASE(4, ptr, nextTokPtr) \
  55. case BT_NONXML: \
  56. case BT_MALFORM: \
  57. case BT_TRAIL: \
  58. *(nextTokPtr) = (ptr); \
  59. return XML_TOK_INVALID;
  60. # define CHECK_NAME_CASE(n, enc, ptr, end, nextTokPtr) \
  61. case BT_LEAD##n: \
  62. if (end - ptr < n) \
  63. return XML_TOK_PARTIAL_CHAR; \
  64. if (IS_INVALID_CHAR(enc, ptr, n) || ! IS_NAME_CHAR(enc, ptr, n)) { \
  65. *nextTokPtr = ptr; \
  66. return XML_TOK_INVALID; \
  67. } \
  68. ptr += n; \
  69. break;
  70. # define CHECK_NAME_CASES(enc, ptr, end, nextTokPtr) \
  71. case BT_NONASCII: \
  72. if (! IS_NAME_CHAR_MINBPC(enc, ptr)) { \
  73. *nextTokPtr = ptr; \
  74. return XML_TOK_INVALID; \
  75. } \
  76. /* fall through */ \
  77. case BT_NMSTRT: \
  78. case BT_HEX: \
  79. case BT_DIGIT: \
  80. case BT_NAME: \
  81. case BT_MINUS: \
  82. ptr += MINBPC(enc); \
  83. break; \
  84. CHECK_NAME_CASE(2, enc, ptr, end, nextTokPtr) \
  85. CHECK_NAME_CASE(3, enc, ptr, end, nextTokPtr) \
  86. CHECK_NAME_CASE(4, enc, ptr, end, nextTokPtr)
  87. # define CHECK_NMSTRT_CASE(n, enc, ptr, end, nextTokPtr) \
  88. case BT_LEAD##n: \
  89. if ((end) - (ptr) < (n)) \
  90. return XML_TOK_PARTIAL_CHAR; \
  91. if (IS_INVALID_CHAR(enc, ptr, n) || ! IS_NMSTRT_CHAR(enc, ptr, n)) { \
  92. *nextTokPtr = ptr; \
  93. return XML_TOK_INVALID; \
  94. } \
  95. ptr += n; \
  96. break;
  97. # define CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr) \
  98. case BT_NONASCII: \
  99. if (! IS_NMSTRT_CHAR_MINBPC(enc, ptr)) { \
  100. *nextTokPtr = ptr; \
  101. return XML_TOK_INVALID; \
  102. } \
  103. /* fall through */ \
  104. case BT_NMSTRT: \
  105. case BT_HEX: \
  106. ptr += MINBPC(enc); \
  107. break; \
  108. CHECK_NMSTRT_CASE(2, enc, ptr, end, nextTokPtr) \
  109. CHECK_NMSTRT_CASE(3, enc, ptr, end, nextTokPtr) \
  110. CHECK_NMSTRT_CASE(4, enc, ptr, end, nextTokPtr)
  111. # ifndef PREFIX
  112. # define PREFIX(ident) ident
  113. # endif
  114. # define HAS_CHARS(enc, ptr, end, count) \
  115. ((end) - (ptr) >= ((count) * MINBPC(enc)))
  116. # define HAS_CHAR(enc, ptr, end) HAS_CHARS(enc, ptr, end, 1)
  117. # define REQUIRE_CHARS(enc, ptr, end, count) \
  118. { \
  119. if (! HAS_CHARS(enc, ptr, end, count)) { \
  120. return XML_TOK_PARTIAL; \
  121. } \
  122. }
  123. # define REQUIRE_CHAR(enc, ptr, end) REQUIRE_CHARS(enc, ptr, end, 1)
  124. /* ptr points to character following "<!-" */
  125. static int PTRCALL
  126. PREFIX(scanComment)(const ENCODING *enc, const char *ptr, const char *end,
  127. const char **nextTokPtr) {
  128. if (HAS_CHAR(enc, ptr, end)) {
  129. if (! CHAR_MATCHES(enc, ptr, ASCII_MINUS)) {
  130. *nextTokPtr = ptr;
  131. return XML_TOK_INVALID;
  132. }
  133. ptr += MINBPC(enc);
  134. while (HAS_CHAR(enc, ptr, end)) {
  135. switch (BYTE_TYPE(enc, ptr)) {
  136. INVALID_CASES(ptr, nextTokPtr)
  137. case BT_MINUS:
  138. ptr += MINBPC(enc);
  139. REQUIRE_CHAR(enc, ptr, end);
  140. if (CHAR_MATCHES(enc, ptr, ASCII_MINUS)) {
  141. ptr += MINBPC(enc);
  142. REQUIRE_CHAR(enc, ptr, end);
  143. if (! CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  144. *nextTokPtr = ptr;
  145. return XML_TOK_INVALID;
  146. }
  147. *nextTokPtr = ptr + MINBPC(enc);
  148. return XML_TOK_COMMENT;
  149. }
  150. break;
  151. default:
  152. ptr += MINBPC(enc);
  153. break;
  154. }
  155. }
  156. }
  157. return XML_TOK_PARTIAL;
  158. }
  159. /* ptr points to character following "<!" */
  160. static int PTRCALL
  161. PREFIX(scanDecl)(const ENCODING *enc, const char *ptr, const char *end,
  162. const char **nextTokPtr) {
  163. REQUIRE_CHAR(enc, ptr, end);
  164. switch (BYTE_TYPE(enc, ptr)) {
  165. case BT_MINUS:
  166. return PREFIX(scanComment)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  167. case BT_LSQB:
  168. *nextTokPtr = ptr + MINBPC(enc);
  169. return XML_TOK_COND_SECT_OPEN;
  170. case BT_NMSTRT:
  171. case BT_HEX:
  172. ptr += MINBPC(enc);
  173. break;
  174. default:
  175. *nextTokPtr = ptr;
  176. return XML_TOK_INVALID;
  177. }
  178. while (HAS_CHAR(enc, ptr, end)) {
  179. switch (BYTE_TYPE(enc, ptr)) {
  180. case BT_PERCNT:
  181. REQUIRE_CHARS(enc, ptr, end, 2);
  182. /* don't allow <!ENTITY% foo "whatever"> */
  183. switch (BYTE_TYPE(enc, ptr + MINBPC(enc))) {
  184. case BT_S:
  185. case BT_CR:
  186. case BT_LF:
  187. case BT_PERCNT:
  188. *nextTokPtr = ptr;
  189. return XML_TOK_INVALID;
  190. }
  191. /* fall through */
  192. case BT_S:
  193. case BT_CR:
  194. case BT_LF:
  195. *nextTokPtr = ptr;
  196. return XML_TOK_DECL_OPEN;
  197. case BT_NMSTRT:
  198. case BT_HEX:
  199. ptr += MINBPC(enc);
  200. break;
  201. default:
  202. *nextTokPtr = ptr;
  203. return XML_TOK_INVALID;
  204. }
  205. }
  206. return XML_TOK_PARTIAL;
  207. }
  208. static int PTRCALL
  209. PREFIX(checkPiTarget)(const ENCODING *enc, const char *ptr, const char *end,
  210. int *tokPtr) {
  211. int upper = 0;
  212. UNUSED_P(enc);
  213. *tokPtr = XML_TOK_PI;
  214. if (end - ptr != MINBPC(enc) * 3)
  215. return 1;
  216. switch (BYTE_TO_ASCII(enc, ptr)) {
  217. case ASCII_x:
  218. break;
  219. case ASCII_X:
  220. upper = 1;
  221. break;
  222. default:
  223. return 1;
  224. }
  225. ptr += MINBPC(enc);
  226. switch (BYTE_TO_ASCII(enc, ptr)) {
  227. case ASCII_m:
  228. break;
  229. case ASCII_M:
  230. upper = 1;
  231. break;
  232. default:
  233. return 1;
  234. }
  235. ptr += MINBPC(enc);
  236. switch (BYTE_TO_ASCII(enc, ptr)) {
  237. case ASCII_l:
  238. break;
  239. case ASCII_L:
  240. upper = 1;
  241. break;
  242. default:
  243. return 1;
  244. }
  245. if (upper)
  246. return 0;
  247. *tokPtr = XML_TOK_XML_DECL;
  248. return 1;
  249. }
  250. /* ptr points to character following "<?" */
  251. static int PTRCALL
  252. PREFIX(scanPi)(const ENCODING *enc, const char *ptr, const char *end,
  253. const char **nextTokPtr) {
  254. int tok;
  255. const char *target = ptr;
  256. REQUIRE_CHAR(enc, ptr, end);
  257. switch (BYTE_TYPE(enc, ptr)) {
  258. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  259. default:
  260. *nextTokPtr = ptr;
  261. return XML_TOK_INVALID;
  262. }
  263. while (HAS_CHAR(enc, ptr, end)) {
  264. switch (BYTE_TYPE(enc, ptr)) {
  265. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  266. case BT_S:
  267. case BT_CR:
  268. case BT_LF:
  269. if (! PREFIX(checkPiTarget)(enc, target, ptr, &tok)) {
  270. *nextTokPtr = ptr;
  271. return XML_TOK_INVALID;
  272. }
  273. ptr += MINBPC(enc);
  274. while (HAS_CHAR(enc, ptr, end)) {
  275. switch (BYTE_TYPE(enc, ptr)) {
  276. INVALID_CASES(ptr, nextTokPtr)
  277. case BT_QUEST:
  278. ptr += MINBPC(enc);
  279. REQUIRE_CHAR(enc, ptr, end);
  280. if (CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  281. *nextTokPtr = ptr + MINBPC(enc);
  282. return tok;
  283. }
  284. break;
  285. default:
  286. ptr += MINBPC(enc);
  287. break;
  288. }
  289. }
  290. return XML_TOK_PARTIAL;
  291. case BT_QUEST:
  292. if (! PREFIX(checkPiTarget)(enc, target, ptr, &tok)) {
  293. *nextTokPtr = ptr;
  294. return XML_TOK_INVALID;
  295. }
  296. ptr += MINBPC(enc);
  297. REQUIRE_CHAR(enc, ptr, end);
  298. if (CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  299. *nextTokPtr = ptr + MINBPC(enc);
  300. return tok;
  301. }
  302. /* fall through */
  303. default:
  304. *nextTokPtr = ptr;
  305. return XML_TOK_INVALID;
  306. }
  307. }
  308. return XML_TOK_PARTIAL;
  309. }
  310. static int PTRCALL
  311. PREFIX(scanCdataSection)(const ENCODING *enc, const char *ptr, const char *end,
  312. const char **nextTokPtr) {
  313. static const char CDATA_LSQB[]
  314. = {ASCII_C, ASCII_D, ASCII_A, ASCII_T, ASCII_A, ASCII_LSQB};
  315. int i;
  316. UNUSED_P(enc);
  317. /* CDATA[ */
  318. REQUIRE_CHARS(enc, ptr, end, 6);
  319. for (i = 0; i < 6; i++, ptr += MINBPC(enc)) {
  320. if (! CHAR_MATCHES(enc, ptr, CDATA_LSQB[i])) {
  321. *nextTokPtr = ptr;
  322. return XML_TOK_INVALID;
  323. }
  324. }
  325. *nextTokPtr = ptr;
  326. return XML_TOK_CDATA_SECT_OPEN;
  327. }
  328. static int PTRCALL
  329. PREFIX(cdataSectionTok)(const ENCODING *enc, const char *ptr, const char *end,
  330. const char **nextTokPtr) {
  331. if (ptr >= end)
  332. return XML_TOK_NONE;
  333. if (MINBPC(enc) > 1) {
  334. size_t n = end - ptr;
  335. if (n & (MINBPC(enc) - 1)) {
  336. n &= ~(MINBPC(enc) - 1);
  337. if (n == 0)
  338. return XML_TOK_PARTIAL;
  339. end = ptr + n;
  340. }
  341. }
  342. switch (BYTE_TYPE(enc, ptr)) {
  343. case BT_RSQB:
  344. ptr += MINBPC(enc);
  345. REQUIRE_CHAR(enc, ptr, end);
  346. if (! CHAR_MATCHES(enc, ptr, ASCII_RSQB))
  347. break;
  348. ptr += MINBPC(enc);
  349. REQUIRE_CHAR(enc, ptr, end);
  350. if (! CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  351. ptr -= MINBPC(enc);
  352. break;
  353. }
  354. *nextTokPtr = ptr + MINBPC(enc);
  355. return XML_TOK_CDATA_SECT_CLOSE;
  356. case BT_CR:
  357. ptr += MINBPC(enc);
  358. REQUIRE_CHAR(enc, ptr, end);
  359. if (BYTE_TYPE(enc, ptr) == BT_LF)
  360. ptr += MINBPC(enc);
  361. *nextTokPtr = ptr;
  362. return XML_TOK_DATA_NEWLINE;
  363. case BT_LF:
  364. *nextTokPtr = ptr + MINBPC(enc);
  365. return XML_TOK_DATA_NEWLINE;
  366. INVALID_CASES(ptr, nextTokPtr)
  367. default:
  368. ptr += MINBPC(enc);
  369. break;
  370. }
  371. while (HAS_CHAR(enc, ptr, end)) {
  372. switch (BYTE_TYPE(enc, ptr)) {
  373. # define LEAD_CASE(n) \
  374. case BT_LEAD##n: \
  375. if (end - ptr < n || IS_INVALID_CHAR(enc, ptr, n)) { \
  376. *nextTokPtr = ptr; \
  377. return XML_TOK_DATA_CHARS; \
  378. } \
  379. ptr += n; \
  380. break;
  381. LEAD_CASE(2)
  382. LEAD_CASE(3)
  383. LEAD_CASE(4)
  384. # undef LEAD_CASE
  385. case BT_NONXML:
  386. case BT_MALFORM:
  387. case BT_TRAIL:
  388. case BT_CR:
  389. case BT_LF:
  390. case BT_RSQB:
  391. *nextTokPtr = ptr;
  392. return XML_TOK_DATA_CHARS;
  393. default:
  394. ptr += MINBPC(enc);
  395. break;
  396. }
  397. }
  398. *nextTokPtr = ptr;
  399. return XML_TOK_DATA_CHARS;
  400. }
  401. /* ptr points to character following "</" */
  402. static int PTRCALL
  403. PREFIX(scanEndTag)(const ENCODING *enc, const char *ptr, const char *end,
  404. const char **nextTokPtr) {
  405. REQUIRE_CHAR(enc, ptr, end);
  406. switch (BYTE_TYPE(enc, ptr)) {
  407. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  408. default:
  409. *nextTokPtr = ptr;
  410. return XML_TOK_INVALID;
  411. }
  412. while (HAS_CHAR(enc, ptr, end)) {
  413. switch (BYTE_TYPE(enc, ptr)) {
  414. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  415. case BT_S:
  416. case BT_CR:
  417. case BT_LF:
  418. for (ptr += MINBPC(enc); HAS_CHAR(enc, ptr, end); ptr += MINBPC(enc)) {
  419. switch (BYTE_TYPE(enc, ptr)) {
  420. case BT_S:
  421. case BT_CR:
  422. case BT_LF:
  423. break;
  424. case BT_GT:
  425. *nextTokPtr = ptr + MINBPC(enc);
  426. return XML_TOK_END_TAG;
  427. default:
  428. *nextTokPtr = ptr;
  429. return XML_TOK_INVALID;
  430. }
  431. }
  432. return XML_TOK_PARTIAL;
  433. # ifdef XML_NS
  434. case BT_COLON:
  435. /* no need to check qname syntax here,
  436. since end-tag must match exactly */
  437. ptr += MINBPC(enc);
  438. break;
  439. # endif
  440. case BT_GT:
  441. *nextTokPtr = ptr + MINBPC(enc);
  442. return XML_TOK_END_TAG;
  443. default:
  444. *nextTokPtr = ptr;
  445. return XML_TOK_INVALID;
  446. }
  447. }
  448. return XML_TOK_PARTIAL;
  449. }
  450. /* ptr points to character following "&#X" */
  451. static int PTRCALL
  452. PREFIX(scanHexCharRef)(const ENCODING *enc, const char *ptr, const char *end,
  453. const char **nextTokPtr) {
  454. if (HAS_CHAR(enc, ptr, end)) {
  455. switch (BYTE_TYPE(enc, ptr)) {
  456. case BT_DIGIT:
  457. case BT_HEX:
  458. break;
  459. default:
  460. *nextTokPtr = ptr;
  461. return XML_TOK_INVALID;
  462. }
  463. for (ptr += MINBPC(enc); HAS_CHAR(enc, ptr, end); ptr += MINBPC(enc)) {
  464. switch (BYTE_TYPE(enc, ptr)) {
  465. case BT_DIGIT:
  466. case BT_HEX:
  467. break;
  468. case BT_SEMI:
  469. *nextTokPtr = ptr + MINBPC(enc);
  470. return XML_TOK_CHAR_REF;
  471. default:
  472. *nextTokPtr = ptr;
  473. return XML_TOK_INVALID;
  474. }
  475. }
  476. }
  477. return XML_TOK_PARTIAL;
  478. }
  479. /* ptr points to character following "&#" */
  480. static int PTRCALL
  481. PREFIX(scanCharRef)(const ENCODING *enc, const char *ptr, const char *end,
  482. const char **nextTokPtr) {
  483. if (HAS_CHAR(enc, ptr, end)) {
  484. if (CHAR_MATCHES(enc, ptr, ASCII_x))
  485. return PREFIX(scanHexCharRef)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  486. switch (BYTE_TYPE(enc, ptr)) {
  487. case BT_DIGIT:
  488. break;
  489. default:
  490. *nextTokPtr = ptr;
  491. return XML_TOK_INVALID;
  492. }
  493. for (ptr += MINBPC(enc); HAS_CHAR(enc, ptr, end); ptr += MINBPC(enc)) {
  494. switch (BYTE_TYPE(enc, ptr)) {
  495. case BT_DIGIT:
  496. break;
  497. case BT_SEMI:
  498. *nextTokPtr = ptr + MINBPC(enc);
  499. return XML_TOK_CHAR_REF;
  500. default:
  501. *nextTokPtr = ptr;
  502. return XML_TOK_INVALID;
  503. }
  504. }
  505. }
  506. return XML_TOK_PARTIAL;
  507. }
  508. /* ptr points to character following "&" */
  509. static int PTRCALL
  510. PREFIX(scanRef)(const ENCODING *enc, const char *ptr, const char *end,
  511. const char **nextTokPtr) {
  512. REQUIRE_CHAR(enc, ptr, end);
  513. switch (BYTE_TYPE(enc, ptr)) {
  514. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  515. case BT_NUM:
  516. return PREFIX(scanCharRef)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  517. default:
  518. *nextTokPtr = ptr;
  519. return XML_TOK_INVALID;
  520. }
  521. while (HAS_CHAR(enc, ptr, end)) {
  522. switch (BYTE_TYPE(enc, ptr)) {
  523. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  524. case BT_SEMI:
  525. *nextTokPtr = ptr + MINBPC(enc);
  526. return XML_TOK_ENTITY_REF;
  527. default:
  528. *nextTokPtr = ptr;
  529. return XML_TOK_INVALID;
  530. }
  531. }
  532. return XML_TOK_PARTIAL;
  533. }
  534. /* ptr points to character following first character of attribute name */
  535. static int PTRCALL
  536. PREFIX(scanAtts)(const ENCODING *enc, const char *ptr, const char *end,
  537. const char **nextTokPtr) {
  538. # ifdef XML_NS
  539. int hadColon = 0;
  540. # endif
  541. while (HAS_CHAR(enc, ptr, end)) {
  542. switch (BYTE_TYPE(enc, ptr)) {
  543. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  544. # ifdef XML_NS
  545. case BT_COLON:
  546. if (hadColon) {
  547. *nextTokPtr = ptr;
  548. return XML_TOK_INVALID;
  549. }
  550. hadColon = 1;
  551. ptr += MINBPC(enc);
  552. REQUIRE_CHAR(enc, ptr, end);
  553. switch (BYTE_TYPE(enc, ptr)) {
  554. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  555. default:
  556. *nextTokPtr = ptr;
  557. return XML_TOK_INVALID;
  558. }
  559. break;
  560. # endif
  561. case BT_S:
  562. case BT_CR:
  563. case BT_LF:
  564. for (;;) {
  565. int t;
  566. ptr += MINBPC(enc);
  567. REQUIRE_CHAR(enc, ptr, end);
  568. t = BYTE_TYPE(enc, ptr);
  569. if (t == BT_EQUALS)
  570. break;
  571. switch (t) {
  572. case BT_S:
  573. case BT_LF:
  574. case BT_CR:
  575. break;
  576. default:
  577. *nextTokPtr = ptr;
  578. return XML_TOK_INVALID;
  579. }
  580. }
  581. /* fall through */
  582. case BT_EQUALS: {
  583. int open;
  584. # ifdef XML_NS
  585. hadColon = 0;
  586. # endif
  587. for (;;) {
  588. ptr += MINBPC(enc);
  589. REQUIRE_CHAR(enc, ptr, end);
  590. open = BYTE_TYPE(enc, ptr);
  591. if (open == BT_QUOT || open == BT_APOS)
  592. break;
  593. switch (open) {
  594. case BT_S:
  595. case BT_LF:
  596. case BT_CR:
  597. break;
  598. default:
  599. *nextTokPtr = ptr;
  600. return XML_TOK_INVALID;
  601. }
  602. }
  603. ptr += MINBPC(enc);
  604. /* in attribute value */
  605. for (;;) {
  606. int t;
  607. REQUIRE_CHAR(enc, ptr, end);
  608. t = BYTE_TYPE(enc, ptr);
  609. if (t == open)
  610. break;
  611. switch (t) {
  612. INVALID_CASES(ptr, nextTokPtr)
  613. case BT_AMP: {
  614. int tok = PREFIX(scanRef)(enc, ptr + MINBPC(enc), end, &ptr);
  615. if (tok <= 0) {
  616. if (tok == XML_TOK_INVALID)
  617. *nextTokPtr = ptr;
  618. return tok;
  619. }
  620. break;
  621. }
  622. case BT_LT:
  623. *nextTokPtr = ptr;
  624. return XML_TOK_INVALID;
  625. default:
  626. ptr += MINBPC(enc);
  627. break;
  628. }
  629. }
  630. ptr += MINBPC(enc);
  631. REQUIRE_CHAR(enc, ptr, end);
  632. switch (BYTE_TYPE(enc, ptr)) {
  633. case BT_S:
  634. case BT_CR:
  635. case BT_LF:
  636. break;
  637. case BT_SOL:
  638. goto sol;
  639. case BT_GT:
  640. goto gt;
  641. default:
  642. *nextTokPtr = ptr;
  643. return XML_TOK_INVALID;
  644. }
  645. /* ptr points to closing quote */
  646. for (;;) {
  647. ptr += MINBPC(enc);
  648. REQUIRE_CHAR(enc, ptr, end);
  649. switch (BYTE_TYPE(enc, ptr)) {
  650. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  651. case BT_S:
  652. case BT_CR:
  653. case BT_LF:
  654. continue;
  655. case BT_GT:
  656. gt:
  657. *nextTokPtr = ptr + MINBPC(enc);
  658. return XML_TOK_START_TAG_WITH_ATTS;
  659. case BT_SOL:
  660. sol:
  661. ptr += MINBPC(enc);
  662. REQUIRE_CHAR(enc, ptr, end);
  663. if (! CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  664. *nextTokPtr = ptr;
  665. return XML_TOK_INVALID;
  666. }
  667. *nextTokPtr = ptr + MINBPC(enc);
  668. return XML_TOK_EMPTY_ELEMENT_WITH_ATTS;
  669. default:
  670. *nextTokPtr = ptr;
  671. return XML_TOK_INVALID;
  672. }
  673. break;
  674. }
  675. break;
  676. }
  677. default:
  678. *nextTokPtr = ptr;
  679. return XML_TOK_INVALID;
  680. }
  681. }
  682. return XML_TOK_PARTIAL;
  683. }
  684. /* ptr points to character following "<" */
  685. static int PTRCALL
  686. PREFIX(scanLt)(const ENCODING *enc, const char *ptr, const char *end,
  687. const char **nextTokPtr) {
  688. # ifdef XML_NS
  689. int hadColon;
  690. # endif
  691. REQUIRE_CHAR(enc, ptr, end);
  692. switch (BYTE_TYPE(enc, ptr)) {
  693. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  694. case BT_EXCL:
  695. ptr += MINBPC(enc);
  696. REQUIRE_CHAR(enc, ptr, end);
  697. switch (BYTE_TYPE(enc, ptr)) {
  698. case BT_MINUS:
  699. return PREFIX(scanComment)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  700. case BT_LSQB:
  701. return PREFIX(scanCdataSection)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  702. }
  703. *nextTokPtr = ptr;
  704. return XML_TOK_INVALID;
  705. case BT_QUEST:
  706. return PREFIX(scanPi)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  707. case BT_SOL:
  708. return PREFIX(scanEndTag)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  709. default:
  710. *nextTokPtr = ptr;
  711. return XML_TOK_INVALID;
  712. }
  713. # ifdef XML_NS
  714. hadColon = 0;
  715. # endif
  716. /* we have a start-tag */
  717. while (HAS_CHAR(enc, ptr, end)) {
  718. switch (BYTE_TYPE(enc, ptr)) {
  719. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  720. # ifdef XML_NS
  721. case BT_COLON:
  722. if (hadColon) {
  723. *nextTokPtr = ptr;
  724. return XML_TOK_INVALID;
  725. }
  726. hadColon = 1;
  727. ptr += MINBPC(enc);
  728. REQUIRE_CHAR(enc, ptr, end);
  729. switch (BYTE_TYPE(enc, ptr)) {
  730. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  731. default:
  732. *nextTokPtr = ptr;
  733. return XML_TOK_INVALID;
  734. }
  735. break;
  736. # endif
  737. case BT_S:
  738. case BT_CR:
  739. case BT_LF: {
  740. ptr += MINBPC(enc);
  741. while (HAS_CHAR(enc, ptr, end)) {
  742. switch (BYTE_TYPE(enc, ptr)) {
  743. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  744. case BT_GT:
  745. goto gt;
  746. case BT_SOL:
  747. goto sol;
  748. case BT_S:
  749. case BT_CR:
  750. case BT_LF:
  751. ptr += MINBPC(enc);
  752. continue;
  753. default:
  754. *nextTokPtr = ptr;
  755. return XML_TOK_INVALID;
  756. }
  757. return PREFIX(scanAtts)(enc, ptr, end, nextTokPtr);
  758. }
  759. return XML_TOK_PARTIAL;
  760. }
  761. case BT_GT:
  762. gt:
  763. *nextTokPtr = ptr + MINBPC(enc);
  764. return XML_TOK_START_TAG_NO_ATTS;
  765. case BT_SOL:
  766. sol:
  767. ptr += MINBPC(enc);
  768. REQUIRE_CHAR(enc, ptr, end);
  769. if (! CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  770. *nextTokPtr = ptr;
  771. return XML_TOK_INVALID;
  772. }
  773. *nextTokPtr = ptr + MINBPC(enc);
  774. return XML_TOK_EMPTY_ELEMENT_NO_ATTS;
  775. default:
  776. *nextTokPtr = ptr;
  777. return XML_TOK_INVALID;
  778. }
  779. }
  780. return XML_TOK_PARTIAL;
  781. }
  782. static int PTRCALL
  783. PREFIX(contentTok)(const ENCODING *enc, const char *ptr, const char *end,
  784. const char **nextTokPtr) {
  785. if (ptr >= end)
  786. return XML_TOK_NONE;
  787. if (MINBPC(enc) > 1) {
  788. size_t n = end - ptr;
  789. if (n & (MINBPC(enc) - 1)) {
  790. n &= ~(MINBPC(enc) - 1);
  791. if (n == 0)
  792. return XML_TOK_PARTIAL;
  793. end = ptr + n;
  794. }
  795. }
  796. switch (BYTE_TYPE(enc, ptr)) {
  797. case BT_LT:
  798. return PREFIX(scanLt)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  799. case BT_AMP:
  800. return PREFIX(scanRef)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  801. case BT_CR:
  802. ptr += MINBPC(enc);
  803. if (! HAS_CHAR(enc, ptr, end))
  804. return XML_TOK_TRAILING_CR;
  805. if (BYTE_TYPE(enc, ptr) == BT_LF)
  806. ptr += MINBPC(enc);
  807. *nextTokPtr = ptr;
  808. return XML_TOK_DATA_NEWLINE;
  809. case BT_LF:
  810. *nextTokPtr = ptr + MINBPC(enc);
  811. return XML_TOK_DATA_NEWLINE;
  812. case BT_RSQB:
  813. ptr += MINBPC(enc);
  814. if (! HAS_CHAR(enc, ptr, end))
  815. return XML_TOK_TRAILING_RSQB;
  816. if (! CHAR_MATCHES(enc, ptr, ASCII_RSQB))
  817. break;
  818. ptr += MINBPC(enc);
  819. if (! HAS_CHAR(enc, ptr, end))
  820. return XML_TOK_TRAILING_RSQB;
  821. if (! CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  822. ptr -= MINBPC(enc);
  823. break;
  824. }
  825. *nextTokPtr = ptr;
  826. return XML_TOK_INVALID;
  827. INVALID_CASES(ptr, nextTokPtr)
  828. default:
  829. ptr += MINBPC(enc);
  830. break;
  831. }
  832. while (HAS_CHAR(enc, ptr, end)) {
  833. switch (BYTE_TYPE(enc, ptr)) {
  834. # define LEAD_CASE(n) \
  835. case BT_LEAD##n: \
  836. if (end - ptr < n || IS_INVALID_CHAR(enc, ptr, n)) { \
  837. *nextTokPtr = ptr; \
  838. return XML_TOK_DATA_CHARS; \
  839. } \
  840. ptr += n; \
  841. break;
  842. LEAD_CASE(2)
  843. LEAD_CASE(3)
  844. LEAD_CASE(4)
  845. # undef LEAD_CASE
  846. case BT_RSQB:
  847. if (HAS_CHARS(enc, ptr, end, 2)) {
  848. if (! CHAR_MATCHES(enc, ptr + MINBPC(enc), ASCII_RSQB)) {
  849. ptr += MINBPC(enc);
  850. break;
  851. }
  852. if (HAS_CHARS(enc, ptr, end, 3)) {
  853. if (! CHAR_MATCHES(enc, ptr + 2 * MINBPC(enc), ASCII_GT)) {
  854. ptr += MINBPC(enc);
  855. break;
  856. }
  857. *nextTokPtr = ptr + 2 * MINBPC(enc);
  858. return XML_TOK_INVALID;
  859. }
  860. }
  861. /* fall through */
  862. case BT_AMP:
  863. case BT_LT:
  864. case BT_NONXML:
  865. case BT_MALFORM:
  866. case BT_TRAIL:
  867. case BT_CR:
  868. case BT_LF:
  869. *nextTokPtr = ptr;
  870. return XML_TOK_DATA_CHARS;
  871. default:
  872. ptr += MINBPC(enc);
  873. break;
  874. }
  875. }
  876. *nextTokPtr = ptr;
  877. return XML_TOK_DATA_CHARS;
  878. }
  879. /* ptr points to character following "%" */
  880. static int PTRCALL
  881. PREFIX(scanPercent)(const ENCODING *enc, const char *ptr, const char *end,
  882. const char **nextTokPtr) {
  883. REQUIRE_CHAR(enc, ptr, end);
  884. switch (BYTE_TYPE(enc, ptr)) {
  885. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  886. case BT_S:
  887. case BT_LF:
  888. case BT_CR:
  889. case BT_PERCNT:
  890. *nextTokPtr = ptr;
  891. return XML_TOK_PERCENT;
  892. default:
  893. *nextTokPtr = ptr;
  894. return XML_TOK_INVALID;
  895. }
  896. while (HAS_CHAR(enc, ptr, end)) {
  897. switch (BYTE_TYPE(enc, ptr)) {
  898. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  899. case BT_SEMI:
  900. *nextTokPtr = ptr + MINBPC(enc);
  901. return XML_TOK_PARAM_ENTITY_REF;
  902. default:
  903. *nextTokPtr = ptr;
  904. return XML_TOK_INVALID;
  905. }
  906. }
  907. return XML_TOK_PARTIAL;
  908. }
  909. static int PTRCALL
  910. PREFIX(scanPoundName)(const ENCODING *enc, const char *ptr, const char *end,
  911. const char **nextTokPtr) {
  912. REQUIRE_CHAR(enc, ptr, end);
  913. switch (BYTE_TYPE(enc, ptr)) {
  914. CHECK_NMSTRT_CASES(enc, ptr, end, nextTokPtr)
  915. default:
  916. *nextTokPtr = ptr;
  917. return XML_TOK_INVALID;
  918. }
  919. while (HAS_CHAR(enc, ptr, end)) {
  920. switch (BYTE_TYPE(enc, ptr)) {
  921. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  922. case BT_CR:
  923. case BT_LF:
  924. case BT_S:
  925. case BT_RPAR:
  926. case BT_GT:
  927. case BT_PERCNT:
  928. case BT_VERBAR:
  929. *nextTokPtr = ptr;
  930. return XML_TOK_POUND_NAME;
  931. default:
  932. *nextTokPtr = ptr;
  933. return XML_TOK_INVALID;
  934. }
  935. }
  936. return -XML_TOK_POUND_NAME;
  937. }
  938. static int PTRCALL
  939. PREFIX(scanLit)(int open, const ENCODING *enc, const char *ptr, const char *end,
  940. const char **nextTokPtr) {
  941. while (HAS_CHAR(enc, ptr, end)) {
  942. int t = BYTE_TYPE(enc, ptr);
  943. switch (t) {
  944. INVALID_CASES(ptr, nextTokPtr)
  945. case BT_QUOT:
  946. case BT_APOS:
  947. ptr += MINBPC(enc);
  948. if (t != open)
  949. break;
  950. if (! HAS_CHAR(enc, ptr, end))
  951. return -XML_TOK_LITERAL;
  952. *nextTokPtr = ptr;
  953. switch (BYTE_TYPE(enc, ptr)) {
  954. case BT_S:
  955. case BT_CR:
  956. case BT_LF:
  957. case BT_GT:
  958. case BT_PERCNT:
  959. case BT_LSQB:
  960. return XML_TOK_LITERAL;
  961. default:
  962. return XML_TOK_INVALID;
  963. }
  964. default:
  965. ptr += MINBPC(enc);
  966. break;
  967. }
  968. }
  969. return XML_TOK_PARTIAL;
  970. }
  971. static int PTRCALL
  972. PREFIX(prologTok)(const ENCODING *enc, const char *ptr, const char *end,
  973. const char **nextTokPtr) {
  974. int tok;
  975. if (ptr >= end)
  976. return XML_TOK_NONE;
  977. if (MINBPC(enc) > 1) {
  978. size_t n = end - ptr;
  979. if (n & (MINBPC(enc) - 1)) {
  980. n &= ~(MINBPC(enc) - 1);
  981. if (n == 0)
  982. return XML_TOK_PARTIAL;
  983. end = ptr + n;
  984. }
  985. }
  986. switch (BYTE_TYPE(enc, ptr)) {
  987. case BT_QUOT:
  988. return PREFIX(scanLit)(BT_QUOT, enc, ptr + MINBPC(enc), end, nextTokPtr);
  989. case BT_APOS:
  990. return PREFIX(scanLit)(BT_APOS, enc, ptr + MINBPC(enc), end, nextTokPtr);
  991. case BT_LT: {
  992. ptr += MINBPC(enc);
  993. REQUIRE_CHAR(enc, ptr, end);
  994. switch (BYTE_TYPE(enc, ptr)) {
  995. case BT_EXCL:
  996. return PREFIX(scanDecl)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  997. case BT_QUEST:
  998. return PREFIX(scanPi)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  999. case BT_NMSTRT:
  1000. case BT_HEX:
  1001. case BT_NONASCII:
  1002. case BT_LEAD2:
  1003. case BT_LEAD3:
  1004. case BT_LEAD4:
  1005. *nextTokPtr = ptr - MINBPC(enc);
  1006. return XML_TOK_INSTANCE_START;
  1007. }
  1008. *nextTokPtr = ptr;
  1009. return XML_TOK_INVALID;
  1010. }
  1011. case BT_CR:
  1012. if (ptr + MINBPC(enc) == end) {
  1013. *nextTokPtr = end;
  1014. /* indicate that this might be part of a CR/LF pair */
  1015. return -XML_TOK_PROLOG_S;
  1016. }
  1017. /* fall through */
  1018. case BT_S:
  1019. case BT_LF:
  1020. for (;;) {
  1021. ptr += MINBPC(enc);
  1022. if (! HAS_CHAR(enc, ptr, end))
  1023. break;
  1024. switch (BYTE_TYPE(enc, ptr)) {
  1025. case BT_S:
  1026. case BT_LF:
  1027. break;
  1028. case BT_CR:
  1029. /* don't split CR/LF pair */
  1030. if (ptr + MINBPC(enc) != end)
  1031. break;
  1032. /* fall through */
  1033. default:
  1034. *nextTokPtr = ptr;
  1035. return XML_TOK_PROLOG_S;
  1036. }
  1037. }
  1038. *nextTokPtr = ptr;
  1039. return XML_TOK_PROLOG_S;
  1040. case BT_PERCNT:
  1041. return PREFIX(scanPercent)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  1042. case BT_COMMA:
  1043. *nextTokPtr = ptr + MINBPC(enc);
  1044. return XML_TOK_COMMA;
  1045. case BT_LSQB:
  1046. *nextTokPtr = ptr + MINBPC(enc);
  1047. return XML_TOK_OPEN_BRACKET;
  1048. case BT_RSQB:
  1049. ptr += MINBPC(enc);
  1050. if (! HAS_CHAR(enc, ptr, end))
  1051. return -XML_TOK_CLOSE_BRACKET;
  1052. if (CHAR_MATCHES(enc, ptr, ASCII_RSQB)) {
  1053. REQUIRE_CHARS(enc, ptr, end, 2);
  1054. if (CHAR_MATCHES(enc, ptr + MINBPC(enc), ASCII_GT)) {
  1055. *nextTokPtr = ptr + 2 * MINBPC(enc);
  1056. return XML_TOK_COND_SECT_CLOSE;
  1057. }
  1058. }
  1059. *nextTokPtr = ptr;
  1060. return XML_TOK_CLOSE_BRACKET;
  1061. case BT_LPAR:
  1062. *nextTokPtr = ptr + MINBPC(enc);
  1063. return XML_TOK_OPEN_PAREN;
  1064. case BT_RPAR:
  1065. ptr += MINBPC(enc);
  1066. if (! HAS_CHAR(enc, ptr, end))
  1067. return -XML_TOK_CLOSE_PAREN;
  1068. switch (BYTE_TYPE(enc, ptr)) {
  1069. case BT_AST:
  1070. *nextTokPtr = ptr + MINBPC(enc);
  1071. return XML_TOK_CLOSE_PAREN_ASTERISK;
  1072. case BT_QUEST:
  1073. *nextTokPtr = ptr + MINBPC(enc);
  1074. return XML_TOK_CLOSE_PAREN_QUESTION;
  1075. case BT_PLUS:
  1076. *nextTokPtr = ptr + MINBPC(enc);
  1077. return XML_TOK_CLOSE_PAREN_PLUS;
  1078. case BT_CR:
  1079. case BT_LF:
  1080. case BT_S:
  1081. case BT_GT:
  1082. case BT_COMMA:
  1083. case BT_VERBAR:
  1084. case BT_RPAR:
  1085. *nextTokPtr = ptr;
  1086. return XML_TOK_CLOSE_PAREN;
  1087. }
  1088. *nextTokPtr = ptr;
  1089. return XML_TOK_INVALID;
  1090. case BT_VERBAR:
  1091. *nextTokPtr = ptr + MINBPC(enc);
  1092. return XML_TOK_OR;
  1093. case BT_GT:
  1094. *nextTokPtr = ptr + MINBPC(enc);
  1095. return XML_TOK_DECL_CLOSE;
  1096. case BT_NUM:
  1097. return PREFIX(scanPoundName)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  1098. # define LEAD_CASE(n) \
  1099. case BT_LEAD##n: \
  1100. if (end - ptr < n) \
  1101. return XML_TOK_PARTIAL_CHAR; \
  1102. if (IS_INVALID_CHAR(enc, ptr, n)) { \
  1103. *nextTokPtr = ptr; \
  1104. return XML_TOK_INVALID; \
  1105. } \
  1106. if (IS_NMSTRT_CHAR(enc, ptr, n)) { \
  1107. ptr += n; \
  1108. tok = XML_TOK_NAME; \
  1109. break; \
  1110. } \
  1111. if (IS_NAME_CHAR(enc, ptr, n)) { \
  1112. ptr += n; \
  1113. tok = XML_TOK_NMTOKEN; \
  1114. break; \
  1115. } \
  1116. *nextTokPtr = ptr; \
  1117. return XML_TOK_INVALID;
  1118. LEAD_CASE(2)
  1119. LEAD_CASE(3)
  1120. LEAD_CASE(4)
  1121. # undef LEAD_CASE
  1122. case BT_NMSTRT:
  1123. case BT_HEX:
  1124. tok = XML_TOK_NAME;
  1125. ptr += MINBPC(enc);
  1126. break;
  1127. case BT_DIGIT:
  1128. case BT_NAME:
  1129. case BT_MINUS:
  1130. # ifdef XML_NS
  1131. case BT_COLON:
  1132. # endif
  1133. tok = XML_TOK_NMTOKEN;
  1134. ptr += MINBPC(enc);
  1135. break;
  1136. case BT_NONASCII:
  1137. if (IS_NMSTRT_CHAR_MINBPC(enc, ptr)) {
  1138. ptr += MINBPC(enc);
  1139. tok = XML_TOK_NAME;
  1140. break;
  1141. }
  1142. if (IS_NAME_CHAR_MINBPC(enc, ptr)) {
  1143. ptr += MINBPC(enc);
  1144. tok = XML_TOK_NMTOKEN;
  1145. break;
  1146. }
  1147. /* fall through */
  1148. default:
  1149. *nextTokPtr = ptr;
  1150. return XML_TOK_INVALID;
  1151. }
  1152. while (HAS_CHAR(enc, ptr, end)) {
  1153. switch (BYTE_TYPE(enc, ptr)) {
  1154. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  1155. case BT_GT:
  1156. case BT_RPAR:
  1157. case BT_COMMA:
  1158. case BT_VERBAR:
  1159. case BT_LSQB:
  1160. case BT_PERCNT:
  1161. case BT_S:
  1162. case BT_CR:
  1163. case BT_LF:
  1164. *nextTokPtr = ptr;
  1165. return tok;
  1166. # ifdef XML_NS
  1167. case BT_COLON:
  1168. ptr += MINBPC(enc);
  1169. switch (tok) {
  1170. case XML_TOK_NAME:
  1171. REQUIRE_CHAR(enc, ptr, end);
  1172. tok = XML_TOK_PREFIXED_NAME;
  1173. switch (BYTE_TYPE(enc, ptr)) {
  1174. CHECK_NAME_CASES(enc, ptr, end, nextTokPtr)
  1175. default:
  1176. tok = XML_TOK_NMTOKEN;
  1177. break;
  1178. }
  1179. break;
  1180. case XML_TOK_PREFIXED_NAME:
  1181. tok = XML_TOK_NMTOKEN;
  1182. break;
  1183. }
  1184. break;
  1185. # endif
  1186. case BT_PLUS:
  1187. if (tok == XML_TOK_NMTOKEN) {
  1188. *nextTokPtr = ptr;
  1189. return XML_TOK_INVALID;
  1190. }
  1191. *nextTokPtr = ptr + MINBPC(enc);
  1192. return XML_TOK_NAME_PLUS;
  1193. case BT_AST:
  1194. if (tok == XML_TOK_NMTOKEN) {
  1195. *nextTokPtr = ptr;
  1196. return XML_TOK_INVALID;
  1197. }
  1198. *nextTokPtr = ptr + MINBPC(enc);
  1199. return XML_TOK_NAME_ASTERISK;
  1200. case BT_QUEST:
  1201. if (tok == XML_TOK_NMTOKEN) {
  1202. *nextTokPtr = ptr;
  1203. return XML_TOK_INVALID;
  1204. }
  1205. *nextTokPtr = ptr + MINBPC(enc);
  1206. return XML_TOK_NAME_QUESTION;
  1207. default:
  1208. *nextTokPtr = ptr;
  1209. return XML_TOK_INVALID;
  1210. }
  1211. }
  1212. return -tok;
  1213. }
  1214. static int PTRCALL
  1215. PREFIX(attributeValueTok)(const ENCODING *enc, const char *ptr, const char *end,
  1216. const char **nextTokPtr) {
  1217. const char *start;
  1218. if (ptr >= end)
  1219. return XML_TOK_NONE;
  1220. else if (! HAS_CHAR(enc, ptr, end)) {
  1221. /* This line cannot be executed. The incoming data has already
  1222. * been tokenized once, so incomplete characters like this have
  1223. * already been eliminated from the input. Retaining the paranoia
  1224. * check is still valuable, however.
  1225. */
  1226. return XML_TOK_PARTIAL; /* LCOV_EXCL_LINE */
  1227. }
  1228. start = ptr;
  1229. while (HAS_CHAR(enc, ptr, end)) {
  1230. switch (BYTE_TYPE(enc, ptr)) {
  1231. # define LEAD_CASE(n) \
  1232. case BT_LEAD##n: \
  1233. ptr += n; /* NOTE: The encoding has already been validated. */ \
  1234. break;
  1235. LEAD_CASE(2)
  1236. LEAD_CASE(3)
  1237. LEAD_CASE(4)
  1238. # undef LEAD_CASE
  1239. case BT_AMP:
  1240. if (ptr == start)
  1241. return PREFIX(scanRef)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  1242. *nextTokPtr = ptr;
  1243. return XML_TOK_DATA_CHARS;
  1244. case BT_LT:
  1245. /* this is for inside entity references */
  1246. *nextTokPtr = ptr;
  1247. return XML_TOK_INVALID;
  1248. case BT_LF:
  1249. if (ptr == start) {
  1250. *nextTokPtr = ptr + MINBPC(enc);
  1251. return XML_TOK_DATA_NEWLINE;
  1252. }
  1253. *nextTokPtr = ptr;
  1254. return XML_TOK_DATA_CHARS;
  1255. case BT_CR:
  1256. if (ptr == start) {
  1257. ptr += MINBPC(enc);
  1258. if (! HAS_CHAR(enc, ptr, end))
  1259. return XML_TOK_TRAILING_CR;
  1260. if (BYTE_TYPE(enc, ptr) == BT_LF)
  1261. ptr += MINBPC(enc);
  1262. *nextTokPtr = ptr;
  1263. return XML_TOK_DATA_NEWLINE;
  1264. }
  1265. *nextTokPtr = ptr;
  1266. return XML_TOK_DATA_CHARS;
  1267. case BT_S:
  1268. if (ptr == start) {
  1269. *nextTokPtr = ptr + MINBPC(enc);
  1270. return XML_TOK_ATTRIBUTE_VALUE_S;
  1271. }
  1272. *nextTokPtr = ptr;
  1273. return XML_TOK_DATA_CHARS;
  1274. default:
  1275. ptr += MINBPC(enc);
  1276. break;
  1277. }
  1278. }
  1279. *nextTokPtr = ptr;
  1280. return XML_TOK_DATA_CHARS;
  1281. }
  1282. static int PTRCALL
  1283. PREFIX(entityValueTok)(const ENCODING *enc, const char *ptr, const char *end,
  1284. const char **nextTokPtr) {
  1285. const char *start;
  1286. if (ptr >= end)
  1287. return XML_TOK_NONE;
  1288. else if (! HAS_CHAR(enc, ptr, end)) {
  1289. /* This line cannot be executed. The incoming data has already
  1290. * been tokenized once, so incomplete characters like this have
  1291. * already been eliminated from the input. Retaining the paranoia
  1292. * check is still valuable, however.
  1293. */
  1294. return XML_TOK_PARTIAL; /* LCOV_EXCL_LINE */
  1295. }
  1296. start = ptr;
  1297. while (HAS_CHAR(enc, ptr, end)) {
  1298. switch (BYTE_TYPE(enc, ptr)) {
  1299. # define LEAD_CASE(n) \
  1300. case BT_LEAD##n: \
  1301. ptr += n; /* NOTE: The encoding has already been validated. */ \
  1302. break;
  1303. LEAD_CASE(2)
  1304. LEAD_CASE(3)
  1305. LEAD_CASE(4)
  1306. # undef LEAD_CASE
  1307. case BT_AMP:
  1308. if (ptr == start)
  1309. return PREFIX(scanRef)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  1310. *nextTokPtr = ptr;
  1311. return XML_TOK_DATA_CHARS;
  1312. case BT_PERCNT:
  1313. if (ptr == start) {
  1314. int tok = PREFIX(scanPercent)(enc, ptr + MINBPC(enc), end, nextTokPtr);
  1315. return (tok == XML_TOK_PERCENT) ? XML_TOK_INVALID : tok;
  1316. }
  1317. *nextTokPtr = ptr;
  1318. return XML_TOK_DATA_CHARS;
  1319. case BT_LF:
  1320. if (ptr == start) {
  1321. *nextTokPtr = ptr + MINBPC(enc);
  1322. return XML_TOK_DATA_NEWLINE;
  1323. }
  1324. *nextTokPtr = ptr;
  1325. return XML_TOK_DATA_CHARS;
  1326. case BT_CR:
  1327. if (ptr == start) {
  1328. ptr += MINBPC(enc);
  1329. if (! HAS_CHAR(enc, ptr, end))
  1330. return XML_TOK_TRAILING_CR;
  1331. if (BYTE_TYPE(enc, ptr) == BT_LF)
  1332. ptr += MINBPC(enc);
  1333. *nextTokPtr = ptr;
  1334. return XML_TOK_DATA_NEWLINE;
  1335. }
  1336. *nextTokPtr = ptr;
  1337. return XML_TOK_DATA_CHARS;
  1338. default:
  1339. ptr += MINBPC(enc);
  1340. break;
  1341. }
  1342. }
  1343. *nextTokPtr = ptr;
  1344. return XML_TOK_DATA_CHARS;
  1345. }
  1346. # ifdef XML_DTD
  1347. static int PTRCALL
  1348. PREFIX(ignoreSectionTok)(const ENCODING *enc, const char *ptr, const char *end,
  1349. const char **nextTokPtr) {
  1350. int level = 0;
  1351. if (MINBPC(enc) > 1) {
  1352. size_t n = end - ptr;
  1353. if (n & (MINBPC(enc) - 1)) {
  1354. n &= ~(MINBPC(enc) - 1);
  1355. end = ptr + n;
  1356. }
  1357. }
  1358. while (HAS_CHAR(enc, ptr, end)) {
  1359. switch (BYTE_TYPE(enc, ptr)) {
  1360. INVALID_CASES(ptr, nextTokPtr)
  1361. case BT_LT:
  1362. ptr += MINBPC(enc);
  1363. REQUIRE_CHAR(enc, ptr, end);
  1364. if (CHAR_MATCHES(enc, ptr, ASCII_EXCL)) {
  1365. ptr += MINBPC(enc);
  1366. REQUIRE_CHAR(enc, ptr, end);
  1367. if (CHAR_MATCHES(enc, ptr, ASCII_LSQB)) {
  1368. ++level;
  1369. ptr += MINBPC(enc);
  1370. }
  1371. }
  1372. break;
  1373. case BT_RSQB:
  1374. ptr += MINBPC(enc);
  1375. REQUIRE_CHAR(enc, ptr, end);
  1376. if (CHAR_MATCHES(enc, ptr, ASCII_RSQB)) {
  1377. ptr += MINBPC(enc);
  1378. REQUIRE_CHAR(enc, ptr, end);
  1379. if (CHAR_MATCHES(enc, ptr, ASCII_GT)) {
  1380. ptr += MINBPC(enc);
  1381. if (level == 0) {
  1382. *nextTokPtr = ptr;
  1383. return XML_TOK_IGNORE_SECT;
  1384. }
  1385. --level;
  1386. }
  1387. }
  1388. break;
  1389. default:
  1390. ptr += MINBPC(enc);
  1391. break;
  1392. }
  1393. }
  1394. return XML_TOK_PARTIAL;
  1395. }
  1396. # endif /* XML_DTD */
  1397. static int PTRCALL
  1398. PREFIX(isPublicId)(const ENCODING *enc, const char *ptr, const char *end,
  1399. const char **badPtr) {
  1400. ptr += MINBPC(enc);
  1401. end -= MINBPC(enc);
  1402. for (; HAS_CHAR(enc, ptr, end); ptr += MINBPC(enc)) {
  1403. switch (BYTE_TYPE(enc, ptr)) {
  1404. case BT_DIGIT:
  1405. case BT_HEX:
  1406. case BT_MINUS:
  1407. case BT_APOS:
  1408. case BT_LPAR:
  1409. case BT_RPAR:
  1410. case BT_PLUS:
  1411. case BT_COMMA:
  1412. case BT_SOL:
  1413. case BT_EQUALS:
  1414. case BT_QUEST:
  1415. case BT_CR:
  1416. case BT_LF:
  1417. case BT_SEMI:
  1418. case BT_EXCL:
  1419. case BT_AST:
  1420. case BT_PERCNT:
  1421. case BT_NUM:
  1422. # ifdef XML_NS
  1423. case BT_COLON:
  1424. # endif
  1425. break;
  1426. case BT_S:
  1427. if (CHAR_MATCHES(enc, ptr, ASCII_TAB)) {
  1428. *badPtr = ptr;
  1429. return 0;
  1430. }
  1431. break;
  1432. case BT_NAME:
  1433. case BT_NMSTRT:
  1434. if (! (BYTE_TO_ASCII(enc, ptr) & ~0x7f))
  1435. break;
  1436. /* fall through */
  1437. default:
  1438. switch (BYTE_TO_ASCII(enc, ptr)) {
  1439. case 0x24: /* $ */
  1440. case 0x40: /* @ */
  1441. break;
  1442. default:
  1443. *badPtr = ptr;
  1444. return 0;
  1445. }
  1446. break;
  1447. }
  1448. }
  1449. return 1;
  1450. }
  1451. /* This must only be called for a well-formed start-tag or empty
  1452. element tag. Returns the number of attributes. Pointers to the
  1453. first attsMax attributes are stored in atts.
  1454. */
  1455. static int PTRCALL
  1456. PREFIX(getAtts)(const ENCODING *enc, const char *ptr, int attsMax,
  1457. ATTRIBUTE *atts) {
  1458. enum { other, inName, inValue } state = inName;
  1459. int nAtts = 0;
  1460. int open = 0; /* defined when state == inValue;
  1461. initialization just to shut up compilers */
  1462. for (ptr += MINBPC(enc);; ptr += MINBPC(enc)) {
  1463. switch (BYTE_TYPE(enc, ptr)) {
  1464. # define START_NAME \
  1465. if (state == other) { \
  1466. if (nAtts < attsMax) { \
  1467. atts[nAtts].name = ptr; \
  1468. atts[nAtts].normalized = 1; \
  1469. } \
  1470. state = inName; \
  1471. }
  1472. # define LEAD_CASE(n) \
  1473. case BT_LEAD##n: /* NOTE: The encoding has already been validated. */ \
  1474. START_NAME ptr += (n - MINBPC(enc)); \
  1475. break;
  1476. LEAD_CASE(2)
  1477. LEAD_CASE(3)
  1478. LEAD_CASE(4)
  1479. # undef LEAD_CASE
  1480. case BT_NONASCII:
  1481. case BT_NMSTRT:
  1482. case BT_HEX:
  1483. START_NAME
  1484. break;
  1485. # undef START_NAME
  1486. case BT_QUOT:
  1487. if (state != inValue) {
  1488. if (nAtts < attsMax)
  1489. atts[nAtts].valuePtr = ptr + MINBPC(enc);
  1490. state = inValue;
  1491. open = BT_QUOT;
  1492. } else if (open == BT_QUOT) {
  1493. state = other;
  1494. if (nAtts < attsMax)
  1495. atts[nAtts].valueEnd = ptr;
  1496. nAtts++;
  1497. }
  1498. break;
  1499. case BT_APOS:
  1500. if (state != inValue) {
  1501. if (nAtts < attsMax)
  1502. atts[nAtts].valuePtr = ptr + MINBPC(enc);
  1503. state = inValue;
  1504. open = BT_APOS;
  1505. } else if (open == BT_APOS) {
  1506. state = other;
  1507. if (nAtts < attsMax)
  1508. atts[nAtts].valueEnd = ptr;
  1509. nAtts++;
  1510. }
  1511. break;
  1512. case BT_AMP:
  1513. if (nAtts < attsMax)
  1514. atts[nAtts].normalized = 0;
  1515. break;
  1516. case BT_S:
  1517. if (state == inName)
  1518. state = other;
  1519. else if (state == inValue && nAtts < attsMax && atts[nAtts].normalized
  1520. && (ptr == atts[nAtts].valuePtr
  1521. || BYTE_TO_ASCII(enc, ptr) != ASCII_SPACE
  1522. || BYTE_TO_ASCII(enc, ptr + MINBPC(enc)) == ASCII_SPACE
  1523. || BYTE_TYPE(enc, ptr + MINBPC(enc)) == open))
  1524. atts[nAtts].normalized = 0;
  1525. break;
  1526. case BT_CR:
  1527. case BT_LF:
  1528. /* This case ensures that the first attribute name is counted
  1529. Apart from that we could just change state on the quote. */
  1530. if (state == inName)
  1531. state = other;
  1532. else if (state == inValue && nAtts < attsMax)
  1533. atts[nAtts].normalized = 0;
  1534. break;
  1535. case BT_GT:
  1536. case BT_SOL:
  1537. if (state != inValue)
  1538. return nAtts;
  1539. break;
  1540. default:
  1541. break;
  1542. }
  1543. }
  1544. /* not reached */
  1545. }
  1546. static int PTRFASTCALL
  1547. PREFIX(charRefNumber)(const ENCODING *enc, const char *ptr) {
  1548. int result = 0;
  1549. /* skip &# */
  1550. UNUSED_P(enc);
  1551. ptr += 2 * MINBPC(enc);
  1552. if (CHAR_MATCHES(enc, ptr, ASCII_x)) {
  1553. for (ptr += MINBPC(enc); ! CHAR_MATCHES(enc, ptr, ASCII_SEMI);
  1554. ptr += MINBPC(enc)) {
  1555. int c = BYTE_TO_ASCII(enc, ptr);
  1556. switch (c) {
  1557. case ASCII_0:
  1558. case ASCII_1:
  1559. case ASCII_2:
  1560. case ASCII_3:
  1561. case ASCII_4:
  1562. case ASCII_5:
  1563. case ASCII_6:
  1564. case ASCII_7:
  1565. case ASCII_8:
  1566. case ASCII_9:
  1567. result <<= 4;
  1568. result |= (c - ASCII_0);
  1569. break;
  1570. case ASCII_A:
  1571. case ASCII_B:
  1572. case ASCII_C:
  1573. case ASCII_D:
  1574. case ASCII_E:
  1575. case ASCII_F:
  1576. result <<= 4;
  1577. result += 10 + (c - ASCII_A);
  1578. break;
  1579. case ASCII_a:
  1580. case ASCII_b:
  1581. case ASCII_c:
  1582. case ASCII_d:
  1583. case ASCII_e:
  1584. case ASCII_f:
  1585. result <<= 4;
  1586. result += 10 + (c - ASCII_a);
  1587. break;
  1588. }
  1589. if (result >= 0x110000)
  1590. return -1;
  1591. }
  1592. } else {
  1593. for (; ! CHAR_MATCHES(enc, ptr, ASCII_SEMI); ptr += MINBPC(enc)) {
  1594. int c = BYTE_TO_ASCII(enc, ptr);
  1595. result *= 10;
  1596. result += (c - ASCII_0);
  1597. if (result >= 0x110000)
  1598. return -1;
  1599. }
  1600. }
  1601. return checkCharRefNumber(result);
  1602. }
  1603. static int PTRCALL
  1604. PREFIX(predefinedEntityName)(const ENCODING *enc, const char *ptr,
  1605. const char *end) {
  1606. UNUSED_P(enc);
  1607. switch ((end - ptr) / MINBPC(enc)) {
  1608. case 2:
  1609. if (CHAR_MATCHES(enc, ptr + MINBPC(enc), ASCII_t)) {
  1610. switch (BYTE_TO_ASCII(enc, ptr)) {
  1611. case ASCII_l:
  1612. return ASCII_LT;
  1613. case ASCII_g:
  1614. return ASCII_GT;
  1615. }
  1616. }
  1617. break;
  1618. case 3:
  1619. if (CHAR_MATCHES(enc, ptr, ASCII_a)) {
  1620. ptr += MINBPC(enc);
  1621. if (CHAR_MATCHES(enc, ptr, ASCII_m)) {
  1622. ptr += MINBPC(enc);
  1623. if (CHAR_MATCHES(enc, ptr, ASCII_p))
  1624. return ASCII_AMP;
  1625. }
  1626. }
  1627. break;
  1628. case 4:
  1629. switch (BYTE_TO_ASCII(enc, ptr)) {
  1630. case ASCII_q:
  1631. ptr += MINBPC(enc);
  1632. if (CHAR_MATCHES(enc, ptr, ASCII_u)) {
  1633. ptr += MINBPC(enc);
  1634. if (CHAR_MATCHES(enc, ptr, ASCII_o)) {
  1635. ptr += MINBPC(enc);
  1636. if (CHAR_MATCHES(enc, ptr, ASCII_t))
  1637. return ASCII_QUOT;
  1638. }
  1639. }
  1640. break;
  1641. case ASCII_a:
  1642. ptr += MINBPC(enc);
  1643. if (CHAR_MATCHES(enc, ptr, ASCII_p)) {
  1644. ptr += MINBPC(enc);
  1645. if (CHAR_MATCHES(enc, ptr, ASCII_o)) {
  1646. ptr += MINBPC(enc);
  1647. if (CHAR_MATCHES(enc, ptr, ASCII_s))
  1648. return ASCII_APOS;
  1649. }
  1650. }
  1651. break;
  1652. }
  1653. }
  1654. return 0;
  1655. }
  1656. static int PTRCALL
  1657. PREFIX(nameMatchesAscii)(const ENCODING *enc, const char *ptr1,
  1658. const char *end1, const char *ptr2) {
  1659. UNUSED_P(enc);
  1660. for (; *ptr2; ptr1 += MINBPC(enc), ptr2++) {
  1661. if (end1 - ptr1 < MINBPC(enc)) {
  1662. /* This line cannot be executed. The incoming data has already
  1663. * been tokenized once, so incomplete characters like this have
  1664. * already been eliminated from the input. Retaining the
  1665. * paranoia check is still valuable, however.
  1666. */
  1667. return 0; /* LCOV_EXCL_LINE */
  1668. }
  1669. if (! CHAR_MATCHES(enc, ptr1, *ptr2))
  1670. return 0;
  1671. }
  1672. return ptr1 == end1;
  1673. }
  1674. static int PTRFASTCALL
  1675. PREFIX(nameLength)(const ENCODING *enc, const char *ptr) {
  1676. const char *start = ptr;
  1677. for (;;) {
  1678. switch (BYTE_TYPE(enc, ptr)) {
  1679. # define LEAD_CASE(n) \
  1680. case BT_LEAD##n: \
  1681. ptr += n; /* NOTE: The encoding has already been validated. */ \
  1682. break;
  1683. LEAD_CASE(2)
  1684. LEAD_CASE(3)
  1685. LEAD_CASE(4)
  1686. # undef LEAD_CASE
  1687. case BT_NONASCII:
  1688. case BT_NMSTRT:
  1689. # ifdef XML_NS
  1690. case BT_COLON:
  1691. # endif
  1692. case BT_HEX:
  1693. case BT_DIGIT:
  1694. case BT_NAME:
  1695. case BT_MINUS:
  1696. ptr += MINBPC(enc);
  1697. break;
  1698. default:
  1699. return (int)(ptr - start);
  1700. }
  1701. }
  1702. }
  1703. static const char *PTRFASTCALL
  1704. PREFIX(skipS)(const ENCODING *enc, const char *ptr) {
  1705. for (;;) {
  1706. switch (BYTE_TYPE(enc, ptr)) {
  1707. case BT_LF:
  1708. case BT_CR:
  1709. case BT_S:
  1710. ptr += MINBPC(enc);
  1711. break;
  1712. default:
  1713. return ptr;
  1714. }
  1715. }
  1716. }
  1717. static void PTRCALL
  1718. PREFIX(updatePosition)(const ENCODING *enc, const char *ptr, const char *end,
  1719. POSITION *pos) {
  1720. while (HAS_CHAR(enc, ptr, end)) {
  1721. switch (BYTE_TYPE(enc, ptr)) {
  1722. # define LEAD_CASE(n) \
  1723. case BT_LEAD##n: \
  1724. ptr += n; /* NOTE: The encoding has already been validated. */ \
  1725. pos->columnNumber++; \
  1726. break;
  1727. LEAD_CASE(2)
  1728. LEAD_CASE(3)
  1729. LEAD_CASE(4)
  1730. # undef LEAD_CASE
  1731. case BT_LF:
  1732. pos->columnNumber = 0;
  1733. pos->lineNumber++;
  1734. ptr += MINBPC(enc);
  1735. break;
  1736. case BT_CR:
  1737. pos->lineNumber++;
  1738. ptr += MINBPC(enc);
  1739. if (HAS_CHAR(enc, ptr, end) && BYTE_TYPE(enc, ptr) == BT_LF)
  1740. ptr += MINBPC(enc);
  1741. pos->columnNumber = 0;
  1742. break;
  1743. default:
  1744. ptr += MINBPC(enc);
  1745. pos->columnNumber++;
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. # undef DO_LEAD_CASE
  1751. # undef MULTIBYTE_CASES
  1752. # undef INVALID_CASES
  1753. # undef CHECK_NAME_CASE
  1754. # undef CHECK_NAME_CASES
  1755. # undef CHECK_NMSTRT_CASE
  1756. # undef CHECK_NMSTRT_CASES
  1757. #endif /* XML_TOK_IMPL_C */