xmltok.c 53 KB

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