parserInternals.c 61 KB

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