xmltok.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
  10. Copyright (c) 2001-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
  11. Copyright (c) 2002 Greg Stein <gstein@users.sourceforge.net>
  12. Copyright (c) 2002-2016 Karl Waclawek <karl@waclawek.net>
  13. Copyright (c) 2005-2009 Steven Solie <steven@solie.ca>
  14. Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
  15. Copyright (c) 2016 Pascal Cuoq <cuoq@trust-in-soft.com>
  16. Copyright (c) 2016 Don Lewis <truckman@apache.org>
  17. Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
  18. Copyright (c) 2017 Alexander Bluhm <alexander.bluhm@gmx.net>
  19. Copyright (c) 2017 Benbuck Nason <bnason@netflix.com>
  20. Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com>
  21. Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
  22. Copyright (c) 2021 Donghee Na <donghee.na@python.org>
  23. Copyright (c) 2022 Martin Ettl <ettl.martin78@googlemail.com>
  24. Copyright (c) 2022 Sean McBride <sean@rogue-research.com>
  25. Copyright (c) 2023 Hanno Böck <hanno@gentoo.org>
  26. Licensed under the MIT license:
  27. Permission is hereby granted, free of charge, to any person obtaining
  28. a copy of this software and associated documentation files (the
  29. "Software"), to deal in the Software without restriction, including
  30. without limitation the rights to use, copy, modify, merge, publish,
  31. distribute, sublicense, and/or sell copies of the Software, and to permit
  32. persons to whom the Software is furnished to do so, subject to the
  33. following conditions:
  34. The above copyright notice and this permission notice shall be included
  35. in all copies or substantial portions of the Software.
  36. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  37. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  38. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  39. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  40. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  41. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  42. USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. */
  44. #include "expat_config.h"
  45. #include <stddef.h>
  46. #include <string.h> /* memcpy */
  47. #include <stdbool.h>
  48. #ifdef _WIN32
  49. # include "winconfig.h"
  50. #endif
  51. #include "expat_external.h"
  52. #include "internal.h"
  53. #include "xmltok.h"
  54. #include "nametab.h"
  55. #ifdef XML_DTD
  56. # define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
  57. #else
  58. # define IGNORE_SECTION_TOK_VTABLE /* as nothing */
  59. #endif
  60. #define VTABLE1 \
  61. {PREFIX(prologTok), PREFIX(contentTok), \
  62. PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE}, \
  63. {PREFIX(attributeValueTok), PREFIX(entityValueTok)}, \
  64. PREFIX(nameMatchesAscii), PREFIX(nameLength), PREFIX(skipS), \
  65. PREFIX(getAtts), PREFIX(charRefNumber), PREFIX(predefinedEntityName), \
  66. PREFIX(updatePosition), PREFIX(isPublicId)
  67. #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
  68. #define UCS2_GET_NAMING(pages, hi, lo) \
  69. (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1u << ((lo) & 0x1F)))
  70. /* A 2 byte UTF-8 representation splits the characters 11 bits between
  71. the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
  72. pages, 3 bits to add to that index and 5 bits to generate the mask.
  73. */
  74. #define UTF8_GET_NAMING2(pages, byte) \
  75. (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
  76. + ((((byte)[0]) & 3) << 1) + ((((byte)[1]) >> 5) & 1)] \
  77. & (1u << (((byte)[1]) & 0x1F)))
  78. /* A 3 byte UTF-8 representation splits the characters 16 bits between
  79. the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
  80. into pages, 3 bits to add to that index and 5 bits to generate the
  81. mask.
  82. */
  83. #define UTF8_GET_NAMING3(pages, byte) \
  84. (namingBitmap \
  85. [((pages)[((((byte)[0]) & 0xF) << 4) + ((((byte)[1]) >> 2) & 0xF)] \
  86. << 3) \
  87. + ((((byte)[1]) & 3) << 1) + ((((byte)[2]) >> 5) & 1)] \
  88. & (1u << (((byte)[2]) & 0x1F)))
  89. /* Detection of invalid UTF-8 sequences is based on Table 3.1B
  90. of Unicode 3.2: https://www.unicode.org/unicode/reports/tr28/
  91. with the additional restriction of not allowing the Unicode
  92. code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
  93. Implementation details:
  94. (A & 0x80) == 0 means A < 0x80
  95. and
  96. (A & 0xC0) == 0xC0 means A > 0xBF
  97. */
  98. #define UTF8_INVALID2(p) \
  99. ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
  100. #define UTF8_INVALID3(p) \
  101. (((p)[2] & 0x80) == 0 \
  102. || ((*p) == 0xEF && (p)[1] == 0xBF ? (p)[2] > 0xBD \
  103. : ((p)[2] & 0xC0) == 0xC0) \
  104. || ((*p) == 0xE0 \
  105. ? (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
  106. : ((p)[1] & 0x80) == 0 \
  107. || ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
  108. #define UTF8_INVALID4(p) \
  109. (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 || ((p)[2] & 0x80) == 0 \
  110. || ((p)[2] & 0xC0) == 0xC0 \
  111. || ((*p) == 0xF0 \
  112. ? (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
  113. : ((p)[1] & 0x80) == 0 \
  114. || ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
  115. static int PTRFASTCALL
  116. isNever(const ENCODING *enc, const char *p) {
  117. UNUSED_P(enc);
  118. UNUSED_P(p);
  119. return 0;
  120. }
  121. static int PTRFASTCALL
  122. utf8_isName2(const ENCODING *enc, const char *p) {
  123. UNUSED_P(enc);
  124. return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
  125. }
  126. static int PTRFASTCALL
  127. utf8_isName3(const ENCODING *enc, const char *p) {
  128. UNUSED_P(enc);
  129. return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
  130. }
  131. #define utf8_isName4 isNever
  132. static int PTRFASTCALL
  133. utf8_isNmstrt2(const ENCODING *enc, const char *p) {
  134. UNUSED_P(enc);
  135. return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
  136. }
  137. static int PTRFASTCALL
  138. utf8_isNmstrt3(const ENCODING *enc, const char *p) {
  139. UNUSED_P(enc);
  140. return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
  141. }
  142. #define utf8_isNmstrt4 isNever
  143. static int PTRFASTCALL
  144. utf8_isInvalid2(const ENCODING *enc, const char *p) {
  145. UNUSED_P(enc);
  146. return UTF8_INVALID2((const unsigned char *)p);
  147. }
  148. static int PTRFASTCALL
  149. utf8_isInvalid3(const ENCODING *enc, const char *p) {
  150. UNUSED_P(enc);
  151. return UTF8_INVALID3((const unsigned char *)p);
  152. }
  153. static int PTRFASTCALL
  154. utf8_isInvalid4(const ENCODING *enc, const char *p) {
  155. UNUSED_P(enc);
  156. return UTF8_INVALID4((const unsigned char *)p);
  157. }
  158. struct normal_encoding {
  159. ENCODING enc;
  160. unsigned char type[256];
  161. #ifdef XML_MIN_SIZE
  162. int(PTRFASTCALL *byteType)(const ENCODING *, const char *);
  163. int(PTRFASTCALL *isNameMin)(const ENCODING *, const char *);
  164. int(PTRFASTCALL *isNmstrtMin)(const ENCODING *, const char *);
  165. int(PTRFASTCALL *byteToAscii)(const ENCODING *, const char *);
  166. int(PTRCALL *charMatches)(const ENCODING *, const char *, int);
  167. #endif /* XML_MIN_SIZE */
  168. int(PTRFASTCALL *isName2)(const ENCODING *, const char *);
  169. int(PTRFASTCALL *isName3)(const ENCODING *, const char *);
  170. int(PTRFASTCALL *isName4)(const ENCODING *, const char *);
  171. int(PTRFASTCALL *isNmstrt2)(const ENCODING *, const char *);
  172. int(PTRFASTCALL *isNmstrt3)(const ENCODING *, const char *);
  173. int(PTRFASTCALL *isNmstrt4)(const ENCODING *, const char *);
  174. int(PTRFASTCALL *isInvalid2)(const ENCODING *, const char *);
  175. int(PTRFASTCALL *isInvalid3)(const ENCODING *, const char *);
  176. int(PTRFASTCALL *isInvalid4)(const ENCODING *, const char *);
  177. };
  178. #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *)(enc))
  179. #ifdef XML_MIN_SIZE
  180. # define STANDARD_VTABLE(E) \
  181. E##byteType, E##isNameMin, E##isNmstrtMin, E##byteToAscii, E##charMatches,
  182. #else
  183. # define STANDARD_VTABLE(E) /* as nothing */
  184. #endif
  185. #define NORMAL_VTABLE(E) \
  186. E##isName2, E##isName3, E##isName4, E##isNmstrt2, E##isNmstrt3, \
  187. E##isNmstrt4, E##isInvalid2, E##isInvalid3, E##isInvalid4
  188. #define NULL_VTABLE \
  189. /* isName2 */ NULL, /* isName3 */ NULL, /* isName4 */ NULL, \
  190. /* isNmstrt2 */ NULL, /* isNmstrt3 */ NULL, /* isNmstrt4 */ NULL, \
  191. /* isInvalid2 */ NULL, /* isInvalid3 */ NULL, /* isInvalid4 */ NULL
  192. static int FASTCALL checkCharRefNumber(int result);
  193. #include "xmltok_impl.h"
  194. #include "ascii.h"
  195. #ifdef XML_MIN_SIZE
  196. # define sb_isNameMin isNever
  197. # define sb_isNmstrtMin isNever
  198. #endif
  199. #ifdef XML_MIN_SIZE
  200. # define MINBPC(enc) ((enc)->minBytesPerChar)
  201. #else
  202. /* minimum bytes per character */
  203. # define MINBPC(enc) 1
  204. #endif
  205. #define SB_BYTE_TYPE(enc, p) \
  206. (((const struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
  207. #ifdef XML_MIN_SIZE
  208. static int PTRFASTCALL
  209. sb_byteType(const ENCODING *enc, const char *p) {
  210. return SB_BYTE_TYPE(enc, p);
  211. }
  212. # define BYTE_TYPE(enc, p) (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
  213. #else
  214. # define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
  215. #endif
  216. #ifdef XML_MIN_SIZE
  217. # define BYTE_TO_ASCII(enc, p) (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
  218. static int PTRFASTCALL
  219. sb_byteToAscii(const ENCODING *enc, const char *p) {
  220. UNUSED_P(enc);
  221. return *p;
  222. }
  223. #else
  224. # define BYTE_TO_ASCII(enc, p) (*(p))
  225. #endif
  226. #define IS_NAME_CHAR(enc, p, n) (AS_NORMAL_ENCODING(enc)->isName##n(enc, p))
  227. #define IS_NMSTRT_CHAR(enc, p, n) (AS_NORMAL_ENCODING(enc)->isNmstrt##n(enc, p))
  228. #ifdef XML_MIN_SIZE
  229. # define IS_INVALID_CHAR(enc, p, n) \
  230. (AS_NORMAL_ENCODING(enc)->isInvalid##n \
  231. && AS_NORMAL_ENCODING(enc)->isInvalid##n(enc, p))
  232. #else
  233. # define IS_INVALID_CHAR(enc, p, n) \
  234. (AS_NORMAL_ENCODING(enc)->isInvalid##n(enc, p))
  235. #endif
  236. #ifdef XML_MIN_SIZE
  237. # define IS_NAME_CHAR_MINBPC(enc, p) \
  238. (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
  239. # define IS_NMSTRT_CHAR_MINBPC(enc, p) \
  240. (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
  241. #else
  242. # define IS_NAME_CHAR_MINBPC(enc, p) (0)
  243. # define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
  244. #endif
  245. #ifdef XML_MIN_SIZE
  246. # define CHAR_MATCHES(enc, p, c) \
  247. (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
  248. static int PTRCALL
  249. sb_charMatches(const ENCODING *enc, const char *p, int c) {
  250. UNUSED_P(enc);
  251. return *p == c;
  252. }
  253. #else
  254. /* c is an ASCII character */
  255. # define CHAR_MATCHES(enc, p, c) (*(p) == (c))
  256. #endif
  257. #define PREFIX(ident) normal_##ident
  258. #define XML_TOK_IMPL_C
  259. #include "xmltok_impl.c"
  260. #undef XML_TOK_IMPL_C
  261. #undef MINBPC
  262. #undef BYTE_TYPE
  263. #undef BYTE_TO_ASCII
  264. #undef CHAR_MATCHES
  265. #undef IS_NAME_CHAR
  266. #undef IS_NAME_CHAR_MINBPC
  267. #undef IS_NMSTRT_CHAR
  268. #undef IS_NMSTRT_CHAR_MINBPC
  269. #undef IS_INVALID_CHAR
  270. enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
  271. UTF8_cval1 = 0x00,
  272. UTF8_cval2 = 0xc0,
  273. UTF8_cval3 = 0xe0,
  274. UTF8_cval4 = 0xf0
  275. };
  276. void
  277. _INTERNAL_trim_to_complete_utf8_characters(const char *from,
  278. const char **fromLimRef) {
  279. const char *fromLim = *fromLimRef;
  280. size_t walked = 0;
  281. for (; fromLim > from; fromLim--, walked++) {
  282. const unsigned char prev = (unsigned char)fromLim[-1];
  283. if ((prev & 0xf8u)
  284. == 0xf0u) { /* 4-byte character, lead by 0b11110xxx byte */
  285. if (walked + 1 >= 4) {
  286. fromLim += 4 - 1;
  287. break;
  288. } else {
  289. walked = 0;
  290. }
  291. } else if ((prev & 0xf0u)
  292. == 0xe0u) { /* 3-byte character, lead by 0b1110xxxx byte */
  293. if (walked + 1 >= 3) {
  294. fromLim += 3 - 1;
  295. break;
  296. } else {
  297. walked = 0;
  298. }
  299. } else if ((prev & 0xe0u)
  300. == 0xc0u) { /* 2-byte character, lead by 0b110xxxxx byte */
  301. if (walked + 1 >= 2) {
  302. fromLim += 2 - 1;
  303. break;
  304. } else {
  305. walked = 0;
  306. }
  307. } else if ((prev & 0x80u)
  308. == 0x00u) { /* 1-byte character, matching 0b0xxxxxxx */
  309. break;
  310. }
  311. }
  312. *fromLimRef = fromLim;
  313. }
  314. static enum XML_Convert_Result PTRCALL
  315. utf8_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
  316. char **toP, const char *toLim) {
  317. bool input_incomplete = false;
  318. bool output_exhausted = false;
  319. /* Avoid copying partial characters (due to limited space). */
  320. const ptrdiff_t bytesAvailable = fromLim - *fromP;
  321. const ptrdiff_t bytesStorable = toLim - *toP;
  322. UNUSED_P(enc);
  323. if (bytesAvailable > bytesStorable) {
  324. fromLim = *fromP + bytesStorable;
  325. output_exhausted = true;
  326. }
  327. /* Avoid copying partial characters (from incomplete input). */
  328. {
  329. const char *const fromLimBefore = fromLim;
  330. _INTERNAL_trim_to_complete_utf8_characters(*fromP, &fromLim);
  331. if (fromLim < fromLimBefore) {
  332. input_incomplete = true;
  333. }
  334. }
  335. {
  336. const ptrdiff_t bytesToCopy = fromLim - *fromP;
  337. memcpy(*toP, *fromP, bytesToCopy);
  338. *fromP += bytesToCopy;
  339. *toP += bytesToCopy;
  340. }
  341. if (output_exhausted) /* needs to go first */
  342. return XML_CONVERT_OUTPUT_EXHAUSTED;
  343. else if (input_incomplete)
  344. return XML_CONVERT_INPUT_INCOMPLETE;
  345. else
  346. return XML_CONVERT_COMPLETED;
  347. }
  348. static enum XML_Convert_Result PTRCALL
  349. utf8_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
  350. unsigned short **toP, const unsigned short *toLim) {
  351. enum XML_Convert_Result res = XML_CONVERT_COMPLETED;
  352. unsigned short *to = *toP;
  353. const char *from = *fromP;
  354. while (from < fromLim && to < toLim) {
  355. switch (SB_BYTE_TYPE(enc, from)) {
  356. case BT_LEAD2:
  357. if (fromLim - from < 2) {
  358. res = XML_CONVERT_INPUT_INCOMPLETE;
  359. goto after;
  360. }
  361. *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
  362. from += 2;
  363. break;
  364. case BT_LEAD3:
  365. if (fromLim - from < 3) {
  366. res = XML_CONVERT_INPUT_INCOMPLETE;
  367. goto after;
  368. }
  369. *to++ = (unsigned short)(((from[0] & 0xf) << 12) | ((from[1] & 0x3f) << 6)
  370. | (from[2] & 0x3f));
  371. from += 3;
  372. break;
  373. case BT_LEAD4: {
  374. unsigned long n;
  375. if (toLim - to < 2) {
  376. res = XML_CONVERT_OUTPUT_EXHAUSTED;
  377. goto after;
  378. }
  379. if (fromLim - from < 4) {
  380. res = XML_CONVERT_INPUT_INCOMPLETE;
  381. goto after;
  382. }
  383. n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
  384. | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
  385. n -= 0x10000;
  386. to[0] = (unsigned short)((n >> 10) | 0xD800);
  387. to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
  388. to += 2;
  389. from += 4;
  390. } break;
  391. default:
  392. *to++ = *from++;
  393. break;
  394. }
  395. }
  396. if (from < fromLim)
  397. res = XML_CONVERT_OUTPUT_EXHAUSTED;
  398. after:
  399. *fromP = from;
  400. *toP = to;
  401. return res;
  402. }
  403. #ifdef XML_NS
  404. static const struct normal_encoding utf8_encoding_ns
  405. = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
  406. {
  407. # include "asciitab.h"
  408. # include "utf8tab.h"
  409. },
  410. STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
  411. #endif
  412. static const struct normal_encoding utf8_encoding
  413. = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
  414. {
  415. #define BT_COLON BT_NMSTRT
  416. #include "asciitab.h"
  417. #undef BT_COLON
  418. #include "utf8tab.h"
  419. },
  420. STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
  421. #ifdef XML_NS
  422. static const struct normal_encoding internal_utf8_encoding_ns
  423. = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
  424. {
  425. # include "iasciitab.h"
  426. # include "utf8tab.h"
  427. },
  428. STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
  429. #endif
  430. static const struct normal_encoding internal_utf8_encoding
  431. = {{VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0},
  432. {
  433. #define BT_COLON BT_NMSTRT
  434. #include "iasciitab.h"
  435. #undef BT_COLON
  436. #include "utf8tab.h"
  437. },
  438. STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)};
  439. static enum XML_Convert_Result PTRCALL
  440. latin1_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
  441. char **toP, const char *toLim) {
  442. UNUSED_P(enc);
  443. for (;;) {
  444. unsigned char c;
  445. if (*fromP == fromLim)
  446. return XML_CONVERT_COMPLETED;
  447. c = (unsigned char)**fromP;
  448. if (c & 0x80) {
  449. if (toLim - *toP < 2)
  450. return XML_CONVERT_OUTPUT_EXHAUSTED;
  451. *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
  452. *(*toP)++ = (char)((c & 0x3f) | 0x80);
  453. (*fromP)++;
  454. } else {
  455. if (*toP == toLim)
  456. return XML_CONVERT_OUTPUT_EXHAUSTED;
  457. *(*toP)++ = *(*fromP)++;
  458. }
  459. }
  460. }
  461. static enum XML_Convert_Result PTRCALL
  462. latin1_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
  463. unsigned short **toP, const unsigned short *toLim) {
  464. UNUSED_P(enc);
  465. while (*fromP < fromLim && *toP < toLim)
  466. *(*toP)++ = (unsigned char)*(*fromP)++;
  467. if ((*toP == toLim) && (*fromP < fromLim))
  468. return XML_CONVERT_OUTPUT_EXHAUSTED;
  469. else
  470. return XML_CONVERT_COMPLETED;
  471. }
  472. #ifdef XML_NS
  473. static const struct normal_encoding latin1_encoding_ns
  474. = {{VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0},
  475. {
  476. # include "asciitab.h"
  477. # include "latin1tab.h"
  478. },
  479. STANDARD_VTABLE(sb_) NULL_VTABLE};
  480. #endif
  481. static const struct normal_encoding latin1_encoding
  482. = {{VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0},
  483. {
  484. #define BT_COLON BT_NMSTRT
  485. #include "asciitab.h"
  486. #undef BT_COLON
  487. #include "latin1tab.h"
  488. },
  489. STANDARD_VTABLE(sb_) NULL_VTABLE};
  490. static enum XML_Convert_Result PTRCALL
  491. ascii_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
  492. char **toP, const char *toLim) {
  493. UNUSED_P(enc);
  494. while (*fromP < fromLim && *toP < toLim)
  495. *(*toP)++ = *(*fromP)++;
  496. if ((*toP == toLim) && (*fromP < fromLim))
  497. return XML_CONVERT_OUTPUT_EXHAUSTED;
  498. else
  499. return XML_CONVERT_COMPLETED;
  500. }
  501. #ifdef XML_NS
  502. static const struct normal_encoding ascii_encoding_ns
  503. = {{VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0},
  504. {
  505. # include "asciitab.h"
  506. /* BT_NONXML == 0 */
  507. },
  508. STANDARD_VTABLE(sb_) NULL_VTABLE};
  509. #endif
  510. static const struct normal_encoding ascii_encoding
  511. = {{VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0},
  512. {
  513. #define BT_COLON BT_NMSTRT
  514. #include "asciitab.h"
  515. #undef BT_COLON
  516. /* BT_NONXML == 0 */
  517. },
  518. STANDARD_VTABLE(sb_) NULL_VTABLE};
  519. static int PTRFASTCALL
  520. unicode_byte_type(char hi, char lo) {
  521. switch ((unsigned char)hi) {
  522. /* 0xD800-0xDBFF first 16-bit code unit or high surrogate (W1) */
  523. case 0xD8:
  524. case 0xD9:
  525. case 0xDA:
  526. case 0xDB:
  527. return BT_LEAD4;
  528. /* 0xDC00-0xDFFF second 16-bit code unit or low surrogate (W2) */
  529. case 0xDC:
  530. case 0xDD:
  531. case 0xDE:
  532. case 0xDF:
  533. return BT_TRAIL;
  534. case 0xFF:
  535. switch ((unsigned char)lo) {
  536. case 0xFF: /* noncharacter-FFFF */
  537. case 0xFE: /* noncharacter-FFFE */
  538. return BT_NONXML;
  539. }
  540. break;
  541. }
  542. return BT_NONASCII;
  543. }
  544. #define DEFINE_UTF16_TO_UTF8(E) \
  545. static enum XML_Convert_Result PTRCALL E##toUtf8( \
  546. const ENCODING *enc, const char **fromP, const char *fromLim, \
  547. char **toP, const char *toLim) { \
  548. const char *from = *fromP; \
  549. UNUSED_P(enc); \
  550. fromLim = from + (((fromLim - from) >> 1) << 1); /* shrink to even */ \
  551. for (; from < fromLim; from += 2) { \
  552. int plane; \
  553. unsigned char lo2; \
  554. unsigned char lo = GET_LO(from); \
  555. unsigned char hi = GET_HI(from); \
  556. switch (hi) { \
  557. case 0: \
  558. if (lo < 0x80) { \
  559. if (*toP == toLim) { \
  560. *fromP = from; \
  561. return XML_CONVERT_OUTPUT_EXHAUSTED; \
  562. } \
  563. *(*toP)++ = lo; \
  564. break; \
  565. } \
  566. /* fall through */ \
  567. case 0x1: \
  568. case 0x2: \
  569. case 0x3: \
  570. case 0x4: \
  571. case 0x5: \
  572. case 0x6: \
  573. case 0x7: \
  574. if (toLim - *toP < 2) { \
  575. *fromP = from; \
  576. return XML_CONVERT_OUTPUT_EXHAUSTED; \
  577. } \
  578. *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
  579. *(*toP)++ = ((lo & 0x3f) | 0x80); \
  580. break; \
  581. default: \
  582. if (toLim - *toP < 3) { \
  583. *fromP = from; \
  584. return XML_CONVERT_OUTPUT_EXHAUSTED; \
  585. } \
  586. /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
  587. *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
  588. *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
  589. *(*toP)++ = ((lo & 0x3f) | 0x80); \
  590. break; \
  591. case 0xD8: \
  592. case 0xD9: \
  593. case 0xDA: \
  594. case 0xDB: \
  595. if (toLim - *toP < 4) { \
  596. *fromP = from; \
  597. return XML_CONVERT_OUTPUT_EXHAUSTED; \
  598. } \
  599. if (fromLim - from < 4) { \
  600. *fromP = from; \
  601. return XML_CONVERT_INPUT_INCOMPLETE; \
  602. } \
  603. plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
  604. *(*toP)++ = (char)((plane >> 2) | UTF8_cval4); \
  605. *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
  606. from += 2; \
  607. lo2 = GET_LO(from); \
  608. *(*toP)++ = (((lo & 0x3) << 4) | ((GET_HI(from) & 0x3) << 2) \
  609. | (lo2 >> 6) | 0x80); \
  610. *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
  611. break; \
  612. } \
  613. } \
  614. *fromP = from; \
  615. if (from < fromLim) \
  616. return XML_CONVERT_INPUT_INCOMPLETE; \
  617. else \
  618. return XML_CONVERT_COMPLETED; \
  619. }
  620. #define DEFINE_UTF16_TO_UTF16(E) \
  621. static enum XML_Convert_Result PTRCALL E##toUtf16( \
  622. const ENCODING *enc, const char **fromP, const char *fromLim, \
  623. unsigned short **toP, const unsigned short *toLim) { \
  624. enum XML_Convert_Result res = XML_CONVERT_COMPLETED; \
  625. UNUSED_P(enc); \
  626. fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1); /* shrink to even */ \
  627. /* Avoid copying first half only of surrogate */ \
  628. if (fromLim - *fromP > ((toLim - *toP) << 1) \
  629. && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) { \
  630. fromLim -= 2; \
  631. res = XML_CONVERT_INPUT_INCOMPLETE; \
  632. } \
  633. for (; *fromP < fromLim && *toP < toLim; *fromP += 2) \
  634. *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
  635. if ((*toP == toLim) && (*fromP < fromLim)) \
  636. return XML_CONVERT_OUTPUT_EXHAUSTED; \
  637. else \
  638. return res; \
  639. }
  640. #define GET_LO(ptr) ((unsigned char)(ptr)[0])
  641. #define GET_HI(ptr) ((unsigned char)(ptr)[1])
  642. DEFINE_UTF16_TO_UTF8(little2_)
  643. DEFINE_UTF16_TO_UTF16(little2_)
  644. #undef GET_LO
  645. #undef GET_HI
  646. #define GET_LO(ptr) ((unsigned char)(ptr)[1])
  647. #define GET_HI(ptr) ((unsigned char)(ptr)[0])
  648. DEFINE_UTF16_TO_UTF8(big2_)
  649. DEFINE_UTF16_TO_UTF16(big2_)
  650. #undef GET_LO
  651. #undef GET_HI
  652. #define LITTLE2_BYTE_TYPE(enc, p) \
  653. ((p)[1] == 0 ? SB_BYTE_TYPE(enc, p) : unicode_byte_type((p)[1], (p)[0]))
  654. #define LITTLE2_BYTE_TO_ASCII(p) ((p)[1] == 0 ? (p)[0] : -1)
  655. #define LITTLE2_CHAR_MATCHES(p, c) ((p)[1] == 0 && (p)[0] == (c))
  656. #define LITTLE2_IS_NAME_CHAR_MINBPC(p) \
  657. UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
  658. #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(p) \
  659. UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
  660. #ifdef XML_MIN_SIZE
  661. static int PTRFASTCALL
  662. little2_byteType(const ENCODING *enc, const char *p) {
  663. return LITTLE2_BYTE_TYPE(enc, p);
  664. }
  665. static int PTRFASTCALL
  666. little2_byteToAscii(const ENCODING *enc, const char *p) {
  667. UNUSED_P(enc);
  668. return LITTLE2_BYTE_TO_ASCII(p);
  669. }
  670. static int PTRCALL
  671. little2_charMatches(const ENCODING *enc, const char *p, int c) {
  672. UNUSED_P(enc);
  673. return LITTLE2_CHAR_MATCHES(p, c);
  674. }
  675. static int PTRFASTCALL
  676. little2_isNameMin(const ENCODING *enc, const char *p) {
  677. UNUSED_P(enc);
  678. return LITTLE2_IS_NAME_CHAR_MINBPC(p);
  679. }
  680. static int PTRFASTCALL
  681. little2_isNmstrtMin(const ENCODING *enc, const char *p) {
  682. UNUSED_P(enc);
  683. return LITTLE2_IS_NMSTRT_CHAR_MINBPC(p);
  684. }
  685. # undef VTABLE
  686. # define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
  687. #else /* not XML_MIN_SIZE */
  688. # undef PREFIX
  689. # define PREFIX(ident) little2_##ident
  690. # define MINBPC(enc) 2
  691. /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
  692. # define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
  693. # define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(p)
  694. # define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(p, c)
  695. # define IS_NAME_CHAR(enc, p, n) 0
  696. # define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(p)
  697. # define IS_NMSTRT_CHAR(enc, p, n) (0)
  698. # define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(p)
  699. # define XML_TOK_IMPL_C
  700. # include "xmltok_impl.c"
  701. # undef XML_TOK_IMPL_C
  702. # undef MINBPC
  703. # undef BYTE_TYPE
  704. # undef BYTE_TO_ASCII
  705. # undef CHAR_MATCHES
  706. # undef IS_NAME_CHAR
  707. # undef IS_NAME_CHAR_MINBPC
  708. # undef IS_NMSTRT_CHAR
  709. # undef IS_NMSTRT_CHAR_MINBPC
  710. # undef IS_INVALID_CHAR
  711. #endif /* not XML_MIN_SIZE */
  712. #ifdef XML_NS
  713. static const struct normal_encoding little2_encoding_ns
  714. = {{VTABLE, 2, 0,
  715. # if BYTEORDER == 1234
  716. 1
  717. # else
  718. 0
  719. # endif
  720. },
  721. {
  722. # include "asciitab.h"
  723. # include "latin1tab.h"
  724. },
  725. STANDARD_VTABLE(little2_) NULL_VTABLE};
  726. #endif
  727. static const struct normal_encoding little2_encoding
  728. = {{VTABLE, 2, 0,
  729. #if BYTEORDER == 1234
  730. 1
  731. #else
  732. 0
  733. #endif
  734. },
  735. {
  736. #define BT_COLON BT_NMSTRT
  737. #include "asciitab.h"
  738. #undef BT_COLON
  739. #include "latin1tab.h"
  740. },
  741. STANDARD_VTABLE(little2_) NULL_VTABLE};
  742. #if BYTEORDER != 4321
  743. # ifdef XML_NS
  744. static const struct normal_encoding internal_little2_encoding_ns
  745. = {{VTABLE, 2, 0, 1},
  746. {
  747. # include "iasciitab.h"
  748. # include "latin1tab.h"
  749. },
  750. STANDARD_VTABLE(little2_) NULL_VTABLE};
  751. # endif
  752. static const struct normal_encoding internal_little2_encoding
  753. = {{VTABLE, 2, 0, 1},
  754. {
  755. # define BT_COLON BT_NMSTRT
  756. # include "iasciitab.h"
  757. # undef BT_COLON
  758. # include "latin1tab.h"
  759. },
  760. STANDARD_VTABLE(little2_) NULL_VTABLE};
  761. #endif
  762. #define BIG2_BYTE_TYPE(enc, p) \
  763. ((p)[0] == 0 ? SB_BYTE_TYPE(enc, p + 1) : unicode_byte_type((p)[0], (p)[1]))
  764. #define BIG2_BYTE_TO_ASCII(p) ((p)[0] == 0 ? (p)[1] : -1)
  765. #define BIG2_CHAR_MATCHES(p, c) ((p)[0] == 0 && (p)[1] == (c))
  766. #define BIG2_IS_NAME_CHAR_MINBPC(p) \
  767. UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
  768. #define BIG2_IS_NMSTRT_CHAR_MINBPC(p) \
  769. UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
  770. #ifdef XML_MIN_SIZE
  771. static int PTRFASTCALL
  772. big2_byteType(const ENCODING *enc, const char *p) {
  773. return BIG2_BYTE_TYPE(enc, p);
  774. }
  775. static int PTRFASTCALL
  776. big2_byteToAscii(const ENCODING *enc, const char *p) {
  777. UNUSED_P(enc);
  778. return BIG2_BYTE_TO_ASCII(p);
  779. }
  780. static int PTRCALL
  781. big2_charMatches(const ENCODING *enc, const char *p, int c) {
  782. UNUSED_P(enc);
  783. return BIG2_CHAR_MATCHES(p, c);
  784. }
  785. static int PTRFASTCALL
  786. big2_isNameMin(const ENCODING *enc, const char *p) {
  787. UNUSED_P(enc);
  788. return BIG2_IS_NAME_CHAR_MINBPC(p);
  789. }
  790. static int PTRFASTCALL
  791. big2_isNmstrtMin(const ENCODING *enc, const char *p) {
  792. UNUSED_P(enc);
  793. return BIG2_IS_NMSTRT_CHAR_MINBPC(p);
  794. }
  795. # undef VTABLE
  796. # define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
  797. #else /* not XML_MIN_SIZE */
  798. # undef PREFIX
  799. # define PREFIX(ident) big2_##ident
  800. # define MINBPC(enc) 2
  801. /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
  802. # define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
  803. # define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(p)
  804. # define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(p, c)
  805. # define IS_NAME_CHAR(enc, p, n) 0
  806. # define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(p)
  807. # define IS_NMSTRT_CHAR(enc, p, n) (0)
  808. # define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(p)
  809. # define XML_TOK_IMPL_C
  810. # include "xmltok_impl.c"
  811. # undef XML_TOK_IMPL_C
  812. # undef MINBPC
  813. # undef BYTE_TYPE
  814. # undef BYTE_TO_ASCII
  815. # undef CHAR_MATCHES
  816. # undef IS_NAME_CHAR
  817. # undef IS_NAME_CHAR_MINBPC
  818. # undef IS_NMSTRT_CHAR
  819. # undef IS_NMSTRT_CHAR_MINBPC
  820. # undef IS_INVALID_CHAR
  821. #endif /* not XML_MIN_SIZE */
  822. #ifdef XML_NS
  823. static const struct normal_encoding big2_encoding_ns
  824. = {{VTABLE, 2, 0,
  825. # if BYTEORDER == 4321
  826. 1
  827. # else
  828. 0
  829. # endif
  830. },
  831. {
  832. # include "asciitab.h"
  833. # include "latin1tab.h"
  834. },
  835. STANDARD_VTABLE(big2_) NULL_VTABLE};
  836. #endif
  837. static const struct normal_encoding big2_encoding
  838. = {{VTABLE, 2, 0,
  839. #if BYTEORDER == 4321
  840. 1
  841. #else
  842. 0
  843. #endif
  844. },
  845. {
  846. #define BT_COLON BT_NMSTRT
  847. #include "asciitab.h"
  848. #undef BT_COLON
  849. #include "latin1tab.h"
  850. },
  851. STANDARD_VTABLE(big2_) NULL_VTABLE};
  852. #if BYTEORDER != 1234
  853. # ifdef XML_NS
  854. static const struct normal_encoding internal_big2_encoding_ns
  855. = {{VTABLE, 2, 0, 1},
  856. {
  857. # include "iasciitab.h"
  858. # include "latin1tab.h"
  859. },
  860. STANDARD_VTABLE(big2_) NULL_VTABLE};
  861. # endif
  862. static const struct normal_encoding internal_big2_encoding
  863. = {{VTABLE, 2, 0, 1},
  864. {
  865. # define BT_COLON BT_NMSTRT
  866. # include "iasciitab.h"
  867. # undef BT_COLON
  868. # include "latin1tab.h"
  869. },
  870. STANDARD_VTABLE(big2_) NULL_VTABLE};
  871. #endif
  872. #undef PREFIX
  873. static int FASTCALL
  874. streqci(const char *s1, const char *s2) {
  875. for (;;) {
  876. char c1 = *s1++;
  877. char c2 = *s2++;
  878. if (ASCII_a <= c1 && c1 <= ASCII_z)
  879. c1 += ASCII_A - ASCII_a;
  880. if (ASCII_a <= c2 && c2 <= ASCII_z)
  881. /* The following line will never get executed. streqci() is
  882. * only called from two places, both of which guarantee to put
  883. * upper-case strings into s2.
  884. */
  885. c2 += ASCII_A - ASCII_a; /* LCOV_EXCL_LINE */
  886. if (c1 != c2)
  887. return 0;
  888. if (! c1)
  889. break;
  890. }
  891. return 1;
  892. }
  893. static void PTRCALL
  894. initUpdatePosition(const ENCODING *enc, const char *ptr, const char *end,
  895. POSITION *pos) {
  896. UNUSED_P(enc);
  897. normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
  898. }
  899. static int
  900. toAscii(const ENCODING *enc, const char *ptr, const char *end) {
  901. char buf[1];
  902. char *p = buf;
  903. XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
  904. if (p == buf)
  905. return -1;
  906. else
  907. return buf[0];
  908. }
  909. static int FASTCALL
  910. isSpace(int c) {
  911. switch (c) {
  912. case 0x20:
  913. case 0xD:
  914. case 0xA:
  915. case 0x9:
  916. return 1;
  917. }
  918. return 0;
  919. }
  920. /* Return 1 if there's just optional white space or there's an S
  921. followed by name=val.
  922. */
  923. static int
  924. parsePseudoAttribute(const ENCODING *enc, const char *ptr, const char *end,
  925. const char **namePtr, const char **nameEndPtr,
  926. const char **valPtr, const char **nextTokPtr) {
  927. int c;
  928. char open;
  929. if (ptr == end) {
  930. *namePtr = NULL;
  931. return 1;
  932. }
  933. if (! isSpace(toAscii(enc, ptr, end))) {
  934. *nextTokPtr = ptr;
  935. return 0;
  936. }
  937. do {
  938. ptr += enc->minBytesPerChar;
  939. } while (isSpace(toAscii(enc, ptr, end)));
  940. if (ptr == end) {
  941. *namePtr = NULL;
  942. return 1;
  943. }
  944. *namePtr = ptr;
  945. for (;;) {
  946. c = toAscii(enc, ptr, end);
  947. if (c == -1) {
  948. *nextTokPtr = ptr;
  949. return 0;
  950. }
  951. if (c == ASCII_EQUALS) {
  952. *nameEndPtr = ptr;
  953. break;
  954. }
  955. if (isSpace(c)) {
  956. *nameEndPtr = ptr;
  957. do {
  958. ptr += enc->minBytesPerChar;
  959. } while (isSpace(c = toAscii(enc, ptr, end)));
  960. if (c != ASCII_EQUALS) {
  961. *nextTokPtr = ptr;
  962. return 0;
  963. }
  964. break;
  965. }
  966. ptr += enc->minBytesPerChar;
  967. }
  968. if (ptr == *namePtr) {
  969. *nextTokPtr = ptr;
  970. return 0;
  971. }
  972. ptr += enc->minBytesPerChar;
  973. c = toAscii(enc, ptr, end);
  974. while (isSpace(c)) {
  975. ptr += enc->minBytesPerChar;
  976. c = toAscii(enc, ptr, end);
  977. }
  978. if (c != ASCII_QUOT && c != ASCII_APOS) {
  979. *nextTokPtr = ptr;
  980. return 0;
  981. }
  982. open = (char)c;
  983. ptr += enc->minBytesPerChar;
  984. *valPtr = ptr;
  985. for (;; ptr += enc->minBytesPerChar) {
  986. c = toAscii(enc, ptr, end);
  987. if (c == open)
  988. break;
  989. if (! (ASCII_a <= c && c <= ASCII_z) && ! (ASCII_A <= c && c <= ASCII_Z)
  990. && ! (ASCII_0 <= c && c <= ASCII_9) && c != ASCII_PERIOD
  991. && c != ASCII_MINUS && c != ASCII_UNDERSCORE) {
  992. *nextTokPtr = ptr;
  993. return 0;
  994. }
  995. }
  996. *nextTokPtr = ptr + enc->minBytesPerChar;
  997. return 1;
  998. }
  999. static const char KW_version[]
  1000. = {ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'};
  1001. static const char KW_encoding[] = {ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d,
  1002. ASCII_i, ASCII_n, ASCII_g, '\0'};
  1003. static const char KW_standalone[]
  1004. = {ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a,
  1005. ASCII_l, ASCII_o, ASCII_n, ASCII_e, '\0'};
  1006. static const char KW_yes[] = {ASCII_y, ASCII_e, ASCII_s, '\0'};
  1007. static const char KW_no[] = {ASCII_n, ASCII_o, '\0'};
  1008. static int
  1009. doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *, const char *,
  1010. const char *),
  1011. int isGeneralTextEntity, const ENCODING *enc, const char *ptr,
  1012. const char *end, const char **badPtr, const char **versionPtr,
  1013. const char **versionEndPtr, const char **encodingName,
  1014. const ENCODING **encoding, int *standalone) {
  1015. const char *val = NULL;
  1016. const char *name = NULL;
  1017. const char *nameEnd = NULL;
  1018. ptr += 5 * enc->minBytesPerChar;
  1019. end -= 2 * enc->minBytesPerChar;
  1020. if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
  1021. || ! name) {
  1022. *badPtr = ptr;
  1023. return 0;
  1024. }
  1025. if (! XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
  1026. if (! isGeneralTextEntity) {
  1027. *badPtr = name;
  1028. return 0;
  1029. }
  1030. } else {
  1031. if (versionPtr)
  1032. *versionPtr = val;
  1033. if (versionEndPtr)
  1034. *versionEndPtr = ptr;
  1035. if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
  1036. *badPtr = ptr;
  1037. return 0;
  1038. }
  1039. if (! name) {
  1040. if (isGeneralTextEntity) {
  1041. /* a TextDecl must have an EncodingDecl */
  1042. *badPtr = ptr;
  1043. return 0;
  1044. }
  1045. return 1;
  1046. }
  1047. }
  1048. if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
  1049. int c = toAscii(enc, val, end);
  1050. if (! (ASCII_a <= c && c <= ASCII_z) && ! (ASCII_A <= c && c <= ASCII_Z)) {
  1051. *badPtr = val;
  1052. return 0;
  1053. }
  1054. if (encodingName)
  1055. *encodingName = val;
  1056. if (encoding)
  1057. *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
  1058. if (! parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
  1059. *badPtr = ptr;
  1060. return 0;
  1061. }
  1062. if (! name)
  1063. return 1;
  1064. }
  1065. if (! XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
  1066. || isGeneralTextEntity) {
  1067. *badPtr = name;
  1068. return 0;
  1069. }
  1070. if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
  1071. if (standalone)
  1072. *standalone = 1;
  1073. } else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
  1074. if (standalone)
  1075. *standalone = 0;
  1076. } else {
  1077. *badPtr = val;
  1078. return 0;
  1079. }
  1080. while (isSpace(toAscii(enc, ptr, end)))
  1081. ptr += enc->minBytesPerChar;
  1082. if (ptr != end) {
  1083. *badPtr = ptr;
  1084. return 0;
  1085. }
  1086. return 1;
  1087. }
  1088. static int FASTCALL
  1089. checkCharRefNumber(int result) {
  1090. switch (result >> 8) {
  1091. case 0xD8:
  1092. case 0xD9:
  1093. case 0xDA:
  1094. case 0xDB:
  1095. case 0xDC:
  1096. case 0xDD:
  1097. case 0xDE:
  1098. case 0xDF:
  1099. return -1;
  1100. case 0:
  1101. if (latin1_encoding.type[result] == BT_NONXML)
  1102. return -1;
  1103. break;
  1104. case 0xFF:
  1105. if (result == 0xFFFE || result == 0xFFFF)
  1106. return -1;
  1107. break;
  1108. }
  1109. return result;
  1110. }
  1111. int FASTCALL
  1112. XmlUtf8Encode(int c, char *buf) {
  1113. enum {
  1114. /* minN is minimum legal resulting value for N byte sequence */
  1115. min2 = 0x80,
  1116. min3 = 0x800,
  1117. min4 = 0x10000
  1118. };
  1119. if (c < 0)
  1120. return 0; /* LCOV_EXCL_LINE: this case is always eliminated beforehand */
  1121. if (c < min2) {
  1122. buf[0] = (char)(c | UTF8_cval1);
  1123. return 1;
  1124. }
  1125. if (c < min3) {
  1126. buf[0] = (char)((c >> 6) | UTF8_cval2);
  1127. buf[1] = (char)((c & 0x3f) | 0x80);
  1128. return 2;
  1129. }
  1130. if (c < min4) {
  1131. buf[0] = (char)((c >> 12) | UTF8_cval3);
  1132. buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
  1133. buf[2] = (char)((c & 0x3f) | 0x80);
  1134. return 3;
  1135. }
  1136. if (c < 0x110000) {
  1137. buf[0] = (char)((c >> 18) | UTF8_cval4);
  1138. buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
  1139. buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
  1140. buf[3] = (char)((c & 0x3f) | 0x80);
  1141. return 4;
  1142. }
  1143. return 0; /* LCOV_EXCL_LINE: this case too is eliminated before calling */
  1144. }
  1145. int FASTCALL
  1146. XmlUtf16Encode(int charNum, unsigned short *buf) {
  1147. if (charNum < 0)
  1148. return 0;
  1149. if (charNum < 0x10000) {
  1150. buf[0] = (unsigned short)charNum;
  1151. return 1;
  1152. }
  1153. if (charNum < 0x110000) {
  1154. charNum -= 0x10000;
  1155. buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
  1156. buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
  1157. return 2;
  1158. }
  1159. return 0;
  1160. }
  1161. struct unknown_encoding {
  1162. struct normal_encoding normal;
  1163. CONVERTER convert;
  1164. void *userData;
  1165. unsigned short utf16[256];
  1166. char utf8[256][4];
  1167. };
  1168. #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *)(enc))
  1169. int
  1170. XmlSizeOfUnknownEncoding(void) {
  1171. return sizeof(struct unknown_encoding);
  1172. }
  1173. static int PTRFASTCALL
  1174. unknown_isName(const ENCODING *enc, const char *p) {
  1175. const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
  1176. int c = uenc->convert(uenc->userData, p);
  1177. if (c & ~0xFFFF)
  1178. return 0;
  1179. return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
  1180. }
  1181. static int PTRFASTCALL
  1182. unknown_isNmstrt(const ENCODING *enc, const char *p) {
  1183. const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
  1184. int c = uenc->convert(uenc->userData, p);
  1185. if (c & ~0xFFFF)
  1186. return 0;
  1187. return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
  1188. }
  1189. static int PTRFASTCALL
  1190. unknown_isInvalid(const ENCODING *enc, const char *p) {
  1191. const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
  1192. int c = uenc->convert(uenc->userData, p);
  1193. return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
  1194. }
  1195. static enum XML_Convert_Result PTRCALL
  1196. unknown_toUtf8(const ENCODING *enc, const char **fromP, const char *fromLim,
  1197. char **toP, const char *toLim) {
  1198. const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
  1199. char buf[XML_UTF8_ENCODE_MAX];
  1200. for (;;) {
  1201. const char *utf8;
  1202. int n;
  1203. if (*fromP == fromLim)
  1204. return XML_CONVERT_COMPLETED;
  1205. utf8 = uenc->utf8[(unsigned char)**fromP];
  1206. n = *utf8++;
  1207. if (n == 0) {
  1208. int c = uenc->convert(uenc->userData, *fromP);
  1209. n = XmlUtf8Encode(c, buf);
  1210. if (n > toLim - *toP)
  1211. return XML_CONVERT_OUTPUT_EXHAUSTED;
  1212. utf8 = buf;
  1213. *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
  1214. - (BT_LEAD2 - 2));
  1215. } else {
  1216. if (n > toLim - *toP)
  1217. return XML_CONVERT_OUTPUT_EXHAUSTED;
  1218. (*fromP)++;
  1219. }
  1220. memcpy(*toP, utf8, n);
  1221. *toP += n;
  1222. }
  1223. }
  1224. static enum XML_Convert_Result PTRCALL
  1225. unknown_toUtf16(const ENCODING *enc, const char **fromP, const char *fromLim,
  1226. unsigned short **toP, const unsigned short *toLim) {
  1227. const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
  1228. while (*fromP < fromLim && *toP < toLim) {
  1229. unsigned short c = uenc->utf16[(unsigned char)**fromP];
  1230. if (c == 0) {
  1231. c = (unsigned short)uenc->convert(uenc->userData, *fromP);
  1232. *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
  1233. - (BT_LEAD2 - 2));
  1234. } else
  1235. (*fromP)++;
  1236. *(*toP)++ = c;
  1237. }
  1238. if ((*toP == toLim) && (*fromP < fromLim))
  1239. return XML_CONVERT_OUTPUT_EXHAUSTED;
  1240. else
  1241. return XML_CONVERT_COMPLETED;
  1242. }
  1243. ENCODING *
  1244. XmlInitUnknownEncoding(void *mem, int *table, CONVERTER convert,
  1245. void *userData) {
  1246. int i;
  1247. struct unknown_encoding *e = (struct unknown_encoding *)mem;
  1248. memcpy(mem, &latin1_encoding, sizeof(struct normal_encoding));
  1249. for (i = 0; i < 128; i++)
  1250. if (latin1_encoding.type[i] != BT_OTHER
  1251. && latin1_encoding.type[i] != BT_NONXML && table[i] != i)
  1252. return 0;
  1253. for (i = 0; i < 256; i++) {
  1254. int c = table[i];
  1255. if (c == -1) {
  1256. e->normal.type[i] = BT_MALFORM;
  1257. /* This shouldn't really get used. */
  1258. e->utf16[i] = 0xFFFF;
  1259. e->utf8[i][0] = 1;
  1260. e->utf8[i][1] = 0;
  1261. } else if (c < 0) {
  1262. if (c < -4)
  1263. return 0;
  1264. /* Multi-byte sequences need a converter function */
  1265. if (! convert)
  1266. return 0;
  1267. e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
  1268. e->utf8[i][0] = 0;
  1269. e->utf16[i] = 0;
  1270. } else if (c < 0x80) {
  1271. if (latin1_encoding.type[c] != BT_OTHER
  1272. && latin1_encoding.type[c] != BT_NONXML && c != i)
  1273. return 0;
  1274. e->normal.type[i] = latin1_encoding.type[c];
  1275. e->utf8[i][0] = 1;
  1276. e->utf8[i][1] = (char)c;
  1277. e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
  1278. } else if (checkCharRefNumber(c) < 0) {
  1279. e->normal.type[i] = BT_NONXML;
  1280. /* This shouldn't really get used. */
  1281. e->utf16[i] = 0xFFFF;
  1282. e->utf8[i][0] = 1;
  1283. e->utf8[i][1] = 0;
  1284. } else {
  1285. if (c > 0xFFFF)
  1286. return 0;
  1287. if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
  1288. e->normal.type[i] = BT_NMSTRT;
  1289. else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
  1290. e->normal.type[i] = BT_NAME;
  1291. else
  1292. e->normal.type[i] = BT_OTHER;
  1293. e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
  1294. e->utf16[i] = (unsigned short)c;
  1295. }
  1296. }
  1297. e->userData = userData;
  1298. e->convert = convert;
  1299. if (convert) {
  1300. e->normal.isName2 = unknown_isName;
  1301. e->normal.isName3 = unknown_isName;
  1302. e->normal.isName4 = unknown_isName;
  1303. e->normal.isNmstrt2 = unknown_isNmstrt;
  1304. e->normal.isNmstrt3 = unknown_isNmstrt;
  1305. e->normal.isNmstrt4 = unknown_isNmstrt;
  1306. e->normal.isInvalid2 = unknown_isInvalid;
  1307. e->normal.isInvalid3 = unknown_isInvalid;
  1308. e->normal.isInvalid4 = unknown_isInvalid;
  1309. }
  1310. e->normal.enc.utf8Convert = unknown_toUtf8;
  1311. e->normal.enc.utf16Convert = unknown_toUtf16;
  1312. return &(e->normal.enc);
  1313. }
  1314. /* If this enumeration is changed, getEncodingIndex and encodings
  1315. must also be changed. */
  1316. enum {
  1317. UNKNOWN_ENC = -1,
  1318. ISO_8859_1_ENC = 0,
  1319. US_ASCII_ENC,
  1320. UTF_8_ENC,
  1321. UTF_16_ENC,
  1322. UTF_16BE_ENC,
  1323. UTF_16LE_ENC,
  1324. /* must match encodingNames up to here */
  1325. NO_ENC
  1326. };
  1327. static const char KW_ISO_8859_1[]
  1328. = {ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8,
  1329. ASCII_5, ASCII_9, ASCII_MINUS, ASCII_1, '\0'};
  1330. static const char KW_US_ASCII[]
  1331. = {ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S,
  1332. ASCII_C, ASCII_I, ASCII_I, '\0'};
  1333. static const char KW_UTF_8[]
  1334. = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'};
  1335. static const char KW_UTF_16[]
  1336. = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'};
  1337. static const char KW_UTF_16BE[]
  1338. = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1,
  1339. ASCII_6, ASCII_B, ASCII_E, '\0'};
  1340. static const char KW_UTF_16LE[]
  1341. = {ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1,
  1342. ASCII_6, ASCII_L, ASCII_E, '\0'};
  1343. static int FASTCALL
  1344. getEncodingIndex(const char *name) {
  1345. static const char *const encodingNames[] = {
  1346. KW_ISO_8859_1, KW_US_ASCII, KW_UTF_8, KW_UTF_16, KW_UTF_16BE, KW_UTF_16LE,
  1347. };
  1348. int i;
  1349. if (name == NULL)
  1350. return NO_ENC;
  1351. for (i = 0; i < (int)(sizeof(encodingNames) / sizeof(encodingNames[0])); i++)
  1352. if (streqci(name, encodingNames[i]))
  1353. return i;
  1354. return UNKNOWN_ENC;
  1355. }
  1356. /* For binary compatibility, we store the index of the encoding
  1357. specified at initialization in the isUtf16 member.
  1358. */
  1359. #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
  1360. #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
  1361. /* This is what detects the encoding. encodingTable maps from
  1362. encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
  1363. the external (protocol) specified encoding; state is
  1364. XML_CONTENT_STATE if we're parsing an external text entity, and
  1365. XML_PROLOG_STATE otherwise.
  1366. */
  1367. static int
  1368. initScan(const ENCODING *const *encodingTable, const INIT_ENCODING *enc,
  1369. int state, const char *ptr, const char *end, const char **nextTokPtr) {
  1370. const ENCODING **encPtr;
  1371. if (ptr >= end)
  1372. return XML_TOK_NONE;
  1373. encPtr = enc->encPtr;
  1374. if (ptr + 1 == end) {
  1375. /* only a single byte available for auto-detection */
  1376. #ifndef XML_DTD /* FIXME */
  1377. /* a well-formed document entity must have more than one byte */
  1378. if (state != XML_CONTENT_STATE)
  1379. return XML_TOK_PARTIAL;
  1380. #endif
  1381. /* so we're parsing an external text entity... */
  1382. /* if UTF-16 was externally specified, then we need at least 2 bytes */
  1383. switch (INIT_ENC_INDEX(enc)) {
  1384. case UTF_16_ENC:
  1385. case UTF_16LE_ENC:
  1386. case UTF_16BE_ENC:
  1387. return XML_TOK_PARTIAL;
  1388. }
  1389. switch ((unsigned char)*ptr) {
  1390. case 0xFE:
  1391. case 0xFF:
  1392. case 0xEF: /* possibly first byte of UTF-8 BOM */
  1393. if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
  1394. break;
  1395. /* fall through */
  1396. case 0x00:
  1397. case 0x3C:
  1398. return XML_TOK_PARTIAL;
  1399. }
  1400. } else {
  1401. switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
  1402. case 0xFEFF:
  1403. if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
  1404. break;
  1405. *nextTokPtr = ptr + 2;
  1406. *encPtr = encodingTable[UTF_16BE_ENC];
  1407. return XML_TOK_BOM;
  1408. /* 00 3C is handled in the default case */
  1409. case 0x3C00:
  1410. if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
  1411. || INIT_ENC_INDEX(enc) == UTF_16_ENC)
  1412. && state == XML_CONTENT_STATE)
  1413. break;
  1414. *encPtr = encodingTable[UTF_16LE_ENC];
  1415. return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
  1416. case 0xFFFE:
  1417. if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC && state == XML_CONTENT_STATE)
  1418. break;
  1419. *nextTokPtr = ptr + 2;
  1420. *encPtr = encodingTable[UTF_16LE_ENC];
  1421. return XML_TOK_BOM;
  1422. case 0xEFBB:
  1423. /* Maybe a UTF-8 BOM (EF BB BF) */
  1424. /* If there's an explicitly specified (external) encoding
  1425. of ISO-8859-1 or some flavour of UTF-16
  1426. and this is an external text entity,
  1427. don't look for the BOM,
  1428. because it might be a legal data.
  1429. */
  1430. if (state == XML_CONTENT_STATE) {
  1431. int e = INIT_ENC_INDEX(enc);
  1432. if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC || e == UTF_16LE_ENC
  1433. || e == UTF_16_ENC)
  1434. break;
  1435. }
  1436. if (ptr + 2 == end)
  1437. return XML_TOK_PARTIAL;
  1438. if ((unsigned char)ptr[2] == 0xBF) {
  1439. *nextTokPtr = ptr + 3;
  1440. *encPtr = encodingTable[UTF_8_ENC];
  1441. return XML_TOK_BOM;
  1442. }
  1443. break;
  1444. default:
  1445. if (ptr[0] == '\0') {
  1446. /* 0 isn't a legal data character. Furthermore a document
  1447. entity can only start with ASCII characters. So the only
  1448. way this can fail to be big-endian UTF-16 if it it's an
  1449. external parsed general entity that's labelled as
  1450. UTF-16LE.
  1451. */
  1452. if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
  1453. break;
  1454. *encPtr = encodingTable[UTF_16BE_ENC];
  1455. return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
  1456. } else if (ptr[1] == '\0') {
  1457. /* We could recover here in the case:
  1458. - parsing an external entity
  1459. - second byte is 0
  1460. - no externally specified encoding
  1461. - no encoding declaration
  1462. by assuming UTF-16LE. But we don't, because this would mean when
  1463. presented just with a single byte, we couldn't reliably determine
  1464. whether we needed further bytes.
  1465. */
  1466. if (state == XML_CONTENT_STATE)
  1467. break;
  1468. *encPtr = encodingTable[UTF_16LE_ENC];
  1469. return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
  1470. }
  1471. break;
  1472. }
  1473. }
  1474. *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
  1475. return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
  1476. }
  1477. #define NS(x) x
  1478. #define ns(x) x
  1479. #define XML_TOK_NS_C
  1480. #include "xmltok_ns.c"
  1481. #undef XML_TOK_NS_C
  1482. #undef NS
  1483. #undef ns
  1484. #ifdef XML_NS
  1485. # define NS(x) x##NS
  1486. # define ns(x) x##_ns
  1487. # define XML_TOK_NS_C
  1488. # include "xmltok_ns.c"
  1489. # undef XML_TOK_NS_C
  1490. # undef NS
  1491. # undef ns
  1492. ENCODING *
  1493. XmlInitUnknownEncodingNS(void *mem, int *table, CONVERTER convert,
  1494. void *userData) {
  1495. ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
  1496. if (enc)
  1497. ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
  1498. return enc;
  1499. }
  1500. #endif /* XML_NS */