xmltok.c 37 KB

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