emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mysterious gzipped images


From: Lars Magne Ingebrigtsen
Subject: Re: Mysterious gzipped images
Date: Thu, 08 Aug 2013 12:35:49 +0200
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

> So curl doesn't implement Accept/Content-Encoding either?  That's weird!

It does, but you have to say "curl --compressed".  That makes curl both
say that it accept gzipped content and decompresses it if it gets it.

Although it's "wrong" for the server to output compressed data if the
client doesn't say that it accepts it, I think it's pretty buggy not to
decode the compressed data, anyway.

> Sounds good, tho I'd prefer to see the patch first.

I've included the new .c file below.  The attendant code change to
lisp.h/emacs.c is what you'd expect.

Now I just have to write the configure.ac code to define HAVE_ZLIB.

/* 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 <http://www.gnu.org/licenses/>.  */

#include <config.h>

#ifdef HAVE_PNG

#include <zlib.h>

#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 */
-- 
(domestic pets only, the antidote for overdose, milk.)
  No Gnus T-Shirt for sale: http://ingebrigtsen.no/no.php
  and http://lars.ingebrigtsen.no/2013/08/twenty-years-of-september.html

reply via email to

[Prev in Thread] Current Thread [Next in Thread]