pax_global_header00006660000000000000000000000064141020201000014466gustar00rootroot0000000000000052 comment=887a515b83feeca309c4740822e676812fff5df9 libnxml-master/000077500000000000000000000000001410202010000137705ustar00rootroot00000000000000libnxml-master/.gitignore000066400000000000000000000005641410202010000157650ustar00rootroot00000000000000*.lo *.o .deps .libs Makefile Makefile.in aclocal.m4 ar-lib autom4te.cache/ compile config.guess config.h config.h.in config.log config.status config.sub configure depcomp doxy.conf install-sh libtool ltmain.sh m4/ missing nxml.pc src/libnxml.la src/nxml.h stamp-h1 test-driver test/*.log test/*.trs test/easy test/namespace test/new test/parser test/test_base test/write libnxml-master/AUTHORS000066400000000000000000000001271410202010000150400ustar00rootroot00000000000000Andrea Marchesini Thanks to: Andreas Krennmair libnxml-master/COPYING000066400000000000000000000575061410202010000150400ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS libnxml-master/ChangeLog000066400000000000000000000031701410202010000155430ustar00rootroot0000000000000020210802 libnxml 0.18.4 Dacav's patches to fix crashes, indentation, tests and much more. 20080821 libnxml 0.18.3 Andreas Krennmair's patch about gzip support applied Gregor Herrmann's patch about signals and curl 20080226 libnxml 0.18.2 Andreas Krennmair's patch applied 20070918 libnxml 0.18.1 Andreas Krennmair bug fix size_t on macosx 20070704 libnxml 0.18.0 new release for a corrent version number 20070510 libnxml 0.17.3 jerome's patch about srcdir in the INCLUDE variable attributes with ns verifypeer default to FALSE CURLCode and nxml_curl_error function 20070418 libnxml 0.17.2 attribute example in nxml.h empty attributes 20070321 libnxml 0.17.1 bug about entities: fixed 20070307 libnxml 0.17 documentations entities 20061229 libnxml 0.16 bug 25 - fixed - No prolog in XML 1.0 cacert is supported correct indent of the TEXT node no debian directory 20061104 libnxml 0.15 proxy authentication (user and password) 20061027 libnxml 0.14 basic/digest authentication 20060911 libnxml 0.13 removed dtd support support for proxy and certificated connection. 20060712 libnxml 0.12 bugfixes in entities parser 20060612 libnxml 0.11 nxml.pc updates 20060607 libnxml 0.10 copyright uodates entities in attributes 20060307 libnxml 0.9 no memory leaks 20060210 libnxml 0.8 conversion in utf8 of a integer only if the encoding IS utff8 20060207 libnxml 0.7 a bugfixes in &#numb; utf8 20060203 libnxml 0.6 litle bug fixes 20060122 libnxml 0.5 doxygen 20060104 libnxml 0.4 GPL -> LGPL 20051215 libnxml 0.3 dtd some bug fixes 20050806 libnxml 0.2 Some defines easy functions libnxml-master/Makefile.am000066400000000000000000000002441410202010000160240ustar00rootroot00000000000000ACLOCAL_AMFLAGS = -I m4 SUBDIRS = src test doc EXTRA_DIST = nxml.pc nxml.pc.in doxy.conf doxy.conf.in pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = nxml.pc libnxml-master/README000066400000000000000000000003111410202010000146430ustar00rootroot00000000000000nXML is a C library for parsing, writing and creating XML 1.0 and 1.1 files or streams. It supports utf-8, utf-16be and utf-16le, ucs-4 (1234, 4321, 2143, 2312). Read the test/*.c files as examples. libnxml-master/autogen.sh000077500000000000000000000001331410202010000157660ustar00rootroot00000000000000#!/bin/sh set -e aclocal libtoolize autoheader autoconf automake --add-missing --foreign libnxml-master/configure.ac000066400000000000000000000024361410202010000162630ustar00rootroot00000000000000m4_define([libnxml_major_version], [0]) m4_define([libnxml_minor_version], [18]) m4_define([libnxml_macro_version], [4]) m4_define([libnxml_version], [libnxml_major_version.libnxml_minor_version.libnxml_macro_version]) m4_define([libnxml_version_info], [libnxml_minor_version:libnxml_macro_version:libnxml_minor_version]) AC_INIT([libnxml], [libnxml_version]) AC_CONFIG_SRCDIR([src/nxml_parser.c]) AC_CONFIG_MACRO_DIRS([m4]) AC_CONFIG_FILES([ Makefile doc/Makefile doxy.conf nxml.pc src/Makefile src/nxml.h test/Makefile ]) AM_INIT_AUTOMAKE([-Wall -Werror subdir-objects foreign]) AM_SANITY_CHECK AM_CONFIG_HEADER(config.h) AM_MAINTAINER_MODE AC_PROG_CC AM_PROG_AR AC_ISC_POSIX LT_INIT AC_PROG_LIBTOOL AC_PROG_INSTALL AM_PROG_CC_STDC AC_HEADER_STDC AC_HEADER_DIRENT AC_CHECK_HEADER(curl/curl.h,,AC_MSG_ERROR(libcurl is required to compile libnxml)) AC_CHECK_LIB(curl,curl_version,,AC_MSG_ERROR(libcurl is required to compile libnxml)) CFLAGS="$CFLAGS -Wall -DNXML_INTERNAL" AC_SUBST(LIBNXML_VERSION, [libnxml_version]) AC_SUBST(LIBNXML_VERSION_INFO, [libnxml_version_info]) AC_SUBST(LIBNXML_MAJOR_VERSION, [libnxml_major_version]) AC_SUBST(LIBNXML_MINOR_VERSION, [libnxml_minor_version]) AC_SUBST(LIBNXML_MICRO_VERSION, [libnxml_macro_version]) AC_OUTPUT libnxml-master/doc/000077500000000000000000000000001410202010000145355ustar00rootroot00000000000000libnxml-master/doc/Makefile.am000066400000000000000000000000501410202010000165640ustar00rootroot00000000000000#SUBDIRS = html EXTRA_DIST = main.doxy libnxml-master/doc/main.doxy000066400000000000000000000064101410202010000163670ustar00rootroot00000000000000/*!\mainpage nXml Library * * \section Author * Andrea Marchesini - Web Site: * https://github.com/bakulf/libnxml * * \section description Description * * nXML is a C library for parsing, writing and creating XML 1.0 and 1.1 files * or streams. It supports utf-8, utf-16be and utf-16le, ucs-4 (1234, 4321, * 2143, 2312). * * This library is tested on Linux, Windows, *BSD, Solaris, Minix 3. * * Why another XML library? Because it is fast, easy-to-use and -important- it * is my personal work about the second layer of the Semantic Web. Other my * softwares are based on this library. Annotea * (http://www.autistici.org/bakunin/annotea/), Morla * (http://www.autistici.org/bakunin/morla/) and libmrss * (https://github.com/bakulf/libmrss/). * * Why Nxml ? N is from * Naples a beautiful italian * city. I was there when I started to write this code. Who doesn't know * Naples, maybe he should spend some day in that city because it is full of * history, culture and wonderful people. * * \section License * * nXml is a FreeSoftware project released under * LGPL 2.0. * * \section Required * * - libcurl is a free and easy-to-use * client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, * SFTP, TFTP, TELNET, DICT, FILE and LDAP. libcurl supports SSL certificates, * HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, * cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, * Kerberos4), file transfer resume, http proxy tunneling and more! * * \section Download * * - libnxml https://github.com/bakulf/libnxml * * \section install1 Installation from source * * \code * tar xvfz libnxml-.tar.gz * cd libnxml- * ./configure --prefix=/usr * make * make install * \endcode * * \section install2 Installation from CVS * Run the following commands in a directory that you have write access to * (such as your home dirctory): * * \code * cvs -d:pserver:anonymous@ippolita.net:/home/cvs login * \endcode * (Just hit enter for the password) * \code * cvs -z3 -d:pserver:anonymous@ippolita.net:/home/cvs co libnxml * \endcode * * Once you have checked the source of libnxml, run the following commands: * * \code * cd libnxml * ./autogen.sh * ./configure --prefix=/usr * make * make install * \endcode * * When there are changes to libnxml's code, you will want to update you local * copy. Run from the directory libnxml: * * \code * cvs update -dP * ./configure --prefix=/usr * make * make install * \endcode * * \section Support * * Support for nXml is available to all via email: baku@ippolita.net. * * \section support1 Commercial Support * Commercial users of nXml are strongly encouraged to purchase a commercial * support contract, please contact me for a quotation. I will always * prioritise support requests from those with support contracts. * * \section Donations * Users not purchasing commercial support are encouraged (but not obliged) to * make a donation. By donating you are supporting the principle of open * source/free software and you will help ensure continued development of nXml. */ libnxml-master/doxy.conf.in000066400000000000000000000202721410202010000162320ustar00rootroot00000000000000# Doxyfile 0.1 #--------------------------------------------------------------------------- # Project related configuration options #--------------------------------------------------------------------------- PROJECT_NAME = @PACKAGE@ PROJECT_NUMBER = @VERSION@ OUTPUT_DIRECTORY = doc CREATE_SUBDIRS = NO OUTPUT_LANGUAGE = English USE_WINDOWS_ENCODING = NO BRIEF_MEMBER_DESC = YES REPEAT_BRIEF = YES ABBREVIATE_BRIEF = ALWAYS_DETAILED_SEC = NO INLINE_INHERITED_MEMB = NO FULL_PATH_NAMES = NO STRIP_FROM_PATH = STRIP_FROM_INC_PATH = SHORT_NAMES = NO JAVADOC_AUTOBRIEF = NO MULTILINE_CPP_IS_BRIEF = NO DETAILS_AT_TOP = YES INHERIT_DOCS = NO DISTRIBUTE_GROUP_DOC = NO SEPARATE_MEMBER_PAGES = NO TAB_SIZE = 8 ALIASES = OPTIMIZE_OUTPUT_FOR_C = YES OPTIMIZE_OUTPUT_JAVA = NO SUBGROUPING = YES #--------------------------------------------------------------------------- # Build related configuration options #--------------------------------------------------------------------------- EXTRACT_ALL = YES EXTRACT_PRIVATE = YES EXTRACT_STATIC = YES EXTRACT_LOCAL_CLASSES = NO EXTRACT_LOCAL_METHODS = NO HIDE_UNDOC_MEMBERS = NO HIDE_UNDOC_CLASSES = NO HIDE_FRIEND_COMPOUNDS = NO HIDE_IN_BODY_DOCS = NO INTERNAL_DOCS = NO CASE_SENSE_NAMES = YES HIDE_SCOPE_NAMES = NO SHOW_INCLUDE_FILES = YES INLINE_INFO = YES SORT_MEMBER_DOCS = YES SORT_BRIEF_DOCS = NO SORT_BY_SCOPE_NAME = NO GENERATE_TODOLIST = YES GENERATE_TESTLIST = YES GENERATE_BUGLIST = YES GENERATE_DEPRECATEDLIST= YES ENABLED_SECTIONS = MAX_INITIALIZER_LINES = 30 SHOW_USED_FILES = YES SHOW_DIRECTORIES = YES FILE_VERSION_FILTER = #--------------------------------------------------------------------------- # configuration options related to warning and progress messages #--------------------------------------------------------------------------- QUIET = NO WARNINGS = YES WARN_IF_UNDOCUMENTED = YES WARN_IF_DOC_ERROR = YES WARN_NO_PARAMDOC = NO WARN_FORMAT = "$file:$line: $text" WARN_LOGFILE = #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- INPUT = src/ \ doc/ FILE_PATTERNS = nxml.h \ main.doxy RECURSIVE = YES EXCLUDE = EXCLUDE_SYMLINKS = NO EXCLUDE_PATTERNS = EXAMPLE_PATH = EXAMPLE_PATTERNS = EXAMPLE_RECURSIVE = NO IMAGE_PATH = INPUT_FILTER = FILTER_PATTERNS = FILTER_SOURCE_FILES = NO #--------------------------------------------------------------------------- # configuration options related to source browsing #--------------------------------------------------------------------------- SOURCE_BROWSER = YES INLINE_SOURCES = YES STRIP_CODE_COMMENTS = NO REFERENCED_BY_RELATION = YES REFERENCES_RELATION = YES USE_HTAGS = NO VERBATIM_HEADERS = YES #--------------------------------------------------------------------------- # configuration options related to the alphabetical class index #--------------------------------------------------------------------------- ALPHABETICAL_INDEX = NO COLS_IN_ALPHA_INDEX = 5 IGNORE_PREFIX = #--------------------------------------------------------------------------- # configuration options related to the HTML output #--------------------------------------------------------------------------- GENERATE_HTML = YES HTML_OUTPUT = html HTML_FILE_EXTENSION = .html HTML_HEADER = HTML_FOOTER = HTML_STYLESHEET = HTML_ALIGN_MEMBERS = YES GENERATE_HTMLHELP = NO CHM_FILE = HHC_LOCATION = GENERATE_CHI = NO BINARY_TOC = NO TOC_EXPAND = NO DISABLE_INDEX = NO ENUM_VALUES_PER_LINE = 4 GENERATE_TREEVIEW = NO TREEVIEW_WIDTH = 250 #--------------------------------------------------------------------------- # configuration options related to the LaTeX output #--------------------------------------------------------------------------- GENERATE_LATEX = NO LATEX_OUTPUT = latex LATEX_CMD_NAME = latex MAKEINDEX_CMD_NAME = makeindex COMPACT_LATEX = NO PAPER_TYPE = a4wide EXTRA_PACKAGES = LATEX_HEADER = PDF_HYPERLINKS = NO USE_PDFLATEX = NO LATEX_BATCHMODE = NO LATEX_HIDE_INDICES = NO #--------------------------------------------------------------------------- # configuration options related to the RTF output #--------------------------------------------------------------------------- GENERATE_RTF = NO RTF_OUTPUT = rtf COMPACT_RTF = NO RTF_HYPERLINKS = NO RTF_STYLESHEET_FILE = RTF_EXTENSIONS_FILE = #--------------------------------------------------------------------------- # configuration options related to the man page output #--------------------------------------------------------------------------- GENERATE_MAN = NO MAN_OUTPUT = man MAN_EXTENSION = .3 MAN_LINKS = NO #--------------------------------------------------------------------------- # configuration options related to the XML output #--------------------------------------------------------------------------- GENERATE_XML = NO XML_OUTPUT = xml XML_SCHEMA = XML_DTD = XML_PROGRAMLISTING = YES #--------------------------------------------------------------------------- # configuration options for the AutoGen Definitions output #--------------------------------------------------------------------------- GENERATE_AUTOGEN_DEF = NO #--------------------------------------------------------------------------- # configuration options related to the Perl module output #--------------------------------------------------------------------------- GENERATE_PERLMOD = NO PERLMOD_LATEX = NO PERLMOD_PRETTY = YES PERLMOD_MAKEVAR_PREFIX = #--------------------------------------------------------------------------- # Configuration options related to the preprocessor #--------------------------------------------------------------------------- ENABLE_PREPROCESSING = YES MACRO_EXPANSION = YES EXPAND_ONLY_PREDEF = NO SEARCH_INCLUDES = YES INCLUDE_PATH = INCLUDE_FILE_PATTERNS = PREDEFINED = EXPAND_AS_DEFINED = SKIP_FUNCTION_MACROS = YES #--------------------------------------------------------------------------- # Configuration::additions related to external references #--------------------------------------------------------------------------- TAGFILES = GENERATE_TAGFILE = ALLEXTERNALS = NO EXTERNAL_GROUPS = YES PERL_PATH = /usr/bin/perl #--------------------------------------------------------------------------- # Configuration options related to the dot tool #--------------------------------------------------------------------------- CLASS_DIAGRAMS = NO HIDE_UNDOC_RELATIONS = YES HAVE_DOT = YES CLASS_GRAPH = NO COLLABORATION_GRAPH = YES GROUP_GRAPHS = YES UML_LOOK = NO TEMPLATE_RELATIONS = YES INCLUDE_GRAPH = YES INCLUDED_BY_GRAPH = YES CALL_GRAPH = YES GRAPHICAL_HIERARCHY = YES DIRECTORY_GRAPH = YES DOT_IMAGE_FORMAT = png DOT_PATH = /usr/bin/dot DOTFILE_DIRS = MAX_DOT_GRAPH_WIDTH = 1024 MAX_DOT_GRAPH_HEIGHT = 1024 MAX_DOT_GRAPH_DEPTH = 1000 DOT_TRANSPARENT = NO DOT_MULTI_TARGETS = NO GENERATE_LEGEND = YES DOT_CLEANUP = YES #--------------------------------------------------------------------------- # Configuration::additions related to the search engine #--------------------------------------------------------------------------- SEARCHENGINE = NO libnxml-master/nxml.pc.in000066400000000000000000000004031410202010000156740ustar00rootroot00000000000000prefix=@prefix@ exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=@includedir@ Name: libnxml Description: library C for creating, parsing and writing xml files or streams Version: @VERSION@ Requires: Libs: -L${libdir} -lnxml Cflags: -I${includedir} libnxml-master/src/000077500000000000000000000000001410202010000145575ustar00rootroot00000000000000libnxml-master/src/Makefile.am000066400000000000000000000006121410202010000166120ustar00rootroot00000000000000lib_LTLIBRARIES = libnxml.la libnxml_la_SOURCES = \ nxml_parser.c \ nxml_download.c \ nxml_init.c \ nxml_error.c \ nxml_free.c \ nxml_write.c \ nxml_edit.c \ nxml_utf.c \ nxml_string.c \ nxml_easy.c \ nxml_tools.c \ nxml_namespace.c libnxml_la_LDFLAGS = -version-info @LIBNXML_VERSION_INFO@ EXTRA_DIST = nxml.h.in nxml.h nxml_internal.h incdir = $(includedir) inc_DATA = nxml.h libnxml-master/src/nxml.h.in000066400000000000000000000675271410202010000163340ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef __N_XML_H__ #define __N_XML_H__ #include #include #include #include #include #include #include #include #include #define LIBNXML_VERSION_STRING "@VERSION@" #define LIBNXML_MAJOR_VERSION @LIBNXML_MAJOR_VERSION@ #define LIBNXML_MINOR_VERSION @LIBNXML_MINOR_VERSION@ #define LIBNXML_MICRO_VERSION @LIBNXML_MICRO_VERSION@ #ifdef __cplusplus extern "C" { #endif typedef struct nxml_t nxml_t; typedef struct nxml_data_t nxml_data_t; typedef struct nxml_attr_t nxml_attr_t; typedef struct nxml_doctype_t nxml_doctype_t; typedef struct nxml_namespace_t nxml_namespace_t; typedef struct __nxml_private_t __nxml_private_t; typedef struct __nxml_entity_t __nxml_entity_t; /** This enum describes the error type of libnxml */ typedef enum { NXML_OK = 0, /**< No error */ NXML_ERR_POSIX, /**< For the correct error, use errno */ NXML_ERR_PARSER, /**< Parser error */ NXML_ERR_DOWNLOAD, /**< Download error */ NXML_ERR_DATA /**< The parameters are incorrect */ } nxml_error_t; /** This enum describes the type of data element of libnxml */ typedef enum { NXML_TYPE_TEXT, /**< Text element */ NXML_TYPE_COMMENT, /**< Comment element */ NXML_TYPE_ELEMENT, /**< Data element */ NXML_TYPE_PI, /**< PI element */ NXML_TYPE_ELEMENT_CLOSE /**< Data element - For internal use only */ } nxml_type_t; /** This enum describes the supported XML version */ typedef enum { NXML_VERSION_1_1, /**< XML 1.1 */ NXML_VERSION_1_0 /**< XML 1.0 */ } nxml_version_t; /** This enum describes the CharSet of XML document */ typedef enum { NXML_CHARSET_UTF8, /**< UTF8 chatset detected */ NXML_CHARSET_UTF16LE, /**< UTF 16 Little Endian detected */ NXML_CHARSET_UTF16BE, /**< UTF 16 Big Endian detected */ NXML_CHARSET_UCS4_1234, /**< UCS 4byte order 1234 detected */ NXML_CHARSET_UCS4_4321, /**< UCS 3byte order 4321 detected */ NXML_CHARSET_UCS4_2143, /**< UCS 3byte order 2143 detected */ NXML_CHARSET_UCS4_3412, /**< UCS 3byte order 3412 detected */ NXML_CHARSET_UNKNOWN /**< Unknown format */ } nxml_charset_t; /** * Data struct for any element of XML stream/files * * \brief * Data struct for any element of XML streams/files */ struct nxml_data_t { nxml_type_t type; /**< type of this nxml_data_t struct */ char *value; /**< The value of this data struct */ nxml_attr_t *attributes; /**< List of attributes of this struct. This list exists only if type == NXML_TYPE_ELEMENT */ nxml_namespace_t *ns; /**< Pointer to the correct namespace */ nxml_namespace_t *ns_list; /**< The namespaces in this element */ nxml_data_t *children; /**< The children of this data struct */ nxml_data_t *next; /**< The next element */ nxml_data_t *parent; /**< The parent */ nxml_t *doc; /**< The nxml_t */ }; /** * Data struct for any element of attribute of xml element * * \brief * Data struct for any element of attribute of xml element */ struct nxml_attr_t { char *name; char *value; nxml_namespace_t *ns; nxml_attr_t *next; }; /** * Data struct for doctype elements * * \brief * Data struct for doctype elements */ struct nxml_doctype_t { char *value; /**< The string no parsers */ char *name; /**< The name of current doctype */ nxml_t *doc; /**< The nxml_t */ nxml_doctype_t *next; }; /** * Data struct for namespace * * \brief * Data struct for namespace */ struct nxml_namespace_t { char *prefix; char *ns; nxml_namespace_t *next; }; /** Data struct private about entities for internal use only * * \brief * Data struct private about entities for internal use only */ struct __nxml_entity_t { char *name; char *entity; __nxml_entity_t *next; }; /** Data struct private for internal use only * * \brief * Data struct private for internal use only */ struct __nxml_private_t { void (*func) (char *, ...); int line; int timeout; char *proxy; char *proxy_authentication; char *cacert; char *certfile; char *password; int verifypeer; char *authentication; char *user_agent; char textindent; CURLcode curl_error; __nxml_entity_t *entities; }; /** * Principal data struct. It describes a XML document and it contains pointers * to any other structures. * * \brief * Principal data struct. It describes a XML document and it contains pointers * to any other structures */ struct nxml_t { char *file; /**< XML document filename or url */ size_t size; /**< Size of XML document in byte */ nxml_version_t version; /**< XML document version */ int standalone; /**< This document is standalone ? */ char *encoding; /**< Encoding type */ nxml_charset_t charset_detected; /**< charset detected when the a XML document is parsed. The document will be convert to UTF-8 */ nxml_data_t *data; /**< The data of XML document */ nxml_doctype_t *doctype; /**< The doctype of XML document */ __nxml_private_t priv; /**< For internal use only */ }; /* INIT FUNCTIONS ************************************************************/ /** * This function creates a new nxml_t data struct. * * \param nxml Pointer to a nxml_t data struct. It will be allocated. * \return the error code */ nxml_error_t nxml_new (nxml_t ** nxml); /** * This function creates a new nxml_data_t child of a parent in the data * struct. If parent is NULL the child will be created in the root level * of XML document. * * \param nxml Pointer to a nxml_t data struct. * \param parent The parent of new data struct child. If it is NULL, the * child is in the root level. * \param child It is the pointer to the new data struct. If *child is NULL, * it will be allocated, else it will be insert as it is. * \return the error code * * \code * nxml_data_t *data1, *data2; * data1=NULL; * nxml_add(nxml, NULL, &data1); * * data2=(nxml_data_t *)malloc(sizeof(nxml_data_t)); * nxml_add(nxml, NULL, &data2); * \endcode */ nxml_error_t nxml_add (nxml_t * nxml, nxml_data_t *parent, nxml_data_t **child); /** * This function removes a nxml_data_t child from a parent in the data * struct. If parent is NULL the child will be removed in the root level of * XML document. This function doesn't free the child. If you want you can * reinsert the child in another parent tree or use the nxml_free_data * function. * * \param nxml Pointer to a nxml_t data struct. * \param parent The parent of data struct child. If it is NULL, the * child will be searched in the root level. * \param child It is the pointer to the child that you want remove * \return the error code */ nxml_error_t nxml_remove (nxml_t * nxml, nxml_data_t *parent, nxml_data_t *child); /** * This function creates a new nxml_attr_t data of a element in the data * struct. * * \param nxml Pointer to a nxml_t data struct. * \param element The element of the new data struct attribute. * \param attribute The pointer to the your data struct. If it is NULL it will * be allocated, else no. * \return the error code */ nxml_error_t nxml_add_attribute (nxml_t *nxml, nxml_data_t *element, nxml_attr_t **attribute); /** * This function removes a nxml_attr_t data of a element. It does not free it * so you can reinsert o free it with nxml_free_attribute. * * \param nxml Pointer to a nxml_t data struct. * \param element The element that contains the attribute * \param attribute The attribute that you want remove. * \return the error code */ nxml_error_t nxml_remove_attribute (nxml_t *nxml, nxml_data_t *element, nxml_attr_t *attribute); /** * This function adds a nxml_namespace_t data in a nxml document. * * \param nxml Pointer to a nxml_t data struct. * \param element The element of the new data struct namespace. * \param ns The namespace that you want add * \return the error code */ nxml_error_t nxml_add_namespace (nxml_t *nxml, nxml_data_t *element, nxml_namespace_t **ns); /** * This function removes a nxml_namespace_t data from a nxml document. * * \param nxml Pointer to a nxml_t data struct. * \param element The element of the new data struct namespace. * \param ns The namespace that you want remove * \return the error code */ nxml_error_t nxml_remove_namespace (nxml_t *nxml, nxml_data_t *element, nxml_namespace_t *ns); /** * This function sets the output function. If you set your function, the * parser'll write the error by this function. As default there is not a * function. If you want tou can set 'nxml_print_general' function that * print to stderr. * * \param nxml The struct create with nxml_new. * \param func Your function. If you don't want the function, set it to NULL. * As default a nxml_t element has not a output function. * \return the error code */ nxml_error_t nxml_set_func (nxml_t * nxml, void (*func) (char *, ...)); void nxml_print_generic (char *, ...); /** * This function sets the timeout in seconds for the download of a remote * XML document. Default is 0 and 0 is no timeout. * * \param nxml The struct create with nxml_new. * \param seconds the timeout in seconds * \return the error code */ nxml_error_t nxml_set_timeout (nxml_t * nxml, int seconds); /** * This functions sets a proxy server for the downloading procedure. * * \param nxml The struct create with nxml_new. * \param proxy the proxy as a string * \param userpwd the user and password in this format user:password * \return the error code */ nxml_error_t nxml_set_proxy (nxml_t * nxml, char *proxy, char *userpwd); /** * This functions sets a user/password for a for the download procedure. * * \param nxml The struct create with nxml_new. * \param userpwd the user and password in this format user:password * \return the error code */ nxml_error_t nxml_set_authentication (nxml_t * nxml, char *userpwd); /** * This functions sets an user agent for a for the download procedure. * * \param nxml The struct create with nxml_new. * \param user_agent The agent * \return the error code */ nxml_error_t nxml_set_user_agent (nxml_t * nxml, char *user_agent); /** * This functions sets a certificate in the http request. You can set a * certificate file and a password. * * \param nxml The struct create with nxml_new. * \param certfile the certfile for the ssl connection (can be NULL) * \param password the password of your certifcate (can be NULL) * \param cacert the CA certificate to verify peer against (can be NULL) * \param verifypeer active/deactive the peer validation * \return the error code */ nxml_error_t nxml_set_certificate (nxml_t * nxml, char *certfile, char *password, char *cacert, int verifypeer); /** * This function (de)actives the indent of the TEXT elements. Default it is * activated. * * \param nxml The struct create with nxml_new * \param textindent If it is != 0, the indent will be activated * \return the error code */ nxml_error_t nxml_set_textindent (nxml_t *nxml, char textindent); /* DOWNLOAD *****************************************************************/ /** * This function downloads a stream from a http/https/ftp server. * * \param nxml The struct create with nxml_new. * \param url the http file * \param buffer a string for the buffer * \param size The function sets here the length of the file if it's not NULL. * \return a buffer or NULL */ nxml_error_t nxml_download_file (nxml_t *nxml, char *url, char ** buffer, size_t *size); /* PARSER FUNCTIONS *********************************************************/ /** * This function parses a url. It downloads a url with curl library and * parses it. * * \param nxml the struct create with nxml_new. * \param url the url that you want parse. * \return the error code */ nxml_error_t nxml_parse_url (nxml_t * nxml, char *url); /** * This function parses a file. * * \param nxml the struct create with nxml_new. * \param file the file that you want parse. * \return the error code */ nxml_error_t nxml_parse_file (nxml_t * nxml, char *file); /** * This function parses a buffer in memory. * * \param nxml the struct create with nxml_new. * \param buffer the buffer that you want parse. * \param size the size of buffer. If size is 0, the function checks the * length of your buffer searching a '\\0'. * \return the error code */ nxml_error_t nxml_parse_buffer (nxml_t * nxml, char *buffer, size_t size); /* WRITE FUNCTIONS **********************************************************/ /** * This function writes the data struct in a local file. * * \param nxml the nxml data strut * \param file the local file * \return the error code */ nxml_error_t nxml_write_file (nxml_t *nxml, char *file); /** * This function writes the data struct in a buffer. * * \code * char *buffer; * buffer=NULL; // This is important! * nxml_write_buffer(nxml, &buffer); * \endcode * * The buffer must be NULL. * * \param nxml * \param buffer the memory buffer * \return the error code */ nxml_error_t nxml_write_buffer (nxml_t *nxml, char **buffer); /* FREE FUNCTIONS ************************************************************/ /** * This function removes the data in a structure nxml_t and makes it clean for * another usage. * * \param nxml the pointer to you data struct. * \return the error code. */ nxml_error_t nxml_empty (nxml_t * nxml); /** * This function frees the memory of a nxml_t *element. After the free, * your data struct is not useful. If you want erase the internal data, use * nxml_empty function * * \param nxml the pointer to your data struct. * \return the error code. */ nxml_error_t nxml_free (nxml_t * nxml); /** * This function frees the memory of a nxml_data_t *element and any its * children and its attributes. * * \param data the pointer to you data struct. * \return the error code */ nxml_error_t nxml_free_data (nxml_data_t *data); /** * This function frees the memory of a nxml_attr_t *element. * * \param data the pointer to you data struct. * \return the error code */ nxml_error_t nxml_free_attribute (nxml_attr_t *data); /** * This function frees the memory of a nxml_namespace_t *element. * * \param data the pointer to you data struct. * \return the error code */ nxml_error_t nxml_free_namespace (nxml_namespace_t *data); /* EDIT FUNCTIONS ***********************************************************/ /** * This function returns the root element of xml data struct. * * \code * nxml_t *nxml; * nxml_data_t *root; * * nxml_new(&nxml); * nxml_parser_file(nxml, "file.xml"); * nxml_root_element(nxml, &root); * printf("%p\n",root); * nxml_free(nxml); * \endcode * * \param nxml the data struct * \param element the pointer to your nxml_data_t struct * \return the error code */ nxml_error_t nxml_root_element (nxml_t *nxml, nxml_data_t **element); /** * This function searchs the request element in the children of the data struct. * * \code * nxml_t *nxml; * nxml_data_t *root; * * nxml_new(&nxml); * nxml_parser_file(nxml, "file.xml"); * nxml_find_element(nxml, NULL, "hello_world", &root); * printf("%p\n",root); * nxml_free(nxml); * \endcode * * \param nxml the data struct * \param parent the data struct nxml_data_t of parent. If it is NULL, this * function searchs in the root element level. * \param name the name of the node that you want. * \param element the pointer to your nxml_data_t struct. If element will be * NULL, the item that you want does not exist. * \return the error code */ nxml_error_t nxml_find_element (nxml_t *nxml, nxml_data_t *parent, char *name, nxml_data_t **element); /** * This function searchs the first doctype element in the nxml_t document. * * \param nxml the data struct * \param doctype the pointer to your nxml_doctype_t struct. If element will be * NULL, the item that you want does not exist. * \return the error code */ nxml_error_t nxml_doctype_element (nxml_t *nxml, nxml_doctype_t **doctype); /** * This function searchs the request attribute and returns its values. * * \code * nxml_t *nxml; * nxml_data_t *root; * * nxml_new(&nxml); * nxml_parser_file(nxml, "file.xml"); * nxml_find_element(nxml, NULL, "hello_world", &root); * if(root) { * nxml_attr_t *attribute=NULL; * nxml_find_attribute(root, "attribute", &attribute); * * if(attribute) * printf("%s\n",attribute->value); * } * nxml_free(nxml); * \endcode * * \param data the data struct * \param name the attribute that you want search * \param attribute the pointer to your nxml_attr_t struct. If attribute will * be NULL, the attribute that you want does not exist. * does not exist. * \return the error code */ nxml_error_t nxml_find_attribute (nxml_data_t *data, char *name, nxml_attr_t **attribute); /** * This function searchs the request namespaceibute and returns its values. * * \param data the data struct * \param name the namespace that you want search * \param ns the pointer to your nxml_attr_t struct. If namespace will * be NULL, the namespace that you want does not exist. * does not exist. * \return the error code */ nxml_error_t nxml_find_namespace (nxml_data_t *data, char *name, nxml_namespace_t **ns); /** * This function returns the string of a XML element. * \code * nxml_t *nxml; * nxml_data_t *root; * char *str; * * nxml_new(&nxml); * nxml_parser_file(nxml, "file.xml"); * nxml_find_element(nxml, NULL, "hello_world", &root); * if(root) { * nxml_get_string(root, &str); * if(str) { * printf("Hello_world item contains: %s\n",str); * free(str); * } * } * nxml_free(nxml); * \endcode * * \param element the xnml_data_t pointer * \param string the pointer to you char *. You must free it after usage. * \return the error code */ nxml_error_t nxml_get_string (nxml_data_t *element, char **string); /* ERROR FUNCTIONS **********************************************************/ /** * This function returns a static string with the description of error code * * \param nxml the pointer to data struct * \param err the error code that you need as string * \return a string. Don't free this string! */ char * nxml_strerror (nxml_t * nxml, nxml_error_t err); /** * This function returns the CURLcode error if there was a problem about the * downloading procedure: * * \param nxml the pointer to data struct * \param err the error code that you need as string * \return the CURLcode */ CURLcode nxml_curl_error (nxml_t * nxml, nxml_error_t err); /** * This function return the line of a error of parse. * * \param nxml the pointer to data struct * \param line pointer to your integer. In this pointer will be set the line. * \return the error code */ nxml_error_t nxml_line_error (nxml_t * nxml, int *line); /* EASY FUNCTIONS ***********************************************************/ /** * This function returns a new nxml_t data. * * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_t data. If it returns NULL, read the err * code. This function use nxml_set_func with nxml_print_generic so the * error will be write in the standard output. */ nxml_t * nxmle_new_data (nxml_error_t *err); /** * This function returns a new nxml_t data and parses a remote url document * from http or ftp protocol. This function use nxml_set_func with * nxml_print_generic so the error will be write in the standard output. * * \param url the url that you want parse. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_t data. */ nxml_t * nxmle_new_data_from_url (char *url, nxml_error_t *err); /** * This function returns a new nxml_t data and parses a local file. This * function use nxml_set_func with nxml_print_generic so the error will be * write in the standard output. * * \param file the file that you want parse. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_t data. */ nxml_t * nxmle_new_data_from_file (char *file, nxml_error_t *err); /** * This function returns a new nxml_t data and parses a buffer. This * function use nxml_set_func with nxml_print_generic so the error will be * write in the standard output. * * \param buffer the buffer that you want parse. * \param size the size of buffer. If size is 0, the function checks the * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_t data. */ nxml_t * nxmle_new_data_from_buffer (char *buffer, size_t size, nxml_error_t *err); /** * This function creates and adds a child nxml_data_t to a parent in your * nxml data struct. * * \param nxml Pointer to your nxml data. * \param parent The parent of new data struct child. If it is NULL, the * child is in the root level. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_data_t * nxmle_add_new (nxml_t * nxml, nxml_data_t *parent, nxml_error_t *err); /** * This function adds a your nxml_data_t to a parent in your nxml * data struct. * * \param nxml Pointer to your nxml data. * \param parent The parent of new data struct child. If it is NULL, the * child is in the root level. * \param child The you child nxml_data_t struct that you want insert. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_data_t * nxmle_add_data (nxml_t * nxml, nxml_data_t *parent, nxml_data_t *child, nxml_error_t *err); /** * This function creates and adds an attribute nxml_attr_t data to a * nxml_data_t struct in your nxml data struct. * * \param nxml Pointer to your nxml data. * \param element The parent of new nxml_attr_t struct. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_attr_t * nxmle_add_attribute_new (nxml_t *nxml, nxml_data_t *element, nxml_error_t *err); /** * This function adds an attribute nxml_attr_t data to a * nxml_data_t struct in your nxml data struct. * * \param nxml Pointer to your nxml data. * \param element The parent of your nxml_attr_t struct. * \param attribute Your attribute element. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_attr_t * nxmle_add_attribute_data (nxml_t *nxml, nxml_data_t *element, nxml_attr_t *attribute, nxml_error_t *err); /** * This function creates and adds a namespace nxml_namespace_t data to a * nxml data struct. * * \param nxml Pointer to your nxml data. * \param element The element of in witch you want add the namespace. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_namespace_t * nxmle_add_namespace_new (nxml_t *nxml, nxml_data_t *element, nxml_error_t *err); /** * This function adds an namespace nxml_namespace-t data to a nxml data struct. * * \param nxml Pointer to your nxml data. * \param element The element of in witch you want add the namespace. * \param ns Your namespace element. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to a new nxml_data_t data child. */ nxml_namespace_t * nxmle_add_namespace_data (nxml_t *nxml, nxml_data_t *element, nxml_namespace_t *ns, nxml_error_t *err); /** * This function returns the root element of a nxml_t. * * \param nxml Pointer to your nxml data. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to the root element. If NULL the element does not * exist. */ nxml_data_t * nxmle_root_element (nxml_t *nxml, nxml_error_t *err); /** * This function returns the first doctype element of a nxml_t. * * \param nxml Pointer to your nxml data. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to the doctype element. If NULL the element does not * exist. */ nxml_doctype_t *nxmle_doctype_element (nxml_t *nxml, nxml_error_t *err); /** * This function returns the nxml_data_t pointer to a element by * a name. * * \param nxml Pointer to your nxml data. * \param parent Pointer to your nxml_data_t parent. If it is NULL, this * function searchs in a root element level. * \param name The name of element that you want. * \param err If err is not NULL, err will be set to the error flag. * \return the pointer to the root element. If NULL the element does not * exist. */ nxml_data_t * nxmle_find_element (nxml_t *nxml, nxml_data_t *parent, char *name, nxml_error_t *err); /** * This function returns the value of a attribute by a name. * * \param element Pointer to your nxml_data_t. * \param name The name of attribute that you want. * \param err If err is not NULL, err will be set to the error flag. * \return a pointer to a char allocated so you must free it after usage. If * it is NULL, the attribute does not exist. */ char * nxmle_find_attribute (nxml_data_t *element, char *name, nxml_error_t *err); /** * This function returns the value of a namespace by a name. * * \param element Pointer to your nxml_data_t. * \param name The name of namespace that you want. * \param err If err is not NULL, err will be set to the error flag. * \return a pointer to a char allocated so you must free it after usage. If * it is NULL, the namespace does not exist. */ char * nxmle_find_namespace (nxml_data_t *element, char *name, nxml_error_t *err); /** * This function returns the contain of a element. * * \param element Pointer to your nxml_data_t. * \param err If err is not NULL, err will be set to the error flag. * \return a pointer to a char allocated so you must free it after usage. If * it is NULL, the attribute does not exist. */ char * nxmle_get_string (nxml_data_t *element, nxml_error_t *err); /** * This function writes the data struct in a buffer. * * \param nxml * \param err If err is not NULL, err will be set to the error flag. * \return a pointer to a char allocated so you must free it after usage. */ char * nxmle_write_buffer (nxml_t *nxml, nxml_error_t *err); /** * This function return the line of a error of parse. * \param nxml the pointer to data struct * \param err If err is not NULL, err will be set to the error flag. * \return the line with the error. */ int nxmle_line_error (nxml_t * nxml, nxml_error_t *err); /* Easy functions defined: */ #define nxmle_remove nxml_remove #define nxmle_remove_attribute nxml_remove_attribute #define nxmle_remove_namespace nxml_remove_namespace #define nxmle_write_file nxml_write_file #define nxmle_empty nxml_empty #define nxmle_free nxml_free #define nxmle_free_data nxml_free_data #define nxmle_free_attribute nxml_free_attribute #define nxmle_strerror nxml_strerror #ifdef NXML_INTERNAL # include "nxml_internal.h" #endif #ifdef __cplusplus } #endif #endif /* EOF */ libnxml-master/src/nxml_download.c000066400000000000000000000071751410202010000176020ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" typedef struct __nxml_download_t__ __nxml_download_t; struct __nxml_download_t__ { char *mm; size_t size; }; static size_t __nxml_memorize_file(void *ptr, size_t size, size_t nmemb, void *data) { register int realsize = size * nmemb; __nxml_download_t *mem = (__nxml_download_t *)data; if (!mem->mm) { if (!(mem->mm = (char *)malloc(realsize + 1))) return -1; } else { if (!(mem->mm = (char *)realloc(mem->mm, mem->size + realsize + 1))) return -1; } memcpy(&(mem->mm[mem->size]), ptr, realsize); mem->size += realsize; mem->mm[mem->size] = 0; return realsize; } nxml_error_t nxml_download_file(nxml_t *nxml, char *fl, char **buffer, size_t *size) { __nxml_download_t *chunk; CURL *curl; CURLcode ret; if (!fl || !buffer || !nxml) return NXML_ERR_DATA; if (!(chunk = (__nxml_download_t *)malloc(sizeof(__nxml_download_t)))) return NXML_ERR_POSIX; chunk->mm = NULL; chunk->size = 0; curl_global_init(CURL_GLOBAL_DEFAULT); if (!(curl = curl_easy_init())) { free(chunk); return NXML_ERR_POSIX; } curl_easy_setopt(curl, CURLOPT_URL, fl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, __nxml_memorize_file); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); curl_easy_setopt(curl, CURLOPT_FILE, (void *)chunk); curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip, deflate"); if (nxml->priv.timeout) curl_easy_setopt(curl, CURLOPT_TIMEOUT, nxml->priv.timeout); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, !nxml->priv.verifypeer); if (nxml->priv.certfile) { curl_easy_setopt(curl, CURLOPT_SSLCERT, nxml->priv.certfile); if (nxml->priv.password) curl_easy_setopt(curl, CURLOPT_SSLCERTPASSWD, nxml->priv.password); if (nxml->priv.cacert) curl_easy_setopt(curl, CURLOPT_CAINFO, nxml->priv.cacert); } if (nxml->priv.authentication) curl_easy_setopt(curl, CURLOPT_USERPWD, nxml->priv.authentication); if (nxml->priv.proxy) { curl_easy_setopt(curl, CURLOPT_PROXY, nxml->priv.proxy); if (nxml->priv.proxy_authentication) curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, nxml->priv.proxy_authentication); } if (nxml->priv.user_agent) curl_easy_setopt(curl, CURLOPT_USERAGENT, nxml->priv.user_agent); if ((ret = curl_easy_perform(curl))) { if (chunk->mm) free(chunk->mm); free(chunk); nxml->priv.curl_error = ret; curl_easy_cleanup(curl); return NXML_ERR_DOWNLOAD; } curl_easy_cleanup(curl); *buffer = chunk->mm; if (size) *size = chunk->size; free(chunk); return NXML_OK; } /* EOF */ libnxml-master/src/nxml_easy.c000066400000000000000000000155021410202010000167250ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" nxml_t *nxmle_new_data(nxml_error_t *err) { nxml_t *data = NULL; nxml_error_t ret; ret = nxml_new(&data); if (err) *err = ret; if (ret != NXML_OK) return NULL; return data; } nxml_t *nxmle_new_data_from_url(char *url, nxml_error_t *err) { nxml_t *data = NULL; nxml_error_t ret; ret = nxml_new(&data); if (err) *err = ret; if (ret != NXML_OK) return NULL; nxml_set_func(data, nxml_print_generic); ret = nxml_parse_url(data, url); if (err) *err = ret; if (ret != NXML_OK) { nxml_free(data); return NULL; } return data; } nxml_t *nxmle_new_data_from_file(char *file, nxml_error_t *err) { nxml_t *data = NULL; nxml_error_t ret; ret = nxml_new(&data); if (err) *err = ret; if (ret != NXML_OK) return NULL; nxml_set_func(data, nxml_print_generic); ret = nxml_parse_file(data, file); if (err) *err = ret; if (ret != NXML_OK) { nxml_free(data); return NULL; } return data; } nxml_t *nxmle_new_data_from_buffer(char *buffer, size_t size, nxml_error_t *err) { nxml_t *data = NULL; nxml_error_t ret; ret = nxml_new(&data); if (err) *err = ret; if (ret != NXML_OK) return NULL; nxml_set_func(data, nxml_print_generic); ret = nxml_parse_buffer(data, buffer, size); if (err) *err = ret; if (ret != NXML_OK) { nxml_free(data); return NULL; } return data; } nxml_data_t *nxmle_add_new(nxml_t *nxml, nxml_data_t *parent, nxml_error_t *err) { nxml_error_t ret; nxml_data_t *child = NULL; ret = nxml_add(nxml, parent, &child); if (err) *err = ret; if (ret != NXML_OK) return NULL; return child; } nxml_data_t *nxmle_add_data(nxml_t *nxml, nxml_data_t *parent, nxml_data_t *child, nxml_error_t *err) { nxml_error_t ret; if (!child) { if (err) *err = NXML_ERR_DATA; return NULL; } ret = nxml_add(nxml, parent, &child); if (err) *err = ret; if (ret != NXML_OK) return NULL; return child; } nxml_attr_t *nxmle_add_attribute_new(nxml_t *nxml, nxml_data_t *element, nxml_error_t *err) { nxml_error_t ret; nxml_attr_t *attribute = NULL; ret = nxml_add_attribute(nxml, element, &attribute); if (err) *err = ret; if (ret != NXML_OK) return NULL; return attribute; } nxml_attr_t *nxmle_add_attribute_data(nxml_t *nxml, nxml_data_t *element, nxml_attr_t *attribute, nxml_error_t *err) { nxml_error_t ret; if (!attribute) { if (err) *err = NXML_ERR_DATA; return NULL; } ret = nxml_add_attribute(nxml, element, &attribute); if (err) *err = ret; if (ret != NXML_OK) return NULL; return attribute; } nxml_namespace_t *nxmle_add_namespace_new(nxml_t *nxml, nxml_data_t *element, nxml_error_t *err) { nxml_error_t ret; nxml_namespace_t *namespace = NULL; ret = nxml_add_namespace(nxml, element, &namespace); if (err) *err = ret; if (ret != NXML_OK) return NULL; return namespace; } nxml_namespace_t *nxmle_add_namespace_data(nxml_t *nxml, nxml_data_t *element, nxml_namespace_t *namespace, nxml_error_t *err) { nxml_error_t ret; if (!namespace) { if (err) *err = NXML_ERR_DATA; return NULL; } ret = nxml_add_namespace(nxml, element, &namespace); if (err) *err = ret; if (ret != NXML_OK) return NULL; return namespace; } nxml_data_t *nxmle_root_element(nxml_t *nxml, nxml_error_t *err) { nxml_data_t *root; nxml_error_t ret; ret = nxml_root_element(nxml, &root); if (err) *err = ret; if (ret != NXML_OK) return NULL; return root; } nxml_doctype_t *nxmle_doctype_element(nxml_t *nxml, nxml_error_t *err) { nxml_doctype_t *doctype; nxml_error_t ret; ret = nxml_doctype_element(nxml, &doctype); if (err) *err = ret; if (ret != NXML_OK) return NULL; return doctype; } nxml_data_t *nxmle_find_element(nxml_t *nxml, nxml_data_t *data, char *name, nxml_error_t *err) { nxml_data_t *element; nxml_error_t ret; ret = nxml_find_element(nxml, data, name, &element); if (err) *err = ret; if (ret != NXML_OK) return NULL; return element; } char *nxmle_find_attribute(nxml_data_t *data, char *name, nxml_error_t *err) { nxml_attr_t *attribute; nxml_error_t ret; char *str; ret = nxml_find_attribute(data, name, &attribute); if (err) *err = ret; if (ret != NXML_OK) return NULL; if (!attribute) return NULL; str = strdup(attribute->value); if (!str) { if (err) *err = NXML_ERR_POSIX; return NULL; } return str; } char *nxmle_find_namespace(nxml_data_t *data, char *name, nxml_error_t *err) { nxml_namespace_t *namespace; nxml_error_t ret; char *str; ret = nxml_find_namespace(data, name, &namespace); if (err) *err = ret; if (ret != NXML_OK) return NULL; if (!namespace) return NULL; str = strdup(namespace->ns); if (!str) { if (err) *err = NXML_ERR_POSIX; return NULL; } return str; } char *nxmle_get_string(nxml_data_t *data, nxml_error_t *err) { nxml_error_t ret; char *str = NULL; ret = nxml_get_string(data, &str); if (err) *err = ret; if (ret != NXML_OK) return NULL; return str; } char *nxmle_write_buffer(nxml_t *nxml, nxml_error_t *err) { char *buffer; nxml_error_t ret; buffer = NULL; ret = nxml_write_buffer(nxml, &buffer); if (err) *err = ret; if (ret != NXML_OK) return NULL; return buffer; } int nxmle_line_error(nxml_t *nxml, nxml_error_t *err) { int line; nxml_error_t ret; ret = nxml_line_error(nxml, &line); if (err) *err = ret; return line; } /* EOF */ libnxml-master/src/nxml_edit.c000066400000000000000000000067151410202010000167170ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" nxml_error_t nxml_root_element(nxml_t *nxml, nxml_data_t **data) { nxml_data_t *tmp; if (!data || !nxml) return NXML_ERR_DATA; tmp = nxml->data; while (tmp) { if (tmp->type == NXML_TYPE_ELEMENT) break; tmp = tmp->next; } *data = tmp; return NXML_OK; } nxml_error_t nxml_doctype_element(nxml_t *nxml, nxml_doctype_t **data) { if (!data || !nxml) return NXML_ERR_DATA; *data = nxml->doctype; return NXML_OK; } nxml_error_t nxml_get_string(nxml_data_t *data, char **string) { if (!data || !string) return NXML_ERR_DATA; if (data->type == NXML_TYPE_TEXT) *string = strdup(data->value); else if (data->type == NXML_TYPE_ELEMENT) { nxml_data_t *tmp; tmp = data->children; *string = NULL; while (tmp) { if (tmp->type == NXML_TYPE_TEXT) { *string = strdup(tmp->value); break; } tmp = tmp->next; } } else *string = NULL; return NXML_OK; } nxml_error_t nxml_find_element(nxml_t *nxml, nxml_data_t *data, char *name, nxml_data_t **element) { nxml_data_t *tmp; if (!nxml || !name || !element) return NXML_ERR_DATA; if (data && data->type != NXML_TYPE_ELEMENT) { *element = NULL; return NXML_OK; } if (data) tmp = data->children; else tmp = nxml->data; while (tmp) { if (tmp->type == NXML_TYPE_ELEMENT && !strcmp(tmp->value, name)) { *element = tmp; return NXML_OK; } tmp = tmp->next; } *element = NULL; return NXML_OK; } nxml_error_t nxml_find_attribute(nxml_data_t *data, char *name, nxml_attr_t **attribute) { nxml_attr_t *tmp; if (!data || !name || !attribute) return NXML_ERR_DATA; if (data->type != NXML_TYPE_ELEMENT) { *attribute = NULL; return NXML_OK; } tmp = data->attributes; while (tmp) { if (!strcmp(tmp->name, name)) { *attribute = tmp; return NXML_OK; } tmp = tmp->next; } *attribute = NULL; return NXML_OK; } nxml_error_t nxml_find_namespace(nxml_data_t *data, char *name, nxml_namespace_t **namespace) { nxml_namespace_t *tmp; if (!data || !name || !namespace) return NXML_ERR_DATA; if (data->type != NXML_TYPE_ELEMENT) { *namespace = NULL; return NXML_OK; } tmp = data->ns_list; while (tmp) { if (!strcmp(tmp->ns, name)) { *namespace = tmp; return NXML_OK; } tmp = tmp->next; } *namespace = NULL; return NXML_OK; } /* EOF */ libnxml-master/src/nxml_error.c000066400000000000000000000033321410202010000171130ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" char *nxml_strerror(nxml_t *nxml, nxml_error_t err) { switch (err) { case NXML_OK: return "Success"; case NXML_ERR_PARSER: return "Parser error"; case NXML_ERR_DOWNLOAD: return nxml && nxml->priv.curl_error ? (char *)curl_easy_strerror(nxml->priv.curl_error) : "Download error"; case NXML_ERR_DATA: return "No correct paramenter in the function"; default: return strerror(errno); } } CURLcode nxml_curl_error(nxml_t *nxml, nxml_error_t err) { if (!nxml || err != NXML_ERR_DOWNLOAD) return CURLE_OK; return nxml->priv.curl_error; } nxml_error_t nxml_line_error(nxml_t *nxml, int *line) { if (!nxml || !line) return NXML_ERR_DATA; *line = nxml->priv.line; return NXML_OK; } /* EOF */ libnxml-master/src/nxml_free.c000066400000000000000000000077651410202010000167210ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" static void __nxml_private_free(__nxml_private_t *priv); static void __nxml_entity_free(__nxml_private_t *priv); nxml_error_t nxml_free_data(nxml_data_t *data) { nxml_namespace_t *namespace; nxml_attr_t *attr; nxml_data_t *tmp; void *old; if (!data) return NXML_ERR_DATA; if (data->value) free(data->value); namespace = data->ns_list; while (namespace) { old = namespace; namespace = namespace->next; nxml_free_namespace(old); } attr = data->attributes; while (attr) { old = attr; attr = attr->next; nxml_free_attribute(old); } tmp = data->children; while (tmp) { old = tmp; tmp = tmp->next; nxml_free_data(old); } free(data); return NXML_OK; } nxml_error_t nxml_free_attribute(nxml_attr_t *t) { if (!t) return NXML_ERR_DATA; if (t->name) free(t->name); if (t->value) free(t->value); free(t); return NXML_OK; } nxml_error_t nxml_free_namespace(nxml_namespace_t *t) { if (!t) return NXML_ERR_DATA; if (t->prefix) free(t->prefix); if (t->ns) free(t->ns); free(t); return NXML_OK; } nxml_error_t nxml_free_doctype(nxml_doctype_t *doctype) { nxml_doctype_t *tmp; if (!doctype) return NXML_ERR_DATA; while (doctype) { if (doctype->value) free(doctype->value); if (doctype->name) free(doctype->name); tmp = doctype; doctype = doctype->next; free(tmp); } return NXML_OK; } nxml_error_t nxml_free(nxml_t *data) { if (!data) return NXML_ERR_DATA; nxml_empty(data); __nxml_private_free(&data->priv); free(data); return NXML_OK; } nxml_error_t nxml_empty(nxml_t *data) { nxml_data_t *t, *old; __nxml_private_t priv; if (!data) return NXML_ERR_DATA; if (data->file) free(data->file); if (data->encoding) free(data->encoding); /* I must free the doctype, I must not empty only! */ if (data->doctype) nxml_free_doctype(data->doctype); t = data->data; while (t) { old = t; t = t->next; nxml_free_data(old); } __nxml_entity_free(&data->priv); memcpy(&priv, &data->priv, sizeof(__nxml_private_t)); memset(data, 0, sizeof(nxml_t)); memcpy(&data->priv, &priv, sizeof(__nxml_private_t)); return NXML_OK; } static void __nxml_entity_free(__nxml_private_t *priv) { __nxml_entity_t *entity; if (!priv) return; while (priv->entities) { entity = priv->entities; priv->entities = priv->entities->next; if (entity->entity) free(entity->entity); if (entity->name) free(entity->name); free(entity); } } static void __nxml_private_free(__nxml_private_t *priv) { if (!priv) return; if (priv->proxy) free(priv->proxy); if (priv->proxy_authentication) free(priv->proxy_authentication); if (priv->certfile) free(priv->certfile); if (priv->password) free(priv->password); if (priv->cacert) free(priv->cacert); if (priv->authentication) free(priv->authentication); if (priv->user_agent) free(priv->user_agent); __nxml_entity_free(priv); } /* EOF */ libnxml-master/src/nxml_init.c000066400000000000000000000162211410202010000167260ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" nxml_error_t nxml_new(nxml_t **nxml) { if (!nxml) return NXML_ERR_DATA; if (!(*nxml = (nxml_t *)calloc(1, sizeof(nxml_t)))) return NXML_ERR_POSIX; return NXML_OK; } static void nxml_add_rec(nxml_t *nxml, nxml_data_t *data) { while (data) { data->doc = nxml; if (data->children) nxml_add_rec(nxml, data->children); data = data->next; } } nxml_error_t nxml_add(nxml_t *nxml, nxml_data_t *parent, nxml_data_t **child) { nxml_data_t *tmp; if (!nxml || !child) return NXML_ERR_DATA; if (!*child && !(*child = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; (*child)->doc = nxml; (*child)->parent = parent; (*child)->next = NULL; if (parent) { if (!parent->children) parent->children = *child; else { tmp = parent->children; while (tmp->next) tmp = tmp->next; tmp->next = *child; } } else { if (!nxml->data) nxml->data = *child; else { tmp = nxml->data; while (tmp->next) tmp = tmp->next; tmp->next = *child; } } nxml_add_rec(nxml, (*child)->children); return NXML_OK; } nxml_error_t nxml_remove(nxml_t *nxml, nxml_data_t *parent, nxml_data_t *child) { nxml_data_t *tmp, *old; if (!nxml || !child) return NXML_ERR_DATA; if (parent) tmp = parent->children; else tmp = nxml->data; old = NULL; while (tmp) { if (tmp == child) { if (old) old->next = child->next; else if (parent) parent->children = child->next; else nxml->data = child->next; break; } old = tmp; tmp = tmp->next; } child->next = NULL; return NXML_OK; } nxml_error_t nxml_add_attribute(nxml_t *nxml, nxml_data_t *element, nxml_attr_t **attr) { nxml_attr_t *tmp; if (!nxml || !element || !attr) return NXML_ERR_DATA; if (!*attr && !(*attr = (nxml_attr_t *)calloc(1, sizeof(nxml_attr_t)))) return NXML_ERR_POSIX; (*attr)->next = NULL; if (!element->attributes) element->attributes = *attr; else { tmp = element->attributes; while (tmp->next) tmp = tmp->next; tmp->next = *attr; } return NXML_OK; } nxml_error_t nxml_remove_attribute(nxml_t *nxml, nxml_data_t *element, nxml_attr_t *attr) { nxml_attr_t *tmp, *old; if (!nxml || !element || !attr) return NXML_ERR_DATA; tmp = element->attributes; old = NULL; while (tmp) { if (tmp == attr) { if (old) old->next = attr->next; else element->attributes = attr->next; break; } old = tmp; tmp = tmp->next; } attr->next = NULL; return NXML_OK; } nxml_error_t nxml_add_namespace(nxml_t *nxml, nxml_data_t *element, nxml_namespace_t **namespace) { nxml_namespace_t *tmp; if (!nxml || !element || !namespace) return NXML_ERR_DATA; if (!*namespace && !(*namespace = (nxml_namespace_t *)calloc(1, sizeof(nxml_namespace_t)))) return NXML_ERR_POSIX; (*namespace)->next = NULL; if (!element->ns_list) element->ns_list = *namespace; else { tmp = element->ns_list; while (tmp->next) tmp = tmp->next; tmp->next = *namespace; } return NXML_OK; } nxml_error_t nxml_remove_namespace(nxml_t *nxml, nxml_data_t *element, nxml_namespace_t *namespace) { nxml_namespace_t *tmp, *old; if (!nxml || !element || !namespace) return NXML_ERR_DATA; tmp = element->ns_list; old = NULL; while (tmp) { if (tmp == namespace) { if (old) old->next = namespace->next; else element->ns_list = namespace->next; break; } old = tmp; tmp = tmp->next; } namespace->next = NULL; return NXML_OK; } nxml_error_t nxml_set_func(nxml_t *nxml, void (*func)(char *, ...)) { if (!nxml) return NXML_ERR_DATA; nxml->priv.func = func; return NXML_OK; } nxml_error_t nxml_set_timeout(nxml_t *nxml, int timeout) { if (!nxml) return NXML_ERR_DATA; nxml->priv.timeout = timeout; return NXML_OK; } nxml_error_t nxml_set_proxy(nxml_t *nxml, char *proxy, char *userpwd) { if (!nxml) return NXML_ERR_DATA; if (nxml->priv.proxy) free(nxml->priv.proxy); if (proxy) nxml->priv.proxy = strdup(proxy); else nxml->priv.proxy = NULL; if (nxml->priv.proxy_authentication) free(nxml->priv.proxy_authentication); if (userpwd) nxml->priv.proxy_authentication = strdup(userpwd); else nxml->priv.proxy_authentication = NULL; return NXML_OK; } nxml_error_t nxml_set_authentication(nxml_t *nxml, char *userpwd) { if (!nxml) return NXML_ERR_DATA; if (nxml->priv.authentication) free(nxml->priv.authentication); if (userpwd) nxml->priv.authentication = strdup(userpwd); else nxml->priv.authentication = NULL; return NXML_OK; } nxml_error_t nxml_set_textindent(nxml_t *nxml, char textindent) { if (!nxml) return NXML_ERR_DATA; if (textindent) nxml->priv.textindent = 1; else nxml->priv.textindent = 0; return NXML_OK; } nxml_error_t nxml_set_user_agent(nxml_t *nxml, char *user_agent) { if (!nxml) return NXML_ERR_DATA; if (nxml->priv.user_agent) free(nxml->priv.user_agent); if (user_agent) nxml->priv.user_agent = strdup(user_agent); else nxml->priv.user_agent = NULL; return NXML_OK; } nxml_error_t nxml_set_certificate(nxml_t *nxml, char *certificate, char *password, char *cacert, int verifypeer) { if (!nxml) return NXML_ERR_DATA; if (nxml->priv.certfile) free(nxml->priv.certfile); if (certificate) nxml->priv.certfile = strdup(certificate); else nxml->priv.certfile = NULL; if (nxml->priv.password) free(nxml->priv.password); if (password) nxml->priv.password = strdup(password); else nxml->priv.password = NULL; if (cacert) nxml->priv.cacert = strdup(cacert); else nxml->priv.cacert = NULL; nxml->priv.verifypeer = !verifypeer; return NXML_OK; } void nxml_print_generic(char *str, ...) { va_list va; va_start(va, str); vfprintf(stderr, str, va); va_end(va); } /* EOF */ libnxml-master/src/nxml_internal.h000066400000000000000000000054571410202010000176150ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef __N_XML_INTERNAL_H__ #define __N_XML_INTERNAL_H__ /* Rule [4] */ #define __NXML_NAMESTARTCHARS \ ((ch = __NXML_U8()) == ':' || (ch >= 'a' && ch <= 'z') || ch == '_' || \ (ch >= 'A' && ch <= 'Z') || (ch >= 0xc0 && ch <= 0xd6) || \ (ch >= 0xd8 && ch <= 0xf6) || (ch >= 0xf8 && ch <= 0x2ff) || \ (ch >= 0x370 && ch <= 0x37d) || (ch >= 0x37f && ch <= 0x1fff) || \ (ch >= 0x200c && ch <= 0x200d) || (ch >= 0x2070 && ch <= 0x218f) || \ (ch >= 0x2c00 && ch <= 0x2fef) || (ch >= 0x3001 && ch <= 0xd7ff) || \ (ch >= 0xf900 && ch <= 0xfdcf) || (ch >= 0xfdf0 && ch <= 0xfffd) || \ (ch >= 0x10000 && ch <= 0xeffff)) /* Rule [4a] */ #define __NXML_NAMECHARS \ (__NXML_NAMESTARTCHARS || ch == '-' || ch == '.' || \ (ch >= '0' && ch <= '9') || ch == 0xb7 || (ch >= 0x0300 && ch <= 0x036f) || \ (ch >= 0x203f && ch <= 0x2040)) #define __NXML_U8() __nxml_utf8((unsigned char **)buffer, size, &byte) typedef struct __nxml_string_t__ __nxml_string_t; int64_t __nxml_utf8(unsigned char **buffer, size_t *size, int *byte); int64_t __nxml_int_charset(int i, unsigned char *buffer, char *charset); int __nxml_utf_detection(char *r_buffer, size_t r_size, char **buffer, size_t *size, nxml_charset_t *); int __nxml_escape_spaces(nxml_t *doc, char **buffer, size_t *size); char *__nxml_get_value(nxml_t *doc, char **buffer, size_t *size); char *__nxml_trim(char *tmp); /* nxml_string.c */ /** * \brief * For internal use only */ struct __nxml_string_t__ { char *string; size_t size; }; __nxml_string_t *__nxml_string_new(void); int __nxml_string_add(__nxml_string_t *st, char *what, size_t size); char *__nxml_string_free(__nxml_string_t *st); void __nxml_namespace_parse(nxml_t *nxml); int __nxml_atoi(char *str); #endif /* EOF */ libnxml-master/src/nxml_namespace.c000066400000000000000000000140141410202010000177150ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" struct __nxml_data_ns_t { nxml_namespace_t *ns; struct __nxml_data_ns_t *next; }; static void __nxml_namespace_free_item(nxml_data_t *e) { nxml_namespace_t *ns; nxml_data_t *child; while (e->ns_list) { ns = e->ns_list->next; if (e->ns_list->prefix) free(e->ns_list->prefix); if (e->ns_list->ns) free(e->ns_list->ns); free(e->ns_list); e->ns_list = ns; } e->ns = NULL; child = e->children; while (child) { __nxml_namespace_free_item(child); child = child->next; } } static void __nxml_namespace_free(nxml_t *nxml) { nxml_data_t *e; e = nxml->data; while (e) { __nxml_namespace_free_item(e); e = e->next; } } int __nxml_namespace_parse_add(nxml_data_t *data, char *prefix, char *namespace) { nxml_namespace_t *ns; if (!(ns = (nxml_namespace_t *)calloc(1, sizeof(nxml_namespace_t)))) return 1; if (prefix && !(ns->prefix = strdup(prefix))) { free(ns); return 1; } if (!(ns->ns = strdup(namespace))) { if (ns->prefix) free(ns->prefix); free(ns); return 1; } ns->next = data->ns_list; data->ns_list = ns; return 0; } static int __nxml_namespace_find_item(nxml_t *nxml, nxml_data_t *e) { nxml_data_t *child; nxml_attr_t *att; att = e->attributes; while (att) { if (!strcmp(att->name, "xmlns")) { if (__nxml_namespace_parse_add(e, NULL, att->value)) { __nxml_namespace_free(nxml); return 1; } } else if (!strncmp(att->name, "xmlns:", 6)) { if (__nxml_namespace_parse_add(e, att->name + 6, att->value)) { __nxml_namespace_free(nxml); return 1; } } att = att->next; } child = e->children; while (child) { if (child->type == NXML_TYPE_ELEMENT) __nxml_namespace_find_item(nxml, child); child = child->next; } return 0; } static int __nxml_namespace_find(nxml_t *nxml) { nxml_data_t *e; e = nxml->data; while (e) { if (e->type == NXML_TYPE_ELEMENT) __nxml_namespace_find_item(nxml, e); e = e->next; } return 0; } static void __nxml_namespace_associate_attribute(struct __nxml_data_ns_t *list, nxml_attr_t *e) { int i; int len = strlen(e->name); int k; for (i = k = 0; i < len; i++) if (e->name[i] == ':') { k = i; break; } if (!k) { while (list) { if (!list->ns->prefix) { e->ns = list->ns; return; } list = list->next; } return; } else { while (list) { if (list->ns->prefix && !strncmp(list->ns->prefix, e->name, k)) { char *a = strdup(e->name + strlen(list->ns->prefix) + 1); if (!a) return; free(e->name); e->name = a; e->ns = list->ns; return; } list = list->next; } } } static void __nxml_namespace_associate_item(struct __nxml_data_ns_t *list, nxml_data_t *e) { int i; int len; int k; nxml_attr_t *attr; attr = e->attributes; while (attr) { __nxml_namespace_associate_attribute(list, attr); attr = attr->next; } len = strlen(e->value); for (i = k = 0; i < len; i++) if (e->value[i] == ':') { k = i; break; } if (!k) { while (list) { if (!list->ns->prefix) { e->ns = list->ns; return; } list = list->next; } return; } else { while (list) { if (list->ns->prefix && !strncmp(list->ns->prefix, e->value, k)) { char *a = strdup(e->value + strlen(list->ns->prefix) + 1); if (!a) return; free(e->value); e->value = a; e->ns = list->ns; return; } list = list->next; } } } static void __nxml_namespace_associate(struct __nxml_data_ns_t **list, nxml_data_t *root) { nxml_data_t *e; nxml_namespace_t *ns; struct __nxml_data_ns_t *tmp, *old; ns = root->ns_list; while (ns) { if (!(tmp = calloc(1, sizeof(struct __nxml_data_ns_t)))) return; tmp->ns = ns; tmp->next = (*list); (*list) = tmp; ns = ns->next; } __nxml_namespace_associate_item(*list, root); e = root->children; while (e) { if (e->type == NXML_TYPE_ELEMENT) __nxml_namespace_associate(list, e); e = e->next; } ns = root->ns_list; while (ns) { tmp = *list; old = NULL; while (tmp) { if (tmp->ns == ns) { if (old) old->next = tmp->next; else *list = tmp->next; free(tmp); break; } old = tmp; tmp = tmp->next; } ns = ns->next; } } static void __nxml_namespace_connect(nxml_t *nxml) { nxml_data_t *e; struct __nxml_data_ns_t *list = NULL; e = nxml->data; while (e) { if (e->type == NXML_TYPE_ELEMENT) __nxml_namespace_associate(&list, e); e = e->next; } } void __nxml_namespace_parse(nxml_t *nxml) { __nxml_namespace_free(nxml); if (__nxml_namespace_find(nxml)) return; __nxml_namespace_connect(nxml); } /* EOF */ libnxml-master/src/nxml_parser.c000066400000000000000000000716561410202010000172740ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" static int __nxml_parse_unique_attribute(nxml_attr_t *attr, char *name) { /* * Rule [40] - Well-formedness contraint: Unique Att Spec */ while (attr) { if (!strcmp(attr->name, name)) return 1; attr = attr->next; } return 0; } static char *__nxml_parse_string(nxml_t *doc, char *buffer, int size) { char *real; int i; int q; __nxml_string_t *ret; ret = __nxml_string_new(); for (q = i = 0; i < size; i++) { if (*(buffer + i) == 0xd) continue; if (*(buffer + i) == 0xa || *(buffer + i) == 0x9 || *(buffer + i) == 0x20) { if (!q) { if (!doc->priv.textindent) q = 1; __nxml_string_add(ret, buffer + i, 1); } } else if (*(buffer + i) == '&') { if (!strncmp(buffer + i, "<", 4)) { __nxml_string_add(ret, "<", 1); i += 3; q = 0; } else if (!strncmp(buffer + i, ">", 4)) { __nxml_string_add(ret, ">", 1); i += 3; q = 0; } else if (!strncmp(buffer + i, "&", 5)) { __nxml_string_add(ret, "&", 1); i += 4; q = 0; } else if (!strncmp(buffer + i, "'", 6)) { __nxml_string_add(ret, "'", 1); i += 5; q = 0; } else if (!strncmp(buffer + i, """, 6)) { __nxml_string_add(ret, "\"", 1); i += 5; q = 0; } else if (*(buffer + i + 1) == '#') { char buf[1024]; int k = i; int last; while (*(buffer + k) != ';' && k < size) k++; last = k - (i + 2) > sizeof(buf) ? sizeof(buf) : k - (i + 2); strncpy(buf, buffer + i + 2, last); buf[last] = 0; if (buf[0] != 'x') last = atoi(buf); else last = __nxml_atoi(&buf[1]); if ((last = __nxml_int_charset(last, (unsigned char *)buf, doc->encoding)) > 0) __nxml_string_add(ret, buf, last); else __nxml_string_add(ret, buffer + i, 1); i += k - i; q = 0; } else { __nxml_entity_t *entity; char buf[1024]; int k = i; int last; while (*(buffer + k) != ';' && k < size) k++; last = k - (i + 1) > sizeof(buf) ? sizeof(buf) : k - (i + 1); strncpy(buf, buffer + i + 1, last); buf[last] = 0; for (entity = doc->priv.entities; entity; entity = entity->next) { if (!strcmp(entity->name, buf)) { __nxml_string_add(ret, entity->entity, strlen(entity->entity)); break; } } if (!entity) __nxml_string_add(ret, buffer + i, 1); else i += strlen(entity->name) + 1; q = 0; } } else { q = 0; __nxml_string_add(ret, buffer + i, 1); } } if (!(real = __nxml_string_free(ret))) real = strdup(""); return real; } /* Returns 0 if there is no attribute * Returns 1 if the attribute was assigned in *result * Returns -1 in case of allocation error. */ static int __nxml_parse_get_attr(nxml_t *doc, char **buffer, size_t *size, char **result) { char attr[1024]; int i; /* NOTE, 2020-07-20, Giovanni Simoni, dacav at fastmail dot com. * * 'bytes' and 'ch' seem to be uninitialized, but they are implicitly * assigned by the __NXML_NAMESTARTCHARS macro, which also assumes that * a variable named 'buffer' exists. */ int byte; int64_t ch; *result = NULL; if (!*size) return 0; if (!__NXML_NAMESTARTCHARS) return 0; memcpy(&attr[0], *buffer, byte); i = byte; (*buffer) += byte; (*size) -= byte; while (__NXML_NAMECHARS && *size && i < sizeof(attr) - 1) { memcpy(&attr[i], *buffer, byte); i += byte; (*buffer) += byte; (*size) -= byte; } if (**buffer != 0x20 && **buffer != 0x9 && **buffer != 0xa && **buffer != 0xd && **buffer != '=') { (*buffer) -= i; (*size) += i; return 0; } i += __nxml_escape_spaces(doc, buffer, size); if (**buffer != '=') { (*buffer) -= i; (*size) += i; return 0; } (*buffer)++; (*size)--; __nxml_escape_spaces(doc, buffer, size); if (i >= sizeof(attr)) return 0; attr[i] = 0; *result = strdup(attr); return (*result) ? 1 : -1; } static nxml_error_t __nxml_parse_get_attribute(nxml_t *doc, char **buffer, size_t *size, nxml_attr_t **attr) { /* * Rule [41] - Attribute ::= Name Eq AttValue * Rule [25] - Eq ::= S? '=' S? * Rule [5] - Name ::= NameStartChar (NameChar)* * Rule [4] - NameStartChar ::= ":" | [A-Z] | ["_"] | [a-z] | Unicode... * Rule [4a] - NameChar ::= NameStarChar | "-" | "." | [0-9] | Unicode... * Rule [10] - AttValue ::= '"' ([^<&"] | Reference)* '"' | * "'" ([^<&'] | Reference)* "'" */ char *tag = NULL, *value, *v; *attr = NULL; if (!*size) return NXML_OK; __nxml_escape_spaces(doc, buffer, size); switch (__nxml_parse_get_attr(doc, buffer, size, &tag)) { case 0: return NXML_OK; case -1: return NXML_ERR_POSIX; default: break; } if (!(value = __nxml_get_value(doc, buffer, size))) { free(tag); if (doc->priv.func) doc->priv.func("%s: expected value of attribute (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } if (!(v = __nxml_parse_string(doc, value, strlen(value)))) { free(tag); return NXML_ERR_POSIX; } free(value); value = v; __nxml_escape_spaces(doc, buffer, size); if (!(*attr = (nxml_attr_t *)calloc(1, sizeof(nxml_attr_t)))) { free(tag); free(value); return NXML_ERR_POSIX; } (*attr)->name = tag; (*attr)->value = value; return NXML_OK; } static nxml_error_t __nxml_parse_cdata(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data) { /* * Rule [18] - CDSect ::= CDStart CData CDEnd * Rule [19] - CDStart ::= '' Char *)) * Rule [21] - CDEnd ::= ']]>' */ int i = 0; nxml_data_t *t; char *value; while (i < *size) { if (!strncmp(*buffer + i, "]]>", 3)) break; if (*(*buffer + i) == 0xa && doc->priv.func) doc->priv.line++; i++; } if (strncmp(*buffer + i, "]]>", 3)) { if (doc->priv.func) doc->priv.func("%s: expected ']]>' as close of a CDATA (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } if (!(t = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; t->doc = doc; if (!(value = (char *)malloc(sizeof(char) * (i + 1)))) { free(t); return NXML_ERR_POSIX; } strncpy(value, *buffer, i); value[i] = 0; t->value = value; t->type = NXML_TYPE_TEXT; (*buffer) += i + 3; (*size) -= i + 3; *data = t; return NXML_OK; } static void __nxml_parse_entity(nxml_t *doc, char **buffer, size_t *size) { /* * [70] EntityDecl ::= GEDecl | PEDecl * [71] GEDecl ::= '' * [72] PEDecl ::= '' * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) * [74] PEDef ::= EntityValue | ExternalID */ int i; char name[1024]; char *entity; int byte; int64_t ch; __nxml_escape_spaces(doc, buffer, size); if (strncmp(*buffer, "' || q) && i < *size) { if (*(*buffer + i) == '<') q++; else if (*(*buffer + i) == '>') q--; i++; } if (*(*buffer) == '>') i++; (*buffer) += i; (*size) -= i; return; } *buffer += 8; *size -= 8; __nxml_escape_spaces(doc, buffer, size); /* Name */ if (!__NXML_NAMESTARTCHARS) { int q = i = 0; while ((*(*buffer + i) != '>' || q) && i < *size) { if (*(*buffer + i) == '<') q++; else if (*(*buffer + i) == '>') q--; i++; } if (*(*buffer) == '>') i++; (*buffer) += i; (*size) -= i; return; } memcpy(&name[0], *buffer, byte); i = byte; (*buffer) += byte; (*size) -= byte; while (__NXML_NAMECHARS && *size && i < sizeof(name) - 1) { memcpy(&name[i], *buffer, byte); i += byte; (*buffer) += byte; (*size) -= byte; } name[i] = 0; if (!i || !strcmp(name, "%")) { int q = i = 0; while ((*(*buffer + i) != '>' || q) && i < *size) { if (*(*buffer + i) == '<') q++; else if (*(*buffer + i) == '>') q--; i++; } if (*(*buffer) == '>') i++; (*buffer) += i; (*size) -= i; return; } __nxml_escape_spaces(doc, buffer, size); entity = __nxml_get_value(doc, buffer, size); __nxml_escape_spaces(doc, buffer, size); if (**buffer == '>') { (*buffer)++; (*size)--; } if (entity) { __nxml_entity_t *e; if (!(e = calloc(1, sizeof(__nxml_entity_t)))) { free(entity); return; } if (!(e->name = strdup(name))) { free(e); free(entity); return; } e->entity = entity; if (!doc->priv.entities) doc->priv.entities = e; else { __nxml_entity_t *tmp = doc->priv.entities; while (tmp->next) tmp = tmp->next; tmp->next = e; } } } static nxml_error_t __nxml_parse_doctype(nxml_t *doc, char **buffer, size_t *size, int *doctype) { /* * Rule [28] - doctypedecl ::= '' */ nxml_doctype_t *t; nxml_doctype_t *tmp; char str[1024]; char *value; int i; int byte; int64_t ch; int q = 0; __nxml_escape_spaces(doc, buffer, size); if (!__NXML_NAMESTARTCHARS) { if (doc->priv.func) doc->priv.func("%s: abnormal char '%c' (line %d)\n", doc->file ? doc->file : "", **buffer, doc->priv.line); return NXML_ERR_PARSER; } memcpy(&str[0], *buffer, byte); i = byte; (*buffer) += byte; (*size) -= byte; while (__NXML_NAMECHARS && *size && i < sizeof(str) - 1) { memcpy(&str[i], *buffer, byte); i += byte; (*buffer) += byte; (*size) -= byte; } str[i] = 0; if (!(t = (nxml_doctype_t *)calloc(1, sizeof(nxml_doctype_t)))) return NXML_ERR_POSIX; t->doc = doc; if (!(t->name = strdup(str))) { free(t); return NXML_ERR_POSIX; } __nxml_escape_spaces(doc, buffer, size); i = 0; while ((*(*buffer + i) != '>' || q) && i < *size) { if (*(*buffer + i) == '<') q++; else if (*(*buffer + i) == '>') q--; if (*(*buffer + i) == 0xa && doc->priv.func) doc->priv.line++; i++; } if (*(*buffer + i) != '>') { if (doc->priv.func) doc->priv.func("%s: expected '>' as close of a doctype (line %d)\n", doc->file ? doc->file : "", doc->priv.line); free(t->value); free(t); return NXML_ERR_PARSER; } if (!(value = __nxml_parse_string(doc, *buffer, i))) { free(t->value); free(t); return NXML_ERR_POSIX; } t->value = value; (*buffer) += i + 1; (*size) -= i + 1; tmp = doc->doctype; if (!tmp) doc->doctype = t; else { while (tmp->next) tmp = tmp->next; tmp->next = t; } *doctype = 1; while (value && *value && *value != '[') value++; if (value && *value == '[') { unsigned int size; value++; size = strlen(value); while (size > 0 && value && *value) __nxml_parse_entity(doc, &value, (size_t *)&size); } return NXML_OK; } static nxml_error_t __nxml_parse_comment(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data) { /* * Rule [15] - Comment ::= '' */ int i = 0; nxml_data_t *t; char *value; while (strncmp(*buffer + i, "-->", 3) && i < *size) { if (*(*buffer + i) == 0xa && doc->priv.func) doc->priv.line++; i++; } if (strncmp(*buffer + i, "-->", 3)) { if (doc->priv.func) doc->priv.func("%s: expected '--' as close comment (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } if (!(t = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; t->doc = doc; if (!(value = __nxml_parse_string(doc, *buffer, i))) { free(t); return NXML_ERR_POSIX; } t->value = value; (*buffer) += i + 3; (*size) -= i + 3; t->type = NXML_TYPE_COMMENT; *data = t; return NXML_OK; } static nxml_error_t __nxml_parse_pi(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data) { /* * Rule [16] - PI ::= '' Char *)))? * '?>' * Rule [17] - PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) */ int i = 0; nxml_data_t *t; char *value; if (!*size) return NXML_OK; *data = NULL; (*buffer) += 1; (*size) -= 1; while (strncmp(*buffer + i, "?>", 2) && i < *size) { if (*(*buffer + i) == 0xa && doc->priv.func) doc->priv.line++; i++; } if (strncmp(*buffer + i, "?>", 2)) { if (doc->priv.func) doc->priv.func("%s: expected '?' as close pi tag (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } if (!strncasecmp(*buffer, "?xml", 4)) { if (doc->priv.func) doc->priv.func("%s: pi xml is reserved! (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } if (!(t = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; t->doc = doc; if (!(value = __nxml_parse_string(doc, *buffer, i))) { free(t); return NXML_ERR_POSIX; } t->value = value; (*buffer) += i + 2; (*size) -= i + 2; t->type = NXML_TYPE_PI; *data = t; return NXML_OK; } static nxml_error_t __nxml_parse_other(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data, int *doctype) { /* Tags 'priv.func) doc->priv.func("%s: abnormal tag (line %d)\n", doc->file ? doc->file : "", doc->priv.line); return NXML_ERR_PARSER; } } static nxml_error_t __nxml_parse_text(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data) { int i = 0; nxml_data_t *t; char *value; char *value_new; *data = NULL; if (!*size) return NXML_OK; while (i < *size && *(*buffer + i) != '<') { if (*(*buffer + i) == 0xa && doc->priv.func) doc->priv.line++; i++; } if (!(t = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; t->doc = doc; if (!(value = __nxml_parse_string(doc, *buffer, i))) { free(t); return NXML_ERR_POSIX; } (*buffer) += i; (*size) -= i; value_new = __nxml_trim(value); free(value); if (!value_new) { free(t); return NXML_ERR_POSIX; } t->value = value_new; t->type = NXML_TYPE_TEXT; *data = t; return NXML_OK; } static nxml_error_t __nxml_parse_close(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data) { /* * Rule [42] - ETag ::= '' */ nxml_data_t *tag; char str[1024]; int i; int byte; int64_t ch; *data = NULL; if (!*size) return NXML_OK; if (!__NXML_NAMESTARTCHARS) { if (doc->priv.func) doc->priv.func("%s: abnormal char '%c' (line %d)\n", doc->file ? doc->file : "", **buffer, doc->priv.line); return NXML_ERR_PARSER; } memcpy(&str[0], *buffer, byte); i = byte; (*buffer) += byte; (*size) -= byte; while (__NXML_NAMECHARS && *size && i < sizeof(str) - 1) { memcpy(&str[i], *buffer, byte); i += byte; (*buffer) += byte; (*size) -= byte; } str[i] = 0; __nxml_escape_spaces(doc, buffer, size); if (**buffer != '>') { if (doc->priv.func) doc->priv.func("%s: expected char '>' after tag %s (line %d)\n", doc->file ? doc->file : "", str, doc->priv.line); return NXML_ERR_PARSER; } (*buffer) += 1; (*size) -= 1; if (!(tag = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; tag->doc = doc; if (!(tag->value = strdup(str))) { free(tag); return NXML_ERR_POSIX; } tag->type = NXML_TYPE_ELEMENT_CLOSE; *data = tag; return NXML_OK; } static nxml_error_t __nxml_parse_get_tag(nxml_t *doc, char **buffer, size_t *size, nxml_data_t **data, int *doctype) { /* Parse the element... */ nxml_attr_t *attr, *last; nxml_data_t *tag, *child, *child_last; nxml_error_t err; char str[1024]; int i; int closed = 0; int byte; int64_t ch; *data = NULL; *doctype = 0; if (!*size) return NXML_OK; __nxml_escape_spaces(doc, buffer, size); /* Text */ if (**buffer != '<') return __nxml_parse_text(doc, buffer, size, data); if (*size <= 1) return NXML_OK; (*buffer) += 1; (*size) -= 1; /* Comment, CData, DocType or other elements */ if (**buffer == '!') return __nxml_parse_other(doc, buffer, size, data, doctype); /* PI */ else if (**buffer == '?') return __nxml_parse_pi(doc, buffer, size, data); /* Close tag */ else if (**buffer == '/') { (*buffer) += 1; (*size) -= 1; return __nxml_parse_close(doc, buffer, size, data); } __nxml_escape_spaces(doc, buffer, size); if (!__NXML_NAMESTARTCHARS) { if (doc->priv.func) doc->priv.func("%s: abnormal char '%c' (line %d)\n", doc->file ? doc->file : "", **buffer, doc->priv.line); return NXML_ERR_PARSER; } memcpy(&str[0], *buffer, byte); i = byte; (*buffer) += byte; (*size) -= byte; while (__NXML_NAMECHARS && *size && i < sizeof(str) - 1) { memcpy(&str[i], *buffer, byte); i += byte; (*buffer) += byte; (*size) -= byte; } str[i] = 0; if (**buffer != 0x20 && **buffer != 0x9 && **buffer != 0xa && **buffer != 0xd && **buffer != '>' && **buffer != '/') { (*buffer) -= i; (*size) += i; if (doc->priv.func) doc->priv.func("%s: abnormal char '%c' after tag %s (line %d)\n", doc->file ? doc->file : "", *(*buffer + i), str, doc->priv.line); return NXML_ERR_PARSER; } if (!(tag = (nxml_data_t *)calloc(1, sizeof(nxml_data_t)))) return NXML_ERR_POSIX; tag->doc = doc; if (!(tag->value = strdup(str))) { free(tag); return NXML_ERR_POSIX; } last = NULL; /* Get attribute: */ while (!(err = __nxml_parse_get_attribute(doc, buffer, size, &attr)) && attr) { if (!*size) return NXML_ERR_PARSER; if (__nxml_parse_unique_attribute(tag->attributes, attr->name)) { if (doc->priv.func) doc->priv.func("%s: Duplicate attribute '%s' in tag '%s' (line %d)\n", doc->file ? doc->file : "", attr->name, tag->value, doc->priv.line); nxml_free_attribute(attr); nxml_free_data(tag); return NXML_ERR_PARSER; } if (last) last->next = attr; else tag->attributes = attr; last = attr; } if (err) { nxml_free_data(tag); return err; } /* Closed element */ if (**buffer == '/') { closed++; (*buffer) += 1; (*size) -= 1; __nxml_escape_spaces(doc, buffer, size); } if (**buffer != '>') { if (doc->priv.func) doc->priv.func("%s: expected char '>' after tag %s (line %d)\n", doc->file ? doc->file : "", tag->value, doc->priv.line); nxml_free_data(tag); return NXML_ERR_PARSER; } (*buffer) += 1; (*size) -= 1; if (!closed) { child_last = NULL; /* Search children: */ while (!(err = __nxml_parse_get_tag(doc, buffer, size, &child, doctype)) && (*doctype || child)) { if (*doctype) continue; /* If the current child, break: */ if (child->type == NXML_TYPE_ELEMENT_CLOSE) { if (!strcmp(child->value, tag->value)) { closed = 1; nxml_free_data(child); break; } else { if (doc->priv.func) doc->priv.func("%s: expected tag '/%s' and not '%s' (line %d)\n", doc->file ? doc->file : "", tag->value, child->value, doc->priv.line); nxml_free_data(child); nxml_free_data(tag); return NXML_ERR_PARSER; } } /* Set the parent */ child->parent = tag; if (child_last) child_last->next = child; else tag->children = child; child_last = child; } if (err) { nxml_free_data(tag); return err; } } tag->type = NXML_TYPE_ELEMENT; if (!closed) { if (doc->priv.func) doc->priv.func("%s: expected tag '/%s' (line %d)\n", doc->file ? doc->file : "", tag->value, doc->priv.line); nxml_free_data(tag); return NXML_ERR_PARSER; } *data = tag; return NXML_OK; } static nxml_error_t __nxml_parse_buffer(nxml_t *nxml, char *r_buffer, size_t r_size) { /* * Rule [1] - Document ::= prolog element Misc* - Char* RestrictedChar Char* */ nxml_attr_t *attr; nxml_error_t err; nxml_charset_t charset; nxml_data_t *tag, *last, *root; int doctype; void *to_free = NULL; char *buffer = NULL; size_t size = 0; if (!r_buffer || !nxml) return NXML_ERR_DATA; if (!r_size) r_size = strlen(r_buffer); /* NOTE, 2020-07-20, Giovanni Simoni, dacav at fastmail dot com. * * For what I can understand, the '__nxml_utf_detection' function checks the * encoding declared in the XML header. * * If the encoding of 'r_buffer' is convenient to parse (e.g. UTF-8), the * returned value is 0, 'buffer' and 'size' are left untouched. * * Otherwise, the function allocates a memory buffer and re-encodes the * content into it. The returned value is 1, while 'buffer' and 'size' are * assigned to the allocated buffer and its size, respectively. We are * responsible of freeing the buffer. */ switch (__nxml_utf_detection(r_buffer, r_size, &buffer, &size, &charset)) { case 0: buffer = r_buffer; size = r_size; break; case 1: to_free = buffer; case -1: return NXML_ERR_POSIX; } nxml->priv.line = 1; nxml->version = NXML_VERSION_1_0; nxml->standalone = 1; /* * Rule [22] - prolog ::= XMLDecl Misc* (doctypedecl Misc*)? * Rule [23] - XMLDecl ::= '' */ if (!strncmp(buffer, "priv.func) nxml->priv.func("%s: expected 'version' attribute (line %d)\n", nxml->file ? nxml->file : "", nxml->priv.line); free(to_free); return NXML_ERR_PARSER; } if (!strcmp(attr->value, "1.0")) nxml->version = NXML_VERSION_1_0; else if (!strcmp(attr->value, "1.1")) nxml->version = NXML_VERSION_1_1; else { if (nxml->priv.func) nxml->priv.func(PACKAGE " " VERSION " supports only xml 1.1 or 1.0 (line %d)\n", nxml->priv.line); free(to_free); nxml_free_attribute(attr); return NXML_ERR_PARSER; } nxml_free_attribute(attr); while (!(err = __nxml_parse_get_attribute(nxml, &buffer, &size, &attr)) && attr) { if (!strcmp(attr->name, "standalone")) { if (!strcmp(attr->value, "yes")) nxml->standalone = 1; else nxml->standalone = 0; } else if (!strcmp(attr->name, "encoding")) { nxml->encoding = strdup(attr->value); if (!nxml->encoding) { nxml_empty(nxml); nxml_free_attribute(attr); free(to_free); return NXML_ERR_POSIX; } } else { if (nxml->priv.func) nxml->priv.func("%s: unexpected attribute '%s' (line %d)\n", nxml->file ? nxml->file : "", attr->name, nxml->priv.line); nxml_empty(nxml); nxml_free_attribute(attr); free(to_free); return NXML_ERR_PARSER; } nxml_free_attribute(attr); } if (err || strncmp(buffer, "?>", 2)) { if (nxml->priv.func) nxml->priv.func("%s expected '?>' (line %d)\n", nxml->file ? nxml->file : "", nxml->priv.line); nxml_empty(nxml); free(to_free); return NXML_ERR_PARSER; } buffer += 2; size -= 2; } root = last = NULL; while (!(err = __nxml_parse_get_tag(nxml, &buffer, &size, &tag, &doctype)) && (doctype || tag)) { if (doctype) continue; if (tag->type == NXML_TYPE_ELEMENT && !root) root = tag; if (last) last->next = tag; else nxml->data = tag; last = tag; } if (err) { nxml_empty(nxml); free(to_free); return NXML_ERR_PARSER; } if (!root) { if (nxml->priv.func) nxml->priv.func("%s: No root element founded!\n", nxml->file ? nxml->file : ""); nxml_empty(nxml); free(to_free); return NXML_ERR_PARSER; } nxml->charset_detected = charset; __nxml_namespace_parse(nxml); free(to_free); return NXML_OK; } /* EXTERNAL FUNCTIONS *******************************************************/ nxml_error_t nxml_parse_url(nxml_t *nxml, char *url) { nxml_error_t err; char *buffer; size_t size; if (!url || !nxml) return NXML_ERR_DATA; if ((err = nxml_download_file(nxml, url, &buffer, &size)) != NXML_OK) return err; if (nxml->file) free(nxml->file); if (!(nxml->file = strdup(url))) { nxml_empty(nxml); return NXML_ERR_POSIX; } nxml->size = size; nxml_empty(nxml); err = __nxml_parse_buffer(nxml, buffer, size); free(buffer); return err; } nxml_error_t nxml_parse_file(nxml_t *nxml, char *file) { nxml_error_t err; char *buffer; struct stat st; int fd, len, ret; if (!file || !nxml) return NXML_ERR_DATA; if (stat(file, &st)) return NXML_ERR_POSIX; if ((fd = open(file, O_RDONLY)) < 0) return NXML_ERR_POSIX; if (!(buffer = (char *)malloc(sizeof(char) * (st.st_size + 1)))) return NXML_ERR_POSIX; len = 0; while (len < st.st_size) { if ((ret = read(fd, buffer + len, st.st_size - len)) <= 0) { free(buffer); close(fd); return NXML_ERR_POSIX; } len += ret; } buffer[len] = 0; close(fd); nxml_empty(nxml); if (nxml->file) free(nxml->file); if (!(nxml->file = strdup(file))) { nxml_empty(nxml); free(buffer); return NXML_ERR_POSIX; } nxml->size = st.st_size; err = __nxml_parse_buffer(nxml, buffer, st.st_size); free(buffer); return err; } nxml_error_t nxml_parse_buffer(nxml_t *nxml, char *buffer, size_t size) { if (!buffer || !nxml) return NXML_ERR_DATA; nxml_empty(nxml); if (nxml->file) free(nxml->file); if (!(nxml->file = strdup("buffer"))) { nxml_empty(nxml); return NXML_ERR_POSIX; } nxml->size = size; return __nxml_parse_buffer(nxml, buffer, size); } /* EOF */ libnxml-master/src/nxml_string.c000066400000000000000000000045011410202010000172670ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" char *__nxml_string_no_space(char *str) { char *ret; int i, j; int q; int size; if (!str) return NULL; size = strlen(str); if (!(ret = (char *)malloc(sizeof(char) * (size + 1)))) return NULL; for (q = i = j = 0; i < size; i++) { if (*(str + i) == 0xd) continue; if (*(str + i) == 0xa || *(str + i) == 0x9 || *(str + i) == 0x20) { if (!q) { q = 1; ret[j++] = *(str + i); } } else { q = 0; ret[j++] = *(str + i); } } ret[j] = 0; return ret; } __nxml_string_t *__nxml_string_new(void) { __nxml_string_t *st; if (!(st = (__nxml_string_t *)calloc(1, sizeof(__nxml_string_t)))) return NULL; return st; } char *__nxml_string_free(__nxml_string_t *st) { char *ret; if (!st) return NULL; ret = st->string; free(st); return ret; } int __nxml_string_add(__nxml_string_t *st, char *what, size_t size) { if (!st || !*what) return 1; if (size <= 0) size = strlen(what); if (!st->size) { if (!(st->string = (char *)malloc(sizeof(char) * (size + 1)))) return 1; } else { if (!(st->string = (char *)realloc(st->string, sizeof(char) * (size + st->size + 1)))) return 1; } memcpy(st->string + st->size, what, size); st->size += size; st->string[st->size] = 0; return 0; } /* EOF */ libnxml-master/src/nxml_tools.c000066400000000000000000000050651410202010000171270ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" int __nxml_atoi(char *str) { int ret; sscanf(str, "%x", (unsigned int *)&ret); return ret; } int __nxml_escape_spaces(nxml_t *doc, char **buffer, size_t *size) { /* * Rule [3] - S ::= (#x20 | #x9 | #xD | #xA)+ */ int k = 0; if (!*size) return 0; while (*size && (**buffer == 0x20 || **buffer == 0x9 || **buffer == 0xd || **buffer == 0xa)) { if (**buffer == 0xa && doc->priv.func) doc->priv.line++; (*buffer)++; (*size)--; k++; } return k; } char *__nxml_get_value(nxml_t *doc, char **buffer, size_t *size) { char *attr; int i; int quot; if (!*size) return NULL; if (**buffer == '"') quot = 1; else if (**buffer == '\'') quot = 0; else return NULL; (*buffer)++; (*size)--; i = 0; while ( ((quot && *(*buffer + i) != '"') || (!quot && *(*buffer + i) != '\''))) { if (*(*buffer + i) == '\n' && doc->priv.func) doc->priv.line++; i++; if (i >= *size) return NULL; } if (quot && *(*buffer + i) != '"') return NULL; else if (!quot && *(*buffer + i) != '\'') return NULL; if (!(attr = (char *)malloc(sizeof(char) * (i + 1)))) return NULL; memcpy(attr, *buffer, i); attr[i] = 0; i++; (*buffer) += i; (*size) -= i; return attr; } char *__nxml_trim(char *tmp) { /* Trim function: */ int i = 0; while (tmp[i] == 0x20 || tmp[i] == 0x9 || tmp[i] == 0xd || tmp[i] == 0xa) tmp++; i = strlen(tmp); i--; while (tmp[i] == 0x20 || tmp[i] == 0x9 || tmp[i] == 0xd || tmp[i] == 0xa) i--; tmp[i + 1] = 0; return strdup(tmp); } /* EOF */ libnxml-master/src/nxml_utf.c000066400000000000000000000302031410202010000165550ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" /* * UTF-8 * 7bits: 0xxxxxxx * 11bits: 110xxxxx 10xxxxxx * 16bits: 1110xxxx 10xxxxxx 10xxxxxx * 21bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx * 26bits: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx * 31bits: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ int64_t __nxml_utf8(unsigned char **buffer, size_t *size, int *byte) { int64_t ret; unsigned char c0 = **buffer, c1, c2, c3, c4; if (c0 < 0x80 || *size < 2) { *byte = 1; ret = (int64_t)c0; return ret; } c1 = *(*buffer + 1); if ((c0 & 0xe0) == 0xc0 || *size < 3) { *byte = 2; ret = (((c0 & 0x1f) << 6) | (c1 & 0x3f)); return ret; } c2 = *(*buffer + 2); if ((c0 & 0xf0) == 0xe0 || *size < 4) { *byte = 3; ret = (((c0 & 0x0f) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f)); return ret; } c3 = *(*buffer + 3); if ((c0 & 0xf8) == 0xf0 || *size < 5) { *byte = 4; ret = (((c0 & 0x7) << 18) | ((c1 & 0x3f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f)); return ret; } c4 = *(*buffer + 4); if ((c0 & 0xfc) == 0xf8) { *byte = 5; ret = (((c0 & 0x3) << 24) | ((c1 & 0x3f) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f)); return ret; } *byte = 1; ret = (int64_t)c0; return ret; } #define __NXML_XTO8(x, b) \ if (byte >= 1023 - b) { \ if (!(ret = realloc(ret, (j + b) * sizeof(char)))) \ return -1; \ byte = 0; \ } \ memcpy(&ret[j], x, b); \ j += b; \ byte += b; static size_t __nxml_utf16to8(int le, unsigned char *buffer, size_t size, unsigned char **ret_buffer) { int64_t ch; int j = 0; int byte = 0; unsigned char *ret; if (!(ret = (unsigned char *)malloc(sizeof(unsigned char) * 1024))) return -1; while (size > 0) { if (le) { if ((*buffer & 0xfc) == 0xd8 && (*(buffer + 2) & 0xfc) == 0xdc) { ch = ((*buffer & 0x03) << 18) + (*(buffer + 1) << 10) + ((*(buffer + 2) & 0x03) << 8) + *(buffer + 3); buffer += 4; size -= 4; } else { ch = (*buffer << 8) + *(buffer + 1); buffer += 2; size -= 2; } } else if ((*(buffer + 1) & 0xfc) == 0xd8 && (*(buffer + 3) & 0xfc) == 0xdc) { ch = ((*(buffer + 1) & 0x03) << 18) + (*buffer << 10) + ((*(buffer + 3) & 0x03) << 8) + *(buffer + 2); buffer += 4; size -= 4; } else { ch = (*(buffer + 1) << 8) + *buffer; buffer += 2; size -= 2; } /* 8bit: 1000000 */ if (ch < 0x80) { __NXML_XTO8((void *)&ch, 1); } /* 11bit: xx100000 xx000000 * 1000 0000 0000 * 0x 8 0 0 */ else if (ch < 0x800) { /* 11bits: 110xxxxx 10xxxxxx */ char a[2]; a[0] = (ch >> 6) | 0xc0; a[1] = (ch & 0x3f) | 0x80; __NXML_XTO8((void *)a, 2); } /* 16bit: xxx10000 xx000000 xx000000 * 1 0000 0000 0000 0000 * 0x 1 0 0 0 0 */ else if (ch < 0x10000) { /* 16bits: 1110xxxx 10xxxxxx 10xxxxxx */ char a[3]; a[0] = (ch >> 12) | 0xe0; a[1] = ((ch >> 6) & 0x3f) | 0x80; a[2] = (ch & 0x3f) | 0x80; __NXML_XTO8((void *)a, 3); } /* 21bit: xxxx1000 xx000000 xx000000 xx000000 * 10 0000 0000 0000 0000 0000 * 0x 2 0 0 0 0 0 */ else if (ch < 0x200000) { /* 21bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ char a[4]; a[0] = (ch >> 18) | 0xf0; a[1] = ((ch >> 12) & 0x3f); a[2] = ((ch >> 6) & 0x3f); a[3] = (ch & 0x3f); __NXML_XTO8((void *)a, 4); } /* 26bit: xxxxx100 xx000000 xx000000 xx000000 xx000000 * 100 0000 0000 0000 0000 0000 0000 * 0x 4 0 0 0 0 0 0 */ else if (ch < 0x4000000) { /* 21bits: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ char a[5]; a[0] = (ch >> 24) | 0xf8; a[1] = ((ch >> 18) & 0x3f); a[2] = ((ch >> 12) & 0x3f); a[3] = ((ch >> 6) & 0x3f); a[4] = (ch & 0x3f); __NXML_XTO8((void *)a, 5); } } ret[j] = 0; (*ret_buffer) = ret; return (size_t)j; } static size_t __nxml_ucs4to8(int type, unsigned char *buffer, size_t size, unsigned char **ret_buffer) { int64_t ch; int j = 0; int byte = 0; unsigned char *ret; if (!(ret = (unsigned char *)malloc(sizeof(unsigned char) * 1024))) return -1; while (size > 0) { switch (type) { case 0: /* 1234 */ ch = (*buffer << 18) + (*(buffer + 1) << 12) + (*(buffer + 2) << 6) + (*(buffer + 3)); break; case 1: /* 4321 */ ch = (*buffer) + (*(buffer + 1) << 6) + (*(buffer + 2) << 12) + (*(buffer + 3) << 18); break; case 2: /* 2143 */ ch = ((*buffer) << 12) + (*(buffer + 1) << 18) + (*(buffer + 2)) + (*(buffer + 3) << 6); break; case 3: /* 3412 */ ch = ((*buffer) << 6) + (*(buffer + 1)) + (*(buffer + 2) << 18) + (*(buffer + 3) << 12); break; } buffer += 4; size -= 4; /* 8bit: 1000000 */ if (ch < 0x80) { __NXML_XTO8((void *)&ch, 1); } /* 11bit: xx100000 xx000000 * 1000 0000 0000 * 0x 8 0 0 */ else if (ch < 0x800) { /* 11bits: 110xxxxx 10xxxxxx */ char a[2]; a[0] = (ch >> 6) | 0xc0; a[1] = (ch & 0x3f) | 0x80; __NXML_XTO8((void *)a, 2); } /* 16bit: xxx10000 xx000000 xx000000 * 1 0000 0000 0000 0000 * 0x 1 0 0 0 0 */ else if (ch < 0x10000) { /* 16bits: 1110xxxx 10xxxxxx 10xxxxxx */ char a[3]; a[0] = (ch >> 12) | 0xe0; a[1] = ((ch >> 6) & 0x3f) | 0x80; a[2] = (ch & 0x3f) | 0x80; __NXML_XTO8((void *)a, 3); } /* 21bit: xxxx1000 xx000000 xx000000 xx000000 * 10 0000 0000 0000 0000 0000 * 0x 2 0 0 0 0 0 */ else if (ch < 0x200000) { /* 21bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ char a[4]; a[0] = (ch >> 18) | 0xf0; a[1] = ((ch >> 12) & 0x3f); a[2] = ((ch >> 6) & 0x3f); a[3] = (ch & 0x3f); __NXML_XTO8((void *)a, 4); } /* 26bit: xxxxx100 xx000000 xx000000 xx000000 xx000000 * 100 0000 0000 0000 0000 0000 0000 * 0x 4 0 0 0 0 0 0 */ else if (ch < 0x4000000) { /* 21bits: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ char a[5]; a[0] = (ch >> 24) | 0xf8; a[1] = ((ch >> 18) & 0x3f); a[2] = ((ch >> 12) & 0x3f); a[3] = ((ch >> 6) & 0x3f); a[4] = (ch & 0x3f); __NXML_XTO8((void *)a, 5); } } ret[j] = 0; (*ret_buffer) = ret; return (size_t)j; } int __nxml_utf_detection(char *r_buffer, size_t r_size, char **buffer, size_t *size, nxml_charset_t *charset) { /* Utf-8: 0x3c 0x3f 0x78 0x6d */ if (strncmp(r_buffer, "> 6) | 0xc0; str[1] = (ch & 0x3f) | 0x80; return 2; } /* 16bit: xxx10000 xx000000 xx000000 * 1 0000 0000 0000 0000 * 0x 1 0 0 0 0 */ else if (ch < 0x10000) { /* 16bits: 1110xxxx 10xxxxxx 10xxxxxx */ str[0] = (ch >> 12) | 0xe0; str[1] = ((ch >> 6) & 0x3f) | 0x80; str[2] = (ch & 0x3f) | 0x80; return 3; } /* 21bit: xxxx1000 xx000000 xx000000 xx000000 * 10 0000 0000 0000 0000 0000 * 0x 2 0 0 0 0 0 */ else if (ch < 0x200000) { /* 21bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ str[0] = (ch >> 18) | 0xf0; str[1] = ((ch >> 12) & 0x3f); str[2] = ((ch >> 6) & 0x3f); str[3] = (ch & 0x3f); return 4; } /* 26bit: xxxxx100 xx000000 xx000000 xx000000 xx000000 * 100 0000 0000 0000 0000 0000 0000 * 0x 4 0 0 0 0 0 0 */ else if (ch < 0x4000000) { /* 21bits: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */ str[0] = (ch >> 24) | 0xf8; str[1] = ((ch >> 18) & 0x3f); str[2] = ((ch >> 12) & 0x3f); str[3] = ((ch >> 6) & 0x3f); str[4] = (ch & 0x3f); return 5; } return 0; } /* EOF */ libnxml-master/src/nxml_write.c000066400000000000000000000216061410202010000171200ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifdef HAVE_CONFIG_H #include #else #error Use configure; make; make install #endif #include "nxml.h" static void __nxml_write_escape_string(void (*func)(void *, char *, ...), void *obj, char *str) { int i; int len; char buf[1024]; int j; #define __NXML_CHECK_BUF \ if (j == sizeof(buf) - 1) { \ buf[j] = 0; \ func(obj, "%s", buf); \ j = 0; \ } if (!str) return; len = strlen(str); for (j = i = 0; i < len; i++) { if (str[i] == '\r') continue; else if (str[i] == '<') { buf[j++] = '&'; __NXML_CHECK_BUF; buf[j++] = 'l'; __NXML_CHECK_BUF; buf[j++] = 't'; __NXML_CHECK_BUF; buf[j++] = ';'; __NXML_CHECK_BUF; } else if (str[i] == '>') { buf[j++] = '&'; __NXML_CHECK_BUF; buf[j++] = 'g'; __NXML_CHECK_BUF; buf[j++] = 't'; __NXML_CHECK_BUF; buf[j++] = ';'; __NXML_CHECK_BUF; } else if (str[i] == '&') { buf[j++] = '&'; __NXML_CHECK_BUF; buf[j++] = 'a'; __NXML_CHECK_BUF; buf[j++] = 'm'; __NXML_CHECK_BUF; buf[j++] = 'p'; __NXML_CHECK_BUF; buf[j++] = ';'; __NXML_CHECK_BUF; } else if (str[i] == '\'') { buf[j++] = '&'; __NXML_CHECK_BUF; buf[j++] = 'a'; __NXML_CHECK_BUF; buf[j++] = 'p'; __NXML_CHECK_BUF; buf[j++] = 'o'; __NXML_CHECK_BUF; buf[j++] = 's'; __NXML_CHECK_BUF; buf[j++] = ';'; __NXML_CHECK_BUF; } else if (str[i] == '\"') { buf[j++] = '&'; __NXML_CHECK_BUF; buf[j++] = 'q'; __NXML_CHECK_BUF; buf[j++] = 'u'; __NXML_CHECK_BUF; buf[j++] = 'o'; __NXML_CHECK_BUF; buf[j++] = 't'; __NXML_CHECK_BUF; buf[j++] = ';'; __NXML_CHECK_BUF; } else { buf[j++] = str[i]; __NXML_CHECK_BUF; } } if (j) { buf[j] = 0; func(obj, "%s", buf); j = 0; } } static int __nxml_write_haslines(char *what) { while (what && *what) { if (*what == '\n') return 1; what++; } return 0; } static void __nxml_write_indent(void (*func)(void *, char *, ...), void *obj, int indent) { int i; for (i = 0; i < indent; i++) func(obj, " "); } static void __nxml_write_newline(void (*func)(void *, char *, ...), void *obj) { func(obj, "\n"); } static void __nxml_write_data_text(nxml_data_t *data, void (*func)(void *, char *, ...), void *obj) { __nxml_write_escape_string(func, obj, data->value); } static void __nxml_write_data_comment(nxml_data_t *data, void (*func)(void *, char *, ...), void *obj) { func(obj, "", data->value); } static void __nxml_write_data_pi(nxml_data_t *data, void (*func)(void *, char *, ...), void *obj) { func(obj, "", data->value); } static void __nxml_write_data_doctype(nxml_doctype_t *data, void (*func)(void *, char *, ...), void *obj) { func(obj, "", data->name, data->value); } static void __nxml_write_data_element(nxml_data_t *data, void (*func)(void *, char *, ...), void *obj) { nxml_attr_t *attr; func(obj, "<"); if (data->ns && data->ns->prefix) func(obj, "%s:", data->ns->prefix); func(obj, "%s", data->value); attr = data->attributes; while (attr) { func(obj, " "); if (attr->ns && attr->ns->prefix) func(obj, "%s:", attr->ns->prefix); func(obj, "%s=\"", attr->name); __nxml_write_escape_string(func, obj, attr->value); func(obj, "\""); attr = attr->next; } if (!data->children) func(obj, " /"); func(obj, ">"); } static void __nxml_write_data(nxml_t *nxml, nxml_data_t *data, void (*func)(void *, char *, ...), void *obj, int indent) { nxml_data_t *tmp; int i; switch (data->type) { case NXML_TYPE_TEXT: if (data->children || data->next || __nxml_write_haslines(data->value) || (data->parent && data->parent->children != data)) { i = 1; __nxml_write_indent(func, obj, indent); } else i = 0; __nxml_write_data_text(data, func, obj); if (i) __nxml_write_newline(func, obj); break; case NXML_TYPE_COMMENT: __nxml_write_indent(func, obj, indent); __nxml_write_data_comment(data, func, obj); __nxml_write_newline(func, obj); break; case NXML_TYPE_PI: __nxml_write_indent(func, obj, indent); __nxml_write_data_pi(data, func, obj); __nxml_write_newline(func, obj); break; default: __nxml_write_indent(func, obj, indent); __nxml_write_data_element(data, func, obj); if (!data->children || data->children->type != NXML_TYPE_TEXT || data->children->next || __nxml_write_haslines(data->children->value)) __nxml_write_newline(func, obj); break; } if (data->children) { tmp = data->children; while (tmp) { __nxml_write_data(nxml, tmp, func, obj, indent + 1); tmp = tmp->next; } if (data->type == NXML_TYPE_ELEMENT) { if (!data->children || data->children->type != NXML_TYPE_TEXT || data->children->next || data->children->children || __nxml_write_haslines(data->children->value)) __nxml_write_indent(func, obj, indent); func(obj, "ns && data->ns->prefix) func(obj, "%s:", data->ns->prefix); func(obj, "%s>", data->value); __nxml_write_newline(func, obj); } } } static nxml_error_t __nxml_write_real(nxml_t *nxml, void (*func)(void *, char *, ...), void *obj) { nxml_data_t *data; nxml_doctype_t *doctype; func(obj, "version) { case NXML_VERSION_1_0: func(obj, "1.0"); break; default: func(obj, "1.1"); } func(obj, "\""); if (nxml->encoding) func(obj, " encoding=\"%s\"", nxml->encoding); func(obj, " standalone=\"%s\"?>\n\n", nxml->standalone ? "yes" : "no"); doctype = nxml->doctype; while (doctype) { __nxml_write_indent(func, obj, 0); __nxml_write_data_doctype(doctype, func, obj); __nxml_write_newline(func, obj); doctype = doctype->next; } data = nxml->data; while (data) { __nxml_write_data(nxml, data, func, obj, 0); data = data->next; } return NXML_OK; } static void __nxml_file_write(void *obj, char *str, ...) { va_list va; va_start(va, str); vfprintf((FILE *)obj, str, va); va_end(va); } static void __nxml_buffer_write(void *obj, char *str, ...) { va_list va; char s[4096]; int len; char **buffer = (char **)obj; va_start(va, str); len = vsnprintf(s, sizeof(s), str, va); va_end(va); if (!*buffer) { if (!(*buffer = (char *)malloc(sizeof(char) * (len + 1)))) return; strcpy(*buffer, s); } else { if (!(*buffer = (char *)realloc(*buffer, sizeof(char) * (strlen(*buffer) + len + 1)))) return; strcat(*buffer, s); } } /*************************** EXTERNAL FUNCTION ******************************/ nxml_error_t nxml_write_file(nxml_t *nxml, char *file) { FILE *fl; nxml_error_t ret; if (!nxml || !file) return NXML_ERR_DATA; if (!(fl = fopen(file, "wb"))) return NXML_ERR_POSIX; ret = __nxml_write_real(nxml, __nxml_file_write, fl); fclose(fl); return ret; } nxml_error_t nxml_write_buffer(nxml_t *nxml, char **buffer) { if (!nxml || !buffer) return NXML_ERR_DATA; return __nxml_write_real(nxml, __nxml_buffer_write, buffer); } /* EOF */ libnxml-master/test/000077500000000000000000000000001410202010000147475ustar00rootroot00000000000000libnxml-master/test/Makefile.am000066400000000000000000000007611410202010000170070ustar00rootroot00000000000000noinst_PROGRAMS = parser write namespace check_PROGRAMS = new easy test_base TESTS = $(check_PROGRAMS) test_base_SOURCES = test_base.c test_base_LDADD = ../src/libnxml.la parser_SOURCES = parser.c parser_LDADD = ../src/libnxml.la write_SOURCES = write.c write_LDADD = ../src/libnxml.la new_SOURCES = new.c new_LDADD = ../src/libnxml.la easy_SOURCES = easy.c easy_LDADD = ../src/libnxml.la namespace_SOURCES = namespace.c namespace_LDADD = ../src/libnxml.la AM_CPPFLAGS = -I$(srcdir)/../src libnxml-master/test/easy.c000066400000000000000000000102321410202010000160520ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "nxml.h" void check(nxml_t *data, nxml_error_t err) { puts(nxmle_strerror(data, err)); if (err != NXML_OK) exit(1); } int main(int argc, char **argv) { nxml_t *data; nxml_data_t *element, *text; nxml_attr_t *attribute; nxml_error_t err; char *buffer; fprintf(stdout, "New data... "); data = nxmle_new_data(&err); check(data, err); fprintf(stdout, "Freeing memory... "); err = nxmle_free(data); check(data, err); fprintf(stdout, "New data from url... "); data = nxmle_new_data_from_url("http://www.autistici.org/bakunin/index.php", &err); check(data, err); buffer = NULL; fprintf(stdout, "Writing in a buffer... "); buffer = nxmle_write_buffer(data, &err); check(data, err); fprintf(stdout, "Freeing memory... "); err = nxmle_free(data); check(data, err); fprintf(stdout, "New data from buffer... "); data = nxmle_new_data_from_buffer(buffer, 0, &err); check(data, err); free(buffer); fprintf(stdout, "Writing to file... "); err = nxmle_write_file(data, "TEST"); check(data, err); fprintf(stdout, "Freeing memory... "); err = nxmle_free(data); check(data, err); fprintf(stdout, "New data from file... "); data = nxmle_new_data_from_file("TEST", &err); check(data, err); unlink("TEST"); fprintf(stdout, "New nxml_data_t element... "); element = nxmle_add_new(data, NULL, &err); check(data, err); fprintf(stdout, "Removing nxml_data_t element... "); err = nxmle_remove(data, NULL, element); check(data, err); fprintf(stdout, "Re-insert nxml_data_t element... "); element = nxmle_add_data(data, NULL, element, &err); check(data, err); element->value = strdup("hello_world"); element->type = NXML_TYPE_ELEMENT; fprintf(stdout, "Adding attribute to element... "); attribute = nxmle_add_attribute_new(data, element, &err); check(data, err); attribute->name = strdup("hello"); attribute->value = strdup("world"); fprintf(stdout, "Removing attribute to element... "); err = nxmle_remove_attribute(data, element, attribute); check(data, err); fprintf(stdout, "Re-insert a attribute to element... "); attribute = nxmle_add_attribute_data(data, element, attribute, &err); check(data, err); fprintf(stdout, "Searching the root element... "); element = nxmle_root_element(data, &err); check(data, err); printf("ROOT element: %s\n", element->value); fprintf(stdout, "Removing the root element element... "); err = nxmle_remove(data, NULL, element); nxml_free_data(element); check(data, err); fprintf(stdout, "Searching the 'hello_world' element... "); element = nxmle_find_element(data, NULL, "hello_world", &err); check(data, err); printf("Founded element: %s\n", element->value); fprintf(stdout, "Searching attribute 'hello' in the root element... "); buffer = nxmle_find_attribute(element, "hello", &err); check(data, err); printf("Value: %s\n", buffer); free(buffer); fprintf(stdout, "New nxml_data_t element... "); text = nxmle_add_new(data, element, &err); check(data, err); text->value = strdup("bla bla bla..."); text->type = NXML_TYPE_TEXT; fprintf(stdout, "Getting string in the root element... "); buffer = nxmle_get_string(element, &err); check(data, err); printf("Value: %s\n", buffer); free(buffer); nxml_free(data); return 0; } libnxml-master/test/namespace.c000066400000000000000000000056001410202010000170500ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "nxml.h" #include void print_data(nxml_data_t *root, int id) { int i; nxml_attr_t *attr; for (i = 0; i < id; i++) printf(" "); printf("%s ", root->value); if (root->ns) { if (root->ns->prefix) printf("Prefix: -%s- NS: -%s-", root->ns->prefix, root->ns->ns); else printf("No Prefix NS: -%s-", root->ns->ns); } printf("\n"); attr = root->attributes; while (attr) { for (i = 0; i < id; i++) printf(" "); printf(" - Attr: -%s- -%s-\n", attr->name, attr->value); for (i = 0; i < id; i++) printf(" "); if (attr->ns) { if (attr->ns->prefix) printf(" - Prefix -%s- NS: -%s-", attr->ns->prefix, attr->ns->ns); else printf(" - No Prefix NS: -%s-", attr->ns->ns); printf("\n"); } attr = attr->next; } root = root->children; id++; while (root) { if (root->type == NXML_TYPE_ELEMENT) print_data(root, id); root = root->next; } } int main(int argc, char **argv) { nxml_t *data; nxml_data_t *root; nxml_error_t ret; if (argc != 2) { fprintf(stderr, "Usage:\n\t%s url_xml\n\nExample:\n\t%s " "[file.xml|http://server/file.xml]\n\n", argv[0], argv[0]); return 1; } printf("Creating a new data struct...\n"); if ((ret = nxml_new(&data)) != NXML_OK) { puts(nxml_strerror(NULL, ret)); return 1; } printf("Setting error function...\n"); if ((ret = nxml_set_func(data, nxml_print_generic)) != NXML_OK) { puts(nxml_strerror(data, ret)); return 1; } printf("Parsing the document...\n"); if (!strncmp(argv[1], "http://", 7)) ret = nxml_parse_url(data, argv[1]); else ret = nxml_parse_file(data, argv[1]); if (ret != NXML_OK) { puts(nxml_strerror(data, ret)); return 1; } printf("Namespaces...\n"); root = data->data; while (root) { if (root->type == NXML_TYPE_ELEMENT) print_data(root, 1); root = root->next; } printf("Freeing memory...\n"); nxml_free(data); puts("Bye!"); return 0; } libnxml-master/test/new.c000066400000000000000000000046161410202010000157130ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "nxml.h" void check(nxml_t *data, nxml_error_t ret) { puts(nxml_strerror(data, ret)); if (ret != NXML_OK) exit(1); } int main(int argc, char **argv) { nxml_t *data; nxml_data_t *item, *root; nxml_error_t ret; char *buffer; char s[1024]; int i; data = NULL; fprintf(stdout, "New data... "); ret = nxml_new(&data); check(NULL, ret); root = NULL; fprintf(stdout, "Creating a new entity... "); ret = nxml_add(data, NULL, &root); check(data, ret); root->value = strdup("root_element"); root->type = NXML_TYPE_ELEMENT; for (i = 0; i < 10; i++) { item = NULL; fprintf(stdout, "Creating a new entity... "); ret = nxml_add(data, root, &item); check(data, ret); snprintf(s, sizeof(s), "item%d", i); item->value = strdup(s); switch (i % 5) { case 0: item->type = NXML_TYPE_TEXT; break; case 1: item->type = NXML_TYPE_COMMENT; break; case 2: { int j; nxml_attr_t *attr; item->type = NXML_TYPE_ELEMENT; for (j = 0; j < 10; j++) { attr = NULL; fprintf(stdout, "Creating a attribute... "); ret = nxml_add_attribute(data, item, &attr); check(data, ret); snprintf(s, sizeof(s), "attr%d", j); attr->name = strdup(s); attr->value = strdup("Hello World"); } } break; case 3: item->type = NXML_TYPE_PI; break; } } buffer = NULL; ret = nxml_write_buffer(data, &buffer); check(data, ret); puts(buffer); free(buffer); nxml_free(data); return 0; } libnxml-master/test/parser.c000066400000000000000000000046731410202010000164210ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "nxml.h" #include void my_printf(char *str, ...) { va_list va; va_start(va, str); vfprintf(stderr, str, va); va_end(va); } int main(int argc, char **argv) { nxml_t *data; nxml_error_t ret; nxml_data_t *element, *tmp; nxml_doctype_t *doctype; if (argc != 2) { fprintf(stderr, "Usage:\n\t%s url_xml\n\nExample:\n\t%s " "[file.xml|http://server/file.xml]\n\n", argv[0], argv[0]); return 1; } ret = nxml_new(&data); if (ret != NXML_OK) { puts(nxml_strerror(data, ret)); return 1; } ret = nxml_set_func(data, my_printf); if (ret != NXML_OK) { puts(nxml_strerror(data, ret)); return 1; } if (!strncmp(argv[1], "http://", 7) || !strncmp(argv[1], "https://", 8)) ret = nxml_parse_url(data, argv[1]); else ret = nxml_parse_file(data, argv[1]); if (ret != NXML_OK) { puts(nxml_strerror(data, ret)); return 1; } nxml_root_element(data, &element); printf("Root element: %p: -%s-\n", (void *)element, element ? element->value : ""); if (element) { tmp = element->children; while (tmp) { printf(" subelement: %p: -%s-\n", (void *)tmp, tmp ? tmp->value : ""); tmp = tmp->next; } } nxml_find_element(data, element, "body", &element); if (element && nxml_find_element(data, element, "div", &element) == NXML_OK && element) { printf("\ndiv element: -%p-\n", (void *)element); } nxml_doctype_element(data, &doctype); if (doctype) printf("\nDoctype: -%s-%s-\n", doctype->name, doctype->value); nxml_free(data); return 0; } libnxml-master/test/test_base.c000066400000000000000000000146571410202010000171010ustar00rootroot00000000000000/* nXml - Copyright (C) 2005-2021 baku - Andrea Marchesini * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include "nxml.h" typedef struct test_case_t { const char *label; nxml_t *nxml; const char *content; size_t content_len; enum { as_buffer, as_file, } parse_as; nxml_error_t expected_result; } test_case_t; static void buffer_to_file(char *path_template, const test_case_t *tc) { int fd; const char *bytes; size_t len; fd = mkstemp(path_template); if (fd == -1) err(1, "mkstemp failed"); bytes = tc->content; len = tc->content_len; while (len > 0) { ssize_t n; n = write(fd, bytes, len); switch (n) { case 0: close(fd); errx(1, "write on %d returned zero?", fd); case -1: close(fd); errx(1, "write on %d failed", fd); default: bytes += n; len -= n; } } close(fd); } static void run_test(const test_case_t *tc) { nxml_error_t e = NXML_OK; char temp_path[] = "libnxml_test_XXXXXX"; switch (tc->parse_as) { case as_buffer: e = nxml_parse_buffer(tc->nxml, (char *)tc->content, tc->content_len); break; case as_file: buffer_to_file(temp_path, tc); e = nxml_parse_file(tc->nxml, temp_path); unlink(temp_path); break; } if (e != tc->expected_result) errx(1, "not ok - %s, expected %s, got %s", tc->label, nxml_strerror(tc->nxml, tc->expected_result), nxml_strerror(tc->nxml, e)); } #define test(_nxml, _test_case, _expected_result) \ do { \ test_case_t tc = {.label = #_test_case, \ .nxml = _nxml, \ .content = _test_case, \ .content_len = sizeof(_test_case) - 1, \ .expected_result = _expected_result}; \ tc.parse_as = as_buffer; \ run_test(&tc); \ \ tc.parse_as = as_file; \ run_test(&tc); \ } while (0) #define good_xml \ "" \ "" \ "" \ " feed title" \ " channel description" \ " " \ " title 0" \ " http://example.com" \ " hello" \ " " \ " " \ " title 1" \ " http://example.com" \ " world" \ " " \ "" \ "" #define weird_version "" #define xml_broken_afl_0 \ "" \ "" \ "" \ " feed e" \ " cha des" \ " " \ "t 0" \ " httcom" \ " hello" \ " " \ " " \ " title 1" \ " om" \ " world" \ " " \ "" \ "" #define xml_broken_afl_1 " * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "nxml.h" int main(int argc, char **argv) { nxml_t *data; nxml_error_t ret; if (argc != 3) { fprintf(stderr, "Usage:\n\t%s url_xml|file output\n\nExample:\n\t%s " "http://ngvision.org/xml file.xml\n\n", argv[0], argv[0]); return 1; } nxml_new(&data); nxml_set_func(data, nxml_print_generic); if (!strncmp(argv[1], "http://", 7) || !strncmp(argv[1], "ftp://", 6)) ret = nxml_parse_url(data, argv[1]); else ret = nxml_parse_file(data, argv[1]); if (ret) { fprintf(stderr, "NXML return error (parsing): %s\n", nxml_strerror(data, ret)); nxml_free(data); return 1; } ret = nxml_write_file(data, argv[2]); if (ret) { fprintf(stderr, "NXML return error (writing): %s\n", nxml_strerror(data, ret)); nxml_free(data); return 1; } nxml_free(data); return 0; }