1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705 |
- /*
- Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
- See the file copying.txt for copying permission.
- */
- /* In 2001, this was part of the Expat package. We copied it into
- Xmlrpc-c because it's easier on the user than making him get and
- link Expat separately, and we don't expect to benefit from separate
- maintenance of Expat.
- But we changed all the external symbols that in Expat are named
- "XML_xxxx" to "xmlrpc_XML_xxxx" because people do link Xmlrpc-c
- libraries into programs that also link Expat (a good example is
- where an Apache module uses Xmlrpc-c). We don't want our names to
- collide with Expat's.
- */
- #include <stddef.h>
- #include <assert.h>
- #include "xmlrpc_config.h"
- #include "c_util.h"
- #include "girmath.h"
- #include "mallocvar.h"
- #include "xmlrpc-c/string_int.h"
- #include "xmldef.h"
- #include "xmlparse.h"
- static const char *
- extractXmlSample(const char * const start,
- const char * const end,
- size_t const maximumLen) {
- size_t const len = MIN(maximumLen, (size_t)(end - start));
-
- return xmlrpc_makePrintable_lp(start, len);
- }
- #ifdef XML_UNICODE
- #define XML_ENCODE_MAX XML_UTF16_ENCODE_MAX
- #define XmlConvert XmlUtf16Convert
- #define XmlGetInternalEncoding xmlrpc_XmlGetUtf16InternalEncoding
- #define XmlGetInternalEncodingNS xmlrpc_XmlGetUtf16InternalEncodingNS
- #define XmlEncode xmlrpc_XmlUtf16Encode
- #define MUST_CONVERT(enc, s) (!(enc)->isUtf16 || (((unsigned long)s) & 1))
- typedef unsigned short ICHAR;
- #else
- #define XML_ENCODE_MAX XML_UTF8_ENCODE_MAX
- #define XmlConvert XmlUtf8Convert
- #define XmlGetInternalEncoding xmlrpc_XmlGetUtf8InternalEncoding
- #define XmlGetInternalEncodingNS xmlrpc_XmlGetUtf8InternalEncodingNS
- #define XmlEncode xmlrpc_XmlUtf8Encode
- #define MUST_CONVERT(enc, s) (!(enc)->isUtf8)
- typedef char ICHAR;
- #endif
- #ifndef XML_NS
- #define XmlInitEncodingNS XmlInitEncoding
- #define XmlInitUnknownEncodingNS XmlInitUnknownEncoding
- #undef XmlGetInternalEncodingNS
- #define XmlGetInternalEncodingNS XmlGetInternalEncoding
- #define XmlParseXmlDeclNS XmlParseXmlDecl
- #endif
- #ifdef XML_UNICODE_WCHAR_T
- #define XML_T(x) L ## x
- #else
- #define XML_T(x) x
- #endif
- /* Round up n to be a multiple of sz, where sz is a power of 2. */
- #define ROUND_UP(n, sz) (((n) + ((sz) - 1)) & ~((sz) - 1))
- #include "xmltok.h"
- #include "xmlrole.h"
- typedef const XML_Char *KEY;
- typedef struct {
- KEY name;
- } NAMED;
- typedef struct {
- NAMED **v;
- size_t size;
- size_t used;
- size_t usedLim;
- } HASH_TABLE;
- typedef struct {
- NAMED **p;
- NAMED **end;
- } HASH_TABLE_ITER;
- #define INIT_TAG_BUF_SIZE 32 /* must be a multiple of sizeof(XML_Char) */
- #define INIT_DATA_BUF_SIZE 1024
- #define INIT_ATTS_SIZE 16
- #define INIT_BLOCK_SIZE 1024
- #define INIT_BUFFER_SIZE 1024
- #define EXPAND_SPARE 24
- typedef struct binding {
- struct prefix *prefix;
- struct binding *nextTagBinding;
- struct binding *prevPrefixBinding;
- const struct attribute_id *attId;
- XML_Char *uri;
- int uriLen;
- int uriAlloc;
- } BINDING;
- typedef struct prefix {
- const XML_Char *name;
- BINDING *binding;
- } PREFIX;
- typedef struct {
- const XML_Char *str;
- const XML_Char *localPart;
- int uriLen;
- } TAG_NAME;
- typedef struct tag {
- struct tag *parent;
- const char *rawName;
- int rawNameLength;
- TAG_NAME name;
- char *buf;
- char *bufEnd;
- BINDING *bindings;
- } TAG;
- typedef struct {
- const XML_Char *name;
- const XML_Char *textPtr;
- size_t textLen;
- const XML_Char *systemId;
- const XML_Char *base;
- const XML_Char *publicId;
- const XML_Char *notation;
- char open;
- } ENTITY;
- typedef struct block {
- struct block *next;
- int size;
- XML_Char s[1];
- } BLOCK;
- typedef struct {
- BLOCK *blocks;
- BLOCK *freeBlocks;
- const XML_Char *end;
- XML_Char *ptr;
- XML_Char *start;
- } STRING_POOL;
- /* The XML_Char before the name is used to determine whether
- an attribute has been specified. */
- typedef struct attribute_id {
- XML_Char *name;
- PREFIX *prefix;
- char maybeTokenized;
- char xmlns;
- } ATTRIBUTE_ID;
- typedef struct {
- const ATTRIBUTE_ID *id;
- char isCdata;
- const XML_Char *value;
- } DEFAULT_ATTRIBUTE;
- typedef struct {
- const XML_Char *name;
- PREFIX *prefix;
- const ATTRIBUTE_ID *idAtt;
- int nDefaultAtts;
- int allocDefaultAtts;
- DEFAULT_ATTRIBUTE *defaultAtts;
- } ELEMENT_TYPE;
- typedef struct {
- HASH_TABLE generalEntities;
- HASH_TABLE elementTypes;
- HASH_TABLE attributeIds;
- HASH_TABLE prefixes;
- STRING_POOL pool;
- int complete;
- int standalone;
- HASH_TABLE paramEntities;
- PREFIX defaultPrefix;
- } DTD;
- typedef struct open_internal_entity {
- const char *internalEventPtr;
- const char *internalEventEndPtr;
- struct open_internal_entity *next;
- ENTITY *entity;
- } OPEN_INTERNAL_ENTITY;
- typedef void Processor(XML_Parser parser,
- const char * const start,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP);
- #define poolStart(pool) ((pool)->start)
- #define poolEnd(pool) ((pool)->ptr)
- #define poolLength(pool) ((pool)->ptr - (pool)->start)
- #define poolChop(pool) ((void)--(pool->ptr))
- #define poolLastChar(pool) (((pool)->ptr)[-1])
- #define poolDiscard(pool) ((pool)->ptr = (pool)->start)
- #define poolFinish(pool) ((pool)->start = (pool)->ptr)
- #define poolAppendChar(pool, c) \
- (((pool)->ptr == (pool)->end && !poolGrow(pool)) \
- ? 0 \
- : ((*((pool)->ptr)++ = c), 1))
- typedef struct {
- /* The first member must be userData so that the XML_GetUserData macro works. */
- void *m_userData;
- void *m_handlerArg;
- char *m_buffer;
- /* first character to be parsed */
- const char *m_bufferPtr;
- /* past last character to be parsed */
- char *m_bufferEnd;
- /* allocated end of buffer */
- const char *m_bufferLim;
- long m_parseEndByteIndex;
- const char *m_parseEndPtr;
- XML_Char *m_dataBuf;
- XML_Char *m_dataBufEnd;
- XML_StartElementHandler m_startElementHandler;
- XML_EndElementHandler m_endElementHandler;
- XML_CharacterDataHandler m_characterDataHandler;
- XML_ProcessingInstructionHandler m_processingInstructionHandler;
- XML_CommentHandler m_commentHandler;
- XML_StartCdataSectionHandler m_startCdataSectionHandler;
- XML_EndCdataSectionHandler m_endCdataSectionHandler;
- XML_DefaultHandler m_defaultHandler;
- XML_StartDoctypeDeclHandler m_startDoctypeDeclHandler;
- XML_EndDoctypeDeclHandler m_endDoctypeDeclHandler;
- XML_UnparsedEntityDeclHandler m_unparsedEntityDeclHandler;
- XML_NotationDeclHandler m_notationDeclHandler;
- XML_ExternalParsedEntityDeclHandler m_externalParsedEntityDeclHandler;
- XML_InternalParsedEntityDeclHandler m_internalParsedEntityDeclHandler;
- XML_StartNamespaceDeclHandler m_startNamespaceDeclHandler;
- XML_EndNamespaceDeclHandler m_endNamespaceDeclHandler;
- XML_NotStandaloneHandler m_notStandaloneHandler;
- XML_ExternalEntityRefHandler m_externalEntityRefHandler;
- void *m_externalEntityRefHandlerArg;
- XML_UnknownEncodingHandler m_unknownEncodingHandler;
- const ENCODING *m_encoding;
- INIT_ENCODING m_initEncoding;
- const ENCODING *m_internalEncoding;
- const XML_Char *m_protocolEncodingName;
- int m_ns;
- void *m_unknownEncodingMem;
- void *m_unknownEncodingData;
- void *m_unknownEncodingHandlerData;
- void (*m_unknownEncodingRelease)(void *);
- PROLOG_STATE m_prologState;
- Processor *m_processor;
- /* The next processor to run */
- enum XML_Error m_errorCode;
- /* Explanation of the failure of the most recent call to Expat.
- XML_ERROR_NONE means it didn't fail. This is redundant with
- m_errorString if the latter is non-null.
- The latter is newer and better.
- */
- const char * m_errorString;
- /* malloc'ed string describing the failure of the most recent call
- to Expat. NULL means m_errorCode is the only error information
- available.
- */
- const char *m_eventPtr;
- const char *m_eventEndPtr;
- const char *m_positionPtr;
- OPEN_INTERNAL_ENTITY *m_openInternalEntities;
- int m_defaultExpandInternalEntities;
- int m_tagLevel;
- ENTITY *m_declEntity;
- const XML_Char *m_declNotationName;
- const XML_Char *m_declNotationPublicId;
- ELEMENT_TYPE *m_declElementType;
- ATTRIBUTE_ID *m_declAttributeId;
- char m_declAttributeIsCdata;
- char m_declAttributeIsId;
- DTD m_dtd;
- const XML_Char *m_curBase;
- TAG *m_tagStack;
- TAG *m_freeTagList;
- BINDING *m_inheritedBindings;
- BINDING *m_freeBindingList;
- int m_attsSize;
- int m_nSpecifiedAtts;
- int m_idAttIndex;
- ATTRIBUTE *m_atts;
- POSITION m_position;
- STRING_POOL m_tempPool;
- STRING_POOL m_temp2Pool;
- char *m_groupConnector;
- unsigned m_groupSize;
- int m_hadExternalDoctype;
- XML_Char m_namespaceSeparator;
- enum XML_ParamEntityParsing m_paramEntityParsing;
- XML_Parser m_parentParser;
- } Parser;
- #define userData (((Parser *)parser)->m_userData)
- #define handlerArg (((Parser *)parser)->m_handlerArg)
- #define startElementHandler (((Parser *)parser)->m_startElementHandler)
- #define endElementHandler (((Parser *)parser)->m_endElementHandler)
- #define characterDataHandler (((Parser *)parser)->m_characterDataHandler)
- #define processingInstructionHandler (((Parser *)parser)->m_processingInstructionHandler)
- #define commentHandler (((Parser *)parser)->m_commentHandler)
- #define startCdataSectionHandler (((Parser *)parser)->m_startCdataSectionHandler)
- #define endCdataSectionHandler (((Parser *)parser)->m_endCdataSectionHandler)
- #define defaultHandler (((Parser *)parser)->m_defaultHandler)
- #define startDoctypeDeclHandler (((Parser *)parser)->m_startDoctypeDeclHandler)
- #define endDoctypeDeclHandler (((Parser *)parser)->m_endDoctypeDeclHandler)
- #define unparsedEntityDeclHandler (((Parser *)parser)->m_unparsedEntityDeclHandler)
- #define notationDeclHandler (((Parser *)parser)->m_notationDeclHandler)
- #define externalParsedEntityDeclHandler (((Parser *)parser)->m_externalParsedEntityDeclHandler)
- #define internalParsedEntityDeclHandler (((Parser *)parser)->m_internalParsedEntityDeclHandler)
- #define startNamespaceDeclHandler (((Parser *)parser)->m_startNamespaceDeclHandler)
- #define endNamespaceDeclHandler (((Parser *)parser)->m_endNamespaceDeclHandler)
- #define notStandaloneHandler (((Parser *)parser)->m_notStandaloneHandler)
- #define externalEntityRefHandler (((Parser *)parser)->m_externalEntityRefHandler)
- #define externalEntityRefHandlerArg (((Parser *)parser)->m_externalEntityRefHandlerArg)
- #define unknownEncodingHandler (((Parser *)parser)->m_unknownEncodingHandler)
- #define initEncoding (((Parser *)parser)->m_initEncoding)
- #define internalEncoding (((Parser *)parser)->m_internalEncoding)
- #define unknownEncodingMem (((Parser *)parser)->m_unknownEncodingMem)
- #define unknownEncodingData (((Parser *)parser)->m_unknownEncodingData)
- #define unknownEncodingHandlerData \
- (((Parser *)parser)->m_unknownEncodingHandlerData)
- #define unknownEncodingRelease (((Parser *)parser)->m_unknownEncodingRelease)
- #define protocolEncodingName (((Parser *)parser)->m_protocolEncodingName)
- #define ns (((Parser *)parser)->m_ns)
- #define prologState (((Parser *)parser)->m_prologState)
- #define processor (((Parser *)parser)->m_processor)
- #define errorCode (((Parser *)parser)->m_errorCode)
- #define errorString (((Parser *)parser)->m_errorString)
- #define eventPtr (((Parser *)parser)->m_eventPtr)
- #define eventEndPtr (((Parser *)parser)->m_eventEndPtr)
- #define positionPtr (((Parser *)parser)->m_positionPtr)
- #define position (((Parser *)parser)->m_position)
- #define openInternalEntities (((Parser *)parser)->m_openInternalEntities)
- #define defaultExpandInternalEntities (((Parser *)parser)->m_defaultExpandInternalEntities)
- #define tagLevel (((Parser *)parser)->m_tagLevel)
- #define buffer (((Parser *)parser)->m_buffer)
- #define bufferPtr (((Parser *)parser)->m_bufferPtr)
- #define bufferEnd (((Parser *)parser)->m_bufferEnd)
- #define parseEndByteIndex (((Parser *)parser)->m_parseEndByteIndex)
- #define parseEndPtr (((Parser *)parser)->m_parseEndPtr)
- #define bufferLim (((Parser *)parser)->m_bufferLim)
- #define dataBuf (((Parser *)parser)->m_dataBuf)
- #define dataBufEnd (((Parser *)parser)->m_dataBufEnd)
- #define dtd (((Parser *)parser)->m_dtd)
- #define curBase (((Parser *)parser)->m_curBase)
- #define declEntity (((Parser *)parser)->m_declEntity)
- #define declNotationName (((Parser *)parser)->m_declNotationName)
- #define declNotationPublicId (((Parser *)parser)->m_declNotationPublicId)
- #define declElementType (((Parser *)parser)->m_declElementType)
- #define declAttributeId (((Parser *)parser)->m_declAttributeId)
- #define declAttributeIsCdata (((Parser *)parser)->m_declAttributeIsCdata)
- #define declAttributeIsId (((Parser *)parser)->m_declAttributeIsId)
- #define freeTagList (((Parser *)parser)->m_freeTagList)
- #define freeBindingList (((Parser *)parser)->m_freeBindingList)
- #define inheritedBindings (((Parser *)parser)->m_inheritedBindings)
- #define tagStack (((Parser *)parser)->m_tagStack)
- #define atts (((Parser *)parser)->m_atts)
- #define attsSize (((Parser *)parser)->m_attsSize)
- #define nSpecifiedAtts (((Parser *)parser)->m_nSpecifiedAtts)
- #define idAttIndex (((Parser *)parser)->m_idAttIndex)
- #define tempPool (((Parser *)parser)->m_tempPool)
- #define temp2Pool (((Parser *)parser)->m_temp2Pool)
- #define groupConnector (((Parser *)parser)->m_groupConnector)
- #define groupSize (((Parser *)parser)->m_groupSize)
- #define hadExternalDoctype (((Parser *)parser)->m_hadExternalDoctype)
- #define namespaceSeparator (((Parser *)parser)->m_namespaceSeparator)
- #define parentParser (((Parser *)parser)->m_parentParser)
- #define paramEntityParsing (((Parser *)parser)->m_paramEntityParsing)
- static
- void poolInit(STRING_POOL *pool)
- {
- pool->blocks = 0;
- pool->freeBlocks = 0;
- pool->start = 0;
- pool->ptr = 0;
- pool->end = 0;
- }
- static
- void poolClear(STRING_POOL *pool)
- {
- if (!pool->freeBlocks)
- pool->freeBlocks = pool->blocks;
- else {
- BLOCK *p = pool->blocks;
- while (p) {
- BLOCK *tem = p->next;
- p->next = pool->freeBlocks;
- pool->freeBlocks = p;
- p = tem;
- }
- }
- pool->blocks = 0;
- pool->start = 0;
- pool->ptr = 0;
- pool->end = 0;
- }
- static
- void poolDestroy(STRING_POOL *pool)
- {
- BLOCK *p = pool->blocks;
- while (p) {
- BLOCK *tem = p->next;
- free(p);
- p = tem;
- }
- pool->blocks = 0;
- p = pool->freeBlocks;
- while (p) {
- BLOCK *tem = p->next;
- free(p);
- p = tem;
- }
- pool->freeBlocks = 0;
- pool->ptr = 0;
- pool->start = 0;
- pool->end = 0;
- }
- static
- int poolGrow(STRING_POOL *pool)
- {
- if (pool->freeBlocks) {
- if (pool->start == 0) {
- pool->blocks = pool->freeBlocks;
- pool->freeBlocks = pool->freeBlocks->next;
- pool->blocks->next = 0;
- pool->start = pool->blocks->s;
- pool->end = pool->start + pool->blocks->size;
- pool->ptr = pool->start;
- return 1;
- }
- if (pool->end - pool->start < pool->freeBlocks->size) {
- BLOCK *tem = pool->freeBlocks->next;
- pool->freeBlocks->next = pool->blocks;
- pool->blocks = pool->freeBlocks;
- pool->freeBlocks = tem;
- memcpy(pool->blocks->s, pool->start,
- (pool->end - pool->start) * sizeof(XML_Char));
- pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
- pool->start = pool->blocks->s;
- pool->end = pool->start + pool->blocks->size;
- return 1;
- }
- }
- if (pool->blocks && pool->start == pool->blocks->s) {
- size_t const blockSize = (pool->end - pool->start)*2;
- pool->blocks = realloc(pool->blocks, offsetof(BLOCK, s) +
- blockSize * sizeof(XML_Char));
- if (!pool->blocks)
- return 0;
- pool->blocks->size = blockSize;
- pool->ptr = pool->blocks->s + (pool->ptr - pool->start);
- pool->start = pool->blocks->s;
- pool->end = pool->start + blockSize;
- }
- else {
- size_t const poolLen = pool->end - pool->start;
- size_t const blockSize =
- poolLen < INIT_BLOCK_SIZE ? INIT_BLOCK_SIZE : poolLen * 2;
- BLOCK *tem;
- tem = malloc(offsetof(BLOCK, s) + blockSize * sizeof(XML_Char));
- if (!tem)
- return 0;
- tem->size = blockSize;
- tem->next = pool->blocks;
- pool->blocks = tem;
- if (pool->ptr != pool->start)
- memcpy(tem->s, pool->start,
- (pool->ptr - pool->start) * sizeof(XML_Char));
- pool->ptr = tem->s + (pool->ptr - pool->start);
- pool->start = tem->s;
- pool->end = tem->s + blockSize;
- }
- return 1;
- }
- static
- XML_Char *poolAppend(STRING_POOL *pool, const ENCODING *enc,
- const char *ptr, const char *end)
- {
- if (!pool->ptr && !poolGrow(pool))
- return 0;
- for (;;) {
- XmlConvert(enc, &ptr, end, (ICHAR **)&(pool->ptr), (ICHAR *)pool->end);
- if (ptr == end)
- break;
- if (!poolGrow(pool))
- return 0;
- }
- return pool->start;
- }
- static const XML_Char *poolCopyString(STRING_POOL *pool, const XML_Char *s)
- {
- do {
- if (!poolAppendChar(pool, *s))
- return 0;
- } while (*s++);
- s = pool->start;
- poolFinish(pool);
- return s;
- }
- static const XML_Char *
- poolCopyStringN(STRING_POOL *pool,
- const XML_Char *s,
- int n)
- {
- if (!pool->ptr && !poolGrow(pool))
- return 0;
- for (; n > 0; --n, s++) {
- if (!poolAppendChar(pool, *s))
- return 0;
- }
- s = pool->start;
- poolFinish(pool);
- return s;
- }
- static
- XML_Char *poolStoreString(STRING_POOL *pool, const ENCODING *enc,
- const char *ptr, const char *end)
- {
- if (!poolAppend(pool, enc, ptr, end))
- return 0;
- if (pool->ptr == pool->end && !poolGrow(pool))
- return 0;
- *(pool->ptr)++ = 0;
- return pool->start;
- }
- #define INIT_SIZE 64
- static
- int keyeq(KEY s1, KEY s2)
- {
- for (; *s1 == *s2; s1++, s2++)
- if (*s1 == 0)
- return 1;
- return 0;
- }
- static
- unsigned long hash(KEY s)
- {
- unsigned long h = 0;
- while (*s)
- h = (h << 5) + h + (unsigned char)*s++;
- return h;
- }
- static
- NAMED *lookup(HASH_TABLE *table, KEY name, size_t createSize)
- {
- size_t i;
- if (table->size == 0) {
- if (!createSize)
- return 0;
- table->v = calloc(INIT_SIZE, sizeof(NAMED *));
- if (!table->v)
- return 0;
- table->size = INIT_SIZE;
- table->usedLim = INIT_SIZE / 2;
- i = hash(name) & (table->size - 1);
- }
- else {
- unsigned long h = hash(name);
- for (i = h & (table->size - 1);
- table->v[i];
- i == 0 ? i = table->size - 1 : --i) {
- if (keyeq(name, table->v[i]->name))
- return table->v[i];
- }
- if (!createSize)
- return 0;
- if (table->used == table->usedLim) {
- /* check for overflow */
- size_t newSize = table->size * 2;
- NAMED **newV = calloc(newSize, sizeof(NAMED *));
- if (!newV)
- return 0;
- for (i = 0; i < table->size; i++)
- if (table->v[i]) {
- size_t j;
- for (j = hash(table->v[i]->name) & (newSize - 1);
- newV[j];
- j == 0 ? j = newSize - 1 : --j)
- ;
- newV[j] = table->v[i];
- }
- free(table->v);
- table->v = newV;
- table->size = newSize;
- table->usedLim = newSize/2;
- for (i = h & (table->size - 1);
- table->v[i];
- i == 0 ? i = table->size - 1 : --i)
- ;
- }
- }
- table->v[i] = calloc(1, createSize);
- if (!table->v[i])
- return 0;
- table->v[i]->name = name;
- (table->used)++;
- return table->v[i];
- }
- static
- void hashTableDestroy(HASH_TABLE *table)
- {
- size_t i;
- for (i = 0; i < table->size; i++) {
- NAMED *p = table->v[i];
- if (p)
- free(p);
- }
- if (table->v)
- free(table->v);
- }
- static
- void hashTableInit(HASH_TABLE *p)
- {
- p->size = 0;
- p->usedLim = 0;
- p->used = 0;
- p->v = 0;
- }
- static
- void hashTableIterInit(HASH_TABLE_ITER *iter, const HASH_TABLE *table)
- {
- iter->p = table->v;
- iter->end = iter->p + table->size;
- }
- static
- NAMED *hashTableIterNext(HASH_TABLE_ITER *iter)
- {
- while (iter->p != iter->end) {
- NAMED *tem = *(iter->p)++;
- if (tem)
- return tem;
- }
- return 0;
- }
- static int dtdInit(DTD *p)
- {
- poolInit(&(p->pool));
- hashTableInit(&(p->generalEntities));
- hashTableInit(&(p->elementTypes));
- hashTableInit(&(p->attributeIds));
- hashTableInit(&(p->prefixes));
- p->complete = 1;
- p->standalone = 0;
- hashTableInit(&(p->paramEntities));
- p->defaultPrefix.name = 0;
- p->defaultPrefix.binding = 0;
- return 1;
- }
- static void dtdSwap(DTD *p1, DTD *p2)
- {
- DTD tem;
- memcpy(&tem, p1, sizeof(DTD));
- memcpy(p1, p2, sizeof(DTD));
- memcpy(p2, &tem, sizeof(DTD));
- }
- static void dtdDestroy(DTD *p)
- {
- HASH_TABLE_ITER iter;
- hashTableIterInit(&iter, &(p->elementTypes));
- for (;;) {
- ELEMENT_TYPE *e = (ELEMENT_TYPE *)hashTableIterNext(&iter);
- if (!e)
- break;
- if (e->allocDefaultAtts != 0)
- free(e->defaultAtts);
- }
- hashTableDestroy(&(p->generalEntities));
- hashTableDestroy(&(p->paramEntities));
- hashTableDestroy(&(p->elementTypes));
- hashTableDestroy(&(p->attributeIds));
- hashTableDestroy(&(p->prefixes));
- poolDestroy(&(p->pool));
- }
- static int copyEntityTable(HASH_TABLE *newTable,
- STRING_POOL *newPool,
- const HASH_TABLE *oldTable)
- {
- HASH_TABLE_ITER iter;
- const XML_Char *cachedOldBase = 0;
- const XML_Char *cachedNewBase = 0;
- hashTableIterInit(&iter, oldTable);
- for (;;) {
- ENTITY *newE;
- const XML_Char *name;
- const ENTITY *oldE = (ENTITY *)hashTableIterNext(&iter);
- if (!oldE)
- break;
- name = poolCopyString(newPool, oldE->name);
- if (!name)
- return 0;
- newE = (ENTITY *)lookup(newTable, name, sizeof(ENTITY));
- if (!newE)
- return 0;
- if (oldE->systemId) {
- const XML_Char *tem = poolCopyString(newPool, oldE->systemId);
- if (!tem)
- return 0;
- newE->systemId = tem;
- if (oldE->base) {
- if (oldE->base == cachedOldBase)
- newE->base = cachedNewBase;
- else {
- cachedOldBase = oldE->base;
- tem = poolCopyString(newPool, cachedOldBase);
- if (!tem)
- return 0;
- cachedNewBase = newE->base = tem;
- }
- }
- }
- else {
- const XML_Char *tem =
- poolCopyStringN(newPool, oldE->textPtr, oldE->textLen);
- if (!tem)
- return 0;
- newE->textPtr = tem;
- newE->textLen = oldE->textLen;
- }
- if (oldE->notation) {
- const XML_Char *tem = poolCopyString(newPool, oldE->notation);
- if (!tem)
- return 0;
- newE->notation = tem;
- }
- }
- return 1;
- }
- /* Do a deep copy of the DTD. Return 0 for out of memory; non-zero otherwise.
- The new DTD has already been initialized. */
- static int dtdCopy(DTD *newDtd, const DTD *oldDtd)
- {
- HASH_TABLE_ITER iter;
- /* Copy the prefix table. */
- hashTableIterInit(&iter, &(oldDtd->prefixes));
- for (;;) {
- const XML_Char *name;
- const PREFIX *oldP = (PREFIX *)hashTableIterNext(&iter);
- if (!oldP)
- break;
- name = poolCopyString(&(newDtd->pool), oldP->name);
- if (!name)
- return 0;
- if (!lookup(&(newDtd->prefixes), name, sizeof(PREFIX)))
- return 0;
- }
- hashTableIterInit(&iter, &(oldDtd->attributeIds));
- /* Copy the attribute id table. */
- for (;;) {
- ATTRIBUTE_ID *newA;
- const XML_Char *name;
- const ATTRIBUTE_ID *oldA = (ATTRIBUTE_ID *)hashTableIterNext(&iter);
- if (!oldA)
- break;
- /* Remember to allocate the scratch byte before the name. */
- if (!poolAppendChar(&(newDtd->pool), XML_T('\0')))
- return 0;
- name = poolCopyString(&(newDtd->pool), oldA->name);
- if (!name)
- return 0;
- ++name;
- newA = (ATTRIBUTE_ID *)
- lookup(&(newDtd->attributeIds), name, sizeof(ATTRIBUTE_ID));
- if (!newA)
- return 0;
- newA->maybeTokenized = oldA->maybeTokenized;
- if (oldA->prefix) {
- newA->xmlns = oldA->xmlns;
- if (oldA->prefix == &oldDtd->defaultPrefix)
- newA->prefix = &newDtd->defaultPrefix;
- else
- newA->prefix = (PREFIX *)
- lookup(&(newDtd->prefixes), oldA->prefix->name, 0);
- }
- }
- /* Copy the element type table. */
- hashTableIterInit(&iter, &(oldDtd->elementTypes));
- for (;;) {
- int i;
- ELEMENT_TYPE *newE;
- const XML_Char *name;
- const ELEMENT_TYPE *oldE = (ELEMENT_TYPE *)hashTableIterNext(&iter);
- if (!oldE)
- break;
- name = poolCopyString(&(newDtd->pool), oldE->name);
- if (!name)
- return 0;
- newE = (ELEMENT_TYPE *)
- lookup(&(newDtd->elementTypes), name, sizeof(ELEMENT_TYPE));
- if (!newE)
- return 0;
- if (oldE->nDefaultAtts) {
- newE->defaultAtts = (DEFAULT_ATTRIBUTE *)
- malloc(oldE->nDefaultAtts * sizeof(DEFAULT_ATTRIBUTE));
- if (!newE->defaultAtts)
- return 0;
- }
- if (oldE->idAtt)
- newE->idAtt = (ATTRIBUTE_ID *)
- lookup(&(newDtd->attributeIds), oldE->idAtt->name, 0);
- newE->allocDefaultAtts = newE->nDefaultAtts = oldE->nDefaultAtts;
- if (oldE->prefix)
- newE->prefix = (PREFIX *)
- lookup(&(newDtd->prefixes), oldE->prefix->name, 0);
- for (i = 0; i < newE->nDefaultAtts; i++) {
- newE->defaultAtts[i].id = (ATTRIBUTE_ID *)
- lookup(&(newDtd->attributeIds), oldE->defaultAtts[i].id->name, 0);
- newE->defaultAtts[i].isCdata = oldE->defaultAtts[i].isCdata;
- if (oldE->defaultAtts[i].value) {
- newE->defaultAtts[i].value =
- poolCopyString(&(newDtd->pool), oldE->defaultAtts[i].value);
- if (!newE->defaultAtts[i].value)
- return 0;
- }
- else
- newE->defaultAtts[i].value = 0;
- }
- }
- /* Copy the entity tables. */
- if (!copyEntityTable(&(newDtd->generalEntities),
- &(newDtd->pool),
- &(oldDtd->generalEntities)))
- return 0;
- if (!copyEntityTable(&(newDtd->paramEntities),
- &(newDtd->pool),
- &(oldDtd->paramEntities)))
- return 0;
- newDtd->complete = oldDtd->complete;
- newDtd->standalone = oldDtd->standalone;
- return 1;
- }
- static
- int addBinding(XML_Parser parser,
- PREFIX *prefix,
- const ATTRIBUTE_ID *attId,
- const XML_Char *uri,
- BINDING **bindingsPtr)
- {
- BINDING *b;
- int len;
- for (len = 0; uri[len]; len++)
- ;
- if (namespaceSeparator)
- len++;
- if (freeBindingList) {
- b = freeBindingList;
- if (len > b->uriAlloc) {
- b->uri = realloc(b->uri, sizeof(XML_Char) * (len + EXPAND_SPARE));
- if (!b->uri)
- return 0;
- b->uriAlloc = len + EXPAND_SPARE;
- }
- freeBindingList = b->nextTagBinding;
- }
- else {
- b = malloc(sizeof(BINDING));
- if (!b)
- return 0;
- b->uri = malloc(sizeof(XML_Char) * (len + EXPAND_SPARE));
- if (!b->uri) {
- free(b);
- return 0;
- }
- b->uriAlloc = len + EXPAND_SPARE;
- }
- b->uriLen = len;
- memcpy(b->uri, uri, len * sizeof(XML_Char));
- if (namespaceSeparator)
- b->uri[len - 1] = namespaceSeparator;
- b->prefix = prefix;
- b->attId = attId;
- b->prevPrefixBinding = prefix->binding;
- if (*uri == XML_T('\0') && prefix == &dtd.defaultPrefix)
- prefix->binding = 0;
- else
- prefix->binding = b;
- b->nextTagBinding = *bindingsPtr;
- *bindingsPtr = b;
- if (startNamespaceDeclHandler)
- startNamespaceDeclHandler(handlerArg, prefix->name,
- prefix->binding ? uri : 0);
- return 1;
- }
- #define CONTEXT_SEP XML_T('\f')
- static
- const XML_Char *getContext(XML_Parser parser)
- {
- HASH_TABLE_ITER iter;
- int needSep = 0;
- if (dtd.defaultPrefix.binding) {
- int i;
- int len;
- if (!poolAppendChar(&tempPool, XML_T('=')))
- return 0;
- len = dtd.defaultPrefix.binding->uriLen;
- if (namespaceSeparator != XML_T('\0'))
- len--;
- for (i = 0; i < len; i++)
- if (!poolAppendChar(&tempPool, dtd.defaultPrefix.binding->uri[i]))
- return 0;
- needSep = 1;
- }
- hashTableIterInit(&iter, &(dtd.prefixes));
- for (;;) {
- int i;
- int len;
- const XML_Char *s;
- PREFIX *prefix = (PREFIX *)hashTableIterNext(&iter);
- if (!prefix)
- break;
- if (!prefix->binding)
- continue;
- if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
- return 0;
- for (s = prefix->name; *s; s++)
- if (!poolAppendChar(&tempPool, *s))
- return 0;
- if (!poolAppendChar(&tempPool, XML_T('=')))
- return 0;
- len = prefix->binding->uriLen;
- if (namespaceSeparator != XML_T('\0'))
- len--;
- for (i = 0; i < len; i++)
- if (!poolAppendChar(&tempPool, prefix->binding->uri[i]))
- return 0;
- needSep = 1;
- }
- hashTableIterInit(&iter, &(dtd.generalEntities));
- for (;;) {
- const XML_Char *s;
- ENTITY *e = (ENTITY *)hashTableIterNext(&iter);
- if (!e)
- break;
- if (!e->open)
- continue;
- if (needSep && !poolAppendChar(&tempPool, CONTEXT_SEP))
- return 0;
- for (s = e->name; *s; s++)
- if (!poolAppendChar(&tempPool, *s))
- return 0;
- needSep = 1;
- }
- if (!poolAppendChar(&tempPool, XML_T('\0')))
- return 0;
- return tempPool.start;
- }
- static
- int setContext(XML_Parser parser, const XML_Char *context)
- {
- const XML_Char *s = context;
- while (*context != XML_T('\0')) {
- if (*s == CONTEXT_SEP || *s == XML_T('\0')) {
- ENTITY *e;
- if (!poolAppendChar(&tempPool, XML_T('\0')))
- return 0;
- e = (ENTITY *)lookup(&dtd.generalEntities, poolStart(&tempPool), 0);
- if (e)
- e->open = 1;
- if (*s != XML_T('\0'))
- s++;
- context = s;
- poolDiscard(&tempPool);
- }
- else if (*s == '=') {
- PREFIX *prefix;
- if (poolLength(&tempPool) == 0)
- prefix = &dtd.defaultPrefix;
- else {
- if (!poolAppendChar(&tempPool, XML_T('\0')))
- return 0;
- prefix = (PREFIX *)
- lookup(&dtd.prefixes, poolStart(&tempPool), sizeof(PREFIX));
- if (!prefix)
- return 0;
- if (prefix->name == poolStart(&tempPool)) {
- prefix->name = poolCopyString(&dtd.pool, prefix->name);
- if (!prefix->name)
- return 0;
- }
- poolDiscard(&tempPool);
- }
- for (context = s + 1;
- *context != CONTEXT_SEP && *context != XML_T('\0');
- ++context)
- if (!poolAppendChar(&tempPool, *context))
- return 0;
- if (!poolAppendChar(&tempPool, XML_T('\0')))
- return 0;
- if (!addBinding(parser, prefix, 0, poolStart(&tempPool),
- &inheritedBindings))
- return 0;
- poolDiscard(&tempPool);
- if (*context != XML_T('\0'))
- ++context;
- s = context;
- }
- else {
- if (!poolAppendChar(&tempPool, *s))
- return 0;
- s++;
- }
- }
- return 1;
- }
- static void
- normalizeLines(XML_Char *s)
- {
- XML_Char *p;
- for (;; s++) {
- if (*s == XML_T('\0'))
- return;
- if (*s == 0xD)
- break;
- }
- p = s;
- do {
- if (*s == 0xD) {
- *p++ = 0xA;
- if (*++s == 0xA)
- s++;
- }
- else
- *p++ = *s++;
- } while (*s);
- *p = XML_T('\0');
- }
- static void
- reportDefault(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const start,
- const char * const end) {
- Parser * const parser = (Parser *)xmlParserP;
- if (MUST_CONVERT(enc, start)) {
- const char * s;
- const char **eventPP;
- const char **eventEndPP;
- if (enc == parser->m_encoding) {
- eventPP = &eventPtr;
- eventEndPP = &eventEndPtr;
- }
- else {
- eventPP = &(openInternalEntities->internalEventPtr);
- eventEndPP = &(openInternalEntities->internalEventEndPtr);
- }
- s = start;
- do {
- ICHAR *dataPtr = (ICHAR *)dataBuf;
- XmlConvert(enc, &s, end, &dataPtr, (ICHAR *)dataBufEnd);
- *eventEndPP = s;
- {
- size_t const len = dataPtr - (ICHAR *)dataBuf;
- assert((size_t)(int)len == len); /* parser requirement */
- defaultHandler(handlerArg, dataBuf, (int)len);
- }
- *eventPP = s;
- } while (s != end);
- } else {
- size_t const len = (XML_Char *)end - (XML_Char *)start;
- assert((size_t)(int)len == len); /* parser requirement */
- defaultHandler(handlerArg, (XML_Char *)start, len);
- }
- }
- static int
- reportProcessingInstruction(XML_Parser parser,
- const ENCODING *enc,
- const char *start,
- const char *end)
- {
- const XML_Char *target;
- XML_Char *data;
- const char *tem;
- if (!processingInstructionHandler) {
- if (defaultHandler)
- reportDefault(parser, enc, start, end);
- return 1;
- }
- start += enc->minBytesPerChar * 2;
- tem = start + XmlNameLength(enc, start);
- target = poolStoreString(&tempPool, enc, start, tem);
- if (!target)
- return 0;
- poolFinish(&tempPool);
- data = poolStoreString(&tempPool, enc,
- XmlSkipS(enc, tem),
- end - enc->minBytesPerChar*2);
- if (!data)
- return 0;
- normalizeLines(data);
- processingInstructionHandler(handlerArg, target, data);
- poolClear(&tempPool);
- return 1;
- }
- static int
- reportComment(XML_Parser parser,
- const ENCODING *enc,
- const char *start,
- const char *end)
- {
- XML_Char *data;
- if (!commentHandler) {
- if (defaultHandler)
- reportDefault(parser, enc, start, end);
- return 1;
- }
- data = poolStoreString(&tempPool,
- enc,
- start + enc->minBytesPerChar * 4,
- end - enc->minBytesPerChar * 3);
- if (!data)
- return 0;
- normalizeLines(data);
- commentHandler(handlerArg, data);
- poolClear(&tempPool);
- return 1;
- }
- static enum XML_Error
- handleUnknownEncoding(XML_Parser const xmlParserP,
- const XML_Char * const encodingName) {
- Parser * const parser = (Parser *) xmlParserP;
- if (unknownEncodingHandler) {
- XML_Encoding info;
- int i;
- for (i = 0; i < 256; i++)
- info.map[i] = -1;
- info.convert = 0;
- info.data = 0;
- info.release = 0;
- if (unknownEncodingHandler(unknownEncodingHandlerData,
- encodingName, &info)) {
- ENCODING *enc;
- unknownEncodingMem = malloc(xmlrpc_XmlSizeOfUnknownEncoding());
- if (!unknownEncodingMem) {
- if (info.release)
- info.release(info.data);
- return XML_ERROR_NO_MEMORY;
- }
- enc = (ns
- ? xmlrpc_XmlInitUnknownEncodingNS
- : xmlrpc_XmlInitUnknownEncoding)(unknownEncodingMem,
- info.map,
- info.convert,
- info.data);
- if (enc) {
- unknownEncodingData = info.data;
- unknownEncodingRelease = info.release;
- parser->m_encoding = enc;
- return XML_ERROR_NONE;
- }
- }
- if (info.release)
- info.release(info.data);
- }
- return XML_ERROR_UNKNOWN_ENCODING;
- }
- static enum XML_Error
- initializeEncoding(XML_Parser const xmlParserP) {
- Parser * const parser = (Parser *) xmlParserP;
- const char *s;
- #ifdef XML_UNICODE
- char encodingBuf[128];
- if (!protocolEncodingName)
- s = 0;
- else {
- int i;
- for (i = 0; protocolEncodingName[i]; i++) {
- if (i == sizeof(encodingBuf) - 1
- || (protocolEncodingName[i] & ~0x7f) != 0) {
- encodingBuf[0] = '\0';
- break;
- }
- encodingBuf[i] = (char)protocolEncodingName[i];
- }
- encodingBuf[i] = '\0';
- s = encodingBuf;
- }
- #else
- s = protocolEncodingName;
- #endif
- if ((ns ? xmlrpc_XmlInitEncodingNS : xmlrpc_XmlInitEncoding)(
- &parser->m_initEncoding, &parser->m_encoding, s))
- return XML_ERROR_NONE;
- return handleUnknownEncoding(xmlParserP, protocolEncodingName);
- }
- static enum XML_Error
- processXmlDecl(XML_Parser const xmlParserP,
- int const isGeneralTextEntity,
- const char * const s,
- const char * const next) {
- Parser * const parser = (Parser *) xmlParserP;
- const char *encodingName = 0;
- const ENCODING *newEncoding = 0;
- const char *version;
- int standalone = -1;
- if (!(ns
- ? xmlrpc_XmlParseXmlDeclNS
- : xmlrpc_XmlParseXmlDecl)(isGeneralTextEntity,
- parser->m_encoding,
- s,
- next,
- &eventPtr,
- &version,
- &encodingName,
- &newEncoding,
- &standalone))
- return XML_ERROR_SYNTAX;
- if (!isGeneralTextEntity && standalone == 1) {
- dtd.standalone = 1;
- if (paramEntityParsing == XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
- paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
- }
- if (defaultHandler)
- reportDefault(xmlParserP, parser->m_encoding, s, next);
- if (!protocolEncodingName) {
- if (newEncoding) {
- if (newEncoding->minBytesPerChar !=
- parser->m_encoding->minBytesPerChar) {
- eventPtr = encodingName;
- return XML_ERROR_INCORRECT_ENCODING;
- }
- parser->m_encoding = newEncoding;
- }
- else if (encodingName) {
- enum XML_Error result;
- const XML_Char * s =
- poolStoreString(&tempPool,
- parser->m_encoding,
- encodingName,
- encodingName
- + XmlNameLength(parser->m_encoding,
- encodingName));
- if (!s)
- return XML_ERROR_NO_MEMORY;
- result = handleUnknownEncoding(xmlParserP, s);
- poolDiscard(&tempPool);
- if (result == XML_ERROR_UNKNOWN_ENCODING)
- eventPtr = encodingName;
- return result;
- }
- }
- return XML_ERROR_NONE;
- }
- static ATTRIBUTE_ID *
- getAttributeId(XML_Parser parser,
- const ENCODING *enc,
- const char *start,
- const char *end)
- {
- ATTRIBUTE_ID *id;
- const XML_Char *name;
- if (!poolAppendChar(&dtd.pool, XML_T('\0')))
- return 0;
- name = poolStoreString(&dtd.pool, enc, start, end);
- if (!name)
- return 0;
- ++name;
- id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, name, sizeof(ATTRIBUTE_ID));
- if (!id)
- return 0;
- if (id->name != name)
- poolDiscard(&dtd.pool);
- else {
- poolFinish(&dtd.pool);
- if (!ns)
- ;
- else if (name[0] == 'x'
- && name[1] == 'm'
- && name[2] == 'l'
- && name[3] == 'n'
- && name[4] == 's'
- && (name[5] == XML_T('\0') || name[5] == XML_T(':'))) {
- if (name[5] == '\0')
- id->prefix = &dtd.defaultPrefix;
- else
- id->prefix = (PREFIX *)lookup(&dtd.prefixes, name + 6, sizeof(PREFIX));
- id->xmlns = 1;
- }
- else {
- int i;
- for (i = 0; name[i]; i++) {
- if (name[i] == XML_T(':')) {
- int j;
- for (j = 0; j < i; j++) {
- if (!poolAppendChar(&dtd.pool, name[j]))
- return 0;
- }
- if (!poolAppendChar(&dtd.pool, XML_T('\0')))
- return 0;
- id->prefix = (PREFIX *)
- lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
- if (id->prefix->name == poolStart(&dtd.pool))
- poolFinish(&dtd.pool);
- else
- poolDiscard(&dtd.pool);
- break;
- }
- }
- }
- }
- return id;
- }
- static
- void normalizePublicId(XML_Char *publicId)
- {
- XML_Char *p = publicId;
- XML_Char *s;
- for (s = publicId; *s; s++) {
- switch (*s) {
- case 0x20:
- case 0xD:
- case 0xA:
- if (p != publicId && p[-1] != 0x20)
- *p++ = 0x20;
- break;
- default:
- *p++ = *s;
- }
- }
- if (p != publicId && p[-1] == 0x20)
- --p;
- *p = XML_T('\0');
- }
- static int setElementTypePrefix(XML_Parser parser, ELEMENT_TYPE *elementType)
- {
- const XML_Char *name;
- for (name = elementType->name; *name; name++) {
- if (*name == XML_T(':')) {
- PREFIX *prefix;
- const XML_Char *s;
- for (s = elementType->name; s != name; s++) {
- if (!poolAppendChar(&dtd.pool, *s))
- return 0;
- }
- if (!poolAppendChar(&dtd.pool, XML_T('\0')))
- return 0;
- prefix = (PREFIX *)
- lookup(&dtd.prefixes, poolStart(&dtd.pool), sizeof(PREFIX));
- if (!prefix)
- return 0;
- if (prefix->name == poolStart(&dtd.pool))
- poolFinish(&dtd.pool);
- else
- poolDiscard(&dtd.pool);
- elementType->prefix = prefix;
- }
- }
- return 1;
- }
- static enum XML_Error
- appendAttributeValue(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- int const isCdata,
- const char * const ptrArg,
- const char * const end,
- STRING_POOL * const pool) {
- Parser * const parser = (Parser *) xmlParserP;
- const char * ptr;
- ptr = ptrArg;
- for (;;) {
- const char *next;
- int tok = XmlAttributeValueTok(enc, ptr, end, &next);
- switch (tok) {
- case XML_TOK_NONE:
- return XML_ERROR_NONE;
- case XML_TOK_INVALID:
- if (enc == parser->m_encoding)
- eventPtr = next;
- return XML_ERROR_INVALID_TOKEN;
- case XML_TOK_PARTIAL:
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_INVALID_TOKEN;
- case XML_TOK_CHAR_REF:
- {
- XML_Char buf[XML_ENCODE_MAX];
- int i;
- int n = XmlCharRefNumber(enc, ptr);
- if (n < 0) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_BAD_CHAR_REF;
- }
- if (!isCdata
- && n == 0x20 /* space */
- && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
- break;
- n = XmlEncode(n, (ICHAR *)buf);
- if (!n) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_BAD_CHAR_REF;
- }
- for (i = 0; i < n; i++) {
- if (!poolAppendChar(pool, buf[i]))
- return XML_ERROR_NO_MEMORY;
- }
- }
- break;
- case XML_TOK_DATA_CHARS:
- if (!poolAppend(pool, enc, ptr, next))
- return XML_ERROR_NO_MEMORY;
- break;
- break;
- case XML_TOK_TRAILING_CR:
- next = ptr + enc->minBytesPerChar;
- /* fall through */
- case XML_TOK_ATTRIBUTE_VALUE_S:
- case XML_TOK_DATA_NEWLINE:
- if (!isCdata && (poolLength(pool) == 0 || poolLastChar(pool) == 0x20))
- break;
- if (!poolAppendChar(pool, 0x20))
- return XML_ERROR_NO_MEMORY;
- break;
- case XML_TOK_ENTITY_REF:
- {
- const XML_Char *name;
- ENTITY *entity;
- XML_Char ch = XmlPredefinedEntityName(enc,
- ptr + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (ch) {
- if (!poolAppendChar(pool, ch))
- return XML_ERROR_NO_MEMORY;
- break;
- }
- name = poolStoreString(&temp2Pool, enc,
- ptr + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!name)
- return XML_ERROR_NO_MEMORY;
- entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
- poolDiscard(&temp2Pool);
- if (!entity) {
- if (dtd.complete) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_UNDEFINED_ENTITY;
- }
- }
- else if (entity->open) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_RECURSIVE_ENTITY_REF;
- }
- else if (entity->notation) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_BINARY_ENTITY_REF;
- }
- else if (!entity->textPtr) {
- if (enc == parser->m_encoding)
- eventPtr = ptr;
- return XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF;
- }
- else {
- enum XML_Error result;
- const XML_Char *textEnd = entity->textPtr + entity->textLen;
- entity->open = 1;
- result = appendAttributeValue(xmlParserP, internalEncoding,
- isCdata, (char *)entity->textPtr,
- (char *)textEnd, pool);
- entity->open = 0;
- if (result)
- return result;
- }
- }
- break;
- default:
- abort();
- }
- ptr = next;
- }
- /* not reached */
- }
- static enum XML_Error
- storeAttributeValue(XML_Parser parser, const ENCODING *enc, int isCdata,
- const char *ptr, const char *end,
- STRING_POOL *pool)
- {
- enum XML_Error result =
- appendAttributeValue(parser, enc, isCdata, ptr, end, pool);
- if (result)
- return result;
- if (!isCdata && poolLength(pool) && poolLastChar(pool) == 0x20)
- poolChop(pool);
- if (!poolAppendChar(pool, XML_T('\0')))
- return XML_ERROR_NO_MEMORY;
- return XML_ERROR_NONE;
- }
- static
- enum XML_Error
- storeEntityValue(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const entityTextPtrArg,
- const char * const entityTextEnd) {
- Parser * const parser = (Parser *) xmlParserP;
- STRING_POOL * const pool = &(dtd.pool);
- const char * entityTextPtr;
- entityTextPtr = entityTextPtrArg;
- for (;;) {
- const char * next;
- int tok;
- tok = XmlEntityValueTok(enc, entityTextPtr, entityTextEnd, &next);
- switch (tok) {
- case XML_TOK_PARAM_ENTITY_REF:
- if (parentParser || enc != parser->m_encoding) {
- enum XML_Error result;
- const XML_Char *name;
- ENTITY *entity;
- name = poolStoreString(&tempPool, enc,
- entityTextPtr + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!name)
- return XML_ERROR_NO_MEMORY;
- entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
- poolDiscard(&tempPool);
- if (!entity) {
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_UNDEFINED_ENTITY;
- }
- if (entity->open) {
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_RECURSIVE_ENTITY_REF;
- }
- if (entity->systemId) {
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_PARAM_ENTITY_REF;
- }
- entity->open = 1;
- result = storeEntityValue(parser,
- internalEncoding,
- (char *)entity->textPtr,
- (char *)(entity->textPtr + entity->textLen));
- entity->open = 0;
- if (result)
- return result;
- break;
- }
- eventPtr = entityTextPtr;
- return XML_ERROR_SYNTAX;
- case XML_TOK_NONE:
- return XML_ERROR_NONE;
- case XML_TOK_ENTITY_REF:
- case XML_TOK_DATA_CHARS:
- if (!poolAppend(pool, enc, entityTextPtr, next))
- return XML_ERROR_NO_MEMORY;
- break;
- case XML_TOK_TRAILING_CR:
- next = entityTextPtr + enc->minBytesPerChar;
- /* fall through */
- case XML_TOK_DATA_NEWLINE:
- if (pool->end == pool->ptr && !poolGrow(pool))
- return XML_ERROR_NO_MEMORY;
- *(pool->ptr)++ = 0xA;
- break;
- case XML_TOK_CHAR_REF:
- {
- XML_Char buf[XML_ENCODE_MAX];
- int i;
- int n = XmlCharRefNumber(enc, entityTextPtr);
- if (n < 0) {
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_BAD_CHAR_REF;
- }
- n = XmlEncode(n, (ICHAR *)buf);
- if (!n) {
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_BAD_CHAR_REF;
- }
- for (i = 0; i < n; i++) {
- if (pool->end == pool->ptr && !poolGrow(pool))
- return XML_ERROR_NO_MEMORY;
- *(pool->ptr)++ = buf[i];
- }
- }
- break;
- case XML_TOK_PARTIAL:
- if (enc == parser->m_encoding)
- eventPtr = entityTextPtr;
- return XML_ERROR_INVALID_TOKEN;
- case XML_TOK_INVALID:
- if (enc == parser->m_encoding)
- eventPtr = next;
- return XML_ERROR_INVALID_TOKEN;
- default:
- abort();
- }
- entityTextPtr = next;
- }
- /* not reached */
- }
- static int
- defineAttribute(ELEMENT_TYPE *type,
- ATTRIBUTE_ID *attId,
- int isCdata,
- int isId,
- const XML_Char *value)
- {
- DEFAULT_ATTRIBUTE *att;
- if (value || isId) {
- /* The handling of default attributes gets messed up if we have
- a default which duplicates a non-default. */
- int i;
- for (i = 0; i < type->nDefaultAtts; i++)
- if (attId == type->defaultAtts[i].id)
- return 1;
- if (isId && !type->idAtt && !attId->xmlns)
- type->idAtt = attId;
- }
- if (type->nDefaultAtts == type->allocDefaultAtts) {
- if (type->allocDefaultAtts == 0) {
- type->allocDefaultAtts = 8;
- type->defaultAtts =
- malloc(type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
- }
- else {
- type->allocDefaultAtts *= 2;
- type->defaultAtts =
- realloc(type->defaultAtts,
- type->allocDefaultAtts*sizeof(DEFAULT_ATTRIBUTE));
- }
- if (!type->defaultAtts)
- return 0;
- }
- att = type->defaultAtts + type->nDefaultAtts;
- att->id = attId;
- att->value = value;
- att->isCdata = isCdata;
- if (!isCdata)
- attId->maybeTokenized = 1;
- type->nDefaultAtts += 1;
- return 1;
- }
- /* If tagNamePtr is non-null, build a real list of attributes,
- otherwise just check the attributes for well-formedness. */
- static enum XML_Error
- storeAtts(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const attStr,
- TAG_NAME * const tagNamePtr,
- BINDING ** const bindingsPtr) {
- Parser * const parser = (Parser *)xmlParserP;
- ELEMENT_TYPE *elementType = 0;
- int nDefaultAtts = 0;
- const XML_Char ** appAtts;
- /* the attribute list to pass to the application */
- int attIndex = 0;
- int i;
- int n;
- int nPrefixes = 0;
- BINDING *binding;
- const XML_Char *localPart;
- /* lookup the element type name */
- if (tagNamePtr) {
- elementType = (ELEMENT_TYPE *)
- lookup(&dtd.elementTypes, tagNamePtr->str, 0);
- if (!elementType) {
- tagNamePtr->str = poolCopyString(&dtd.pool, tagNamePtr->str);
- if (!tagNamePtr->str)
- return XML_ERROR_NO_MEMORY;
- elementType = (ELEMENT_TYPE *)
- lookup(&dtd.elementTypes, tagNamePtr->str, sizeof(ELEMENT_TYPE));
- if (!elementType)
- return XML_ERROR_NO_MEMORY;
- if (ns && !setElementTypePrefix(xmlParserP, elementType))
- return XML_ERROR_NO_MEMORY;
- }
- nDefaultAtts = elementType->nDefaultAtts;
- }
- /* get the attributes from the tokenizer */
- n = XmlGetAttributes(enc, attStr, attsSize, atts);
- if (n + nDefaultAtts > attsSize) {
- int oldAttsSize = attsSize;
- attsSize = n + nDefaultAtts + INIT_ATTS_SIZE;
- atts = realloc((void *)atts, attsSize * sizeof(ATTRIBUTE));
- if (!atts)
- return XML_ERROR_NO_MEMORY;
- if (n > oldAttsSize)
- XmlGetAttributes(enc, attStr, n, atts);
- }
- appAtts = (const XML_Char **)atts;
- for (i = 0; i < n; i++) {
- /* add the name and value to the attribute list */
- ATTRIBUTE_ID *attId = getAttributeId(xmlParserP, enc, atts[i].name,
- atts[i].name
- + XmlNameLength(enc, atts[i].name));
- if (!attId)
- return XML_ERROR_NO_MEMORY;
- /* detect duplicate attributes */
- if ((attId->name)[-1]) {
- if (enc == parser->m_encoding)
- eventPtr = atts[i].name;
- return XML_ERROR_DUPLICATE_ATTRIBUTE;
- }
- (attId->name)[-1] = 1;
- appAtts[attIndex++] = attId->name;
- if (!atts[i].normalized) {
- enum XML_Error result;
- int isCdata = 1;
- /* figure out whether declared as other than CDATA */
- if (attId->maybeTokenized) {
- int j;
- for (j = 0; j < nDefaultAtts; j++) {
- if (attId == elementType->defaultAtts[j].id) {
- isCdata = elementType->defaultAtts[j].isCdata;
- break;
- }
- }
- }
- /* normalize the attribute value */
- result = storeAttributeValue(xmlParserP, enc, isCdata,
- atts[i].valuePtr, atts[i].valueEnd,
- &tempPool);
- if (result)
- return result;
- if (tagNamePtr) {
- appAtts[attIndex] = poolStart(&tempPool);
- poolFinish(&tempPool);
- }
- else
- poolDiscard(&tempPool);
- }
- else if (tagNamePtr) {
- /* the value did not need normalizing */
- appAtts[attIndex] =
- poolStoreString(&tempPool, enc, atts[i].valuePtr, atts[i].valueEnd);
- if (appAtts[attIndex] == 0)
- return XML_ERROR_NO_MEMORY;
- poolFinish(&tempPool);
- }
- /* handle prefixed attribute names */
- if (attId->prefix && tagNamePtr) {
- if (attId->xmlns) {
- /* deal with namespace declarations here */
- if (!addBinding(xmlParserP, attId->prefix, attId, appAtts[attIndex],
- bindingsPtr))
- return XML_ERROR_NO_MEMORY;
- --attIndex;
- }
- else {
- /* deal with other prefixed names later */
- attIndex++;
- nPrefixes++;
- (attId->name)[-1] = 2;
- }
- }
- else
- attIndex++;
- }
- if (tagNamePtr) {
- int j;
- nSpecifiedAtts = attIndex;
- if (elementType->idAtt && (elementType->idAtt->name)[-1]) {
- for (i = 0; i < attIndex; i += 2)
- if (appAtts[i] == elementType->idAtt->name) {
- idAttIndex = i;
- break;
- }
- }
- else
- idAttIndex = -1;
- /* do attribute defaulting */
- for (j = 0; j < nDefaultAtts; j++) {
- const DEFAULT_ATTRIBUTE *da = elementType->defaultAtts + j;
- if (!(da->id->name)[-1] && da->value) {
- if (da->id->prefix) {
- if (da->id->xmlns) {
- if (!addBinding(xmlParserP, da->id->prefix, da->id, da->value,
- bindingsPtr))
- return XML_ERROR_NO_MEMORY;
- }
- else {
- (da->id->name)[-1] = 2;
- nPrefixes++;
- appAtts[attIndex++] = da->id->name;
- appAtts[attIndex++] = da->value;
- }
- }
- else {
- (da->id->name)[-1] = 1;
- appAtts[attIndex++] = da->id->name;
- appAtts[attIndex++] = da->value;
- }
- }
- }
- appAtts[attIndex] = 0;
- }
- i = 0;
- if (nPrefixes) {
- /* expand prefixed attribute names */
- for (; i < attIndex; i += 2) {
- if (appAtts[i][-1] == 2) {
- ATTRIBUTE_ID *id;
- ((XML_Char *)(appAtts[i]))[-1] = 0;
- id = (ATTRIBUTE_ID *)lookup(&dtd.attributeIds, appAtts[i], 0);
- if (id->prefix->binding) {
- int j;
- const BINDING *b = id->prefix->binding;
- const XML_Char *s = appAtts[i];
- for (j = 0; j < b->uriLen; j++) {
- if (!poolAppendChar(&tempPool, b->uri[j]))
- return XML_ERROR_NO_MEMORY;
- }
- while (*s++ != ':')
- ;
- do {
- if (!poolAppendChar(&tempPool, *s))
- return XML_ERROR_NO_MEMORY;
- } while (*s++);
- appAtts[i] = poolStart(&tempPool);
- poolFinish(&tempPool);
- }
- if (!--nPrefixes)
- break;
- }
- else
- ((XML_Char *)(appAtts[i]))[-1] = 0;
- }
- }
- /* clear the flags that say whether attributes were specified */
- for (; i < attIndex; i += 2)
- ((XML_Char *)(appAtts[i]))[-1] = 0;
- if (!tagNamePtr)
- return XML_ERROR_NONE;
- for (binding = *bindingsPtr; binding; binding = binding->nextTagBinding)
- binding->attId->name[-1] = 0;
- /* expand the element type name */
- if (elementType->prefix) {
- binding = elementType->prefix->binding;
- if (!binding)
- return XML_ERROR_NONE;
- localPart = tagNamePtr->str;
- while (*localPart++ != XML_T(':'))
- ;
- }
- else if (dtd.defaultPrefix.binding) {
- binding = dtd.defaultPrefix.binding;
- localPart = tagNamePtr->str;
- }
- else
- return XML_ERROR_NONE;
- tagNamePtr->localPart = localPart;
- tagNamePtr->uriLen = binding->uriLen;
- for (i = 0; localPart[i++];)
- ;
- n = i + binding->uriLen;
- if (n > binding->uriAlloc) {
- TAG *p;
- XML_Char *uri = malloc((n + EXPAND_SPARE) * sizeof(XML_Char));
- if (!uri)
- return XML_ERROR_NO_MEMORY;
- binding->uriAlloc = n + EXPAND_SPARE;
- memcpy(uri, binding->uri, binding->uriLen * sizeof(XML_Char));
- for (p = tagStack; p; p = p->parent)
- if (p->name.str == binding->uri)
- p->name.str = uri;
- free(binding->uri);
- binding->uri = uri;
- }
- memcpy(binding->uri + binding->uriLen, localPart, i * sizeof(XML_Char));
- tagNamePtr->str = binding->uri;
- return XML_ERROR_NONE;
- }
- static Processor epilogProcessor;
- static void
- epilogProcessor(XML_Parser const xmlParserP,
- const char * const startArg,
- const char * const end,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
- const char * s;
- *errorP = NULL;
- s = startArg;
- processor = epilogProcessor;
- eventPtr = s;
- for (;;) {
- const char *next;
- int tok = XmlPrologTok(parser->m_encoding, s, end, &next);
- eventEndPtr = next;
- switch (tok) {
- case -XML_TOK_PROLOG_S:
- if (defaultHandler) {
- eventEndPtr = end;
- reportDefault(xmlParserP, parser->m_encoding, s, end);
- }
- /* fall through */
- case XML_TOK_NONE:
- if (nextPtr)
- *nextPtr = end;
- *errorCodeP = XML_ERROR_NONE;
- return;
- case XML_TOK_PROLOG_S:
- if (defaultHandler)
- reportDefault(xmlParserP, parser->m_encoding, s, next);
- break;
- case XML_TOK_PI:
- if (!reportProcessingInstruction(xmlParserP, parser->m_encoding,
- s, next)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- case XML_TOK_COMMENT:
- if (!reportComment(xmlParserP, parser->m_encoding, s, next)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- case XML_TOK_INVALID:
- eventPtr = next;
- *errorCodeP = XML_ERROR_INVALID_TOKEN;
- return;
- case XML_TOK_PARTIAL:
- if (nextPtr) {
- *nextPtr = s;
- *errorCodeP = XML_ERROR_NONE;
- } else
- *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
- return;
- case XML_TOK_PARTIAL_CHAR:
- if (nextPtr) {
- *nextPtr = s;
- *errorCodeP = XML_ERROR_NONE;
- } else
- *errorCodeP = XML_ERROR_PARTIAL_CHAR;
- return;
- default:
- *errorCodeP = XML_ERROR_JUNK_AFTER_DOC_ELEMENT;
- return;
- }
- eventPtr = s = next;
- }
- }
- static enum XML_Error
- doCdataSection(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char ** const startPtr,
- const char * const end,
- const char ** const nextPtr) {
- Parser * const parser = (Parser *) xmlParserP;
- const char *s = *startPtr;
- const char **eventPP;
- const char **eventEndPP;
- if (enc == parser->m_encoding) {
- eventPP = &eventPtr;
- *eventPP = s;
- eventEndPP = &eventEndPtr;
- }
- else {
- eventPP = &(openInternalEntities->internalEventPtr);
- eventEndPP = &(openInternalEntities->internalEventEndPtr);
- }
- *eventPP = s;
- *startPtr = 0;
- for (;;) {
- const char *next;
- int tok = XmlCdataSectionTok(enc, s, end, &next);
- *eventEndPP = next;
- switch (tok) {
- case XML_TOK_CDATA_SECT_CLOSE:
- if (endCdataSectionHandler)
- endCdataSectionHandler(handlerArg);
- else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- *startPtr = next;
- return XML_ERROR_NONE;
- case XML_TOK_DATA_NEWLINE:
- if (characterDataHandler) {
- XML_Char c = 0xA;
- characterDataHandler(handlerArg, &c, 1);
- }
- else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- break;
- case XML_TOK_DATA_CHARS:
- if (characterDataHandler) {
- if (MUST_CONVERT(enc, s)) {
- for (;;) {
- ICHAR *dataPtr = (ICHAR *)dataBuf;
- XmlConvert(enc, &s, next, &dataPtr, (ICHAR *)dataBufEnd);
- *eventEndPP = next;
- {
- size_t const len = dataPtr - (ICHAR *)dataBuf;
- assert((size_t)(int)len == len); /* parser requirement */
- characterDataHandler(handlerArg, dataBuf, (int)len);
- }
- if (s == next)
- break;
- *eventPP = s;
- }
- }
- else {
- size_t const len = (XML_Char *)next - (XML_Char *)s;
- assert((size_t)(int)len == len); /* parser requirement */
- characterDataHandler(handlerArg, (XML_Char *)s, (int)len);
- }
- }
- else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- break;
- case XML_TOK_INVALID:
- *eventPP = next;
- return XML_ERROR_INVALID_TOKEN;
- case XML_TOK_PARTIAL_CHAR:
- if (nextPtr) {
- *nextPtr = s;
- return XML_ERROR_NONE;
- }
- return XML_ERROR_PARTIAL_CHAR;
- case XML_TOK_PARTIAL:
- case XML_TOK_NONE:
- if (nextPtr) {
- *nextPtr = s;
- return XML_ERROR_NONE;
- }
- return XML_ERROR_UNCLOSED_CDATA_SECTION;
- default:
- abort();
- }
- *eventPP = s = next;
- }
- /* not reached */
- }
- /* Forward declaration for recursive reference: */
- static void
- doContent(XML_Parser const xmlParserP,
- int const startTagLevel,
- const ENCODING * const enc,
- const char * const startArg,
- const char * const end,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP);
- static Processor contentProcessor;
- static void
- contentProcessor(XML_Parser const xmlParserP,
- const char * const start,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- const char * error;
- parser->m_errorString = NULL;
- doContent(xmlParserP, 0, parser->m_encoding, start, end, endPtr,
- errorCodeP, &error);
- if (*errorCodeP != XML_ERROR_NONE) {
- if (error) {
- xmlrpc_asprintf(errorP, "Invalid XML \"content\". %s", error);
-
- xmlrpc_strfree(error);
- } else {
- const char * const sampleXml = extractXmlSample(start, end, 40);
- xmlrpc_asprintf(errorP, "Invalid XML \"content\" starting "
- "with '%s'. %s",
- sampleXml,
- xmlrpc_XML_ErrorString(*errorCodeP));
- xmlrpc_strfree(sampleXml);
- }
- } else
- *errorP = NULL;
- }
- /* The idea here is to avoid using stack for each CDATA section when
- the whole file is parsed with one call. */
- static Processor cdataSectionProcessor;
- static void
- cdataSectionProcessor(XML_Parser const xmlParserP,
- const char * const startArg,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
-
- enum XML_Error result;
- const char * start;
- start = startArg;
- result =
- doCdataSection(xmlParserP, parser->m_encoding, &start, end, endPtr);
- if (start) {
- processor = contentProcessor;
- contentProcessor(xmlParserP, start, end, endPtr, errorCodeP, errorP);
- } else {
- *errorCodeP = result;
- *errorP = NULL;
- }
- }
- static void
- doEntityRef(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const s,
- const char * const next,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
- XML_Char const ch = XmlPredefinedEntityName(enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- const XML_Char *name;
- ENTITY *entity;
- *errorP = NULL;
- if (ch) {
- if (characterDataHandler)
- characterDataHandler(handlerArg, &ch, 1);
- else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- *errorCodeP = XML_ERROR_NONE;
- return;
- }
- name = poolStoreString(&dtd.pool, enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- entity = (ENTITY *)lookup(&dtd.generalEntities, name, 0);
- poolDiscard(&dtd.pool);
- if (!entity) {
- if (dtd.complete || dtd.standalone)
- *errorCodeP = XML_ERROR_UNDEFINED_ENTITY;
- else {
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- *errorCodeP = XML_ERROR_NONE;
- }
- return;
- }
- if (entity->open) {
- *errorCodeP = XML_ERROR_RECURSIVE_ENTITY_REF;
- return;
- }
- if (entity->notation) {
- *errorCodeP = XML_ERROR_BINARY_ENTITY_REF;
- return;
- }
- if (entity) {
- if (entity->textPtr) {
- OPEN_INTERNAL_ENTITY openEntity;
- if (defaultHandler && !defaultExpandInternalEntities) {
- reportDefault(xmlParserP, enc, s, next);
- *errorCodeP = XML_ERROR_NONE;
- return;
- }
- entity->open = 1;
- openEntity.next = openInternalEntities;
- openInternalEntities = &openEntity;
- openEntity.entity = entity;
- openEntity.internalEventPtr = 0;
- openEntity.internalEventEndPtr = 0;
- doContent(xmlParserP,
- tagLevel,
- internalEncoding,
- (char *)entity->textPtr,
- (char *)(entity->textPtr + entity->textLen),
- 0, errorCodeP, errorP);
- entity->open = 0;
- openInternalEntities = openEntity.next;
- if (*errorCodeP != XML_ERROR_NONE)
- return;
- } else if (externalEntityRefHandler) {
- const XML_Char *context;
- entity->open = 1;
- context = getContext(xmlParserP);
- entity->open = 0;
- if (!context) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- if (!externalEntityRefHandler(externalEntityRefHandlerArg,
- context,
- entity->base,
- entity->systemId,
- entity->publicId)) {
- *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
- return;
- }
- poolDiscard(&tempPool);
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- }
- *errorCodeP = XML_ERROR_NONE;
- }
- static void
- doStartTagNoAtts(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const s,
- const char * const next,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- TAG *tag;
- *errorP = NULL;
- if (freeTagList) {
- tag = freeTagList;
- freeTagList = freeTagList->parent;
- } else {
- tag = malloc(sizeof(TAG));
- if (!tag) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- tag->buf = malloc(INIT_TAG_BUF_SIZE);
- if (!tag->buf) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- tag->bufEnd = tag->buf + INIT_TAG_BUF_SIZE;
- }
- tag->bindings = NULL;
- tag->parent = tagStack;
- tagStack = tag;
- tag->name.localPart = 0;
- tag->rawName = s + enc->minBytesPerChar;
- tag->rawNameLength = XmlNameLength(enc, tag->rawName);
- if (nextPtr) {
- /* Need to guarantee that: tag->buf +
- ROUND_UP(tag->rawNameLength, sizeof(XML_Char)) <=
- tag->bufEnd - sizeof(XML_Char)
- */
- if (tag->rawNameLength +
- (int)(sizeof(XML_Char) - 1) +
- (int)sizeof(XML_Char) > tag->bufEnd - tag->buf) {
- int bufSize = tag->rawNameLength * 4;
- bufSize = ROUND_UP(bufSize, sizeof(XML_Char));
- tag->buf = realloc(tag->buf, bufSize);
- if (!tag->buf) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- tag->bufEnd = tag->buf + bufSize;
- }
- memcpy(tag->buf, tag->rawName, tag->rawNameLength);
- tag->rawName = tag->buf;
- }
- ++tagLevel;
- if (startElementHandler) {
- enum XML_Error result;
- XML_Char *toPtr;
- for (;;) {
- const char *rawNameEnd = tag->rawName + tag->rawNameLength;
- const char *fromPtr = tag->rawName;
- if (nextPtr)
- toPtr = (XML_Char *)
- (tag->buf + ROUND_UP(tag->rawNameLength,
- sizeof(XML_Char)));
- else
- toPtr = (XML_Char *)tag->buf;
- tag->name.str = toPtr;
- XmlConvert(enc,
- &fromPtr, rawNameEnd,
- (ICHAR **)&toPtr, (ICHAR *)tag->bufEnd - 1);
- if (fromPtr == rawNameEnd)
- break;
- else {
- size_t const bufSize = (tag->bufEnd - tag->buf) << 1;
- tag->buf = realloc(tag->buf, bufSize);
- if (!tag->buf) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- tag->bufEnd = tag->buf + bufSize;
- if (nextPtr)
- tag->rawName = tag->buf;
- }
- }
- *toPtr = XML_T('\0');
- result = storeAtts(xmlParserP, enc, s,
- &(tag->name), &(tag->bindings));
- if (result) {
- *errorCodeP = result;
- return;
- }
- startElementHandler(handlerArg, tag->name.str,
- (const XML_Char **)atts);
- poolClear(&tempPool);
- } else {
- tag->name.str = 0;
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- }
- }
- static void
- doEmptyElementNoAtts(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const s,
- const char * const end,
- const char * const next,
- const char ** const nextPtr,
- const char ** const eventPP,
- const char ** const eventEndPP,
- bool * const doneP,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
- if (startElementHandler || endElementHandler) {
- const char * const rawName = s + enc->minBytesPerChar;
- enum XML_Error result;
- BINDING * bindings;
- TAG_NAME name;
- bindings = NULL; /* initial value */
- name.str = poolStoreString(&tempPool, enc, rawName,
- rawName + XmlNameLength(enc, rawName));
- if (!name.str) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- poolFinish(&tempPool);
- result = storeAtts(xmlParserP, enc, s, &name, &bindings);
- if (result) {
- *errorCodeP = result;
- return;
- }
- poolFinish(&tempPool);
- if (startElementHandler)
- startElementHandler(handlerArg, name.str, (const XML_Char **)atts);
- if (endElementHandler) {
- if (startElementHandler)
- *eventPP = *eventEndPP;
- endElementHandler(handlerArg, name.str);
- }
- poolClear(&tempPool);
- while (bindings) {
- BINDING * const b = bindings;
- if (endNamespaceDeclHandler)
- endNamespaceDeclHandler(handlerArg, b->prefix->name);
- bindings = bindings->nextTagBinding;
- b->nextTagBinding = freeBindingList;
- freeBindingList = b;
- b->prefix->binding = b->prevPrefixBinding;
- }
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- if (tagLevel == 0) {
- epilogProcessor(xmlParserP, next, end, nextPtr, errorCodeP, errorP);
- *doneP = true;
- } else
- *doneP = false;
- }
- static void
- doEndTag(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char * const s,
- const char * const end,
- const char * const next,
- const char ** const nextPtr,
- int const startTagLevel,
- const char ** const eventPP,
- bool * const doneP,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- if (tagLevel == startTagLevel)
- *errorCodeP = XML_ERROR_ASYNC_ENTITY;
- else {
- TAG * const tag = tagStack;
- int len;
- const char * rawName;
- tagStack = tag->parent;
- tag->parent = freeTagList;
- freeTagList = tag;
- rawName = s + enc->minBytesPerChar*2;
- len = XmlNameLength(enc, rawName);
- if (len != tag->rawNameLength
- || memcmp(tag->rawName, rawName, len) != 0) {
- *eventPP = rawName;
- *errorCodeP = XML_ERROR_TAG_MISMATCH;
- } else {
- --tagLevel;
- if (endElementHandler && tag->name.str) {
- if (tag->name.localPart) {
- XML_Char * to;
- const XML_Char * from;
- to = (XML_Char *)tag->name.str + tag->name.uriLen;
- from = tag->name.localPart;
- while ((*to++ = *from++) != 0)
- ;
- }
- endElementHandler(handlerArg, tag->name.str);
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- while (tag->bindings) {
- BINDING * const b = tag->bindings;
- if (endNamespaceDeclHandler)
- endNamespaceDeclHandler(handlerArg, b->prefix->name);
- tag->bindings = tag->bindings->nextTagBinding;
- b->nextTagBinding = freeBindingList;
- freeBindingList = b;
- b->prefix->binding = b->prevPrefixBinding;
- }
- if (tagLevel == 0) {
- epilogProcessor(xmlParserP, next, end, nextPtr,
- errorCodeP, errorP);
- *doneP = true;
- } else {
- *errorCodeP = XML_ERROR_NONE;
- *doneP = false;
- }
- }
- }
- }
- static void
- processContentToken(XML_Parser const xmlParserP,
- int const tok,
- const ENCODING * const enc,
- const char * const s,
- const char * const end,
- const char ** const nextP,
- const char ** const nextPtr,
- int const startTagLevel,
- const char ** const eventPP,
- const char ** const eventEndPP,
- bool * const doneP,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- *errorP = NULL;
- *errorCodeP = XML_ERROR_NONE;
- switch (tok) {
- case XML_TOK_TRAILING_CR:
- if (nextPtr) {
- *nextPtr = s;
- *doneP = true;
- } else {
- *eventEndPP = end;
- if (characterDataHandler) {
- XML_Char c = 0xA;
- characterDataHandler(handlerArg, &c, 1);
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, end);
- if (startTagLevel == 0)
- *errorCodeP = XML_ERROR_NO_ELEMENTS;
- else if (tagLevel != startTagLevel) {
- *errorCodeP = XML_ERROR_ASYNC_ENTITY;
- } else
- *doneP = true;
- }
- break;
- case XML_TOK_NONE:
- if (nextPtr) {
- *nextPtr = s;
- *doneP = true;
- } else if (startTagLevel > 0) {
- if (tagLevel != startTagLevel)
- *errorCodeP = XML_ERROR_ASYNC_ENTITY;
- else
- *doneP = true;
- } else
- *errorCodeP = XML_ERROR_NO_ELEMENTS;
- break;
- case XML_TOK_INVALID:
- *eventPP = *nextP;
- *errorCodeP = XML_ERROR_INVALID_TOKEN;
- xmlrpc_asprintf(errorP, "Invalid token, starting %ld bytes in",
- (long)(*nextP - s));
- break;
- case XML_TOK_PARTIAL:
- if (nextPtr) {
- *nextPtr = s;
- *doneP = true;
- } else
- *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
- break;
- case XML_TOK_PARTIAL_CHAR:
- if (nextPtr) {
- *nextPtr = s;
- *doneP = true;
- } else
- *errorCodeP = XML_ERROR_PARTIAL_CHAR;
- break;
- case XML_TOK_ENTITY_REF:
- doEntityRef(xmlParserP, enc, s, *nextP, errorCodeP, errorP);
- break;
- case XML_TOK_START_TAG_WITH_ATTS:
- if (!startElementHandler)
- *errorCodeP = storeAtts(xmlParserP, enc, s, 0, 0);
- if (*errorCodeP == XML_ERROR_NONE)
- doStartTagNoAtts(xmlParserP, enc, s, *nextP, nextPtr,
- errorCodeP, errorP);
- break;
- case XML_TOK_START_TAG_NO_ATTS:
- doStartTagNoAtts(xmlParserP, enc, s, *nextP, nextPtr,
- errorCodeP, errorP);
- break;
- case XML_TOK_EMPTY_ELEMENT_WITH_ATTS:
- if (!startElementHandler)
- *errorCodeP = storeAtts(xmlParserP, enc, s, 0, 0);
-
- if (*errorCodeP == XML_ERROR_NONE)
- doEmptyElementNoAtts(xmlParserP, enc, s, end, *nextP, nextPtr,
- eventPP, eventEndPP,
- doneP, errorCodeP, errorP);
- break;
- case XML_TOK_EMPTY_ELEMENT_NO_ATTS:
- doEmptyElementNoAtts(xmlParserP, enc, s, end, *nextP, nextPtr,
- eventPP, eventEndPP,
- doneP, errorCodeP, errorP);
- break;
- case XML_TOK_END_TAG:
- doEndTag(xmlParserP, enc, s, end, *nextP, nextPtr, startTagLevel,
- eventPP, doneP, errorCodeP, errorP);
- break;
- case XML_TOK_CHAR_REF: {
- int const n = XmlCharRefNumber(enc, s);
- if (n < 0)
- *errorCodeP = XML_ERROR_BAD_CHAR_REF;
- else {
- if (characterDataHandler) {
- XML_Char buf[XML_ENCODE_MAX];
- characterDataHandler(handlerArg, buf,
- XmlEncode(n, (ICHAR *)buf));
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, *nextP);
- }
- } break;
- case XML_TOK_XML_DECL:
- *errorCodeP = XML_ERROR_MISPLACED_XML_PI;
- break;
- case XML_TOK_DATA_NEWLINE:
- if (characterDataHandler) {
- XML_Char c = 0xA;
- characterDataHandler(handlerArg, &c, 1);
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, *nextP);
- break;
- case XML_TOK_CDATA_SECT_OPEN: {
- enum XML_Error result;
- if (startCdataSectionHandler)
- startCdataSectionHandler(handlerArg);
- else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, *nextP);
- result = doCdataSection(xmlParserP, enc, nextP, end, nextPtr);
- if (!*nextP) {
- processor = cdataSectionProcessor;
- *errorCodeP = result;
- }
- } break;
- case XML_TOK_TRAILING_RSQB:
- if (nextPtr) {
- *nextPtr = s;
- *errorCodeP = XML_ERROR_NONE;
- } else {
- if (characterDataHandler) {
- if (MUST_CONVERT(enc, s)) {
- const char * from;
- ICHAR * dataPtr;
- from = s;
- dataPtr = (ICHAR *)dataBuf;
- XmlConvert(enc, &from, end, &dataPtr, (ICHAR *)dataBufEnd);
- {
- size_t const len = dataPtr - (ICHAR *)dataBuf;
- assert((size_t)(int)len == len); /* parser reqt */
- characterDataHandler(handlerArg, dataBuf, (int)len);
- }
- } else {
- size_t const len = (XML_Char *)end - (XML_Char *)s;
- assert((size_t)(int)len == len); /* parser reqt */
- characterDataHandler(handlerArg, (XML_Char *)s, (int)len);
- }
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, end);
- if (startTagLevel == 0) {
- *eventPP = end;
- *errorCodeP = XML_ERROR_NO_ELEMENTS;
- } else if (tagLevel != startTagLevel) {
- *eventPP = end;
- *errorCodeP = XML_ERROR_ASYNC_ENTITY;
- } else
- *doneP = true;
- }
- break;
- case XML_TOK_DATA_CHARS:
- if (characterDataHandler) {
- if (MUST_CONVERT(enc, s)) {
- for (;;) {
- const char * from;
- ICHAR * dataPtr;
- dataPtr = (ICHAR *)dataBuf;
- from = s;
- XmlConvert(enc, &from, *nextP, &dataPtr,
- (ICHAR *)dataBufEnd);
- *eventEndPP = from;
- {
- size_t const len = dataPtr - (ICHAR *)dataBuf;
- assert((size_t)(int)len == len); /* parser reqt */
- characterDataHandler(handlerArg, dataBuf, (int)len);
- }
- if (from == *nextP)
- break;
- *eventPP = from;
- }
- } else {
- size_t const len = (XML_Char *)*nextP - (XML_Char *)s;
- assert((size_t)(int)len == len); /* parser reqt */
- characterDataHandler(handlerArg, (XML_Char *)s, len);
- }
- } else if (defaultHandler)
- reportDefault(xmlParserP, enc, s, *nextP);
- break;
- case XML_TOK_PI:
- if (!reportProcessingInstruction(xmlParserP, enc, s, *nextP))
- *errorCodeP = XML_ERROR_NO_MEMORY;
- break;
- case XML_TOK_COMMENT:
- if (!reportComment(xmlParserP, enc, s, *nextP))
- *errorCodeP = XML_ERROR_NO_MEMORY;
- break;
- default:
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, *nextP);
- break;
- }
- }
- static void
- doContent(XML_Parser const xmlParserP,
- int const startTagLevel,
- const ENCODING * const enc,
- const char * const startArg,
- const char * const end,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- const char **eventPP;
- const char **eventEndPP;
- const char * s;
- bool done;
- if (enc == parser->m_encoding) {
- eventPP = &eventPtr;
- eventEndPP = &eventEndPtr;
- } else {
- eventPP = &(openInternalEntities->internalEventPtr);
- eventEndPP = &(openInternalEntities->internalEventEndPtr);
- }
- s = startArg;
- *eventPP = s;
- done = false;
- *errorCodeP = XML_ERROR_NONE;
- *errorP = NULL;
- while (*errorCodeP == XML_ERROR_NONE && !done) {
- int tok;
- const char * next;
- const char * error;
- next = s; /* XmlContentTok doesn't always set the last arg */
- /* XmlContentTok() is normally normal_contentTok(), aka
- PREFIX(contentTok)() in xmltok/xmltok_impl.c
- */
- tok = XmlContentTok(enc, s, end, &next);
- *eventEndPP = next;
- processContentToken(xmlParserP, tok, enc, s, end, &next, nextPtr,
- startTagLevel, eventPP, eventEndPP, &done,
- errorCodeP, &error);
- if (*errorCodeP != XML_ERROR_NONE) {
- const char * const xmlSample = extractXmlSample(s, end, 40);
- if (error) {
- xmlrpc_asprintf(errorP, "Problem with token at '%s...': %s",
- xmlSample, error);
- xmlrpc_strfree(error);
- } else
- xmlrpc_asprintf(errorP, "Problem with token at '%s...': %s",
- xmlSample,
- xmlrpc_XML_ErrorString(*errorCodeP));
- xmlrpc_strfree(xmlSample);
- }
- *eventPP = s = next;
- }
- }
- static Processor externalEntityContentProcessor;
- static void
- externalEntityContentProcessor(XML_Parser const xmlParserP,
- const char * const start,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- *errorP = NULL;
- doContent(xmlParserP, 1, parser->m_encoding, start, end, endPtr,
- errorCodeP, errorP);
- }
- static Processor externalEntityInitProcessor3;
- static void
- externalEntityInitProcessor3(XML_Parser const xmlParserP,
- const char * const startArg,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- const char * start;
- const char *next;
- int tok;
-
- tok = XmlContentTok(parser->m_encoding, startArg, end, &next);
- *errorP = NULL;
- start = startArg;
- switch (tok) {
- case XML_TOK_XML_DECL:
- {
- enum XML_Error result = processXmlDecl(xmlParserP, 1, start, next);
- if (result != XML_ERROR_NONE) {
- *errorCodeP = result;
- return;
- }
- start = next;
- }
- break;
- case XML_TOK_PARTIAL:
- if (endPtr) {
- *endPtr = start;
- *errorCodeP = XML_ERROR_NONE;
- return;
- }
- eventPtr = start;
- *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
- return;
- case XML_TOK_PARTIAL_CHAR:
- if (endPtr) {
- *endPtr = start;
- *errorCodeP = XML_ERROR_NONE;
- return;
- }
- eventPtr = start;
- *errorCodeP = XML_ERROR_PARTIAL_CHAR;
- return;
- }
- processor = externalEntityContentProcessor;
- tagLevel = 1;
- doContent(xmlParserP, 1, parser->m_encoding, start, end, endPtr,
- errorCodeP, errorP);
- }
- static Processor externalEntityInitProcessor2;
- static void
- externalEntityInitProcessor2(XML_Parser const xmlParserP,
- const char * const startArg,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *)xmlParserP;
- const char * start;
- const char * next;
- int tok;
-
- tok = XmlContentTok(parser->m_encoding, startArg, end, &next);
- start = startArg;
- switch (tok) {
- case XML_TOK_BOM:
- start = next;
- break;
- case XML_TOK_PARTIAL:
- if (endPtr) {
- *endPtr = start;
- *errorCodeP = XML_ERROR_NONE;
- *errorP = NULL;
- } else {
- eventPtr = start;
- *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
- *errorP = NULL;
- }
- return;
- case XML_TOK_PARTIAL_CHAR:
- if (endPtr) {
- *endPtr = start;
- *errorCodeP = XML_ERROR_NONE;
- *errorP = NULL;
- } else {
- eventPtr = start;
- *errorCodeP = XML_ERROR_PARTIAL_CHAR;
- *errorP = NULL;
- }
- return;
- }
- processor = externalEntityInitProcessor3;
- externalEntityInitProcessor3(xmlParserP, start, end, endPtr,
- errorCodeP, errorP);
- }
- static Processor externalEntityInitProcessor;
- static void
- externalEntityInitProcessor(XML_Parser const parser,
- const char * const start,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- enum XML_Error result;
- result = initializeEncoding(parser);
- if (result != XML_ERROR_NONE) {
- *errorCodeP = result;
- *errorP = NULL;
- } else {
- processor = externalEntityInitProcessor2;
- externalEntityInitProcessor2(parser, start, end, endPtr,
- errorCodeP, errorP);
- }
- }
- static enum XML_Error
- doIgnoreSection(XML_Parser const xmlParserP,
- const ENCODING * const enc,
- const char ** const startPtr,
- const char * const end,
- const char ** const nextPtr) {
- /*----------------------------------------------------------------------------
- We set *startPtr to non-null is the section is closed, and to null if
- the section is not yet closed.
- -----------------------------------------------------------------------------*/
- Parser * const parser = (Parser *) xmlParserP;
- const char * const s = *startPtr;
- enum XML_Error retval;
- const char * next;
- int tok;
- const char ** eventPP;
- const char ** eventEndPP;
- if (enc == parser->m_encoding) {
- eventPP = &eventPtr;
- eventEndPP = &eventEndPtr;
- } else {
- eventPP = &(openInternalEntities->internalEventPtr);
- eventEndPP = &(openInternalEntities->internalEventEndPtr);
- }
- *eventPP = s;
- *startPtr = '\0';
- tok = XmlIgnoreSectionTok(enc, s, end, &next);
- *eventEndPP = next;
- switch (tok) {
- case XML_TOK_IGNORE_SECT:
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- *startPtr = next;
- retval = XML_ERROR_NONE;
- break;
- case XML_TOK_INVALID:
- *eventPP = next;
- retval = XML_ERROR_INVALID_TOKEN;
- break;
- case XML_TOK_PARTIAL_CHAR:
- if (nextPtr) {
- *nextPtr = s;
- retval = XML_ERROR_NONE;
- } else
- retval = XML_ERROR_PARTIAL_CHAR;
- break;
- case XML_TOK_PARTIAL:
- case XML_TOK_NONE:
- if (nextPtr) {
- *nextPtr = s;
- retval = XML_ERROR_NONE;
- } else
- retval = XML_ERROR_SYNTAX; /* XML_ERROR_UNCLOSED_IGNORE_SECTION */
- break;
- default:
- assert(false); /* All possibilities are handled above */
- retval = 99; /* quiet compiler warning */
- }
- return retval;
- }
- static Processor prologProcessor;
- /* The idea here is to avoid using stack for each IGNORE section when
- the whole file is parsed with one call. */
- static Processor ignoreSectionProcessor;
- static void
- ignoreSectionProcessor(XML_Parser const xmlParserP,
- const char * const startArg,
- const char * const end,
- const char ** const endPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
- enum XML_Error result;
- const char * start;
- start = startArg; /* initial value */
- result = doIgnoreSection(parser, parser->m_encoding, &start, end, endPtr);
- if (start) {
- processor = prologProcessor;
- prologProcessor(xmlParserP, start, end, endPtr, errorCodeP, errorP);
- } else {
- *errorCodeP = result;
- *errorP = NULL;
- }
- }
- /* Forward declaration for recursive reference: */
- static void
- processInternalParamEntity(XML_Parser const parser,
- ENTITY * const entity,
- enum XML_Error * const errorCodeP,
- const char ** const errorP);
- static void
- doProlog(XML_Parser const xmlParserP,
- const ENCODING * const encArg,
- const char * const startArg,
- const char * const end,
- int const tokArg,
- const char * const nextArg,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
-
- Parser * const parser = (Parser *) xmlParserP;
- int tok;
- const char * next;
- const ENCODING * enc;
- const char * s;
- static const XML_Char externalSubsetName[] = { '#' , '\0' };
- const char **eventPP;
- const char **eventEndPP;
- *errorP = NULL;
- tok = tokArg;
- next = nextArg;
- enc = encArg;
- s = startArg;
- if (enc == parser->m_encoding) {
- eventPP = &eventPtr;
- eventEndPP = &eventEndPtr;
- }
- else {
- eventPP = &(openInternalEntities->internalEventPtr);
- eventEndPP = &(openInternalEntities->internalEventEndPtr);
- }
- for (;;) {
- int role;
- *eventPP = s;
- *eventEndPP = next;
- if (tok <= 0) {
- if (nextPtr != 0 && tok != XML_TOK_INVALID) {
- *nextPtr = s;
- *errorCodeP = XML_ERROR_NONE;
- return;
- }
- switch (tok) {
- case XML_TOK_INVALID:
- *eventPP = next;
- *errorCodeP = XML_ERROR_INVALID_TOKEN;
- return;
- case XML_TOK_PARTIAL:
- *errorCodeP = XML_ERROR_UNCLOSED_TOKEN;
- return;
- case XML_TOK_PARTIAL_CHAR:
- *errorCodeP = XML_ERROR_PARTIAL_CHAR;
- return;
- case XML_TOK_NONE:
- if (enc != parser->m_encoding)
- *errorCodeP = XML_ERROR_NONE;
- else {
- if (parentParser) {
- if (XmlTokenRole(&prologState, XML_TOK_NONE, end, end, enc)
- == XML_ROLE_ERROR) {
- *errorCodeP = XML_ERROR_SYNTAX;
- } else {
- *errorCodeP = XML_ERROR_NONE;
- hadExternalDoctype = 0;
- }
- } else
- *errorCodeP = XML_ERROR_NO_ELEMENTS;
- }
- return;
- default:
- tok = -tok;
- next = end;
- break;
- }
- }
- role = XmlTokenRole(&prologState, tok, s, next, enc);
- switch (role) {
- case XML_ROLE_XML_DECL: {
- enum XML_Error result = processXmlDecl(xmlParserP, 0, s, next);
- if (result != XML_ERROR_NONE) {
- *errorCodeP = result;
- return;
- }
- enc = parser->m_encoding;
- }
- break;
- case XML_ROLE_DOCTYPE_NAME:
- if (startDoctypeDeclHandler) {
- const XML_Char *name = poolStoreString(&tempPool, enc, s, next);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- startDoctypeDeclHandler(handlerArg, name);
- poolClear(&tempPool);
- }
- break;
- case XML_ROLE_TEXT_DECL: {
- enum XML_Error result = processXmlDecl(xmlParserP, 1, s, next);
- if (result != XML_ERROR_NONE) {
- *errorCodeP = result;
- return;
- }
- enc = parser->m_encoding;
- }
- break;
- case XML_ROLE_DOCTYPE_PUBLIC_ID:
- declEntity = (ENTITY *)lookup(&dtd.paramEntities,
- externalSubsetName,
- sizeof(ENTITY));
- if (!declEntity) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- /* fall through */
- case XML_ROLE_ENTITY_PUBLIC_ID:
- if (!XmlIsPublicId(enc, s, next, eventPP)) {
- *errorCodeP = XML_ERROR_SYNTAX;
- return;
- }
- if (declEntity) {
- XML_Char *tem = poolStoreString(&dtd.pool,
- enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!tem) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- normalizePublicId(tem);
- declEntity->publicId = tem;
- poolFinish(&dtd.pool);
- }
- break;
- case XML_ROLE_DOCTYPE_CLOSE:
- if (dtd.complete && hadExternalDoctype) {
- dtd.complete = 0;
- if (paramEntityParsing && externalEntityRefHandler) {
- ENTITY *entity = (ENTITY *)lookup(&dtd.paramEntities,
- externalSubsetName,
- 0);
- if (!externalEntityRefHandler(externalEntityRefHandlerArg,
- 0,
- entity->base,
- entity->systemId,
- entity->publicId)) {
- *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
- return;
- }
- }
- if (!dtd.complete
- && !dtd.standalone
- && notStandaloneHandler
- && !notStandaloneHandler(handlerArg)) {
- *errorCodeP = XML_ERROR_NOT_STANDALONE;
- return;
- }
- }
- if (endDoctypeDeclHandler)
- endDoctypeDeclHandler(handlerArg);
- break;
- case XML_ROLE_INSTANCE_START: {
- processor = contentProcessor;
- contentProcessor(xmlParserP, s, end, nextPtr, errorCodeP, errorP);
- return;
- }
- case XML_ROLE_ATTLIST_ELEMENT_NAME:
- {
- const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- declElementType = (ELEMENT_TYPE *)
- lookup(&dtd.elementTypes, name, sizeof(ELEMENT_TYPE));
- if (!declElementType) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- if (declElementType->name != name)
- poolDiscard(&dtd.pool);
- else {
- poolFinish(&dtd.pool);
- if (!setElementTypePrefix(xmlParserP, declElementType)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- }
- break;
- }
- case XML_ROLE_ATTRIBUTE_NAME:
- declAttributeId = getAttributeId(xmlParserP, enc, s, next);
- if (!declAttributeId) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- declAttributeIsCdata = 0;
- declAttributeIsId = 0;
- break;
- case XML_ROLE_ATTRIBUTE_TYPE_CDATA:
- declAttributeIsCdata = 1;
- break;
- case XML_ROLE_ATTRIBUTE_TYPE_ID:
- declAttributeIsId = 1;
- break;
- case XML_ROLE_IMPLIED_ATTRIBUTE_VALUE:
- case XML_ROLE_REQUIRED_ATTRIBUTE_VALUE:
- if (dtd.complete
- && !defineAttribute(declElementType, declAttributeId,
- declAttributeIsCdata,
- declAttributeIsId, 0)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- case XML_ROLE_DEFAULT_ATTRIBUTE_VALUE:
- case XML_ROLE_FIXED_ATTRIBUTE_VALUE:
- {
- const XML_Char *attVal;
- enum XML_Error result
- = storeAttributeValue(xmlParserP, enc, declAttributeIsCdata,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar,
- &dtd.pool);
- if (result) {
- *errorCodeP = result;
- return;
- }
- attVal = poolStart(&dtd.pool);
- poolFinish(&dtd.pool);
- if (dtd.complete
- /* ID attributes aren't allowed to have a default */
- && !defineAttribute(declElementType, declAttributeId,
- declAttributeIsCdata, 0, attVal)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- }
- case XML_ROLE_ENTITY_VALUE:
- {
- enum XML_Error result = storeEntityValue(xmlParserP, enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (declEntity) {
- declEntity->textPtr = poolStart(&dtd.pool);
- declEntity->textLen = poolLength(&dtd.pool);
- poolFinish(&dtd.pool);
- if (internalParsedEntityDeclHandler
- /* Check it's not a parameter entity */
- && ((ENTITY *)lookup(&dtd.generalEntities, declEntity->name, 0)
- == declEntity)) {
- *eventEndPP = s;
- internalParsedEntityDeclHandler(handlerArg,
- declEntity->name,
- declEntity->textPtr,
- declEntity->textLen);
- }
- }
- else
- poolDiscard(&dtd.pool);
- if (result != XML_ERROR_NONE) {
- *errorCodeP = result;
- return;
- }
- }
- break;
- case XML_ROLE_DOCTYPE_SYSTEM_ID:
- if (!dtd.standalone
- && !paramEntityParsing
- && notStandaloneHandler
- && !notStandaloneHandler(handlerArg)) {
- *errorCodeP = XML_ERROR_NOT_STANDALONE;
- return;
- }
- hadExternalDoctype = 1;
- if (!declEntity) {
- declEntity = (ENTITY *)lookup(&dtd.paramEntities,
- externalSubsetName,
- sizeof(ENTITY));
- if (!declEntity) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- }
- /* fall through */
- case XML_ROLE_ENTITY_SYSTEM_ID:
- if (declEntity) {
- declEntity->systemId = poolStoreString(&dtd.pool, enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!declEntity->systemId) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- declEntity->base = curBase;
- poolFinish(&dtd.pool);
- }
- break;
- case XML_ROLE_ENTITY_NOTATION_NAME:
- if (declEntity) {
- declEntity->notation = poolStoreString(&dtd.pool, enc, s, next);
- if (!declEntity->notation) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- poolFinish(&dtd.pool);
- if (unparsedEntityDeclHandler) {
- *eventEndPP = s;
- unparsedEntityDeclHandler(handlerArg,
- declEntity->name,
- declEntity->base,
- declEntity->systemId,
- declEntity->publicId,
- declEntity->notation);
- }
- }
- break;
- case XML_ROLE_EXTERNAL_GENERAL_ENTITY_NO_NOTATION:
- if (declEntity && externalParsedEntityDeclHandler) {
- *eventEndPP = s;
- externalParsedEntityDeclHandler(handlerArg,
- declEntity->name,
- declEntity->base,
- declEntity->systemId,
- declEntity->publicId);
- }
- break;
- case XML_ROLE_GENERAL_ENTITY_NAME:
- {
- const XML_Char *name;
- if (XmlPredefinedEntityName(enc, s, next)) {
- declEntity = 0;
- break;
- }
- name = poolStoreString(&dtd.pool, enc, s, next);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- if (dtd.complete) {
- declEntity = (ENTITY *)
- lookup(&dtd.generalEntities, name, sizeof(ENTITY));
- if (!declEntity) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- if (declEntity->name != name) {
- poolDiscard(&dtd.pool);
- declEntity = 0;
- }
- else
- poolFinish(&dtd.pool);
- }
- else {
- poolDiscard(&dtd.pool);
- declEntity = 0;
- }
- }
- break;
- case XML_ROLE_PARAM_ENTITY_NAME:
- if (dtd.complete) {
- const XML_Char *name = poolStoreString(&dtd.pool, enc, s, next);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- declEntity = (ENTITY *)
- lookup(&dtd.paramEntities, name, sizeof(ENTITY));
- if (!declEntity) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- if (declEntity->name != name) {
- poolDiscard(&dtd.pool);
- declEntity = 0;
- }
- else
- poolFinish(&dtd.pool);
- }
- break;
- case XML_ROLE_NOTATION_NAME:
- declNotationPublicId = 0;
- declNotationName = 0;
- if (notationDeclHandler) {
- declNotationName = poolStoreString(&tempPool, enc, s, next);
- if (!declNotationName) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- poolFinish(&tempPool);
- }
- break;
- case XML_ROLE_NOTATION_PUBLIC_ID:
- if (!XmlIsPublicId(enc, s, next, eventPP)) {
- *errorCodeP = XML_ERROR_SYNTAX;
- return;
- }
- if (declNotationName) {
- XML_Char *tem = poolStoreString(&tempPool,
- enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!tem) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- normalizePublicId(tem);
- declNotationPublicId = tem;
- poolFinish(&tempPool);
- }
- break;
- case XML_ROLE_NOTATION_SYSTEM_ID:
- if (declNotationName && notationDeclHandler) {
- const XML_Char *systemId
- = poolStoreString(&tempPool, enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!systemId) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- *eventEndPP = s;
- notationDeclHandler(handlerArg,
- declNotationName,
- curBase,
- systemId,
- declNotationPublicId);
- }
- poolClear(&tempPool);
- break;
- case XML_ROLE_NOTATION_NO_SYSTEM_ID:
- if (declNotationPublicId && notationDeclHandler) {
- *eventEndPP = s;
- notationDeclHandler(handlerArg,
- declNotationName,
- curBase,
- 0,
- declNotationPublicId);
- }
- poolClear(&tempPool);
- break;
- case XML_ROLE_ERROR:
- switch (tok) {
- case XML_TOK_PARAM_ENTITY_REF:
- *errorCodeP = XML_ERROR_PARAM_ENTITY_REF;
- case XML_TOK_XML_DECL:
- *errorCodeP = XML_ERROR_MISPLACED_XML_PI;
- default:
- *errorCodeP = XML_ERROR_SYNTAX;
- }
- return;
- case XML_ROLE_IGNORE_SECT:
- {
- enum XML_Error result;
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- result = doIgnoreSection(xmlParserP, enc, &next, end, nextPtr);
- if (!next) {
- processor = ignoreSectionProcessor;
- *errorCodeP = result;
- return;
- }
- }
- break;
- case XML_ROLE_GROUP_OPEN:
- if (prologState.level >= groupSize) {
- if (groupSize)
- groupConnector = realloc(groupConnector, groupSize *= 2);
- else
- groupConnector = malloc(groupSize = 32);
- if (!groupConnector) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- }
- groupConnector[prologState.level] = 0;
- break;
- case XML_ROLE_GROUP_SEQUENCE:
- if (groupConnector[prologState.level] == '|') {
- *errorCodeP = XML_ERROR_SYNTAX;
- return;
- }
- groupConnector[prologState.level] = ',';
- break;
- case XML_ROLE_GROUP_CHOICE:
- if (groupConnector[prologState.level] == ',') {
- *errorCodeP = XML_ERROR_SYNTAX;
- return;
- }
- groupConnector[prologState.level] = '|';
- break;
- case XML_ROLE_PARAM_ENTITY_REF:
- case XML_ROLE_INNER_PARAM_ENTITY_REF:
- if (paramEntityParsing
- && (dtd.complete || role == XML_ROLE_INNER_PARAM_ENTITY_REF)) {
- const XML_Char *name;
- ENTITY *entity;
- name = poolStoreString(&dtd.pool, enc,
- s + enc->minBytesPerChar,
- next - enc->minBytesPerChar);
- if (!name) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- entity = (ENTITY *)lookup(&dtd.paramEntities, name, 0);
- poolDiscard(&dtd.pool);
- if (!entity) {
- /* FIXME what to do if !dtd.complete? */
- *errorCodeP = XML_ERROR_UNDEFINED_ENTITY;
- return;
- }
- if (entity->open) {
- *errorCodeP = XML_ERROR_RECURSIVE_ENTITY_REF;
- return;
- }
- if (entity->textPtr) {
- processInternalParamEntity(xmlParserP, entity, errorCodeP, errorP);
- if (*errorCodeP != XML_ERROR_NONE)
- return;
- break;
- }
- if (role == XML_ROLE_INNER_PARAM_ENTITY_REF) {
- *errorCodeP = XML_ERROR_PARAM_ENTITY_REF;
- return;
- }
- if (externalEntityRefHandler) {
- dtd.complete = 0;
- entity->open = 1;
- if (!externalEntityRefHandler(externalEntityRefHandlerArg,
- 0,
- entity->base,
- entity->systemId,
- entity->publicId)) {
- entity->open = 0;
- *errorCodeP = XML_ERROR_EXTERNAL_ENTITY_HANDLING;
- return;
- }
- entity->open = 0;
- if (dtd.complete)
- break;
- }
- }
- if (!dtd.standalone
- && notStandaloneHandler
- && !notStandaloneHandler(handlerArg)) {
- *errorCodeP = XML_ERROR_NOT_STANDALONE;
- return;
- }
- dtd.complete = 0;
- if (defaultHandler)
- reportDefault(xmlParserP, enc, s, next);
- break;
- case XML_ROLE_NONE:
- switch (tok) {
- case XML_TOK_PI:
- if (!reportProcessingInstruction(xmlParserP, enc, s, next)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- case XML_TOK_COMMENT:
- if (!reportComment(xmlParserP, enc, s, next)) {
- *errorCodeP = XML_ERROR_NO_MEMORY;
- return;
- }
- break;
- }
- break;
- }
- if (defaultHandler) {
- switch (tok) {
- case XML_TOK_PI:
- case XML_TOK_COMMENT:
- case XML_TOK_BOM:
- case XML_TOK_XML_DECL:
- case XML_TOK_IGNORE_SECT:
- case XML_TOK_PARAM_ENTITY_REF:
- break;
- default:
- if (role != XML_ROLE_IGNORE_SECT)
- reportDefault(xmlParserP, enc, s, next);
- }
- }
- s = next;
- tok = XmlPrologTok(enc, s, end, &next);
- }
- /* not reached */
- }
- static Processor prologProcessor;
- static void
- prologProcessor(XML_Parser const xmlParserP,
- const char * const s,
- const char * const end,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- Parser * const parser = (Parser *) xmlParserP;
- const char * next;
- int tok;
- *errorP = NULL;
- tok = XmlPrologTok(parser->m_encoding, s, end, &next);
- doProlog(xmlParserP, parser->m_encoding, s, end, tok, next, nextPtr,
- errorCodeP, errorP);
- }
- static Processor prologInitProcessor;
- static void
- prologInitProcessor(XML_Parser const parser,
- const char * const s,
- const char * const end,
- const char ** const nextPtr,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- enum XML_Error result;
- *errorP = NULL;
-
- result = initializeEncoding(parser);
-
- if (result != XML_ERROR_NONE)
- *errorCodeP = result;
- else {
- processor = prologProcessor;
- prologProcessor(parser, s, end, nextPtr, errorCodeP, errorP);
- }
- }
- static void
- processInternalParamEntity(XML_Parser const parser,
- ENTITY * const entity,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- const char *s, *end, *next;
- int tok;
- OPEN_INTERNAL_ENTITY openEntity;
- entity->open = 1;
- openEntity.next = openInternalEntities;
- openInternalEntities = &openEntity;
- openEntity.entity = entity;
- openEntity.internalEventPtr = 0;
- openEntity.internalEventEndPtr = 0;
- s = (char *)entity->textPtr;
- end = (char *)(entity->textPtr + entity->textLen);
- tok = XmlPrologTok(internalEncoding, s, end, &next);
- doProlog(parser, internalEncoding, s, end, tok, next, 0,
- errorCodeP, errorP);
- entity->open = 0;
- openInternalEntities = openEntity.next;
- }
- XML_Parser
- xmlrpc_XML_ParserCreate(const XML_Char * const encodingName) {
- XML_Parser const xmlParserP = malloc(sizeof(Parser));
- Parser * const parser = (Parser *)xmlParserP;
- if (xmlParserP) {
- processor = prologInitProcessor;
- xmlrpc_XmlPrologStateInit(&prologState);
- userData = 0;
- handlerArg = 0;
- startElementHandler = 0;
- endElementHandler = 0;
- characterDataHandler = 0;
- processingInstructionHandler = 0;
- commentHandler = 0;
- startCdataSectionHandler = 0;
- endCdataSectionHandler = 0;
- defaultHandler = 0;
- startDoctypeDeclHandler = 0;
- endDoctypeDeclHandler = 0;
- unparsedEntityDeclHandler = 0;
- notationDeclHandler = 0;
- externalParsedEntityDeclHandler = 0;
- internalParsedEntityDeclHandler = 0;
- startNamespaceDeclHandler = 0;
- endNamespaceDeclHandler = 0;
- notStandaloneHandler = 0;
- externalEntityRefHandler = 0;
- externalEntityRefHandlerArg = parser;
- unknownEncodingHandler = 0;
- buffer = 0;
- bufferPtr = 0;
- bufferEnd = 0;
- parseEndByteIndex = 0;
- parseEndPtr = 0;
- bufferLim = 0;
- declElementType = 0;
- declAttributeId = 0;
- declEntity = 0;
- declNotationName = 0;
- declNotationPublicId = 0;
- memset(&position, 0, sizeof(POSITION));
- errorCode = XML_ERROR_NONE;
- errorString = NULL;
- eventPtr = 0;
- eventEndPtr = 0;
- positionPtr = 0;
- openInternalEntities = 0;
- tagLevel = 0;
- tagStack = 0;
- freeTagList = 0;
- freeBindingList = 0;
- inheritedBindings = 0;
- attsSize = INIT_ATTS_SIZE;
- atts = malloc(attsSize * sizeof(ATTRIBUTE));
- nSpecifiedAtts = 0;
- dataBuf = malloc(INIT_DATA_BUF_SIZE * sizeof(XML_Char));
- groupSize = 0;
- groupConnector = 0;
- hadExternalDoctype = 0;
- unknownEncodingMem = 0;
- unknownEncodingRelease = 0;
- unknownEncodingData = 0;
- unknownEncodingHandlerData = 0;
- namespaceSeparator = '!';
- parentParser = 0;
- paramEntityParsing = XML_PARAM_ENTITY_PARSING_NEVER;
- ns = 0;
- poolInit(&tempPool);
- poolInit(&temp2Pool);
- protocolEncodingName =
- encodingName ? poolCopyString(&tempPool, encodingName) : 0;
- curBase = 0;
- if (!dtdInit(&dtd) || !atts || !dataBuf
- || (encodingName && !protocolEncodingName)) {
- xmlrpc_XML_ParserFree(xmlParserP);
- return 0;
- }
- dataBufEnd = dataBuf + INIT_DATA_BUF_SIZE;
- xmlrpc_XmlInitEncoding(&parser->m_initEncoding,
- &parser->m_encoding,
- 0);
- internalEncoding = XmlGetInternalEncoding();
- }
- return xmlParserP;
- }
- XML_Parser
- xmlrpc_XML_ParserCreateNS(const XML_Char * const encodingName,
- XML_Char const nsSep) {
- static
- const XML_Char implicitContext[] = {
- XML_T('x'), XML_T('m'), XML_T('l'), XML_T('='),
- XML_T('h'), XML_T('t'), XML_T('t'), XML_T('p'), XML_T(':'),
- XML_T('/'), XML_T('/'), XML_T('w'), XML_T('w'), XML_T('w'),
- XML_T('.'), XML_T('w'), XML_T('3'),
- XML_T('.'), XML_T('o'), XML_T('r'), XML_T('g'),
- XML_T('/'), XML_T('X'), XML_T('M'), XML_T('L'),
- XML_T('/'), XML_T('1'), XML_T('9'), XML_T('9'), XML_T('8'),
- XML_T('/'), XML_T('n'), XML_T('a'), XML_T('m'), XML_T('e'),
- XML_T('s'), XML_T('p'), XML_T('a'), XML_T('c'), XML_T('e'),
- XML_T('\0')
- };
- XML_Parser const xmlParserP = xmlrpc_XML_ParserCreate(encodingName);
- Parser * const parser = (Parser *)xmlParserP;
- XML_Parser retval;
- if (xmlParserP) {
- int succeeded;
- xmlrpc_XmlInitEncodingNS(&initEncoding, &parser->m_encoding, 0);
- ns = 1;
- internalEncoding = XmlGetInternalEncodingNS();
- namespaceSeparator = nsSep;
- succeeded = setContext(xmlParserP, implicitContext);
- if (succeeded)
- retval = xmlParserP;
- else {
- xmlrpc_XML_ParserFree(xmlParserP);
- retval = NULL;
- }
- } else
- retval = NULL;
- return retval;
- }
- int
- xmlrpc_XML_SetEncoding(XML_Parser parser, const XML_Char *encodingName)
- {
- if (!encodingName)
- protocolEncodingName = 0;
- else {
- protocolEncodingName = poolCopyString(&tempPool, encodingName);
- if (!protocolEncodingName)
- return 0;
- }
- return 1;
- }
- XML_Parser
- xmlrpc_XML_ExternalEntityParserCreate(XML_Parser oldParser,
- const XML_Char *context,
- const XML_Char *encodingName)
- {
- XML_Parser parser = oldParser;
- DTD *oldDtd = &dtd;
- XML_StartElementHandler oldStartElementHandler = startElementHandler;
- XML_EndElementHandler oldEndElementHandler = endElementHandler;
- XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler;
- XML_ProcessingInstructionHandler oldProcessingInstructionHandler = processingInstructionHandler;
- XML_CommentHandler oldCommentHandler = commentHandler;
- XML_StartCdataSectionHandler oldStartCdataSectionHandler = startCdataSectionHandler;
- XML_EndCdataSectionHandler oldEndCdataSectionHandler = endCdataSectionHandler;
- XML_DefaultHandler oldDefaultHandler = defaultHandler;
- XML_UnparsedEntityDeclHandler oldUnparsedEntityDeclHandler = unparsedEntityDeclHandler;
- XML_NotationDeclHandler oldNotationDeclHandler = notationDeclHandler;
- XML_ExternalParsedEntityDeclHandler oldExternalParsedEntityDeclHandler = externalParsedEntityDeclHandler;
- XML_InternalParsedEntityDeclHandler oldInternalParsedEntityDeclHandler = internalParsedEntityDeclHandler;
- XML_StartNamespaceDeclHandler oldStartNamespaceDeclHandler = startNamespaceDeclHandler;
- XML_EndNamespaceDeclHandler oldEndNamespaceDeclHandler = endNamespaceDeclHandler;
- XML_NotStandaloneHandler oldNotStandaloneHandler = notStandaloneHandler;
- XML_ExternalEntityRefHandler oldExternalEntityRefHandler = externalEntityRefHandler;
- XML_UnknownEncodingHandler oldUnknownEncodingHandler = unknownEncodingHandler;
- void *oldUserData = userData;
- void *oldHandlerArg = handlerArg;
- int oldDefaultExpandInternalEntities = defaultExpandInternalEntities;
- void *oldExternalEntityRefHandlerArg = externalEntityRefHandlerArg;
- int oldParamEntityParsing = paramEntityParsing;
- parser = (ns
- ? xmlrpc_XML_ParserCreateNS(encodingName, namespaceSeparator)
- : xmlrpc_XML_ParserCreate(encodingName));
- if (!parser)
- return 0;
- startElementHandler = oldStartElementHandler;
- endElementHandler = oldEndElementHandler;
- characterDataHandler = oldCharacterDataHandler;
- processingInstructionHandler = oldProcessingInstructionHandler;
- commentHandler = oldCommentHandler;
- startCdataSectionHandler = oldStartCdataSectionHandler;
- endCdataSectionHandler = oldEndCdataSectionHandler;
- defaultHandler = oldDefaultHandler;
- unparsedEntityDeclHandler = oldUnparsedEntityDeclHandler;
- notationDeclHandler = oldNotationDeclHandler;
- externalParsedEntityDeclHandler = oldExternalParsedEntityDeclHandler;
- internalParsedEntityDeclHandler = oldInternalParsedEntityDeclHandler;
- startNamespaceDeclHandler = oldStartNamespaceDeclHandler;
- endNamespaceDeclHandler = oldEndNamespaceDeclHandler;
- notStandaloneHandler = oldNotStandaloneHandler;
- externalEntityRefHandler = oldExternalEntityRefHandler;
- unknownEncodingHandler = oldUnknownEncodingHandler;
- userData = oldUserData;
- if (oldUserData == oldHandlerArg)
- handlerArg = userData;
- else
- handlerArg = parser;
- if (oldExternalEntityRefHandlerArg != oldParser)
- externalEntityRefHandlerArg = oldExternalEntityRefHandlerArg;
- defaultExpandInternalEntities = oldDefaultExpandInternalEntities;
- paramEntityParsing = oldParamEntityParsing;
- if (context) {
- if (!dtdCopy(&dtd, oldDtd) || !setContext(parser, context)) {
- xmlrpc_XML_ParserFree(parser);
- return 0;
- }
- processor = externalEntityInitProcessor;
- }
- else {
- dtdSwap(&dtd, oldDtd);
- parentParser = oldParser;
- xmlrpc_XmlPrologStateInitExternalEntity(&prologState);
- dtd.complete = 1;
- hadExternalDoctype = 1;
- }
- return parser;
- }
- static
- void destroyBindings(BINDING *bindings)
- {
- for (;;) {
- BINDING *b = bindings;
- if (!b)
- break;
- bindings = b->nextTagBinding;
- free(b->uri);
- free(b);
- }
- }
- void
- xmlrpc_XML_ParserFree(XML_Parser parser)
- {
- for (;;) {
- TAG *p;
- if (tagStack == 0) {
- if (freeTagList == 0)
- break;
- tagStack = freeTagList;
- freeTagList = 0;
- }
- p = tagStack;
- tagStack = tagStack->parent;
- free(p->buf);
- destroyBindings(p->bindings);
- free(p);
- }
- destroyBindings(freeBindingList);
- destroyBindings(inheritedBindings);
- poolDestroy(&tempPool);
- poolDestroy(&temp2Pool);
- if (parentParser) {
- if (hadExternalDoctype)
- dtd.complete = 0;
- dtdSwap(&dtd, &((Parser *)parentParser)->m_dtd);
- }
- dtdDestroy(&dtd);
- free((void *)atts);
- free(groupConnector);
- free(buffer);
- free(dataBuf);
- free(unknownEncodingMem);
- if (unknownEncodingRelease)
- unknownEncodingRelease(unknownEncodingData);
- if (errorString)
- xmlrpc_strfree(errorString);
- free(parser);
- }
- void
- xmlrpc_XML_UseParserAsHandlerArg(XML_Parser parser)
- {
- handlerArg = parser;
- }
- void
- xmlrpc_XML_SetUserData(XML_Parser parser, void *p)
- {
- if (handlerArg == userData)
- handlerArg = userData = p;
- else
- userData = p;
- }
- int
- xmlrpc_XML_SetBase(XML_Parser parser, const XML_Char *p)
- {
- if (p) {
- p = poolCopyString(&dtd.pool, p);
- if (!p)
- return 0;
- curBase = p;
- }
- else
- curBase = 0;
- return 1;
- }
- const XML_Char *
- xmlrpc_XML_GetBase(XML_Parser parser)
- {
- return curBase;
- }
- int
- xmlrpc_XML_GetSpecifiedAttributeCount(XML_Parser parser)
- {
- return nSpecifiedAtts;
- }
- int
- xmlrpc_XML_GetIdAttributeIndex(XML_Parser parser)
- {
- return idAttIndex;
- }
- void
- xmlrpc_XML_SetElementHandler(XML_Parser parser,
- XML_StartElementHandler start,
- XML_EndElementHandler end)
- {
- startElementHandler = start;
- endElementHandler = end;
- }
- void
- xmlrpc_XML_SetCharacterDataHandler(XML_Parser parser,
- XML_CharacterDataHandler handler)
- {
- characterDataHandler = handler;
- }
- void
- xmlrpc_XML_SetProcessingInstructionHandler(
- XML_Parser parser,
- XML_ProcessingInstructionHandler handler)
- {
- processingInstructionHandler = handler;
- }
- void
- xmlrpc_XML_SetCommentHandler(XML_Parser parser,
- XML_CommentHandler handler)
- {
- commentHandler = handler;
- }
- void
- xmlrpc_XML_SetCdataSectionHandler(XML_Parser parser,
- XML_StartCdataSectionHandler start,
- XML_EndCdataSectionHandler end)
- {
- startCdataSectionHandler = start;
- endCdataSectionHandler = end;
- }
- void
- xmlrpc_XML_SetDefaultHandler(XML_Parser parser,
- XML_DefaultHandler handler)
- {
- defaultHandler = handler;
- defaultExpandInternalEntities = 0;
- }
- void
- xmlrpc_XML_SetDefaultHandlerExpand(XML_Parser parser,
- XML_DefaultHandler handler)
- {
- defaultHandler = handler;
- defaultExpandInternalEntities = 1;
- }
- void
- xmlrpc_XML_SetDoctypeDeclHandler(XML_Parser parser,
- XML_StartDoctypeDeclHandler start,
- XML_EndDoctypeDeclHandler end)
- {
- startDoctypeDeclHandler = start;
- endDoctypeDeclHandler = end;
- }
- void
- xmlrpc_XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
- XML_UnparsedEntityDeclHandler handler)
- {
- unparsedEntityDeclHandler = handler;
- }
- void
- xmlrpc_XML_SetExternalParsedEntityDeclHandler(
- XML_Parser parser,
- XML_ExternalParsedEntityDeclHandler handler)
- {
- externalParsedEntityDeclHandler = handler;
- }
- void
- xmlrpc_XML_SetInternalParsedEntityDeclHandler(
- XML_Parser parser,
- XML_InternalParsedEntityDeclHandler handler)
- {
- internalParsedEntityDeclHandler = handler;
- }
- void
- xmlrpc_XML_SetNotationDeclHandler(XML_Parser parser,
- XML_NotationDeclHandler handler)
- {
- notationDeclHandler = handler;
- }
- void
- xmlrpc_XML_SetNamespaceDeclHandler(XML_Parser parser,
- XML_StartNamespaceDeclHandler start,
- XML_EndNamespaceDeclHandler end)
- {
- startNamespaceDeclHandler = start;
- endNamespaceDeclHandler = end;
- }
- void
- xmlrpc_XML_SetNotStandaloneHandler(XML_Parser parser,
- XML_NotStandaloneHandler handler)
- {
- notStandaloneHandler = handler;
- }
- void
- xmlrpc_XML_SetExternalEntityRefHandler(XML_Parser parser,
- XML_ExternalEntityRefHandler handler)
- {
- externalEntityRefHandler = handler;
- }
- void
- xmlrpc_XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg)
- {
- if (arg)
- externalEntityRefHandlerArg = arg;
- else
- externalEntityRefHandlerArg = parser;
- }
- void
- xmlrpc_XML_SetUnknownEncodingHandler(XML_Parser parser,
- XML_UnknownEncodingHandler handler,
- void *data)
- {
- unknownEncodingHandler = handler;
- unknownEncodingHandlerData = data;
- }
- int
- xmlrpc_XML_SetParamEntityParsing(
- XML_Parser const parser ATTR_UNUSED,
- enum XML_ParamEntityParsing const parsing) {
-
- int retval;
- paramEntityParsing = parsing;
- retval = 1;
- return retval;
- }
- static Processor errorProcessor;
- static void
- errorProcessor(XML_Parser const parser ATTR_UNUSED,
- const char * const s ATTR_UNUSED,
- const char * const end ATTR_UNUSED,
- const char ** const nextPtr ATTR_UNUSED,
- enum XML_Error * const errorCodeP,
- const char ** const errorP) {
- *errorP = errorString;
- *errorCodeP = errorCode;
- }
- static void
- parseFinalLen0(Parser * const parser,
- int * const retvalP) {
- positionPtr = bufferPtr;
- parseEndPtr = bufferEnd;
- processor(parser, bufferPtr, bufferEnd, 0, &errorCode, &errorString);
- if (errorCode == XML_ERROR_NONE)
- *retvalP = 1;
- else {
- eventEndPtr = eventPtr;
- processor = errorProcessor;
- *retvalP = 0;
- }
- }
- static void
- parseNoBuffer(Parser * const parser,
- const char * const s,
- size_t const len,
- bool const isFinal,
- int * const succeededP) {
- parseEndByteIndex += len;
- positionPtr = s;
- if (isFinal) {
- parseEndPtr = s + len;
- processor(parser, s, parseEndPtr, 0, &errorCode, &errorString);
- if (errorCode == XML_ERROR_NONE)
- *succeededP = true;
- else {
- eventEndPtr = eventPtr;
- processor = errorProcessor;
- *succeededP = false;
- }
- } else {
- const char * end;
- parseEndPtr = s + len;
- processor(parser, s, s + len, &end, &errorCode, &errorString);
- if (errorCode != XML_ERROR_NONE) {
- eventEndPtr = eventPtr;
- processor = errorProcessor;
- *succeededP = false;
- } else {
- int const nLeftOver = s + len - end;
- XmlUpdatePosition(parser->m_encoding, positionPtr, end, &position);
- if (nLeftOver > 0) {
- if (buffer == 0 || nLeftOver > bufferLim - buffer) {
- REALLOCARRAY(buffer, len * 2);
- if (buffer)
- bufferLim = buffer + len * 2;
- }
- if (buffer) {
- memcpy(buffer, end, nLeftOver);
- bufferPtr = buffer;
- bufferEnd = buffer + nLeftOver;
- *succeededP = true;
- } else {
- errorCode = XML_ERROR_NO_MEMORY;
- eventPtr = eventEndPtr = 0;
- processor = errorProcessor;
- *succeededP = false;
- }
- } else
- *succeededP = true;
- }
- }
- }
- int
- xmlrpc_XML_Parse(XML_Parser const xmlParserP,
- const char * const s,
- size_t const len,
- int const isFinal) {
- Parser * const parser = (Parser *) xmlParserP;
- int retval;
- if (errorString) {
- xmlrpc_strfree(errorString);
- errorString = NULL;
- }
- if (len == 0) {
- if (!isFinal)
- retval = 1;
- else
- parseFinalLen0(parser, &retval);
- } else if (bufferPtr == bufferEnd)
- parseNoBuffer(parser, s, len, isFinal, &retval);
- else {
- memcpy(xmlrpc_XML_GetBuffer(parser, len), s, len);
- retval = xmlrpc_XML_ParseBuffer(parser, len, isFinal);
- }
- return retval;
- }
- int
- xmlrpc_XML_ParseBuffer(XML_Parser const xmlParserP,
- int const len,
- int const isFinal) {
- Parser * const parser = (Parser *)xmlParserP;
- const char * const start = bufferPtr;
- if (errorString) {
- xmlrpc_strfree(errorString);
- errorString = NULL;
- }
- positionPtr = start;
- bufferEnd += len;
- parseEndByteIndex += len;
- processor(xmlParserP, start, parseEndPtr = bufferEnd,
- isFinal ? (const char **)0 : &bufferPtr,
- &errorCode, &errorString);
- if (errorCode == XML_ERROR_NONE) {
- if (!isFinal)
- XmlUpdatePosition(parser->m_encoding, positionPtr, bufferPtr,
- &position);
- return 1;
- } else {
- eventEndPtr = eventPtr;
- processor = errorProcessor;
- return 0;
- }
- }
- void *
- xmlrpc_XML_GetBuffer(XML_Parser const xmlParserP,
- size_t const len) {
- Parser * const parser = (Parser *)xmlParserP;
- assert(bufferLim >= bufferEnd);
- if (len > (size_t)(bufferLim - bufferEnd)) {
- /* FIXME avoid integer overflow */
- size_t neededSize = len + (bufferEnd - bufferPtr);
- assert(bufferLim >= buffer);
- if (neededSize <= (size_t)(bufferLim - buffer)) {
- memmove(buffer, bufferPtr, bufferEnd - bufferPtr);
- bufferEnd = buffer + (bufferEnd - bufferPtr);
- bufferPtr = buffer;
- } else {
- size_t bufferSize;
- char * newBuf;
- bufferSize = bufferLim > bufferPtr ?
- bufferLim - bufferPtr : INIT_BUFFER_SIZE;
-
- do {
- bufferSize *= 2;
- } while (bufferSize < neededSize);
- newBuf = malloc(bufferSize);
- if (newBuf == 0) {
- errorCode = XML_ERROR_NO_MEMORY;
- return 0;
- }
- bufferLim = newBuf + bufferSize;
- if (bufferPtr) {
- memcpy(newBuf, bufferPtr, bufferEnd - bufferPtr);
- free(buffer);
- }
- bufferEnd = newBuf + (bufferEnd - bufferPtr);
- bufferPtr = buffer = newBuf;
- }
- }
- return bufferEnd;
- }
- enum XML_Error
- xmlrpc_XML_GetErrorCode(XML_Parser const parser) {
- return errorCode;
- }
- const char *
- xmlrpc_XML_GetErrorString(XML_Parser const parser) {
- if (errorString)
- return errorString;
- else if (errorCode == XML_ERROR_NONE)
- return NULL;
- else
- return xmlrpc_XML_ErrorString(errorCode);
- }
- long
- xmlrpc_XML_GetCurrentByteIndex(XML_Parser const parser) {
- long retval;
- if (eventPtr) {
- size_t const bytesLeft = parseEndPtr - eventPtr;
- if ((size_t)(long)(bytesLeft) != bytesLeft)
- retval = -1;
- else
- retval = parseEndByteIndex - (long)bytesLeft;
- } else
- retval = -1;
- return retval;
- }
- int
- xmlrpc_XML_GetCurrentByteCount(XML_Parser const parser) {
- int retval;
- if (eventEndPtr && eventPtr) {
- size_t const byteCount = eventEndPtr - eventPtr;
- assert((size_t)(int)byteCount == byteCount);
- retval = (int)byteCount;
- } else
- retval = 0;
- return retval;
- }
- int
- xmlrpc_XML_GetCurrentLineNumber(XML_Parser const xmlParserP) {
- Parser * const parser = (Parser *) xmlParserP;
- if (eventPtr) {
- XmlUpdatePosition(parser->m_encoding, positionPtr, eventPtr,
- &position);
- positionPtr = eventPtr;
- }
- return position.lineNumber + 1;
- }
- int
- xmlrpc_XML_GetCurrentColumnNumber(XML_Parser const xmlParserP) {
- Parser * const parser = (Parser *) xmlParserP;
- if (eventPtr) {
- XmlUpdatePosition(parser->m_encoding, positionPtr, eventPtr,
- &position);
- positionPtr = eventPtr;
- }
- return position.columnNumber;
- }
- void
- xmlrpc_XML_DefaultCurrent(XML_Parser const xmlParserP) {
- Parser * const parser = (Parser *) xmlParserP;
- if (defaultHandler) {
- if (openInternalEntities)
- reportDefault(xmlParserP,
- internalEncoding,
- openInternalEntities->internalEventPtr,
- openInternalEntities->internalEventEndPtr);
- else
- reportDefault(xmlParserP, parser->m_encoding,
- eventPtr, eventEndPtr);
- }
- }
- const XML_LChar *
- xmlrpc_XML_ErrorString(int const code) {
- static const XML_LChar * const message[] = {
- /* NONE */ NULL,
- /* NO_MEMORY */ XML_T("out of memory"),
- /* SYNTAX */ XML_T("syntax error"),
- /* NO_ELEMENTS */ XML_T("no element found"),
- /* INVALID_TOKEN */ XML_T("not well-formed"),
- /* UNCLOSED_TOKEN */ XML_T("unclosed token"),
- /* PARTIAL_CHAR */ XML_T("unclosed token"),
- /* TAG_MISMATCH */ XML_T("mismatched tag"),
- /* DUPLICATE_ATTRIBUTE */ XML_T("duplicate attribute"),
- /* JUNK_AFTER_DOC_ELEMENT */ XML_T("junk after document element"),
- /* PARAM_ENTITY_REF */
- XML_T("illegal parameter entity reference"),
- /* UNDEFINED_ENTITY */ XML_T("undefined entity"),
- /* RECURSIVE_ENTITY_REF */ XML_T("recursive entity reference"),
- /* ASYNC_ENTITY */ XML_T("asynchronous entity"),
- /* BAD_CHAR_REF */
- XML_T("reference to invalid character number"),
- /* BINARY_ENTITY_REF */ XML_T("reference to binary entity"),
- /* ATTRIBUTE_EXTERNAL_ENTITY_REF */
- XML_T("reference to external entity in attribute"),
- /* MISPLACED_XML_PI */
- XML_T("xml processing instruction not at start "
- "of external entity"),
- /* UNKNOWN_ENCODING */ XML_T("unknown encoding"),
- /* INCORRECT_ENCODING */
- XML_T("encoding specified in XML declaration is incorrect"),
- /* UNCLOSED_CDATA_SECTION */ XML_T("unclosed CDATA section"),
- /* EXTERNAL_ENTITY_HANDLING */
- XML_T("error in processing external entity reference"),
- /* NOT_STANDALONE */ XML_T("document is not standalone")
- };
- const XML_LChar * retval;
- if (code > 0 && (unsigned)code < ARRAY_SIZE(message))
- retval = message[code];
- else
- retval = NULL;
-
- return retval;
- }
|