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 static const struct archive_read_filter_bidder_vtable
76 rpm_bidder_vtable = {
77 	.bid = rpm_bidder_bid,
78 	.init = rpm_bidder_init,
79 };
80 
81 int
82 archive_read_support_filter_rpm(struct archive *_a)
83 {
84 	struct archive_read *a = (struct archive_read *)_a;
85 
86 	return __archive_read_register_bidder(a, NULL, "rpm",
87 			&rpm_bidder_vtable);
88 }
89 
90 static int
91 rpm_bidder_bid(struct archive_read_filter_bidder *self,
92     struct archive_read_filter *filter)
93 {
94 	const unsigned char *b;
95 	ssize_t avail;
96 	int bits_checked;
97 
98 	(void)self; /* UNUSED */
99 
100 	b = __archive_read_filter_ahead(filter, 8, &avail);
101 	if (b == NULL)
102 		return (0);
103 
104 	bits_checked = 0;
105 	/*
106 	 * Verify Header Magic Bytes : 0XED 0XAB 0XEE 0XDB
107 	 */
108 	if (memcmp(b, "\xED\xAB\xEE\xDB", 4) != 0)
109 		return (0);
110 	bits_checked += 32;
111 	/*
112 	 * Check major version.
113 	 */
114 	if (b[4] != 3 && b[4] != 4)
115 		return (0);
116 	bits_checked += 8;
117 	/*
118 	 * Check package type; binary or source.
119 	 */
120 	if (b[6] != 0)
121 		return (0);
122 	bits_checked += 8;
123 	if (b[7] != 0 && b[7] != 1)
124 		return (0);
125 	bits_checked += 8;
126 
127 	return (bits_checked);
128 }
129 
130 static const struct archive_read_filter_vtable
131 rpm_reader_vtable = {
132 	.read = rpm_filter_read,
133 	.close = rpm_filter_close,
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 
144 	rpm = (struct rpm *)calloc(sizeof(*rpm), 1);
145 	if (rpm == NULL) {
146 		archive_set_error(&self->archive->archive, ENOMEM,
147 		    "Can't allocate data for rpm");
148 		return (ARCHIVE_FATAL);
149 	}
150 
151 	self->data = rpm;
152 	rpm->state = ST_LEAD;
153 	self->vtable = &rpm_reader_vtable;
154 
155 	return (ARCHIVE_OK);
156 }
157 
158 static ssize_t
159 rpm_filter_read(struct archive_read_filter *self, const void **buff)
160 {
161 	struct rpm *rpm;
162 	const unsigned char *b;
163 	ssize_t avail_in, total;
164 	size_t used, n;
165 	uint32_t section;
166 	uint32_t bytes;
167 
168 	rpm = (struct rpm *)self->data;
169 	*buff = NULL;
170 	total = avail_in = 0;
171 	b = NULL;
172 	used = 0;
173 	do {
174 		if (b == NULL) {
175 			b = __archive_read_filter_ahead(self->upstream, 1,
176 			    &avail_in);
177 			if (b == NULL) {
178 				if (avail_in < 0)
179 					return (ARCHIVE_FATAL);
180 				else
181 					break;
182 			}
183 		}
184 
185 		switch (rpm->state) {
186 		case ST_LEAD:
187 			if (rpm->total_in + avail_in < RPM_LEAD_SIZE)
188 				used += avail_in;
189 			else {
190 				n = (size_t)(RPM_LEAD_SIZE - rpm->total_in);
191 				used += n;
192 				b += n;
193 				rpm->state = ST_HEADER;
194 				rpm->hpos = 0;
195 				rpm->hlen = 0;
196 				rpm->first_header = 1;
197 			}
198 			break;
199 		case ST_HEADER:
200 			n = 16 - rpm->hpos;
201 			if (n > avail_in - used)
202 				n = avail_in - used;
203 			memcpy(rpm->header+rpm->hpos, b, n);
204 			b += n;
205 			used += n;
206 			rpm->hpos += n;
207 
208 			if (rpm->hpos == 16) {
209 				if (rpm->header[0] != 0x8e ||
210 				    rpm->header[1] != 0xad ||
211 				    rpm->header[2] != 0xe8 ||
212 				    rpm->header[3] != 0x01) {
213 					if (rpm->first_header) {
214 						archive_set_error(
215 						    &self->archive->archive,
216 						    ARCHIVE_ERRNO_FILE_FORMAT,
217 						    "Unrecognized rpm header");
218 						return (ARCHIVE_FATAL);
219 					}
220 					rpm->state = ST_ARCHIVE;
221 					*buff = rpm->header;
222 					total = rpm->hpos;
223 					break;
224 				}
225 				/* Calculate 'Header' length. */
226 				section = archive_be32dec(rpm->header+8);
227 				bytes = archive_be32dec(rpm->header+12);
228 				rpm->hlen = 16 + section * 16 + bytes;
229 				rpm->state = ST_HEADER_DATA;
230 				rpm->first_header = 0;
231 			}
232 			break;
233 		case ST_HEADER_DATA:
234 			n = rpm->hlen - rpm->hpos;
235 			if (n > avail_in - used)
236 				n = avail_in - used;
237 			b += n;
238 			used += n;
239 			rpm->hpos += n;
240 			if (rpm->hpos == rpm->hlen)
241 				rpm->state = ST_PADDING;
242 			break;
243 		case ST_PADDING:
244 			while (used < (size_t)avail_in) {
245 				if (*b != 0) {
246 					/* Read next header. */
247 					rpm->state = ST_HEADER;
248 					rpm->hpos = 0;
249 					rpm->hlen = 0;
250 					break;
251 				}
252 				b++;
253 				used++;
254 			}
255 			break;
256 		case ST_ARCHIVE:
257 			*buff = b;
258 			total = avail_in;
259 			used = avail_in;
260 			break;
261 		}
262 		if (used == (size_t)avail_in) {
263 			rpm->total_in += used;
264 			__archive_read_filter_consume(self->upstream, used);
265 			b = NULL;
266 			used = 0;
267 		}
268 	} while (total == 0 && avail_in > 0);
269 
270 	if (used > 0 && b != NULL) {
271 		rpm->total_in += used;
272 		__archive_read_filter_consume(self->upstream, used);
273 	}
274 	return (total);
275 }
276 
277 static int
278 rpm_filter_close(struct archive_read_filter *self)
279 {
280 	struct rpm *rpm;
281 
282 	rpm = (struct rpm *)self->data;
283 	free(rpm);
284 
285 	return (ARCHIVE_OK);
286 }
287 
288