1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-03-05 05:21:26 -08:00
emacs/src/decompress.c
Paul Eggert 63cfb75f3a Include-file cleanup for src directory
Omit ‘#include "foo.h"’ unless the file needs foo.h (Bug#21707).
In a few cases, add ‘#include "foo.h"’ if the file needs foo.h
but does not include it directly.  As a general rule, a source
file should include foo.h if it needs the interfaces that foo.h
defines.
* src/alloc.c: Don’t include process.h.  Include dispextern.h,
systime.h.
* src/atimer.c: Don’t include blockinput.h.
* src/buffer.c: Include coding.h, systime.h.  Don’t include
keyboard.h, coding.h.
* src/callint.c: Don’t include commands.h, keymap.h.
* src/callproc.c: Don’t include character.h, ccl.h, composite.h,
systty.h, termhooks.h.
* src/casetab.c: Don’t include character.h.
* src/category.c: Don’t include charset.h, keymap.h.
* src/ccl.h: Don’t include character.h.
* src/character.c: Don’t include charset.h.
* src/charset.c: Don’t include disptab.h.
* src/chartab.c: Don’t include ccl.h.
* src/cm.c: Don’t include frame.h, termhooks.h.
* src/cmds.c: Don’t include window.h, dispextern.h.
* src/coding.c: Don’t include window.h, frame.h.
* src/composite.c: Include composite.h.  Don’t include window.h,
font.h.
* src/data.c: Don’t include syssignal.h, termhooks.h, font.h.
* src/dbusbind.c: Don’t include frame.h.
* src/decompress.c: Don’t include character.h.
* src/dired.c: Don’t include character.h, commands.h, charset.h.
* src/dispnew.c: Don’t include character.h, indent.h, intervals.h,
process.h, timespec.h.  Include systime.h.
* src/doc.c: Include coding.h.  Don’t include keyboard.h.
* src/editfns.c: Include composite.h.  Don’t include frame.h.
* src/emacs.c: Include fcntl.h, coding.h.  Don’t include
commands.h, systty.h..
* src/fileio.c: Don’t include intervals.h, dispextern.h.
Include composite.h.
* src/filelock.c: Don’t include character.h, systime.h.
* src/fns.c: Don’t include time.h, commands.h, keyboard.h,
keymap.h, frame.h, blockinput.h, xterm.h.  Include composite.h.
* src/font.c: Include termhooks.h.
* src/font.h: Don’t include ccl.h, frame.h.  Add forward decls of
struct composition_it, struct face, struct glyph_string.
* src/fontset.c: Don’t include buffer.h, ccl.h, keyboard.h,
intervals.h, window.h, termhooks.h.
* src/frame.c: Don’t include character.h, commands.h, font.h.
* src/frame.h: Don’t include dispextern.h.
* src/fringe.c: Don’t include character.h.
* src/ftcrfont.c: Don’t include dispextern.h, frame.h,
character.h, charset.h, fontset.h.
* src/ftfont.c: Don’t include frame.h, blockinput.h, coding.h,
fontset.h.
* src/ftxfont.c: Don’t include dispextern.h, character.h,
charset.h, fontset.h.
* src/gfilenotify.c: Don’t include frame.h, process.h.
* src/gtkutil.c: Include dispextern.h, frame.h, systime.h.
Don’t include syssignal.h, buffer.h, charset.h, font.h.
* src/gtkutil.h: Don’t include frame.h.
* src/image.c: Include fcntl.h and stdio.h instead of sysstdio.h.
Don’t include character.h.
* src/indent.c: Don’t include keyboard.h, termchar.h.
* src/inotify.c: Don’t include character.h, frame.h.
* src/insdel.c: Include composite.h.  Don’t include blockinput.h.
* src/intervals.c: Don’t include character.h, keyboard.h.
* src/intervals.h: Don’t include dispextern.h, composite.h.
* src/keyboard.c: Don’t include sysstdio.h, disptab.h, puresize.h.
Include coding.h.
* src/keyboard.h: Don’t incldue systime.h.
* src/keymap.c: Don’t include charset.h, frame.h.
* src/lread.c: Include dispextern.h and systime.h.
Don’t include frame.h.  Include systime.h.
* src/macros.c: Don’t include commands.h, character.h, buffer.h.
* src/menu.c: Include character.h, coding.h.  Don’t include
dispextern.h.
* src/menu.h: Don’t include systime.h.
* src/minibuf.c: Don’t include commands.h, dispextern.h, syntax.h,
intervals.h, termhooks.h.
* src/print.c: Include coding.h.  Don’t include keyboard.h,
window.h, dispextern.h, termchar.h, termhooks.h, font.h.
Add forward decl of struct terminal.
* src/process.c: Don’t include termhooks.h, commands.h,
dispextern.h, composite.h.
* src/region-cache.c: Don’t include character.h.
* src/scroll.c: Don’t include keyboard.h, window.h.
* src/search.c: Don’t include category.h, commands.h.
* src/sound.c: Don’t include dispextern.h.
* src/syntax.c: Don’t include command.h, keymap.h.
* src/sysdep.c: Don’t include window.h, dispextern.h.
* src/systime.h: Use ‘#ifdef emacs’, not ‘#ifdef EMACS_LISP_H’,
* src/term.c: Don’t include systty.h, intervals.h, xterm.h.
* src/terminal.c: Include character.h.
Don’t include charset.h, coding.h.
* src/textprop.c: Don’t include character.h.
* src/undo.c: Don’t include character.h, commands.h, window.h.
* src/unexsol.c: Don’t include character.h, charset.h.
* src/widget.c: Include widget.h.  Don’t include keyboard.h,
window.h, dispextern.h, blockinput.h, character.h, font.h.
* src/widgetprv.h: Don’t include widget.h.
* src/window.c: Don’t include character.h, menu.h, intervals.h.
* src/xdisp.c: Include composite.h, systime.h.  Don’t include
macros.h, process.h.
* src/xfaces.c: Don’t include charset.h, keyboard.h, termhooks.h,
intervals.h.
* src/xfns.c: Don’t include menu.h, character.h, intervals.h,
epaths.h, fontset.h, systime.h, atimer.h, termchar.h.
* src/xfont.c: Don’t include dispextern.h, fontset.h, ccl.h.
* src/xftfont.c: Don’t include dispextern.h, character.h, fontset.h.
* src/xgselect.c: Don’t include timespec.h, frame.h.
Include systime.h.
* src/xgselect.h: Don’t include time.h.
Use a forward decl to struct timespec instead.
* src/xmenu.c: Don’t include keymap.h, character.h, charset.h,
dispextern.h.  Include systime.h.
* src/xml.c: Don’t include character.h.
* src/xrdb.c [USE_MOTIF]: Don’t include keyboard.h.
* src/xselect.c: Don’t include dispextern.h, character.h,
buffer.h, process.h.
* src/xsmfns.c: Don’t include systime.h, sysselect.h.
* src/xterm.c: Don’t include syssignal.h, charset.h, disptab.h,
intervals.h process.h, keymap.h, xgselect.h.  Include composite.h.
2015-10-20 18:35:53 -07:00

