SAX2.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. /*
  2. * SAX2.c : Default SAX2 handler to build a tree.
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * Daniel Veillard <daniel@veillard.com>
  7. */
  8. #define IN_LIBXML
  9. #include "libxml.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #include <stddef.h>
  14. #include <libxml/SAX2.h>
  15. #include <libxml/xmlmemory.h>
  16. #include <libxml/tree.h>
  17. #include <libxml/parser.h>
  18. #include <libxml/parserInternals.h>
  19. #include <libxml/valid.h>
  20. #include <libxml/entities.h>
  21. #include <libxml/xmlerror.h>
  22. #include <libxml/debugXML.h>
  23. #include <libxml/xmlIO.h>
  24. #include <libxml/uri.h>
  25. #include <libxml/valid.h>
  26. #include <libxml/HTMLtree.h>
  27. #include "private/error.h"
  28. #include "private/parser.h"
  29. #include "private/tree.h"
  30. /**
  31. * TODO:
  32. *
  33. * macro to flag unimplemented blocks
  34. * XML_CATALOG_PREFER user env to select between system/public preferred
  35. * option. C.f. Richard Tobin <richard@cogsci.ed.ac.uk>
  36. *> Just FYI, I am using an environment variable XML_CATALOG_PREFER with
  37. *> values "system" and "public". I have made the default be "system" to
  38. *> match yours.
  39. */
  40. #define TODO \
  41. xmlGenericError(xmlGenericErrorContext, \
  42. "Unimplemented block at %s:%d\n", \
  43. __FILE__, __LINE__);
  44. /*
  45. * xmlSAX2ErrMemory:
  46. * @ctxt: an XML validation parser context
  47. * @msg: a string to accompany the error message
  48. */
  49. static void LIBXML_ATTR_FORMAT(2,0)
  50. xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
  51. xmlStructuredErrorFunc schannel = NULL;
  52. const char *str1 = "out of memory\n";
  53. if (ctxt != NULL) {
  54. ctxt->errNo = XML_ERR_NO_MEMORY;
  55. if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
  56. schannel = ctxt->sax->serror;
  57. __xmlRaiseError(schannel,
  58. ctxt->vctxt.error, ctxt->vctxt.userData,
  59. ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
  60. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  61. NULL, NULL, 0, 0,
  62. msg, (const char *) str1, NULL);
  63. ctxt->errNo = XML_ERR_NO_MEMORY;
  64. ctxt->instate = XML_PARSER_EOF;
  65. ctxt->disableSAX = 1;
  66. } else {
  67. __xmlRaiseError(schannel,
  68. NULL, NULL,
  69. ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
  70. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  71. NULL, NULL, 0, 0,
  72. msg, (const char *) str1, NULL);
  73. }
  74. }
  75. /**
  76. * xmlValidError:
  77. * @ctxt: an XML validation parser context
  78. * @error: the error number
  79. * @msg: the error message
  80. * @str1: extra data
  81. * @str2: extra data
  82. *
  83. * Handle a validation error
  84. */
  85. static void LIBXML_ATTR_FORMAT(3,0)
  86. xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  87. const char *msg, const char *str1, const char *str2)
  88. {
  89. xmlStructuredErrorFunc schannel = NULL;
  90. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  91. (ctxt->instate == XML_PARSER_EOF))
  92. return;
  93. if (ctxt != NULL) {
  94. ctxt->errNo = error;
  95. if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
  96. schannel = ctxt->sax->serror;
  97. __xmlRaiseError(schannel,
  98. ctxt->vctxt.error, ctxt->vctxt.userData,
  99. ctxt, NULL, XML_FROM_DTD, error,
  100. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  101. (const char *) str2, NULL, 0, 0,
  102. msg, (const char *) str1, (const char *) str2);
  103. ctxt->valid = 0;
  104. } else {
  105. __xmlRaiseError(schannel,
  106. NULL, NULL,
  107. ctxt, NULL, XML_FROM_DTD, error,
  108. XML_ERR_ERROR, NULL, 0, (const char *) str1,
  109. (const char *) str2, NULL, 0, 0,
  110. msg, (const char *) str1, (const char *) str2);
  111. }
  112. }
  113. /**
  114. * xmlFatalErrMsg:
  115. * @ctxt: an XML parser context
  116. * @error: the error number
  117. * @msg: the error message
  118. * @str1: an error string
  119. * @str2: an error string
  120. *
  121. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  122. */
  123. static void LIBXML_ATTR_FORMAT(3,0)
  124. xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  125. const char *msg, const xmlChar *str1, const xmlChar *str2)
  126. {
  127. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  128. (ctxt->instate == XML_PARSER_EOF))
  129. return;
  130. if (ctxt != NULL)
  131. ctxt->errNo = error;
  132. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
  133. XML_ERR_FATAL, NULL, 0,
  134. (const char *) str1, (const char *) str2,
  135. NULL, 0, 0, msg, str1, str2);
  136. if (ctxt != NULL) {
  137. ctxt->wellFormed = 0;
  138. ctxt->valid = 0;
  139. if (ctxt->recovery == 0)
  140. ctxt->disableSAX = 1;
  141. }
  142. }
  143. /**
  144. * xmlWarnMsg:
  145. * @ctxt: an XML parser context
  146. * @error: the error number
  147. * @msg: the error message
  148. * @str1: an error string
  149. * @str2: an error string
  150. *
  151. * Handle a parser warning
  152. */
  153. static void LIBXML_ATTR_FORMAT(3,0)
  154. xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  155. const char *msg, const xmlChar *str1)
  156. {
  157. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  158. (ctxt->instate == XML_PARSER_EOF))
  159. return;
  160. if (ctxt != NULL)
  161. ctxt->errNo = error;
  162. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
  163. XML_ERR_WARNING, NULL, 0,
  164. (const char *) str1, NULL,
  165. NULL, 0, 0, msg, str1);
  166. }
  167. /**
  168. * xmlNsWarnMsg:
  169. * @ctxt: an XML parser context
  170. * @error: the error number
  171. * @msg: the error message
  172. * @str1: an error string
  173. *
  174. * Handle a namespace warning
  175. */
  176. static void LIBXML_ATTR_FORMAT(3,0)
  177. xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  178. const char *msg, const xmlChar *str1, const xmlChar *str2)
  179. {
  180. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  181. (ctxt->instate == XML_PARSER_EOF))
  182. return;
  183. if (ctxt != NULL)
  184. ctxt->errNo = error;
  185. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  186. XML_ERR_WARNING, NULL, 0,
  187. (const char *) str1, (const char *) str2,
  188. NULL, 0, 0, msg, str1, str2);
  189. }
  190. /**
  191. * xmlSAX2GetPublicId:
  192. * @ctx: the user data (XML parser context)
  193. *
  194. * Provides the public ID e.g. "-//SGMLSOURCE//DTD DEMO//EN"
  195. *
  196. * Returns a xmlChar *
  197. */
  198. const xmlChar *
  199. xmlSAX2GetPublicId(void *ctx ATTRIBUTE_UNUSED)
  200. {
  201. /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
  202. return(NULL);
  203. }
  204. /**
  205. * xmlSAX2GetSystemId:
  206. * @ctx: the user data (XML parser context)
  207. *
  208. * Provides the system ID, basically URL or filename e.g.
  209. * http://www.sgmlsource.com/dtds/memo.dtd
  210. *
  211. * Returns a xmlChar *
  212. */
  213. const xmlChar *
  214. xmlSAX2GetSystemId(void *ctx)
  215. {
  216. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  217. if ((ctx == NULL) || (ctxt->input == NULL)) return(NULL);
  218. return((const xmlChar *) ctxt->input->filename);
  219. }
  220. /**
  221. * xmlSAX2GetLineNumber:
  222. * @ctx: the user data (XML parser context)
  223. *
  224. * Provide the line number of the current parsing point.
  225. *
  226. * Returns an int
  227. */
  228. int
  229. xmlSAX2GetLineNumber(void *ctx)
  230. {
  231. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  232. if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
  233. return(ctxt->input->line);
  234. }
  235. /**
  236. * xmlSAX2GetColumnNumber:
  237. * @ctx: the user data (XML parser context)
  238. *
  239. * Provide the column number of the current parsing point.
  240. *
  241. * Returns an int
  242. */
  243. int
  244. xmlSAX2GetColumnNumber(void *ctx)
  245. {
  246. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  247. if ((ctx == NULL) || (ctxt->input == NULL)) return(0);
  248. return(ctxt->input->col);
  249. }
  250. /**
  251. * xmlSAX2IsStandalone:
  252. * @ctx: the user data (XML parser context)
  253. *
  254. * Is this document tagged standalone ?
  255. *
  256. * Returns 1 if true
  257. */
  258. int
  259. xmlSAX2IsStandalone(void *ctx)
  260. {
  261. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  262. if ((ctx == NULL) || (ctxt->myDoc == NULL)) return(0);
  263. return(ctxt->myDoc->standalone == 1);
  264. }
  265. /**
  266. * xmlSAX2HasInternalSubset:
  267. * @ctx: the user data (XML parser context)
  268. *
  269. * Does this document has an internal subset
  270. *
  271. * Returns 1 if true
  272. */
  273. int
  274. xmlSAX2HasInternalSubset(void *ctx)
  275. {
  276. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  277. if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
  278. return(ctxt->myDoc->intSubset != NULL);
  279. }
  280. /**
  281. * xmlSAX2HasExternalSubset:
  282. * @ctx: the user data (XML parser context)
  283. *
  284. * Does this document has an external subset
  285. *
  286. * Returns 1 if true
  287. */
  288. int
  289. xmlSAX2HasExternalSubset(void *ctx)
  290. {
  291. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  292. if ((ctxt == NULL) || (ctxt->myDoc == NULL)) return(0);
  293. return(ctxt->myDoc->extSubset != NULL);
  294. }
  295. /**
  296. * xmlSAX2InternalSubset:
  297. * @ctx: the user data (XML parser context)
  298. * @name: the root element name
  299. * @ExternalID: the external ID
  300. * @SystemID: the SYSTEM ID (e.g. filename or URL)
  301. *
  302. * Callback on internal subset declaration.
  303. */
  304. void
  305. xmlSAX2InternalSubset(void *ctx, const xmlChar *name,
  306. const xmlChar *ExternalID, const xmlChar *SystemID)
  307. {
  308. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  309. xmlDtdPtr dtd;
  310. if (ctx == NULL) return;
  311. if (ctxt->myDoc == NULL)
  312. return;
  313. dtd = xmlGetIntSubset(ctxt->myDoc);
  314. if (dtd != NULL) {
  315. if (ctxt->html)
  316. return;
  317. xmlUnlinkNode((xmlNodePtr) dtd);
  318. xmlFreeDtd(dtd);
  319. ctxt->myDoc->intSubset = NULL;
  320. }
  321. ctxt->myDoc->intSubset =
  322. xmlCreateIntSubset(ctxt->myDoc, name, ExternalID, SystemID);
  323. if (ctxt->myDoc->intSubset == NULL)
  324. xmlSAX2ErrMemory(ctxt, "xmlSAX2InternalSubset");
  325. }
  326. /**
  327. * xmlSAX2ExternalSubset:
  328. * @ctx: the user data (XML parser context)
  329. * @name: the root element name
  330. * @ExternalID: the external ID
  331. * @SystemID: the SYSTEM ID (e.g. filename or URL)
  332. *
  333. * Callback on external subset declaration.
  334. */
  335. void
  336. xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
  337. const xmlChar *ExternalID, const xmlChar *SystemID)
  338. {
  339. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  340. if (ctx == NULL) return;
  341. if (((ExternalID != NULL) || (SystemID != NULL)) &&
  342. (((ctxt->validate) || (ctxt->loadsubset != 0)) &&
  343. (ctxt->wellFormed && ctxt->myDoc))) {
  344. /*
  345. * Try to fetch and parse the external subset.
  346. */
  347. xmlParserInputPtr oldinput;
  348. int oldinputNr;
  349. int oldinputMax;
  350. xmlParserInputPtr *oldinputTab;
  351. xmlParserInputPtr input = NULL;
  352. const xmlChar *oldencoding;
  353. int oldprogressive;
  354. unsigned long consumed;
  355. size_t buffered;
  356. /*
  357. * Ask the Entity resolver to load the damn thing
  358. */
  359. if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
  360. input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID,
  361. SystemID);
  362. if (input == NULL) {
  363. return;
  364. }
  365. xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
  366. /*
  367. * make sure we won't destroy the main document context
  368. */
  369. oldinput = ctxt->input;
  370. oldinputNr = ctxt->inputNr;
  371. oldinputMax = ctxt->inputMax;
  372. oldinputTab = ctxt->inputTab;
  373. oldencoding = ctxt->encoding;
  374. oldprogressive = ctxt->progressive;
  375. ctxt->encoding = NULL;
  376. ctxt->progressive = 0;
  377. ctxt->inputTab = (xmlParserInputPtr *)
  378. xmlMalloc(5 * sizeof(xmlParserInputPtr));
  379. if (ctxt->inputTab == NULL) {
  380. xmlSAX2ErrMemory(ctxt, "xmlSAX2ExternalSubset");
  381. xmlFreeInputStream(input);
  382. ctxt->input = oldinput;
  383. ctxt->inputNr = oldinputNr;
  384. ctxt->inputMax = oldinputMax;
  385. ctxt->inputTab = oldinputTab;
  386. ctxt->encoding = oldencoding;
  387. ctxt->progressive = oldprogressive;
  388. return;
  389. }
  390. ctxt->inputNr = 0;
  391. ctxt->inputMax = 5;
  392. ctxt->input = NULL;
  393. xmlPushInput(ctxt, input);
  394. if (input->filename == NULL)
  395. input->filename = (char *) xmlCanonicPath(SystemID);
  396. input->line = 1;
  397. input->col = 1;
  398. input->base = ctxt->input->cur;
  399. input->cur = ctxt->input->cur;
  400. input->free = NULL;
  401. /*
  402. * let's parse that entity knowing it's an external subset.
  403. */
  404. xmlParseExternalSubset(ctxt, ExternalID, SystemID);
  405. /*
  406. * Free up the external entities
  407. */
  408. while (ctxt->inputNr > 1)
  409. xmlPopInput(ctxt);
  410. consumed = ctxt->input->consumed;
  411. buffered = ctxt->input->cur - ctxt->input->base;
  412. if (buffered > ULONG_MAX - consumed)
  413. consumed = ULONG_MAX;
  414. else
  415. consumed += buffered;
  416. if (consumed > ULONG_MAX - ctxt->sizeentities)
  417. ctxt->sizeentities = ULONG_MAX;
  418. else
  419. ctxt->sizeentities += consumed;
  420. xmlFreeInputStream(ctxt->input);
  421. xmlFree(ctxt->inputTab);
  422. /*
  423. * Restore the parsing context of the main entity
  424. */
  425. ctxt->input = oldinput;
  426. ctxt->inputNr = oldinputNr;
  427. ctxt->inputMax = oldinputMax;
  428. ctxt->inputTab = oldinputTab;
  429. if ((ctxt->encoding != NULL) &&
  430. ((ctxt->dict == NULL) ||
  431. (!xmlDictOwns(ctxt->dict, ctxt->encoding))))
  432. xmlFree((xmlChar *) ctxt->encoding);
  433. ctxt->encoding = oldencoding;
  434. ctxt->progressive = oldprogressive;
  435. /* ctxt->wellFormed = oldwellFormed; */
  436. }
  437. }
  438. /**
  439. * xmlSAX2ResolveEntity:
  440. * @ctx: the user data (XML parser context)
  441. * @publicId: The public ID of the entity
  442. * @systemId: The system ID of the entity
  443. *
  444. * The entity loader, to control the loading of external entities,
  445. * the application can either:
  446. * - override this xmlSAX2ResolveEntity() callback in the SAX block
  447. * - or better use the xmlSetExternalEntityLoader() function to
  448. * set up it's own entity resolution routine
  449. *
  450. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
  451. */
  452. xmlParserInputPtr
  453. xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
  454. {
  455. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  456. xmlParserInputPtr ret;
  457. xmlChar *URI;
  458. const char *base = NULL;
  459. if (ctx == NULL) return(NULL);
  460. if (ctxt->input != NULL)
  461. base = ctxt->input->filename;
  462. if (base == NULL)
  463. base = ctxt->directory;
  464. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  465. ret = xmlLoadExternalEntity((const char *) URI,
  466. (const char *) publicId, ctxt);
  467. if (URI != NULL)
  468. xmlFree(URI);
  469. return(ret);
  470. }
  471. /**
  472. * xmlSAX2GetEntity:
  473. * @ctx: the user data (XML parser context)
  474. * @name: The entity name
  475. *
  476. * Get an entity by name
  477. *
  478. * Returns the xmlEntityPtr if found.
  479. */
  480. xmlEntityPtr
  481. xmlSAX2GetEntity(void *ctx, const xmlChar *name)
  482. {
  483. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  484. xmlEntityPtr ret = NULL;
  485. if (ctx == NULL) return(NULL);
  486. if (ctxt->inSubset == 0) {
  487. ret = xmlGetPredefinedEntity(name);
  488. if (ret != NULL)
  489. return(ret);
  490. }
  491. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->standalone == 1)) {
  492. if (ctxt->inSubset == 2) {
  493. ctxt->myDoc->standalone = 0;
  494. ret = xmlGetDocEntity(ctxt->myDoc, name);
  495. ctxt->myDoc->standalone = 1;
  496. } else {
  497. ret = xmlGetDocEntity(ctxt->myDoc, name);
  498. if (ret == NULL) {
  499. ctxt->myDoc->standalone = 0;
  500. ret = xmlGetDocEntity(ctxt->myDoc, name);
  501. if (ret != NULL) {
  502. xmlFatalErrMsg(ctxt, XML_ERR_NOT_STANDALONE,
  503. "Entity(%s) document marked standalone but requires external subset\n",
  504. name, NULL);
  505. }
  506. ctxt->myDoc->standalone = 1;
  507. }
  508. }
  509. } else {
  510. ret = xmlGetDocEntity(ctxt->myDoc, name);
  511. }
  512. return(ret);
  513. }
  514. /**
  515. * xmlSAX2GetParameterEntity:
  516. * @ctx: the user data (XML parser context)
  517. * @name: The entity name
  518. *
  519. * Get a parameter entity by name
  520. *
  521. * Returns the xmlEntityPtr if found.
  522. */
  523. xmlEntityPtr
  524. xmlSAX2GetParameterEntity(void *ctx, const xmlChar *name)
  525. {
  526. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  527. xmlEntityPtr ret;
  528. if (ctx == NULL) return(NULL);
  529. ret = xmlGetParameterEntity(ctxt->myDoc, name);
  530. return(ret);
  531. }
  532. /**
  533. * xmlSAX2EntityDecl:
  534. * @ctx: the user data (XML parser context)
  535. * @name: the entity name
  536. * @type: the entity type
  537. * @publicId: The public ID of the entity
  538. * @systemId: The system ID of the entity
  539. * @content: the entity value (without processing).
  540. *
  541. * An entity definition has been parsed
  542. */
  543. void
  544. xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
  545. const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
  546. {
  547. xmlEntityPtr ent;
  548. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  549. if (ctx == NULL) return;
  550. if (ctxt->inSubset == 1) {
  551. ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
  552. systemId, content);
  553. if ((ent == NULL) && (ctxt->pedantic))
  554. xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
  555. "Entity(%s) already defined in the internal subset\n",
  556. name);
  557. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  558. xmlChar *URI;
  559. const char *base = NULL;
  560. if (ctxt->input != NULL)
  561. base = ctxt->input->filename;
  562. if (base == NULL)
  563. base = ctxt->directory;
  564. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  565. ent->URI = URI;
  566. }
  567. } else if (ctxt->inSubset == 2) {
  568. ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
  569. systemId, content);
  570. if ((ent == NULL) && (ctxt->pedantic) &&
  571. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  572. ctxt->sax->warning(ctxt->userData,
  573. "Entity(%s) already defined in the external subset\n", name);
  574. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  575. xmlChar *URI;
  576. const char *base = NULL;
  577. if (ctxt->input != NULL)
  578. base = ctxt->input->filename;
  579. if (base == NULL)
  580. base = ctxt->directory;
  581. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  582. ent->URI = URI;
  583. }
  584. } else {
  585. xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
  586. "SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
  587. name, NULL);
  588. }
  589. }
  590. /**
  591. * xmlSAX2AttributeDecl:
  592. * @ctx: the user data (XML parser context)
  593. * @elem: the name of the element
  594. * @fullname: the attribute name
  595. * @type: the attribute type
  596. * @def: the type of default value
  597. * @defaultValue: the attribute default value
  598. * @tree: the tree of enumerated value set
  599. *
  600. * An attribute definition has been parsed
  601. */
  602. void
  603. xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
  604. int type, int def, const xmlChar *defaultValue,
  605. xmlEnumerationPtr tree)
  606. {
  607. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  608. xmlAttributePtr attr;
  609. xmlChar *name = NULL, *prefix = NULL;
  610. /* Avoid unused variable warning if features are disabled. */
  611. (void) attr;
  612. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  613. return;
  614. if ((xmlStrEqual(fullname, BAD_CAST "xml:id")) &&
  615. (type != XML_ATTRIBUTE_ID)) {
  616. /*
  617. * Raise the error but keep the validity flag
  618. */
  619. int tmp = ctxt->valid;
  620. xmlErrValid(ctxt, XML_DTD_XMLID_TYPE,
  621. "xml:id : attribute type should be ID\n", NULL, NULL);
  622. ctxt->valid = tmp;
  623. }
  624. /* TODO: optimize name/prefix allocation */
  625. name = xmlSplitQName(ctxt, fullname, &prefix);
  626. ctxt->vctxt.valid = 1;
  627. if (ctxt->inSubset == 1)
  628. attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
  629. name, prefix, (xmlAttributeType) type,
  630. (xmlAttributeDefault) def, defaultValue, tree);
  631. else if (ctxt->inSubset == 2)
  632. attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, elem,
  633. name, prefix, (xmlAttributeType) type,
  634. (xmlAttributeDefault) def, defaultValue, tree);
  635. else {
  636. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  637. "SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
  638. name, NULL);
  639. xmlFree(name);
  640. xmlFreeEnumeration(tree);
  641. return;
  642. }
  643. #ifdef LIBXML_VALID_ENABLED
  644. if (ctxt->vctxt.valid == 0)
  645. ctxt->valid = 0;
  646. if ((attr != NULL) && (ctxt->validate) && (ctxt->wellFormed) &&
  647. (ctxt->myDoc->intSubset != NULL))
  648. ctxt->valid &= xmlValidateAttributeDecl(&ctxt->vctxt, ctxt->myDoc,
  649. attr);
  650. #endif /* LIBXML_VALID_ENABLED */
  651. if (prefix != NULL)
  652. xmlFree(prefix);
  653. if (name != NULL)
  654. xmlFree(name);
  655. }
  656. /**
  657. * xmlSAX2ElementDecl:
  658. * @ctx: the user data (XML parser context)
  659. * @name: the element name
  660. * @type: the element type
  661. * @content: the element value tree
  662. *
  663. * An element definition has been parsed
  664. */
  665. void
  666. xmlSAX2ElementDecl(void *ctx, const xmlChar * name, int type,
  667. xmlElementContentPtr content)
  668. {
  669. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  670. xmlElementPtr elem = NULL;
  671. /* Avoid unused variable warning if features are disabled. */
  672. (void) elem;
  673. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  674. return;
  675. if (ctxt->inSubset == 1)
  676. elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->intSubset,
  677. name, (xmlElementTypeVal) type, content);
  678. else if (ctxt->inSubset == 2)
  679. elem = xmlAddElementDecl(&ctxt->vctxt, ctxt->myDoc->extSubset,
  680. name, (xmlElementTypeVal) type, content);
  681. else {
  682. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  683. "SAX.xmlSAX2ElementDecl(%s) called while not in subset\n",
  684. name, NULL);
  685. return;
  686. }
  687. #ifdef LIBXML_VALID_ENABLED
  688. if (elem == NULL)
  689. ctxt->valid = 0;
  690. if (ctxt->validate && ctxt->wellFormed &&
  691. ctxt->myDoc && ctxt->myDoc->intSubset)
  692. ctxt->valid &=
  693. xmlValidateElementDecl(&ctxt->vctxt, ctxt->myDoc, elem);
  694. #endif /* LIBXML_VALID_ENABLED */
  695. }
  696. /**
  697. * xmlSAX2NotationDecl:
  698. * @ctx: the user data (XML parser context)
  699. * @name: The name of the notation
  700. * @publicId: The public ID of the entity
  701. * @systemId: The system ID of the entity
  702. *
  703. * What to do when a notation declaration has been parsed.
  704. */
  705. void
  706. xmlSAX2NotationDecl(void *ctx, const xmlChar *name,
  707. const xmlChar *publicId, const xmlChar *systemId)
  708. {
  709. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  710. xmlNotationPtr nota = NULL;
  711. /* Avoid unused variable warning if features are disabled. */
  712. (void) nota;
  713. if ((ctxt == NULL) || (ctxt->myDoc == NULL))
  714. return;
  715. if ((publicId == NULL) && (systemId == NULL)) {
  716. xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
  717. "SAX.xmlSAX2NotationDecl(%s) externalID or PublicID missing\n",
  718. name, NULL);
  719. return;
  720. } else if (ctxt->inSubset == 1)
  721. nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, name,
  722. publicId, systemId);
  723. else if (ctxt->inSubset == 2)
  724. nota = xmlAddNotationDecl(&ctxt->vctxt, ctxt->myDoc->extSubset, name,
  725. publicId, systemId);
  726. else {
  727. xmlFatalErrMsg(ctxt, XML_ERR_NOTATION_PROCESSING,
  728. "SAX.xmlSAX2NotationDecl(%s) called while not in subset\n",
  729. name, NULL);
  730. return;
  731. }
  732. #ifdef LIBXML_VALID_ENABLED
  733. if (nota == NULL) ctxt->valid = 0;
  734. if ((ctxt->validate) && (ctxt->wellFormed) &&
  735. (ctxt->myDoc->intSubset != NULL))
  736. ctxt->valid &= xmlValidateNotationDecl(&ctxt->vctxt, ctxt->myDoc,
  737. nota);
  738. #endif /* LIBXML_VALID_ENABLED */
  739. }
  740. /**
  741. * xmlSAX2UnparsedEntityDecl:
  742. * @ctx: the user data (XML parser context)
  743. * @name: The name of the entity
  744. * @publicId: The public ID of the entity
  745. * @systemId: The system ID of the entity
  746. * @notationName: the name of the notation
  747. *
  748. * What to do when an unparsed entity declaration is parsed
  749. */
  750. void
  751. xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
  752. const xmlChar *publicId, const xmlChar *systemId,
  753. const xmlChar *notationName)
  754. {
  755. xmlEntityPtr ent;
  756. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  757. if (ctx == NULL) return;
  758. if (ctxt->inSubset == 1) {
  759. ent = xmlAddDocEntity(ctxt->myDoc, name,
  760. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
  761. publicId, systemId, notationName);
  762. if ((ent == NULL) && (ctxt->pedantic) &&
  763. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  764. ctxt->sax->warning(ctxt->userData,
  765. "Entity(%s) already defined in the internal subset\n", name);
  766. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  767. xmlChar *URI;
  768. const char *base = NULL;
  769. if (ctxt->input != NULL)
  770. base = ctxt->input->filename;
  771. if (base == NULL)
  772. base = ctxt->directory;
  773. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  774. ent->URI = URI;
  775. }
  776. } else if (ctxt->inSubset == 2) {
  777. ent = xmlAddDtdEntity(ctxt->myDoc, name,
  778. XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
  779. publicId, systemId, notationName);
  780. if ((ent == NULL) && (ctxt->pedantic) &&
  781. (ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  782. ctxt->sax->warning(ctxt->userData,
  783. "Entity(%s) already defined in the external subset\n", name);
  784. if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
  785. xmlChar *URI;
  786. const char *base = NULL;
  787. if (ctxt->input != NULL)
  788. base = ctxt->input->filename;
  789. if (base == NULL)
  790. base = ctxt->directory;
  791. URI = xmlBuildURI(systemId, (const xmlChar *) base);
  792. ent->URI = URI;
  793. }
  794. } else {
  795. xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
  796. "SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
  797. name, NULL);
  798. }
  799. }
  800. /**
  801. * xmlSAX2SetDocumentLocator:
  802. * @ctx: the user data (XML parser context)
  803. * @loc: A SAX Locator
  804. *
  805. * Receive the document locator at startup, actually xmlDefaultSAXLocator
  806. * Everything is available on the context, so this is useless in our case.
  807. */
  808. void
  809. xmlSAX2SetDocumentLocator(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
  810. {
  811. }
  812. /**
  813. * xmlSAX2StartDocument:
  814. * @ctx: the user data (XML parser context)
  815. *
  816. * called when the document start being processed.
  817. */
  818. void
  819. xmlSAX2StartDocument(void *ctx)
  820. {
  821. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  822. xmlDocPtr doc;
  823. if (ctx == NULL) return;
  824. if (ctxt->html) {
  825. #ifdef LIBXML_HTML_ENABLED
  826. if (ctxt->myDoc == NULL)
  827. ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
  828. if (ctxt->myDoc == NULL) {
  829. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  830. return;
  831. }
  832. ctxt->myDoc->properties = XML_DOC_HTML;
  833. ctxt->myDoc->parseFlags = ctxt->options;
  834. #else
  835. xmlGenericError(xmlGenericErrorContext,
  836. "libxml2 built without HTML support\n");
  837. ctxt->errNo = XML_ERR_INTERNAL_ERROR;
  838. ctxt->instate = XML_PARSER_EOF;
  839. ctxt->disableSAX = 1;
  840. return;
  841. #endif
  842. } else {
  843. doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
  844. if (doc != NULL) {
  845. doc->properties = 0;
  846. if (ctxt->options & XML_PARSE_OLD10)
  847. doc->properties |= XML_DOC_OLD10;
  848. doc->parseFlags = ctxt->options;
  849. doc->standalone = ctxt->standalone;
  850. } else {
  851. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  852. return;
  853. }
  854. if ((ctxt->dictNames) && (doc != NULL)) {
  855. doc->dict = ctxt->dict;
  856. xmlDictReference(doc->dict);
  857. }
  858. }
  859. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
  860. (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
  861. ctxt->myDoc->URL = xmlPathToURI((const xmlChar *)ctxt->input->filename);
  862. if (ctxt->myDoc->URL == NULL)
  863. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartDocument");
  864. }
  865. }
  866. /**
  867. * xmlSAX2EndDocument:
  868. * @ctx: the user data (XML parser context)
  869. *
  870. * called when the document end has been detected.
  871. */
  872. void
  873. xmlSAX2EndDocument(void *ctx)
  874. {
  875. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  876. xmlDocPtr doc;
  877. if (ctx == NULL) return;
  878. #ifdef LIBXML_VALID_ENABLED
  879. if (ctxt->validate && ctxt->wellFormed &&
  880. ctxt->myDoc && ctxt->myDoc->intSubset)
  881. ctxt->valid &= xmlValidateDocumentFinal(&ctxt->vctxt, ctxt->myDoc);
  882. #endif /* LIBXML_VALID_ENABLED */
  883. doc = ctxt->myDoc;
  884. if ((doc != NULL) && (doc->encoding == NULL)) {
  885. const xmlChar *encoding = xmlGetActualEncoding(ctxt);
  886. if (encoding != NULL) {
  887. doc->encoding = xmlStrdup(encoding);
  888. if (doc->encoding == NULL)
  889. xmlSAX2ErrMemory(ctxt, "xmlSAX2EndDocument");
  890. }
  891. }
  892. }
  893. #if defined(LIBXML_SAX1_ENABLED) || defined(LIBXML_HTML_ENABLED) || defined(LIBXML_WRITER_ENABLED) || defined(LIBXML_LEGACY_ENABLED)
  894. /**
  895. * xmlNsErrMsg:
  896. * @ctxt: an XML parser context
  897. * @error: the error number
  898. * @msg: the error message
  899. * @str1: an error string
  900. * @str2: an error string
  901. *
  902. * Handle a namespace error
  903. */
  904. static void LIBXML_ATTR_FORMAT(3,0)
  905. xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  906. const char *msg, const xmlChar *str1, const xmlChar *str2)
  907. {
  908. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  909. (ctxt->instate == XML_PARSER_EOF))
  910. return;
  911. if (ctxt != NULL)
  912. ctxt->errNo = error;
  913. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
  914. XML_ERR_ERROR, NULL, 0,
  915. (const char *) str1, (const char *) str2,
  916. NULL, 0, 0, msg, str1, str2);
  917. }
  918. /**
  919. * xmlSAX2AttributeInternal:
  920. * @ctx: the user data (XML parser context)
  921. * @fullname: The attribute name, including namespace prefix
  922. * @value: The attribute value
  923. * @prefix: the prefix on the element node
  924. *
  925. * Handle an attribute that has been read by the parser.
  926. * The default handling is to convert the attribute into an
  927. * DOM subtree and past it in a new xmlAttr element added to
  928. * the element.
  929. */
  930. static void
  931. xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
  932. const xmlChar *value, const xmlChar *prefix ATTRIBUTE_UNUSED)
  933. {
  934. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  935. xmlAttrPtr ret;
  936. xmlChar *name;
  937. xmlChar *ns;
  938. xmlChar *nval;
  939. xmlNsPtr namespace;
  940. if (ctxt->html) {
  941. name = xmlStrdup(fullname);
  942. ns = NULL;
  943. namespace = NULL;
  944. } else {
  945. /*
  946. * Split the full name into a namespace prefix and the tag name
  947. */
  948. name = xmlSplitQName(ctxt, fullname, &ns);
  949. if ((name != NULL) && (name[0] == 0)) {
  950. if (xmlStrEqual(ns, BAD_CAST "xmlns")) {
  951. xmlNsErrMsg(ctxt, XML_ERR_NS_DECL_ERROR,
  952. "invalid namespace declaration '%s'\n",
  953. fullname, NULL);
  954. } else {
  955. xmlNsWarnMsg(ctxt, XML_WAR_NS_COLUMN,
  956. "Avoid attribute ending with ':' like '%s'\n",
  957. fullname, NULL);
  958. }
  959. if (ns != NULL)
  960. xmlFree(ns);
  961. ns = NULL;
  962. xmlFree(name);
  963. name = xmlStrdup(fullname);
  964. }
  965. }
  966. if (name == NULL) {
  967. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  968. if (ns != NULL)
  969. xmlFree(ns);
  970. return;
  971. }
  972. #ifdef LIBXML_HTML_ENABLED
  973. if ((ctxt->html) &&
  974. (value == NULL) && (htmlIsBooleanAttr(fullname))) {
  975. nval = xmlStrdup(fullname);
  976. value = (const xmlChar *) nval;
  977. } else
  978. #endif
  979. {
  980. #ifdef LIBXML_VALID_ENABLED
  981. /*
  982. * Do the last stage of the attribute normalization
  983. * Needed for HTML too:
  984. * http://www.w3.org/TR/html4/types.html#h-6.2
  985. */
  986. ctxt->vctxt.valid = 1;
  987. nval = xmlValidCtxtNormalizeAttributeValue(&ctxt->vctxt,
  988. ctxt->myDoc, ctxt->node,
  989. fullname, value);
  990. if (ctxt->vctxt.valid != 1) {
  991. ctxt->valid = 0;
  992. }
  993. if (nval != NULL)
  994. value = nval;
  995. #else
  996. nval = NULL;
  997. #endif /* LIBXML_VALID_ENABLED */
  998. }
  999. /*
  1000. * Check whether it's a namespace definition
  1001. */
  1002. if ((!ctxt->html) && (ns == NULL) &&
  1003. (name[0] == 'x') && (name[1] == 'm') && (name[2] == 'l') &&
  1004. (name[3] == 'n') && (name[4] == 's') && (name[5] == 0)) {
  1005. xmlNsPtr nsret;
  1006. xmlChar *val;
  1007. /* Avoid unused variable warning if features are disabled. */
  1008. (void) nsret;
  1009. if (!ctxt->replaceEntities) {
  1010. ctxt->depth++;
  1011. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1012. 0,0,0);
  1013. ctxt->depth--;
  1014. if (val == NULL) {
  1015. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1016. if (name != NULL)
  1017. xmlFree(name);
  1018. if (nval != NULL)
  1019. xmlFree(nval);
  1020. return;
  1021. }
  1022. } else {
  1023. val = (xmlChar *) value;
  1024. }
  1025. if (val[0] != 0) {
  1026. xmlURIPtr uri;
  1027. uri = xmlParseURI((const char *)val);
  1028. if (uri == NULL) {
  1029. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1030. ctxt->sax->warning(ctxt->userData,
  1031. "xmlns: %s not a valid URI\n", val);
  1032. } else {
  1033. if (uri->scheme == NULL) {
  1034. if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
  1035. ctxt->sax->warning(ctxt->userData,
  1036. "xmlns: URI %s is not absolute\n", val);
  1037. }
  1038. xmlFreeURI(uri);
  1039. }
  1040. }
  1041. /* a default namespace definition */
  1042. nsret = xmlNewNs(ctxt->node, val, NULL);
  1043. #ifdef LIBXML_VALID_ENABLED
  1044. /*
  1045. * Validate also for namespace decls, they are attributes from
  1046. * an XML-1.0 perspective
  1047. */
  1048. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1049. ctxt->myDoc && ctxt->myDoc->intSubset)
  1050. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1051. ctxt->node, prefix, nsret, val);
  1052. #endif /* LIBXML_VALID_ENABLED */
  1053. if (name != NULL)
  1054. xmlFree(name);
  1055. if (nval != NULL)
  1056. xmlFree(nval);
  1057. if (val != value)
  1058. xmlFree(val);
  1059. return;
  1060. }
  1061. if ((!ctxt->html) &&
  1062. (ns != NULL) && (ns[0] == 'x') && (ns[1] == 'm') && (ns[2] == 'l') &&
  1063. (ns[3] == 'n') && (ns[4] == 's') && (ns[5] == 0)) {
  1064. xmlNsPtr nsret;
  1065. xmlChar *val;
  1066. /* Avoid unused variable warning if features are disabled. */
  1067. (void) nsret;
  1068. if (!ctxt->replaceEntities) {
  1069. ctxt->depth++;
  1070. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1071. 0,0,0);
  1072. ctxt->depth--;
  1073. if (val == NULL) {
  1074. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1075. xmlFree(ns);
  1076. if (name != NULL)
  1077. xmlFree(name);
  1078. if (nval != NULL)
  1079. xmlFree(nval);
  1080. return;
  1081. }
  1082. } else {
  1083. val = (xmlChar *) value;
  1084. }
  1085. if (val[0] == 0) {
  1086. xmlNsErrMsg(ctxt, XML_NS_ERR_EMPTY,
  1087. "Empty namespace name for prefix %s\n", name, NULL);
  1088. }
  1089. if ((ctxt->pedantic != 0) && (val[0] != 0)) {
  1090. xmlURIPtr uri;
  1091. uri = xmlParseURI((const char *)val);
  1092. if (uri == NULL) {
  1093. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
  1094. "xmlns:%s: %s not a valid URI\n", name, value);
  1095. } else {
  1096. if (uri->scheme == NULL) {
  1097. xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
  1098. "xmlns:%s: URI %s is not absolute\n", name, value);
  1099. }
  1100. xmlFreeURI(uri);
  1101. }
  1102. }
  1103. /* a standard namespace definition */
  1104. nsret = xmlNewNs(ctxt->node, val, name);
  1105. xmlFree(ns);
  1106. #ifdef LIBXML_VALID_ENABLED
  1107. /*
  1108. * Validate also for namespace decls, they are attributes from
  1109. * an XML-1.0 perspective
  1110. */
  1111. if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
  1112. ctxt->myDoc && ctxt->myDoc->intSubset)
  1113. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  1114. ctxt->node, prefix, nsret, value);
  1115. #endif /* LIBXML_VALID_ENABLED */
  1116. if (name != NULL)
  1117. xmlFree(name);
  1118. if (nval != NULL)
  1119. xmlFree(nval);
  1120. if (val != value)
  1121. xmlFree(val);
  1122. return;
  1123. }
  1124. if (ns != NULL) {
  1125. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, ns);
  1126. if (namespace == NULL) {
  1127. xmlNsErrMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1128. "Namespace prefix %s of attribute %s is not defined\n",
  1129. ns, name);
  1130. } else {
  1131. xmlAttrPtr prop;
  1132. prop = ctxt->node->properties;
  1133. while (prop != NULL) {
  1134. if (prop->ns != NULL) {
  1135. if ((xmlStrEqual(name, prop->name)) &&
  1136. ((namespace == prop->ns) ||
  1137. (xmlStrEqual(namespace->href, prop->ns->href)))) {
  1138. xmlNsErrMsg(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
  1139. "Attribute %s in %s redefined\n",
  1140. name, namespace->href);
  1141. ctxt->wellFormed = 0;
  1142. if (ctxt->recovery == 0) ctxt->disableSAX = 1;
  1143. if (name != NULL)
  1144. xmlFree(name);
  1145. goto error;
  1146. }
  1147. }
  1148. prop = prop->next;
  1149. }
  1150. }
  1151. } else {
  1152. namespace = NULL;
  1153. }
  1154. /* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
  1155. ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
  1156. if (ret == NULL)
  1157. goto error;
  1158. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1159. xmlNodePtr tmp;
  1160. ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
  1161. tmp = ret->children;
  1162. while (tmp != NULL) {
  1163. tmp->parent = (xmlNodePtr) ret;
  1164. if (tmp->next == NULL)
  1165. ret->last = tmp;
  1166. tmp = tmp->next;
  1167. }
  1168. } else if (value != NULL) {
  1169. ret->children = xmlNewDocText(ctxt->myDoc, value);
  1170. ret->last = ret->children;
  1171. if (ret->children != NULL)
  1172. ret->children->parent = (xmlNodePtr) ret;
  1173. }
  1174. #ifdef LIBXML_VALID_ENABLED
  1175. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1176. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1177. /*
  1178. * If we don't substitute entities, the validation should be
  1179. * done on a value with replaced entities anyway.
  1180. */
  1181. if (!ctxt->replaceEntities) {
  1182. xmlChar *val;
  1183. ctxt->depth++;
  1184. val = xmlStringDecodeEntities(ctxt, value, XML_SUBSTITUTE_REF,
  1185. 0,0,0);
  1186. ctxt->depth--;
  1187. if (val == NULL)
  1188. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1189. ctxt->myDoc, ctxt->node, ret, value);
  1190. else {
  1191. xmlChar *nvalnorm;
  1192. /*
  1193. * Do the last stage of the attribute normalization
  1194. * It need to be done twice ... it's an extra burden related
  1195. * to the ability to keep xmlSAX2References in attributes
  1196. */
  1197. nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
  1198. ctxt->node, fullname, val);
  1199. if (nvalnorm != NULL) {
  1200. xmlFree(val);
  1201. val = nvalnorm;
  1202. }
  1203. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1204. ctxt->myDoc, ctxt->node, ret, val);
  1205. xmlFree(val);
  1206. }
  1207. } else {
  1208. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt, ctxt->myDoc,
  1209. ctxt->node, ret, value);
  1210. }
  1211. } else
  1212. #endif /* LIBXML_VALID_ENABLED */
  1213. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1214. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1215. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) &&
  1216. /* Don't create IDs containing entity references */
  1217. (ret->children != NULL) &&
  1218. (ret->children->type == XML_TEXT_NODE) &&
  1219. (ret->children->next == NULL)) {
  1220. xmlChar *content = ret->children->content;
  1221. /*
  1222. * when validating, the ID registration is done at the attribute
  1223. * validation level. Otherwise we have to do specific handling here.
  1224. */
  1225. if (xmlStrEqual(fullname, BAD_CAST "xml:id")) {
  1226. /*
  1227. * Add the xml:id value
  1228. *
  1229. * Open issue: normalization of the value.
  1230. */
  1231. if (xmlValidateNCName(content, 1) != 0) {
  1232. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1233. "xml:id : attribute value %s is not an NCName\n",
  1234. (const char *) content, NULL);
  1235. }
  1236. xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1237. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
  1238. xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1239. else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
  1240. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1241. }
  1242. error:
  1243. if (nval != NULL)
  1244. xmlFree(nval);
  1245. if (ns != NULL)
  1246. xmlFree(ns);
  1247. }
  1248. /*
  1249. * xmlCheckDefaultedAttributes:
  1250. *
  1251. * Check defaulted attributes from the DTD
  1252. */
  1253. static void
  1254. xmlCheckDefaultedAttributes(xmlParserCtxtPtr ctxt, const xmlChar *name,
  1255. const xmlChar *prefix, const xmlChar **atts) {
  1256. xmlElementPtr elemDecl;
  1257. const xmlChar *att;
  1258. int internal = 1;
  1259. int i;
  1260. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->intSubset, name, prefix);
  1261. if (elemDecl == NULL) {
  1262. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset, name, prefix);
  1263. internal = 0;
  1264. }
  1265. process_external_subset:
  1266. if (elemDecl != NULL) {
  1267. xmlAttributePtr attr = elemDecl->attributes;
  1268. /*
  1269. * Check against defaulted attributes from the external subset
  1270. * if the document is stamped as standalone
  1271. */
  1272. if ((ctxt->myDoc->standalone == 1) &&
  1273. (ctxt->myDoc->extSubset != NULL) &&
  1274. (ctxt->validate)) {
  1275. while (attr != NULL) {
  1276. if ((attr->defaultValue != NULL) &&
  1277. (xmlGetDtdQAttrDesc(ctxt->myDoc->extSubset,
  1278. attr->elem, attr->name,
  1279. attr->prefix) == attr) &&
  1280. (xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1281. attr->elem, attr->name,
  1282. attr->prefix) == NULL)) {
  1283. xmlChar *fulln;
  1284. if (attr->prefix != NULL) {
  1285. fulln = xmlStrdup(attr->prefix);
  1286. fulln = xmlStrcat(fulln, BAD_CAST ":");
  1287. fulln = xmlStrcat(fulln, attr->name);
  1288. } else {
  1289. fulln = xmlStrdup(attr->name);
  1290. }
  1291. if (fulln == NULL) {
  1292. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1293. break;
  1294. }
  1295. /*
  1296. * Check that the attribute is not declared in the
  1297. * serialization
  1298. */
  1299. att = NULL;
  1300. if (atts != NULL) {
  1301. i = 0;
  1302. att = atts[i];
  1303. while (att != NULL) {
  1304. if (xmlStrEqual(att, fulln))
  1305. break;
  1306. i += 2;
  1307. att = atts[i];
  1308. }
  1309. }
  1310. if (att == NULL) {
  1311. xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
  1312. "standalone: attribute %s on %s defaulted from external subset\n",
  1313. (const char *)fulln,
  1314. (const char *)attr->elem);
  1315. }
  1316. xmlFree(fulln);
  1317. }
  1318. attr = attr->nexth;
  1319. }
  1320. }
  1321. /*
  1322. * Actually insert defaulted values when needed
  1323. */
  1324. attr = elemDecl->attributes;
  1325. while (attr != NULL) {
  1326. /*
  1327. * Make sure that attributes redefinition occurring in the
  1328. * internal subset are not overridden by definitions in the
  1329. * external subset.
  1330. */
  1331. if (attr->defaultValue != NULL) {
  1332. /*
  1333. * the element should be instantiated in the tree if:
  1334. * - this is a namespace prefix
  1335. * - the user required for completion in the tree
  1336. * like XSLT
  1337. * - there isn't already an attribute definition
  1338. * in the internal subset overriding it.
  1339. */
  1340. if (((attr->prefix != NULL) &&
  1341. (xmlStrEqual(attr->prefix, BAD_CAST "xmlns"))) ||
  1342. ((attr->prefix == NULL) &&
  1343. (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) ||
  1344. (ctxt->loadsubset & XML_COMPLETE_ATTRS)) {
  1345. xmlAttributePtr tst;
  1346. tst = xmlGetDtdQAttrDesc(ctxt->myDoc->intSubset,
  1347. attr->elem, attr->name,
  1348. attr->prefix);
  1349. if ((tst == attr) || (tst == NULL)) {
  1350. xmlChar fn[50];
  1351. xmlChar *fulln;
  1352. fulln = xmlBuildQName(attr->name, attr->prefix, fn, 50);
  1353. if (fulln == NULL) {
  1354. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1355. return;
  1356. }
  1357. /*
  1358. * Check that the attribute is not declared in the
  1359. * serialization
  1360. */
  1361. att = NULL;
  1362. if (atts != NULL) {
  1363. i = 0;
  1364. att = atts[i];
  1365. while (att != NULL) {
  1366. if (xmlStrEqual(att, fulln))
  1367. break;
  1368. i += 2;
  1369. att = atts[i];
  1370. }
  1371. }
  1372. if (att == NULL) {
  1373. xmlSAX2AttributeInternal(ctxt, fulln,
  1374. attr->defaultValue, prefix);
  1375. }
  1376. if ((fulln != fn) && (fulln != attr->name))
  1377. xmlFree(fulln);
  1378. }
  1379. }
  1380. }
  1381. attr = attr->nexth;
  1382. }
  1383. if (internal == 1) {
  1384. elemDecl = xmlGetDtdQElementDesc(ctxt->myDoc->extSubset,
  1385. name, prefix);
  1386. internal = 0;
  1387. goto process_external_subset;
  1388. }
  1389. }
  1390. }
  1391. /**
  1392. * xmlSAX2StartElement:
  1393. * @ctx: the user data (XML parser context)
  1394. * @fullname: The element name, including namespace prefix
  1395. * @atts: An array of name/value attributes pairs, NULL terminated
  1396. *
  1397. * called when an opening tag has been processed.
  1398. */
  1399. void
  1400. xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
  1401. {
  1402. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1403. xmlNodePtr ret;
  1404. xmlNodePtr parent;
  1405. xmlNsPtr ns;
  1406. xmlChar *name;
  1407. xmlChar *prefix;
  1408. const xmlChar *att;
  1409. const xmlChar *value;
  1410. int i;
  1411. if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
  1412. /*
  1413. * First check on validity:
  1414. */
  1415. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  1416. ((ctxt->myDoc->intSubset == NULL) ||
  1417. ((ctxt->myDoc->intSubset->notations == NULL) &&
  1418. (ctxt->myDoc->intSubset->elements == NULL) &&
  1419. (ctxt->myDoc->intSubset->attributes == NULL) &&
  1420. (ctxt->myDoc->intSubset->entities == NULL)))) {
  1421. xmlErrValid(ctxt, XML_ERR_NO_DTD,
  1422. "Validation failed: no DTD found !", NULL, NULL);
  1423. ctxt->validate = 0;
  1424. }
  1425. if (ctxt->html) {
  1426. prefix = NULL;
  1427. name = xmlStrdup(fullname);
  1428. } else {
  1429. /*
  1430. * Split the full name into a namespace prefix and the tag name
  1431. */
  1432. name = xmlSplitQName(ctxt, fullname, &prefix);
  1433. }
  1434. /*
  1435. * Note : the namespace resolution is deferred until the end of the
  1436. * attributes parsing, since local namespace can be defined as
  1437. * an attribute at this level.
  1438. */
  1439. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
  1440. if (ret == NULL) {
  1441. if (prefix != NULL)
  1442. xmlFree(prefix);
  1443. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
  1444. return;
  1445. }
  1446. ctxt->nodemem = -1;
  1447. if (ctxt->linenumbers) {
  1448. if (ctxt->input != NULL) {
  1449. if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
  1450. ret->line = ctxt->input->line;
  1451. else
  1452. ret->line = USHRT_MAX;
  1453. }
  1454. }
  1455. /* Initialize parent before pushing node */
  1456. parent = ctxt->node;
  1457. if (parent == NULL)
  1458. parent = (xmlNodePtr) ctxt->myDoc;
  1459. /*
  1460. * We are parsing a new node.
  1461. */
  1462. if (nodePush(ctxt, ret) < 0) {
  1463. xmlUnlinkNode(ret);
  1464. xmlFreeNode(ret);
  1465. if (prefix != NULL)
  1466. xmlFree(prefix);
  1467. return;
  1468. }
  1469. /*
  1470. * Link the child element
  1471. */
  1472. xmlAddChild(parent, ret);
  1473. if (!ctxt->html) {
  1474. /*
  1475. * Insert all the defaulted attributes from the DTD especially
  1476. * namespaces
  1477. */
  1478. if ((ctxt->myDoc->intSubset != NULL) ||
  1479. (ctxt->myDoc->extSubset != NULL)) {
  1480. xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
  1481. }
  1482. /*
  1483. * process all the attributes whose name start with "xmlns"
  1484. */
  1485. if (atts != NULL) {
  1486. i = 0;
  1487. att = atts[i++];
  1488. value = atts[i++];
  1489. while ((att != NULL) && (value != NULL)) {
  1490. if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
  1491. (att[3] == 'n') && (att[4] == 's'))
  1492. xmlSAX2AttributeInternal(ctxt, att, value, prefix);
  1493. att = atts[i++];
  1494. value = atts[i++];
  1495. }
  1496. }
  1497. /*
  1498. * Search the namespace, note that since the attributes have been
  1499. * processed, the local namespaces are available.
  1500. */
  1501. ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  1502. if ((ns == NULL) && (parent != NULL))
  1503. ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
  1504. if ((prefix != NULL) && (ns == NULL)) {
  1505. ns = xmlNewNs(ret, NULL, prefix);
  1506. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  1507. "Namespace prefix %s is not defined\n",
  1508. prefix, NULL);
  1509. }
  1510. /*
  1511. * set the namespace node, making sure that if the default namespace
  1512. * is unbound on a parent we simply keep it NULL
  1513. */
  1514. if ((ns != NULL) && (ns->href != NULL) &&
  1515. ((ns->href[0] != 0) || (ns->prefix != NULL)))
  1516. xmlSetNs(ret, ns);
  1517. }
  1518. /*
  1519. * process all the other attributes
  1520. */
  1521. if (atts != NULL) {
  1522. i = 0;
  1523. att = atts[i++];
  1524. value = atts[i++];
  1525. if (ctxt->html) {
  1526. while (att != NULL) {
  1527. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1528. att = atts[i++];
  1529. value = atts[i++];
  1530. }
  1531. } else {
  1532. while ((att != NULL) && (value != NULL)) {
  1533. if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
  1534. (att[3] != 'n') || (att[4] != 's'))
  1535. xmlSAX2AttributeInternal(ctxt, att, value, NULL);
  1536. /*
  1537. * Next ones
  1538. */
  1539. att = atts[i++];
  1540. value = atts[i++];
  1541. }
  1542. }
  1543. }
  1544. #ifdef LIBXML_VALID_ENABLED
  1545. /*
  1546. * If it's the Document root, finish the DTD validation and
  1547. * check the document root element for validity
  1548. */
  1549. if ((ctxt->validate) &&
  1550. ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) {
  1551. int chk;
  1552. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  1553. if (chk <= 0)
  1554. ctxt->valid = 0;
  1555. if (chk < 0)
  1556. ctxt->wellFormed = 0;
  1557. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  1558. ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED;
  1559. }
  1560. #endif /* LIBXML_VALID_ENABLED */
  1561. if (prefix != NULL)
  1562. xmlFree(prefix);
  1563. }
  1564. /**
  1565. * xmlSAX2EndElement:
  1566. * @ctx: the user data (XML parser context)
  1567. * @name: The element name
  1568. *
  1569. * called when the end of an element has been detected.
  1570. */
  1571. void
  1572. xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED)
  1573. {
  1574. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1575. if (ctx == NULL) return;
  1576. ctxt->nodemem = -1;
  1577. #ifdef LIBXML_VALID_ENABLED
  1578. if (ctxt->validate && ctxt->wellFormed &&
  1579. ctxt->myDoc && ctxt->myDoc->intSubset)
  1580. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
  1581. ctxt->node);
  1582. #endif /* LIBXML_VALID_ENABLED */
  1583. /*
  1584. * end of parsing of this node.
  1585. */
  1586. nodePop(ctxt);
  1587. }
  1588. #endif /* LIBXML_SAX1_ENABLED || LIBXML_HTML_ENABLED || LIBXML_LEGACY_ENABLED */
  1589. /*
  1590. * xmlSAX2TextNode:
  1591. * @ctxt: the parser context
  1592. * @str: the input string
  1593. * @len: the string length
  1594. *
  1595. * Callback for a text node
  1596. *
  1597. * Returns the newly allocated string or NULL if not needed or error
  1598. */
  1599. static xmlNodePtr
  1600. xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
  1601. xmlNodePtr ret;
  1602. const xmlChar *intern = NULL;
  1603. /*
  1604. * Allocate
  1605. */
  1606. if (ctxt->freeElems != NULL) {
  1607. ret = ctxt->freeElems;
  1608. ctxt->freeElems = ret->next;
  1609. ctxt->freeElemsNr--;
  1610. } else {
  1611. ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
  1612. }
  1613. if (ret == NULL) {
  1614. xmlErrMemory(ctxt, "xmlSAX2Characters");
  1615. return(NULL);
  1616. }
  1617. memset(ret, 0, sizeof(xmlNode));
  1618. /*
  1619. * intern the formatting blanks found between tags, or the
  1620. * very short strings
  1621. */
  1622. if (ctxt->dictNames) {
  1623. xmlChar cur = str[len];
  1624. if ((len < (int) (2 * sizeof(void *))) &&
  1625. (ctxt->options & XML_PARSE_COMPACT)) {
  1626. /* store the string in the node overriding properties and nsDef */
  1627. xmlChar *tmp = (xmlChar *) &(ret->properties);
  1628. memcpy(tmp, str, len);
  1629. tmp[len] = 0;
  1630. intern = tmp;
  1631. } else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
  1632. ((cur == '<') && (str[len + 1] != '!')))) {
  1633. intern = xmlDictLookup(ctxt->dict, str, len);
  1634. } else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
  1635. (str[len + 1] != '!')) {
  1636. int i;
  1637. for (i = 1;i < len;i++) {
  1638. if (!IS_BLANK_CH(str[i])) goto skip;
  1639. }
  1640. intern = xmlDictLookup(ctxt->dict, str, len);
  1641. }
  1642. }
  1643. skip:
  1644. ret->type = XML_TEXT_NODE;
  1645. ret->name = xmlStringText;
  1646. if (intern == NULL) {
  1647. ret->content = xmlStrndup(str, len);
  1648. if (ret->content == NULL) {
  1649. xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
  1650. xmlFree(ret);
  1651. return(NULL);
  1652. }
  1653. } else
  1654. ret->content = (xmlChar *) intern;
  1655. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1656. xmlRegisterNodeDefaultValue(ret);
  1657. return(ret);
  1658. }
  1659. #ifdef LIBXML_VALID_ENABLED
  1660. /*
  1661. * xmlSAX2DecodeAttrEntities:
  1662. * @ctxt: the parser context
  1663. * @str: the input string
  1664. * @len: the string length
  1665. *
  1666. * Remove the entities from an attribute value
  1667. *
  1668. * Returns the newly allocated string or NULL if not needed or error
  1669. */
  1670. static xmlChar *
  1671. xmlSAX2DecodeAttrEntities(xmlParserCtxtPtr ctxt, const xmlChar *str,
  1672. const xmlChar *end) {
  1673. const xmlChar *in;
  1674. xmlChar *ret;
  1675. in = str;
  1676. while (in < end)
  1677. if (*in++ == '&')
  1678. goto decode;
  1679. return(NULL);
  1680. decode:
  1681. ctxt->depth++;
  1682. ret = xmlStringLenDecodeEntities(ctxt, str, end - str,
  1683. XML_SUBSTITUTE_REF, 0,0,0);
  1684. ctxt->depth--;
  1685. return(ret);
  1686. }
  1687. #endif /* LIBXML_VALID_ENABLED */
  1688. /**
  1689. * xmlSAX2AttributeNs:
  1690. * @ctx: the user data (XML parser context)
  1691. * @localname: the local name of the attribute
  1692. * @prefix: the attribute namespace prefix if available
  1693. * @URI: the attribute namespace name if available
  1694. * @value: Start of the attribute value
  1695. * @valueend: end of the attribute value
  1696. *
  1697. * Handle an attribute that has been read by the parser.
  1698. * The default handling is to convert the attribute into an
  1699. * DOM subtree and past it in a new xmlAttr element added to
  1700. * the element.
  1701. *
  1702. * Returns the new attribute or NULL in case of error.
  1703. */
  1704. static xmlAttrPtr
  1705. xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
  1706. const xmlChar * localname,
  1707. const xmlChar * prefix,
  1708. const xmlChar * value,
  1709. const xmlChar * valueend)
  1710. {
  1711. xmlAttrPtr ret;
  1712. xmlNsPtr namespace = NULL;
  1713. xmlChar *dup = NULL;
  1714. /*
  1715. * Note: if prefix == NULL, the attribute is not in the default namespace
  1716. */
  1717. if (prefix != NULL) {
  1718. namespace = xmlParserNsLookupSax(ctxt, prefix);
  1719. if ((namespace == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
  1720. namespace = xmlSearchNs(ctxt->myDoc, ctxt->node, prefix);
  1721. }
  1722. }
  1723. /*
  1724. * allocate the node
  1725. */
  1726. if (ctxt->freeAttrs != NULL) {
  1727. ret = ctxt->freeAttrs;
  1728. ctxt->freeAttrs = ret->next;
  1729. ctxt->freeAttrsNr--;
  1730. } else {
  1731. ret = xmlMalloc(sizeof(*ret));
  1732. if (ret == NULL) {
  1733. xmlSAX2ErrMemory(ctxt, NULL);
  1734. return(NULL);
  1735. }
  1736. }
  1737. memset(ret, 0, sizeof(xmlAttr));
  1738. ret->type = XML_ATTRIBUTE_NODE;
  1739. /*
  1740. * xmlParseBalancedChunkMemoryRecover had a bug that could result in
  1741. * a mismatch between ctxt->node->doc and ctxt->myDoc. We use
  1742. * ctxt->node->doc here, but we should somehow make sure that the
  1743. * document pointers match.
  1744. */
  1745. /* assert(ctxt->node->doc == ctxt->myDoc); */
  1746. ret->parent = ctxt->node;
  1747. ret->doc = ctxt->node->doc;
  1748. ret->ns = namespace;
  1749. if (ctxt->dictNames)
  1750. ret->name = localname;
  1751. else
  1752. ret->name = xmlStrdup(localname);
  1753. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1754. xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
  1755. if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
  1756. xmlNodePtr tmp;
  1757. /*
  1758. * We know that if there is an entity reference, then
  1759. * the string has been dup'ed and terminates with 0
  1760. * otherwise with ' or "
  1761. */
  1762. if (*valueend != 0) {
  1763. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1764. ret->children = tmp;
  1765. ret->last = tmp;
  1766. if (tmp != NULL) {
  1767. tmp->doc = ret->doc;
  1768. tmp->parent = (xmlNodePtr) ret;
  1769. }
  1770. } else {
  1771. ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
  1772. valueend - value);
  1773. tmp = ret->children;
  1774. while (tmp != NULL) {
  1775. tmp->doc = ret->doc;
  1776. tmp->parent = (xmlNodePtr) ret;
  1777. if (tmp->next == NULL)
  1778. ret->last = tmp;
  1779. tmp = tmp->next;
  1780. }
  1781. }
  1782. } else if (value != NULL) {
  1783. xmlNodePtr tmp;
  1784. tmp = xmlSAX2TextNode(ctxt, value, valueend - value);
  1785. ret->children = tmp;
  1786. ret->last = tmp;
  1787. if (tmp != NULL) {
  1788. tmp->doc = ret->doc;
  1789. tmp->parent = (xmlNodePtr) ret;
  1790. }
  1791. }
  1792. #ifdef LIBXML_VALID_ENABLED
  1793. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  1794. ctxt->myDoc && ctxt->myDoc->intSubset) {
  1795. /*
  1796. * If we don't substitute entities, the validation should be
  1797. * done on a value with replaced entities anyway.
  1798. */
  1799. if (!ctxt->replaceEntities) {
  1800. dup = xmlSAX2DecodeAttrEntities(ctxt, value, valueend);
  1801. if (dup == NULL) {
  1802. if (*valueend == 0) {
  1803. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1804. ctxt->myDoc, ctxt->node, ret, value);
  1805. } else {
  1806. /*
  1807. * That should already be normalized.
  1808. * cheaper to finally allocate here than duplicate
  1809. * entry points in the full validation code
  1810. */
  1811. dup = xmlStrndup(value, valueend - value);
  1812. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1813. ctxt->myDoc, ctxt->node, ret, dup);
  1814. }
  1815. } else {
  1816. /*
  1817. * dup now contains a string of the flattened attribute
  1818. * content with entities substituted. Check if we need to
  1819. * apply an extra layer of normalization.
  1820. * It need to be done twice ... it's an extra burden related
  1821. * to the ability to keep references in attributes
  1822. */
  1823. if (ctxt->attsSpecial != NULL) {
  1824. xmlChar *nvalnorm;
  1825. xmlChar fn[50];
  1826. xmlChar *fullname;
  1827. fullname = xmlBuildQName(localname, prefix, fn, 50);
  1828. if (fullname != NULL) {
  1829. ctxt->vctxt.valid = 1;
  1830. nvalnorm = xmlValidCtxtNormalizeAttributeValue(
  1831. &ctxt->vctxt, ctxt->myDoc,
  1832. ctxt->node, fullname, dup);
  1833. if (ctxt->vctxt.valid != 1)
  1834. ctxt->valid = 0;
  1835. if ((fullname != fn) && (fullname != localname))
  1836. xmlFree(fullname);
  1837. if (nvalnorm != NULL) {
  1838. xmlFree(dup);
  1839. dup = nvalnorm;
  1840. }
  1841. }
  1842. }
  1843. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1844. ctxt->myDoc, ctxt->node, ret, dup);
  1845. }
  1846. } else {
  1847. /*
  1848. * if entities already have been substituted, then
  1849. * the attribute as passed is already normalized
  1850. */
  1851. dup = xmlStrndup(value, valueend - value);
  1852. ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
  1853. ctxt->myDoc, ctxt->node, ret, dup);
  1854. }
  1855. } else
  1856. #endif /* LIBXML_VALID_ENABLED */
  1857. if (((ctxt->loadsubset & XML_SKIP_IDS) == 0) &&
  1858. (((ctxt->replaceEntities == 0) && (ctxt->external != 2)) ||
  1859. ((ctxt->replaceEntities != 0) && (ctxt->inSubset == 0))) &&
  1860. /* Don't create IDs containing entity references */
  1861. (ret->children != NULL) &&
  1862. (ret->children->type == XML_TEXT_NODE) &&
  1863. (ret->children->next == NULL)) {
  1864. xmlChar *content = ret->children->content;
  1865. /*
  1866. * when validating, the ID registration is done at the attribute
  1867. * validation level. Otherwise we have to do specific handling here.
  1868. */
  1869. if ((prefix == ctxt->str_xml) &&
  1870. (localname[0] == 'i') && (localname[1] == 'd') &&
  1871. (localname[2] == 0)) {
  1872. /*
  1873. * Add the xml:id value
  1874. *
  1875. * Open issue: normalization of the value.
  1876. */
  1877. if (xmlValidateNCName(content, 1) != 0) {
  1878. xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
  1879. "xml:id : attribute value %s is not an NCName\n",
  1880. (const char *) content, NULL);
  1881. }
  1882. xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1883. } else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
  1884. xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1885. } else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
  1886. xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
  1887. }
  1888. }
  1889. if (dup != NULL)
  1890. xmlFree(dup);
  1891. return(ret);
  1892. }
  1893. /**
  1894. * xmlSAX2StartElementNs:
  1895. * @ctx: the user data (XML parser context)
  1896. * @localname: the local name of the element
  1897. * @prefix: the element namespace prefix if available
  1898. * @URI: the element namespace name if available
  1899. * @nb_namespaces: number of namespace definitions on that node
  1900. * @namespaces: pointer to the array of prefix/URI pairs namespace definitions
  1901. * @nb_attributes: the number of attributes on that node
  1902. * @nb_defaulted: the number of defaulted attributes.
  1903. * @attributes: pointer to the array of (localname/prefix/URI/value/end)
  1904. * attribute values.
  1905. *
  1906. * SAX2 callback when an element start has been detected by the parser.
  1907. * It provides the namespace information for the element, as well as
  1908. * the new namespace declarations on the element.
  1909. */
  1910. void
  1911. xmlSAX2StartElementNs(void *ctx,
  1912. const xmlChar *localname,
  1913. const xmlChar *prefix,
  1914. const xmlChar *URI,
  1915. int nb_namespaces,
  1916. const xmlChar **namespaces,
  1917. int nb_attributes,
  1918. int nb_defaulted,
  1919. const xmlChar **attributes)
  1920. {
  1921. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  1922. xmlNodePtr ret;
  1923. xmlNodePtr parent;
  1924. xmlNsPtr last = NULL, ns;
  1925. const xmlChar *uri, *pref;
  1926. xmlChar *lname = NULL;
  1927. int i, j;
  1928. if (ctx == NULL) return;
  1929. /*
  1930. * First check on validity:
  1931. */
  1932. if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
  1933. ((ctxt->myDoc->intSubset == NULL) ||
  1934. ((ctxt->myDoc->intSubset->notations == NULL) &&
  1935. (ctxt->myDoc->intSubset->elements == NULL) &&
  1936. (ctxt->myDoc->intSubset->attributes == NULL) &&
  1937. (ctxt->myDoc->intSubset->entities == NULL)))) {
  1938. xmlErrValid(ctxt, XML_DTD_NO_DTD,
  1939. "Validation failed: no DTD found !", NULL, NULL);
  1940. ctxt->validate = 0;
  1941. }
  1942. /*
  1943. * Take care of the rare case of an undefined namespace prefix
  1944. */
  1945. if ((prefix != NULL) && (URI == NULL)) {
  1946. if (ctxt->dictNames) {
  1947. const xmlChar *fullname;
  1948. fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
  1949. if (fullname != NULL)
  1950. localname = fullname;
  1951. } else {
  1952. lname = xmlBuildQName(localname, prefix, NULL, 0);
  1953. }
  1954. }
  1955. /*
  1956. * allocate the node
  1957. */
  1958. if (ctxt->freeElems != NULL) {
  1959. ret = ctxt->freeElems;
  1960. ctxt->freeElems = ret->next;
  1961. ctxt->freeElemsNr--;
  1962. memset(ret, 0, sizeof(xmlNode));
  1963. ret->doc = ctxt->myDoc;
  1964. ret->type = XML_ELEMENT_NODE;
  1965. if (ctxt->dictNames)
  1966. ret->name = localname;
  1967. else {
  1968. if (lname == NULL)
  1969. ret->name = xmlStrdup(localname);
  1970. else
  1971. ret->name = lname;
  1972. if (ret->name == NULL) {
  1973. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  1974. xmlFree(ret);
  1975. return;
  1976. }
  1977. }
  1978. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  1979. xmlRegisterNodeDefaultValue(ret);
  1980. } else {
  1981. if (ctxt->dictNames)
  1982. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
  1983. (xmlChar *) localname, NULL);
  1984. else if (lname == NULL)
  1985. ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
  1986. else
  1987. ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
  1988. (xmlChar *) lname, NULL);
  1989. if (ret == NULL) {
  1990. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  1991. return;
  1992. }
  1993. }
  1994. if (ctxt->linenumbers) {
  1995. if (ctxt->input != NULL) {
  1996. if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
  1997. ret->line = ctxt->input->line;
  1998. else
  1999. ret->line = USHRT_MAX;
  2000. }
  2001. }
  2002. /*
  2003. * Build the namespace list
  2004. */
  2005. for (i = 0,j = 0;j < nb_namespaces;j++) {
  2006. pref = namespaces[i++];
  2007. uri = namespaces[i++];
  2008. ns = xmlNewNs(NULL, uri, pref);
  2009. if (ns != NULL) {
  2010. if (last == NULL) {
  2011. ret->nsDef = last = ns;
  2012. } else {
  2013. last->next = ns;
  2014. last = ns;
  2015. }
  2016. if ((URI != NULL) && (prefix == pref))
  2017. ret->ns = ns;
  2018. } else {
  2019. /*
  2020. * any out of memory error would already have been raised
  2021. * but we can't be guaranteed it's the actual error due to the
  2022. * API, best is to skip in this case
  2023. */
  2024. continue;
  2025. }
  2026. xmlParserNsUpdateSax(ctxt, pref, ns);
  2027. #ifdef LIBXML_VALID_ENABLED
  2028. if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
  2029. ctxt->myDoc && ctxt->myDoc->intSubset) {
  2030. ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
  2031. ret, prefix, ns, uri);
  2032. }
  2033. #endif /* LIBXML_VALID_ENABLED */
  2034. }
  2035. ctxt->nodemem = -1;
  2036. /* Initialize parent before pushing node */
  2037. parent = ctxt->node;
  2038. if (parent == NULL)
  2039. parent = (xmlNodePtr) ctxt->myDoc;
  2040. /*
  2041. * We are parsing a new node.
  2042. */
  2043. if (nodePush(ctxt, ret) < 0) {
  2044. xmlUnlinkNode(ret);
  2045. xmlFreeNode(ret);
  2046. return;
  2047. }
  2048. /*
  2049. * Link the child element
  2050. */
  2051. xmlAddChild(parent, ret);
  2052. /*
  2053. * Insert the defaulted attributes from the DTD only if requested:
  2054. */
  2055. if ((nb_defaulted != 0) &&
  2056. ((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
  2057. nb_attributes -= nb_defaulted;
  2058. /*
  2059. * Search the namespace if it wasn't already found
  2060. * Note that, if prefix is NULL, this searches for the default Ns
  2061. */
  2062. if ((URI != NULL) && (ret->ns == NULL)) {
  2063. ret->ns = xmlParserNsLookupSax(ctxt, prefix);
  2064. if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
  2065. ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
  2066. }
  2067. if (ret->ns == NULL) {
  2068. ns = xmlNewNs(ret, NULL, prefix);
  2069. if (ns == NULL) {
  2070. xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
  2071. return;
  2072. }
  2073. if (prefix != NULL)
  2074. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2075. "Namespace prefix %s was not found\n",
  2076. prefix, NULL);
  2077. else
  2078. xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
  2079. "Namespace default prefix was not found\n",
  2080. NULL, NULL);
  2081. }
  2082. }
  2083. /*
  2084. * process all the other attributes
  2085. */
  2086. if (nb_attributes > 0) {
  2087. xmlAttrPtr prev = NULL;
  2088. for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
  2089. xmlAttrPtr attr = NULL;
  2090. /*
  2091. * Handle the rare case of an undefined attribute prefix
  2092. */
  2093. if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
  2094. if (ctxt->dictNames) {
  2095. const xmlChar *fullname;
  2096. fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
  2097. attributes[j]);
  2098. if (fullname != NULL) {
  2099. attr = xmlSAX2AttributeNs(ctxt, fullname, NULL,
  2100. attributes[j+3],
  2101. attributes[j+4]);
  2102. goto have_attr;
  2103. }
  2104. } else {
  2105. lname = xmlBuildQName(attributes[j], attributes[j+1],
  2106. NULL, 0);
  2107. if (lname != NULL) {
  2108. attr = xmlSAX2AttributeNs(ctxt, lname, NULL,
  2109. attributes[j+3],
  2110. attributes[j+4]);
  2111. xmlFree(lname);
  2112. goto have_attr;
  2113. }
  2114. }
  2115. }
  2116. attr = xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
  2117. attributes[j+3], attributes[j+4]);
  2118. have_attr:
  2119. if (attr == NULL)
  2120. continue;
  2121. /* link at the end to preserve order */
  2122. if (prev == NULL) {
  2123. ctxt->node->properties = attr;
  2124. } else {
  2125. prev->next = attr;
  2126. attr->prev = prev;
  2127. }
  2128. prev = attr;
  2129. }
  2130. }
  2131. #ifdef LIBXML_VALID_ENABLED
  2132. /*
  2133. * If it's the Document root, finish the DTD validation and
  2134. * check the document root element for validity
  2135. */
  2136. if ((ctxt->validate) &&
  2137. ((ctxt->vctxt.flags & XML_VCTXT_DTD_VALIDATED) == 0)) {
  2138. int chk;
  2139. chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
  2140. if (chk <= 0)
  2141. ctxt->valid = 0;
  2142. if (chk < 0)
  2143. ctxt->wellFormed = 0;
  2144. ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
  2145. ctxt->vctxt.flags |= XML_VCTXT_DTD_VALIDATED;
  2146. }
  2147. #endif /* LIBXML_VALID_ENABLED */
  2148. }
  2149. /**
  2150. * xmlSAX2EndElementNs:
  2151. * @ctx: the user data (XML parser context)
  2152. * @localname: the local name of the element
  2153. * @prefix: the element namespace prefix if available
  2154. * @URI: the element namespace name if available
  2155. *
  2156. * SAX2 callback when an element end has been detected by the parser.
  2157. * It provides the namespace information for the element.
  2158. */
  2159. void
  2160. xmlSAX2EndElementNs(void *ctx,
  2161. const xmlChar * localname ATTRIBUTE_UNUSED,
  2162. const xmlChar * prefix ATTRIBUTE_UNUSED,
  2163. const xmlChar * URI ATTRIBUTE_UNUSED)
  2164. {
  2165. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2166. if (ctx == NULL) return;
  2167. ctxt->nodemem = -1;
  2168. #ifdef LIBXML_VALID_ENABLED
  2169. if (ctxt->validate && ctxt->wellFormed &&
  2170. ctxt->myDoc && ctxt->myDoc->intSubset)
  2171. ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc,
  2172. ctxt->node);
  2173. #endif /* LIBXML_VALID_ENABLED */
  2174. /*
  2175. * end of parsing of this node.
  2176. */
  2177. nodePop(ctxt);
  2178. }
  2179. /**
  2180. * xmlSAX2Reference:
  2181. * @ctx: the user data (XML parser context)
  2182. * @name: The entity name
  2183. *
  2184. * called when an entity xmlSAX2Reference is detected.
  2185. */
  2186. void
  2187. xmlSAX2Reference(void *ctx, const xmlChar *name)
  2188. {
  2189. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2190. xmlNodePtr ret;
  2191. if (ctx == NULL) return;
  2192. ret = xmlNewReference(ctxt->myDoc, name);
  2193. if (xmlAddChild(ctxt->node, ret) == NULL) {
  2194. xmlFreeNode(ret);
  2195. }
  2196. }
  2197. /**
  2198. * xmlSAX2Text:
  2199. * @ctx: the user data (XML parser context)
  2200. * @ch: a xmlChar string
  2201. * @len: the number of xmlChar
  2202. * @type: text or cdata
  2203. *
  2204. * Append characters.
  2205. */
  2206. static void
  2207. xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
  2208. xmlElementType type)
  2209. {
  2210. xmlNodePtr lastChild;
  2211. if (ctxt == NULL) return;
  2212. /*
  2213. * Handle the data if any. If there is no child
  2214. * add it as content, otherwise if the last child is text,
  2215. * concatenate it, else create a new node of type text.
  2216. */
  2217. if (ctxt->node == NULL) {
  2218. return;
  2219. }
  2220. lastChild = ctxt->node->last;
  2221. /*
  2222. * Here we needed an accelerator mechanism in case of very large
  2223. * elements. Use an attribute in the structure !!!
  2224. */
  2225. if (lastChild == NULL) {
  2226. if (type == XML_TEXT_NODE)
  2227. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2228. else
  2229. lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
  2230. if (lastChild != NULL) {
  2231. ctxt->node->children = lastChild;
  2232. ctxt->node->last = lastChild;
  2233. lastChild->parent = ctxt->node;
  2234. lastChild->doc = ctxt->node->doc;
  2235. ctxt->nodelen = len;
  2236. ctxt->nodemem = len + 1;
  2237. } else {
  2238. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2239. return;
  2240. }
  2241. } else {
  2242. int coalesceText = (lastChild != NULL) &&
  2243. (lastChild->type == type) &&
  2244. ((type != XML_TEXT_NODE) ||
  2245. (lastChild->name == xmlStringText));
  2246. if ((coalesceText) && (ctxt->nodemem != 0)) {
  2247. /*
  2248. * The whole point of maintaining nodelen and nodemem,
  2249. * xmlTextConcat is too costly, i.e. compute length,
  2250. * reallocate a new buffer, move data, append ch. Here
  2251. * We try to minimize realloc() uses and avoid copying
  2252. * and recomputing length over and over.
  2253. */
  2254. if (lastChild->content == (xmlChar *)&(lastChild->properties)) {
  2255. lastChild->content = xmlStrdup(lastChild->content);
  2256. lastChild->properties = NULL;
  2257. } else if ((ctxt->nodemem == ctxt->nodelen + 1) &&
  2258. (xmlDictOwns(ctxt->dict, lastChild->content))) {
  2259. lastChild->content = xmlStrdup(lastChild->content);
  2260. }
  2261. if (lastChild->content == NULL) {
  2262. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: xmlStrdup returned NULL");
  2263. return;
  2264. }
  2265. if (ctxt->nodelen > INT_MAX - len) {
  2266. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters overflow prevented");
  2267. return;
  2268. }
  2269. if ((ctxt->nodelen + len > XML_MAX_TEXT_LENGTH) &&
  2270. ((ctxt->options & XML_PARSE_HUGE) == 0)) {
  2271. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters: huge text node");
  2272. return;
  2273. }
  2274. if (ctxt->nodelen + len >= ctxt->nodemem) {
  2275. xmlChar *newbuf;
  2276. int size;
  2277. size = ctxt->nodemem > INT_MAX - len ?
  2278. INT_MAX :
  2279. ctxt->nodemem + len;
  2280. size = size > INT_MAX / 2 ? INT_MAX : size * 2;
  2281. newbuf = (xmlChar *) xmlRealloc(lastChild->content,size);
  2282. if (newbuf == NULL) {
  2283. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2284. return;
  2285. }
  2286. ctxt->nodemem = size;
  2287. lastChild->content = newbuf;
  2288. }
  2289. memcpy(&lastChild->content[ctxt->nodelen], ch, len);
  2290. ctxt->nodelen += len;
  2291. lastChild->content[ctxt->nodelen] = 0;
  2292. } else if (coalesceText) {
  2293. if (xmlTextConcat(lastChild, ch, len)) {
  2294. xmlSAX2ErrMemory(ctxt, "xmlSAX2Characters");
  2295. }
  2296. if (ctxt->node->children != NULL) {
  2297. ctxt->nodelen = xmlStrlen(lastChild->content);
  2298. ctxt->nodemem = ctxt->nodelen + 1;
  2299. }
  2300. } else {
  2301. /* Mixed content, first time */
  2302. if (type == XML_TEXT_NODE) {
  2303. lastChild = xmlSAX2TextNode(ctxt, ch, len);
  2304. if (lastChild != NULL)
  2305. lastChild->doc = ctxt->myDoc;
  2306. } else
  2307. lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
  2308. if (lastChild != NULL) {
  2309. xmlAddChild(ctxt->node, lastChild);
  2310. if (ctxt->node->children != NULL) {
  2311. ctxt->nodelen = len;
  2312. ctxt->nodemem = len + 1;
  2313. }
  2314. }
  2315. }
  2316. }
  2317. if ((lastChild != NULL) &&
  2318. (type == XML_TEXT_NODE) &&
  2319. (ctxt->linenumbers) &&
  2320. (ctxt->input != NULL)) {
  2321. if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
  2322. lastChild->line = ctxt->input->line;
  2323. else {
  2324. lastChild->line = USHRT_MAX;
  2325. if (ctxt->options & XML_PARSE_BIG_LINES)
  2326. lastChild->psvi = (void *) (ptrdiff_t) ctxt->input->line;
  2327. }
  2328. }
  2329. }
  2330. /**
  2331. * xmlSAX2Characters:
  2332. * @ctx: the user data (XML parser context)
  2333. * @ch: a xmlChar string
  2334. * @len: the number of xmlChar
  2335. *
  2336. * receiving some chars from the parser.
  2337. */
  2338. void
  2339. xmlSAX2Characters(void *ctx, const xmlChar *ch, int len)
  2340. {
  2341. xmlSAX2Text((xmlParserCtxtPtr) ctx, ch, len, XML_TEXT_NODE);
  2342. }
  2343. /**
  2344. * xmlSAX2IgnorableWhitespace:
  2345. * @ctx: the user data (XML parser context)
  2346. * @ch: a xmlChar string
  2347. * @len: the number of xmlChar
  2348. *
  2349. * receiving some ignorable whitespaces from the parser.
  2350. * UNUSED: by default the DOM building will use xmlSAX2Characters
  2351. */
  2352. void
  2353. xmlSAX2IgnorableWhitespace(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED)
  2354. {
  2355. }
  2356. /**
  2357. * xmlSAX2ProcessingInstruction:
  2358. * @ctx: the user data (XML parser context)
  2359. * @target: the target name
  2360. * @data: the PI data's
  2361. *
  2362. * A processing instruction has been parsed.
  2363. */
  2364. void
  2365. xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
  2366. const xmlChar *data)
  2367. {
  2368. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2369. xmlNodePtr ret;
  2370. xmlNodePtr parent;
  2371. if (ctx == NULL) return;
  2372. parent = ctxt->node;
  2373. ret = xmlNewDocPI(ctxt->myDoc, target, data);
  2374. if (ret == NULL) return;
  2375. if (ctxt->linenumbers) {
  2376. if (ctxt->input != NULL) {
  2377. if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
  2378. ret->line = ctxt->input->line;
  2379. else
  2380. ret->line = USHRT_MAX;
  2381. }
  2382. }
  2383. if (ctxt->inSubset == 1) {
  2384. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2385. return;
  2386. } else if (ctxt->inSubset == 2) {
  2387. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2388. return;
  2389. }
  2390. if (parent == NULL) {
  2391. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2392. return;
  2393. }
  2394. if (parent->type == XML_ELEMENT_NODE) {
  2395. xmlAddChild(parent, ret);
  2396. } else {
  2397. xmlAddSibling(parent, ret);
  2398. }
  2399. }
  2400. /**
  2401. * xmlSAX2Comment:
  2402. * @ctx: the user data (XML parser context)
  2403. * @value: the xmlSAX2Comment content
  2404. *
  2405. * A xmlSAX2Comment has been parsed.
  2406. */
  2407. void
  2408. xmlSAX2Comment(void *ctx, const xmlChar *value)
  2409. {
  2410. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  2411. xmlNodePtr ret;
  2412. xmlNodePtr parent;
  2413. if (ctx == NULL) return;
  2414. parent = ctxt->node;
  2415. ret = xmlNewDocComment(ctxt->myDoc, value);
  2416. if (ret == NULL) return;
  2417. if (ctxt->linenumbers) {
  2418. if (ctxt->input != NULL) {
  2419. if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)
  2420. ret->line = ctxt->input->line;
  2421. else
  2422. ret->line = USHRT_MAX;
  2423. }
  2424. }
  2425. if (ctxt->inSubset == 1) {
  2426. xmlAddChild((xmlNodePtr) ctxt->myDoc->intSubset, ret);
  2427. return;
  2428. } else if (ctxt->inSubset == 2) {
  2429. xmlAddChild((xmlNodePtr) ctxt->myDoc->extSubset, ret);
  2430. return;
  2431. }
  2432. if (parent == NULL) {
  2433. xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
  2434. return;
  2435. }
  2436. if (parent->type == XML_ELEMENT_NODE) {
  2437. xmlAddChild(parent, ret);
  2438. } else {
  2439. xmlAddSibling(parent, ret);
  2440. }
  2441. }
  2442. /**
  2443. * xmlSAX2CDataBlock:
  2444. * @ctx: the user data (XML parser context)
  2445. * @value: The pcdata content
  2446. * @len: the block length
  2447. *
  2448. * called when a pcdata block has been parsed
  2449. */
  2450. void
  2451. xmlSAX2CDataBlock(void *ctx, const xmlChar *value, int len)
  2452. {
  2453. xmlSAX2Text((xmlParserCtxtPtr) ctx, value, len, XML_CDATA_SECTION_NODE);
  2454. }
  2455. static int xmlSAX2DefaultVersionValue = 2;
  2456. #ifdef LIBXML_SAX1_ENABLED
  2457. /**
  2458. * xmlSAXDefaultVersion:
  2459. * @version: the version, 1 or 2
  2460. *
  2461. * DEPRECATED: Use parser option XML_PARSE_SAX1.
  2462. *
  2463. * Set the default version of SAX used globally by the library.
  2464. * By default, during initialization the default is set to 2.
  2465. * Note that it is generally a better coding style to use
  2466. * xmlSAXVersion() to set up the version explicitly for a given
  2467. * parsing context.
  2468. *
  2469. * Returns the previous value in case of success and -1 in case of error.
  2470. */
  2471. int
  2472. xmlSAXDefaultVersion(int version)
  2473. {
  2474. int ret = xmlSAX2DefaultVersionValue;
  2475. if ((version != 1) && (version != 2))
  2476. return(-1);
  2477. xmlSAX2DefaultVersionValue = version;
  2478. return(ret);
  2479. }
  2480. #endif /* LIBXML_SAX1_ENABLED */
  2481. /**
  2482. * xmlSAXVersion:
  2483. * @hdlr: the SAX handler
  2484. * @version: the version, 1 or 2
  2485. *
  2486. * Initialize the default XML SAX handler according to the version
  2487. *
  2488. * Returns 0 in case of success and -1 in case of error.
  2489. */
  2490. int
  2491. xmlSAXVersion(xmlSAXHandler *hdlr, int version)
  2492. {
  2493. if (hdlr == NULL) return(-1);
  2494. if (version == 2) {
  2495. hdlr->startElementNs = xmlSAX2StartElementNs;
  2496. hdlr->endElementNs = xmlSAX2EndElementNs;
  2497. hdlr->serror = NULL;
  2498. hdlr->initialized = XML_SAX2_MAGIC;
  2499. #ifdef LIBXML_SAX1_ENABLED
  2500. } else if (version == 1) {
  2501. hdlr->initialized = 1;
  2502. #endif /* LIBXML_SAX1_ENABLED */
  2503. } else
  2504. return(-1);
  2505. #ifdef LIBXML_SAX1_ENABLED
  2506. hdlr->startElement = xmlSAX2StartElement;
  2507. hdlr->endElement = xmlSAX2EndElement;
  2508. #else
  2509. hdlr->startElement = NULL;
  2510. hdlr->endElement = NULL;
  2511. #endif /* LIBXML_SAX1_ENABLED */
  2512. hdlr->internalSubset = xmlSAX2InternalSubset;
  2513. hdlr->externalSubset = xmlSAX2ExternalSubset;
  2514. hdlr->isStandalone = xmlSAX2IsStandalone;
  2515. hdlr->hasInternalSubset = xmlSAX2HasInternalSubset;
  2516. hdlr->hasExternalSubset = xmlSAX2HasExternalSubset;
  2517. hdlr->resolveEntity = xmlSAX2ResolveEntity;
  2518. hdlr->getEntity = xmlSAX2GetEntity;
  2519. hdlr->getParameterEntity = xmlSAX2GetParameterEntity;
  2520. hdlr->entityDecl = xmlSAX2EntityDecl;
  2521. hdlr->attributeDecl = xmlSAX2AttributeDecl;
  2522. hdlr->elementDecl = xmlSAX2ElementDecl;
  2523. hdlr->notationDecl = xmlSAX2NotationDecl;
  2524. hdlr->unparsedEntityDecl = xmlSAX2UnparsedEntityDecl;
  2525. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2526. hdlr->startDocument = xmlSAX2StartDocument;
  2527. hdlr->endDocument = xmlSAX2EndDocument;
  2528. hdlr->reference = xmlSAX2Reference;
  2529. hdlr->characters = xmlSAX2Characters;
  2530. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2531. hdlr->ignorableWhitespace = xmlSAX2Characters;
  2532. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2533. hdlr->comment = xmlSAX2Comment;
  2534. hdlr->warning = xmlParserWarning;
  2535. hdlr->error = xmlParserError;
  2536. hdlr->fatalError = xmlParserError;
  2537. return(0);
  2538. }
  2539. /**
  2540. * xmlSAX2InitDefaultSAXHandler:
  2541. * @hdlr: the SAX handler
  2542. * @warning: flag if non-zero sets the handler warning procedure
  2543. *
  2544. * Initialize the default XML SAX2 handler
  2545. */
  2546. void
  2547. xmlSAX2InitDefaultSAXHandler(xmlSAXHandler *hdlr, int warning)
  2548. {
  2549. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2550. return;
  2551. xmlSAXVersion(hdlr, xmlSAX2DefaultVersionValue);
  2552. if (warning == 0)
  2553. hdlr->warning = NULL;
  2554. else
  2555. hdlr->warning = xmlParserWarning;
  2556. }
  2557. /**
  2558. * xmlDefaultSAXHandlerInit:
  2559. *
  2560. * DEPRECATED: This function is a no-op. Call xmlInitParser to
  2561. * initialize the library.
  2562. *
  2563. * Initialize the default SAX2 handler
  2564. */
  2565. void
  2566. xmlDefaultSAXHandlerInit(void)
  2567. {
  2568. }
  2569. #ifdef LIBXML_HTML_ENABLED
  2570. /**
  2571. * xmlSAX2InitHtmlDefaultSAXHandler:
  2572. * @hdlr: the SAX handler
  2573. *
  2574. * Initialize the default HTML SAX2 handler
  2575. */
  2576. void
  2577. xmlSAX2InitHtmlDefaultSAXHandler(xmlSAXHandler *hdlr)
  2578. {
  2579. if ((hdlr == NULL) || (hdlr->initialized != 0))
  2580. return;
  2581. hdlr->internalSubset = xmlSAX2InternalSubset;
  2582. hdlr->externalSubset = NULL;
  2583. hdlr->isStandalone = NULL;
  2584. hdlr->hasInternalSubset = NULL;
  2585. hdlr->hasExternalSubset = NULL;
  2586. hdlr->resolveEntity = NULL;
  2587. hdlr->getEntity = xmlSAX2GetEntity;
  2588. hdlr->getParameterEntity = NULL;
  2589. hdlr->entityDecl = NULL;
  2590. hdlr->attributeDecl = NULL;
  2591. hdlr->elementDecl = NULL;
  2592. hdlr->notationDecl = NULL;
  2593. hdlr->unparsedEntityDecl = NULL;
  2594. hdlr->setDocumentLocator = xmlSAX2SetDocumentLocator;
  2595. hdlr->startDocument = xmlSAX2StartDocument;
  2596. hdlr->endDocument = xmlSAX2EndDocument;
  2597. hdlr->startElement = xmlSAX2StartElement;
  2598. hdlr->endElement = xmlSAX2EndElement;
  2599. hdlr->reference = NULL;
  2600. hdlr->characters = xmlSAX2Characters;
  2601. hdlr->cdataBlock = xmlSAX2CDataBlock;
  2602. hdlr->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  2603. hdlr->processingInstruction = xmlSAX2ProcessingInstruction;
  2604. hdlr->comment = xmlSAX2Comment;
  2605. hdlr->warning = xmlParserWarning;
  2606. hdlr->error = xmlParserError;
  2607. hdlr->fatalError = xmlParserError;
  2608. hdlr->initialized = 1;
  2609. }
  2610. /**
  2611. * htmlDefaultSAXHandlerInit:
  2612. *
  2613. * DEPRECATED: This function is a no-op. Call xmlInitParser to
  2614. * initialize the library.
  2615. */
  2616. void
  2617. htmlDefaultSAXHandlerInit(void)
  2618. {
  2619. }
  2620. #endif /* LIBXML_HTML_ENABLED */