xmlwriter.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732
  1. /*
  2. * xmlwriter.c: XML text writer implementation
  3. *
  4. * For license and disclaimer see the license and disclaimer of
  5. * libxml2.
  6. *
  7. * alfred@mickautsch.de
  8. */
  9. #define IN_LIBXML
  10. #include "libxml.h"
  11. #include <string.h>
  12. #include <libxml/xmlmemory.h>
  13. #include <libxml/parser.h>
  14. #include <libxml/uri.h>
  15. #include <libxml/HTMLtree.h>
  16. #ifdef LIBXML_WRITER_ENABLED
  17. #include <libxml/xmlwriter.h>
  18. #include "buf.h"
  19. #include "enc.h"
  20. #include "save.h"
  21. #define B64LINELEN 72
  22. #define B64CRLF "\r\n"
  23. /*
  24. * The following VA_COPY was coded following an example in
  25. * the Samba project. It may not be sufficient for some
  26. * esoteric implementations of va_list but (hopefully) will
  27. * be sufficient for libxml2.
  28. */
  29. #ifndef VA_COPY
  30. #ifndef _WIN32
  31. #ifndef va_copy
  32. #define va_copy __va_copy
  33. #endif
  34. #define VA_COPY(dest, src) va_copy(dest, src)
  35. #else
  36. #define VA_COPY(dest,src) (dest) = (src)
  37. #endif
  38. #endif
  39. /*
  40. * Types are kept private
  41. */
  42. typedef enum {
  43. XML_TEXTWRITER_NONE = 0,
  44. XML_TEXTWRITER_NAME,
  45. XML_TEXTWRITER_ATTRIBUTE,
  46. XML_TEXTWRITER_TEXT,
  47. XML_TEXTWRITER_PI,
  48. XML_TEXTWRITER_PI_TEXT,
  49. XML_TEXTWRITER_CDATA,
  50. XML_TEXTWRITER_DTD,
  51. XML_TEXTWRITER_DTD_TEXT,
  52. XML_TEXTWRITER_DTD_ELEM,
  53. XML_TEXTWRITER_DTD_ELEM_TEXT,
  54. XML_TEXTWRITER_DTD_ATTL,
  55. XML_TEXTWRITER_DTD_ATTL_TEXT,
  56. XML_TEXTWRITER_DTD_ENTY, /* entity */
  57. XML_TEXTWRITER_DTD_ENTY_TEXT,
  58. XML_TEXTWRITER_DTD_PENT, /* parameter entity */
  59. XML_TEXTWRITER_COMMENT
  60. } xmlTextWriterState;
  61. typedef struct _xmlTextWriterStackEntry xmlTextWriterStackEntry;
  62. struct _xmlTextWriterStackEntry {
  63. xmlChar *name;
  64. xmlTextWriterState state;
  65. };
  66. typedef struct _xmlTextWriterNsStackEntry xmlTextWriterNsStackEntry;
  67. struct _xmlTextWriterNsStackEntry {
  68. xmlChar *prefix;
  69. xmlChar *uri;
  70. xmlLinkPtr elem;
  71. };
  72. struct _xmlTextWriter {
  73. xmlOutputBufferPtr out; /* output buffer */
  74. xmlListPtr nodes; /* element name stack */
  75. xmlListPtr nsstack; /* name spaces stack */
  76. int level;
  77. int indent; /* enable indent */
  78. int doindent; /* internal indent flag */
  79. xmlChar *ichar; /* indent character */
  80. char qchar; /* character used for quoting attribute values */
  81. xmlParserCtxtPtr ctxt;
  82. int no_doc_free;
  83. xmlDocPtr doc;
  84. };
  85. static void xmlFreeTextWriterStackEntry(xmlLinkPtr lk);
  86. static int xmlCmpTextWriterStackEntry(const void *data0,
  87. const void *data1);
  88. static int xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer);
  89. static void xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk);
  90. static int xmlCmpTextWriterNsStackEntry(const void *data0,
  91. const void *data1);
  92. static int xmlTextWriterWriteDocCallback(void *context,
  93. const char *str, int len);
  94. static int xmlTextWriterCloseDocCallback(void *context);
  95. static xmlChar *xmlTextWriterVSprintf(const char *format, va_list argptr) LIBXML_ATTR_FORMAT(1,0);
  96. static int xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
  97. const unsigned char *data);
  98. static void xmlTextWriterStartDocumentCallback(void *ctx);
  99. static int xmlTextWriterWriteIndent(xmlTextWriterPtr writer);
  100. static int
  101. xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
  102. xmlTextWriterStackEntry * p);
  103. /**
  104. * xmlWriterErrMsg:
  105. * @ctxt: a writer context
  106. * @error: the error number
  107. * @msg: the error message
  108. *
  109. * Handle a writer error
  110. */
  111. static void
  112. xmlWriterErrMsg(xmlTextWriterPtr ctxt, xmlParserErrors error,
  113. const char *msg)
  114. {
  115. if (ctxt != NULL) {
  116. __xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
  117. NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
  118. NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
  119. } else {
  120. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
  121. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
  122. }
  123. }
  124. /**
  125. * xmlWriterErrMsgInt:
  126. * @ctxt: a writer context
  127. * @error: the error number
  128. * @msg: the error message
  129. * @val: an int
  130. *
  131. * Handle a writer error
  132. */
  133. static void LIBXML_ATTR_FORMAT(3,0)
  134. xmlWriterErrMsgInt(xmlTextWriterPtr ctxt, xmlParserErrors error,
  135. const char *msg, int val)
  136. {
  137. if (ctxt != NULL) {
  138. __xmlRaiseError(NULL, NULL, NULL, ctxt->ctxt,
  139. NULL, XML_FROM_WRITER, error, XML_ERR_FATAL,
  140. NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  141. } else {
  142. __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_WRITER, error,
  143. XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
  144. }
  145. }
  146. /**
  147. * xmlNewTextWriter:
  148. * @out: an xmlOutputBufferPtr
  149. *
  150. * Create a new xmlNewTextWriter structure using an xmlOutputBufferPtr
  151. * NOTE: the @out parameter will be deallocated when the writer is closed
  152. * (if the call succeed.)
  153. *
  154. * Returns the new xmlTextWriterPtr or NULL in case of error
  155. */
  156. xmlTextWriterPtr
  157. xmlNewTextWriter(xmlOutputBufferPtr out)
  158. {
  159. xmlTextWriterPtr ret;
  160. ret = (xmlTextWriterPtr) xmlMalloc(sizeof(xmlTextWriter));
  161. if (ret == NULL) {
  162. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  163. "xmlNewTextWriter : out of memory!\n");
  164. return NULL;
  165. }
  166. memset(ret, 0, (size_t) sizeof(xmlTextWriter));
  167. ret->nodes = xmlListCreate(xmlFreeTextWriterStackEntry,
  168. xmlCmpTextWriterStackEntry);
  169. if (ret->nodes == NULL) {
  170. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  171. "xmlNewTextWriter : out of memory!\n");
  172. xmlFree(ret);
  173. return NULL;
  174. }
  175. ret->nsstack = xmlListCreate(xmlFreeTextWriterNsStackEntry,
  176. xmlCmpTextWriterNsStackEntry);
  177. if (ret->nsstack == NULL) {
  178. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  179. "xmlNewTextWriter : out of memory!\n");
  180. xmlListDelete(ret->nodes);
  181. xmlFree(ret);
  182. return NULL;
  183. }
  184. ret->out = out;
  185. ret->ichar = xmlStrdup(BAD_CAST " ");
  186. ret->qchar = '"';
  187. if (!ret->ichar) {
  188. xmlListDelete(ret->nodes);
  189. xmlListDelete(ret->nsstack);
  190. xmlFree(ret);
  191. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  192. "xmlNewTextWriter : out of memory!\n");
  193. return NULL;
  194. }
  195. ret->doc = xmlNewDoc(NULL);
  196. ret->no_doc_free = 0;
  197. return ret;
  198. }
  199. /**
  200. * xmlNewTextWriterFilename:
  201. * @uri: the URI of the resource for the output
  202. * @compression: compress the output?
  203. *
  204. * Create a new xmlNewTextWriter structure with @uri as output
  205. *
  206. * Returns the new xmlTextWriterPtr or NULL in case of error
  207. */
  208. xmlTextWriterPtr
  209. xmlNewTextWriterFilename(const char *uri, int compression)
  210. {
  211. xmlTextWriterPtr ret;
  212. xmlOutputBufferPtr out;
  213. out = xmlOutputBufferCreateFilename(uri, NULL, compression);
  214. if (out == NULL) {
  215. xmlWriterErrMsg(NULL, XML_IO_EIO,
  216. "xmlNewTextWriterFilename : cannot open uri\n");
  217. return NULL;
  218. }
  219. ret = xmlNewTextWriter(out);
  220. if (ret == NULL) {
  221. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  222. "xmlNewTextWriterFilename : out of memory!\n");
  223. xmlOutputBufferClose(out);
  224. return NULL;
  225. }
  226. ret->indent = 0;
  227. ret->doindent = 0;
  228. return ret;
  229. }
  230. /**
  231. * xmlNewTextWriterMemory:
  232. * @buf: xmlBufferPtr
  233. * @compression: compress the output?
  234. *
  235. * Create a new xmlNewTextWriter structure with @buf as output
  236. * TODO: handle compression
  237. *
  238. * Returns the new xmlTextWriterPtr or NULL in case of error
  239. */
  240. xmlTextWriterPtr
  241. xmlNewTextWriterMemory(xmlBufferPtr buf, int compression ATTRIBUTE_UNUSED)
  242. {
  243. xmlTextWriterPtr ret;
  244. xmlOutputBufferPtr out;
  245. /*::todo handle compression */
  246. out = xmlOutputBufferCreateBuffer(buf, NULL);
  247. if (out == NULL) {
  248. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  249. "xmlNewTextWriterMemory : out of memory!\n");
  250. return NULL;
  251. }
  252. ret = xmlNewTextWriter(out);
  253. if (ret == NULL) {
  254. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  255. "xmlNewTextWriterMemory : out of memory!\n");
  256. xmlOutputBufferClose(out);
  257. return NULL;
  258. }
  259. return ret;
  260. }
  261. /**
  262. * xmlNewTextWriterPushParser:
  263. * @ctxt: xmlParserCtxtPtr to hold the new XML document tree
  264. * @compression: compress the output?
  265. *
  266. * Create a new xmlNewTextWriter structure with @ctxt as output
  267. * NOTE: the @ctxt context will be freed with the resulting writer
  268. * (if the call succeeds).
  269. * TODO: handle compression
  270. *
  271. * Returns the new xmlTextWriterPtr or NULL in case of error
  272. */
  273. xmlTextWriterPtr
  274. xmlNewTextWriterPushParser(xmlParserCtxtPtr ctxt,
  275. int compression ATTRIBUTE_UNUSED)
  276. {
  277. xmlTextWriterPtr ret;
  278. xmlOutputBufferPtr out;
  279. if (ctxt == NULL) {
  280. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  281. "xmlNewTextWriterPushParser : invalid context!\n");
  282. return NULL;
  283. }
  284. out = xmlOutputBufferCreateIO(xmlTextWriterWriteDocCallback,
  285. xmlTextWriterCloseDocCallback,
  286. (void *) ctxt, NULL);
  287. if (out == NULL) {
  288. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  289. "xmlNewTextWriterPushParser : error at xmlOutputBufferCreateIO!\n");
  290. return NULL;
  291. }
  292. ret = xmlNewTextWriter(out);
  293. if (ret == NULL) {
  294. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  295. "xmlNewTextWriterPushParser : error at xmlNewTextWriter!\n");
  296. xmlOutputBufferClose(out);
  297. return NULL;
  298. }
  299. ret->ctxt = ctxt;
  300. return ret;
  301. }
  302. /**
  303. * xmlNewTextWriterDoc:
  304. * @doc: address of a xmlDocPtr to hold the new XML document tree
  305. * @compression: compress the output?
  306. *
  307. * Create a new xmlNewTextWriter structure with @*doc as output
  308. *
  309. * Returns the new xmlTextWriterPtr or NULL in case of error
  310. */
  311. xmlTextWriterPtr
  312. xmlNewTextWriterDoc(xmlDocPtr * doc, int compression)
  313. {
  314. xmlTextWriterPtr ret;
  315. xmlSAXHandler saxHandler;
  316. xmlParserCtxtPtr ctxt;
  317. memset(&saxHandler, '\0', sizeof(saxHandler));
  318. xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
  319. saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
  320. saxHandler.startElement = xmlSAX2StartElement;
  321. saxHandler.endElement = xmlSAX2EndElement;
  322. ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
  323. if (ctxt == NULL) {
  324. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  325. "xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
  326. return NULL;
  327. }
  328. /*
  329. * For some reason this seems to completely break if node names
  330. * are interned.
  331. */
  332. ctxt->dictNames = 0;
  333. ctxt->myDoc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
  334. if (ctxt->myDoc == NULL) {
  335. xmlFreeParserCtxt(ctxt);
  336. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  337. "xmlNewTextWriterDoc : error at xmlNewDoc!\n");
  338. return NULL;
  339. }
  340. ret = xmlNewTextWriterPushParser(ctxt, compression);
  341. if (ret == NULL) {
  342. xmlFreeDoc(ctxt->myDoc);
  343. xmlFreeParserCtxt(ctxt);
  344. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  345. "xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
  346. return NULL;
  347. }
  348. xmlSetDocCompressMode(ctxt->myDoc, compression);
  349. if (doc != NULL) {
  350. *doc = ctxt->myDoc;
  351. ret->no_doc_free = 1;
  352. }
  353. return ret;
  354. }
  355. /**
  356. * xmlNewTextWriterTree:
  357. * @doc: xmlDocPtr
  358. * @node: xmlNodePtr or NULL for doc->children
  359. * @compression: compress the output?
  360. *
  361. * Create a new xmlNewTextWriter structure with @doc as output
  362. * starting at @node
  363. *
  364. * Returns the new xmlTextWriterPtr or NULL in case of error
  365. */
  366. xmlTextWriterPtr
  367. xmlNewTextWriterTree(xmlDocPtr doc, xmlNodePtr node, int compression)
  368. {
  369. xmlTextWriterPtr ret;
  370. xmlSAXHandler saxHandler;
  371. xmlParserCtxtPtr ctxt;
  372. if (doc == NULL) {
  373. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  374. "xmlNewTextWriterTree : invalid document tree!\n");
  375. return NULL;
  376. }
  377. memset(&saxHandler, '\0', sizeof(saxHandler));
  378. xmlSAX2InitDefaultSAXHandler(&saxHandler, 1);
  379. saxHandler.startDocument = xmlTextWriterStartDocumentCallback;
  380. saxHandler.startElement = xmlSAX2StartElement;
  381. saxHandler.endElement = xmlSAX2EndElement;
  382. ctxt = xmlCreatePushParserCtxt(&saxHandler, NULL, NULL, 0, NULL);
  383. if (ctxt == NULL) {
  384. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  385. "xmlNewTextWriterDoc : error at xmlCreatePushParserCtxt!\n");
  386. return NULL;
  387. }
  388. /*
  389. * For some reason this seems to completely break if node names
  390. * are interned.
  391. */
  392. ctxt->dictNames = 0;
  393. ret = xmlNewTextWriterPushParser(ctxt, compression);
  394. if (ret == NULL) {
  395. xmlFreeParserCtxt(ctxt);
  396. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  397. "xmlNewTextWriterDoc : error at xmlNewTextWriterPushParser!\n");
  398. return NULL;
  399. }
  400. ctxt->myDoc = doc;
  401. ctxt->node = node;
  402. ret->no_doc_free = 1;
  403. xmlSetDocCompressMode(doc, compression);
  404. return ret;
  405. }
  406. /**
  407. * xmlFreeTextWriter:
  408. * @writer: the xmlTextWriterPtr
  409. *
  410. * Deallocate all the resources associated to the writer
  411. */
  412. void
  413. xmlFreeTextWriter(xmlTextWriterPtr writer)
  414. {
  415. if (writer == NULL)
  416. return;
  417. if (writer->out != NULL)
  418. xmlOutputBufferClose(writer->out);
  419. if (writer->nodes != NULL)
  420. xmlListDelete(writer->nodes);
  421. if (writer->nsstack != NULL)
  422. xmlListDelete(writer->nsstack);
  423. if (writer->ctxt != NULL) {
  424. if ((writer->ctxt->myDoc != NULL) && (writer->no_doc_free == 0)) {
  425. xmlFreeDoc(writer->ctxt->myDoc);
  426. writer->ctxt->myDoc = NULL;
  427. }
  428. xmlFreeParserCtxt(writer->ctxt);
  429. }
  430. if (writer->doc != NULL)
  431. xmlFreeDoc(writer->doc);
  432. if (writer->ichar != NULL)
  433. xmlFree(writer->ichar);
  434. xmlFree(writer);
  435. }
  436. /**
  437. * xmlTextWriterStartDocument:
  438. * @writer: the xmlTextWriterPtr
  439. * @version: the xml version ("1.0") or NULL for default ("1.0")
  440. * @encoding: the encoding or NULL for default
  441. * @standalone: "yes" or "no" or NULL for default
  442. *
  443. * Start a new xml document
  444. *
  445. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  446. */
  447. int
  448. xmlTextWriterStartDocument(xmlTextWriterPtr writer, const char *version,
  449. const char *encoding, const char *standalone)
  450. {
  451. int count;
  452. int sum;
  453. xmlLinkPtr lk;
  454. xmlCharEncodingHandlerPtr encoder;
  455. if ((writer == NULL) || (writer->out == NULL)) {
  456. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  457. "xmlTextWriterStartDocument : invalid writer!\n");
  458. return -1;
  459. }
  460. lk = xmlListFront(writer->nodes);
  461. if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
  462. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  463. "xmlTextWriterStartDocument : not allowed in this context!\n");
  464. return -1;
  465. }
  466. encoder = NULL;
  467. if (encoding != NULL) {
  468. encoder = xmlFindCharEncodingHandler(encoding);
  469. if (encoder == NULL) {
  470. xmlWriterErrMsg(writer, XML_ERR_UNSUPPORTED_ENCODING,
  471. "xmlTextWriterStartDocument : unsupported encoding\n");
  472. return -1;
  473. }
  474. }
  475. writer->out->encoder = encoder;
  476. if (encoder != NULL) {
  477. if (writer->out->conv == NULL) {
  478. writer->out->conv = xmlBufCreateSize(4000);
  479. }
  480. xmlCharEncOutput(writer->out, 1);
  481. if ((writer->doc != NULL) && (writer->doc->encoding == NULL))
  482. writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->name);
  483. } else
  484. writer->out->conv = NULL;
  485. sum = 0;
  486. count = xmlOutputBufferWriteString(writer->out, "<?xml version=");
  487. if (count < 0)
  488. return -1;
  489. sum += count;
  490. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  491. if (count < 0)
  492. return -1;
  493. sum += count;
  494. if (version != 0)
  495. count = xmlOutputBufferWriteString(writer->out, version);
  496. else
  497. count = xmlOutputBufferWriteString(writer->out, "1.0");
  498. if (count < 0)
  499. return -1;
  500. sum += count;
  501. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  502. if (count < 0)
  503. return -1;
  504. sum += count;
  505. if (writer->out->encoder != 0) {
  506. count = xmlOutputBufferWriteString(writer->out, " encoding=");
  507. if (count < 0)
  508. return -1;
  509. sum += count;
  510. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  511. if (count < 0)
  512. return -1;
  513. sum += count;
  514. count =
  515. xmlOutputBufferWriteString(writer->out,
  516. writer->out->encoder->name);
  517. if (count < 0)
  518. return -1;
  519. sum += count;
  520. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  521. if (count < 0)
  522. return -1;
  523. sum += count;
  524. }
  525. if (standalone != 0) {
  526. count = xmlOutputBufferWriteString(writer->out, " standalone=");
  527. if (count < 0)
  528. return -1;
  529. sum += count;
  530. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  531. if (count < 0)
  532. return -1;
  533. sum += count;
  534. count = xmlOutputBufferWriteString(writer->out, standalone);
  535. if (count < 0)
  536. return -1;
  537. sum += count;
  538. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  539. if (count < 0)
  540. return -1;
  541. sum += count;
  542. }
  543. count = xmlOutputBufferWriteString(writer->out, "?>\n");
  544. if (count < 0)
  545. return -1;
  546. sum += count;
  547. return sum;
  548. }
  549. /**
  550. * xmlTextWriterEndDocument:
  551. * @writer: the xmlTextWriterPtr
  552. *
  553. * End an xml document. All open elements are closed, and
  554. * the content is flushed to the output.
  555. *
  556. * Returns the bytes written or -1 in case of error
  557. */
  558. int
  559. xmlTextWriterEndDocument(xmlTextWriterPtr writer)
  560. {
  561. int count;
  562. int sum;
  563. xmlLinkPtr lk;
  564. xmlTextWriterStackEntry *p;
  565. if (writer == NULL) {
  566. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  567. "xmlTextWriterEndDocument : invalid writer!\n");
  568. return -1;
  569. }
  570. sum = 0;
  571. while ((lk = xmlListFront(writer->nodes)) != NULL) {
  572. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  573. if (p == 0)
  574. break;
  575. switch (p->state) {
  576. case XML_TEXTWRITER_NAME:
  577. case XML_TEXTWRITER_ATTRIBUTE:
  578. case XML_TEXTWRITER_TEXT:
  579. count = xmlTextWriterEndElement(writer);
  580. if (count < 0)
  581. return -1;
  582. sum += count;
  583. break;
  584. case XML_TEXTWRITER_PI:
  585. case XML_TEXTWRITER_PI_TEXT:
  586. count = xmlTextWriterEndPI(writer);
  587. if (count < 0)
  588. return -1;
  589. sum += count;
  590. break;
  591. case XML_TEXTWRITER_CDATA:
  592. count = xmlTextWriterEndCDATA(writer);
  593. if (count < 0)
  594. return -1;
  595. sum += count;
  596. break;
  597. case XML_TEXTWRITER_DTD:
  598. case XML_TEXTWRITER_DTD_TEXT:
  599. case XML_TEXTWRITER_DTD_ELEM:
  600. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  601. case XML_TEXTWRITER_DTD_ATTL:
  602. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  603. case XML_TEXTWRITER_DTD_ENTY:
  604. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  605. case XML_TEXTWRITER_DTD_PENT:
  606. count = xmlTextWriterEndDTD(writer);
  607. if (count < 0)
  608. return -1;
  609. sum += count;
  610. break;
  611. case XML_TEXTWRITER_COMMENT:
  612. count = xmlTextWriterEndComment(writer);
  613. if (count < 0)
  614. return -1;
  615. sum += count;
  616. break;
  617. default:
  618. break;
  619. }
  620. }
  621. if (!writer->indent) {
  622. count = xmlOutputBufferWriteString(writer->out, "\n");
  623. if (count < 0)
  624. return -1;
  625. sum += count;
  626. }
  627. sum += xmlTextWriterFlush(writer);
  628. return sum;
  629. }
  630. /**
  631. * xmlTextWriterStartComment:
  632. * @writer: the xmlTextWriterPtr
  633. *
  634. * Start an xml comment.
  635. *
  636. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  637. */
  638. int
  639. xmlTextWriterStartComment(xmlTextWriterPtr writer)
  640. {
  641. int count;
  642. int sum;
  643. xmlLinkPtr lk;
  644. xmlTextWriterStackEntry *p;
  645. if (writer == NULL) {
  646. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  647. "xmlTextWriterStartComment : invalid writer!\n");
  648. return -1;
  649. }
  650. sum = 0;
  651. lk = xmlListFront(writer->nodes);
  652. if (lk != 0) {
  653. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  654. if (p != 0) {
  655. switch (p->state) {
  656. case XML_TEXTWRITER_TEXT:
  657. case XML_TEXTWRITER_NONE:
  658. break;
  659. case XML_TEXTWRITER_NAME:
  660. /* Output namespace declarations */
  661. count = xmlTextWriterOutputNSDecl(writer);
  662. if (count < 0)
  663. return -1;
  664. sum += count;
  665. count = xmlOutputBufferWriteString(writer->out, ">");
  666. if (count < 0)
  667. return -1;
  668. sum += count;
  669. if (writer->indent) {
  670. count =
  671. xmlOutputBufferWriteString(writer->out, "\n");
  672. if (count < 0)
  673. return -1;
  674. sum += count;
  675. }
  676. p->state = XML_TEXTWRITER_TEXT;
  677. break;
  678. default:
  679. return -1;
  680. }
  681. }
  682. }
  683. p = (xmlTextWriterStackEntry *)
  684. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  685. if (p == 0) {
  686. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  687. "xmlTextWriterStartElement : out of memory!\n");
  688. return -1;
  689. }
  690. p->name = NULL;
  691. p->state = XML_TEXTWRITER_COMMENT;
  692. xmlListPushFront(writer->nodes, p);
  693. if (writer->indent) {
  694. count = xmlTextWriterWriteIndent(writer);
  695. if (count < 0)
  696. return -1;
  697. sum += count;
  698. }
  699. count = xmlOutputBufferWriteString(writer->out, "<!--");
  700. if (count < 0)
  701. return -1;
  702. sum += count;
  703. return sum;
  704. }
  705. /**
  706. * xmlTextWriterEndComment:
  707. * @writer: the xmlTextWriterPtr
  708. *
  709. * End the current xml comment.
  710. *
  711. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  712. */
  713. int
  714. xmlTextWriterEndComment(xmlTextWriterPtr writer)
  715. {
  716. int count;
  717. int sum;
  718. xmlLinkPtr lk;
  719. xmlTextWriterStackEntry *p;
  720. if (writer == NULL) {
  721. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  722. "xmlTextWriterEndComment : invalid writer!\n");
  723. return -1;
  724. }
  725. lk = xmlListFront(writer->nodes);
  726. if (lk == 0) {
  727. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  728. "xmlTextWriterEndComment : not allowed in this context!\n");
  729. return -1;
  730. }
  731. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  732. if (p == 0)
  733. return -1;
  734. sum = 0;
  735. switch (p->state) {
  736. case XML_TEXTWRITER_COMMENT:
  737. count = xmlOutputBufferWriteString(writer->out, "-->");
  738. if (count < 0)
  739. return -1;
  740. sum += count;
  741. break;
  742. default:
  743. return -1;
  744. }
  745. if (writer->indent) {
  746. count = xmlOutputBufferWriteString(writer->out, "\n");
  747. if (count < 0)
  748. return -1;
  749. sum += count;
  750. }
  751. xmlListPopFront(writer->nodes);
  752. return sum;
  753. }
  754. /**
  755. * xmlTextWriterWriteFormatComment:
  756. * @writer: the xmlTextWriterPtr
  757. * @format: format string (see printf)
  758. * @...: extra parameters for the format
  759. *
  760. * Write an xml comment.
  761. *
  762. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  763. */
  764. int XMLCDECL
  765. xmlTextWriterWriteFormatComment(xmlTextWriterPtr writer,
  766. const char *format, ...)
  767. {
  768. int rc;
  769. va_list ap;
  770. va_start(ap, format);
  771. rc = xmlTextWriterWriteVFormatComment(writer, format, ap);
  772. va_end(ap);
  773. return rc;
  774. }
  775. /**
  776. * xmlTextWriterWriteVFormatComment:
  777. * @writer: the xmlTextWriterPtr
  778. * @format: format string (see printf)
  779. * @argptr: pointer to the first member of the variable argument list.
  780. *
  781. * Write an xml comment.
  782. *
  783. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  784. */
  785. int
  786. xmlTextWriterWriteVFormatComment(xmlTextWriterPtr writer,
  787. const char *format, va_list argptr)
  788. {
  789. int rc;
  790. xmlChar *buf;
  791. if (writer == NULL) {
  792. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  793. "xmlTextWriterWriteVFormatComment : invalid writer!\n");
  794. return -1;
  795. }
  796. buf = xmlTextWriterVSprintf(format, argptr);
  797. if (buf == NULL)
  798. return -1;
  799. rc = xmlTextWriterWriteComment(writer, buf);
  800. xmlFree(buf);
  801. return rc;
  802. }
  803. /**
  804. * xmlTextWriterWriteComment:
  805. * @writer: the xmlTextWriterPtr
  806. * @content: comment string
  807. *
  808. * Write an xml comment.
  809. *
  810. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  811. */
  812. int
  813. xmlTextWriterWriteComment(xmlTextWriterPtr writer, const xmlChar * content)
  814. {
  815. int count;
  816. int sum;
  817. sum = 0;
  818. count = xmlTextWriterStartComment(writer);
  819. if (count < 0)
  820. return -1;
  821. sum += count;
  822. count = xmlTextWriterWriteString(writer, content);
  823. if (count < 0)
  824. return -1;
  825. sum += count;
  826. count = xmlTextWriterEndComment(writer);
  827. if (count < 0)
  828. return -1;
  829. sum += count;
  830. return sum;
  831. }
  832. /**
  833. * xmlTextWriterStartElement:
  834. * @writer: the xmlTextWriterPtr
  835. * @name: element name
  836. *
  837. * Start an xml element.
  838. *
  839. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  840. */
  841. int
  842. xmlTextWriterStartElement(xmlTextWriterPtr writer, const xmlChar * name)
  843. {
  844. int count;
  845. int sum;
  846. xmlLinkPtr lk;
  847. xmlTextWriterStackEntry *p;
  848. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  849. return -1;
  850. sum = 0;
  851. lk = xmlListFront(writer->nodes);
  852. if (lk != 0) {
  853. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  854. if (p != 0) {
  855. switch (p->state) {
  856. case XML_TEXTWRITER_PI:
  857. case XML_TEXTWRITER_PI_TEXT:
  858. return -1;
  859. case XML_TEXTWRITER_NONE:
  860. break;
  861. case XML_TEXTWRITER_ATTRIBUTE:
  862. count = xmlTextWriterEndAttribute(writer);
  863. if (count < 0)
  864. return -1;
  865. sum += count;
  866. /* fallthrough */
  867. case XML_TEXTWRITER_NAME:
  868. /* Output namespace declarations */
  869. count = xmlTextWriterOutputNSDecl(writer);
  870. if (count < 0)
  871. return -1;
  872. sum += count;
  873. count = xmlOutputBufferWriteString(writer->out, ">");
  874. if (count < 0)
  875. return -1;
  876. sum += count;
  877. if (writer->indent)
  878. count =
  879. xmlOutputBufferWriteString(writer->out, "\n");
  880. p->state = XML_TEXTWRITER_TEXT;
  881. break;
  882. default:
  883. break;
  884. }
  885. }
  886. }
  887. p = (xmlTextWriterStackEntry *)
  888. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  889. if (p == 0) {
  890. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  891. "xmlTextWriterStartElement : out of memory!\n");
  892. return -1;
  893. }
  894. p->name = xmlStrdup(name);
  895. if (p->name == 0) {
  896. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  897. "xmlTextWriterStartElement : out of memory!\n");
  898. xmlFree(p);
  899. return -1;
  900. }
  901. p->state = XML_TEXTWRITER_NAME;
  902. xmlListPushFront(writer->nodes, p);
  903. if (writer->indent) {
  904. count = xmlTextWriterWriteIndent(writer);
  905. sum += count;
  906. }
  907. count = xmlOutputBufferWriteString(writer->out, "<");
  908. if (count < 0)
  909. return -1;
  910. sum += count;
  911. count =
  912. xmlOutputBufferWriteString(writer->out, (const char *) p->name);
  913. if (count < 0)
  914. return -1;
  915. sum += count;
  916. return sum;
  917. }
  918. /**
  919. * xmlTextWriterStartElementNS:
  920. * @writer: the xmlTextWriterPtr
  921. * @prefix: namespace prefix or NULL
  922. * @name: element local name
  923. * @namespaceURI: namespace URI or NULL
  924. *
  925. * Start an xml element with namespace support.
  926. *
  927. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  928. */
  929. int
  930. xmlTextWriterStartElementNS(xmlTextWriterPtr writer,
  931. const xmlChar * prefix, const xmlChar * name,
  932. const xmlChar * namespaceURI)
  933. {
  934. int count;
  935. int sum;
  936. xmlChar *buf;
  937. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  938. return -1;
  939. buf = NULL;
  940. if (prefix != 0) {
  941. buf = xmlStrdup(prefix);
  942. buf = xmlStrcat(buf, BAD_CAST ":");
  943. }
  944. buf = xmlStrcat(buf, name);
  945. sum = 0;
  946. count = xmlTextWriterStartElement(writer, buf);
  947. xmlFree(buf);
  948. if (count < 0)
  949. return -1;
  950. sum += count;
  951. if (namespaceURI != 0) {
  952. xmlTextWriterNsStackEntry *p = (xmlTextWriterNsStackEntry *)
  953. xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
  954. if (p == 0) {
  955. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  956. "xmlTextWriterStartElementNS : out of memory!\n");
  957. return -1;
  958. }
  959. buf = xmlStrdup(BAD_CAST "xmlns");
  960. if (prefix != 0) {
  961. buf = xmlStrcat(buf, BAD_CAST ":");
  962. buf = xmlStrcat(buf, prefix);
  963. }
  964. p->prefix = buf;
  965. p->uri = xmlStrdup(namespaceURI);
  966. if (p->uri == 0) {
  967. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  968. "xmlTextWriterStartElementNS : out of memory!\n");
  969. xmlFree(p);
  970. return -1;
  971. }
  972. p->elem = xmlListFront(writer->nodes);
  973. xmlListPushFront(writer->nsstack, p);
  974. }
  975. return sum;
  976. }
  977. /**
  978. * xmlTextWriterEndElement:
  979. * @writer: the xmlTextWriterPtr
  980. *
  981. * End the current xml element.
  982. *
  983. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  984. */
  985. int
  986. xmlTextWriterEndElement(xmlTextWriterPtr writer)
  987. {
  988. int count;
  989. int sum;
  990. xmlLinkPtr lk;
  991. xmlTextWriterStackEntry *p;
  992. if (writer == NULL)
  993. return -1;
  994. lk = xmlListFront(writer->nodes);
  995. if (lk == 0) {
  996. xmlListDelete(writer->nsstack);
  997. writer->nsstack = NULL;
  998. return -1;
  999. }
  1000. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1001. if (p == 0) {
  1002. xmlListDelete(writer->nsstack);
  1003. writer->nsstack = NULL;
  1004. return -1;
  1005. }
  1006. sum = 0;
  1007. switch (p->state) {
  1008. case XML_TEXTWRITER_ATTRIBUTE:
  1009. count = xmlTextWriterEndAttribute(writer);
  1010. if (count < 0) {
  1011. xmlListDelete(writer->nsstack);
  1012. writer->nsstack = NULL;
  1013. return -1;
  1014. }
  1015. sum += count;
  1016. /* fallthrough */
  1017. case XML_TEXTWRITER_NAME:
  1018. /* Output namespace declarations */
  1019. count = xmlTextWriterOutputNSDecl(writer);
  1020. if (count < 0)
  1021. return -1;
  1022. sum += count;
  1023. if (writer->indent) /* next element needs indent */
  1024. writer->doindent = 1;
  1025. count = xmlOutputBufferWriteString(writer->out, "/>");
  1026. if (count < 0)
  1027. return -1;
  1028. sum += count;
  1029. break;
  1030. case XML_TEXTWRITER_TEXT:
  1031. if ((writer->indent) && (writer->doindent)) {
  1032. count = xmlTextWriterWriteIndent(writer);
  1033. sum += count;
  1034. writer->doindent = 1;
  1035. } else
  1036. writer->doindent = 1;
  1037. count = xmlOutputBufferWriteString(writer->out, "</");
  1038. if (count < 0)
  1039. return -1;
  1040. sum += count;
  1041. count = xmlOutputBufferWriteString(writer->out,
  1042. (const char *) p->name);
  1043. if (count < 0)
  1044. return -1;
  1045. sum += count;
  1046. count = xmlOutputBufferWriteString(writer->out, ">");
  1047. if (count < 0)
  1048. return -1;
  1049. sum += count;
  1050. break;
  1051. default:
  1052. return -1;
  1053. }
  1054. if (writer->indent) {
  1055. count = xmlOutputBufferWriteString(writer->out, "\n");
  1056. sum += count;
  1057. }
  1058. xmlListPopFront(writer->nodes);
  1059. return sum;
  1060. }
  1061. /**
  1062. * xmlTextWriterFullEndElement:
  1063. * @writer: the xmlTextWriterPtr
  1064. *
  1065. * End the current xml element. Writes an end tag even if the element is empty
  1066. *
  1067. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1068. */
  1069. int
  1070. xmlTextWriterFullEndElement(xmlTextWriterPtr writer)
  1071. {
  1072. int count;
  1073. int sum;
  1074. xmlLinkPtr lk;
  1075. xmlTextWriterStackEntry *p;
  1076. if (writer == NULL)
  1077. return -1;
  1078. lk = xmlListFront(writer->nodes);
  1079. if (lk == 0)
  1080. return -1;
  1081. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1082. if (p == 0)
  1083. return -1;
  1084. sum = 0;
  1085. switch (p->state) {
  1086. case XML_TEXTWRITER_ATTRIBUTE:
  1087. count = xmlTextWriterEndAttribute(writer);
  1088. if (count < 0)
  1089. return -1;
  1090. sum += count;
  1091. /* fallthrough */
  1092. case XML_TEXTWRITER_NAME:
  1093. /* Output namespace declarations */
  1094. count = xmlTextWriterOutputNSDecl(writer);
  1095. if (count < 0)
  1096. return -1;
  1097. sum += count;
  1098. count = xmlOutputBufferWriteString(writer->out, ">");
  1099. if (count < 0)
  1100. return -1;
  1101. sum += count;
  1102. if (writer->indent)
  1103. writer->doindent = 0;
  1104. /* fallthrough */
  1105. case XML_TEXTWRITER_TEXT:
  1106. if ((writer->indent) && (writer->doindent)) {
  1107. count = xmlTextWriterWriteIndent(writer);
  1108. sum += count;
  1109. writer->doindent = 1;
  1110. } else
  1111. writer->doindent = 1;
  1112. count = xmlOutputBufferWriteString(writer->out, "</");
  1113. if (count < 0)
  1114. return -1;
  1115. sum += count;
  1116. count = xmlOutputBufferWriteString(writer->out,
  1117. (const char *) p->name);
  1118. if (count < 0)
  1119. return -1;
  1120. sum += count;
  1121. count = xmlOutputBufferWriteString(writer->out, ">");
  1122. if (count < 0)
  1123. return -1;
  1124. sum += count;
  1125. break;
  1126. default:
  1127. return -1;
  1128. }
  1129. if (writer->indent) {
  1130. count = xmlOutputBufferWriteString(writer->out, "\n");
  1131. sum += count;
  1132. }
  1133. xmlListPopFront(writer->nodes);
  1134. return sum;
  1135. }
  1136. /**
  1137. * xmlTextWriterWriteFormatRaw:
  1138. * @writer: the xmlTextWriterPtr
  1139. * @format: format string (see printf)
  1140. * @...: extra parameters for the format
  1141. *
  1142. * Write a formatted raw xml text.
  1143. *
  1144. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1145. */
  1146. int XMLCDECL
  1147. xmlTextWriterWriteFormatRaw(xmlTextWriterPtr writer, const char *format,
  1148. ...)
  1149. {
  1150. int rc;
  1151. va_list ap;
  1152. va_start(ap, format);
  1153. rc = xmlTextWriterWriteVFormatRaw(writer, format, ap);
  1154. va_end(ap);
  1155. return rc;
  1156. }
  1157. /**
  1158. * xmlTextWriterWriteVFormatRaw:
  1159. * @writer: the xmlTextWriterPtr
  1160. * @format: format string (see printf)
  1161. * @argptr: pointer to the first member of the variable argument list.
  1162. *
  1163. * Write a formatted raw xml text.
  1164. *
  1165. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1166. */
  1167. int
  1168. xmlTextWriterWriteVFormatRaw(xmlTextWriterPtr writer, const char *format,
  1169. va_list argptr)
  1170. {
  1171. int rc;
  1172. xmlChar *buf;
  1173. if (writer == NULL)
  1174. return -1;
  1175. buf = xmlTextWriterVSprintf(format, argptr);
  1176. if (buf == NULL)
  1177. return -1;
  1178. rc = xmlTextWriterWriteRaw(writer, buf);
  1179. xmlFree(buf);
  1180. return rc;
  1181. }
  1182. /**
  1183. * xmlTextWriterWriteRawLen:
  1184. * @writer: the xmlTextWriterPtr
  1185. * @content: text string
  1186. * @len: length of the text string
  1187. *
  1188. * Write an xml text.
  1189. * TODO: what about entities and special chars??
  1190. *
  1191. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1192. */
  1193. int
  1194. xmlTextWriterWriteRawLen(xmlTextWriterPtr writer, const xmlChar * content,
  1195. int len)
  1196. {
  1197. int count;
  1198. int sum;
  1199. xmlLinkPtr lk;
  1200. xmlTextWriterStackEntry *p;
  1201. if (writer == NULL) {
  1202. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  1203. "xmlTextWriterWriteRawLen : invalid writer!\n");
  1204. return -1;
  1205. }
  1206. if ((content == NULL) || (len < 0)) {
  1207. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  1208. "xmlTextWriterWriteRawLen : invalid content!\n");
  1209. return -1;
  1210. }
  1211. sum = 0;
  1212. lk = xmlListFront(writer->nodes);
  1213. if (lk != 0) {
  1214. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1215. count = xmlTextWriterHandleStateDependencies(writer, p);
  1216. if (count < 0)
  1217. return -1;
  1218. sum += count;
  1219. }
  1220. if (writer->indent)
  1221. writer->doindent = 0;
  1222. if (content != NULL) {
  1223. count =
  1224. xmlOutputBufferWrite(writer->out, len, (const char *) content);
  1225. if (count < 0)
  1226. return -1;
  1227. sum += count;
  1228. }
  1229. return sum;
  1230. }
  1231. /**
  1232. * xmlTextWriterWriteRaw:
  1233. * @writer: the xmlTextWriterPtr
  1234. * @content: text string
  1235. *
  1236. * Write a raw xml text.
  1237. *
  1238. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1239. */
  1240. int
  1241. xmlTextWriterWriteRaw(xmlTextWriterPtr writer, const xmlChar * content)
  1242. {
  1243. return xmlTextWriterWriteRawLen(writer, content, xmlStrlen(content));
  1244. }
  1245. /**
  1246. * xmlTextWriterWriteFormatString:
  1247. * @writer: the xmlTextWriterPtr
  1248. * @format: format string (see printf)
  1249. * @...: extra parameters for the format
  1250. *
  1251. * Write a formatted xml text.
  1252. *
  1253. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1254. */
  1255. int XMLCDECL
  1256. xmlTextWriterWriteFormatString(xmlTextWriterPtr writer, const char *format,
  1257. ...)
  1258. {
  1259. int rc;
  1260. va_list ap;
  1261. if ((writer == NULL) || (format == NULL))
  1262. return -1;
  1263. va_start(ap, format);
  1264. rc = xmlTextWriterWriteVFormatString(writer, format, ap);
  1265. va_end(ap);
  1266. return rc;
  1267. }
  1268. /**
  1269. * xmlTextWriterWriteVFormatString:
  1270. * @writer: the xmlTextWriterPtr
  1271. * @format: format string (see printf)
  1272. * @argptr: pointer to the first member of the variable argument list.
  1273. *
  1274. * Write a formatted xml text.
  1275. *
  1276. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1277. */
  1278. int
  1279. xmlTextWriterWriteVFormatString(xmlTextWriterPtr writer,
  1280. const char *format, va_list argptr)
  1281. {
  1282. int rc;
  1283. xmlChar *buf;
  1284. if ((writer == NULL) || (format == NULL))
  1285. return -1;
  1286. buf = xmlTextWriterVSprintf(format, argptr);
  1287. if (buf == NULL)
  1288. return -1;
  1289. rc = xmlTextWriterWriteString(writer, buf);
  1290. xmlFree(buf);
  1291. return rc;
  1292. }
  1293. /**
  1294. * xmlTextWriterWriteString:
  1295. * @writer: the xmlTextWriterPtr
  1296. * @content: text string
  1297. *
  1298. * Write an xml text.
  1299. *
  1300. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1301. */
  1302. int
  1303. xmlTextWriterWriteString(xmlTextWriterPtr writer, const xmlChar * content)
  1304. {
  1305. int count;
  1306. int sum;
  1307. xmlLinkPtr lk;
  1308. xmlTextWriterStackEntry *p;
  1309. xmlChar *buf;
  1310. if ((writer == NULL) || (content == NULL))
  1311. return -1;
  1312. sum = 0;
  1313. buf = (xmlChar *) content;
  1314. lk = xmlListFront(writer->nodes);
  1315. if (lk != 0) {
  1316. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1317. if (p != 0) {
  1318. switch (p->state) {
  1319. case XML_TEXTWRITER_NAME:
  1320. case XML_TEXTWRITER_TEXT:
  1321. #if 0
  1322. buf = NULL;
  1323. xmlOutputBufferWriteEscape(writer->out, content, NULL);
  1324. #endif
  1325. buf = xmlEncodeSpecialChars(NULL, content);
  1326. break;
  1327. case XML_TEXTWRITER_ATTRIBUTE:
  1328. buf = NULL;
  1329. xmlBufAttrSerializeTxtContent(writer->out->buffer,
  1330. writer->doc, NULL, content);
  1331. break;
  1332. default:
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. if (buf != NULL) {
  1338. count = xmlTextWriterWriteRaw(writer, buf);
  1339. if (buf != content) /* buf was allocated by us, so free it */
  1340. xmlFree(buf);
  1341. if (count < 0)
  1342. return -1;
  1343. sum += count;
  1344. }
  1345. return sum;
  1346. }
  1347. /**
  1348. * xmlOutputBufferWriteBase64:
  1349. * @out: the xmlOutputBufferPtr
  1350. * @data: binary data
  1351. * @len: the number of bytes to encode
  1352. *
  1353. * Write base64 encoded data to an xmlOutputBuffer.
  1354. * Adapted from John Walker's base64.c (http://www.fourmilab.ch/).
  1355. *
  1356. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1357. */
  1358. static int
  1359. xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len,
  1360. const unsigned char *data)
  1361. {
  1362. static unsigned char dtable[64] =
  1363. {'A','B','C','D','E','F','G','H','I','J','K','L','M',
  1364. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  1365. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  1366. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  1367. '0','1','2','3','4','5','6','7','8','9','+','/'};
  1368. int i;
  1369. int linelen;
  1370. int count;
  1371. int sum;
  1372. if ((out == NULL) || (len < 0) || (data == NULL))
  1373. return(-1);
  1374. linelen = 0;
  1375. sum = 0;
  1376. i = 0;
  1377. while (1) {
  1378. unsigned char igroup[3];
  1379. unsigned char ogroup[4];
  1380. int c;
  1381. int n;
  1382. igroup[0] = igroup[1] = igroup[2] = 0;
  1383. for (n = 0; n < 3 && i < len; n++, i++) {
  1384. c = data[i];
  1385. igroup[n] = (unsigned char) c;
  1386. }
  1387. if (n > 0) {
  1388. ogroup[0] = dtable[igroup[0] >> 2];
  1389. ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
  1390. ogroup[2] =
  1391. dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
  1392. ogroup[3] = dtable[igroup[2] & 0x3F];
  1393. if (n < 3) {
  1394. ogroup[3] = '=';
  1395. if (n < 2) {
  1396. ogroup[2] = '=';
  1397. }
  1398. }
  1399. if (linelen >= B64LINELEN) {
  1400. count = xmlOutputBufferWrite(out, 2, B64CRLF);
  1401. if (count == -1)
  1402. return -1;
  1403. sum += count;
  1404. linelen = 0;
  1405. }
  1406. count = xmlOutputBufferWrite(out, 4, (const char *) ogroup);
  1407. if (count == -1)
  1408. return -1;
  1409. sum += count;
  1410. linelen += 4;
  1411. }
  1412. if (i >= len)
  1413. break;
  1414. }
  1415. return sum;
  1416. }
  1417. /**
  1418. * xmlTextWriterWriteBase64:
  1419. * @writer: the xmlTextWriterPtr
  1420. * @data: binary data
  1421. * @start: the position within the data of the first byte to encode
  1422. * @len: the number of bytes to encode
  1423. *
  1424. * Write an base64 encoded xml text.
  1425. *
  1426. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1427. */
  1428. int
  1429. xmlTextWriterWriteBase64(xmlTextWriterPtr writer, const char *data,
  1430. int start, int len)
  1431. {
  1432. int count;
  1433. int sum;
  1434. xmlLinkPtr lk;
  1435. xmlTextWriterStackEntry *p;
  1436. if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
  1437. return -1;
  1438. sum = 0;
  1439. lk = xmlListFront(writer->nodes);
  1440. if (lk != 0) {
  1441. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1442. if (p != 0) {
  1443. count = xmlTextWriterHandleStateDependencies(writer, p);
  1444. if (count < 0)
  1445. return -1;
  1446. sum += count;
  1447. }
  1448. }
  1449. if (writer->indent)
  1450. writer->doindent = 0;
  1451. count =
  1452. xmlOutputBufferWriteBase64(writer->out, len,
  1453. (unsigned char *) data + start);
  1454. if (count < 0)
  1455. return -1;
  1456. sum += count;
  1457. return sum;
  1458. }
  1459. /**
  1460. * xmlOutputBufferWriteBinHex:
  1461. * @out: the xmlOutputBufferPtr
  1462. * @data: binary data
  1463. * @len: the number of bytes to encode
  1464. *
  1465. * Write hqx encoded data to an xmlOutputBuffer.
  1466. * ::todo
  1467. *
  1468. * Returns the bytes written (may be 0 because of buffering)
  1469. * or -1 in case of error
  1470. */
  1471. static int
  1472. xmlOutputBufferWriteBinHex(xmlOutputBufferPtr out,
  1473. int len, const unsigned char *data)
  1474. {
  1475. int count;
  1476. int sum;
  1477. static char hex[16] =
  1478. {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  1479. int i;
  1480. if ((out == NULL) || (data == NULL) || (len < 0)) {
  1481. return -1;
  1482. }
  1483. sum = 0;
  1484. for (i = 0; i < len; i++) {
  1485. count =
  1486. xmlOutputBufferWrite(out, 1,
  1487. (const char *) &hex[data[i] >> 4]);
  1488. if (count == -1)
  1489. return -1;
  1490. sum += count;
  1491. count =
  1492. xmlOutputBufferWrite(out, 1,
  1493. (const char *) &hex[data[i] & 0xF]);
  1494. if (count == -1)
  1495. return -1;
  1496. sum += count;
  1497. }
  1498. return sum;
  1499. }
  1500. /**
  1501. * xmlTextWriterWriteBinHex:
  1502. * @writer: the xmlTextWriterPtr
  1503. * @data: binary data
  1504. * @start: the position within the data of the first byte to encode
  1505. * @len: the number of bytes to encode
  1506. *
  1507. * Write a BinHex encoded xml text.
  1508. *
  1509. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1510. */
  1511. int
  1512. xmlTextWriterWriteBinHex(xmlTextWriterPtr writer, const char *data,
  1513. int start, int len)
  1514. {
  1515. int count;
  1516. int sum;
  1517. xmlLinkPtr lk;
  1518. xmlTextWriterStackEntry *p;
  1519. if ((writer == NULL) || (data == NULL) || (start < 0) || (len < 0))
  1520. return -1;
  1521. sum = 0;
  1522. lk = xmlListFront(writer->nodes);
  1523. if (lk != 0) {
  1524. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1525. if (p != 0) {
  1526. count = xmlTextWriterHandleStateDependencies(writer, p);
  1527. if (count < 0)
  1528. return -1;
  1529. sum += count;
  1530. }
  1531. }
  1532. if (writer->indent)
  1533. writer->doindent = 0;
  1534. count =
  1535. xmlOutputBufferWriteBinHex(writer->out, len,
  1536. (unsigned char *) data + start);
  1537. if (count < 0)
  1538. return -1;
  1539. sum += count;
  1540. return sum;
  1541. }
  1542. /**
  1543. * xmlTextWriterStartAttribute:
  1544. * @writer: the xmlTextWriterPtr
  1545. * @name: element name
  1546. *
  1547. * Start an xml attribute.
  1548. *
  1549. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1550. */
  1551. int
  1552. xmlTextWriterStartAttribute(xmlTextWriterPtr writer, const xmlChar * name)
  1553. {
  1554. int count;
  1555. int sum;
  1556. xmlLinkPtr lk;
  1557. xmlTextWriterStackEntry *p;
  1558. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1559. return -1;
  1560. sum = 0;
  1561. lk = xmlListFront(writer->nodes);
  1562. if (lk == 0)
  1563. return -1;
  1564. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1565. if (p == 0)
  1566. return -1;
  1567. switch (p->state) {
  1568. case XML_TEXTWRITER_ATTRIBUTE:
  1569. count = xmlTextWriterEndAttribute(writer);
  1570. if (count < 0)
  1571. return -1;
  1572. sum += count;
  1573. /* fallthrough */
  1574. case XML_TEXTWRITER_NAME:
  1575. count = xmlOutputBufferWriteString(writer->out, " ");
  1576. if (count < 0)
  1577. return -1;
  1578. sum += count;
  1579. count =
  1580. xmlOutputBufferWriteString(writer->out,
  1581. (const char *) name);
  1582. if (count < 0)
  1583. return -1;
  1584. sum += count;
  1585. count = xmlOutputBufferWriteString(writer->out, "=");
  1586. if (count < 0)
  1587. return -1;
  1588. sum += count;
  1589. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  1590. if (count < 0)
  1591. return -1;
  1592. sum += count;
  1593. p->state = XML_TEXTWRITER_ATTRIBUTE;
  1594. break;
  1595. default:
  1596. return -1;
  1597. }
  1598. return sum;
  1599. }
  1600. /**
  1601. * xmlTextWriterStartAttributeNS:
  1602. * @writer: the xmlTextWriterPtr
  1603. * @prefix: namespace prefix or NULL
  1604. * @name: element local name
  1605. * @namespaceURI: namespace URI or NULL
  1606. *
  1607. * Start an xml attribute with namespace support.
  1608. *
  1609. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1610. */
  1611. int
  1612. xmlTextWriterStartAttributeNS(xmlTextWriterPtr writer,
  1613. const xmlChar * prefix, const xmlChar * name,
  1614. const xmlChar * namespaceURI)
  1615. {
  1616. int count;
  1617. int sum;
  1618. xmlChar *buf;
  1619. xmlTextWriterNsStackEntry *p;
  1620. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1621. return -1;
  1622. /* Handle namespace first in case of error */
  1623. if (namespaceURI != 0) {
  1624. xmlTextWriterNsStackEntry nsentry, *curns;
  1625. buf = xmlStrdup(BAD_CAST "xmlns");
  1626. if (prefix != 0) {
  1627. buf = xmlStrcat(buf, BAD_CAST ":");
  1628. buf = xmlStrcat(buf, prefix);
  1629. }
  1630. nsentry.prefix = buf;
  1631. nsentry.uri = (xmlChar *)namespaceURI;
  1632. nsentry.elem = xmlListFront(writer->nodes);
  1633. curns = (xmlTextWriterNsStackEntry *)xmlListSearch(writer->nsstack,
  1634. (void *)&nsentry);
  1635. if ((curns != NULL)) {
  1636. xmlFree(buf);
  1637. if (xmlStrcmp(curns->uri, namespaceURI) == 0) {
  1638. /* Namespace already defined on element skip */
  1639. buf = NULL;
  1640. } else {
  1641. /* Prefix mismatch so error out */
  1642. return -1;
  1643. }
  1644. }
  1645. /* Do not add namespace decl to list - it is already there */
  1646. if (buf != NULL) {
  1647. p = (xmlTextWriterNsStackEntry *)
  1648. xmlMalloc(sizeof(xmlTextWriterNsStackEntry));
  1649. if (p == 0) {
  1650. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  1651. "xmlTextWriterStartAttributeNS : out of memory!\n");
  1652. return -1;
  1653. }
  1654. p->prefix = buf;
  1655. p->uri = xmlStrdup(namespaceURI);
  1656. if (p->uri == 0) {
  1657. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  1658. "xmlTextWriterStartAttributeNS : out of memory!\n");
  1659. xmlFree(p);
  1660. return -1;
  1661. }
  1662. p->elem = xmlListFront(writer->nodes);
  1663. xmlListPushFront(writer->nsstack, p);
  1664. }
  1665. }
  1666. buf = NULL;
  1667. if (prefix != 0) {
  1668. buf = xmlStrdup(prefix);
  1669. buf = xmlStrcat(buf, BAD_CAST ":");
  1670. }
  1671. buf = xmlStrcat(buf, name);
  1672. sum = 0;
  1673. count = xmlTextWriterStartAttribute(writer, buf);
  1674. xmlFree(buf);
  1675. if (count < 0)
  1676. return -1;
  1677. sum += count;
  1678. return sum;
  1679. }
  1680. /**
  1681. * xmlTextWriterEndAttribute:
  1682. * @writer: the xmlTextWriterPtr
  1683. *
  1684. * End the current xml element.
  1685. *
  1686. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1687. */
  1688. int
  1689. xmlTextWriterEndAttribute(xmlTextWriterPtr writer)
  1690. {
  1691. int count;
  1692. int sum;
  1693. xmlLinkPtr lk;
  1694. xmlTextWriterStackEntry *p;
  1695. if (writer == NULL)
  1696. return -1;
  1697. lk = xmlListFront(writer->nodes);
  1698. if (lk == 0) {
  1699. return -1;
  1700. }
  1701. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  1702. if (p == 0) {
  1703. return -1;
  1704. }
  1705. sum = 0;
  1706. switch (p->state) {
  1707. case XML_TEXTWRITER_ATTRIBUTE:
  1708. p->state = XML_TEXTWRITER_NAME;
  1709. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  1710. if (count < 0) {
  1711. return -1;
  1712. }
  1713. sum += count;
  1714. break;
  1715. default:
  1716. return -1;
  1717. }
  1718. return sum;
  1719. }
  1720. /**
  1721. * xmlTextWriterWriteFormatAttribute:
  1722. * @writer: the xmlTextWriterPtr
  1723. * @name: attribute name
  1724. * @format: format string (see printf)
  1725. * @...: extra parameters for the format
  1726. *
  1727. * Write a formatted xml attribute.
  1728. *
  1729. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1730. */
  1731. int XMLCDECL
  1732. xmlTextWriterWriteFormatAttribute(xmlTextWriterPtr writer,
  1733. const xmlChar * name, const char *format,
  1734. ...)
  1735. {
  1736. int rc;
  1737. va_list ap;
  1738. va_start(ap, format);
  1739. rc = xmlTextWriterWriteVFormatAttribute(writer, name, format, ap);
  1740. va_end(ap);
  1741. return rc;
  1742. }
  1743. /**
  1744. * xmlTextWriterWriteVFormatAttribute:
  1745. * @writer: the xmlTextWriterPtr
  1746. * @name: attribute name
  1747. * @format: format string (see printf)
  1748. * @argptr: pointer to the first member of the variable argument list.
  1749. *
  1750. * Write a formatted xml attribute.
  1751. *
  1752. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1753. */
  1754. int
  1755. xmlTextWriterWriteVFormatAttribute(xmlTextWriterPtr writer,
  1756. const xmlChar * name,
  1757. const char *format, va_list argptr)
  1758. {
  1759. int rc;
  1760. xmlChar *buf;
  1761. if (writer == NULL)
  1762. return -1;
  1763. buf = xmlTextWriterVSprintf(format, argptr);
  1764. if (buf == NULL)
  1765. return -1;
  1766. rc = xmlTextWriterWriteAttribute(writer, name, buf);
  1767. xmlFree(buf);
  1768. return rc;
  1769. }
  1770. /**
  1771. * xmlTextWriterWriteAttribute:
  1772. * @writer: the xmlTextWriterPtr
  1773. * @name: attribute name
  1774. * @content: attribute content
  1775. *
  1776. * Write an xml attribute.
  1777. *
  1778. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1779. */
  1780. int
  1781. xmlTextWriterWriteAttribute(xmlTextWriterPtr writer, const xmlChar * name,
  1782. const xmlChar * content)
  1783. {
  1784. int count;
  1785. int sum;
  1786. sum = 0;
  1787. count = xmlTextWriterStartAttribute(writer, name);
  1788. if (count < 0)
  1789. return -1;
  1790. sum += count;
  1791. count = xmlTextWriterWriteString(writer, content);
  1792. if (count < 0)
  1793. return -1;
  1794. sum += count;
  1795. count = xmlTextWriterEndAttribute(writer);
  1796. if (count < 0)
  1797. return -1;
  1798. sum += count;
  1799. return sum;
  1800. }
  1801. /**
  1802. * xmlTextWriterWriteFormatAttributeNS:
  1803. * @writer: the xmlTextWriterPtr
  1804. * @prefix: namespace prefix
  1805. * @name: attribute local name
  1806. * @namespaceURI: namespace URI
  1807. * @format: format string (see printf)
  1808. * @...: extra parameters for the format
  1809. *
  1810. * Write a formatted xml attribute.with namespace support
  1811. *
  1812. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1813. */
  1814. int XMLCDECL
  1815. xmlTextWriterWriteFormatAttributeNS(xmlTextWriterPtr writer,
  1816. const xmlChar * prefix,
  1817. const xmlChar * name,
  1818. const xmlChar * namespaceURI,
  1819. const char *format, ...)
  1820. {
  1821. int rc;
  1822. va_list ap;
  1823. va_start(ap, format);
  1824. rc = xmlTextWriterWriteVFormatAttributeNS(writer, prefix, name,
  1825. namespaceURI, format, ap);
  1826. va_end(ap);
  1827. return rc;
  1828. }
  1829. /**
  1830. * xmlTextWriterWriteVFormatAttributeNS:
  1831. * @writer: the xmlTextWriterPtr
  1832. * @prefix: namespace prefix
  1833. * @name: attribute local name
  1834. * @namespaceURI: namespace URI
  1835. * @format: format string (see printf)
  1836. * @argptr: pointer to the first member of the variable argument list.
  1837. *
  1838. * Write a formatted xml attribute.with namespace support
  1839. *
  1840. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1841. */
  1842. int
  1843. xmlTextWriterWriteVFormatAttributeNS(xmlTextWriterPtr writer,
  1844. const xmlChar * prefix,
  1845. const xmlChar * name,
  1846. const xmlChar * namespaceURI,
  1847. const char *format, va_list argptr)
  1848. {
  1849. int rc;
  1850. xmlChar *buf;
  1851. if (writer == NULL)
  1852. return -1;
  1853. buf = xmlTextWriterVSprintf(format, argptr);
  1854. if (buf == NULL)
  1855. return -1;
  1856. rc = xmlTextWriterWriteAttributeNS(writer, prefix, name, namespaceURI,
  1857. buf);
  1858. xmlFree(buf);
  1859. return rc;
  1860. }
  1861. /**
  1862. * xmlTextWriterWriteAttributeNS:
  1863. * @writer: the xmlTextWriterPtr
  1864. * @prefix: namespace prefix
  1865. * @name: attribute local name
  1866. * @namespaceURI: namespace URI
  1867. * @content: attribute content
  1868. *
  1869. * Write an xml attribute.
  1870. *
  1871. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1872. */
  1873. int
  1874. xmlTextWriterWriteAttributeNS(xmlTextWriterPtr writer,
  1875. const xmlChar * prefix, const xmlChar * name,
  1876. const xmlChar * namespaceURI,
  1877. const xmlChar * content)
  1878. {
  1879. int count;
  1880. int sum;
  1881. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  1882. return -1;
  1883. sum = 0;
  1884. count = xmlTextWriterStartAttributeNS(writer, prefix, name, namespaceURI);
  1885. if (count < 0)
  1886. return -1;
  1887. sum += count;
  1888. count = xmlTextWriterWriteString(writer, content);
  1889. if (count < 0)
  1890. return -1;
  1891. sum += count;
  1892. count = xmlTextWriterEndAttribute(writer);
  1893. if (count < 0)
  1894. return -1;
  1895. sum += count;
  1896. return sum;
  1897. }
  1898. /**
  1899. * xmlTextWriterWriteFormatElement:
  1900. * @writer: the xmlTextWriterPtr
  1901. * @name: element name
  1902. * @format: format string (see printf)
  1903. * @...: extra parameters for the format
  1904. *
  1905. * Write a formatted xml element.
  1906. *
  1907. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1908. */
  1909. int XMLCDECL
  1910. xmlTextWriterWriteFormatElement(xmlTextWriterPtr writer,
  1911. const xmlChar * name, const char *format,
  1912. ...)
  1913. {
  1914. int rc;
  1915. va_list ap;
  1916. va_start(ap, format);
  1917. rc = xmlTextWriterWriteVFormatElement(writer, name, format, ap);
  1918. va_end(ap);
  1919. return rc;
  1920. }
  1921. /**
  1922. * xmlTextWriterWriteVFormatElement:
  1923. * @writer: the xmlTextWriterPtr
  1924. * @name: element name
  1925. * @format: format string (see printf)
  1926. * @argptr: pointer to the first member of the variable argument list.
  1927. *
  1928. * Write a formatted xml element.
  1929. *
  1930. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1931. */
  1932. int
  1933. xmlTextWriterWriteVFormatElement(xmlTextWriterPtr writer,
  1934. const xmlChar * name, const char *format,
  1935. va_list argptr)
  1936. {
  1937. int rc;
  1938. xmlChar *buf;
  1939. if (writer == NULL)
  1940. return -1;
  1941. buf = xmlTextWriterVSprintf(format, argptr);
  1942. if (buf == NULL)
  1943. return -1;
  1944. rc = xmlTextWriterWriteElement(writer, name, buf);
  1945. xmlFree(buf);
  1946. return rc;
  1947. }
  1948. /**
  1949. * xmlTextWriterWriteElement:
  1950. * @writer: the xmlTextWriterPtr
  1951. * @name: element name
  1952. * @content: element content
  1953. *
  1954. * Write an xml element.
  1955. *
  1956. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1957. */
  1958. int
  1959. xmlTextWriterWriteElement(xmlTextWriterPtr writer, const xmlChar * name,
  1960. const xmlChar * content)
  1961. {
  1962. int count;
  1963. int sum;
  1964. sum = 0;
  1965. count = xmlTextWriterStartElement(writer, name);
  1966. if (count == -1)
  1967. return -1;
  1968. sum += count;
  1969. if (content != NULL) {
  1970. count = xmlTextWriterWriteString(writer, content);
  1971. if (count == -1)
  1972. return -1;
  1973. sum += count;
  1974. }
  1975. count = xmlTextWriterEndElement(writer);
  1976. if (count == -1)
  1977. return -1;
  1978. sum += count;
  1979. return sum;
  1980. }
  1981. /**
  1982. * xmlTextWriterWriteFormatElementNS:
  1983. * @writer: the xmlTextWriterPtr
  1984. * @prefix: namespace prefix
  1985. * @name: element local name
  1986. * @namespaceURI: namespace URI
  1987. * @format: format string (see printf)
  1988. * @...: extra parameters for the format
  1989. *
  1990. * Write a formatted xml element with namespace support.
  1991. *
  1992. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  1993. */
  1994. int XMLCDECL
  1995. xmlTextWriterWriteFormatElementNS(xmlTextWriterPtr writer,
  1996. const xmlChar * prefix,
  1997. const xmlChar * name,
  1998. const xmlChar * namespaceURI,
  1999. const char *format, ...)
  2000. {
  2001. int rc;
  2002. va_list ap;
  2003. va_start(ap, format);
  2004. rc = xmlTextWriterWriteVFormatElementNS(writer, prefix, name,
  2005. namespaceURI, format, ap);
  2006. va_end(ap);
  2007. return rc;
  2008. }
  2009. /**
  2010. * xmlTextWriterWriteVFormatElementNS:
  2011. * @writer: the xmlTextWriterPtr
  2012. * @prefix: namespace prefix
  2013. * @name: element local name
  2014. * @namespaceURI: namespace URI
  2015. * @format: format string (see printf)
  2016. * @argptr: pointer to the first member of the variable argument list.
  2017. *
  2018. * Write a formatted xml element with namespace support.
  2019. *
  2020. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2021. */
  2022. int
  2023. xmlTextWriterWriteVFormatElementNS(xmlTextWriterPtr writer,
  2024. const xmlChar * prefix,
  2025. const xmlChar * name,
  2026. const xmlChar * namespaceURI,
  2027. const char *format, va_list argptr)
  2028. {
  2029. int rc;
  2030. xmlChar *buf;
  2031. if (writer == NULL)
  2032. return -1;
  2033. buf = xmlTextWriterVSprintf(format, argptr);
  2034. if (buf == NULL)
  2035. return -1;
  2036. rc = xmlTextWriterWriteElementNS(writer, prefix, name, namespaceURI,
  2037. buf);
  2038. xmlFree(buf);
  2039. return rc;
  2040. }
  2041. /**
  2042. * xmlTextWriterWriteElementNS:
  2043. * @writer: the xmlTextWriterPtr
  2044. * @prefix: namespace prefix
  2045. * @name: element local name
  2046. * @namespaceURI: namespace URI
  2047. * @content: element content
  2048. *
  2049. * Write an xml element with namespace support.
  2050. *
  2051. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2052. */
  2053. int
  2054. xmlTextWriterWriteElementNS(xmlTextWriterPtr writer,
  2055. const xmlChar * prefix, const xmlChar * name,
  2056. const xmlChar * namespaceURI,
  2057. const xmlChar * content)
  2058. {
  2059. int count;
  2060. int sum;
  2061. if ((writer == NULL) || (name == NULL) || (*name == '\0'))
  2062. return -1;
  2063. sum = 0;
  2064. count =
  2065. xmlTextWriterStartElementNS(writer, prefix, name, namespaceURI);
  2066. if (count < 0)
  2067. return -1;
  2068. sum += count;
  2069. count = xmlTextWriterWriteString(writer, content);
  2070. if (count == -1)
  2071. return -1;
  2072. sum += count;
  2073. count = xmlTextWriterEndElement(writer);
  2074. if (count == -1)
  2075. return -1;
  2076. sum += count;
  2077. return sum;
  2078. }
  2079. /**
  2080. * xmlTextWriterStartPI:
  2081. * @writer: the xmlTextWriterPtr
  2082. * @target: PI target
  2083. *
  2084. * Start an xml PI.
  2085. *
  2086. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2087. */
  2088. int
  2089. xmlTextWriterStartPI(xmlTextWriterPtr writer, const xmlChar * target)
  2090. {
  2091. int count;
  2092. int sum;
  2093. xmlLinkPtr lk;
  2094. xmlTextWriterStackEntry *p;
  2095. if ((writer == NULL) || (target == NULL) || (*target == '\0'))
  2096. return -1;
  2097. if (xmlStrcasecmp(target, (const xmlChar *) "xml") == 0) {
  2098. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2099. "xmlTextWriterStartPI : target name [Xx][Mm][Ll] is reserved for xml standardization!\n");
  2100. return -1;
  2101. }
  2102. sum = 0;
  2103. lk = xmlListFront(writer->nodes);
  2104. if (lk != 0) {
  2105. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2106. if (p != 0) {
  2107. switch (p->state) {
  2108. case XML_TEXTWRITER_ATTRIBUTE:
  2109. count = xmlTextWriterEndAttribute(writer);
  2110. if (count < 0)
  2111. return -1;
  2112. sum += count;
  2113. /* fallthrough */
  2114. case XML_TEXTWRITER_NAME:
  2115. /* Output namespace declarations */
  2116. count = xmlTextWriterOutputNSDecl(writer);
  2117. if (count < 0)
  2118. return -1;
  2119. sum += count;
  2120. count = xmlOutputBufferWriteString(writer->out, ">");
  2121. if (count < 0)
  2122. return -1;
  2123. sum += count;
  2124. p->state = XML_TEXTWRITER_TEXT;
  2125. break;
  2126. case XML_TEXTWRITER_NONE:
  2127. case XML_TEXTWRITER_TEXT:
  2128. case XML_TEXTWRITER_DTD:
  2129. break;
  2130. case XML_TEXTWRITER_PI:
  2131. case XML_TEXTWRITER_PI_TEXT:
  2132. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2133. "xmlTextWriterStartPI : nested PI!\n");
  2134. return -1;
  2135. default:
  2136. return -1;
  2137. }
  2138. }
  2139. }
  2140. p = (xmlTextWriterStackEntry *)
  2141. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2142. if (p == 0) {
  2143. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2144. "xmlTextWriterStartPI : out of memory!\n");
  2145. return -1;
  2146. }
  2147. p->name = xmlStrdup(target);
  2148. if (p->name == 0) {
  2149. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2150. "xmlTextWriterStartPI : out of memory!\n");
  2151. xmlFree(p);
  2152. return -1;
  2153. }
  2154. p->state = XML_TEXTWRITER_PI;
  2155. xmlListPushFront(writer->nodes, p);
  2156. count = xmlOutputBufferWriteString(writer->out, "<?");
  2157. if (count < 0)
  2158. return -1;
  2159. sum += count;
  2160. count =
  2161. xmlOutputBufferWriteString(writer->out, (const char *) p->name);
  2162. if (count < 0)
  2163. return -1;
  2164. sum += count;
  2165. return sum;
  2166. }
  2167. /**
  2168. * xmlTextWriterEndPI:
  2169. * @writer: the xmlTextWriterPtr
  2170. *
  2171. * End the current xml PI.
  2172. *
  2173. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2174. */
  2175. int
  2176. xmlTextWriterEndPI(xmlTextWriterPtr writer)
  2177. {
  2178. int count;
  2179. int sum;
  2180. xmlLinkPtr lk;
  2181. xmlTextWriterStackEntry *p;
  2182. if (writer == NULL)
  2183. return -1;
  2184. lk = xmlListFront(writer->nodes);
  2185. if (lk == 0)
  2186. return 0;
  2187. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2188. if (p == 0)
  2189. return 0;
  2190. sum = 0;
  2191. switch (p->state) {
  2192. case XML_TEXTWRITER_PI:
  2193. case XML_TEXTWRITER_PI_TEXT:
  2194. count = xmlOutputBufferWriteString(writer->out, "?>");
  2195. if (count < 0)
  2196. return -1;
  2197. sum += count;
  2198. break;
  2199. default:
  2200. return -1;
  2201. }
  2202. if (writer->indent) {
  2203. count = xmlOutputBufferWriteString(writer->out, "\n");
  2204. if (count < 0)
  2205. return -1;
  2206. sum += count;
  2207. }
  2208. xmlListPopFront(writer->nodes);
  2209. return sum;
  2210. }
  2211. /**
  2212. * xmlTextWriterWriteFormatPI:
  2213. * @writer: the xmlTextWriterPtr
  2214. * @target: PI target
  2215. * @format: format string (see printf)
  2216. * @...: extra parameters for the format
  2217. *
  2218. * Write a formatted PI.
  2219. *
  2220. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2221. */
  2222. int XMLCDECL
  2223. xmlTextWriterWriteFormatPI(xmlTextWriterPtr writer, const xmlChar * target,
  2224. const char *format, ...)
  2225. {
  2226. int rc;
  2227. va_list ap;
  2228. va_start(ap, format);
  2229. rc = xmlTextWriterWriteVFormatPI(writer, target, format, ap);
  2230. va_end(ap);
  2231. return rc;
  2232. }
  2233. /**
  2234. * xmlTextWriterWriteVFormatPI:
  2235. * @writer: the xmlTextWriterPtr
  2236. * @target: PI target
  2237. * @format: format string (see printf)
  2238. * @argptr: pointer to the first member of the variable argument list.
  2239. *
  2240. * Write a formatted xml PI.
  2241. *
  2242. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2243. */
  2244. int
  2245. xmlTextWriterWriteVFormatPI(xmlTextWriterPtr writer,
  2246. const xmlChar * target, const char *format,
  2247. va_list argptr)
  2248. {
  2249. int rc;
  2250. xmlChar *buf;
  2251. if (writer == NULL)
  2252. return -1;
  2253. buf = xmlTextWriterVSprintf(format, argptr);
  2254. if (buf == NULL)
  2255. return -1;
  2256. rc = xmlTextWriterWritePI(writer, target, buf);
  2257. xmlFree(buf);
  2258. return rc;
  2259. }
  2260. /**
  2261. * xmlTextWriterWritePI:
  2262. * @writer: the xmlTextWriterPtr
  2263. * @target: PI target
  2264. * @content: PI content
  2265. *
  2266. * Write an xml PI.
  2267. *
  2268. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2269. */
  2270. int
  2271. xmlTextWriterWritePI(xmlTextWriterPtr writer, const xmlChar * target,
  2272. const xmlChar * content)
  2273. {
  2274. int count;
  2275. int sum;
  2276. sum = 0;
  2277. count = xmlTextWriterStartPI(writer, target);
  2278. if (count == -1)
  2279. return -1;
  2280. sum += count;
  2281. if (content != 0) {
  2282. count = xmlTextWriterWriteString(writer, content);
  2283. if (count == -1)
  2284. return -1;
  2285. sum += count;
  2286. }
  2287. count = xmlTextWriterEndPI(writer);
  2288. if (count == -1)
  2289. return -1;
  2290. sum += count;
  2291. return sum;
  2292. }
  2293. /**
  2294. * xmlTextWriterStartCDATA:
  2295. * @writer: the xmlTextWriterPtr
  2296. *
  2297. * Start an xml CDATA section.
  2298. *
  2299. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2300. */
  2301. int
  2302. xmlTextWriterStartCDATA(xmlTextWriterPtr writer)
  2303. {
  2304. int count;
  2305. int sum;
  2306. xmlLinkPtr lk;
  2307. xmlTextWriterStackEntry *p;
  2308. if (writer == NULL)
  2309. return -1;
  2310. sum = 0;
  2311. lk = xmlListFront(writer->nodes);
  2312. if (lk != 0) {
  2313. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2314. if (p != 0) {
  2315. switch (p->state) {
  2316. case XML_TEXTWRITER_NONE:
  2317. case XML_TEXTWRITER_TEXT:
  2318. case XML_TEXTWRITER_PI:
  2319. case XML_TEXTWRITER_PI_TEXT:
  2320. break;
  2321. case XML_TEXTWRITER_ATTRIBUTE:
  2322. count = xmlTextWriterEndAttribute(writer);
  2323. if (count < 0)
  2324. return -1;
  2325. sum += count;
  2326. /* fallthrough */
  2327. case XML_TEXTWRITER_NAME:
  2328. /* Output namespace declarations */
  2329. count = xmlTextWriterOutputNSDecl(writer);
  2330. if (count < 0)
  2331. return -1;
  2332. sum += count;
  2333. count = xmlOutputBufferWriteString(writer->out, ">");
  2334. if (count < 0)
  2335. return -1;
  2336. sum += count;
  2337. p->state = XML_TEXTWRITER_TEXT;
  2338. break;
  2339. case XML_TEXTWRITER_CDATA:
  2340. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2341. "xmlTextWriterStartCDATA : CDATA not allowed in this context!\n");
  2342. return -1;
  2343. default:
  2344. return -1;
  2345. }
  2346. }
  2347. }
  2348. p = (xmlTextWriterStackEntry *)
  2349. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2350. if (p == 0) {
  2351. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2352. "xmlTextWriterStartCDATA : out of memory!\n");
  2353. return -1;
  2354. }
  2355. p->name = NULL;
  2356. p->state = XML_TEXTWRITER_CDATA;
  2357. xmlListPushFront(writer->nodes, p);
  2358. count = xmlOutputBufferWriteString(writer->out, "<![CDATA[");
  2359. if (count < 0)
  2360. return -1;
  2361. sum += count;
  2362. return sum;
  2363. }
  2364. /**
  2365. * xmlTextWriterEndCDATA:
  2366. * @writer: the xmlTextWriterPtr
  2367. *
  2368. * End an xml CDATA section.
  2369. *
  2370. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2371. */
  2372. int
  2373. xmlTextWriterEndCDATA(xmlTextWriterPtr writer)
  2374. {
  2375. int count;
  2376. int sum;
  2377. xmlLinkPtr lk;
  2378. xmlTextWriterStackEntry *p;
  2379. if (writer == NULL)
  2380. return -1;
  2381. lk = xmlListFront(writer->nodes);
  2382. if (lk == 0)
  2383. return -1;
  2384. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2385. if (p == 0)
  2386. return -1;
  2387. sum = 0;
  2388. switch (p->state) {
  2389. case XML_TEXTWRITER_CDATA:
  2390. count = xmlOutputBufferWriteString(writer->out, "]]>");
  2391. if (count < 0)
  2392. return -1;
  2393. sum += count;
  2394. break;
  2395. default:
  2396. return -1;
  2397. }
  2398. xmlListPopFront(writer->nodes);
  2399. return sum;
  2400. }
  2401. /**
  2402. * xmlTextWriterWriteFormatCDATA:
  2403. * @writer: the xmlTextWriterPtr
  2404. * @format: format string (see printf)
  2405. * @...: extra parameters for the format
  2406. *
  2407. * Write a formatted xml CDATA.
  2408. *
  2409. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2410. */
  2411. int XMLCDECL
  2412. xmlTextWriterWriteFormatCDATA(xmlTextWriterPtr writer, const char *format,
  2413. ...)
  2414. {
  2415. int rc;
  2416. va_list ap;
  2417. va_start(ap, format);
  2418. rc = xmlTextWriterWriteVFormatCDATA(writer, format, ap);
  2419. va_end(ap);
  2420. return rc;
  2421. }
  2422. /**
  2423. * xmlTextWriterWriteVFormatCDATA:
  2424. * @writer: the xmlTextWriterPtr
  2425. * @format: format string (see printf)
  2426. * @argptr: pointer to the first member of the variable argument list.
  2427. *
  2428. * Write a formatted xml CDATA.
  2429. *
  2430. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2431. */
  2432. int
  2433. xmlTextWriterWriteVFormatCDATA(xmlTextWriterPtr writer, const char *format,
  2434. va_list argptr)
  2435. {
  2436. int rc;
  2437. xmlChar *buf;
  2438. if (writer == NULL)
  2439. return -1;
  2440. buf = xmlTextWriterVSprintf(format, argptr);
  2441. if (buf == NULL)
  2442. return -1;
  2443. rc = xmlTextWriterWriteCDATA(writer, buf);
  2444. xmlFree(buf);
  2445. return rc;
  2446. }
  2447. /**
  2448. * xmlTextWriterWriteCDATA:
  2449. * @writer: the xmlTextWriterPtr
  2450. * @content: CDATA content
  2451. *
  2452. * Write an xml CDATA.
  2453. *
  2454. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2455. */
  2456. int
  2457. xmlTextWriterWriteCDATA(xmlTextWriterPtr writer, const xmlChar * content)
  2458. {
  2459. int count;
  2460. int sum;
  2461. sum = 0;
  2462. count = xmlTextWriterStartCDATA(writer);
  2463. if (count == -1)
  2464. return -1;
  2465. sum += count;
  2466. if (content != 0) {
  2467. count = xmlTextWriterWriteString(writer, content);
  2468. if (count == -1)
  2469. return -1;
  2470. sum += count;
  2471. }
  2472. count = xmlTextWriterEndCDATA(writer);
  2473. if (count == -1)
  2474. return -1;
  2475. sum += count;
  2476. return sum;
  2477. }
  2478. /**
  2479. * xmlTextWriterStartDTD:
  2480. * @writer: the xmlTextWriterPtr
  2481. * @name: the name of the DTD
  2482. * @pubid: the public identifier, which is an alternative to the system identifier
  2483. * @sysid: the system identifier, which is the URI of the DTD
  2484. *
  2485. * Start an xml DTD.
  2486. *
  2487. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2488. */
  2489. int
  2490. xmlTextWriterStartDTD(xmlTextWriterPtr writer,
  2491. const xmlChar * name,
  2492. const xmlChar * pubid, const xmlChar * sysid)
  2493. {
  2494. int count;
  2495. int sum;
  2496. xmlLinkPtr lk;
  2497. xmlTextWriterStackEntry *p;
  2498. if (writer == NULL || name == NULL || *name == '\0')
  2499. return -1;
  2500. sum = 0;
  2501. lk = xmlListFront(writer->nodes);
  2502. if ((lk != NULL) && (xmlLinkGetData(lk) != NULL)) {
  2503. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2504. "xmlTextWriterStartDTD : DTD allowed only in prolog!\n");
  2505. return -1;
  2506. }
  2507. p = (xmlTextWriterStackEntry *)
  2508. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2509. if (p == 0) {
  2510. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2511. "xmlTextWriterStartDTD : out of memory!\n");
  2512. return -1;
  2513. }
  2514. p->name = xmlStrdup(name);
  2515. if (p->name == 0) {
  2516. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2517. "xmlTextWriterStartDTD : out of memory!\n");
  2518. xmlFree(p);
  2519. return -1;
  2520. }
  2521. p->state = XML_TEXTWRITER_DTD;
  2522. xmlListPushFront(writer->nodes, p);
  2523. count = xmlOutputBufferWriteString(writer->out, "<!DOCTYPE ");
  2524. if (count < 0)
  2525. return -1;
  2526. sum += count;
  2527. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  2528. if (count < 0)
  2529. return -1;
  2530. sum += count;
  2531. if (pubid != 0) {
  2532. if (sysid == 0) {
  2533. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  2534. "xmlTextWriterStartDTD : system identifier needed!\n");
  2535. return -1;
  2536. }
  2537. if (writer->indent)
  2538. count = xmlOutputBufferWrite(writer->out, 1, "\n");
  2539. else
  2540. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2541. if (count < 0)
  2542. return -1;
  2543. sum += count;
  2544. count = xmlOutputBufferWriteString(writer->out, "PUBLIC ");
  2545. if (count < 0)
  2546. return -1;
  2547. sum += count;
  2548. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2549. if (count < 0)
  2550. return -1;
  2551. sum += count;
  2552. count =
  2553. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  2554. if (count < 0)
  2555. return -1;
  2556. sum += count;
  2557. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2558. if (count < 0)
  2559. return -1;
  2560. sum += count;
  2561. }
  2562. if (sysid != 0) {
  2563. if (pubid == 0) {
  2564. if (writer->indent)
  2565. count = xmlOutputBufferWrite(writer->out, 1, "\n");
  2566. else
  2567. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2568. if (count < 0)
  2569. return -1;
  2570. sum += count;
  2571. count = xmlOutputBufferWriteString(writer->out, "SYSTEM ");
  2572. if (count < 0)
  2573. return -1;
  2574. sum += count;
  2575. } else {
  2576. if (writer->indent)
  2577. count = xmlOutputBufferWriteString(writer->out, "\n ");
  2578. else
  2579. count = xmlOutputBufferWrite(writer->out, 1, " ");
  2580. if (count < 0)
  2581. return -1;
  2582. sum += count;
  2583. }
  2584. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2585. if (count < 0)
  2586. return -1;
  2587. sum += count;
  2588. count =
  2589. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  2590. if (count < 0)
  2591. return -1;
  2592. sum += count;
  2593. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  2594. if (count < 0)
  2595. return -1;
  2596. sum += count;
  2597. }
  2598. return sum;
  2599. }
  2600. /**
  2601. * xmlTextWriterEndDTD:
  2602. * @writer: the xmlTextWriterPtr
  2603. *
  2604. * End an xml DTD.
  2605. *
  2606. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2607. */
  2608. int
  2609. xmlTextWriterEndDTD(xmlTextWriterPtr writer)
  2610. {
  2611. int loop;
  2612. int count;
  2613. int sum;
  2614. xmlLinkPtr lk;
  2615. xmlTextWriterStackEntry *p;
  2616. if (writer == NULL)
  2617. return -1;
  2618. sum = 0;
  2619. loop = 1;
  2620. while (loop) {
  2621. lk = xmlListFront(writer->nodes);
  2622. if (lk == NULL)
  2623. break;
  2624. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2625. if (p == 0)
  2626. break;
  2627. switch (p->state) {
  2628. case XML_TEXTWRITER_DTD_TEXT:
  2629. count = xmlOutputBufferWriteString(writer->out, "]");
  2630. if (count < 0)
  2631. return -1;
  2632. sum += count;
  2633. /* fallthrough */
  2634. case XML_TEXTWRITER_DTD:
  2635. count = xmlOutputBufferWriteString(writer->out, ">");
  2636. if (writer->indent) {
  2637. if (count < 0)
  2638. return -1;
  2639. sum += count;
  2640. count = xmlOutputBufferWriteString(writer->out, "\n");
  2641. }
  2642. xmlListPopFront(writer->nodes);
  2643. break;
  2644. case XML_TEXTWRITER_DTD_ELEM:
  2645. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  2646. count = xmlTextWriterEndDTDElement(writer);
  2647. break;
  2648. case XML_TEXTWRITER_DTD_ATTL:
  2649. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  2650. count = xmlTextWriterEndDTDAttlist(writer);
  2651. break;
  2652. case XML_TEXTWRITER_DTD_ENTY:
  2653. case XML_TEXTWRITER_DTD_PENT:
  2654. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  2655. count = xmlTextWriterEndDTDEntity(writer);
  2656. break;
  2657. case XML_TEXTWRITER_COMMENT:
  2658. count = xmlTextWriterEndComment(writer);
  2659. break;
  2660. default:
  2661. loop = 0;
  2662. continue;
  2663. }
  2664. if (count < 0)
  2665. return -1;
  2666. sum += count;
  2667. }
  2668. return sum;
  2669. }
  2670. /**
  2671. * xmlTextWriterWriteFormatDTD:
  2672. * @writer: the xmlTextWriterPtr
  2673. * @name: the name of the DTD
  2674. * @pubid: the public identifier, which is an alternative to the system identifier
  2675. * @sysid: the system identifier, which is the URI of the DTD
  2676. * @format: format string (see printf)
  2677. * @...: extra parameters for the format
  2678. *
  2679. * Write a DTD with a formatted markup declarations part.
  2680. *
  2681. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2682. */
  2683. int XMLCDECL
  2684. xmlTextWriterWriteFormatDTD(xmlTextWriterPtr writer,
  2685. const xmlChar * name,
  2686. const xmlChar * pubid,
  2687. const xmlChar * sysid, const char *format, ...)
  2688. {
  2689. int rc;
  2690. va_list ap;
  2691. va_start(ap, format);
  2692. rc = xmlTextWriterWriteVFormatDTD(writer, name, pubid, sysid, format,
  2693. ap);
  2694. va_end(ap);
  2695. return rc;
  2696. }
  2697. /**
  2698. * xmlTextWriterWriteVFormatDTD:
  2699. * @writer: the xmlTextWriterPtr
  2700. * @name: the name of the DTD
  2701. * @pubid: the public identifier, which is an alternative to the system identifier
  2702. * @sysid: the system identifier, which is the URI of the DTD
  2703. * @format: format string (see printf)
  2704. * @argptr: pointer to the first member of the variable argument list.
  2705. *
  2706. * Write a DTD with a formatted markup declarations part.
  2707. *
  2708. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2709. */
  2710. int
  2711. xmlTextWriterWriteVFormatDTD(xmlTextWriterPtr writer,
  2712. const xmlChar * name,
  2713. const xmlChar * pubid,
  2714. const xmlChar * sysid,
  2715. const char *format, va_list argptr)
  2716. {
  2717. int rc;
  2718. xmlChar *buf;
  2719. if (writer == NULL)
  2720. return -1;
  2721. buf = xmlTextWriterVSprintf(format, argptr);
  2722. if (buf == NULL)
  2723. return -1;
  2724. rc = xmlTextWriterWriteDTD(writer, name, pubid, sysid, buf);
  2725. xmlFree(buf);
  2726. return rc;
  2727. }
  2728. /**
  2729. * xmlTextWriterWriteDTD:
  2730. * @writer: the xmlTextWriterPtr
  2731. * @name: the name of the DTD
  2732. * @pubid: the public identifier, which is an alternative to the system identifier
  2733. * @sysid: the system identifier, which is the URI of the DTD
  2734. * @subset: string content of the DTD
  2735. *
  2736. * Write a DTD.
  2737. *
  2738. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2739. */
  2740. int
  2741. xmlTextWriterWriteDTD(xmlTextWriterPtr writer,
  2742. const xmlChar * name,
  2743. const xmlChar * pubid,
  2744. const xmlChar * sysid, const xmlChar * subset)
  2745. {
  2746. int count;
  2747. int sum;
  2748. sum = 0;
  2749. count = xmlTextWriterStartDTD(writer, name, pubid, sysid);
  2750. if (count == -1)
  2751. return -1;
  2752. sum += count;
  2753. if (subset != 0) {
  2754. count = xmlTextWriterWriteString(writer, subset);
  2755. if (count == -1)
  2756. return -1;
  2757. sum += count;
  2758. }
  2759. count = xmlTextWriterEndDTD(writer);
  2760. if (count == -1)
  2761. return -1;
  2762. sum += count;
  2763. return sum;
  2764. }
  2765. /**
  2766. * xmlTextWriterStartDTDElement:
  2767. * @writer: the xmlTextWriterPtr
  2768. * @name: the name of the DTD element
  2769. *
  2770. * Start an xml DTD element.
  2771. *
  2772. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2773. */
  2774. int
  2775. xmlTextWriterStartDTDElement(xmlTextWriterPtr writer, const xmlChar * name)
  2776. {
  2777. int count;
  2778. int sum;
  2779. xmlLinkPtr lk;
  2780. xmlTextWriterStackEntry *p;
  2781. if (writer == NULL || name == NULL || *name == '\0')
  2782. return -1;
  2783. sum = 0;
  2784. lk = xmlListFront(writer->nodes);
  2785. if (lk == 0) {
  2786. return -1;
  2787. }
  2788. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2789. if (p != 0) {
  2790. switch (p->state) {
  2791. case XML_TEXTWRITER_DTD:
  2792. count = xmlOutputBufferWriteString(writer->out, " [");
  2793. if (count < 0)
  2794. return -1;
  2795. sum += count;
  2796. if (writer->indent) {
  2797. count = xmlOutputBufferWriteString(writer->out, "\n");
  2798. if (count < 0)
  2799. return -1;
  2800. sum += count;
  2801. }
  2802. p->state = XML_TEXTWRITER_DTD_TEXT;
  2803. /* fallthrough */
  2804. case XML_TEXTWRITER_DTD_TEXT:
  2805. case XML_TEXTWRITER_NONE:
  2806. break;
  2807. default:
  2808. return -1;
  2809. }
  2810. }
  2811. p = (xmlTextWriterStackEntry *)
  2812. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  2813. if (p == 0) {
  2814. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2815. "xmlTextWriterStartDTDElement : out of memory!\n");
  2816. return -1;
  2817. }
  2818. p->name = xmlStrdup(name);
  2819. if (p->name == 0) {
  2820. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  2821. "xmlTextWriterStartDTDElement : out of memory!\n");
  2822. xmlFree(p);
  2823. return -1;
  2824. }
  2825. p->state = XML_TEXTWRITER_DTD_ELEM;
  2826. xmlListPushFront(writer->nodes, p);
  2827. if (writer->indent) {
  2828. count = xmlTextWriterWriteIndent(writer);
  2829. if (count < 0)
  2830. return -1;
  2831. sum += count;
  2832. }
  2833. count = xmlOutputBufferWriteString(writer->out, "<!ELEMENT ");
  2834. if (count < 0)
  2835. return -1;
  2836. sum += count;
  2837. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  2838. if (count < 0)
  2839. return -1;
  2840. sum += count;
  2841. return sum;
  2842. }
  2843. /**
  2844. * xmlTextWriterEndDTDElement:
  2845. * @writer: the xmlTextWriterPtr
  2846. *
  2847. * End an xml DTD element.
  2848. *
  2849. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2850. */
  2851. int
  2852. xmlTextWriterEndDTDElement(xmlTextWriterPtr writer)
  2853. {
  2854. int count;
  2855. int sum;
  2856. xmlLinkPtr lk;
  2857. xmlTextWriterStackEntry *p;
  2858. if (writer == NULL)
  2859. return -1;
  2860. sum = 0;
  2861. lk = xmlListFront(writer->nodes);
  2862. if (lk == 0)
  2863. return -1;
  2864. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2865. if (p == 0)
  2866. return -1;
  2867. switch (p->state) {
  2868. case XML_TEXTWRITER_DTD_ELEM:
  2869. case XML_TEXTWRITER_DTD_ELEM_TEXT:
  2870. count = xmlOutputBufferWriteString(writer->out, ">");
  2871. if (count < 0)
  2872. return -1;
  2873. sum += count;
  2874. break;
  2875. default:
  2876. return -1;
  2877. }
  2878. if (writer->indent) {
  2879. count = xmlOutputBufferWriteString(writer->out, "\n");
  2880. if (count < 0)
  2881. return -1;
  2882. sum += count;
  2883. }
  2884. xmlListPopFront(writer->nodes);
  2885. return sum;
  2886. }
  2887. /**
  2888. * xmlTextWriterWriteFormatDTDElement:
  2889. * @writer: the xmlTextWriterPtr
  2890. * @name: the name of the DTD element
  2891. * @format: format string (see printf)
  2892. * @...: extra parameters for the format
  2893. *
  2894. * Write a formatted DTD element.
  2895. *
  2896. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2897. */
  2898. int XMLCDECL
  2899. xmlTextWriterWriteFormatDTDElement(xmlTextWriterPtr writer,
  2900. const xmlChar * name,
  2901. const char *format, ...)
  2902. {
  2903. int rc;
  2904. va_list ap;
  2905. va_start(ap, format);
  2906. rc = xmlTextWriterWriteVFormatDTDElement(writer, name, format, ap);
  2907. va_end(ap);
  2908. return rc;
  2909. }
  2910. /**
  2911. * xmlTextWriterWriteVFormatDTDElement:
  2912. * @writer: the xmlTextWriterPtr
  2913. * @name: the name of the DTD element
  2914. * @format: format string (see printf)
  2915. * @argptr: pointer to the first member of the variable argument list.
  2916. *
  2917. * Write a formatted DTD element.
  2918. *
  2919. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2920. */
  2921. int
  2922. xmlTextWriterWriteVFormatDTDElement(xmlTextWriterPtr writer,
  2923. const xmlChar * name,
  2924. const char *format, va_list argptr)
  2925. {
  2926. int rc;
  2927. xmlChar *buf;
  2928. if (writer == NULL)
  2929. return -1;
  2930. buf = xmlTextWriterVSprintf(format, argptr);
  2931. if (buf == NULL)
  2932. return -1;
  2933. rc = xmlTextWriterWriteDTDElement(writer, name, buf);
  2934. xmlFree(buf);
  2935. return rc;
  2936. }
  2937. /**
  2938. * xmlTextWriterWriteDTDElement:
  2939. * @writer: the xmlTextWriterPtr
  2940. * @name: the name of the DTD element
  2941. * @content: content of the element
  2942. *
  2943. * Write a DTD element.
  2944. *
  2945. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2946. */
  2947. int
  2948. xmlTextWriterWriteDTDElement(xmlTextWriterPtr writer,
  2949. const xmlChar * name, const xmlChar * content)
  2950. {
  2951. int count;
  2952. int sum;
  2953. if (content == NULL)
  2954. return -1;
  2955. sum = 0;
  2956. count = xmlTextWriterStartDTDElement(writer, name);
  2957. if (count == -1)
  2958. return -1;
  2959. sum += count;
  2960. count = xmlTextWriterWriteString(writer, content);
  2961. if (count == -1)
  2962. return -1;
  2963. sum += count;
  2964. count = xmlTextWriterEndDTDElement(writer);
  2965. if (count == -1)
  2966. return -1;
  2967. sum += count;
  2968. return sum;
  2969. }
  2970. /**
  2971. * xmlTextWriterStartDTDAttlist:
  2972. * @writer: the xmlTextWriterPtr
  2973. * @name: the name of the DTD ATTLIST
  2974. *
  2975. * Start an xml DTD ATTLIST.
  2976. *
  2977. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  2978. */
  2979. int
  2980. xmlTextWriterStartDTDAttlist(xmlTextWriterPtr writer, const xmlChar * name)
  2981. {
  2982. int count;
  2983. int sum;
  2984. xmlLinkPtr lk;
  2985. xmlTextWriterStackEntry *p;
  2986. if (writer == NULL || name == NULL || *name == '\0')
  2987. return -1;
  2988. sum = 0;
  2989. lk = xmlListFront(writer->nodes);
  2990. if (lk == 0) {
  2991. return -1;
  2992. }
  2993. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  2994. if (p != 0) {
  2995. switch (p->state) {
  2996. case XML_TEXTWRITER_DTD:
  2997. count = xmlOutputBufferWriteString(writer->out, " [");
  2998. if (count < 0)
  2999. return -1;
  3000. sum += count;
  3001. if (writer->indent) {
  3002. count = xmlOutputBufferWriteString(writer->out, "\n");
  3003. if (count < 0)
  3004. return -1;
  3005. sum += count;
  3006. }
  3007. p->state = XML_TEXTWRITER_DTD_TEXT;
  3008. /* fallthrough */
  3009. case XML_TEXTWRITER_DTD_TEXT:
  3010. case XML_TEXTWRITER_NONE:
  3011. break;
  3012. default:
  3013. return -1;
  3014. }
  3015. }
  3016. p = (xmlTextWriterStackEntry *)
  3017. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  3018. if (p == 0) {
  3019. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3020. "xmlTextWriterStartDTDAttlist : out of memory!\n");
  3021. return -1;
  3022. }
  3023. p->name = xmlStrdup(name);
  3024. if (p->name == 0) {
  3025. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3026. "xmlTextWriterStartDTDAttlist : out of memory!\n");
  3027. xmlFree(p);
  3028. return -1;
  3029. }
  3030. p->state = XML_TEXTWRITER_DTD_ATTL;
  3031. xmlListPushFront(writer->nodes, p);
  3032. if (writer->indent) {
  3033. count = xmlTextWriterWriteIndent(writer);
  3034. if (count < 0)
  3035. return -1;
  3036. sum += count;
  3037. }
  3038. count = xmlOutputBufferWriteString(writer->out, "<!ATTLIST ");
  3039. if (count < 0)
  3040. return -1;
  3041. sum += count;
  3042. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3043. if (count < 0)
  3044. return -1;
  3045. sum += count;
  3046. return sum;
  3047. }
  3048. /**
  3049. * xmlTextWriterEndDTDAttlist:
  3050. * @writer: the xmlTextWriterPtr
  3051. *
  3052. * End an xml DTD attribute list.
  3053. *
  3054. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3055. */
  3056. int
  3057. xmlTextWriterEndDTDAttlist(xmlTextWriterPtr writer)
  3058. {
  3059. int count;
  3060. int sum;
  3061. xmlLinkPtr lk;
  3062. xmlTextWriterStackEntry *p;
  3063. if (writer == NULL)
  3064. return -1;
  3065. sum = 0;
  3066. lk = xmlListFront(writer->nodes);
  3067. if (lk == 0)
  3068. return -1;
  3069. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3070. if (p == 0)
  3071. return -1;
  3072. switch (p->state) {
  3073. case XML_TEXTWRITER_DTD_ATTL:
  3074. case XML_TEXTWRITER_DTD_ATTL_TEXT:
  3075. count = xmlOutputBufferWriteString(writer->out, ">");
  3076. if (count < 0)
  3077. return -1;
  3078. sum += count;
  3079. break;
  3080. default:
  3081. return -1;
  3082. }
  3083. if (writer->indent) {
  3084. count = xmlOutputBufferWriteString(writer->out, "\n");
  3085. if (count < 0)
  3086. return -1;
  3087. sum += count;
  3088. }
  3089. xmlListPopFront(writer->nodes);
  3090. return sum;
  3091. }
  3092. /**
  3093. * xmlTextWriterWriteFormatDTDAttlist:
  3094. * @writer: the xmlTextWriterPtr
  3095. * @name: the name of the DTD ATTLIST
  3096. * @format: format string (see printf)
  3097. * @...: extra parameters for the format
  3098. *
  3099. * Write a formatted DTD ATTLIST.
  3100. *
  3101. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3102. */
  3103. int XMLCDECL
  3104. xmlTextWriterWriteFormatDTDAttlist(xmlTextWriterPtr writer,
  3105. const xmlChar * name,
  3106. const char *format, ...)
  3107. {
  3108. int rc;
  3109. va_list ap;
  3110. va_start(ap, format);
  3111. rc = xmlTextWriterWriteVFormatDTDAttlist(writer, name, format, ap);
  3112. va_end(ap);
  3113. return rc;
  3114. }
  3115. /**
  3116. * xmlTextWriterWriteVFormatDTDAttlist:
  3117. * @writer: the xmlTextWriterPtr
  3118. * @name: the name of the DTD ATTLIST
  3119. * @format: format string (see printf)
  3120. * @argptr: pointer to the first member of the variable argument list.
  3121. *
  3122. * Write a formatted DTD ATTLIST.
  3123. *
  3124. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3125. */
  3126. int
  3127. xmlTextWriterWriteVFormatDTDAttlist(xmlTextWriterPtr writer,
  3128. const xmlChar * name,
  3129. const char *format, va_list argptr)
  3130. {
  3131. int rc;
  3132. xmlChar *buf;
  3133. if (writer == NULL)
  3134. return -1;
  3135. buf = xmlTextWriterVSprintf(format, argptr);
  3136. if (buf == NULL)
  3137. return -1;
  3138. rc = xmlTextWriterWriteDTDAttlist(writer, name, buf);
  3139. xmlFree(buf);
  3140. return rc;
  3141. }
  3142. /**
  3143. * xmlTextWriterWriteDTDAttlist:
  3144. * @writer: the xmlTextWriterPtr
  3145. * @name: the name of the DTD ATTLIST
  3146. * @content: content of the ATTLIST
  3147. *
  3148. * Write a DTD ATTLIST.
  3149. *
  3150. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3151. */
  3152. int
  3153. xmlTextWriterWriteDTDAttlist(xmlTextWriterPtr writer,
  3154. const xmlChar * name, const xmlChar * content)
  3155. {
  3156. int count;
  3157. int sum;
  3158. if (content == NULL)
  3159. return -1;
  3160. sum = 0;
  3161. count = xmlTextWriterStartDTDAttlist(writer, name);
  3162. if (count == -1)
  3163. return -1;
  3164. sum += count;
  3165. count = xmlTextWriterWriteString(writer, content);
  3166. if (count == -1)
  3167. return -1;
  3168. sum += count;
  3169. count = xmlTextWriterEndDTDAttlist(writer);
  3170. if (count == -1)
  3171. return -1;
  3172. sum += count;
  3173. return sum;
  3174. }
  3175. /**
  3176. * xmlTextWriterStartDTDEntity:
  3177. * @writer: the xmlTextWriterPtr
  3178. * @pe: TRUE if this is a parameter entity, FALSE if not
  3179. * @name: the name of the DTD ATTLIST
  3180. *
  3181. * Start an xml DTD ATTLIST.
  3182. *
  3183. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3184. */
  3185. int
  3186. xmlTextWriterStartDTDEntity(xmlTextWriterPtr writer,
  3187. int pe, const xmlChar * name)
  3188. {
  3189. int count;
  3190. int sum;
  3191. xmlLinkPtr lk;
  3192. xmlTextWriterStackEntry *p;
  3193. if (writer == NULL || name == NULL || *name == '\0')
  3194. return -1;
  3195. sum = 0;
  3196. lk = xmlListFront(writer->nodes);
  3197. if (lk != 0) {
  3198. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3199. if (p != 0) {
  3200. switch (p->state) {
  3201. case XML_TEXTWRITER_DTD:
  3202. count = xmlOutputBufferWriteString(writer->out, " [");
  3203. if (count < 0)
  3204. return -1;
  3205. sum += count;
  3206. if (writer->indent) {
  3207. count =
  3208. xmlOutputBufferWriteString(writer->out, "\n");
  3209. if (count < 0)
  3210. return -1;
  3211. sum += count;
  3212. }
  3213. p->state = XML_TEXTWRITER_DTD_TEXT;
  3214. /* fallthrough */
  3215. case XML_TEXTWRITER_DTD_TEXT:
  3216. case XML_TEXTWRITER_NONE:
  3217. break;
  3218. default:
  3219. return -1;
  3220. }
  3221. }
  3222. }
  3223. p = (xmlTextWriterStackEntry *)
  3224. xmlMalloc(sizeof(xmlTextWriterStackEntry));
  3225. if (p == 0) {
  3226. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3227. "xmlTextWriterStartDTDElement : out of memory!\n");
  3228. return -1;
  3229. }
  3230. p->name = xmlStrdup(name);
  3231. if (p->name == 0) {
  3232. xmlWriterErrMsg(writer, XML_ERR_NO_MEMORY,
  3233. "xmlTextWriterStartDTDElement : out of memory!\n");
  3234. xmlFree(p);
  3235. return -1;
  3236. }
  3237. if (pe != 0)
  3238. p->state = XML_TEXTWRITER_DTD_PENT;
  3239. else
  3240. p->state = XML_TEXTWRITER_DTD_ENTY;
  3241. xmlListPushFront(writer->nodes, p);
  3242. if (writer->indent) {
  3243. count = xmlTextWriterWriteIndent(writer);
  3244. if (count < 0)
  3245. return -1;
  3246. sum += count;
  3247. }
  3248. count = xmlOutputBufferWriteString(writer->out, "<!ENTITY ");
  3249. if (count < 0)
  3250. return -1;
  3251. sum += count;
  3252. if (pe != 0) {
  3253. count = xmlOutputBufferWriteString(writer->out, "% ");
  3254. if (count < 0)
  3255. return -1;
  3256. sum += count;
  3257. }
  3258. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3259. if (count < 0)
  3260. return -1;
  3261. sum += count;
  3262. return sum;
  3263. }
  3264. /**
  3265. * xmlTextWriterEndDTDEntity:
  3266. * @writer: the xmlTextWriterPtr
  3267. *
  3268. * End an xml DTD entity.
  3269. *
  3270. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3271. */
  3272. int
  3273. xmlTextWriterEndDTDEntity(xmlTextWriterPtr writer)
  3274. {
  3275. int count;
  3276. int sum;
  3277. xmlLinkPtr lk;
  3278. xmlTextWriterStackEntry *p;
  3279. if (writer == NULL)
  3280. return -1;
  3281. sum = 0;
  3282. lk = xmlListFront(writer->nodes);
  3283. if (lk == 0)
  3284. return -1;
  3285. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3286. if (p == 0)
  3287. return -1;
  3288. switch (p->state) {
  3289. case XML_TEXTWRITER_DTD_ENTY_TEXT:
  3290. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3291. if (count < 0)
  3292. return -1;
  3293. sum += count;
  3294. /* Falls through. */
  3295. case XML_TEXTWRITER_DTD_ENTY:
  3296. case XML_TEXTWRITER_DTD_PENT:
  3297. count = xmlOutputBufferWriteString(writer->out, ">");
  3298. if (count < 0)
  3299. return -1;
  3300. sum += count;
  3301. break;
  3302. default:
  3303. return -1;
  3304. }
  3305. if (writer->indent) {
  3306. count = xmlOutputBufferWriteString(writer->out, "\n");
  3307. if (count < 0)
  3308. return -1;
  3309. sum += count;
  3310. }
  3311. xmlListPopFront(writer->nodes);
  3312. return sum;
  3313. }
  3314. /**
  3315. * xmlTextWriterWriteFormatDTDInternalEntity:
  3316. * @writer: the xmlTextWriterPtr
  3317. * @pe: TRUE if this is a parameter entity, FALSE if not
  3318. * @name: the name of the DTD entity
  3319. * @format: format string (see printf)
  3320. * @...: extra parameters for the format
  3321. *
  3322. * Write a formatted DTD internal entity.
  3323. *
  3324. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3325. */
  3326. int XMLCDECL
  3327. xmlTextWriterWriteFormatDTDInternalEntity(xmlTextWriterPtr writer,
  3328. int pe,
  3329. const xmlChar * name,
  3330. const char *format, ...)
  3331. {
  3332. int rc;
  3333. va_list ap;
  3334. va_start(ap, format);
  3335. rc = xmlTextWriterWriteVFormatDTDInternalEntity(writer, pe, name,
  3336. format, ap);
  3337. va_end(ap);
  3338. return rc;
  3339. }
  3340. /**
  3341. * xmlTextWriterWriteVFormatDTDInternalEntity:
  3342. * @writer: the xmlTextWriterPtr
  3343. * @pe: TRUE if this is a parameter entity, FALSE if not
  3344. * @name: the name of the DTD entity
  3345. * @format: format string (see printf)
  3346. * @argptr: pointer to the first member of the variable argument list.
  3347. *
  3348. * Write a formatted DTD internal entity.
  3349. *
  3350. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3351. */
  3352. int
  3353. xmlTextWriterWriteVFormatDTDInternalEntity(xmlTextWriterPtr writer,
  3354. int pe,
  3355. const xmlChar * name,
  3356. const char *format,
  3357. va_list argptr)
  3358. {
  3359. int rc;
  3360. xmlChar *buf;
  3361. if (writer == NULL)
  3362. return -1;
  3363. buf = xmlTextWriterVSprintf(format, argptr);
  3364. if (buf == NULL)
  3365. return -1;
  3366. rc = xmlTextWriterWriteDTDInternalEntity(writer, pe, name, buf);
  3367. xmlFree(buf);
  3368. return rc;
  3369. }
  3370. /**
  3371. * xmlTextWriterWriteDTDEntity:
  3372. * @writer: the xmlTextWriterPtr
  3373. * @pe: TRUE if this is a parameter entity, FALSE if not
  3374. * @name: the name of the DTD entity
  3375. * @pubid: the public identifier, which is an alternative to the system identifier
  3376. * @sysid: the system identifier, which is the URI of the DTD
  3377. * @ndataid: the xml notation name.
  3378. * @content: content of the entity
  3379. *
  3380. * Write a DTD entity.
  3381. *
  3382. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3383. */
  3384. int
  3385. xmlTextWriterWriteDTDEntity(xmlTextWriterPtr writer,
  3386. int pe,
  3387. const xmlChar * name,
  3388. const xmlChar * pubid,
  3389. const xmlChar * sysid,
  3390. const xmlChar * ndataid,
  3391. const xmlChar * content)
  3392. {
  3393. if ((content == NULL) && (pubid == NULL) && (sysid == NULL))
  3394. return -1;
  3395. if ((pe != 0) && (ndataid != NULL))
  3396. return -1;
  3397. if ((pubid == NULL) && (sysid == NULL))
  3398. return xmlTextWriterWriteDTDInternalEntity(writer, pe, name,
  3399. content);
  3400. return xmlTextWriterWriteDTDExternalEntity(writer, pe, name, pubid,
  3401. sysid, ndataid);
  3402. }
  3403. /**
  3404. * xmlTextWriterWriteDTDInternalEntity:
  3405. * @writer: the xmlTextWriterPtr
  3406. * @pe: TRUE if this is a parameter entity, FALSE if not
  3407. * @name: the name of the DTD entity
  3408. * @content: content of the entity
  3409. *
  3410. * Write a DTD internal entity.
  3411. *
  3412. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3413. */
  3414. int
  3415. xmlTextWriterWriteDTDInternalEntity(xmlTextWriterPtr writer,
  3416. int pe,
  3417. const xmlChar * name,
  3418. const xmlChar * content)
  3419. {
  3420. int count;
  3421. int sum;
  3422. if ((name == NULL) || (*name == '\0') || (content == NULL))
  3423. return -1;
  3424. sum = 0;
  3425. count = xmlTextWriterStartDTDEntity(writer, pe, name);
  3426. if (count == -1)
  3427. return -1;
  3428. sum += count;
  3429. count = xmlTextWriterWriteString(writer, content);
  3430. if (count == -1)
  3431. return -1;
  3432. sum += count;
  3433. count = xmlTextWriterEndDTDEntity(writer);
  3434. if (count == -1)
  3435. return -1;
  3436. sum += count;
  3437. return sum;
  3438. }
  3439. /**
  3440. * xmlTextWriterWriteDTDExternalEntity:
  3441. * @writer: the xmlTextWriterPtr
  3442. * @pe: TRUE if this is a parameter entity, FALSE if not
  3443. * @name: the name of the DTD entity
  3444. * @pubid: the public identifier, which is an alternative to the system identifier
  3445. * @sysid: the system identifier, which is the URI of the DTD
  3446. * @ndataid: the xml notation name.
  3447. *
  3448. * Write a DTD external entity. The entity must have been started with xmlTextWriterStartDTDEntity
  3449. *
  3450. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3451. */
  3452. int
  3453. xmlTextWriterWriteDTDExternalEntity(xmlTextWriterPtr writer,
  3454. int pe,
  3455. const xmlChar * name,
  3456. const xmlChar * pubid,
  3457. const xmlChar * sysid,
  3458. const xmlChar * ndataid)
  3459. {
  3460. int count;
  3461. int sum;
  3462. if (((pubid == NULL) && (sysid == NULL)))
  3463. return -1;
  3464. if ((pe != 0) && (ndataid != NULL))
  3465. return -1;
  3466. sum = 0;
  3467. count = xmlTextWriterStartDTDEntity(writer, pe, name);
  3468. if (count == -1)
  3469. return -1;
  3470. sum += count;
  3471. count =
  3472. xmlTextWriterWriteDTDExternalEntityContents(writer, pubid, sysid,
  3473. ndataid);
  3474. if (count < 0)
  3475. return -1;
  3476. sum += count;
  3477. count = xmlTextWriterEndDTDEntity(writer);
  3478. if (count == -1)
  3479. return -1;
  3480. sum += count;
  3481. return sum;
  3482. }
  3483. /**
  3484. * xmlTextWriterWriteDTDExternalEntityContents:
  3485. * @writer: the xmlTextWriterPtr
  3486. * @pubid: the public identifier, which is an alternative to the system identifier
  3487. * @sysid: the system identifier, which is the URI of the DTD
  3488. * @ndataid: the xml notation name.
  3489. *
  3490. * Write the contents of a DTD external entity.
  3491. *
  3492. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3493. */
  3494. int
  3495. xmlTextWriterWriteDTDExternalEntityContents(xmlTextWriterPtr writer,
  3496. const xmlChar * pubid,
  3497. const xmlChar * sysid,
  3498. const xmlChar * ndataid)
  3499. {
  3500. int count;
  3501. int sum;
  3502. xmlLinkPtr lk;
  3503. xmlTextWriterStackEntry *p;
  3504. if (writer == NULL) {
  3505. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3506. "xmlTextWriterWriteDTDExternalEntityContents: xmlTextWriterPtr invalid!\n");
  3507. return -1;
  3508. }
  3509. sum = 0;
  3510. lk = xmlListFront(writer->nodes);
  3511. if (lk == 0) {
  3512. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3513. "xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
  3514. return -1;
  3515. }
  3516. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3517. if (p == 0)
  3518. return -1;
  3519. switch (p->state) {
  3520. case XML_TEXTWRITER_DTD_ENTY:
  3521. break;
  3522. case XML_TEXTWRITER_DTD_PENT:
  3523. if (ndataid != NULL) {
  3524. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3525. "xmlTextWriterWriteDTDExternalEntityContents: notation not allowed with parameter entities!\n");
  3526. return -1;
  3527. }
  3528. break;
  3529. default:
  3530. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3531. "xmlTextWriterWriteDTDExternalEntityContents: you must call xmlTextWriterStartDTDEntity before the call to this function!\n");
  3532. return -1;
  3533. }
  3534. if (pubid != 0) {
  3535. if (sysid == 0) {
  3536. xmlWriterErrMsg(writer, XML_ERR_INTERNAL_ERROR,
  3537. "xmlTextWriterWriteDTDExternalEntityContents: system identifier needed!\n");
  3538. return -1;
  3539. }
  3540. count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
  3541. if (count < 0)
  3542. return -1;
  3543. sum += count;
  3544. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3545. if (count < 0)
  3546. return -1;
  3547. sum += count;
  3548. count =
  3549. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  3550. if (count < 0)
  3551. return -1;
  3552. sum += count;
  3553. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3554. if (count < 0)
  3555. return -1;
  3556. sum += count;
  3557. }
  3558. if (sysid != 0) {
  3559. if (pubid == 0) {
  3560. count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
  3561. if (count < 0)
  3562. return -1;
  3563. sum += count;
  3564. }
  3565. count = xmlOutputBufferWriteString(writer->out, " ");
  3566. if (count < 0)
  3567. return -1;
  3568. sum += count;
  3569. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3570. if (count < 0)
  3571. return -1;
  3572. sum += count;
  3573. count =
  3574. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  3575. if (count < 0)
  3576. return -1;
  3577. sum += count;
  3578. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3579. if (count < 0)
  3580. return -1;
  3581. sum += count;
  3582. }
  3583. if (ndataid != NULL) {
  3584. count = xmlOutputBufferWriteString(writer->out, " NDATA ");
  3585. if (count < 0)
  3586. return -1;
  3587. sum += count;
  3588. count =
  3589. xmlOutputBufferWriteString(writer->out,
  3590. (const char *) ndataid);
  3591. if (count < 0)
  3592. return -1;
  3593. sum += count;
  3594. }
  3595. return sum;
  3596. }
  3597. /**
  3598. * xmlTextWriterWriteDTDNotation:
  3599. * @writer: the xmlTextWriterPtr
  3600. * @name: the name of the xml notation
  3601. * @pubid: the public identifier, which is an alternative to the system identifier
  3602. * @sysid: the system identifier, which is the URI of the DTD
  3603. *
  3604. * Write a DTD entity.
  3605. *
  3606. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3607. */
  3608. int
  3609. xmlTextWriterWriteDTDNotation(xmlTextWriterPtr writer,
  3610. const xmlChar * name,
  3611. const xmlChar * pubid, const xmlChar * sysid)
  3612. {
  3613. int count;
  3614. int sum;
  3615. xmlLinkPtr lk;
  3616. xmlTextWriterStackEntry *p;
  3617. if (writer == NULL || name == NULL || *name == '\0')
  3618. return -1;
  3619. sum = 0;
  3620. lk = xmlListFront(writer->nodes);
  3621. if (lk == 0) {
  3622. return -1;
  3623. }
  3624. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3625. if (p != 0) {
  3626. switch (p->state) {
  3627. case XML_TEXTWRITER_DTD:
  3628. count = xmlOutputBufferWriteString(writer->out, " [");
  3629. if (count < 0)
  3630. return -1;
  3631. sum += count;
  3632. if (writer->indent) {
  3633. count = xmlOutputBufferWriteString(writer->out, "\n");
  3634. if (count < 0)
  3635. return -1;
  3636. sum += count;
  3637. }
  3638. p->state = XML_TEXTWRITER_DTD_TEXT;
  3639. /* fallthrough */
  3640. case XML_TEXTWRITER_DTD_TEXT:
  3641. break;
  3642. default:
  3643. return -1;
  3644. }
  3645. }
  3646. if (writer->indent) {
  3647. count = xmlTextWriterWriteIndent(writer);
  3648. if (count < 0)
  3649. return -1;
  3650. sum += count;
  3651. }
  3652. count = xmlOutputBufferWriteString(writer->out, "<!NOTATION ");
  3653. if (count < 0)
  3654. return -1;
  3655. sum += count;
  3656. count = xmlOutputBufferWriteString(writer->out, (const char *) name);
  3657. if (count < 0)
  3658. return -1;
  3659. sum += count;
  3660. if (pubid != 0) {
  3661. count = xmlOutputBufferWriteString(writer->out, " PUBLIC ");
  3662. if (count < 0)
  3663. return -1;
  3664. sum += count;
  3665. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3666. if (count < 0)
  3667. return -1;
  3668. sum += count;
  3669. count =
  3670. xmlOutputBufferWriteString(writer->out, (const char *) pubid);
  3671. if (count < 0)
  3672. return -1;
  3673. sum += count;
  3674. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3675. if (count < 0)
  3676. return -1;
  3677. sum += count;
  3678. }
  3679. if (sysid != 0) {
  3680. if (pubid == 0) {
  3681. count = xmlOutputBufferWriteString(writer->out, " SYSTEM");
  3682. if (count < 0)
  3683. return -1;
  3684. sum += count;
  3685. }
  3686. count = xmlOutputBufferWriteString(writer->out, " ");
  3687. if (count < 0)
  3688. return -1;
  3689. sum += count;
  3690. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3691. if (count < 0)
  3692. return -1;
  3693. sum += count;
  3694. count =
  3695. xmlOutputBufferWriteString(writer->out, (const char *) sysid);
  3696. if (count < 0)
  3697. return -1;
  3698. sum += count;
  3699. count = xmlOutputBufferWrite(writer->out, 1, &writer->qchar);
  3700. if (count < 0)
  3701. return -1;
  3702. sum += count;
  3703. }
  3704. count = xmlOutputBufferWriteString(writer->out, ">");
  3705. if (count < 0)
  3706. return -1;
  3707. sum += count;
  3708. return sum;
  3709. }
  3710. /**
  3711. * xmlTextWriterFlush:
  3712. * @writer: the xmlTextWriterPtr
  3713. *
  3714. * Flush the output buffer.
  3715. *
  3716. * Returns the bytes written (may be 0 because of buffering) or -1 in case of error
  3717. */
  3718. int
  3719. xmlTextWriterFlush(xmlTextWriterPtr writer)
  3720. {
  3721. int count;
  3722. if (writer == NULL)
  3723. return -1;
  3724. if (writer->out == NULL)
  3725. count = 0;
  3726. else
  3727. count = xmlOutputBufferFlush(writer->out);
  3728. return count;
  3729. }
  3730. /**
  3731. * misc
  3732. */
  3733. /**
  3734. * xmlFreeTextWriterStackEntry:
  3735. * @lk: the xmlLinkPtr
  3736. *
  3737. * Free callback for the xmlList.
  3738. */
  3739. static void
  3740. xmlFreeTextWriterStackEntry(xmlLinkPtr lk)
  3741. {
  3742. xmlTextWriterStackEntry *p;
  3743. p = (xmlTextWriterStackEntry *) xmlLinkGetData(lk);
  3744. if (p == 0)
  3745. return;
  3746. if (p->name != 0)
  3747. xmlFree(p->name);
  3748. xmlFree(p);
  3749. }
  3750. /**
  3751. * xmlCmpTextWriterStackEntry:
  3752. * @data0: the first data
  3753. * @data1: the second data
  3754. *
  3755. * Compare callback for the xmlList.
  3756. *
  3757. * Returns -1, 0, 1
  3758. */
  3759. static int
  3760. xmlCmpTextWriterStackEntry(const void *data0, const void *data1)
  3761. {
  3762. xmlTextWriterStackEntry *p0;
  3763. xmlTextWriterStackEntry *p1;
  3764. if (data0 == data1)
  3765. return 0;
  3766. if (data0 == 0)
  3767. return -1;
  3768. if (data1 == 0)
  3769. return 1;
  3770. p0 = (xmlTextWriterStackEntry *) data0;
  3771. p1 = (xmlTextWriterStackEntry *) data1;
  3772. return xmlStrcmp(p0->name, p1->name);
  3773. }
  3774. /**
  3775. * misc
  3776. */
  3777. /**
  3778. * xmlTextWriterOutputNSDecl:
  3779. * @writer: the xmlTextWriterPtr
  3780. *
  3781. * Output the current namespace declarations.
  3782. */
  3783. static int
  3784. xmlTextWriterOutputNSDecl(xmlTextWriterPtr writer)
  3785. {
  3786. xmlLinkPtr lk;
  3787. xmlTextWriterNsStackEntry *np;
  3788. int count;
  3789. int sum;
  3790. sum = 0;
  3791. while (!xmlListEmpty(writer->nsstack)) {
  3792. xmlChar *namespaceURI = NULL;
  3793. xmlChar *prefix = NULL;
  3794. lk = xmlListFront(writer->nsstack);
  3795. np = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
  3796. if (np != 0) {
  3797. namespaceURI = xmlStrdup(np->uri);
  3798. prefix = xmlStrdup(np->prefix);
  3799. }
  3800. xmlListPopFront(writer->nsstack);
  3801. if (np != 0) {
  3802. count = xmlTextWriterWriteAttribute(writer, prefix, namespaceURI);
  3803. xmlFree(namespaceURI);
  3804. xmlFree(prefix);
  3805. if (count < 0) {
  3806. xmlListDelete(writer->nsstack);
  3807. writer->nsstack = NULL;
  3808. return -1;
  3809. }
  3810. sum += count;
  3811. }
  3812. }
  3813. return sum;
  3814. }
  3815. /**
  3816. * xmlFreeTextWriterNsStackEntry:
  3817. * @lk: the xmlLinkPtr
  3818. *
  3819. * Free callback for the xmlList.
  3820. */
  3821. static void
  3822. xmlFreeTextWriterNsStackEntry(xmlLinkPtr lk)
  3823. {
  3824. xmlTextWriterNsStackEntry *p;
  3825. p = (xmlTextWriterNsStackEntry *) xmlLinkGetData(lk);
  3826. if (p == 0)
  3827. return;
  3828. if (p->prefix != 0)
  3829. xmlFree(p->prefix);
  3830. if (p->uri != 0)
  3831. xmlFree(p->uri);
  3832. xmlFree(p);
  3833. }
  3834. /**
  3835. * xmlCmpTextWriterNsStackEntry:
  3836. * @data0: the first data
  3837. * @data1: the second data
  3838. *
  3839. * Compare callback for the xmlList.
  3840. *
  3841. * Returns -1, 0, 1
  3842. */
  3843. static int
  3844. xmlCmpTextWriterNsStackEntry(const void *data0, const void *data1)
  3845. {
  3846. xmlTextWriterNsStackEntry *p0;
  3847. xmlTextWriterNsStackEntry *p1;
  3848. int rc;
  3849. if (data0 == data1)
  3850. return 0;
  3851. if (data0 == 0)
  3852. return -1;
  3853. if (data1 == 0)
  3854. return 1;
  3855. p0 = (xmlTextWriterNsStackEntry *) data0;
  3856. p1 = (xmlTextWriterNsStackEntry *) data1;
  3857. rc = xmlStrcmp(p0->prefix, p1->prefix);
  3858. if ((rc != 0) || (p0->elem != p1->elem))
  3859. rc = -1;
  3860. return rc;
  3861. }
  3862. /**
  3863. * xmlTextWriterWriteDocCallback:
  3864. * @context: the xmlBufferPtr
  3865. * @str: the data to write
  3866. * @len: the length of the data
  3867. *
  3868. * Write callback for the xmlOutputBuffer with target xmlBuffer
  3869. *
  3870. * Returns -1, 0, 1
  3871. */
  3872. static int
  3873. xmlTextWriterWriteDocCallback(void *context, const char *str, int len)
  3874. {
  3875. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
  3876. int rc;
  3877. if ((rc = xmlParseChunk(ctxt, str, len, 0)) != 0) {
  3878. xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
  3879. "xmlTextWriterWriteDocCallback : XML error %d !\n",
  3880. rc);
  3881. return -1;
  3882. }
  3883. return len;
  3884. }
  3885. /**
  3886. * xmlTextWriterCloseDocCallback:
  3887. * @context: the xmlBufferPtr
  3888. *
  3889. * Close callback for the xmlOutputBuffer with target xmlBuffer
  3890. *
  3891. * Returns -1, 0, 1
  3892. */
  3893. static int
  3894. xmlTextWriterCloseDocCallback(void *context)
  3895. {
  3896. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) context;
  3897. int rc;
  3898. if ((rc = xmlParseChunk(ctxt, NULL, 0, 1)) != 0) {
  3899. xmlWriterErrMsgInt(NULL, XML_ERR_INTERNAL_ERROR,
  3900. "xmlTextWriterCloseDocCallback : XML error %d !\n",
  3901. rc);
  3902. return -1;
  3903. }
  3904. return 0;
  3905. }
  3906. /**
  3907. * xmlTextWriterVSprintf:
  3908. * @format: see printf
  3909. * @argptr: pointer to the first member of the variable argument list.
  3910. *
  3911. * Utility function for formatted output
  3912. *
  3913. * Returns a new xmlChar buffer with the data or NULL on error. This buffer must be freed.
  3914. */
  3915. static xmlChar *
  3916. xmlTextWriterVSprintf(const char *format, va_list argptr)
  3917. {
  3918. int size;
  3919. int count;
  3920. xmlChar *buf;
  3921. va_list locarg;
  3922. size = BUFSIZ;
  3923. buf = (xmlChar *) xmlMalloc(size);
  3924. if (buf == NULL) {
  3925. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  3926. "xmlTextWriterVSprintf : out of memory!\n");
  3927. return NULL;
  3928. }
  3929. VA_COPY(locarg, argptr);
  3930. while (((count = vsnprintf((char *) buf, size, format, locarg)) < 0)
  3931. || (count == size - 1) || (count == size) || (count > size)) {
  3932. va_end(locarg);
  3933. xmlFree(buf);
  3934. size += BUFSIZ;
  3935. buf = (xmlChar *) xmlMalloc(size);
  3936. if (buf == NULL) {
  3937. xmlWriterErrMsg(NULL, XML_ERR_NO_MEMORY,
  3938. "xmlTextWriterVSprintf : out of memory!\n");
  3939. return NULL;
  3940. }
  3941. VA_COPY(locarg, argptr);
  3942. }
  3943. va_end(locarg);
  3944. return buf;
  3945. }
  3946. /**
  3947. * xmlTextWriterStartDocumentCallback:
  3948. * @ctx: the user data (XML parser context)
  3949. *
  3950. * called at the start of document processing.
  3951. */
  3952. static void
  3953. xmlTextWriterStartDocumentCallback(void *ctx)
  3954. {
  3955. xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
  3956. xmlDocPtr doc;
  3957. if (ctxt->html) {
  3958. #ifdef LIBXML_HTML_ENABLED
  3959. if (ctxt->myDoc == NULL)
  3960. ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
  3961. if (ctxt->myDoc == NULL) {
  3962. if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
  3963. ctxt->sax->error(ctxt->userData,
  3964. "SAX.startDocument(): out of memory\n");
  3965. ctxt->errNo = XML_ERR_NO_MEMORY;
  3966. ctxt->instate = XML_PARSER_EOF;
  3967. ctxt->disableSAX = 1;
  3968. return;
  3969. }
  3970. #else
  3971. xmlWriterErrMsg(NULL, XML_ERR_INTERNAL_ERROR,
  3972. "libxml2 built without HTML support\n");
  3973. ctxt->errNo = XML_ERR_INTERNAL_ERROR;
  3974. ctxt->instate = XML_PARSER_EOF;
  3975. ctxt->disableSAX = 1;
  3976. return;
  3977. #endif
  3978. } else {
  3979. doc = ctxt->myDoc;
  3980. if (doc == NULL)
  3981. doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
  3982. if (doc != NULL) {
  3983. if (doc->children == NULL) {
  3984. if (ctxt->encoding != NULL)
  3985. doc->encoding = xmlStrdup(ctxt->encoding);
  3986. else
  3987. doc->encoding = NULL;
  3988. doc->standalone = ctxt->standalone;
  3989. }
  3990. } else {
  3991. if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
  3992. ctxt->sax->error(ctxt->userData,
  3993. "SAX.startDocument(): out of memory\n");
  3994. ctxt->errNo = XML_ERR_NO_MEMORY;
  3995. ctxt->instate = XML_PARSER_EOF;
  3996. ctxt->disableSAX = 1;
  3997. return;
  3998. }
  3999. }
  4000. if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
  4001. (ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
  4002. ctxt->myDoc->URL =
  4003. xmlCanonicPath((const xmlChar *) ctxt->input->filename);
  4004. if (ctxt->myDoc->URL == NULL)
  4005. ctxt->myDoc->URL =
  4006. xmlStrdup((const xmlChar *) ctxt->input->filename);
  4007. }
  4008. }
  4009. /**
  4010. * xmlTextWriterSetIndent:
  4011. * @writer: the xmlTextWriterPtr
  4012. * @indent: do indentation?
  4013. *
  4014. * Set indentation output. indent = 0 do not indentation. indent > 0 do indentation.
  4015. *
  4016. * Returns -1 on error or 0 otherwise.
  4017. */
  4018. int
  4019. xmlTextWriterSetIndent(xmlTextWriterPtr writer, int indent)
  4020. {
  4021. if ((writer == NULL) || (indent < 0))
  4022. return -1;
  4023. writer->indent = indent;
  4024. writer->doindent = 1;
  4025. return 0;
  4026. }
  4027. /**
  4028. * xmlTextWriterSetIndentString:
  4029. * @writer: the xmlTextWriterPtr
  4030. * @str: the xmlChar string
  4031. *
  4032. * Set string indentation.
  4033. *
  4034. * Returns -1 on error or 0 otherwise.
  4035. */
  4036. int
  4037. xmlTextWriterSetIndentString(xmlTextWriterPtr writer, const xmlChar * str)
  4038. {
  4039. if ((writer == NULL) || (!str))
  4040. return -1;
  4041. if (writer->ichar != NULL)
  4042. xmlFree(writer->ichar);
  4043. writer->ichar = xmlStrdup(str);
  4044. if (!writer->ichar)
  4045. return -1;
  4046. else
  4047. return 0;
  4048. }
  4049. /**
  4050. * xmlTextWriterSetQuoteChar:
  4051. * @writer: the xmlTextWriterPtr
  4052. * @quotechar: the quote character
  4053. *
  4054. * Set the character used for quoting attributes.
  4055. *
  4056. * Returns -1 on error or 0 otherwise.
  4057. */
  4058. int
  4059. xmlTextWriterSetQuoteChar(xmlTextWriterPtr writer, xmlChar quotechar)
  4060. {
  4061. if ((writer == NULL) || ((quotechar != '\'') && (quotechar != '"')))
  4062. return -1;
  4063. writer->qchar = quotechar;
  4064. return 0;
  4065. }
  4066. /**
  4067. * xmlTextWriterWriteIndent:
  4068. * @writer: the xmlTextWriterPtr
  4069. *
  4070. * Write indent string.
  4071. *
  4072. * Returns -1 on error or the number of strings written.
  4073. */
  4074. static int
  4075. xmlTextWriterWriteIndent(xmlTextWriterPtr writer)
  4076. {
  4077. int lksize;
  4078. int i;
  4079. int ret;
  4080. lksize = xmlListSize(writer->nodes);
  4081. if (lksize < 1)
  4082. return (-1); /* list is empty */
  4083. for (i = 0; i < (lksize - 1); i++) {
  4084. ret = xmlOutputBufferWriteString(writer->out,
  4085. (const char *) writer->ichar);
  4086. if (ret == -1)
  4087. return (-1);
  4088. }
  4089. return (lksize - 1);
  4090. }
  4091. /**
  4092. * xmlTextWriterHandleStateDependencies:
  4093. * @writer: the xmlTextWriterPtr
  4094. * @p: the xmlTextWriterStackEntry
  4095. *
  4096. * Write state dependent strings.
  4097. *
  4098. * Returns -1 on error or the number of characters written.
  4099. */
  4100. static int
  4101. xmlTextWriterHandleStateDependencies(xmlTextWriterPtr writer,
  4102. xmlTextWriterStackEntry * p)
  4103. {
  4104. int count;
  4105. int sum;
  4106. char extra[3];
  4107. if (writer == NULL)
  4108. return -1;
  4109. if (p == NULL)
  4110. return 0;
  4111. sum = 0;
  4112. extra[0] = extra[1] = extra[2] = '\0';
  4113. if (p != 0) {
  4114. sum = 0;
  4115. switch (p->state) {
  4116. case XML_TEXTWRITER_NAME:
  4117. /* Output namespace declarations */
  4118. count = xmlTextWriterOutputNSDecl(writer);
  4119. if (count < 0)
  4120. return -1;
  4121. sum += count;
  4122. extra[0] = '>';
  4123. p->state = XML_TEXTWRITER_TEXT;
  4124. break;
  4125. case XML_TEXTWRITER_PI:
  4126. extra[0] = ' ';
  4127. p->state = XML_TEXTWRITER_PI_TEXT;
  4128. break;
  4129. case XML_TEXTWRITER_DTD:
  4130. extra[0] = ' ';
  4131. extra[1] = '[';
  4132. p->state = XML_TEXTWRITER_DTD_TEXT;
  4133. break;
  4134. case XML_TEXTWRITER_DTD_ELEM:
  4135. extra[0] = ' ';
  4136. p->state = XML_TEXTWRITER_DTD_ELEM_TEXT;
  4137. break;
  4138. case XML_TEXTWRITER_DTD_ATTL:
  4139. extra[0] = ' ';
  4140. p->state = XML_TEXTWRITER_DTD_ATTL_TEXT;
  4141. break;
  4142. case XML_TEXTWRITER_DTD_ENTY:
  4143. case XML_TEXTWRITER_DTD_PENT:
  4144. extra[0] = ' ';
  4145. extra[1] = writer->qchar;
  4146. p->state = XML_TEXTWRITER_DTD_ENTY_TEXT;
  4147. break;
  4148. default:
  4149. break;
  4150. }
  4151. }
  4152. if (*extra != '\0') {
  4153. count = xmlOutputBufferWriteString(writer->out, extra);
  4154. if (count < 0)
  4155. return -1;
  4156. sum += count;
  4157. }
  4158. return sum;
  4159. }
  4160. #define bottom_xmlwriter
  4161. #include "elfgcchack.h"
  4162. #endif