Skip to content

Commit 728293e

Browse files
committed
Backport export of lzma_mt_block_size symbol.
This restores binary compatibility against liblzma 5.6.0 library. PR: 278127 (cherry picked from commit 5ffb19a)
1 parent 6b55e41 commit 728293e

File tree

11 files changed

+78
-27
lines changed

11 files changed

+78
-27
lines changed

contrib/xz/src/liblzma/api/lzma/container.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,34 @@ extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
435435
lzma_nothrow lzma_attr_warn_unused_result;
436436

437437

438+
/**
439+
* \brief Calculate recommended Block size for multithreaded .xz encoder
440+
*
441+
* This calculates a recommended Block size for multithreaded encoding given
442+
* a filter chain. This is used internally by lzma_stream_encoder_mt() to
443+
* determine the Block size if the block_size member is not set to the
444+
* special value of 0 in the lzma_mt options struct.
445+
*
446+
* If one wishes to change the filters between Blocks, this function is
447+
* helpful to set the block_size member of the lzma_mt struct before calling
448+
* lzma_stream_encoder_mt(). Since the block_size member represents the
449+
* maximum possible Block size for the multithreaded .xz encoder, one can
450+
* use this function to find the maximum recommended Block size based on
451+
* all planned filter chains. Otherwise, the multithreaded encoder will
452+
* base its maximum Block size on the first filter chain used (if the
453+
* block_size member is not set), which may unnecessarily limit the Block
454+
* size for a later filter chain.
455+
*
456+
* \param filters Array of filters terminated with
457+
* .id == LZMA_VLI_UNKNOWN.
458+
*
459+
* \return Recommended Block size in bytes, or UINT64_MAX if
460+
* an error occurred.
461+
*/
462+
extern LZMA_API(uint64_t) lzma_mt_block_size(const lzma_filter *filters)
463+
lzma_nothrow;
464+
465+
438466
/**
439467
* \brief Initialize .lzma encoder (legacy file format)
440468
*

contrib/xz/src/liblzma/common/filter_encoder.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ typedef struct {
3333
/// Calculates the recommended Uncompressed Size for .xz Blocks to
3434
/// which the input data can be split to make multithreaded
3535
/// encoding possible. If this is NULL, it is assumed that
36-
/// the encoder is fast enough with single thread.
36+
/// the encoder is fast enough with single thread. If the options
37+
/// are invalid, UINT64_MAX is returned.
3738
uint64_t (*block_size)(const void *options);
3839

3940
/// Tells the size of the Filter Properties field. If options are
@@ -248,26 +249,29 @@ lzma_raw_encoder_memusage(const lzma_filter *filters)
248249
}
249250

250251

251-
extern uint64_t
252+
extern LZMA_API(uint64_t)
252253
lzma_mt_block_size(const lzma_filter *filters)
253254
{
255+
if (filters == NULL)
256+
return UINT64_MAX;
257+
254258
uint64_t max = 0;
255259

256260
for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
257261
const lzma_filter_encoder *const fe
258262
= encoder_find(filters[i].id);
263+
if (fe == NULL)
264+
return UINT64_MAX;
265+
259266
if (fe->block_size != NULL) {
260267
const uint64_t size
261268
= fe->block_size(filters[i].options);
262-
if (size == 0)
263-
return 0;
264-
265269
if (size > max)
266270
max = size;
267271
}
268272
}
269273

270-
return max;
274+
return max == 0 ? UINT64_MAX : max;
271275
}
272276

273277

contrib/xz/src/liblzma/common/filter_encoder.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///////////////////////////////////////////////////////////////////////////////
22
//
3-
/// \file filter_encoder.c
3+
/// \file filter_encoder.h
44
/// \brief Filter ID mapping to filter-specific functions
55
//
66
// Author: Lasse Collin
@@ -16,10 +16,6 @@
1616
#include "common.h"
1717

1818

19-
// FIXME: Might become a part of the public API.
20-
extern uint64_t lzma_mt_block_size(const lzma_filter *filters);
21-
22-
2319
extern lzma_ret lzma_raw_encoder_init(
2420
lzma_next_coder *next, const lzma_allocator *allocator,
2521
const lzma_filter *filters);

contrib/xz/src/liblzma/common/stream_encoder_mt.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -979,20 +979,18 @@ get_options(const lzma_mt *options, lzma_options_easy *opt_easy,
979979
*filters = opt_easy->filters;
980980
}
981981

982-
// Block size
983-
if (options->block_size > 0) {
984-
if (options->block_size > BLOCK_SIZE_MAX)
985-
return LZMA_OPTIONS_ERROR;
986-
982+
// If the Block size is not set, determine it from the filter chain.
983+
if (options->block_size > 0)
987984
*block_size = options->block_size;
988-
} else {
989-
// Determine the Block size from the filter chain.
985+
else
990986
*block_size = lzma_mt_block_size(*filters);
991-
if (*block_size == 0)
992-
return LZMA_OPTIONS_ERROR;
993987

994-
assert(*block_size <= BLOCK_SIZE_MAX);
995-
}
988+
// UINT64_MAX > BLOCK_SIZE_MAX, so the second condition
989+
// should be optimized out by any reasonable compiler.
990+
// The second condition should be there in the unlikely event that
991+
// the macros change and UINT64_MAX < BLOCK_SIZE_MAX.
992+
if (*block_size > BLOCK_SIZE_MAX || *block_size == UINT64_MAX)
993+
return LZMA_OPTIONS_ERROR;
996994

997995
// Calculate the maximum amount output that a single output buffer
998996
// may need to hold. This is the same as the maximum total size of

contrib/xz/src/liblzma/liblzma_generic.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,8 @@ global:
119119
lzma_str_list_filters;
120120
lzma_str_to_filters;
121121
} XZ_5.2;
122+
123+
XZ_5.5.0alpha {
124+
global:
125+
lzma_mt_block_size;
126+
} XZ_5.4;

contrib/xz/src/liblzma/liblzma_linux.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,8 @@ global:
134134
lzma_str_list_filters;
135135
lzma_str_to_filters;
136136
} XZ_5.2;
137+
138+
XZ_5.5.0alpha {
139+
global:
140+
lzma_mt_block_size;
141+
} XZ_5.4;

contrib/xz/src/liblzma/lz/lz_encoder.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ lz_encoder_prepare(lzma_mf *mf, const lzma_allocator *allocator,
196196
// For now, the dictionary size is limited to 1.5 GiB. This may grow
197197
// in the future if needed, but it needs a little more work than just
198198
// changing this check.
199-
if (lz_options->dict_size < LZMA_DICT_SIZE_MIN
200-
|| lz_options->dict_size
201-
> (UINT32_C(1) << 30) + (UINT32_C(1) << 29)
199+
if (!IS_ENC_DICT_SIZE_VALID(lz_options->dict_size)
202200
|| lz_options->nice_len > lz_options->match_len_max)
203201
return true;
204202

contrib/xz/src/liblzma/lz/lz_encoder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
#include "common.h"
1818

1919

20+
// For now, the dictionary size is limited to 1.5 GiB. This may grow
21+
// in the future if needed, but it needs a little more work than just
22+
// changing this check.
23+
#define IS_ENC_DICT_SIZE_VALID(size) \
24+
((size) >= LZMA_DICT_SIZE_MIN \
25+
&& (size) <= (UINT32_C(1) << 30) + (UINT32_C(1) << 29))
26+
27+
2028
/// A table of these is used by the LZ-based encoder to hold
2129
/// the length-distance pairs found by the match finder.
2230
typedef struct {

contrib/xz/src/liblzma/lzma/lzma2_encoder.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ lzma_lzma2_block_size(const void *options)
409409
{
410410
const lzma_options_lzma *const opt = options;
411411

412+
if (!IS_ENC_DICT_SIZE_VALID(opt->dict_size))
413+
return UINT64_MAX;
414+
412415
// Use at least 1 MiB to keep compression ratio better.
413416
return my_max((uint64_t)(opt->dict_size) * 3, UINT64_C(1) << 20);
414417
}

lib/liblzma/Symbol.map

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ XZ_5.4 {
114114
lzma_str_to_filters;
115115
};
116116

117+
XZ_5.6 {
118+
lzma_mt_block_size;
119+
};
120+
117121
XZprivate_1.0 {
118122
lzma_alloc;
119123
lzma_alloc_zero;
@@ -173,7 +177,6 @@ XZprivate_1.0 {
173177
lzma_mf_hc3_skip;
174178
lzma_mf_hc4_find;
175179
lzma_mf_hc4_skip;
176-
lzma_mt_block_size;
177180
lzma_next_end;
178181
lzma_next_filter_init;
179182
lzma_next_filter_update;

0 commit comments

Comments
 (0)