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);
89 bidder->bid = rpm_bidder_bid;
90 bidder->init = rpm_bidder_init;
91 bidder->options = NULL;
97 rpm_bidder_bid(struct archive_read_filter_bidder *self,
98 struct archive_read_filter *filter)
100 const unsigned char *b;
104 (void)self; /* UNUSED */
106 b = __archive_read_filter_ahead(filter, 8, &avail);
112 * Verify Header Magic Bytes : 0XED 0XAB 0XEE 0XDB
114 if (memcmp(b, "\xED\xAB\xEE\xDB", 4) != 0)
118 * Check major version.
120 if (b[4] != 3 && b[4] != 4)
124 * Check package type; binary or source.
129 if (b[7] != 0 && b[7] != 1)
133 return (bits_checked);
137 rpm_bidder_init(struct archive_read_filter *self)
141 self->code = ARCHIVE_FILTER_RPM;
143 self->read = rpm_filter_read;
144 self->skip = NULL; /* not supported */
145 self->close = rpm_filter_close;
147 rpm = (struct rpm *)calloc(sizeof(*rpm), 1);
149 archive_set_error(&self->archive->archive, ENOMEM,
150 "Can't allocate data for rpm");
151 return (ARCHIVE_FATAL);
155 rpm->state = ST_LEAD;
161 rpm_filter_read(struct archive_read_filter *self, const void **buff)
164 const unsigned char *b;
165 ssize_t avail_in, total;
170 rpm = (struct rpm *)self->data;
172 total = avail_in = 0;
177 b = __archive_read_filter_ahead(self->upstream, 1,
181 return (ARCHIVE_FATAL);
187 switch (rpm->state) {
189 if (rpm->total_in + avail_in < RPM_LEAD_SIZE)
192 n = (size_t)(RPM_LEAD_SIZE - rpm->total_in);
195 rpm->state = ST_HEADER;
198 rpm->first_header = 1;
203 if (n > avail_in - used)
205 memcpy(rpm->header+rpm->hpos, b, n);
210 if (rpm->hpos == 16) {
211 if (rpm->header[0] != 0x8e ||
212 rpm->header[1] != 0xad ||
213 rpm->header[2] != 0xe8 ||
214 rpm->header[3] != 0x01) {
215 if (rpm->first_header) {
217 &self->archive->archive,
218 ARCHIVE_ERRNO_FILE_FORMAT,
219 "Unrecoginized rpm header");
220 return (ARCHIVE_FATAL);
222 rpm->state = ST_ARCHIVE;
227 /* Calculate 'Header' length. */
228 section = archive_be32dec(rpm->header+8);
229 bytes = archive_be32dec(rpm->header+12);
230 rpm->hlen = 16 + section * 16 + bytes;
231 rpm->state = ST_HEADER_DATA;
232 rpm->first_header = 0;
236 n = rpm->hlen - rpm->hpos;
237 if (n > avail_in - used)
242 if (rpm->hpos == rpm->hlen)
243 rpm->state = ST_PADDING;
246 while (used < (size_t)avail_in) {
248 /* Read next header. */
249 rpm->state = ST_HEADER;
264 if (used == (size_t)avail_in) {
265 rpm->total_in += used;
266 __archive_read_filter_consume(self->upstream, used);
270 } while (total == 0 && avail_in > 0);
272 if (used > 0 && b != NULL) {
273 rpm->total_in += used;
274 __archive_read_filter_consume(self->upstream, used);
280 rpm_filter_close(struct archive_read_filter *self)
284 rpm = (struct rpm *)self->data;