2
0

xmlparse.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705
  1. /*
  2. Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
  3. See the file copying.txt for copying permission.
  4. */
  5. /* In 2001, this was part of the Expat package. We copied it into
  6. Xmlrpc-c because it's easier on the user than making him get and
  7. link Expat separately, and we don't expect to benefit from separate
  8. maintenance of Expat.
  9. But we changed all the external symbols that in Expat are named
  10. "XML_xxxx" to "xmlrpc_XML_xxxx" because people do link Xmlrpc-c
  11. libraries into programs that also link Expat (a good example is
  12. where an Apache module uses Xmlrpc-c). We don't want our names to
  13. collide with Expat's.
  14. */
  15. #include <stddef.h>
  16. #include <assert.h>
  17. #include "xmlrpc_config.h"
  18. #include "c_util.h"
  19. #include "girmath.h"
  20. #include "mallocvar.h"
  21. #include "xmlrpc-c/string_int.h"
  22. #include "xmldef.h"
  23. #include "xmlparse.h"
  24. static const char *
  25. extractXmlSample(const char * const start,
  26. const char * const end,
  27. size_t const maximumLen) {
  28. size_t const len = MIN(maximumLen, (size_t)(end - start));
  29. return xmlrpc_makePrintable_lp(start, len);
  30. }
  31. #ifdef XML_UNICODE
  32. #define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
  33. #define XmlConvert XmlUtf16Convert
  34. #define XmlGetInternalEncoding xmlrpc_XmlGetUtf16InternalEncoding
  35. #define XmlGetInternalEncodingNS xmlrpc_XmlGetUtf16InternalEncodingNS
  36. #define XmlEncode xmlrpc_XmlUtf16Encode
  37. #define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((unsigned long)s) & 1))
  38. typedef unsigned short ICHAR;
  39. #else
  40. #define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
  41. #define XmlConvert XmlUtf8Convert
  42. #define XmlGetInternalEncoding xmlrpc_XmlGetUtf8InternalEncoding
  43. #define XmlGetInternalEncodingNS xmlrpc_XmlGetUtf8InternalEncodingNS
  44. #define XmlEncode xmlrpc_XmlUtf8Encode
  45. #define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
  46. typedef char ICHAR;
  47. #endif
  48. #ifndef XML_NS
  49. #define XmlInitEncodingNS XmlInitEncoding
  50. #define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
  51. #undef XmlGetInternalEncodingNS
  52. #define XmlGetInternalEncodingNS XmlGetInternalEncoding
  53. #define XmlParseXmlDeclNS XmlParseXmlDecl
  54. #endif
  55. #ifdef XML_UNICODE_WCHAR_T
  56. #define XML_T(x) L ## x
  57. #else
  58. #define XML_T(x) x
  59. #endif
  60. /* Round up n to be a multiple of sz, where sz is a power of 2. */
  61. #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
  62. #include "xmltok.h"
  63. #include "xmlrole.h"
  64. typedef const XML_Char *KEY;
  65. typedef struct {
  66. KEY name;
  67. } NAMED;
  68. typedef struct {
  69. NAMED **v;
  70. size_t size;
  71. size_t used;
  72. size_t usedLim;
  73. } HASH_TABLE;
  74. typedef struct {
  75. NAMED **p;
  76. NAMED **end;
  77. } HASH_TABLE_ITER;
  78. #define INIT_TAG_BUF_SIZE 32 /* must be a multiple of sizeof(XML_Char) */
  79. #define INIT_DATA_BUF_SIZE 1024
  80. #define INIT_ATTS_SIZE 16
  81. #define INIT_BLOCK_SIZE 1024
  82. #define INIT_BUFFER_SIZE 1024
  83. #define EXPAND_SPARE 24
  84. typedef struct binding {
  85. struct prefix *prefix;
  86. struct binding *nextTagBinding;
  87. struct binding *prevPrefixBinding;
  88. const struct attribute_id *attId;
  89. XML_Char *uri;
  90. int uriLen;
  91. int uriAlloc;
  92. } BINDING;
  93. typedef struct prefix {
  94. const XML_Char *name;
  95. BINDING *binding;
  96. } PREFIX;
  97. typedef struct {
  98. const XML_Char *str;
  99. const XML_Char *localPart;
  100. int uriLen;
  101. } TAG_NAME;
  102. typedef struct tag {
  103. struct tag *parent;
  104. const char *rawName;
  105. int rawNameLength;
  106. TAG_NAME name;
  107. char *buf;
  108. char *bufEnd;
  109. BINDING *bindings;
  110. } TAG;
  111. typedef struct {
  112. const XML_Char *name;
  113. const XML_Char *textPtr;
  114. size_t textLen;
  115. const XML_Char *systemId;
  116. const XML_Char *base;
  117. const XML_Char *publicId;
  118. const XML_Char *notation;
  119. char open;
  120. } ENTITY;
  121. typedef struct block {
  122. struct block *next;
  123. int size;
  124. XML_Char s[1];
  125. } BLOCK;
  126. typedef struct {
  127. BLOCK *blocks;
  128. BLOCK *freeBlocks;
  129. const XML_Char *end;
  130. XML_Char *ptr;
  131. XML_Char *start;
  132. } STRING_POOL;
  133. /* The XML_Char before the name is used to determine whether
  134. an attribute has been specified. */
  135. typedef struct attribute_id {
  136. XML_Char *name;
  137. PREFIX *prefix;
  138. char maybeTokenized;
  139. char xmlns;
  140. } ATTRIBUTE_ID;
  141. typedef struct {
  142. const ATTRIBUTE_ID *id;
  143. char isCdata;
  144. const XML_Char *value;
  145. } DEFAULT_ATTRIBUTE;
  146. typedef struct {
  147. const XML_Char *name;
  148. PREFIX *prefix;
  149. const ATTRIBUTE_ID *idAtt;
  150. int nDefaultAtts;
  151. int allocDefaultAtts;
  152. DEFAULT_ATTRIBUTE *defaultAtts;
  153. } ELEMENT_TYPE;
  154. typedef struct {
  155. HASH_TABLE generalEntities;
  156. HASH_TABLE elementTypes;
  157. HASH_TABLE attributeIds;
  158. HASH_TABLE prefixes;
  159. STRING_POOL pool;
  160. int complete;
  161. int standalone;
  162. HASH_TABLE paramEntities;
  163. PREFIX defaultPrefix;
  164. } DTD;
  165. typedef struct open_internal_entity {
  166. const char *internalEventPtr;
  167. const char *internalEventEndPtr;
  168. struct open_internal_entity *next;
  169. ENTITY *entity;
  170. } OPEN_INTERNAL_ENTITY;
  171. typedef void Processor(XML_Parser parser,
  172. const char * const start,
  173. const char * const end,
  174. const char ** const endPtr,
  175. enum XML_Error * const errorCodeP,
  176. const char ** const errorP);
  177. #define poolStart(pool) ((pool)->start)
  178. #define poolEnd(pool) ((pool)->ptr)
  179. #define poolLength(pool) ((pool)->ptr - (pool)->start)
  180. #define poolChop(pool) ((void)--(pool->ptr))
  181. #define poolLastChar(pool) (((pool)->ptr)[-1])
  182. #define poolDiscard(pool) ((pool)->ptr = (pool)->start)
  183. #define poolFinish(pool) ((pool)->start = (pool)->ptr)
  184. #define poolAppendChar(pool, c) \
  185. (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
  186. ? 0 \
  187. : ((*((pool)->ptr)++ = c), 1))
  188. typedef struct {
  189. /* The first member must be userData so that the XML_GetUserData macro works. */
  190. void *m_userData;
  191. void *m_handlerArg;
  192. char *m_buffer;
  193. /* first character to be parsed */
  194. const char *m_bufferPtr;
  195. /* past last character to be parsed */
  196. char *m_bufferEnd;
  197. /* allocated end of buffer */
  198. const char *m_bufferLim;
  199. long m_parseEndByteIndex;
  200. const char *m_parseEndPtr;
  201. XML_Char *m_dataBuf;
  202. XML_Char *m_dataBufEnd;
  203. XML_StartElementHandler m_startElementHandler;
  204. XML_EndElementHandler m_endElementHandler;
  205. XML_CharacterDataHandler m_characterDataHandler;
  206. XML_ProcessingInstructionHandler m_processingInstructionHandler;
  207. XML_CommentHandler m_commentHandler;
  208. XML_StartCdataSectionHandler m_startCdataSectionHandler;
  209. XML_EndCdataSectionHandler m_endCdataSectionHandler;
  210. XML_DefaultHandler m_defaultHandler;
  211. XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
  212. XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
  213. XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
  214. XML_NotationDeclHandler m_notationDeclHandler;
  215. XML_ExternalParsedEntityDeclHandler m_externalParsedEntityDeclHandler;
  216. XML_InternalParsedEntityDeclHandler m_internalParsedEntityDeclHandler;
  217. XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
  218. XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
  219. XML_NotStandaloneHandler m_notStandaloneHandler;
  220. XML_ExternalEntityRefHandler m_externalEntityRefHandler;
  221. void *m_externalEntityRefHandlerArg;
  222. XML_UnknownEncodingHandler m_unknownEncodingHandler;
  223. const ENCODING *m_encoding;
  224. INIT_ENCODING m_initEncoding;
  225. const ENCODING *m_internalEncoding;
  226. const XML_Char *m_protocolEncodingName;
  227. int m_ns;
  228. void *m_unknownEncodingMem;
  229. void *m_unknownEncodingData;
  230. void *m_unknownEncodingHandlerData;
  231. void (*m_unknownEncodingRelease)(void *);
  232. PROLOG_STATE m_prologState;
  233. Processor *m_processor;
  234. /* The next processor to run */
  235. enum XML_Error m_errorCode;
  236. /* Explanation of the failure of the most recent call to Expat.
  237. XML_ERROR_NONE means it didn't fail. This is redundant with
  238. m_errorString if the latter is non-null.
  239. The latter is newer and better.
  240. */
  241. const char * m_errorString;
  242. /* malloc'ed string describing the failure of the most recent call
  243. to Expat. NULL means m_errorCode is the only error information
  244. available.
  245. */
  246. const char *m_eventPtr;
  247. const char *m_eventEndPtr;
  248. const char *m_positionPtr;
  249. OPEN_INTERNAL_ENTITY *m_openInternalEntities;
  250. int m_defaultExpandInternalEntities;
  251. int m_tagLevel;
  252. ENTITY *m_declEntity;
  253. const XML_Char *m_declNotationName;
  254. const XML_Char *m_declNotationPublicId;
  255. ELEMENT_TYPE *m_declElementType;
  256. ATTRIBUTE_ID *m_declAttributeId;
  257. char m_declAttributeIsCdata;
  258. char m_declAttributeIsId;
  259. DTD m_dtd;
  260. const XML_Char *m_curBase;
  261. TAG *m_tagStack;
  262. TAG *m_freeTagList;
  263. BINDING *m_inheritedBindings;
  264. BINDING *m_freeBindingList;
  265. int m_attsSize;
  266. int m_nSpecifiedAtts;
  267. int m_idAttIndex;
  268. ATTRIBUTE *m_atts;
  269. POSITION m_position;
  270. STRING_POOL m_tempPool;
  271. STRING_POOL m_temp2Pool;
  272. char *m_groupConnector;
  273. unsigned m_groupSize;
  274. int m_hadExternalDoctype;
  275. XML_Char m_namespaceSeparator;
  276. enum XML_ParamEntityParsing m_paramEntityParsing;
  277. XML_Parser m_parentParser;
  278. } Parser;
  279. #define userData (((Parser *)parser)->m_userData)
  280. #define handlerArg (((Parser *)parser)->m_handlerArg)
  281. #define startElementHandler (((Parser *)parser)->m_startElementHandler)
  282. #define endElementHandler (((Parser *)parser)->m_endElementHandler)
  283. #define characterDataHandler (((Parser *)parser)->m_characterDataHandler)
  284. #define processingInstructionHandler (((Parser *)parser)->m_processingInstructionHandler)
  285. #define commentHandler (((Parser *)parser)->m_commentHandler)
  286. #define startCdataSectionHandler (((Parser *)parser)->m_startCdataSectionHandler)
  287. #define endCdataSectionHandler (((Parser *)parser)->m_endCdataSectionHandler)
  288. #define defaultHandler (((Parser *)parser)->m_defaultHandler)
  289. #define startDoctypeDeclHandler (((Parser *)parser)->m_startDoctypeDeclHandler)
  290. #define endDoctypeDeclHandler (((Parser *)parser)->m_endDoctypeDeclHandler)
  291. #define unparsedEntityDeclHandler (((Parser *)parser)->m_unparsedEntityDeclHandler)
  292. #define notationDeclHandler (((Parser *)parser)->m_notationDeclHandler)
  293. #define externalParsedEntityDeclHandler (((Parser *)parser)->m_externalParsedEntityDeclHandler)
  294. #define internalParsedEntityDeclHandler (((Parser *)parser)->m_internalParsedEntityDeclHandler)
  295. #define startNamespaceDeclHandler (((Parser *)parser)->m_startNamespaceDeclHandler)
  296. #define endNamespaceDeclHandler (((Parser *)parser)->m_endNamespaceDeclHandler)
  297. #define notStandaloneHandler (((Parser *)parser)->m_notStandaloneHandler)
  298. #define externalEntityRefHandler (((Parser *)parser)->m_externalEntityRefHandler)
  299. #define externalEntityRefHandlerArg (((Parser *)parser)->m_externalEntityRefHandlerArg)
  300. #define unknownEncodingHandler (((Parser *)parser)->m_unknownEncodingHandler)
  301. #define initEncoding (((Parser *)parser)->m_initEncoding)
  302. #define internalEncoding (((Parser *)parser)->m_internalEncoding)
  303. #define unknownEncodingMem (((Parser *)parser)->m_unknownEncodingMem)
  304. #define unknownEncodingData (((Parser *)parser)->m_unknownEncodingData)
  305. #define unknownEncodingHandlerData \
  306. (((Parser *)parser)->m_unknownEncodingHandlerData)
  307. #define unknownEncodingRelease (((Parser *)parser)->m_unknownEncodingRelease)
  308. #define protocolEncodingName (((Parser *)parser)->m_protocolEncodingName)
  309. #define ns (((Parser *)parser)->m_ns)
  310. #define prologState (((Parser *)parser)->m_prologState)
  311. #define processor (((Parser *)parser)->m_processor)
  312. #define errorCode (((Parser *)parser)->m_errorCode)
  313. #define errorString (((Parser *)parser)->m_errorString)
  314. #define eventPtr (((Parser *)parser)->m_eventPtr)
  315. #define eventEndPtr (((Parser *)parser)->m_eventEndPtr)
  316. #define positionPtr (((Parser *)parser)->m_positionPtr)
  317. #define position (((Parser *)parser)->m_position)
  318. #define openInternalEntities (((Parser *)parser)->m_openInternalEntities)
  319. #define defaultExpandInternalEntities (((Parser *)parser)->m_defaultExpandInternalEntities)
  320. #define tagLevel (((Parser *)parser)->m_tagLevel)
  321. #define buffer (((Parser *)parser)->m_buffer)
  322. #define bufferPtr (((Parser *)parser)->m_bufferPtr)
  323. #define bufferEnd (((Parser *)parser)->m_bufferEnd)
  324. #define parseEndByteIndex (((Parser *)parser)->m_parseEndByteIndex)
  325. #define parseEndPtr (((Parser *)parser)->m_parseEndPtr)
  326. #define bufferLim (((Parser *)parser)->m_bufferLim)
  327. #define dataBuf (((Parser *)parser)->m_dataBuf)
  328. #define dataBufEnd (((Parser *)parser)->m_dataBufEnd)
  329. #define dtd (((Parser *)parser)->m_dtd)
  330. #define curBase (((Parser *)parser)->m_curBase)
  331. #define declEntity (((Parser *)parser)->m_declEntity)
  332. #define declNotationName (((Parser *)parser)->m_declNotationName)
  333. #define declNotationPublicId (((Parser *)parser)->m_declNotationPublicId)
  334. #define declElementType (((Parser *)parser)->m_declElementType)
  335. #define declAttributeId (((Parser *)parser)->m_declAttributeId)
  336. #define declAttributeIsCdata (((Parser *)parser)->m_declAttributeIsCdata)
  337. #define declAttributeIsId (((Parser *)parser)->m_declAttributeIsId)
  338. #define freeTagList (((Parser *)parser)->m_freeTagList)
  339. #define freeBindingList (((Parser *)parser)->m_freeBindingList)
  340. #define inheritedBindings (((Parser *)parser)->m_inheritedBindings)
  341. #define tagStack (((Parser *)parser)->m_tagStack)
  342. #define atts (((Parser *)parser)->m_atts)
  343. #define attsSize (((Parser *)parser)->m_attsSize)
  344. #define nSpecifiedAtts (((Parser *)parser)->m_nSpecifiedAtts)
  345. #define idAttIndex (((Parser *)parser)->m_idAttIndex)
  346. #define tempPool (((Parser *)parser)->m_tempPool)
  347. #define temp2Pool (((Parser *)parser)->m_temp2Pool)
  348. #define groupConnector (((Parser *)parser)->m_groupConnector)
  349. #define groupSize (((Parser *)parser)->m_groupSize)
  350. #define hadExternalDoctype (((Parser *)parser)->m_hadExternalDoctype)
  351. #define namespaceSeparator (((Parser *)parser)->m_namespaceSeparator)
  352. #define parentParser (((Parser *)parser)->m_parentParser)
  353. #define paramEntityParsing (((Parser *)parser)->m_paramEntityParsing)
  354. static
  355. void poolInit(STRING_POOL *pool)
  356. {
  357. pool->blocks = 0;
  358. pool->freeBlocks = 0;
  359. pool->start = 0;
  360. pool->ptr = 0;
  361. pool->end = 0;
  362. }
  363. static
  364. void poolClear(STRING_POOL *pool)
  365. {
  366. if (!pool->freeBlocks)
  367. pool->freeBlocks = pool->blocks;
  368. else {
  369. BLOCK *p = pool->blocks;
  370. while (p) {
  371. BLOCK *tem = p->next;
  372. p->next = pool->freeBlocks;
  373. pool->freeBlocks = p;
  374. p = tem;
  375. }
  376. }
  377. pool->blocks = 0;
  378. pool->start = 0;
  379. pool->ptr = 0;
  380. pool->end = 0;
  381. }
  382. static
  383. void poolDestroy(STRING_POOL *pool)
  384. {
  385. BLOCK *p = pool->blocks;
  386. while (p) {
  387. BLOCK *tem = p->next;
  388. free(p);
  389. p = tem;
  390. }
  391. pool->blocks = 0;
  392. p = pool->freeBlocks;
  393. while (p) {
  394. BLOCK *tem = p->next;
  395. free(p);
  396. p = tem;
  397. }
  398. pool->freeBlocks = 0;
  399. pool->ptr = 0;
  400. pool->start = 0;
  401. pool->end = 0;
  402. }
  403. static
  404. int poolGrow(STRING_POOL *pool)
  405. {
  406. if (pool->freeBlocks) {
  407. if (pool->start == 0) {
  408. pool->blocks = pool->freeBlocks;
  409. pool->freeBlocks = pool->freeBlocks->next;
  410. pool->blocks->next = 0;
  411. pool->start = pool->blocks->s;
  412. pool->end = pool->start + pool->blocks->size;
  413. pool->ptr = pool->start;
  414. return 1;
  415. }
  416. if (pool->end - pool->start < pool->freeBlocks->size) {
  417. BLOCK *tem = pool->freeBlocks->next;
  418. pool->freeBlocks->next = pool->blocks;
  419. pool->blocks = pool->freeBlocks;
  420. pool->freeBlocks = tem;
  421. memcpy(pool->blocks->s, pool->start,
  422. (pool->end - pool->start) * sizeof(XML_Char));
  423. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  424. pool->start = pool->blocks->s;
  425. pool->end = pool->start + pool->blocks->size;
  426. return 1;
  427. }
  428. }
  429. if (pool->blocks && pool->start == pool->blocks->s) {
  430. size_t const blockSize = (pool->end - pool->start)*2;
  431. pool->blocks = realloc(pool->blocks, offsetof(BLOCK, s) +
  432. blockSize * sizeof(XML_Char));
  433. if (!pool->blocks)
  434. return 0;
  435. pool->blocks->size = blockSize;
  436. pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
  437. pool->start = pool->blocks->s;
  438. pool->end = pool->start + blockSize;
  439. }
  440. else {
  441. size_t const poolLen = pool->end - pool->start;
  442. size_t const blockSize =
  443. poolLen < INIT_BLOCK_SIZE ? INIT_BLOCK_SIZE : poolLen * 2;
  444. BLOCK *tem;
  445. tem = malloc(offsetof(BLOCK, s) + blockSize * sizeof(XML_Char));
  446. if (!tem)
  447. return 0;
  448. tem->size = blockSize;
  449. tem->next = pool->blocks;
  450. pool->blocks = tem;
  451. if (pool->ptr != pool->start)
  452. memcpy(tem->s, pool->start,
  453. (pool->ptr - pool->start) * sizeof(XML_Char));
  454. pool->ptr = tem->s + (pool->ptr - pool->start);
  455. pool->start = tem->s;
  456. pool->end = tem->s + blockSize;
  457. }
  458. return 1;
  459. }
  460. static
  461. XML_Char *poolAppend(STRING_POOL *pool, const ENCODING *enc,
  462. const char *ptr, const char *end)
  463. {
  464. if (!pool->ptr && !poolGrow(pool))
  465. return 0;
  466. for (;;) {
  467. XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
  468. if (ptr == end)
  469. break;
  470. if (!poolGrow(pool))
  471. return 0;
  472. }
  473. return pool->start;
  474. }
  475. static const XML_Char *poolCopyString(STRING_POOL *pool, const XML_Char *s)
  476. {
  477. do {
  478. if (!poolAppendChar(pool, *s))
  479. return 0;
  480. } while (*s++);
  481. s = pool->start;
  482. poolFinish(pool);
  483. return s;
  484. }
  485. static const XML_Char *
  486. poolCopyStringN(STRING_POOL *pool,
  487. const XML_Char *s,
  488. int n)
  489. {
  490. if (!pool->ptr && !poolGrow(pool))
  491. return 0;
  492. for (; n > 0; --n, s++) {
  493. if (!poolAppendChar(pool, *s))
  494. return 0;
  495. }
  496. s = pool->start;
  497. poolFinish(pool);
  498. return s;
  499. }
  500. static
  501. XML_Char *poolStoreString(STRING_POOL *pool, const ENCODING *enc,
  502. const char *ptr, const char *end)
  503. {
  504. if (!poolAppend(pool, enc, ptr, end))
  505. return 0;
  506. if (pool->ptr == pool->end && !poolGrow(pool))
  507. return 0;
  508. *(pool->ptr)++ = 0;
  509. return pool->start;
  510. }
  511. #define INIT_SIZE 64
  512. static
  513. int keyeq(KEY s1, KEY s2)
  514. {
  515. for (; *s1 == *s2; s1++, s2++)
  516. if (*s1 == 0)
  517. return 1;
  518. return 0;
  519. }
  520. static
  521. unsigned long hash(KEY s)
  522. {
  523. unsigned long h = 0;
  524. while (*s)
  525. h = (h << 5) + h + (unsigned char)*s++;
  526. return h;
  527. }
  528. static
  529. NAMED *lookup(HASH_TABLE *table, KEY name, size_t createSize)
  530. {
  531. size_t i;
  532. if (table->size == 0) {
  533. if (!createSize)
  534. return 0;
  535. table->v = calloc(INIT_SIZE, sizeof(NAMED *));
  536. if (!table->v)
  537. return 0;
  538. table->size = INIT_SIZE;
  539. table->usedLim = INIT_SIZE / 2;
  540. i = hash(name) & (table->size - 1);
  541. }
  542. else {
  543. unsigned long h = hash(name);
  544. for (i = h & (table->size - 1);
  545. table->v[i];
  546. i == 0 ? i = table->size - 1 : --i) {
  547. if (keyeq(name, table->v[i]->name))
  548. return table->v[i];
  549. }
  550. if (!createSize)
  551. return 0;
  552. if (table->used == table->usedLim) {
  553. /* check for overflow */
  554. size_t newSize = table->size * 2;
  555. NAMED **newV = calloc(newSize, sizeof(NAMED *));
  556. if (!newV)
  557. return 0;
  558. for (i = 0; i < table->size; i++)
  559. if (table->v[i]) {
  560. size_t j;
  561. for (j = hash(table->v[i]->name) & (newSize - 1);
  562. newV[j];
  563. j == 0 ? j = newSize - 1 : --j)
  564. ;
  565. newV[j] = table->v[i];
  566. }
  567. free(table->v);
  568. table->v = newV;
  569. table->size = newSize;
  570. table->usedLim = newSize/2;
  571. for (i = h & (table->size - 1);
  572. table->v[i];
  573. i == 0 ? i = table->size - 1 : --i)
  574. ;
  575. }
  576. }
  577. table->v[i] = calloc(1, createSize);
  578. if (!table->v[i])
  579. return 0;
  580. table->v[i]->name = name;
  581. (table->used)++;
  582. return table->v[i];
  583. }
  584. static
  585. void hashTableDestroy(HASH_TABLE *table)
  586. {
  587. size_t i;
  588. for (i = 0; i < table->size; i++) {
  589. NAMED *p = table->v[i];
  590. if (p)
  591. free(p);
  592. }
  593. if (table->v)
  594. free(table->v);
  595. }
  596. static
  597. void hashTableInit(HASH_TABLE *p)
  598. {
  599. p->size = 0;
  600. p->usedLim = 0;
  601. p->used = 0;
  602. p->v = 0;
  603. }
  604. static
  605. void hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
  606. {
  607. iter->p = table->v;
  608. iter->end = iter->p + table->size;
  609. }
  610. static
  611. NAMED *hashTableIterNext(HASH_TABLE_ITER *iter)
  612. {
  613. while (iter->p != iter->end) {
  614. NAMED *tem = *(iter->p)++;
  615. if (tem)
  616. return tem;
  617. }
  618. return 0;
  619. }
  620. static int dtdInit(DTD *p)
  621. {
  622. poolInit(&(p->pool));
  623. hashTableInit(&(p->generalEntities));
  624. hashTableInit(&(p->elementTypes));
  625. hashTableInit(&(p->attributeIds));
  626. hashTableInit(&(p->prefixes));
  627. p->complete = 1;
  628. p->standalone = 0;
  629. hashTableInit(&(p->paramEntities));
  630. p->defaultPrefix.name = 0;
  631. p->defaultPrefix.binding = 0;
  632. return 1;
  633. }
  634. static void dtdSwap(DTD *p1, DTD *p2)
  635. {
  636. DTD tem;
  637. memcpy(&tem, p1, sizeof(DTD));
  638. memcpy(p1, p2, sizeof(DTD));
  639. memcpy(p2, &tem, sizeof(DTD));
  640. }
  641. static void dtdDestroy(DTD *p)
  642. {
  643. HASH_TABLE_ITER iter;
  644. hashTableIterInit(&iter, &(p->elementTypes));
  645. for (;;) {
  646. ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  647. if (!e)
  648. break;
  649. if (e->allocDefaultAtts != 0)
  650. free(e->defaultAtts);
  651. }
  652. hashTableDestroy(&(p->generalEntities));
  653. hashTableDestroy(&(p->paramEntities));
  654. hashTableDestroy(&(p->elementTypes));
  655. hashTableDestroy(&(p->attributeIds));
  656. hashTableDestroy(&(p->prefixes));
  657. poolDestroy(&(p->pool));
  658. }
  659. static int copyEntityTable(HASH_TABLE *newTable,
  660. STRING_POOL *newPool,
  661. const HASH_TABLE *oldTable)
  662. {
  663. HASH_TABLE_ITER iter;
  664. const XML_Char *cachedOldBase = 0;
  665. const XML_Char *cachedNewBase = 0;
  666. hashTableIterInit(&iter, oldTable);
  667. for (;;) {
  668. ENTITY *newE;
  669. const XML_Char *name;
  670. const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
  671. if (!oldE)
  672. break;
  673. name = poolCopyString(newPool, oldE->name);
  674. if (!name)
  675. return 0;
  676. newE = (ENTITY *)lookup(newTable, name, sizeof(ENTITY));
  677. if (!newE)
  678. return 0;
  679. if (oldE->systemId) {
  680. const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
  681. if (!tem)
  682. return 0;
  683. newE->systemId = tem;
  684. if (oldE->base) {
  685. if (oldE->base == cachedOldBase)
  686. newE->base = cachedNewBase;
  687. else {
  688. cachedOldBase = oldE->base;
  689. tem = poolCopyString(newPool, cachedOldBase);
  690. if (!tem)
  691. return 0;
  692. cachedNewBase = newE->base = tem;
  693. }
  694. }
  695. }
  696. else {
  697. const XML_Char *tem =
  698. poolCopyStringN(newPool, oldE->textPtr, oldE->textLen);
  699. if (!tem)
  700. return 0;
  701. newE->textPtr = tem;
  702. newE->textLen = oldE->textLen;
  703. }
  704. if (oldE->notation) {
  705. const XML_Char *tem = poolCopyString(newPool, oldE->notation);
  706. if (!tem)
  707. return 0;
  708. newE->notation = tem;
  709. }
  710. }
  711. return 1;
  712. }
  713. /* Do a deep copy of the DTD. Return 0 for out of memory; non-zero otherwise.
  714. The new DTD has already been initialized. */
  715. static int dtdCopy(DTD *newDtd, const DTD *oldDtd)
  716. {
  717. HASH_TABLE_ITER iter;
  718. /* Copy the prefix table. */
  719. hashTableIterInit(&iter, &(oldDtd->prefixes));
  720. for (;;) {
  721. const XML_Char *name;
  722. const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
  723. if (!oldP)
  724. break;
  725. name = poolCopyString(&(newDtd->pool), oldP->name);
  726. if (!name)
  727. return 0;
  728. if (!lookup(&(newDtd->prefixes), name, sizeof(PREFIX)))
  729. return 0;
  730. }
  731. hashTableIterInit(&iter, &(oldDtd->attributeIds));
  732. /* Copy the attribute id table. */
  733. for (;;) {
  734. ATTRIBUTE_ID *newA;
  735. const XML_Char *name;
  736. const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
  737. if (!oldA)
  738. break;
  739. /* Remember to allocate the scratch byte before the name. */
  740. if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
  741. return 0;
  742. name = poolCopyString(&(newDtd->pool), oldA->name);
  743. if (!name)
  744. return 0;
  745. ++name;
  746. newA = (ATTRIBUTE_ID *)
  747. lookup(&(newDtd->attributeIds), name, sizeof(ATTRIBUTE_ID));
  748. if (!newA)
  749. return 0;
  750. newA->maybeTokenized = oldA->maybeTokenized;
  751. if (oldA->prefix) {
  752. newA->xmlns = oldA->xmlns;
  753. if (oldA->prefix == &oldDtd->defaultPrefix)
  754. newA->prefix = &newDtd->defaultPrefix;
  755. else
  756. newA->prefix = (PREFIX *)
  757. lookup(&(newDtd->prefixes), oldA->prefix->name, 0);
  758. }
  759. }
  760. /* Copy the element type table. */
  761. hashTableIterInit(&iter, &(oldDtd->elementTypes));
  762. for (;;) {
  763. int i;
  764. ELEMENT_TYPE *newE;
  765. const XML_Char *name;
  766. const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
  767. if (!oldE)
  768. break;
  769. name = poolCopyString(&(newDtd->pool), oldE->name);
  770. if (!name)
  771. return 0;
  772. newE = (ELEMENT_TYPE *)
  773. lookup(&(newDtd->elementTypes), name, sizeof(ELEMENT_TYPE));
  774. if (!newE)
  775. return 0;
  776. if (oldE->nDefaultAtts) {
  777. newE->defaultAtts = (DEFAULT_ATTRIBUTE *)
  778. malloc(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
  779. if (!newE->defaultAtts)
  780. return 0;
  781. }
  782. if (oldE->idAtt)
  783. newE->idAtt = (ATTRIBUTE_ID *)
  784. lookup(&(newDtd->attributeIds), oldE->idAtt->name, 0);
  785. newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
  786. if (oldE->prefix)
  787. newE->prefix = (PREFIX *)
  788. lookup(&(newDtd->prefixes), oldE->prefix->name, 0);
  789. for (i = 0; i < newE->nDefaultAtts; i++) {
  790. newE->defaultAtts[i].id = (ATTRIBUTE_ID *)
  791. lookup(&(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
  792. newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
  793. if (oldE->defaultAtts[i].value) {
  794. newE->defaultAtts[i].value =
  795. poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
  796. if (!newE->defaultAtts[i].value)
  797. return 0;
  798. }
  799. else
  800. newE->defaultAtts[i].value = 0;
  801. }
  802. }
  803. /* Copy the entity tables. */
  804. if (!copyEntityTable(&(newDtd->generalEntities),
  805. &(newDtd->pool),
  806. &(oldDtd->generalEntities)))
  807. return 0;
  808. if (!copyEntityTable(&(newDtd->paramEntities),
  809. &(newDtd->pool),
  810. &(oldDtd->paramEntities)))
  811. return 0;
  812. newDtd->complete = oldDtd->complete;
  813. newDtd->standalone = oldDtd->standalone;
  814. return 1;
  815. }
  816. static
  817. int addBinding(XML_Parser parser,
  818. PREFIX *prefix,
  819. const ATTRIBUTE_ID *attId,
  820. const XML_Char *uri,
  821. BINDING **bindingsPtr)
  822. {
  823. BINDING *b;
  824. int len;
  825. for (len = 0; uri[len]; len++)
  826. ;
  827. if (namespaceSeparator)
  828. len++;
  829. if (freeBindingList) {
  830. b = freeBindingList;
  831. if (len > b->uriAlloc) {
  832. b->uri = realloc(b->uri, sizeof(XML_Char) * (len + EXPAND_SPARE));
  833. if (!b->uri)
  834. return 0;
  835. b->uriAlloc = len + EXPAND_SPARE;
  836. }
  837. freeBindingList = b->nextTagBinding;
  838. }
  839. else {
  840. b = malloc(sizeof(BINDING));
  841. if (!b)
  842. return 0;
  843. b->uri = malloc(sizeof(XML_Char) * (len + EXPAND_SPARE));
  844. if (!b->uri) {
  845. free(b);
  846. return 0;
  847. }
  848. b->uriAlloc = len + EXPAND_SPARE;
  849. }
  850. b->uriLen = len;
  851. memcpy(b->uri, uri, len * sizeof(XML_Char));
  852. if (namespaceSeparator)
  853. b->uri[len - 1] = namespaceSeparator;
  854. b->prefix = prefix;
  855. b->attId = attId;
  856. b->prevPrefixBinding = prefix->binding;
  857. if (*uri == XML_T('\0') && prefix == &dtd.defaultPrefix)
  858. prefix->binding = 0;
  859. else
  860. prefix->binding = b;
  861. b->nextTagBinding = *bindingsPtr;
  862. *bindingsPtr = b;
  863. if (startNamespaceDeclHandler)
  864. startNamespaceDeclHandler(handlerArg, prefix->name,
  865. prefix->binding ? uri : 0);
  866. return 1;
  867. }
  868. #define CONTEXT_SEP XML_T('\f')
  869. static
  870. const XML_Char *getContext(XML_Parser parser)
  871. {
  872. HASH_TABLE_ITER iter;
  873. int needSep = 0;
  874. if (dtd.defaultPrefix.binding) {
  875. int i;
  876. int len;
  877. if (!poolAppendChar(&tempPool, XML_T('=')))
  878. return 0;
  879. len = dtd.defaultPrefix.binding->uriLen;
  880. if (namespaceSeparator != XML_T('\0'))
  881. len--;
  882. for (i = 0; i < len; i++)
  883. if (!poolAppendChar(&tempPool, dtd.defaultPrefix.binding->uri[i]))
  884. return 0;
  885. needSep = 1;
  886. }
  887. hashTableIterInit(&iter, &(dtd.prefixes));
  888. for (;;) {
  889. int i;
  890. int len;
  891. const XML_Char *s;
  892. PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
  893. if (!prefix)
  894. break;
  895. if (!prefix->binding)
  896. continue;
  897. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  898. return 0;
  899. for (s = prefix->name; *s; s++)
  900. if (!poolAppendChar(&tempPool, *s))
  901. return 0;
  902. if (!poolAppendChar(&tempPool, XML_T('=')))
  903. return 0;
  904. len = prefix->binding->uriLen;
  905. if (namespaceSeparator != XML_T('\0'))
  906. len--;
  907. for (i = 0; i < len; i++)
  908. if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
  909. return 0;
  910. needSep = 1;
  911. }
  912. hashTableIterInit(&iter, &(dtd.generalEntities));
  913. for (;;) {
  914. const XML_Char *s;
  915. ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
  916. if (!e)
  917. break;
  918. if (!e->open)
  919. continue;
  920. if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
  921. return 0;
  922. for (s = e->name; *s; s++)
  923. if (!poolAppendChar(&tempPool, *s))
  924. return 0;
  925. needSep = 1;
  926. }
  927. if (!poolAppendChar(&tempPool, XML_T('\0')))
  928. return 0;
  929. return tempPool.start;
  930. }
  931. static
  932. int setContext(XML_Parser parser, const XML_Char *context)
  933. {
  934. const XML_Char *s = context;
  935. while (*context != XML_T('\0')) {
  936. if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
  937. ENTITY *e;
  938. if (!poolAppendChar(&tempPool, XML_T('\0')))
  939. return 0;
  940. e = (ENTITY *)lookup(&dtd.generalEntities, poolStart(&tempPool), 0);
  941. if (e)
  942. e->open = 1;
  943. if (*s != XML_T('\0'))
  944. s++;
  945. context = s;
  946. poolDiscard(&tempPool);
  947. }
  948. else if (*s == '=') {
  949. PREFIX *prefix;
  950. if (poolLength(&tempPool) == 0)
  951. prefix = &dtd.defaultPrefix;
  952. else {
  953. if (!poolAppendChar(&tempPool, XML_T('\0')))
  954. return 0;
  955. prefix = (PREFIX *)
  956. lookup(&dtd.prefixes, poolStart(&tempPool), sizeof(PREFIX));
  957. if (!prefix)
  958. return 0;
  959. if (prefix->name == poolStart(&tempPool)) {
  960. prefix->name = poolCopyString(&dtd.pool, prefix->name);
  961. if (!prefix->name)
  962. return 0;
  963. }
  964. poolDiscard(&tempPool);
  965. }
  966. for (context = s + 1;
  967. *context != CONTEXT_SEP && *context != XML_T('\0');
  968. ++context)
  969. if (!poolAppendChar(&tempPool, *context))
  970. return 0;
  971. if (!poolAppendChar(&tempPool, XML_T('\0')))
  972. return 0;
  973. if (!addBinding(parser, prefix, 0, poolStart(&tempPool),
  974. &inheritedBindings))
  975. return 0;
  976. poolDiscard(&tempPool);
  977. if (*context != XML_T('\0'))
  978. ++context;
  979. s = context;
  980. }
  981. else {
  982. if (!poolAppendChar(&tempPool, *s))
  983. return 0;
  984. s++;
  985. }
  986. }
  987. return 1;
  988. }
  989. static void
  990. normalizeLines(XML_Char *s)
  991. {
  992. XML_Char *p;
  993. for (;; s++) {
  994. if (*s == XML_T('\0'))
  995. return;
  996. if (*s == 0xD)
  997. break;
  998. }
  999. p = s;
  1000. do {
  1001. if (*s == 0xD) {
  1002. *p++ = 0xA;
  1003. if (*++s == 0xA)
  1004. s++;
  1005. }
  1006. else
  1007. *p++ = *s++;
  1008. } while (*s);
  1009. *p = XML_T('\0');
  1010. }
  1011. static void
  1012. reportDefault(XML_Parser const xmlParserP,
  1013. const ENCODING * const enc,
  1014. const char * const start,
  1015. const char * const end) {
  1016. Parser * const parser = (Parser *)xmlParserP;
  1017. if (MUST_CONVERT(enc, start)) {
  1018. const char * s;
  1019. const char **eventPP;
  1020. const char **eventEndPP;
  1021. if (enc == parser->m_encoding) {
  1022. eventPP = &eventPtr;
  1023. eventEndPP = &eventEndPtr;
  1024. }
  1025. else {
  1026. eventPP = &(openInternalEntities->internalEventPtr);
  1027. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  1028. }
  1029. s = start;
  1030. do {
  1031. ICHAR *dataPtr = (ICHAR *)dataBuf;
  1032. XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
  1033. *eventEndPP = s;
  1034. {
  1035. size_t const len = dataPtr - (ICHAR *)dataBuf;
  1036. assert((size_t)(int)len == len); /* parser requirement */
  1037. defaultHandler(handlerArg, dataBuf, (int)len);
  1038. }
  1039. *eventPP = s;
  1040. } while (s != end);
  1041. } else {
  1042. size_t const len = (XML_Char *)end - (XML_Char *)start;
  1043. assert((size_t)(int)len == len); /* parser requirement */
  1044. defaultHandler(handlerArg, (XML_Char *)start, len);
  1045. }
  1046. }
  1047. static int
  1048. reportProcessingInstruction(XML_Parser parser,
  1049. const ENCODING *enc,
  1050. const char *start,
  1051. const char *end)
  1052. {
  1053. const XML_Char *target;
  1054. XML_Char *data;
  1055. const char *tem;
  1056. if (!processingInstructionHandler) {
  1057. if (defaultHandler)
  1058. reportDefault(parser, enc, start, end);
  1059. return 1;
  1060. }
  1061. start += enc->minBytesPerChar * 2;
  1062. tem = start + XmlNameLength(enc, start);
  1063. target = poolStoreString(&tempPool, enc, start, tem);
  1064. if (!target)
  1065. return 0;
  1066. poolFinish(&tempPool);
  1067. data = poolStoreString(&tempPool, enc,
  1068. XmlSkipS(enc, tem),
  1069. end - enc->minBytesPerChar*2);
  1070. if (!data)
  1071. return 0;
  1072. normalizeLines(data);
  1073. processingInstructionHandler(handlerArg, target, data);
  1074. poolClear(&tempPool);
  1075. return 1;
  1076. }
  1077. static int
  1078. reportComment(XML_Parser parser,
  1079. const ENCODING *enc,
  1080. const char *start,
  1081. const char *end)
  1082. {
  1083. XML_Char *data;
  1084. if (!commentHandler) {
  1085. if (defaultHandler)
  1086. reportDefault(parser, enc, start, end);
  1087. return 1;
  1088. }
  1089. data = poolStoreString(&tempPool,
  1090. enc,
  1091. start + enc->minBytesPerChar * 4,
  1092. end - enc->minBytesPerChar * 3);
  1093. if (!data)
  1094. return 0;
  1095. normalizeLines(data);
  1096. commentHandler(handlerArg, data);
  1097. poolClear(&tempPool);
  1098. return 1;
  1099. }
  1100. static enum XML_Error
  1101. handleUnknownEncoding(XML_Parser const xmlParserP,
  1102. const XML_Char * const encodingName) {
  1103. Parser * const parser = (Parser *) xmlParserP;
  1104. if (unknownEncodingHandler) {
  1105. XML_Encoding info;
  1106. int i;
  1107. for (i = 0; i < 256; i++)
  1108. info.map[i] = -1;
  1109. info.convert = 0;
  1110. info.data = 0;
  1111. info.release = 0;
  1112. if (unknownEncodingHandler(unknownEncodingHandlerData,
  1113. encodingName, &info)) {
  1114. ENCODING *enc;
  1115. unknownEncodingMem = malloc(xmlrpc_XmlSizeOfUnknownEncoding());
  1116. if (!unknownEncodingMem) {
  1117. if (info.release)
  1118. info.release(info.data);
  1119. return XML_ERROR_NO_MEMORY;
  1120. }
  1121. enc = (ns
  1122. ? xmlrpc_XmlInitUnknownEncodingNS
  1123. : xmlrpc_XmlInitUnknownEncoding)(unknownEncodingMem,
  1124. info.map,
  1125. info.convert,
  1126. info.data);
  1127. if (enc) {
  1128. unknownEncodingData = info.data;
  1129. unknownEncodingRelease = info.release;
  1130. parser->m_encoding = enc;
  1131. return XML_ERROR_NONE;
  1132. }
  1133. }
  1134. if (info.release)
  1135. info.release(info.data);
  1136. }
  1137. return XML_ERROR_UNKNOWN_ENCODING;
  1138. }
  1139. static enum XML_Error
  1140. initializeEncoding(XML_Parser const xmlParserP) {
  1141. Parser * const parser = (Parser *) xmlParserP;
  1142. const char *s;
  1143. #ifdef XML_UNICODE
  1144. char encodingBuf[128];
  1145. if (!protocolEncodingName)
  1146. s = 0;
  1147. else {
  1148. int i;
  1149. for (i = 0; protocolEncodingName[i]; i++) {
  1150. if (i == sizeof(encodingBuf) - 1
  1151. || (protocolEncodingName[i] & ~0x7f) != 0) {
  1152. encodingBuf[0] = '\0';
  1153. break;
  1154. }
  1155. encodingBuf[i] = (char)protocolEncodingName[i];
  1156. }
  1157. encodingBuf[i] = '\0';
  1158. s = encodingBuf;
  1159. }
  1160. #else
  1161. s = protocolEncodingName;
  1162. #endif
  1163. if ((ns ? xmlrpc_XmlInitEncodingNS : xmlrpc_XmlInitEncoding)(
  1164. &parser->m_initEncoding, &parser->m_encoding, s))
  1165. return XML_ERROR_NONE;
  1166. return handleUnknownEncoding(xmlParserP, protocolEncodingName);
  1167. }
  1168. static enum XML_Error
  1169. processXmlDecl(XML_Parser const xmlParserP,
  1170. int const isGeneralTextEntity,
  1171. const char * const s,
  1172. const char * const next) {
  1173. Parser * const parser = (Parser *) xmlParserP;
  1174. const char *encodingName = 0;
  1175. const ENCODING *newEncoding = 0;
  1176. const char *version;
  1177. int standalone = -1;
  1178. if (!(ns
  1179. ? xmlrpc_XmlParseXmlDeclNS
  1180. : xmlrpc_XmlParseXmlDecl)(isGeneralTextEntity,
  1181. parser->m_encoding,
  1182. s,
  1183. next,
  1184. &eventPtr,
  1185. &version,
  1186. &encodingName,
  1187. &newEncoding,
  1188. &standalone))
  1189. return XML_ERROR_SYNTAX;
  1190. if (!isGeneralTextEntity && standalone == 1) {
  1191. dtd.standalone = 1;
  1192. if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
  1193. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  1194. }
  1195. if (defaultHandler)
  1196. reportDefault(xmlParserP, parser->m_encoding, s, next);
  1197. if (!protocolEncodingName) {
  1198. if (newEncoding) {
  1199. if (newEncoding->minBytesPerChar !=
  1200. parser->m_encoding->minBytesPerChar) {
  1201. eventPtr = encodingName;
  1202. return XML_ERROR_INCORRECT_ENCODING;
  1203. }
  1204. parser->m_encoding = newEncoding;
  1205. }
  1206. else if (encodingName) {
  1207. enum XML_Error result;
  1208. const XML_Char * s =
  1209. poolStoreString(&tempPool,
  1210. parser->m_encoding,
  1211. encodingName,
  1212. encodingName
  1213. + XmlNameLength(parser->m_encoding,
  1214. encodingName));
  1215. if (!s)
  1216. return XML_ERROR_NO_MEMORY;
  1217. result = handleUnknownEncoding(xmlParserP, s);
  1218. poolDiscard(&tempPool);
  1219. if (result == XML_ERROR_UNKNOWN_ENCODING)
  1220. eventPtr = encodingName;
  1221. return result;
  1222. }
  1223. }
  1224. return XML_ERROR_NONE;
  1225. }
  1226. static ATTRIBUTE_ID *
  1227. getAttributeId(XML_Parser parser,
  1228. const ENCODING *enc,
  1229. const char *start,
  1230. const char *end)
  1231. {
  1232. ATTRIBUTE_ID *id;
  1233. const XML_Char *name;
  1234. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  1235. return 0;
  1236. name = poolStoreString(&dtd.pool, enc, start, end);
  1237. if (!name)
  1238. return 0;
  1239. ++name;
  1240. id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, name, sizeof(ATTRIBUTE_ID));
  1241. if (!id)
  1242. return 0;
  1243. if (id->name != name)
  1244. poolDiscard(&dtd.pool);
  1245. else {
  1246. poolFinish(&dtd.pool);
  1247. if (!ns)
  1248. ;
  1249. else if (name[0] == 'x'
  1250. && name[1] == 'm'
  1251. && name[2] == 'l'
  1252. && name[3] == 'n'
  1253. && name[4] == 's'
  1254. && (name[5] == XML_T('\0') || name[5] == XML_T(':'))) {
  1255. if (name[5] == '\0')
  1256. id->prefix = &dtd.defaultPrefix;
  1257. else
  1258. id->prefix = (PREFIX *)lookup(&dtd.prefixes, name + 6, sizeof(PREFIX));
  1259. id->xmlns = 1;
  1260. }
  1261. else {
  1262. int i;
  1263. for (i = 0; name[i]; i++) {
  1264. if (name[i] == XML_T(':')) {
  1265. int j;
  1266. for (j = 0; j < i; j++) {
  1267. if (!poolAppendChar(&dtd.pool, name[j]))
  1268. return 0;
  1269. }
  1270. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  1271. return 0;
  1272. id->prefix = (PREFIX *)
  1273. lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
  1274. if (id->prefix->name == poolStart(&dtd.pool))
  1275. poolFinish(&dtd.pool);
  1276. else
  1277. poolDiscard(&dtd.pool);
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. return id;
  1284. }
  1285. static
  1286. void normalizePublicId(XML_Char *publicId)
  1287. {
  1288. XML_Char *p = publicId;
  1289. XML_Char *s;
  1290. for (s = publicId; *s; s++) {
  1291. switch (*s) {
  1292. case 0x20:
  1293. case 0xD:
  1294. case 0xA:
  1295. if (p != publicId && p[-1] != 0x20)
  1296. *p++ = 0x20;
  1297. break;
  1298. default:
  1299. *p++ = *s;
  1300. }
  1301. }
  1302. if (p != publicId && p[-1] == 0x20)
  1303. --p;
  1304. *p = XML_T('\0');
  1305. }
  1306. static int setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
  1307. {
  1308. const XML_Char *name;
  1309. for (name = elementType->name; *name; name++) {
  1310. if (*name == XML_T(':')) {
  1311. PREFIX *prefix;
  1312. const XML_Char *s;
  1313. for (s = elementType->name; s != name; s++) {
  1314. if (!poolAppendChar(&dtd.pool, *s))
  1315. return 0;
  1316. }
  1317. if (!poolAppendChar(&dtd.pool, XML_T('\0')))
  1318. return 0;
  1319. prefix = (PREFIX *)
  1320. lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
  1321. if (!prefix)
  1322. return 0;
  1323. if (prefix->name == poolStart(&dtd.pool))
  1324. poolFinish(&dtd.pool);
  1325. else
  1326. poolDiscard(&dtd.pool);
  1327. elementType->prefix = prefix;
  1328. }
  1329. }
  1330. return 1;
  1331. }
  1332. static enum XML_Error
  1333. appendAttributeValue(XML_Parser const xmlParserP,
  1334. const ENCODING * const enc,
  1335. int const isCdata,
  1336. const char * const ptrArg,
  1337. const char * const end,
  1338. STRING_POOL * const pool) {
  1339. Parser * const parser = (Parser *) xmlParserP;
  1340. const char * ptr;
  1341. ptr = ptrArg;
  1342. for (;;) {
  1343. const char *next;
  1344. int tok = XmlAttributeValueTok(enc, ptr, end, &next);
  1345. switch (tok) {
  1346. case XML_TOK_NONE:
  1347. return XML_ERROR_NONE;
  1348. case XML_TOK_INVALID:
  1349. if (enc == parser->m_encoding)
  1350. eventPtr = next;
  1351. return XML_ERROR_INVALID_TOKEN;
  1352. case XML_TOK_PARTIAL:
  1353. if (enc == parser->m_encoding)
  1354. eventPtr = ptr;
  1355. return XML_ERROR_INVALID_TOKEN;
  1356. case XML_TOK_CHAR_REF:
  1357. {
  1358. XML_Char buf[XML_ENCODE_MAX];
  1359. int i;
  1360. int n = XmlCharRefNumber(enc, ptr);
  1361. if (n < 0) {
  1362. if (enc == parser->m_encoding)
  1363. eventPtr = ptr;
  1364. return XML_ERROR_BAD_CHAR_REF;
  1365. }
  1366. if (!isCdata
  1367. && n == 0x20 /* space */
  1368. && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  1369. break;
  1370. n = XmlEncode(n, (ICHAR *)buf);
  1371. if (!n) {
  1372. if (enc == parser->m_encoding)
  1373. eventPtr = ptr;
  1374. return XML_ERROR_BAD_CHAR_REF;
  1375. }
  1376. for (i = 0; i < n; i++) {
  1377. if (!poolAppendChar(pool, buf[i]))
  1378. return XML_ERROR_NO_MEMORY;
  1379. }
  1380. }
  1381. break;
  1382. case XML_TOK_DATA_CHARS:
  1383. if (!poolAppend(pool, enc, ptr, next))
  1384. return XML_ERROR_NO_MEMORY;
  1385. break;
  1386. break;
  1387. case XML_TOK_TRAILING_CR:
  1388. next = ptr + enc->minBytesPerChar;
  1389. /* fall through */
  1390. case XML_TOK_ATTRIBUTE_VALUE_S:
  1391. case XML_TOK_DATA_NEWLINE:
  1392. if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
  1393. break;
  1394. if (!poolAppendChar(pool, 0x20))
  1395. return XML_ERROR_NO_MEMORY;
  1396. break;
  1397. case XML_TOK_ENTITY_REF:
  1398. {
  1399. const XML_Char *name;
  1400. ENTITY *entity;
  1401. XML_Char ch = XmlPredefinedEntityName(enc,
  1402. ptr + enc->minBytesPerChar,
  1403. next - enc->minBytesPerChar);
  1404. if (ch) {
  1405. if (!poolAppendChar(pool, ch))
  1406. return XML_ERROR_NO_MEMORY;
  1407. break;
  1408. }
  1409. name = poolStoreString(&temp2Pool, enc,
  1410. ptr + enc->minBytesPerChar,
  1411. next - enc->minBytesPerChar);
  1412. if (!name)
  1413. return XML_ERROR_NO_MEMORY;
  1414. entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
  1415. poolDiscard(&temp2Pool);
  1416. if (!entity) {
  1417. if (dtd.complete) {
  1418. if (enc == parser->m_encoding)
  1419. eventPtr = ptr;
  1420. return XML_ERROR_UNDEFINED_ENTITY;
  1421. }
  1422. }
  1423. else if (entity->open) {
  1424. if (enc == parser->m_encoding)
  1425. eventPtr = ptr;
  1426. return XML_ERROR_RECURSIVE_ENTITY_REF;
  1427. }
  1428. else if (entity->notation) {
  1429. if (enc == parser->m_encoding)
  1430. eventPtr = ptr;
  1431. return XML_ERROR_BINARY_ENTITY_REF;
  1432. }
  1433. else if (!entity->textPtr) {
  1434. if (enc == parser->m_encoding)
  1435. eventPtr = ptr;
  1436. return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
  1437. }
  1438. else {
  1439. enum XML_Error result;
  1440. const XML_Char *textEnd = entity->textPtr + entity->textLen;
  1441. entity->open = 1;
  1442. result = appendAttributeValue(xmlParserP, internalEncoding,
  1443. isCdata, (char *)entity->textPtr,
  1444. (char *)textEnd, pool);
  1445. entity->open = 0;
  1446. if (result)
  1447. return result;
  1448. }
  1449. }
  1450. break;
  1451. default:
  1452. abort();
  1453. }
  1454. ptr = next;
  1455. }
  1456. /* not reached */
  1457. }
  1458. static enum XML_Error
  1459. storeAttributeValue(XML_Parser parser, const ENCODING *enc, int isCdata,
  1460. const char *ptr, const char *end,
  1461. STRING_POOL *pool)
  1462. {
  1463. enum XML_Error result =
  1464. appendAttributeValue(parser, enc, isCdata, ptr, end, pool);
  1465. if (result)
  1466. return result;
  1467. if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
  1468. poolChop(pool);
  1469. if (!poolAppendChar(pool, XML_T('\0')))
  1470. return XML_ERROR_NO_MEMORY;
  1471. return XML_ERROR_NONE;
  1472. }
  1473. static
  1474. enum XML_Error
  1475. storeEntityValue(XML_Parser const xmlParserP,
  1476. const ENCODING * const enc,
  1477. const char * const entityTextPtrArg,
  1478. const char * const entityTextEnd) {
  1479. Parser * const parser = (Parser *) xmlParserP;
  1480. STRING_POOL * const pool = &(dtd.pool);
  1481. const char * entityTextPtr;
  1482. entityTextPtr = entityTextPtrArg;
  1483. for (;;) {
  1484. const char * next;
  1485. int tok;
  1486. tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
  1487. switch (tok) {
  1488. case XML_TOK_PARAM_ENTITY_REF:
  1489. if (parentParser || enc != parser->m_encoding) {
  1490. enum XML_Error result;
  1491. const XML_Char *name;
  1492. ENTITY *entity;
  1493. name = poolStoreString(&tempPool, enc,
  1494. entityTextPtr + enc->minBytesPerChar,
  1495. next - enc->minBytesPerChar);
  1496. if (!name)
  1497. return XML_ERROR_NO_MEMORY;
  1498. entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
  1499. poolDiscard(&tempPool);
  1500. if (!entity) {
  1501. if (enc == parser->m_encoding)
  1502. eventPtr = entityTextPtr;
  1503. return XML_ERROR_UNDEFINED_ENTITY;
  1504. }
  1505. if (entity->open) {
  1506. if (enc == parser->m_encoding)
  1507. eventPtr = entityTextPtr;
  1508. return XML_ERROR_RECURSIVE_ENTITY_REF;
  1509. }
  1510. if (entity->systemId) {
  1511. if (enc == parser->m_encoding)
  1512. eventPtr = entityTextPtr;
  1513. return XML_ERROR_PARAM_ENTITY_REF;
  1514. }
  1515. entity->open = 1;
  1516. result = storeEntityValue(parser,
  1517. internalEncoding,
  1518. (char *)entity->textPtr,
  1519. (char *)(entity->textPtr + entity->textLen));
  1520. entity->open = 0;
  1521. if (result)
  1522. return result;
  1523. break;
  1524. }
  1525. eventPtr = entityTextPtr;
  1526. return XML_ERROR_SYNTAX;
  1527. case XML_TOK_NONE:
  1528. return XML_ERROR_NONE;
  1529. case XML_TOK_ENTITY_REF:
  1530. case XML_TOK_DATA_CHARS:
  1531. if (!poolAppend(pool, enc, entityTextPtr, next))
  1532. return XML_ERROR_NO_MEMORY;
  1533. break;
  1534. case XML_TOK_TRAILING_CR:
  1535. next = entityTextPtr + enc->minBytesPerChar;
  1536. /* fall through */
  1537. case XML_TOK_DATA_NEWLINE:
  1538. if (pool->end == pool->ptr && !poolGrow(pool))
  1539. return XML_ERROR_NO_MEMORY;
  1540. *(pool->ptr)++ = 0xA;
  1541. break;
  1542. case XML_TOK_CHAR_REF:
  1543. {
  1544. XML_Char buf[XML_ENCODE_MAX];
  1545. int i;
  1546. int n = XmlCharRefNumber(enc, entityTextPtr);
  1547. if (n < 0) {
  1548. if (enc == parser->m_encoding)
  1549. eventPtr = entityTextPtr;
  1550. return XML_ERROR_BAD_CHAR_REF;
  1551. }
  1552. n = XmlEncode(n, (ICHAR *)buf);
  1553. if (!n) {
  1554. if (enc == parser->m_encoding)
  1555. eventPtr = entityTextPtr;
  1556. return XML_ERROR_BAD_CHAR_REF;
  1557. }
  1558. for (i = 0; i < n; i++) {
  1559. if (pool->end == pool->ptr && !poolGrow(pool))
  1560. return XML_ERROR_NO_MEMORY;
  1561. *(pool->ptr)++ = buf[i];
  1562. }
  1563. }
  1564. break;
  1565. case XML_TOK_PARTIAL:
  1566. if (enc == parser->m_encoding)
  1567. eventPtr = entityTextPtr;
  1568. return XML_ERROR_INVALID_TOKEN;
  1569. case XML_TOK_INVALID:
  1570. if (enc == parser->m_encoding)
  1571. eventPtr = next;
  1572. return XML_ERROR_INVALID_TOKEN;
  1573. default:
  1574. abort();
  1575. }
  1576. entityTextPtr = next;
  1577. }
  1578. /* not reached */
  1579. }
  1580. static int
  1581. defineAttribute(ELEMENT_TYPE *type,
  1582. ATTRIBUTE_ID *attId,
  1583. int isCdata,
  1584. int isId,
  1585. const XML_Char *value)
  1586. {
  1587. DEFAULT_ATTRIBUTE *att;
  1588. if (value || isId) {
  1589. /* The handling of default attributes gets messed up if we have
  1590. a default which duplicates a non-default. */
  1591. int i;
  1592. for (i = 0; i < type->nDefaultAtts; i++)
  1593. if (attId == type->defaultAtts[i].id)
  1594. return 1;
  1595. if (isId && !type->idAtt && !attId->xmlns)
  1596. type->idAtt = attId;
  1597. }
  1598. if (type->nDefaultAtts == type->allocDefaultAtts) {
  1599. if (type->allocDefaultAtts == 0) {
  1600. type->allocDefaultAtts = 8;
  1601. type->defaultAtts =
  1602. malloc(type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
  1603. }
  1604. else {
  1605. type->allocDefaultAtts *= 2;
  1606. type->defaultAtts =
  1607. realloc(type->defaultAtts,
  1608. type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
  1609. }
  1610. if (!type->defaultAtts)
  1611. return 0;
  1612. }
  1613. att = type->defaultAtts + type->nDefaultAtts;
  1614. att->id = attId;
  1615. att->value = value;
  1616. att->isCdata = isCdata;
  1617. if (!isCdata)
  1618. attId->maybeTokenized = 1;
  1619. type->nDefaultAtts += 1;
  1620. return 1;
  1621. }
  1622. /* If tagNamePtr is non-null, build a real list of attributes,
  1623. otherwise just check the attributes for well-formedness. */
  1624. static enum XML_Error
  1625. storeAtts(XML_Parser const xmlParserP,
  1626. const ENCODING * const enc,
  1627. const char * const attStr,
  1628. TAG_NAME * const tagNamePtr,
  1629. BINDING ** const bindingsPtr) {
  1630. Parser * const parser = (Parser *)xmlParserP;
  1631. ELEMENT_TYPE *elementType = 0;
  1632. int nDefaultAtts = 0;
  1633. const XML_Char ** appAtts;
  1634. /* the attribute list to pass to the application */
  1635. int attIndex = 0;
  1636. int i;
  1637. int n;
  1638. int nPrefixes = 0;
  1639. BINDING *binding;
  1640. const XML_Char *localPart;
  1641. /* lookup the element type name */
  1642. if (tagNamePtr) {
  1643. elementType = (ELEMENT_TYPE *)
  1644. lookup(&dtd.elementTypes, tagNamePtr->str, 0);
  1645. if (!elementType) {
  1646. tagNamePtr->str = poolCopyString(&dtd.pool, tagNamePtr->str);
  1647. if (!tagNamePtr->str)
  1648. return XML_ERROR_NO_MEMORY;
  1649. elementType = (ELEMENT_TYPE *)
  1650. lookup(&dtd.elementTypes, tagNamePtr->str, sizeof(ELEMENT_TYPE));
  1651. if (!elementType)
  1652. return XML_ERROR_NO_MEMORY;
  1653. if (ns && !setElementTypePrefix(xmlParserP, elementType))
  1654. return XML_ERROR_NO_MEMORY;
  1655. }
  1656. nDefaultAtts = elementType->nDefaultAtts;
  1657. }
  1658. /* get the attributes from the tokenizer */
  1659. n = XmlGetAttributes(enc, attStr, attsSize, atts);
  1660. if (n + nDefaultAtts > attsSize) {
  1661. int oldAttsSize = attsSize;
  1662. attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
  1663. atts = realloc((void *)atts, attsSize * sizeof(ATTRIBUTE));
  1664. if (!atts)
  1665. return XML_ERROR_NO_MEMORY;
  1666. if (n > oldAttsSize)
  1667. XmlGetAttributes(enc, attStr, n, atts);
  1668. }
  1669. appAtts = (const XML_Char **)atts;
  1670. for (i = 0; i < n; i++) {
  1671. /* add the name and value to the attribute list */
  1672. ATTRIBUTE_ID *attId = getAttributeId(xmlParserP, enc, atts[i].name,
  1673. atts[i].name
  1674. + XmlNameLength(enc, atts[i].name));
  1675. if (!attId)
  1676. return XML_ERROR_NO_MEMORY;
  1677. /* detect duplicate attributes */
  1678. if ((attId->name)[-1]) {
  1679. if (enc == parser->m_encoding)
  1680. eventPtr = atts[i].name;
  1681. return XML_ERROR_DUPLICATE_ATTRIBUTE;
  1682. }
  1683. (attId->name)[-1] = 1;
  1684. appAtts[attIndex++] = attId->name;
  1685. if (!atts[i].normalized) {
  1686. enum XML_Error result;
  1687. int isCdata = 1;
  1688. /* figure out whether declared as other than CDATA */
  1689. if (attId->maybeTokenized) {
  1690. int j;
  1691. for (j = 0; j < nDefaultAtts; j++) {
  1692. if (attId == elementType->defaultAtts[j].id) {
  1693. isCdata = elementType->defaultAtts[j].isCdata;
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. /* normalize the attribute value */
  1699. result = storeAttributeValue(xmlParserP, enc, isCdata,
  1700. atts[i].valuePtr, atts[i].valueEnd,
  1701. &tempPool);
  1702. if (result)
  1703. return result;
  1704. if (tagNamePtr) {
  1705. appAtts[attIndex] = poolStart(&tempPool);
  1706. poolFinish(&tempPool);
  1707. }
  1708. else
  1709. poolDiscard(&tempPool);
  1710. }
  1711. else if (tagNamePtr) {
  1712. /* the value did not need normalizing */
  1713. appAtts[attIndex] =
  1714. poolStoreString(&tempPool, enc, atts[i].valuePtr, atts[i].valueEnd);
  1715. if (appAtts[attIndex] == 0)
  1716. return XML_ERROR_NO_MEMORY;
  1717. poolFinish(&tempPool);
  1718. }
  1719. /* handle prefixed attribute names */
  1720. if (attId->prefix && tagNamePtr) {
  1721. if (attId->xmlns) {
  1722. /* deal with namespace declarations here */
  1723. if (!addBinding(xmlParserP, attId->prefix, attId, appAtts[attIndex],
  1724. bindingsPtr))
  1725. return XML_ERROR_NO_MEMORY;
  1726. --attIndex;
  1727. }
  1728. else {
  1729. /* deal with other prefixed names later */
  1730. attIndex++;
  1731. nPrefixes++;
  1732. (attId->name)[-1] = 2;
  1733. }
  1734. }
  1735. else
  1736. attIndex++;
  1737. }
  1738. if (tagNamePtr) {
  1739. int j;
  1740. nSpecifiedAtts = attIndex;
  1741. if (elementType->idAtt && (elementType->idAtt->name)[-1]) {
  1742. for (i = 0; i < attIndex; i += 2)
  1743. if (appAtts[i] == elementType->idAtt->name) {
  1744. idAttIndex = i;
  1745. break;
  1746. }
  1747. }
  1748. else
  1749. idAttIndex = -1;
  1750. /* do attribute defaulting */
  1751. for (j = 0; j < nDefaultAtts; j++) {
  1752. const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + j;
  1753. if (!(da->id->name)[-1] && da->value) {
  1754. if (da->id->prefix) {
  1755. if (da->id->xmlns) {
  1756. if (!addBinding(xmlParserP, da->id->prefix, da->id, da->value,
  1757. bindingsPtr))
  1758. return XML_ERROR_NO_MEMORY;
  1759. }
  1760. else {
  1761. (da->id->name)[-1] = 2;
  1762. nPrefixes++;
  1763. appAtts[attIndex++] = da->id->name;
  1764. appAtts[attIndex++] = da->value;
  1765. }
  1766. }
  1767. else {
  1768. (da->id->name)[-1] = 1;
  1769. appAtts[attIndex++] = da->id->name;
  1770. appAtts[attIndex++] = da->value;
  1771. }
  1772. }
  1773. }
  1774. appAtts[attIndex] = 0;
  1775. }
  1776. i = 0;
  1777. if (nPrefixes) {
  1778. /* expand prefixed attribute names */
  1779. for (; i < attIndex; i += 2) {
  1780. if (appAtts[i][-1] == 2) {
  1781. ATTRIBUTE_ID *id;
  1782. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1783. id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, appAtts[i], 0);
  1784. if (id->prefix->binding) {
  1785. int j;
  1786. const BINDING *b = id->prefix->binding;
  1787. const XML_Char *s = appAtts[i];
  1788. for (j = 0; j < b->uriLen; j++) {
  1789. if (!poolAppendChar(&tempPool, b->uri[j]))
  1790. return XML_ERROR_NO_MEMORY;
  1791. }
  1792. while (*s++ != ':')
  1793. ;
  1794. do {
  1795. if (!poolAppendChar(&tempPool, *s))
  1796. return XML_ERROR_NO_MEMORY;
  1797. } while (*s++);
  1798. appAtts[i] = poolStart(&tempPool);
  1799. poolFinish(&tempPool);
  1800. }
  1801. if (!--nPrefixes)
  1802. break;
  1803. }
  1804. else
  1805. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1806. }
  1807. }
  1808. /* clear the flags that say whether attributes were specified */
  1809. for (; i < attIndex; i += 2)
  1810. ((XML_Char *)(appAtts[i]))[-1] = 0;
  1811. if (!tagNamePtr)
  1812. return XML_ERROR_NONE;
  1813. for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
  1814. binding->attId->name[-1] = 0;
  1815. /* expand the element type name */
  1816. if (elementType->prefix) {
  1817. binding = elementType->prefix->binding;
  1818. if (!binding)
  1819. return XML_ERROR_NONE;
  1820. localPart = tagNamePtr->str;
  1821. while (*localPart++ != XML_T(':'))
  1822. ;
  1823. }
  1824. else if (dtd.defaultPrefix.binding) {
  1825. binding = dtd.defaultPrefix.binding;
  1826. localPart = tagNamePtr->str;
  1827. }
  1828. else
  1829. return XML_ERROR_NONE;
  1830. tagNamePtr->localPart = localPart;
  1831. tagNamePtr->uriLen = binding->uriLen;
  1832. for (i = 0; localPart[i++];)
  1833. ;
  1834. n = i + binding->uriLen;
  1835. if (n > binding->uriAlloc) {
  1836. TAG *p;
  1837. XML_Char *uri = malloc((n + EXPAND_SPARE) * sizeof(XML_Char));
  1838. if (!uri)
  1839. return XML_ERROR_NO_MEMORY;
  1840. binding->uriAlloc = n + EXPAND_SPARE;
  1841. memcpy(uri, binding->uri, binding->uriLen * sizeof(XML_Char));
  1842. for (p = tagStack; p; p = p->parent)
  1843. if (p->name.str == binding->uri)
  1844. p->name.str = uri;
  1845. free(binding->uri);
  1846. binding->uri = uri;
  1847. }
  1848. memcpy(binding->uri + binding->uriLen, localPart, i * sizeof(XML_Char));
  1849. tagNamePtr->str = binding->uri;
  1850. return XML_ERROR_NONE;
  1851. }
  1852. static Processor epilogProcessor;
  1853. static void
  1854. epilogProcessor(XML_Parser const xmlParserP,
  1855. const char * const startArg,
  1856. const char * const end,
  1857. const char ** const nextPtr,
  1858. enum XML_Error * const errorCodeP,
  1859. const char ** const errorP) {
  1860. Parser * const parser = (Parser *) xmlParserP;
  1861. const char * s;
  1862. *errorP = NULL;
  1863. s = startArg;
  1864. processor = epilogProcessor;
  1865. eventPtr = s;
  1866. for (;;) {
  1867. const char *next;
  1868. int tok = XmlPrologTok(parser->m_encoding, s, end, &next);
  1869. eventEndPtr = next;
  1870. switch (tok) {
  1871. case -XML_TOK_PROLOG_S:
  1872. if (defaultHandler) {
  1873. eventEndPtr = end;
  1874. reportDefault(xmlParserP, parser->m_encoding, s, end);
  1875. }
  1876. /* fall through */
  1877. case XML_TOK_NONE:
  1878. if (nextPtr)
  1879. *nextPtr = end;
  1880. *errorCodeP = XML_ERROR_NONE;
  1881. return;
  1882. case XML_TOK_PROLOG_S:
  1883. if (defaultHandler)
  1884. reportDefault(xmlParserP, parser->m_encoding, s, next);
  1885. break;
  1886. case XML_TOK_PI:
  1887. if (!reportProcessingInstruction(xmlParserP, parser->m_encoding,
  1888. s, next)) {
  1889. *errorCodeP = XML_ERROR_NO_MEMORY;
  1890. return;
  1891. }
  1892. break;
  1893. case XML_TOK_COMMENT:
  1894. if (!reportComment(xmlParserP, parser->m_encoding, s, next)) {
  1895. *errorCodeP = XML_ERROR_NO_MEMORY;
  1896. return;
  1897. }
  1898. break;
  1899. case XML_TOK_INVALID:
  1900. eventPtr = next;
  1901. *errorCodeP = XML_ERROR_INVALID_TOKEN;
  1902. return;
  1903. case XML_TOK_PARTIAL:
  1904. if (nextPtr) {
  1905. *nextPtr = s;
  1906. *errorCodeP = XML_ERROR_NONE;
  1907. } else
  1908. *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
  1909. return;
  1910. case XML_TOK_PARTIAL_CHAR:
  1911. if (nextPtr) {
  1912. *nextPtr = s;
  1913. *errorCodeP = XML_ERROR_NONE;
  1914. } else
  1915. *errorCodeP = XML_ERROR_PARTIAL_CHAR;
  1916. return;
  1917. default:
  1918. *errorCodeP = XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
  1919. return;
  1920. }
  1921. eventPtr = s = next;
  1922. }
  1923. }
  1924. static enum XML_Error
  1925. doCdataSection(XML_Parser const xmlParserP,
  1926. const ENCODING * const enc,
  1927. const char ** const startPtr,
  1928. const char * const end,
  1929. const char ** const nextPtr) {
  1930. Parser * const parser = (Parser *) xmlParserP;
  1931. const char *s = *startPtr;
  1932. const char **eventPP;
  1933. const char **eventEndPP;
  1934. if (enc == parser->m_encoding) {
  1935. eventPP = &eventPtr;
  1936. *eventPP = s;
  1937. eventEndPP = &eventEndPtr;
  1938. }
  1939. else {
  1940. eventPP = &(openInternalEntities->internalEventPtr);
  1941. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  1942. }
  1943. *eventPP = s;
  1944. *startPtr = 0;
  1945. for (;;) {
  1946. const char *next;
  1947. int tok = XmlCdataSectionTok(enc, s, end, &next);
  1948. *eventEndPP = next;
  1949. switch (tok) {
  1950. case XML_TOK_CDATA_SECT_CLOSE:
  1951. if (endCdataSectionHandler)
  1952. endCdataSectionHandler(handlerArg);
  1953. else if (defaultHandler)
  1954. reportDefault(xmlParserP, enc, s, next);
  1955. *startPtr = next;
  1956. return XML_ERROR_NONE;
  1957. case XML_TOK_DATA_NEWLINE:
  1958. if (characterDataHandler) {
  1959. XML_Char c = 0xA;
  1960. characterDataHandler(handlerArg, &c, 1);
  1961. }
  1962. else if (defaultHandler)
  1963. reportDefault(xmlParserP, enc, s, next);
  1964. break;
  1965. case XML_TOK_DATA_CHARS:
  1966. if (characterDataHandler) {
  1967. if (MUST_CONVERT(enc, s)) {
  1968. for (;;) {
  1969. ICHAR *dataPtr = (ICHAR *)dataBuf;
  1970. XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
  1971. *eventEndPP = next;
  1972. {
  1973. size_t const len = dataPtr - (ICHAR *)dataBuf;
  1974. assert((size_t)(int)len == len); /* parser requirement */
  1975. characterDataHandler(handlerArg, dataBuf, (int)len);
  1976. }
  1977. if (s == next)
  1978. break;
  1979. *eventPP = s;
  1980. }
  1981. }
  1982. else {
  1983. size_t const len = (XML_Char *)next - (XML_Char *)s;
  1984. assert((size_t)(int)len == len); /* parser requirement */
  1985. characterDataHandler(handlerArg, (XML_Char *)s, (int)len);
  1986. }
  1987. }
  1988. else if (defaultHandler)
  1989. reportDefault(xmlParserP, enc, s, next);
  1990. break;
  1991. case XML_TOK_INVALID:
  1992. *eventPP = next;
  1993. return XML_ERROR_INVALID_TOKEN;
  1994. case XML_TOK_PARTIAL_CHAR:
  1995. if (nextPtr) {
  1996. *nextPtr = s;
  1997. return XML_ERROR_NONE;
  1998. }
  1999. return XML_ERROR_PARTIAL_CHAR;
  2000. case XML_TOK_PARTIAL:
  2001. case XML_TOK_NONE:
  2002. if (nextPtr) {
  2003. *nextPtr = s;
  2004. return XML_ERROR_NONE;
  2005. }
  2006. return XML_ERROR_UNCLOSED_CDATA_SECTION;
  2007. default:
  2008. abort();
  2009. }
  2010. *eventPP = s = next;
  2011. }
  2012. /* not reached */
  2013. }
  2014. /* Forward declaration for recursive reference: */
  2015. static void
  2016. doContent(XML_Parser const xmlParserP,
  2017. int const startTagLevel,
  2018. const ENCODING * const enc,
  2019. const char * const startArg,
  2020. const char * const end,
  2021. const char ** const nextPtr,
  2022. enum XML_Error * const errorCodeP,
  2023. const char ** const errorP);
  2024. static Processor contentProcessor;
  2025. static void
  2026. contentProcessor(XML_Parser const xmlParserP,
  2027. const char * const start,
  2028. const char * const end,
  2029. const char ** const endPtr,
  2030. enum XML_Error * const errorCodeP,
  2031. const char ** const errorP) {
  2032. Parser * const parser = (Parser *) xmlParserP;
  2033. const char * error;
  2034. parser->m_errorString = NULL;
  2035. doContent(xmlParserP, 0, parser->m_encoding, start, end, endPtr,
  2036. errorCodeP, &error);
  2037. if (*errorCodeP != XML_ERROR_NONE) {
  2038. if (error) {
  2039. xmlrpc_asprintf(errorP, "Invalid XML \"content\". %s", error);
  2040. xmlrpc_strfree(error);
  2041. } else {
  2042. const char * const sampleXml = extractXmlSample(start, end, 40);
  2043. xmlrpc_asprintf(errorP, "Invalid XML \"content\" starting "
  2044. "with '%s'. %s",
  2045. sampleXml,
  2046. xmlrpc_XML_ErrorString(*errorCodeP));
  2047. xmlrpc_strfree(sampleXml);
  2048. }
  2049. } else
  2050. *errorP = NULL;
  2051. }
  2052. /* The idea here is to avoid using stack for each CDATA section when
  2053. the whole file is parsed with one call. */
  2054. static Processor cdataSectionProcessor;
  2055. static void
  2056. cdataSectionProcessor(XML_Parser const xmlParserP,
  2057. const char * const startArg,
  2058. const char * const end,
  2059. const char ** const endPtr,
  2060. enum XML_Error * const errorCodeP,
  2061. const char ** const errorP) {
  2062. Parser * const parser = (Parser *) xmlParserP;
  2063. enum XML_Error result;
  2064. const char * start;
  2065. start = startArg;
  2066. result =
  2067. doCdataSection(xmlParserP, parser->m_encoding, &start, end, endPtr);
  2068. if (start) {
  2069. processor = contentProcessor;
  2070. contentProcessor(xmlParserP, start, end, endPtr, errorCodeP, errorP);
  2071. } else {
  2072. *errorCodeP = result;
  2073. *errorP = NULL;
  2074. }
  2075. }
  2076. static void
  2077. doEntityRef(XML_Parser const xmlParserP,
  2078. const ENCODING * const enc,
  2079. const char * const s,
  2080. const char * const next,
  2081. enum XML_Error * const errorCodeP,
  2082. const char ** const errorP) {
  2083. Parser * const parser = (Parser *) xmlParserP;
  2084. XML_Char const ch = XmlPredefinedEntityName(enc,
  2085. s + enc->minBytesPerChar,
  2086. next - enc->minBytesPerChar);
  2087. const XML_Char *name;
  2088. ENTITY *entity;
  2089. *errorP = NULL;
  2090. if (ch) {
  2091. if (characterDataHandler)
  2092. characterDataHandler(handlerArg, &ch, 1);
  2093. else if (defaultHandler)
  2094. reportDefault(xmlParserP, enc, s, next);
  2095. *errorCodeP = XML_ERROR_NONE;
  2096. return;
  2097. }
  2098. name = poolStoreString(&dtd.pool, enc,
  2099. s + enc->minBytesPerChar,
  2100. next - enc->minBytesPerChar);
  2101. if (!name) {
  2102. *errorCodeP = XML_ERROR_NO_MEMORY;
  2103. return;
  2104. }
  2105. entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
  2106. poolDiscard(&dtd.pool);
  2107. if (!entity) {
  2108. if (dtd.complete || dtd.standalone)
  2109. *errorCodeP = XML_ERROR_UNDEFINED_ENTITY;
  2110. else {
  2111. if (defaultHandler)
  2112. reportDefault(xmlParserP, enc, s, next);
  2113. *errorCodeP = XML_ERROR_NONE;
  2114. }
  2115. return;
  2116. }
  2117. if (entity->open) {
  2118. *errorCodeP = XML_ERROR_RECURSIVE_ENTITY_REF;
  2119. return;
  2120. }
  2121. if (entity->notation) {
  2122. *errorCodeP = XML_ERROR_BINARY_ENTITY_REF;
  2123. return;
  2124. }
  2125. if (entity) {
  2126. if (entity->textPtr) {
  2127. OPEN_INTERNAL_ENTITY openEntity;
  2128. if (defaultHandler && !defaultExpandInternalEntities) {
  2129. reportDefault(xmlParserP, enc, s, next);
  2130. *errorCodeP = XML_ERROR_NONE;
  2131. return;
  2132. }
  2133. entity->open = 1;
  2134. openEntity.next = openInternalEntities;
  2135. openInternalEntities = &openEntity;
  2136. openEntity.entity = entity;
  2137. openEntity.internalEventPtr = 0;
  2138. openEntity.internalEventEndPtr = 0;
  2139. doContent(xmlParserP,
  2140. tagLevel,
  2141. internalEncoding,
  2142. (char *)entity->textPtr,
  2143. (char *)(entity->textPtr + entity->textLen),
  2144. 0, errorCodeP, errorP);
  2145. entity->open = 0;
  2146. openInternalEntities = openEntity.next;
  2147. if (*errorCodeP != XML_ERROR_NONE)
  2148. return;
  2149. } else if (externalEntityRefHandler) {
  2150. const XML_Char *context;
  2151. entity->open = 1;
  2152. context = getContext(xmlParserP);
  2153. entity->open = 0;
  2154. if (!context) {
  2155. *errorCodeP = XML_ERROR_NO_MEMORY;
  2156. return;
  2157. }
  2158. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  2159. context,
  2160. entity->base,
  2161. entity->systemId,
  2162. entity->publicId)) {
  2163. *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  2164. return;
  2165. }
  2166. poolDiscard(&tempPool);
  2167. } else if (defaultHandler)
  2168. reportDefault(xmlParserP, enc, s, next);
  2169. }
  2170. *errorCodeP = XML_ERROR_NONE;
  2171. }
  2172. static void
  2173. doStartTagNoAtts(XML_Parser const xmlParserP,
  2174. const ENCODING * const enc,
  2175. const char * const s,
  2176. const char * const next,
  2177. const char ** const nextPtr,
  2178. enum XML_Error * const errorCodeP,
  2179. const char ** const errorP) {
  2180. Parser * const parser = (Parser *) xmlParserP;
  2181. TAG *tag;
  2182. *errorP = NULL;
  2183. if (freeTagList) {
  2184. tag = freeTagList;
  2185. freeTagList = freeTagList->parent;
  2186. } else {
  2187. tag = malloc(sizeof(TAG));
  2188. if (!tag) {
  2189. *errorCodeP = XML_ERROR_NO_MEMORY;
  2190. return;
  2191. }
  2192. tag->buf = malloc(INIT_TAG_BUF_SIZE);
  2193. if (!tag->buf) {
  2194. *errorCodeP = XML_ERROR_NO_MEMORY;
  2195. return;
  2196. }
  2197. tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
  2198. }
  2199. tag->bindings = NULL;
  2200. tag->parent = tagStack;
  2201. tagStack = tag;
  2202. tag->name.localPart = 0;
  2203. tag->rawName = s + enc->minBytesPerChar;
  2204. tag->rawNameLength = XmlNameLength(enc, tag->rawName);
  2205. if (nextPtr) {
  2206. /* Need to guarantee that: tag->buf +
  2207. ROUND_UP(tag->rawNameLength, sizeof(XML_Char)) <=
  2208. tag->bufEnd - sizeof(XML_Char)
  2209. */
  2210. if (tag->rawNameLength +
  2211. (int)(sizeof(XML_Char) - 1) +
  2212. (int)sizeof(XML_Char) > tag->bufEnd - tag->buf) {
  2213. int bufSize = tag->rawNameLength * 4;
  2214. bufSize = ROUND_UP(bufSize, sizeof(XML_Char));
  2215. tag->buf = realloc(tag->buf, bufSize);
  2216. if (!tag->buf) {
  2217. *errorCodeP = XML_ERROR_NO_MEMORY;
  2218. return;
  2219. }
  2220. tag->bufEnd = tag->buf + bufSize;
  2221. }
  2222. memcpy(tag->buf, tag->rawName, tag->rawNameLength);
  2223. tag->rawName = tag->buf;
  2224. }
  2225. ++tagLevel;
  2226. if (startElementHandler) {
  2227. enum XML_Error result;
  2228. XML_Char *toPtr;
  2229. for (;;) {
  2230. const char *rawNameEnd = tag->rawName + tag->rawNameLength;
  2231. const char *fromPtr = tag->rawName;
  2232. if (nextPtr)
  2233. toPtr = (XML_Char *)
  2234. (tag->buf + ROUND_UP(tag->rawNameLength,
  2235. sizeof(XML_Char)));
  2236. else
  2237. toPtr = (XML_Char *)tag->buf;
  2238. tag->name.str = toPtr;
  2239. XmlConvert(enc,
  2240. &fromPtr, rawNameEnd,
  2241. (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
  2242. if (fromPtr == rawNameEnd)
  2243. break;
  2244. else {
  2245. size_t const bufSize = (tag->bufEnd - tag->buf) << 1;
  2246. tag->buf = realloc(tag->buf, bufSize);
  2247. if (!tag->buf) {
  2248. *errorCodeP = XML_ERROR_NO_MEMORY;
  2249. return;
  2250. }
  2251. tag->bufEnd = tag->buf + bufSize;
  2252. if (nextPtr)
  2253. tag->rawName = tag->buf;
  2254. }
  2255. }
  2256. *toPtr = XML_T('\0');
  2257. result = storeAtts(xmlParserP, enc, s,
  2258. &(tag->name), &(tag->bindings));
  2259. if (result) {
  2260. *errorCodeP = result;
  2261. return;
  2262. }
  2263. startElementHandler(handlerArg, tag->name.str,
  2264. (const XML_Char **)atts);
  2265. poolClear(&tempPool);
  2266. } else {
  2267. tag->name.str = 0;
  2268. if (defaultHandler)
  2269. reportDefault(xmlParserP, enc, s, next);
  2270. }
  2271. }
  2272. static void
  2273. doEmptyElementNoAtts(XML_Parser const xmlParserP,
  2274. const ENCODING * const enc,
  2275. const char * const s,
  2276. const char * const end,
  2277. const char * const next,
  2278. const char ** const nextPtr,
  2279. const char ** const eventPP,
  2280. const char ** const eventEndPP,
  2281. bool * const doneP,
  2282. enum XML_Error * const errorCodeP,
  2283. const char ** const errorP) {
  2284. Parser * const parser = (Parser *) xmlParserP;
  2285. if (startElementHandler || endElementHandler) {
  2286. const char * const rawName = s + enc->minBytesPerChar;
  2287. enum XML_Error result;
  2288. BINDING * bindings;
  2289. TAG_NAME name;
  2290. bindings = NULL; /* initial value */
  2291. name.str = poolStoreString(&tempPool, enc, rawName,
  2292. rawName + XmlNameLength(enc, rawName));
  2293. if (!name.str) {
  2294. *errorCodeP = XML_ERROR_NO_MEMORY;
  2295. return;
  2296. }
  2297. poolFinish(&tempPool);
  2298. result = storeAtts(xmlParserP, enc, s, &name, &bindings);
  2299. if (result) {
  2300. *errorCodeP = result;
  2301. return;
  2302. }
  2303. poolFinish(&tempPool);
  2304. if (startElementHandler)
  2305. startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
  2306. if (endElementHandler) {
  2307. if (startElementHandler)
  2308. *eventPP = *eventEndPP;
  2309. endElementHandler(handlerArg, name.str);
  2310. }
  2311. poolClear(&tempPool);
  2312. while (bindings) {
  2313. BINDING * const b = bindings;
  2314. if (endNamespaceDeclHandler)
  2315. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  2316. bindings = bindings->nextTagBinding;
  2317. b->nextTagBinding = freeBindingList;
  2318. freeBindingList = b;
  2319. b->prefix->binding = b->prevPrefixBinding;
  2320. }
  2321. } else if (defaultHandler)
  2322. reportDefault(xmlParserP, enc, s, next);
  2323. if (tagLevel == 0) {
  2324. epilogProcessor(xmlParserP, next, end, nextPtr, errorCodeP, errorP);
  2325. *doneP = true;
  2326. } else
  2327. *doneP = false;
  2328. }
  2329. static void
  2330. doEndTag(XML_Parser const xmlParserP,
  2331. const ENCODING * const enc,
  2332. const char * const s,
  2333. const char * const end,
  2334. const char * const next,
  2335. const char ** const nextPtr,
  2336. int const startTagLevel,
  2337. const char ** const eventPP,
  2338. bool * const doneP,
  2339. enum XML_Error * const errorCodeP,
  2340. const char ** const errorP) {
  2341. Parser * const parser = (Parser *) xmlParserP;
  2342. if (tagLevel == startTagLevel)
  2343. *errorCodeP = XML_ERROR_ASYNC_ENTITY;
  2344. else {
  2345. TAG * const tag = tagStack;
  2346. int len;
  2347. const char * rawName;
  2348. tagStack = tag->parent;
  2349. tag->parent = freeTagList;
  2350. freeTagList = tag;
  2351. rawName = s + enc->minBytesPerChar*2;
  2352. len = XmlNameLength(enc, rawName);
  2353. if (len != tag->rawNameLength
  2354. || memcmp(tag->rawName, rawName, len) != 0) {
  2355. *eventPP = rawName;
  2356. *errorCodeP = XML_ERROR_TAG_MISMATCH;
  2357. } else {
  2358. --tagLevel;
  2359. if (endElementHandler && tag->name.str) {
  2360. if (tag->name.localPart) {
  2361. XML_Char * to;
  2362. const XML_Char * from;
  2363. to = (XML_Char *)tag->name.str + tag->name.uriLen;
  2364. from = tag->name.localPart;
  2365. while ((*to++ = *from++) != 0)
  2366. ;
  2367. }
  2368. endElementHandler(handlerArg, tag->name.str);
  2369. } else if (defaultHandler)
  2370. reportDefault(xmlParserP, enc, s, next);
  2371. while (tag->bindings) {
  2372. BINDING * const b = tag->bindings;
  2373. if (endNamespaceDeclHandler)
  2374. endNamespaceDeclHandler(handlerArg, b->prefix->name);
  2375. tag->bindings = tag->bindings->nextTagBinding;
  2376. b->nextTagBinding = freeBindingList;
  2377. freeBindingList = b;
  2378. b->prefix->binding = b->prevPrefixBinding;
  2379. }
  2380. if (tagLevel == 0) {
  2381. epilogProcessor(xmlParserP, next, end, nextPtr,
  2382. errorCodeP, errorP);
  2383. *doneP = true;
  2384. } else {
  2385. *errorCodeP = XML_ERROR_NONE;
  2386. *doneP = false;
  2387. }
  2388. }
  2389. }
  2390. }
  2391. static void
  2392. processContentToken(XML_Parser const xmlParserP,
  2393. int const tok,
  2394. const ENCODING * const enc,
  2395. const char * const s,
  2396. const char * const end,
  2397. const char ** const nextP,
  2398. const char ** const nextPtr,
  2399. int const startTagLevel,
  2400. const char ** const eventPP,
  2401. const char ** const eventEndPP,
  2402. bool * const doneP,
  2403. enum XML_Error * const errorCodeP,
  2404. const char ** const errorP) {
  2405. Parser * const parser = (Parser *) xmlParserP;
  2406. *errorP = NULL;
  2407. *errorCodeP = XML_ERROR_NONE;
  2408. switch (tok) {
  2409. case XML_TOK_TRAILING_CR:
  2410. if (nextPtr) {
  2411. *nextPtr = s;
  2412. *doneP = true;
  2413. } else {
  2414. *eventEndPP = end;
  2415. if (characterDataHandler) {
  2416. XML_Char c = 0xA;
  2417. characterDataHandler(handlerArg, &c, 1);
  2418. } else if (defaultHandler)
  2419. reportDefault(xmlParserP, enc, s, end);
  2420. if (startTagLevel == 0)
  2421. *errorCodeP = XML_ERROR_NO_ELEMENTS;
  2422. else if (tagLevel != startTagLevel) {
  2423. *errorCodeP = XML_ERROR_ASYNC_ENTITY;
  2424. } else
  2425. *doneP = true;
  2426. }
  2427. break;
  2428. case XML_TOK_NONE:
  2429. if (nextPtr) {
  2430. *nextPtr = s;
  2431. *doneP = true;
  2432. } else if (startTagLevel > 0) {
  2433. if (tagLevel != startTagLevel)
  2434. *errorCodeP = XML_ERROR_ASYNC_ENTITY;
  2435. else
  2436. *doneP = true;
  2437. } else
  2438. *errorCodeP = XML_ERROR_NO_ELEMENTS;
  2439. break;
  2440. case XML_TOK_INVALID:
  2441. *eventPP = *nextP;
  2442. *errorCodeP = XML_ERROR_INVALID_TOKEN;
  2443. xmlrpc_asprintf(errorP, "Invalid token, starting %ld bytes in",
  2444. (long)(*nextP - s));
  2445. break;
  2446. case XML_TOK_PARTIAL:
  2447. if (nextPtr) {
  2448. *nextPtr = s;
  2449. *doneP = true;
  2450. } else
  2451. *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
  2452. break;
  2453. case XML_TOK_PARTIAL_CHAR:
  2454. if (nextPtr) {
  2455. *nextPtr = s;
  2456. *doneP = true;
  2457. } else
  2458. *errorCodeP = XML_ERROR_PARTIAL_CHAR;
  2459. break;
  2460. case XML_TOK_ENTITY_REF:
  2461. doEntityRef(xmlParserP, enc, s, *nextP, errorCodeP, errorP);
  2462. break;
  2463. case XML_TOK_START_TAG_WITH_ATTS:
  2464. if (!startElementHandler)
  2465. *errorCodeP = storeAtts(xmlParserP, enc, s, 0, 0);
  2466. if (*errorCodeP == XML_ERROR_NONE)
  2467. doStartTagNoAtts(xmlParserP, enc, s, *nextP, nextPtr,
  2468. errorCodeP, errorP);
  2469. break;
  2470. case XML_TOK_START_TAG_NO_ATTS:
  2471. doStartTagNoAtts(xmlParserP, enc, s, *nextP, nextPtr,
  2472. errorCodeP, errorP);
  2473. break;
  2474. case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
  2475. if (!startElementHandler)
  2476. *errorCodeP = storeAtts(xmlParserP, enc, s, 0, 0);
  2477. if (*errorCodeP == XML_ERROR_NONE)
  2478. doEmptyElementNoAtts(xmlParserP, enc, s, end, *nextP, nextPtr,
  2479. eventPP, eventEndPP,
  2480. doneP, errorCodeP, errorP);
  2481. break;
  2482. case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
  2483. doEmptyElementNoAtts(xmlParserP, enc, s, end, *nextP, nextPtr,
  2484. eventPP, eventEndPP,
  2485. doneP, errorCodeP, errorP);
  2486. break;
  2487. case XML_TOK_END_TAG:
  2488. doEndTag(xmlParserP, enc, s, end, *nextP, nextPtr, startTagLevel,
  2489. eventPP, doneP, errorCodeP, errorP);
  2490. break;
  2491. case XML_TOK_CHAR_REF: {
  2492. int const n = XmlCharRefNumber(enc, s);
  2493. if (n < 0)
  2494. *errorCodeP = XML_ERROR_BAD_CHAR_REF;
  2495. else {
  2496. if (characterDataHandler) {
  2497. XML_Char buf[XML_ENCODE_MAX];
  2498. characterDataHandler(handlerArg, buf,
  2499. XmlEncode(n, (ICHAR *)buf));
  2500. } else if (defaultHandler)
  2501. reportDefault(xmlParserP, enc, s, *nextP);
  2502. }
  2503. } break;
  2504. case XML_TOK_XML_DECL:
  2505. *errorCodeP = XML_ERROR_MISPLACED_XML_PI;
  2506. break;
  2507. case XML_TOK_DATA_NEWLINE:
  2508. if (characterDataHandler) {
  2509. XML_Char c = 0xA;
  2510. characterDataHandler(handlerArg, &c, 1);
  2511. } else if (defaultHandler)
  2512. reportDefault(xmlParserP, enc, s, *nextP);
  2513. break;
  2514. case XML_TOK_CDATA_SECT_OPEN: {
  2515. enum XML_Error result;
  2516. if (startCdataSectionHandler)
  2517. startCdataSectionHandler(handlerArg);
  2518. else if (defaultHandler)
  2519. reportDefault(xmlParserP, enc, s, *nextP);
  2520. result = doCdataSection(xmlParserP, enc, nextP, end, nextPtr);
  2521. if (!*nextP) {
  2522. processor = cdataSectionProcessor;
  2523. *errorCodeP = result;
  2524. }
  2525. } break;
  2526. case XML_TOK_TRAILING_RSQB:
  2527. if (nextPtr) {
  2528. *nextPtr = s;
  2529. *errorCodeP = XML_ERROR_NONE;
  2530. } else {
  2531. if (characterDataHandler) {
  2532. if (MUST_CONVERT(enc, s)) {
  2533. const char * from;
  2534. ICHAR * dataPtr;
  2535. from = s;
  2536. dataPtr = (ICHAR *)dataBuf;
  2537. XmlConvert(enc, &from, end, &dataPtr, (ICHAR *)dataBufEnd);
  2538. {
  2539. size_t const len = dataPtr - (ICHAR *)dataBuf;
  2540. assert((size_t)(int)len == len); /* parser reqt */
  2541. characterDataHandler(handlerArg, dataBuf, (int)len);
  2542. }
  2543. } else {
  2544. size_t const len = (XML_Char *)end - (XML_Char *)s;
  2545. assert((size_t)(int)len == len); /* parser reqt */
  2546. characterDataHandler(handlerArg, (XML_Char *)s, (int)len);
  2547. }
  2548. } else if (defaultHandler)
  2549. reportDefault(xmlParserP, enc, s, end);
  2550. if (startTagLevel == 0) {
  2551. *eventPP = end;
  2552. *errorCodeP = XML_ERROR_NO_ELEMENTS;
  2553. } else if (tagLevel != startTagLevel) {
  2554. *eventPP = end;
  2555. *errorCodeP = XML_ERROR_ASYNC_ENTITY;
  2556. } else
  2557. *doneP = true;
  2558. }
  2559. break;
  2560. case XML_TOK_DATA_CHARS:
  2561. if (characterDataHandler) {
  2562. if (MUST_CONVERT(enc, s)) {
  2563. for (;;) {
  2564. const char * from;
  2565. ICHAR * dataPtr;
  2566. dataPtr = (ICHAR *)dataBuf;
  2567. from = s;
  2568. XmlConvert(enc, &from, *nextP, &dataPtr,
  2569. (ICHAR *)dataBufEnd);
  2570. *eventEndPP = from;
  2571. {
  2572. size_t const len = dataPtr - (ICHAR *)dataBuf;
  2573. assert((size_t)(int)len == len); /* parser reqt */
  2574. characterDataHandler(handlerArg, dataBuf, (int)len);
  2575. }
  2576. if (from == *nextP)
  2577. break;
  2578. *eventPP = from;
  2579. }
  2580. } else {
  2581. size_t const len = (XML_Char *)*nextP - (XML_Char *)s;
  2582. assert((size_t)(int)len == len); /* parser reqt */
  2583. characterDataHandler(handlerArg, (XML_Char *)s, len);
  2584. }
  2585. } else if (defaultHandler)
  2586. reportDefault(xmlParserP, enc, s, *nextP);
  2587. break;
  2588. case XML_TOK_PI:
  2589. if (!reportProcessingInstruction(xmlParserP, enc, s, *nextP))
  2590. *errorCodeP = XML_ERROR_NO_MEMORY;
  2591. break;
  2592. case XML_TOK_COMMENT:
  2593. if (!reportComment(xmlParserP, enc, s, *nextP))
  2594. *errorCodeP = XML_ERROR_NO_MEMORY;
  2595. break;
  2596. default:
  2597. if (defaultHandler)
  2598. reportDefault(xmlParserP, enc, s, *nextP);
  2599. break;
  2600. }
  2601. }
  2602. static void
  2603. doContent(XML_Parser const xmlParserP,
  2604. int const startTagLevel,
  2605. const ENCODING * const enc,
  2606. const char * const startArg,
  2607. const char * const end,
  2608. const char ** const nextPtr,
  2609. enum XML_Error * const errorCodeP,
  2610. const char ** const errorP) {
  2611. Parser * const parser = (Parser *) xmlParserP;
  2612. const char **eventPP;
  2613. const char **eventEndPP;
  2614. const char * s;
  2615. bool done;
  2616. if (enc == parser->m_encoding) {
  2617. eventPP = &eventPtr;
  2618. eventEndPP = &eventEndPtr;
  2619. } else {
  2620. eventPP = &(openInternalEntities->internalEventPtr);
  2621. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2622. }
  2623. s = startArg;
  2624. *eventPP = s;
  2625. done = false;
  2626. *errorCodeP = XML_ERROR_NONE;
  2627. *errorP = NULL;
  2628. while (*errorCodeP == XML_ERROR_NONE && !done) {
  2629. int tok;
  2630. const char * next;
  2631. const char * error;
  2632. next = s; /* XmlContentTok doesn't always set the last arg */
  2633. /* XmlContentTok() is normally normal_contentTok(), aka
  2634. PREFIX(contentTok)() in xmltok/xmltok_impl.c
  2635. */
  2636. tok = XmlContentTok(enc, s, end, &next);
  2637. *eventEndPP = next;
  2638. processContentToken(xmlParserP, tok, enc, s, end, &next, nextPtr,
  2639. startTagLevel, eventPP, eventEndPP, &done,
  2640. errorCodeP, &error);
  2641. if (*errorCodeP != XML_ERROR_NONE) {
  2642. const char * const xmlSample = extractXmlSample(s, end, 40);
  2643. if (error) {
  2644. xmlrpc_asprintf(errorP, "Problem with token at '%s...': %s",
  2645. xmlSample, error);
  2646. xmlrpc_strfree(error);
  2647. } else
  2648. xmlrpc_asprintf(errorP, "Problem with token at '%s...': %s",
  2649. xmlSample,
  2650. xmlrpc_XML_ErrorString(*errorCodeP));
  2651. xmlrpc_strfree(xmlSample);
  2652. }
  2653. *eventPP = s = next;
  2654. }
  2655. }
  2656. static Processor externalEntityContentProcessor;
  2657. static void
  2658. externalEntityContentProcessor(XML_Parser const xmlParserP,
  2659. const char * const start,
  2660. const char * const end,
  2661. const char ** const endPtr,
  2662. enum XML_Error * const errorCodeP,
  2663. const char ** const errorP) {
  2664. Parser * const parser = (Parser *) xmlParserP;
  2665. *errorP = NULL;
  2666. doContent(xmlParserP, 1, parser->m_encoding, start, end, endPtr,
  2667. errorCodeP, errorP);
  2668. }
  2669. static Processor externalEntityInitProcessor3;
  2670. static void
  2671. externalEntityInitProcessor3(XML_Parser const xmlParserP,
  2672. const char * const startArg,
  2673. const char * const end,
  2674. const char ** const endPtr,
  2675. enum XML_Error * const errorCodeP,
  2676. const char ** const errorP) {
  2677. Parser * const parser = (Parser *) xmlParserP;
  2678. const char * start;
  2679. const char *next;
  2680. int tok;
  2681. tok = XmlContentTok(parser->m_encoding, startArg, end, &next);
  2682. *errorP = NULL;
  2683. start = startArg;
  2684. switch (tok) {
  2685. case XML_TOK_XML_DECL:
  2686. {
  2687. enum XML_Error result = processXmlDecl(xmlParserP, 1, start, next);
  2688. if (result != XML_ERROR_NONE) {
  2689. *errorCodeP = result;
  2690. return;
  2691. }
  2692. start = next;
  2693. }
  2694. break;
  2695. case XML_TOK_PARTIAL:
  2696. if (endPtr) {
  2697. *endPtr = start;
  2698. *errorCodeP = XML_ERROR_NONE;
  2699. return;
  2700. }
  2701. eventPtr = start;
  2702. *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
  2703. return;
  2704. case XML_TOK_PARTIAL_CHAR:
  2705. if (endPtr) {
  2706. *endPtr = start;
  2707. *errorCodeP = XML_ERROR_NONE;
  2708. return;
  2709. }
  2710. eventPtr = start;
  2711. *errorCodeP = XML_ERROR_PARTIAL_CHAR;
  2712. return;
  2713. }
  2714. processor = externalEntityContentProcessor;
  2715. tagLevel = 1;
  2716. doContent(xmlParserP, 1, parser->m_encoding, start, end, endPtr,
  2717. errorCodeP, errorP);
  2718. }
  2719. static Processor externalEntityInitProcessor2;
  2720. static void
  2721. externalEntityInitProcessor2(XML_Parser const xmlParserP,
  2722. const char * const startArg,
  2723. const char * const end,
  2724. const char ** const endPtr,
  2725. enum XML_Error * const errorCodeP,
  2726. const char ** const errorP) {
  2727. Parser * const parser = (Parser *)xmlParserP;
  2728. const char * start;
  2729. const char * next;
  2730. int tok;
  2731. tok = XmlContentTok(parser->m_encoding, startArg, end, &next);
  2732. start = startArg;
  2733. switch (tok) {
  2734. case XML_TOK_BOM:
  2735. start = next;
  2736. break;
  2737. case XML_TOK_PARTIAL:
  2738. if (endPtr) {
  2739. *endPtr = start;
  2740. *errorCodeP = XML_ERROR_NONE;
  2741. *errorP = NULL;
  2742. } else {
  2743. eventPtr = start;
  2744. *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
  2745. *errorP = NULL;
  2746. }
  2747. return;
  2748. case XML_TOK_PARTIAL_CHAR:
  2749. if (endPtr) {
  2750. *endPtr = start;
  2751. *errorCodeP = XML_ERROR_NONE;
  2752. *errorP = NULL;
  2753. } else {
  2754. eventPtr = start;
  2755. *errorCodeP = XML_ERROR_PARTIAL_CHAR;
  2756. *errorP = NULL;
  2757. }
  2758. return;
  2759. }
  2760. processor = externalEntityInitProcessor3;
  2761. externalEntityInitProcessor3(xmlParserP, start, end, endPtr,
  2762. errorCodeP, errorP);
  2763. }
  2764. static Processor externalEntityInitProcessor;
  2765. static void
  2766. externalEntityInitProcessor(XML_Parser const parser,
  2767. const char * const start,
  2768. const char * const end,
  2769. const char ** const endPtr,
  2770. enum XML_Error * const errorCodeP,
  2771. const char ** const errorP) {
  2772. enum XML_Error result;
  2773. result = initializeEncoding(parser);
  2774. if (result != XML_ERROR_NONE) {
  2775. *errorCodeP = result;
  2776. *errorP = NULL;
  2777. } else {
  2778. processor = externalEntityInitProcessor2;
  2779. externalEntityInitProcessor2(parser, start, end, endPtr,
  2780. errorCodeP, errorP);
  2781. }
  2782. }
  2783. static enum XML_Error
  2784. doIgnoreSection(XML_Parser const xmlParserP,
  2785. const ENCODING * const enc,
  2786. const char ** const startPtr,
  2787. const char * const end,
  2788. const char ** const nextPtr) {
  2789. /*----------------------------------------------------------------------------
  2790. We set *startPtr to non-null is the section is closed, and to null if
  2791. the section is not yet closed.
  2792. -----------------------------------------------------------------------------*/
  2793. Parser * const parser = (Parser *) xmlParserP;
  2794. const char * const s = *startPtr;
  2795. enum XML_Error retval;
  2796. const char * next;
  2797. int tok;
  2798. const char ** eventPP;
  2799. const char ** eventEndPP;
  2800. if (enc == parser->m_encoding) {
  2801. eventPP = &eventPtr;
  2802. eventEndPP = &eventEndPtr;
  2803. } else {
  2804. eventPP = &(openInternalEntities->internalEventPtr);
  2805. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2806. }
  2807. *eventPP = s;
  2808. *startPtr = '\0';
  2809. tok = XmlIgnoreSectionTok(enc, s, end, &next);
  2810. *eventEndPP = next;
  2811. switch (tok) {
  2812. case XML_TOK_IGNORE_SECT:
  2813. if (defaultHandler)
  2814. reportDefault(xmlParserP, enc, s, next);
  2815. *startPtr = next;
  2816. retval = XML_ERROR_NONE;
  2817. break;
  2818. case XML_TOK_INVALID:
  2819. *eventPP = next;
  2820. retval = XML_ERROR_INVALID_TOKEN;
  2821. break;
  2822. case XML_TOK_PARTIAL_CHAR:
  2823. if (nextPtr) {
  2824. *nextPtr = s;
  2825. retval = XML_ERROR_NONE;
  2826. } else
  2827. retval = XML_ERROR_PARTIAL_CHAR;
  2828. break;
  2829. case XML_TOK_PARTIAL:
  2830. case XML_TOK_NONE:
  2831. if (nextPtr) {
  2832. *nextPtr = s;
  2833. retval = XML_ERROR_NONE;
  2834. } else
  2835. retval = XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
  2836. break;
  2837. default:
  2838. assert(false); /* All possibilities are handled above */
  2839. retval = 99; /* quiet compiler warning */
  2840. }
  2841. return retval;
  2842. }
  2843. static Processor prologProcessor;
  2844. /* The idea here is to avoid using stack for each IGNORE section when
  2845. the whole file is parsed with one call. */
  2846. static Processor ignoreSectionProcessor;
  2847. static void
  2848. ignoreSectionProcessor(XML_Parser const xmlParserP,
  2849. const char * const startArg,
  2850. const char * const end,
  2851. const char ** const endPtr,
  2852. enum XML_Error * const errorCodeP,
  2853. const char ** const errorP) {
  2854. Parser * const parser = (Parser *) xmlParserP;
  2855. enum XML_Error result;
  2856. const char * start;
  2857. start = startArg; /* initial value */
  2858. result = doIgnoreSection(parser, parser->m_encoding, &start, end, endPtr);
  2859. if (start) {
  2860. processor = prologProcessor;
  2861. prologProcessor(xmlParserP, start, end, endPtr, errorCodeP, errorP);
  2862. } else {
  2863. *errorCodeP = result;
  2864. *errorP = NULL;
  2865. }
  2866. }
  2867. /* Forward declaration for recursive reference: */
  2868. static void
  2869. processInternalParamEntity(XML_Parser const parser,
  2870. ENTITY * const entity,
  2871. enum XML_Error * const errorCodeP,
  2872. const char ** const errorP);
  2873. static void
  2874. doProlog(XML_Parser const xmlParserP,
  2875. const ENCODING * const encArg,
  2876. const char * const startArg,
  2877. const char * const end,
  2878. int const tokArg,
  2879. const char * const nextArg,
  2880. const char ** const nextPtr,
  2881. enum XML_Error * const errorCodeP,
  2882. const char ** const errorP) {
  2883. Parser * const parser = (Parser *) xmlParserP;
  2884. int tok;
  2885. const char * next;
  2886. const ENCODING * enc;
  2887. const char * s;
  2888. static const XML_Char externalSubsetName[] = { '#' , '\0' };
  2889. const char **eventPP;
  2890. const char **eventEndPP;
  2891. *errorP = NULL;
  2892. tok = tokArg;
  2893. next = nextArg;
  2894. enc = encArg;
  2895. s = startArg;
  2896. if (enc == parser->m_encoding) {
  2897. eventPP = &eventPtr;
  2898. eventEndPP = &eventEndPtr;
  2899. }
  2900. else {
  2901. eventPP = &(openInternalEntities->internalEventPtr);
  2902. eventEndPP = &(openInternalEntities->internalEventEndPtr);
  2903. }
  2904. for (;;) {
  2905. int role;
  2906. *eventPP = s;
  2907. *eventEndPP = next;
  2908. if (tok <= 0) {
  2909. if (nextPtr != 0 && tok != XML_TOK_INVALID) {
  2910. *nextPtr = s;
  2911. *errorCodeP = XML_ERROR_NONE;
  2912. return;
  2913. }
  2914. switch (tok) {
  2915. case XML_TOK_INVALID:
  2916. *eventPP = next;
  2917. *errorCodeP = XML_ERROR_INVALID_TOKEN;
  2918. return;
  2919. case XML_TOK_PARTIAL:
  2920. *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
  2921. return;
  2922. case XML_TOK_PARTIAL_CHAR:
  2923. *errorCodeP = XML_ERROR_PARTIAL_CHAR;
  2924. return;
  2925. case XML_TOK_NONE:
  2926. if (enc != parser->m_encoding)
  2927. *errorCodeP = XML_ERROR_NONE;
  2928. else {
  2929. if (parentParser) {
  2930. if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
  2931. == XML_ROLE_ERROR) {
  2932. *errorCodeP = XML_ERROR_SYNTAX;
  2933. } else {
  2934. *errorCodeP = XML_ERROR_NONE;
  2935. hadExternalDoctype = 0;
  2936. }
  2937. } else
  2938. *errorCodeP = XML_ERROR_NO_ELEMENTS;
  2939. }
  2940. return;
  2941. default:
  2942. tok = -tok;
  2943. next = end;
  2944. break;
  2945. }
  2946. }
  2947. role = XmlTokenRole(&prologState, tok, s, next, enc);
  2948. switch (role) {
  2949. case XML_ROLE_XML_DECL: {
  2950. enum XML_Error result = processXmlDecl(xmlParserP, 0, s, next);
  2951. if (result != XML_ERROR_NONE) {
  2952. *errorCodeP = result;
  2953. return;
  2954. }
  2955. enc = parser->m_encoding;
  2956. }
  2957. break;
  2958. case XML_ROLE_DOCTYPE_NAME:
  2959. if (startDoctypeDeclHandler) {
  2960. const XML_Char *name = poolStoreString(&tempPool, enc, s, next);
  2961. if (!name) {
  2962. *errorCodeP = XML_ERROR_NO_MEMORY;
  2963. return;
  2964. }
  2965. startDoctypeDeclHandler(handlerArg, name);
  2966. poolClear(&tempPool);
  2967. }
  2968. break;
  2969. case XML_ROLE_TEXT_DECL: {
  2970. enum XML_Error result = processXmlDecl(xmlParserP, 1, s, next);
  2971. if (result != XML_ERROR_NONE) {
  2972. *errorCodeP = result;
  2973. return;
  2974. }
  2975. enc = parser->m_encoding;
  2976. }
  2977. break;
  2978. case XML_ROLE_DOCTYPE_PUBLIC_ID:
  2979. declEntity = (ENTITY *)lookup(&dtd.paramEntities,
  2980. externalSubsetName,
  2981. sizeof(ENTITY));
  2982. if (!declEntity) {
  2983. *errorCodeP = XML_ERROR_NO_MEMORY;
  2984. return;
  2985. }
  2986. /* fall through */
  2987. case XML_ROLE_ENTITY_PUBLIC_ID:
  2988. if (!XmlIsPublicId(enc, s, next, eventPP)) {
  2989. *errorCodeP = XML_ERROR_SYNTAX;
  2990. return;
  2991. }
  2992. if (declEntity) {
  2993. XML_Char *tem = poolStoreString(&dtd.pool,
  2994. enc,
  2995. s + enc->minBytesPerChar,
  2996. next - enc->minBytesPerChar);
  2997. if (!tem) {
  2998. *errorCodeP = XML_ERROR_NO_MEMORY;
  2999. return;
  3000. }
  3001. normalizePublicId(tem);
  3002. declEntity->publicId = tem;
  3003. poolFinish(&dtd.pool);
  3004. }
  3005. break;
  3006. case XML_ROLE_DOCTYPE_CLOSE:
  3007. if (dtd.complete && hadExternalDoctype) {
  3008. dtd.complete = 0;
  3009. if (paramEntityParsing && externalEntityRefHandler) {
  3010. ENTITY *entity = (ENTITY *)lookup(&dtd.paramEntities,
  3011. externalSubsetName,
  3012. 0);
  3013. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  3014. 0,
  3015. entity->base,
  3016. entity->systemId,
  3017. entity->publicId)) {
  3018. *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  3019. return;
  3020. }
  3021. }
  3022. if (!dtd.complete
  3023. && !dtd.standalone
  3024. && notStandaloneHandler
  3025. && !notStandaloneHandler(handlerArg)) {
  3026. *errorCodeP = XML_ERROR_NOT_STANDALONE;
  3027. return;
  3028. }
  3029. }
  3030. if (endDoctypeDeclHandler)
  3031. endDoctypeDeclHandler(handlerArg);
  3032. break;
  3033. case XML_ROLE_INSTANCE_START: {
  3034. processor = contentProcessor;
  3035. contentProcessor(xmlParserP, s, end, nextPtr, errorCodeP, errorP);
  3036. return;
  3037. }
  3038. case XML_ROLE_ATTLIST_ELEMENT_NAME:
  3039. {
  3040. const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
  3041. if (!name) {
  3042. *errorCodeP = XML_ERROR_NO_MEMORY;
  3043. return;
  3044. }
  3045. declElementType = (ELEMENT_TYPE *)
  3046. lookup(&dtd.elementTypes, name, sizeof(ELEMENT_TYPE));
  3047. if (!declElementType) {
  3048. *errorCodeP = XML_ERROR_NO_MEMORY;
  3049. return;
  3050. }
  3051. if (declElementType->name != name)
  3052. poolDiscard(&dtd.pool);
  3053. else {
  3054. poolFinish(&dtd.pool);
  3055. if (!setElementTypePrefix(xmlParserP, declElementType)) {
  3056. *errorCodeP = XML_ERROR_NO_MEMORY;
  3057. return;
  3058. }
  3059. }
  3060. break;
  3061. }
  3062. case XML_ROLE_ATTRIBUTE_NAME:
  3063. declAttributeId = getAttributeId(xmlParserP, enc, s, next);
  3064. if (!declAttributeId) {
  3065. *errorCodeP = XML_ERROR_NO_MEMORY;
  3066. return;
  3067. }
  3068. declAttributeIsCdata = 0;
  3069. declAttributeIsId = 0;
  3070. break;
  3071. case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
  3072. declAttributeIsCdata = 1;
  3073. break;
  3074. case XML_ROLE_ATTRIBUTE_TYPE_ID:
  3075. declAttributeIsId = 1;
  3076. break;
  3077. case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
  3078. case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
  3079. if (dtd.complete
  3080. && !defineAttribute(declElementType, declAttributeId,
  3081. declAttributeIsCdata,
  3082. declAttributeIsId, 0)) {
  3083. *errorCodeP = XML_ERROR_NO_MEMORY;
  3084. return;
  3085. }
  3086. break;
  3087. case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
  3088. case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
  3089. {
  3090. const XML_Char *attVal;
  3091. enum XML_Error result
  3092. = storeAttributeValue(xmlParserP, enc, declAttributeIsCdata,
  3093. s + enc->minBytesPerChar,
  3094. next - enc->minBytesPerChar,
  3095. &dtd.pool);
  3096. if (result) {
  3097. *errorCodeP = result;
  3098. return;
  3099. }
  3100. attVal = poolStart(&dtd.pool);
  3101. poolFinish(&dtd.pool);
  3102. if (dtd.complete
  3103. /* ID attributes aren't allowed to have a default */
  3104. && !defineAttribute(declElementType, declAttributeId,
  3105. declAttributeIsCdata, 0, attVal)) {
  3106. *errorCodeP = XML_ERROR_NO_MEMORY;
  3107. return;
  3108. }
  3109. break;
  3110. }
  3111. case XML_ROLE_ENTITY_VALUE:
  3112. {
  3113. enum XML_Error result = storeEntityValue(xmlParserP, enc,
  3114. s + enc->minBytesPerChar,
  3115. next - enc->minBytesPerChar);
  3116. if (declEntity) {
  3117. declEntity->textPtr = poolStart(&dtd.pool);
  3118. declEntity->textLen = poolLength(&dtd.pool);
  3119. poolFinish(&dtd.pool);
  3120. if (internalParsedEntityDeclHandler
  3121. /* Check it's not a parameter entity */
  3122. && ((ENTITY *)lookup(&dtd.generalEntities, declEntity->name, 0)
  3123. == declEntity)) {
  3124. *eventEndPP = s;
  3125. internalParsedEntityDeclHandler(handlerArg,
  3126. declEntity->name,
  3127. declEntity->textPtr,
  3128. declEntity->textLen);
  3129. }
  3130. }
  3131. else
  3132. poolDiscard(&dtd.pool);
  3133. if (result != XML_ERROR_NONE) {
  3134. *errorCodeP = result;
  3135. return;
  3136. }
  3137. }
  3138. break;
  3139. case XML_ROLE_DOCTYPE_SYSTEM_ID:
  3140. if (!dtd.standalone
  3141. && !paramEntityParsing
  3142. && notStandaloneHandler
  3143. && !notStandaloneHandler(handlerArg)) {
  3144. *errorCodeP = XML_ERROR_NOT_STANDALONE;
  3145. return;
  3146. }
  3147. hadExternalDoctype = 1;
  3148. if (!declEntity) {
  3149. declEntity = (ENTITY *)lookup(&dtd.paramEntities,
  3150. externalSubsetName,
  3151. sizeof(ENTITY));
  3152. if (!declEntity) {
  3153. *errorCodeP = XML_ERROR_NO_MEMORY;
  3154. return;
  3155. }
  3156. }
  3157. /* fall through */
  3158. case XML_ROLE_ENTITY_SYSTEM_ID:
  3159. if (declEntity) {
  3160. declEntity->systemId = poolStoreString(&dtd.pool, enc,
  3161. s + enc->minBytesPerChar,
  3162. next - enc->minBytesPerChar);
  3163. if (!declEntity->systemId) {
  3164. *errorCodeP = XML_ERROR_NO_MEMORY;
  3165. return;
  3166. }
  3167. declEntity->base = curBase;
  3168. poolFinish(&dtd.pool);
  3169. }
  3170. break;
  3171. case XML_ROLE_ENTITY_NOTATION_NAME:
  3172. if (declEntity) {
  3173. declEntity->notation = poolStoreString(&dtd.pool, enc, s, next);
  3174. if (!declEntity->notation) {
  3175. *errorCodeP = XML_ERROR_NO_MEMORY;
  3176. return;
  3177. }
  3178. poolFinish(&dtd.pool);
  3179. if (unparsedEntityDeclHandler) {
  3180. *eventEndPP = s;
  3181. unparsedEntityDeclHandler(handlerArg,
  3182. declEntity->name,
  3183. declEntity->base,
  3184. declEntity->systemId,
  3185. declEntity->publicId,
  3186. declEntity->notation);
  3187. }
  3188. }
  3189. break;
  3190. case XML_ROLE_EXTERNAL_GENERAL_ENTITY_NO_NOTATION:
  3191. if (declEntity && externalParsedEntityDeclHandler) {
  3192. *eventEndPP = s;
  3193. externalParsedEntityDeclHandler(handlerArg,
  3194. declEntity->name,
  3195. declEntity->base,
  3196. declEntity->systemId,
  3197. declEntity->publicId);
  3198. }
  3199. break;
  3200. case XML_ROLE_GENERAL_ENTITY_NAME:
  3201. {
  3202. const XML_Char *name;
  3203. if (XmlPredefinedEntityName(enc, s, next)) {
  3204. declEntity = 0;
  3205. break;
  3206. }
  3207. name = poolStoreString(&dtd.pool, enc, s, next);
  3208. if (!name) {
  3209. *errorCodeP = XML_ERROR_NO_MEMORY;
  3210. return;
  3211. }
  3212. if (dtd.complete) {
  3213. declEntity = (ENTITY *)
  3214. lookup(&dtd.generalEntities, name, sizeof(ENTITY));
  3215. if (!declEntity) {
  3216. *errorCodeP = XML_ERROR_NO_MEMORY;
  3217. return;
  3218. }
  3219. if (declEntity->name != name) {
  3220. poolDiscard(&dtd.pool);
  3221. declEntity = 0;
  3222. }
  3223. else
  3224. poolFinish(&dtd.pool);
  3225. }
  3226. else {
  3227. poolDiscard(&dtd.pool);
  3228. declEntity = 0;
  3229. }
  3230. }
  3231. break;
  3232. case XML_ROLE_PARAM_ENTITY_NAME:
  3233. if (dtd.complete) {
  3234. const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
  3235. if (!name) {
  3236. *errorCodeP = XML_ERROR_NO_MEMORY;
  3237. return;
  3238. }
  3239. declEntity = (ENTITY *)
  3240. lookup(&dtd.paramEntities, name, sizeof(ENTITY));
  3241. if (!declEntity) {
  3242. *errorCodeP = XML_ERROR_NO_MEMORY;
  3243. return;
  3244. }
  3245. if (declEntity->name != name) {
  3246. poolDiscard(&dtd.pool);
  3247. declEntity = 0;
  3248. }
  3249. else
  3250. poolFinish(&dtd.pool);
  3251. }
  3252. break;
  3253. case XML_ROLE_NOTATION_NAME:
  3254. declNotationPublicId = 0;
  3255. declNotationName = 0;
  3256. if (notationDeclHandler) {
  3257. declNotationName = poolStoreString(&tempPool, enc, s, next);
  3258. if (!declNotationName) {
  3259. *errorCodeP = XML_ERROR_NO_MEMORY;
  3260. return;
  3261. }
  3262. poolFinish(&tempPool);
  3263. }
  3264. break;
  3265. case XML_ROLE_NOTATION_PUBLIC_ID:
  3266. if (!XmlIsPublicId(enc, s, next, eventPP)) {
  3267. *errorCodeP = XML_ERROR_SYNTAX;
  3268. return;
  3269. }
  3270. if (declNotationName) {
  3271. XML_Char *tem = poolStoreString(&tempPool,
  3272. enc,
  3273. s + enc->minBytesPerChar,
  3274. next - enc->minBytesPerChar);
  3275. if (!tem) {
  3276. *errorCodeP = XML_ERROR_NO_MEMORY;
  3277. return;
  3278. }
  3279. normalizePublicId(tem);
  3280. declNotationPublicId = tem;
  3281. poolFinish(&tempPool);
  3282. }
  3283. break;
  3284. case XML_ROLE_NOTATION_SYSTEM_ID:
  3285. if (declNotationName && notationDeclHandler) {
  3286. const XML_Char *systemId
  3287. = poolStoreString(&tempPool, enc,
  3288. s + enc->minBytesPerChar,
  3289. next - enc->minBytesPerChar);
  3290. if (!systemId) {
  3291. *errorCodeP = XML_ERROR_NO_MEMORY;
  3292. return;
  3293. }
  3294. *eventEndPP = s;
  3295. notationDeclHandler(handlerArg,
  3296. declNotationName,
  3297. curBase,
  3298. systemId,
  3299. declNotationPublicId);
  3300. }
  3301. poolClear(&tempPool);
  3302. break;
  3303. case XML_ROLE_NOTATION_NO_SYSTEM_ID:
  3304. if (declNotationPublicId && notationDeclHandler) {
  3305. *eventEndPP = s;
  3306. notationDeclHandler(handlerArg,
  3307. declNotationName,
  3308. curBase,
  3309. 0,
  3310. declNotationPublicId);
  3311. }
  3312. poolClear(&tempPool);
  3313. break;
  3314. case XML_ROLE_ERROR:
  3315. switch (tok) {
  3316. case XML_TOK_PARAM_ENTITY_REF:
  3317. *errorCodeP = XML_ERROR_PARAM_ENTITY_REF;
  3318. case XML_TOK_XML_DECL:
  3319. *errorCodeP = XML_ERROR_MISPLACED_XML_PI;
  3320. default:
  3321. *errorCodeP = XML_ERROR_SYNTAX;
  3322. }
  3323. return;
  3324. case XML_ROLE_IGNORE_SECT:
  3325. {
  3326. enum XML_Error result;
  3327. if (defaultHandler)
  3328. reportDefault(xmlParserP, enc, s, next);
  3329. result = doIgnoreSection(xmlParserP, enc, &next, end, nextPtr);
  3330. if (!next) {
  3331. processor = ignoreSectionProcessor;
  3332. *errorCodeP = result;
  3333. return;
  3334. }
  3335. }
  3336. break;
  3337. case XML_ROLE_GROUP_OPEN:
  3338. if (prologState.level >= groupSize) {
  3339. if (groupSize)
  3340. groupConnector = realloc(groupConnector, groupSize *= 2);
  3341. else
  3342. groupConnector = malloc(groupSize = 32);
  3343. if (!groupConnector) {
  3344. *errorCodeP = XML_ERROR_NO_MEMORY;
  3345. return;
  3346. }
  3347. }
  3348. groupConnector[prologState.level] = 0;
  3349. break;
  3350. case XML_ROLE_GROUP_SEQUENCE:
  3351. if (groupConnector[prologState.level] == '|') {
  3352. *errorCodeP = XML_ERROR_SYNTAX;
  3353. return;
  3354. }
  3355. groupConnector[prologState.level] = ',';
  3356. break;
  3357. case XML_ROLE_GROUP_CHOICE:
  3358. if (groupConnector[prologState.level] == ',') {
  3359. *errorCodeP = XML_ERROR_SYNTAX;
  3360. return;
  3361. }
  3362. groupConnector[prologState.level] = '|';
  3363. break;
  3364. case XML_ROLE_PARAM_ENTITY_REF:
  3365. case XML_ROLE_INNER_PARAM_ENTITY_REF:
  3366. if (paramEntityParsing
  3367. && (dtd.complete || role == XML_ROLE_INNER_PARAM_ENTITY_REF)) {
  3368. const XML_Char *name;
  3369. ENTITY *entity;
  3370. name = poolStoreString(&dtd.pool, enc,
  3371. s + enc->minBytesPerChar,
  3372. next - enc->minBytesPerChar);
  3373. if (!name) {
  3374. *errorCodeP = XML_ERROR_NO_MEMORY;
  3375. return;
  3376. }
  3377. entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
  3378. poolDiscard(&dtd.pool);
  3379. if (!entity) {
  3380. /* FIXME what to do if !dtd.complete? */
  3381. *errorCodeP = XML_ERROR_UNDEFINED_ENTITY;
  3382. return;
  3383. }
  3384. if (entity->open) {
  3385. *errorCodeP = XML_ERROR_RECURSIVE_ENTITY_REF;
  3386. return;
  3387. }
  3388. if (entity->textPtr) {
  3389. processInternalParamEntity(xmlParserP, entity, errorCodeP, errorP);
  3390. if (*errorCodeP != XML_ERROR_NONE)
  3391. return;
  3392. break;
  3393. }
  3394. if (role == XML_ROLE_INNER_PARAM_ENTITY_REF) {
  3395. *errorCodeP = XML_ERROR_PARAM_ENTITY_REF;
  3396. return;
  3397. }
  3398. if (externalEntityRefHandler) {
  3399. dtd.complete = 0;
  3400. entity->open = 1;
  3401. if (!externalEntityRefHandler(externalEntityRefHandlerArg,
  3402. 0,
  3403. entity->base,
  3404. entity->systemId,
  3405. entity->publicId)) {
  3406. entity->open = 0;
  3407. *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
  3408. return;
  3409. }
  3410. entity->open = 0;
  3411. if (dtd.complete)
  3412. break;
  3413. }
  3414. }
  3415. if (!dtd.standalone
  3416. && notStandaloneHandler
  3417. && !notStandaloneHandler(handlerArg)) {
  3418. *errorCodeP = XML_ERROR_NOT_STANDALONE;
  3419. return;
  3420. }
  3421. dtd.complete = 0;
  3422. if (defaultHandler)
  3423. reportDefault(xmlParserP, enc, s, next);
  3424. break;
  3425. case XML_ROLE_NONE:
  3426. switch (tok) {
  3427. case XML_TOK_PI:
  3428. if (!reportProcessingInstruction(xmlParserP, enc, s, next)) {
  3429. *errorCodeP = XML_ERROR_NO_MEMORY;
  3430. return;
  3431. }
  3432. break;
  3433. case XML_TOK_COMMENT:
  3434. if (!reportComment(xmlParserP, enc, s, next)) {
  3435. *errorCodeP = XML_ERROR_NO_MEMORY;
  3436. return;
  3437. }
  3438. break;
  3439. }
  3440. break;
  3441. }
  3442. if (defaultHandler) {
  3443. switch (tok) {
  3444. case XML_TOK_PI:
  3445. case XML_TOK_COMMENT:
  3446. case XML_TOK_BOM:
  3447. case XML_TOK_XML_DECL:
  3448. case XML_TOK_IGNORE_SECT:
  3449. case XML_TOK_PARAM_ENTITY_REF:
  3450. break;
  3451. default:
  3452. if (role != XML_ROLE_IGNORE_SECT)
  3453. reportDefault(xmlParserP, enc, s, next);
  3454. }
  3455. }
  3456. s = next;
  3457. tok = XmlPrologTok(enc, s, end, &next);
  3458. }
  3459. /* not reached */
  3460. }
  3461. static Processor prologProcessor;
  3462. static void
  3463. prologProcessor(XML_Parser const xmlParserP,
  3464. const char * const s,
  3465. const char * const end,
  3466. const char ** const nextPtr,
  3467. enum XML_Error * const errorCodeP,
  3468. const char ** const errorP) {
  3469. Parser * const parser = (Parser *) xmlParserP;
  3470. const char * next;
  3471. int tok;
  3472. *errorP = NULL;
  3473. tok = XmlPrologTok(parser->m_encoding, s, end, &next);
  3474. doProlog(xmlParserP, parser->m_encoding, s, end, tok, next, nextPtr,
  3475. errorCodeP, errorP);
  3476. }
  3477. static Processor prologInitProcessor;
  3478. static void
  3479. prologInitProcessor(XML_Parser const parser,
  3480. const char * const s,
  3481. const char * const end,
  3482. const char ** const nextPtr,
  3483. enum XML_Error * const errorCodeP,
  3484. const char ** const errorP) {
  3485. enum XML_Error result;
  3486. *errorP = NULL;
  3487. result = initializeEncoding(parser);
  3488. if (result != XML_ERROR_NONE)
  3489. *errorCodeP = result;
  3490. else {
  3491. processor = prologProcessor;
  3492. prologProcessor(parser, s, end, nextPtr, errorCodeP, errorP);
  3493. }
  3494. }
  3495. static void
  3496. processInternalParamEntity(XML_Parser const parser,
  3497. ENTITY * const entity,
  3498. enum XML_Error * const errorCodeP,
  3499. const char ** const errorP) {
  3500. const char *s, *end, *next;
  3501. int tok;
  3502. OPEN_INTERNAL_ENTITY openEntity;
  3503. entity->open = 1;
  3504. openEntity.next = openInternalEntities;
  3505. openInternalEntities = &openEntity;
  3506. openEntity.entity = entity;
  3507. openEntity.internalEventPtr = 0;
  3508. openEntity.internalEventEndPtr = 0;
  3509. s = (char *)entity->textPtr;
  3510. end = (char *)(entity->textPtr + entity->textLen);
  3511. tok = XmlPrologTok(internalEncoding, s, end, &next);
  3512. doProlog(parser, internalEncoding, s, end, tok, next, 0,
  3513. errorCodeP, errorP);
  3514. entity->open = 0;
  3515. openInternalEntities = openEntity.next;
  3516. }
  3517. XML_Parser
  3518. xmlrpc_XML_ParserCreate(const XML_Char * const encodingName) {
  3519. XML_Parser const xmlParserP = malloc(sizeof(Parser));
  3520. Parser * const parser = (Parser *)xmlParserP;
  3521. if (xmlParserP) {
  3522. processor = prologInitProcessor;
  3523. xmlrpc_XmlPrologStateInit(&prologState);
  3524. userData = 0;
  3525. handlerArg = 0;
  3526. startElementHandler = 0;
  3527. endElementHandler = 0;
  3528. characterDataHandler = 0;
  3529. processingInstructionHandler = 0;
  3530. commentHandler = 0;
  3531. startCdataSectionHandler = 0;
  3532. endCdataSectionHandler = 0;
  3533. defaultHandler = 0;
  3534. startDoctypeDeclHandler = 0;
  3535. endDoctypeDeclHandler = 0;
  3536. unparsedEntityDeclHandler = 0;
  3537. notationDeclHandler = 0;
  3538. externalParsedEntityDeclHandler = 0;
  3539. internalParsedEntityDeclHandler = 0;
  3540. startNamespaceDeclHandler = 0;
  3541. endNamespaceDeclHandler = 0;
  3542. notStandaloneHandler = 0;
  3543. externalEntityRefHandler = 0;
  3544. externalEntityRefHandlerArg = parser;
  3545. unknownEncodingHandler = 0;
  3546. buffer = 0;
  3547. bufferPtr = 0;
  3548. bufferEnd = 0;
  3549. parseEndByteIndex = 0;
  3550. parseEndPtr = 0;
  3551. bufferLim = 0;
  3552. declElementType = 0;
  3553. declAttributeId = 0;
  3554. declEntity = 0;
  3555. declNotationName = 0;
  3556. declNotationPublicId = 0;
  3557. memset(&position, 0, sizeof(POSITION));
  3558. errorCode = XML_ERROR_NONE;
  3559. errorString = NULL;
  3560. eventPtr = 0;
  3561. eventEndPtr = 0;
  3562. positionPtr = 0;
  3563. openInternalEntities = 0;
  3564. tagLevel = 0;
  3565. tagStack = 0;
  3566. freeTagList = 0;
  3567. freeBindingList = 0;
  3568. inheritedBindings = 0;
  3569. attsSize = INIT_ATTS_SIZE;
  3570. atts = malloc(attsSize * sizeof(ATTRIBUTE));
  3571. nSpecifiedAtts = 0;
  3572. dataBuf = malloc(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
  3573. groupSize = 0;
  3574. groupConnector = 0;
  3575. hadExternalDoctype = 0;
  3576. unknownEncodingMem = 0;
  3577. unknownEncodingRelease = 0;
  3578. unknownEncodingData = 0;
  3579. unknownEncodingHandlerData = 0;
  3580. namespaceSeparator = '!';
  3581. parentParser = 0;
  3582. paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
  3583. ns = 0;
  3584. poolInit(&tempPool);
  3585. poolInit(&temp2Pool);
  3586. protocolEncodingName =
  3587. encodingName ? poolCopyString(&tempPool, encodingName) : 0;
  3588. curBase = 0;
  3589. if (!dtdInit(&dtd) || !atts || !dataBuf
  3590. || (encodingName && !protocolEncodingName)) {
  3591. xmlrpc_XML_ParserFree(xmlParserP);
  3592. return 0;
  3593. }
  3594. dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
  3595. xmlrpc_XmlInitEncoding(&parser->m_initEncoding,
  3596. &parser->m_encoding,
  3597. 0);
  3598. internalEncoding = XmlGetInternalEncoding();
  3599. }
  3600. return xmlParserP;
  3601. }
  3602. XML_Parser
  3603. xmlrpc_XML_ParserCreateNS(const XML_Char * const encodingName,
  3604. XML_Char const nsSep) {
  3605. static
  3606. const XML_Char implicitContext[] = {
  3607. XML_T('x'), XML_T('m'), XML_T('l'), XML_T('='),
  3608. XML_T('h'), XML_T('t'), XML_T('t'), XML_T('p'), XML_T(':'),
  3609. XML_T('/'), XML_T('/'), XML_T('w'), XML_T('w'), XML_T('w'),
  3610. XML_T('.'), XML_T('w'), XML_T('3'),
  3611. XML_T('.'), XML_T('o'), XML_T('r'), XML_T('g'),
  3612. XML_T('/'), XML_T('X'), XML_T('M'), XML_T('L'),
  3613. XML_T('/'), XML_T('1'), XML_T('9'), XML_T('9'), XML_T('8'),
  3614. XML_T('/'), XML_T('n'), XML_T('a'), XML_T('m'), XML_T('e'),
  3615. XML_T('s'), XML_T('p'), XML_T('a'), XML_T('c'), XML_T('e'),
  3616. XML_T('\0')
  3617. };
  3618. XML_Parser const xmlParserP = xmlrpc_XML_ParserCreate(encodingName);
  3619. Parser * const parser = (Parser *)xmlParserP;
  3620. XML_Parser retval;
  3621. if (xmlParserP) {
  3622. int succeeded;
  3623. xmlrpc_XmlInitEncodingNS(&initEncoding, &parser->m_encoding, 0);
  3624. ns = 1;
  3625. internalEncoding = XmlGetInternalEncodingNS();
  3626. namespaceSeparator = nsSep;
  3627. succeeded = setContext(xmlParserP, implicitContext);
  3628. if (succeeded)
  3629. retval = xmlParserP;
  3630. else {
  3631. xmlrpc_XML_ParserFree(xmlParserP);
  3632. retval = NULL;
  3633. }
  3634. } else
  3635. retval = NULL;
  3636. return retval;
  3637. }
  3638. int
  3639. xmlrpc_XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
  3640. {
  3641. if (!encodingName)
  3642. protocolEncodingName = 0;
  3643. else {
  3644. protocolEncodingName = poolCopyString(&tempPool, encodingName);
  3645. if (!protocolEncodingName)
  3646. return 0;
  3647. }
  3648. return 1;
  3649. }
  3650. XML_Parser
  3651. xmlrpc_XML_ExternalEntityParserCreate(XML_Parser oldParser,
  3652. const XML_Char *context,
  3653. const XML_Char *encodingName)
  3654. {
  3655. XML_Parser parser = oldParser;
  3656. DTD *oldDtd = &dtd;
  3657. XML_StartElementHandler oldStartElementHandler = startElementHandler;
  3658. XML_EndElementHandler oldEndElementHandler = endElementHandler;
  3659. XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler;
  3660. XML_ProcessingInstructionHandler oldProcessingInstructionHandler = processingInstructionHandler;
  3661. XML_CommentHandler oldCommentHandler = commentHandler;
  3662. XML_StartCdataSectionHandler oldStartCdataSectionHandler = startCdataSectionHandler;
  3663. XML_EndCdataSectionHandler oldEndCdataSectionHandler = endCdataSectionHandler;
  3664. XML_DefaultHandler oldDefaultHandler = defaultHandler;
  3665. XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler = unparsedEntityDeclHandler;
  3666. XML_NotationDeclHandler oldNotationDeclHandler = notationDeclHandler;
  3667. XML_ExternalParsedEntityDeclHandler oldExternalParsedEntityDeclHandler = externalParsedEntityDeclHandler;
  3668. XML_InternalParsedEntityDeclHandler oldInternalParsedEntityDeclHandler = internalParsedEntityDeclHandler;
  3669. XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler = startNamespaceDeclHandler;
  3670. XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler = endNamespaceDeclHandler;
  3671. XML_NotStandaloneHandler oldNotStandaloneHandler = notStandaloneHandler;
  3672. XML_ExternalEntityRefHandler oldExternalEntityRefHandler = externalEntityRefHandler;
  3673. XML_UnknownEncodingHandler oldUnknownEncodingHandler = unknownEncodingHandler;
  3674. void *oldUserData = userData;
  3675. void *oldHandlerArg = handlerArg;
  3676. int oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
  3677. void *oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
  3678. int oldParamEntityParsing = paramEntityParsing;
  3679. parser = (ns
  3680. ? xmlrpc_XML_ParserCreateNS(encodingName, namespaceSeparator)
  3681. : xmlrpc_XML_ParserCreate(encodingName));
  3682. if (!parser)
  3683. return 0;
  3684. startElementHandler = oldStartElementHandler;
  3685. endElementHandler = oldEndElementHandler;
  3686. characterDataHandler = oldCharacterDataHandler;
  3687. processingInstructionHandler = oldProcessingInstructionHandler;
  3688. commentHandler = oldCommentHandler;
  3689. startCdataSectionHandler = oldStartCdataSectionHandler;
  3690. endCdataSectionHandler = oldEndCdataSectionHandler;
  3691. defaultHandler = oldDefaultHandler;
  3692. unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
  3693. notationDeclHandler = oldNotationDeclHandler;
  3694. externalParsedEntityDeclHandler = oldExternalParsedEntityDeclHandler;
  3695. internalParsedEntityDeclHandler = oldInternalParsedEntityDeclHandler;
  3696. startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
  3697. endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
  3698. notStandaloneHandler = oldNotStandaloneHandler;
  3699. externalEntityRefHandler = oldExternalEntityRefHandler;
  3700. unknownEncodingHandler = oldUnknownEncodingHandler;
  3701. userData = oldUserData;
  3702. if (oldUserData == oldHandlerArg)
  3703. handlerArg = userData;
  3704. else
  3705. handlerArg = parser;
  3706. if (oldExternalEntityRefHandlerArg != oldParser)
  3707. externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
  3708. defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
  3709. paramEntityParsing = oldParamEntityParsing;
  3710. if (context) {
  3711. if (!dtdCopy(&dtd, oldDtd) || !setContext(parser, context)) {
  3712. xmlrpc_XML_ParserFree(parser);
  3713. return 0;
  3714. }
  3715. processor = externalEntityInitProcessor;
  3716. }
  3717. else {
  3718. dtdSwap(&dtd, oldDtd);
  3719. parentParser = oldParser;
  3720. xmlrpc_XmlPrologStateInitExternalEntity(&prologState);
  3721. dtd.complete = 1;
  3722. hadExternalDoctype = 1;
  3723. }
  3724. return parser;
  3725. }
  3726. static
  3727. void destroyBindings(BINDING *bindings)
  3728. {
  3729. for (;;) {
  3730. BINDING *b = bindings;
  3731. if (!b)
  3732. break;
  3733. bindings = b->nextTagBinding;
  3734. free(b->uri);
  3735. free(b);
  3736. }
  3737. }
  3738. void
  3739. xmlrpc_XML_ParserFree(XML_Parser parser)
  3740. {
  3741. for (;;) {
  3742. TAG *p;
  3743. if (tagStack == 0) {
  3744. if (freeTagList == 0)
  3745. break;
  3746. tagStack = freeTagList;
  3747. freeTagList = 0;
  3748. }
  3749. p = tagStack;
  3750. tagStack = tagStack->parent;
  3751. free(p->buf);
  3752. destroyBindings(p->bindings);
  3753. free(p);
  3754. }
  3755. destroyBindings(freeBindingList);
  3756. destroyBindings(inheritedBindings);
  3757. poolDestroy(&tempPool);
  3758. poolDestroy(&temp2Pool);
  3759. if (parentParser) {
  3760. if (hadExternalDoctype)
  3761. dtd.complete = 0;
  3762. dtdSwap(&dtd, &((Parser *)parentParser)->m_dtd);
  3763. }
  3764. dtdDestroy(&dtd);
  3765. free((void *)atts);
  3766. free(groupConnector);
  3767. free(buffer);
  3768. free(dataBuf);
  3769. free(unknownEncodingMem);
  3770. if (unknownEncodingRelease)
  3771. unknownEncodingRelease(unknownEncodingData);
  3772. if (errorString)
  3773. xmlrpc_strfree(errorString);
  3774. free(parser);
  3775. }
  3776. void
  3777. xmlrpc_XML_UseParserAsHandlerArg(XML_Parser parser)
  3778. {
  3779. handlerArg = parser;
  3780. }
  3781. void
  3782. xmlrpc_XML_SetUserData(XML_Parser parser, void *p)
  3783. {
  3784. if (handlerArg == userData)
  3785. handlerArg = userData = p;
  3786. else
  3787. userData = p;
  3788. }
  3789. int
  3790. xmlrpc_XML_SetBase(XML_Parser parser, const XML_Char *p)
  3791. {
  3792. if (p) {
  3793. p = poolCopyString(&dtd.pool, p);
  3794. if (!p)
  3795. return 0;
  3796. curBase = p;
  3797. }
  3798. else
  3799. curBase = 0;
  3800. return 1;
  3801. }
  3802. const XML_Char *
  3803. xmlrpc_XML_GetBase(XML_Parser parser)
  3804. {
  3805. return curBase;
  3806. }
  3807. int
  3808. xmlrpc_XML_GetSpecifiedAttributeCount(XML_Parser parser)
  3809. {
  3810. return nSpecifiedAtts;
  3811. }
  3812. int
  3813. xmlrpc_XML_GetIdAttributeIndex(XML_Parser parser)
  3814. {
  3815. return idAttIndex;
  3816. }
  3817. void
  3818. xmlrpc_XML_SetElementHandler(XML_Parser parser,
  3819. XML_StartElementHandler start,
  3820. XML_EndElementHandler end)
  3821. {
  3822. startElementHandler = start;
  3823. endElementHandler = end;
  3824. }
  3825. void
  3826. xmlrpc_XML_SetCharacterDataHandler(XML_Parser parser,
  3827. XML_CharacterDataHandler handler)
  3828. {
  3829. characterDataHandler = handler;
  3830. }
  3831. void
  3832. xmlrpc_XML_SetProcessingInstructionHandler(
  3833. XML_Parser parser,
  3834. XML_ProcessingInstructionHandler handler)
  3835. {
  3836. processingInstructionHandler = handler;
  3837. }
  3838. void
  3839. xmlrpc_XML_SetCommentHandler(XML_Parser parser,
  3840. XML_CommentHandler handler)
  3841. {
  3842. commentHandler = handler;
  3843. }
  3844. void
  3845. xmlrpc_XML_SetCdataSectionHandler(XML_Parser parser,
  3846. XML_StartCdataSectionHandler start,
  3847. XML_EndCdataSectionHandler end)
  3848. {
  3849. startCdataSectionHandler = start;
  3850. endCdataSectionHandler = end;
  3851. }
  3852. void
  3853. xmlrpc_XML_SetDefaultHandler(XML_Parser parser,
  3854. XML_DefaultHandler handler)
  3855. {
  3856. defaultHandler = handler;
  3857. defaultExpandInternalEntities = 0;
  3858. }
  3859. void
  3860. xmlrpc_XML_SetDefaultHandlerExpand(XML_Parser parser,
  3861. XML_DefaultHandler handler)
  3862. {
  3863. defaultHandler = handler;
  3864. defaultExpandInternalEntities = 1;
  3865. }
  3866. void
  3867. xmlrpc_XML_SetDoctypeDeclHandler(XML_Parser parser,
  3868. XML_StartDoctypeDeclHandler start,
  3869. XML_EndDoctypeDeclHandler end)
  3870. {
  3871. startDoctypeDeclHandler = start;
  3872. endDoctypeDeclHandler = end;
  3873. }
  3874. void
  3875. xmlrpc_XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
  3876. XML_UnparsedEntityDeclHandler handler)
  3877. {
  3878. unparsedEntityDeclHandler = handler;
  3879. }
  3880. void
  3881. xmlrpc_XML_SetExternalParsedEntityDeclHandler(
  3882. XML_Parser parser,
  3883. XML_ExternalParsedEntityDeclHandler handler)
  3884. {
  3885. externalParsedEntityDeclHandler = handler;
  3886. }
  3887. void
  3888. xmlrpc_XML_SetInternalParsedEntityDeclHandler(
  3889. XML_Parser parser,
  3890. XML_InternalParsedEntityDeclHandler handler)
  3891. {
  3892. internalParsedEntityDeclHandler = handler;
  3893. }
  3894. void
  3895. xmlrpc_XML_SetNotationDeclHandler(XML_Parser parser,
  3896. XML_NotationDeclHandler handler)
  3897. {
  3898. notationDeclHandler = handler;
  3899. }
  3900. void
  3901. xmlrpc_XML_SetNamespaceDeclHandler(XML_Parser parser,
  3902. XML_StartNamespaceDeclHandler start,
  3903. XML_EndNamespaceDeclHandler end)
  3904. {
  3905. startNamespaceDeclHandler = start;
  3906. endNamespaceDeclHandler = end;
  3907. }
  3908. void
  3909. xmlrpc_XML_SetNotStandaloneHandler(XML_Parser parser,
  3910. XML_NotStandaloneHandler handler)
  3911. {
  3912. notStandaloneHandler = handler;
  3913. }
  3914. void
  3915. xmlrpc_XML_SetExternalEntityRefHandler(XML_Parser parser,
  3916. XML_ExternalEntityRefHandler handler)
  3917. {
  3918. externalEntityRefHandler = handler;
  3919. }
  3920. void
  3921. xmlrpc_XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
  3922. {
  3923. if (arg)
  3924. externalEntityRefHandlerArg = arg;
  3925. else
  3926. externalEntityRefHandlerArg = parser;
  3927. }
  3928. void
  3929. xmlrpc_XML_SetUnknownEncodingHandler(XML_Parser parser,
  3930. XML_UnknownEncodingHandler handler,
  3931. void *data)
  3932. {
  3933. unknownEncodingHandler = handler;
  3934. unknownEncodingHandlerData = data;
  3935. }
  3936. int
  3937. xmlrpc_XML_SetParamEntityParsing(
  3938. XML_Parser const parser ATTR_UNUSED,
  3939. enum XML_ParamEntityParsing const parsing) {
  3940. int retval;
  3941. paramEntityParsing = parsing;
  3942. retval = 1;
  3943. return retval;
  3944. }
  3945. static Processor errorProcessor;
  3946. static void
  3947. errorProcessor(XML_Parser const parser ATTR_UNUSED,
  3948. const char * const s ATTR_UNUSED,
  3949. const char * const end ATTR_UNUSED,
  3950. const char ** const nextPtr ATTR_UNUSED,
  3951. enum XML_Error * const errorCodeP,
  3952. const char ** const errorP) {
  3953. *errorP = errorString;
  3954. *errorCodeP = errorCode;
  3955. }
  3956. static void
  3957. parseFinalLen0(Parser * const parser,
  3958. int * const retvalP) {
  3959. positionPtr = bufferPtr;
  3960. parseEndPtr = bufferEnd;
  3961. processor(parser, bufferPtr, bufferEnd, 0, &errorCode, &errorString);
  3962. if (errorCode == XML_ERROR_NONE)
  3963. *retvalP = 1;
  3964. else {
  3965. eventEndPtr = eventPtr;
  3966. processor = errorProcessor;
  3967. *retvalP = 0;
  3968. }
  3969. }
  3970. static void
  3971. parseNoBuffer(Parser * const parser,
  3972. const char * const s,
  3973. size_t const len,
  3974. bool const isFinal,
  3975. int * const succeededP) {
  3976. parseEndByteIndex += len;
  3977. positionPtr = s;
  3978. if (isFinal) {
  3979. parseEndPtr = s + len;
  3980. processor(parser, s, parseEndPtr, 0, &errorCode, &errorString);
  3981. if (errorCode == XML_ERROR_NONE)
  3982. *succeededP = true;
  3983. else {
  3984. eventEndPtr = eventPtr;
  3985. processor = errorProcessor;
  3986. *succeededP = false;
  3987. }
  3988. } else {
  3989. const char * end;
  3990. parseEndPtr = s + len;
  3991. processor(parser, s, s + len, &end, &errorCode, &errorString);
  3992. if (errorCode != XML_ERROR_NONE) {
  3993. eventEndPtr = eventPtr;
  3994. processor = errorProcessor;
  3995. *succeededP = false;
  3996. } else {
  3997. int const nLeftOver = s + len - end;
  3998. XmlUpdatePosition(parser->m_encoding, positionPtr, end, &position);
  3999. if (nLeftOver > 0) {
  4000. if (buffer == 0 || nLeftOver > bufferLim - buffer) {
  4001. REALLOCARRAY(buffer, len * 2);
  4002. if (buffer)
  4003. bufferLim = buffer + len * 2;
  4004. }
  4005. if (buffer) {
  4006. memcpy(buffer, end, nLeftOver);
  4007. bufferPtr = buffer;
  4008. bufferEnd = buffer + nLeftOver;
  4009. *succeededP = true;
  4010. } else {
  4011. errorCode = XML_ERROR_NO_MEMORY;
  4012. eventPtr = eventEndPtr = 0;
  4013. processor = errorProcessor;
  4014. *succeededP = false;
  4015. }
  4016. } else
  4017. *succeededP = true;
  4018. }
  4019. }
  4020. }
  4021. int
  4022. xmlrpc_XML_Parse(XML_Parser const xmlParserP,
  4023. const char * const s,
  4024. size_t const len,
  4025. int const isFinal) {
  4026. Parser * const parser = (Parser *) xmlParserP;
  4027. int retval;
  4028. if (errorString) {
  4029. xmlrpc_strfree(errorString);
  4030. errorString = NULL;
  4031. }
  4032. if (len == 0) {
  4033. if (!isFinal)
  4034. retval = 1;
  4035. else
  4036. parseFinalLen0(parser, &retval);
  4037. } else if (bufferPtr == bufferEnd)
  4038. parseNoBuffer(parser, s, len, isFinal, &retval);
  4039. else {
  4040. memcpy(xmlrpc_XML_GetBuffer(parser, len), s, len);
  4041. retval = xmlrpc_XML_ParseBuffer(parser, len, isFinal);
  4042. }
  4043. return retval;
  4044. }
  4045. int
  4046. xmlrpc_XML_ParseBuffer(XML_Parser const xmlParserP,
  4047. int const len,
  4048. int const isFinal) {
  4049. Parser * const parser = (Parser *)xmlParserP;
  4050. const char * const start = bufferPtr;
  4051. if (errorString) {
  4052. xmlrpc_strfree(errorString);
  4053. errorString = NULL;
  4054. }
  4055. positionPtr = start;
  4056. bufferEnd += len;
  4057. parseEndByteIndex += len;
  4058. processor(xmlParserP, start, parseEndPtr = bufferEnd,
  4059. isFinal ? (const char **)0 : &bufferPtr,
  4060. &errorCode, &errorString);
  4061. if (errorCode == XML_ERROR_NONE) {
  4062. if (!isFinal)
  4063. XmlUpdatePosition(parser->m_encoding, positionPtr, bufferPtr,
  4064. &position);
  4065. return 1;
  4066. } else {
  4067. eventEndPtr = eventPtr;
  4068. processor = errorProcessor;
  4069. return 0;
  4070. }
  4071. }
  4072. void *
  4073. xmlrpc_XML_GetBuffer(XML_Parser const xmlParserP,
  4074. size_t const len) {
  4075. Parser * const parser = (Parser *)xmlParserP;
  4076. assert(bufferLim >= bufferEnd);
  4077. if (len > (size_t)(bufferLim - bufferEnd)) {
  4078. /* FIXME avoid integer overflow */
  4079. size_t neededSize = len + (bufferEnd - bufferPtr);
  4080. assert(bufferLim >= buffer);
  4081. if (neededSize <= (size_t)(bufferLim - buffer)) {
  4082. memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
  4083. bufferEnd = buffer + (bufferEnd - bufferPtr);
  4084. bufferPtr = buffer;
  4085. } else {
  4086. size_t bufferSize;
  4087. char * newBuf;
  4088. bufferSize = bufferLim > bufferPtr ?
  4089. bufferLim - bufferPtr : INIT_BUFFER_SIZE;
  4090. do {
  4091. bufferSize *= 2;
  4092. } while (bufferSize < neededSize);
  4093. newBuf = malloc(bufferSize);
  4094. if (newBuf == 0) {
  4095. errorCode = XML_ERROR_NO_MEMORY;
  4096. return 0;
  4097. }
  4098. bufferLim = newBuf + bufferSize;
  4099. if (bufferPtr) {
  4100. memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
  4101. free(buffer);
  4102. }
  4103. bufferEnd = newBuf + (bufferEnd - bufferPtr);
  4104. bufferPtr = buffer = newBuf;
  4105. }
  4106. }
  4107. return bufferEnd;
  4108. }
  4109. enum XML_Error
  4110. xmlrpc_XML_GetErrorCode(XML_Parser const parser) {
  4111. return errorCode;
  4112. }
  4113. const char *
  4114. xmlrpc_XML_GetErrorString(XML_Parser const parser) {
  4115. if (errorString)
  4116. return errorString;
  4117. else if (errorCode == XML_ERROR_NONE)
  4118. return NULL;
  4119. else
  4120. return xmlrpc_XML_ErrorString(errorCode);
  4121. }
  4122. long
  4123. xmlrpc_XML_GetCurrentByteIndex(XML_Parser const parser) {
  4124. long retval;
  4125. if (eventPtr) {
  4126. size_t const bytesLeft = parseEndPtr - eventPtr;
  4127. if ((size_t)(long)(bytesLeft) != bytesLeft)
  4128. retval = -1;
  4129. else
  4130. retval = parseEndByteIndex - (long)bytesLeft;
  4131. } else
  4132. retval = -1;
  4133. return retval;
  4134. }
  4135. int
  4136. xmlrpc_XML_GetCurrentByteCount(XML_Parser const parser) {
  4137. int retval;
  4138. if (eventEndPtr && eventPtr) {
  4139. size_t const byteCount = eventEndPtr - eventPtr;
  4140. assert((size_t)(int)byteCount == byteCount);
  4141. retval = (int)byteCount;
  4142. } else
  4143. retval = 0;
  4144. return retval;
  4145. }
  4146. int
  4147. xmlrpc_XML_GetCurrentLineNumber(XML_Parser const xmlParserP) {
  4148. Parser * const parser = (Parser *) xmlParserP;
  4149. if (eventPtr) {
  4150. XmlUpdatePosition(parser->m_encoding, positionPtr, eventPtr,
  4151. &position);
  4152. positionPtr = eventPtr;
  4153. }
  4154. return position.lineNumber + 1;
  4155. }
  4156. int
  4157. xmlrpc_XML_GetCurrentColumnNumber(XML_Parser const xmlParserP) {
  4158. Parser * const parser = (Parser *) xmlParserP;
  4159. if (eventPtr) {
  4160. XmlUpdatePosition(parser->m_encoding, positionPtr, eventPtr,
  4161. &position);
  4162. positionPtr = eventPtr;
  4163. }
  4164. return position.columnNumber;
  4165. }
  4166. void
  4167. xmlrpc_XML_DefaultCurrent(XML_Parser const xmlParserP) {
  4168. Parser * const parser = (Parser *) xmlParserP;
  4169. if (defaultHandler) {
  4170. if (openInternalEntities)
  4171. reportDefault(xmlParserP,
  4172. internalEncoding,
  4173. openInternalEntities->internalEventPtr,
  4174. openInternalEntities->internalEventEndPtr);
  4175. else
  4176. reportDefault(xmlParserP, parser->m_encoding,
  4177. eventPtr, eventEndPtr);
  4178. }
  4179. }
  4180. const XML_LChar *
  4181. xmlrpc_XML_ErrorString(int const code) {
  4182. static const XML_LChar * const message[] = {
  4183. /* NONE */ NULL,
  4184. /* NO_MEMORY */ XML_T("out of memory"),
  4185. /* SYNTAX */ XML_T("syntax error"),
  4186. /* NO_ELEMENTS */ XML_T("no element found"),
  4187. /* INVALID_TOKEN */ XML_T("not well-formed"),
  4188. /* UNCLOSED_TOKEN */ XML_T("unclosed token"),
  4189. /* PARTIAL_CHAR */ XML_T("unclosed token"),
  4190. /* TAG_MISMATCH */ XML_T("mismatched tag"),
  4191. /* DUPLICATE_ATTRIBUTE */ XML_T("duplicate attribute"),
  4192. /* JUNK_AFTER_DOC_ELEMENT */ XML_T("junk after document element"),
  4193. /* PARAM_ENTITY_REF */
  4194. XML_T("illegal parameter entity reference"),
  4195. /* UNDEFINED_ENTITY */ XML_T("undefined entity"),
  4196. /* RECURSIVE_ENTITY_REF */ XML_T("recursive entity reference"),
  4197. /* ASYNC_ENTITY */ XML_T("asynchronous entity"),
  4198. /* BAD_CHAR_REF */
  4199. XML_T("reference to invalid character number"),
  4200. /* BINARY_ENTITY_REF */ XML_T("reference to binary entity"),
  4201. /* ATTRIBUTE_EXTERNAL_ENTITY_REF */
  4202. XML_T("reference to external entity in attribute"),
  4203. /* MISPLACED_XML_PI */
  4204. XML_T("xml processing instruction not at start "
  4205. "of external entity"),
  4206. /* UNKNOWN_ENCODING */ XML_T("unknown encoding"),
  4207. /* INCORRECT_ENCODING */
  4208. XML_T("encoding specified in XML declaration is incorrect"),
  4209. /* UNCLOSED_CDATA_SECTION */ XML_T("unclosed CDATA section"),
  4210. /* EXTERNAL_ENTITY_HANDLING */
  4211. XML_T("error in processing external entity reference"),
  4212. /* NOT_STANDALONE */ XML_T("document is not standalone")
  4213. };
  4214. const XML_LChar * retval;
  4215. if (code > 0 && (unsigned)code < ARRAY_SIZE(message))
  4216. retval = message[code];
  4217. else
  4218. retval = NULL;
  4219. return retval;
  4220. }