parserInternals.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * parserInternals.c : Internal routines (and obsolete ones) needed for the
  3. * XML and HTML parsers.
  4. *
  5. * See Copyright for the status of this software.
  6. *
  7. * daniel@veillard.com
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #if defined(WIN32) && !defined (__CYGWIN__)
  12. #define XML_DIR_SEP '\\'
  13. #else
  14. #define XML_DIR_SEP '/'
  15. #endif
  16. #include <string.h>
  17. #ifdef HAVE_CTYPE_H
  18. #include <ctype.h>
  19. #endif
  20. #ifdef HAVE_STDLIB_H
  21. #include <stdlib.h>
  22. #endif
  23. #ifdef HAVE_SYS_STAT_H
  24. #include <sys/stat.h>
  25. #endif
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #endif
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #ifdef HAVE_ZLIB_H
  33. #include <zlib.h>
  34. #endif
  35. #include <libxml/xmlmemory.h>
  36. #include <libxml/tree.h>
  37. #include <libxml/parser.h>
  38. #include <libxml/parserInternals.h>
  39. #include <libxml/valid.h>
  40. #include <libxml/entities.h>
  41. #include <libxml/xmlerror.h>
  42. #include <libxml/encoding.h>
  43. #include <libxml/valid.h>
  44. #include <libxml/xmlIO.h>
  45. #include <libxml/uri.h>
  46. #include <libxml/dict.h>
  47. #include <libxml/SAX.h>
  48. #ifdef LIBXML_CATALOG_ENABLED
  49. #include <libxml/catalog.h>
  50. #endif
  51. #include <libxml/globals.h>
  52. #include <libxml/chvalid.h>
  53. /*
  54. * Various global defaults for parsing
  55. */
  56. /**
  57. * xmlCheckVersion:
  58. * @version: the include version number
  59. *
  60. * check the compiled lib version against the include one.
  61. * This can warn or immediately kill the application
  62. */
  63. void
  64. xmlCheckVersion(int version) {
  65. int myversion = (int) LIBXML_VERSION;
  66. xmlInitParser();
  67. if ((myversion / 10000) != (version / 10000)) {
  68. xmlGenericError(xmlGenericErrorContext,
  69. "Fatal: program compiled against libxml %d using libxml %d\n",
  70. (version / 10000), (myversion / 10000));
  71. fprintf(stderr,
  72. "Fatal: program compiled against libxml %d using libxml %d\n",
  73. (version / 10000), (myversion / 10000));
  74. }
  75. if ((myversion / 100) < (version / 100)) {
  76. xmlGenericError(xmlGenericErrorContext,
  77. "Warning: program compiled against libxml %d using older %d\n",
  78. (version / 100), (myversion / 100));
  79. }
  80. }
  81. /************************************************************************
  82. * *
  83. * Some factorized error routines *
  84. * *
  85. ************************************************************************/
  86. /**
  87. * xmlErrMemory:
  88. * @ctxt: an XML parser context
  89. * @extra: extra informations
  90. *
  91. * Handle a redefinition of attribute error
  92. */
  93. void
  94. xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
  95. {
  96. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  97. (ctxt->instate == XML_PARSER_EOF))
  98. return;
  99. if (ctxt != NULL) {
  100. ctxt->errNo = XML_ERR_NO_MEMORY;
  101. ctxt->instate = XML_PARSER_EOF;
  102. ctxt->disableSAX = 1;
  103. }
  104. if (extra)
  105. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  106. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  107. NULL, NULL, 0, 0,
  108. "Memory allocation failed : %s\n", extra);
  109. else
  110. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  111. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
  112. NULL, NULL, 0, 0, "Memory allocation failed\n");
  113. }
  114. /**
  115. * __xmlErrEncoding:
  116. * @ctxt: an XML parser context
  117. * @xmlerr: the error number
  118. * @msg: the error message
  119. * @str1: an string info
  120. * @str2: an string info
  121. *
  122. * Handle an encoding error
  123. */
  124. void
  125. __xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
  126. const char *msg, const xmlChar * str1, const xmlChar * str2)
  127. {
  128. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  129. (ctxt->instate == XML_PARSER_EOF))
  130. return;
  131. if (ctxt != NULL)
  132. ctxt->errNo = xmlerr;
  133. __xmlRaiseError(NULL, NULL, NULL,
  134. ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
  135. NULL, 0, (const char *) str1, (const char *) str2,
  136. NULL, 0, 0, msg, str1, str2);
  137. if (ctxt != NULL) {
  138. ctxt->wellFormed = 0;
  139. if (ctxt->recovery == 0)
  140. ctxt->disableSAX = 1;
  141. }
  142. }
  143. /**
  144. * xmlErrInternal:
  145. * @ctxt: an XML parser context
  146. * @msg: the error message
  147. * @str: error informations
  148. *
  149. * Handle an internal error
  150. */
  151. static void
  152. xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
  153. {
  154. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  155. (ctxt->instate == XML_PARSER_EOF))
  156. return;
  157. if (ctxt != NULL)
  158. ctxt->errNo = XML_ERR_INTERNAL_ERROR;
  159. __xmlRaiseError(NULL, NULL, NULL,
  160. ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
  161. XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
  162. 0, 0, msg, str);
  163. if (ctxt != NULL) {
  164. ctxt->wellFormed = 0;
  165. if (ctxt->recovery == 0)
  166. ctxt->disableSAX = 1;
  167. }
  168. }
  169. /**
  170. * xmlErrEncodingInt:
  171. * @ctxt: an XML parser context
  172. * @error: the error number
  173. * @msg: the error message
  174. * @val: an integer value
  175. *
  176. * n encoding error
  177. */
  178. static void
  179. xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  180. const char *msg, int val)
  181. {
  182. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  183. (ctxt->instate == XML_PARSER_EOF))
  184. return;
  185. if (ctxt != NULL)
  186. ctxt->errNo = error;
  187. __xmlRaiseError(NULL, NULL, NULL,
  188. ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
  189. NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  190. if (ctxt != NULL) {
  191. ctxt->wellFormed = 0;
  192. if (ctxt->recovery == 0)
  193. ctxt->disableSAX = 1;
  194. }
  195. }
  196. /**
  197. * xmlIsLetter:
  198. * @c: an unicode character (int)
  199. *
  200. * Check whether the character is allowed by the production
  201. * [84] Letter ::= BaseChar | Ideographic
  202. *
  203. * Returns 0 if not, non-zero otherwise
  204. */
  205. int
  206. xmlIsLetter(int c) {
  207. return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
  208. }
  209. /************************************************************************
  210. * *
  211. * Input handling functions for progressive parsing *
  212. * *
  213. ************************************************************************/
  214. /* #define DEBUG_INPUT */
  215. /* #define DEBUG_STACK */
  216. /* #define DEBUG_PUSH */
  217. /* we need to keep enough input to show errors in context */
  218. #define LINE_LEN 80
  219. #ifdef DEBUG_INPUT
  220. #define CHECK_BUFFER(in) check_buffer(in)
  221. static
  222. void check_buffer(xmlParserInputPtr in) {
  223. if (in->base != in->buf->buffer->content) {
  224. xmlGenericError(xmlGenericErrorContext,
  225. "xmlParserInput: base mismatch problem\n");
  226. }
  227. if (in->cur < in->base) {
  228. xmlGenericError(xmlGenericErrorContext,
  229. "xmlParserInput: cur < base problem\n");
  230. }
  231. if (in->cur > in->base + in->buf->buffer->use) {
  232. xmlGenericError(xmlGenericErrorContext,
  233. "xmlParserInput: cur > base + use problem\n");
  234. }
  235. xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
  236. (int) in, (int) in->buf->buffer->content, in->cur - in->base,
  237. in->buf->buffer->use, in->buf->buffer->size);
  238. }
  239. #else
  240. #define CHECK_BUFFER(in)
  241. #endif
  242. /**
  243. * xmlParserInputRead:
  244. * @in: an XML parser input
  245. * @len: an indicative size for the lookahead
  246. *
  247. * This function refresh the input for the parser. It doesn't try to
  248. * preserve pointers to the input buffer, and discard already read data
  249. *
  250. * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
  251. * end of this entity
  252. */
  253. int
  254. xmlParserInputRead(xmlParserInputPtr in, int len) {
  255. int ret;
  256. int used;
  257. int indx;
  258. if (in == NULL) return(-1);
  259. #ifdef DEBUG_INPUT
  260. xmlGenericError(xmlGenericErrorContext, "Read\n");
  261. #endif
  262. if (in->buf == NULL) return(-1);
  263. if (in->base == NULL) return(-1);
  264. if (in->cur == NULL) return(-1);
  265. if (in->buf->buffer == NULL) return(-1);
  266. if (in->buf->readcallback == NULL) return(-1);
  267. CHECK_BUFFER(in);
  268. used = in->cur - in->buf->buffer->content;
  269. ret = xmlBufferShrink(in->buf->buffer, used);
  270. if (ret > 0) {
  271. in->cur -= ret;
  272. in->consumed += ret;
  273. }
  274. ret = xmlParserInputBufferRead(in->buf, len);
  275. if (in->base != in->buf->buffer->content) {
  276. /*
  277. * the buffer has been reallocated
  278. */
  279. indx = in->cur - in->base;
  280. in->base = in->buf->buffer->content;
  281. in->cur = &in->buf->buffer->content[indx];
  282. }
  283. in->end = &in->buf->buffer->content[in->buf->buffer->use];
  284. CHECK_BUFFER(in);
  285. return(ret);
  286. }
  287. /**
  288. * xmlParserInputGrow:
  289. * @in: an XML parser input
  290. * @len: an indicative size for the lookahead
  291. *
  292. * This function increase the input for the parser. It tries to
  293. * preserve pointers to the input buffer, and keep already read data
  294. *
  295. * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
  296. * end of this entity
  297. */
  298. int
  299. xmlParserInputGrow(xmlParserInputPtr in, int len) {
  300. int ret;
  301. int indx;
  302. if (in == NULL) return(-1);
  303. #ifdef DEBUG_INPUT
  304. xmlGenericError(xmlGenericErrorContext, "Grow\n");
  305. #endif
  306. if (in->buf == NULL) return(-1);
  307. if (in->base == NULL) return(-1);
  308. if (in->cur == NULL) return(-1);
  309. if (in->buf->buffer == NULL) return(-1);
  310. CHECK_BUFFER(in);
  311. indx = in->cur - in->base;
  312. if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
  313. CHECK_BUFFER(in);
  314. return(0);
  315. }
  316. if (in->buf->readcallback != NULL)
  317. ret = xmlParserInputBufferGrow(in->buf, len);
  318. else
  319. return(0);
  320. /*
  321. * NOTE : in->base may be a "dangling" i.e. freed pointer in this
  322. * block, but we use it really as an integer to do some
  323. * pointer arithmetic. Insure will raise it as a bug but in
  324. * that specific case, that's not !
  325. */
  326. if (in->base != in->buf->buffer->content) {
  327. /*
  328. * the buffer has been reallocated
  329. */
  330. indx = in->cur - in->base;
  331. in->base = in->buf->buffer->content;
  332. in->cur = &in->buf->buffer->content[indx];
  333. }
  334. in->end = &in->buf->buffer->content[in->buf->buffer->use];
  335. CHECK_BUFFER(in);
  336. return(ret);
  337. }
  338. /**
  339. * xmlParserInputShrink:
  340. * @in: an XML parser input
  341. *
  342. * This function removes used input for the parser.
  343. */
  344. void
  345. xmlParserInputShrink(xmlParserInputPtr in) {
  346. int used;
  347. int ret;
  348. int indx;
  349. #ifdef DEBUG_INPUT
  350. xmlGenericError(xmlGenericErrorContext, "Shrink\n");
  351. #endif
  352. if (in == NULL) return;
  353. if (in->buf == NULL) return;
  354. if (in->base == NULL) return;
  355. if (in->cur == NULL) return;
  356. if (in->buf->buffer == NULL) return;
  357. CHECK_BUFFER(in);
  358. used = in->cur - in->buf->buffer->content;
  359. /*
  360. * Do not shrink on large buffers whose only a tiny fraction
  361. * was consumed
  362. */
  363. if (used > INPUT_CHUNK) {
  364. ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
  365. if (ret > 0) {
  366. in->cur -= ret;
  367. in->consumed += ret;
  368. }
  369. in->end = &in->buf->buffer->content[in->buf->buffer->use];
  370. }
  371. CHECK_BUFFER(in);
  372. if (in->buf->buffer->use > INPUT_CHUNK) {
  373. return;
  374. }
  375. xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
  376. if (in->base != in->buf->buffer->content) {
  377. /*
  378. * the buffer has been reallocated
  379. */
  380. indx = in->cur - in->base;
  381. in->base = in->buf->buffer->content;
  382. in->cur = &in->buf->buffer->content[indx];
  383. }
  384. in->end = &in->buf->buffer->content[in->buf->buffer->use];
  385. CHECK_BUFFER(in);
  386. }
  387. /************************************************************************
  388. * *
  389. * UTF8 character input and related functions *
  390. * *
  391. ************************************************************************/
  392. /**
  393. * xmlNextChar:
  394. * @ctxt: the XML parser context
  395. *
  396. * Skip to the next char input char.
  397. */
  398. void
  399. xmlNextChar(xmlParserCtxtPtr ctxt)
  400. {
  401. if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
  402. (ctxt->input == NULL))
  403. return;
  404. if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
  405. if ((*ctxt->input->cur == 0) &&
  406. (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
  407. (ctxt->instate != XML_PARSER_COMMENT)) {
  408. /*
  409. * If we are at the end of the current entity and
  410. * the context allows it, we pop consumed entities
  411. * automatically.
  412. * the auto closing should be blocked in other cases
  413. */
  414. xmlPopInput(ctxt);
  415. } else {
  416. const unsigned char *cur;
  417. unsigned char c;
  418. /*
  419. * 2.11 End-of-Line Handling
  420. * the literal two-character sequence "#xD#xA" or a standalone
  421. * literal #xD, an XML processor must pass to the application
  422. * the single character #xA.
  423. */
  424. if (*(ctxt->input->cur) == '\n') {
  425. ctxt->input->line++; ctxt->input->col = 1;
  426. } else
  427. ctxt->input->col++;
  428. /*
  429. * We are supposed to handle UTF8, check it's valid
  430. * From rfc2044: encoding of the Unicode values on UTF-8:
  431. *
  432. * UCS-4 range (hex.) UTF-8 octet sequence (binary)
  433. * 0000 0000-0000 007F 0xxxxxxx
  434. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
  435. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  436. *
  437. * Check for the 0x110000 limit too
  438. */
  439. cur = ctxt->input->cur;
  440. c = *cur;
  441. if (c & 0x80) {
  442. if (c == 0xC0)
  443. goto encoding_error;
  444. if (cur[1] == 0) {
  445. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  446. cur = ctxt->input->cur;
  447. }
  448. if ((cur[1] & 0xc0) != 0x80)
  449. goto encoding_error;
  450. if ((c & 0xe0) == 0xe0) {
  451. unsigned int val;
  452. if (cur[2] == 0) {
  453. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  454. cur = ctxt->input->cur;
  455. }
  456. if ((cur[2] & 0xc0) != 0x80)
  457. goto encoding_error;
  458. if ((c & 0xf0) == 0xf0) {
  459. if (cur[3] == 0) {
  460. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  461. cur = ctxt->input->cur;
  462. }
  463. if (((c & 0xf8) != 0xf0) ||
  464. ((cur[3] & 0xc0) != 0x80))
  465. goto encoding_error;
  466. /* 4-byte code */
  467. ctxt->input->cur += 4;
  468. val = (cur[0] & 0x7) << 18;
  469. val |= (cur[1] & 0x3f) << 12;
  470. val |= (cur[2] & 0x3f) << 6;
  471. val |= cur[3] & 0x3f;
  472. } else {
  473. /* 3-byte code */
  474. ctxt->input->cur += 3;
  475. val = (cur[0] & 0xf) << 12;
  476. val |= (cur[1] & 0x3f) << 6;
  477. val |= cur[2] & 0x3f;
  478. }
  479. if (((val > 0xd7ff) && (val < 0xe000)) ||
  480. ((val > 0xfffd) && (val < 0x10000)) ||
  481. (val >= 0x110000)) {
  482. xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
  483. "Char 0x%X out of allowed range\n",
  484. val);
  485. }
  486. } else
  487. /* 2-byte code */
  488. ctxt->input->cur += 2;
  489. } else
  490. /* 1-byte code */
  491. ctxt->input->cur++;
  492. ctxt->nbChars++;
  493. if (*ctxt->input->cur == 0)
  494. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  495. }
  496. } else {
  497. /*
  498. * Assume it's a fixed length encoding (1) with
  499. * a compatible encoding for the ASCII set, since
  500. * XML constructs only use < 128 chars
  501. */
  502. if (*(ctxt->input->cur) == '\n') {
  503. ctxt->input->line++; ctxt->input->col = 1;
  504. } else
  505. ctxt->input->col++;
  506. ctxt->input->cur++;
  507. ctxt->nbChars++;
  508. if (*ctxt->input->cur == 0)
  509. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  510. }
  511. if ((*ctxt->input->cur == '%') && (!ctxt->html))
  512. xmlParserHandlePEReference(ctxt);
  513. if ((*ctxt->input->cur == 0) &&
  514. (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
  515. xmlPopInput(ctxt);
  516. return;
  517. encoding_error:
  518. /*
  519. * If we detect an UTF8 error that probably mean that the
  520. * input encoding didn't get properly advertised in the
  521. * declaration header. Report the error and switch the encoding
  522. * to ISO-Latin-1 (if you don't like this policy, just declare the
  523. * encoding !)
  524. */
  525. if ((ctxt == NULL) || (ctxt->input == NULL) ||
  526. (ctxt->input->end - ctxt->input->cur < 4)) {
  527. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  528. "Input is not proper UTF-8, indicate encoding !\n",
  529. NULL, NULL);
  530. } else {
  531. char buffer[150];
  532. snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
  533. ctxt->input->cur[0], ctxt->input->cur[1],
  534. ctxt->input->cur[2], ctxt->input->cur[3]);
  535. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  536. "Input is not proper UTF-8, indicate encoding !\n%s",
  537. BAD_CAST buffer, NULL);
  538. }
  539. ctxt->charset = XML_CHAR_ENCODING_8859_1;
  540. ctxt->input->cur++;
  541. return;
  542. }
  543. /**
  544. * xmlCurrentChar:
  545. * @ctxt: the XML parser context
  546. * @len: pointer to the length of the char read
  547. *
  548. * The current char value, if using UTF-8 this may actually span multiple
  549. * bytes in the input buffer. Implement the end of line normalization:
  550. * 2.11 End-of-Line Handling
  551. * Wherever an external parsed entity or the literal entity value
  552. * of an internal parsed entity contains either the literal two-character
  553. * sequence "#xD#xA" or a standalone literal #xD, an XML processor
  554. * must pass to the application the single character #xA.
  555. * This behavior can conveniently be produced by normalizing all
  556. * line breaks to #xA on input, before parsing.)
  557. *
  558. * Returns the current char value and its length
  559. */
  560. int
  561. xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
  562. if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
  563. if (ctxt->instate == XML_PARSER_EOF)
  564. return(0);
  565. if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
  566. *len = 1;
  567. return((int) *ctxt->input->cur);
  568. }
  569. if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
  570. /*
  571. * We are supposed to handle UTF8, check it's valid
  572. * From rfc2044: encoding of the Unicode values on UTF-8:
  573. *
  574. * UCS-4 range (hex.) UTF-8 octet sequence (binary)
  575. * 0000 0000-0000 007F 0xxxxxxx
  576. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
  577. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  578. *
  579. * Check for the 0x110000 limit too
  580. */
  581. const unsigned char *cur = ctxt->input->cur;
  582. unsigned char c;
  583. unsigned int val;
  584. c = *cur;
  585. if (c & 0x80) {
  586. if (((c & 0x40) == 0) || (c == 0xC0))
  587. goto encoding_error;
  588. if (cur[1] == 0) {
  589. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  590. cur = ctxt->input->cur;
  591. }
  592. if ((cur[1] & 0xc0) != 0x80)
  593. goto encoding_error;
  594. if ((c & 0xe0) == 0xe0) {
  595. if (cur[2] == 0) {
  596. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  597. cur = ctxt->input->cur;
  598. }
  599. if ((cur[2] & 0xc0) != 0x80)
  600. goto encoding_error;
  601. if ((c & 0xf0) == 0xf0) {
  602. if (cur[3] == 0) {
  603. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  604. cur = ctxt->input->cur;
  605. }
  606. if (((c & 0xf8) != 0xf0) ||
  607. ((cur[3] & 0xc0) != 0x80))
  608. goto encoding_error;
  609. /* 4-byte code */
  610. *len = 4;
  611. val = (cur[0] & 0x7) << 18;
  612. val |= (cur[1] & 0x3f) << 12;
  613. val |= (cur[2] & 0x3f) << 6;
  614. val |= cur[3] & 0x3f;
  615. if (val < 0x10000)
  616. goto encoding_error;
  617. } else {
  618. /* 3-byte code */
  619. *len = 3;
  620. val = (cur[0] & 0xf) << 12;
  621. val |= (cur[1] & 0x3f) << 6;
  622. val |= cur[2] & 0x3f;
  623. if (val < 0x800)
  624. goto encoding_error;
  625. }
  626. } else {
  627. /* 2-byte code */
  628. *len = 2;
  629. val = (cur[0] & 0x1f) << 6;
  630. val |= cur[1] & 0x3f;
  631. if (val < 0x80)
  632. goto encoding_error;
  633. }
  634. if (!IS_CHAR(val)) {
  635. xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
  636. "Char 0x%X out of allowed range\n", val);
  637. }
  638. return(val);
  639. } else {
  640. /* 1-byte code */
  641. *len = 1;
  642. if (*ctxt->input->cur == 0)
  643. xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
  644. if ((*ctxt->input->cur == 0) &&
  645. (ctxt->input->end > ctxt->input->cur)) {
  646. xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
  647. "Char 0x0 out of allowed range\n", 0);
  648. }
  649. if (*ctxt->input->cur == 0xD) {
  650. if (ctxt->input->cur[1] == 0xA) {
  651. ctxt->nbChars++;
  652. ctxt->input->cur++;
  653. }
  654. return(0xA);
  655. }
  656. return((int) *ctxt->input->cur);
  657. }
  658. }
  659. /*
  660. * Assume it's a fixed length encoding (1) with
  661. * a compatible encoding for the ASCII set, since
  662. * XML constructs only use < 128 chars
  663. */
  664. *len = 1;
  665. if (*ctxt->input->cur == 0xD) {
  666. if (ctxt->input->cur[1] == 0xA) {
  667. ctxt->nbChars++;
  668. ctxt->input->cur++;
  669. }
  670. return(0xA);
  671. }
  672. return((int) *ctxt->input->cur);
  673. encoding_error:
  674. /*
  675. * An encoding problem may arise from a truncated input buffer
  676. * splitting a character in the middle. In that case do not raise
  677. * an error but return 0 to endicate an end of stream problem
  678. */
  679. if (ctxt->input->end - ctxt->input->cur < 4) {
  680. *len = 0;
  681. return(0);
  682. }
  683. /*
  684. * If we detect an UTF8 error that probably mean that the
  685. * input encoding didn't get properly advertised in the
  686. * declaration header. Report the error and switch the encoding
  687. * to ISO-Latin-1 (if you don't like this policy, just declare the
  688. * encoding !)
  689. */
  690. {
  691. char buffer[150];
  692. snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
  693. ctxt->input->cur[0], ctxt->input->cur[1],
  694. ctxt->input->cur[2], ctxt->input->cur[3]);
  695. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  696. "Input is not proper UTF-8, indicate encoding !\n%s",
  697. BAD_CAST buffer, NULL);
  698. }
  699. ctxt->charset = XML_CHAR_ENCODING_8859_1;
  700. *len = 1;
  701. return((int) *ctxt->input->cur);
  702. }
  703. /**
  704. * xmlStringCurrentChar:
  705. * @ctxt: the XML parser context
  706. * @cur: pointer to the beginning of the char
  707. * @len: pointer to the length of the char read
  708. *
  709. * The current char value, if using UTF-8 this may actually span multiple
  710. * bytes in the input buffer.
  711. *
  712. * Returns the current char value and its length
  713. */
  714. int
  715. xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
  716. {
  717. if ((len == NULL) || (cur == NULL)) return(0);
  718. if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
  719. /*
  720. * We are supposed to handle UTF8, check it's valid
  721. * From rfc2044: encoding of the Unicode values on UTF-8:
  722. *
  723. * UCS-4 range (hex.) UTF-8 octet sequence (binary)
  724. * 0000 0000-0000 007F 0xxxxxxx
  725. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
  726. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  727. *
  728. * Check for the 0x110000 limit too
  729. */
  730. unsigned char c;
  731. unsigned int val;
  732. c = *cur;
  733. if (c & 0x80) {
  734. if ((cur[1] & 0xc0) != 0x80)
  735. goto encoding_error;
  736. if ((c & 0xe0) == 0xe0) {
  737. if ((cur[2] & 0xc0) != 0x80)
  738. goto encoding_error;
  739. if ((c & 0xf0) == 0xf0) {
  740. if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
  741. goto encoding_error;
  742. /* 4-byte code */
  743. *len = 4;
  744. val = (cur[0] & 0x7) << 18;
  745. val |= (cur[1] & 0x3f) << 12;
  746. val |= (cur[2] & 0x3f) << 6;
  747. val |= cur[3] & 0x3f;
  748. } else {
  749. /* 3-byte code */
  750. *len = 3;
  751. val = (cur[0] & 0xf) << 12;
  752. val |= (cur[1] & 0x3f) << 6;
  753. val |= cur[2] & 0x3f;
  754. }
  755. } else {
  756. /* 2-byte code */
  757. *len = 2;
  758. val = (cur[0] & 0x1f) << 6;
  759. val |= cur[1] & 0x3f;
  760. }
  761. if (!IS_CHAR(val)) {
  762. xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
  763. "Char 0x%X out of allowed range\n", val);
  764. }
  765. return (val);
  766. } else {
  767. /* 1-byte code */
  768. *len = 1;
  769. return ((int) *cur);
  770. }
  771. }
  772. /*
  773. * Assume it's a fixed length encoding (1) with
  774. * a compatible encoding for the ASCII set, since
  775. * XML constructs only use < 128 chars
  776. */
  777. *len = 1;
  778. return ((int) *cur);
  779. encoding_error:
  780. /*
  781. * An encoding problem may arise from a truncated input buffer
  782. * splitting a character in the middle. In that case do not raise
  783. * an error but return 0 to endicate an end of stream problem
  784. */
  785. if ((ctxt == NULL) || (ctxt->input == NULL) ||
  786. (ctxt->input->end - ctxt->input->cur < 4)) {
  787. *len = 0;
  788. return(0);
  789. }
  790. /*
  791. * If we detect an UTF8 error that probably mean that the
  792. * input encoding didn't get properly advertised in the
  793. * declaration header. Report the error and switch the encoding
  794. * to ISO-Latin-1 (if you don't like this policy, just declare the
  795. * encoding !)
  796. */
  797. {
  798. char buffer[150];
  799. snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
  800. ctxt->input->cur[0], ctxt->input->cur[1],
  801. ctxt->input->cur[2], ctxt->input->cur[3]);
  802. __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
  803. "Input is not proper UTF-8, indicate encoding !\n%s",
  804. BAD_CAST buffer, NULL);
  805. }
  806. *len = 1;
  807. return ((int) *cur);
  808. }
  809. /**
  810. * xmlCopyCharMultiByte:
  811. * @out: pointer to an array of xmlChar
  812. * @val: the char value
  813. *
  814. * append the char value in the array
  815. *
  816. * Returns the number of xmlChar written
  817. */
  818. int
  819. xmlCopyCharMultiByte(xmlChar *out, int val) {
  820. if (out == NULL) return(0);
  821. /*
  822. * We are supposed to handle UTF8, check it's valid
  823. * From rfc2044: encoding of the Unicode values on UTF-8:
  824. *
  825. * UCS-4 range (hex.) UTF-8 octet sequence (binary)
  826. * 0000 0000-0000 007F 0xxxxxxx
  827. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
  828. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  829. */
  830. if (val >= 0x80) {
  831. xmlChar *savedout = out;
  832. int bits;
  833. if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
  834. else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
  835. else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
  836. else {
  837. xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
  838. "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
  839. val);
  840. return(0);
  841. }
  842. for ( ; bits >= 0; bits-= 6)
  843. *out++= ((val >> bits) & 0x3F) | 0x80 ;
  844. return (out - savedout);
  845. }
  846. *out = (xmlChar) val;
  847. return 1;
  848. }
  849. /**
  850. * xmlCopyChar:
  851. * @len: Ignored, compatibility
  852. * @out: pointer to an array of xmlChar
  853. * @val: the char value
  854. *
  855. * append the char value in the array
  856. *
  857. * Returns the number of xmlChar written
  858. */
  859. int
  860. xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
  861. if (out == NULL) return(0);
  862. /* the len parameter is ignored */
  863. if (val >= 0x80) {
  864. return(xmlCopyCharMultiByte (out, val));
  865. }
  866. *out = (xmlChar) val;
  867. return 1;
  868. }
  869. /************************************************************************
  870. * *
  871. * Commodity functions to switch encodings *
  872. * *
  873. ************************************************************************/
  874. /* defined in encoding.c, not public */
  875. int
  876. xmlCharEncFirstLineInt(xmlCharEncodingHandler *handler, xmlBufferPtr out,
  877. xmlBufferPtr in, int len);
  878. static int
  879. xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
  880. xmlCharEncodingHandlerPtr handler, int len);
  881. static int
  882. xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
  883. xmlCharEncodingHandlerPtr handler, int len);
  884. /**
  885. * xmlSwitchEncoding:
  886. * @ctxt: the parser context
  887. * @enc: the encoding value (number)
  888. *
  889. * change the input functions when discovering the character encoding
  890. * of a given entity.
  891. *
  892. * Returns 0 in case of success, -1 otherwise
  893. */
  894. int
  895. xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
  896. {
  897. xmlCharEncodingHandlerPtr handler;
  898. int len = -1;
  899. if (ctxt == NULL) return(-1);
  900. switch (enc) {
  901. case XML_CHAR_ENCODING_ERROR:
  902. __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
  903. "encoding unknown\n", NULL, NULL);
  904. return(-1);
  905. case XML_CHAR_ENCODING_NONE:
  906. /* let's assume it's UTF-8 without the XML decl */
  907. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  908. return(0);
  909. case XML_CHAR_ENCODING_UTF8:
  910. /* default encoding, no conversion should be needed */
  911. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  912. /*
  913. * Errata on XML-1.0 June 20 2001
  914. * Specific handling of the Byte Order Mark for
  915. * UTF-8
  916. */
  917. if ((ctxt->input != NULL) &&
  918. (ctxt->input->cur[0] == 0xEF) &&
  919. (ctxt->input->cur[1] == 0xBB) &&
  920. (ctxt->input->cur[2] == 0xBF)) {
  921. ctxt->input->cur += 3;
  922. }
  923. return(0);
  924. case XML_CHAR_ENCODING_UTF16LE:
  925. case XML_CHAR_ENCODING_UTF16BE:
  926. /*The raw input characters are encoded
  927. *in UTF-16. As we expect this function
  928. *to be called after xmlCharEncInFunc, we expect
  929. *ctxt->input->cur to contain UTF-8 encoded characters.
  930. *So the raw UTF16 Byte Order Mark
  931. *has also been converted into
  932. *an UTF-8 BOM. Let's skip that BOM.
  933. */
  934. if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
  935. (ctxt->input->cur[0] == 0xEF) &&
  936. (ctxt->input->cur[1] == 0xBB) &&
  937. (ctxt->input->cur[2] == 0xBF)) {
  938. ctxt->input->cur += 3;
  939. }
  940. len = 90;
  941. break;
  942. case XML_CHAR_ENCODING_UCS2:
  943. len = 90;
  944. break;
  945. case XML_CHAR_ENCODING_UCS4BE:
  946. case XML_CHAR_ENCODING_UCS4LE:
  947. case XML_CHAR_ENCODING_UCS4_2143:
  948. case XML_CHAR_ENCODING_UCS4_3412:
  949. len = 180;
  950. break;
  951. case XML_CHAR_ENCODING_EBCDIC:
  952. case XML_CHAR_ENCODING_8859_1:
  953. case XML_CHAR_ENCODING_8859_2:
  954. case XML_CHAR_ENCODING_8859_3:
  955. case XML_CHAR_ENCODING_8859_4:
  956. case XML_CHAR_ENCODING_8859_5:
  957. case XML_CHAR_ENCODING_8859_6:
  958. case XML_CHAR_ENCODING_8859_7:
  959. case XML_CHAR_ENCODING_8859_8:
  960. case XML_CHAR_ENCODING_8859_9:
  961. case XML_CHAR_ENCODING_ASCII:
  962. case XML_CHAR_ENCODING_2022_JP:
  963. case XML_CHAR_ENCODING_SHIFT_JIS:
  964. case XML_CHAR_ENCODING_EUC_JP:
  965. len = 45;
  966. break;
  967. }
  968. handler = xmlGetCharEncodingHandler(enc);
  969. if (handler == NULL) {
  970. /*
  971. * Default handlers.
  972. */
  973. switch (enc) {
  974. case XML_CHAR_ENCODING_ASCII:
  975. /* default encoding, no conversion should be needed */
  976. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  977. return(0);
  978. case XML_CHAR_ENCODING_UTF16LE:
  979. break;
  980. case XML_CHAR_ENCODING_UTF16BE:
  981. break;
  982. case XML_CHAR_ENCODING_UCS4LE:
  983. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  984. "encoding not supported %s\n",
  985. BAD_CAST "USC4 little endian", NULL);
  986. break;
  987. case XML_CHAR_ENCODING_UCS4BE:
  988. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  989. "encoding not supported %s\n",
  990. BAD_CAST "USC4 big endian", NULL);
  991. break;
  992. case XML_CHAR_ENCODING_EBCDIC:
  993. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  994. "encoding not supported %s\n",
  995. BAD_CAST "EBCDIC", NULL);
  996. break;
  997. case XML_CHAR_ENCODING_UCS4_2143:
  998. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  999. "encoding not supported %s\n",
  1000. BAD_CAST "UCS4 2143", NULL);
  1001. break;
  1002. case XML_CHAR_ENCODING_UCS4_3412:
  1003. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  1004. "encoding not supported %s\n",
  1005. BAD_CAST "UCS4 3412", NULL);
  1006. break;
  1007. case XML_CHAR_ENCODING_UCS2:
  1008. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  1009. "encoding not supported %s\n",
  1010. BAD_CAST "UCS2", NULL);
  1011. break;
  1012. case XML_CHAR_ENCODING_8859_1:
  1013. case XML_CHAR_ENCODING_8859_2:
  1014. case XML_CHAR_ENCODING_8859_3:
  1015. case XML_CHAR_ENCODING_8859_4:
  1016. case XML_CHAR_ENCODING_8859_5:
  1017. case XML_CHAR_ENCODING_8859_6:
  1018. case XML_CHAR_ENCODING_8859_7:
  1019. case XML_CHAR_ENCODING_8859_8:
  1020. case XML_CHAR_ENCODING_8859_9:
  1021. /*
  1022. * We used to keep the internal content in the
  1023. * document encoding however this turns being unmaintainable
  1024. * So xmlGetCharEncodingHandler() will return non-null
  1025. * values for this now.
  1026. */
  1027. if ((ctxt->inputNr == 1) &&
  1028. (ctxt->encoding == NULL) &&
  1029. (ctxt->input != NULL) &&
  1030. (ctxt->input->encoding != NULL)) {
  1031. ctxt->encoding = xmlStrdup(ctxt->input->encoding);
  1032. }
  1033. ctxt->charset = enc;
  1034. return(0);
  1035. case XML_CHAR_ENCODING_2022_JP:
  1036. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  1037. "encoding not supported %s\n",
  1038. BAD_CAST "ISO-2022-JP", NULL);
  1039. break;
  1040. case XML_CHAR_ENCODING_SHIFT_JIS:
  1041. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  1042. "encoding not supported %s\n",
  1043. BAD_CAST "Shift_JIS", NULL);
  1044. break;
  1045. case XML_CHAR_ENCODING_EUC_JP:
  1046. __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  1047. "encoding not supported %s\n",
  1048. BAD_CAST "EUC-JP", NULL);
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. }
  1054. if (handler == NULL)
  1055. return(-1);
  1056. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  1057. return(xmlSwitchToEncodingInt(ctxt, handler, len));
  1058. }
  1059. /**
  1060. * xmlSwitchInputEncoding:
  1061. * @ctxt: the parser context
  1062. * @input: the input stream
  1063. * @handler: the encoding handler
  1064. * @len: the number of bytes to convert for the first line or -1
  1065. *
  1066. * change the input functions when discovering the character encoding
  1067. * of a given entity.
  1068. *
  1069. * Returns 0 in case of success, -1 otherwise
  1070. */
  1071. static int
  1072. xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
  1073. xmlCharEncodingHandlerPtr handler, int len)
  1074. {
  1075. int nbchars;
  1076. if (handler == NULL)
  1077. return (-1);
  1078. if (input == NULL)
  1079. return (-1);
  1080. if (input->buf != NULL) {
  1081. if (input->buf->encoder != NULL) {
  1082. /*
  1083. * Check in case the auto encoding detetection triggered
  1084. * in already.
  1085. */
  1086. if (input->buf->encoder == handler)
  1087. return (0);
  1088. /*
  1089. * "UTF-16" can be used for both LE and BE
  1090. if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
  1091. BAD_CAST "UTF-16", 6)) &&
  1092. (!xmlStrncmp(BAD_CAST handler->name,
  1093. BAD_CAST "UTF-16", 6))) {
  1094. return(0);
  1095. }
  1096. */
  1097. /*
  1098. * Note: this is a bit dangerous, but that's what it
  1099. * takes to use nearly compatible signature for different
  1100. * encodings.
  1101. */
  1102. xmlCharEncCloseFunc(input->buf->encoder);
  1103. input->buf->encoder = handler;
  1104. return (0);
  1105. }
  1106. input->buf->encoder = handler;
  1107. /*
  1108. * Is there already some content down the pipe to convert ?
  1109. */
  1110. if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) {
  1111. int processed;
  1112. unsigned int use;
  1113. /*
  1114. * Specific handling of the Byte Order Mark for
  1115. * UTF-16
  1116. */
  1117. if ((handler->name != NULL) &&
  1118. (!strcmp(handler->name, "UTF-16LE") ||
  1119. !strcmp(handler->name, "UTF-16")) &&
  1120. (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
  1121. input->cur += 2;
  1122. }
  1123. if ((handler->name != NULL) &&
  1124. (!strcmp(handler->name, "UTF-16BE")) &&
  1125. (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
  1126. input->cur += 2;
  1127. }
  1128. /*
  1129. * Errata on XML-1.0 June 20 2001
  1130. * Specific handling of the Byte Order Mark for
  1131. * UTF-8
  1132. */
  1133. if ((handler->name != NULL) &&
  1134. (!strcmp(handler->name, "UTF-8")) &&
  1135. (input->cur[0] == 0xEF) &&
  1136. (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
  1137. input->cur += 3;
  1138. }
  1139. /*
  1140. * Shrink the current input buffer.
  1141. * Move it as the raw buffer and create a new input buffer
  1142. */
  1143. processed = input->cur - input->base;
  1144. xmlBufferShrink(input->buf->buffer, processed);
  1145. input->buf->raw = input->buf->buffer;
  1146. input->buf->buffer = xmlBufferCreate();
  1147. input->buf->rawconsumed = processed;
  1148. use = input->buf->raw->use;
  1149. if (ctxt->html) {
  1150. /*
  1151. * convert as much as possible of the buffer
  1152. */
  1153. nbchars = xmlCharEncInFunc(input->buf->encoder,
  1154. input->buf->buffer,
  1155. input->buf->raw);
  1156. } else {
  1157. /*
  1158. * convert just enough to get
  1159. * '<?xml version="1.0" encoding="xxx"?>'
  1160. * parsed with the autodetected encoding
  1161. * into the parser reading buffer.
  1162. */
  1163. nbchars = xmlCharEncFirstLineInt(input->buf->encoder,
  1164. input->buf->buffer,
  1165. input->buf->raw,
  1166. len);
  1167. }
  1168. if (nbchars < 0) {
  1169. xmlErrInternal(ctxt,
  1170. "switching encoding: encoder error\n",
  1171. NULL);
  1172. return (-1);
  1173. }
  1174. input->buf->rawconsumed += use - input->buf->raw->use;
  1175. input->base = input->cur = input->buf->buffer->content;
  1176. input->end = &input->base[input->buf->buffer->use];
  1177. }
  1178. return (0);
  1179. } else if (input->length == 0) {
  1180. /*
  1181. * When parsing a static memory array one must know the
  1182. * size to be able to convert the buffer.
  1183. */
  1184. xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
  1185. return (-1);
  1186. }
  1187. return (0);
  1188. }
  1189. /**
  1190. * xmlSwitchInputEncoding:
  1191. * @ctxt: the parser context
  1192. * @input: the input stream
  1193. * @handler: the encoding handler
  1194. *
  1195. * change the input functions when discovering the character encoding
  1196. * of a given entity.
  1197. *
  1198. * Returns 0 in case of success, -1 otherwise
  1199. */
  1200. int
  1201. xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
  1202. xmlCharEncodingHandlerPtr handler) {
  1203. return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
  1204. }
  1205. /**
  1206. * xmlSwitchToEncodingInt:
  1207. * @ctxt: the parser context
  1208. * @handler: the encoding handler
  1209. * @len: the lenght to convert or -1
  1210. *
  1211. * change the input functions when discovering the character encoding
  1212. * of a given entity, and convert only @len bytes of the output, this
  1213. * is needed on auto detect to allows any declared encoding later to
  1214. * convert the actual content after the xmlDecl
  1215. *
  1216. * Returns 0 in case of success, -1 otherwise
  1217. */
  1218. static int
  1219. xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
  1220. xmlCharEncodingHandlerPtr handler, int len) {
  1221. int ret = 0;
  1222. if (handler != NULL) {
  1223. if (ctxt->input != NULL) {
  1224. ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
  1225. } else {
  1226. xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
  1227. NULL);
  1228. return(-1);
  1229. }
  1230. /*
  1231. * The parsing is now done in UTF8 natively
  1232. */
  1233. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  1234. } else
  1235. return(-1);
  1236. return(ret);
  1237. }
  1238. /**
  1239. * xmlSwitchToEncoding:
  1240. * @ctxt: the parser context
  1241. * @handler: the encoding handler
  1242. *
  1243. * change the input functions when discovering the character encoding
  1244. * of a given entity.
  1245. *
  1246. * Returns 0 in case of success, -1 otherwise
  1247. */
  1248. int
  1249. xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
  1250. {
  1251. return (xmlSwitchToEncodingInt(ctxt, handler, -1));
  1252. }
  1253. /************************************************************************
  1254. * *
  1255. * Commodity functions to handle entities processing *
  1256. * *
  1257. ************************************************************************/
  1258. /**
  1259. * xmlFreeInputStream:
  1260. * @input: an xmlParserInputPtr
  1261. *
  1262. * Free up an input stream.
  1263. */
  1264. void
  1265. xmlFreeInputStream(xmlParserInputPtr input) {
  1266. if (input == NULL) return;
  1267. if (input->filename != NULL) xmlFree((char *) input->filename);
  1268. if (input->directory != NULL) xmlFree((char *) input->directory);
  1269. if (input->encoding != NULL) xmlFree((char *) input->encoding);
  1270. if (input->version != NULL) xmlFree((char *) input->version);
  1271. if ((input->free != NULL) && (input->base != NULL))
  1272. input->free((xmlChar *) input->base);
  1273. if (input->buf != NULL)
  1274. xmlFreeParserInputBuffer(input->buf);
  1275. xmlFree(input);
  1276. }
  1277. /**
  1278. * xmlNewInputStream:
  1279. * @ctxt: an XML parser context
  1280. *
  1281. * Create a new input stream structure
  1282. * Returns the new input stream or NULL
  1283. */
  1284. xmlParserInputPtr
  1285. xmlNewInputStream(xmlParserCtxtPtr ctxt) {
  1286. xmlParserInputPtr input;
  1287. static int id = 0;
  1288. input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
  1289. if (input == NULL) {
  1290. xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
  1291. return(NULL);
  1292. }
  1293. memset(input, 0, sizeof(xmlParserInput));
  1294. input->line = 1;
  1295. input->col = 1;
  1296. input->standalone = -1;
  1297. /*
  1298. * we don't care about thread reentrancy unicity for a single
  1299. * parser context (and hence thread) is sufficient.
  1300. */
  1301. input->id = id++;
  1302. return(input);
  1303. }
  1304. /**
  1305. * xmlNewIOInputStream:
  1306. * @ctxt: an XML parser context
  1307. * @input: an I/O Input
  1308. * @enc: the charset encoding if known
  1309. *
  1310. * Create a new input stream structure encapsulating the @input into
  1311. * a stream suitable for the parser.
  1312. *
  1313. * Returns the new input stream or NULL
  1314. */
  1315. xmlParserInputPtr
  1316. xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
  1317. xmlCharEncoding enc) {
  1318. xmlParserInputPtr inputStream;
  1319. if (input == NULL) return(NULL);
  1320. if (xmlParserDebugEntities)
  1321. xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
  1322. inputStream = xmlNewInputStream(ctxt);
  1323. if (inputStream == NULL) {
  1324. return(NULL);
  1325. }
  1326. inputStream->filename = NULL;
  1327. inputStream->buf = input;
  1328. inputStream->base = inputStream->buf->buffer->content;
  1329. inputStream->cur = inputStream->buf->buffer->content;
  1330. inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
  1331. if (enc != XML_CHAR_ENCODING_NONE) {
  1332. xmlSwitchEncoding(ctxt, enc);
  1333. }
  1334. return(inputStream);
  1335. }
  1336. /**
  1337. * xmlNewEntityInputStream:
  1338. * @ctxt: an XML parser context
  1339. * @entity: an Entity pointer
  1340. *
  1341. * Create a new input stream based on an xmlEntityPtr
  1342. *
  1343. * Returns the new input stream or NULL
  1344. */
  1345. xmlParserInputPtr
  1346. xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
  1347. xmlParserInputPtr input;
  1348. if (entity == NULL) {
  1349. xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
  1350. NULL);
  1351. return(NULL);
  1352. }
  1353. if (xmlParserDebugEntities)
  1354. xmlGenericError(xmlGenericErrorContext,
  1355. "new input from entity: %s\n", entity->name);
  1356. if (entity->content == NULL) {
  1357. switch (entity->etype) {
  1358. case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
  1359. xmlErrInternal(ctxt, "Cannot parse entity %s\n",
  1360. entity->name);
  1361. break;
  1362. case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
  1363. case XML_EXTERNAL_PARAMETER_ENTITY:
  1364. return(xmlLoadExternalEntity((char *) entity->URI,
  1365. (char *) entity->ExternalID, ctxt));
  1366. case XML_INTERNAL_GENERAL_ENTITY:
  1367. xmlErrInternal(ctxt,
  1368. "Internal entity %s without content !\n",
  1369. entity->name);
  1370. break;
  1371. case XML_INTERNAL_PARAMETER_ENTITY:
  1372. xmlErrInternal(ctxt,
  1373. "Internal parameter entity %s without content !\n",
  1374. entity->name);
  1375. break;
  1376. case XML_INTERNAL_PREDEFINED_ENTITY:
  1377. xmlErrInternal(ctxt,
  1378. "Predefined entity %s without content !\n",
  1379. entity->name);
  1380. break;
  1381. }
  1382. return(NULL);
  1383. }
  1384. input = xmlNewInputStream(ctxt);
  1385. if (input == NULL) {
  1386. return(NULL);
  1387. }
  1388. if (entity->URI != NULL)
  1389. input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
  1390. input->base = entity->content;
  1391. input->cur = entity->content;
  1392. input->length = entity->length;
  1393. input->end = &entity->content[input->length];
  1394. return(input);
  1395. }
  1396. /**
  1397. * xmlNewStringInputStream:
  1398. * @ctxt: an XML parser context
  1399. * @buffer: an memory buffer
  1400. *
  1401. * Create a new input stream based on a memory buffer.
  1402. * Returns the new input stream
  1403. */
  1404. xmlParserInputPtr
  1405. xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
  1406. xmlParserInputPtr input;
  1407. if (buffer == NULL) {
  1408. xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
  1409. NULL);
  1410. return(NULL);
  1411. }
  1412. if (xmlParserDebugEntities)
  1413. xmlGenericError(xmlGenericErrorContext,
  1414. "new fixed input: %.30s\n", buffer);
  1415. input = xmlNewInputStream(ctxt);
  1416. if (input == NULL) {
  1417. xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
  1418. return(NULL);
  1419. }
  1420. input->base = buffer;
  1421. input->cur = buffer;
  1422. input->length = xmlStrlen(buffer);
  1423. input->end = &buffer[input->length];
  1424. return(input);
  1425. }
  1426. /**
  1427. * xmlNewInputFromFile:
  1428. * @ctxt: an XML parser context
  1429. * @filename: the filename to use as entity
  1430. *
  1431. * Create a new input stream based on a file or an URL.
  1432. *
  1433. * Returns the new input stream or NULL in case of error
  1434. */
  1435. xmlParserInputPtr
  1436. xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
  1437. xmlParserInputBufferPtr buf;
  1438. xmlParserInputPtr inputStream;
  1439. char *directory = NULL;
  1440. xmlChar *URI = NULL;
  1441. if (xmlParserDebugEntities)
  1442. xmlGenericError(xmlGenericErrorContext,
  1443. "new input from file: %s\n", filename);
  1444. if (ctxt == NULL) return(NULL);
  1445. buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
  1446. if (buf == NULL) {
  1447. if (filename == NULL)
  1448. __xmlLoaderErr(ctxt,
  1449. "failed to load external entity: NULL filename \n",
  1450. NULL);
  1451. else
  1452. __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
  1453. (const char *) filename);
  1454. return(NULL);
  1455. }
  1456. inputStream = xmlNewInputStream(ctxt);
  1457. if (inputStream == NULL)
  1458. return(NULL);
  1459. inputStream->buf = buf;
  1460. inputStream = xmlCheckHTTPInput(ctxt, inputStream);
  1461. if (inputStream == NULL)
  1462. return(NULL);
  1463. if (inputStream->filename == NULL)
  1464. URI = xmlStrdup((xmlChar *) filename);
  1465. else
  1466. URI = xmlStrdup((xmlChar *) inputStream->filename);
  1467. directory = xmlParserGetDirectory((const char *) URI);
  1468. if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
  1469. inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
  1470. if (URI != NULL) xmlFree((char *) URI);
  1471. inputStream->directory = directory;
  1472. inputStream->base = inputStream->buf->buffer->content;
  1473. inputStream->cur = inputStream->buf->buffer->content;
  1474. inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
  1475. if ((ctxt->directory == NULL) && (directory != NULL))
  1476. ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
  1477. return(inputStream);
  1478. }
  1479. /************************************************************************
  1480. * *
  1481. * Commodity functions to handle parser contexts *
  1482. * *
  1483. ************************************************************************/
  1484. /**
  1485. * xmlInitParserCtxt:
  1486. * @ctxt: an XML parser context
  1487. *
  1488. * Initialize a parser context
  1489. *
  1490. * Returns 0 in case of success and -1 in case of error
  1491. */
  1492. int
  1493. xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
  1494. {
  1495. xmlParserInputPtr input;
  1496. if(ctxt==NULL) {
  1497. xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
  1498. return(-1);
  1499. }
  1500. xmlDefaultSAXHandlerInit();
  1501. if (ctxt->dict == NULL)
  1502. ctxt->dict = xmlDictCreate();
  1503. if (ctxt->dict == NULL) {
  1504. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1505. return(-1);
  1506. }
  1507. if (ctxt->sax == NULL)
  1508. ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
  1509. if (ctxt->sax == NULL) {
  1510. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1511. return(-1);
  1512. }
  1513. else
  1514. xmlSAXVersion(ctxt->sax, 2);
  1515. ctxt->maxatts = 0;
  1516. ctxt->atts = NULL;
  1517. /* Allocate the Input stack */
  1518. if (ctxt->inputTab == NULL) {
  1519. ctxt->inputTab = (xmlParserInputPtr *)
  1520. xmlMalloc(5 * sizeof(xmlParserInputPtr));
  1521. ctxt->inputMax = 5;
  1522. }
  1523. if (ctxt->inputTab == NULL) {
  1524. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1525. ctxt->inputNr = 0;
  1526. ctxt->inputMax = 0;
  1527. ctxt->input = NULL;
  1528. return(-1);
  1529. }
  1530. while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
  1531. xmlFreeInputStream(input);
  1532. }
  1533. ctxt->inputNr = 0;
  1534. ctxt->input = NULL;
  1535. ctxt->version = NULL;
  1536. ctxt->encoding = NULL;
  1537. ctxt->standalone = -1;
  1538. ctxt->hasExternalSubset = 0;
  1539. ctxt->hasPErefs = 0;
  1540. ctxt->html = 0;
  1541. ctxt->external = 0;
  1542. ctxt->instate = XML_PARSER_START;
  1543. ctxt->token = 0;
  1544. ctxt->directory = NULL;
  1545. /* Allocate the Node stack */
  1546. if (ctxt->nodeTab == NULL) {
  1547. ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
  1548. ctxt->nodeMax = 10;
  1549. }
  1550. if (ctxt->nodeTab == NULL) {
  1551. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1552. ctxt->nodeNr = 0;
  1553. ctxt->nodeMax = 0;
  1554. ctxt->node = NULL;
  1555. ctxt->inputNr = 0;
  1556. ctxt->inputMax = 0;
  1557. ctxt->input = NULL;
  1558. return(-1);
  1559. }
  1560. ctxt->nodeNr = 0;
  1561. ctxt->node = NULL;
  1562. /* Allocate the Name stack */
  1563. if (ctxt->nameTab == NULL) {
  1564. ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
  1565. ctxt->nameMax = 10;
  1566. }
  1567. if (ctxt->nameTab == NULL) {
  1568. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1569. ctxt->nodeNr = 0;
  1570. ctxt->nodeMax = 0;
  1571. ctxt->node = NULL;
  1572. ctxt->inputNr = 0;
  1573. ctxt->inputMax = 0;
  1574. ctxt->input = NULL;
  1575. ctxt->nameNr = 0;
  1576. ctxt->nameMax = 0;
  1577. ctxt->name = NULL;
  1578. return(-1);
  1579. }
  1580. ctxt->nameNr = 0;
  1581. ctxt->name = NULL;
  1582. /* Allocate the space stack */
  1583. if (ctxt->spaceTab == NULL) {
  1584. ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
  1585. ctxt->spaceMax = 10;
  1586. }
  1587. if (ctxt->spaceTab == NULL) {
  1588. xmlErrMemory(NULL, "cannot initialize parser context\n");
  1589. ctxt->nodeNr = 0;
  1590. ctxt->nodeMax = 0;
  1591. ctxt->node = NULL;
  1592. ctxt->inputNr = 0;
  1593. ctxt->inputMax = 0;
  1594. ctxt->input = NULL;
  1595. ctxt->nameNr = 0;
  1596. ctxt->nameMax = 0;
  1597. ctxt->name = NULL;
  1598. ctxt->spaceNr = 0;
  1599. ctxt->spaceMax = 0;
  1600. ctxt->space = NULL;
  1601. return(-1);
  1602. }
  1603. ctxt->spaceNr = 1;
  1604. ctxt->spaceMax = 10;
  1605. ctxt->spaceTab[0] = -1;
  1606. ctxt->space = &ctxt->spaceTab[0];
  1607. ctxt->userData = ctxt;
  1608. ctxt->myDoc = NULL;
  1609. ctxt->wellFormed = 1;
  1610. ctxt->nsWellFormed = 1;
  1611. ctxt->valid = 1;
  1612. ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
  1613. ctxt->validate = xmlDoValidityCheckingDefaultValue;
  1614. ctxt->pedantic = xmlPedanticParserDefaultValue;
  1615. ctxt->linenumbers = xmlLineNumbersDefaultValue;
  1616. ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
  1617. if (ctxt->keepBlanks == 0)
  1618. ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  1619. ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
  1620. ctxt->vctxt.userData = ctxt;
  1621. ctxt->vctxt.error = xmlParserValidityError;
  1622. ctxt->vctxt.warning = xmlParserValidityWarning;
  1623. if (ctxt->validate) {
  1624. if (xmlGetWarningsDefaultValue == 0)
  1625. ctxt->vctxt.warning = NULL;
  1626. else
  1627. ctxt->vctxt.warning = xmlParserValidityWarning;
  1628. ctxt->vctxt.nodeMax = 0;
  1629. }
  1630. ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
  1631. ctxt->record_info = 0;
  1632. ctxt->nbChars = 0;
  1633. ctxt->checkIndex = 0;
  1634. ctxt->inSubset = 0;
  1635. ctxt->errNo = XML_ERR_OK;
  1636. ctxt->depth = 0;
  1637. ctxt->charset = XML_CHAR_ENCODING_UTF8;
  1638. ctxt->catalogs = NULL;
  1639. ctxt->nbentities = 0;
  1640. xmlInitNodeInfoSeq(&ctxt->node_seq);
  1641. return(0);
  1642. }
  1643. /**
  1644. * xmlFreeParserCtxt:
  1645. * @ctxt: an XML parser context
  1646. *
  1647. * Free all the memory used by a parser context. However the parsed
  1648. * document in ctxt->myDoc is not freed.
  1649. */
  1650. void
  1651. xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
  1652. {
  1653. xmlParserInputPtr input;
  1654. if (ctxt == NULL) return;
  1655. while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
  1656. xmlFreeInputStream(input);
  1657. }
  1658. if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
  1659. if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
  1660. if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
  1661. if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
  1662. if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
  1663. if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
  1664. if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
  1665. if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
  1666. if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
  1667. #ifdef LIBXML_SAX1_ENABLED
  1668. if ((ctxt->sax != NULL) &&
  1669. (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
  1670. #else
  1671. if (ctxt->sax != NULL)
  1672. #endif /* LIBXML_SAX1_ENABLED */
  1673. xmlFree(ctxt->sax);
  1674. if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
  1675. if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
  1676. if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
  1677. if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
  1678. if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
  1679. if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
  1680. if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
  1681. if (ctxt->attsDefault != NULL)
  1682. xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
  1683. if (ctxt->attsSpecial != NULL)
  1684. xmlHashFree(ctxt->attsSpecial, NULL);
  1685. if (ctxt->freeElems != NULL) {
  1686. xmlNodePtr cur, next;
  1687. cur = ctxt->freeElems;
  1688. while (cur != NULL) {
  1689. next = cur->next;
  1690. xmlFree(cur);
  1691. cur = next;
  1692. }
  1693. }
  1694. if (ctxt->freeAttrs != NULL) {
  1695. xmlAttrPtr cur, next;
  1696. cur = ctxt->freeAttrs;
  1697. while (cur != NULL) {
  1698. next = cur->next;
  1699. xmlFree(cur);
  1700. cur = next;
  1701. }
  1702. }
  1703. /*
  1704. * cleanup the error strings
  1705. */
  1706. if (ctxt->lastError.message != NULL)
  1707. xmlFree(ctxt->lastError.message);
  1708. if (ctxt->lastError.file != NULL)
  1709. xmlFree(ctxt->lastError.file);
  1710. if (ctxt->lastError.str1 != NULL)
  1711. xmlFree(ctxt->lastError.str1);
  1712. if (ctxt->lastError.str2 != NULL)
  1713. xmlFree(ctxt->lastError.str2);
  1714. if (ctxt->lastError.str3 != NULL)
  1715. xmlFree(ctxt->lastError.str3);
  1716. #ifdef LIBXML_CATALOG_ENABLED
  1717. if (ctxt->catalogs != NULL)
  1718. xmlCatalogFreeLocal(ctxt->catalogs);
  1719. #endif
  1720. xmlFree(ctxt);
  1721. }
  1722. /**
  1723. * xmlNewParserCtxt:
  1724. *
  1725. * Allocate and initialize a new parser context.
  1726. *
  1727. * Returns the xmlParserCtxtPtr or NULL
  1728. */
  1729. xmlParserCtxtPtr
  1730. xmlNewParserCtxt(void)
  1731. {
  1732. xmlParserCtxtPtr ctxt;
  1733. ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
  1734. if (ctxt == NULL) {
  1735. xmlErrMemory(NULL, "cannot allocate parser context\n");
  1736. return(NULL);
  1737. }
  1738. memset(ctxt, 0, sizeof(xmlParserCtxt));
  1739. if (xmlInitParserCtxt(ctxt) < 0) {
  1740. xmlFreeParserCtxt(ctxt);
  1741. return(NULL);
  1742. }
  1743. return(ctxt);
  1744. }
  1745. /************************************************************************
  1746. * *
  1747. * Handling of node informations *
  1748. * *
  1749. ************************************************************************/
  1750. /**
  1751. * xmlClearParserCtxt:
  1752. * @ctxt: an XML parser context
  1753. *
  1754. * Clear (release owned resources) and reinitialize a parser context
  1755. */
  1756. void
  1757. xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
  1758. {
  1759. if (ctxt==NULL)
  1760. return;
  1761. xmlClearNodeInfoSeq(&ctxt->node_seq);
  1762. xmlCtxtReset(ctxt);
  1763. }
  1764. /**
  1765. * xmlParserFindNodeInfo:
  1766. * @ctx: an XML parser context
  1767. * @node: an XML node within the tree
  1768. *
  1769. * Find the parser node info struct for a given node
  1770. *
  1771. * Returns an xmlParserNodeInfo block pointer or NULL
  1772. */
  1773. const xmlParserNodeInfo *
  1774. xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
  1775. {
  1776. unsigned long pos;
  1777. if ((ctx == NULL) || (node == NULL))
  1778. return (NULL);
  1779. /* Find position where node should be at */
  1780. pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
  1781. if (pos < ctx->node_seq.length
  1782. && ctx->node_seq.buffer[pos].node == node)
  1783. return &ctx->node_seq.buffer[pos];
  1784. else
  1785. return NULL;
  1786. }
  1787. /**
  1788. * xmlInitNodeInfoSeq:
  1789. * @seq: a node info sequence pointer
  1790. *
  1791. * -- Initialize (set to initial state) node info sequence
  1792. */
  1793. void
  1794. xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
  1795. {
  1796. if (seq == NULL)
  1797. return;
  1798. seq->length = 0;
  1799. seq->maximum = 0;
  1800. seq->buffer = NULL;
  1801. }
  1802. /**
  1803. * xmlClearNodeInfoSeq:
  1804. * @seq: a node info sequence pointer
  1805. *
  1806. * -- Clear (release memory and reinitialize) node
  1807. * info sequence
  1808. */
  1809. void
  1810. xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
  1811. {
  1812. if (seq == NULL)
  1813. return;
  1814. if (seq->buffer != NULL)
  1815. xmlFree(seq->buffer);
  1816. xmlInitNodeInfoSeq(seq);
  1817. }
  1818. /**
  1819. * xmlParserFindNodeInfoIndex:
  1820. * @seq: a node info sequence pointer
  1821. * @node: an XML node pointer
  1822. *
  1823. *
  1824. * xmlParserFindNodeInfoIndex : Find the index that the info record for
  1825. * the given node is or should be at in a sorted sequence
  1826. *
  1827. * Returns a long indicating the position of the record
  1828. */
  1829. unsigned long
  1830. xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
  1831. const xmlNodePtr node)
  1832. {
  1833. unsigned long upper, lower, middle;
  1834. int found = 0;
  1835. if ((seq == NULL) || (node == NULL))
  1836. return ((unsigned long) -1);
  1837. /* Do a binary search for the key */
  1838. lower = 1;
  1839. upper = seq->length;
  1840. middle = 0;
  1841. while (lower <= upper && !found) {
  1842. middle = lower + (upper - lower) / 2;
  1843. if (node == seq->buffer[middle - 1].node)
  1844. found = 1;
  1845. else if (node < seq->buffer[middle - 1].node)
  1846. upper = middle - 1;
  1847. else
  1848. lower = middle + 1;
  1849. }
  1850. /* Return position */
  1851. if (middle == 0 || seq->buffer[middle - 1].node < node)
  1852. return middle;
  1853. else
  1854. return middle - 1;
  1855. }
  1856. /**
  1857. * xmlParserAddNodeInfo:
  1858. * @ctxt: an XML parser context
  1859. * @info: a node info sequence pointer
  1860. *
  1861. * Insert node info record into the sorted sequence
  1862. */
  1863. void
  1864. xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
  1865. const xmlParserNodeInfoPtr info)
  1866. {
  1867. unsigned long pos;
  1868. if ((ctxt == NULL) || (info == NULL)) return;
  1869. /* Find pos and check to see if node is already in the sequence */
  1870. pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
  1871. info->node);
  1872. if ((pos < ctxt->node_seq.length) &&
  1873. (ctxt->node_seq.buffer != NULL) &&
  1874. (ctxt->node_seq.buffer[pos].node == info->node)) {
  1875. ctxt->node_seq.buffer[pos] = *info;
  1876. }
  1877. /* Otherwise, we need to add new node to buffer */
  1878. else {
  1879. if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
  1880. xmlParserNodeInfo *tmp_buffer;
  1881. unsigned int byte_size;
  1882. if (ctxt->node_seq.maximum == 0)
  1883. ctxt->node_seq.maximum = 2;
  1884. byte_size = (sizeof(*ctxt->node_seq.buffer) *
  1885. (2 * ctxt->node_seq.maximum));
  1886. if (ctxt->node_seq.buffer == NULL)
  1887. tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
  1888. else
  1889. tmp_buffer =
  1890. (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
  1891. byte_size);
  1892. if (tmp_buffer == NULL) {
  1893. xmlErrMemory(ctxt, "failed to allocate buffer\n");
  1894. return;
  1895. }
  1896. ctxt->node_seq.buffer = tmp_buffer;
  1897. ctxt->node_seq.maximum *= 2;
  1898. }
  1899. /* If position is not at end, move elements out of the way */
  1900. if (pos != ctxt->node_seq.length) {
  1901. unsigned long i;
  1902. for (i = ctxt->node_seq.length; i > pos; i--)
  1903. ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
  1904. }
  1905. /* Copy element and increase length */
  1906. ctxt->node_seq.buffer[pos] = *info;
  1907. ctxt->node_seq.length++;
  1908. }
  1909. }
  1910. /************************************************************************
  1911. * *
  1912. * Defaults settings *
  1913. * *
  1914. ************************************************************************/
  1915. /**
  1916. * xmlPedanticParserDefault:
  1917. * @val: int 0 or 1
  1918. *
  1919. * Set and return the previous value for enabling pedantic warnings.
  1920. *
  1921. * Returns the last value for 0 for no substitution, 1 for substitution.
  1922. */
  1923. int
  1924. xmlPedanticParserDefault(int val) {
  1925. int old = xmlPedanticParserDefaultValue;
  1926. xmlPedanticParserDefaultValue = val;
  1927. return(old);
  1928. }
  1929. /**
  1930. * xmlLineNumbersDefault:
  1931. * @val: int 0 or 1
  1932. *
  1933. * Set and return the previous value for enabling line numbers in elements
  1934. * contents. This may break on old application and is turned off by default.
  1935. *
  1936. * Returns the last value for 0 for no substitution, 1 for substitution.
  1937. */
  1938. int
  1939. xmlLineNumbersDefault(int val) {
  1940. int old = xmlLineNumbersDefaultValue;
  1941. xmlLineNumbersDefaultValue = val;
  1942. return(old);
  1943. }
  1944. /**
  1945. * xmlSubstituteEntitiesDefault:
  1946. * @val: int 0 or 1
  1947. *
  1948. * Set and return the previous value for default entity support.
  1949. * Initially the parser always keep entity references instead of substituting
  1950. * entity values in the output. This function has to be used to change the
  1951. * default parser behavior
  1952. * SAX::substituteEntities() has to be used for changing that on a file by
  1953. * file basis.
  1954. *
  1955. * Returns the last value for 0 for no substitution, 1 for substitution.
  1956. */
  1957. int
  1958. xmlSubstituteEntitiesDefault(int val) {
  1959. int old = xmlSubstituteEntitiesDefaultValue;
  1960. xmlSubstituteEntitiesDefaultValue = val;
  1961. return(old);
  1962. }
  1963. /**
  1964. * xmlKeepBlanksDefault:
  1965. * @val: int 0 or 1
  1966. *
  1967. * Set and return the previous value for default blanks text nodes support.
  1968. * The 1.x version of the parser used an heuristic to try to detect
  1969. * ignorable white spaces. As a result the SAX callback was generating
  1970. * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
  1971. * using the DOM output text nodes containing those blanks were not generated.
  1972. * The 2.x and later version will switch to the XML standard way and
  1973. * ignorableWhitespace() are only generated when running the parser in
  1974. * validating mode and when the current element doesn't allow CDATA or
  1975. * mixed content.
  1976. * This function is provided as a way to force the standard behavior
  1977. * on 1.X libs and to switch back to the old mode for compatibility when
  1978. * running 1.X client code on 2.X . Upgrade of 1.X code should be done
  1979. * by using xmlIsBlankNode() commodity function to detect the "empty"
  1980. * nodes generated.
  1981. * This value also affect autogeneration of indentation when saving code
  1982. * if blanks sections are kept, indentation is not generated.
  1983. *
  1984. * Returns the last value for 0 for no substitution, 1 for substitution.
  1985. */
  1986. int
  1987. xmlKeepBlanksDefault(int val) {
  1988. int old = xmlKeepBlanksDefaultValue;
  1989. xmlKeepBlanksDefaultValue = val;
  1990. if (!val) xmlIndentTreeOutput = 1;
  1991. return(old);
  1992. }
  1993. #define bottom_parserInternals
  1994. #include "elfgcchack.h"