mxml-file.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154
  1. /*
  2. * File loading code for Mini-XML, a small XML file parsing library.
  3. *
  4. * https://www.msweet.org/mxml
  5. *
  6. * Copyright © 2003-2020 by Michael R Sweet.
  7. *
  8. * Licensed under Apache License v2.0. See the file "LICENSE" for more
  9. * information.
  10. */
  11. /*
  12. * Include necessary headers...
  13. */
  14. #ifndef _WIN32
  15. # include <unistd.h>
  16. #endif /* !_WIN32 */
  17. #include "mxml-private.h"
  18. /*
  19. * Character encoding...
  20. */
  21. #define ENCODE_UTF8 0 /* UTF-8 */
  22. #define ENCODE_UTF16BE 1 /* UTF-16 Big-Endian */
  23. #define ENCODE_UTF16LE 2 /* UTF-16 Little-Endian */
  24. /*
  25. * Macro to test for a bad XML character...
  26. */
  27. #define mxml_bad_char(ch) ((ch) < ' ' && (ch) != '\n' && (ch) != '\r' && (ch) != '\t')
  28. /*
  29. * Types and structures...
  30. */
  31. typedef int (*_mxml_getc_cb_t)(void *, int *);
  32. typedef int (*_mxml_putc_cb_t)(int, void *);
  33. typedef struct _mxml_fdbuf_s /**** File descriptor buffer ****/
  34. {
  35. int fd; /* File descriptor */
  36. unsigned char *current, /* Current position in buffer */
  37. *end, /* End of buffer */
  38. buffer[8192]; /* Character buffer */
  39. } _mxml_fdbuf_t;
  40. /*
  41. * Local functions...
  42. */
  43. static int mxml_add_char(int ch, char **ptr, char **buffer, int *bufsize);
  44. static int mxml_fd_getc(void *p, int *encoding);
  45. static int mxml_fd_putc(int ch, void *p);
  46. static int mxml_fd_read(_mxml_fdbuf_t *buf);
  47. static int mxml_fd_write(_mxml_fdbuf_t *buf);
  48. static int mxml_file_getc(void *p, int *encoding);
  49. static int mxml_file_putc(int ch, void *p);
  50. static int mxml_get_entity(mxml_node_t *parent, void *p, int *encoding, _mxml_getc_cb_t getc_cb, int *line);
  51. static inline int mxml_isspace(int ch)
  52. {
  53. return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
  54. }
  55. static mxml_node_t *mxml_load_data(mxml_node_t *top, void *p, mxml_load_cb_t cb, _mxml_getc_cb_t getc_cb, mxml_sax_cb_t sax_cb, void *sax_data);
  56. static int mxml_parse_element(mxml_node_t *node, void *p, int *encoding, _mxml_getc_cb_t getc_cb, int *line);
  57. static int mxml_string_getc(void *p, int *encoding);
  58. static int mxml_string_putc(int ch, void *p);
  59. static int mxml_write_name(const char *s, void *p, _mxml_putc_cb_t putc_cb);
  60. static int mxml_write_node(mxml_node_t *node, void *p, mxml_save_cb_t cb, int col, _mxml_putc_cb_t putc_cb, _mxml_global_t *global);
  61. static int mxml_write_string(const char *s, void *p, _mxml_putc_cb_t putc_cb);
  62. static int mxml_write_ws(mxml_node_t *node, void *p, mxml_save_cb_t cb, int ws, int col, _mxml_putc_cb_t putc_cb);
  63. /*
  64. * 'mxmlLoadFd()' - Load a file descriptor into an XML node tree.
  65. *
  66. * The nodes in the specified file are added to the specified top node.
  67. * If no top node is provided, the XML file MUST be well-formed with a
  68. * single parent node like <?xml> for the entire file. The callback
  69. * function returns the value type that should be used for child nodes.
  70. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  71. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  72. * loading child (data) nodes of the specified type.
  73. *
  74. * Note: The most common programming error when using the Mini-XML library is
  75. * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
  76. * text as a series of whitespace-delimited words, instead of using the
  77. * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
  78. * (including whitespace).
  79. */
  80. mxml_node_t * /* O - First node or @code NULL@ if the file could not be read. */
  81. mxmlLoadFd(mxml_node_t *top, /* I - Top node */
  82. int fd, /* I - File descriptor to read from */
  83. mxml_load_cb_t cb) /* I - Callback function or constant */
  84. {
  85. _mxml_fdbuf_t buf; /* File descriptor buffer */
  86. /*
  87. * Initialize the file descriptor buffer...
  88. */
  89. buf.fd = fd;
  90. buf.current = buf.buffer;
  91. buf.end = buf.buffer;
  92. /*
  93. * Read the XML data...
  94. */
  95. return (mxml_load_data(top, &buf, cb, mxml_fd_getc, MXML_NO_CALLBACK, NULL));
  96. }
  97. /*
  98. * 'mxmlLoadFile()' - Load a file into an XML node tree.
  99. *
  100. * The nodes in the specified file are added to the specified top node.
  101. * If no top node is provided, the XML file MUST be well-formed with a
  102. * single parent node like <?xml> for the entire file. The callback
  103. * function returns the value type that should be used for child nodes.
  104. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  105. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  106. * loading child (data) nodes of the specified type.
  107. *
  108. * Note: The most common programming error when using the Mini-XML library is
  109. * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
  110. * text as a series of whitespace-delimited words, instead of using the
  111. * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
  112. * (including whitespace).
  113. */
  114. mxml_node_t * /* O - First node or @code NULL@ if the file could not be read. */
  115. mxmlLoadFile(mxml_node_t *top, /* I - Top node */
  116. FILE *fp, /* I - File to read from */
  117. mxml_load_cb_t cb) /* I - Callback function or constant */
  118. {
  119. /*
  120. * Read the XML data...
  121. */
  122. return (mxml_load_data(top, fp, cb, mxml_file_getc, MXML_NO_CALLBACK, NULL));
  123. }
  124. /*
  125. * 'mxmlLoadString()' - Load a string into an XML node tree.
  126. *
  127. * The nodes in the specified string are added to the specified top node.
  128. * If no top node is provided, the XML string MUST be well-formed with a
  129. * single parent node like <?xml> for the entire string. The callback
  130. * function returns the value type that should be used for child nodes.
  131. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  132. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  133. * loading child (data) nodes of the specified type.
  134. *
  135. * Note: The most common programming error when using the Mini-XML library is
  136. * to load an XML file using the @code MXML_TEXT_CALLBACK@, which returns inline
  137. * text as a series of whitespace-delimited words, instead of using the
  138. * @code MXML_OPAQUE_CALLBACK@ which returns the inline text as a single string
  139. * (including whitespace).
  140. */
  141. mxml_node_t * /* O - First node or @code NULL@ if the string has errors. */
  142. mxmlLoadString(mxml_node_t *top, /* I - Top node */
  143. const char *s, /* I - String to load */
  144. mxml_load_cb_t cb) /* I - Callback function or constant */
  145. {
  146. /*
  147. * Read the XML data...
  148. */
  149. return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, MXML_NO_CALLBACK,
  150. NULL));
  151. }
  152. /*
  153. * 'mxmlSaveAllocString()' - Save an XML tree to an allocated string.
  154. *
  155. * This function returns a pointer to a string containing the textual
  156. * representation of the XML node tree. The string should be freed
  157. * using the free() function when you are done with it. @code NULL@ is returned
  158. * if the node would produce an empty string or if the string cannot be
  159. * allocated.
  160. *
  161. * The callback argument specifies a function that returns a whitespace
  162. * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
  163. * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
  164. * with leading whitespace and before attribute names inside opening
  165. * element tags.
  166. */
  167. char * /* O - Allocated string or @code NULL@ */
  168. mxmlSaveAllocString(
  169. mxml_node_t *node, /* I - Node to write */
  170. mxml_save_cb_t cb) /* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
  171. {
  172. int bytes; /* Required bytes */
  173. char buffer[8192]; /* Temporary buffer */
  174. char *s; /* Allocated string */
  175. /*
  176. * Write the node to the temporary buffer...
  177. */
  178. bytes = mxmlSaveString(node, buffer, sizeof(buffer), cb);
  179. if (bytes <= 0)
  180. return (NULL);
  181. if (bytes < (int)(sizeof(buffer) - 1))
  182. {
  183. /*
  184. * Node fit inside the buffer, so just duplicate that string and
  185. * return...
  186. */
  187. return (strdup(buffer));
  188. }
  189. /*
  190. * Allocate a buffer of the required size and save the node to the
  191. * new buffer...
  192. */
  193. if ((s = malloc(bytes + 1)) == NULL)
  194. return (NULL);
  195. mxmlSaveString(node, s, bytes + 1, cb);
  196. /*
  197. * Return the allocated string...
  198. */
  199. return (s);
  200. }
  201. /*
  202. * 'mxmlSaveFd()' - Save an XML tree to a file descriptor.
  203. *
  204. * The callback argument specifies a function that returns a whitespace
  205. * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
  206. * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
  207. * with leading whitespace and before attribute names inside opening
  208. * element tags.
  209. */
  210. int /* O - 0 on success, -1 on error. */
  211. mxmlSaveFd(mxml_node_t *node, /* I - Node to write */
  212. int fd, /* I - File descriptor to write to */
  213. mxml_save_cb_t cb) /* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
  214. {
  215. int col; /* Final column */
  216. _mxml_fdbuf_t buf; /* File descriptor buffer */
  217. _mxml_global_t *global = _mxml_global();
  218. /* Global data */
  219. /*
  220. * Initialize the file descriptor buffer...
  221. */
  222. buf.fd = fd;
  223. buf.current = buf.buffer;
  224. buf.end = buf.buffer + sizeof(buf.buffer);
  225. /*
  226. * Write the node...
  227. */
  228. if ((col = mxml_write_node(node, &buf, cb, 0, mxml_fd_putc, global)) < 0)
  229. return (-1);
  230. if (col > 0)
  231. if (mxml_fd_putc('\n', &buf) < 0)
  232. return (-1);
  233. /*
  234. * Flush and return...
  235. */
  236. return (mxml_fd_write(&buf));
  237. }
  238. /*
  239. * 'mxmlSaveFile()' - Save an XML tree to a file.
  240. *
  241. * The callback argument specifies a function that returns a whitespace
  242. * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
  243. * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
  244. * with leading whitespace and before attribute names inside opening
  245. * element tags.
  246. */
  247. int /* O - 0 on success, -1 on error. */
  248. mxmlSaveFile(mxml_node_t *node, /* I - Node to write */
  249. FILE *fp, /* I - File to write to */
  250. mxml_save_cb_t cb) /* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
  251. {
  252. int col; /* Final column */
  253. _mxml_global_t *global = _mxml_global();
  254. /* Global data */
  255. /*
  256. * Write the node...
  257. */
  258. if ((col = mxml_write_node(node, fp, cb, 0, mxml_file_putc, global)) < 0)
  259. return (-1);
  260. if (col > 0)
  261. if (putc('\n', fp) < 0)
  262. return (-1);
  263. /*
  264. * Return 0 (success)...
  265. */
  266. return (0);
  267. }
  268. /*
  269. * 'mxmlSaveString()' - Save an XML node tree to a string.
  270. *
  271. * This function returns the total number of bytes that would be
  272. * required for the string but only copies (bufsize - 1) characters
  273. * into the specified buffer.
  274. *
  275. * The callback argument specifies a function that returns a whitespace
  276. * string or NULL before and after each element. If @code MXML_NO_CALLBACK@
  277. * is specified, whitespace will only be added before @code MXML_TEXT@ nodes
  278. * with leading whitespace and before attribute names inside opening
  279. * element tags.
  280. */
  281. int /* O - Size of string */
  282. mxmlSaveString(mxml_node_t *node, /* I - Node to write */
  283. char *buffer, /* I - String buffer */
  284. int bufsize, /* I - Size of string buffer */
  285. mxml_save_cb_t cb) /* I - Whitespace callback or @code MXML_NO_CALLBACK@ */
  286. {
  287. int col; /* Final column */
  288. char *ptr[2]; /* Pointers for putc_cb */
  289. _mxml_global_t *global = _mxml_global();
  290. /* Global data */
  291. /*
  292. * Write the node...
  293. */
  294. ptr[0] = buffer;
  295. ptr[1] = buffer + bufsize;
  296. if ((col = mxml_write_node(node, ptr, cb, 0, mxml_string_putc, global)) < 0)
  297. return (-1);
  298. if (col > 0)
  299. mxml_string_putc('\n', ptr);
  300. /*
  301. * Nul-terminate the buffer...
  302. */
  303. if (ptr[0] >= ptr[1])
  304. buffer[bufsize - 1] = '\0';
  305. else
  306. ptr[0][0] = '\0';
  307. /*
  308. * Return the number of characters...
  309. */
  310. return ((int)(ptr[0] - buffer));
  311. }
  312. /*
  313. * 'mxmlSAXLoadFd()' - Load a file descriptor into an XML node tree
  314. * using a SAX callback.
  315. *
  316. * The nodes in the specified file are added to the specified top node.
  317. * If no top node is provided, the XML file MUST be well-formed with a
  318. * single parent node like <?xml> for the entire file. The callback
  319. * function returns the value type that should be used for child nodes.
  320. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  321. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  322. * loading child nodes of the specified type.
  323. *
  324. * The SAX callback must call @link mxmlRetain@ for any nodes that need to
  325. * be kept for later use. Otherwise, nodes are deleted when the parent
  326. * node is closed or after each data, comment, CDATA, or directive node.
  327. *
  328. * @since Mini-XML 2.3@
  329. */
  330. mxml_node_t * /* O - First node or @code NULL@ if the file could not be read. */
  331. mxmlSAXLoadFd(mxml_node_t *top, /* I - Top node */
  332. int fd, /* I - File descriptor to read from */
  333. mxml_load_cb_t cb, /* I - Callback function or constant */
  334. mxml_sax_cb_t sax_cb, /* I - SAX callback or @code MXML_NO_CALLBACK@ */
  335. void *sax_data) /* I - SAX user data */
  336. {
  337. _mxml_fdbuf_t buf; /* File descriptor buffer */
  338. /*
  339. * Initialize the file descriptor buffer...
  340. */
  341. buf.fd = fd;
  342. buf.current = buf.buffer;
  343. buf.end = buf.buffer;
  344. /*
  345. * Read the XML data...
  346. */
  347. return (mxml_load_data(top, &buf, cb, mxml_fd_getc, sax_cb, sax_data));
  348. }
  349. /*
  350. * 'mxmlSAXLoadFile()' - Load a file into an XML node tree
  351. * using a SAX callback.
  352. *
  353. * The nodes in the specified file are added to the specified top node.
  354. * If no top node is provided, the XML file MUST be well-formed with a
  355. * single parent node like <?xml> for the entire file. The callback
  356. * function returns the value type that should be used for child nodes.
  357. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  358. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  359. * loading child nodes of the specified type.
  360. *
  361. * The SAX callback must call @link mxmlRetain@ for any nodes that need to
  362. * be kept for later use. Otherwise, nodes are deleted when the parent
  363. * node is closed or after each data, comment, CDATA, or directive node.
  364. *
  365. * @since Mini-XML 2.3@
  366. */
  367. mxml_node_t * /* O - First node or @code NULL@ if the file could not be read. */
  368. mxmlSAXLoadFile(
  369. mxml_node_t *top, /* I - Top node */
  370. FILE *fp, /* I - File to read from */
  371. mxml_load_cb_t cb, /* I - Callback function or constant */
  372. mxml_sax_cb_t sax_cb, /* I - SAX callback or @code MXML_NO_CALLBACK@ */
  373. void *sax_data) /* I - SAX user data */
  374. {
  375. /*
  376. * Read the XML data...
  377. */
  378. return (mxml_load_data(top, fp, cb, mxml_file_getc, sax_cb, sax_data));
  379. }
  380. /*
  381. * 'mxmlSAXLoadString()' - Load a string into an XML node tree
  382. * using a SAX callback.
  383. *
  384. * The nodes in the specified string are added to the specified top node.
  385. * If no top node is provided, the XML string MUST be well-formed with a
  386. * single parent node like <?xml> for the entire string. The callback
  387. * function returns the value type that should be used for child nodes.
  388. * The constants @code MXML_INTEGER_CALLBACK@, @code MXML_OPAQUE_CALLBACK@,
  389. * @code MXML_REAL_CALLBACK@, and @code MXML_TEXT_CALLBACK@ are defined for
  390. * loading child nodes of the specified type.
  391. *
  392. * The SAX callback must call @link mxmlRetain@ for any nodes that need to
  393. * be kept for later use. Otherwise, nodes are deleted when the parent
  394. * node is closed or after each data, comment, CDATA, or directive node.
  395. *
  396. * @since Mini-XML 2.3@
  397. */
  398. mxml_node_t * /* O - First node or @code NULL@ if the string has errors. */
  399. mxmlSAXLoadString(
  400. mxml_node_t *top, /* I - Top node */
  401. const char *s, /* I - String to load */
  402. mxml_load_cb_t cb, /* I - Callback function or constant */
  403. mxml_sax_cb_t sax_cb, /* I - SAX callback or @code MXML_NO_CALLBACK@ */
  404. void *sax_data) /* I - SAX user data */
  405. {
  406. /*
  407. * Read the XML data...
  408. */
  409. return (mxml_load_data(top, (void *)&s, cb, mxml_string_getc, sax_cb, sax_data));
  410. }
  411. /*
  412. * 'mxmlSetCustomHandlers()' - Set the handling functions for custom data.
  413. *
  414. * The load function accepts a node pointer and a data string and must
  415. * return 0 on success and non-zero on error.
  416. *
  417. * The save function accepts a node pointer and must return a malloc'd
  418. * string on success and @code NULL@ on error.
  419. *
  420. */
  421. void
  422. mxmlSetCustomHandlers(
  423. mxml_custom_load_cb_t load, /* I - Load function */
  424. mxml_custom_save_cb_t save) /* I - Save function */
  425. {
  426. _mxml_global_t *global = _mxml_global();
  427. /* Global data */
  428. global->custom_load_cb = load;
  429. global->custom_save_cb = save;
  430. }
  431. /*
  432. * 'mxmlSetErrorCallback()' - Set the error message callback.
  433. */
  434. void
  435. mxmlSetErrorCallback(mxml_error_cb_t cb)/* I - Error callback function */
  436. {
  437. _mxml_global_t *global = _mxml_global();
  438. /* Global data */
  439. global->error_cb = cb;
  440. }
  441. /*
  442. * 'mxmlSetWrapMargin()' - Set the wrap margin when saving XML data.
  443. *
  444. * Wrapping is disabled when "column" is 0.
  445. *
  446. * @since Mini-XML 2.3@
  447. */
  448. void
  449. mxmlSetWrapMargin(int column) /* I - Column for wrapping, 0 to disable wrapping */
  450. {
  451. _mxml_global_t *global = _mxml_global();
  452. /* Global data */
  453. global->wrap = column;
  454. }
  455. /*
  456. * 'mxml_add_char()' - Add a character to a buffer, expanding as needed.
  457. */
  458. static int /* O - 0 on success, -1 on error */
  459. mxml_add_char(int ch, /* I - Character to add */
  460. char **bufptr, /* IO - Current position in buffer */
  461. char **buffer, /* IO - Current buffer */
  462. int *bufsize) /* IO - Current buffer size */
  463. {
  464. char *newbuffer; /* New buffer value */
  465. if (*bufptr >= (*buffer + *bufsize - 4))
  466. {
  467. /*
  468. * Increase the size of the buffer...
  469. */
  470. if (*bufsize < 1024)
  471. (*bufsize) *= 2;
  472. else
  473. (*bufsize) += 1024;
  474. if ((newbuffer = realloc(*buffer, *bufsize)) == NULL)
  475. {
  476. free(*buffer);
  477. mxml_error("Unable to expand string buffer to %d bytes!", *bufsize);
  478. return (-1);
  479. }
  480. *bufptr = newbuffer + (*bufptr - *buffer);
  481. *buffer = newbuffer;
  482. }
  483. if (ch < 0x80)
  484. {
  485. /*
  486. * Single byte ASCII...
  487. */
  488. *(*bufptr)++ = ch;
  489. }
  490. else if (ch < 0x800)
  491. {
  492. /*
  493. * Two-byte UTF-8...
  494. */
  495. *(*bufptr)++ = 0xc0 | (ch >> 6);
  496. *(*bufptr)++ = 0x80 | (ch & 0x3f);
  497. }
  498. else if (ch < 0x10000)
  499. {
  500. /*
  501. * Three-byte UTF-8...
  502. */
  503. *(*bufptr)++ = 0xe0 | (ch >> 12);
  504. *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
  505. *(*bufptr)++ = 0x80 | (ch & 0x3f);
  506. }
  507. else
  508. {
  509. /*
  510. * Four-byte UTF-8...
  511. */
  512. *(*bufptr)++ = 0xf0 | (ch >> 18);
  513. *(*bufptr)++ = 0x80 | ((ch >> 12) & 0x3f);
  514. *(*bufptr)++ = 0x80 | ((ch >> 6) & 0x3f);
  515. *(*bufptr)++ = 0x80 | (ch & 0x3f);
  516. }
  517. return (0);
  518. }
  519. /*
  520. * 'mxml_fd_getc()' - Read a character from a file descriptor.
  521. */
  522. static int /* O - Character or EOF */
  523. mxml_fd_getc(void *p, /* I - File descriptor buffer */
  524. int *encoding) /* IO - Encoding */
  525. {
  526. _mxml_fdbuf_t *buf; /* File descriptor buffer */
  527. int ch, /* Current character */
  528. temp; /* Temporary character */
  529. /*
  530. * Grab the next character in the buffer...
  531. */
  532. buf = (_mxml_fdbuf_t *)p;
  533. if (buf->current >= buf->end)
  534. if (mxml_fd_read(buf) < 0)
  535. return (EOF);
  536. ch = *(buf->current)++;
  537. switch (*encoding)
  538. {
  539. case ENCODE_UTF8 :
  540. /*
  541. * Got a UTF-8 character; convert UTF-8 to Unicode and return...
  542. */
  543. if (!(ch & 0x80))
  544. {
  545. #if DEBUG > 1
  546. printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  547. #endif /* DEBUG > 1 */
  548. if (mxml_bad_char(ch))
  549. {
  550. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  551. ch);
  552. return (EOF);
  553. }
  554. return (ch);
  555. }
  556. else if (ch == 0xfe)
  557. {
  558. /*
  559. * UTF-16 big-endian BOM?
  560. */
  561. if (buf->current >= buf->end)
  562. if (mxml_fd_read(buf) < 0)
  563. return (EOF);
  564. ch = *(buf->current)++;
  565. if (ch != 0xff)
  566. return (EOF);
  567. *encoding = ENCODE_UTF16BE;
  568. return (mxml_fd_getc(p, encoding));
  569. }
  570. else if (ch == 0xff)
  571. {
  572. /*
  573. * UTF-16 little-endian BOM?
  574. */
  575. if (buf->current >= buf->end)
  576. if (mxml_fd_read(buf) < 0)
  577. return (EOF);
  578. ch = *(buf->current)++;
  579. if (ch != 0xfe)
  580. return (EOF);
  581. *encoding = ENCODE_UTF16LE;
  582. return (mxml_fd_getc(p, encoding));
  583. }
  584. else if ((ch & 0xe0) == 0xc0)
  585. {
  586. /*
  587. * Two-byte value...
  588. */
  589. if (buf->current >= buf->end)
  590. if (mxml_fd_read(buf) < 0)
  591. return (EOF);
  592. temp = *(buf->current)++;
  593. if ((temp & 0xc0) != 0x80)
  594. return (EOF);
  595. ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
  596. if (ch < 0x80)
  597. {
  598. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  599. return (EOF);
  600. }
  601. }
  602. else if ((ch & 0xf0) == 0xe0)
  603. {
  604. /*
  605. * Three-byte value...
  606. */
  607. if (buf->current >= buf->end)
  608. if (mxml_fd_read(buf) < 0)
  609. return (EOF);
  610. temp = *(buf->current)++;
  611. if ((temp & 0xc0) != 0x80)
  612. return (EOF);
  613. ch = ((ch & 0x0f) << 6) | (temp & 0x3f);
  614. if (buf->current >= buf->end)
  615. if (mxml_fd_read(buf) < 0)
  616. return (EOF);
  617. temp = *(buf->current)++;
  618. if ((temp & 0xc0) != 0x80)
  619. return (EOF);
  620. ch = (ch << 6) | (temp & 0x3f);
  621. if (ch < 0x800)
  622. {
  623. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  624. return (EOF);
  625. }
  626. /*
  627. * Ignore (strip) Byte Order Mark (BOM)...
  628. */
  629. if (ch == 0xfeff)
  630. return (mxml_fd_getc(p, encoding));
  631. }
  632. else if ((ch & 0xf8) == 0xf0)
  633. {
  634. /*
  635. * Four-byte value...
  636. */
  637. if (buf->current >= buf->end)
  638. if (mxml_fd_read(buf) < 0)
  639. return (EOF);
  640. temp = *(buf->current)++;
  641. if ((temp & 0xc0) != 0x80)
  642. return (EOF);
  643. ch = ((ch & 0x07) << 6) | (temp & 0x3f);
  644. if (buf->current >= buf->end)
  645. if (mxml_fd_read(buf) < 0)
  646. return (EOF);
  647. temp = *(buf->current)++;
  648. if ((temp & 0xc0) != 0x80)
  649. return (EOF);
  650. ch = (ch << 6) | (temp & 0x3f);
  651. if (buf->current >= buf->end)
  652. if (mxml_fd_read(buf) < 0)
  653. return (EOF);
  654. temp = *(buf->current)++;
  655. if ((temp & 0xc0) != 0x80)
  656. return (EOF);
  657. ch = (ch << 6) | (temp & 0x3f);
  658. if (ch < 0x10000)
  659. {
  660. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  661. return (EOF);
  662. }
  663. }
  664. else
  665. return (EOF);
  666. break;
  667. case ENCODE_UTF16BE :
  668. /*
  669. * Read UTF-16 big-endian char...
  670. */
  671. if (buf->current >= buf->end)
  672. if (mxml_fd_read(buf) < 0)
  673. return (EOF);
  674. temp = *(buf->current)++;
  675. ch = (ch << 8) | temp;
  676. if (mxml_bad_char(ch))
  677. {
  678. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  679. ch);
  680. return (EOF);
  681. }
  682. else if (ch >= 0xd800 && ch <= 0xdbff)
  683. {
  684. /*
  685. * Multi-word UTF-16 char...
  686. */
  687. int lch;
  688. if (buf->current >= buf->end)
  689. if (mxml_fd_read(buf) < 0)
  690. return (EOF);
  691. lch = *(buf->current)++;
  692. if (buf->current >= buf->end)
  693. if (mxml_fd_read(buf) < 0)
  694. return (EOF);
  695. temp = *(buf->current)++;
  696. lch = (lch << 8) | temp;
  697. if (lch < 0xdc00 || lch >= 0xdfff)
  698. return (EOF);
  699. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  700. }
  701. break;
  702. case ENCODE_UTF16LE :
  703. /*
  704. * Read UTF-16 little-endian char...
  705. */
  706. if (buf->current >= buf->end)
  707. if (mxml_fd_read(buf) < 0)
  708. return (EOF);
  709. temp = *(buf->current)++;
  710. ch |= (temp << 8);
  711. if (mxml_bad_char(ch))
  712. {
  713. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  714. ch);
  715. return (EOF);
  716. }
  717. else if (ch >= 0xd800 && ch <= 0xdbff)
  718. {
  719. /*
  720. * Multi-word UTF-16 char...
  721. */
  722. int lch;
  723. if (buf->current >= buf->end)
  724. if (mxml_fd_read(buf) < 0)
  725. return (EOF);
  726. lch = *(buf->current)++;
  727. if (buf->current >= buf->end)
  728. if (mxml_fd_read(buf) < 0)
  729. return (EOF);
  730. temp = *(buf->current)++;
  731. lch |= (temp << 8);
  732. if (lch < 0xdc00 || lch >= 0xdfff)
  733. return (EOF);
  734. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  735. }
  736. break;
  737. }
  738. #if DEBUG > 1
  739. printf("mxml_fd_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  740. #endif /* DEBUG > 1 */
  741. return (ch);
  742. }
  743. /*
  744. * 'mxml_fd_putc()' - Write a character to a file descriptor.
  745. */
  746. static int /* O - 0 on success, -1 on error */
  747. mxml_fd_putc(int ch, /* I - Character */
  748. void *p) /* I - File descriptor buffer */
  749. {
  750. _mxml_fdbuf_t *buf; /* File descriptor buffer */
  751. /*
  752. * Flush the write buffer as needed...
  753. */
  754. buf = (_mxml_fdbuf_t *)p;
  755. if (buf->current >= buf->end)
  756. if (mxml_fd_write(buf) < 0)
  757. return (-1);
  758. *(buf->current)++ = ch;
  759. /*
  760. * Return successfully...
  761. */
  762. return (0);
  763. }
  764. /*
  765. * 'mxml_fd_read()' - Read a buffer of data from a file descriptor.
  766. */
  767. static int /* O - 0 on success, -1 on error */
  768. mxml_fd_read(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
  769. {
  770. int bytes; /* Bytes read... */
  771. /*
  772. * Range check input...
  773. */
  774. if (!buf)
  775. return (-1);
  776. /*
  777. * Read from the file descriptor...
  778. */
  779. while ((bytes = (int)read(buf->fd, buf->buffer, sizeof(buf->buffer))) < 0)
  780. #ifdef EINTR
  781. if (errno != EAGAIN && errno != EINTR)
  782. #else
  783. if (errno != EAGAIN)
  784. #endif /* EINTR */
  785. return (-1);
  786. if (bytes == 0)
  787. return (-1);
  788. /*
  789. * Update the pointers and return success...
  790. */
  791. buf->current = buf->buffer;
  792. buf->end = buf->buffer + bytes;
  793. return (0);
  794. }
  795. /*
  796. * 'mxml_fd_write()' - Write a buffer of data to a file descriptor.
  797. */
  798. static int /* O - 0 on success, -1 on error */
  799. mxml_fd_write(_mxml_fdbuf_t *buf) /* I - File descriptor buffer */
  800. {
  801. int bytes; /* Bytes written */
  802. unsigned char *ptr; /* Pointer into buffer */
  803. /*
  804. * Range check...
  805. */
  806. if (!buf)
  807. return (-1);
  808. /*
  809. * Return 0 if there is nothing to write...
  810. */
  811. if (buf->current == buf->buffer)
  812. return (0);
  813. /*
  814. * Loop until we have written everything...
  815. */
  816. for (ptr = buf->buffer; ptr < buf->current; ptr += bytes)
  817. if ((bytes = (int)write(buf->fd, ptr, buf->current - ptr)) < 0)
  818. return (-1);
  819. /*
  820. * All done, reset pointers and return success...
  821. */
  822. buf->current = buf->buffer;
  823. return (0);
  824. }
  825. /*
  826. * 'mxml_file_getc()' - Get a character from a file.
  827. */
  828. static int /* O - Character or EOF */
  829. mxml_file_getc(void *p, /* I - Pointer to file */
  830. int *encoding) /* IO - Encoding */
  831. {
  832. int ch, /* Character from file */
  833. temp; /* Temporary character */
  834. FILE *fp; /* Pointer to file */
  835. /*
  836. * Read a character from the file and see if it is EOF or ASCII...
  837. */
  838. fp = (FILE *)p;
  839. ch = getc(fp);
  840. if (ch == EOF)
  841. return (EOF);
  842. switch (*encoding)
  843. {
  844. case ENCODE_UTF8 :
  845. /*
  846. * Got a UTF-8 character; convert UTF-8 to Unicode and return...
  847. */
  848. if (!(ch & 0x80))
  849. {
  850. if (mxml_bad_char(ch))
  851. {
  852. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  853. ch);
  854. return (EOF);
  855. }
  856. #if DEBUG > 1
  857. printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  858. #endif /* DEBUG > 1 */
  859. return (ch);
  860. }
  861. else if (ch == 0xfe)
  862. {
  863. /*
  864. * UTF-16 big-endian BOM?
  865. */
  866. ch = getc(fp);
  867. if (ch != 0xff)
  868. return (EOF);
  869. *encoding = ENCODE_UTF16BE;
  870. return (mxml_file_getc(p, encoding));
  871. }
  872. else if (ch == 0xff)
  873. {
  874. /*
  875. * UTF-16 little-endian BOM?
  876. */
  877. ch = getc(fp);
  878. if (ch != 0xfe)
  879. return (EOF);
  880. *encoding = ENCODE_UTF16LE;
  881. return (mxml_file_getc(p, encoding));
  882. }
  883. else if ((ch & 0xe0) == 0xc0)
  884. {
  885. /*
  886. * Two-byte value...
  887. */
  888. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  889. return (EOF);
  890. ch = ((ch & 0x1f) << 6) | (temp & 0x3f);
  891. if (ch < 0x80)
  892. {
  893. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  894. return (EOF);
  895. }
  896. }
  897. else if ((ch & 0xf0) == 0xe0)
  898. {
  899. /*
  900. * Three-byte value...
  901. */
  902. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  903. return (EOF);
  904. ch = ((ch & 0x0f) << 6) | (temp & 0x3f);
  905. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  906. return (EOF);
  907. ch = (ch << 6) | (temp & 0x3f);
  908. if (ch < 0x800)
  909. {
  910. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  911. return (EOF);
  912. }
  913. /*
  914. * Ignore (strip) Byte Order Mark (BOM)...
  915. */
  916. if (ch == 0xfeff)
  917. return (mxml_file_getc(p, encoding));
  918. }
  919. else if ((ch & 0xf8) == 0xf0)
  920. {
  921. /*
  922. * Four-byte value...
  923. */
  924. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  925. return (EOF);
  926. ch = ((ch & 0x07) << 6) | (temp & 0x3f);
  927. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  928. return (EOF);
  929. ch = (ch << 6) | (temp & 0x3f);
  930. if ((temp = getc(fp)) == EOF || (temp & 0xc0) != 0x80)
  931. return (EOF);
  932. ch = (ch << 6) | (temp & 0x3f);
  933. if (ch < 0x10000)
  934. {
  935. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  936. return (EOF);
  937. }
  938. }
  939. else
  940. return (EOF);
  941. break;
  942. case ENCODE_UTF16BE :
  943. /*
  944. * Read UTF-16 big-endian char...
  945. */
  946. ch = (ch << 8) | getc(fp);
  947. if (mxml_bad_char(ch))
  948. {
  949. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  950. ch);
  951. return (EOF);
  952. }
  953. else if (ch >= 0xd800 && ch <= 0xdbff)
  954. {
  955. /*
  956. * Multi-word UTF-16 char...
  957. */
  958. int lch = getc(fp);
  959. lch = (lch << 8) | getc(fp);
  960. if (lch < 0xdc00 || lch >= 0xdfff)
  961. return (EOF);
  962. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  963. }
  964. break;
  965. case ENCODE_UTF16LE :
  966. /*
  967. * Read UTF-16 little-endian char...
  968. */
  969. ch |= (getc(fp) << 8);
  970. if (mxml_bad_char(ch))
  971. {
  972. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  973. ch);
  974. return (EOF);
  975. }
  976. else if (ch >= 0xd800 && ch <= 0xdbff)
  977. {
  978. /*
  979. * Multi-word UTF-16 char...
  980. */
  981. int lch = getc(fp);
  982. lch |= (getc(fp) << 8);
  983. if (lch < 0xdc00 || lch >= 0xdfff)
  984. return (EOF);
  985. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  986. }
  987. break;
  988. }
  989. #if DEBUG > 1
  990. printf("mxml_file_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  991. #endif /* DEBUG > 1 */
  992. return (ch);
  993. }
  994. /*
  995. * 'mxml_file_putc()' - Write a character to a file.
  996. */
  997. static int /* O - 0 on success, -1 on failure */
  998. mxml_file_putc(int ch, /* I - Character to write */
  999. void *p) /* I - Pointer to file */
  1000. {
  1001. return (putc(ch, (FILE *)p) == EOF ? -1 : 0);
  1002. }
  1003. /*
  1004. * 'mxml_get_entity()' - Get the character corresponding to an entity...
  1005. */
  1006. static int /* O - Character value or EOF on error */
  1007. mxml_get_entity(mxml_node_t *parent, /* I - Parent node */
  1008. void *p, /* I - Pointer to source */
  1009. int *encoding, /* IO - Character encoding */
  1010. int (*getc_cb)(void *, int *),
  1011. /* I - Get character function */
  1012. int *line) /* IO - Current line number */
  1013. {
  1014. int ch; /* Current character */
  1015. char entity[64], /* Entity string */
  1016. *entptr; /* Pointer into entity */
  1017. entptr = entity;
  1018. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1019. {
  1020. if (ch > 126 || (!isalnum(ch) && ch != '#'))
  1021. break;
  1022. else if (entptr < (entity + sizeof(entity) - 1))
  1023. *entptr++ = ch;
  1024. else
  1025. {
  1026. mxml_error("Entity name too long under parent <%s> on line %d.", parent ? parent->value.element.name : "null", *line);
  1027. break;
  1028. }
  1029. }
  1030. *entptr = '\0';
  1031. if (ch != ';')
  1032. {
  1033. mxml_error("Character entity '%s' not terminated under parent <%s> on line %d.", entity, parent ? parent->value.element.name : "null", *line);
  1034. if (ch == '\n')
  1035. (*line)++;
  1036. return (EOF);
  1037. }
  1038. if (entity[0] == '#')
  1039. {
  1040. if (entity[1] == 'x')
  1041. ch = (int)strtol(entity + 2, NULL, 16);
  1042. else
  1043. ch = (int)strtol(entity + 1, NULL, 10);
  1044. }
  1045. else if ((ch = mxmlEntityGetValue(entity)) < 0)
  1046. mxml_error("Entity name '%s;' not supported under parent <%s> on line %d.", entity, parent ? parent->value.element.name : "null", *line);
  1047. if (mxml_bad_char(ch))
  1048. {
  1049. mxml_error("Bad control character 0x%02x under parent <%s> on line %d not allowed by XML standard.", ch, parent ? parent->value.element.name : "null", *line);
  1050. return (EOF);
  1051. }
  1052. return (ch);
  1053. }
  1054. /*
  1055. * 'mxml_load_data()' - Load data into an XML node tree.
  1056. */
  1057. static mxml_node_t * /* O - First node or NULL if the file could not be read. */
  1058. mxml_load_data(
  1059. mxml_node_t *top, /* I - Top node */
  1060. void *p, /* I - Pointer to data */
  1061. mxml_load_cb_t cb, /* I - Callback function or MXML_NO_CALLBACK */
  1062. _mxml_getc_cb_t getc_cb, /* I - Read function */
  1063. mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */
  1064. void *sax_data) /* I - SAX user data */
  1065. {
  1066. mxml_node_t *node, /* Current node */
  1067. *first, /* First node added */
  1068. *parent; /* Current parent node */
  1069. int line = 1, /* Current line number */
  1070. ch, /* Character from file */
  1071. whitespace; /* Non-zero if whitespace seen */
  1072. char *buffer, /* String buffer */
  1073. *bufptr; /* Pointer into buffer */
  1074. int bufsize; /* Size of buffer */
  1075. mxml_type_t type; /* Current node type */
  1076. int encoding; /* Character encoding */
  1077. _mxml_global_t *global = _mxml_global();
  1078. /* Global data */
  1079. static const char * const types[] = /* Type strings... */
  1080. {
  1081. "MXML_ELEMENT", /* XML element with attributes */
  1082. "MXML_INTEGER", /* Integer value */
  1083. "MXML_OPAQUE", /* Opaque string */
  1084. "MXML_REAL", /* Real value */
  1085. "MXML_TEXT", /* Text fragment */
  1086. "MXML_CUSTOM" /* Custom data */
  1087. };
  1088. /*
  1089. * Read elements and other nodes from the file...
  1090. */
  1091. if ((buffer = malloc(64)) == NULL)
  1092. {
  1093. mxml_error("Unable to allocate string buffer!");
  1094. return (NULL);
  1095. }
  1096. bufsize = 64;
  1097. bufptr = buffer;
  1098. parent = top;
  1099. first = NULL;
  1100. whitespace = 0;
  1101. encoding = ENCODE_UTF8;
  1102. if (cb && parent)
  1103. type = (*cb)(parent);
  1104. else if (parent)
  1105. type = MXML_TEXT;
  1106. else
  1107. type = MXML_IGNORE;
  1108. if ((ch = (*getc_cb)(p, &encoding)) == EOF)
  1109. {
  1110. free(buffer);
  1111. return (NULL);
  1112. }
  1113. else if (ch != '<' && !top)
  1114. {
  1115. free(buffer);
  1116. mxml_error("XML does not start with '<' (saw '%c').", ch);
  1117. return (NULL);
  1118. }
  1119. do
  1120. {
  1121. if ((ch == '<' ||
  1122. (mxml_isspace(ch) && type != MXML_OPAQUE && type != MXML_CUSTOM)) &&
  1123. bufptr > buffer)
  1124. {
  1125. /*
  1126. * Add a new value node...
  1127. */
  1128. *bufptr = '\0';
  1129. switch (type)
  1130. {
  1131. case MXML_INTEGER :
  1132. node = mxmlNewInteger(parent, (int)strtol(buffer, &bufptr, 0));
  1133. break;
  1134. case MXML_OPAQUE :
  1135. node = mxmlNewOpaque(parent, buffer);
  1136. break;
  1137. case MXML_REAL :
  1138. node = mxmlNewReal(parent, strtod(buffer, &bufptr));
  1139. break;
  1140. case MXML_TEXT :
  1141. node = mxmlNewText(parent, whitespace, buffer);
  1142. break;
  1143. case MXML_CUSTOM :
  1144. if (global->custom_load_cb)
  1145. {
  1146. /*
  1147. * Use the callback to fill in the custom data...
  1148. */
  1149. node = mxmlNewCustom(parent, NULL, NULL);
  1150. if ((*global->custom_load_cb)(node, buffer))
  1151. {
  1152. mxml_error("Bad custom value '%s' in parent <%s> on line %d.", buffer, parent ? parent->value.element.name : "null", line);
  1153. mxmlDelete(node);
  1154. node = NULL;
  1155. }
  1156. break;
  1157. }
  1158. default : /* Ignore... */
  1159. node = NULL;
  1160. break;
  1161. }
  1162. if (*bufptr)
  1163. {
  1164. /*
  1165. * Bad integer/real number value...
  1166. */
  1167. mxml_error("Bad %s value '%s' in parent <%s> on line %d.", type == MXML_INTEGER ? "integer" : "real", buffer, parent ? parent->value.element.name : "null", line);
  1168. break;
  1169. }
  1170. bufptr = buffer;
  1171. whitespace = mxml_isspace(ch) && type == MXML_TEXT;
  1172. if (!node && type != MXML_IGNORE)
  1173. {
  1174. /*
  1175. * Print error and return...
  1176. */
  1177. mxml_error("Unable to add value node of type %s to parent <%s> on line %d.", types[type], parent ? parent->value.element.name : "null", line);
  1178. goto error;
  1179. }
  1180. if (sax_cb)
  1181. {
  1182. (*sax_cb)(node, MXML_SAX_DATA, sax_data);
  1183. if (!mxmlRelease(node))
  1184. node = NULL;
  1185. }
  1186. if (!first && node)
  1187. first = node;
  1188. }
  1189. else if (mxml_isspace(ch) && type == MXML_TEXT)
  1190. whitespace = 1;
  1191. if (ch == '\n')
  1192. line ++;
  1193. /*
  1194. * Add lone whitespace node if we have an element and existing
  1195. * whitespace...
  1196. */
  1197. if (ch == '<' && whitespace && type == MXML_TEXT)
  1198. {
  1199. if (parent)
  1200. {
  1201. node = mxmlNewText(parent, whitespace, "");
  1202. if (sax_cb)
  1203. {
  1204. (*sax_cb)(node, MXML_SAX_DATA, sax_data);
  1205. if (!mxmlRelease(node))
  1206. node = NULL;
  1207. }
  1208. if (!first && node)
  1209. first = node;
  1210. }
  1211. whitespace = 0;
  1212. }
  1213. if (ch == '<')
  1214. {
  1215. /*
  1216. * Start of open/close tag...
  1217. */
  1218. bufptr = buffer;
  1219. while ((ch = (*getc_cb)(p, &encoding)) != EOF)
  1220. {
  1221. if (mxml_isspace(ch) || ch == '>' || (ch == '/' && bufptr > buffer))
  1222. break;
  1223. else if (ch == '<')
  1224. {
  1225. mxml_error("Bare < in element!");
  1226. goto error;
  1227. }
  1228. else if (ch == '&')
  1229. {
  1230. if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb, &line)) == EOF)
  1231. goto error;
  1232. if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1233. goto error;
  1234. }
  1235. else if (ch < '0' && ch != '!' && ch != '-' && ch != '.' && ch != '/')
  1236. goto error;
  1237. else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1238. goto error;
  1239. else if (((bufptr - buffer) == 1 && buffer[0] == '?') ||
  1240. ((bufptr - buffer) == 3 && !strncmp(buffer, "!--", 3)) ||
  1241. ((bufptr - buffer) == 8 && !strncmp(buffer, "![CDATA[", 8)))
  1242. break;
  1243. if (ch == '\n')
  1244. line ++;
  1245. }
  1246. *bufptr = '\0';
  1247. if (!strcmp(buffer, "!--"))
  1248. {
  1249. /*
  1250. * Gather rest of comment...
  1251. */
  1252. while ((ch = (*getc_cb)(p, &encoding)) != EOF)
  1253. {
  1254. if (ch == '>' && bufptr > (buffer + 4) &&
  1255. bufptr[-3] != '-' && bufptr[-2] == '-' && bufptr[-1] == '-')
  1256. break;
  1257. else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1258. goto error;
  1259. if (ch == '\n')
  1260. line ++;
  1261. }
  1262. /*
  1263. * Error out if we didn't get the whole comment...
  1264. */
  1265. if (ch != '>')
  1266. {
  1267. /*
  1268. * Print error and return...
  1269. */
  1270. mxml_error("Early EOF in comment node on line %d.", line);
  1271. goto error;
  1272. }
  1273. /*
  1274. * Otherwise add this as an element under the current parent...
  1275. */
  1276. *bufptr = '\0';
  1277. if (!parent && first)
  1278. {
  1279. /*
  1280. * There can only be one root element!
  1281. */
  1282. mxml_error("<%s> cannot be a second root node after <%s> on line %d.", buffer, first->value.element.name, line);
  1283. goto error;
  1284. }
  1285. if ((node = mxmlNewElement(parent, buffer)) == NULL)
  1286. {
  1287. /*
  1288. * Just print error for now...
  1289. */
  1290. mxml_error("Unable to add comment node to parent <%s> on line %d.", parent ? parent->value.element.name : "null", line);
  1291. break;
  1292. }
  1293. if (sax_cb)
  1294. {
  1295. (*sax_cb)(node, MXML_SAX_COMMENT, sax_data);
  1296. if (!mxmlRelease(node))
  1297. node = NULL;
  1298. }
  1299. if (node && !first)
  1300. first = node;
  1301. }
  1302. else if (!strcmp(buffer, "![CDATA["))
  1303. {
  1304. /*
  1305. * Gather CDATA section...
  1306. */
  1307. while ((ch = (*getc_cb)(p, &encoding)) != EOF)
  1308. {
  1309. if (ch == '>' && !strncmp(bufptr - 2, "]]", 2))
  1310. {
  1311. /*
  1312. * Drop terminator from CDATA string...
  1313. */
  1314. bufptr[-2] = '\0';
  1315. break;
  1316. }
  1317. else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1318. goto error;
  1319. if (ch == '\n')
  1320. line ++;
  1321. }
  1322. /*
  1323. * Error out if we didn't get the whole comment...
  1324. */
  1325. if (ch != '>')
  1326. {
  1327. /*
  1328. * Print error and return...
  1329. */
  1330. mxml_error("Early EOF in CDATA node on line %d.", line);
  1331. goto error;
  1332. }
  1333. /*
  1334. * Otherwise add this as an element under the current parent...
  1335. */
  1336. *bufptr = '\0';
  1337. if (!parent && first)
  1338. {
  1339. /*
  1340. * There can only be one root element!
  1341. */
  1342. mxml_error("<%s> cannot be a second root node after <%s> on line %d.", buffer, first->value.element.name, line);
  1343. goto error;
  1344. }
  1345. if ((node = mxmlNewElement(parent, buffer)) == NULL)
  1346. {
  1347. /*
  1348. * Print error and return...
  1349. */
  1350. mxml_error("Unable to add CDATA node to parent <%s> on line %d.", parent ? parent->value.element.name : "null", line);
  1351. goto error;
  1352. }
  1353. if (sax_cb)
  1354. {
  1355. (*sax_cb)(node, MXML_SAX_CDATA, sax_data);
  1356. if (!mxmlRelease(node))
  1357. node = NULL;
  1358. }
  1359. if (node && !first)
  1360. first = node;
  1361. }
  1362. else if (buffer[0] == '?')
  1363. {
  1364. /*
  1365. * Gather rest of processing instruction...
  1366. */
  1367. while ((ch = (*getc_cb)(p, &encoding)) != EOF)
  1368. {
  1369. if (ch == '>' && bufptr > buffer && bufptr[-1] == '?')
  1370. break;
  1371. else if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1372. goto error;
  1373. if (ch == '\n')
  1374. line ++;
  1375. }
  1376. /*
  1377. * Error out if we didn't get the whole processing instruction...
  1378. */
  1379. if (ch != '>')
  1380. {
  1381. /*
  1382. * Print error and return...
  1383. */
  1384. mxml_error("Early EOF in processing instruction node on line %d.", line);
  1385. goto error;
  1386. }
  1387. /*
  1388. * Otherwise add this as an element under the current parent...
  1389. */
  1390. *bufptr = '\0';
  1391. if (!parent && first)
  1392. {
  1393. /*
  1394. * There can only be one root element!
  1395. */
  1396. mxml_error("<%s> cannot be a second root node after <%s> on line %d.", buffer, first->value.element.name, line);
  1397. goto error;
  1398. }
  1399. if ((node = mxmlNewElement(parent, buffer)) == NULL)
  1400. {
  1401. /*
  1402. * Print error and return...
  1403. */
  1404. mxml_error("Unable to add processing instruction node to parent <%s> on line %d.", parent ? parent->value.element.name : "null", line);
  1405. goto error;
  1406. }
  1407. if (sax_cb)
  1408. {
  1409. (*sax_cb)(node, MXML_SAX_DIRECTIVE, sax_data);
  1410. if (strncmp(node->value.element.name, "?xml ", 5) && !mxmlRelease(node))
  1411. node = NULL;
  1412. }
  1413. if (node)
  1414. {
  1415. if (!first)
  1416. first = node;
  1417. if (!parent)
  1418. {
  1419. parent = node;
  1420. if (cb)
  1421. type = (*cb)(parent);
  1422. else
  1423. type = MXML_TEXT;
  1424. }
  1425. }
  1426. }
  1427. else if (buffer[0] == '!')
  1428. {
  1429. /*
  1430. * Gather rest of declaration...
  1431. */
  1432. do
  1433. {
  1434. if (ch == '>')
  1435. break;
  1436. else
  1437. {
  1438. if (ch == '&')
  1439. {
  1440. if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb, &line)) == EOF)
  1441. goto error;
  1442. }
  1443. if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1444. goto error;
  1445. }
  1446. if (ch == '\n')
  1447. line ++;
  1448. }
  1449. while ((ch = (*getc_cb)(p, &encoding)) != EOF);
  1450. /*
  1451. * Error out if we didn't get the whole declaration...
  1452. */
  1453. if (ch != '>')
  1454. {
  1455. /*
  1456. * Print error and return...
  1457. */
  1458. mxml_error("Early EOF in declaration node on line %d.", line);
  1459. goto error;
  1460. }
  1461. /*
  1462. * Otherwise add this as an element under the current parent...
  1463. */
  1464. *bufptr = '\0';
  1465. if (!parent && first)
  1466. {
  1467. /*
  1468. * There can only be one root element!
  1469. */
  1470. mxml_error("<%s> cannot be a second root node after <%s> on line %d.", buffer, first->value.element.name, line);
  1471. goto error;
  1472. }
  1473. if ((node = mxmlNewElement(parent, buffer)) == NULL)
  1474. {
  1475. /*
  1476. * Print error and return...
  1477. */
  1478. mxml_error("Unable to add declaration node to parent <%s> on line %d.", parent ? parent->value.element.name : "null", line);
  1479. goto error;
  1480. }
  1481. if (sax_cb)
  1482. {
  1483. (*sax_cb)(node, MXML_SAX_DIRECTIVE, sax_data);
  1484. if (!mxmlRelease(node))
  1485. node = NULL;
  1486. }
  1487. if (node)
  1488. {
  1489. if (!first)
  1490. first = node;
  1491. if (!parent)
  1492. {
  1493. parent = node;
  1494. if (cb)
  1495. type = (*cb)(parent);
  1496. else
  1497. type = MXML_TEXT;
  1498. }
  1499. }
  1500. }
  1501. else if (buffer[0] == '/')
  1502. {
  1503. /*
  1504. * Handle close tag...
  1505. */
  1506. if (!parent || strcmp(buffer + 1, parent->value.element.name))
  1507. {
  1508. /*
  1509. * Close tag doesn't match tree; print an error for now...
  1510. */
  1511. mxml_error("Mismatched close tag <%s> under parent <%s> on line %d.", buffer, parent ? parent->value.element.name : "(null)", line);
  1512. goto error;
  1513. }
  1514. /*
  1515. * Keep reading until we see >...
  1516. */
  1517. while (ch != '>' && ch != EOF)
  1518. ch = (*getc_cb)(p, &encoding);
  1519. node = parent;
  1520. parent = parent->parent;
  1521. if (sax_cb)
  1522. {
  1523. (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data);
  1524. if (!mxmlRelease(node) && first == node)
  1525. first = NULL;
  1526. }
  1527. /*
  1528. * Ascend into the parent and set the value type as needed...
  1529. */
  1530. if (cb && parent)
  1531. type = (*cb)(parent);
  1532. }
  1533. else
  1534. {
  1535. /*
  1536. * Handle open tag...
  1537. */
  1538. if (!parent && first)
  1539. {
  1540. /*
  1541. * There can only be one root element!
  1542. */
  1543. mxml_error("<%s> cannot be a second root node after <%s> on line %d.", buffer, first->value.element.name, line);
  1544. goto error;
  1545. }
  1546. if ((node = mxmlNewElement(parent, buffer)) == NULL)
  1547. {
  1548. /*
  1549. * Just print error for now...
  1550. */
  1551. mxml_error("Unable to add element node to parent <%s> on line %d.", parent ? parent->value.element.name : "null", line);
  1552. goto error;
  1553. }
  1554. if (mxml_isspace(ch))
  1555. {
  1556. if ((ch = mxml_parse_element(node, p, &encoding, getc_cb, &line)) == EOF)
  1557. goto error;
  1558. }
  1559. else if (ch == '/')
  1560. {
  1561. if ((ch = (*getc_cb)(p, &encoding)) != '>')
  1562. {
  1563. mxml_error("Expected > but got '%c' instead for element <%s/> on line %d.", ch, buffer, line);
  1564. mxmlDelete(node);
  1565. goto error;
  1566. }
  1567. ch = '/';
  1568. }
  1569. if (sax_cb)
  1570. (*sax_cb)(node, MXML_SAX_ELEMENT_OPEN, sax_data);
  1571. if (!first)
  1572. first = node;
  1573. if (ch == EOF)
  1574. break;
  1575. if (ch != '/')
  1576. {
  1577. /*
  1578. * Descend into this node, setting the value type as needed...
  1579. */
  1580. parent = node;
  1581. if (cb && parent)
  1582. type = (*cb)(parent);
  1583. else
  1584. type = MXML_TEXT;
  1585. }
  1586. else if (sax_cb)
  1587. {
  1588. (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data);
  1589. if (!mxmlRelease(node) && first == node)
  1590. first = NULL;
  1591. }
  1592. }
  1593. bufptr = buffer;
  1594. }
  1595. else if (ch == '&')
  1596. {
  1597. /*
  1598. * Add character entity to current buffer...
  1599. */
  1600. if ((ch = mxml_get_entity(parent, p, &encoding, getc_cb, &line)) == EOF)
  1601. goto error;
  1602. if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1603. goto error;
  1604. }
  1605. else if (type == MXML_OPAQUE || type == MXML_CUSTOM || !mxml_isspace(ch))
  1606. {
  1607. /*
  1608. * Add character to current buffer...
  1609. */
  1610. if (mxml_add_char(ch, &bufptr, &buffer, &bufsize))
  1611. goto error;
  1612. }
  1613. }
  1614. while ((ch = (*getc_cb)(p, &encoding)) != EOF);
  1615. /*
  1616. * Free the string buffer - we don't need it anymore...
  1617. */
  1618. free(buffer);
  1619. /*
  1620. * Find the top element and return it...
  1621. */
  1622. if (parent)
  1623. {
  1624. node = parent;
  1625. while (parent != top && parent->parent)
  1626. parent = parent->parent;
  1627. if (node != parent)
  1628. {
  1629. mxml_error("Missing close tag </%s> under parent <%s> on line %d.", node->value.element.name, node->parent ? node->parent->value.element.name : "(null)", line);
  1630. mxmlDelete(first);
  1631. return (NULL);
  1632. }
  1633. }
  1634. if (parent)
  1635. return (parent);
  1636. else
  1637. return (first);
  1638. /*
  1639. * Common error return...
  1640. */
  1641. error:
  1642. mxmlDelete(first);
  1643. free(buffer);
  1644. return (NULL);
  1645. }
  1646. /*
  1647. * 'mxml_parse_element()' - Parse an element for any attributes...
  1648. */
  1649. static int /* O - Terminating character */
  1650. mxml_parse_element(
  1651. mxml_node_t *node, /* I - Element node */
  1652. void *p, /* I - Data to read from */
  1653. int *encoding, /* IO - Encoding */
  1654. _mxml_getc_cb_t getc_cb, /* I - Data callback */
  1655. int *line) /* IO - Current line number */
  1656. {
  1657. int ch, /* Current character in file */
  1658. quote; /* Quoting character */
  1659. char *name, /* Attribute name */
  1660. *value, /* Attribute value */
  1661. *ptr; /* Pointer into name/value */
  1662. int namesize, /* Size of name string */
  1663. valsize; /* Size of value string */
  1664. /*
  1665. * Initialize the name and value buffers...
  1666. */
  1667. if ((name = malloc(64)) == NULL)
  1668. {
  1669. mxml_error("Unable to allocate memory for name!");
  1670. return (EOF);
  1671. }
  1672. namesize = 64;
  1673. if ((value = malloc(64)) == NULL)
  1674. {
  1675. free(name);
  1676. mxml_error("Unable to allocate memory for value!");
  1677. return (EOF);
  1678. }
  1679. valsize = 64;
  1680. /*
  1681. * Loop until we hit a >, /, ?, or EOF...
  1682. */
  1683. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1684. {
  1685. #if DEBUG > 1
  1686. fprintf(stderr, "parse_element: ch='%c'\n", ch);
  1687. #endif /* DEBUG > 1 */
  1688. /*
  1689. * Skip leading whitespace...
  1690. */
  1691. if (mxml_isspace(ch))
  1692. {
  1693. if (ch == '\n')
  1694. (*line)++;
  1695. continue;
  1696. }
  1697. /*
  1698. * Stop at /, ?, or >...
  1699. */
  1700. if (ch == '/' || ch == '?')
  1701. {
  1702. /*
  1703. * Grab the > character and print an error if it isn't there...
  1704. */
  1705. quote = (*getc_cb)(p, encoding);
  1706. if (quote != '>')
  1707. {
  1708. mxml_error("Expected '>' after '%c' for element %s, but got '%c' on line %d.", ch, node->value.element.name, quote, *line);
  1709. goto error;
  1710. }
  1711. break;
  1712. }
  1713. else if (ch == '<')
  1714. {
  1715. mxml_error("Bare < in element %s on line %d.", node->value.element.name, *line);
  1716. goto error;
  1717. }
  1718. else if (ch == '>')
  1719. break;
  1720. /*
  1721. * Read the attribute name...
  1722. */
  1723. ptr = name;
  1724. if (mxml_add_char(ch, &ptr, &name, &namesize))
  1725. goto error;
  1726. if (ch == '\"' || ch == '\'')
  1727. {
  1728. /*
  1729. * Name is in quotes, so get a quoted string...
  1730. */
  1731. quote = ch;
  1732. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1733. {
  1734. if (ch == '&')
  1735. {
  1736. if ((ch = mxml_get_entity(node, p, encoding, getc_cb, line)) == EOF)
  1737. goto error;
  1738. }
  1739. else if (ch == '\n')
  1740. (*line)++;
  1741. if (mxml_add_char(ch, &ptr, &name, &namesize))
  1742. goto error;
  1743. if (ch == quote)
  1744. break;
  1745. }
  1746. }
  1747. else
  1748. {
  1749. /*
  1750. * Grab an normal, non-quoted name...
  1751. */
  1752. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1753. {
  1754. if (mxml_isspace(ch) || ch == '=' || ch == '/' || ch == '>' ||
  1755. ch == '?')
  1756. {
  1757. if (ch == '\n')
  1758. (*line)++;
  1759. break;
  1760. }
  1761. else
  1762. {
  1763. if (ch == '&')
  1764. {
  1765. if ((ch = mxml_get_entity(node, p, encoding, getc_cb, line)) == EOF)
  1766. goto error;
  1767. }
  1768. if (mxml_add_char(ch, &ptr, &name, &namesize))
  1769. goto error;
  1770. }
  1771. }
  1772. }
  1773. *ptr = '\0';
  1774. if (mxmlElementGetAttr(node, name))
  1775. {
  1776. mxml_error("Duplicate attribute '%s' in element %s on line %d.", name, node->value.element.name, *line);
  1777. goto error;
  1778. }
  1779. while (ch != EOF && mxml_isspace(ch))
  1780. {
  1781. ch = (*getc_cb)(p, encoding);
  1782. if (ch == '\n')
  1783. (*line)++;
  1784. }
  1785. if (ch == '=')
  1786. {
  1787. /*
  1788. * Read the attribute value...
  1789. */
  1790. while ((ch = (*getc_cb)(p, encoding)) != EOF && mxml_isspace(ch))
  1791. {
  1792. if (ch == '\n')
  1793. (*line)++;
  1794. }
  1795. if (ch == EOF)
  1796. {
  1797. mxml_error("Missing value for attribute '%s' in element %s on line %d.", name, node->value.element.name, *line);
  1798. goto error;
  1799. }
  1800. if (ch == '\'' || ch == '\"')
  1801. {
  1802. /*
  1803. * Read quoted value...
  1804. */
  1805. quote = ch;
  1806. ptr = value;
  1807. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1808. {
  1809. if (ch == quote)
  1810. {
  1811. break;
  1812. }
  1813. else
  1814. {
  1815. if (ch == '&')
  1816. {
  1817. if ((ch = mxml_get_entity(node, p, encoding, getc_cb, line)) == EOF)
  1818. goto error;
  1819. }
  1820. else if (ch == '\n')
  1821. (*line)++;
  1822. if (mxml_add_char(ch, &ptr, &value, &valsize))
  1823. goto error;
  1824. }
  1825. }
  1826. *ptr = '\0';
  1827. }
  1828. else
  1829. {
  1830. /*
  1831. * Read unquoted value...
  1832. */
  1833. ptr = value;
  1834. if (mxml_add_char(ch, &ptr, &value, &valsize))
  1835. goto error;
  1836. while ((ch = (*getc_cb)(p, encoding)) != EOF)
  1837. {
  1838. if (mxml_isspace(ch) || ch == '=' || ch == '/' || ch == '>')
  1839. {
  1840. if (ch == '\n')
  1841. (*line)++;
  1842. break;
  1843. }
  1844. else
  1845. {
  1846. if (ch == '&')
  1847. {
  1848. if ((ch = mxml_get_entity(node, p, encoding, getc_cb, line)) == EOF)
  1849. goto error;
  1850. }
  1851. if (mxml_add_char(ch, &ptr, &value, &valsize))
  1852. goto error;
  1853. }
  1854. }
  1855. *ptr = '\0';
  1856. }
  1857. /*
  1858. * Set the attribute with the given string value...
  1859. */
  1860. mxmlElementSetAttr(node, name, value);
  1861. }
  1862. else
  1863. {
  1864. mxml_error("Missing value for attribute '%s' in element %s on line %d.", name, node->value.element.name, *line);
  1865. goto error;
  1866. }
  1867. /*
  1868. * Check the end character...
  1869. */
  1870. if (ch == '/' || ch == '?')
  1871. {
  1872. /*
  1873. * Grab the > character and print an error if it isn't there...
  1874. */
  1875. quote = (*getc_cb)(p, encoding);
  1876. if (quote != '>')
  1877. {
  1878. mxml_error("Expected '>' after '%c' for element %s, but got '%c' on line %d.", ch, node->value.element.name, quote, *line);
  1879. ch = EOF;
  1880. }
  1881. break;
  1882. }
  1883. else if (ch == '>')
  1884. break;
  1885. }
  1886. /*
  1887. * Free the name and value buffers and return...
  1888. */
  1889. free(name);
  1890. free(value);
  1891. return (ch);
  1892. /*
  1893. * Common error return point...
  1894. */
  1895. error:
  1896. free(name);
  1897. free(value);
  1898. return (EOF);
  1899. }
  1900. /*
  1901. * 'mxml_string_getc()' - Get a character from a string.
  1902. */
  1903. static int /* O - Character or EOF */
  1904. mxml_string_getc(void *p, /* I - Pointer to file */
  1905. int *encoding) /* IO - Encoding */
  1906. {
  1907. int ch; /* Character */
  1908. const char **s; /* Pointer to string pointer */
  1909. s = (const char **)p;
  1910. if ((ch = (*s)[0] & 255) != 0 || *encoding == ENCODE_UTF16LE)
  1911. {
  1912. /*
  1913. * Got character; convert UTF-8 to integer and return...
  1914. */
  1915. (*s)++;
  1916. switch (*encoding)
  1917. {
  1918. case ENCODE_UTF8 :
  1919. if (!(ch & 0x80))
  1920. {
  1921. #if DEBUG > 1
  1922. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  1923. #endif /* DEBUG > 1 */
  1924. if (mxml_bad_char(ch))
  1925. {
  1926. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  1927. ch);
  1928. return (EOF);
  1929. }
  1930. return (ch);
  1931. }
  1932. else if (ch == 0xfe)
  1933. {
  1934. /*
  1935. * UTF-16 big-endian BOM?
  1936. */
  1937. if (((*s)[0] & 255) != 0xff)
  1938. return (EOF);
  1939. *encoding = ENCODE_UTF16BE;
  1940. (*s)++;
  1941. return (mxml_string_getc(p, encoding));
  1942. }
  1943. else if (ch == 0xff)
  1944. {
  1945. /*
  1946. * UTF-16 little-endian BOM?
  1947. */
  1948. if (((*s)[0] & 255) != 0xfe)
  1949. return (EOF);
  1950. *encoding = ENCODE_UTF16LE;
  1951. (*s)++;
  1952. return (mxml_string_getc(p, encoding));
  1953. }
  1954. else if ((ch & 0xe0) == 0xc0)
  1955. {
  1956. /*
  1957. * Two-byte value...
  1958. */
  1959. if (((*s)[0] & 0xc0) != 0x80)
  1960. return (EOF);
  1961. ch = ((ch & 0x1f) << 6) | ((*s)[0] & 0x3f);
  1962. (*s)++;
  1963. if (ch < 0x80)
  1964. {
  1965. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  1966. return (EOF);
  1967. }
  1968. #if DEBUG > 1
  1969. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  1970. #endif /* DEBUG > 1 */
  1971. return (ch);
  1972. }
  1973. else if ((ch & 0xf0) == 0xe0)
  1974. {
  1975. /*
  1976. * Three-byte value...
  1977. */
  1978. if (((*s)[0] & 0xc0) != 0x80 ||
  1979. ((*s)[1] & 0xc0) != 0x80)
  1980. return (EOF);
  1981. ch = ((((ch & 0x0f) << 6) | ((*s)[0] & 0x3f)) << 6) | ((*s)[1] & 0x3f);
  1982. (*s) += 2;
  1983. if (ch < 0x800)
  1984. {
  1985. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  1986. return (EOF);
  1987. }
  1988. /*
  1989. * Ignore (strip) Byte Order Mark (BOM)...
  1990. */
  1991. if (ch == 0xfeff)
  1992. return (mxml_string_getc(p, encoding));
  1993. #if DEBUG > 1
  1994. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  1995. #endif /* DEBUG > 1 */
  1996. return (ch);
  1997. }
  1998. else if ((ch & 0xf8) == 0xf0)
  1999. {
  2000. /*
  2001. * Four-byte value...
  2002. */
  2003. if (((*s)[0] & 0xc0) != 0x80 ||
  2004. ((*s)[1] & 0xc0) != 0x80 ||
  2005. ((*s)[2] & 0xc0) != 0x80)
  2006. return (EOF);
  2007. ch = ((((((ch & 0x07) << 6) | ((*s)[0] & 0x3f)) << 6) |
  2008. ((*s)[1] & 0x3f)) << 6) | ((*s)[2] & 0x3f);
  2009. (*s) += 3;
  2010. if (ch < 0x10000)
  2011. {
  2012. mxml_error("Invalid UTF-8 sequence for character 0x%04x!", ch);
  2013. return (EOF);
  2014. }
  2015. #if DEBUG > 1
  2016. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  2017. #endif /* DEBUG > 1 */
  2018. return (ch);
  2019. }
  2020. else
  2021. return (EOF);
  2022. case ENCODE_UTF16BE :
  2023. /*
  2024. * Read UTF-16 big-endian char...
  2025. */
  2026. ch = (ch << 8) | ((*s)[0] & 255);
  2027. (*s) ++;
  2028. if (mxml_bad_char(ch))
  2029. {
  2030. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  2031. ch);
  2032. return (EOF);
  2033. }
  2034. else if (ch >= 0xd800 && ch <= 0xdbff)
  2035. {
  2036. /*
  2037. * Multi-word UTF-16 char...
  2038. */
  2039. int lch; /* Lower word */
  2040. if (!(*s)[0])
  2041. return (EOF);
  2042. lch = (((*s)[0] & 255) << 8) | ((*s)[1] & 255);
  2043. (*s) += 2;
  2044. if (lch < 0xdc00 || lch >= 0xdfff)
  2045. return (EOF);
  2046. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  2047. }
  2048. #if DEBUG > 1
  2049. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  2050. #endif /* DEBUG > 1 */
  2051. return (ch);
  2052. case ENCODE_UTF16LE :
  2053. /*
  2054. * Read UTF-16 little-endian char...
  2055. */
  2056. ch = ch | (((*s)[0] & 255) << 8);
  2057. if (!ch)
  2058. {
  2059. (*s) --;
  2060. return (EOF);
  2061. }
  2062. (*s) ++;
  2063. if (mxml_bad_char(ch))
  2064. {
  2065. mxml_error("Bad control character 0x%02x not allowed by XML standard!",
  2066. ch);
  2067. return (EOF);
  2068. }
  2069. else if (ch >= 0xd800 && ch <= 0xdbff)
  2070. {
  2071. /*
  2072. * Multi-word UTF-16 char...
  2073. */
  2074. int lch; /* Lower word */
  2075. if (!(*s)[1])
  2076. return (EOF);
  2077. lch = (((*s)[1] & 255) << 8) | ((*s)[0] & 255);
  2078. (*s) += 2;
  2079. if (lch < 0xdc00 || lch >= 0xdfff)
  2080. return (EOF);
  2081. ch = (((ch & 0x3ff) << 10) | (lch & 0x3ff)) + 0x10000;
  2082. }
  2083. #if DEBUG > 1
  2084. printf("mxml_string_getc: %c (0x%04x)\n", ch < ' ' ? '.' : ch, ch);
  2085. #endif /* DEBUG > 1 */
  2086. return (ch);
  2087. }
  2088. }
  2089. return (EOF);
  2090. }
  2091. /*
  2092. * 'mxml_string_putc()' - Write a character to a string.
  2093. */
  2094. static int /* O - 0 on success, -1 on failure */
  2095. mxml_string_putc(int ch, /* I - Character to write */
  2096. void *p) /* I - Pointer to string pointers */
  2097. {
  2098. char **pp; /* Pointer to string pointers */
  2099. pp = (char **)p;
  2100. if (pp[0] < pp[1])
  2101. pp[0][0] = ch;
  2102. pp[0] ++;
  2103. return (0);
  2104. }
  2105. /*
  2106. * 'mxml_write_name()' - Write a name string.
  2107. */
  2108. static int /* O - 0 on success, -1 on failure */
  2109. mxml_write_name(const char *s, /* I - Name to write */
  2110. void *p, /* I - Write pointer */
  2111. int (*putc_cb)(int, void *))
  2112. /* I - Write callback */
  2113. {
  2114. char quote; /* Quote character */
  2115. const char *name; /* Entity name */
  2116. if (*s == '\"' || *s == '\'')
  2117. {
  2118. /*
  2119. * Write a quoted name string...
  2120. */
  2121. if ((*putc_cb)(*s, p) < 0)
  2122. return (-1);
  2123. quote = *s++;
  2124. while (*s && *s != quote)
  2125. {
  2126. if ((name = mxmlEntityGetName(*s)) != NULL)
  2127. {
  2128. if ((*putc_cb)('&', p) < 0)
  2129. return (-1);
  2130. while (*name)
  2131. {
  2132. if ((*putc_cb)(*name, p) < 0)
  2133. return (-1);
  2134. name ++;
  2135. }
  2136. if ((*putc_cb)(';', p) < 0)
  2137. return (-1);
  2138. }
  2139. else if ((*putc_cb)(*s, p) < 0)
  2140. return (-1);
  2141. s ++;
  2142. }
  2143. /*
  2144. * Write the end quote...
  2145. */
  2146. if ((*putc_cb)(quote, p) < 0)
  2147. return (-1);
  2148. }
  2149. else
  2150. {
  2151. /*
  2152. * Write a non-quoted name string...
  2153. */
  2154. while (*s)
  2155. {
  2156. if ((*putc_cb)(*s, p) < 0)
  2157. return (-1);
  2158. s ++;
  2159. }
  2160. }
  2161. return (0);
  2162. }
  2163. /*
  2164. * 'mxml_write_node()' - Save an XML node to a file.
  2165. */
  2166. static int /* O - Column or -1 on error */
  2167. mxml_write_node(mxml_node_t *node, /* I - Node to write */
  2168. void *p, /* I - File to write to */
  2169. mxml_save_cb_t cb, /* I - Whitespace callback */
  2170. int col, /* I - Current column */
  2171. _mxml_putc_cb_t putc_cb,/* I - Output callback */
  2172. _mxml_global_t *global)/* I - Global data */
  2173. {
  2174. mxml_node_t *current, /* Current node */
  2175. *next; /* Next node */
  2176. int i, /* Looping var */
  2177. width; /* Width of attr + value */
  2178. _mxml_attr_t *attr; /* Current attribute */
  2179. char s[255]; /* Temporary string */
  2180. /*
  2181. * Loop through this node and all of its children...
  2182. */
  2183. for (current = node; current; current = next)
  2184. {
  2185. /*
  2186. * Print the node value...
  2187. */
  2188. switch (current->type)
  2189. {
  2190. case MXML_ELEMENT :
  2191. col = mxml_write_ws(current, p, cb, MXML_WS_BEFORE_OPEN, col, putc_cb);
  2192. if ((*putc_cb)('<', p) < 0)
  2193. return (-1);
  2194. if (current->value.element.name[0] == '?' ||
  2195. !strncmp(current->value.element.name, "!--", 3))
  2196. {
  2197. /*
  2198. * Comments and processing instructions do not use character
  2199. * entities.
  2200. */
  2201. const char *ptr; /* Pointer into name */
  2202. for (ptr = current->value.element.name; *ptr; ptr ++)
  2203. if ((*putc_cb)(*ptr, p) < 0)
  2204. return (-1);
  2205. }
  2206. else if (!strncmp(current->value.element.name, "![CDATA[", 8))
  2207. {
  2208. /*
  2209. * CDATA elements do not use character entities, but also need the
  2210. * "]]" terminator added at the end.
  2211. */
  2212. const char *ptr; /* Pointer into name */
  2213. for (ptr = current->value.element.name; *ptr; ptr ++)
  2214. if ((*putc_cb)(*ptr, p) < 0)
  2215. return (-1);
  2216. if ((*putc_cb)(']', p) < 0)
  2217. return (-1);
  2218. if ((*putc_cb)(']', p) < 0)
  2219. return (-1);
  2220. }
  2221. else if (mxml_write_name(current->value.element.name, p, putc_cb) < 0)
  2222. return (-1);
  2223. col += strlen(current->value.element.name) + 1;
  2224. for (i = current->value.element.num_attrs, attr = current->value.element.attrs;
  2225. i > 0;
  2226. i --, attr ++)
  2227. {
  2228. width = (int)strlen(attr->name);
  2229. if (attr->value)
  2230. width += strlen(attr->value) + 3;
  2231. if (global->wrap > 0 && (col + width) > global->wrap)
  2232. {
  2233. if ((*putc_cb)('\n', p) < 0)
  2234. return (-1);
  2235. col = 0;
  2236. }
  2237. else
  2238. {
  2239. if ((*putc_cb)(' ', p) < 0)
  2240. return (-1);
  2241. col ++;
  2242. }
  2243. if (mxml_write_name(attr->name, p, putc_cb) < 0)
  2244. return (-1);
  2245. if (attr->value)
  2246. {
  2247. if ((*putc_cb)('=', p) < 0)
  2248. return (-1);
  2249. if ((*putc_cb)('\"', p) < 0)
  2250. return (-1);
  2251. if (mxml_write_string(attr->value, p, putc_cb) < 0)
  2252. return (-1);
  2253. if ((*putc_cb)('\"', p) < 0)
  2254. return (-1);
  2255. }
  2256. col += width;
  2257. }
  2258. if (current->child)
  2259. {
  2260. /*
  2261. * Write children...
  2262. */
  2263. if ((*putc_cb)('>', p) < 0)
  2264. return (-1);
  2265. else
  2266. col ++;
  2267. col = mxml_write_ws(current, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
  2268. }
  2269. else if (current->value.element.name[0] == '!' ||
  2270. current->value.element.name[0] == '?')
  2271. {
  2272. /*
  2273. * The ? and ! elements are special-cases...
  2274. */
  2275. if ((*putc_cb)('>', p) < 0)
  2276. return (-1);
  2277. else
  2278. col ++;
  2279. col = mxml_write_ws(current, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
  2280. }
  2281. else
  2282. {
  2283. if ((*putc_cb)(' ', p) < 0)
  2284. return (-1);
  2285. if ((*putc_cb)('/', p) < 0)
  2286. return (-1);
  2287. if ((*putc_cb)('>', p) < 0)
  2288. return (-1);
  2289. col += 3;
  2290. col = mxml_write_ws(current, p, cb, MXML_WS_AFTER_OPEN, col, putc_cb);
  2291. }
  2292. break;
  2293. case MXML_INTEGER :
  2294. if (current->prev)
  2295. {
  2296. if (global->wrap > 0 && col > global->wrap)
  2297. {
  2298. if ((*putc_cb)('\n', p) < 0)
  2299. return (-1);
  2300. col = 0;
  2301. }
  2302. else if ((*putc_cb)(' ', p) < 0)
  2303. return (-1);
  2304. else
  2305. col ++;
  2306. }
  2307. snprintf(s, sizeof(s), "%d", current->value.integer);
  2308. if (mxml_write_string(s, p, putc_cb) < 0)
  2309. return (-1);
  2310. col += strlen(s);
  2311. break;
  2312. case MXML_OPAQUE :
  2313. if (mxml_write_string(current->value.opaque, p, putc_cb) < 0)
  2314. return (-1);
  2315. col += strlen(current->value.opaque);
  2316. break;
  2317. case MXML_REAL :
  2318. if (current->prev)
  2319. {
  2320. if (global->wrap > 0 && col > global->wrap)
  2321. {
  2322. if ((*putc_cb)('\n', p) < 0)
  2323. return (-1);
  2324. col = 0;
  2325. }
  2326. else if ((*putc_cb)(' ', p) < 0)
  2327. return (-1);
  2328. else
  2329. col ++;
  2330. }
  2331. snprintf(s, sizeof(s), "%f", current->value.real);
  2332. if (mxml_write_string(s, p, putc_cb) < 0)
  2333. return (-1);
  2334. col += strlen(s);
  2335. break;
  2336. case MXML_TEXT :
  2337. if (current->value.text.whitespace && col > 0)
  2338. {
  2339. if (global->wrap > 0 && col > global->wrap)
  2340. {
  2341. if ((*putc_cb)('\n', p) < 0)
  2342. return (-1);
  2343. col = 0;
  2344. }
  2345. else if ((*putc_cb)(' ', p) < 0)
  2346. return (-1);
  2347. else
  2348. col ++;
  2349. }
  2350. if (mxml_write_string(current->value.text.string, p, putc_cb) < 0)
  2351. return (-1);
  2352. col += strlen(current->value.text.string);
  2353. break;
  2354. case MXML_CUSTOM :
  2355. if (global->custom_save_cb)
  2356. {
  2357. char *data; /* Custom data string */
  2358. const char *newline; /* Last newline in string */
  2359. if ((data = (*global->custom_save_cb)(current)) == NULL)
  2360. return (-1);
  2361. if (mxml_write_string(data, p, putc_cb) < 0)
  2362. return (-1);
  2363. if ((newline = strrchr(data, '\n')) == NULL)
  2364. col += strlen(data);
  2365. else
  2366. col = (int)strlen(newline);
  2367. free(data);
  2368. break;
  2369. }
  2370. default : /* Should never happen */
  2371. return (-1);
  2372. }
  2373. /*
  2374. * Figure out the next node...
  2375. */
  2376. if ((next = current->child) == NULL)
  2377. {
  2378. if (current == node)
  2379. {
  2380. /*
  2381. * Don't traverse to sibling node if we are at the "root" node...
  2382. */
  2383. next = NULL;
  2384. }
  2385. else
  2386. {
  2387. /*
  2388. * Try the next sibling, and continue traversing upwards as needed...
  2389. */
  2390. while ((next = current->next) == NULL)
  2391. {
  2392. if (current == node || !current->parent)
  2393. break;
  2394. /*
  2395. * The ? and ! elements are special-cases and have no end tags...
  2396. */
  2397. current = current->parent;
  2398. if (current->value.element.name[0] != '!' &&
  2399. current->value.element.name[0] != '?')
  2400. {
  2401. col = mxml_write_ws(current, p, cb, MXML_WS_BEFORE_CLOSE, col, putc_cb);
  2402. if ((*putc_cb)('<', p) < 0)
  2403. return (-1);
  2404. if ((*putc_cb)('/', p) < 0)
  2405. return (-1);
  2406. if (mxml_write_string(current->value.element.name, p, putc_cb) < 0)
  2407. return (-1);
  2408. if ((*putc_cb)('>', p) < 0)
  2409. return (-1);
  2410. col += strlen(current->value.element.name) + 3;
  2411. col = mxml_write_ws(current, p, cb, MXML_WS_AFTER_CLOSE, col, putc_cb);
  2412. }
  2413. if (current == node)
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. }
  2419. return (col);
  2420. }
  2421. /*
  2422. * 'mxml_write_string()' - Write a string, escaping & and < as needed.
  2423. */
  2424. static int /* O - 0 on success, -1 on failure */
  2425. mxml_write_string(
  2426. const char *s, /* I - String to write */
  2427. void *p, /* I - Write pointer */
  2428. _mxml_putc_cb_t putc_cb) /* I - Write callback */
  2429. {
  2430. const char *name; /* Entity name, if any */
  2431. while (*s)
  2432. {
  2433. if ((name = mxmlEntityGetName(*s)) != NULL)
  2434. {
  2435. if ((*putc_cb)('&', p) < 0)
  2436. return (-1);
  2437. while (*name)
  2438. {
  2439. if ((*putc_cb)(*name, p) < 0)
  2440. return (-1);
  2441. name ++;
  2442. }
  2443. if ((*putc_cb)(';', p) < 0)
  2444. return (-1);
  2445. }
  2446. else if ((*putc_cb)(*s, p) < 0)
  2447. return (-1);
  2448. s ++;
  2449. }
  2450. return (0);
  2451. }
  2452. /*
  2453. * 'mxml_write_ws()' - Do whitespace callback...
  2454. */
  2455. static int /* O - New column */
  2456. mxml_write_ws(mxml_node_t *node, /* I - Current node */
  2457. void *p, /* I - Write pointer */
  2458. mxml_save_cb_t cb, /* I - Callback function */
  2459. int ws, /* I - Where value */
  2460. int col, /* I - Current column */
  2461. _mxml_putc_cb_t putc_cb) /* I - Write callback */
  2462. {
  2463. const char *s; /* Whitespace string */
  2464. if (cb && (s = (*cb)(node, ws)) != NULL)
  2465. {
  2466. while (*s)
  2467. {
  2468. if ((*putc_cb)(*s, p) < 0)
  2469. return (-1);
  2470. else if (*s == '\n')
  2471. col = 0;
  2472. else if (*s == '\t')
  2473. {
  2474. col += MXML_TAB;
  2475. col = col - (col % MXML_TAB);
  2476. }
  2477. else
  2478. col ++;
  2479. s ++;
  2480. }
  2481. }
  2482. return (col);
  2483. }