Skip to content

Commit b36913d

Browse files
committed
Allow partial decompression (Bug#33133)
* src/decompress.c (Fzlib_decompress_region): Add optional ALLOW-PARTIAL parameter. * lisp/url/url-http.el (url-handle-content-transfer-encoding): Use it. * doc/lispref/text.texi (Decompression): Document it. * etc/NEWS: Announce it.
1 parent 2bd3e48 commit b36913d

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

doc/lispref/text.texi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,14 +4513,17 @@ This function returns non-@code{nil} if built-in zlib decompression is
45134513
available.
45144514
@end defun
45154515

4516-
@defun zlib-decompress-region start end
4516+
@defun zlib-decompress-region start end &optional allow-partial
45174517
This function decompresses the region between @var{start} and
45184518
@var{end}, using built-in zlib decompression. The region should
45194519
contain data that were compressed with gzip or zlib. On success, the
45204520
function replaces the contents of the region with the decompressed
4521-
data. On failure, the function leaves the region unchanged and
4522-
returns @code{nil}. This function can be called only in unibyte
4523-
buffers.
4521+
data. If @var{allow-partial} is @code{nil} or omitted, then on
4522+
failure, the function leaves the region unchanged and returns
4523+
@code{nil}. Otherwise, it returns the number of bytes that were not
4524+
decompressed and replaces the region text by whatever data was
4525+
successfully decompressed. This function can be called only in
4526+
unibyte buffers.
45244527
@end defun
45254528

45264529

etc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,12 @@ are implemented in C using the Jansson library.
16721672
and 'flatten' it such that the result is a list of all the terminal
16731673
nodes.
16741674

1675+
+++
1676+
** 'zlib-decompress-region' can partially decompress corrupted data.
1677+
If the new optional ALLOW-PARTIAL argument is passed, then the data
1678+
that was decompressed successfully before failing will be inserted
1679+
into the buffer.
1680+
16751681
** Mailcap
16761682

16771683
---

lisp/url/url-http.el

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ should be shown to the user."
939939
(goto-char (point-min))
940940
success))
941941

942-
(declare-function zlib-decompress-region "decompress.c" (start end))
942+
(declare-function zlib-decompress-region "decompress.c"
943+
(start end &optional allow-partial))
943944

944945
(defun url-handle-content-transfer-encoding ()
945946
(let ((encoding (mail-fetch-field "content-encoding")))
@@ -951,7 +952,7 @@ should be shown to the user."
951952
(widen)
952953
(goto-char (point-min))
953954
(when (search-forward "\n\n")
954-
(zlib-decompress-region (point) (point-max)))))))
955+
(zlib-decompress-region (point) (point-max) t))))))
955956

956957
;; Miscellaneous
957958
(defun url-http-activate-callback ()

src/decompress.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ DEFUN ("zlib-available-p", Fzlib_available_p, Szlib_available_p, 0, 0, 0,
120120

121121
DEFUN ("zlib-decompress-region", Fzlib_decompress_region,
122122
Szlib_decompress_region,
123-
2, 2, 0,
123+
2, 3, 0,
124124
doc: /* Decompress a gzip- or zlib-compressed region.
125125
Replace the text in the region by the decompressed data.
126-
On failure, return nil and leave the data in place.
126+
127+
If optional parameter ALLOW-PARTIAL is nil or omitted, then on
128+
failure, return nil and leave the data in place. Otherwise, return
129+
the number of bytes that were not decompressed and replace the region
130+
text by whatever data was successfully decompressed (similar to gzip).
131+
If decompression is completely successful return t.
132+
127133
This function can be called only in unibyte buffers. */)
128-
(Lisp_Object start, Lisp_Object end)
134+
(Lisp_Object start, Lisp_Object end, Lisp_Object allow_partial)
129135
{
130136
ptrdiff_t istart, iend, pos_byte;
131137
z_stream stream;
@@ -206,8 +212,14 @@ This function can be called only in unibyte buffers. */)
206212
}
207213
while (inflate_status == Z_OK);
208214

215+
Lisp_Object ret = Qt;
209216
if (inflate_status != Z_STREAM_END)
210-
return unbind_to (count, Qnil);
217+
{
218+
if (!NILP (allow_partial))
219+
ret = make_int (iend - pos_byte);
220+
else
221+
return unbind_to (count, Qnil);
222+
}
211223

212224
unwind_data.start = 0;
213225

@@ -218,7 +230,7 @@ This function can be called only in unibyte buffers. */)
218230
signal_after_change (istart, iend - istart, unwind_data.nbytes);
219231
update_compositions (istart, istart, CHECK_HEAD);
220232

221-
return unbind_to (count, Qt);
233+
return unbind_to (count, ret);
222234
}
223235

224236

0 commit comments

Comments
 (0)