1 /*-
2  * Copyright (c) 2009 Michihiro NAKAJIMA
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  * 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.
13  *
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.
24  */
25 
26 #include "archive_platform.h"
27 
28 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 
35 #include "archive.h"
36 #include "archive_endian.h"
37 #include "archive_private.h"
38 #include "archive_read_private.h"
39 
40 struct rpm {
41 	int64_t		 total_in;
42 	size_t		 hpos;
43 	size_t		 hlen;
44 	unsigned char	 header[16];
45 	enum {
46 		ST_LEAD,	/* Skipping 'Lead' section. */
47 		ST_HEADER,	/* Reading 'Header' section;
48 				 * first 16 bytes. */
49 		ST_HEADER_DATA,	/* Skipping 'Header' section. */
50 		ST_PADDING,	/* Skipping padding data after the
51 				 * 'Header' section. */
52 		ST_ARCHIVE	/* Reading 'Archive' section. */
53 	}		 state;
54 	int		 first_header;
55 };
56 #define RPM_LEAD_SIZE	96	/* Size of 'Lead' section. */
57 
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 *);
61 
62 static ssize_t	rpm_filter_read(struct archive_read_filter *,
63 		    const void **);
64 static int	rpm_filter_close(struct archive_read_filter *);
65 
66 #if ARCHIVE_VERSION_NUMBER < 4000000
67 /* Deprecated; remove in libarchive 4.0 */
68 int
69 archive_read_support_compression_rpm(struct archive *a)
70 {
71 	return archive_read_support_filter_rpm(a);
72 }
73 #endif
74 
75 int
76 archive_read_support_filter_rpm(struct archive *_a)
77 {
78 	struct archive_read *a = (struct archive_read *)_a;
79 	struct archive_read_filter_bidder *bidder;
80 
81 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
82 	    ARCHIVE_STATE_NEW, "archive_read_support_filter_rpm");
83 
84 	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
85 		return (ARCHIVE_FATAL);
86 
87 	bidder->data = NULL;
88 	bidder->name = "rpm";
89 	bidder->bid = rpm_bidder_bid;
90 	bidder->init = rpm_bidder_init;
91 	bidder->options = NULL;
92 	bidder->free = NULL;
93 	return (ARCHIVE_OK);
94 }
95 
96 static int
97 rpm_bidder_bid(struct archive_read_filter_bidder *self,
98     struct archive_read_filter *filter)
99 {
100 	const unsigned char *b;
101 	ssize_t avail;
102 	int bits_checked;
103 
104 	(void)self; /* UNUSED */
105 
106 	b = __archive_read_filter_ahead(filter, 8, &avail);
107 	if (b == NULL)
108 		return (0);
109 
110 	bits_checked = 0;
111 	/*
112 	 * Verify Header Magic Bytes : 0XED 0XAB 0XEE 0XDB
113 	 */
114 	if (memcmp(b, "\xED\xAB\xEE\xDB", 4) != 0)
115 		return (0);
116 	bits_checked += 32;
117 	/*
118 	 * Check major version.
119 	 */
120 	if (b[4] != 3 && b[4] != 4)
121 		return (0);
122 	bits_checked += 8;
123 	/*
124 	 * Check package type; binary or source.
125 	 */
126 	if (b[6] != 0)
127 		return (0);
128 	bits_checked += 8;
129 	if (b[7] != 0 && b[7] != 1)
130 		return (0);
131 	bits_checked += 8;
132 
133 	return (bits_checked);
134 }
135 
136 static int
137 rpm_bidder_init(struct archive_read_filter *self)
138 {
139 	struct rpm   *rpm;
140 
141 	self->code = ARCHIVE_FILTER_RPM;
142 	self->name = "rpm";
143 	self->read = rpm_filter_read;
144 	self->skip = NULL; /* not supported */
145 	self->close = rpm_filter_close;
146 
147 	rpm = (struct rpm *)calloc(sizeof(*rpm), 1);
148 	if (rpm == NULL) {
149 		archive_set_error(&self->archive->archive, ENOMEM,
150 		    "Can't allocate data for rpm");
151 		return (ARCHIVE_FATAL);
152 	}
153 
154 	self->data = rpm;
155 	rpm->state = ST_LEAD;
156 
157 	return (ARCHIVE_OK);
158 }
159 
160 static ssize_t
161 rpm_filter_read(struct archive_read_filter *self, const void **buff)
162 {
163 	struct rpm *rpm;
164 	const unsigned char *b;
165 	ssize_t avail_in, total;
166 	size_t used, n;
167 	uint32_t section;
168 	uint32_t bytes;
169 
170 	rpm = (struct rpm *)self->data;
171 	*buff = NULL;
172 	total = avail_in = 0;
173 	b = NULL;
174 	used = 0;
175 	do {
176 		if (b == NULL) {
177 			b = __archive_read_filter_ahead(self->upstream, 1,
178 			    &avail_in);
179 			if (b == NULL) {
180 				if (avail_in < 0)
181 					return (ARCHIVE_FATAL);
182 				else
183 					break;
184 			}
185 		}
186 
187 		switch (rpm->state) {
188 		case ST_LEAD:
189 			if (rpm->total_in + avail_in < RPM_LEAD_SIZE)
190 				used += avail_in;
191 			else {
192 				n = (size_t)(RPM_LEAD_SIZE - rpm->total_in);
193 				used += n;
194 				b += n;
195 				rpm->state = ST_HEADER;
196 				rpm->hpos = 0;
197 				rpm->hlen = 0;
198 				rpm->first_header = 1;
199 			}
200 			break;
201 		case ST_HEADER:
202 			n = 16 - rpm->hpos;
203 			if (n > avail_in - used)
204 				n = avail_in - used;
205 			memcpy(rpm->header+rpm->hpos, b, n);
206 			b += n;
207 			used += n;
208 			rpm->hpos += n;
209 
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) {
216 						archive_set_error(
217 						    &self->archive->archive,
218 						    ARCHIVE_ERRNO_FILE_FORMAT,
219 						    "Unrecoginized rpm header");
220 						return (ARCHIVE_FATAL);
221 					}
222 					rpm->state = ST_ARCHIVE;
223 					*buff = rpm->header;
224 					total = rpm->hpos;
225 					break;
226 				}
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;
233 			}
234 			break;
235 		case ST_HEADER_DATA:
236 			n = rpm->hlen - rpm->hpos;
237 			if (n > avail_in - used)
238 				n = avail_in - used;
239 			b += n;
240 			used += n;
241 			rpm->hpos += n;
242 			if (rpm->hpos == rpm->hlen)
243 				rpm->state = ST_PADDING;
244 			break;
245 		case ST_PADDING:
246 			while (used < (size_t)avail_in) {
247 				if (*b != 0) {
248 					/* Read next header. */
249 					rpm->state = ST_HEADER;
250 					rpm->hpos = 0;
251 					rpm->hlen = 0;
252 					break;
253 				}
254 				b++;
255 				used++;
256 			}
257 			break;
258 		case ST_ARCHIVE:
259 			*buff = b;
260 			total = avail_in;
261 			used = avail_in;
262 			break;
263 		}
264 		if (used == (size_t)avail_in) {
265 			rpm->total_in += used;
266 			__archive_read_filter_consume(self->upstream, used);
267 			b = NULL;
268 			used = 0;
269 		}
270 	} while (total == 0 && avail_in > 0);
271 
272 	if (used > 0 && b != NULL) {
273 		rpm->total_in += used;
274 		__archive_read_filter_consume(self->upstream, used);
275 	}
276 	return (total);
277 }
278 
279 static int
280 rpm_filter_close(struct archive_read_filter *self)
281 {
282 	struct rpm *rpm;
283 
284 	rpm = (struct rpm *)self->data;
285 	free(rpm);
286 
287 	return (ARCHIVE_OK);
288 }
289 
290