2 * Copyright (c) 2010-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"
45 #include "archive_entry.h"
46 #include "archive_entry_locale.h"
47 #include "archive_private.h"
48 #include "archive_read_private.h"
49 #include "archive_endian.h"
53 /* Decoding status. */
57 * Window to see last decoded data, from 32KBi to 2MBi.
61 /* Window buffer, which is a loop buffer. */
62 unsigned char *w_buff;
63 /* The insert position to the window. */
65 /* The position where we can copy decoded code from the window. */
67 /* The length how many bytes we can copy decoded code from
70 /* Translation reversal for x86 proccessor CALL byte sequence(E8).
71 * This is used for LZX only. */
72 uint32_t translation_size;
75 #define VERBATIM_BLOCK 1
76 #define ALIGNED_OFFSET_BLOCK 2
77 #define UNCOMPRESSED_BLOCK 3
79 size_t block_bytes_avail;
80 /* Repeated offset. */
82 unsigned char rbytes[4];
96 #define CACHE_TYPE uint64_t
97 #define CACHE_BITS (8 * sizeof(CACHE_TYPE))
99 CACHE_TYPE cache_buffer;
100 /* Indicates how many bits avail in cache_buffer. */
112 unsigned char *bitlen;
115 * Use a index table. It's faster than searching a huffman
116 * coding tree, which is a binary tree. But a use of a large
117 * index table causes L1 cache read miss many times.
125 /* Direct access table. */
127 /* Binary tree table for extra bits over the direct access. */
138 static const int slots[] = {
139 30, 32, 34, 36, 38, 42, 50, 66, 98, 162, 290
142 #define SLOT_MAX 21/*->25*/
145 const unsigned char *next_in;
148 unsigned char *next_out;
155 * Cabinet file definitions.
157 /* CFHEADER offset */
158 #define CFHEADER_signature 0
159 #define CFHEADER_cbCabinet 8
160 #define CFHEADER_coffFiles 16
161 #define CFHEADER_versionMinor 24
162 #define CFHEADER_versionMajor 25
163 #define CFHEADER_cFolders 26
164 #define CFHEADER_cFiles 28
165 #define CFHEADER_flags 30
166 #define CFHEADER_setID 32
167 #define CFHEADER_iCabinet 34
168 #define CFHEADER_cbCFHeader 36
169 #define CFHEADER_cbCFFolder 38
170 #define CFHEADER_cbCFData 39
172 /* CFFOLDER offset */
173 #define CFFOLDER_coffCabStart 0
174 #define CFFOLDER_cCFData 4
175 #define CFFOLDER_typeCompress 6
176 #define CFFOLDER_abReserve 8
179 #define CFFILE_cbFile 0
180 #define CFFILE_uoffFolderStart 4
181 #define CFFILE_iFolder 8
182 #define CFFILE_date_time 10
183 #define CFFILE_attribs 14
186 #define CFDATA_csum 0
187 #define CFDATA_cbData 4
188 #define CFDATA_cbUncomp 6
190 static const char *compression_name[] = {
198 /* Sum value of this CFDATA. */
200 uint16_t compressed_size;
201 uint16_t compressed_bytes_remaining;
202 uint16_t uncompressed_size;
203 uint16_t uncompressed_bytes_remaining;
204 /* To know how many bytes we have decompressed. */
205 uint16_t uncompressed_avail;
206 /* Offset from the beginning of compressed data of this CFDATA */
207 uint16_t read_offset;
209 /* To keep memory image of this CFDATA to compute the sum. */
210 size_t memimage_size;
211 unsigned char *memimage;
212 /* Result of calculation of sum. */
213 uint32_t sum_calculated;
214 unsigned char sum_extra[4];
220 uint32_t cfdata_offset_in_cab;
221 uint16_t cfdata_count;
223 #define COMPTYPE_NONE 0x0000
224 #define COMPTYPE_MSZIP 0x0001
225 #define COMPTYPE_QUANTUM 0x0002
226 #define COMPTYPE_LZX 0x0003
228 const char *compname;
229 /* At the time reading CFDATA */
230 struct cfdata cfdata;
232 /* Flags to mark progress of decompression. */
233 char decompress_init;
237 uint32_t uncompressed_size;
241 #define iFoldCONTINUED_FROM_PREV 0xFFFD
242 #define iFoldCONTINUED_TO_NEXT 0xFFFE
243 #define iFoldCONTINUED_PREV_AND_NEXT 0xFFFF
245 #define ATTR_RDONLY 0x01
246 #define ATTR_NAME_IS_UTF 0x80
247 struct archive_string pathname;
251 /* Total bytes of all file size in a Cabinet. */
252 uint32_t total_bytes;
253 uint32_t files_offset;
254 uint16_t folder_count;
257 #define PREV_CABINET 0x0001
258 #define NEXT_CABINET 0x0002
259 #define RESERVE_PRESENT 0x0004
262 /* Version number. */
265 unsigned char cffolder;
266 unsigned char cfdata;
267 /* All folders in a cabinet. */
268 struct cffolder *folder_array;
269 /* All files in a cabinet. */
270 struct cffile *file_array;
275 /* entry_bytes_remaining is the number of bytes we expect. */
276 int64_t entry_offset;
277 int64_t entry_bytes_remaining;
278 int64_t entry_unconsumed;
279 int64_t entry_compressed_bytes_read;
280 int64_t entry_uncompressed_bytes_read;
281 struct cffolder *entry_cffolder;
282 struct cffile *entry_cffile;
283 struct cfdata *entry_cfdata;
285 /* Offset from beginning of a cabinet file. */
287 struct cfheader cfheader;
288 struct archive_wstring ws;
290 /* Flag to mark progress that an archive was read their first header.*/
294 char end_of_entry_cleanup;
296 unsigned char *uncompressed_buffer;
297 size_t uncompressed_buffer_size;
299 int init_default_conversion;
300 struct archive_string_conv *sconv;
301 struct archive_string_conv *sconv_default;
302 struct archive_string_conv *sconv_utf8;
303 char format_name[64];
309 struct lzx_stream xstrm;
312 static int archive_read_format_cab_bid(struct archive_read *, int);
313 static int archive_read_format_cab_options(struct archive_read *,
314 const char *, const char *);
315 static int archive_read_format_cab_read_header(struct archive_read *,
316 struct archive_entry *);
317 static int archive_read_format_cab_read_data(struct archive_read *,
318 const void **, size_t *, int64_t *);
319 static int archive_read_format_cab_read_data_skip(struct archive_read *);
320 static int archive_read_format_cab_cleanup(struct archive_read *);
322 static int cab_skip_sfx(struct archive_read *);
323 static time_t cab_dos_time(const unsigned char *);
324 static int cab_read_data(struct archive_read *, const void **,
325 size_t *, int64_t *);
326 static int cab_read_header(struct archive_read *);
327 static uint32_t cab_checksum_cfdata_4(const void *, size_t bytes, uint32_t);
328 static uint32_t cab_checksum_cfdata(const void *, size_t bytes, uint32_t);
329 static void cab_checksum_update(struct archive_read *, size_t);
330 static int cab_checksum_finish(struct archive_read *);
331 static int cab_next_cfdata(struct archive_read *);
332 static const void *cab_read_ahead_cfdata(struct archive_read *, ssize_t *);
333 static const void *cab_read_ahead_cfdata_none(struct archive_read *, ssize_t *);
334 static const void *cab_read_ahead_cfdata_deflate(struct archive_read *,
336 static const void *cab_read_ahead_cfdata_lzx(struct archive_read *,
338 static int64_t cab_consume_cfdata(struct archive_read *, int64_t);
339 static int64_t cab_minimum_consume_cfdata(struct archive_read *, int64_t);
340 static int lzx_decode_init(struct lzx_stream *, int);
341 static int lzx_read_blocks(struct lzx_stream *, int);
342 static int lzx_decode_blocks(struct lzx_stream *, int);
343 static void lzx_decode_free(struct lzx_stream *);
344 static void lzx_translation(struct lzx_stream *, void *, size_t, uint32_t);
345 static void lzx_cleanup_bitstream(struct lzx_stream *);
346 static int lzx_decode(struct lzx_stream *, int);
347 static int lzx_read_pre_tree(struct lzx_stream *);
348 static int lzx_read_bitlen(struct lzx_stream *, struct huffman *, int);
349 static int lzx_huffman_init(struct huffman *, size_t, int);
350 static void lzx_huffman_free(struct huffman *);
351 static int lzx_make_huffman_table(struct huffman *);
352 static inline int lzx_decode_huffman(struct huffman *, unsigned);
353 static int lzx_decode_huffman_tree(struct huffman *, unsigned, int);
357 archive_read_support_format_cab(struct archive *_a)
359 struct archive_read *a = (struct archive_read *)_a;
363 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
364 ARCHIVE_STATE_NEW, "archive_read_support_format_cab");
366 cab = (struct cab *)calloc(1, sizeof(*cab));
368 archive_set_error(&a->archive, ENOMEM,
369 "Can't allocate CAB data");
370 return (ARCHIVE_FATAL);
372 archive_string_init(&cab->ws);
373 archive_wstring_ensure(&cab->ws, 256);
375 r = __archive_read_register_format(a,
378 archive_read_format_cab_bid,
379 archive_read_format_cab_options,
380 archive_read_format_cab_read_header,
381 archive_read_format_cab_read_data,
382 archive_read_format_cab_read_data_skip,
383 archive_read_format_cab_cleanup);
391 find_cab_magic(const char *p)
396 * Note: Self-Extraction program has 'MSCF' string in their
397 * program. If we were finding 'MSCF' string only, we got
398 * wrong place for Cabinet header, thus, we have to check
399 * following four bytes which are reserved and must be set
402 if (memcmp(p, "MSCF\0\0\0\0", 8) == 0)
414 archive_read_format_cab_bid(struct archive_read *a, int best_bid)
417 ssize_t bytes_avail, offset, window;
419 /* If there's already a better bid than we can ever
420 make, don't bother testing. */
424 if ((p = __archive_read_ahead(a, 8, NULL)) == NULL)
427 if (memcmp(p, "MSCF\0\0\0\0", 8) == 0)
431 * Attempt to handle self-extracting archives
432 * by noting a PE header and searching forward
433 * up to 128k for a 'MSCF' marker.
435 if (p[0] == 'M' && p[1] == 'Z') {
438 while (offset < (1024 * 128)) {
439 const char *h = __archive_read_ahead(a, offset + window,
442 /* Remaining bytes are less than window. */
449 while (p + 8 < h + bytes_avail) {
451 if ((next = find_cab_magic(p)) == 0)
462 archive_read_format_cab_options(struct archive_read *a,
463 const char *key, const char *val)
466 int ret = ARCHIVE_FAILED;
468 cab = (struct cab *)(a->format->data);
469 if (strcmp(key, "hdrcharset") == 0) {
470 if (val == NULL || val[0] == 0)
471 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
472 "cab: hdrcharset option needs a character-set name");
474 cab->sconv = archive_string_conversion_from_charset(
475 &a->archive, val, 0);
476 if (cab->sconv != NULL)
484 /* Note: The "warn" return is just to inform the options
485 * supervisor that we didn't handle it. It will generate
486 * a suitable error if no one used this option. */
487 return (ARCHIVE_WARN);
491 cab_skip_sfx(struct archive_read *a)
495 ssize_t bytes, window;
499 const char *h = __archive_read_ahead(a, window, &bytes);
501 /* Remaining size are less than window. */
504 archive_set_error(&a->archive,
505 ARCHIVE_ERRNO_FILE_FORMAT,
506 "Couldn't find out CAB header");
507 return (ARCHIVE_FATAL);
515 * Scan ahead until we find something that looks
516 * like the cab header.
520 if ((next = find_cab_magic(p)) == 0) {
522 __archive_read_consume(a, skip);
528 __archive_read_consume(a, skip);
533 truncated_error(struct archive_read *a)
535 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
536 "Truncated CAB header");
537 return (ARCHIVE_FATAL);
541 cab_strnlen(const unsigned char *p, size_t maxlen)
545 for (i = 0; i <= maxlen; i++) {
550 return (-1);/* invalid */
554 /* Read bytes as much as remaining. */
556 cab_read_ahead_remaining(struct archive_read *a, size_t min, ssize_t *avail)
561 p = __archive_read_ahead(a, min, avail);
569 /* Convert a path separator '\' -> '/' */
571 cab_convert_path_separator_1(struct archive_string *fn, unsigned char attr)
576 /* Easy check if we have '\' in multi-byte string. */
578 for (i = 0; i < archive_strlen(fn); i++) {
579 if (fn->s[i] == '\\') {
581 /* This may be second byte of multi-byte
587 } else if ((fn->s[i] & 0x80) && !(attr & ATTR_NAME_IS_UTF))
592 if (i == archive_strlen(fn))
598 * Replace a character '\' with '/' in wide character.
601 cab_convert_path_separator_2(struct cab *cab, struct archive_entry *entry)
606 /* If a conversion to wide character failed, force the replacement. */
607 if ((wp = archive_entry_pathname_w(entry)) != NULL) {
608 archive_wstrcpy(&(cab->ws), wp);
609 for (i = 0; i < archive_strlen(&(cab->ws)); i++) {
610 if (cab->ws.s[i] == L'\\')
613 archive_entry_copy_pathname_w(entry, cab->ws.s);
618 * Read CFHEADER, CFFOLDER and CFFILE.
621 cab_read_header(struct archive_read *a)
623 const unsigned char *p;
629 int cur_folder, prev_folder;
632 a->archive.archive_format = ARCHIVE_FORMAT_CAB;
633 if (a->archive.archive_format_name == NULL)
634 a->archive.archive_format_name = "CAB";
636 if ((p = __archive_read_ahead(a, 42, NULL)) == NULL)
637 return (truncated_error(a));
639 cab = (struct cab *)(a->format->data);
640 if (cab->found_header == 0 &&
641 p[0] == 'M' && p[1] == 'Z') {
642 /* This is an executable? Must be self-extracting... */
643 err = cab_skip_sfx(a);
644 if (err < ARCHIVE_WARN)
647 if ((p = __archive_read_ahead(a, sizeof(*p), NULL)) == NULL)
648 return (truncated_error(a));
656 if (p[CFHEADER_signature+0] != 'M' || p[CFHEADER_signature+1] != 'S' ||
657 p[CFHEADER_signature+2] != 'C' || p[CFHEADER_signature+3] != 'F') {
658 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
659 "Couldn't find out CAB header");
660 return (ARCHIVE_FATAL);
662 hd->total_bytes = archive_le32dec(p + CFHEADER_cbCabinet);
663 hd->files_offset = archive_le32dec(p + CFHEADER_coffFiles);
664 hd->minor = p[CFHEADER_versionMinor];
665 hd->major = p[CFHEADER_versionMajor];
666 hd->folder_count = archive_le16dec(p + CFHEADER_cFolders);
667 if (hd->folder_count == 0)
669 hd->file_count = archive_le16dec(p + CFHEADER_cFiles);
670 if (hd->file_count == 0)
672 hd->flags = archive_le16dec(p + CFHEADER_flags);
673 hd->setid = archive_le16dec(p + CFHEADER_setID);
674 hd->cabinet = archive_le16dec(p + CFHEADER_iCabinet);
675 used = CFHEADER_iCabinet + 2;
676 if (hd->flags & RESERVE_PRESENT) {
678 cfheader = archive_le16dec(p + CFHEADER_cbCFHeader);
679 if (cfheader > 60000U)
681 hd->cffolder = p[CFHEADER_cbCFFolder];
682 hd->cfdata = p[CFHEADER_cbCFData];
683 used += 4;/* cbCFHeader, cbCFFolder and cbCFData */
684 used += cfheader;/* abReserve */
686 hd->cffolder = 0;/* Avoid compiling warning. */
687 if (hd->flags & PREV_CABINET) {
688 /* How many bytes are used for szCabinetPrev. */
689 if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
690 return (truncated_error(a));
691 if ((len = cab_strnlen(p + used, 255)) <= 0)
694 /* How many bytes are used for szDiskPrev. */
695 if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
696 return (truncated_error(a));
697 if ((len = cab_strnlen(p + used, 255)) <= 0)
701 if (hd->flags & NEXT_CABINET) {
702 /* How many bytes are used for szCabinetNext. */
703 if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
704 return (truncated_error(a));
705 if ((len = cab_strnlen(p + used, 255)) <= 0)
708 /* How many bytes are used for szDiskNext. */
709 if ((p = __archive_read_ahead(a, used+256, NULL)) == NULL)
710 return (truncated_error(a));
711 if ((len = cab_strnlen(p + used, 255)) <= 0)
715 __archive_read_consume(a, used);
716 cab->cab_offset += used;
722 hd->folder_array = (struct cffolder *)calloc(
723 hd->folder_count, sizeof(struct cffolder));
724 if (hd->folder_array == NULL)
728 if (hd->flags & RESERVE_PRESENT)
729 bytes += hd->cffolder;
730 bytes *= hd->folder_count;
731 if ((p = __archive_read_ahead(a, bytes, NULL)) == NULL)
732 return (truncated_error(a));
734 for (i = 0; i < hd->folder_count; i++) {
735 struct cffolder *folder = &(hd->folder_array[i]);
736 folder->cfdata_offset_in_cab =
737 archive_le32dec(p + CFFOLDER_coffCabStart);
738 folder->cfdata_count = archive_le16dec(p+CFFOLDER_cCFData);
740 archive_le16dec(p+CFFOLDER_typeCompress) & 0x0F;
742 archive_le16dec(p+CFFOLDER_typeCompress) >> 8;
743 /* Get a compression name. */
744 if (folder->comptype <
745 sizeof(compression_name) / sizeof(compression_name[0]))
746 folder->compname = compression_name[folder->comptype];
748 folder->compname = "UNKNOWN";
751 if (hd->flags & RESERVE_PRESENT) {
752 p += hd->cffolder;/* abReserve */
753 used += hd->cffolder;
756 * Sanity check if each data is acceptable.
758 if (offset32 >= folder->cfdata_offset_in_cab)
760 offset32 = folder->cfdata_offset_in_cab;
762 /* Set a request to initialize zlib for the CFDATA of
764 folder->decompress_init = 0;
766 __archive_read_consume(a, used);
767 cab->cab_offset += used;
772 /* Seek read pointer to the offset of CFFILE if needed. */
773 skip = (int64_t)hd->files_offset - cab->cab_offset;
775 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
776 "Invalid offset of CFFILE %jd < %jd",
777 (intmax_t)hd->files_offset, (intmax_t)cab->cab_offset);
778 return (ARCHIVE_FATAL);
781 __archive_read_consume(a, skip);
782 cab->cab_offset += skip;
784 /* Allocate memory for CFDATA */
785 hd->file_array = (struct cffile *)calloc(
786 hd->file_count, sizeof(struct cffile));
787 if (hd->file_array == NULL)
791 for (i = 0; i < hd->file_count; i++) {
792 struct cffile *file = &(hd->file_array[i]);
795 if ((p = __archive_read_ahead(a, 16, NULL)) == NULL)
796 return (truncated_error(a));
797 file->uncompressed_size = archive_le32dec(p + CFFILE_cbFile);
798 file->offset = archive_le32dec(p + CFFILE_uoffFolderStart);
799 file->folder = archive_le16dec(p + CFFILE_iFolder);
800 file->mtime = cab_dos_time(p + CFFILE_date_time);
801 file->attr = archive_le16dec(p + CFFILE_attribs);
802 __archive_read_consume(a, 16);
804 cab->cab_offset += 16;
805 if ((p = cab_read_ahead_remaining(a, 256, &avail)) == NULL)
806 return (truncated_error(a));
807 if ((len = cab_strnlen(p, avail-1)) <= 0)
810 /* Copy a pathname. */
811 archive_string_init(&(file->pathname));
812 archive_strncpy(&(file->pathname), p, len);
813 __archive_read_consume(a, len + 1);
814 cab->cab_offset += len + 1;
817 * Sanity check if each data is acceptable.
819 if (file->uncompressed_size > 0x7FFF8000)
820 goto invalid;/* Too large */
821 if ((int64_t)file->offset + (int64_t)file->uncompressed_size
822 > ARCHIVE_LITERAL_LL(0x7FFF8000))
823 goto invalid;/* Too large */
824 switch (file->folder) {
825 case iFoldCONTINUED_TO_NEXT:
826 /* This must be last file in a folder. */
827 if (i != hd->file_count -1)
829 cur_folder = hd->folder_count -1;
831 case iFoldCONTINUED_PREV_AND_NEXT:
832 /* This must be only one file in a folder. */
833 if (hd->file_count != 1)
836 case iFoldCONTINUED_FROM_PREV:
837 /* This must be first file in a folder. */
840 prev_folder = cur_folder = 0;
841 offset32 = file->offset;
844 if (file->folder >= hd->folder_count)
846 cur_folder = file->folder;
849 /* Dot not back track. */
850 if (cur_folder < prev_folder)
852 if (cur_folder != prev_folder)
854 prev_folder = cur_folder;
856 /* Make sure there are not any blanks from last file
858 if (offset32 != file->offset)
860 offset32 += file->uncompressed_size;
862 /* CFDATA is available for file contents. */
863 if (file->uncompressed_size > 0 &&
864 hd->folder_array[cur_folder].cfdata_count == 0)
868 if (hd->cabinet != 0 || hd->flags & (PREV_CABINET | NEXT_CABINET)) {
869 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
870 "Multivolume cabinet file is unsupported");
871 return (ARCHIVE_WARN);
875 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
876 "Invalid CAB header");
877 return (ARCHIVE_FATAL);
879 archive_set_error(&a->archive, ENOMEM,
880 "Can't allocate memory for CAB data");
881 return (ARCHIVE_FATAL);
885 archive_read_format_cab_read_header(struct archive_read *a,
886 struct archive_entry *entry)
890 struct cffolder *prev_folder;
892 struct archive_string_conv *sconv;
893 int err = ARCHIVE_OK, r;
895 cab = (struct cab *)(a->format->data);
896 if (cab->found_header == 0) {
897 err = cab_read_header(a);
898 if (err < ARCHIVE_WARN)
900 /* We've found the header. */
901 cab->found_header = 1;
905 if (hd->file_index >= hd->file_count) {
906 cab->end_of_archive = 1;
907 return (ARCHIVE_EOF);
909 file = &hd->file_array[hd->file_index++];
911 cab->end_of_entry = 0;
912 cab->end_of_entry_cleanup = 0;
913 cab->entry_compressed_bytes_read = 0;
914 cab->entry_uncompressed_bytes_read = 0;
915 cab->entry_unconsumed = 0;
916 cab->entry_cffile = file;
919 * Choose a proper folder.
921 prev_folder = cab->entry_cffolder;
922 switch (file->folder) {
923 case iFoldCONTINUED_FROM_PREV:
924 case iFoldCONTINUED_PREV_AND_NEXT:
925 cab->entry_cffolder = &hd->folder_array[0];
927 case iFoldCONTINUED_TO_NEXT:
928 cab->entry_cffolder = &hd->folder_array[hd->folder_count-1];
931 cab->entry_cffolder = &hd->folder_array[file->folder];
934 /* If a cffolder of this file is changed, reset a cfdata to read
935 * file contents from next cfdata. */
936 if (prev_folder != cab->entry_cffolder)
937 cab->entry_cfdata = NULL;
939 /* If a pathname is UTF-8, prepare a string conversion object
940 * for UTF-8 and use it. */
941 if (file->attr & ATTR_NAME_IS_UTF) {
942 if (cab->sconv_utf8 == NULL) {
944 archive_string_conversion_from_charset(
945 &(a->archive), "UTF-8", 1);
946 if (cab->sconv_utf8 == NULL)
947 return (ARCHIVE_FATAL);
949 sconv = cab->sconv_utf8;
950 } else if (cab->sconv != NULL) {
951 /* Choose the conversion specified by the option. */
954 /* Choose the default conversion. */
955 if (!cab->init_default_conversion) {
957 archive_string_default_conversion_for_read(
959 cab->init_default_conversion = 1;
961 sconv = cab->sconv_default;
965 * Set a default value and common data
967 r = cab_convert_path_separator_1(&(file->pathname), file->attr);
968 if (archive_entry_copy_pathname_l(entry, file->pathname.s,
969 archive_strlen(&(file->pathname)), sconv) != 0) {
970 if (errno == ENOMEM) {
971 archive_set_error(&a->archive, ENOMEM,
972 "Can't allocate memory for Pathname");
973 return (ARCHIVE_FATAL);
975 archive_set_error(&a->archive,
976 ARCHIVE_ERRNO_FILE_FORMAT,
977 "Pathname cannot be converted "
978 "from %s to current locale.",
979 archive_string_conversion_charset_name(sconv));
983 /* Convert a path separator '\' -> '/' */
984 cab_convert_path_separator_2(cab, entry);
987 archive_entry_set_size(entry, file->uncompressed_size);
988 if (file->attr & ATTR_RDONLY)
989 archive_entry_set_mode(entry, AE_IFREG | 0555);
991 archive_entry_set_mode(entry, AE_IFREG | 0777);
992 archive_entry_set_mtime(entry, file->mtime, 0);
994 cab->entry_bytes_remaining = file->uncompressed_size;
995 cab->entry_offset = 0;
996 /* We don't need compress data. */
997 if (file->uncompressed_size == 0)
998 cab->end_of_entry_cleanup = cab->end_of_entry = 1;
1000 /* Set up a more descriptive format name. */
1001 sprintf(cab->format_name, "CAB %d.%d (%s)",
1002 hd->major, hd->minor, cab->entry_cffolder->compname);
1003 a->archive.archive_format_name = cab->format_name;
1009 archive_read_format_cab_read_data(struct archive_read *a,
1010 const void **buff, size_t *size, int64_t *offset)
1012 struct cab *cab = (struct cab *)(a->format->data);
1015 switch (cab->entry_cffile->folder) {
1016 case iFoldCONTINUED_FROM_PREV:
1017 case iFoldCONTINUED_TO_NEXT:
1018 case iFoldCONTINUED_PREV_AND_NEXT:
1022 archive_clear_error(&a->archive);
1023 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1024 "Cannot restore this file split in multivolume.");
1025 return (ARCHIVE_FAILED);
1029 if (cab->entry_unconsumed) {
1030 /* Consume as much as the compressor actually used. */
1031 r = cab_consume_cfdata(a, cab->entry_unconsumed);
1032 cab->entry_unconsumed = 0;
1036 if (cab->end_of_archive || cab->end_of_entry) {
1037 if (!cab->end_of_entry_cleanup) {
1038 /* End-of-entry cleanup done. */
1039 cab->end_of_entry_cleanup = 1;
1041 *offset = cab->entry_offset;
1044 return (ARCHIVE_EOF);
1047 return (cab_read_data(a, buff, size, offset));
1051 cab_checksum_cfdata_4(const void *p, size_t bytes, uint32_t seed)
1053 const unsigned char *b;
1060 while (--u32num >= 0) {
1061 sum ^= archive_le32dec(b);
1068 cab_checksum_cfdata(const void *p, size_t bytes, uint32_t seed)
1070 const unsigned char *b;
1074 sum = cab_checksum_cfdata_4(p, bytes, seed);
1078 switch (bytes & 3) {
1080 t |= ((uint32_t)(*b++)) << 16;
1083 t |= ((uint32_t)(*b++)) << 8;
1097 cab_checksum_update(struct archive_read *a, size_t bytes)
1099 struct cab *cab = (struct cab *)(a->format->data);
1100 struct cfdata *cfdata = cab->entry_cfdata;
1101 const unsigned char *p;
1104 if (cfdata->sum == 0 || cfdata->sum_ptr == NULL)
1107 * Calculate the sum of this CFDATA.
1108 * Make sure CFDATA must be calculated in four bytes.
1110 p = cfdata->sum_ptr;
1112 if (cfdata->sum_extra_avail) {
1113 while (cfdata->sum_extra_avail < 4 && sumbytes > 0) {
1115 cfdata->sum_extra_avail++] = *p++;
1118 if (cfdata->sum_extra_avail == 4) {
1119 cfdata->sum_calculated = cab_checksum_cfdata_4(
1120 cfdata->sum_extra, 4, cfdata->sum_calculated);
1121 cfdata->sum_extra_avail = 0;
1125 int odd = sumbytes & 3;
1126 if (sumbytes - odd > 0)
1127 cfdata->sum_calculated = cab_checksum_cfdata_4(
1128 p, sumbytes - odd, cfdata->sum_calculated);
1130 memcpy(cfdata->sum_extra, p + sumbytes - odd, odd);
1131 cfdata->sum_extra_avail = odd;
1133 cfdata->sum_ptr = NULL;
1137 cab_checksum_finish(struct archive_read *a)
1139 struct cab *cab = (struct cab *)(a->format->data);
1140 struct cfdata *cfdata = cab->entry_cfdata;
1143 /* Do not need to compute a sum. */
1144 if (cfdata->sum == 0)
1145 return (ARCHIVE_OK);
1148 * Calculate the sum of remaining CFDATA.
1150 if (cfdata->sum_extra_avail) {
1151 cfdata->sum_calculated =
1152 cab_checksum_cfdata(cfdata->sum_extra,
1153 cfdata->sum_extra_avail, cfdata->sum_calculated);
1154 cfdata->sum_extra_avail = 0;
1158 if (cab->cfheader.flags & RESERVE_PRESENT)
1159 l += cab->cfheader.cfdata;
1160 cfdata->sum_calculated = cab_checksum_cfdata(
1161 cfdata->memimage + CFDATA_cbData, l, cfdata->sum_calculated);
1162 if (cfdata->sum_calculated != cfdata->sum) {
1163 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1164 "Checksum error CFDATA[%d] %x:%x in %d bytes",
1165 cab->entry_cffolder->cfdata_index -1,
1166 cfdata->sum, cfdata->sum_calculated,
1167 cfdata->compressed_size);
1168 return (ARCHIVE_FAILED);
1170 return (ARCHIVE_OK);
1174 * Read CFDATA if needed.
1177 cab_next_cfdata(struct archive_read *a)
1179 struct cab *cab = (struct cab *)(a->format->data);
1180 struct cfdata *cfdata = cab->entry_cfdata;
1182 /* There are remaining bytes in current CFDATA, use it first. */
1183 if (cfdata != NULL && cfdata->uncompressed_bytes_remaining > 0)
1184 return (ARCHIVE_OK);
1186 if (cfdata == NULL) {
1189 cab->entry_cffolder->cfdata_index = 0;
1191 /* Seek read pointer to the offset of CFDATA if needed. */
1192 skip = cab->entry_cffolder->cfdata_offset_in_cab
1196 switch (cab->entry_cffile->folder) {
1197 case iFoldCONTINUED_FROM_PREV:
1198 case iFoldCONTINUED_PREV_AND_NEXT:
1201 case iFoldCONTINUED_TO_NEXT:
1202 folder_index = cab->cfheader.folder_count-1;
1205 folder_index = cab->entry_cffile->folder;
1208 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1209 "Invalid offset of CFDATA in folder(%d) %jd < %jd",
1211 (intmax_t)cab->entry_cffolder->cfdata_offset_in_cab,
1212 (intmax_t)cab->cab_offset);
1213 return (ARCHIVE_FATAL);
1216 if (__archive_read_consume(a, skip) < 0)
1217 return (ARCHIVE_FATAL);
1219 cab->entry_cffolder->cfdata_offset_in_cab;
1226 if (cab->entry_cffolder->cfdata_index <
1227 cab->entry_cffolder->cfdata_count) {
1228 const unsigned char *p;
1231 cfdata = &(cab->entry_cffolder->cfdata);
1232 cab->entry_cffolder->cfdata_index++;
1233 cab->entry_cfdata = cfdata;
1234 cfdata->sum_calculated = 0;
1235 cfdata->sum_extra_avail = 0;
1236 cfdata->sum_ptr = NULL;
1238 if (cab->cfheader.flags & RESERVE_PRESENT)
1239 l += cab->cfheader.cfdata;
1240 if ((p = __archive_read_ahead(a, l, NULL)) == NULL)
1241 return (truncated_error(a));
1242 cfdata->sum = archive_le32dec(p + CFDATA_csum);
1243 cfdata->compressed_size = archive_le16dec(p + CFDATA_cbData);
1244 cfdata->compressed_bytes_remaining = cfdata->compressed_size;
1245 cfdata->uncompressed_size =
1246 archive_le16dec(p + CFDATA_cbUncomp);
1247 cfdata->uncompressed_bytes_remaining =
1248 cfdata->uncompressed_size;
1249 cfdata->uncompressed_avail = 0;
1250 cfdata->read_offset = 0;
1251 cfdata->unconsumed = 0;
1254 * Sanity check if data size is acceptable.
1256 if (cfdata->compressed_size == 0 ||
1257 cfdata->compressed_size > (0x8000+6144))
1259 if (cfdata->uncompressed_size > 0x8000)
1261 if (cfdata->uncompressed_size == 0) {
1262 switch (cab->entry_cffile->folder) {
1263 case iFoldCONTINUED_PREV_AND_NEXT:
1264 case iFoldCONTINUED_TO_NEXT:
1266 case iFoldCONTINUED_FROM_PREV:
1271 /* If CFDATA is not last in a folder, an uncompressed
1272 * size must be 0x8000(32KBi) */
1273 if ((cab->entry_cffolder->cfdata_index <
1274 cab->entry_cffolder->cfdata_count) &&
1275 cfdata->uncompressed_size != 0x8000)
1278 /* A compressed data size and an uncompressed data size must
1279 * be the same in no compression mode. */
1280 if (cab->entry_cffolder->comptype == COMPTYPE_NONE &&
1281 cfdata->compressed_size != cfdata->uncompressed_size)
1285 * Save CFDATA image for sum check.
1287 if (cfdata->memimage_size < (size_t)l) {
1288 free(cfdata->memimage);
1289 cfdata->memimage = malloc(l);
1290 if (cfdata->memimage == NULL) {
1291 archive_set_error(&a->archive, ENOMEM,
1292 "Can't allocate memory for CAB data");
1293 return (ARCHIVE_FATAL);
1295 cfdata->memimage_size = l;
1297 memcpy(cfdata->memimage, p, l);
1299 /* Consume bytes as much as we used. */
1300 __archive_read_consume(a, l);
1301 cab->cab_offset += l;
1302 } else if (cab->entry_cffolder->cfdata_count > 0) {
1303 /* Run out of all CFDATA in a folder. */
1304 cfdata->compressed_size = 0;
1305 cfdata->uncompressed_size = 0;
1306 cfdata->compressed_bytes_remaining = 0;
1307 cfdata->uncompressed_bytes_remaining = 0;
1309 /* Current folder does not have any CFDATA. */
1310 cfdata = &(cab->entry_cffolder->cfdata);
1311 cab->entry_cfdata = cfdata;
1312 memset(cfdata, 0, sizeof(*cfdata));
1314 return (ARCHIVE_OK);
1316 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1318 return (ARCHIVE_FATAL);
1322 * Read ahead CFDATA.
1325 cab_read_ahead_cfdata(struct archive_read *a, ssize_t *avail)
1327 struct cab *cab = (struct cab *)(a->format->data);
1330 err = cab_next_cfdata(a);
1331 if (err < ARCHIVE_OK) {
1336 switch (cab->entry_cffolder->comptype) {
1338 return (cab_read_ahead_cfdata_none(a, avail));
1339 case COMPTYPE_MSZIP:
1340 return (cab_read_ahead_cfdata_deflate(a, avail));
1342 return (cab_read_ahead_cfdata_lzx(a, avail));
1343 default: /* Unsupported compression. */
1344 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1345 "Unsupported CAB compression : %s",
1346 cab->entry_cffolder->compname);
1347 *avail = ARCHIVE_FAILED;
1353 * Read ahead CFDATA as uncompressed data.
1356 cab_read_ahead_cfdata_none(struct archive_read *a, ssize_t *avail)
1358 struct cab *cab = (struct cab *)(a->format->data);
1359 struct cfdata *cfdata;
1361 int64_t skipped_bytes;
1363 cfdata = cab->entry_cfdata;
1365 if (cfdata->uncompressed_avail == 0 &&
1366 cfdata->read_offset > 0) {
1367 /* we've already skipped some bytes before really read. */
1368 skipped_bytes = cfdata->read_offset;
1369 cfdata->read_offset = 0;
1370 cfdata->uncompressed_bytes_remaining += skipped_bytes;
1375 * Note: '1' here is a performance optimization.
1376 * Recall that the decompression layer returns a count of
1377 * available bytes; asking for more than that forces the
1378 * decompressor to combine reads by copying data.
1380 d = __archive_read_ahead(a, 1, avail);
1382 *avail = truncated_error(a);
1385 if (*avail > cfdata->uncompressed_bytes_remaining)
1386 *avail = cfdata->uncompressed_bytes_remaining;
1387 cfdata->uncompressed_avail = cfdata->uncompressed_size;
1388 cfdata->unconsumed = *avail;
1389 cfdata->sum_ptr = d;
1390 if (skipped_bytes > 0) {
1392 cab_minimum_consume_cfdata(a, skipped_bytes);
1393 if (skipped_bytes < 0) {
1394 *avail = ARCHIVE_FATAL;
1405 * Read ahead CFDATA as deflate data.
1409 cab_read_ahead_cfdata_deflate(struct archive_read *a, ssize_t *avail)
1411 struct cab *cab = (struct cab *)(a->format->data);
1412 struct cfdata *cfdata;
1418 cfdata = cab->entry_cfdata;
1419 /* If the buffer hasn't been allocated, allocate it now. */
1420 if (cab->uncompressed_buffer == NULL) {
1421 cab->uncompressed_buffer_size = 0x8000;
1422 cab->uncompressed_buffer
1423 = (unsigned char *)malloc(cab->uncompressed_buffer_size);
1424 if (cab->uncompressed_buffer == NULL) {
1425 archive_set_error(&a->archive, ENOMEM,
1426 "No memory for CAB reader");
1427 *avail = ARCHIVE_FATAL;
1432 uavail = cfdata->uncompressed_avail;
1433 if (uavail == cfdata->uncompressed_size) {
1434 d = cab->uncompressed_buffer + cfdata->read_offset;
1435 *avail = uavail - cfdata->read_offset;
1439 if (!cab->entry_cffolder->decompress_init) {
1440 cab->stream.next_in = NULL;
1441 cab->stream.avail_in = 0;
1442 cab->stream.total_in = 0;
1443 cab->stream.next_out = NULL;
1444 cab->stream.avail_out = 0;
1445 cab->stream.total_out = 0;
1446 if (cab->stream_valid)
1447 r = inflateReset(&cab->stream);
1449 r = inflateInit2(&cab->stream,
1450 -15 /* Don't check for zlib header */);
1452 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1453 "Can't initialize deflate decompression.");
1454 *avail = ARCHIVE_FATAL;
1457 /* Stream structure has been set up. */
1458 cab->stream_valid = 1;
1459 /* We've initialized decompression for this stream. */
1460 cab->entry_cffolder->decompress_init = 1;
1463 if (cfdata->compressed_bytes_remaining == cfdata->compressed_size)
1468 cab->stream.total_out = uavail;
1470 * We always uncompress all data in current CFDATA.
1472 while (!eod && cab->stream.total_out < cfdata->uncompressed_size) {
1473 ssize_t bytes_avail;
1475 cab->stream.next_out =
1476 cab->uncompressed_buffer + cab->stream.total_out;
1477 cab->stream.avail_out =
1478 cfdata->uncompressed_size - cab->stream.total_out;
1480 d = __archive_read_ahead(a, 1, &bytes_avail);
1481 if (bytes_avail <= 0) {
1482 *avail = truncated_error(a);
1485 if (bytes_avail > cfdata->compressed_bytes_remaining)
1486 bytes_avail = cfdata->compressed_bytes_remaining;
1488 * A bug in zlib.h: stream.next_in should be marked 'const'
1489 * but isn't (the library never alters data through the
1490 * next_in pointer, only reads it). The result: this ugly
1491 * cast to remove 'const'.
1493 cab->stream.next_in = (Bytef *)(uintptr_t)d;
1494 cab->stream.avail_in = bytes_avail;
1495 cab->stream.total_in = 0;
1497 /* Cut out a tow-byte MSZIP signature(0x43, 0x4b). */
1499 if (bytes_avail <= mszip) {
1501 if (cab->stream.next_in[0] != 0x43)
1503 if (bytes_avail > 1 &&
1504 cab->stream.next_in[1] != 0x4b)
1506 } else if (cab->stream.next_in[0] != 0x4b)
1508 cfdata->unconsumed = bytes_avail;
1509 cfdata->sum_ptr = d;
1510 if (cab_minimum_consume_cfdata(
1511 a, cfdata->unconsumed) < 0) {
1512 *avail = ARCHIVE_FATAL;
1515 mszip -= bytes_avail;
1518 if (mszip == 1 && cab->stream.next_in[0] != 0x4b)
1520 else if (cab->stream.next_in[0] != 0x43 ||
1521 cab->stream.next_in[1] != 0x4b)
1523 cab->stream.next_in += mszip;
1524 cab->stream.avail_in -= mszip;
1525 cab->stream.total_in += mszip;
1529 r = inflate(&cab->stream, 0);
1539 cfdata->unconsumed = cab->stream.total_in;
1540 cfdata->sum_ptr = d;
1541 if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
1542 *avail = ARCHIVE_FATAL;
1546 uavail = cab->stream.total_out;
1548 if (uavail < cfdata->uncompressed_size) {
1549 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1550 "Invalid uncompressed size (%d < %d)",
1551 uavail, cfdata->uncompressed_size);
1552 *avail = ARCHIVE_FATAL;
1557 * Note: I suspect there is a bug in makecab.exe because, in rare
1558 * case, compressed bytes are still remaining regardless we have
1559 * gotten all uncompressed bytes, which size is recoded in CFDATA,
1560 * as much as we need, and we have to use the garbage so as to
1561 * correctly compute the sum of CFDATA accordingly.
1563 if (cfdata->compressed_bytes_remaining > 0) {
1564 ssize_t bytes_avail;
1566 d = __archive_read_ahead(a, cfdata->compressed_bytes_remaining,
1568 if (bytes_avail <= 0) {
1569 *avail = truncated_error(a);
1572 cfdata->unconsumed = cfdata->compressed_bytes_remaining;
1573 cfdata->sum_ptr = d;
1574 if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
1575 *avail = ARCHIVE_FATAL;
1581 * Set dictionary data for decompressing of next CFDATA, which
1582 * in the same folder. This is why we always do decompress CFDATA
1583 * even if beginning CFDATA or some of CFDATA are not used in
1584 * skipping file data.
1586 if (cab->entry_cffolder->cfdata_index <
1587 cab->entry_cffolder->cfdata_count) {
1588 r = inflateReset(&cab->stream);
1591 r = inflateSetDictionary(&cab->stream,
1592 cab->uncompressed_buffer, cfdata->uncompressed_size);
1597 d = cab->uncompressed_buffer + cfdata->read_offset;
1598 *avail = uavail - cfdata->read_offset;
1599 cfdata->uncompressed_avail = uavail;
1606 archive_set_error(&a->archive, ENOMEM,
1607 "Out of memory for deflate decompression");
1610 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1611 "Deflate decompression failed (%d)", r);
1614 *avail = ARCHIVE_FATAL;
1617 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1618 "CFDATA incorrect(no MSZIP signature)");
1619 *avail = ARCHIVE_FATAL;
1623 #else /* HAVE_ZLIB_H */
1626 cab_read_ahead_cfdata_deflate(struct archive_read *a, ssize_t *avail)
1628 *avail = ARCHIVE_FATAL;
1629 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1630 "libarchive compiled without deflate support (no libz)");
1634 #endif /* HAVE_ZLIB_H */
1637 cab_read_ahead_cfdata_lzx(struct archive_read *a, ssize_t *avail)
1639 struct cab *cab = (struct cab *)(a->format->data);
1640 struct cfdata *cfdata;
1645 cfdata = cab->entry_cfdata;
1646 /* If the buffer hasn't been allocated, allocate it now. */
1647 if (cab->uncompressed_buffer == NULL) {
1648 cab->uncompressed_buffer_size = 0x8000;
1649 cab->uncompressed_buffer
1650 = (unsigned char *)malloc(cab->uncompressed_buffer_size);
1651 if (cab->uncompressed_buffer == NULL) {
1652 archive_set_error(&a->archive, ENOMEM,
1653 "No memory for CAB reader");
1654 *avail = ARCHIVE_FATAL;
1659 uavail = cfdata->uncompressed_avail;
1660 if (uavail == cfdata->uncompressed_size) {
1661 d = cab->uncompressed_buffer + cfdata->read_offset;
1662 *avail = uavail - cfdata->read_offset;
1666 if (!cab->entry_cffolder->decompress_init) {
1667 r = lzx_decode_init(&cab->xstrm,
1668 cab->entry_cffolder->compdata);
1669 if (r != ARCHIVE_OK) {
1670 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1671 "Can't initialize LZX decompression.");
1672 *avail = ARCHIVE_FATAL;
1675 /* We've initialized decompression for this stream. */
1676 cab->entry_cffolder->decompress_init = 1;
1679 /* Clean up remaining bits of previous CFDATA. */
1680 lzx_cleanup_bitstream(&cab->xstrm);
1681 cab->xstrm.total_out = uavail;
1682 while (cab->xstrm.total_out < cfdata->uncompressed_size) {
1683 ssize_t bytes_avail;
1685 cab->xstrm.next_out =
1686 cab->uncompressed_buffer + cab->xstrm.total_out;
1687 cab->xstrm.avail_out =
1688 cfdata->uncompressed_size - cab->xstrm.total_out;
1690 d = __archive_read_ahead(a, 1, &bytes_avail);
1691 if (bytes_avail <= 0) {
1692 archive_set_error(&a->archive,
1693 ARCHIVE_ERRNO_FILE_FORMAT,
1694 "Truncated CAB file data");
1695 *avail = ARCHIVE_FATAL;
1698 if (bytes_avail > cfdata->compressed_bytes_remaining)
1699 bytes_avail = cfdata->compressed_bytes_remaining;
1701 cab->xstrm.next_in = d;
1702 cab->xstrm.avail_in = bytes_avail;
1703 cab->xstrm.total_in = 0;
1704 r = lzx_decode(&cab->xstrm,
1705 cfdata->compressed_bytes_remaining == bytes_avail);
1711 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1712 "LZX decompression failed (%d)", r);
1713 *avail = ARCHIVE_FATAL;
1716 cfdata->unconsumed = cab->xstrm.total_in;
1717 cfdata->sum_ptr = d;
1718 if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
1719 *avail = ARCHIVE_FATAL;
1724 uavail = cab->xstrm.total_out;
1726 * Make sure a read pointer advances to next CFDATA.
1728 if (cfdata->compressed_bytes_remaining > 0) {
1729 ssize_t bytes_avail;
1731 d = __archive_read_ahead(a, cfdata->compressed_bytes_remaining,
1733 if (bytes_avail <= 0) {
1734 *avail = truncated_error(a);
1737 cfdata->unconsumed = cfdata->compressed_bytes_remaining;
1738 cfdata->sum_ptr = d;
1739 if (cab_minimum_consume_cfdata(a, cfdata->unconsumed) < 0) {
1740 *avail = ARCHIVE_FATAL;
1746 * Translation reversal of x86 proccessor CALL byte sequence(E8).
1748 lzx_translation(&cab->xstrm, cab->uncompressed_buffer,
1749 cfdata->uncompressed_size,
1750 (cab->entry_cffolder->cfdata_index-1) * 0x8000);
1752 d = cab->uncompressed_buffer + cfdata->read_offset;
1753 *avail = uavail - cfdata->read_offset;
1754 cfdata->uncompressed_avail = uavail;
1761 * We always decompress CFDATA to consume CFDATA as much as we need
1762 * in uncompressed bytes because all CFDATA in a folder are related
1763 * so we do not skip any CFDATA without decompressing.
1764 * Note: If the folder of a CFFILE is iFoldCONTINUED_PREV_AND_NEXT or
1765 * iFoldCONTINUED_FROM_PREV, we won't decompress because a CFDATA for
1766 * the CFFILE is remaining bytes of previous Multivolume CAB file.
1769 cab_consume_cfdata(struct archive_read *a, int64_t consumed_bytes)
1771 struct cab *cab = (struct cab *)(a->format->data);
1772 struct cfdata *cfdata;
1773 int64_t cbytes, rbytes;
1776 rbytes = cab_minimum_consume_cfdata(a, consumed_bytes);
1778 return (ARCHIVE_FATAL);
1780 cfdata = cab->entry_cfdata;
1781 while (rbytes > 0) {
1784 if (cfdata->compressed_size == 0) {
1785 archive_set_error(&a->archive,
1786 ARCHIVE_ERRNO_FILE_FORMAT,
1788 return (ARCHIVE_FATAL);
1790 cbytes = cfdata->uncompressed_bytes_remaining;
1791 if (cbytes > rbytes)
1795 if (cfdata->uncompressed_avail == 0 &&
1796 (cab->entry_cffolder->comptype == COMPTYPE_NONE ||
1797 cab->entry_cffile->folder == iFoldCONTINUED_PREV_AND_NEXT ||
1798 cab->entry_cffile->folder == iFoldCONTINUED_FROM_PREV)) {
1799 /* We have not read any data yet. */
1800 if (cbytes == cfdata->uncompressed_bytes_remaining) {
1801 /* Skip whole current CFDATA. */
1802 __archive_read_consume(a,
1803 cfdata->compressed_size);
1804 cab->cab_offset += cfdata->compressed_size;
1805 cfdata->compressed_bytes_remaining = 0;
1806 cfdata->uncompressed_bytes_remaining = 0;
1807 err = cab_next_cfdata(a);
1810 cfdata = cab->entry_cfdata;
1811 if (cfdata->uncompressed_size == 0) {
1812 switch (cab->entry_cffile->folder) {
1813 case iFoldCONTINUED_PREV_AND_NEXT:
1814 case iFoldCONTINUED_TO_NEXT:
1815 case iFoldCONTINUED_FROM_PREV:
1824 cfdata->read_offset += cbytes;
1825 cfdata->uncompressed_bytes_remaining -= cbytes;
1827 } else if (cbytes == 0) {
1828 err = cab_next_cfdata(a);
1831 cfdata = cab->entry_cfdata;
1832 if (cfdata->uncompressed_size == 0) {
1833 switch (cab->entry_cffile->folder) {
1834 case iFoldCONTINUED_PREV_AND_NEXT:
1835 case iFoldCONTINUED_TO_NEXT:
1836 case iFoldCONTINUED_FROM_PREV:
1837 return (ARCHIVE_FATAL);
1844 while (cbytes > 0) {
1845 (void)cab_read_ahead_cfdata(a, &avail);
1847 return (ARCHIVE_FATAL);
1850 if (cab_minimum_consume_cfdata(a, avail) < 0)
1851 return (ARCHIVE_FATAL);
1855 return (consumed_bytes);
1859 * Consume CFDATA as much as we have already gotten and
1860 * compute the sum of CFDATA.
1863 cab_minimum_consume_cfdata(struct archive_read *a, int64_t consumed_bytes)
1865 struct cab *cab = (struct cab *)(a->format->data);
1866 struct cfdata *cfdata;
1867 int64_t cbytes, rbytes;
1870 cfdata = cab->entry_cfdata;
1871 rbytes = consumed_bytes;
1872 if (cab->entry_cffolder->comptype == COMPTYPE_NONE) {
1873 if (consumed_bytes < cfdata->unconsumed)
1874 cbytes = consumed_bytes;
1876 cbytes = cfdata->unconsumed;
1878 cfdata->read_offset += cbytes;
1879 cfdata->uncompressed_bytes_remaining -= cbytes;
1880 cfdata->unconsumed -= cbytes;
1882 cbytes = cfdata->uncompressed_avail - cfdata->read_offset;
1884 if (consumed_bytes < cbytes)
1885 cbytes = consumed_bytes;
1887 cfdata->read_offset += cbytes;
1888 cfdata->uncompressed_bytes_remaining -= cbytes;
1891 if (cfdata->unconsumed) {
1892 cbytes = cfdata->unconsumed;
1893 cfdata->unconsumed = 0;
1898 /* Compute the sum. */
1899 cab_checksum_update(a, cbytes);
1901 /* Consume as much as the compressor actually used. */
1902 __archive_read_consume(a, cbytes);
1903 cab->cab_offset += cbytes;
1904 cfdata->compressed_bytes_remaining -= cbytes;
1905 if (cfdata->compressed_bytes_remaining == 0) {
1906 err = cab_checksum_finish(a);
1915 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1916 * cab->end_of_entry if it consumes all of the data.
1919 cab_read_data(struct archive_read *a, const void **buff,
1920 size_t *size, int64_t *offset)
1922 struct cab *cab = (struct cab *)(a->format->data);
1923 ssize_t bytes_avail;
1925 if (cab->entry_bytes_remaining == 0) {
1928 *offset = cab->entry_offset;
1929 cab->end_of_entry = 1;
1930 return (ARCHIVE_OK);
1933 *buff = cab_read_ahead_cfdata(a, &bytes_avail);
1934 if (bytes_avail <= 0) {
1938 if (bytes_avail == 0 &&
1939 cab->entry_cfdata->uncompressed_size == 0) {
1940 /* All of CFDATA in a folder has been handled. */
1941 archive_set_error(&a->archive,
1942 ARCHIVE_ERRNO_FILE_FORMAT, "Invalid CFDATA");
1943 return (ARCHIVE_FATAL);
1945 return (bytes_avail);
1947 if (bytes_avail > cab->entry_bytes_remaining)
1948 bytes_avail = cab->entry_bytes_remaining;
1950 *size = bytes_avail;
1951 *offset = cab->entry_offset;
1952 cab->entry_offset += bytes_avail;
1953 cab->entry_bytes_remaining -= bytes_avail;
1954 if (cab->entry_bytes_remaining == 0)
1955 cab->end_of_entry = 1;
1956 cab->entry_unconsumed = bytes_avail;
1957 return (ARCHIVE_OK);
1961 archive_read_format_cab_read_data_skip(struct archive_read *a)
1964 int64_t bytes_skipped;
1967 cab = (struct cab *)(a->format->data);
1969 if (cab->end_of_archive)
1970 return (ARCHIVE_EOF);
1972 if (cab->entry_unconsumed) {
1973 /* Consume as much as the compressor actually used. */
1974 r = cab_consume_cfdata(a, cab->entry_unconsumed);
1975 cab->entry_unconsumed = 0;
1978 } else if (cab->entry_cfdata == NULL) {
1979 r = cab_next_cfdata(a);
1984 /* if we've already read to end of data, we're done. */
1985 if (cab->end_of_entry_cleanup)
1986 return (ARCHIVE_OK);
1989 * If the length is at the beginning, we can skip the
1990 * compressed data much more quickly.
1992 bytes_skipped = cab_consume_cfdata(a, cab->entry_bytes_remaining);
1993 if (bytes_skipped < 0)
1994 return (ARCHIVE_FATAL);
1996 /* This entry is finished and done. */
1997 cab->end_of_entry_cleanup = cab->end_of_entry = 1;
1998 return (ARCHIVE_OK);
2002 archive_read_format_cab_cleanup(struct archive_read *a)
2004 struct cab *cab = (struct cab *)(a->format->data);
2005 struct cfheader *hd = &cab->cfheader;
2008 if (hd->folder_array != NULL) {
2009 for (i = 0; i < hd->folder_count; i++)
2010 free(hd->folder_array[i].cfdata.memimage);
2011 free(hd->folder_array);
2013 if (hd->file_array != NULL) {
2014 for (i = 0; i < cab->cfheader.file_count; i++)
2015 archive_string_free(&(hd->file_array[i].pathname));
2016 free(hd->file_array);
2019 if (cab->stream_valid)
2020 inflateEnd(&cab->stream);
2022 lzx_decode_free(&cab->xstrm);
2023 archive_wstring_free(&cab->ws);
2024 free(cab->uncompressed_buffer);
2026 (a->format->data) = NULL;
2027 return (ARCHIVE_OK);
2030 /* Convert an MSDOS-style date/time into Unix-style time. */
2032 cab_dos_time(const unsigned char *p)
2037 msDate = archive_le16dec(p);
2038 msTime = archive_le16dec(p+2);
2040 memset(&ts, 0, sizeof(ts));
2041 ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
2042 ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
2043 ts.tm_mday = msDate & 0x1f; /* Day of month. */
2044 ts.tm_hour = (msTime >> 11) & 0x1f;
2045 ts.tm_min = (msTime >> 5) & 0x3f;
2046 ts.tm_sec = (msTime << 1) & 0x3e;
2048 return (mktime(&ts));
2051 /*****************************************************************
2053 * LZX decompression code.
2055 *****************************************************************/
2058 * Initialize LZX decoder.
2060 * Returns ARCHIVE_OK if initialization was successful.
2061 * Returns ARCHIVE_FAILED if w_bits has unsupported value.
2062 * Returns ARCHIVE_FATAL if initialization failed; memory allocation
2066 lzx_decode_init(struct lzx_stream *strm, int w_bits)
2069 int slot, w_size, w_slot;
2073 if (strm->ds == NULL) {
2074 strm->ds = calloc(1, sizeof(*strm->ds));
2075 if (strm->ds == NULL)
2076 return (ARCHIVE_FATAL);
2079 ds->error = ARCHIVE_FAILED;
2081 /* Allow bits from 15(32KBi) up to 21(2MBi) */
2082 if (w_bits < SLOT_BASE || w_bits > SLOT_MAX)
2083 return (ARCHIVE_FAILED);
2085 ds->error = ARCHIVE_FATAL;
2090 w_size = ds->w_size;
2091 w_slot = slots[w_bits - SLOT_BASE];
2092 ds->w_size = 1U << w_bits;
2093 ds->w_mask = ds->w_size -1;
2094 if (ds->w_buff == NULL || w_size != ds->w_size) {
2096 ds->w_buff = malloc(ds->w_size);
2097 if (ds->w_buff == NULL)
2098 return (ARCHIVE_FATAL);
2100 ds->pos_tbl = malloc(sizeof(ds->pos_tbl[0]) * w_slot);
2101 if (ds->pos_tbl == NULL)
2102 return (ARCHIVE_FATAL);
2103 lzx_huffman_free(&(ds->mt));
2106 for (footer = 0; footer < 18; footer++)
2107 base_inc[footer] = 1 << footer;
2109 for (slot = 0; slot < w_slot; slot++) {
2114 base += base_inc[footer];
2117 for (n = base; n; n >>= 1)
2122 ds->pos_tbl[slot].base = base;
2123 ds->pos_tbl[slot].footer_bits = footer;
2128 ds->br.cache_buffer = 0;
2129 ds->br.cache_avail = 0;
2130 ds->r0 = ds->r1 = ds->r2 = 1;
2132 /* Initialize aligned offset tree. */
2133 if (lzx_huffman_init(&(ds->at), 8, 8) != ARCHIVE_OK)
2134 return (ARCHIVE_FATAL);
2136 /* Initialize pre-tree. */
2137 if (lzx_huffman_init(&(ds->pt), 20, 10) != ARCHIVE_OK)
2138 return (ARCHIVE_FATAL);
2140 /* Initialize Main tree. */
2141 if (lzx_huffman_init(&(ds->mt), 256+(w_slot<<3), 16)
2143 return (ARCHIVE_FATAL);
2145 /* Initialize Length tree. */
2146 if (lzx_huffman_init(&(ds->lt), 249, 16) != ARCHIVE_OK)
2147 return (ARCHIVE_FATAL);
2151 return (ARCHIVE_OK);
2155 * Release LZX decoder.
2158 lzx_decode_free(struct lzx_stream *strm)
2161 if (strm->ds == NULL)
2163 free(strm->ds->w_buff);
2164 free(strm->ds->pos_tbl);
2165 lzx_huffman_free(&(strm->ds->at));
2166 lzx_huffman_free(&(strm->ds->pt));
2167 lzx_huffman_free(&(strm->ds->mt));
2168 lzx_huffman_free(&(strm->ds->lt));
2174 * E8 Call Translation reversal.
2177 lzx_translation(struct lzx_stream *strm, void *p, size_t size, uint32_t offset)
2179 struct lzx_dec *ds = strm->ds;
2180 unsigned char *b, *end;
2182 if (!ds->translation || size <= 10)
2185 end = b + size - 10;
2186 while (b < end && (b = memchr(b, 0xE8, end - b)) != NULL) {
2187 size_t i = b - (unsigned char *)p;
2188 int32_t cp, displacement, value;
2191 value = archive_le32dec(&b[1]);
2192 if (value >= -cp && value < (int32_t)ds->translation_size) {
2194 displacement = value - cp;
2196 displacement = value + ds->translation_size;
2197 archive_le32enc(&b[1], (uint32_t)displacement);
2204 * Bit stream reader.
2206 /* Check that the cache buffer has enough bits. */
2207 #define lzx_br_has(br, n) ((br)->cache_avail >= n)
2208 /* Get compressed data by bit. */
2209 #define lzx_br_bits(br, n) \
2210 (((uint32_t)((br)->cache_buffer >> \
2211 ((br)->cache_avail - (n)))) & cache_masks[n])
2212 #define lzx_br_bits_forced(br, n) \
2213 (((uint32_t)((br)->cache_buffer << \
2214 ((n) - (br)->cache_avail))) & cache_masks[n])
2215 /* Read ahead to make sure the cache buffer has enough compressed data we
2217 * True : completed, there is enough data in the cache buffer.
2218 * False : we met that strm->next_in is empty, we have to get following
2220 #define lzx_br_read_ahead_0(strm, br, n) \
2221 (lzx_br_has((br), (n)) || lzx_br_fillup(strm, br))
2222 /* True : the cache buffer has some bits as much as we need.
2223 * False : there are no enough bits in the cache buffer to be used,
2224 * we have to get following bytes if we could. */
2225 #define lzx_br_read_ahead(strm, br, n) \
2226 (lzx_br_read_ahead_0((strm), (br), (n)) || lzx_br_has((br), (n)))
2228 /* Notify how many bits we consumed. */
2229 #define lzx_br_consume(br, n) ((br)->cache_avail -= (n))
2230 #define lzx_br_consume_unalined_bits(br) ((br)->cache_avail &= ~0x0f)
2232 static const uint32_t cache_masks[] = {
2233 0x00000000, 0x00000001, 0x00000003, 0x00000007,
2234 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
2235 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
2236 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
2237 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
2238 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
2239 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
2240 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
2241 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
2245 * Shift away used bits in the cache data and fill it up with following bits.
2246 * Call this when cache buffer does not have enough bits you need.
2248 * Returns 1 if the cache buffer is full.
2249 * Returns 0 if the cache buffer is not full; input buffer is empty.
2252 lzx_br_fillup(struct lzx_stream *strm, struct lzx_br *br)
2255 * x86 proccessor family can read misaligned data without an access error.
2257 int n = CACHE_BITS - br->cache_avail;
2262 if (strm->avail_in >= 8) {
2264 ((uint64_t)strm->next_in[1]) << 56 |
2265 ((uint64_t)strm->next_in[0]) << 48 |
2266 ((uint64_t)strm->next_in[3]) << 40 |
2267 ((uint64_t)strm->next_in[2]) << 32 |
2268 ((uint32_t)strm->next_in[5]) << 24 |
2269 ((uint32_t)strm->next_in[4]) << 16 |
2270 ((uint32_t)strm->next_in[7]) << 8 |
2271 (uint32_t)strm->next_in[6];
2273 strm->avail_in -= 8;
2274 br->cache_avail += 8 * 8;
2279 if (strm->avail_in >= 6) {
2281 (br->cache_buffer << 48) |
2282 ((uint64_t)strm->next_in[1]) << 40 |
2283 ((uint64_t)strm->next_in[0]) << 32 |
2284 ((uint32_t)strm->next_in[3]) << 24 |
2285 ((uint32_t)strm->next_in[2]) << 16 |
2286 ((uint32_t)strm->next_in[5]) << 8 |
2287 (uint32_t)strm->next_in[4];
2289 strm->avail_in -= 6;
2290 br->cache_avail += 6 * 8;
2295 /* We have enough compressed data in
2296 * the cache buffer.*/
2301 if (strm->avail_in < 2) {
2302 /* There is not enough compressed data to
2303 * fill up the cache buffer. */
2304 if (strm->avail_in == 1) {
2305 br->odd = *strm->next_in++;
2312 (br->cache_buffer << 16) |
2313 archive_le16dec(strm->next_in);
2315 strm->avail_in -= 2;
2316 br->cache_avail += 16;
2322 lzx_br_fixup(struct lzx_stream *strm, struct lzx_br *br)
2324 int n = CACHE_BITS - br->cache_avail;
2326 if (br->have_odd && n >= 16 && strm->avail_in > 0) {
2328 (br->cache_buffer << 16) |
2329 ((uint16_t)(*strm->next_in)) << 8 | br->odd;
2332 br->cache_avail += 16;
2338 lzx_cleanup_bitstream(struct lzx_stream *strm)
2340 strm->ds->br.cache_avail = 0;
2341 strm->ds->br.have_odd = 0;
2347 * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
2348 * Please set available buffer and call this function again.
2349 * 2. Returns ARCHIVE_EOF if decompression has been completed.
2350 * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
2351 * is broken or you do not set 'last' flag properly.
2353 #define ST_RD_TRANSLATION 0
2354 #define ST_RD_TRANSLATION_SIZE 1
2355 #define ST_RD_BLOCK_TYPE 2
2356 #define ST_RD_BLOCK_SIZE 3
2360 #define ST_COPY_UNCOMP1 7
2361 #define ST_COPY_UNCOMP2 8
2362 #define ST_RD_ALIGNED_OFFSET 9
2363 #define ST_RD_VERBATIM 10
2364 #define ST_RD_PRE_MAIN_TREE_256 11
2365 #define ST_MAIN_TREE_256 12
2366 #define ST_RD_PRE_MAIN_TREE_REM 13
2367 #define ST_MAIN_TREE_REM 14
2368 #define ST_RD_PRE_LENGTH_TREE 15
2369 #define ST_LENGTH_TREE 16
2371 #define ST_LENGTH 18
2372 #define ST_OFFSET 19
2373 #define ST_REAL_POS 20
2377 lzx_decode(struct lzx_stream *strm, int last)
2379 struct lzx_dec *ds = strm->ds;
2386 avail_in = strm->avail_in;
2387 lzx_br_fixup(strm, &(ds->br));
2389 if (ds->state < ST_MAIN)
2390 r = lzx_read_blocks(strm, last);
2392 int64_t bytes_written = strm->avail_out;
2393 r = lzx_decode_blocks(strm, last);
2394 bytes_written -= strm->avail_out;
2395 strm->next_out += bytes_written;
2396 strm->total_out += bytes_written;
2399 strm->total_in += avail_in - strm->avail_in;
2404 lzx_read_blocks(struct lzx_stream *strm, int last)
2406 struct lzx_dec *ds = strm->ds;
2407 struct lzx_br *br = &(ds->br);
2411 switch (ds->state) {
2412 case ST_RD_TRANSLATION:
2413 if (!lzx_br_read_ahead(strm, br, 1)) {
2414 ds->state = ST_RD_TRANSLATION;
2417 return (ARCHIVE_OK);
2419 ds->translation = lzx_br_bits(br, 1);
2420 lzx_br_consume(br, 1);
2422 case ST_RD_TRANSLATION_SIZE:
2423 if (ds->translation) {
2424 if (!lzx_br_read_ahead(strm, br, 32)) {
2425 ds->state = ST_RD_TRANSLATION_SIZE;
2428 return (ARCHIVE_OK);
2430 ds->translation_size = lzx_br_bits(br, 16);
2431 lzx_br_consume(br, 16);
2432 ds->translation_size <<= 16;
2433 ds->translation_size |= lzx_br_bits(br, 16);
2434 lzx_br_consume(br, 16);
2437 case ST_RD_BLOCK_TYPE:
2438 if (!lzx_br_read_ahead(strm, br, 3)) {
2439 ds->state = ST_RD_BLOCK_TYPE;
2442 return (ARCHIVE_OK);
2444 ds->block_type = lzx_br_bits(br, 3);
2445 lzx_br_consume(br, 3);
2446 /* Check a block type. */
2447 switch (ds->block_type) {
2448 case VERBATIM_BLOCK:
2449 case ALIGNED_OFFSET_BLOCK:
2450 case UNCOMPRESSED_BLOCK:
2453 goto failed;/* Invalid */
2456 case ST_RD_BLOCK_SIZE:
2457 if (!lzx_br_read_ahead(strm, br, 24)) {
2458 ds->state = ST_RD_BLOCK_SIZE;
2461 return (ARCHIVE_OK);
2463 ds->block_size = lzx_br_bits(br, 8);
2464 lzx_br_consume(br, 8);
2465 ds->block_size <<= 16;
2466 ds->block_size |= lzx_br_bits(br, 16);
2467 lzx_br_consume(br, 16);
2468 if (ds->block_size == 0)
2470 ds->block_bytes_avail = ds->block_size;
2471 if (ds->block_type != UNCOMPRESSED_BLOCK) {
2472 if (ds->block_type == VERBATIM_BLOCK)
2473 ds->state = ST_RD_VERBATIM;
2475 ds->state = ST_RD_ALIGNED_OFFSET;
2479 * Handle an Uncompressed Block.
2481 /* Skip padding to align following field on
2482 * 16-bit boundary. */
2483 if (br->cache_avail == 32 || br->cache_avail == 16)
2484 lzx_br_consume(br, 16);
2486 lzx_br_consume_unalined_bits(br);
2487 /* Preparation to read repeated offsets R0,R1 and R2. */
2488 ds->rbytes_avail = 0;
2489 ds->state = ST_RD_R0;
2496 /* Drain bits in the cache buffer of
2498 if (lzx_br_has(br, 32)) {
2499 u16 = lzx_br_bits(br, 16);
2500 lzx_br_consume(br, 16);
2501 archive_le16enc(ds->rbytes, u16);
2502 u16 = lzx_br_bits(br, 16);
2503 lzx_br_consume(br, 16);
2504 archive_le16enc(ds->rbytes+2, u16);
2505 ds->rbytes_avail = 4;
2506 } else if (lzx_br_has(br, 16)) {
2507 u16 = lzx_br_bits(br, 16);
2508 lzx_br_consume(br, 16);
2509 archive_le16enc(ds->rbytes, u16);
2510 ds->rbytes_avail = 2;
2512 ds->rbytes_avail = 0;
2513 if (ds->rbytes_avail < 4 && ds->br.have_odd) {
2514 ds->rbytes[ds->rbytes_avail++] =
2516 ds->br.have_odd = 0;
2518 while (ds->rbytes_avail < 4) {
2519 if (strm->avail_in <= 0) {
2522 return (ARCHIVE_OK);
2524 ds->rbytes[ds->rbytes_avail++] =
2528 if (ds->state == ST_RD_R0) {
2529 ds->r0 = archive_le32dec(ds->rbytes);
2532 ds->state = ST_RD_R1;
2533 } else if (ds->state == ST_RD_R1) {
2534 ds->r1 = archive_le32dec(ds->rbytes);
2537 ds->state = ST_RD_R2;
2538 } else if (ds->state == ST_RD_R2) {
2539 ds->r2 = archive_le32dec(ds->rbytes);
2542 /* We've gotten all repeated offsets. */
2543 ds->state = ST_COPY_UNCOMP1;
2545 } while (ds->state != ST_COPY_UNCOMP1);
2547 case ST_COPY_UNCOMP1:
2549 * Copy bytes form next_in to next_out directly.
2551 while (ds->block_bytes_avail) {
2555 if (strm->avail_out <= 0)
2556 /* Output buffer is empty. */
2557 return (ARCHIVE_OK);
2558 if (strm->avail_in <= 0) {
2559 /* Input buffer is empty. */
2562 return (ARCHIVE_OK);
2564 l = ds->block_bytes_avail;
2565 if (l > ds->w_size - ds->w_pos)
2566 l = ds->w_size - ds->w_pos;
2567 if (l > strm->avail_out)
2568 l = (int)strm->avail_out;
2569 if (l > strm->avail_in)
2570 l = (int)strm->avail_in;
2572 d = &(ds->w_buff[ds->w_pos]);
2574 *strm->next_out++ = *strm->next_in;
2575 *d++ = *strm->next_in++;
2577 strm->avail_out -= ll;
2578 strm->total_out += ll;
2579 strm->avail_in -= ll;
2580 ds->w_pos = (ds->w_pos + ll) & ds->w_mask;
2581 ds->block_bytes_avail -= ll;
2584 case ST_COPY_UNCOMP2:
2585 /* Re-align; skip padding byte. */
2586 if (ds->block_size & 1) {
2587 if (strm->avail_in <= 0) {
2588 /* Input buffer is empty. */
2589 ds->state = ST_COPY_UNCOMP2;
2592 return (ARCHIVE_OK);
2597 /* This block ended. */
2598 ds->state = ST_RD_BLOCK_TYPE;
2599 return (ARCHIVE_EOF);
2600 /********************/
2601 case ST_RD_ALIGNED_OFFSET:
2603 * Read Aligned offset tree.
2605 if (!lzx_br_read_ahead(strm, br, 3 * ds->at.len_size)) {
2606 ds->state = ST_RD_ALIGNED_OFFSET;
2609 return (ARCHIVE_OK);
2611 memset(ds->at.freq, 0, sizeof(ds->at.freq));
2612 for (i = 0; i < ds->at.len_size; i++) {
2613 ds->at.bitlen[i] = lzx_br_bits(br, 3);
2614 ds->at.freq[ds->at.bitlen[i]]++;
2615 lzx_br_consume(br, 3);
2617 if (!lzx_make_huffman_table(&ds->at))
2620 case ST_RD_VERBATIM:
2623 case ST_RD_PRE_MAIN_TREE_256:
2625 * Read Pre-tree for first 256 elements of main tree.
2627 if (!lzx_read_pre_tree(strm)) {
2628 ds->state = ST_RD_PRE_MAIN_TREE_256;
2631 return (ARCHIVE_OK);
2633 if (!lzx_make_huffman_table(&ds->pt))
2637 case ST_MAIN_TREE_256:
2639 * Get path lengths of first 256 elements of main tree.
2641 r = lzx_read_bitlen(strm, &ds->mt, 256);
2645 ds->state = ST_MAIN_TREE_256;
2648 return (ARCHIVE_OK);
2652 case ST_RD_PRE_MAIN_TREE_REM:
2654 * Read Pre-tree for remaining elements of main tree.
2656 if (!lzx_read_pre_tree(strm)) {
2657 ds->state = ST_RD_PRE_MAIN_TREE_REM;
2660 return (ARCHIVE_OK);
2662 if (!lzx_make_huffman_table(&ds->pt))
2666 case ST_MAIN_TREE_REM:
2668 * Get path lengths of remaining elements of main tree.
2670 r = lzx_read_bitlen(strm, &ds->mt, -1);
2674 ds->state = ST_MAIN_TREE_REM;
2677 return (ARCHIVE_OK);
2679 if (!lzx_make_huffman_table(&ds->mt))
2683 case ST_RD_PRE_LENGTH_TREE:
2685 * Read Pre-tree for remaining elements of main tree.
2687 if (!lzx_read_pre_tree(strm)) {
2688 ds->state = ST_RD_PRE_LENGTH_TREE;
2691 return (ARCHIVE_OK);
2693 if (!lzx_make_huffman_table(&ds->pt))
2697 case ST_LENGTH_TREE:
2699 * Get path lengths of remaining elements of main tree.
2701 r = lzx_read_bitlen(strm, &ds->lt, -1);
2705 ds->state = ST_LENGTH_TREE;
2708 return (ARCHIVE_OK);
2710 if (!lzx_make_huffman_table(&ds->lt))
2712 ds->state = ST_MAIN;
2717 return (ds->error = ARCHIVE_FAILED);
2721 lzx_decode_blocks(struct lzx_stream *strm, int last)
2723 struct lzx_dec *ds = strm->ds;
2724 struct lzx_br bre = ds->br;
2725 struct huffman *at = &(ds->at), *lt = &(ds->lt), *mt = &(ds->mt);
2726 const struct lzx_pos_tbl *pos_tbl = ds->pos_tbl;
2727 unsigned char *outp = strm->next_out;
2728 unsigned char *endp = outp + strm->avail_out;
2729 unsigned char *w_buff = ds->w_buff;
2730 unsigned char *at_bitlen = at->bitlen;
2731 unsigned char *lt_bitlen = lt->bitlen;
2732 unsigned char *mt_bitlen = mt->bitlen;
2733 size_t block_bytes_avail = ds->block_bytes_avail;
2734 int at_max_bits = at->max_bits;
2735 int lt_max_bits = lt->max_bits;
2736 int mt_max_bits = mt->max_bits;
2737 int c, copy_len = ds->copy_len, copy_pos = ds->copy_pos;
2738 int w_pos = ds->w_pos, w_mask = ds->w_mask, w_size = ds->w_size;
2739 int length_header = ds->length_header;
2740 int offset_bits = ds->offset_bits;
2741 int position_slot = ds->position_slot;
2742 int r0 = ds->r0, r1 = ds->r1, r2 = ds->r2;
2743 int state = ds->state;
2744 char block_type = ds->block_type;
2750 if (block_bytes_avail == 0) {
2751 /* This block ended. */
2752 ds->state = ST_RD_BLOCK_TYPE;
2754 ds->block_bytes_avail =
2756 ds->copy_len = copy_len;
2757 ds->copy_pos = copy_pos;
2758 ds->length_header = length_header;
2759 ds->position_slot = position_slot;
2760 ds->r0 = r0; ds->r1 = r1; ds->r2 = r2;
2762 strm->avail_out = endp - outp;
2763 return (ARCHIVE_EOF);
2766 /* Output buffer is empty. */
2769 if (!lzx_br_read_ahead(strm, &bre,
2773 /* Remaining bits are less than
2774 * maximum bits(mt.max_bits) but maybe
2775 * it still remains as much as we need,
2776 * so we should try to use it with
2778 c = lzx_decode_huffman(mt,
2780 &bre, mt_max_bits));
2781 lzx_br_consume(&bre, mt_bitlen[c]);
2782 if (!lzx_br_has(&bre, 0))
2783 goto failed;/* Over read. */
2785 c = lzx_decode_huffman(mt,
2786 lzx_br_bits(&bre, mt_max_bits));
2787 lzx_br_consume(&bre, mt_bitlen[c]);
2792 * 'c' is exactly literal code.
2794 /* Save a decoded code to reference it
2797 w_pos = (w_pos + 1) & w_mask;
2798 /* Store the decoded code to output buffer. */
2800 block_bytes_avail--;
2803 * Get a match code, its length and offset.
2806 length_header = c & 7;
2807 position_slot = c >> 3;
2813 if (length_header == 7) {
2814 if (!lzx_br_read_ahead(strm, &bre,
2820 c = lzx_decode_huffman(lt,
2822 &bre, lt_max_bits));
2823 lzx_br_consume(&bre, lt_bitlen[c]);
2824 if (!lzx_br_has(&bre, 0))
2825 goto failed;/* Over read. */
2827 c = lzx_decode_huffman(lt,
2828 lzx_br_bits(&bre, lt_max_bits));
2829 lzx_br_consume(&bre, lt_bitlen[c]);
2831 copy_len = c + 7 + 2;
2833 copy_len = length_header + 2;
2834 if ((size_t)copy_len > block_bytes_avail)
2839 switch (position_slot) {
2840 case 0: /* Use repeated offset 0. */
2842 state = ST_REAL_POS;
2844 case 1: /* Use repeated offset 1. */
2846 /* Swap repeated offset. */
2849 state = ST_REAL_POS;
2851 case 2: /* Use repeated offset 2. */
2853 /* Swap repeated offset. */
2856 state = ST_REAL_POS;
2860 pos_tbl[position_slot].footer_bits;
2866 * Get the offset, which is a distance from
2867 * current window position.
2869 if (block_type == ALIGNED_OFFSET_BLOCK &&
2871 int offbits = offset_bits - 3;
2873 if (!lzx_br_read_ahead(strm, &bre, offbits)) {
2879 copy_pos = lzx_br_bits(&bre, offbits) << 3;
2881 /* Get an aligned number. */
2882 if (!lzx_br_read_ahead(strm, &bre,
2883 offbits + at_max_bits)) {
2888 lzx_br_consume(&bre, offbits);
2889 c = lzx_decode_huffman(at,
2890 lzx_br_bits_forced(&bre,
2892 lzx_br_consume(&bre, at_bitlen[c]);
2893 if (!lzx_br_has(&bre, 0))
2894 goto failed;/* Over read. */
2896 lzx_br_consume(&bre, offbits);
2897 c = lzx_decode_huffman(at,
2898 lzx_br_bits(&bre, at_max_bits));
2899 lzx_br_consume(&bre, at_bitlen[c]);
2901 /* Add an aligned number. */
2904 if (!lzx_br_read_ahead(strm, &bre,
2911 copy_pos = lzx_br_bits(&bre, offset_bits);
2912 lzx_br_consume(&bre, offset_bits);
2914 copy_pos += pos_tbl[position_slot].base -2;
2916 /* Update repeated offset LRU queue. */
2923 * Compute a real position in window.
2925 copy_pos = (w_pos - copy_pos) & w_mask;
2929 * Copy several bytes as extracted data from the window
2930 * into the output buffer.
2933 const unsigned char *s;
2937 if (copy_pos > w_pos) {
2938 if (l > w_size - copy_pos)
2939 l = w_size - copy_pos;
2941 if (l > w_size - w_pos)
2944 if (outp + l >= endp)
2946 s = w_buff + copy_pos;
2947 if (l >= 8 && ((copy_pos + l < w_pos)
2948 || (w_pos + l < copy_pos))) {
2949 memcpy(w_buff + w_pos, s, l);
2956 for (li = 0; li < l; li++)
2957 outp[li] = d[li] = s[li];
2960 copy_pos = (copy_pos + l) & w_mask;
2961 w_pos = (w_pos + l) & w_mask;
2962 block_bytes_avail -= l;
2964 /* A copy of current pattern ended. */
2968 /* Output buffer is empty. */
2978 return (ds->error = ARCHIVE_FAILED);
2981 ds->block_bytes_avail = block_bytes_avail;
2982 ds->copy_len = copy_len;
2983 ds->copy_pos = copy_pos;
2984 ds->length_header = length_header;
2985 ds->offset_bits = offset_bits;
2986 ds->position_slot = position_slot;
2987 ds->r0 = r0; ds->r1 = r1; ds->r2 = r2;
2990 strm->avail_out = endp - outp;
2991 return (ARCHIVE_OK);
2995 lzx_read_pre_tree(struct lzx_stream *strm)
2997 struct lzx_dec *ds = strm->ds;
2998 struct lzx_br *br = &(ds->br);
3002 memset(ds->pt.freq, 0, sizeof(ds->pt.freq));
3003 for (i = ds->loop; i < ds->pt.len_size; i++) {
3004 if (!lzx_br_read_ahead(strm, br, 4)) {
3008 ds->pt.bitlen[i] = lzx_br_bits(br, 4);
3009 ds->pt.freq[ds->pt.bitlen[i]]++;
3010 lzx_br_consume(br, 4);
3017 * Read a bunch of bit-lengths from pre-tree.
3020 lzx_read_bitlen(struct lzx_stream *strm, struct huffman *d, int end)
3022 struct lzx_dec *ds = strm->ds;
3023 struct lzx_br *br = &(ds->br);
3024 int c, i, j, ret, same;
3029 memset(d->freq, 0, sizeof(d->freq));
3035 if (!lzx_br_read_ahead(strm, br, ds->pt.max_bits))
3037 rbits = lzx_br_bits(br, ds->pt.max_bits);
3038 c = lzx_decode_huffman(&(ds->pt), rbits);
3040 case 17:/* several zero lengths, from 4 to 19. */
3041 if (!lzx_br_read_ahead(strm, br, ds->pt.bitlen[c]+4))
3043 lzx_br_consume(br, ds->pt.bitlen[c]);
3044 same = lzx_br_bits(br, 4) + 4;
3046 return (-1);/* Invalid */
3047 lzx_br_consume(br, 4);
3048 for (j = 0; j < same; j++)
3051 case 18:/* many zero lengths, from 20 to 51. */
3052 if (!lzx_br_read_ahead(strm, br, ds->pt.bitlen[c]+5))
3054 lzx_br_consume(br, ds->pt.bitlen[c]);
3055 same = lzx_br_bits(br, 5) + 20;
3057 return (-1);/* Invalid */
3058 lzx_br_consume(br, 5);
3059 memset(d->bitlen + i, 0, same);
3062 case 19:/* a few same lengths. */
3063 if (!lzx_br_read_ahead(strm, br,
3064 ds->pt.bitlen[c]+1+ds->pt.max_bits))
3066 lzx_br_consume(br, ds->pt.bitlen[c]);
3067 same = lzx_br_bits(br, 1) + 4;
3070 lzx_br_consume(br, 1);
3071 rbits = lzx_br_bits(br, ds->pt.max_bits);
3072 c = lzx_decode_huffman(&(ds->pt), rbits);
3073 lzx_br_consume(br, ds->pt.bitlen[c]);
3074 c = (d->bitlen[i] - c + 17) % 17;
3076 return (-1);/* Invalid */
3077 for (j = 0; j < same; j++)
3082 lzx_br_consume(br, ds->pt.bitlen[c]);
3083 c = (d->bitlen[i] - c + 17) % 17;
3085 return (-1);/* Invalid */
3098 lzx_huffman_init(struct huffman *hf, size_t len_size, int tbl_bits)
3102 if (hf->bitlen == NULL || hf->len_size != (int)len_size) {
3104 hf->bitlen = calloc(len_size, sizeof(hf->bitlen[0]));
3105 if (hf->bitlen == NULL)
3106 return (ARCHIVE_FATAL);
3107 hf->len_size = len_size;
3109 memset(hf->bitlen, 0, len_size * sizeof(hf->bitlen[0]));
3110 if (hf->tbl == NULL) {
3111 if (tbl_bits < HTBL_BITS)
3115 hf->tbl = malloc((1 << bits) * sizeof(hf->tbl[0]));
3116 if (hf->tbl == NULL)
3117 return (ARCHIVE_FATAL);
3118 hf->tbl_bits = tbl_bits;
3120 if (hf->tree == NULL && tbl_bits > HTBL_BITS) {
3121 hf->tree_avail = 1 << (tbl_bits - HTBL_BITS + 4);
3122 hf->tree = malloc(hf->tree_avail * sizeof(hf->tree[0]));
3123 if (hf->tree == NULL)
3124 return (ARCHIVE_FATAL);
3126 return (ARCHIVE_OK);
3130 lzx_huffman_free(struct huffman *hf)
3138 * Make a huffman coding table.
3141 lzx_make_huffman_table(struct huffman *hf)
3144 const unsigned char *bitlen;
3145 int bitptn[17], weight[17];
3146 int i, maxbits = 0, ptn, tbl_size, w;
3147 int diffbits, len_avail;
3150 * Initialize bit patterns.
3153 for (i = 1, w = 1 << 15; i <= 16; i++, w >>= 1) {
3157 ptn += hf->freq[i] * w;
3161 if ((ptn & 0xffff) != 0 || maxbits > hf->tbl_bits)
3162 return (0);/* Invalid */
3164 hf->max_bits = maxbits;
3167 * Cut out extra bits which we won't house in the table.
3168 * This preparation reduces the same calculation in the for-loop
3172 int ebits = 16 - maxbits;
3173 for (i = 1; i <= maxbits; i++) {
3174 bitptn[i] >>= ebits;
3175 weight[i] >>= ebits;
3178 if (maxbits > HTBL_BITS) {
3182 diffbits = maxbits - HTBL_BITS;
3183 for (i = 1; i <= HTBL_BITS; i++) {
3184 bitptn[i] >>= diffbits;
3185 weight[i] >>= diffbits;
3187 htbl_max = bitptn[HTBL_BITS] +
3188 weight[HTBL_BITS] * hf->freq[HTBL_BITS];
3189 p = &(hf->tbl[htbl_max]);
3190 while (p < &hf->tbl[1U<<HTBL_BITS])
3194 hf->shift_bits = diffbits;
3199 tbl_size = 1 << HTBL_BITS;
3201 bitlen = hf->bitlen;
3202 len_avail = hf->len_size;
3204 for (i = 0; i < len_avail; i++) {
3213 /* Get a bit pattern */
3217 if (len <= HTBL_BITS) {
3218 /* Calculate next bit pattern */
3219 if ((bitptn[len] = ptn + cnt) > tbl_size)
3220 return (0);/* Invalid */
3221 /* Update the table */
3224 p[cnt] = (uint16_t)i;
3229 * A bit length is too big to be housed to a direct table,
3230 * so we use a tree model for its extra bits.
3232 bitptn[len] = ptn + cnt;
3233 bit = 1U << (diffbits -1);
3234 extlen = len - HTBL_BITS;
3236 p = &(tbl[ptn >> diffbits]);
3238 *p = len_avail + hf->tree_used;
3239 ht = &(hf->tree[hf->tree_used++]);
3240 if (hf->tree_used > hf->tree_avail)
3241 return (0);/* Invalid */
3245 if (*p < len_avail ||
3246 *p >= (len_avail + hf->tree_used))
3247 return (0);/* Invalid */
3248 ht = &(hf->tree[*p - len_avail]);
3250 while (--extlen > 0) {
3252 if (ht->left < len_avail) {
3253 ht->left = len_avail + hf->tree_used;
3254 ht = &(hf->tree[hf->tree_used++]);
3255 if (hf->tree_used > hf->tree_avail)
3256 return (0);/* Invalid */
3260 ht = &(hf->tree[ht->left - len_avail]);
3263 if (ht->right < len_avail) {
3264 ht->right = len_avail + hf->tree_used;
3265 ht = &(hf->tree[hf->tree_used++]);
3266 if (hf->tree_used > hf->tree_avail)
3267 return (0);/* Invalid */
3271 ht = &(hf->tree[ht->right - len_avail]);
3278 return (0);/* Invalid */
3279 ht->left = (uint16_t)i;
3282 return (0);/* Invalid */
3283 ht->right = (uint16_t)i;
3290 lzx_decode_huffman_tree(struct huffman *hf, unsigned rbits, int c)
3296 extlen = hf->shift_bits;
3297 while (c >= hf->len_size) {
3299 if (extlen-- <= 0 || c >= hf->tree_used)
3301 if (rbits & (1U << extlen))
3310 lzx_decode_huffman(struct huffman *hf, unsigned rbits)
3314 * At first search an index table for a bit pattern.
3315 * If it fails, search a huffman tree for.
3317 c = hf->tbl[rbits >> hf->shift_bits];
3318 if (c < hf->len_size)
3320 /* This bit pattern needs to be found out at a huffman tree. */
3321 return (lzx_decode_huffman_tree(hf, rbits, c));