xmltok.c 44 KB

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