1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Ivan Trubach <mr.trubach@icloud.com>
3 Date: Sat, 27 Jul 2024 16:34:17 +0300
4 Subject: [PATCH 02/19] Update for modern liblzma5 versions
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 This change updates liblzma usage for modern xz versions (≥ 5, that is,
10 released less than a decade ago).
12 It also fixes missing realloc buffer calls that were supposed to be
13 there but were lost in xar-420 (and Apple does not ship xar with LZMA
14 support so nobody noticed). See also the offending commit:
15 https://github.com/apple-oss-distributions/xar/commit/2426082efec74e9ed545cc4f5812ad16322bdf2c
17 xar/lib/lzmaxar.c | 65 ++++++++---------------------------------------
18 1 file changed, 10 insertions(+), 55 deletions(-)
20 diff --git a/xar/lib/lzmaxar.c b/xar/lib/lzmaxar.c
21 index ba9c868..8dcb484 100644
22 --- a/xar/lib/lzmaxar.c
23 +++ b/xar/lib/lzmaxar.c
29 -#define UINT32_C(v) (v ## U) /* from <stdint.h> normally */
32 -#define LZMA_VERSION UINT32_C(40420000) /* = 4.42.0alpha6 */
36 uint8_t lzmacompressed;
38 - lzma_options_stream options;
39 - lzma_allocator allocator;
40 -#if LZMA_VERSION < 40420010U
41 - lzma_memory_limitter *limit;
43 - lzma_memlimit *limit;
47 #define preset_level 7
48 -#define memory_limit 93*1024*1024 /* 1=1M, 5=24M, 6=39M, 7=93M, 8=185M, 9=369M */
50 #define LZMA_CONTEXT(x) ((struct _lzma_context *)(*x))
52 @@ -116,9 +101,7 @@ int xar_lzma_fromheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_t
54 if( strcmp(opt, "application/x-lzma") != 0 ) return 0;
56 - lzma_init_decoder();
57 - LZMA_CONTEXT(context)->lzma = LZMA_STREAM_INIT_VAR;
58 - r = lzma_stream_decoder(&LZMA_CONTEXT(context)->lzma, NULL, NULL);
59 + r = lzma_stream_decoder(&LZMA_CONTEXT(context)->lzma, UINT64_MAX, LZMA_CONCATENATED);
60 if( (r != LZMA_OK) ) {
62 xar_err_set_file(x, f);
63 @@ -194,11 +177,6 @@ int xar_lzma_toheap_done(xar_t x, xar_file_t f, xar_prop_t p, void **context) {
65 if( LZMA_CONTEXT(context)->lzmacompressed){
66 lzma_end(&LZMA_CONTEXT(context)->lzma);
67 -#if LZMA_VERSION < 40420010U
68 - lzma_memory_limitter_end(LZMA_CONTEXT(context)->limit, 1);
70 - lzma_memlimit_end(LZMA_CONTEXT(context)->limit, 1);
73 tmpp = xar_prop_pset(f, p, "encoding", NULL);
75 @@ -222,7 +200,7 @@ int32_t xar_lzma_toheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_
77 /* on first run, we init the context and check the compression type */
78 if( !LZMA_CONTEXT(context) ) {
79 - int level = preset_level;
80 + uint32_t level = preset_level;
81 *context = calloc(1,sizeof(struct _lzma_context));
83 opt = xar_opt_get(x, XAR_OPT_COMPRESSION);
84 @@ -243,35 +221,7 @@ int32_t xar_lzma_toheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_
88 - lzma_init_encoder();
89 - LZMA_CONTEXT(context)->options.check = LZMA_CHECK_CRC64;
90 - LZMA_CONTEXT(context)->options.has_crc32 = 1; /* true */
91 - LZMA_CONTEXT(context)->options.alignment = 0;
92 -#if defined (__ppc__) || defined (powerpc) || defined (__ppc64__)
93 - LZMA_CONTEXT(context)->options.filters[0].id = LZMA_FILTER_POWERPC;
94 -#elif defined (__i386__) || defined (__amd64__) || defined(__x86_64__)
95 - LZMA_CONTEXT(context)->options.filters[0].id = LZMA_FILTER_X86;
97 - LZMA_CONTEXT(context)->options.filters[0].id = LZMA_FILTER_COPY;
99 - LZMA_CONTEXT(context)->options.filters[0].options = NULL;
100 - LZMA_CONTEXT(context)->options.filters[1].id = LZMA_FILTER_LZMA;
101 - LZMA_CONTEXT(context)->options.filters[1].options = (lzma_options_lzma *)(lzma_preset_lzma + level - 1);
102 - /* Terminate the filter options array. */
103 - LZMA_CONTEXT(context)->options.filters[2].id = UINT64_MAX;
104 - LZMA_CONTEXT(context)->lzma = LZMA_STREAM_INIT_VAR;
105 -#if LZMA_VERSION < 40420010U
106 - LZMA_CONTEXT(context)->limit = lzma_memory_limitter_create(memory_limit);
107 - LZMA_CONTEXT(context)->allocator.alloc = (void*) lzma_memory_alloc;
108 - LZMA_CONTEXT(context)->allocator.free = (void*) lzma_memory_free;
110 - LZMA_CONTEXT(context)->limit = lzma_memlimit_create(memory_limit);
111 - LZMA_CONTEXT(context)->allocator.alloc = (void*) lzma_memlimit_alloc;
112 - LZMA_CONTEXT(context)->allocator.free = (void*) lzma_memlimit_free;
114 - LZMA_CONTEXT(context)->allocator.opaque = LZMA_CONTEXT(context)->limit;
115 - LZMA_CONTEXT(context)->lzma.allocator = &LZMA_CONTEXT(context)->allocator;
116 - r = lzma_stream_encoder_single(&LZMA_CONTEXT(context)->lzma, &(LZMA_CONTEXT(context)->options));
117 + r = lzma_easy_encoder(&LZMA_CONTEXT(context)->lzma, level, LZMA_CHECK_CRC64);
118 if( (r != LZMA_OK) ) {
120 xar_err_set_file(x, f);
121 @@ -279,6 +229,7 @@ int32_t xar_lzma_toheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_
122 xar_err_callback(x, XAR_SEVERITY_FATAL, XAR_ERR_ARCHIVE_CREATION);
126 LZMA_CONTEXT(context)->lzmacompressed = 1;
129 @@ -303,7 +254,8 @@ int32_t xar_lzma_toheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_
132 abort(); /* Someone has somehow malloced over 2^64 bits of ram. */
135 + out = realloc(out, outlen);
136 if( out == NULL ) abort();
138 LZMA_CONTEXT(context)->lzma.next_out = ((unsigned char *)out) + offset;
139 @@ -318,7 +270,10 @@ int32_t xar_lzma_toheap_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_
143 - abort(); /* Someone has somehow malloced over 2^64 bits of ram. */ if( out == NULL ) abort();
144 + abort(); /* Someone has somehow malloced over 2^64 bits of ram. */
146 + out = realloc(out, outlen);
147 + if( out == NULL ) abort();
149 LZMA_CONTEXT(context)->lzma.next_out = ((unsigned char *)out) + offset;
150 LZMA_CONTEXT(context)->lzma.avail_out = outlen - offset;