From 956dfd2dddfe9b88ce9643f5495bf9a746bcbcfd Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sat, 27 Sep 2025 21:43:57 +0200 Subject: [PATCH] xmllint: Use zlib directly This prepares xmllint for eventually removing decompression support from libxml2. --- CMakeLists.txt | 3 +++ Makefile.am | 4 +-- configure.ac | 2 ++ meson.build | 4 +++ xmllint.c | 69 +++++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 116c77dd2..b9981fdc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -455,6 +455,9 @@ if(LIBXML2_WITH_PROGRAMS) endif() install(TARGETS ${PROGRAM} EXPORT LibXml2 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT programs) endforeach() + if(LIBXML2_WITH_ZLIB) + target_link_libraries(xmllint ZLIB::ZLIB) + endif() endif() if(LIBXML2_WITH_TESTS) diff --git a/Makefile.am b/Makefile.am index 19ec30550..f053b2f04 100644 --- a/Makefile.am +++ b/Makefile.am @@ -137,9 +137,9 @@ runsuite_DEPENDENCIES = $(DEPS) runsuite_LDADD= $(LDADDS) xmllint_SOURCES = xmllint.c shell.c lintmain.c -xmllint_CFLAGS = $(AM_CFLAGS) $(RDL_CFLAGS) +xmllint_CFLAGS = $(AM_CFLAGS) $(RDL_CFLAGS) $(Z_CFLAGS) xmllint_DEPENDENCIES = $(DEPS) -xmllint_LDADD= $(RDL_LIBS) $(LDADDS) +xmllint_LDADD = $(RDL_LIBS) $(Z_LIBS) $(LDADDS) testModule_SOURCES=testModule.c testModule_DEPENDENCIES = $(DEPS) diff --git a/configure.ac b/configure.ac index 4eb629bb3..d8dc4e1b4 100644 --- a/configure.ac +++ b/configure.ac @@ -838,6 +838,8 @@ if test "$with_zlib" != "no" && test "$with_zlib" != ""; then XML_PRIVATE_LIBS="${XML_PRIVATE_LIBS} ${Z_LIBS}" fi AC_SUBST(WITH_ZLIB) +AC_SUBST(Z_CFLAGS) +AC_SUBST(Z_LIBS) dnl dnl Checks for iconv library. diff --git a/meson.build b/meson.build index e0f1c729c..1d4b6c3b5 100644 --- a/meson.build +++ b/meson.build @@ -356,6 +356,10 @@ if want_history xmllint_deps += history_dep endif +if want_zlib + xmllint_deps += dependency('zlib') +endif + ### crypto if sys_windows == true bcrypt_dep = cc.find_library('bcrypt', required: true) diff --git a/xmllint.c b/xmllint.c index 5140e9358..cd629ee11 100644 --- a/xmllint.c +++ b/xmllint.c @@ -33,6 +33,10 @@ #endif #endif +#ifdef LIBXML_ZLIB_ENABLED + #include +#endif + #include #include #include @@ -329,10 +333,28 @@ xmllintResourceLoader(void *ctxt, const char *URL, * * ************************************************************************/ +#ifdef LIBXML_ZLIB_ENABLED +static int +xmllintGzRead(void *ctxt, char *buf, int len) { + return gzread(ctxt, buf, len); +} + +static int +xmllintGzClose(void *ctxt) { + if (gzclose(ctxt) != Z_OK) + return -1; + + return 0; +} +#endif + static xmlDocPtr parseXml(xmllintState *lint, const char *filename) { xmlParserCtxtPtr ctxt = lint->ctxt; xmlDocPtr doc; +#ifdef LIBXML_ZLIB_ENABLED + gzFile gz; +#endif #ifdef LIBXML_PUSH_ENABLED if (lint->appOptions & XML_LINT_PUSH_ENABLED) { @@ -380,12 +402,26 @@ parseXml(xmllintState *lint, const char *filename) { } #endif +#ifdef LIBXML_ZLIB_ENABLED if (strcmp(filename, "-") == 0) - doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, - lint->parseOptions | XML_PARSE_UNZIP); + gz = gzdopen(STDIN_FILENO, "rb"); else - doc = xmlCtxtReadFile(ctxt, filename, NULL, - lint->parseOptions | XML_PARSE_UNZIP); + gz = gzopen(filename, "rb"); + + if (gz == NULL) { + fprintf(lint->errStream, "Can't open %s\n", filename); + lint->progresult = XMLLINT_ERR_RDFILE; + return(NULL); + } + + doc = xmlCtxtReadIO(ctxt, xmllintGzRead, xmllintGzClose, gz, + filename, NULL, lint->parseOptions); +#else + if (strcmp(filename, "-") == 0) + doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, lint->parseOptions); + else + doc = xmlCtxtReadFile(ctxt, filename, NULL, lint->parseOptions); +#endif return(doc); } @@ -1351,16 +1387,35 @@ static void streamFile(xmllintState *lint, const char *filename) { } else #endif { +#ifdef LIBXML_ZLIB_ENABLED + gzFile gz; +#endif + xmlResetLastError(); +#ifdef LIBXML_ZLIB_ENABLED + if (strcmp(filename, "-") == 0) + gz = gzdopen(STDIN_FILENO, "rb"); + else + gz = gzopen(filename, "rb"); + + if (gz == NULL) { + fprintf(lint->errStream, "Can't open %s\n", filename); + lint->progresult = XMLLINT_ERR_RDFILE; + return; + } + + reader = xmlReaderForIO(xmllintGzRead, xmllintGzClose, gz, + filename, NULL, lint->parseOptions); +#else if (strcmp(filename, "-") == 0) { reader = xmlReaderForFd(STDIN_FILENO, "-", NULL, - lint->parseOptions | XML_PARSE_UNZIP); + lint->parseOptions); } else { - reader = xmlReaderForFile(filename, NULL, - lint->parseOptions | XML_PARSE_UNZIP); + reader = xmlReaderForFile(filename, NULL, lint->parseOptions); } +#endif if (reader == NULL) { const xmlError *error = xmlGetLastError();