/* Interface to zlib. Copyright (C) 2013 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 . */ #include #ifdef HAVE_PNG #include #include "lisp.h" #include "character.h" #include "buffer.h" #define BUFFER_SIZE 16384 DEFUN ("decompress-gzipped-region", Fdecompress_gzipped_region, Sdecompress_gzipped_region, 2, 2, 0, doc: /* Decompress a gzip-compressed region. The text in the region will be replaced by the decompressed data. On failure, nil is returned and the data is left in place. This function can only be called in unibyte buffers.*/) (Lisp_Object start, Lisp_Object end) { ptrdiff_t istart, iend, point = PT; z_stream stream; int decompressed; unsigned char *out; validate_region (&start, &end); move_gap_both (iend, iend); if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) error ("This function can only be called in unibyte buffers"); /* This is a unibyte buffer, so character positions and bytes are the same. */ istart = XINT (start); iend = XINT (end); stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; stream.avail_in = 0; stream.next_in = Z_NULL; /* This magic number apparently means "this is gzip". */ if (inflateInit2 (&stream, 16 + MAX_WBITS) != Z_OK) return Qnil; out = (char *) malloc (BUFFER_SIZE); /* We're inserting the decompressed data at the end of the compressed data. */ SET_PT (iend); stream.avail_in = iend - istart; stream.next_in = (char *) BYTE_POS_ADDR (istart); /* Run inflate() on input until the output buffer isn't full. */ do { stream.avail_out = BUFFER_SIZE; stream.next_out = out; switch (inflate (&stream, Z_NO_FLUSH)) { case Z_STREAM_ERROR: case Z_NEED_DICT: case Z_DATA_ERROR: case Z_MEM_ERROR: inflateEnd (&stream); /* Delete any uncompressed data already inserted and restore point. */ del_range (iend, PT); SET_PT (point); free (out); return Qnil; } decompressed = BUFFER_SIZE - stream.avail_out; insert_1_both (out, decompressed, decompressed, 0, 0, 0); } while (stream.avail_out == 0); inflateEnd (&stream); free (out); /* Delete the compressed data. */ del_range (istart, iend); return Qt; } /*********************************************************************** Initialization ***********************************************************************/ void syms_of_decompress (void) { defsubr (&Sdecompress_gzipped_region); } #endif /* HAVE_PNG */