David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 David Gibson, IBM Corporation. |
| 3 | * Based on earlier work, Copyright (C) Paul Mackerras 1997. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <stddef.h> |
| 12 | #include "string.h" |
| 13 | #include "stdio.h" |
| 14 | #include "ops.h" |
| 15 | #include "gunzip_util.h" |
| 16 | |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 17 | #define HEAD_CRC 2 |
| 18 | #define EXTRA_FIELD 4 |
| 19 | #define ORIG_NAME 8 |
| 20 | #define COMMENT 0x10 |
| 21 | #define RESERVED 0xe0 |
| 22 | |
David Gibson | 7850ad5c | 2007-03-14 16:32:17 +1100 | [diff] [blame] | 23 | /** |
| 24 | * gunzip_start - prepare to decompress gzip data |
| 25 | * @state: decompressor state structure to be initialized |
| 26 | * @src: buffer containing gzip compressed or uncompressed data |
| 27 | * @srclen: size in bytes of the buffer at src |
| 28 | * |
| 29 | * If the buffer at @src contains a gzip header, this function |
| 30 | * initializes zlib to decompress the data, storing the decompression |
| 31 | * state in @state. The other functions in this file can then be used |
| 32 | * to decompress data from the gzipped stream. |
| 33 | * |
| 34 | * If the buffer at @src does not contain a gzip header, it is assumed |
| 35 | * to contain uncompressed data. The buffer information is recorded |
| 36 | * in @state and the other functions in this file will simply copy |
| 37 | * data from the uncompressed data stream at @src. |
| 38 | * |
| 39 | * Any errors, such as bad compressed data, cause an error to be |
| 40 | * printed an the platform's exit() function to be called. |
| 41 | */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 42 | void gunzip_start(struct gunzip_state *state, void *src, int srclen) |
| 43 | { |
| 44 | char *hdr = src; |
| 45 | int hdrlen = 0; |
| 46 | |
| 47 | memset(state, 0, sizeof(*state)); |
| 48 | |
| 49 | /* Check for gzip magic number */ |
| 50 | if ((hdr[0] == 0x1f) && (hdr[1] == 0x8b)) { |
| 51 | /* gzip data, initialize zlib parameters */ |
| 52 | int r, flags; |
| 53 | |
| 54 | state->s.workspace = state->scratch; |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 55 | if (zlib_inflate_workspacesize() > sizeof(state->scratch)) |
| 56 | fatal("insufficient scratch space for gunzip\n\r"); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 57 | |
| 58 | /* skip header */ |
| 59 | hdrlen = 10; |
| 60 | flags = hdr[3]; |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 61 | if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0) |
| 62 | fatal("bad gzipped data\n\r"); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 63 | if ((flags & EXTRA_FIELD) != 0) |
| 64 | hdrlen = 12 + hdr[10] + (hdr[11] << 8); |
| 65 | if ((flags & ORIG_NAME) != 0) |
| 66 | while (hdr[hdrlen++] != 0) |
| 67 | ; |
| 68 | if ((flags & COMMENT) != 0) |
| 69 | while (hdr[hdrlen++] != 0) |
| 70 | ; |
| 71 | if ((flags & HEAD_CRC) != 0) |
| 72 | hdrlen += 2; |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 73 | if (hdrlen >= srclen) |
| 74 | fatal("gunzip_start: ran out of data in header\n\r"); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 75 | |
| 76 | r = zlib_inflateInit2(&state->s, -MAX_WBITS); |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 77 | if (r != Z_OK) |
| 78 | fatal("inflateInit2 returned %d\n\r", r); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | state->s.next_in = src + hdrlen; |
| 82 | state->s.avail_in = srclen - hdrlen; |
| 83 | } |
| 84 | |
David Gibson | 7850ad5c | 2007-03-14 16:32:17 +1100 | [diff] [blame] | 85 | /** |
| 86 | * gunzip_partial - extract bytes from a gzip data stream |
| 87 | * @state: gzip state structure previously initialized by gunzip_start() |
| 88 | * @dst: buffer to store extracted data |
| 89 | * @dstlen: maximum number of bytes to extract |
| 90 | * |
| 91 | * This function extracts at most @dstlen bytes from the data stream |
| 92 | * previously associated with @state by gunzip_start(), decompressing |
| 93 | * if necessary. Exactly @dstlen bytes are extracted unless the data |
| 94 | * stream doesn't contain enough bytes, in which case the entire |
| 95 | * remainder of the stream is decompressed. |
| 96 | * |
| 97 | * Returns the actual number of bytes extracted. If any errors occur, |
| 98 | * such as a corrupted compressed stream, an error is printed an the |
| 99 | * platform's exit() function is called. |
| 100 | */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 101 | int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen) |
| 102 | { |
| 103 | int len; |
| 104 | |
| 105 | if (state->s.workspace) { |
| 106 | /* gunzipping */ |
| 107 | int r; |
| 108 | |
| 109 | state->s.next_out = dst; |
| 110 | state->s.avail_out = dstlen; |
| 111 | r = zlib_inflate(&state->s, Z_FULL_FLUSH); |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 112 | if (r != Z_OK && r != Z_STREAM_END) |
| 113 | fatal("inflate returned %d msg: %s\n\r", r, state->s.msg); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 114 | len = state->s.next_out - (unsigned char *)dst; |
| 115 | } else { |
| 116 | /* uncompressed image */ |
| 117 | len = min(state->s.avail_in, (unsigned)dstlen); |
| 118 | memcpy(dst, state->s.next_in, len); |
| 119 | state->s.next_in += len; |
| 120 | state->s.avail_in -= len; |
| 121 | } |
| 122 | return len; |
| 123 | } |
| 124 | |
David Gibson | 7850ad5c | 2007-03-14 16:32:17 +1100 | [diff] [blame] | 125 | /** |
| 126 | * gunzip_exactly - extract a fixed number of bytes from a gzip data stream |
| 127 | * @state: gzip state structure previously initialized by gunzip_start() |
| 128 | * @dst: buffer to store extracted data |
| 129 | * @dstlen: number of bytes to extract |
| 130 | * |
| 131 | * This function extracts exactly @dstlen bytes from the data stream |
| 132 | * previously associated with @state by gunzip_start(), decompressing |
| 133 | * if necessary. |
| 134 | * |
| 135 | * If there are less @dstlen bytes available in the data stream, or if |
| 136 | * any other errors occur, such as a corrupted compressed stream, an |
| 137 | * error is printed an the platform's exit() function is called. |
| 138 | */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 139 | void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen) |
| 140 | { |
| 141 | int len; |
| 142 | |
| 143 | len = gunzip_partial(state, dst, dstlen); |
Milton Miller | 6a92321 | 2007-03-21 09:02:44 -0600 | [diff] [blame^] | 144 | if (len < dstlen) |
| 145 | fatal("gunzip_block: ran out of data\n\r"); |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 146 | } |
| 147 | |
David Gibson | 7850ad5c | 2007-03-14 16:32:17 +1100 | [diff] [blame] | 148 | /** |
| 149 | * gunzip_discard - discard bytes from a gzip data stream |
| 150 | * @state: gzip state structure previously initialized by gunzip_start() |
| 151 | * @len: number of bytes to discard |
| 152 | * |
| 153 | * This function extracts, then discards exactly @len bytes from the |
| 154 | * data stream previously associated with @state by gunzip_start(). |
| 155 | * Subsequent gunzip_partial(), gunzip_exactly() or gunzip_finish() |
| 156 | * calls will extract the data following the discarded bytes in the |
| 157 | * data stream. |
| 158 | * |
| 159 | * If there are less @len bytes available in the data stream, or if |
| 160 | * any other errors occur, such as a corrupted compressed stream, an |
| 161 | * error is printed an the platform's exit() function is called. |
| 162 | */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 163 | void gunzip_discard(struct gunzip_state *state, int len) |
| 164 | { |
| 165 | static char discard_buf[128]; |
| 166 | |
| 167 | while (len > sizeof(discard_buf)) { |
| 168 | gunzip_exactly(state, discard_buf, sizeof(discard_buf)); |
| 169 | len -= sizeof(discard_buf); |
| 170 | } |
| 171 | |
| 172 | if (len > 0) |
| 173 | gunzip_exactly(state, discard_buf, len); |
| 174 | } |
| 175 | |
David Gibson | 7850ad5c | 2007-03-14 16:32:17 +1100 | [diff] [blame] | 176 | /** |
| 177 | * gunzip_finish - extract all remaining bytes from a gzip data stream |
| 178 | * @state: gzip state structure previously initialized by gunzip_start() |
| 179 | * @dst: buffer to store extracted data |
| 180 | * @dstlen: maximum number of bytes to extract |
| 181 | * |
| 182 | * This function extracts all remaining data, or at most @dstlen |
| 183 | * bytes, from the stream previously associated with @state by |
| 184 | * gunzip_start(). zlib is then shut down, so it is an error to use |
| 185 | * any of the functions in this file on @state until it is |
| 186 | * re-initialized with another call to gunzip_start(). |
| 187 | * |
| 188 | * If any errors occur, such as a corrupted compressed stream, an |
| 189 | * error is printed an the platform's exit() function is called. |
| 190 | */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame] | 191 | int gunzip_finish(struct gunzip_state *state, void *dst, int dstlen) |
| 192 | { |
| 193 | int len; |
| 194 | |
| 195 | if (state->s.workspace) { |
| 196 | len = gunzip_partial(state, dst, dstlen); |
| 197 | zlib_inflateEnd(&state->s); |
| 198 | } else { |
| 199 | /* uncompressed image */ |
| 200 | len = min(state->s.avail_in, (unsigned)dstlen); |
| 201 | memcpy(dst, state->s.next_in, len); |
| 202 | } |
| 203 | |
| 204 | return len; |
| 205 | } |