2 * Copyright (c) 2009-2011 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"
27 __FBSDID("$FreeBSD$");
40 #include "archive_private.h"
41 #include "archive_read_private.h"
43 /* Maximum lookahead during bid phase */
44 #define UUENCODE_BID_MAX_READ 128*1024 /* in bytes */
48 unsigned char *in_buff;
49 #define IN_BUFF_SIZE (1024)
52 unsigned char *out_buff;
53 #define OUT_BUFF_SIZE (64 * 1024)
55 #define ST_FIND_HEAD 0
58 #define ST_READ_BASE64 3
62 static int uudecode_bidder_bid(struct archive_read_filter_bidder *,
63 struct archive_read_filter *filter);
64 static int uudecode_bidder_init(struct archive_read_filter *);
66 static ssize_t uudecode_filter_read(struct archive_read_filter *,
68 static int uudecode_filter_close(struct archive_read_filter *);
70 #if ARCHIVE_VERSION_NUMBER < 4000000
71 /* Deprecated; remove in libarchive 4.0 */
73 archive_read_support_compression_uu(struct archive *a)
75 return archive_read_support_filter_uu(a);
79 static const struct archive_read_filter_bidder_vtable
80 uudecode_bidder_vtable = {
81 .bid = uudecode_bidder_bid,
82 .init = uudecode_bidder_init,
86 archive_read_support_filter_uu(struct archive *_a)
88 struct archive_read *a = (struct archive_read *)_a;
90 return __archive_read_register_bidder(a, NULL, "uu",
91 &uudecode_bidder_vtable);
94 static const unsigned char ascii[256] = {
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\n', 0, 0, '\r', 0, 0, /* 00 - 0F */
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
97 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
98 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
99 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
101 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
113 static const unsigned char uuchar[256] = {
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
116 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
117 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 30 - 3F */
118 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
119 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
120 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6F */
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 - 7F */
122 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
123 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
132 static const unsigned char base64[256] = {
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, /* 20 - 2F */
136 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 30 - 3F */
137 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
138 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 50 - 5F */
139 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
140 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 70 - 7F */
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
144 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
145 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
148 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
151 static const int base64num[128] = {
152 0, 0, 0, 0, 0, 0, 0, 0,
153 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
154 0, 0, 0, 0, 0, 0, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
156 0, 0, 0, 0, 0, 0, 0, 0,
157 0, 0, 0, 62, 0, 0, 0, 63, /* 20 - 2F */
158 52, 53, 54, 55, 56, 57, 58, 59,
159 60, 61, 0, 0, 0, 0, 0, 0, /* 30 - 3F */
160 0, 0, 1, 2, 3, 4, 5, 6,
161 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
162 15, 16, 17, 18, 19, 20, 21, 22,
163 23, 24, 25, 0, 0, 0, 0, 0, /* 50 - 5F */
164 0, 26, 27, 28, 29, 30, 31, 32,
165 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
166 41, 42, 43, 44, 45, 46, 47, 48,
167 49, 50, 51, 0, 0, 0, 0, 0, /* 70 - 7F */
171 get_line(const unsigned char *b, ssize_t avail, ssize_t *nlsize)
176 while (len < avail) {
178 case 0: /* Non-ascii character or control character. */
183 if (avail-len > 1 && b[1] == '\n') {
205 bid_get_line(struct archive_read_filter *filter,
206 const unsigned char **b, ssize_t *avail, ssize_t *ravail,
207 ssize_t *nl, size_t* nbytes_read)
217 len = get_line(*b, *avail, nl);
220 * Read bytes more while it does not reach the end of line.
222 while (*nl == 0 && len == *avail && !quit &&
223 *nbytes_read < UUENCODE_BID_MAX_READ) {
224 ssize_t diff = *ravail - *avail;
225 size_t nbytes_req = (*ravail+1023) & ~1023U;
228 /* Increase reading bytes if it is not enough to at least
230 if (nbytes_req < (size_t)*ravail + 160)
233 *b = __archive_read_filter_ahead(filter, nbytes_req, avail);
235 if (*ravail >= *avail)
237 /* Reading bytes reaches the end of a stream. */
238 *b = __archive_read_filter_ahead(filter, *avail, avail);
241 *nbytes_read = *avail;
245 tested = len;/* Skip some bytes we already determined. */
246 len = get_line(*b + tested, *avail - tested, nl);
253 #define UUDECODE(c) (((c) - 0x20) & 0x3f)
256 uudecode_bidder_bid(struct archive_read_filter_bidder *self,
257 struct archive_read_filter *filter)
259 const unsigned char *b;
260 ssize_t avail, ravail;
266 (void)self; /* UNUSED */
268 b = __archive_read_filter_ahead(filter, 1, &avail);
276 len = bid_get_line(filter, &b, &avail, &ravail, &nl, &nbytes_read);
277 if (len < 0 || nl == 0)
278 return (0); /* No match found. */
279 if (len - nl >= 11 && memcmp(b, "begin ", 6) == 0)
281 else if (len -nl >= 18 && memcmp(b, "begin-base64 ", 13) == 0)
286 if (l > 0 && (b[l] < '0' || b[l] > '7' ||
287 b[l+1] < '0' || b[l+1] > '7' ||
288 b[l+2] < '0' || b[l+2] > '7' || b[l+3] != ' '))
297 /* Do not read more than UUENCODE_BID_MAX_READ bytes */
298 if (nbytes_read >= UUENCODE_BID_MAX_READ)
303 len = bid_get_line(filter, &b, &avail, &ravail, &nl, &nbytes_read);
304 if (len < 0 || nl == 0)
305 return (0);/* There are non-ascii characters. */
312 /* Get a length of decoded bytes. */
313 l = UUDECODE(*b++); len--;
315 /* Normally, maximum length is 45(character 'M'). */
318 return (0); /* Line too short. */
326 (uuchar[*b] || /* Check sum. */
327 (*b >= 'a' && *b <= 'z'))) {/* Padding data(MINIX). */
332 if (avail && uuchar[*b])
333 return (firstline+30);
334 } else if (l == 13) {
335 /* "begin-base64 " */
343 if (avail >= 5 && memcmp(b, "====\n", 5) == 0)
344 return (firstline+40);
345 if (avail >= 6 && memcmp(b, "====\r\n", 6) == 0)
346 return (firstline+40);
347 if (avail > 0 && base64[*b])
348 return (firstline+30);
354 static const struct archive_read_filter_vtable
355 uudecode_reader_vtable = {
356 .read = uudecode_filter_read,
357 .close = uudecode_filter_close,
361 uudecode_bidder_init(struct archive_read_filter *self)
363 struct uudecode *uudecode;
367 self->code = ARCHIVE_FILTER_UU;
370 uudecode = (struct uudecode *)calloc(sizeof(*uudecode), 1);
371 out_buff = malloc(OUT_BUFF_SIZE);
372 in_buff = malloc(IN_BUFF_SIZE);
373 if (uudecode == NULL || out_buff == NULL || in_buff == NULL) {
374 archive_set_error(&self->archive->archive, ENOMEM,
375 "Can't allocate data for uudecode");
379 return (ARCHIVE_FATAL);
382 self->data = uudecode;
383 uudecode->in_buff = in_buff;
384 uudecode->in_cnt = 0;
385 uudecode->in_allocated = IN_BUFF_SIZE;
386 uudecode->out_buff = out_buff;
387 uudecode->state = ST_FIND_HEAD;
388 self->vtable = &uudecode_reader_vtable;
394 ensure_in_buff_size(struct archive_read_filter *self,
395 struct uudecode *uudecode, size_t size)
398 if (size > uudecode->in_allocated) {
403 * Calculate a new buffer size for in_buff.
404 * Increase its value until it has enough size we need.
406 newsize = uudecode->in_allocated;
408 if (newsize < IN_BUFF_SIZE*32)
411 newsize += IN_BUFF_SIZE;
412 } while (size > newsize);
413 /* Allocate the new buffer. */
414 ptr = malloc(newsize);
417 archive_set_error(&self->archive->archive,
419 "Can't allocate data for uudecode");
420 return (ARCHIVE_FATAL);
422 /* Move the remaining data in in_buff into the new buffer. */
423 if (uudecode->in_cnt)
424 memmove(ptr, uudecode->in_buff, uudecode->in_cnt);
425 /* Replace in_buff with the new buffer. */
426 free(uudecode->in_buff);
427 uudecode->in_buff = ptr;
428 uudecode->in_allocated = newsize;
434 uudecode_filter_read(struct archive_read_filter *self, const void **buff)
436 struct uudecode *uudecode;
437 const unsigned char *b, *d;
439 ssize_t avail_in, ravail;
442 ssize_t len, llen, nl;
444 uudecode = (struct uudecode *)self->data;
447 d = __archive_read_filter_ahead(self->upstream, 1, &avail_in);
448 if (d == NULL && avail_in < 0)
449 return (ARCHIVE_FATAL);
450 /* Quiet a code analyzer; make sure avail_in must be zero
456 out = uudecode->out_buff;
458 if (uudecode->state == ST_IGNORE) {
462 if (uudecode->in_cnt) {
464 * If there is remaining data which is saved by
465 * previous calling, use it first.
467 if (ensure_in_buff_size(self, uudecode,
468 avail_in + uudecode->in_cnt) != ARCHIVE_OK)
469 return (ARCHIVE_FATAL);
470 memcpy(uudecode->in_buff + uudecode->in_cnt,
472 d = uudecode->in_buff;
473 avail_in += uudecode->in_cnt;
474 uudecode->in_cnt = 0;
476 for (;used < avail_in; d += llen, used += llen) {
480 len = get_line(b, avail_in - used, &nl);
482 /* Non-ascii character is found. */
483 if (uudecode->state == ST_FIND_HEAD &&
484 (uudecode->total > 0 || total > 0)) {
485 uudecode->state = ST_IGNORE;
489 archive_set_error(&self->archive->archive,
491 "Insufficient compressed data");
492 return (ARCHIVE_FATAL);
495 if ((nl == 0) && (uudecode->state != ST_UUEND)) {
496 if (total == 0 && ravail <= 0) {
497 /* There is nothing more to read, fail */
498 archive_set_error(&self->archive->archive,
499 ARCHIVE_ERRNO_FILE_FORMAT,
500 "Missing format data");
501 return (ARCHIVE_FATAL);
504 * Save remaining data which does not contain
507 if (ensure_in_buff_size(self, uudecode, len)
509 return (ARCHIVE_FATAL);
510 if (uudecode->in_buff != b)
511 memmove(uudecode->in_buff, b, len);
512 uudecode->in_cnt = (int)len;
514 /* Do not return 0; it means end-of-file.
515 * We should try to read bytes more. */
516 __archive_read_filter_consume(
517 self->upstream, ravail);
523 switch (uudecode->state) {
526 /* Do not read more than UUENCODE_BID_MAX_READ bytes */
527 if (total + len >= UUENCODE_BID_MAX_READ) {
528 archive_set_error(&self->archive->archive,
529 ARCHIVE_ERRNO_FILE_FORMAT,
530 "Invalid format data");
531 return (ARCHIVE_FATAL);
533 if (len - nl >= 11 && memcmp(b, "begin ", 6) == 0)
535 else if (len - nl >= 18 &&
536 memcmp(b, "begin-base64 ", 13) == 0)
540 if (l != 0 && b[l] >= '0' && b[l] <= '7' &&
541 b[l+1] >= '0' && b[l+1] <= '7' &&
542 b[l+2] >= '0' && b[l+2] <= '7' && b[l+3] == ' ') {
544 uudecode->state = ST_READ_UU;
546 uudecode->state = ST_READ_BASE64;
550 if (total + len * 2 > OUT_BUFF_SIZE)
553 if (!uuchar[*b] || body <= 0) {
554 archive_set_error(&self->archive->archive,
556 "Insufficient compressed data");
557 return (ARCHIVE_FATAL);
559 /* Get length of undecoded bytes of current line. */
563 archive_set_error(&self->archive->archive,
565 "Insufficient compressed data");
566 return (ARCHIVE_FATAL);
569 uudecode->state = ST_UUEND;
575 if (!uuchar[b[0]] || !uuchar[b[1]])
577 n = UUDECODE(*b++) << 18;
578 n |= UUDECODE(*b++) << 12;
579 *out++ = n >> 16; total++;
585 n |= UUDECODE(*b++) << 6;
586 *out++ = (n >> 8) & 0xFF; total++;
593 *out++ = n & 0xFF; total++;
598 archive_set_error(&self->archive->archive,
600 "Insufficient compressed data");
601 return (ARCHIVE_FATAL);
605 if (len - nl == 3 && memcmp(b, "end ", 3) == 0)
606 uudecode->state = ST_FIND_HEAD;
608 archive_set_error(&self->archive->archive,
610 "Insufficient compressed data");
611 return (ARCHIVE_FATAL);
615 if (total + len * 2 > OUT_BUFF_SIZE)
618 if (l >= 3 && b[0] == '=' && b[1] == '=' &&
620 uudecode->state = ST_FIND_HEAD;
626 if (!base64[b[0]] || !base64[b[1]])
628 n = base64num[*b++] << 18;
629 n |= base64num[*b++] << 12;
630 *out++ = n >> 16; total++;
638 n |= base64num[*b++] << 6;
639 *out++ = (n >> 8) & 0xFF; total++;
647 n |= base64num[*b++];
648 *out++ = n & 0xFF; total++;
652 if (l && *b != '=') {
653 archive_set_error(&self->archive->archive,
655 "Insufficient compressed data");
656 return (ARCHIVE_FATAL);
662 if (ravail < avail_in)
663 used -= avail_in - ravail;
664 __archive_read_filter_consume(self->upstream, used);
666 *buff = uudecode->out_buff;
667 uudecode->total += total;
672 uudecode_filter_close(struct archive_read_filter *self)
674 struct uudecode *uudecode;
676 uudecode = (struct uudecode *)self->data;
677 free(uudecode->in_buff);
678 free(uudecode->out_buff);