parserInternals.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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->inputTab != NULL) xmlFree(ctxt->inputTab);
  1662. if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
  1663. if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
  1664. if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
  1665. if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
  1666. #ifdef LIBXML_SAX1_ENABLED
  1667. if ((ctxt->sax != NULL) &&
  1668. (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
  1669. #else
  1670. if (ctxt->sax != NULL)
  1671. #endif /* LIBXML_SAX1_ENABLED */
  1672. xmlFree(ctxt->sax);
  1673. if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
  1674. if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
  1675. if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
  1676. if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
  1677. if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
  1678. if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
  1679. if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
  1680. if (ctxt->attsDefault != NULL)
  1681. xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
  1682. if (ctxt->attsSpecial != NULL)
  1683. xmlHashFree(ctxt->attsSpecial, NULL);
  1684. if (ctxt->freeElems != NULL) {
  1685. xmlNodePtr cur, next;
  1686. cur = ctxt->freeElems;
  1687. while (cur != NULL) {
  1688. next = cur->next;
  1689. xmlFree(cur);
  1690. cur = next;
  1691. }
  1692. }
  1693. if (ctxt->freeAttrs != NULL) {
  1694. xmlAttrPtr cur, next;
  1695. cur = ctxt->freeAttrs;
  1696. while (cur != NULL) {
  1697. next = cur->next;
  1698. xmlFree(cur);
  1699. cur = next;
  1700. }
  1701. }
  1702. /*
  1703. * cleanup the error strings
  1704. */
  1705. if (ctxt->lastError.message != NULL)
  1706. xmlFree(ctxt->lastError.message);
  1707. if (ctxt->lastError.file != NULL)
  1708. xmlFree(ctxt->lastError.file);
  1709. if (ctxt->lastError.str1 != NULL)
  1710. xmlFree(ctxt->lastError.str1);
  1711. if (ctxt->lastError.str2 != NULL)
  1712. xmlFree(ctxt->lastError.str2);
  1713. if (ctxt->lastError.str3 != NULL)
  1714. xmlFree(ctxt->lastError.str3);
  1715. #ifdef LIBXML_CATALOG_ENABLED
  1716. if (ctxt->catalogs != NULL)
  1717. xmlCatalogFreeLocal(ctxt->catalogs);
  1718. #endif
  1719. xmlFree(ctxt);
  1720. }
  1721. /**
  1722. * xmlNewParserCtxt:
  1723. *
  1724. * Allocate and initialize a new parser context.
  1725. *
  1726. * Returns the xmlParserCtxtPtr or NULL
  1727. */
  1728. xmlParserCtxtPtr
  1729. xmlNewParserCtxt(void)
  1730. {
  1731. xmlParserCtxtPtr ctxt;
  1732. ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
  1733. if (ctxt == NULL) {
  1734. xmlErrMemory(NULL, "cannot allocate parser context\n");
  1735. return(NULL);
  1736. }
  1737. memset(ctxt, 0, sizeof(xmlParserCtxt));
  1738. if (xmlInitParserCtxt(ctxt) < 0) {
  1739. xmlFreeParserCtxt(ctxt);
  1740. return(NULL);
  1741. }
  1742. return(ctxt);
  1743. }
  1744. /************************************************************************
  1745. * *
  1746. * Handling of node informations *
  1747. * *
  1748. ************************************************************************/
  1749. /**
  1750. * xmlClearParserCtxt:
  1751. * @ctxt: an XML parser context
  1752. *
  1753. * Clear (release owned resources) and reinitialize a parser context
  1754. */
  1755. void
  1756. xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
  1757. {
  1758. if (ctxt==NULL)
  1759. return;
  1760. xmlClearNodeInfoSeq(&ctxt->node_seq);
  1761. xmlCtxtReset(ctxt);
  1762. }
  1763. /**
  1764. * xmlParserFindNodeInfo:
  1765. * @ctx: an XML parser context
  1766. * @node: an XML node within the tree
  1767. *
  1768. * Find the parser node info struct for a given node
  1769. *
  1770. * Returns an xmlParserNodeInfo block pointer or NULL
  1771. */
  1772. const xmlParserNodeInfo *
  1773. xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
  1774. {
  1775. unsigned long pos;
  1776. if ((ctx == NULL) || (node == NULL))
  1777. return (NULL);
  1778. /* Find position where node should be at */
  1779. pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
  1780. if (pos < ctx->node_seq.length
  1781. && ctx->node_seq.buffer[pos].node == node)
  1782. return &ctx->node_seq.buffer[pos];
  1783. else
  1784. return NULL;
  1785. }
  1786. /**
  1787. * xmlInitNodeInfoSeq:
  1788. * @seq: a node info sequence pointer
  1789. *
  1790. * -- Initialize (set to initial state) node info sequence
  1791. */
  1792. void
  1793. xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
  1794. {
  1795. if (seq == NULL)
  1796. return;
  1797. seq->length = 0;
  1798. seq->maximum = 0;
  1799. seq->buffer = NULL;
  1800. }
  1801. /**
  1802. * xmlClearNodeInfoSeq:
  1803. * @seq: a node info sequence pointer
  1804. *
  1805. * -- Clear (release memory and reinitialize) node
  1806. * info sequence
  1807. */
  1808. void
  1809. xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
  1810. {
  1811. if (seq == NULL)
  1812. return;
  1813. if (seq->buffer != NULL)
  1814. xmlFree(seq->buffer);
  1815. xmlInitNodeInfoSeq(seq);
  1816. }
  1817. /**
  1818. * xmlParserFindNodeInfoIndex:
  1819. * @seq: a node info sequence pointer
  1820. * @node: an XML node pointer
  1821. *
  1822. *
  1823. * xmlParserFindNodeInfoIndex : Find the index that the info record for
  1824. * the given node is or should be at in a sorted sequence
  1825. *
  1826. * Returns a long indicating the position of the record
  1827. */
  1828. unsigned long
  1829. xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
  1830. const xmlNodePtr node)
  1831. {
  1832. unsigned long upper, lower, middle;
  1833. int found = 0;
  1834. if ((seq == NULL) || (node == NULL))
  1835. return ((unsigned long) -1);
  1836. /* Do a binary search for the key */
  1837. lower = 1;
  1838. upper = seq->length;
  1839. middle = 0;
  1840. while (lower <= upper && !found) {
  1841. middle = lower + (upper - lower) / 2;
  1842. if (node == seq->buffer[middle - 1].node)
  1843. found = 1;
  1844. else if (node < seq->buffer[middle - 1].node)
  1845. upper = middle - 1;
  1846. else
  1847. lower = middle + 1;
  1848. }
  1849. /* Return position */
  1850. if (middle == 0 || seq->buffer[middle - 1].node < node)
  1851. return middle;
  1852. else
  1853. return middle - 1;
  1854. }
  1855. /**
  1856. * xmlParserAddNodeInfo:
  1857. * @ctxt: an XML parser context
  1858. * @info: a node info sequence pointer
  1859. *
  1860. * Insert node info record into the sorted sequence
  1861. */
  1862. void
  1863. xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
  1864. const xmlParserNodeInfoPtr info)
  1865. {
  1866. unsigned long pos;
  1867. if ((ctxt == NULL) || (info == NULL)) return;
  1868. /* Find pos and check to see if node is already in the sequence */
  1869. pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
  1870. info->node);
  1871. if ((pos < ctxt->node_seq.length) &&
  1872. (ctxt->node_seq.buffer != NULL) &&
  1873. (ctxt->node_seq.buffer[pos].node == info->node)) {
  1874. ctxt->node_seq.buffer[pos] = *info;
  1875. }
  1876. /* Otherwise, we need to add new node to buffer */
  1877. else {
  1878. if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
  1879. xmlParserNodeInfo *tmp_buffer;
  1880. unsigned int byte_size;
  1881. if (ctxt->node_seq.maximum == 0)
  1882. ctxt->node_seq.maximum = 2;
  1883. byte_size = (sizeof(*ctxt->node_seq.buffer) *
  1884. (2 * ctxt->node_seq.maximum));
  1885. if (ctxt->node_seq.buffer == NULL)
  1886. tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
  1887. else
  1888. tmp_buffer =
  1889. (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
  1890. byte_size);
  1891. if (tmp_buffer == NULL) {
  1892. xmlErrMemory(ctxt, "failed to allocate buffer\n");
  1893. return;
  1894. }
  1895. ctxt->node_seq.buffer = tmp_buffer;
  1896. ctxt->node_seq.maximum *= 2;
  1897. }
  1898. /* If position is not at end, move elements out of the way */
  1899. if (pos != ctxt->node_seq.length) {
  1900. unsigned long i;
  1901. for (i = ctxt->node_seq.length; i > pos; i--)
  1902. ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
  1903. }
  1904. /* Copy element and increase length */
  1905. ctxt->node_seq.buffer[pos] = *info;
  1906. ctxt->node_seq.length++;
  1907. }
  1908. }
  1909. /************************************************************************
  1910. * *
  1911. * Defaults settings *
  1912. * *
  1913. ************************************************************************/
  1914. /**
  1915. * xmlPedanticParserDefault:
  1916. * @val: int 0 or 1
  1917. *
  1918. * Set and return the previous value for enabling pedantic warnings.
  1919. *
  1920. * Returns the last value for 0 for no substitution, 1 for substitution.
  1921. */
  1922. int
  1923. xmlPedanticParserDefault(int val) {
  1924. int old = xmlPedanticParserDefaultValue;
  1925. xmlPedanticParserDefaultValue = val;
  1926. return(old);
  1927. }
  1928. /**
  1929. * xmlLineNumbersDefault:
  1930. * @val: int 0 or 1
  1931. *
  1932. * Set and return the previous value for enabling line numbers in elements
  1933. * contents. This may break on old application and is turned off by default.
  1934. *
  1935. * Returns the last value for 0 for no substitution, 1 for substitution.
  1936. */
  1937. int
  1938. xmlLineNumbersDefault(int val) {
  1939. int old = xmlLineNumbersDefaultValue;
  1940. xmlLineNumbersDefaultValue = val;
  1941. return(old);
  1942. }
  1943. /**
  1944. * xmlSubstituteEntitiesDefault:
  1945. * @val: int 0 or 1
  1946. *
  1947. * Set and return the previous value for default entity support.
  1948. * Initially the parser always keep entity references instead of substituting
  1949. * entity values in the output. This function has to be used to change the
  1950. * default parser behavior
  1951. * SAX::substituteEntities() has to be used for changing that on a file by
  1952. * file basis.
  1953. *
  1954. * Returns the last value for 0 for no substitution, 1 for substitution.
  1955. */
  1956. int
  1957. xmlSubstituteEntitiesDefault(int val) {
  1958. int old = xmlSubstituteEntitiesDefaultValue;
  1959. xmlSubstituteEntitiesDefaultValue = val;
  1960. return(old);
  1961. }
  1962. /**
  1963. * xmlKeepBlanksDefault:
  1964. * @val: int 0 or 1
  1965. *
  1966. * Set and return the previous value for default blanks text nodes support.
  1967. * The 1.x version of the parser used an heuristic to try to detect
  1968. * ignorable white spaces. As a result the SAX callback was generating
  1969. * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
  1970. * using the DOM output text nodes containing those blanks were not generated.
  1971. * The 2.x and later version will switch to the XML standard way and
  1972. * ignorableWhitespace() are only generated when running the parser in
  1973. * validating mode and when the current element doesn't allow CDATA or
  1974. * mixed content.
  1975. * This function is provided as a way to force the standard behavior
  1976. * on 1.X libs and to switch back to the old mode for compatibility when
  1977. * running 1.X client code on 2.X . Upgrade of 1.X code should be done
  1978. * by using xmlIsBlankNode() commodity function to detect the "empty"
  1979. * nodes generated.
  1980. * This value also affect autogeneration of indentation when saving code
  1981. * if blanks sections are kept, indentation is not generated.
  1982. *
  1983. * Returns the last value for 0 for no substitution, 1 for substitution.
  1984. */
  1985. int
  1986. xmlKeepBlanksDefault(int val) {
  1987. int old = xmlKeepBlanksDefaultValue;
  1988. xmlKeepBlanksDefaultValue = val;
  1989. if (!val) xmlIndentTreeOutput = 1;
  1990. return(old);
  1991. }
  1992. #define bottom_parserInternals
  1993. #include "elfgcchack.h"