2
0

xmltok.c 38 KB

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