Fix build with zlib on MS-Windows.
[bpt/emacs.git] / src / decompress.c
index 866f4f5..af8435b 100644 (file)
@@ -26,6 +26,58 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "character.h"
 #include "buffer.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 0;                                          \
+  }
+
+DEF_ZLIB_FN (int, inflateInit2_,
+            (z_streamp strm, int  windowBits, const char *version, int stream_size));
+
+DEF_ZLIB_FN (int, inflate,
+            (z_streamp strm, int flush));
+
+DEF_ZLIB_FN (int, inflateEnd,
+            (z_streamp strm));
+
+static bool
+init_zlib_functions (void)
+{
+  HMODULE library = w32_delayed_load (Qzlib_dll);
+
+  if (!library)
+    {
+      message1 ("zlib library not found");
+      return 0;
+    }
+
+  LOAD_ZLIB_FN (library, inflateInit2_);
+  LOAD_ZLIB_FN (library, inflate);
+  LOAD_ZLIB_FN (library, inflateEnd);
+  return 1;
+}
+
+#else /* !WINDOWSNT */
+
+#define fn_inflateInit2_       inflateInit2_
+#define fn_inflate             inflate
+#define fn_inflateEnd          inflateEnd
+
+#endif /* WINDOWSNT */
+
+#define fn_inflateInit2(strm, windowBits) \
+        fn_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+
 \f
 struct decompress_unwind_data
 {
@@ -37,7 +89,7 @@ static void
 unwind_decompress (void *ddata)
 {
   struct decompress_unwind_data *data = ddata;
-  inflateEnd (data->stream);
+  fn_inflateEnd (data->stream);
 
   /* Delete any uncompressed data already inserted and restore point.  */
   if (data->start)
@@ -47,6 +99,26 @@ unwind_decompress (void *ddata)
     }
 }
 
+DEFUN ("zlib-available-p", Fzlib_available_p, Szlib_available_p, 0, 0, 0,
+       doc: /* Return t if zlib decompression is available in this instance of Emacs.  */)
+     (void)
+{
+#ifdef WINDOWSNT
+  Lisp_Object found = Fassq (Qzlib_dll, Vlibrary_cache);
+  if (CONSP (found))
+    return XCDR (found);
+  else
+    {
+      Lisp_Object status;
+      status = init_zlib_functions () ? Qt : Qnil;
+      Vlibrary_cache = Fcons (Fcons (Qzlib_dll, status), Vlibrary_cache);
+      return status;
+    }
+#else
+  return Qt;
+#endif
+}
+
 DEFUN ("decompress-gzipped-region", Fdecompress_gzipped_region,
        Sdecompress_gzipped_region,
        2, 2, 0,
@@ -80,7 +152,7 @@ This function can be called only in unibyte buffers.  */)
   stream.next_in = Z_NULL;
 
   /* This magic number apparently means "this is gzip".  */
-  if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK)
+  if (fn_inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK)
     return Qnil;
 
   unwind_data.start = iend;
@@ -111,7 +183,7 @@ 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 = inflate (&stream, Z_NO_FLUSH);
+      inflate_status = fn_inflate (&stream, Z_NO_FLUSH);
       pos_byte += avail_in - stream.avail_in;
       decompressed = avail_out - stream.avail_out;
       insert_from_gap (decompressed, decompressed, 0);
@@ -137,7 +209,9 @@ This function can be called only in unibyte buffers.  */)
 void
 syms_of_decompress (void)
 {
+  DEFSYM (Qzlib_dll, "zlib");
   defsubr (&Sdecompress_gzipped_region);
+  defsubr (&Szlib_available_p);
 }
 
 #endif /* HAVE_ZLIB */