2 * Copyright (c) 2012 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"
28 __FBSDID("$FreeBSD$");
41 #include "archive_private.h"
42 #include "archive_string.h"
43 #include "archive_write_private.h"
47 struct private_uuencode {
49 struct archive_string name;
50 struct archive_string encoded_buff;
53 unsigned char hold[LBYTES];
56 static int archive_filter_uuencode_options(struct archive_write_filter *,
57 const char *, const char *);
58 static int archive_filter_uuencode_open(struct archive_write_filter *);
59 static int archive_filter_uuencode_write(struct archive_write_filter *,
60 const void *, size_t);
61 static int archive_filter_uuencode_close(struct archive_write_filter *);
62 static int archive_filter_uuencode_free(struct archive_write_filter *);
63 static void uu_encode(struct archive_string *, const unsigned char *, size_t);
64 static int64_t atol8(const char *, size_t);
67 * Add a compress filter to this write handle.
70 archive_write_add_filter_uuencode(struct archive *_a)
72 struct archive_write *a = (struct archive_write *)_a;
73 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
74 struct private_uuencode *state;
76 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
77 ARCHIVE_STATE_NEW, "archive_write_add_filter_uu");
79 state = (struct private_uuencode *)calloc(1, sizeof(*state));
81 archive_set_error(f->archive, ENOMEM,
82 "Can't allocate data for uuencode filter");
83 return (ARCHIVE_FATAL);
85 archive_strcpy(&state->name, "-");
90 f->code = ARCHIVE_FILTER_UU;
91 f->open = archive_filter_uuencode_open;
92 f->options = archive_filter_uuencode_options;
93 f->write = archive_filter_uuencode_write;
94 f->close = archive_filter_uuencode_close;
95 f->free = archive_filter_uuencode_free;
104 archive_filter_uuencode_options(struct archive_write_filter *f, const char *key,
107 struct private_uuencode *state = (struct private_uuencode *)f->data;
109 if (strcmp(key, "mode") == 0) {
111 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
112 "mode option requires octal digits");
113 return (ARCHIVE_FAILED);
115 state->mode = (int)atol8(value, strlen(value)) & 0777;
117 } else if (strcmp(key, "name") == 0) {
119 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
120 "name option requires a string");
121 return (ARCHIVE_FAILED);
123 archive_strcpy(&state->name, value);
127 /* Note: The "warn" return is just to inform the options
128 * supervisor that we didn't handle it. It will generate
129 * a suitable error if no one used this option. */
130 return (ARCHIVE_WARN);
137 archive_filter_uuencode_open(struct archive_write_filter *f)
139 struct private_uuencode *state = (struct private_uuencode *)f->data;
140 size_t bs = 65536, bpb;
143 ret = __archive_write_open_filter(f->next_filter);
144 if (ret != ARCHIVE_OK)
147 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
148 /* Buffer size should be a multiple number of the of bytes
149 * per block for performance. */
150 bpb = archive_write_get_bytes_per_block(f->archive);
158 if (archive_string_ensure(&state->encoded_buff, bs + 512) == NULL) {
159 archive_set_error(f->archive, ENOMEM,
160 "Can't allocate data for uuencode buffer");
161 return (ARCHIVE_FATAL);
164 archive_string_sprintf(&state->encoded_buff, "begin %o %s\n",
165 state->mode, state->name.s);
172 uu_encode(struct archive_string *as, const unsigned char *p, size_t len)
177 archive_strappend_char(as, c?c + 0x20:'`');
178 for (; len >= 3; p += 3, len -= 3) {
180 archive_strappend_char(as, c?c + 0x20:'`');
181 c = ((p[0] & 0x03) << 4) | ((p[1] & 0xf0) >> 4);
182 archive_strappend_char(as, c?c + 0x20:'`');
183 c = ((p[1] & 0x0f) << 2) | ((p[2] & 0xc0) >> 6);
184 archive_strappend_char(as, c?c + 0x20:'`');
186 archive_strappend_char(as, c?c + 0x20:'`');
190 archive_strappend_char(as, c?c + 0x20:'`');
191 c = (p[0] & 0x03) << 4;
193 archive_strappend_char(as, c?c + 0x20:'`');
194 archive_strappend_char(as, '`');
195 archive_strappend_char(as, '`');
197 c |= (p[1] & 0xf0) >> 4;
198 archive_strappend_char(as, c?c + 0x20:'`');
199 c = (p[1] & 0x0f) << 2;
200 archive_strappend_char(as, c?c + 0x20:'`');
201 archive_strappend_char(as, '`');
204 archive_strappend_char(as, '\n');
208 * Write data to the encoded stream.
211 archive_filter_uuencode_write(struct archive_write_filter *f, const void *buff,
214 struct private_uuencode *state = (struct private_uuencode *)f->data;
215 const unsigned char *p = buff;
216 int ret = ARCHIVE_OK;
221 if (state->hold_len) {
222 while (state->hold_len < LBYTES && length > 0) {
223 state->hold[state->hold_len++] = *p++;
226 if (state->hold_len < LBYTES)
228 uu_encode(&state->encoded_buff, state->hold, LBYTES);
232 for (; length >= LBYTES; length -= LBYTES, p += LBYTES)
233 uu_encode(&state->encoded_buff, p, LBYTES);
235 /* Save remaining bytes. */
237 memcpy(state->hold, p, length);
238 state->hold_len = length;
240 while (archive_strlen(&state->encoded_buff) >= state->bs) {
241 ret = __archive_write_filter(f->next_filter,
242 state->encoded_buff.s, state->bs);
243 memmove(state->encoded_buff.s,
244 state->encoded_buff.s + state->bs,
245 state->encoded_buff.length - state->bs);
246 state->encoded_buff.length -= state->bs;
254 * Finish the compression...
257 archive_filter_uuencode_close(struct archive_write_filter *f)
259 struct private_uuencode *state = (struct private_uuencode *)f->data;
262 /* Flush remaining bytes. */
263 if (state->hold_len != 0)
264 uu_encode(&state->encoded_buff, state->hold, state->hold_len);
265 archive_string_sprintf(&state->encoded_buff, "`\nend\n");
266 /* Write the last block */
267 archive_write_set_bytes_in_last_block(f->archive, 1);
268 ret = __archive_write_filter(f->next_filter,
269 state->encoded_buff.s, archive_strlen(&state->encoded_buff));
270 ret2 = __archive_write_close_filter(f->next_filter);
277 archive_filter_uuencode_free(struct archive_write_filter *f)
279 struct private_uuencode *state = (struct private_uuencode *)f->data;
281 archive_string_free(&state->name);
282 archive_string_free(&state->encoded_buff);
288 atol8(const char *p, size_t char_cnt)
294 while (char_cnt-- > 0) {
295 if (*p >= '0' && *p <= '7')