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