lynx-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lynx-dev [PATCH 2.8.5-dev15] bzlib support


From: Ilya Zakharevich
Subject: lynx-dev [PATCH 2.8.5-dev15] bzlib support
Date: Wed, 2 Jul 2003 12:22:30 -0700
User-agent: Mutt/1.4i

This patch implements the support for bzip compression by internal
calls to libzb2.  This patch does not provide ./configure support; so
configure script should be better updated too:

  a) -lbz2 should be added to the libraries lines in makefiles;
  b) ./cfg_defs.h should include additional info;
  c) ./lynx_cfg.h should have USE_BZLIB set.

Enjoy,
Ilya

--- ././src/HTFWriter.c-ini     Wed Jul  2 20:44:36 2003
+++ ././src/HTFWriter.c Wed Jul  2 20:58:54 2003
@@ -136,7 +136,7 @@ PRIVATE void HTFWriter_free ARGS1(HTStre
     char *path = NULL;
     char *addr = NULL;
     int status;
-    BOOL use_gzread = NO;
+    BOOL use_zread = NO;
     BOOLEAN found = FALSE;
 #ifdef WIN_EX
     HANDLE cur_handle;
@@ -183,7 +183,7 @@ PRIVATE void HTFWriter_free ARGS1(HTStre
                    !strcasecomp(&path[len-2], "gz")) {
 #ifdef USE_ZLIB
                    if (!skip_loadfile) {
-                       use_gzread = YES;
+                       use_zread = YES;
                    } else
 #endif /* USE_ZLIB */
                    {
@@ -191,13 +191,20 @@ PRIVATE void HTFWriter_free ARGS1(HTStre
                        remove(path);
                    }
                } else if (len > 4 && !strcasecomp(&path[len-3], "bz2")) {
+#ifdef USE_BZLIB
+                   if (!skip_loadfile) {
+                       use_zread = YES;
+                   } else
+#endif /* USE_BZLIB */
+                   {
                    path[len-4] = '\0';
                    remove(path);
+                   }
                } else if (len > 2 && !strcasecomp(&path[len-1], "Z")) {
                    path[len-2] = '\0';
                    remove(path);
                }
-               if (!use_gzread) {
+               if (!use_zread) {
                    if (!dump_output_immediately) {
                        /*
                         *  Tell user what's happening. - FM
@@ -255,7 +262,7 @@ PRIVATE void HTFWriter_free ARGS1(HTStre
                    }
 #endif /* FNAMES_8_3 */
                    LYLocalFileToURL (&addr, path);
-                   if (!use_gzread) {
+                   if (!use_zread) {
                        LYRenamedTemp(me->anchor->FileCache, path);
                        StrAllocCopy(me->anchor->FileCache, path);
                        StrAllocCopy(me->anchor->content_encoding, "binary");
@@ -1209,8 +1216,11 @@ PUBLIC HTStream* HTCompressed ARGS3(
      * Make command to process file. - FM
      */
 #ifdef USE_ZLIB
-    if (compress_suffix[0] == 'g' && /* must be gzip */
-       !me->viewer_command) {
+    if ((compress_suffix[0] == 'g'     /* must be gzip */
+#ifdef USE_BZLIB
+       || compress_suffix[0] == 'b'    /* must be bzip2 */
+#endif
+       ) && !me->viewer_command) {
        /*
         *  We won't call gzip externally, so we don't need to supply
         *  a command for it. - kw
--- ././WWW/Library/Implementation/HTFile.c-ini Mon Apr 28 02:38:00 2003
+++ ././WWW/Library/Implementation/HTFile.c     Wed Jul  2 20:52:43 2003
@@ -46,7 +46,7 @@
 #include <stat.h>
 #endif /* VMS */
 
-#ifdef USE_ZLIB
+#if defined (USE_ZLIB) || defined (USE_BZLIB) 
 #include <GridText.h>
 #endif
 
@@ -2094,6 +2094,10 @@ PUBLIC int HTLoadFile ARGS4(
     gzFile gzfp = 0;
     BOOL use_gzread = NO;
 #endif /* USE_ZLIB */
+#ifdef USE_BZLIB
+    BZFILE *bzfp = 0;
+    BOOL use_bzread = NO;
+#endif /* USE_ZLIB */
 
     /*
     ** Reduce the filename to a basic form (hopefully unique!).
@@ -2255,6 +2259,20 @@ PUBLIC int HTLoadFile ARGS4(
                    use_gzread = YES;
                } else
 #endif /* USE_ZLIB */
+#ifdef USE_BZLIB
+               if (strcmp(format_out->name, "www/download") != 0 &&
+                   (!strcmp(HTAtom_name(myEncoding), "bzip2") ||
+                    !strcmp(HTAtom_name(myEncoding), "x-bzip2"))) {
+                   fclose(fp);
+                   if (semicolon != NULL)
+                       *semicolon = ';';
+                   bzfp = BZ2_bzopen(vmsname, BIN_R);
+
+                   CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives %p\n",
+                               vmsname, (void*)bzfp));
+                   use_zread = YES;
+               } else
+#endif /* USE_BZLIB */
                {
                    StrAllocCopy(anchor->content_type, format->name);
                    StrAllocCopy(anchor->content_encoding, 
HTAtom_name(myEncoding));
@@ -2300,7 +2318,20 @@ PUBLIC int HTLoadFile ARGS4(
                    break;
                case cftBzip2:
                    StrAllocCopy(anchor->content_encoding, "x-bzip2");
+#ifdef USE_BZLIB
+                   if (strcmp(format_out->name, "www/download") != 0) {
+                       fclose(fp);
+                       if (semicolon != NULL)
+                           *semicolon = ';';
+                       bzfp = BZ2_bzopen(vmsname, BIN_R);
+
+                       CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives %p\n",
+                                   vmsname, (void*)bzfp));
+                       use_bzread = YES;
+                   }
+#else  /* USE_BZLIB */
                    format = HTAtom_for("www/compressed");
+#endif /* USE_BZLIB */
                    break;
                case cftNone:
                    break;
@@ -2311,8 +2342,16 @@ PUBLIC int HTLoadFile ARGS4(
            FREE(filename);
            FREE(nodename);
 #ifdef USE_ZLIB
-           if (use_gzread) {
-               if (gzfp) {
+           if (use_gzread
+#  ifdef USE_BZLIB
+               || use_bzread
+#  endif
+               ) {
+               if (gzfp
+#  ifdef USE_BZLIB
+                   || bzfp
+#  endif
+                   ) {
                    char * sugfname = NULL;
                    if (anchor->SugFname) {
                        StrAllocCopy(sugfname, anchor->SugFname);
@@ -2333,6 +2372,13 @@ PUBLIC int HTLoadFile ARGS4(
                    if (sugfname && *sugfname)
                        StrAllocCopy(anchor->SugFname, sugfname);
                    FREE(sugfname);
+#  ifdef USE_BZLIB
+                   if (bzfp)
+                       status = HTParseBzFile(format, format_out,
+                                              anchor,
+                                              bzfp, sink);
+                   else
+#  endif
                    status = HTParseGzFile(format, format_out,
                                           anchor,
                                           gzfp, sink);
@@ -2615,6 +2661,18 @@ PUBLIC int HTLoadFile ARGS4(
                        use_gzread = YES;
                    } else
 #endif /* USE_ZLIB */
+#ifdef USE_BZLIB
+                   if (strcmp(format_out->name, "www/download") != 0 &&
+                       (!strcmp(HTAtom_name(myEncoding), "bzip2") ||
+                        !strcmp(HTAtom_name(myEncoding), "x-bzip2"))) {
+                       fclose(fp);
+                       bzfp = BZ2_bzopen(localname, BIN_R);
+
+                       CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives %p\n",
+                                   localname, (void*)bzfp));
+                       use_bzread = YES;
+                   } else
+#endif /* USE_BZLIB */
                    {
                        StrAllocCopy(anchor->content_type, format->name);
                        StrAllocCopy(anchor->content_encoding, 
HTAtom_name(myEncoding));
@@ -2657,7 +2715,18 @@ PUBLIC int HTLoadFile ARGS4(
                        break;
                    case cftBzip2:
                        StrAllocCopy(anchor->content_encoding, "x-bzip2");
+#ifdef USE_BZLIB
+                       if (strcmp(format_out->name, "www/download") != 0) {
+                           fclose(fp);
+                           bzfp = BZ2_bzopen(localname, BIN_R);
+
+                           CTRACE((tfp, "HTLoadFile: bzopen of `%s' gives 
%p\n",
+                                       localname, (void*)bzfp));
+                           use_bzread = YES;
+                       }
+#else  /* USE_BZLIB */
                        format = HTAtom_for("www/compressed");
+#endif /* USE_BZLIB */
                        break;
                    case cftNone:
                        break;
@@ -2666,8 +2735,16 @@ PUBLIC int HTLoadFile ARGS4(
                FREE(localname);
                FREE(nodename);
 #ifdef USE_ZLIB
-               if (use_gzread) {
-                   if (gzfp) {
+               if (use_gzread
+#  ifdef USE_BZLIB
+                   || use_bzread
+#  endif
+                   ) {
+                   if (gzfp
+#  ifdef USE_BZLIB
+                       || bzfp
+#  endif
+                       ) {
                        char * sugfname = NULL;
                        if (anchor->SugFname) {
                            StrAllocCopy(sugfname, anchor->SugFname);
@@ -2688,6 +2765,13 @@ PUBLIC int HTLoadFile ARGS4(
                        if (sugfname && *sugfname)
                            StrAllocCopy(anchor->SugFname, sugfname);
                        FREE(sugfname);
+#  ifdef USE_BZLIB
+                       if (bzfp)
+                           status = HTParseBzFile(format, format_out,
+                                                  anchor,
+                                                  bzfp, sink);
+                       else
+#  endif
                        status = HTParseGzFile(format, format_out,
                                               anchor,
                                               gzfp, sink);
--- ././WWW/Library/Implementation/HTFormat.c-ini       Wed Jul  2 20:30:21 2003
+++ ././WWW/Library/Implementation/HTFormat.c   Wed Jul  2 20:58:27 2003
@@ -1057,6 +1057,87 @@ PRIVATE int HTGzFileCopy ARGS2(
 }
 #endif /* USE_ZLIB */
 
+#ifdef USE_BZLIB
+/*     Push data from a bzip file pointer down a stream
+**     -------------------------------------
+**
+**   This routine is responsible for creating and PRESENTING any
+**   graphic (or other) objects described by the file.
+**
+**
+**  State of file and target stream on entry:
+**                   BZFILE (bzfp) assumed open (should have bzipped content),
+**                   target (sink) assumed valid.
+**
+**  Return values:
+**     HT_INTERRUPTED  Interruption after some data read.
+**     HT_PARTIAL_CONTENT      Error after some data read.
+**     -1              Error before any data read.
+**     HT_LOADED       Normal end of file indication on reading.
+**
+**  State of file and target stream on return:
+**     always          bzfp still open, target stream still valid.
+*/
+PRIVATE int HTBzFileCopy ARGS2(
+       BZFILE *,               bzfp,
+       HTStream*,              sink)
+{
+    HTStreamClass targetClass;
+    int status, bytes;
+    int bzerrnum;
+    int rv = HT_OK;
+
+    /* Push the data down the stream
+    */
+    targetClass = *(sink->isa); /* Copy pointers to procedures */
+
+    /* read and inflate bzip'd file, and push binary down sink
+    */
+    HTReadProgress(bytes = 0, 0);
+    for (;;) {
+       status = BZ2_bzread(bzfp, input_buffer, INPUT_BUFFER_SIZE);
+       if (status <= 0) { /* EOF or error */
+           if (status == 0) {
+               rv = HT_LOADED;
+               break;
+           }
+           CTRACE((tfp, "HTBzFileCopy: Read error, bzread returns %d\n",
+                       status));
+           CTRACE((tfp, "bzerror   : %s\n",
+                       BZ2_bzerror(bzfp, &bzerrnum)));
+           if (TRACE) {
+               if (bzerrnum == Z_ERRNO)
+                   perror("bzerror   ");
+           }
+           if (bytes) {
+               rv = HT_PARTIAL_CONTENT;
+           } else {
+               rv = -1;
+           }
+           break;
+       }
+
+       (*targetClass.put_block)(sink, input_buffer, status);
+       bytes += status;
+       HTReadProgress(bytes, -1);
+       HTDisplayPartial();
+
+       if (HTCheckForInterrupt()) {
+           _HTProgress (TRANSFER_INTERRUPTED);
+           if (bytes) {
+               rv = HT_INTERRUPTED;
+           } else {
+               rv = -1;
+           }
+           break;
+       }
+    } /* next bufferload */
+
+    HTFinishDisplayPartial();
+    return rv;
+}
+#endif /* USE_BZLIB */
+
 /*     Push data from a socket down a stream STRIPPING CR
 **     --------------------------------------------------
 **
@@ -1376,6 +1457,82 @@ PUBLIC int HTParseGzFile ARGS5(
 }
 #endif /* USE_ZLIB */
 
+#ifdef USE_BZLIB
+PRIVATE void HTCloseBzFile ARGS1(
+       BZFILE *,               bzfp)
+{
+    if (bzfp)
+       BZ2_bzclose(bzfp);
+}
+
+/*     HTParseBzFile
+**
+**  State of file and target stream on entry:
+**                     bzFile (bzfp) assumed open,
+**                     target (sink) usually NULL (will call stream stack).
+**
+**  Return values:
+**     -501            Stream stack failed (cannot present or convert).
+**     -1              Download cancelled.
+**     HT_NO_DATA      Error before any data read.
+**     HT_PARTIAL_CONTENT      Interruption or error after some data read.
+**     HT_LOADED       Normal end of file indication on reading.
+**
+**  State of file and target stream on return:
+**     always          bzfp closed; target freed, aborted, or NULL.
+*/
+PUBLIC int HTParseBzFile ARGS5(
+       HTFormat,               rep_in,
+       HTFormat,               format_out,
+       HTParentAnchor *,       anchor,
+       BZFILE*,                bzfp,
+       HTStream*,              sink)
+{
+    HTStream * stream;
+    HTStreamClass targetClass;
+    int rv;
+
+    stream = HTStreamStack(rep_in, format_out, sink, anchor);
+
+    if (!stream) {
+       char *buffer = 0;
+       HTCloseBzFile(bzfp);
+       if (LYCancelDownload) {
+           LYCancelDownload = FALSE;
+           return -1;
+       }
+       HTSprintf0(&buffer, CANNOT_CONVERT_I_TO_O,
+               HTAtom_name(rep_in), HTAtom_name(format_out));
+       CTRACE((tfp, "HTFormat(in HTParseBzFile): %s\n", buffer));
+       rv = HTLoadError(sink, 501, buffer);
+       FREE(buffer);
+       return rv;
+    }
+
+    /* Push the data down the stream
+    **
+    ** @@  Bug:  This decision ought to be made based on "encoding"
+    ** rather than on content-type.  @@@  When we handle encoding.
+    ** The current method smells anyway.
+    */
+    targetClass = *(stream->isa);      /* Copy pointers to procedures */
+    rv = HTBzFileCopy(bzfp, stream);
+    if (rv == -1 || rv == HT_INTERRUPTED) {
+       (*targetClass._abort)(stream, NULL);
+    } else {
+       (*targetClass._free)(stream);
+    }
+
+    HTCloseBzFile(bzfp);
+    if (rv == -1)
+       return HT_NO_DATA;
+    else if (rv == HT_INTERRUPTED || (rv > 0 && rv != HT_LOADED))
+       return HT_PARTIAL_CONTENT;
+    else
+       return HT_LOADED;
+}
+#endif /* USE_BZLIB */
+
 /*     Converter stream: Network Telnet to internal character text
 **     -----------------------------------------------------------
 **
--- ././WWW/Library/Implementation/HTFormat.h-ini       Wed Jul  2 20:41:00 2003
+++ ././WWW/Library/Implementation/HTFormat.h   Wed Jul  2 20:43:44 2003
@@ -444,10 +444,7 @@ extern int HTParseMem PARAMS((
 #endif
 
 #ifdef USE_ZLIB
-
-#ifdef USE_ZLIB
 #include <zlib.h>
-#endif /* USE_ZLIB */
 /*
 HTParseGzFile: Parse a gzipped File through a file pointer
 
@@ -464,6 +461,24 @@ extern int HTParseGzFile PARAMS((
 
 #endif /* USE_ZLIB */
 
+#ifdef USE_BZLIB
+#include <bzlib.h>
+/*
+HTParseBzFile: Parse a bzipped File through a file pointer
+
+   This routine is called by protocols modules to load an object.  uses
+   HTStreamStack and HTGzFileCopy.  Returns HT_LOADED if successful, can also
+   return HT_PARTIAL_CONTENT, HT_NO_DATA, or other <0 for failure.
+ */
+extern int HTParseBzFile PARAMS((
+        HTFormat        format_in,
+        HTFormat        format_out,
+        HTParentAnchor  *anchor,
+        BZFILE          *bzfp,
+        HTStream*       sink));
+
+#endif /* USE_BZLIB */
+
 /*
 
 HTNetToText: Convert Net ASCII to local representation

; To UNSUBSCRIBE: Send "unsubscribe lynx-dev" to address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]