2 Copyright (C) 2013 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include "character.h"
31 static Lisp_Object Qzlib_dll
;
37 /* Macro for defining functions that will be loaded from the zlib DLL. */
38 #define DEF_ZLIB_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
40 /* Macro for loading zlib functions from the library. */
41 #define LOAD_ZLIB_FN(lib,func) { \
42 fn_##func = (void *) GetProcAddress (lib, #func); \
43 if (!fn_##func) return false; \
46 DEF_ZLIB_FN (int, inflateInit2_
,
47 (z_streamp strm
, int windowBits
, const char *version
, int stream_size
));
49 DEF_ZLIB_FN (int, inflate
,
50 (z_streamp strm
, int flush
));
52 DEF_ZLIB_FN (int, inflateEnd
,
55 static bool zlib_initialized
;
58 init_zlib_functions (void)
60 HMODULE library
= w32_delayed_load (Qzlib_dll
);
64 message1 ("zlib library not found");
68 LOAD_ZLIB_FN (library
, inflateInit2_
);
69 LOAD_ZLIB_FN (library
, inflate
);
70 LOAD_ZLIB_FN (library
, inflateEnd
);
74 #define fn_inflateInit2(strm, windowBits) \
75 fn_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
77 #else /* !WINDOWSNT */
79 #define fn_inflateInit2 inflateInit2
80 #define fn_inflate inflate
81 #define fn_inflateEnd inflateEnd
83 #endif /* WINDOWSNT */
86 struct decompress_unwind_data
88 ptrdiff_t old_point
, start
;
93 unwind_decompress (void *ddata
)
95 struct decompress_unwind_data
*data
= ddata
;
96 fn_inflateEnd (data
->stream
);
98 /* Delete any uncompressed data already inserted on error. */
100 del_range (data
->start
, PT
);
102 /* Put point where it was, or if the buffer has shrunk because the
103 compressed data is bigger than the uncompressed, at
105 SET_PT (min (data
->old_point
, ZV
));
108 DEFUN ("zlib-available-p", Fzlib_available_p
, Szlib_available_p
, 0, 0, 0,
109 doc
: /* Return t if zlib decompression is available in this instance of Emacs. */)
113 Lisp_Object found
= Fassq (Qzlib_dll
, Vlibrary_cache
);
119 status
= init_zlib_functions () ? Qt
: Qnil
;
120 Vlibrary_cache
= Fcons (Fcons (Qzlib_dll
, status
), Vlibrary_cache
);
128 DEFUN ("zlib-decompress-region", Fzlib_decompress_region
,
129 Szlib_decompress_region
,
131 doc
: /* Decompress a gzip- or zlib-compressed region.
132 Replace the text in the region by the decompressed data.
133 On failure, return nil and leave the data in place.
134 This function can be called only in unibyte buffers. */)
135 (Lisp_Object start
, Lisp_Object end
)
137 ptrdiff_t istart
, iend
, pos_byte
;
140 struct decompress_unwind_data unwind_data
;
141 ptrdiff_t count
= SPECPDL_INDEX ();
143 validate_region (&start
, &end
);
145 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
146 error ("This function can be called only in unibyte buffers");
149 if (!zlib_initialized
)
150 zlib_initialized
= init_zlib_functions ();
151 if (!zlib_initialized
)
155 /* This is a unibyte buffer, so character positions and bytes are
157 istart
= XINT (start
);
159 move_gap_both (iend
, iend
);
161 stream
.zalloc
= Z_NULL
;
162 stream
.zfree
= Z_NULL
;
163 stream
.opaque
= Z_NULL
;
165 stream
.next_in
= Z_NULL
;
167 /* The magic number 32 apparently means "autodetect both the gzip and
168 zlib formats" according to zlib.h. */
169 if (fn_inflateInit2 (&stream
, MAX_WBITS
+ 32) != Z_OK
)
172 unwind_data
.start
= iend
;
173 unwind_data
.stream
= &stream
;
174 unwind_data
.old_point
= PT
;
176 record_unwind_protect_ptr (unwind_decompress
, &unwind_data
);
178 /* Insert the decompressed data at the end of the compressed data. */
183 /* Keep calling 'inflate' until it reports an error or end-of-input. */
186 /* Maximum number of bytes that one 'inflate' call should read and write.
187 Do not make avail_out too large, as that might unduly delay C-g.
188 zlib requires that avail_in and avail_out not exceed UINT_MAX. */
189 ptrdiff_t avail_in
= min (iend
- pos_byte
, UINT_MAX
);
190 int avail_out
= 16 * 1024;
193 if (GAP_SIZE
< avail_out
)
194 make_gap (avail_out
- GAP_SIZE
);
195 stream
.next_in
= BYTE_POS_ADDR (pos_byte
);
196 stream
.avail_in
= avail_in
;
197 stream
.next_out
= GPT_ADDR
;
198 stream
.avail_out
= avail_out
;
199 inflate_status
= fn_inflate (&stream
, Z_NO_FLUSH
);
200 pos_byte
+= avail_in
- stream
.avail_in
;
201 decompressed
= avail_out
- stream
.avail_out
;
202 insert_from_gap (decompressed
, decompressed
, 0);
205 while (inflate_status
== Z_OK
);
207 if (inflate_status
!= Z_STREAM_END
)
208 return unbind_to (count
, Qnil
);
210 unwind_data
.start
= 0;
212 /* Delete the compressed data. */
213 del_range (istart
, iend
);
215 return unbind_to (count
, Qt
);
219 /***********************************************************************
221 ***********************************************************************/
223 syms_of_decompress (void)
225 DEFSYM (Qzlib_dll
, "zlib");
226 defsubr (&Szlib_decompress_region
);
227 defsubr (&Szlib_available_p
);
230 #endif /* HAVE_ZLIB */