unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Lars Magne Ingebrigtsen <larsi@gnus.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: emacs-devel@gnu.org
Subject: Re: Mysterious gzipped images
Date: Thu, 08 Aug 2013 12:35:49 +0200	[thread overview]
Message-ID: <m3eha44ghm.fsf@stories.gnus.org> (raw)
In-Reply-To: <jwvfvul7esl.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Wed, 07 Aug 2013 10:35:54 -0400")

[-- Attachment #1: Type: text/plain, Size: 678 bytes --]

Stefan Monnier <monnier@iro.umontreal.ca> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: decompress.c --]
[-- Type: text/x-csrc, Size: 3198 bytes --]

/* 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"

\f
#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;
}

\f
/***********************************************************************
			    Initialization
 ***********************************************************************/
void
syms_of_decompress (void)
{
  defsubr (&Sdecompress_gzipped_region);
}

#endif /* HAVE_PNG */

[-- Attachment #3: Type: text/plain, Size: 191 bytes --]


-- 
(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

  parent reply	other threads:[~2013-08-08 10:35 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-06 22:20 Mysterious gzipped images Lars Magne Ingebrigtsen
2013-08-06 22:30 ` Lars Magne Ingebrigtsen
2013-08-06 23:05   ` Andreas Schwab
2013-08-06 23:38     ` Lars Magne Ingebrigtsen
2013-08-07  1:08       ` Lars Magne Ingebrigtsen
2013-08-07  8:51         ` Julien Danjou
2013-08-07 14:35         ` Stefan Monnier
2013-08-07 16:47           ` chad
2013-08-08 10:35           ` Lars Magne Ingebrigtsen [this message]
2013-08-08 10:57             ` Andreas Schwab
2013-08-11 19:44               ` Lars Magne Ingebrigtsen
2013-08-08 13:22             ` Paul Eggert
2013-08-08 14:06               ` Lars Magne Ingebrigtsen
2013-08-08 15:31                 ` Lars Magne Ingebrigtsen
2013-08-11 22:05                   ` Lars Magne Ingebrigtsen
2013-08-08 13:24             ` Romain Francoise
2013-08-08 13:28               ` Lars Magne Ingebrigtsen
2013-08-08 13:36                 ` Christopher Schmidt
2013-08-08 13:55                   ` Lars Magne Ingebrigtsen
2013-08-08 14:06                     ` Christopher Schmidt
2013-08-08 14:09                       ` Lars Magne Ingebrigtsen
2013-08-08 15:43                 ` Romain Francoise
2013-08-08 15:52                   ` Lars Magne Ingebrigtsen
2013-08-11 19:51                   ` Lars Magne Ingebrigtsen
2013-08-08 14:47             ` Stefan Monnier
2013-08-11 19:47               ` Lars Magne Ingebrigtsen
2013-08-12  0:54                 ` Stefan Monnier
2013-08-12 14:05                   ` Lars Magne Ingebrigtsen
2013-08-12 15:09                     ` Stefan Monnier
2013-08-12 15:20                     ` Eli Zaretskii
2013-08-12 16:27                       ` Lars Magne Ingebrigtsen
2013-08-12 16:42                         ` Eli Zaretskii
2013-08-12 12:13                 ` Eli Zaretskii
2013-08-12 12:21                   ` Eli Zaretskii
2013-08-12 13:06                     ` Lars Magne Ingebrigtsen
2013-08-12 13:29                       ` Eli Zaretskii
2013-08-13 23:40                       ` Lars Magne Ingebrigtsen
2013-08-14 13:09                         ` Lars Magne Ingebrigtsen
2013-08-14 15:12                           ` Eli Zaretskii
2013-08-17 15:01                             ` Lars Magne Ingebrigtsen
2013-08-17 15:27                               ` Eli Zaretskii
2013-08-17 17:04                                 ` Eli Zaretskii
2013-08-18 17:06                                   ` Lars Magne Ingebrigtsen
2013-08-07 18:25         ` Rüdiger Sonderfeld
2013-08-07 21:06           ` FFI (was: Mysterious gzipped images) Stefan Monnier
2013-08-08  0:50             ` Stephen J. Turnbull

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3eha44ghm.fsf@stories.gnus.org \
    --to=larsi@gnus.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).