]> code.delx.au - gnu-emacs/blobdiff - src/decompress.c
rmailsum.el minor optional argument fix
[gnu-emacs] / src / decompress.c
index b4e122c3ba88f4dc1a6cf12da930b0622fd2ca42..b14f0a2cd79f75f7c976d6ddc6de6586ae4bc548 100644 (file)
@@ -1,5 +1,5 @@
 /* Interface to zlib.
-   Copyright (C) 2013 Free Software Foundation, Inc.
+   Copyright (C) 2013-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -28,29 +28,15 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <verify.h>
 
-static Lisp_Object Qzlib_dll;
-
 #ifdef WINDOWSNT
-#include <windows.h>
-#include "w32.h"
-
-/* Macro for defining functions that will be loaded from the zlib DLL.  */
-#define DEF_ZLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
-
-/* Macro for loading zlib functions from the library.  */
-#define LOAD_ZLIB_FN(lib,func) {                                       \
-    fn_##func = (void *) GetProcAddress (lib, #func);                  \
-    if (!fn_##func) return false;                                      \
-  }
-
-DEF_ZLIB_FN (int, inflateInit2_,
-            (z_streamp strm, int  windowBits, const char *version, int stream_size));
+# include <windows.h>
+# include "w32.h"
 
-DEF_ZLIB_FN (int, inflate,
-            (z_streamp strm, int flush));
-
-DEF_ZLIB_FN (int, inflateEnd,
-            (z_streamp strm));
+DEF_DLL_FN (int, inflateInit2_,
+           (z_streamp strm, int windowBits, const char *version,
+            int stream_size));
+DEF_DLL_FN (int, inflate, (z_streamp strm, int flush));
+DEF_DLL_FN (int, inflateEnd, (z_streamp strm));
 
 static bool zlib_initialized;
 
@@ -60,32 +46,28 @@ init_zlib_functions (void)
   HMODULE library = w32_delayed_load (Qzlib_dll);
 
   if (!library)
-    {
-      message1 ("zlib library not found");
-      return false;
-    }
+    return false;
 
-  LOAD_ZLIB_FN (library, inflateInit2_);
-  LOAD_ZLIB_FN (library, inflate);
-  LOAD_ZLIB_FN (library, inflateEnd);
+  LOAD_DLL_FN (library, inflateInit2_);
+  LOAD_DLL_FN (library, inflate);
+  LOAD_DLL_FN (library, inflateEnd);
   return true;
 }
 
-#define fn_inflateInit2(strm, windowBits) \
-        fn_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
-
-#else /* !WINDOWSNT */
+# undef inflate
+# undef inflateEnd
+# undef inflateInit2_
 
-#define fn_inflateInit2                inflateInit2
-#define fn_inflate             inflate
-#define fn_inflateEnd          inflateEnd
+# define inflate fn_inflate
+# define inflateEnd fn_inflateEnd
+# define inflateInit2_ fn_inflateInit2_
 
 #endif /* WINDOWSNT */
 
 \f
 struct decompress_unwind_data
 {
-  ptrdiff_t old_point, start;
+  ptrdiff_t old_point, start, nbytes;
   z_stream *stream;
 };
 
@@ -93,11 +75,11 @@ static void
 unwind_decompress (void *ddata)
 {
   struct decompress_unwind_data *data = ddata;
-  fn_inflateEnd (data->stream);
+  inflateEnd (data->stream);
 
   /* Delete any uncompressed data already inserted on error.  */
   if (data->start)
-    del_range (data->start, PT);
+    del_range (data->start, data->start + data->nbytes);
 
   /* Put point where it was, or if the buffer has shrunk because the
      compressed data is bigger than the uncompressed, at
@@ -116,7 +98,8 @@ DEFUN ("zlib-available-p", Fzlib_available_p, Szlib_available_p, 0, 0, 0,
   else
     {
       Lisp_Object status;
-      status = init_zlib_functions () ? Qt : Qnil;
+      zlib_initialized = init_zlib_functions ();
+      status = zlib_initialized ? Qt : Qnil;
       Vlibrary_cache = Fcons (Fcons (Qzlib_dll, status), Vlibrary_cache);
       return status;
     }
@@ -149,7 +132,10 @@ This function can be called only in unibyte buffers.  */)
   if (!zlib_initialized)
     zlib_initialized = init_zlib_functions ();
   if (!zlib_initialized)
-    return Qnil;
+    {
+      message1 ("zlib library not found");
+      return Qnil;
+    }
 #endif
 
   /* This is a unibyte buffer, so character positions and bytes are
@@ -164,15 +150,15 @@ This function can be called only in unibyte buffers.  */)
   stream.avail_in = 0;
   stream.next_in = Z_NULL;
 
-  /* The magic number 32 apparently means "autodect both the gzip and
+  /* The magic number 32 apparently means "autodetect both the gzip and
      zlib formats" according to zlib.h.  */
-  if (fn_inflateInit2 (&stream, MAX_WBITS + 32) != Z_OK)
+  if (inflateInit2 (&stream, MAX_WBITS + 32) != Z_OK)
     return Qnil;
 
   unwind_data.start = iend;
   unwind_data.stream = &stream;
   unwind_data.old_point = PT;
-
+  unwind_data.nbytes = 0;
   record_unwind_protect_ptr (unwind_decompress, &unwind_data);
 
   /* Insert the decompressed data at the end of the compressed data.  */
@@ -196,10 +182,11 @@ This function can be called only in unibyte buffers.  */)
       stream.avail_in = avail_in;
       stream.next_out = GPT_ADDR;
       stream.avail_out = avail_out;
-      inflate_status = fn_inflate (&stream, Z_NO_FLUSH);
+      inflate_status = inflate (&stream, Z_NO_FLUSH);
       pos_byte += avail_in - stream.avail_in;
       decompressed = avail_out - stream.avail_out;
       insert_from_gap (decompressed, decompressed, 0);
+      unwind_data.nbytes += decompressed;
       QUIT;
     }
   while (inflate_status == Z_OK);