| 1 | /* |
| 2 | * librd - Rapid Development C library |
| 3 | * |
| 4 | * Copyright (c) 2012, Magnus Edenhill |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "rd.h" |
| 30 | #include "rdgz.h" |
| 31 | |
| 32 | #include <zlib.h> |
| 33 | |
| 34 | |
| 35 | #define RD_GZ_CHUNK 262144 |
| 36 | |
| 37 | void *rd_gz_decompress (const void *compressed, int compressed_len, |
| 38 | uint64_t *decompressed_lenp) { |
| 39 | int pass = 1; |
| 40 | char *decompressed = NULL; |
| 41 | |
| 42 | /* First pass (1): calculate decompressed size. |
| 43 | * (pass-1 is skipped if *decompressed_lenp is |
| 44 | * non-zero). |
| 45 | * Second pass (2): perform actual decompression. |
| 46 | */ |
| 47 | |
| 48 | if (*decompressed_lenp != 0LLU) |
| 49 | pass++; |
| 50 | |
| 51 | for (; pass <= 2 ; pass++) { |
| 52 | z_stream strm = RD_ZERO_INIT; |
| 53 | gz_header hdr; |
| 54 | char buf[512]; |
| 55 | char *p; |
| 56 | int len; |
| 57 | int r; |
| 58 | |
| 59 | if ((r = inflateInit2(&strm, 15+32)) != Z_OK) |
| 60 | goto fail; |
| 61 | |
| 62 | strm.next_in = (void *)compressed; |
| 63 | strm.avail_in = compressed_len; |
| 64 | |
| 65 | if ((r = inflateGetHeader(&strm, &hdr)) != Z_OK) { |
| 66 | inflateEnd(&strm); |
| 67 | goto fail; |
| 68 | } |
| 69 | |
| 70 | if (pass == 1) { |
| 71 | /* Use dummy output buffer */ |
| 72 | p = buf; |
| 73 | len = sizeof(buf); |
| 74 | } else { |
| 75 | /* Use real output buffer */ |
| 76 | p = decompressed; |
| 77 | len = (int)*decompressed_lenp; |
| 78 | } |
| 79 | |
| 80 | do { |
| 81 | strm.next_out = (unsigned char *)p; |
| 82 | strm.avail_out = len; |
| 83 | |
| 84 | r = inflate(&strm, Z_NO_FLUSH); |
| 85 | switch (r) { |
| 86 | case Z_STREAM_ERROR: |
| 87 | case Z_NEED_DICT: |
| 88 | case Z_DATA_ERROR: |
| 89 | case Z_MEM_ERROR: |
| 90 | inflateEnd(&strm); |
| 91 | goto fail; |
| 92 | } |
| 93 | |
| 94 | if (pass == 2) { |
| 95 | /* Advance output pointer (in pass 2). */ |
| 96 | p += len - strm.avail_out; |
| 97 | len -= len - strm.avail_out; |
| 98 | } |
| 99 | |
| 100 | } while (strm.avail_out == 0 && r != Z_STREAM_END); |
| 101 | |
| 102 | |
| 103 | if (pass == 1) { |
| 104 | *decompressed_lenp = strm.total_out; |
| 105 | if (!(decompressed = malloc((size_t)(*decompressed_lenp)+1))) { |
| 106 | inflateEnd(&strm); |
| 107 | return NULL; |
| 108 | } |
| 109 | /* For convenience of the caller we nul-terminate |
| 110 | * the buffer. If it happens to be a string there |
| 111 | * is no need for extra copies. */ |
| 112 | decompressed[*decompressed_lenp] = '\0'; |
| 113 | } |
| 114 | |
| 115 | inflateEnd(&strm); |
| 116 | } |
| 117 | |
| 118 | return decompressed; |
| 119 | |
| 120 | fail: |
| 121 | if (decompressed) |
| 122 | free(decompressed); |
| 123 | return NULL; |
| 124 | } |
| 125 | |