2 * Copyright (c) 2009 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
36 #include "archive_endian.h"
37 #include "archive_private.h"
38 #include "archive_read_private.h"
44 unsigned char header[16];
46 ST_LEAD, /* Skipping 'Lead' section. */
47 ST_HEADER, /* Reading 'Header' section;
49 ST_HEADER_DATA, /* Skipping 'Header' section. */
50 ST_PADDING, /* Skipping padding data after the
51 * 'Header' section. */
52 ST_ARCHIVE /* Reading 'Archive' section. */
56 #define RPM_LEAD_SIZE 96 /* Size of 'Lead' section. */
58 static int rpm_bidder_bid(struct archive_read_filter_bidder *,
59 struct archive_read_filter *);
60 static int rpm_bidder_init(struct archive_read_filter *);
62 static ssize_t rpm_filter_read(struct archive_read_filter *,
64 static int rpm_filter_close(struct archive_read_filter *);
66 #if ARCHIVE_VERSION_NUMBER < 4000000
67 /* Deprecated; remove in libarchive 4.0 */
69 archive_read_support_compression_rpm(struct archive *a)
71 return archive_read_support_filter_rpm(a);
76 archive_read_support_filter_rpm(struct archive *_a)
78 struct archive_read *a = (struct archive_read *)_a;
79 struct archive_read_filter_bidder *bidder;
81 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
82 ARCHIVE_STATE_NEW, "archive_read_support_filter_rpm");
84 if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
85 return (ARCHIVE_FATAL);
88 bidder->bid = rpm_bidder_bid;
89 bidder->init = rpm_bidder_init;
90 bidder->options = NULL;
96 rpm_bidder_bid(struct archive_read_filter_bidder *self,
97 struct archive_read_filter *filter)
99 const unsigned char *b;
103 (void)self; /* UNUSED */
105 b = __archive_read_filter_ahead(filter, 8, &avail);
111 * Verify Header Magic Bytes : 0XED 0XAB 0XEE 0XDB
113 if (memcmp(b, "\xED\xAB\xEE\xDB", 4) != 0)
117 * Check major version.
119 if (b[4] != 3 && b[4] != 4)
123 * Check package type; binary or source.
128 if (b[7] != 0 && b[7] != 1)
132 return (bits_checked);
136 rpm_bidder_init(struct archive_read_filter *self)
140 self->code = ARCHIVE_COMPRESSION_RPM;
142 self->read = rpm_filter_read;
143 self->skip = NULL; /* not supported */
144 self->close = rpm_filter_close;
146 rpm = (struct rpm *)calloc(sizeof(*rpm), 1);
148 archive_set_error(&self->archive->archive, ENOMEM,
149 "Can't allocate data for rpm");
150 return (ARCHIVE_FATAL);
154 rpm->state = ST_LEAD;
160 rpm_filter_read(struct archive_read_filter *self, const void **buff)
163 const unsigned char *b;
164 ssize_t avail_in, total;
169 rpm = (struct rpm *)self->data;
171 total = avail_in = 0;
176 b = __archive_read_filter_ahead(self->upstream, 1,
180 return (ARCHIVE_FATAL);
186 switch (rpm->state) {
188 if (rpm->total_in + avail_in < RPM_LEAD_SIZE)
191 n = (size_t)(RPM_LEAD_SIZE - rpm->total_in);
194 rpm->state = ST_HEADER;
197 rpm->first_header = 1;
202 if (n > avail_in - used)
204 memcpy(rpm->header+rpm->hpos, b, n);
209 if (rpm->hpos == 16) {
210 if (rpm->header[0] != 0x8e ||
211 rpm->header[1] != 0xad ||
212 rpm->header[2] != 0xe8 ||
213 rpm->header[3] != 0x01) {
214 if (rpm->first_header) {
216 &self->archive->archive,
217 ARCHIVE_ERRNO_FILE_FORMAT,
218 "Unrecoginized rpm header");
219 return (ARCHIVE_FATAL);
221 rpm->state = ST_ARCHIVE;
226 /* Calculate 'Header' length. */
227 section = archive_be32dec(rpm->header+8);
228 bytes = archive_be32dec(rpm->header+12);
229 rpm->hlen = 16 + section * 16 + bytes;
230 rpm->state = ST_HEADER_DATA;
231 rpm->first_header = 0;
235 n = rpm->hlen - rpm->hpos;
236 if (n > avail_in - used)
241 if (rpm->hpos == rpm->hlen)
242 rpm->state = ST_PADDING;
245 while (used < (size_t)avail_in) {
247 /* Read next header. */
248 rpm->state = ST_HEADER;
263 if (used == (size_t)avail_in) {
264 rpm->total_in += used;
265 __archive_read_filter_consume(self->upstream, used);
269 } while (total == 0 && avail_in > 0);
271 if (used > 0 && b != NULL) {
272 rpm->total_in += used;
273 __archive_read_filter_consume(self->upstream, used);
279 rpm_filter_close(struct archive_read_filter *self)
283 rpm = (struct rpm *)self->data;