Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 1 | /* Decompression support for libdwfl: zlib (gzip) and/or bzlib (bzip2). |
| 2 | Copyright (C) 2009 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 8 | * the GNU Lesser General Public License as published by the Free |
| 9 | Software Foundation; either version 3 of the License, or (at |
| 10 | your option) any later version |
| 11 | |
| 12 | or |
| 13 | |
| 14 | * the GNU General Public License as published by the Free |
| 15 | Software Foundation; either version 2 of the License, or (at |
| 16 | your option) any later version |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 25 | You should have received copies of the GNU General Public License and |
| 26 | the GNU Lesser General Public License along with this program. If |
| 27 | not, see <http://www.gnu.org/licenses/>. */ |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 28 | |
Ulf Hermann | 575198c | 2017-04-20 16:31:02 +0200 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 33 | #include "libdwflP.h" |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 34 | #include "system.h" |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 35 | |
| 36 | #include <unistd.h> |
| 37 | |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 38 | #ifdef LZMA |
| 39 | # define USE_INFLATE 1 |
| 40 | # include <lzma.h> |
| 41 | # define unzip __libdw_unlzma |
| 42 | # define DWFL_E_ZLIB DWFL_E_LZMA |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 43 | # define MAGIC "\xFD" "7zXZ\0" /* XZ file format. */ |
| 44 | # define MAGIC2 "\x5d\0" /* Raw LZMA format. */ |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 45 | # define Z(what) LZMA_##what |
| 46 | # define LZMA_ERRNO LZMA_PROG_ERROR |
| 47 | # define z_stream lzma_stream |
| 48 | # define inflateInit(z) lzma_auto_decoder (z, 1 << 30, 0) |
| 49 | # define do_inflate(z) lzma_code (z, LZMA_RUN) |
| 50 | # define inflateEnd(z) lzma_end (z) |
Mark Wielaard | 52536d7 | 2020-09-18 12:49:29 +0200 | [diff] [blame] | 51 | #elif defined ZSTD |
| 52 | # define USE_INFLATE 1 |
| 53 | # include <zstd.h> |
| 54 | # define unzip __libdw_unzstd |
| 55 | # define DWFL_E_ZLIB DWFL_E_ZSTD |
| 56 | # define MAGIC "\x28\xb5\x2f\xfd" |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 57 | #elif defined BZLIB |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 58 | # define USE_INFLATE 1 |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 59 | # include <bzlib.h> |
| 60 | # define unzip __libdw_bunzip2 |
| 61 | # define DWFL_E_ZLIB DWFL_E_BZLIB |
| 62 | # define MAGIC "BZh" |
| 63 | # define Z(what) BZ_##what |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 64 | # define BZ_ERRNO BZ_IO_ERROR |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 65 | # define z_stream bz_stream |
| 66 | # define inflateInit(z) BZ2_bzDecompressInit (z, 0, 0) |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 67 | # define do_inflate(z) BZ2_bzDecompress (z) |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 68 | # define inflateEnd(z) BZ2_bzDecompressEnd (z) |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 69 | #else |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 70 | # define USE_INFLATE 0 |
| 71 | # define crc32 loser_crc32 |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 72 | # include <zlib.h> |
| 73 | # define unzip __libdw_gunzip |
| 74 | # define MAGIC "\037\213" |
| 75 | # define Z(what) Z_##what |
Roland McGrath | d6ccdc1 | 2009-08-26 00:23:01 -0700 | [diff] [blame] | 76 | #endif |
Roland McGrath | c4ac861 | 2009-01-14 03:01:49 -0800 | [diff] [blame] | 77 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 78 | #define READ_SIZE (1 << 20) |
| 79 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 80 | struct unzip_state { |
| 81 | #if !USE_INFLATE |
| 82 | gzFile zf; |
| 83 | #endif |
| 84 | size_t mapped_size; |
| 85 | void **whole; |
| 86 | void *buffer; |
| 87 | size_t size; |
| 88 | void *input_buffer; |
| 89 | off_t input_pos; |
| 90 | }; |
| 91 | |
| 92 | static inline bool |
| 93 | bigger_buffer (struct unzip_state *state, size_t start) |
| 94 | { |
| 95 | size_t more = state->size ? state->size * 2 : start; |
| 96 | char *b = realloc (state->buffer, more); |
| 97 | while (unlikely (b == NULL) && more >= state->size + 1024) |
| 98 | b = realloc (state->buffer, more -= 1024); |
| 99 | if (unlikely (b == NULL)) |
| 100 | return false; |
| 101 | state->buffer = b; |
| 102 | state->size = more; |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static inline void |
| 107 | smaller_buffer (struct unzip_state *state, size_t end) |
| 108 | { |
| 109 | state->buffer = |
| 110 | realloc (state->buffer, end) ?: end == 0 ? NULL : state->buffer; |
| 111 | state->size = end; |
| 112 | } |
| 113 | |
| 114 | static inline Dwfl_Error |
| 115 | fail (struct unzip_state *state, Dwfl_Error failure) |
| 116 | { |
| 117 | if (state->input_pos == (off_t) state->mapped_size) |
| 118 | *state->whole = state->input_buffer; |
| 119 | else |
| 120 | { |
| 121 | free (state->input_buffer); |
| 122 | *state->whole = NULL; |
| 123 | } |
| 124 | free (state->buffer); |
| 125 | return failure; |
| 126 | } |
| 127 | |
Mark Wielaard | 52536d7 | 2020-09-18 12:49:29 +0200 | [diff] [blame] | 128 | #ifndef ZSTD |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 129 | static inline Dwfl_Error |
| 130 | zlib_fail (struct unzip_state *state, int result) |
| 131 | { |
| 132 | switch (result) |
| 133 | { |
| 134 | case Z (MEM_ERROR): |
| 135 | return fail (state, DWFL_E_NOMEM); |
| 136 | case Z (ERRNO): |
| 137 | return fail (state, DWFL_E_ERRNO); |
| 138 | default: |
| 139 | return fail (state, DWFL_E_ZLIB); |
| 140 | } |
| 141 | } |
Mark Wielaard | 52536d7 | 2020-09-18 12:49:29 +0200 | [diff] [blame] | 142 | #endif |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 143 | |
| 144 | #if !USE_INFLATE |
| 145 | static Dwfl_Error |
| 146 | open_stream (int fd, off_t start_offset, struct unzip_state *state) |
| 147 | { |
| 148 | int d = dup (fd); |
| 149 | if (unlikely (d < 0)) |
Mark Wielaard | 7ea8224 | 2019-08-12 00:43:22 +0200 | [diff] [blame] | 150 | return DWFL_E_ERRNO; |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 151 | if (start_offset != 0) |
| 152 | { |
| 153 | off_t off = lseek (d, start_offset, SEEK_SET); |
| 154 | if (off != start_offset) |
| 155 | { |
| 156 | close (d); |
Mark Wielaard | 7ea8224 | 2019-08-12 00:43:22 +0200 | [diff] [blame] | 157 | return DWFL_E_ERRNO; |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | state->zf = gzdopen (d, "r"); |
| 161 | if (unlikely (state->zf == NULL)) |
| 162 | { |
| 163 | close (d); |
Mark Wielaard | a894c63 | 2020-04-26 02:10:41 +0200 | [diff] [blame] | 164 | return DWFL_E_NOMEM; |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* From here on, zlib will close D. */ |
| 168 | |
| 169 | return DWFL_E_NOERROR; |
| 170 | } |
| 171 | #endif |
| 172 | |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 173 | /* If this is not a compressed image, return DWFL_E_BADELF. |
| 174 | If we uncompressed it into *WHOLE, *WHOLE_SIZE, return DWFL_E_NOERROR. |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 175 | Otherwise return an error for bad compressed data or I/O failure. |
| 176 | If we return an error after reading the first part of the file, |
| 177 | leave that portion malloc'd in *WHOLE, *WHOLE_SIZE. If *WHOLE |
| 178 | is not null on entry, we'll use it in lieu of repeating a read. */ |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 179 | |
| 180 | Dwfl_Error internal_function |
Josh Stone | 3425454 | 2015-10-09 10:10:37 -0700 | [diff] [blame] | 181 | unzip (int fd, off_t start_offset, |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 182 | void *mapped, size_t _mapped_size, |
| 183 | void **_whole, size_t *whole_size) |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 184 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 185 | struct unzip_state state = |
| 186 | { |
| 187 | #if !USE_INFLATE |
| 188 | .zf = NULL, |
| 189 | #endif |
| 190 | .mapped_size = _mapped_size, |
| 191 | .whole = _whole, |
| 192 | .buffer = NULL, |
| 193 | .size = 0, |
| 194 | .input_buffer = NULL, |
| 195 | .input_pos = 0 |
| 196 | }; |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 197 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 198 | if (mapped == NULL) |
| 199 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 200 | if (*state.whole == NULL) |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 201 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 202 | state.input_buffer = malloc (READ_SIZE); |
| 203 | if (unlikely (state.input_buffer == NULL)) |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 204 | return DWFL_E_NOMEM; |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 205 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 206 | ssize_t n = pread_retry (fd, state.input_buffer, READ_SIZE, start_offset); |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 207 | if (unlikely (n < 0)) |
Mark Wielaard | 52536d7 | 2020-09-18 12:49:29 +0200 | [diff] [blame] | 208 | return fail (&state, DWFL_E_ERRNO); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 209 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 210 | state.input_pos = n; |
| 211 | mapped = state.input_buffer; |
| 212 | state.mapped_size = n; |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 213 | } |
| 214 | else |
| 215 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 216 | state.input_buffer = *state.whole; |
| 217 | state.input_pos = state.mapped_size = *whole_size; |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 218 | } |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 221 | #define NOMAGIC(magic) \ |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 222 | (state.mapped_size <= sizeof magic || \ |
| 223 | memcmp (mapped, magic, sizeof magic - 1)) |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 224 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 225 | /* First, look at the header. */ |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 226 | if (NOMAGIC (MAGIC) |
| 227 | #ifdef MAGIC2 |
| 228 | && NOMAGIC (MAGIC2) |
| 229 | #endif |
| 230 | ) |
Roland McGrath | a6ef1dc | 2009-01-06 00:54:49 -0800 | [diff] [blame] | 231 | /* Not a compressed file. */ |
| 232 | return DWFL_E_BADELF; |
| 233 | |
Mark Wielaard | 52536d7 | 2020-09-18 12:49:29 +0200 | [diff] [blame] | 234 | #ifdef ZSTD |
| 235 | /* special case for libzstd since it is slightly different from the |
| 236 | API provided by bzlib and liblzma. */ |
| 237 | |
| 238 | void *next_in = mapped; |
| 239 | size_t avail_in = state.mapped_size; |
| 240 | void *next_out = NULL; |
| 241 | size_t avail_out = 0; |
| 242 | size_t total_out = 0; |
| 243 | |
| 244 | size_t result; |
| 245 | ZSTD_DCtx *dctx = ZSTD_createDCtx(); |
| 246 | if (dctx == NULL) |
| 247 | return fail (&state, DWFL_E_NOMEM); |
| 248 | |
| 249 | do |
| 250 | { |
| 251 | if (avail_in == 0 && state.input_buffer != NULL) |
| 252 | { |
| 253 | ssize_t n = pread_retry (fd, state.input_buffer, READ_SIZE, |
| 254 | start_offset + state.input_pos); |
| 255 | if (unlikely (n < 0)) |
| 256 | { |
| 257 | ZSTD_freeDCtx (dctx); |
| 258 | return fail (&state, DWFL_E_ERRNO); |
| 259 | } |
| 260 | next_in = state.input_buffer; |
| 261 | avail_in = n; |
| 262 | state.input_pos += n; |
| 263 | } |
| 264 | if (avail_out == 0) |
| 265 | { |
| 266 | ptrdiff_t pos = (void *) next_out - state.buffer; |
| 267 | if (!bigger_buffer (&state, avail_in)) |
| 268 | { |
| 269 | ZSTD_freeDCtx (dctx); |
| 270 | return fail (&state, DWFL_E_NOMEM); |
| 271 | } |
| 272 | next_out = state.buffer + pos; |
| 273 | avail_out = state.size - pos; |
| 274 | } |
| 275 | |
| 276 | ZSTD_inBuffer input = { next_in, avail_in, 0 }; |
| 277 | ZSTD_outBuffer output = { next_out, avail_out, 0 }; |
| 278 | result = ZSTD_decompressStream (dctx, &output, &input); |
| 279 | |
| 280 | if (! ZSTD_isError (result)) |
| 281 | { |
| 282 | total_out += output.pos; |
| 283 | next_out += output.pos; |
| 284 | avail_out -= output.pos; |
| 285 | next_in += input.pos; |
| 286 | avail_in -= input.pos; |
| 287 | } |
| 288 | |
| 289 | if (result == 0) |
| 290 | break; |
| 291 | } |
| 292 | while (avail_in > 0 && ! ZSTD_isError (result)); |
| 293 | |
| 294 | ZSTD_freeDCtx (dctx); |
| 295 | |
| 296 | if (ZSTD_isError (result)) |
| 297 | return fail (&state, DWFL_E_ZSTD); |
| 298 | |
| 299 | smaller_buffer (&state, total_out); |
| 300 | |
| 301 | #elif USE_INFLATE |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 302 | |
| 303 | /* This style actually only works with bzlib and liblzma. |
| 304 | The stupid zlib interface has nothing to grok the |
| 305 | gzip file headers except the slow gzFile interface. */ |
| 306 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 307 | z_stream z = { .next_in = mapped, .avail_in = state.mapped_size }; |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 308 | int result = inflateInit (&z); |
| 309 | if (result != Z (OK)) |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 310 | { |
| 311 | inflateEnd (&z); |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 312 | return zlib_fail (&state, result); |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 313 | } |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 314 | |
| 315 | do |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 316 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 317 | if (z.avail_in == 0 && state.input_buffer != NULL) |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 318 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 319 | ssize_t n = pread_retry (fd, state.input_buffer, READ_SIZE, |
| 320 | start_offset + state.input_pos); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 321 | if (unlikely (n < 0)) |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 322 | { |
| 323 | inflateEnd (&z); |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 324 | return zlib_fail (&state, Z (ERRNO)); |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 325 | } |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 326 | z.next_in = state.input_buffer; |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 327 | z.avail_in = n; |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 328 | state.input_pos += n; |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 329 | } |
| 330 | if (z.avail_out == 0) |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 331 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 332 | ptrdiff_t pos = (void *) z.next_out - state.buffer; |
| 333 | if (!bigger_buffer (&state, z.avail_in)) |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 334 | { |
| 335 | result = Z (MEM_ERROR); |
| 336 | break; |
| 337 | } |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 338 | z.next_out = state.buffer + pos; |
| 339 | z.avail_out = state.size - pos; |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 340 | } |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 341 | } |
Roland McGrath | 2416964 | 2009-08-26 02:26:34 -0700 | [diff] [blame] | 342 | while ((result = do_inflate (&z)) == Z (OK)); |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 343 | |
| 344 | #ifdef BZLIB |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 345 | uint64_t total_out = (((uint64_t) z.total_out_hi32 << 32) |
| 346 | | z.total_out_lo32); |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 347 | smaller_buffer (&state, total_out); |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 348 | #else |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 349 | smaller_buffer (&state, z.total_out); |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 350 | #endif |
| 351 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 352 | inflateEnd (&z); |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 353 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 354 | if (result != Z (STREAM_END)) |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 355 | return zlib_fail (&state, result); |
Roland McGrath | a6ef1dc | 2009-01-06 00:54:49 -0800 | [diff] [blame] | 356 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 357 | #else /* gzip only. */ |
| 358 | |
| 359 | /* Let the decompression library read the file directly. */ |
| 360 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 361 | Dwfl_Error result = open_stream (fd, start_offset, &state); |
Roland McGrath | c4ac861 | 2009-01-14 03:01:49 -0800 | [diff] [blame] | 362 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 363 | if (result == DWFL_E_NOERROR && gzdirect (state.zf)) |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 364 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 365 | gzclose (state.zf); |
Mark Wielaard | 7ea8224 | 2019-08-12 00:43:22 +0200 | [diff] [blame] | 366 | /* Not a compressed stream after all. */ |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 367 | return fail (&state, DWFL_E_BADELF); |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 370 | if (result != DWFL_E_NOERROR) |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 371 | return fail (&state, result); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 372 | |
| 373 | ptrdiff_t pos = 0; |
| 374 | while (1) |
| 375 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 376 | if (!bigger_buffer (&state, 1024)) |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 377 | { |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 378 | gzclose (state.zf); |
| 379 | return zlib_fail (&state, Z (MEM_ERROR)); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 380 | } |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 381 | int n = gzread (state.zf, state.buffer + pos, state.size - pos); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 382 | if (n < 0) |
| 383 | { |
| 384 | int code; |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 385 | gzerror (state.zf, &code); |
| 386 | gzclose (state.zf); |
| 387 | return zlib_fail (&state, code); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 388 | } |
| 389 | if (n == 0) |
| 390 | break; |
| 391 | pos += n; |
| 392 | } |
| 393 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 394 | gzclose (state.zf); |
| 395 | smaller_buffer (&state, pos); |
Roland McGrath | ae1d7dc | 2009-08-26 01:27:59 -0700 | [diff] [blame] | 396 | #endif |
| 397 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 398 | free (state.input_buffer); |
Roland McGrath | 6bb9071 | 2009-08-27 12:36:47 -0700 | [diff] [blame] | 399 | |
Chih-Hung Hsieh | 6bbea3d | 2015-11-13 11:48:07 -0800 | [diff] [blame] | 400 | *state.whole = state.buffer; |
| 401 | *whole_size = state.size; |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 402 | |
Roland McGrath | a6ef1dc | 2009-01-06 00:54:49 -0800 | [diff] [blame] | 403 | return DWFL_E_NOERROR; |
Roland McGrath | bca4315 | 2009-01-05 23:59:32 -0800 | [diff] [blame] | 404 | } |