2 * Copyright (c) 2011-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"
27 __FBSDID("$FreeBSD$");
45 #include "archive_crc32.h"
47 #include "archive_endian.h"
48 #include "archive_entry.h"
49 #include "archive_entry_locale.h"
50 #include "archive_ppmd7_private.h"
51 #include "archive_private.h"
52 #include "archive_rb.h"
53 #include "archive_string.h"
54 #include "archive_write_private.h"
55 #include "archive_write_set_format_private.h"
61 #define _7Z_LZMA1 0x030101
62 #define _7Z_LZMA2 0x21
63 #define _7Z_DEFLATE 0x040108
64 #define _7Z_BZIP2 0x040202
65 #define _7Z_PPMD 0x030401
68 * 7-Zip header property IDs.
72 #define kArchiveProperties 0x02
73 #define kAdditionalStreamsInfo 0x03
74 #define kMainStreamsInfo 0x04
75 #define kFilesInfo 0x05
76 #define kPackInfo 0x06
77 #define kUnPackInfo 0x07
78 #define kSubStreamsInfo 0x08
82 #define kCodersUnPackSize 0x0C
83 #define kNumUnPackStream 0x0D
84 #define kEmptyStream 0x0E
85 #define kEmptyFile 0x0F
91 #define kAttributes 0x15
92 #define kEncodedHeader 0x17
100 * A stream object of universal compressor.
103 const uint8_t *next_in;
116 int (*code) (struct archive *a,
117 struct la_zstream *lastrm,
118 enum la_zaction action);
119 int (*end)(struct archive *a,
120 struct la_zstream *lastrm);
123 #define PPMD7_DEFAULT_ORDER 6
124 #define PPMD7_DEFAULT_MEM_SIZE (1 << 24)
128 CPpmd7 ppmd7_context;
129 CPpmd7z_RangeEnc range_enc;
144 struct archive_rb_node rbnode;
148 uint8_t *utf16name;/* UTF16-LE name. */
151 #define MTIME_IS_SET (1<<0)
152 #define ATIME_IS_SET (1<<1)
153 #define CTIME_IS_SET (1<<2)
154 #define CRC32_IS_SET (1<<3)
155 #define HAS_STREAM (1<<4)
173 uint64_t temp_offset;
175 struct file *cur_file;
176 size_t total_number_entry;
177 size_t total_number_nonempty_entry;
178 size_t total_number_empty_entry;
179 size_t total_number_dir_entry;
180 size_t total_bytes_entry_name;
181 size_t total_number_time_defined[3];
182 uint64_t total_bytes_compressed;
183 uint64_t total_bytes_uncompressed;
184 uint64_t entry_bytes_remaining;
185 uint32_t entry_crc32;
186 uint32_t precode_crc32;
187 uint32_t encoded_crc32;
189 #define PRECODE_CRC32 1
190 #define ENCODED_CRC32 2
192 unsigned opt_compression;
193 int opt_compression_level;
195 struct la_zstream stream;
198 struct archive_string_conv *sconv;
201 * Compressed data buffer.
203 unsigned char wbuff[512 * 20 * 6];
204 size_t wbuff_remaining;
207 * The list of the file entries which has its contents is used to
208 * manage struct file objects.
209 * We use 'next' (a member of struct file) to chain.
214 } file_list, empty_list;
215 struct archive_rb_tree rbtree;/* for empty files */
218 static int _7z_options(struct archive_write *,
219 const char *, const char *);
220 static int _7z_write_header(struct archive_write *,
221 struct archive_entry *);
222 static ssize_t _7z_write_data(struct archive_write *,
223 const void *, size_t);
224 static int _7z_finish_entry(struct archive_write *);
225 static int _7z_close(struct archive_write *);
226 static int _7z_free(struct archive_write *);
227 static int file_cmp_node(const struct archive_rb_node *,
228 const struct archive_rb_node *);
229 static int file_cmp_key(const struct archive_rb_node *, const void *);
230 static int file_new(struct archive_write *a, struct archive_entry *,
232 static void file_free(struct file *);
233 static void file_register(struct _7zip *, struct file *);
234 static void file_register_empty(struct _7zip *, struct file *);
235 static void file_init_register(struct _7zip *);
236 static void file_init_register_empty(struct _7zip *);
237 static void file_free_register(struct _7zip *);
238 static ssize_t compress_out(struct archive_write *, const void *, size_t ,
240 static int compression_init_encoder_copy(struct archive *,
241 struct la_zstream *);
242 static int compression_code_copy(struct archive *,
243 struct la_zstream *, enum la_zaction);
244 static int compression_end_copy(struct archive *, struct la_zstream *);
245 static int compression_init_encoder_deflate(struct archive *,
246 struct la_zstream *, int, int);
248 static int compression_code_deflate(struct archive *,
249 struct la_zstream *, enum la_zaction);
250 static int compression_end_deflate(struct archive *, struct la_zstream *);
252 static int compression_init_encoder_bzip2(struct archive *,
253 struct la_zstream *, int);
254 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
255 static int compression_code_bzip2(struct archive *,
256 struct la_zstream *, enum la_zaction);
257 static int compression_end_bzip2(struct archive *, struct la_zstream *);
259 static int compression_init_encoder_lzma1(struct archive *,
260 struct la_zstream *, int);
261 static int compression_init_encoder_lzma2(struct archive *,
262 struct la_zstream *, int);
263 #if defined(HAVE_LZMA_H)
264 static int compression_code_lzma(struct archive *,
265 struct la_zstream *, enum la_zaction);
266 static int compression_end_lzma(struct archive *, struct la_zstream *);
268 static int compression_init_encoder_ppmd(struct archive *,
269 struct la_zstream *, unsigned, uint32_t);
270 static int compression_code_ppmd(struct archive *,
271 struct la_zstream *, enum la_zaction);
272 static int compression_end_ppmd(struct archive *, struct la_zstream *);
273 static int _7z_compression_init_encoder(struct archive_write *, unsigned,
275 static int compression_code(struct archive *,
276 struct la_zstream *, enum la_zaction);
277 static int compression_end(struct archive *,
278 struct la_zstream *);
279 static int enc_uint64(struct archive_write *, uint64_t);
280 static int make_header(struct archive_write *, uint64_t, uint64_t,
281 uint64_t, int, struct coder *);
282 static int make_streamsInfo(struct archive_write *, uint64_t, uint64_t,
283 uint64_t, int, struct coder *, int, uint32_t);
286 archive_write_set_format_7zip(struct archive *_a)
288 static const struct archive_rb_tree_ops rb_ops = {
289 file_cmp_node, file_cmp_key
291 struct archive_write *a = (struct archive_write *)_a;
294 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
295 ARCHIVE_STATE_NEW, "archive_write_set_format_7zip");
297 /* If another format was already registered, unregister it. */
298 if (a->format_free != NULL)
301 zip = calloc(1, sizeof(*zip));
303 archive_set_error(&a->archive, ENOMEM,
304 "Can't allocate 7-Zip data");
305 return (ARCHIVE_FATAL);
308 __archive_rb_tree_init(&(zip->rbtree), &rb_ops);
309 file_init_register(zip);
310 file_init_register_empty(zip);
312 /* Set default compression type and its level. */
314 zip->opt_compression = _7Z_LZMA1;
315 #elif defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
316 zip->opt_compression = _7Z_BZIP2;
317 #elif defined(HAVE_ZLIB_H)
318 zip->opt_compression = _7Z_DEFLATE;
320 zip->opt_compression = _7Z_COPY;
322 zip->opt_compression_level = 6;
324 a->format_data = zip;
326 a->format_name = "7zip";
327 a->format_options = _7z_options;
328 a->format_write_header = _7z_write_header;
329 a->format_write_data = _7z_write_data;
330 a->format_finish_entry = _7z_finish_entry;
331 a->format_close = _7z_close;
332 a->format_free = _7z_free;
333 a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
334 a->archive.archive_format_name = "7zip";
340 _7z_options(struct archive_write *a, const char *key, const char *value)
344 zip = (struct _7zip *)a->format_data;
346 if (strcmp(key, "compression") == 0) {
347 const char *name = NULL;
349 if (value == NULL || strcmp(value, "copy") == 0 ||
350 strcmp(value, "COPY") == 0 ||
351 strcmp(value, "store") == 0 ||
352 strcmp(value, "STORE") == 0)
353 zip->opt_compression = _7Z_COPY;
354 else if (strcmp(value, "deflate") == 0 ||
355 strcmp(value, "DEFLATE") == 0)
357 zip->opt_compression = _7Z_DEFLATE;
361 else if (strcmp(value, "bzip2") == 0 ||
362 strcmp(value, "BZIP2") == 0)
363 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
364 zip->opt_compression = _7Z_BZIP2;
368 else if (strcmp(value, "lzma1") == 0 ||
369 strcmp(value, "LZMA1") == 0)
371 zip->opt_compression = _7Z_LZMA1;
375 else if (strcmp(value, "lzma2") == 0 ||
376 strcmp(value, "LZMA2") == 0)
378 zip->opt_compression = _7Z_LZMA2;
382 else if (strcmp(value, "ppmd") == 0 ||
383 strcmp(value, "PPMD") == 0 ||
384 strcmp(value, "PPMd") == 0)
385 zip->opt_compression = _7Z_PPMD;
387 archive_set_error(&(a->archive),
389 "Unknown compression name: `%s'",
391 return (ARCHIVE_FAILED);
394 archive_set_error(&(a->archive),
396 "`%s' compression not supported "
399 return (ARCHIVE_FAILED);
403 if (strcmp(key, "compression-level") == 0) {
405 !(value[0] >= '0' && value[0] <= '9') ||
407 archive_set_error(&(a->archive),
409 "Illegal value `%s'",
411 return (ARCHIVE_FAILED);
413 zip->opt_compression_level = value[0] - '0';
417 /* Note: The "warn" return is just to inform the options
418 * supervisor that we didn't handle it. It will generate
419 * a suitable error if no one used this option. */
420 return (ARCHIVE_WARN);
424 _7z_write_header(struct archive_write *a, struct archive_entry *entry)
430 zip = (struct _7zip *)a->format_data;
431 zip->cur_file = NULL;
432 zip->entry_bytes_remaining = 0;
434 if (zip->sconv == NULL) {
435 zip->sconv = archive_string_conversion_to_charset(
436 &a->archive, "UTF-16LE", 1);
437 if (zip->sconv == NULL)
438 return (ARCHIVE_FATAL);
441 r = file_new(a, entry, &file);
442 if (r < ARCHIVE_WARN) {
447 if (file->size == 0 && file->dir) {
448 if (!__archive_rb_tree_insert_node(&(zip->rbtree),
449 (struct archive_rb_node *)file)) {
450 /* We have already had the same file. */
456 if (file->flg & MTIME_IS_SET)
457 zip->total_number_time_defined[MTIME]++;
458 if (file->flg & CTIME_IS_SET)
459 zip->total_number_time_defined[CTIME]++;
460 if (file->flg & ATIME_IS_SET)
461 zip->total_number_time_defined[ATIME]++;
463 zip->total_number_entry++;
464 zip->total_bytes_entry_name += file->name_len + 2;
465 if (file->size == 0) {
466 /* Count up the number of empty files. */
467 zip->total_number_empty_entry++;
469 zip->total_number_dir_entry++;
471 file_register_empty(zip, file);
478 if ((zip->total_number_entry - zip->total_number_empty_entry) == 1) {
479 r = _7z_compression_init_encoder(a, zip->opt_compression,
480 zip->opt_compression_level);
483 return (ARCHIVE_FATAL);
487 /* Register a non-empty file. */
488 file_register(zip, file);
491 * Set the current file to cur_file to read its contents.
493 zip->cur_file = file;
496 /* Save a offset of current file in temporary file. */
497 zip->entry_bytes_remaining = file->size;
498 zip->entry_crc32 = 0;
501 * Store a symbolic link name as file contents.
503 if (archive_entry_filetype(entry) == AE_IFLNK) {
505 const void *p = (const void *)archive_entry_symlink(entry);
506 bytes = compress_out(a, p, (size_t)file->size, ARCHIVE_Z_RUN);
509 zip->entry_crc32 = crc32(zip->entry_crc32, p, (unsigned)bytes);
510 zip->entry_bytes_remaining -= bytes;
517 * Write data to a temporary file.
520 write_to_temp(struct archive_write *a, const void *buff, size_t s)
523 const unsigned char *p;
526 zip = (struct _7zip *)a->format_data;
529 * Open a temporary file.
531 if (zip->temp_fd == -1) {
532 zip->temp_offset = 0;
533 zip->temp_fd = __archive_mktemp(NULL);
534 if (zip->temp_fd < 0) {
535 archive_set_error(&a->archive, errno,
536 "Couldn't create temporary file");
537 return (ARCHIVE_FATAL);
541 p = (const unsigned char *)buff;
543 ws = write(zip->temp_fd, p, s);
545 archive_set_error(&(a->archive), errno,
546 "fwrite function failed");
547 return (ARCHIVE_FATAL);
551 zip->temp_offset += ws;
557 compress_out(struct archive_write *a, const void *buff, size_t s,
560 struct _7zip *zip = (struct _7zip *)a->format_data;
563 if (run == ARCHIVE_Z_FINISH && zip->stream.total_in == 0 && s == 0)
566 if ((zip->crc32flg & PRECODE_CRC32) && s)
567 zip->precode_crc32 = crc32(zip->precode_crc32, buff,
569 zip->stream.next_in = (const unsigned char *)buff;
570 zip->stream.avail_in = s;
572 /* Compress file data. */
573 r = compression_code(&(a->archive), &(zip->stream), run);
574 if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
575 return (ARCHIVE_FATAL);
576 if (zip->stream.avail_out == 0) {
577 if (write_to_temp(a, zip->wbuff, sizeof(zip->wbuff))
579 return (ARCHIVE_FATAL);
580 zip->stream.next_out = zip->wbuff;
581 zip->stream.avail_out = sizeof(zip->wbuff);
582 if (zip->crc32flg & ENCODED_CRC32)
583 zip->encoded_crc32 = crc32(zip->encoded_crc32,
584 zip->wbuff, sizeof(zip->wbuff));
585 if (run == ARCHIVE_Z_FINISH && r != ARCHIVE_EOF)
588 if (zip->stream.avail_in == 0)
591 if (run == ARCHIVE_Z_FINISH) {
592 uint64_t bytes = sizeof(zip->wbuff) - zip->stream.avail_out;
593 if (write_to_temp(a, zip->wbuff, (size_t)bytes) != ARCHIVE_OK)
594 return (ARCHIVE_FATAL);
595 if ((zip->crc32flg & ENCODED_CRC32) && bytes)
596 zip->encoded_crc32 = crc32(zip->encoded_crc32,
597 zip->wbuff, (unsigned)bytes);
604 _7z_write_data(struct archive_write *a, const void *buff, size_t s)
609 zip = (struct _7zip *)a->format_data;
611 if (s > zip->entry_bytes_remaining)
612 s = (size_t)zip->entry_bytes_remaining;
613 if (s == 0 || zip->cur_file == NULL)
615 bytes = compress_out(a, buff, s, ARCHIVE_Z_RUN);
618 zip->entry_crc32 = crc32(zip->entry_crc32, buff, (unsigned)bytes);
619 zip->entry_bytes_remaining -= bytes;
624 _7z_finish_entry(struct archive_write *a)
630 zip = (struct _7zip *)a->format_data;
631 if (zip->cur_file == NULL)
634 while (zip->entry_bytes_remaining > 0) {
635 s = (size_t)zip->entry_bytes_remaining;
636 if (s > a->null_length)
638 r = _7z_write_data(a, a->nulls, s);
642 zip->total_bytes_compressed += zip->stream.total_in;
643 zip->total_bytes_uncompressed += zip->stream.total_out;
644 zip->cur_file->crc32 = zip->entry_crc32;
645 zip->cur_file = NULL;
651 flush_wbuff(struct archive_write *a)
657 zip = (struct _7zip *)a->format_data;
658 s = sizeof(zip->wbuff) - zip->wbuff_remaining;
659 r = __archive_write_output(a, zip->wbuff, s);
662 zip->wbuff_remaining = sizeof(zip->wbuff);
667 copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
672 zip = (struct _7zip *)a->format_data;
673 if (zip->temp_offset > 0 &&
674 lseek(zip->temp_fd, offset, SEEK_SET) < 0) {
675 archive_set_error(&(a->archive), errno, "lseek failed");
676 return (ARCHIVE_FATAL);
683 if (length > zip->wbuff_remaining)
684 rsize = zip->wbuff_remaining;
686 rsize = (size_t)length;
687 wb = zip->wbuff + (sizeof(zip->wbuff) - zip->wbuff_remaining);
688 rs = read(zip->temp_fd, wb, rsize);
690 archive_set_error(&(a->archive), errno,
691 "Can't read temporary file(%jd)",
693 return (ARCHIVE_FATAL);
696 archive_set_error(&(a->archive), 0,
697 "Truncated 7-Zip archive");
698 return (ARCHIVE_FATAL);
700 zip->wbuff_remaining -= rs;
702 if (zip->wbuff_remaining == 0) {
712 _7z_close(struct archive_write *a)
716 uint64_t header_offset, header_size, header_unpacksize;
718 uint32_t header_crc32;
721 zip = (struct _7zip *)a->format_data;
723 if (zip->total_number_entry > 0) {
724 struct archive_rb_node *n;
725 uint64_t data_offset, data_size, data_unpacksize;
726 unsigned header_compression;
728 r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
732 data_size = zip->stream.total_out;
733 data_unpacksize = zip->stream.total_in;
734 zip->coder.codec = zip->opt_compression;
735 zip->coder.prop_size = zip->stream.prop_size;
736 zip->coder.props = zip->stream.props;
737 zip->stream.prop_size = 0;
738 zip->stream.props = NULL;
739 zip->total_number_nonempty_entry =
740 zip->total_number_entry - zip->total_number_empty_entry;
742 /* Connect an empty file list. */
743 if (zip->empty_list.first != NULL) {
744 *zip->file_list.last = zip->empty_list.first;
745 zip->file_list.last = zip->empty_list.last;
747 /* Connect a directory file list. */
748 ARCHIVE_RB_TREE_FOREACH(n, &(zip->rbtree)) {
749 file_register(zip, (struct file *)n);
753 * NOTE: 7z command supports just LZMA1, LZMA2 and COPY for
754 * the compression type for encoding the header.
757 header_compression = _7Z_LZMA1;
758 if(zip->opt_compression == _7Z_LZMA2 ||
759 zip->opt_compression == _7Z_COPY)
760 header_compression = zip->opt_compression;
762 /* If the stored file is only one, do not encode the header.
763 * This is the same way 7z command does. */
764 if (zip->total_number_entry == 1)
765 header_compression = _7Z_COPY;
767 header_compression = _7Z_COPY;
769 r = _7z_compression_init_encoder(a, header_compression,
770 zip->opt_compression_level);
773 zip->crc32flg = PRECODE_CRC32;
774 zip->precode_crc32 = 0;
775 r = make_header(a, data_offset, data_size, data_unpacksize,
779 r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
782 header_offset = data_offset + data_size;
783 header_size = zip->stream.total_out;
784 header_crc32 = zip->precode_crc32;
785 header_unpacksize = zip->stream.total_in;
787 if (header_compression != _7Z_COPY) {
789 * Encode the header in order to reduce the size
792 free(zip->coder.props);
793 zip->coder.codec = header_compression;
794 zip->coder.prop_size = zip->stream.prop_size;
795 zip->coder.props = zip->stream.props;
796 zip->stream.prop_size = 0;
797 zip->stream.props = NULL;
799 r = _7z_compression_init_encoder(a, _7Z_COPY, 0);
802 zip->crc32flg = ENCODED_CRC32;
803 zip->encoded_crc32 = 0;
806 * Make EncodedHeader.
808 r = enc_uint64(a, kEncodedHeader);
811 r = make_streamsInfo(a, header_offset, header_size,
812 header_unpacksize, 1, &(zip->coder), 0,
816 r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
819 header_offset = header_offset + header_size;
820 header_size = zip->stream.total_out;
821 header_crc32 = zip->encoded_crc32;
825 header_offset = header_size = 0;
829 length = zip->temp_offset;
832 * Make the zip header on wbuff(write buffer).
835 zip->wbuff_remaining = sizeof(zip->wbuff);
836 memcpy(&wb[0], "7z\xBC\xAF\x27\x1C", 6);
837 wb[6] = 0;/* Major version. */
838 wb[7] = 3;/* Minor version. */
839 archive_le64enc(&wb[12], header_offset);/* Next Header Offset */
840 archive_le64enc(&wb[20], header_size);/* Next Header Size */
841 archive_le32enc(&wb[28], header_crc32);/* Next Header CRC */
842 archive_le32enc(&wb[8], crc32(0, &wb[12], 20));/* Start Header CRC */
843 zip->wbuff_remaining -= 32;
846 * Read all file contents and an encoded header from the temporary
847 * file and write out it.
849 r = copy_out(a, 0, length);
857 * Encode 64 bits value into 7-Zip's encoded UINT64 value.
860 enc_uint64(struct archive_write *a, uint64_t val)
862 unsigned mask = 0x80;
867 for (i = 1; i < (int)sizeof(numdata); i++) {
869 numdata[0] |= (uint8_t)val;
872 numdata[i] = (uint8_t)val;
877 return ((int)compress_out(a, numdata, i, ARCHIVE_Z_RUN));
881 make_substreamsInfo(struct archive_write *a, struct coder *coders)
883 struct _7zip *zip = (struct _7zip *)a->format_data;
888 * Make SubStreamsInfo.
890 r = enc_uint64(a, kSubStreamsInfo);
894 if (zip->total_number_nonempty_entry > 1 && coders->codec != _7Z_COPY) {
896 * Make NumUnPackStream.
898 r = enc_uint64(a, kNumUnPackStream);
902 /* Write numUnpackStreams */
903 r = enc_uint64(a, zip->total_number_nonempty_entry);
910 r = enc_uint64(a, kSize);
913 file = zip->file_list.first;
914 for (;file != NULL; file = file->next) {
915 if (file->next == NULL ||
916 file->next->size == 0)
918 r = enc_uint64(a, file->size);
927 r = enc_uint64(a, kCRC);
932 /* All are defined */
933 r = enc_uint64(a, 1);
936 file = zip->file_list.first;
937 for (;file != NULL; file = file->next) {
941 archive_le32enc(crc, file->crc32);
942 r = (int)compress_out(a, crc, 4, ARCHIVE_Z_RUN);
948 r = enc_uint64(a, kEnd);
955 make_streamsInfo(struct archive_write *a, uint64_t offset, uint64_t pack_size,
956 uint64_t unpack_size, int num_coder, struct coder *coders, int substrm,
959 struct _7zip *zip = (struct _7zip *)a->format_data;
960 uint8_t codec_buff[8];
965 if (coders->codec == _7Z_COPY)
966 numFolders = (int)zip->total_number_nonempty_entry;
973 r = enc_uint64(a, kPackInfo);
978 r = enc_uint64(a, offset);
982 /* Write NumPackStreams. */
983 r = enc_uint64(a, numFolders);
988 r = enc_uint64(a, kSize);
992 if (numFolders > 1) {
993 struct file *file = zip->file_list.first;
994 for (;file != NULL; file = file->next) {
997 r = enc_uint64(a, file->size);
1003 r = enc_uint64(a, pack_size);
1008 r = enc_uint64(a, kEnd);
1015 r = enc_uint64(a, kUnPackInfo);
1022 r = enc_uint64(a, kFolder);
1026 /* Write NumFolders. */
1027 r = enc_uint64(a, numFolders);
1031 /* Write External. */
1032 r = enc_uint64(a, 0);
1036 for (fi = 0; fi < numFolders; fi++) {
1037 /* Write NumCoders. */
1038 r = enc_uint64(a, num_coder);
1042 for (i = 0; i < num_coder; i++) {
1043 unsigned codec_id = coders[i].codec;
1045 /* Write Codec flag. */
1046 archive_be64enc(codec_buff, codec_id);
1047 for (codec_size = 8; codec_size > 0; codec_size--) {
1048 if (codec_buff[8 - codec_size])
1051 if (codec_size == 0)
1053 if (coders[i].prop_size)
1054 r = enc_uint64(a, codec_size | 0x20);
1056 r = enc_uint64(a, codec_size);
1060 /* Write Codec ID. */
1062 r = (int)compress_out(a, &codec_buff[8-codec_size],
1063 codec_size, ARCHIVE_Z_RUN);
1067 if (coders[i].prop_size) {
1068 /* Write Codec property size. */
1069 r = enc_uint64(a, coders[i].prop_size);
1073 /* Write Codec properties. */
1074 r = (int)compress_out(a, coders[i].props,
1075 coders[i].prop_size, ARCHIVE_Z_RUN);
1083 * Make CodersUnPackSize.
1085 r = enc_uint64(a, kCodersUnPackSize);
1089 if (numFolders > 1) {
1090 struct file *file = zip->file_list.first;
1091 for (;file != NULL; file = file->next) {
1092 if (file->size == 0)
1094 r = enc_uint64(a, file->size);
1100 /* Write UnPackSize. */
1101 r = enc_uint64(a, unpack_size);
1111 r = enc_uint64(a, kCRC);
1115 /* All are defined */
1116 r = enc_uint64(a, 1);
1119 archive_le32enc(crc, header_crc);
1120 r = (int)compress_out(a, crc, 4, ARCHIVE_Z_RUN);
1126 r = enc_uint64(a, kEnd);
1132 * Make SubStreamsInfo.
1134 r = make_substreamsInfo(a, coders);
1141 r = enc_uint64(a, kEnd);
1145 return (ARCHIVE_OK);
1149 #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
1151 utcToFiletime(time_t t, long ns)
1156 fileTime *= 10000000;
1157 fileTime += ns / 100;
1158 fileTime += EPOC_TIME;
1163 make_time(struct archive_write *a, uint8_t type, unsigned flg, int ti)
1165 uint8_t filetime[8];
1166 struct _7zip *zip = (struct _7zip *)a->format_data;
1174 if (zip->total_number_time_defined[ti] == zip->total_number_entry) {
1175 /* Write Time Type. */
1176 r = enc_uint64(a, type);
1179 /* Write EmptyStream Size. */
1180 r = enc_uint64(a, 2 + zip->total_number_entry * 8);
1183 /* All are defined. */
1184 r = enc_uint64(a, 1);
1188 if (zip->total_number_time_defined[ti] == 0)
1189 return (ARCHIVE_OK);
1191 /* Write Time Type. */
1192 r = enc_uint64(a, type);
1195 /* Write EmptyStream Size. */
1196 r = enc_uint64(a, 2 + ((zip->total_number_entry + 7) >> 3)
1197 + zip->total_number_time_defined[ti] * 8);
1201 /* All are not defined. */
1202 r = enc_uint64(a, 0);
1208 file = zip->file_list.first;
1209 for (;file != NULL; file = file->next) {
1210 if (file->flg & flg)
1214 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1222 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1229 r = enc_uint64(a, 0);
1237 file = zip->file_list.first;
1238 for (;file != NULL; file = file->next) {
1239 if ((file->flg & flg) == 0)
1241 archive_le64enc(filetime, utcToFiletime(file->times[ti].time,
1242 file->times[ti].time_ns));
1243 r = (int)compress_out(a, filetime, 8, ARCHIVE_Z_RUN);
1248 return (ARCHIVE_OK);
1252 make_header(struct archive_write *a, uint64_t offset, uint64_t pack_size,
1253 uint64_t unpack_size, int codernum, struct coder *coders)
1255 struct _7zip *zip = (struct _7zip *)a->format_data;
1263 r = enc_uint64(a, kHeader);
1268 * If there are empty files only, do not write MainStreamInfo.
1270 if (zip->total_number_nonempty_entry) {
1272 * Make MainStreamInfo.
1274 r = enc_uint64(a, kMainStreamsInfo);
1277 r = make_streamsInfo(a, offset, pack_size, unpack_size,
1278 codernum, coders, 1, 0);
1286 r = enc_uint64(a, kFilesInfo);
1290 /* Write numFiles. */
1291 r = enc_uint64(a, zip->total_number_entry);
1295 if (zip->total_number_empty_entry > 0) {
1296 /* Make EmptyStream. */
1297 r = enc_uint64(a, kEmptyStream);
1301 /* Write EmptyStream Size. */
1302 r = enc_uint64(a, (zip->total_number_entry+7)>>3);
1308 file = zip->file_list.first;
1309 for (;file != NULL; file = file->next) {
1310 if (file->size == 0)
1314 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1322 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1328 if (zip->total_number_empty_entry > zip->total_number_dir_entry) {
1329 /* Make EmptyFile. */
1330 r = enc_uint64(a, kEmptyFile);
1334 /* Write EmptyFile Size. */
1335 r = enc_uint64(a, (zip->total_number_empty_entry + 7) >> 3);
1341 file = zip->file_list.first;
1342 for (;file != NULL; file = file->next) {
1349 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1357 r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
1364 r = enc_uint64(a, kName);
1368 /* Write Name size. */
1369 r = enc_uint64(a, zip->total_bytes_entry_name+1);
1373 /* Write dmy byte. */
1374 r = enc_uint64(a, 0);
1378 file = zip->file_list.first;
1379 for (;file != NULL; file = file->next) {
1380 r = (int)compress_out(a, file->utf16name, file->name_len+2,
1387 r = make_time(a, kMTime, MTIME_IS_SET, MTIME);
1392 r = make_time(a, kCTime, CTIME_IS_SET, CTIME);
1397 r = make_time(a, kATime, ATIME_IS_SET, ATIME);
1401 /* Make Attributes. */
1402 r = enc_uint64(a, kAttributes);
1406 /* Write Attributes size. */
1407 r = enc_uint64(a, 2 + zip->total_number_entry * 4);
1411 /* Write "All Are Defined". */
1412 r = enc_uint64(a, 1);
1416 /* Write dmy byte. */
1417 r = enc_uint64(a, 0);
1421 file = zip->file_list.first;
1422 for (;file != NULL; file = file->next) {
1424 * High 16bits is unix mode.
1425 * Low 16bits is Windows attributes.
1427 uint32_t encattr, attr;
1432 if ((file->mode & 0222) == 0)
1433 attr |= 1;/* Read Only. */
1434 attr |= ((uint32_t)file->mode) << 16;
1435 archive_le32enc(&encattr, attr);
1436 r = (int)compress_out(a, &encattr, 4, ARCHIVE_Z_RUN);
1442 r = enc_uint64(a, kEnd);
1447 r = enc_uint64(a, kEnd);
1451 return (ARCHIVE_OK);
1456 _7z_free(struct archive_write *a)
1458 struct _7zip *zip = (struct _7zip *)a->format_data;
1460 /* Close the temporary file. */
1461 if (zip->temp_fd >= 0)
1462 close(zip->temp_fd);
1464 file_free_register(zip);
1465 compression_end(&(a->archive), &(zip->stream));
1466 free(zip->coder.props);
1469 return (ARCHIVE_OK);
1473 file_cmp_node(const struct archive_rb_node *n1,
1474 const struct archive_rb_node *n2)
1476 const struct file *f1 = (const struct file *)n1;
1477 const struct file *f2 = (const struct file *)n2;
1479 if (f1->name_len == f2->name_len)
1480 return (memcmp(f1->utf16name, f2->utf16name, f1->name_len));
1481 return (f1->name_len > f2->name_len)?1:-1;
1485 file_cmp_key(const struct archive_rb_node *n, const void *key)
1487 const struct file *f = (const struct file *)n;
1489 return (f->name_len - *(const char *)key);
1493 file_new(struct archive_write *a, struct archive_entry *entry,
1494 struct file **newfile)
1500 int ret = ARCHIVE_OK;
1502 zip = (struct _7zip *)a->format_data;
1505 file = calloc(1, sizeof(*file));
1507 archive_set_error(&a->archive, ENOMEM,
1508 "Can't allocate memory");
1509 return (ARCHIVE_FATAL);
1512 if (0 > archive_entry_pathname_l(entry, &u16, &u16len, zip->sconv)) {
1513 if (errno == ENOMEM) {
1515 archive_set_error(&a->archive, ENOMEM,
1516 "Can't allocate memory for UTF-16LE");
1517 return (ARCHIVE_FATAL);
1519 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1520 "A filename cannot be converted to UTF-16LE;"
1521 "You should disable making Joliet extension");
1524 file->utf16name = malloc(u16len + 2);
1525 if (file->utf16name == NULL) {
1527 archive_set_error(&a->archive, ENOMEM,
1528 "Can't allocate memory for Name");
1529 return (ARCHIVE_FATAL);
1531 memcpy(file->utf16name, u16, u16len);
1532 file->utf16name[u16len+0] = 0;
1533 file->utf16name[u16len+1] = 0;
1534 file->name_len = (unsigned)u16len;
1535 file->mode = archive_entry_mode(entry);
1536 if (archive_entry_filetype(entry) == AE_IFREG)
1537 file->size = archive_entry_size(entry);
1539 archive_entry_set_size(entry, 0);
1540 if (archive_entry_filetype(entry) == AE_IFDIR)
1542 else if (archive_entry_filetype(entry) == AE_IFLNK)
1543 file->size = strlen(archive_entry_symlink(entry));
1544 if (archive_entry_mtime_is_set(entry)) {
1545 file->flg |= MTIME_IS_SET;
1546 file->times[MTIME].time = archive_entry_mtime(entry);
1547 file->times[MTIME].time_ns = archive_entry_mtime_nsec(entry);
1549 if (archive_entry_atime_is_set(entry)) {
1550 file->flg |= ATIME_IS_SET;
1551 file->times[ATIME].time = archive_entry_atime(entry);
1552 file->times[ATIME].time_ns = archive_entry_atime_nsec(entry);
1554 if (archive_entry_ctime_is_set(entry)) {
1555 file->flg |= CTIME_IS_SET;
1556 file->times[CTIME].time = archive_entry_ctime(entry);
1557 file->times[CTIME].time_ns = archive_entry_ctime_nsec(entry);
1565 file_free(struct file *file)
1567 free(file->utf16name);
1572 file_register(struct _7zip *zip, struct file *file)
1575 *zip->file_list.last = file;
1576 zip->file_list.last = &(file->next);
1580 file_init_register(struct _7zip *zip)
1582 zip->file_list.first = NULL;
1583 zip->file_list.last = &(zip->file_list.first);
1587 file_free_register(struct _7zip *zip)
1589 struct file *file, *file_next;
1591 file = zip->file_list.first;
1592 while (file != NULL) {
1593 file_next = file->next;
1600 file_register_empty(struct _7zip *zip, struct file *file)
1603 *zip->empty_list.last = file;
1604 zip->empty_list.last = &(file->next);
1608 file_init_register_empty(struct _7zip *zip)
1610 zip->empty_list.first = NULL;
1611 zip->empty_list.last = &(zip->empty_list.first);
1614 #if !defined(HAVE_ZLIB_H) || !defined(HAVE_BZLIB_H) ||\
1615 !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
1617 compression_unsupported_encoder(struct archive *a,
1618 struct la_zstream *lastrm, const char *name)
1621 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1622 "%s compression not supported on this platform", name);
1624 lastrm->real_stream = NULL;
1625 return (ARCHIVE_FAILED);
1630 * _7_COPY compressor.
1633 compression_init_encoder_copy(struct archive *a, struct la_zstream *lastrm)
1637 compression_end(a, lastrm);
1639 lastrm->code = compression_code_copy;
1640 lastrm->end = compression_end_copy;
1641 return (ARCHIVE_OK);
1645 compression_code_copy(struct archive *a,
1646 struct la_zstream *lastrm, enum la_zaction action)
1650 (void)a; /* UNUSED */
1651 if (lastrm->avail_out > lastrm->avail_in)
1652 bytes = lastrm->avail_in;
1654 bytes = lastrm->avail_out;
1656 memcpy(lastrm->next_out, lastrm->next_in, bytes);
1657 lastrm->next_in += bytes;
1658 lastrm->avail_in -= bytes;
1659 lastrm->total_in += bytes;
1660 lastrm->next_out += bytes;
1661 lastrm->avail_out -= bytes;
1662 lastrm->total_out += bytes;
1664 if (action == ARCHIVE_Z_FINISH && lastrm->avail_in == 0)
1665 return (ARCHIVE_EOF);
1666 return (ARCHIVE_OK);
1670 compression_end_copy(struct archive *a, struct la_zstream *lastrm)
1672 (void)a; /* UNUSED */
1674 return (ARCHIVE_OK);
1678 * _7_DEFLATE compressor.
1682 compression_init_encoder_deflate(struct archive *a,
1683 struct la_zstream *lastrm, int level, int withheader)
1688 compression_end(a, lastrm);
1689 strm = calloc(1, sizeof(*strm));
1691 archive_set_error(a, ENOMEM,
1692 "Can't allocate memory for gzip stream");
1693 return (ARCHIVE_FATAL);
1695 /* zlib.h is not const-correct, so we need this one bit
1696 * of ugly hackery to convert a const * pointer to
1697 * a non-const pointer. */
1698 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
1699 strm->avail_in = (uInt)lastrm->avail_in;
1700 strm->total_in = (uLong)lastrm->total_in;
1701 strm->next_out = lastrm->next_out;
1702 strm->avail_out = (uInt)lastrm->avail_out;
1703 strm->total_out = (uLong)lastrm->total_out;
1704 if (deflateInit2(strm, level, Z_DEFLATED,
1705 (withheader)?15:-15,
1706 8, Z_DEFAULT_STRATEGY) != Z_OK) {
1708 lastrm->real_stream = NULL;
1709 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1710 "Internal error initializing compression library");
1711 return (ARCHIVE_FATAL);
1713 lastrm->real_stream = strm;
1715 lastrm->code = compression_code_deflate;
1716 lastrm->end = compression_end_deflate;
1717 return (ARCHIVE_OK);
1721 compression_code_deflate(struct archive *a,
1722 struct la_zstream *lastrm, enum la_zaction action)
1727 strm = (z_stream *)lastrm->real_stream;
1728 /* zlib.h is not const-correct, so we need this one bit
1729 * of ugly hackery to convert a const * pointer to
1730 * a non-const pointer. */
1731 strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
1732 strm->avail_in = (uInt)lastrm->avail_in;
1733 strm->total_in = (uLong)lastrm->total_in;
1734 strm->next_out = lastrm->next_out;
1735 strm->avail_out = (uInt)lastrm->avail_out;
1736 strm->total_out = (uLong)lastrm->total_out;
1738 (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
1739 lastrm->next_in = strm->next_in;
1740 lastrm->avail_in = strm->avail_in;
1741 lastrm->total_in = strm->total_in;
1742 lastrm->next_out = strm->next_out;
1743 lastrm->avail_out = strm->avail_out;
1744 lastrm->total_out = strm->total_out;
1747 return (ARCHIVE_OK);
1749 return (ARCHIVE_EOF);
1751 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1752 "GZip compression failed:"
1753 " deflate() call returned status %d", r);
1754 return (ARCHIVE_FATAL);
1759 compression_end_deflate(struct archive *a, struct la_zstream *lastrm)
1764 strm = (z_stream *)lastrm->real_stream;
1765 r = deflateEnd(strm);
1767 lastrm->real_stream = NULL;
1770 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1771 "Failed to clean up compressor");
1772 return (ARCHIVE_FATAL);
1774 return (ARCHIVE_OK);
1778 compression_init_encoder_deflate(struct archive *a,
1779 struct la_zstream *lastrm, int level, int withheader)
1782 (void) level; /* UNUSED */
1783 (void) withheader; /* UNUSED */
1785 compression_end(a, lastrm);
1786 return (compression_unsupported_encoder(a, lastrm, "deflate"));
1791 * _7_BZIP2 compressor.
1793 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1795 compression_init_encoder_bzip2(struct archive *a,
1796 struct la_zstream *lastrm, int level)
1801 compression_end(a, lastrm);
1802 strm = calloc(1, sizeof(*strm));
1804 archive_set_error(a, ENOMEM,
1805 "Can't allocate memory for bzip2 stream");
1806 return (ARCHIVE_FATAL);
1808 /* bzlib.h is not const-correct, so we need this one bit
1809 * of ugly hackery to convert a const * pointer to
1810 * a non-const pointer. */
1811 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
1812 strm->avail_in = lastrm->avail_in;
1813 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
1814 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
1815 strm->next_out = (char *)lastrm->next_out;
1816 strm->avail_out = lastrm->avail_out;
1817 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
1818 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
1819 if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
1821 lastrm->real_stream = NULL;
1822 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1823 "Internal error initializing compression library");
1824 return (ARCHIVE_FATAL);
1826 lastrm->real_stream = strm;
1828 lastrm->code = compression_code_bzip2;
1829 lastrm->end = compression_end_bzip2;
1830 return (ARCHIVE_OK);
1834 compression_code_bzip2(struct archive *a,
1835 struct la_zstream *lastrm, enum la_zaction action)
1840 strm = (bz_stream *)lastrm->real_stream;
1841 /* bzlib.h is not const-correct, so we need this one bit
1842 * of ugly hackery to convert a const * pointer to
1843 * a non-const pointer. */
1844 strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
1845 strm->avail_in = lastrm->avail_in;
1846 strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
1847 strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
1848 strm->next_out = (char *)lastrm->next_out;
1849 strm->avail_out = lastrm->avail_out;
1850 strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
1851 strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
1852 r = BZ2_bzCompress(strm,
1853 (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
1854 lastrm->next_in = (const unsigned char *)strm->next_in;
1855 lastrm->avail_in = strm->avail_in;
1857 (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
1858 + (uint64_t)(uint32_t)strm->total_in_lo32;
1859 lastrm->next_out = (unsigned char *)strm->next_out;
1860 lastrm->avail_out = strm->avail_out;
1862 (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
1863 + (uint64_t)(uint32_t)strm->total_out_lo32;
1865 case BZ_RUN_OK: /* Non-finishing */
1866 case BZ_FINISH_OK: /* Finishing: There's more work to do */
1867 return (ARCHIVE_OK);
1868 case BZ_STREAM_END: /* Finishing: all done */
1869 /* Only occurs in finishing case */
1870 return (ARCHIVE_EOF);
1872 /* Any other return value indicates an error */
1873 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1874 "Bzip2 compression failed:"
1875 " BZ2_bzCompress() call returned status %d", r);
1876 return (ARCHIVE_FATAL);
1881 compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
1886 strm = (bz_stream *)lastrm->real_stream;
1887 r = BZ2_bzCompressEnd(strm);
1889 lastrm->real_stream = NULL;
1892 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1893 "Failed to clean up compressor");
1894 return (ARCHIVE_FATAL);
1896 return (ARCHIVE_OK);
1901 compression_init_encoder_bzip2(struct archive *a,
1902 struct la_zstream *lastrm, int level)
1905 (void) level; /* UNUSED */
1907 compression_end(a, lastrm);
1908 return (compression_unsupported_encoder(a, lastrm, "bzip2"));
1913 * _7_LZMA1, _7_LZMA2 compressor.
1915 #if defined(HAVE_LZMA_H)
1917 compression_init_encoder_lzma(struct archive *a,
1918 struct la_zstream *lastrm, int level, uint64_t filter_id)
1920 static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
1922 lzma_filter *lzmafilters;
1923 lzma_options_lzma lzma_opt;
1927 compression_end(a, lastrm);
1928 strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
1930 archive_set_error(a, ENOMEM,
1931 "Can't allocate memory for lzma stream");
1932 return (ARCHIVE_FATAL);
1934 lzmafilters = (lzma_filter *)(strm+1);
1937 if (lzma_lzma_preset(&lzma_opt, level)) {
1939 lastrm->real_stream = NULL;
1940 archive_set_error(a, ENOMEM,
1941 "Internal error initializing compression library");
1942 return (ARCHIVE_FATAL);
1944 lzmafilters[0].id = filter_id;
1945 lzmafilters[0].options = &lzma_opt;
1946 lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
1948 r = lzma_properties_size(&(lastrm->prop_size), lzmafilters);
1951 lastrm->real_stream = NULL;
1952 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1953 "lzma_properties_size failed");
1954 return (ARCHIVE_FATAL);
1956 if (lastrm->prop_size) {
1957 lastrm->props = malloc(lastrm->prop_size);
1958 if (lastrm->props == NULL) {
1960 lastrm->real_stream = NULL;
1961 archive_set_error(a, ENOMEM,
1962 "Cannot allocate memory");
1963 return (ARCHIVE_FATAL);
1965 r = lzma_properties_encode(lzmafilters, lastrm->props);
1968 lastrm->real_stream = NULL;
1969 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1970 "lzma_properties_encode failed");
1971 return (ARCHIVE_FATAL);
1975 *strm = lzma_init_data;
1976 r = lzma_raw_encoder(strm, lzmafilters);
1979 lastrm->real_stream = strm;
1981 lastrm->code = compression_code_lzma;
1982 lastrm->end = compression_end_lzma;
1985 case LZMA_MEM_ERROR:
1987 lastrm->real_stream = NULL;
1988 archive_set_error(a, ENOMEM,
1989 "Internal error initializing compression library: "
1990 "Cannot allocate memory");
1995 lastrm->real_stream = NULL;
1996 archive_set_error(a, ARCHIVE_ERRNO_MISC,
1997 "Internal error initializing compression library: "
1998 "It's a bug in liblzma");
2006 compression_init_encoder_lzma1(struct archive *a,
2007 struct la_zstream *lastrm, int level)
2009 return compression_init_encoder_lzma(a, lastrm, level,
2014 compression_init_encoder_lzma2(struct archive *a,
2015 struct la_zstream *lastrm, int level)
2017 return compression_init_encoder_lzma(a, lastrm, level,
2022 compression_code_lzma(struct archive *a,
2023 struct la_zstream *lastrm, enum la_zaction action)
2028 strm = (lzma_stream *)lastrm->real_stream;
2029 strm->next_in = lastrm->next_in;
2030 strm->avail_in = lastrm->avail_in;
2031 strm->total_in = lastrm->total_in;
2032 strm->next_out = lastrm->next_out;
2033 strm->avail_out = lastrm->avail_out;
2034 strm->total_out = lastrm->total_out;
2036 (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
2037 lastrm->next_in = strm->next_in;
2038 lastrm->avail_in = strm->avail_in;
2039 lastrm->total_in = strm->total_in;
2040 lastrm->next_out = strm->next_out;
2041 lastrm->avail_out = strm->avail_out;
2042 lastrm->total_out = strm->total_out;
2045 /* Non-finishing case */
2046 return (ARCHIVE_OK);
2047 case LZMA_STREAM_END:
2048 /* This return can only occur in finishing case. */
2049 return (ARCHIVE_EOF);
2050 case LZMA_MEMLIMIT_ERROR:
2051 archive_set_error(a, ENOMEM,
2052 "lzma compression error:"
2053 " %ju MiB would have been needed",
2054 (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
2056 return (ARCHIVE_FATAL);
2058 /* Any other return value indicates an error */
2059 archive_set_error(a, ARCHIVE_ERRNO_MISC,
2060 "lzma compression failed:"
2061 " lzma_code() call returned status %d", r);
2062 return (ARCHIVE_FATAL);
2067 compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
2071 (void)a; /* UNUSED */
2072 strm = (lzma_stream *)lastrm->real_stream;
2076 lastrm->real_stream = NULL;
2077 return (ARCHIVE_OK);
2081 compression_init_encoder_lzma1(struct archive *a,
2082 struct la_zstream *lastrm, int level)
2085 (void) level; /* UNUSED */
2087 compression_end(a, lastrm);
2088 return (compression_unsupported_encoder(a, lastrm, "lzma"));
2091 compression_init_encoder_lzma2(struct archive *a,
2092 struct la_zstream *lastrm, int level)
2095 (void) level; /* UNUSED */
2097 compression_end(a, lastrm);
2098 return (compression_unsupported_encoder(a, lastrm, "lzma"));
2103 * _7_PPMD compressor.
2106 ppmd_write(void *p, Byte b)
2108 struct archive_write *a = ((IByteOut *)p)->a;
2109 struct _7zip *zip = (struct _7zip *)(a->format_data);
2110 struct la_zstream *lastrm = &(zip->stream);
2111 struct ppmd_stream *strm;
2113 if (lastrm->avail_out) {
2114 *lastrm->next_out++ = b;
2115 lastrm->avail_out--;
2116 lastrm->total_out++;
2119 strm = (struct ppmd_stream *)lastrm->real_stream;
2120 if (strm->buff_ptr < strm->buff_end) {
2121 *strm->buff_ptr++ = b;
2127 compression_init_encoder_ppmd(struct archive *a,
2128 struct la_zstream *lastrm, unsigned maxOrder, uint32_t msize)
2130 struct ppmd_stream *strm;
2135 compression_end(a, lastrm);
2136 strm = calloc(1, sizeof(*strm));
2138 archive_set_error(a, ENOMEM,
2139 "Can't allocate memory for PPMd");
2140 return (ARCHIVE_FATAL);
2142 strm->buff = malloc(32);
2143 if (strm->buff == NULL) {
2145 archive_set_error(a, ENOMEM,
2146 "Can't allocate memory for PPMd");
2147 return (ARCHIVE_FATAL);
2149 strm->buff_ptr = strm->buff;
2150 strm->buff_end = strm->buff + 32;
2152 props = malloc(1+4);
2153 if (props == NULL) {
2156 archive_set_error(a, ENOMEM,
2157 "Coludn't allocate memory for PPMd");
2158 return (ARCHIVE_FATAL);
2160 props[0] = maxOrder;
2161 archive_le32enc(props+1, msize);
2162 __archive_ppmd7_functions.Ppmd7_Construct(&strm->ppmd7_context);
2163 r = __archive_ppmd7_functions.Ppmd7_Alloc(
2164 &strm->ppmd7_context, msize);
2169 archive_set_error(a, ENOMEM,
2170 "Coludn't allocate memory for PPMd");
2171 return (ARCHIVE_FATAL);
2173 __archive_ppmd7_functions.Ppmd7_Init(&(strm->ppmd7_context), maxOrder);
2174 strm->byteout.a = (struct archive_write *)a;
2175 strm->byteout.Write = ppmd_write;
2176 strm->range_enc.Stream = &(strm->byteout);
2177 __archive_ppmd7_functions.Ppmd7z_RangeEnc_Init(&(strm->range_enc));
2180 lastrm->real_stream = strm;
2182 lastrm->code = compression_code_ppmd;
2183 lastrm->end = compression_end_ppmd;
2184 lastrm->prop_size = 5;
2185 lastrm->props = props;
2186 return (ARCHIVE_OK);
2190 compression_code_ppmd(struct archive *a,
2191 struct la_zstream *lastrm, enum la_zaction action)
2193 struct ppmd_stream *strm;
2195 (void)a; /* UNUSED */
2197 strm = (struct ppmd_stream *)lastrm->real_stream;
2199 /* Copy encoded data if there are remaining bytes from previous call. */
2200 if (strm->buff_bytes) {
2201 uint8_t *p = strm->buff_ptr - strm->buff_bytes;
2202 while (lastrm->avail_out && strm->buff_bytes) {
2203 *lastrm->next_out++ = *p++;
2204 lastrm->avail_out--;
2205 lastrm->total_out++;
2208 if (strm->buff_bytes)
2209 return (ARCHIVE_OK);
2210 if (strm->stat == 1)
2211 return (ARCHIVE_EOF);
2212 strm->buff_ptr = strm->buff;
2214 while (lastrm->avail_in && lastrm->avail_out) {
2215 __archive_ppmd7_functions.Ppmd7_EncodeSymbol(
2216 &(strm->ppmd7_context), &(strm->range_enc),
2217 *lastrm->next_in++);
2221 if (lastrm->avail_in == 0 && action == ARCHIVE_Z_FINISH) {
2222 __archive_ppmd7_functions.Ppmd7z_RangeEnc_FlushData(
2223 &(strm->range_enc));
2225 /* Return EOF if there are no remaining bytes. */
2226 if (strm->buff_bytes == 0)
2227 return (ARCHIVE_EOF);
2229 return (ARCHIVE_OK);
2233 compression_end_ppmd(struct archive *a, struct la_zstream *lastrm)
2235 struct ppmd_stream *strm;
2237 (void)a; /* UNUSED */
2239 strm = (struct ppmd_stream *)lastrm->real_stream;
2240 __archive_ppmd7_functions.Ppmd7_Free(&strm->ppmd7_context);
2243 lastrm->real_stream = NULL;
2245 return (ARCHIVE_OK);
2249 * Universal compressor initializer.
2252 _7z_compression_init_encoder(struct archive_write *a, unsigned compression,
2253 int compression_level)
2258 zip = (struct _7zip *)a->format_data;
2259 switch (compression) {
2261 r = compression_init_encoder_deflate(
2262 &(a->archive), &(zip->stream),
2263 compression_level, 0);
2266 r = compression_init_encoder_bzip2(
2267 &(a->archive), &(zip->stream),
2271 r = compression_init_encoder_lzma1(
2272 &(a->archive), &(zip->stream),
2276 r = compression_init_encoder_lzma2(
2277 &(a->archive), &(zip->stream),
2281 r = compression_init_encoder_ppmd(
2282 &(a->archive), &(zip->stream),
2283 PPMD7_DEFAULT_ORDER, PPMD7_DEFAULT_MEM_SIZE);
2287 r = compression_init_encoder_copy(
2288 &(a->archive), &(zip->stream));
2291 if (r == ARCHIVE_OK) {
2292 zip->stream.total_in = 0;
2293 zip->stream.next_out = zip->wbuff;
2294 zip->stream.avail_out = sizeof(zip->wbuff);
2295 zip->stream.total_out = 0;
2302 compression_code(struct archive *a, struct la_zstream *lastrm,
2303 enum la_zaction action)
2306 return (lastrm->code(a, lastrm, action));
2307 return (ARCHIVE_OK);
2311 compression_end(struct archive *a, struct la_zstream *lastrm)
2313 if (lastrm->valid) {
2314 lastrm->prop_size = 0;
2315 free(lastrm->props);
2316 lastrm->props = NULL;
2317 return (lastrm->end(a, lastrm));
2319 return (ARCHIVE_OK);