@@ -1158,65 +1158,36 @@ xmlIODefaultMatch(const char *filename ATTRIBUTE_UNUSED) {
11581158 return (1 );
11591159}
11601160
1161- /**
1162- * xmlInputDefaultOpen:
1163- * @buf: input buffer to be filled
1164- * @filename: filename or URI
1165- *
1166- * Returns an xmlParserErrors code.
1167- */
1168- static int
1169- xmlInputDefaultOpen (xmlParserInputBufferPtr buf , const char * filename ) {
1170- int ret ;
1171- int fd ;
1172-
1173- #ifdef LIBXML_FTP_ENABLED
1174- if (xmlIOFTPMatch (filename )) {
1175- buf -> context = xmlIOFTPOpen (filename );
1176-
1177- if (buf -> context != NULL ) {
1178- buf -> readcallback = xmlIOFTPRead ;
1179- buf -> closecallback = xmlIOFTPClose ;
1180- return (XML_ERR_OK );
1181- }
1182- }
1183- #endif /* LIBXML_FTP_ENABLED */
1184-
1185- #ifdef LIBXML_HTTP_ENABLED
1186- if (xmlIOHTTPMatch (filename )) {
1187- buf -> context = xmlIOHTTPOpen (filename );
1188-
1189- if (buf -> context != NULL ) {
1190- buf -> readcallback = xmlIOHTTPRead ;
1191- buf -> closecallback = xmlIOHTTPClose ;
1192- return (XML_ERR_OK );
1193- }
1194- }
1195- #endif /* LIBXML_HTTP_ENABLED */
1161+ int
1162+ xmlInputFromFd (xmlParserInputBufferPtr buf , int fd , int unzip ) {
1163+ int copy ;
11961164
1197- if (!xmlFileMatch (filename ))
1198- return (XML_IO_ENOENT );
1165+ (void ) unzip ;
11991166
12001167#ifdef LIBXML_LZMA_ENABLED
1201- {
1168+ if ( unzip ) {
12021169 xzFile xzStream ;
1170+ off_t pos ;
12031171
1204- ret = xmlFdOpen (filename , 0 , & fd );
1205- if (ret != XML_ERR_OK )
1206- return (ret );
1172+ pos = lseek (fd , 0 , SEEK_CUR );
12071173
1208- xzStream = __libxml2_xzdopen (filename , fd , "rb" );
1174+ copy = dup (fd );
1175+ if (copy == -1 )
1176+ return (xmlIOErr (0 , "dup()" ));
1177+
1178+ xzStream = __libxml2_xzdopen ("?" , copy , "rb" );
12091179
12101180 if (xzStream == NULL ) {
1211- close (fd );
1181+ close (copy );
12121182 } else {
1213- /*
1214- * Non-regular files like pipes can't be reopened.
1215- * If a file isn't seekable, we pipe uncompressed
1216- * input through xzlib.
1217- */
1218- if ((lseek (fd , 0 , SEEK_CUR ) < 0 ) ||
1219- (__libxml2_xzcompressed (xzStream ) > 0 )) {
1183+ if ((__libxml2_xzcompressed (xzStream ) > 0 ) ||
1184+ /* Try to rewind if not gzip compressed */
1185+ (pos < 0 ) ||
1186+ (lseek (fd , pos , SEEK_SET ) < 0 )) {
1187+ /*
1188+ * If a file isn't seekable, we pipe uncompressed
1189+ * input through xzlib.
1190+ */
12201191 buf -> context = xzStream ;
12211192 buf -> readcallback = xmlXzfileRead ;
12221193 buf -> closecallback = xmlXzfileClose ;
@@ -1231,25 +1202,29 @@ xmlInputDefaultOpen(xmlParserInputBufferPtr buf, const char *filename) {
12311202#endif /* LIBXML_LZMA_ENABLED */
12321203
12331204#ifdef LIBXML_ZLIB_ENABLED
1234- {
1205+ if ( unzip ) {
12351206 gzFile gzStream ;
1207+ off_t pos ;
12361208
1237- ret = xmlFdOpen (filename , 0 , & fd );
1238- if (ret != XML_ERR_OK )
1239- return (ret );
1209+ pos = lseek (fd , 0 , SEEK_CUR );
1210+
1211+ copy = dup (fd );
1212+ if (copy == -1 )
1213+ return (xmlIOErr (0 , "dup()" ));
12401214
1241- gzStream = gzdopen (fd , "rb" );
1215+ gzStream = gzdopen (copy , "rb" );
12421216
12431217 if (gzStream == NULL ) {
1244- close (fd );
1218+ close (copy );
12451219 } else {
1246- /*
1247- * Non-regular files like pipes can't be reopened.
1248- * If a file isn't seekable, we pipe uncompressed
1249- * input through zlib.
1250- */
1251- if ((lseek (fd , 0 , SEEK_CUR ) < 0 ) ||
1252- (gzdirect (gzStream ) == 0 )) {
1220+ if ((gzdirect (gzStream ) == 0 ) ||
1221+ /* Try to rewind if not gzip compressed */
1222+ (pos < 0 ) ||
1223+ (lseek (fd , pos , SEEK_SET ) < 0 )) {
1224+ /*
1225+ * If a file isn't seekable, we pipe uncompressed
1226+ * input through zlib.
1227+ */
12531228 buf -> context = gzStream ;
12541229 buf -> readcallback = xmlGzfileRead ;
12551230 buf -> closecallback = xmlGzfileClose ;
@@ -1263,16 +1238,67 @@ xmlInputDefaultOpen(xmlParserInputBufferPtr buf, const char *filename) {
12631238 }
12641239#endif /* LIBXML_ZLIB_ENABLED */
12651240
1266- ret = xmlFdOpen ( filename , 0 , & fd );
1267- if (ret != XML_ERR_OK )
1268- return (ret );
1241+ copy = dup ( fd );
1242+ if (copy == -1 )
1243+ return (xmlIOErr ( 0 , "dup()" ) );
12691244
1270- buf -> context = (void * ) (ptrdiff_t ) fd ;
1245+ buf -> context = (void * ) (ptrdiff_t ) copy ;
12711246 buf -> readcallback = xmlFdRead ;
12721247 buf -> closecallback = xmlFdClose ;
1248+
12731249 return (XML_ERR_OK );
12741250}
12751251
1252+ /**
1253+ * xmlInputDefaultOpen:
1254+ * @buf: input buffer to be filled
1255+ * @filename: filename or URI
1256+ *
1257+ * Returns an xmlParserErrors code.
1258+ */
1259+ static int
1260+ xmlInputDefaultOpen (xmlParserInputBufferPtr buf , const char * filename ) {
1261+ int ret ;
1262+ int fd ;
1263+
1264+ #ifdef LIBXML_FTP_ENABLED
1265+ if (xmlIOFTPMatch (filename )) {
1266+ buf -> context = xmlIOFTPOpen (filename );
1267+
1268+ if (buf -> context != NULL ) {
1269+ buf -> readcallback = xmlIOFTPRead ;
1270+ buf -> closecallback = xmlIOFTPClose ;
1271+ return (XML_ERR_OK );
1272+ }
1273+ }
1274+ #endif /* LIBXML_FTP_ENABLED */
1275+
1276+ #ifdef LIBXML_HTTP_ENABLED
1277+ if (xmlIOHTTPMatch (filename )) {
1278+ buf -> context = xmlIOHTTPOpen (filename );
1279+
1280+ if (buf -> context != NULL ) {
1281+ buf -> readcallback = xmlIOHTTPRead ;
1282+ buf -> closecallback = xmlIOHTTPClose ;
1283+ return (XML_ERR_OK );
1284+ }
1285+ }
1286+ #endif /* LIBXML_HTTP_ENABLED */
1287+
1288+ if (!xmlFileMatch (filename ))
1289+ return (XML_IO_ENOENT );
1290+
1291+ ret = xmlFdOpen (filename , 0 , & fd );
1292+ if (ret != XML_ERR_OK )
1293+ return (ret );
1294+
1295+ ret = xmlInputFromFd (buf , fd , /* unzip */ 1 );
1296+
1297+ close (fd );
1298+
1299+ return (ret );
1300+ }
1301+
12761302#ifdef LIBXML_OUTPUT_ENABLED
12771303/**
12781304 * xmlOutputDefaultOpen:
0 commit comments