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
;
295 char read_data_invoked
;
296 int64_t bytes_skipped
;
298 unsigned char *uncompressed_buffer
;
299 size_t uncompressed_buffer_size
;
301 int init_default_conversion
;
302 struct archive_string_conv
*sconv
;
303 struct archive_string_conv
*sconv_default
;
304 struct archive_string_conv
*sconv_utf8
;
305 char format_name
[64];
311 struct lzx_stream xstrm
;
314 static int archive_read_format_cab_bid(struct archive_read
*, int);
315 static int archive_read_format_cab_options(struct archive_read
*,
316 const char *, const char *);
317 static int archive_read_format_cab_read_header(struct archive_read
*,
318 struct archive_entry
*);
319 static int archive_read_format_cab_read_data(struct archive_read
*,
320 const void **, size_t *, int64_t *);
321 static int archive_read_format_cab_read_data_skip(struct archive_read
*);
322 static int archive_read_format_cab_cleanup(struct archive_read
*);
324 static int cab_skip_sfx(struct archive_read
*);
325 static time_t cab_dos_time(const unsigned char *);
326 static int cab_read_data(struct archive_read
*, const void **,
327 size_t *, int64_t *);
328 static int cab_read_header(struct archive_read
*);
329 static uint32_t cab_checksum_cfdata_4(const void *, size_t bytes
, uint32_t);
330 static uint32_t cab_checksum_cfdata(const void *, size_t bytes
, uint32_t);
331 static void cab_checksum_update(struct archive_read
*, size_t);
332 static int cab_checksum_finish(struct archive_read
*);
333 static int cab_next_cfdata(struct archive_read
*);
334 static const void *cab_read_ahead_cfdata(struct archive_read
*, ssize_t
*);
335 static const void *cab_read_ahead_cfdata_none(struct archive_read
*, ssize_t
*);
336 static const void *cab_read_ahead_cfdata_deflate(struct archive_read
*,
338 static const void *cab_read_ahead_cfdata_lzx(struct archive_read
*,
340 static int64_t cab_consume_cfdata(struct archive_read
*, int64_t);
341 static int64_t cab_minimum_consume_cfdata(struct archive_read
*, int64_t);
342 static int lzx_decode_init(struct lzx_stream
*, int);
343 static int lzx_read_blocks(struct lzx_stream
*, int);
344 static int lzx_decode_blocks(struct lzx_stream
*, int);
345 static void lzx_decode_free(struct lzx_stream
*);
346 static void lzx_translation(struct lzx_stream
*, void *, size_t, uint32_t);
347 static void lzx_cleanup_bitstream(struct lzx_stream
*);
348 static int lzx_decode(struct lzx_stream
*, int);
349 static int lzx_read_pre_tree(struct lzx_stream
*);
350 static int lzx_read_bitlen(struct lzx_stream
*, struct huffman
*, int);
351 static int lzx_huffman_init(struct huffman
*, size_t, int);
352 static void lzx_huffman_free(struct huffman
*);
353 static int lzx_make_huffman_table(struct huffman
*);
354 static inline int lzx_decode_huffman(struct huffman
*, unsigned);
355 static int lzx_decode_huffman_tree(struct huffman
*, unsigned, int);
359 archive_read_support_format_cab(struct archive
*_a
)
361 struct archive_read
*a
= (struct archive_read
*)_a
;
365 archive_check_magic(_a
, ARCHIVE_READ_MAGIC
,
366 ARCHIVE_STATE_NEW
, "archive_read_support_format_cab");
368 cab
= (struct cab
*)calloc(1, sizeof(*cab
));
370 archive_set_error(&a
->archive
, ENOMEM
,
371 "Can't allocate CAB data");
372 return (ARCHIVE_FATAL
);
374 archive_string_init(&cab
->ws
);
375 archive_wstring_ensure(&cab
->ws
, 256);
377 r
= __archive_read_register_format(a
,
380 archive_read_format_cab_bid
,
381 archive_read_format_cab_options
,
382 archive_read_format_cab_read_header
,
383 archive_read_format_cab_read_data
,
384 archive_read_format_cab_read_data_skip
,
386 archive_read_format_cab_cleanup
,
396 find_cab_magic(const char *p
)
401 * Note: Self-Extraction program has 'MSCF' string in their
402 * program. If we were finding 'MSCF' string only, we got
403 * wrong place for Cabinet header, thus, we have to check
404 * following four bytes which are reserved and must be set
407 if (memcmp(p
, "MSCF\0\0\0\0", 8) == 0)
419 archive_read_format_cab_bid(struct archive_read
*a
, int best_bid
)
422 ssize_t bytes_avail
, offset
, window
;
424 /* If there's already a better bid than we can ever
425 make, don't bother testing. */
429 if ((p
= __archive_read_ahead(a
, 8, NULL
)) == NULL
)
432 if (memcmp(p
, "MSCF\0\0\0\0", 8) == 0)
436 * Attempt to handle self-extracting archives
437 * by noting a PE header and searching forward
438 * up to 128k for a 'MSCF' marker.
440 if (p
[0] == 'M' && p
[1] == 'Z') {
443 while (offset
< (1024 * 128)) {
444 const char *h
= __archive_read_ahead(a
, offset
+ window
,
447 /* Remaining bytes are less than window. */
454 while (p
+ 8 < h
+ bytes_avail
) {
456 if ((next
= find_cab_magic(p
)) == 0)
467 archive_read_format_cab_options(struct archive_read
*a
,
468 const char *key
, const char *val
)
471 int ret
= ARCHIVE_FAILED
;
473 cab
= (struct cab
*)(a
->format
->data
);
474 if (strcmp(key
, "hdrcharset") == 0) {
475 if (val
== NULL
|| val
[0] == 0)
476 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
477 "cab: hdrcharset option needs a character-set name");
479 cab
->sconv
= archive_string_conversion_from_charset(
480 &a
->archive
, val
, 0);
481 if (cab
->sconv
!= NULL
)
489 /* Note: The "warn" return is just to inform the options
490 * supervisor that we didn't handle it. It will generate
491 * a suitable error if no one used this option. */
492 return (ARCHIVE_WARN
);
496 cab_skip_sfx(struct archive_read
*a
)
500 ssize_t bytes
, window
;
504 const char *h
= __archive_read_ahead(a
, window
, &bytes
);
506 /* Remaining size are less than window. */
509 archive_set_error(&a
->archive
,
510 ARCHIVE_ERRNO_FILE_FORMAT
,
511 "Couldn't find out CAB header");
512 return (ARCHIVE_FATAL
);
520 * Scan ahead until we find something that looks
521 * like the cab header.
525 if ((next
= find_cab_magic(p
)) == 0) {
527 __archive_read_consume(a
, skip
);
533 __archive_read_consume(a
, skip
);
538 truncated_error(struct archive_read
*a
)
540 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
541 "Truncated CAB header");
542 return (ARCHIVE_FATAL
);
546 cab_strnlen(const unsigned char *p
, size_t maxlen
)
550 for (i
= 0; i
<= maxlen
; i
++) {
555 return (-1);/* invalid */
559 /* Read bytes as much as remaining. */
561 cab_read_ahead_remaining(struct archive_read
*a
, size_t min
, ssize_t
*avail
)
566 p
= __archive_read_ahead(a
, min
, avail
);
574 /* Convert a path separator '\' -> '/' */
576 cab_convert_path_separator_1(struct archive_string
*fn
, unsigned char attr
)
581 /* Easy check if we have '\' in multi-byte string. */
583 for (i
= 0; i
< archive_strlen(fn
); i
++) {
584 if (fn
->s
[i
] == '\\') {
586 /* This may be second byte of multi-byte
592 } else if ((fn
->s
[i
] & 0x80) && !(attr
& ATTR_NAME_IS_UTF
))
597 if (i
== archive_strlen(fn
))
603 * Replace a character '\' with '/' in wide character.
606 cab_convert_path_separator_2(struct cab
*cab
, struct archive_entry
*entry
)
611 /* If a conversion to wide character failed, force the replacement. */
612 if ((wp
= archive_entry_pathname_w(entry
)) != NULL
) {
613 archive_wstrcpy(&(cab
->ws
), wp
);
614 for (i
= 0; i
< archive_strlen(&(cab
->ws
)); i
++) {
615 if (cab
->ws
.s
[i
] == L
'\\')
618 archive_entry_copy_pathname_w(entry
, cab
->ws
.s
);
623 * Read CFHEADER, CFFOLDER and CFFILE.
626 cab_read_header(struct archive_read
*a
)
628 const unsigned char *p
;
635 int cur_folder
, prev_folder
;
638 a
->archive
.archive_format
= ARCHIVE_FORMAT_CAB
;
639 if (a
->archive
.archive_format_name
== NULL
)
640 a
->archive
.archive_format_name
= "CAB";
642 if ((p
= __archive_read_ahead(a
, 42, NULL
)) == NULL
)
643 return (truncated_error(a
));
645 cab
= (struct cab
*)(a
->format
->data
);
646 if (cab
->found_header
== 0 &&
647 p
[0] == 'M' && p
[1] == 'Z') {
648 /* This is an executable? Must be self-extracting... */
649 err
= cab_skip_sfx(a
);
650 if (err
< ARCHIVE_WARN
)
653 if ((p
= __archive_read_ahead(a
, sizeof(*p
), NULL
)) == NULL
)
654 return (truncated_error(a
));
662 if (p
[CFHEADER_signature
+0] != 'M' || p
[CFHEADER_signature
+1] != 'S' ||
663 p
[CFHEADER_signature
+2] != 'C' || p
[CFHEADER_signature
+3] != 'F') {
664 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
665 "Couldn't find out CAB header");
666 return (ARCHIVE_FATAL
);
668 hd
->total_bytes
= archive_le32dec(p
+ CFHEADER_cbCabinet
);
669 hd
->files_offset
= archive_le32dec(p
+ CFHEADER_coffFiles
);
670 hd
->minor
= p
[CFHEADER_versionMinor
];
671 hd
->major
= p
[CFHEADER_versionMajor
];
672 hd
->folder_count
= archive_le16dec(p
+ CFHEADER_cFolders
);
673 if (hd
->folder_count
== 0)
675 hd
->file_count
= archive_le16dec(p
+ CFHEADER_cFiles
);
676 if (hd
->file_count
== 0)
678 hd
->flags
= archive_le16dec(p
+ CFHEADER_flags
);
679 hd
->setid
= archive_le16dec(p
+ CFHEADER_setID
);
680 hd
->cabinet
= archive_le16dec(p
+ CFHEADER_iCabinet
);
681 used
= CFHEADER_iCabinet
+ 2;
682 if (hd
->flags
& RESERVE_PRESENT
) {
684 cfheader
= archive_le16dec(p
+ CFHEADER_cbCFHeader
);
685 if (cfheader
> 60000U)
687 hd
->cffolder
= p
[CFHEADER_cbCFFolder
];
688 hd
->cfdata
= p
[CFHEADER_cbCFData
];
689 used
+= 4;/* cbCFHeader, cbCFFolder and cbCFData */
690 used
+= cfheader
;/* abReserve */
692 hd
->cffolder
= 0;/* Avoid compiling warning. */
693 if (hd
->flags
& PREV_CABINET
) {
694 /* How many bytes are used for szCabinetPrev. */
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)
700 /* How many bytes are used for szDiskPrev. */
701 if ((p
= __archive_read_ahead(a
, used
+256, NULL
)) == NULL
)
702 return (truncated_error(a
));
703 if ((len
= cab_strnlen(p
+ used
, 255)) <= 0)
707 if (hd
->flags
& NEXT_CABINET
) {
708 /* How many bytes are used for szCabinetNext. */
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)
714 /* How many bytes are used for szDiskNext. */
715 if ((p
= __archive_read_ahead(a
, used
+256, NULL
)) == NULL
)
716 return (truncated_error(a
));
717 if ((len
= cab_strnlen(p
+ used
, 255)) <= 0)
721 __archive_read_consume(a
, used
);
722 cab
->cab_offset
+= used
;
728 hd
->folder_array
= (struct cffolder
*)calloc(
729 hd
->folder_count
, sizeof(struct cffolder
));
730 if (hd
->folder_array
== NULL
)
734 if (hd
->flags
& RESERVE_PRESENT
)
735 bytes
+= hd
->cffolder
;
736 bytes
*= hd
->folder_count
;
737 if ((p
= __archive_read_ahead(a
, bytes
, NULL
)) == NULL
)
738 return (truncated_error(a
));
740 for (i
= 0; i
< hd
->folder_count
; i
++) {
741 struct cffolder
*folder
= &(hd
->folder_array
[i
]);
742 folder
->cfdata_offset_in_cab
=
743 archive_le32dec(p
+ CFFOLDER_coffCabStart
);
744 folder
->cfdata_count
= archive_le16dec(p
+CFFOLDER_cCFData
);
746 archive_le16dec(p
+CFFOLDER_typeCompress
) & 0x0F;
748 archive_le16dec(p
+CFFOLDER_typeCompress
) >> 8;
749 /* Get a compression name. */
750 if (folder
->comptype
<
751 sizeof(compression_name
) / sizeof(compression_name
[0]))
752 folder
->compname
= compression_name
[folder
->comptype
];
754 folder
->compname
= "UNKNOWN";
757 if (hd
->flags
& RESERVE_PRESENT
) {
758 p
+= hd
->cffolder
;/* abReserve */
759 used
+= hd
->cffolder
;
762 * Sanity check if each data is acceptable.
764 if (offset32
>= folder
->cfdata_offset_in_cab
)
766 offset32
= folder
->cfdata_offset_in_cab
;
768 /* Set a request to initialize zlib for the CFDATA of
770 folder
->decompress_init
= 0;
772 __archive_read_consume(a
, used
);
773 cab
->cab_offset
+= used
;
778 /* Seek read pointer to the offset of CFFILE if needed. */
779 skip
= (int64_t)hd
->files_offset
- cab
->cab_offset
;
781 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
782 "Invalid offset of CFFILE %jd < %jd",
783 (intmax_t)hd
->files_offset
, (intmax_t)cab
->cab_offset
);
784 return (ARCHIVE_FATAL
);
787 __archive_read_consume(a
, skip
);
788 cab
->cab_offset
+= skip
;
790 /* Allocate memory for CFDATA */
791 hd
->file_array
= (struct cffile
*)calloc(
792 hd
->file_count
, sizeof(struct cffile
));
793 if (hd
->file_array
== NULL
)
797 for (i
= 0; i
< hd
->file_count
; i
++) {
798 struct cffile
*file
= &(hd
->file_array
[i
]);
801 if ((p
= __archive_read_ahead(a
, 16, NULL
)) == NULL
)
802 return (truncated_error(a
));
803 file
->uncompressed_size
= archive_le32dec(p
+ CFFILE_cbFile
);
804 file
->offset
= archive_le32dec(p
+ CFFILE_uoffFolderStart
);
805 file
->folder
= archive_le16dec(p
+ CFFILE_iFolder
);
806 file
->mtime
= cab_dos_time(p
+ CFFILE_date_time
);
807 file
->attr
= (uint8_t)archive_le16dec(p
+ CFFILE_attribs
);
808 __archive_read_consume(a
, 16);
810 cab
->cab_offset
+= 16;
811 if ((p
= cab_read_ahead_remaining(a
, 256, &avail
)) == NULL
)
812 return (truncated_error(a
));
813 if ((len
= cab_strnlen(p
, avail
-1)) <= 0)
816 /* Copy a pathname. */
817 archive_string_init(&(file
->pathname
));
818 archive_strncpy(&(file
->pathname
), p
, len
);
819 __archive_read_consume(a
, len
+ 1);
820 cab
->cab_offset
+= len
+ 1;
823 * Sanity check if each data is acceptable.
825 if (file
->uncompressed_size
> 0x7FFF8000)
826 goto invalid
;/* Too large */
827 if ((int64_t)file
->offset
+ (int64_t)file
->uncompressed_size
828 > ARCHIVE_LITERAL_LL(0x7FFF8000))
829 goto invalid
;/* Too large */
830 switch (file
->folder
) {
831 case iFoldCONTINUED_TO_NEXT
:
832 /* This must be last file in a folder. */
833 if (i
!= hd
->file_count
-1)
835 cur_folder
= hd
->folder_count
-1;
837 case iFoldCONTINUED_PREV_AND_NEXT
:
838 /* This must be only one file in a folder. */
839 if (hd
->file_count
!= 1)
842 case iFoldCONTINUED_FROM_PREV
:
843 /* This must be first file in a folder. */
846 prev_folder
= cur_folder
= 0;
847 offset32
= file
->offset
;
850 if (file
->folder
>= hd
->folder_count
)
852 cur_folder
= file
->folder
;
855 /* Dot not back track. */
856 if (cur_folder
< prev_folder
)
858 if (cur_folder
!= prev_folder
)
860 prev_folder
= cur_folder
;
862 /* Make sure there are not any blanks from last file
864 if (offset32
!= file
->offset
)
866 offset32
+= file
->uncompressed_size
;
868 /* CFDATA is available for file contents. */
869 if (file
->uncompressed_size
> 0 &&
870 hd
->folder_array
[cur_folder
].cfdata_count
== 0)
874 if (hd
->cabinet
!= 0 || hd
->flags
& (PREV_CABINET
| NEXT_CABINET
)) {
875 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
876 "Multivolume cabinet file is unsupported");
877 return (ARCHIVE_WARN
);
881 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
882 "Invalid CAB header");
883 return (ARCHIVE_FATAL
);
885 archive_set_error(&a
->archive
, ENOMEM
,
886 "Can't allocate memory for CAB data");
887 return (ARCHIVE_FATAL
);
891 archive_read_format_cab_read_header(struct archive_read
*a
,
892 struct archive_entry
*entry
)
896 struct cffolder
*prev_folder
;
898 struct archive_string_conv
*sconv
;
899 int err
= ARCHIVE_OK
, r
;
901 cab
= (struct cab
*)(a
->format
->data
);
902 if (cab
->found_header
== 0) {
903 err
= cab_read_header(a
);
904 if (err
< ARCHIVE_WARN
)
906 /* We've found the header. */
907 cab
->found_header
= 1;
911 if (hd
->file_index
>= hd
->file_count
) {
912 cab
->end_of_archive
= 1;
913 return (ARCHIVE_EOF
);
915 file
= &hd
->file_array
[hd
->file_index
++];
917 cab
->end_of_entry
= 0;
918 cab
->end_of_entry_cleanup
= 0;
919 cab
->entry_compressed_bytes_read
= 0;
920 cab
->entry_uncompressed_bytes_read
= 0;
921 cab
->entry_unconsumed
= 0;
922 cab
->entry_cffile
= file
;
925 * Choose a proper folder.
927 prev_folder
= cab
->entry_cffolder
;
928 switch (file
->folder
) {
929 case iFoldCONTINUED_FROM_PREV
:
930 case iFoldCONTINUED_PREV_AND_NEXT
:
931 cab
->entry_cffolder
= &hd
->folder_array
[0];
933 case iFoldCONTINUED_TO_NEXT
:
934 cab
->entry_cffolder
= &hd
->folder_array
[hd
->folder_count
-1];
937 cab
->entry_cffolder
= &hd
->folder_array
[file
->folder
];
940 /* If a cffolder of this file is changed, reset a cfdata to read
941 * file contents from next cfdata. */
942 if (prev_folder
!= cab
->entry_cffolder
)
943 cab
->entry_cfdata
= NULL
;
945 /* If a pathname is UTF-8, prepare a string conversion object
946 * for UTF-8 and use it. */
947 if (file
->attr
& ATTR_NAME_IS_UTF
) {
948 if (cab
->sconv_utf8
== NULL
) {
950 archive_string_conversion_from_charset(
951 &(a
->archive
), "UTF-8", 1);
952 if (cab
->sconv_utf8
== NULL
)
953 return (ARCHIVE_FATAL
);
955 sconv
= cab
->sconv_utf8
;
956 } else if (cab
->sconv
!= NULL
) {
957 /* Choose the conversion specified by the option. */
960 /* Choose the default conversion. */
961 if (!cab
->init_default_conversion
) {
963 archive_string_default_conversion_for_read(
965 cab
->init_default_conversion
= 1;
967 sconv
= cab
->sconv_default
;
971 * Set a default value and common data
973 r
= cab_convert_path_separator_1(&(file
->pathname
), file
->attr
);
974 if (archive_entry_copy_pathname_l(entry
, file
->pathname
.s
,
975 archive_strlen(&(file
->pathname
)), sconv
) != 0) {
976 if (errno
== ENOMEM
) {
977 archive_set_error(&a
->archive
, ENOMEM
,
978 "Can't allocate memory for Pathname");
979 return (ARCHIVE_FATAL
);
981 archive_set_error(&a
->archive
,
982 ARCHIVE_ERRNO_FILE_FORMAT
,
983 "Pathname cannot be converted "
984 "from %s to current locale.",
985 archive_string_conversion_charset_name(sconv
));
989 /* Convert a path separator '\' -> '/' */
990 cab_convert_path_separator_2(cab
, entry
);
993 archive_entry_set_size(entry
, file
->uncompressed_size
);
994 if (file
->attr
& ATTR_RDONLY
)
995 archive_entry_set_mode(entry
, AE_IFREG
| 0555);
997 archive_entry_set_mode(entry
, AE_IFREG
| 0666);
998 archive_entry_set_mtime(entry
, file
->mtime
, 0);
1000 cab
->entry_bytes_remaining
= file
->uncompressed_size
;
1001 cab
->entry_offset
= 0;
1002 /* We don't need compress data. */
1003 if (file
->uncompressed_size
== 0)
1004 cab
->end_of_entry_cleanup
= cab
->end_of_entry
= 1;
1006 /* Set up a more descriptive format name. */
1007 sprintf(cab
->format_name
, "CAB %d.%d (%s)",
1008 hd
->major
, hd
->minor
, cab
->entry_cffolder
->compname
);
1009 a
->archive
.archive_format_name
= cab
->format_name
;
1015 archive_read_format_cab_read_data(struct archive_read
*a
,
1016 const void **buff
, size_t *size
, int64_t *offset
)
1018 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1021 switch (cab
->entry_cffile
->folder
) {
1022 case iFoldCONTINUED_FROM_PREV
:
1023 case iFoldCONTINUED_TO_NEXT
:
1024 case iFoldCONTINUED_PREV_AND_NEXT
:
1028 archive_clear_error(&a
->archive
);
1029 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1030 "Cannot restore this file split in multivolume.");
1031 return (ARCHIVE_FAILED
);
1035 if (cab
->read_data_invoked
== 0) {
1036 if (cab
->bytes_skipped
) {
1037 if (cab
->entry_cfdata
== NULL
) {
1038 r
= cab_next_cfdata(a
);
1042 if (cab_consume_cfdata(a
, cab
->bytes_skipped
) < 0)
1043 return (ARCHIVE_FATAL
);
1044 cab
->bytes_skipped
= 0;
1046 cab
->read_data_invoked
= 1;
1048 if (cab
->entry_unconsumed
) {
1049 /* Consume as much as the compressor actually used. */
1050 r
= (int)cab_consume_cfdata(a
, cab
->entry_unconsumed
);
1051 cab
->entry_unconsumed
= 0;
1055 if (cab
->end_of_archive
|| cab
->end_of_entry
) {
1056 if (!cab
->end_of_entry_cleanup
) {
1057 /* End-of-entry cleanup done. */
1058 cab
->end_of_entry_cleanup
= 1;
1060 *offset
= cab
->entry_offset
;
1063 return (ARCHIVE_EOF
);
1066 return (cab_read_data(a
, buff
, size
, offset
));
1070 cab_checksum_cfdata_4(const void *p
, size_t bytes
, uint32_t seed
)
1072 const unsigned char *b
;
1076 u32num
= (unsigned)bytes
/ 4;
1079 for (;u32num
> 0; --u32num
) {
1080 sum
^= archive_le32dec(b
);
1087 cab_checksum_cfdata(const void *p
, size_t bytes
, uint32_t seed
)
1089 const unsigned char *b
;
1093 sum
= cab_checksum_cfdata_4(p
, bytes
, seed
);
1097 switch (bytes
& 3) {
1099 t
|= ((uint32_t)(*b
++)) << 16;
1102 t
|= ((uint32_t)(*b
++)) << 8;
1116 cab_checksum_update(struct archive_read
*a
, size_t bytes
)
1118 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1119 struct cfdata
*cfdata
= cab
->entry_cfdata
;
1120 const unsigned char *p
;
1123 if (cfdata
->sum
== 0 || cfdata
->sum_ptr
== NULL
)
1126 * Calculate the sum of this CFDATA.
1127 * Make sure CFDATA must be calculated in four bytes.
1129 p
= cfdata
->sum_ptr
;
1131 if (cfdata
->sum_extra_avail
) {
1132 while (cfdata
->sum_extra_avail
< 4 && sumbytes
> 0) {
1134 cfdata
->sum_extra_avail
++] = *p
++;
1137 if (cfdata
->sum_extra_avail
== 4) {
1138 cfdata
->sum_calculated
= cab_checksum_cfdata_4(
1139 cfdata
->sum_extra
, 4, cfdata
->sum_calculated
);
1140 cfdata
->sum_extra_avail
= 0;
1144 int odd
= sumbytes
& 3;
1145 if (sumbytes
- odd
> 0)
1146 cfdata
->sum_calculated
= cab_checksum_cfdata_4(
1147 p
, sumbytes
- odd
, cfdata
->sum_calculated
);
1149 memcpy(cfdata
->sum_extra
, p
+ sumbytes
- odd
, odd
);
1150 cfdata
->sum_extra_avail
= odd
;
1152 cfdata
->sum_ptr
= NULL
;
1156 cab_checksum_finish(struct archive_read
*a
)
1158 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1159 struct cfdata
*cfdata
= cab
->entry_cfdata
;
1162 /* Do not need to compute a sum. */
1163 if (cfdata
->sum
== 0)
1164 return (ARCHIVE_OK
);
1167 * Calculate the sum of remaining CFDATA.
1169 if (cfdata
->sum_extra_avail
) {
1170 cfdata
->sum_calculated
=
1171 cab_checksum_cfdata(cfdata
->sum_extra
,
1172 cfdata
->sum_extra_avail
, cfdata
->sum_calculated
);
1173 cfdata
->sum_extra_avail
= 0;
1177 if (cab
->cfheader
.flags
& RESERVE_PRESENT
)
1178 l
+= cab
->cfheader
.cfdata
;
1179 cfdata
->sum_calculated
= cab_checksum_cfdata(
1180 cfdata
->memimage
+ CFDATA_cbData
, l
, cfdata
->sum_calculated
);
1181 if (cfdata
->sum_calculated
!= cfdata
->sum
) {
1182 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1183 "Checksum error CFDATA[%d] %x:%x in %d bytes",
1184 cab
->entry_cffolder
->cfdata_index
-1,
1185 cfdata
->sum
, cfdata
->sum_calculated
,
1186 cfdata
->compressed_size
);
1187 return (ARCHIVE_FAILED
);
1189 return (ARCHIVE_OK
);
1193 * Read CFDATA if needed.
1196 cab_next_cfdata(struct archive_read
*a
)
1198 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1199 struct cfdata
*cfdata
= cab
->entry_cfdata
;
1201 /* There are remaining bytes in current CFDATA, use it first. */
1202 if (cfdata
!= NULL
&& cfdata
->uncompressed_bytes_remaining
> 0)
1203 return (ARCHIVE_OK
);
1205 if (cfdata
== NULL
) {
1208 cab
->entry_cffolder
->cfdata_index
= 0;
1210 /* Seek read pointer to the offset of CFDATA if needed. */
1211 skip
= cab
->entry_cffolder
->cfdata_offset_in_cab
1215 switch (cab
->entry_cffile
->folder
) {
1216 case iFoldCONTINUED_FROM_PREV
:
1217 case iFoldCONTINUED_PREV_AND_NEXT
:
1220 case iFoldCONTINUED_TO_NEXT
:
1221 folder_index
= cab
->cfheader
.folder_count
-1;
1224 folder_index
= cab
->entry_cffile
->folder
;
1227 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1228 "Invalid offset of CFDATA in folder(%d) %jd < %jd",
1230 (intmax_t)cab
->entry_cffolder
->cfdata_offset_in_cab
,
1231 (intmax_t)cab
->cab_offset
);
1232 return (ARCHIVE_FATAL
);
1235 if (__archive_read_consume(a
, skip
) < 0)
1236 return (ARCHIVE_FATAL
);
1238 cab
->entry_cffolder
->cfdata_offset_in_cab
;
1245 if (cab
->entry_cffolder
->cfdata_index
<
1246 cab
->entry_cffolder
->cfdata_count
) {
1247 const unsigned char *p
;
1250 cfdata
= &(cab
->entry_cffolder
->cfdata
);
1251 cab
->entry_cffolder
->cfdata_index
++;
1252 cab
->entry_cfdata
= cfdata
;
1253 cfdata
->sum_calculated
= 0;
1254 cfdata
->sum_extra_avail
= 0;
1255 cfdata
->sum_ptr
= NULL
;
1257 if (cab
->cfheader
.flags
& RESERVE_PRESENT
)
1258 l
+= cab
->cfheader
.cfdata
;
1259 if ((p
= __archive_read_ahead(a
, l
, NULL
)) == NULL
)
1260 return (truncated_error(a
));
1261 cfdata
->sum
= archive_le32dec(p
+ CFDATA_csum
);
1262 cfdata
->compressed_size
= archive_le16dec(p
+ CFDATA_cbData
);
1263 cfdata
->compressed_bytes_remaining
= cfdata
->compressed_size
;
1264 cfdata
->uncompressed_size
=
1265 archive_le16dec(p
+ CFDATA_cbUncomp
);
1266 cfdata
->uncompressed_bytes_remaining
=
1267 cfdata
->uncompressed_size
;
1268 cfdata
->uncompressed_avail
= 0;
1269 cfdata
->read_offset
= 0;
1270 cfdata
->unconsumed
= 0;
1273 * Sanity check if data size is acceptable.
1275 if (cfdata
->compressed_size
== 0 ||
1276 cfdata
->compressed_size
> (0x8000+6144))
1278 if (cfdata
->uncompressed_size
> 0x8000)
1280 if (cfdata
->uncompressed_size
== 0) {
1281 switch (cab
->entry_cffile
->folder
) {
1282 case iFoldCONTINUED_PREV_AND_NEXT
:
1283 case iFoldCONTINUED_TO_NEXT
:
1285 case iFoldCONTINUED_FROM_PREV
:
1290 /* If CFDATA is not last in a folder, an uncompressed
1291 * size must be 0x8000(32KBi) */
1292 if ((cab
->entry_cffolder
->cfdata_index
<
1293 cab
->entry_cffolder
->cfdata_count
) &&
1294 cfdata
->uncompressed_size
!= 0x8000)
1297 /* A compressed data size and an uncompressed data size must
1298 * be the same in no compression mode. */
1299 if (cab
->entry_cffolder
->comptype
== COMPTYPE_NONE
&&
1300 cfdata
->compressed_size
!= cfdata
->uncompressed_size
)
1304 * Save CFDATA image for sum check.
1306 if (cfdata
->memimage_size
< (size_t)l
) {
1307 free(cfdata
->memimage
);
1308 cfdata
->memimage
= malloc(l
);
1309 if (cfdata
->memimage
== NULL
) {
1310 archive_set_error(&a
->archive
, ENOMEM
,
1311 "Can't allocate memory for CAB data");
1312 return (ARCHIVE_FATAL
);
1314 cfdata
->memimage_size
= l
;
1316 memcpy(cfdata
->memimage
, p
, l
);
1318 /* Consume bytes as much as we used. */
1319 __archive_read_consume(a
, l
);
1320 cab
->cab_offset
+= l
;
1321 } else if (cab
->entry_cffolder
->cfdata_count
> 0) {
1322 /* Run out of all CFDATA in a folder. */
1323 cfdata
->compressed_size
= 0;
1324 cfdata
->uncompressed_size
= 0;
1325 cfdata
->compressed_bytes_remaining
= 0;
1326 cfdata
->uncompressed_bytes_remaining
= 0;
1328 /* Current folder does not have any CFDATA. */
1329 cfdata
= &(cab
->entry_cffolder
->cfdata
);
1330 cab
->entry_cfdata
= cfdata
;
1331 memset(cfdata
, 0, sizeof(*cfdata
));
1333 return (ARCHIVE_OK
);
1335 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1337 return (ARCHIVE_FATAL
);
1341 * Read ahead CFDATA.
1344 cab_read_ahead_cfdata(struct archive_read
*a
, ssize_t
*avail
)
1346 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1349 err
= cab_next_cfdata(a
);
1350 if (err
< ARCHIVE_OK
) {
1355 switch (cab
->entry_cffolder
->comptype
) {
1357 return (cab_read_ahead_cfdata_none(a
, avail
));
1358 case COMPTYPE_MSZIP
:
1359 return (cab_read_ahead_cfdata_deflate(a
, avail
));
1361 return (cab_read_ahead_cfdata_lzx(a
, avail
));
1362 default: /* Unsupported compression. */
1363 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1364 "Unsupported CAB compression : %s",
1365 cab
->entry_cffolder
->compname
);
1366 *avail
= ARCHIVE_FAILED
;
1372 * Read ahead CFDATA as uncompressed data.
1375 cab_read_ahead_cfdata_none(struct archive_read
*a
, ssize_t
*avail
)
1377 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1378 struct cfdata
*cfdata
;
1381 cfdata
= cab
->entry_cfdata
;
1384 * Note: '1' here is a performance optimization.
1385 * Recall that the decompression layer returns a count of
1386 * available bytes; asking for more than that forces the
1387 * decompressor to combine reads by copying data.
1389 d
= __archive_read_ahead(a
, 1, avail
);
1391 *avail
= truncated_error(a
);
1394 if (*avail
> cfdata
->uncompressed_bytes_remaining
)
1395 *avail
= cfdata
->uncompressed_bytes_remaining
;
1396 cfdata
->uncompressed_avail
= cfdata
->uncompressed_size
;
1397 cfdata
->unconsumed
= *avail
;
1398 cfdata
->sum_ptr
= d
;
1403 * Read ahead CFDATA as deflate data.
1407 cab_read_ahead_cfdata_deflate(struct archive_read
*a
, ssize_t
*avail
)
1409 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1410 struct cfdata
*cfdata
;
1416 cfdata
= cab
->entry_cfdata
;
1417 /* If the buffer hasn't been allocated, allocate it now. */
1418 if (cab
->uncompressed_buffer
== NULL
) {
1419 cab
->uncompressed_buffer_size
= 0x8000;
1420 cab
->uncompressed_buffer
1421 = (unsigned char *)malloc(cab
->uncompressed_buffer_size
);
1422 if (cab
->uncompressed_buffer
== NULL
) {
1423 archive_set_error(&a
->archive
, ENOMEM
,
1424 "No memory for CAB reader");
1425 *avail
= ARCHIVE_FATAL
;
1430 uavail
= cfdata
->uncompressed_avail
;
1431 if (uavail
== cfdata
->uncompressed_size
) {
1432 d
= cab
->uncompressed_buffer
+ cfdata
->read_offset
;
1433 *avail
= uavail
- cfdata
->read_offset
;
1437 if (!cab
->entry_cffolder
->decompress_init
) {
1438 cab
->stream
.next_in
= NULL
;
1439 cab
->stream
.avail_in
= 0;
1440 cab
->stream
.total_in
= 0;
1441 cab
->stream
.next_out
= NULL
;
1442 cab
->stream
.avail_out
= 0;
1443 cab
->stream
.total_out
= 0;
1444 if (cab
->stream_valid
)
1445 r
= inflateReset(&cab
->stream
);
1447 r
= inflateInit2(&cab
->stream
,
1448 -15 /* Don't check for zlib header */);
1450 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1451 "Can't initialize deflate decompression.");
1452 *avail
= ARCHIVE_FATAL
;
1455 /* Stream structure has been set up. */
1456 cab
->stream_valid
= 1;
1457 /* We've initialized decompression for this stream. */
1458 cab
->entry_cffolder
->decompress_init
= 1;
1461 if (cfdata
->compressed_bytes_remaining
== cfdata
->compressed_size
)
1466 cab
->stream
.total_out
= uavail
;
1468 * We always uncompress all data in current CFDATA.
1470 while (!eod
&& cab
->stream
.total_out
< cfdata
->uncompressed_size
) {
1471 ssize_t bytes_avail
;
1473 cab
->stream
.next_out
=
1474 cab
->uncompressed_buffer
+ cab
->stream
.total_out
;
1475 cab
->stream
.avail_out
=
1476 cfdata
->uncompressed_size
- cab
->stream
.total_out
;
1478 d
= __archive_read_ahead(a
, 1, &bytes_avail
);
1479 if (bytes_avail
<= 0) {
1480 *avail
= truncated_error(a
);
1483 if (bytes_avail
> cfdata
->compressed_bytes_remaining
)
1484 bytes_avail
= cfdata
->compressed_bytes_remaining
;
1486 * A bug in zlib.h: stream.next_in should be marked 'const'
1487 * but isn't (the library never alters data through the
1488 * next_in pointer, only reads it). The result: this ugly
1489 * cast to remove 'const'.
1491 cab
->stream
.next_in
= (Bytef
*)(uintptr_t)d
;
1492 cab
->stream
.avail_in
= (uInt
)bytes_avail
;
1493 cab
->stream
.total_in
= 0;
1495 /* Cut out a tow-byte MSZIP signature(0x43, 0x4b). */
1497 if (bytes_avail
<= mszip
) {
1499 if (cab
->stream
.next_in
[0] != 0x43)
1501 if (bytes_avail
> 1 &&
1502 cab
->stream
.next_in
[1] != 0x4b)
1504 } else if (cab
->stream
.next_in
[0] != 0x4b)
1506 cfdata
->unconsumed
= bytes_avail
;
1507 cfdata
->sum_ptr
= d
;
1508 if (cab_minimum_consume_cfdata(
1509 a
, cfdata
->unconsumed
) < 0) {
1510 *avail
= ARCHIVE_FATAL
;
1513 mszip
-= (int)bytes_avail
;
1516 if (mszip
== 1 && cab
->stream
.next_in
[0] != 0x4b)
1518 else if (cab
->stream
.next_in
[0] != 0x43 ||
1519 cab
->stream
.next_in
[1] != 0x4b)
1521 cab
->stream
.next_in
+= mszip
;
1522 cab
->stream
.avail_in
-= mszip
;
1523 cab
->stream
.total_in
+= mszip
;
1527 r
= inflate(&cab
->stream
, 0);
1537 cfdata
->unconsumed
= cab
->stream
.total_in
;
1538 cfdata
->sum_ptr
= d
;
1539 if (cab_minimum_consume_cfdata(a
, cfdata
->unconsumed
) < 0) {
1540 *avail
= ARCHIVE_FATAL
;
1544 uavail
= (uint16_t)cab
->stream
.total_out
;
1546 if (uavail
< cfdata
->uncompressed_size
) {
1547 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1548 "Invalid uncompressed size (%d < %d)",
1549 uavail
, cfdata
->uncompressed_size
);
1550 *avail
= ARCHIVE_FATAL
;
1555 * Note: I suspect there is a bug in makecab.exe because, in rare
1556 * case, compressed bytes are still remaining regardless we have
1557 * gotten all uncompressed bytes, which size is recoded in CFDATA,
1558 * as much as we need, and we have to use the garbage so as to
1559 * correctly compute the sum of CFDATA accordingly.
1561 if (cfdata
->compressed_bytes_remaining
> 0) {
1562 ssize_t bytes_avail
;
1564 d
= __archive_read_ahead(a
, cfdata
->compressed_bytes_remaining
,
1566 if (bytes_avail
<= 0) {
1567 *avail
= truncated_error(a
);
1570 cfdata
->unconsumed
= cfdata
->compressed_bytes_remaining
;
1571 cfdata
->sum_ptr
= d
;
1572 if (cab_minimum_consume_cfdata(a
, cfdata
->unconsumed
) < 0) {
1573 *avail
= ARCHIVE_FATAL
;
1579 * Set dictionary data for decompressing of next CFDATA, which
1580 * in the same folder. This is why we always do decompress CFDATA
1581 * even if beginning CFDATA or some of CFDATA are not used in
1582 * skipping file data.
1584 if (cab
->entry_cffolder
->cfdata_index
<
1585 cab
->entry_cffolder
->cfdata_count
) {
1586 r
= inflateReset(&cab
->stream
);
1589 r
= inflateSetDictionary(&cab
->stream
,
1590 cab
->uncompressed_buffer
, cfdata
->uncompressed_size
);
1595 d
= cab
->uncompressed_buffer
+ cfdata
->read_offset
;
1596 *avail
= uavail
- cfdata
->read_offset
;
1597 cfdata
->uncompressed_avail
= uavail
;
1604 archive_set_error(&a
->archive
, ENOMEM
,
1605 "Out of memory for deflate decompression");
1608 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1609 "Deflate decompression failed (%d)", r
);
1612 *avail
= ARCHIVE_FATAL
;
1615 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1616 "CFDATA incorrect(no MSZIP signature)");
1617 *avail
= ARCHIVE_FATAL
;
1621 #else /* HAVE_ZLIB_H */
1624 cab_read_ahead_cfdata_deflate(struct archive_read
*a
, ssize_t
*avail
)
1626 *avail
= ARCHIVE_FATAL
;
1627 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1628 "libarchive compiled without deflate support (no libz)");
1632 #endif /* HAVE_ZLIB_H */
1635 cab_read_ahead_cfdata_lzx(struct archive_read
*a
, ssize_t
*avail
)
1637 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1638 struct cfdata
*cfdata
;
1643 cfdata
= cab
->entry_cfdata
;
1644 /* If the buffer hasn't been allocated, allocate it now. */
1645 if (cab
->uncompressed_buffer
== NULL
) {
1646 cab
->uncompressed_buffer_size
= 0x8000;
1647 cab
->uncompressed_buffer
1648 = (unsigned char *)malloc(cab
->uncompressed_buffer_size
);
1649 if (cab
->uncompressed_buffer
== NULL
) {
1650 archive_set_error(&a
->archive
, ENOMEM
,
1651 "No memory for CAB reader");
1652 *avail
= ARCHIVE_FATAL
;
1657 uavail
= cfdata
->uncompressed_avail
;
1658 if (uavail
== cfdata
->uncompressed_size
) {
1659 d
= cab
->uncompressed_buffer
+ cfdata
->read_offset
;
1660 *avail
= uavail
- cfdata
->read_offset
;
1664 if (!cab
->entry_cffolder
->decompress_init
) {
1665 r
= lzx_decode_init(&cab
->xstrm
,
1666 cab
->entry_cffolder
->compdata
);
1667 if (r
!= ARCHIVE_OK
) {
1668 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1669 "Can't initialize LZX decompression.");
1670 *avail
= ARCHIVE_FATAL
;
1673 /* We've initialized decompression for this stream. */
1674 cab
->entry_cffolder
->decompress_init
= 1;
1677 /* Clean up remaining bits of previous CFDATA. */
1678 lzx_cleanup_bitstream(&cab
->xstrm
);
1679 cab
->xstrm
.total_out
= uavail
;
1680 while (cab
->xstrm
.total_out
< cfdata
->uncompressed_size
) {
1681 ssize_t bytes_avail
;
1683 cab
->xstrm
.next_out
=
1684 cab
->uncompressed_buffer
+ cab
->xstrm
.total_out
;
1685 cab
->xstrm
.avail_out
=
1686 cfdata
->uncompressed_size
- cab
->xstrm
.total_out
;
1688 d
= __archive_read_ahead(a
, 1, &bytes_avail
);
1689 if (bytes_avail
<= 0) {
1690 archive_set_error(&a
->archive
,
1691 ARCHIVE_ERRNO_FILE_FORMAT
,
1692 "Truncated CAB file data");
1693 *avail
= ARCHIVE_FATAL
;
1696 if (bytes_avail
> cfdata
->compressed_bytes_remaining
)
1697 bytes_avail
= cfdata
->compressed_bytes_remaining
;
1699 cab
->xstrm
.next_in
= d
;
1700 cab
->xstrm
.avail_in
= bytes_avail
;
1701 cab
->xstrm
.total_in
= 0;
1702 r
= lzx_decode(&cab
->xstrm
,
1703 cfdata
->compressed_bytes_remaining
== bytes_avail
);
1709 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1710 "LZX decompression failed (%d)", r
);
1711 *avail
= ARCHIVE_FATAL
;
1714 cfdata
->unconsumed
= cab
->xstrm
.total_in
;
1715 cfdata
->sum_ptr
= d
;
1716 if (cab_minimum_consume_cfdata(a
, cfdata
->unconsumed
) < 0) {
1717 *avail
= ARCHIVE_FATAL
;
1722 uavail
= (uint16_t)cab
->xstrm
.total_out
;
1724 * Make sure a read pointer advances to next CFDATA.
1726 if (cfdata
->compressed_bytes_remaining
> 0) {
1727 ssize_t bytes_avail
;
1729 d
= __archive_read_ahead(a
, cfdata
->compressed_bytes_remaining
,
1731 if (bytes_avail
<= 0) {
1732 *avail
= truncated_error(a
);
1735 cfdata
->unconsumed
= cfdata
->compressed_bytes_remaining
;
1736 cfdata
->sum_ptr
= d
;
1737 if (cab_minimum_consume_cfdata(a
, cfdata
->unconsumed
) < 0) {
1738 *avail
= ARCHIVE_FATAL
;
1744 * Translation reversal of x86 proccessor CALL byte sequence(E8).
1746 lzx_translation(&cab
->xstrm
, cab
->uncompressed_buffer
,
1747 cfdata
->uncompressed_size
,
1748 (cab
->entry_cffolder
->cfdata_index
-1) * 0x8000);
1750 d
= cab
->uncompressed_buffer
+ cfdata
->read_offset
;
1751 *avail
= uavail
- cfdata
->read_offset
;
1752 cfdata
->uncompressed_avail
= uavail
;
1759 * We always decompress CFDATA to consume CFDATA as much as we need
1760 * in uncompressed bytes because all CFDATA in a folder are related
1761 * so we do not skip any CFDATA without decompressing.
1762 * Note: If the folder of a CFFILE is iFoldCONTINUED_PREV_AND_NEXT or
1763 * iFoldCONTINUED_FROM_PREV, we won't decompress because a CFDATA for
1764 * the CFFILE is remaining bytes of previous Multivolume CAB file.
1767 cab_consume_cfdata(struct archive_read
*a
, int64_t consumed_bytes
)
1769 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1770 struct cfdata
*cfdata
;
1771 int64_t cbytes
, rbytes
;
1774 rbytes
= cab_minimum_consume_cfdata(a
, consumed_bytes
);
1776 return (ARCHIVE_FATAL
);
1778 cfdata
= cab
->entry_cfdata
;
1779 while (rbytes
> 0) {
1782 if (cfdata
->compressed_size
== 0) {
1783 archive_set_error(&a
->archive
,
1784 ARCHIVE_ERRNO_FILE_FORMAT
,
1786 return (ARCHIVE_FATAL
);
1788 cbytes
= cfdata
->uncompressed_bytes_remaining
;
1789 if (cbytes
> rbytes
)
1793 if (cfdata
->uncompressed_avail
== 0 &&
1794 (cab
->entry_cffile
->folder
== iFoldCONTINUED_PREV_AND_NEXT
||
1795 cab
->entry_cffile
->folder
== iFoldCONTINUED_FROM_PREV
)) {
1796 /* We have not read any data yet. */
1797 if (cbytes
== cfdata
->uncompressed_bytes_remaining
) {
1798 /* Skip whole current CFDATA. */
1799 __archive_read_consume(a
,
1800 cfdata
->compressed_size
);
1801 cab
->cab_offset
+= cfdata
->compressed_size
;
1802 cfdata
->compressed_bytes_remaining
= 0;
1803 cfdata
->uncompressed_bytes_remaining
= 0;
1804 err
= cab_next_cfdata(a
);
1807 cfdata
= cab
->entry_cfdata
;
1808 if (cfdata
->uncompressed_size
== 0) {
1809 switch (cab
->entry_cffile
->folder
) {
1810 case iFoldCONTINUED_PREV_AND_NEXT
:
1811 case iFoldCONTINUED_TO_NEXT
:
1812 case iFoldCONTINUED_FROM_PREV
:
1821 cfdata
->read_offset
+= (uint16_t)cbytes
;
1822 cfdata
->uncompressed_bytes_remaining
-= (uint16_t)cbytes
;
1824 } else if (cbytes
== 0) {
1825 err
= cab_next_cfdata(a
);
1828 cfdata
= cab
->entry_cfdata
;
1829 if (cfdata
->uncompressed_size
== 0) {
1830 switch (cab
->entry_cffile
->folder
) {
1831 case iFoldCONTINUED_PREV_AND_NEXT
:
1832 case iFoldCONTINUED_TO_NEXT
:
1833 case iFoldCONTINUED_FROM_PREV
:
1834 return (ARCHIVE_FATAL
);
1841 while (cbytes
> 0) {
1842 (void)cab_read_ahead_cfdata(a
, &avail
);
1844 return (ARCHIVE_FATAL
);
1846 avail
= (ssize_t
)cbytes
;
1847 if (cab_minimum_consume_cfdata(a
, avail
) < 0)
1848 return (ARCHIVE_FATAL
);
1852 return (consumed_bytes
);
1856 * Consume CFDATA as much as we have already gotten and
1857 * compute the sum of CFDATA.
1860 cab_minimum_consume_cfdata(struct archive_read
*a
, int64_t consumed_bytes
)
1862 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1863 struct cfdata
*cfdata
;
1864 int64_t cbytes
, rbytes
;
1867 cfdata
= cab
->entry_cfdata
;
1868 rbytes
= consumed_bytes
;
1869 if (cab
->entry_cffolder
->comptype
== COMPTYPE_NONE
) {
1870 if (consumed_bytes
< cfdata
->unconsumed
)
1871 cbytes
= consumed_bytes
;
1873 cbytes
= cfdata
->unconsumed
;
1875 cfdata
->read_offset
+= (uint16_t)cbytes
;
1876 cfdata
->uncompressed_bytes_remaining
-= (uint16_t)cbytes
;
1877 cfdata
->unconsumed
-= cbytes
;
1879 cbytes
= cfdata
->uncompressed_avail
- cfdata
->read_offset
;
1881 if (consumed_bytes
< cbytes
)
1882 cbytes
= consumed_bytes
;
1884 cfdata
->read_offset
+= (uint16_t)cbytes
;
1885 cfdata
->uncompressed_bytes_remaining
-= (uint16_t)cbytes
;
1888 if (cfdata
->unconsumed
) {
1889 cbytes
= cfdata
->unconsumed
;
1890 cfdata
->unconsumed
= 0;
1895 /* Compute the sum. */
1896 cab_checksum_update(a
, (size_t)cbytes
);
1898 /* Consume as much as the compressor actually used. */
1899 __archive_read_consume(a
, cbytes
);
1900 cab
->cab_offset
+= cbytes
;
1901 cfdata
->compressed_bytes_remaining
-= (uint16_t)cbytes
;
1902 if (cfdata
->compressed_bytes_remaining
== 0) {
1903 err
= cab_checksum_finish(a
);
1912 * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
1913 * cab->end_of_entry if it consumes all of the data.
1916 cab_read_data(struct archive_read
*a
, const void **buff
,
1917 size_t *size
, int64_t *offset
)
1919 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
1920 ssize_t bytes_avail
;
1922 if (cab
->entry_bytes_remaining
== 0) {
1925 *offset
= cab
->entry_offset
;
1926 cab
->end_of_entry
= 1;
1927 return (ARCHIVE_OK
);
1930 *buff
= cab_read_ahead_cfdata(a
, &bytes_avail
);
1931 if (bytes_avail
<= 0) {
1935 if (bytes_avail
== 0 &&
1936 cab
->entry_cfdata
->uncompressed_size
== 0) {
1937 /* All of CFDATA in a folder has been handled. */
1938 archive_set_error(&a
->archive
,
1939 ARCHIVE_ERRNO_FILE_FORMAT
, "Invalid CFDATA");
1940 return (ARCHIVE_FATAL
);
1942 return ((int)bytes_avail
);
1944 if (bytes_avail
> cab
->entry_bytes_remaining
)
1945 bytes_avail
= (ssize_t
)cab
->entry_bytes_remaining
;
1947 *size
= bytes_avail
;
1948 *offset
= cab
->entry_offset
;
1949 cab
->entry_offset
+= bytes_avail
;
1950 cab
->entry_bytes_remaining
-= bytes_avail
;
1951 if (cab
->entry_bytes_remaining
== 0)
1952 cab
->end_of_entry
= 1;
1953 cab
->entry_unconsumed
= bytes_avail
;
1954 if (cab
->entry_cffolder
->comptype
== COMPTYPE_NONE
) {
1955 /* Don't consume more than current entry used. */
1956 if (cab
->entry_cfdata
->unconsumed
> cab
->entry_unconsumed
)
1957 cab
->entry_cfdata
->unconsumed
= cab
->entry_unconsumed
;
1959 return (ARCHIVE_OK
);
1963 archive_read_format_cab_read_data_skip(struct archive_read
*a
)
1966 int64_t bytes_skipped
;
1969 cab
= (struct cab
*)(a
->format
->data
);
1971 if (cab
->end_of_archive
)
1972 return (ARCHIVE_EOF
);
1974 if (!cab
->read_data_invoked
) {
1975 cab
->bytes_skipped
+= cab
->entry_bytes_remaining
;
1976 cab
->entry_bytes_remaining
= 0;
1977 /* This entry is finished and done. */
1978 cab
->end_of_entry_cleanup
= cab
->end_of_entry
= 1;
1979 return (ARCHIVE_OK
);
1982 if (cab
->entry_unconsumed
) {
1983 /* Consume as much as the compressor actually used. */
1984 r
= (int)cab_consume_cfdata(a
, cab
->entry_unconsumed
);
1985 cab
->entry_unconsumed
= 0;
1988 } else if (cab
->entry_cfdata
== NULL
) {
1989 r
= cab_next_cfdata(a
);
1994 /* if we've already read to end of data, we're done. */
1995 if (cab
->end_of_entry_cleanup
)
1996 return (ARCHIVE_OK
);
1999 * If the length is at the beginning, we can skip the
2000 * compressed data much more quickly.
2002 bytes_skipped
= cab_consume_cfdata(a
, cab
->entry_bytes_remaining
);
2003 if (bytes_skipped
< 0)
2004 return (ARCHIVE_FATAL
);
2006 /* If the compression type is none(uncompressed), we've already
2007 * consumed data as much as the current entry size. */
2008 if (cab
->entry_cffolder
->comptype
== COMPTYPE_NONE
&&
2009 cab
->entry_cfdata
!= NULL
)
2010 cab
->entry_cfdata
->unconsumed
= 0;
2012 /* This entry is finished and done. */
2013 cab
->end_of_entry_cleanup
= cab
->end_of_entry
= 1;
2014 return (ARCHIVE_OK
);
2018 archive_read_format_cab_cleanup(struct archive_read
*a
)
2020 struct cab
*cab
= (struct cab
*)(a
->format
->data
);
2021 struct cfheader
*hd
= &cab
->cfheader
;
2024 if (hd
->folder_array
!= NULL
) {
2025 for (i
= 0; i
< hd
->folder_count
; i
++)
2026 free(hd
->folder_array
[i
].cfdata
.memimage
);
2027 free(hd
->folder_array
);
2029 if (hd
->file_array
!= NULL
) {
2030 for (i
= 0; i
< cab
->cfheader
.file_count
; i
++)
2031 archive_string_free(&(hd
->file_array
[i
].pathname
));
2032 free(hd
->file_array
);
2035 if (cab
->stream_valid
)
2036 inflateEnd(&cab
->stream
);
2038 lzx_decode_free(&cab
->xstrm
);
2039 archive_wstring_free(&cab
->ws
);
2040 free(cab
->uncompressed_buffer
);
2042 (a
->format
->data
) = NULL
;
2043 return (ARCHIVE_OK
);
2046 /* Convert an MSDOS-style date/time into Unix-style time. */
2048 cab_dos_time(const unsigned char *p
)
2053 msDate
= archive_le16dec(p
);
2054 msTime
= archive_le16dec(p
+2);
2056 memset(&ts
, 0, sizeof(ts
));
2057 ts
.tm_year
= ((msDate
>> 9) & 0x7f) + 80; /* Years since 1900. */
2058 ts
.tm_mon
= ((msDate
>> 5) & 0x0f) - 1; /* Month number. */
2059 ts
.tm_mday
= msDate
& 0x1f; /* Day of month. */
2060 ts
.tm_hour
= (msTime
>> 11) & 0x1f;
2061 ts
.tm_min
= (msTime
>> 5) & 0x3f;
2062 ts
.tm_sec
= (msTime
<< 1) & 0x3e;
2064 return (mktime(&ts
));
2067 /*****************************************************************
2069 * LZX decompression code.
2071 *****************************************************************/
2074 * Initialize LZX decoder.
2076 * Returns ARCHIVE_OK if initialization was successful.
2077 * Returns ARCHIVE_FAILED if w_bits has unsupported value.
2078 * Returns ARCHIVE_FATAL if initialization failed; memory allocation
2082 lzx_decode_init(struct lzx_stream
*strm
, int w_bits
)
2085 int slot
, w_size
, w_slot
;
2089 if (strm
->ds
== NULL
) {
2090 strm
->ds
= calloc(1, sizeof(*strm
->ds
));
2091 if (strm
->ds
== NULL
)
2092 return (ARCHIVE_FATAL
);
2095 ds
->error
= ARCHIVE_FAILED
;
2097 /* Allow bits from 15(32KBi) up to 21(2MBi) */
2098 if (w_bits
< SLOT_BASE
|| w_bits
> SLOT_MAX
)
2099 return (ARCHIVE_FAILED
);
2101 ds
->error
= ARCHIVE_FATAL
;
2106 w_size
= ds
->w_size
;
2107 w_slot
= slots
[w_bits
- SLOT_BASE
];
2108 ds
->w_size
= 1U << w_bits
;
2109 ds
->w_mask
= ds
->w_size
-1;
2110 if (ds
->w_buff
== NULL
|| w_size
!= ds
->w_size
) {
2112 ds
->w_buff
= malloc(ds
->w_size
);
2113 if (ds
->w_buff
== NULL
)
2114 return (ARCHIVE_FATAL
);
2116 ds
->pos_tbl
= malloc(sizeof(ds
->pos_tbl
[0]) * w_slot
);
2117 if (ds
->pos_tbl
== NULL
)
2118 return (ARCHIVE_FATAL
);
2119 lzx_huffman_free(&(ds
->mt
));
2122 for (footer
= 0; footer
< 18; footer
++)
2123 base_inc
[footer
] = 1 << footer
;
2125 for (slot
= 0; slot
< w_slot
; slot
++) {
2130 base
+= base_inc
[footer
];
2133 for (n
= base
; n
; n
>>= 1)
2138 ds
->pos_tbl
[slot
].base
= base
;
2139 ds
->pos_tbl
[slot
].footer_bits
= footer
;
2144 ds
->br
.cache_buffer
= 0;
2145 ds
->br
.cache_avail
= 0;
2146 ds
->r0
= ds
->r1
= ds
->r2
= 1;
2148 /* Initialize aligned offset tree. */
2149 if (lzx_huffman_init(&(ds
->at
), 8, 8) != ARCHIVE_OK
)
2150 return (ARCHIVE_FATAL
);
2152 /* Initialize pre-tree. */
2153 if (lzx_huffman_init(&(ds
->pt
), 20, 10) != ARCHIVE_OK
)
2154 return (ARCHIVE_FATAL
);
2156 /* Initialize Main tree. */
2157 if (lzx_huffman_init(&(ds
->mt
), 256+(w_slot
<<3), 16)
2159 return (ARCHIVE_FATAL
);
2161 /* Initialize Length tree. */
2162 if (lzx_huffman_init(&(ds
->lt
), 249, 16) != ARCHIVE_OK
)
2163 return (ARCHIVE_FATAL
);
2167 return (ARCHIVE_OK
);
2171 * Release LZX decoder.
2174 lzx_decode_free(struct lzx_stream
*strm
)
2177 if (strm
->ds
== NULL
)
2179 free(strm
->ds
->w_buff
);
2180 free(strm
->ds
->pos_tbl
);
2181 lzx_huffman_free(&(strm
->ds
->at
));
2182 lzx_huffman_free(&(strm
->ds
->pt
));
2183 lzx_huffman_free(&(strm
->ds
->mt
));
2184 lzx_huffman_free(&(strm
->ds
->lt
));
2190 * E8 Call Translation reversal.
2193 lzx_translation(struct lzx_stream
*strm
, void *p
, size_t size
, uint32_t offset
)
2195 struct lzx_dec
*ds
= strm
->ds
;
2196 unsigned char *b
, *end
;
2198 if (!ds
->translation
|| size
<= 10)
2201 end
= b
+ size
- 10;
2202 while (b
< end
&& (b
= memchr(b
, 0xE8, end
- b
)) != NULL
) {
2203 size_t i
= b
- (unsigned char *)p
;
2204 int32_t cp
, displacement
, value
;
2206 cp
= (int32_t)(offset
+ (uint32_t)i
);
2207 value
= archive_le32dec(&b
[1]);
2208 if (value
>= -cp
&& value
< (int32_t)ds
->translation_size
) {
2210 displacement
= value
- cp
;
2212 displacement
= value
+ ds
->translation_size
;
2213 archive_le32enc(&b
[1], (uint32_t)displacement
);
2220 * Bit stream reader.
2222 /* Check that the cache buffer has enough bits. */
2223 #define lzx_br_has(br, n) ((br)->cache_avail >= n)
2224 /* Get compressed data by bit. */
2225 #define lzx_br_bits(br, n) \
2226 (((uint32_t)((br)->cache_buffer >> \
2227 ((br)->cache_avail - (n)))) & cache_masks[n])
2228 #define lzx_br_bits_forced(br, n) \
2229 (((uint32_t)((br)->cache_buffer << \
2230 ((n) - (br)->cache_avail))) & cache_masks[n])
2231 /* Read ahead to make sure the cache buffer has enough compressed data we
2233 * True : completed, there is enough data in the cache buffer.
2234 * False : we met that strm->next_in is empty, we have to get following
2236 #define lzx_br_read_ahead_0(strm, br, n) \
2237 (lzx_br_has((br), (n)) || lzx_br_fillup(strm, br))
2238 /* True : the cache buffer has some bits as much as we need.
2239 * False : there are no enough bits in the cache buffer to be used,
2240 * we have to get following bytes if we could. */
2241 #define lzx_br_read_ahead(strm, br, n) \
2242 (lzx_br_read_ahead_0((strm), (br), (n)) || lzx_br_has((br), (n)))
2244 /* Notify how many bits we consumed. */
2245 #define lzx_br_consume(br, n) ((br)->cache_avail -= (n))
2246 #define lzx_br_consume_unaligned_bits(br) ((br)->cache_avail &= ~0x0f)
2248 #define lzx_br_is_unaligned(br) ((br)->cache_avail & 0x0f)
2250 static const uint32_t cache_masks
[] = {
2251 0x00000000, 0x00000001, 0x00000003, 0x00000007,
2252 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F,
2253 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
2254 0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF,
2255 0x0000FFFF, 0x0001FFFF, 0x0003FFFF, 0x0007FFFF,
2256 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
2257 0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF,
2258 0x0FFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
2259 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
2263 * Shift away used bits in the cache data and fill it up with following bits.
2264 * Call this when cache buffer does not have enough bits you need.
2266 * Returns 1 if the cache buffer is full.
2267 * Returns 0 if the cache buffer is not full; input buffer is empty.
2270 lzx_br_fillup(struct lzx_stream
*strm
, struct lzx_br
*br
)
2273 * x86 proccessor family can read misaligned data without an access error.
2275 int n
= CACHE_BITS
- br
->cache_avail
;
2280 if (strm
->avail_in
>= 8) {
2282 ((uint64_t)strm
->next_in
[1]) << 56 |
2283 ((uint64_t)strm
->next_in
[0]) << 48 |
2284 ((uint64_t)strm
->next_in
[3]) << 40 |
2285 ((uint64_t)strm
->next_in
[2]) << 32 |
2286 ((uint32_t)strm
->next_in
[5]) << 24 |
2287 ((uint32_t)strm
->next_in
[4]) << 16 |
2288 ((uint32_t)strm
->next_in
[7]) << 8 |
2289 (uint32_t)strm
->next_in
[6];
2291 strm
->avail_in
-= 8;
2292 br
->cache_avail
+= 8 * 8;
2297 if (strm
->avail_in
>= 6) {
2299 (br
->cache_buffer
<< 48) |
2300 ((uint64_t)strm
->next_in
[1]) << 40 |
2301 ((uint64_t)strm
->next_in
[0]) << 32 |
2302 ((uint32_t)strm
->next_in
[3]) << 24 |
2303 ((uint32_t)strm
->next_in
[2]) << 16 |
2304 ((uint32_t)strm
->next_in
[5]) << 8 |
2305 (uint32_t)strm
->next_in
[4];
2307 strm
->avail_in
-= 6;
2308 br
->cache_avail
+= 6 * 8;
2313 /* We have enough compressed data in
2314 * the cache buffer.*/
2319 if (strm
->avail_in
< 2) {
2320 /* There is not enough compressed data to
2321 * fill up the cache buffer. */
2322 if (strm
->avail_in
== 1) {
2323 br
->odd
= *strm
->next_in
++;
2330 (br
->cache_buffer
<< 16) |
2331 archive_le16dec(strm
->next_in
);
2333 strm
->avail_in
-= 2;
2334 br
->cache_avail
+= 16;
2340 lzx_br_fixup(struct lzx_stream
*strm
, struct lzx_br
*br
)
2342 int n
= CACHE_BITS
- br
->cache_avail
;
2344 if (br
->have_odd
&& n
>= 16 && strm
->avail_in
> 0) {
2346 (br
->cache_buffer
<< 16) |
2347 ((uint16_t)(*strm
->next_in
)) << 8 | br
->odd
;
2350 br
->cache_avail
+= 16;
2356 lzx_cleanup_bitstream(struct lzx_stream
*strm
)
2358 strm
->ds
->br
.cache_avail
= 0;
2359 strm
->ds
->br
.have_odd
= 0;
2365 * 1. Returns ARCHIVE_OK if output buffer or input buffer are empty.
2366 * Please set available buffer and call this function again.
2367 * 2. Returns ARCHIVE_EOF if decompression has been completed.
2368 * 3. Returns ARCHIVE_FAILED if an error occurred; compressed data
2369 * is broken or you do not set 'last' flag properly.
2371 #define ST_RD_TRANSLATION 0
2372 #define ST_RD_TRANSLATION_SIZE 1
2373 #define ST_RD_BLOCK_TYPE 2
2374 #define ST_RD_BLOCK_SIZE 3
2375 #define ST_RD_ALIGNMENT 4
2379 #define ST_COPY_UNCOMP1 8
2380 #define ST_COPY_UNCOMP2 9
2381 #define ST_RD_ALIGNED_OFFSET 10
2382 #define ST_RD_VERBATIM 11
2383 #define ST_RD_PRE_MAIN_TREE_256 12
2384 #define ST_MAIN_TREE_256 13
2385 #define ST_RD_PRE_MAIN_TREE_REM 14
2386 #define ST_MAIN_TREE_REM 15
2387 #define ST_RD_PRE_LENGTH_TREE 16
2388 #define ST_LENGTH_TREE 17
2390 #define ST_LENGTH 19
2391 #define ST_OFFSET 20
2392 #define ST_REAL_POS 21
2396 lzx_decode(struct lzx_stream
*strm
, int last
)
2398 struct lzx_dec
*ds
= strm
->ds
;
2405 avail_in
= strm
->avail_in
;
2406 lzx_br_fixup(strm
, &(ds
->br
));
2408 if (ds
->state
< ST_MAIN
)
2409 r
= lzx_read_blocks(strm
, last
);
2411 int64_t bytes_written
= strm
->avail_out
;
2412 r
= lzx_decode_blocks(strm
, last
);
2413 bytes_written
-= strm
->avail_out
;
2414 strm
->next_out
+= bytes_written
;
2415 strm
->total_out
+= bytes_written
;
2418 strm
->total_in
+= avail_in
- strm
->avail_in
;
2423 lzx_read_blocks(struct lzx_stream
*strm
, int last
)
2425 struct lzx_dec
*ds
= strm
->ds
;
2426 struct lzx_br
*br
= &(ds
->br
);
2430 switch (ds
->state
) {
2431 case ST_RD_TRANSLATION
:
2432 if (!lzx_br_read_ahead(strm
, br
, 1)) {
2433 ds
->state
= ST_RD_TRANSLATION
;
2436 return (ARCHIVE_OK
);
2438 ds
->translation
= lzx_br_bits(br
, 1);
2439 lzx_br_consume(br
, 1);
2441 case ST_RD_TRANSLATION_SIZE
:
2442 if (ds
->translation
) {
2443 if (!lzx_br_read_ahead(strm
, br
, 32)) {
2444 ds
->state
= ST_RD_TRANSLATION_SIZE
;
2447 return (ARCHIVE_OK
);
2449 ds
->translation_size
= lzx_br_bits(br
, 16);
2450 lzx_br_consume(br
, 16);
2451 ds
->translation_size
<<= 16;
2452 ds
->translation_size
|= lzx_br_bits(br
, 16);
2453 lzx_br_consume(br
, 16);
2456 case ST_RD_BLOCK_TYPE
:
2457 if (!lzx_br_read_ahead(strm
, br
, 3)) {
2458 ds
->state
= ST_RD_BLOCK_TYPE
;
2461 return (ARCHIVE_OK
);
2463 ds
->block_type
= lzx_br_bits(br
, 3);
2464 lzx_br_consume(br
, 3);
2465 /* Check a block type. */
2466 switch (ds
->block_type
) {
2467 case VERBATIM_BLOCK
:
2468 case ALIGNED_OFFSET_BLOCK
:
2469 case UNCOMPRESSED_BLOCK
:
2472 goto failed
;/* Invalid */
2475 case ST_RD_BLOCK_SIZE
:
2476 if (!lzx_br_read_ahead(strm
, br
, 24)) {
2477 ds
->state
= ST_RD_BLOCK_SIZE
;
2480 return (ARCHIVE_OK
);
2482 ds
->block_size
= lzx_br_bits(br
, 8);
2483 lzx_br_consume(br
, 8);
2484 ds
->block_size
<<= 16;
2485 ds
->block_size
|= lzx_br_bits(br
, 16);
2486 lzx_br_consume(br
, 16);
2487 if (ds
->block_size
== 0)
2489 ds
->block_bytes_avail
= ds
->block_size
;
2490 if (ds
->block_type
!= UNCOMPRESSED_BLOCK
) {
2491 if (ds
->block_type
== VERBATIM_BLOCK
)
2492 ds
->state
= ST_RD_VERBATIM
;
2494 ds
->state
= ST_RD_ALIGNED_OFFSET
;
2498 case ST_RD_ALIGNMENT
:
2500 * Handle an Uncompressed Block.
2502 /* Skip padding to align following field on
2503 * 16-bit boundary. */
2504 if (lzx_br_is_unaligned(br
))
2505 lzx_br_consume_unaligned_bits(br
);
2507 if (lzx_br_read_ahead(strm
, br
, 16))
2508 lzx_br_consume(br
, 16);
2510 ds
->state
= ST_RD_ALIGNMENT
;
2513 return (ARCHIVE_OK
);
2516 /* Preparation to read repeated offsets R0,R1 and R2. */
2517 ds
->rbytes_avail
= 0;
2518 ds
->state
= ST_RD_R0
;
2525 /* Drain bits in the cache buffer of
2527 if (lzx_br_has(br
, 32)) {
2528 u16
= lzx_br_bits(br
, 16);
2529 lzx_br_consume(br
, 16);
2530 archive_le16enc(ds
->rbytes
, u16
);
2531 u16
= lzx_br_bits(br
, 16);
2532 lzx_br_consume(br
, 16);
2533 archive_le16enc(ds
->rbytes
+2, u16
);
2534 ds
->rbytes_avail
= 4;
2535 } else if (lzx_br_has(br
, 16)) {
2536 u16
= lzx_br_bits(br
, 16);
2537 lzx_br_consume(br
, 16);
2538 archive_le16enc(ds
->rbytes
, u16
);
2539 ds
->rbytes_avail
= 2;
2541 if (ds
->rbytes_avail
< 4 && ds
->br
.have_odd
) {
2542 ds
->rbytes
[ds
->rbytes_avail
++] =
2544 ds
->br
.have_odd
= 0;
2546 while (ds
->rbytes_avail
< 4) {
2547 if (strm
->avail_in
<= 0) {
2550 return (ARCHIVE_OK
);
2552 ds
->rbytes
[ds
->rbytes_avail
++] =
2556 ds
->rbytes_avail
= 0;
2557 if (ds
->state
== ST_RD_R0
) {
2558 ds
->r0
= archive_le32dec(ds
->rbytes
);
2561 ds
->state
= ST_RD_R1
;
2562 } else if (ds
->state
== ST_RD_R1
) {
2563 ds
->r1
= archive_le32dec(ds
->rbytes
);
2566 ds
->state
= ST_RD_R2
;
2567 } else if (ds
->state
== ST_RD_R2
) {
2568 ds
->r2
= archive_le32dec(ds
->rbytes
);
2571 /* We've gotten all repeated offsets. */
2572 ds
->state
= ST_COPY_UNCOMP1
;
2574 } while (ds
->state
!= ST_COPY_UNCOMP1
);
2576 case ST_COPY_UNCOMP1
:
2578 * Copy bytes form next_in to next_out directly.
2580 while (ds
->block_bytes_avail
) {
2583 if (strm
->avail_out
<= 0)
2584 /* Output buffer is empty. */
2585 return (ARCHIVE_OK
);
2586 if (strm
->avail_in
<= 0) {
2587 /* Input buffer is empty. */
2590 return (ARCHIVE_OK
);
2592 l
= (int)ds
->block_bytes_avail
;
2593 if (l
> ds
->w_size
- ds
->w_pos
)
2594 l
= ds
->w_size
- ds
->w_pos
;
2595 if (l
> strm
->avail_out
)
2596 l
= (int)strm
->avail_out
;
2597 if (l
> strm
->avail_in
)
2598 l
= (int)strm
->avail_in
;
2599 memcpy(strm
->next_out
, strm
->next_in
, l
);
2600 memcpy(&(ds
->w_buff
[ds
->w_pos
]),
2603 strm
->avail_in
-= l
;
2604 strm
->next_out
+= l
;
2605 strm
->avail_out
-= l
;
2606 strm
->total_out
+= l
;
2607 ds
->w_pos
= (ds
->w_pos
+ l
) & ds
->w_mask
;
2608 ds
->block_bytes_avail
-= l
;
2611 case ST_COPY_UNCOMP2
:
2612 /* Re-align; skip padding byte. */
2613 if (ds
->block_size
& 1) {
2614 if (strm
->avail_in
<= 0) {
2615 /* Input buffer is empty. */
2616 ds
->state
= ST_COPY_UNCOMP2
;
2619 return (ARCHIVE_OK
);
2624 /* This block ended. */
2625 ds
->state
= ST_RD_BLOCK_TYPE
;
2626 return (ARCHIVE_EOF
);
2627 /********************/
2628 case ST_RD_ALIGNED_OFFSET
:
2630 * Read Aligned offset tree.
2632 if (!lzx_br_read_ahead(strm
, br
, 3 * ds
->at
.len_size
)) {
2633 ds
->state
= ST_RD_ALIGNED_OFFSET
;
2636 return (ARCHIVE_OK
);
2638 memset(ds
->at
.freq
, 0, sizeof(ds
->at
.freq
));
2639 for (i
= 0; i
< ds
->at
.len_size
; i
++) {
2640 ds
->at
.bitlen
[i
] = lzx_br_bits(br
, 3);
2641 ds
->at
.freq
[ds
->at
.bitlen
[i
]]++;
2642 lzx_br_consume(br
, 3);
2644 if (!lzx_make_huffman_table(&ds
->at
))
2647 case ST_RD_VERBATIM
:
2650 case ST_RD_PRE_MAIN_TREE_256
:
2652 * Read Pre-tree for first 256 elements of main tree.
2654 if (!lzx_read_pre_tree(strm
)) {
2655 ds
->state
= ST_RD_PRE_MAIN_TREE_256
;
2658 return (ARCHIVE_OK
);
2660 if (!lzx_make_huffman_table(&ds
->pt
))
2664 case ST_MAIN_TREE_256
:
2666 * Get path lengths of first 256 elements of main tree.
2668 r
= lzx_read_bitlen(strm
, &ds
->mt
, 256);
2672 ds
->state
= ST_MAIN_TREE_256
;
2675 return (ARCHIVE_OK
);
2679 case ST_RD_PRE_MAIN_TREE_REM
:
2681 * Read Pre-tree for remaining elements of main tree.
2683 if (!lzx_read_pre_tree(strm
)) {
2684 ds
->state
= ST_RD_PRE_MAIN_TREE_REM
;
2687 return (ARCHIVE_OK
);
2689 if (!lzx_make_huffman_table(&ds
->pt
))
2693 case ST_MAIN_TREE_REM
:
2695 * Get path lengths of remaining elements of main tree.
2697 r
= lzx_read_bitlen(strm
, &ds
->mt
, -1);
2701 ds
->state
= ST_MAIN_TREE_REM
;
2704 return (ARCHIVE_OK
);
2706 if (!lzx_make_huffman_table(&ds
->mt
))
2710 case ST_RD_PRE_LENGTH_TREE
:
2712 * Read Pre-tree for remaining elements of main tree.
2714 if (!lzx_read_pre_tree(strm
)) {
2715 ds
->state
= ST_RD_PRE_LENGTH_TREE
;
2718 return (ARCHIVE_OK
);
2720 if (!lzx_make_huffman_table(&ds
->pt
))
2724 case ST_LENGTH_TREE
:
2726 * Get path lengths of remaining elements of main tree.
2728 r
= lzx_read_bitlen(strm
, &ds
->lt
, -1);
2732 ds
->state
= ST_LENGTH_TREE
;
2735 return (ARCHIVE_OK
);
2737 if (!lzx_make_huffman_table(&ds
->lt
))
2739 ds
->state
= ST_MAIN
;
2744 return (ds
->error
= ARCHIVE_FAILED
);
2748 lzx_decode_blocks(struct lzx_stream
*strm
, int last
)
2750 struct lzx_dec
*ds
= strm
->ds
;
2751 struct lzx_br bre
= ds
->br
;
2752 struct huffman
*at
= &(ds
->at
), *lt
= &(ds
->lt
), *mt
= &(ds
->mt
);
2753 const struct lzx_pos_tbl
*pos_tbl
= ds
->pos_tbl
;
2754 unsigned char *noutp
= strm
->next_out
;
2755 unsigned char *endp
= noutp
+ strm
->avail_out
;
2756 unsigned char *w_buff
= ds
->w_buff
;
2757 unsigned char *at_bitlen
= at
->bitlen
;
2758 unsigned char *lt_bitlen
= lt
->bitlen
;
2759 unsigned char *mt_bitlen
= mt
->bitlen
;
2760 size_t block_bytes_avail
= ds
->block_bytes_avail
;
2761 int at_max_bits
= at
->max_bits
;
2762 int lt_max_bits
= lt
->max_bits
;
2763 int mt_max_bits
= mt
->max_bits
;
2764 int c
, copy_len
= ds
->copy_len
, copy_pos
= ds
->copy_pos
;
2765 int w_pos
= ds
->w_pos
, w_mask
= ds
->w_mask
, w_size
= ds
->w_size
;
2766 int length_header
= ds
->length_header
;
2767 int offset_bits
= ds
->offset_bits
;
2768 int position_slot
= ds
->position_slot
;
2769 int r0
= ds
->r0
, r1
= ds
->r1
, r2
= ds
->r2
;
2770 int state
= ds
->state
;
2771 char block_type
= ds
->block_type
;
2777 if (block_bytes_avail
== 0) {
2778 /* This block ended. */
2779 ds
->state
= ST_RD_BLOCK_TYPE
;
2781 ds
->block_bytes_avail
=
2783 ds
->copy_len
= copy_len
;
2784 ds
->copy_pos
= copy_pos
;
2785 ds
->length_header
= length_header
;
2786 ds
->position_slot
= position_slot
;
2787 ds
->r0
= r0
; ds
->r1
= r1
; ds
->r2
= r2
;
2789 strm
->avail_out
= endp
- noutp
;
2790 return (ARCHIVE_EOF
);
2793 /* Output buffer is empty. */
2796 if (!lzx_br_read_ahead(strm
, &bre
,
2800 /* Remaining bits are less than
2801 * maximum bits(mt.max_bits) but maybe
2802 * it still remains as much as we need,
2803 * so we should try to use it with
2805 c
= lzx_decode_huffman(mt
,
2807 &bre
, mt_max_bits
));
2808 lzx_br_consume(&bre
, mt_bitlen
[c
]);
2809 if (!lzx_br_has(&bre
, 0))
2810 goto failed
;/* Over read. */
2812 c
= lzx_decode_huffman(mt
,
2813 lzx_br_bits(&bre
, mt_max_bits
));
2814 lzx_br_consume(&bre
, mt_bitlen
[c
]);
2819 * 'c' is exactly literal code.
2821 /* Save a decoded code to reference it
2824 w_pos
= (w_pos
+ 1) & w_mask
;
2825 /* Store the decoded code to output buffer. */
2827 block_bytes_avail
--;
2830 * Get a match code, its length and offset.
2833 length_header
= c
& 7;
2834 position_slot
= c
>> 3;
2840 if (length_header
== 7) {
2841 if (!lzx_br_read_ahead(strm
, &bre
,
2847 c
= lzx_decode_huffman(lt
,
2849 &bre
, lt_max_bits
));
2850 lzx_br_consume(&bre
, lt_bitlen
[c
]);
2851 if (!lzx_br_has(&bre
, 0))
2852 goto failed
;/* Over read. */
2854 c
= lzx_decode_huffman(lt
,
2855 lzx_br_bits(&bre
, lt_max_bits
));
2856 lzx_br_consume(&bre
, lt_bitlen
[c
]);
2858 copy_len
= c
+ 7 + 2;
2860 copy_len
= length_header
+ 2;
2861 if ((size_t)copy_len
> block_bytes_avail
)
2866 switch (position_slot
) {
2867 case 0: /* Use repeated offset 0. */
2869 state
= ST_REAL_POS
;
2871 case 1: /* Use repeated offset 1. */
2873 /* Swap repeated offset. */
2876 state
= ST_REAL_POS
;
2878 case 2: /* Use repeated offset 2. */
2880 /* Swap repeated offset. */
2883 state
= ST_REAL_POS
;
2887 pos_tbl
[position_slot
].footer_bits
;
2893 * Get the offset, which is a distance from
2894 * current window position.
2896 if (block_type
== ALIGNED_OFFSET_BLOCK
&&
2898 int offbits
= offset_bits
- 3;
2900 if (!lzx_br_read_ahead(strm
, &bre
, offbits
)) {
2906 copy_pos
= lzx_br_bits(&bre
, offbits
) << 3;
2908 /* Get an aligned number. */
2909 if (!lzx_br_read_ahead(strm
, &bre
,
2910 offbits
+ at_max_bits
)) {
2915 lzx_br_consume(&bre
, offbits
);
2916 c
= lzx_decode_huffman(at
,
2917 lzx_br_bits_forced(&bre
,
2919 lzx_br_consume(&bre
, at_bitlen
[c
]);
2920 if (!lzx_br_has(&bre
, 0))
2921 goto failed
;/* Over read. */
2923 lzx_br_consume(&bre
, offbits
);
2924 c
= lzx_decode_huffman(at
,
2925 lzx_br_bits(&bre
, at_max_bits
));
2926 lzx_br_consume(&bre
, at_bitlen
[c
]);
2928 /* Add an aligned number. */
2931 if (!lzx_br_read_ahead(strm
, &bre
,
2938 copy_pos
= lzx_br_bits(&bre
, offset_bits
);
2939 lzx_br_consume(&bre
, offset_bits
);
2941 copy_pos
+= pos_tbl
[position_slot
].base
-2;
2943 /* Update repeated offset LRU queue. */
2950 * Compute a real position in window.
2952 copy_pos
= (w_pos
- copy_pos
) & w_mask
;
2956 * Copy several bytes as extracted data from the window
2957 * into the output buffer.
2960 const unsigned char *s
;
2964 if (copy_pos
> w_pos
) {
2965 if (l
> w_size
- copy_pos
)
2966 l
= w_size
- copy_pos
;
2968 if (l
> w_size
- w_pos
)
2971 if (noutp
+ l
>= endp
)
2972 l
= (int)(endp
- noutp
);
2973 s
= w_buff
+ copy_pos
;
2974 if (l
>= 8 && ((copy_pos
+ l
< w_pos
)
2975 || (w_pos
+ l
< copy_pos
))) {
2976 memcpy(w_buff
+ w_pos
, s
, l
);
2977 memcpy(noutp
, s
, l
);
2983 for (li
= 0; li
< l
; li
++)
2984 noutp
[li
] = d
[li
] = s
[li
];
2987 copy_pos
= (copy_pos
+ l
) & w_mask
;
2988 w_pos
= (w_pos
+ l
) & w_mask
;
2989 block_bytes_avail
-= l
;
2991 /* A copy of current pattern ended. */
2994 if (noutp
>= endp
) {
2995 /* Output buffer is empty. */
3005 return (ds
->error
= ARCHIVE_FAILED
);
3008 ds
->block_bytes_avail
= block_bytes_avail
;
3009 ds
->copy_len
= copy_len
;
3010 ds
->copy_pos
= copy_pos
;
3011 ds
->length_header
= length_header
;
3012 ds
->offset_bits
= offset_bits
;
3013 ds
->position_slot
= position_slot
;
3014 ds
->r0
= r0
; ds
->r1
= r1
; ds
->r2
= r2
;
3017 strm
->avail_out
= endp
- noutp
;
3018 return (ARCHIVE_OK
);
3022 lzx_read_pre_tree(struct lzx_stream
*strm
)
3024 struct lzx_dec
*ds
= strm
->ds
;
3025 struct lzx_br
*br
= &(ds
->br
);
3029 memset(ds
->pt
.freq
, 0, sizeof(ds
->pt
.freq
));
3030 for (i
= ds
->loop
; i
< ds
->pt
.len_size
; i
++) {
3031 if (!lzx_br_read_ahead(strm
, br
, 4)) {
3035 ds
->pt
.bitlen
[i
] = lzx_br_bits(br
, 4);
3036 ds
->pt
.freq
[ds
->pt
.bitlen
[i
]]++;
3037 lzx_br_consume(br
, 4);
3044 * Read a bunch of bit-lengths from pre-tree.
3047 lzx_read_bitlen(struct lzx_stream
*strm
, struct huffman
*d
, int end
)
3049 struct lzx_dec
*ds
= strm
->ds
;
3050 struct lzx_br
*br
= &(ds
->br
);
3051 int c
, i
, j
, ret
, same
;
3056 memset(d
->freq
, 0, sizeof(d
->freq
));
3062 if (!lzx_br_read_ahead(strm
, br
, ds
->pt
.max_bits
))
3064 rbits
= lzx_br_bits(br
, ds
->pt
.max_bits
);
3065 c
= lzx_decode_huffman(&(ds
->pt
), rbits
);
3067 case 17:/* several zero lengths, from 4 to 19. */
3068 if (!lzx_br_read_ahead(strm
, br
, ds
->pt
.bitlen
[c
]+4))
3070 lzx_br_consume(br
, ds
->pt
.bitlen
[c
]);
3071 same
= lzx_br_bits(br
, 4) + 4;
3073 return (-1);/* Invalid */
3074 lzx_br_consume(br
, 4);
3075 for (j
= 0; j
< same
; j
++)
3078 case 18:/* many zero lengths, from 20 to 51. */
3079 if (!lzx_br_read_ahead(strm
, br
, ds
->pt
.bitlen
[c
]+5))
3081 lzx_br_consume(br
, ds
->pt
.bitlen
[c
]);
3082 same
= lzx_br_bits(br
, 5) + 20;
3084 return (-1);/* Invalid */
3085 lzx_br_consume(br
, 5);
3086 memset(d
->bitlen
+ i
, 0, same
);
3089 case 19:/* a few same lengths. */
3090 if (!lzx_br_read_ahead(strm
, br
,
3091 ds
->pt
.bitlen
[c
]+1+ds
->pt
.max_bits
))
3093 lzx_br_consume(br
, ds
->pt
.bitlen
[c
]);
3094 same
= lzx_br_bits(br
, 1) + 4;
3097 lzx_br_consume(br
, 1);
3098 rbits
= lzx_br_bits(br
, ds
->pt
.max_bits
);
3099 c
= lzx_decode_huffman(&(ds
->pt
), rbits
);
3100 lzx_br_consume(br
, ds
->pt
.bitlen
[c
]);
3101 c
= (d
->bitlen
[i
] - c
+ 17) % 17;
3103 return (-1);/* Invalid */
3104 for (j
= 0; j
< same
; j
++)
3109 lzx_br_consume(br
, ds
->pt
.bitlen
[c
]);
3110 c
= (d
->bitlen
[i
] - c
+ 17) % 17;
3112 return (-1);/* Invalid */
3125 lzx_huffman_init(struct huffman
*hf
, size_t len_size
, int tbl_bits
)
3129 if (hf
->bitlen
== NULL
|| hf
->len_size
!= (int)len_size
) {
3131 hf
->bitlen
= calloc(len_size
, sizeof(hf
->bitlen
[0]));
3132 if (hf
->bitlen
== NULL
)
3133 return (ARCHIVE_FATAL
);
3134 hf
->len_size
= (int)len_size
;
3136 memset(hf
->bitlen
, 0, len_size
* sizeof(hf
->bitlen
[0]));
3137 if (hf
->tbl
== NULL
) {
3138 if (tbl_bits
< HTBL_BITS
)
3142 hf
->tbl
= malloc(((size_t)1 << bits
) * sizeof(hf
->tbl
[0]));
3143 if (hf
->tbl
== NULL
)
3144 return (ARCHIVE_FATAL
);
3145 hf
->tbl_bits
= tbl_bits
;
3147 if (hf
->tree
== NULL
&& tbl_bits
> HTBL_BITS
) {
3148 hf
->tree_avail
= 1 << (tbl_bits
- HTBL_BITS
+ 4);
3149 hf
->tree
= malloc(hf
->tree_avail
* sizeof(hf
->tree
[0]));
3150 if (hf
->tree
== NULL
)
3151 return (ARCHIVE_FATAL
);
3153 return (ARCHIVE_OK
);
3157 lzx_huffman_free(struct huffman
*hf
)
3165 * Make a huffman coding table.
3168 lzx_make_huffman_table(struct huffman
*hf
)
3171 const unsigned char *bitlen
;
3172 int bitptn
[17], weight
[17];
3173 int i
, maxbits
= 0, ptn
, tbl_size
, w
;
3174 int diffbits
, len_avail
;
3177 * Initialize bit patterns.
3180 for (i
= 1, w
= 1 << 15; i
<= 16; i
++, w
>>= 1) {
3184 ptn
+= hf
->freq
[i
] * w
;
3188 if ((ptn
& 0xffff) != 0 || maxbits
> hf
->tbl_bits
)
3189 return (0);/* Invalid */
3191 hf
->max_bits
= maxbits
;
3194 * Cut out extra bits which we won't house in the table.
3195 * This preparation reduces the same calculation in the for-loop
3199 int ebits
= 16 - maxbits
;
3200 for (i
= 1; i
<= maxbits
; i
++) {
3201 bitptn
[i
] >>= ebits
;
3202 weight
[i
] >>= ebits
;
3205 if (maxbits
> HTBL_BITS
) {
3209 diffbits
= maxbits
- HTBL_BITS
;
3210 for (i
= 1; i
<= HTBL_BITS
; i
++) {
3211 bitptn
[i
] >>= diffbits
;
3212 weight
[i
] >>= diffbits
;
3214 htbl_max
= bitptn
[HTBL_BITS
] +
3215 weight
[HTBL_BITS
] * hf
->freq
[HTBL_BITS
];
3216 p
= &(hf
->tbl
[htbl_max
]);
3217 while (p
< &hf
->tbl
[1U<<HTBL_BITS
])
3221 hf
->shift_bits
= diffbits
;
3226 tbl_size
= 1 << HTBL_BITS
;
3228 bitlen
= hf
->bitlen
;
3229 len_avail
= hf
->len_size
;
3231 for (i
= 0; i
< len_avail
; i
++) {
3240 /* Get a bit pattern */
3244 if (len
<= HTBL_BITS
) {
3245 /* Calculate next bit pattern */
3246 if ((bitptn
[len
] = ptn
+ cnt
) > tbl_size
)
3247 return (0);/* Invalid */
3248 /* Update the table */
3251 p
[cnt
] = (uint16_t)i
;
3256 * A bit length is too big to be housed to a direct table,
3257 * so we use a tree model for its extra bits.
3259 bitptn
[len
] = ptn
+ cnt
;
3260 bit
= 1U << (diffbits
-1);
3261 extlen
= len
- HTBL_BITS
;
3263 p
= &(tbl
[ptn
>> diffbits
]);
3265 *p
= len_avail
+ hf
->tree_used
;
3266 ht
= &(hf
->tree
[hf
->tree_used
++]);
3267 if (hf
->tree_used
> hf
->tree_avail
)
3268 return (0);/* Invalid */
3272 if (*p
< len_avail
||
3273 *p
>= (len_avail
+ hf
->tree_used
))
3274 return (0);/* Invalid */
3275 ht
= &(hf
->tree
[*p
- len_avail
]);
3277 while (--extlen
> 0) {
3279 if (ht
->left
< len_avail
) {
3280 ht
->left
= len_avail
+ hf
->tree_used
;
3281 ht
= &(hf
->tree
[hf
->tree_used
++]);
3282 if (hf
->tree_used
> hf
->tree_avail
)
3283 return (0);/* Invalid */
3287 ht
= &(hf
->tree
[ht
->left
- len_avail
]);
3290 if (ht
->right
< len_avail
) {
3291 ht
->right
= len_avail
+ hf
->tree_used
;
3292 ht
= &(hf
->tree
[hf
->tree_used
++]);
3293 if (hf
->tree_used
> hf
->tree_avail
)
3294 return (0);/* Invalid */
3298 ht
= &(hf
->tree
[ht
->right
- len_avail
]);
3305 return (0);/* Invalid */
3306 ht
->left
= (uint16_t)i
;
3309 return (0);/* Invalid */
3310 ht
->right
= (uint16_t)i
;
3317 lzx_decode_huffman_tree(struct huffman
*hf
, unsigned rbits
, int c
)
3323 extlen
= hf
->shift_bits
;
3324 while (c
>= hf
->len_size
) {
3326 if (extlen
-- <= 0 || c
>= hf
->tree_used
)
3328 if (rbits
& (1U << extlen
))
3337 lzx_decode_huffman(struct huffman
*hf
, unsigned rbits
)
3341 * At first search an index table for a bit pattern.
3342 * If it fails, search a huffman tree for.
3344 c
= hf
->tbl
[rbits
>> hf
->shift_bits
];
3345 if (c
< hf
->len_size
)
3347 /* This bit pattern needs to be found out at a huffman tree. */
3348 return (lzx_decode_huffman_tree(hf
, rbits
, c
));