mxml-file.c 64 KB

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