[e37ddde1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2014 Martin Decky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[8d2dd7f2] | 29 | #include <stdint.h>
|
---|
[582a0b8] | 30 | #include <stddef.h>
|
---|
[e37ddde1] | 31 | #include <errno.h>
|
---|
| 32 | #include <mem.h>
|
---|
[e8b8b74] | 33 | #include <byteorder.h>
|
---|
[38d150e] | 34 | #include <stdlib.h>
|
---|
[e37ddde1] | 35 | #include "gzip.h"
|
---|
| 36 | #include "inflate.h"
|
---|
| 37 |
|
---|
| 38 | #define GZIP_ID1 UINT8_C(0x1f)
|
---|
| 39 | #define GZIP_ID2 UINT8_C(0x8b)
|
---|
| 40 |
|
---|
| 41 | #define GZIP_METHOD_DEFLATE UINT8_C(0x08)
|
---|
| 42 |
|
---|
| 43 | #define GZIP_FLAGS_MASK UINT8_C(0x1f)
|
---|
| 44 | #define GZIP_FLAG_FHCRC UINT8_C(1 << 1)
|
---|
| 45 | #define GZIP_FLAG_FEXTRA UINT8_C(1 << 2)
|
---|
| 46 | #define GZIP_FLAG_FNAME UINT8_C(1 << 3)
|
---|
| 47 | #define GZIP_FLAG_FCOMMENT UINT8_C(1 << 4)
|
---|
| 48 |
|
---|
| 49 | typedef struct {
|
---|
| 50 | uint8_t id1;
|
---|
| 51 | uint8_t id2;
|
---|
| 52 | uint8_t method;
|
---|
| 53 | uint8_t flags;
|
---|
| 54 | uint32_t mtime;
|
---|
| 55 | uint8_t extra_flags;
|
---|
| 56 | uint8_t os;
|
---|
| 57 | } __attribute__((packed)) gzip_header_t;
|
---|
| 58 |
|
---|
| 59 | typedef struct {
|
---|
| 60 | uint32_t crc32;
|
---|
| 61 | uint32_t size;
|
---|
| 62 | } __attribute__((packed)) gzip_footer_t;
|
---|
| 63 |
|
---|
| 64 | /** Expand GZIP compressed data
|
---|
| 65 | *
|
---|
| 66 | * The routine allocates the output buffer based
|
---|
| 67 | * on the size encoded in the input stream. This
|
---|
| 68 | * effectively limits the size of the uncompressed
|
---|
| 69 | * data to 4 GiB (expanding input streams that actually
|
---|
| 70 | * encode more data will always fail).
|
---|
| 71 | *
|
---|
| 72 | * So far, no CRC is perfomed.
|
---|
| 73 | *
|
---|
| 74 | * @param[in] src Source data buffer.
|
---|
| 75 | * @param[in] srclen Source buffer size (bytes).
|
---|
| 76 | * @param[out] dest Destination data buffer.
|
---|
| 77 | * @param[out] destlen Destination buffer size (bytes).
|
---|
| 78 | *
|
---|
| 79 | * @return EOK on success.
|
---|
| 80 | * @return ENOENT on distance too large.
|
---|
| 81 | * @return EINVAL on invalid Huffman code, invalid deflate data,
|
---|
| 82 | * invalid compression method or invalid stream.
|
---|
| 83 | * @return ELIMIT on input buffer overrun.
|
---|
| 84 | * @return ENOMEM on output buffer overrun.
|
---|
| 85 | *
|
---|
| 86 | */
|
---|
[b7fd2a0] | 87 | errno_t gzip_expand(void *src, size_t srclen, void **dest, size_t *destlen)
|
---|
[e37ddde1] | 88 | {
|
---|
| 89 | gzip_header_t header;
|
---|
| 90 | gzip_footer_t footer;
|
---|
[a35b458] | 91 |
|
---|
[e37ddde1] | 92 | if ((srclen < sizeof(header)) || (srclen < sizeof(footer)))
|
---|
| 93 | return EINVAL;
|
---|
[a35b458] | 94 |
|
---|
[e37ddde1] | 95 | /* Decode header and footer */
|
---|
[a35b458] | 96 |
|
---|
[e37ddde1] | 97 | memcpy(&header, src, sizeof(header));
|
---|
| 98 | memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
|
---|
[a35b458] | 99 |
|
---|
[e37ddde1] | 100 | if ((header.id1 != GZIP_ID1) ||
|
---|
| 101 | (header.id2 != GZIP_ID2) ||
|
---|
| 102 | (header.method != GZIP_METHOD_DEFLATE) ||
|
---|
| 103 | ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
|
---|
| 104 | return EINVAL;
|
---|
[a35b458] | 105 |
|
---|
[e8b8b74] | 106 | *destlen = uint32_t_le2host(footer.size);
|
---|
[a35b458] | 107 |
|
---|
[e37ddde1] | 108 | /* Ignore extra metadata */
|
---|
[a35b458] | 109 |
|
---|
[e37ddde1] | 110 | void *stream = src + sizeof(header);
|
---|
| 111 | size_t stream_length = srclen - sizeof(header) - sizeof(footer);
|
---|
[a35b458] | 112 |
|
---|
[e37ddde1] | 113 | if ((header.flags & GZIP_FLAG_FEXTRA) != 0) {
|
---|
| 114 | uint16_t extra_length;
|
---|
[a35b458] | 115 |
|
---|
[e37ddde1] | 116 | if (stream_length < sizeof(extra_length))
|
---|
| 117 | return EINVAL;
|
---|
[a35b458] | 118 |
|
---|
[e37ddde1] | 119 | memcpy(&extra_length, stream, sizeof(extra_length));
|
---|
| 120 | stream += sizeof(extra_length);
|
---|
| 121 | stream_length -= sizeof(extra_length);
|
---|
[a35b458] | 122 |
|
---|
[e37ddde1] | 123 | if (stream_length < extra_length)
|
---|
| 124 | return EINVAL;
|
---|
[a35b458] | 125 |
|
---|
[e37ddde1] | 126 | stream += extra_length;
|
---|
| 127 | stream_length -= extra_length;
|
---|
| 128 | }
|
---|
[a35b458] | 129 |
|
---|
[e37ddde1] | 130 | if ((header.flags & GZIP_FLAG_FNAME) != 0) {
|
---|
| 131 | while (*((uint8_t *) stream) != 0) {
|
---|
| 132 | if (stream_length == 0)
|
---|
| 133 | return EINVAL;
|
---|
[a35b458] | 134 |
|
---|
[e37ddde1] | 135 | stream++;
|
---|
| 136 | stream_length--;
|
---|
| 137 | }
|
---|
[a35b458] | 138 |
|
---|
[e37ddde1] | 139 | if (stream_length == 0)
|
---|
| 140 | return EINVAL;
|
---|
[a35b458] | 141 |
|
---|
[e37ddde1] | 142 | stream++;
|
---|
| 143 | stream_length--;
|
---|
| 144 | }
|
---|
[a35b458] | 145 |
|
---|
[e37ddde1] | 146 | if ((header.flags & GZIP_FLAG_FCOMMENT) != 0) {
|
---|
| 147 | while (*((uint8_t *) stream) != 0) {
|
---|
| 148 | if (stream_length == 0)
|
---|
| 149 | return EINVAL;
|
---|
[a35b458] | 150 |
|
---|
[e37ddde1] | 151 | stream++;
|
---|
| 152 | stream_length--;
|
---|
| 153 | }
|
---|
[a35b458] | 154 |
|
---|
[e37ddde1] | 155 | if (stream_length == 0)
|
---|
| 156 | return EINVAL;
|
---|
[a35b458] | 157 |
|
---|
[e37ddde1] | 158 | stream++;
|
---|
| 159 | stream_length--;
|
---|
| 160 | }
|
---|
[a35b458] | 161 |
|
---|
[e37ddde1] | 162 | if ((header.flags & GZIP_FLAG_FHCRC) != 0) {
|
---|
| 163 | if (stream_length < 2)
|
---|
| 164 | return EINVAL;
|
---|
[a35b458] | 165 |
|
---|
[e37ddde1] | 166 | stream += 2;
|
---|
| 167 | stream_length -= 2;
|
---|
| 168 | }
|
---|
[a35b458] | 169 |
|
---|
[e37ddde1] | 170 | /* Allocate output buffer and inflate the data */
|
---|
[a35b458] | 171 |
|
---|
[e37ddde1] | 172 | *dest = malloc(*destlen);
|
---|
| 173 | if (*dest == NULL)
|
---|
| 174 | return ENOMEM;
|
---|
[a35b458] | 175 |
|
---|
[b7fd2a0] | 176 | errno_t ret = inflate(stream, stream_length, *dest, *destlen);
|
---|
[e37ddde1] | 177 | if (ret != EOK) {
|
---|
| 178 | free(dest);
|
---|
| 179 | return ret;
|
---|
| 180 | }
|
---|
[a35b458] | 181 |
|
---|
[e37ddde1] | 182 | return EOK;
|
---|
| 183 | }
|
---|