diff options
Diffstat (limited to 'contrib/libarchive/libarchive/archive_write_set_format_shar.c')
-rw-r--r-- | contrib/libarchive/libarchive/archive_write_set_format_shar.c | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/contrib/libarchive/libarchive/archive_write_set_format_shar.c b/contrib/libarchive/libarchive/archive_write_set_format_shar.c index bd6276846658..02367d423849 100644 --- a/contrib/libarchive/libarchive/archive_write_set_format_shar.c +++ b/contrib/libarchive/libarchive/archive_write_set_format_shar.c @@ -59,8 +59,8 @@ struct shar { struct archive_string quoted_name; }; -static int archive_write_shar_finish(struct archive_write *); -static int archive_write_shar_destroy(struct archive_write *); +static int archive_write_shar_close(struct archive_write *); +static int archive_write_shar_free(struct archive_write *); static int archive_write_shar_header(struct archive_write *, struct archive_entry *); static ssize_t archive_write_shar_data_sed(struct archive_write *, @@ -106,9 +106,12 @@ archive_write_set_format_shar(struct archive *_a) struct archive_write *a = (struct archive_write *)_a; struct shar *shar; + archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, + ARCHIVE_STATE_NEW, "archive_write_set_format_shar"); + /* If someone else was already registered, unregister them. */ - if (a->format_destroy != NULL) - (a->format_destroy)(a); + if (a->format_free != NULL) + (a->format_free)(a); shar = (struct shar *)malloc(sizeof(*shar)); if (shar == NULL) { @@ -119,12 +122,10 @@ archive_write_set_format_shar(struct archive *_a) archive_string_init(&shar->work); archive_string_init(&shar->quoted_name); a->format_data = shar; - - a->pad_uncompressed = 0; a->format_name = "shar"; a->format_write_header = archive_write_shar_header; - a->format_finish = archive_write_shar_finish; - a->format_destroy = archive_write_shar_destroy; + a->format_close = archive_write_shar_close; + a->format_free = archive_write_shar_free; a->format_write_data = archive_write_shar_data_sed; a->format_finish_entry = archive_write_shar_finish_entry; a->archive.archive_format = ARCHIVE_FORMAT_SHAR_BASE; @@ -266,12 +267,12 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry) shar->quoted_name.s, shar->quoted_name.s); } else { if (shar->dump) { + unsigned int mode = archive_entry_mode(entry) & 0777; archive_string_sprintf(&shar->work, "uudecode -p > %s << 'SHAR_END'\n", shar->quoted_name.s); archive_string_sprintf(&shar->work, - "begin %o ", - archive_entry_mode(entry) & 0777); + "begin %o ", mode); shar_quote(&shar->work, name, 0); archive_strcat(&shar->work, "\n"); } else { @@ -308,15 +309,15 @@ archive_write_shar_header(struct archive_write *a, struct archive_entry *entry) break; case AE_IFCHR: archive_string_sprintf(&shar->work, - "mknod %s c %d %d\n", shar->quoted_name.s, - archive_entry_rdevmajor(entry), - archive_entry_rdevminor(entry)); + "mknod %s c %ju %ju\n", shar->quoted_name.s, + (uintmax_t)archive_entry_rdevmajor(entry), + (uintmax_t)archive_entry_rdevminor(entry)); break; case AE_IFBLK: archive_string_sprintf(&shar->work, - "mknod %s b %d %d\n", shar->quoted_name.s, - archive_entry_rdevmajor(entry), - archive_entry_rdevminor(entry)); + "mknod %s b %ju %ju\n", shar->quoted_name.s, + (uintmax_t)archive_entry_rdevmajor(entry), + (uintmax_t)archive_entry_rdevminor(entry)); break; default: return (ARCHIVE_WARN); @@ -349,11 +350,13 @@ archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n) * twice before entering the loop, so make sure three additional * bytes can be written. */ - if (archive_string_ensure(&shar->work, ensured + 3) == NULL) - __archive_errx(1, "Out of memory"); + if (archive_string_ensure(&shar->work, ensured + 3) == NULL) { + archive_set_error(&a->archive, ENOMEM, "Out of memory"); + return (ARCHIVE_FATAL); + } if (shar->work.length > ensured) { - ret = (*a->compressor.write)(a, shar->work.s, + ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); @@ -377,7 +380,7 @@ archive_write_shar_data_sed(struct archive_write *a, const void *buff, size_t n) if (buf >= buf_end) { shar->work.length = buf - shar->work.s; - ret = (*a->compressor.write)(a, shar->work.s, + ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); @@ -406,16 +409,18 @@ uuencode_group(const char _in[3], char out[4]) out[3] = UUENC( 0x3f & t ); } -static void -uuencode_line(struct shar *shar, const char *inbuf, size_t len) +static int +_uuencode_line(struct archive_write *a, struct shar *shar, const char *inbuf, size_t len) { - char tmp_buf[3], *buf; + char *buf; size_t alloc_len; /* len <= 45 -> expanded to 60 + len byte + new line */ alloc_len = shar->work.length + 62; - if (archive_string_ensure(&shar->work, alloc_len) == NULL) - __archive_errx(1, "Out of memory"); + if (archive_string_ensure(&shar->work, alloc_len) == NULL) { + archive_set_error(&a->archive, ENOMEM, "Out of memory"); + return (ARCHIVE_FATAL); + } buf = shar->work.s + shar->work.length; *buf++ = UUENC(len); @@ -426,21 +431,33 @@ uuencode_line(struct shar *shar, const char *inbuf, size_t len) buf += 4; } if (len != 0) { + char tmp_buf[3]; tmp_buf[0] = inbuf[0]; if (len == 1) tmp_buf[1] = '\0'; else tmp_buf[1] = inbuf[1]; tmp_buf[2] = '\0'; - uuencode_group(inbuf, buf); + uuencode_group(tmp_buf, buf); buf += 4; } *buf++ = '\n'; - if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) - __archive_errx(1, "Buffer overflow"); + if ((buf - shar->work.s) > (ptrdiff_t)(shar->work.length + 62)) { + archive_set_error(&a->archive, + ARCHIVE_ERRNO_MISC, "Buffer overflow"); + return (ARCHIVE_FATAL); + } shar->work.length = buf - shar->work.s; + return (ARCHIVE_OK); } +#define uuencode_line(__a, __shar, __inbuf, __len) \ + do { \ + int r = _uuencode_line(__a, __shar, __inbuf, __len); \ + if (r != ARCHIVE_OK) \ + return (ARCHIVE_FATAL); \ + } while (0) + static ssize_t archive_write_shar_data_uuencode(struct archive_write *a, const void *buff, size_t length) @@ -464,7 +481,7 @@ archive_write_shar_data_uuencode(struct archive_write *a, const void *buff, shar->outpos += n; return length; } - uuencode_line(shar, shar->outbuff, 45); + uuencode_line(a, shar, shar->outbuff, 45); src += n; n = length - n; } else { @@ -472,13 +489,13 @@ archive_write_shar_data_uuencode(struct archive_write *a, const void *buff, } while (n >= 45) { - uuencode_line(shar, src, 45); + uuencode_line(a, shar, src, 45); src += 45; n -= 45; if (shar->work.length < 65536) continue; - ret = (*a->compressor.write)(a, shar->work.s, + ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); @@ -506,7 +523,7 @@ archive_write_shar_finish_entry(struct archive_write *a) /* Finish uuencoded data. */ if (shar->has_data) { if (shar->outpos > 0) - uuencode_line(shar, shar->outbuff, + uuencode_line(a, shar, shar->outbuff, shar->outpos); archive_strcat(&shar->work, "`\nend\n"); archive_strcat(&shar->work, "SHAR_END\n"); @@ -517,7 +534,7 @@ archive_write_shar_finish_entry(struct archive_write *a) * directories; defer that to end of script. */ archive_string_sprintf(&shar->work, "chmod %o ", - archive_entry_mode(shar->entry) & 07777); + (unsigned int)(archive_entry_mode(shar->entry) & 07777)); shar_quote(&shar->work, archive_entry_pathname(shar->entry), 1); archive_strcat(&shar->work, "\n"); @@ -560,7 +577,7 @@ archive_write_shar_finish_entry(struct archive_write *a) if (shar->work.length < 65536) return (ARCHIVE_OK); - ret = (*a->compressor.write)(a, shar->work.s, shar->work.length); + ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); archive_string_empty(&shar->work); @@ -569,7 +586,7 @@ archive_write_shar_finish_entry(struct archive_write *a) } static int -archive_write_shar_finish(struct archive_write *a) +archive_write_shar_close(struct archive_write *a) { struct shar *shar; int ret; @@ -592,7 +609,7 @@ archive_write_shar_finish(struct archive_write *a) archive_strcat(&shar->work, "exit\n"); - ret = (*a->compressor.write)(a, shar->work.s, shar->work.length); + ret = __archive_write_output(a, shar->work.s, shar->work.length); if (ret != ARCHIVE_OK) return (ARCHIVE_FATAL); @@ -607,7 +624,7 @@ archive_write_shar_finish(struct archive_write *a) } static int -archive_write_shar_destroy(struct archive_write *a) +archive_write_shar_free(struct archive_write *a) { struct shar *shar; |