215 lines
5.7 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Interface to zlib.
Copyright (C) 2013-2015 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#include "lisp.h"
#include "buffer.h"
#include <verify.h>
#ifdef WINDOWSNT
# include <windows.h>
# include "w32.h"
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;
static bool
init_zlib_functions (void)
{
HMODULE library = w32_delayed_load (Qzlib_dll);
if (!library)
return false;
LOAD_DLL_FN (library, inflateInit2_);
LOAD_DLL_FN (library, inflate);
LOAD_DLL_FN (library, inflateEnd);
return true;
}
# undef inflate
# undef inflateEnd
# undef inflateInit2_
# define inflate fn_inflate
# define inflateEnd fn_inflateEnd
# define inflateInit2_ fn_inflateInit2_
#endif /* WINDOWSNT */
struct decompress_unwind_data
{
ptrdiff_t old_point, start, nbytes;
z_stream *stream;
};
static void
unwind_decompress (void *ddata)
{
struct decompress_unwind_data *data = ddata;
inflateEnd (data->stream);
/* Delete any uncompressed data already inserted on error. */
if (data->start)
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
point-max. */
SET_PT (min (data->old_point, ZV));
}
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;
zlib_initialized = init_zlib_functions ();
status = zlib_initialized ? Qt : Qnil;
Vlibrary_cache = Fcons (Fcons (Qzlib_dll, status), Vlibrary_cache);
return status;
}
#else
return Qt;
#endif
}
DEFUN ("zlib-decompress-region", Fzlib_decompress_region,
Szlib_decompress_region,
2, 2, 0,
doc: /* Decompress a gzip- or zlib-compressed region.
Replace the text in the region by the decompressed data.
On failure, return nil and leave the data in place.
This function can be called only in unibyte buffers. */)
(Lisp_Object start, Lisp_Object end)
{
ptrdiff_t istart, iend, pos_byte;
z_stream stream;
int inflate_status;
struct decompress_unwind_data unwind_data;
ptrdiff_t count = SPECPDL_INDEX ();
validate_region (&start, &end);
if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
error ("This function can be called only in unibyte buffers");
#ifdef WINDOWSNT
if (!zlib_initialized)
zlib_initialized = init_zlib_functions ();
if (!zlib_initialized)
{
message1 ("zlib library not found");
return Qnil;
}
#endif
/* This is a unibyte buffer, so character positions and bytes are
the same. */
istart = XINT (start);
iend = XINT (end);
move_gap_both (iend, iend);
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = 0;
stream.next_in = Z_NULL;
/* The magic number 32 apparently means "autodetect both the gzip and
zlib formats" according to zlib.h. */
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. */
SET_PT (iend);
pos_byte = istart;
/* Keep calling 'inflate' until it reports an error or end-of-input. */
do
{
/* Maximum number of bytes that one 'inflate' call should read and write.
Do not make avail_out too large, as that might unduly delay C-g.
zlib requires that avail_in and avail_out not exceed UINT_MAX. */
ptrdiff_t avail_in = min (iend - pos_byte, UINT_MAX);
int avail_out = 16 * 1024;
int decompressed;
if (GAP_SIZE < avail_out)
make_gap (avail_out - GAP_SIZE);
stream.next_in = BYTE_POS_ADDR (pos_byte);
stream.avail_in = avail_in;
stream.next_out = GPT_ADDR;
stream.avail_out = avail_out;
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);
if (inflate_status != Z_STREAM_END)
return unbind_to (count, Qnil);
unwind_data.start = 0;
/* Delete the compressed data. */
del_range (istart, iend);
return unbind_to (count, Qt);
}
/***********************************************************************
Initialization
***********************************************************************/
void
syms_of_decompress (void)
{
defsubr (&Szlib_decompress_region);
defsubr (&Szlib_available_p);
}
#endif /* HAVE_ZLIB */