mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Merge claude/project-thread-0izcgq (encoder raw dictionaries)
At qualities 10 and 11, attaching a dictionary makes transform_io return "#unsupported encoder quality" until the Zopfli path's compound dictionary lookups are ported.
This commit is contained in:
10 files changed
+3056
-111
No files matched your search
@@ -31,16 +31,22 @@ enough for realistic inputs: with a workbuf shorter than `max_incl`, an
|
||||
unusual input can make `transform_io` return `"#bad workbuf length"`.
|
||||
|
||||
Encoders may have further, format-specific methods. For example, the brotli
|
||||
encoder has `request_flush`, `request_emit_metadata` and
|
||||
`max_compressed_size`.
|
||||
encoder has `request_flush`, `request_emit_metadata`, `max_compressed_size`
|
||||
and, for raw (LZ77 prefix) dictionaries, `add_dictionary` and
|
||||
`add_dictionary_workbuf_len`. Like quirks, dictionaries are added before the
|
||||
first `transform_io` call.
|
||||
|
||||
|
||||
## Implementations
|
||||
|
||||
- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 11, without
|
||||
encoder dictionaries, including large-window brotli, with windows of up to
|
||||
1 GiB, at qualities 3 to 11). Its `workbuf_len` is the worst case for the
|
||||
parameters (more than 2 GiB for large-window brotli at lgwin 30; at
|
||||
qualities 10 and 11, `min_incl` is a smaller practical bound), but a
|
||||
shorter workbuf works for short inputs: see the README's "Encoder Workbuf
|
||||
Layout" section.
|
||||
- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 11,
|
||||
including large-window brotli, with windows of up to 1 GiB, at qualities 3
|
||||
to 11). Its `workbuf_len` is the worst case for the parameters (more than
|
||||
2 GiB for large-window brotli at lgwin 30; at qualities 10 and 11,
|
||||
`min_incl` is a smaller practical bound), but a shorter workbuf works for
|
||||
short inputs: see the README's "Encoder Workbuf Layout" section. Up to 15
|
||||
raw dictionaries can be attached (like libbrotlienc's
|
||||
`BrotliEncoderAttachPreparedDictionary`) at qualities 0 to 9, see the
|
||||
README's "Encoder Dictionaries" section. They live at the start of the
|
||||
workbuf, with their prepared hash tables, and `workbuf_len` grows
|
||||
accordingly.
|
||||
@@ -9236,6 +9236,7 @@ extern const char wuffs_brotli__error__truncated_input[];
|
||||
extern const char wuffs_brotli__error__unsupported_large_window[];
|
||||
extern const char wuffs_brotli__error__unsupported_multiple_custom_static_dictionaries[];
|
||||
extern const char wuffs_brotli__error__unsupported_encoder_quality[];
|
||||
extern const char wuffs_brotli__error__unsupported_serialized_encoder_dictionary[];
|
||||
|
||||
// ---------------- Public Consts
|
||||
|
||||
@@ -9457,6 +9458,20 @@ wuffs_brotli__encoder__transform_io(
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__encoder__add_dictionary(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint32_t a_dict_type,
|
||||
wuffs_base__slice_u8 a_dict,
|
||||
wuffs_base__slice_u8 a_workbuf);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_dict_len);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__metablock_tester__bits_entropy(
|
||||
@@ -10032,6 +10047,15 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint32_t f_hr_factor_remove;
|
||||
uint64_t f_hr_next_ix;
|
||||
uint64_t f_hr_off;
|
||||
uint32_t f_dict_num_chunks;
|
||||
uint64_t f_dict_total_size;
|
||||
uint64_t f_dict_end;
|
||||
uint64_t f_dict_chunk_offsets[16];
|
||||
uint64_t f_dc_src_off[15];
|
||||
uint64_t f_dc_size[15];
|
||||
uint32_t f_dc_bucket_bits[15];
|
||||
uint32_t f_dc_slot_bits[15];
|
||||
uint64_t f_dc_num_items[15];
|
||||
uint64_t f_sr_len;
|
||||
uint64_t f_sr_distance;
|
||||
uint64_t f_sr_score;
|
||||
@@ -10067,6 +10091,7 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint32_t f_zp_costdiff;
|
||||
|
||||
uint32_t p_transform_io;
|
||||
uint32_t p_do_transform_io;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
@@ -10142,6 +10167,9 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint8_t f_cl_depth[32];
|
||||
uint16_t f_cl_bits[32];
|
||||
uint32_t f_hash_table[131072];
|
||||
uint32_t f_dp_slot_size[4096];
|
||||
uint32_t f_dp_slot_limit[4096];
|
||||
uint32_t f_dp_slot_off[4096];
|
||||
uint32_t f_command_buf[131072];
|
||||
uint8_t f_literal_buf[131072];
|
||||
uint32_t f_sd_matches[38];
|
||||
@@ -10167,7 +10195,7 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint64_t v_mark;
|
||||
uint64_t v_copy_input_size;
|
||||
uint64_t scratch;
|
||||
} s_transform_io;
|
||||
} s_do_transform_io;
|
||||
} private_data;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -10280,6 +10308,20 @@ struct wuffs_brotli__encoder__struct {
|
||||
return wuffs_brotli__encoder__transform_io(this, a_dst, a_src, a_workbuf);
|
||||
}
|
||||
|
||||
inline wuffs_base__status
|
||||
add_dictionary(
|
||||
uint32_t a_dict_type,
|
||||
wuffs_base__slice_u8 a_dict,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
return wuffs_brotli__encoder__add_dictionary(this, a_dict_type, a_dict, a_workbuf);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
add_dictionary_workbuf_len(
|
||||
uint64_t a_dict_len) const {
|
||||
return wuffs_brotli__encoder__add_dictionary_workbuf_len(this, a_dict_len);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
}; // struct wuffs_brotli__encoder__struct
|
||||
|
||||
@@ -43040,6 +43082,7 @@ const char wuffs_brotli__error__internal_error_inconsistent_huffman_table[] = "#
|
||||
const char wuffs_brotli__error__internal_error_inconsistent_ring_buffer[] = "#brotli: internal error: inconsistent ring buffer";
|
||||
const char wuffs_brotli__error__unsupported_encoder_quality[] = "#brotli: unsupported encoder quality";
|
||||
const char wuffs_brotli__error__internal_error_inconsistent_encoder_state[] = "#brotli: internal error: inconsistent encoder state";
|
||||
const char wuffs_brotli__error__unsupported_serialized_encoder_dictionary[] = "#brotli: unsupported serialized encoder dictionary";
|
||||
|
||||
// ---------------- Private Consts
|
||||
|
||||
@@ -76648,6 +76691,10 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__F64_3 4613937818241073152u
|
||||
|
||||
#define WUFFS_BROTLI__ENCODER_DICTIONARY_CHUNKS_MAX 15u
|
||||
|
||||
#define WUFFS_BROTLI__PREPARED_DICTIONARY_HASH_MUL_64_LONG 2297779722762296275u
|
||||
|
||||
#define WUFFS_BROTLI__TREE_NONE 65535u
|
||||
|
||||
#define WUFFS_BROTLI__FRAGMENT_MAX_DISTANCE 262128u
|
||||
@@ -77254,6 +77301,7 @@ static wuffs_base__status
|
||||
wuffs_brotli__encoder__create_backward_references(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_num_bytes,
|
||||
uint64_t a_position);
|
||||
|
||||
@@ -77825,6 +77873,15 @@ wuffs_brotli__encoder__write_metadata_header(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint32_t a_block_size);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__encoder__do_transform_io(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_dwb);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__metablock_builder__histogram_pair_is_less(
|
||||
@@ -78058,6 +78115,7 @@ static uint64_t
|
||||
wuffs_brotli__encoder__extend_last_command(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_bytes,
|
||||
uint64_t a_wrapped_last_processed_pos);
|
||||
|
||||
@@ -78127,9 +78185,68 @@ static wuffs_base__status
|
||||
wuffs_brotli__encoder__encode_data(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
bool a_is_last,
|
||||
bool a_force_flush);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__prepare_dictionary(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_src_off,
|
||||
uint64_t a_n,
|
||||
uint32_t a_bucket_bits,
|
||||
uint32_t a_slot_bits);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__encoder__compound_dictionary_enabled(
|
||||
const wuffs_brotli__encoder* self);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__lookup_compound_dictionary_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_ring_buffer_distance,
|
||||
uint64_t a_max_distance);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__find_compound_dictionary_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint32_t a_d,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_distance_offset,
|
||||
uint64_t a_max_distance);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__find_match_length_2(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_a,
|
||||
wuffs_base__slice_u8 a_b,
|
||||
uint64_t a_limit);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__extend_last_command_compound(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_bytes,
|
||||
uint64_t a_wrapped_last_processed_pos,
|
||||
uint64_t a_cmd_dist,
|
||||
uint64_t a_max_distance,
|
||||
uint64_t a_last_copy_len);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__encoder__set_depth(
|
||||
@@ -84538,6 +84655,7 @@ static wuffs_base__status
|
||||
wuffs_brotli__encoder__create_backward_references(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_num_bytes,
|
||||
uint64_t a_position) {
|
||||
uint64_t v_position = 0;
|
||||
@@ -84562,6 +84680,9 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
uint64_t v_range_end = 0;
|
||||
uint64_t v_k_margin = 0;
|
||||
uint64_t v_pos_jump = 0;
|
||||
uint64_t v_gap = 0;
|
||||
bool v_use_compound = false;
|
||||
wuffs_base__slice_u8 v_data = {0};
|
||||
|
||||
v_position = a_position;
|
||||
v_max_backward_limit = ((uint64_t)((((uint64_t)(1u)) << self->private_impl.f_lgwin) - 16u));
|
||||
@@ -84578,6 +84699,11 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
v_random_heuristics_window_size = 512u;
|
||||
}
|
||||
v_apply_random_heuristics = ((uint64_t)(v_position + v_random_heuristics_window_size));
|
||||
v_gap = self->private_impl.f_dict_total_size;
|
||||
v_use_compound = wuffs_brotli__encoder__compound_dictionary_enabled(self);
|
||||
if (self->private_impl.f_rb_buf_off <= ((uint64_t)(a_wb.len))) {
|
||||
v_data = wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_rb_buf_off);
|
||||
}
|
||||
wuffs_brotli__encoder__prepare_distance_cache(self);
|
||||
while (((uint64_t)(v_position + v_htl)) < v_pos_end) {
|
||||
v_max_length = ((uint64_t)(v_pos_end - v_position));
|
||||
@@ -84592,8 +84718,17 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
v_position,
|
||||
v_max_length,
|
||||
v_max_distance,
|
||||
v_dictionary_start,
|
||||
((uint64_t)(v_dictionary_start + v_gap)),
|
||||
self->private_impl.f_dist_max_distance);
|
||||
if (v_use_compound) {
|
||||
wuffs_brotli__encoder__lookup_compound_dictionary_match(self,
|
||||
v_data,
|
||||
a_dwb,
|
||||
v_position,
|
||||
v_max_length,
|
||||
v_dictionary_start,
|
||||
self->private_impl.f_dist_max_distance);
|
||||
}
|
||||
if (self->private_impl.f_sr_score > 2020u) {
|
||||
v_delayed_backward_references = 0u;
|
||||
v_max_length -= 1u;
|
||||
@@ -84616,8 +84751,17 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
((uint64_t)(v_position + 1u)),
|
||||
v_max_length,
|
||||
v_max_distance,
|
||||
v_dictionary_start,
|
||||
((uint64_t)(v_dictionary_start + v_gap)),
|
||||
self->private_impl.f_dist_max_distance);
|
||||
if (v_use_compound) {
|
||||
wuffs_brotli__encoder__lookup_compound_dictionary_match(self,
|
||||
v_data,
|
||||
a_dwb,
|
||||
((uint64_t)(v_position + 1u)),
|
||||
v_max_length,
|
||||
v_dictionary_start,
|
||||
self->private_impl.f_dist_max_distance);
|
||||
}
|
||||
if (self->private_impl.f_sr_score >= ((uint64_t)(v_sr_score + 175u))) {
|
||||
v_position += 1u;
|
||||
v_insert_length += 1u;
|
||||
@@ -84636,8 +84780,8 @@ wuffs_brotli__encoder__create_backward_references(
|
||||
}
|
||||
v_apply_random_heuristics = ((uint64_t)(((uint64_t)(v_position + ((uint64_t)(2u * self->private_impl.f_sr_len)))) + v_random_heuristics_window_size));
|
||||
v_dictionary_start = wuffs_brotli__encoder__min64(self, ((uint64_t)(v_position + v_position_offset)), v_max_backward_limit);
|
||||
v_distance_code = wuffs_brotli__encoder__compute_distance_code(self, self->private_impl.f_sr_distance, v_dictionary_start);
|
||||
if ((self->private_impl.f_sr_distance <= v_dictionary_start) && (v_distance_code > 0u)) {
|
||||
v_distance_code = wuffs_brotli__encoder__compute_distance_code(self, self->private_impl.f_sr_distance, ((uint64_t)(v_dictionary_start + v_gap)));
|
||||
if ((self->private_impl.f_sr_distance <= ((uint64_t)(v_dictionary_start + v_gap))) && (v_distance_code > 0u)) {
|
||||
self->private_impl.f_dist_cache[3u] = self->private_impl.f_dist_cache[2u];
|
||||
self->private_impl.f_dist_cache[2u] = self->private_impl.f_dist_cache[1u];
|
||||
self->private_impl.f_dist_cache[1u] = self->private_impl.f_dist_cache[0u];
|
||||
@@ -87670,6 +87814,7 @@ wuffs_brotli__encoder__workbuf_len(
|
||||
uint32_t v_q = 0;
|
||||
uint32_t v_lgwin = 0;
|
||||
uint64_t v_n = 0;
|
||||
wuffs_base__range_ii_u64 v_r = {0};
|
||||
|
||||
v_q = wuffs_brotli__encoder__sanitized_quality(self);
|
||||
v_lgwin = wuffs_brotli__encoder__sanitized_lgwin(self, v_q);
|
||||
@@ -87682,8 +87827,10 @@ wuffs_brotli__encoder__workbuf_len(
|
||||
wuffs_brotli__encoder__computed_lgblock(self, v_q, v_lgwin),
|
||||
23u);
|
||||
} else {
|
||||
return wuffs_brotli__encoder__workbuf_len_hq(self, v_q, v_lgwin);
|
||||
v_r = wuffs_brotli__encoder__workbuf_len_hq(self, v_q, v_lgwin);
|
||||
return wuffs_base__utility__make_range_ii_u64(wuffs_base__u64__sat_add(wuffs_private_impl__range_ii_u64__get_min_incl(&v_r), self->private_impl.f_dict_end), wuffs_base__u64__sat_add(wuffs_private_impl__range_ii_u64__get_max_incl(&v_r), self->private_impl.f_dict_end));
|
||||
}
|
||||
wuffs_private_impl__u64__sat_add_indirect(&v_n, self->private_impl.f_dict_end);
|
||||
return wuffs_base__utility__make_range_ii_u64(v_n, v_n);
|
||||
}
|
||||
|
||||
@@ -88147,6 +88294,72 @@ wuffs_brotli__encoder__transform_io(
|
||||
self->private_impl.active_coroutine = 0;
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
wuffs_base__status v_status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_transform_io;
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
while (true) {
|
||||
if (self->private_impl.f_dict_end > ((uint64_t)(a_workbuf.len))) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
goto exit;
|
||||
}
|
||||
{
|
||||
wuffs_base__status t_0 = wuffs_brotli__encoder__do_transform_io(self,
|
||||
a_dst,
|
||||
a_src,
|
||||
wuffs_base__slice_u8__subslice_i(a_workbuf, self->private_impl.f_dict_end),
|
||||
wuffs_base__slice_u8__subslice_j(a_workbuf, self->private_impl.f_dict_end));
|
||||
v_status = t_0;
|
||||
}
|
||||
if (wuffs_base__status__is_ok(&v_status)) {
|
||||
status = wuffs_base__make_status(NULL);
|
||||
goto ok;
|
||||
} else if ( ! wuffs_base__status__is_suspension(&v_status)) {
|
||||
status = v_status;
|
||||
if (wuffs_base__status__is_error(&status)) {
|
||||
goto exit;
|
||||
} else if (wuffs_base__status__is_suspension(&status)) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);
|
||||
goto exit;
|
||||
}
|
||||
goto ok;
|
||||
}
|
||||
status = v_status;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1);
|
||||
}
|
||||
|
||||
ok:
|
||||
self->private_impl.p_transform_io = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_transform_io = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
if (wuffs_base__status__is_error(&status)) {
|
||||
self->private_impl.magic = WUFFS_BASE__DISABLED;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.do_transform_io
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__status
|
||||
wuffs_brotli__encoder__do_transform_io(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__io_buffer* a_dst,
|
||||
wuffs_base__io_buffer* a_src,
|
||||
wuffs_base__slice_u8 a_workbuf,
|
||||
wuffs_base__slice_u8 a_dwb) {
|
||||
wuffs_base__status status = wuffs_base__make_status(NULL);
|
||||
|
||||
uint32_t v_op = 0;
|
||||
uint64_t v_avail = 0;
|
||||
uint64_t v_block_size = 0;
|
||||
@@ -88187,20 +88400,24 @@ wuffs_brotli__encoder__transform_io(
|
||||
io2_a_src = io0_a_src + a_src->meta.wi;
|
||||
}
|
||||
|
||||
uint32_t coro_susp_point = self->private_impl.p_transform_io;
|
||||
uint32_t coro_susp_point = self->private_impl.p_do_transform_io;
|
||||
if (coro_susp_point) {
|
||||
v_op = self->private_data.s_transform_io.v_op;
|
||||
v_is_last = self->private_data.s_transform_io.v_is_last;
|
||||
v_force_flush = self->private_data.s_transform_io.v_force_flush;
|
||||
v_storage_len = self->private_data.s_transform_io.v_storage_len;
|
||||
v_table_bits = self->private_data.s_transform_io.v_table_bits;
|
||||
v_mark = self->private_data.s_transform_io.v_mark;
|
||||
v_copy_input_size = self->private_data.s_transform_io.v_copy_input_size;
|
||||
v_op = self->private_data.s_do_transform_io.v_op;
|
||||
v_is_last = self->private_data.s_do_transform_io.v_is_last;
|
||||
v_force_flush = self->private_data.s_do_transform_io.v_force_flush;
|
||||
v_storage_len = self->private_data.s_do_transform_io.v_storage_len;
|
||||
v_table_bits = self->private_data.s_do_transform_io.v_table_bits;
|
||||
v_mark = self->private_data.s_do_transform_io.v_mark;
|
||||
v_copy_input_size = self->private_data.s_do_transform_io.v_copy_input_size;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
wuffs_brotli__encoder__ensure_initialized(self);
|
||||
if ((self->private_impl.f_quality >= 10u) && (self->private_impl.f_dict_num_chunks > 0u)) {
|
||||
status = wuffs_base__make_status(wuffs_brotli__error__unsupported_encoder_quality);
|
||||
goto exit;
|
||||
}
|
||||
wuffs_brotli__encoder__hq_extend_var_region(self, ((uint64_t)(a_workbuf.len)));
|
||||
if ((self->private_impl.f_quality >= 2u) && (((uint64_t)(a_workbuf.len)) < self->private_impl.f_workbuf_end)) {
|
||||
self->private_impl.f_rb_short_workbuf = true;
|
||||
@@ -88239,7 +88456,11 @@ wuffs_brotli__encoder__transform_io(
|
||||
continue;
|
||||
}
|
||||
if ((self->private_impl.f_quality >= 2u) && (self->private_impl.f_input_pos != self->private_impl.f_last_flush_pos)) {
|
||||
v_status = wuffs_brotli__encoder__encode_data(self, a_workbuf, false, true);
|
||||
v_status = wuffs_brotli__encoder__encode_data(self,
|
||||
a_workbuf,
|
||||
a_dwb,
|
||||
false,
|
||||
true);
|
||||
if ( ! wuffs_base__status__is_ok(&v_status)) {
|
||||
status = v_status;
|
||||
if (wuffs_base__status__is_error(&status)) {
|
||||
@@ -88313,15 +88534,15 @@ wuffs_brotli__encoder__transform_io(
|
||||
}
|
||||
}
|
||||
v_mark = ((uint64_t)(iop_a_src - io0_a_src));
|
||||
self->private_data.s_transform_io.scratch = v_copy_input_size;
|
||||
self->private_data.s_do_transform_io.scratch = v_copy_input_size;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
if (self->private_data.s_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
if (self->private_data.s_do_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_do_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
iop_a_src = io2_a_src;
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
}
|
||||
iop_a_src += self->private_data.s_transform_io.scratch;
|
||||
iop_a_src += self->private_data.s_do_transform_io.scratch;
|
||||
wuffs_brotli__encoder__copy_input_to_ring_buffer(self, a_workbuf, wuffs_private_impl__io__since(v_mark, ((uint64_t)(iop_a_src - io0_a_src)), io0_a_src));
|
||||
if ((self->private_impl.f_flint > 0u) && (self->private_impl.f_flint < 2147483648u)) {
|
||||
self->private_impl.f_flint = ((uint32_t)(self->private_impl.f_flint - ((uint32_t)((v_copy_input_size & 255u)))));
|
||||
@@ -88360,7 +88581,11 @@ wuffs_brotli__encoder__transform_io(
|
||||
v_force_flush = true;
|
||||
}
|
||||
wuffs_brotli__encoder__update_size_hint(self, ((uint64_t)(io2_a_src - iop_a_src)));
|
||||
v_status = wuffs_brotli__encoder__encode_data(self, a_workbuf, v_is_last, v_force_flush);
|
||||
v_status = wuffs_brotli__encoder__encode_data(self,
|
||||
a_workbuf,
|
||||
a_dwb,
|
||||
v_is_last,
|
||||
v_force_flush);
|
||||
if ( ! wuffs_base__status__is_ok(&v_status)) {
|
||||
status = v_status;
|
||||
if (wuffs_base__status__is_error(&status)) {
|
||||
@@ -88416,15 +88641,15 @@ wuffs_brotli__encoder__transform_io(
|
||||
self->private_impl.f_bw_ix = ((uint64_t)(self->private_impl.f_last_bytes_bits));
|
||||
v_table_bits = wuffs_brotli__encoder__get_hash_table(self, v_block_size);
|
||||
v_mark = ((uint64_t)(iop_a_src - io0_a_src));
|
||||
self->private_data.s_transform_io.scratch = v_block_size;
|
||||
self->private_data.s_do_transform_io.scratch = v_block_size;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5);
|
||||
if (self->private_data.s_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
if (self->private_data.s_do_transform_io.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
|
||||
self->private_data.s_do_transform_io.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||
iop_a_src = io2_a_src;
|
||||
status = wuffs_base__make_status(wuffs_base__suspension__short_read);
|
||||
goto suspend;
|
||||
}
|
||||
iop_a_src += self->private_data.s_transform_io.scratch;
|
||||
iop_a_src += self->private_data.s_do_transform_io.scratch;
|
||||
if (v_storage_len > ((uint64_t)(a_workbuf.len))) {
|
||||
status = wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_encoder_state);
|
||||
goto exit;
|
||||
@@ -88482,21 +88707,20 @@ wuffs_brotli__encoder__transform_io(
|
||||
}
|
||||
|
||||
ok:
|
||||
self->private_impl.p_transform_io = 0;
|
||||
self->private_impl.p_do_transform_io = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
goto suspend;
|
||||
suspend:
|
||||
self->private_impl.p_transform_io = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0;
|
||||
self->private_data.s_transform_io.v_op = v_op;
|
||||
self->private_data.s_transform_io.v_is_last = v_is_last;
|
||||
self->private_data.s_transform_io.v_force_flush = v_force_flush;
|
||||
self->private_data.s_transform_io.v_storage_len = v_storage_len;
|
||||
self->private_data.s_transform_io.v_table_bits = v_table_bits;
|
||||
self->private_data.s_transform_io.v_mark = v_mark;
|
||||
self->private_data.s_transform_io.v_copy_input_size = v_copy_input_size;
|
||||
self->private_impl.p_do_transform_io = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;
|
||||
self->private_data.s_do_transform_io.v_op = v_op;
|
||||
self->private_data.s_do_transform_io.v_is_last = v_is_last;
|
||||
self->private_data.s_do_transform_io.v_force_flush = v_force_flush;
|
||||
self->private_data.s_do_transform_io.v_storage_len = v_storage_len;
|
||||
self->private_data.s_do_transform_io.v_table_bits = v_table_bits;
|
||||
self->private_data.s_do_transform_io.v_mark = v_mark;
|
||||
self->private_data.s_do_transform_io.v_copy_input_size = v_copy_input_size;
|
||||
|
||||
goto exit;
|
||||
exit:
|
||||
@@ -88507,9 +88731,6 @@ wuffs_brotli__encoder__transform_io(
|
||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
||||
}
|
||||
|
||||
if (wuffs_base__status__is_error(&status)) {
|
||||
self->private_impl.magic = WUFFS_BASE__DISABLED;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -89697,6 +89918,7 @@ static uint64_t
|
||||
wuffs_brotli__encoder__extend_last_command(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_bytes,
|
||||
uint64_t a_wrapped_last_processed_pos) {
|
||||
uint64_t v_last = 0;
|
||||
@@ -89726,6 +89948,16 @@ wuffs_brotli__encoder__extend_last_command(
|
||||
v_n += 1u;
|
||||
v_p = (((uint64_t)(v_p + 1u)) & 4294967295u);
|
||||
}
|
||||
} else {
|
||||
v_n = wuffs_brotli__encoder__extend_last_command_compound(self,
|
||||
a_wb,
|
||||
a_dwb,
|
||||
a_bytes,
|
||||
a_wrapped_last_processed_pos,
|
||||
v_cmd_dist,
|
||||
v_max_distance,
|
||||
v_last_copy_len);
|
||||
v_copy_len += ((uint32_t)(v_n));
|
||||
}
|
||||
wuffs_brotli__encoder__set_cmd(self,
|
||||
a_wb,
|
||||
@@ -90132,6 +90364,7 @@ static wuffs_base__status
|
||||
wuffs_brotli__encoder__encode_data(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
bool a_is_last,
|
||||
bool a_force_flush) {
|
||||
wuffs_base__status v_status = wuffs_base__make_status(NULL);
|
||||
@@ -90189,7 +90422,11 @@ wuffs_brotli__encoder__encode_data(
|
||||
self->private_impl.f_literal_context_mode = wuffs_brotli__encoder__choose_context_mode(self, a_wb, wuffs_brotli__encoder__wrap_position(self, self->private_impl.f_last_flush_pos), ((uint64_t)(self->private_impl.f_input_pos - self->private_impl.f_last_flush_pos)));
|
||||
}
|
||||
if ((self->private_impl.f_num_cmds > 0u) && (self->private_impl.f_last_insert_len == 0u)) {
|
||||
v_n = wuffs_brotli__encoder__extend_last_command(self, a_wb, v_bytes, v_wrapped_last_processed_pos);
|
||||
v_n = wuffs_brotli__encoder__extend_last_command(self,
|
||||
a_wb,
|
||||
a_dwb,
|
||||
v_bytes,
|
||||
v_wrapped_last_processed_pos);
|
||||
v_bytes -= v_n;
|
||||
v_wrapped_last_processed_pos = (((uint64_t)(v_wrapped_last_processed_pos + v_n)) & 4294967295u);
|
||||
}
|
||||
@@ -90198,7 +90435,11 @@ wuffs_brotli__encoder__encode_data(
|
||||
} else if (self->private_impl.f_quality == 11u) {
|
||||
v_status = wuffs_brotli__encoder__create_hq_zopfli_backward_references(self, a_wb, v_bytes, v_wrapped_last_processed_pos);
|
||||
} else {
|
||||
v_status = wuffs_brotli__encoder__create_backward_references(self, a_wb, v_bytes, v_wrapped_last_processed_pos);
|
||||
v_status = wuffs_brotli__encoder__create_backward_references(self,
|
||||
a_wb,
|
||||
a_dwb,
|
||||
v_bytes,
|
||||
v_wrapped_last_processed_pos);
|
||||
}
|
||||
if ( ! wuffs_base__status__is_ok(&v_status)) {
|
||||
return wuffs_private_impl__status__ensure_not_a_suspension(v_status);
|
||||
@@ -90280,6 +90521,562 @@ wuffs_brotli__encoder__encode_data(
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.add_dictionary
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_brotli__encoder__add_dictionary(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint32_t a_dict_type,
|
||||
wuffs_base__slice_u8 a_dict,
|
||||
wuffs_base__slice_u8 a_workbuf) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_status(
|
||||
(self->private_impl.magic == WUFFS_BASE__DISABLED)
|
||||
? wuffs_base__error__disabled_by_previous_error
|
||||
: wuffs_base__error__initialize_not_called);
|
||||
}
|
||||
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_lo = 0;
|
||||
uint64_t v_hi = 0;
|
||||
uint32_t v_bucket_bits = 0;
|
||||
uint32_t v_slot_bits = 0;
|
||||
uint64_t v_num_items = 0;
|
||||
uint32_t v_c = 0;
|
||||
|
||||
if (self->private_impl.f_is_initialized) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_call_sequence);
|
||||
} else if (a_dict_type == 1u) {
|
||||
return wuffs_base__make_status(wuffs_brotli__error__unsupported_serialized_encoder_dictionary);
|
||||
} else if (a_dict_type != 0u) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_argument);
|
||||
} else if (self->private_impl.f_dict_num_chunks >= 15u) {
|
||||
return wuffs_base__make_status(wuffs_brotli__error__too_many_compound_dictionaries);
|
||||
}
|
||||
v_n = ((uint64_t)(a_dict.len));
|
||||
if ((v_n > 2147483647u) || (wuffs_base__u64__sat_add(self->private_impl.f_dict_total_size, v_n) > 2147483647u)) {
|
||||
return wuffs_base__make_status(wuffs_brotli__error__too_much_dictionary_data);
|
||||
} else if (wuffs_brotli__encoder__add_dictionary_workbuf_len(self, v_n) > ((uint64_t)(a_workbuf.len))) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
v_bucket_bits = 17u;
|
||||
while (((((uint64_t)(16u)) << v_bucket_bits) < v_n) && (v_bucket_bits < 22u)) {
|
||||
v_bucket_bits += 1u;
|
||||
}
|
||||
if (v_bucket_bits >= 10u) {
|
||||
v_slot_bits = (v_bucket_bits - 10u);
|
||||
}
|
||||
v_lo = self->private_impl.f_dict_end;
|
||||
v_hi = ((uint64_t)(v_lo + v_n));
|
||||
if ((v_lo > v_hi) || (v_hi > ((uint64_t)(a_workbuf.len)))) {
|
||||
return wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_encoder_state);
|
||||
}
|
||||
wuffs_private_impl__slice_u8__copy_from_slice(wuffs_base__slice_u8__subslice_ij(a_workbuf, v_lo, v_hi), a_dict);
|
||||
v_num_items = wuffs_brotli__encoder__prepare_dictionary(self,
|
||||
a_workbuf,
|
||||
v_lo,
|
||||
v_n,
|
||||
v_bucket_bits,
|
||||
v_slot_bits);
|
||||
v_c = self->private_impl.f_dict_num_chunks;
|
||||
if (v_c >= 15u) {
|
||||
return wuffs_base__make_status(wuffs_brotli__error__internal_error_inconsistent_encoder_state);
|
||||
}
|
||||
self->private_impl.f_dc_src_off[v_c] = v_lo;
|
||||
self->private_impl.f_dc_size[v_c] = v_n;
|
||||
self->private_impl.f_dc_bucket_bits[v_c] = v_bucket_bits;
|
||||
self->private_impl.f_dc_slot_bits[v_c] = v_slot_bits;
|
||||
self->private_impl.f_dc_num_items[v_c] = v_num_items;
|
||||
self->private_impl.f_dict_total_size += v_n;
|
||||
self->private_impl.f_dict_chunk_offsets[(v_c + 1u)] = self->private_impl.f_dict_total_size;
|
||||
self->private_impl.f_dict_num_chunks = (v_c + 1u);
|
||||
self->private_impl.f_dict_end = ((uint64_t)(((uint64_t)(((uint64_t)(v_hi + (((uint64_t)(4u)) << v_slot_bits))) + (((uint64_t)(2u)) << v_bucket_bits))) + ((uint64_t)(4u * v_num_items))));
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.add_dictionary_workbuf_len
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_dict_len) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t v_bucket_bits = 0;
|
||||
uint32_t v_slot_bits = 0;
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_max_items = 0;
|
||||
|
||||
if (a_dict_len > 2147483647u) {
|
||||
return 18446744073709551615u;
|
||||
}
|
||||
v_n = a_dict_len;
|
||||
v_bucket_bits = 17u;
|
||||
while (((((uint64_t)(16u)) << v_bucket_bits) < v_n) && (v_bucket_bits < 22u)) {
|
||||
v_bucket_bits += 1u;
|
||||
}
|
||||
if (v_bucket_bits >= 10u) {
|
||||
v_slot_bits = (v_bucket_bits - 10u);
|
||||
}
|
||||
if (v_n > 7u) {
|
||||
v_max_items = (v_n - 7u);
|
||||
}
|
||||
return wuffs_base__u64__sat_add(wuffs_base__u64__sat_add(wuffs_base__u64__sat_add(wuffs_base__u64__sat_add(wuffs_base__u64__sat_add(self->private_impl.f_dict_end, v_n), (((uint64_t)(4u)) << v_slot_bits)), (((uint64_t)(2u)) << v_bucket_bits)), ((uint64_t)(4u * v_max_items))), wuffs_base__u64__sat_add((((uint64_t)(6u)) << v_bucket_bits), ((uint64_t)(4u * v_n))));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.prepare_dictionary
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__prepare_dictionary(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_src_off,
|
||||
uint64_t a_n,
|
||||
uint32_t a_bucket_bits,
|
||||
uint32_t a_slot_bits) {
|
||||
uint64_t v_num_slots = 0;
|
||||
uint64_t v_num_buckets = 0;
|
||||
uint64_t v_slot_mask = 0;
|
||||
uint64_t v_slot_off = 0;
|
||||
uint64_t v_heads_off = 0;
|
||||
uint64_t v_items_off = 0;
|
||||
uint64_t v_num_off = 0;
|
||||
uint64_t v_bh_off = 0;
|
||||
uint64_t v_nb_off = 0;
|
||||
uint64_t v_max_items = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_j = 0;
|
||||
uint64_t v_h = 0;
|
||||
uint64_t v_key = 0;
|
||||
uint32_t v_count = 0;
|
||||
uint32_t v_limit = 0;
|
||||
uint32_t v_size = 0;
|
||||
bool v_overflow = false;
|
||||
uint64_t v_total_items = 0;
|
||||
uint64_t v_slot = 0;
|
||||
uint64_t v_cursor = 0;
|
||||
uint64_t v_pos = 0;
|
||||
uint32_t v_k = 0;
|
||||
|
||||
v_num_slots = (((uint64_t)(1u)) << a_slot_bits);
|
||||
v_num_buckets = (((uint64_t)(1u)) << a_bucket_bits);
|
||||
v_slot_mask = (v_num_slots - 1u);
|
||||
if (a_n > 7u) {
|
||||
v_max_items = (a_n - 7u);
|
||||
}
|
||||
v_slot_off = ((uint64_t)(a_src_off + a_n));
|
||||
v_heads_off = ((uint64_t)(v_slot_off + ((uint64_t)(4u * v_num_slots))));
|
||||
v_items_off = ((uint64_t)(v_heads_off + ((uint64_t)(2u * v_num_buckets))));
|
||||
v_num_off = ((uint64_t)(v_items_off + ((uint64_t)(4u * v_max_items))));
|
||||
v_bh_off = ((uint64_t)(v_num_off + ((uint64_t)(2u * v_num_buckets))));
|
||||
v_nb_off = ((uint64_t)(v_bh_off + ((uint64_t)(4u * v_num_buckets))));
|
||||
wuffs_brotli__encoder__fill_bytes(self,
|
||||
a_wb,
|
||||
v_num_off,
|
||||
((uint64_t)(2u * v_num_buckets)),
|
||||
0u);
|
||||
v_i = 0u;
|
||||
while (((uint64_t)(v_i + 7u)) < a_n) {
|
||||
v_h = ((uint64_t)((wuffs_brotli__encoder__load64le(self, a_wb, ((uint64_t)(a_src_off + v_i))) & 1099511627775u) * 2297779722762296275u));
|
||||
v_key = ((v_h >> 32u) >> (32u - a_bucket_bits));
|
||||
v_count = ((uint32_t)(wuffs_brotli__encoder__peek_u16(self, a_wb, ((uint64_t)(v_num_off + ((uint64_t)(2u * v_key)))))));
|
||||
if (v_count == 0u) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_nb_off + ((uint64_t)(4u * v_i)))), 4294967295u);
|
||||
} else {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_nb_off + ((uint64_t)(4u * v_i)))), wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bh_off + ((uint64_t)(4u * v_key))))));
|
||||
}
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_bh_off + ((uint64_t)(4u * v_key)))), ((uint32_t)(v_i)));
|
||||
v_count += 1u;
|
||||
if (v_count > 32u) {
|
||||
v_count = 32u;
|
||||
}
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, ((uint64_t)(v_num_off + ((uint64_t)(2u * v_key)))), ((uint16_t)(v_count)));
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_slots) {
|
||||
v_limit = 32u;
|
||||
while (true) {
|
||||
v_count = 0u;
|
||||
v_overflow = false;
|
||||
v_j = v_i;
|
||||
while (v_j < v_num_buckets) {
|
||||
v_size = ((uint32_t)(wuffs_brotli__encoder__peek_u16(self, a_wb, ((uint64_t)(v_num_off + ((uint64_t)(2u * v_j)))))));
|
||||
if (v_count >= 65535u) {
|
||||
v_overflow = true;
|
||||
break;
|
||||
}
|
||||
if (v_size > v_limit) {
|
||||
v_size = v_limit;
|
||||
}
|
||||
v_count += v_size;
|
||||
v_j += v_num_slots;
|
||||
}
|
||||
if ( ! v_overflow) {
|
||||
self->private_data.f_dp_slot_size[(v_i & 4095u)] = v_count;
|
||||
v_total_items += ((uint64_t)(v_count));
|
||||
break;
|
||||
} else if (v_limit == 0u) {
|
||||
self->private_data.f_dp_slot_size[(v_i & 4095u)] = 0u;
|
||||
break;
|
||||
}
|
||||
v_limit -= 1u;
|
||||
}
|
||||
self->private_data.f_dp_slot_limit[(v_i & 4095u)] = v_limit;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_total_items = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_slots) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_slot_off + ((uint64_t)(4u * v_i)))), ((uint32_t)(v_total_items)));
|
||||
self->private_data.f_dp_slot_off[(v_i & 4095u)] = ((uint32_t)(v_total_items));
|
||||
v_total_items += ((uint64_t)(self->private_data.f_dp_slot_size[(v_i & 4095u)]));
|
||||
self->private_data.f_dp_slot_size[(v_i & 4095u)] = 0u;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_num_buckets) {
|
||||
v_slot = (v_i & v_slot_mask & 4095u);
|
||||
v_count = ((uint32_t)(wuffs_brotli__encoder__peek_u16(self, a_wb, ((uint64_t)(v_num_off + ((uint64_t)(2u * v_i)))))));
|
||||
if (v_count > self->private_data.f_dp_slot_limit[v_slot]) {
|
||||
v_count = self->private_data.f_dp_slot_limit[v_slot];
|
||||
}
|
||||
if (v_count == 0u) {
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, ((uint64_t)(v_heads_off + ((uint64_t)(2u * v_i)))), 65535u);
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
v_cursor = ((uint64_t)(self->private_data.f_dp_slot_size[v_slot]));
|
||||
wuffs_brotli__encoder__poke_u16(self, a_wb, ((uint64_t)(v_heads_off + ((uint64_t)(2u * v_i)))), ((uint16_t)(v_cursor)));
|
||||
v_cursor += ((uint64_t)(self->private_data.f_dp_slot_off[v_slot]));
|
||||
self->private_data.f_dp_slot_size[v_slot] += v_count;
|
||||
v_pos = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_bh_off + ((uint64_t)(4u * v_i)))))));
|
||||
v_k = 0u;
|
||||
while (v_k < v_count) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_items_off + ((uint64_t)(4u * v_cursor)))), ((uint32_t)(v_pos)));
|
||||
v_cursor += 1u;
|
||||
v_pos = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_nb_off + ((uint64_t)(4u * v_pos)))))));
|
||||
v_k += 1u;
|
||||
}
|
||||
v_cursor -= 1u;
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(v_items_off + ((uint64_t)(4u * v_cursor)))), (wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(v_items_off + ((uint64_t)(4u * v_cursor))))) | 2147483648u));
|
||||
v_i += 1u;
|
||||
}
|
||||
return v_total_items;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.compound_dictionary_enabled
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static bool
|
||||
wuffs_brotli__encoder__compound_dictionary_enabled(
|
||||
const wuffs_brotli__encoder* self) {
|
||||
uint32_t v_htype = 0;
|
||||
|
||||
if (self->private_impl.f_dict_num_chunks == 0u) {
|
||||
return false;
|
||||
}
|
||||
v_htype = self->private_impl.f_hasher_type;
|
||||
return ((v_htype == 5u) ||
|
||||
(v_htype == 6u) ||
|
||||
(v_htype == 58u) ||
|
||||
(v_htype == 68u) ||
|
||||
(v_htype == 40u) ||
|
||||
(v_htype == 41u) ||
|
||||
(v_htype == 42u) ||
|
||||
((v_htype == 54u) && (self->private_impl.f_hr_jump != 0u)));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.lookup_compound_dictionary_match
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__lookup_compound_dictionary_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_ring_buffer_distance,
|
||||
uint64_t a_max_distance) {
|
||||
uint64_t v_base_offset = 0;
|
||||
uint32_t v_d = 0;
|
||||
|
||||
v_base_offset = ((uint64_t)(a_max_ring_buffer_distance + self->private_impl.f_dict_total_size));
|
||||
v_d = 0u;
|
||||
while (v_d < self->private_impl.f_dict_num_chunks) {
|
||||
wuffs_brotli__encoder__find_compound_dictionary_match(self,
|
||||
a_data,
|
||||
a_dwb,
|
||||
v_d,
|
||||
a_cur_ix,
|
||||
a_max_length,
|
||||
((uint64_t)(v_base_offset - self->private_impl.f_dict_chunk_offsets[v_d])),
|
||||
a_max_distance);
|
||||
v_d += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.find_compound_dictionary_match
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__find_compound_dictionary_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_data,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint32_t a_d,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_distance_offset,
|
||||
uint64_t a_max_distance) {
|
||||
uint64_t v_source_size = 0;
|
||||
uint64_t v_boundary = 0;
|
||||
uint64_t v_src_off = 0;
|
||||
uint64_t v_slot_off = 0;
|
||||
uint64_t v_heads_off = 0;
|
||||
uint64_t v_items_off = 0;
|
||||
wuffs_base__slice_u8 v_source = {0};
|
||||
wuffs_base__slice_u8 v_cur = {0};
|
||||
uint64_t v_cur_ix_masked = 0;
|
||||
uint64_t v_mask = 0;
|
||||
uint64_t v_best_score = 0;
|
||||
uint64_t v_best_len = 0;
|
||||
uint32_t v_i = 0;
|
||||
uint64_t v_h = 0;
|
||||
uint64_t v_key = 0;
|
||||
uint64_t v_slot = 0;
|
||||
uint64_t v_head = 0;
|
||||
uint64_t v_chain = 0;
|
||||
uint32_t v_item = 0;
|
||||
uint64_t v_distance = 0;
|
||||
uint64_t v_offset = 0;
|
||||
uint64_t v_limit = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
uint64_t v_num_items = 0;
|
||||
uint32_t v_bb = 0;
|
||||
uint32_t v_sb = 0;
|
||||
|
||||
v_bb = wuffs_base__u32__min(self->private_impl.f_dc_bucket_bits[a_d], 22u);
|
||||
v_sb = wuffs_base__u32__min(self->private_impl.f_dc_slot_bits[a_d], 12u);
|
||||
v_num_items = self->private_impl.f_dc_num_items[a_d];
|
||||
v_source_size = self->private_impl.f_dc_size[a_d];
|
||||
v_boundary = ((uint64_t)(a_distance_offset - v_source_size));
|
||||
v_src_off = self->private_impl.f_dc_src_off[a_d];
|
||||
v_slot_off = ((uint64_t)(v_src_off + v_source_size));
|
||||
v_heads_off = ((uint64_t)(v_slot_off + (((uint64_t)(4u)) << v_sb)));
|
||||
v_items_off = ((uint64_t)(v_heads_off + (((uint64_t)(2u)) << v_bb)));
|
||||
if ((v_src_off > v_slot_off) || (v_slot_off > ((uint64_t)(a_dwb.len)))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_source = wuffs_base__slice_u8__subslice_ij(a_dwb, v_src_off, v_slot_off);
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_cur_ix_masked = (a_cur_ix & v_mask);
|
||||
if (v_cur_ix_masked > ((uint64_t)(a_data.len))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_cur = wuffs_base__slice_u8__subslice_i(a_data, v_cur_ix_masked);
|
||||
v_best_score = self->private_impl.f_sr_score;
|
||||
v_best_len = self->private_impl.f_sr_len;
|
||||
v_h = ((uint64_t)((wuffs_brotli__encoder__load64le(self, a_data, v_cur_ix_masked) & 1099511627775u) * 2297779722762296275u));
|
||||
v_key = ((v_h >> 32u) >> (32u - v_bb));
|
||||
v_slot = (v_key & ((((uint64_t)(1u)) << v_sb) - 1u));
|
||||
v_head = ((uint64_t)(wuffs_brotli__encoder__peek_u16(self, a_dwb, ((uint64_t)(v_heads_off + ((uint64_t)(2u * v_key)))))));
|
||||
v_chain = ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__load32le(self, a_dwb, ((uint64_t)(v_slot_off + ((uint64_t)(4u * v_slot))))))) + v_head));
|
||||
v_item = 0u;
|
||||
if (v_head == 65535u) {
|
||||
v_item = 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < 4u) {
|
||||
v_distance = wuffs_brotli__encoder__dist_cache_u64(self, v_i);
|
||||
if ((v_distance <= v_boundary) || (v_distance > a_distance_offset)) {
|
||||
v_i += 1u;
|
||||
continue;
|
||||
}
|
||||
v_offset = ((uint64_t)(a_distance_offset - v_distance));
|
||||
v_limit = ((uint64_t)(v_source_size - v_offset));
|
||||
v_limit = wuffs_base__u64__min(v_limit, a_max_length);
|
||||
v_len = 0u;
|
||||
if (v_offset <= ((uint64_t)(v_source.len))) {
|
||||
v_len = wuffs_brotli__encoder__find_match_length_2(self, wuffs_base__slice_u8__subslice_i(v_source, v_offset), v_cur, v_limit);
|
||||
}
|
||||
if (v_len >= 2u) {
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score_using_last_distance(self, v_len);
|
||||
if (v_best_score < v_score) {
|
||||
if (v_i != 0u) {
|
||||
v_score -= wuffs_brotli__encoder__backward_reference_penalty_using_last_distance(self, ((uint64_t)(v_i)));
|
||||
}
|
||||
if (v_best_score < v_score) {
|
||||
v_best_score = v_score;
|
||||
if (v_len > v_best_len) {
|
||||
v_best_len = v_len;
|
||||
}
|
||||
self->private_impl.f_sr_len = v_len;
|
||||
self->private_impl.f_sr_len_code_delta = 0u;
|
||||
self->private_impl.f_sr_distance = v_distance;
|
||||
self->private_impl.f_sr_score = v_best_score;
|
||||
}
|
||||
}
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_best_len < 3u) {
|
||||
v_best_len = 3u;
|
||||
}
|
||||
while (v_item == 0u) {
|
||||
if (v_chain >= v_num_items) {
|
||||
break;
|
||||
}
|
||||
v_item = wuffs_brotli__encoder__load32le(self, a_dwb, ((uint64_t)(v_items_off + ((uint64_t)(4u * v_chain)))));
|
||||
v_chain += 1u;
|
||||
v_offset = ((uint64_t)((v_item & 2147483647u)));
|
||||
v_item &= 2147483648u;
|
||||
v_distance = ((uint64_t)(a_distance_offset - v_offset));
|
||||
v_limit = ((uint64_t)(v_source_size - v_offset));
|
||||
v_limit = wuffs_base__u64__min(v_limit, a_max_length);
|
||||
if (v_distance > a_max_distance) {
|
||||
continue;
|
||||
}
|
||||
if ((((uint64_t)(v_cur_ix_masked + v_best_len)) > v_mask) || (v_best_len >= v_limit) || (wuffs_brotli__encoder__load32le(self, a_data, ((uint64_t)(((uint64_t)(v_cur_ix_masked + v_best_len)) - 3u))) != wuffs_brotli__encoder__load32le(self, v_source, ((uint64_t)(((uint64_t)(v_offset + v_best_len)) - 3u))))) {
|
||||
continue;
|
||||
}
|
||||
v_len = 0u;
|
||||
if (v_offset <= ((uint64_t)(v_source.len))) {
|
||||
v_len = wuffs_brotli__encoder__find_match_length_2(self, wuffs_base__slice_u8__subslice_i(v_source, v_offset), v_cur, v_limit);
|
||||
}
|
||||
if (v_len >= 4u) {
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_distance);
|
||||
if (v_best_score < v_score) {
|
||||
v_best_score = v_score;
|
||||
v_best_len = v_len;
|
||||
self->private_impl.f_sr_len = v_best_len;
|
||||
self->private_impl.f_sr_len_code_delta = 0u;
|
||||
self->private_impl.f_sr_distance = v_distance;
|
||||
self->private_impl.f_sr_score = v_best_score;
|
||||
}
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.find_match_length_2
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__find_match_length_2(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_a,
|
||||
wuffs_base__slice_u8 a_b,
|
||||
uint64_t a_limit) {
|
||||
uint64_t v_matched = 0;
|
||||
uint64_t v_n = 0;
|
||||
wuffs_base__slice_u8 v_a = {0};
|
||||
wuffs_base__slice_u8 v_b = {0};
|
||||
uint64_t v_x = 0;
|
||||
|
||||
v_a = a_a;
|
||||
v_b = a_b;
|
||||
v_n = a_limit;
|
||||
while ((v_n >= 8u) && (((uint64_t)(v_a.len)) >= 8u) && (((uint64_t)(v_b.len)) >= 8u)) {
|
||||
v_x = (wuffs_base__peek_u64le__no_bounds_check(v_a.ptr) ^ wuffs_base__peek_u64le__no_bounds_check(v_b.ptr));
|
||||
if (v_x != 0u) {
|
||||
while ((v_x & 255u) == 0u) {
|
||||
v_matched += 1u;
|
||||
v_x >>= 8u;
|
||||
}
|
||||
return v_matched;
|
||||
}
|
||||
v_matched += 8u;
|
||||
v_n -= 8u;
|
||||
v_a = wuffs_base__slice_u8__subslice_i(v_a, 8u);
|
||||
v_b = wuffs_base__slice_u8__subslice_i(v_b, 8u);
|
||||
}
|
||||
while ((v_n > 0u) && (((uint64_t)(v_a.len)) > 0u) && (((uint64_t)(v_b.len)) > 0u)) {
|
||||
if (v_a.ptr[0u] != v_b.ptr[0u]) {
|
||||
break;
|
||||
}
|
||||
v_matched += 1u;
|
||||
v_n -= 1u;
|
||||
v_a = wuffs_base__slice_u8__subslice_i(v_a, 1u);
|
||||
v_b = wuffs_base__slice_u8__subslice_i(v_b, 1u);
|
||||
}
|
||||
return v_matched;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.extend_last_command_compound
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__extend_last_command_compound(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
wuffs_base__slice_u8 a_dwb,
|
||||
uint64_t a_bytes,
|
||||
uint64_t a_wrapped_last_processed_pos,
|
||||
uint64_t a_cmd_dist,
|
||||
uint64_t a_max_distance,
|
||||
uint64_t a_last_copy_len) {
|
||||
uint64_t v_address = 0;
|
||||
uint32_t v_br_index = 0;
|
||||
uint64_t v_br_offset = 0;
|
||||
uint64_t v_chunk_length = 0;
|
||||
uint64_t v_n = 0;
|
||||
uint64_t v_p = 0;
|
||||
|
||||
if (((uint64_t)(((uint64_t)(a_cmd_dist - a_max_distance)) - 1u)) >= self->private_impl.f_dict_total_size) {
|
||||
return 0u;
|
||||
} else if (a_last_copy_len >= ((uint64_t)(a_cmd_dist - a_max_distance))) {
|
||||
return 0u;
|
||||
}
|
||||
v_address = ((uint64_t)(((uint64_t)(self->private_impl.f_dict_total_size - ((uint64_t)(a_cmd_dist - a_max_distance)))) + a_last_copy_len));
|
||||
v_br_index = 0u;
|
||||
while ((v_br_index < 15u) && (v_address >= self->private_impl.f_dict_chunk_offsets[(v_br_index + 1u)])) {
|
||||
v_br_index += 1u;
|
||||
}
|
||||
if (v_br_index >= 15u) {
|
||||
return 0u;
|
||||
}
|
||||
v_br_offset = ((uint64_t)(v_address - self->private_impl.f_dict_chunk_offsets[v_br_index]));
|
||||
v_chunk_length = ((uint64_t)(self->private_impl.f_dict_chunk_offsets[(v_br_index + 1u)] - self->private_impl.f_dict_chunk_offsets[v_br_index]));
|
||||
v_p = a_wrapped_last_processed_pos;
|
||||
while (v_n < a_bytes) {
|
||||
if ((v_br_offset >= v_chunk_length) || (v_br_index >= 15u)) {
|
||||
break;
|
||||
}
|
||||
if (wuffs_brotli__encoder__rb_byte(self, a_wb, v_p) != wuffs_brotli__encoder__byte_at(self, a_dwb, ((uint64_t)(self->private_impl.f_dc_src_off[v_br_index] + v_br_offset)))) {
|
||||
break;
|
||||
}
|
||||
v_n += 1u;
|
||||
v_p = (((uint64_t)(v_p + 1u)) & 4294967295u);
|
||||
v_br_offset += 1u;
|
||||
if (v_br_offset == v_chunk_length) {
|
||||
v_br_index += 1u;
|
||||
v_br_offset = 0u;
|
||||
if ((v_br_index < self->private_impl.f_dict_num_chunks) && (v_br_index < 15u)) {
|
||||
v_chunk_length = ((uint64_t)(self->private_impl.f_dict_chunk_offsets[(v_br_index + 1u)] - self->private_impl.f_dict_chunk_offsets[v_br_index]));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return v_n;
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.set_depth
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "brotli/decode.h"
|
||||
#include "brotli/encode.h"
|
||||
|
||||
#define WUFFS_IMPLEMENTATION
|
||||
@@ -113,6 +114,26 @@ static uint32_t g_qmin = 0;
|
||||
static uint32_t g_qmax = 9;
|
||||
static uint32_t g_oneshot_lgwin = 0;
|
||||
static bool g_lw_only = false;
|
||||
static bool g_dict_only = false;
|
||||
|
||||
// The attached raw dictionaries (see set_dicts), for both encoders: the C
|
||||
// code's prepared dictionaries and a Wuffs encoder (g_dict_template) that has
|
||||
// them added, with the dictionaries in g_workbuf[.. g_dict_end]. For a small
|
||||
// total dictionary size, encode_wuffs adds them to a fresh encoder instead.
|
||||
#define MAX_DICTS 16
|
||||
static const uint8_t* g_dict_ptrs[MAX_DICTS];
|
||||
static size_t g_dict_lens[MAX_DICTS];
|
||||
static size_t g_num_dicts;
|
||||
static size_t g_dict_total;
|
||||
static BrotliEncoderPreparedDictionary* g_dict_prepared[MAX_DICTS];
|
||||
static wuffs_brotli__encoder* g_dict_template;
|
||||
static uint64_t g_dict_end;
|
||||
static uint64_t g_num_roundtrips;
|
||||
static uint64_t g_last_wlen; // encode_wuffs' workbuf_len.
|
||||
static char g_dict_desc[160];
|
||||
static uint8_t* g_foreign_dict; // test/data/enwik5.
|
||||
static size_t g_foreign_dict_len;
|
||||
static uint64_t g_num_smaller_checks;
|
||||
|
||||
typedef struct {
|
||||
uint32_t keys[MAX_PARAMS];
|
||||
@@ -348,6 +369,74 @@ brotli_enc_quirk_key(uint32_t param) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// grow_workbuf makes g_workbuf at least n bytes long, keeping its contents
|
||||
// (and so any dictionaries at its start).
|
||||
static bool //
|
||||
grow_workbuf(uint64_t n) {
|
||||
if (n <= g_workbuf_len) {
|
||||
return true;
|
||||
} else if (n > ((uint64_t)3 << 30)) {
|
||||
return false;
|
||||
}
|
||||
uint8_t* p = realloc(g_workbuf, (size_t)n);
|
||||
if (!p) {
|
||||
return false;
|
||||
}
|
||||
g_workbuf = p;
|
||||
g_workbuf_len = (size_t)n;
|
||||
return true;
|
||||
}
|
||||
|
||||
// set_dicts sets the attached dictionaries for the following checks (none if
|
||||
// n is 0), preparing them for both encoders.
|
||||
static const char* //
|
||||
set_dicts(size_t n, const uint8_t* const* ptrs, const size_t* lens) {
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
BrotliEncoderDestroyPreparedDictionary(g_dict_prepared[j]);
|
||||
g_dict_prepared[j] = NULL;
|
||||
}
|
||||
free(g_dict_template);
|
||||
g_dict_template = NULL;
|
||||
g_num_dicts = 0;
|
||||
g_dict_total = 0;
|
||||
g_dict_end = 0;
|
||||
for (size_t j = 0; (j < n) && (j < MAX_DICTS); j++) {
|
||||
g_dict_ptrs[j] = ptrs[j];
|
||||
g_dict_lens[j] = lens[j];
|
||||
g_dict_total += lens[j];
|
||||
// The quality argument does not matter for raw dictionaries.
|
||||
g_dict_prepared[j] = BrotliEncoderPrepareDictionary(
|
||||
BROTLI_SHARED_DICTIONARY_RAW, lens[j], ptrs[j], 11, NULL, NULL, NULL);
|
||||
g_num_dicts = j + 1;
|
||||
if (!g_dict_prepared[j]) {
|
||||
return "BrotliEncoderPrepareDictionary failed";
|
||||
}
|
||||
}
|
||||
if (g_dict_total <= (256 << 10)) {
|
||||
return NULL;
|
||||
}
|
||||
g_dict_template = wuffs_brotli__encoder__alloc();
|
||||
if (!g_dict_template) {
|
||||
return "alloc failed";
|
||||
}
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
uint64_t wlen = wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
g_dict_template, g_dict_lens[j]);
|
||||
if (!grow_workbuf(wlen)) {
|
||||
return "malloc failed";
|
||||
}
|
||||
wuffs_base__status status = wuffs_brotli__encoder__add_dictionary(
|
||||
g_dict_template, WUFFS_BROTLI__DICTIONARY_TYPE_RAW,
|
||||
wuffs_base__make_slice_u8((uint8_t*)g_dict_ptrs[j], g_dict_lens[j]),
|
||||
wuffs_base__make_slice_u8(g_workbuf, (size_t)wlen));
|
||||
if (status.repr) {
|
||||
return status.repr;
|
||||
}
|
||||
}
|
||||
g_dict_end = wuffs_brotli__encoder__workbuf_len(g_dict_template).max_incl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// encode_wuffs mirrors wuffs_brotli_encode_plan in test/c/std/brotli.c.
|
||||
static const char* //
|
||||
encode_wuffs(const config* c, size_t* have_len) {
|
||||
@@ -356,6 +445,26 @@ encode_wuffs(const config* c, size_t* have_len) {
|
||||
return "alloc failed";
|
||||
}
|
||||
const char* ret = NULL;
|
||||
if (g_dict_template) {
|
||||
memcpy(enc, g_dict_template, sizeof__wuffs_brotli__encoder());
|
||||
} else if (g_num_dicts > 0) {
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
uint64_t n = wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
enc, g_dict_lens[j]);
|
||||
if (!grow_workbuf(n)) {
|
||||
ret = "malloc failed";
|
||||
goto cleanup;
|
||||
}
|
||||
wuffs_base__status status = wuffs_brotli__encoder__add_dictionary(
|
||||
enc, WUFFS_BROTLI__DICTIONARY_TYPE_RAW,
|
||||
wuffs_base__make_slice_u8((uint8_t*)g_dict_ptrs[j], g_dict_lens[j]),
|
||||
wuffs_base__make_slice_u8(g_workbuf, (size_t)n));
|
||||
if (status.repr) {
|
||||
ret = status.repr;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
}
|
||||
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(g_have, MAX_OUTPUT);
|
||||
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(g_src, g_src_len,
|
||||
false);
|
||||
@@ -378,15 +487,11 @@ encode_wuffs(const config* c, size_t* have_len) {
|
||||
if (wlen > ((uint64_t)12 << 30)) {
|
||||
ret = g_too_large;
|
||||
goto cleanup;
|
||||
} else if (wlen > g_workbuf_len) {
|
||||
free(g_workbuf);
|
||||
g_workbuf = malloc((size_t)wlen);
|
||||
g_workbuf_len = g_workbuf ? (size_t)wlen : 0;
|
||||
if (!g_workbuf) {
|
||||
ret = "malloc failed";
|
||||
goto cleanup;
|
||||
}
|
||||
} else if (!grow_workbuf(wlen)) {
|
||||
ret = "malloc failed";
|
||||
goto cleanup;
|
||||
}
|
||||
g_last_wlen = wlen;
|
||||
if ((g_short_workbuf_len > 0) && (g_short_workbuf_len < wlen)) {
|
||||
wlen = g_short_workbuf_len;
|
||||
}
|
||||
@@ -461,6 +566,12 @@ encode_mimic(const config* c, size_t* want_len) {
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
if (!BrotliEncoderAttachPreparedDictionary(state, g_dict_prepared[j])) {
|
||||
ret = "BrotliEncoderAttachPreparedDictionary failed";
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
const uint8_t* next_in = g_src;
|
||||
for (size_t i = 0; i < g_num_steps; i++) {
|
||||
size_t avail_in = g_step_lens[i];
|
||||
@@ -509,6 +620,10 @@ describe(char* buf, size_t n, const char* name, const config* c) {
|
||||
c->chunking, c->flush, c->metadata_position, c->metadata_len,
|
||||
c->dst_chunk, c->seed);
|
||||
}
|
||||
k = (int)strlen(buf);
|
||||
if ((g_num_dicts > 0) && (k > 0) && ((size_t)k < n)) {
|
||||
snprintf(buf + k, n - (size_t)k, " dicts=%s", g_dict_desc);
|
||||
}
|
||||
}
|
||||
|
||||
// short_workbuf_len returns the shortest workbuf (at qualities 2 to 9) that
|
||||
@@ -568,9 +683,12 @@ short_workbuf_len(const config* c, size_t len, uint64_t wlen) {
|
||||
return (n < wlen) ? (size_t)n : 0;
|
||||
}
|
||||
|
||||
static const char* //
|
||||
roundtrip(const config* c, const uint8_t* data, size_t len, size_t want_len);
|
||||
|
||||
static void //
|
||||
check(const char* name, const config* c, const uint8_t* data, size_t len) {
|
||||
char desc[512];
|
||||
char desc[768];
|
||||
g_num_configs++;
|
||||
const char* err = make_plan(c, data, len);
|
||||
if (err) {
|
||||
@@ -594,16 +712,7 @@ check(const char* name, const config* c, const uint8_t* data, size_t len) {
|
||||
if (c->try_short && !have_err && !want_err) {
|
||||
// Also check the shortest workbuf that should work, and that one byte
|
||||
// less fails.
|
||||
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
|
||||
uint64_t wlen = 0;
|
||||
if (enc) {
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
wuffs_brotli__encoder__set_quirk(enc, brotli_enc_quirk_key(c->keys[j]),
|
||||
c->values[j]);
|
||||
}
|
||||
wlen = wuffs_brotli__encoder__workbuf_len(enc).min_incl;
|
||||
free(enc);
|
||||
}
|
||||
uint64_t wlen = g_last_wlen;
|
||||
size_t short_len = short_workbuf_len(c, len, wlen);
|
||||
if (short_len > 0) {
|
||||
g_num_short_checks++;
|
||||
@@ -646,7 +755,121 @@ check(const char* name, const config* c, const uint8_t* data, size_t len) {
|
||||
"at offset %zu\n",
|
||||
desc, have_len, want_len, i);
|
||||
fflush(stdout);
|
||||
return;
|
||||
}
|
||||
if (g_num_dicts > 0) {
|
||||
// A stream offset makes the output a continuation of another stream, so
|
||||
// it cannot be decoded on its own.
|
||||
bool stream_offset = false;
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
if ((c->keys[j] == 9) && c->values[j]) {
|
||||
stream_offset = true;
|
||||
}
|
||||
}
|
||||
if (!stream_offset) {
|
||||
g_num_roundtrips++;
|
||||
const char* rt_err = roundtrip(c, data, len, want_len);
|
||||
if (rt_err) {
|
||||
g_num_mismatches++;
|
||||
describe(desc, sizeof desc, name, c);
|
||||
printf("ROUNDTRIP FAILURE %s: %s\n", desc, rt_err);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// roundtrip checks that both libbrotlidec and Wuffs' decoder (with the same
|
||||
// dictionaries attached) decode g_want (of want_len bytes) to data.
|
||||
static const char* //
|
||||
roundtrip(const config* c, const uint8_t* data, size_t len, size_t want_len) {
|
||||
static uint8_t* out = NULL;
|
||||
static uint8_t* dec_workbuf = NULL;
|
||||
static size_t dec_workbuf_len = 0;
|
||||
if (!out) {
|
||||
out = malloc(MAX_INPUT);
|
||||
if (!out) {
|
||||
return "malloc failed";
|
||||
}
|
||||
}
|
||||
bool lw = false;
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
if (c->keys[j] == 6) {
|
||||
lw = c->values[j] != 0;
|
||||
}
|
||||
}
|
||||
|
||||
// libbrotlidec.
|
||||
BrotliDecoderState* d = BrotliDecoderCreateInstance(NULL, NULL, NULL);
|
||||
if (!d) {
|
||||
return "BrotliDecoderCreateInstance failed";
|
||||
}
|
||||
BrotliDecoderSetParameter(d, BROTLI_DECODER_PARAM_LARGE_WINDOW, lw ? 1 : 0);
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
if (!BrotliDecoderAttachDictionary(d, BROTLI_SHARED_DICTIONARY_RAW,
|
||||
g_dict_lens[j], g_dict_ptrs[j])) {
|
||||
BrotliDecoderDestroyInstance(d);
|
||||
return "BrotliDecoderAttachDictionary failed";
|
||||
}
|
||||
}
|
||||
size_t avail_in = want_len;
|
||||
const uint8_t* next_in = g_want;
|
||||
size_t avail_out = MAX_INPUT;
|
||||
uint8_t* next_out = out;
|
||||
BrotliDecoderResult r = BrotliDecoderDecompressStream(
|
||||
d, &avail_in, &next_in, &avail_out, &next_out, NULL);
|
||||
BrotliDecoderDestroyInstance(d);
|
||||
if (r != BROTLI_DECODER_RESULT_SUCCESS) {
|
||||
return "libbrotlidec failed";
|
||||
} else if (((MAX_INPUT - avail_out) != len) || (len && memcmp(out, data, len))) {
|
||||
return "libbrotlidec round trip mismatch";
|
||||
}
|
||||
|
||||
// Wuffs' decoder.
|
||||
wuffs_brotli__decoder* dec = wuffs_brotli__decoder__alloc();
|
||||
if (!dec) {
|
||||
return "alloc failed";
|
||||
}
|
||||
const char* ret = NULL;
|
||||
wuffs_brotli__decoder__set_quirk(dec, WUFFS_BROTLI__QUIRK_LARGE_WINDOW,
|
||||
lw ? 1 : 0);
|
||||
size_t need = g_dict_total + ((size_t)1 << 30) + 542;
|
||||
if (need > dec_workbuf_len) {
|
||||
free(dec_workbuf);
|
||||
dec_workbuf = malloc(need);
|
||||
dec_workbuf_len = dec_workbuf ? need : 0;
|
||||
if (!dec_workbuf) {
|
||||
free(dec);
|
||||
return "malloc failed";
|
||||
}
|
||||
}
|
||||
wuffs_base__slice_u8 wb = wuffs_base__make_slice_u8(dec_workbuf, dec_workbuf_len);
|
||||
for (size_t j = 0; j < g_num_dicts; j++) {
|
||||
wuffs_base__status status = wuffs_brotli__decoder__add_dictionary(
|
||||
dec, WUFFS_BROTLI__DICTIONARY_TYPE_RAW,
|
||||
wuffs_base__make_slice_u8((uint8_t*)g_dict_ptrs[j], g_dict_lens[j]), wb);
|
||||
if (status.repr) {
|
||||
ret = status.repr;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
uint64_t wlen = wuffs_brotli__decoder__workbuf_len(dec).max_incl;
|
||||
if (wlen > dec_workbuf_len) {
|
||||
ret = "decoder workbuf_len is too large";
|
||||
goto cleanup;
|
||||
}
|
||||
wuffs_base__io_buffer dst = wuffs_base__ptr_u8__writer(out, MAX_INPUT);
|
||||
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(g_want, want_len, true);
|
||||
wuffs_base__status status = wuffs_brotli__decoder__transform_io(
|
||||
dec, &dst, &src, wuffs_base__make_slice_u8(dec_workbuf, (size_t)wlen));
|
||||
if (status.repr) {
|
||||
ret = status.repr;
|
||||
} else if ((dst.meta.wi != len) || (len && memcmp(out, data, len))) {
|
||||
ret = "Wuffs decoder round trip mismatch";
|
||||
}
|
||||
cleanup:
|
||||
free(dec);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static config //
|
||||
@@ -959,8 +1182,318 @@ run_input_q2(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
}
|
||||
}
|
||||
|
||||
// run_dict_configs runs the dictionary grid's configurations for one
|
||||
// quality and the current dictionaries. level 0 is the full grid, level 1 a
|
||||
// smaller one and level 2 a much smaller one (for long inputs).
|
||||
static void //
|
||||
run_dict_configs(const char* name, const uint8_t* data, size_t len, uint32_t q,
|
||||
int level) {
|
||||
static const uint32_t lgwins[] = {10, 16, 22, 24};
|
||||
for (size_t w = 0; w < 4; w++) {
|
||||
for (uint32_t ch = 0; ch < 5; ch += 4) {
|
||||
for (uint32_t fl = 0; fl < 3; fl += 2) {
|
||||
if ((level >= 1) && ((w == 1) || ((ch == 0) && (fl == 2)))) {
|
||||
continue;
|
||||
} else if ((level >= 2) && ((w == 0) || (ch != fl * 2))) {
|
||||
continue;
|
||||
}
|
||||
config c = make_config(q, lgwins[w]);
|
||||
c.chunking = ch;
|
||||
c.flush = fl;
|
||||
c.seed = 0x60000 | (w << 8) | (ch << 4) | fl | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const uint32_t extras[][6] = {
|
||||
// Pairs of (BrotliEncoderParameter, value). A zero key ends the list.
|
||||
{5, 0xFFFFFFFF}, // BROTLI_PARAM_SIZE_HINT: the exact size.
|
||||
{5, 1u << 20}, // H54 (quality 4), H6 or H68.
|
||||
{5, 1}, //
|
||||
{9, 1}, // BROTLI_PARAM_STREAM_OFFSET.
|
||||
{9, 1000}, //
|
||||
{9, 1u << 30}, //
|
||||
{6, 1, 2, 18}, // BROTLI_PARAM_LARGE_WINDOW.
|
||||
{6, 1, 2, 30}, //
|
||||
{6, 1, 2, 25, 5, 1u << 20}, // H35, H55 or H65.
|
||||
{9, 1000, 6, 1, 2, 26}, // Stream offset and large window.
|
||||
{3, 16}, // BROTLI_PARAM_LGBLOCK.
|
||||
{3, 24, 5, 1u << 21}, //
|
||||
{7, 2, 8, 12}, // BROTLI_PARAM_NPOSTFIX and NDIRECT.
|
||||
{0, 2}, // FONT mode.
|
||||
{4, 1}, // DISABLE_LITERAL_CONTEXT_MODELING.
|
||||
};
|
||||
for (size_t i = 0; i < sizeof extras / sizeof extras[0]; i++) {
|
||||
for (uint32_t ch = 0; ch < 5; ch += 4) {
|
||||
if ((level >= 1) && (ch == 0)) {
|
||||
continue;
|
||||
} else if ((level >= 2) && (i != 1) && (i != 4) && (i != 8)) {
|
||||
continue;
|
||||
}
|
||||
config c = make_config(q, 22);
|
||||
for (size_t k = 0; (k < 6) && ((k == 0) || extras[i][k]); k += 2) {
|
||||
uint32_t v = extras[i][k + 1];
|
||||
if ((extras[i][k] == 5) && (v == 0xFFFFFFFF)) {
|
||||
v = (uint32_t)len;
|
||||
}
|
||||
add_param(&c, extras[i][k], v);
|
||||
}
|
||||
c.chunking = ch;
|
||||
c.flush = 2;
|
||||
c.seed = 0x70000 | i | (ch << 8) | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
if (level >= 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Metadata blocks, limited dst lengths and a flush after every chunk.
|
||||
for (uint32_t pos = 1; pos <= 2; pos++) {
|
||||
config c = make_config(q, 18);
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
c.metadata_position = pos;
|
||||
c.metadata_len = (pos == 1) ? 0 : 100;
|
||||
c.seed = 0x80000 | pos | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
{
|
||||
config c = make_config(q, 16);
|
||||
c.chunking = 4;
|
||||
c.flush = 0;
|
||||
c.dst_chunk = (len <= (32 << 10)) ? 3 : 4096;
|
||||
c.seed = 0x90000 | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
if (len <= (8 << 10)) {
|
||||
config c = make_config(q, 22);
|
||||
c.chunking = 2;
|
||||
c.flush = 1;
|
||||
c.seed = 0xA0000 | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
// check_smaller checks that, with the current dictionaries (which should
|
||||
// hold much of data), libbrotlienc's output (which Wuffs' output matches) is
|
||||
// smaller than without them (unless that is already tiny).
|
||||
static void //
|
||||
check_smaller(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
config c = make_config(q, 22);
|
||||
check(name, &c, data, len);
|
||||
size_t with_len = g_last_want_len;
|
||||
size_t saved = g_num_dicts;
|
||||
size_t without_len = 0;
|
||||
g_num_dicts = 0;
|
||||
const char* err = make_plan(&c, data, len);
|
||||
if (!err) {
|
||||
err = encode_mimic(&c, &without_len);
|
||||
}
|
||||
g_num_dicts = saved;
|
||||
g_num_smaller_checks++;
|
||||
// Tiny outputs (e.g. for long runs of zeroes) cannot shrink.
|
||||
if (err || (with_len > without_len) ||
|
||||
((without_len >= 64) && (with_len >= without_len))) {
|
||||
g_num_mismatches++;
|
||||
printf("NOT SMALLER %s: q=%u dicts=%s: %zu bytes with, %zu without: %s\n",
|
||||
name, q, g_dict_desc, with_len, without_len, err ? err : "");
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
// run_input_dict runs the dictionary grid (several sets of raw dictionaries,
|
||||
// mostly made from the input, so that there are matches) for an input.
|
||||
static void //
|
||||
run_input_dict(const char* name, const uint8_t* data, size_t len) {
|
||||
uint64_t configs0 = g_num_configs;
|
||||
uint64_t mismatches0 = g_num_mismatches;
|
||||
time_t start = time(NULL);
|
||||
static uint8_t noise[1000];
|
||||
fill_random(noise, sizeof noise, 0xD1C7);
|
||||
static const char text[] = "the quick brown fox jumps over the lazy dog";
|
||||
bool big = len > (256 << 10);
|
||||
|
||||
for (int set = 0; set < 7; set++) {
|
||||
const uint8_t* ptrs[MAX_DICTS];
|
||||
size_t lens[MAX_DICTS];
|
||||
size_t n = 0;
|
||||
int level = big ? 2 : 1;
|
||||
switch (set) {
|
||||
case 0: // A prefix of the input.
|
||||
ptrs[n] = data;
|
||||
lens[n++] = len / 2;
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "prefix");
|
||||
level = big ? 2 : 0;
|
||||
break;
|
||||
case 1: // The whole input.
|
||||
ptrs[n] = data;
|
||||
lens[n++] = len;
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "whole");
|
||||
level = big ? 2 : 0;
|
||||
break;
|
||||
case 2: // Another file.
|
||||
ptrs[n] = g_foreign_dict;
|
||||
lens[n++] = g_foreign_dict_len;
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "enwik5");
|
||||
break;
|
||||
case 3: // Noise and the input's middle third.
|
||||
ptrs[n] = noise;
|
||||
lens[n++] = sizeof noise;
|
||||
ptrs[n] = data + (len / 3);
|
||||
lens[n++] = (len / 3);
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "noise+middle");
|
||||
break;
|
||||
case 4: { // 15 (the maximum number of) pieces of the input.
|
||||
if (len == 0) {
|
||||
continue;
|
||||
}
|
||||
size_t k = (len + 14) / 15;
|
||||
for (size_t j = 0; j < 15; j++) {
|
||||
size_t at = (j * k) % len;
|
||||
ptrs[n] = data + at;
|
||||
lens[n++] = ((len - at) < k) ? (len - at) : k;
|
||||
}
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "15 pieces of %zu", k);
|
||||
break;
|
||||
}
|
||||
case 5: // Tiny dictionaries (and an empty one, first).
|
||||
ptrs[n] = (const uint8_t*)text;
|
||||
lens[n++] = 0;
|
||||
ptrs[n] = (const uint8_t*)text;
|
||||
lens[n++] = 1;
|
||||
ptrs[n] = (const uint8_t*)text + 4;
|
||||
lens[n++] = 7;
|
||||
ptrs[n] = (const uint8_t*)text + 10;
|
||||
lens[n++] = 8;
|
||||
ptrs[n] = (len >= 9) ? data + (len - 9) : (const uint8_t*)text;
|
||||
lens[n++] = 9;
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "tiny");
|
||||
break;
|
||||
case 6: // The input's two halves, swapped.
|
||||
ptrs[n] = data + (len / 2);
|
||||
lens[n++] = len - (len / 2);
|
||||
ptrs[n] = data;
|
||||
lens[n++] = len / 2;
|
||||
ptrs[n] = (const uint8_t*)text;
|
||||
lens[n++] = sizeof text - 1;
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "swapped halves+text");
|
||||
break;
|
||||
}
|
||||
const char* err = set_dicts(n, ptrs, lens);
|
||||
if (err) {
|
||||
g_num_mismatches++;
|
||||
printf("FAILURE %s: set_dicts(%s): %s\n", name, g_dict_desc, err);
|
||||
continue;
|
||||
}
|
||||
for (uint32_t q = g_qmin; q <= g_qmax; q++) {
|
||||
run_dict_configs(name, data, len, q, level);
|
||||
if ((set == 1) && (len >= 1000) && (q >= 5)) {
|
||||
check_smaller(name, data, len, q);
|
||||
}
|
||||
}
|
||||
}
|
||||
set_dicts(0, NULL, NULL);
|
||||
printf("%-50s %8zu bytes: %6" PRIu64 " configs, %" PRIu64
|
||||
" mismatches (%lds)\n",
|
||||
name, len, g_num_configs - configs0, g_num_mismatches - mismatches0,
|
||||
(long)(time(NULL) - start));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// run_big_dicts checks dictionaries of 2 to 40 MiB (every hash table size
|
||||
// class of CreatePreparedDictionary), with pieces of a 1 MiB input copied
|
||||
// into them at various offsets.
|
||||
static void //
|
||||
run_big_dicts(size_t which) {
|
||||
static const size_t sizes[] = {
|
||||
(2 << 20), (2 << 20) + 1, (3 << 20) + 77, (5 << 20) + 3,
|
||||
(9 << 20) + 5, (17 << 20) + 1, (40 << 20) + 9,
|
||||
};
|
||||
if (which >= sizeof sizes / sizeof sizes[0]) {
|
||||
return;
|
||||
}
|
||||
uint64_t configs0 = g_num_configs;
|
||||
uint64_t mismatches0 = g_num_mismatches;
|
||||
time_t start = time(NULL);
|
||||
size_t dlen = sizes[which];
|
||||
size_t len = 1 << 20;
|
||||
uint8_t* input = malloc(len);
|
||||
uint8_t* dict = malloc(dlen);
|
||||
if (!input || !dict) {
|
||||
printf("FAILURE: malloc failed\n");
|
||||
g_num_mismatches++;
|
||||
free(input);
|
||||
free(dict);
|
||||
return;
|
||||
}
|
||||
fill_mixed(input, len, 0xB16 + which);
|
||||
fill_mixed(dict, dlen, 0xD1C + which);
|
||||
// Copy 64 KiB pieces of the input to the dictionary's start, middle and
|
||||
// end (with sparse changes).
|
||||
for (size_t j = 0; j < 3; j++) {
|
||||
size_t at = (j == 0) ? 1234 : (j == 1) ? (dlen / 2) : (dlen - (70 << 10));
|
||||
memcpy(dict + at, input + (j * (300 << 10)), 64 << 10);
|
||||
for (size_t i = 777; i < (64 << 10); i += 4099) {
|
||||
dict[at + i] ^= 0x33;
|
||||
}
|
||||
}
|
||||
char name[96];
|
||||
snprintf(name, sizeof name, "synthetic: 1 MiB with a %zu byte dictionary",
|
||||
dlen);
|
||||
const uint8_t* ptrs[2] = {dict, input + (900 << 10)};
|
||||
size_t lens[2] = {dlen, 1000};
|
||||
snprintf(g_dict_desc, sizeof g_dict_desc, "big %zu + 1000", dlen);
|
||||
const char* err = set_dicts(2, ptrs, lens);
|
||||
if (err) {
|
||||
g_num_mismatches++;
|
||||
printf("FAILURE %s: set_dicts: %s\n", name, err);
|
||||
} else {
|
||||
for (uint32_t q = (g_qmin > 2) ? g_qmin : 2; q <= g_qmax; q++) {
|
||||
static const uint32_t lgwins[] = {16, 24};
|
||||
for (size_t w = 0; w < 2; w++) {
|
||||
config c = make_config(q, lgwins[w]);
|
||||
c.seed = 0xB0000 | (w << 8) | (q << 12);
|
||||
check(name, &c, input, len);
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
check(name, &c, input, len);
|
||||
}
|
||||
static const uint32_t extras[][6] = {
|
||||
{6, 1, 2, 30, 5, 1u << 20},
|
||||
{9, 1000},
|
||||
{5, 1u << 20},
|
||||
};
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
config c = make_config(q, 22);
|
||||
for (size_t k = 0; (k < 6) && ((k == 0) || extras[i][k]); k += 2) {
|
||||
add_param(&c, extras[i][k], extras[i][k + 1]);
|
||||
}
|
||||
c.seed = 0xC0000 | i | (q << 12);
|
||||
check(name, &c, input, len);
|
||||
}
|
||||
if (q >= 5) {
|
||||
check_smaller(name, input, len, q);
|
||||
}
|
||||
}
|
||||
}
|
||||
set_dicts(0, NULL, NULL);
|
||||
free(input);
|
||||
free(dict);
|
||||
printf("%-50s %8zu bytes: %6" PRIu64 " configs, %" PRIu64
|
||||
" mismatches (%lds)\n",
|
||||
name, len, g_num_configs - configs0, g_num_mismatches - mismatches0,
|
||||
(long)(time(NULL) - start));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static void //
|
||||
run_input(const char* name, const uint8_t* data, size_t len) {
|
||||
if (g_dict_only) {
|
||||
run_input_dict(name, data, len);
|
||||
return;
|
||||
}
|
||||
uint64_t configs0 = g_num_configs;
|
||||
uint64_t mismatches0 = g_num_mismatches;
|
||||
time_t start = time(NULL);
|
||||
@@ -1070,6 +1603,8 @@ main(int argc, char** argv) {
|
||||
g_oneshot_lgwin = a;
|
||||
} else if (strcmp(argv[argi], "-lw") == 0) {
|
||||
g_lw_only = true;
|
||||
} else if (strcmp(argv[argi], "-dict") == 0) {
|
||||
g_dict_only = true;
|
||||
} else if (strcmp(argv[argi], "-big") == 0) {
|
||||
big = true;
|
||||
} else if (sscanf(argv[argi], "-shard=%u/%u", &a, &b) == 2) {
|
||||
@@ -1115,7 +1650,20 @@ main(int argc, char** argv) {
|
||||
compare_strings);
|
||||
}
|
||||
|
||||
g_foreign_dict = read_whole_file("test/data/enwik5", &g_foreign_dict_len);
|
||||
if (!g_foreign_dict) {
|
||||
fprintf(stderr, "could not read test/data/enwik5\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int input_index = 0;
|
||||
if (g_dict_only && (argi >= argc)) {
|
||||
for (size_t j = 0; j < 7; j++) {
|
||||
if (((input_index++) % shard_n) == shard_k) {
|
||||
run_big_dicts(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (big) {
|
||||
// Synthetic inputs of several MiB (with the usual grid) and of 24 to 70
|
||||
// MiB (with run_input_far's configurations), with repeats at distances
|
||||
@@ -1205,8 +1753,10 @@ main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
printf("TOTAL: %" PRIu64 " configs (%" PRIu64
|
||||
" also with a short workbuf, %" PRIu64
|
||||
" also with a short workbuf, %" PRIu64 " round trips, %" PRIu64
|
||||
" smaller-with-dictionary checks, %" PRIu64
|
||||
" skipped as too large), %" PRIu64 " mismatches\n",
|
||||
g_num_configs, g_num_short_checks, g_num_skipped, g_num_mismatches);
|
||||
g_num_configs, g_num_short_checks, g_num_roundtrips,
|
||||
g_num_smaller_checks, g_num_skipped, g_num_mismatches);
|
||||
return g_num_mismatches ? 1 : 0;
|
||||
}
|
||||
+179
-7
@@ -154,7 +154,8 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
|
||||
- Metadata callbacks (metadata blocks are currently skipped). See the
|
||||
"Metadata Blocks" section below.
|
||||
- Fast paths.
|
||||
- Encoder dictionaries (see the "Encoder" section below).
|
||||
- Compound dictionary matches at encoder qualities 10 and 11 (see the
|
||||
"Encoder" section below).
|
||||
|
||||
|
||||
## Encoder
|
||||
@@ -184,6 +185,11 @@ hasher, `BrotliCreateZopfliBackwardReferences` at quality 10 and
|
||||
`BrotliStoreMetaBlock`). Large-window brotli (see "Encoder Large Window"
|
||||
below) works at qualities 3 to 11 with lgwin 10 to 30. Like the C code,
|
||||
qualities 0 to 2 ignore the large-window quirk.
|
||||
Raw (LZ77 prefix) dictionaries (see "Encoder Dictionaries" below) work at
|
||||
qualities 0 to 9, and, like the C code, qualities 0 and 1 ignore them. At
|
||||
qualities 10 and 11, `transform_io` returns `"#unsupported encoder quality"`
|
||||
if a dictionary is attached (the Zopfli path's dictionary lookups are not
|
||||
implemented yet).
|
||||
|
||||
The source files are:
|
||||
|
||||
@@ -218,6 +224,13 @@ The source files are:
|
||||
`hash_composite_inc.h` (H35, H55 and H65, which combine H3, H54 or H6 with
|
||||
a rolling hasher) and `hash_rolling_inc.h` (`HROLLING_FAST` and
|
||||
`HROLLING`).
|
||||
- `encode_dictionary.wuffs`: `compound_dictionary.c`
|
||||
(`CreatePreparedDictionary`, `AttachPreparedDictionary`), the raw
|
||||
dictionary cases of `BrotliEncoderPrepareDictionary` and
|
||||
`BrotliEncoderAttachPreparedDictionary` (as `add_dictionary`), the
|
||||
compound dictionary search of `hash.h` (`FindCompoundDictionaryMatch`,
|
||||
`LookupCompoundDictionaryMatch`) and the compound dictionary branch of
|
||||
`ExtendLastCommand`.
|
||||
- `encode_backward_references.wuffs`: `backward_references.c` and
|
||||
`backward_references_inc.h` (`BrotliCreateBackwardReferences`),
|
||||
`command.h` and `ComputeDistanceCode`.
|
||||
@@ -286,6 +299,10 @@ In addition to the `base.io_transformer` methods, the encoder has:
|
||||
another request is pending.
|
||||
- `max_compressed_size(input_size: u64) u64`: mirrors
|
||||
`BrotliEncoderMaxCompressedSize` (0 means overflow).
|
||||
- `add_dictionary!(dict_type: u32, dict: roslice u8, workbuf: slice u8)
|
||||
status` and `add_dictionary_workbuf_len(dict_len: u64) u64`: attach a raw
|
||||
dictionary, like `BrotliEncoderPrepareDictionary` plus
|
||||
`BrotliEncoderAttachPreparedDictionary`. See "Encoder Dictionaries" below.
|
||||
|
||||
`dst_history_retain_length` is 0: the encoder never re-reads `dst`.
|
||||
|
||||
@@ -297,7 +314,8 @@ Each quirk mirrors one `BrotliEncoderParameter`, with the value semantics of
|
||||
otherwise `set_quirk` returns `"#bad argument"`). Like the C code, they are
|
||||
sanitized (`SanitizeParams`, `ComputeLgBlock`, `ChooseDistanceParams`) at the
|
||||
first `transform_io` call, after which `set_quirk` returns
|
||||
`"#bad call sequence"`.
|
||||
`"#bad call sequence"`. Quirks and dictionaries (`add_dictionary`) can be set
|
||||
in any order before that first call.
|
||||
|
||||
| Quirk | C parameter |
|
||||
| ---------------------------------------- | ---------------------------------------------- |
|
||||
@@ -329,6 +347,12 @@ size hint is replaced by the C code's `UpdateSizeHint` estimate.
|
||||
|
||||
### Encoder Workbuf Layout
|
||||
|
||||
Any attached dictionaries come first (see "Encoder Dictionaries" below): the
|
||||
workbuf's first `D` bytes hold them (with their prepared hash tables) and
|
||||
everything described in this section starts at offset `D` (so all of the
|
||||
offsets and lengths below are relative to `workbuf[D ..]`, and `workbuf_len`
|
||||
is `D` more than the lengths given here). `D` is 0 without dictionaries.
|
||||
|
||||
At qualities 0 and 1, the workbuf is the C code's metablock storage (its
|
||||
`GetBrotliStorage` buffer): `workbuf_len` is `(2 << lgwin) + 503 + 16` bytes
|
||||
(the storage for a `1 << lgwin` byte block plus 16 bytes of slack for 8-byte
|
||||
@@ -623,9 +647,139 @@ truncated for windows up to `1 << 30`. The rolling hashers' table holds
|
||||
32-bit positions, like the C code's.
|
||||
|
||||
|
||||
### Encoder Dictionaries
|
||||
|
||||
`encoder.add_dictionary!(dict_type: base.u32, dict: roslice base.u8, workbuf:
|
||||
slice base.u8) base.status` mirrors the C API's
|
||||
`BrotliEncoderPrepareDictionary(BROTLI_SHARED_DICTIONARY_RAW, ...)`
|
||||
followed by `BrotliEncoderAttachPreparedDictionary`: it attaches a raw (LZ77
|
||||
prefix) dictionary, as the next chunk of the C code's compound dictionary.
|
||||
The decoder must be given the same dictionaries, in the same order (with
|
||||
this package's `decoder.add_dictionary` or libbrotlidec's
|
||||
`BrotliDecoderAttachDictionary`, as `DICTIONARY_TYPE_RAW` /
|
||||
`BROTLI_SHARED_DICTIONARY_RAW`).
|
||||
|
||||
Ordering: like quirks, dictionaries must be added before the first
|
||||
`transform_io` call (the C API's "before the first
|
||||
`BrotliEncoderCompressStream`"). Quirks and dictionaries can be set in any
|
||||
order (preparing a raw dictionary does not depend on any parameter: the
|
||||
`quality` argument of `BrotliEncoderPrepareDictionary` is unused for raw
|
||||
dictionaries). Call `workbuf_len` after the last `add_dictionary` and
|
||||
`set_quirk` call.
|
||||
|
||||
Like the decoder, the encoder copies each dictionary into the workbuf, so
|
||||
every `add_dictionary` and `transform_io` call must be passed the same
|
||||
workbuf (or at least one whose first bytes hold the same contents). Each
|
||||
dictionary is followed by its prepared hash tables (the C code's
|
||||
`PreparedDictionary`), which `add_dictionary` builds exactly like
|
||||
`CreatePreparedDictionary` and `CreatePreparedDictionaryWithParams` in
|
||||
`compound_dictionary.c`, so that the compound dictionary search finds the
|
||||
same candidates in the same order:
|
||||
|
||||
- The hash table size depends only on the dictionary length `n`: 17 bucket
|
||||
bits and 7 slot bits for `n` up to 2 MiB, one more of each for each
|
||||
doubling of `n` (18 and 8 up to 4 MiB, etc), up to 22 and 12 (for more than
|
||||
32 MiB). Each position `i` with `i + 7 < n` is hashed (the low 40 bits of
|
||||
its 8 little-endian bytes, times `kPreparedDictionaryHashMul64Long`), with
|
||||
at most 32 positions per bucket (the most recent ones), and the bucket
|
||||
limits are lowered per slot so that each slot has fewer than 65535 items.
|
||||
- Its layout, after the `n` dictionary bytes, is `slot_offsets` (`1 <<
|
||||
slot_bits` u32le values), `heads` (`1 << bucket_bits` u16le values, 0xFFFF
|
||||
for an empty bucket) and `items` (`num_items` u32le values, each a
|
||||
dictionary position, with the high bit marking the end of a bucket's
|
||||
chain), like the C struct's dynamic members.
|
||||
|
||||
So the workbuf layout is:
|
||||
|
||||
[0 .. D) the dictionaries, each one being its n bytes, then
|
||||
(4 << slot_bits) + (2 << bucket_bits) + (4 * num_items)
|
||||
bytes of hash tables. num_items is at most max(0, n - 7).
|
||||
[D .. ) the encoder's other regions (see "Encoder Workbuf Layout").
|
||||
|
||||
Preparing a dictionary needs some scratch space (the C code's temporary
|
||||
"bloated" hasher: `(6 << bucket_bits) + (4 * n)` bytes), which `add_dictionary`
|
||||
puts after the largest possible hash tables. So `add_dictionary`'s workbuf
|
||||
must be at least `add_dictionary_workbuf_len(n)` bytes long: `D` (for the
|
||||
dictionaries added so far) plus `n + (4 << slot_bits) + (2 << bucket_bits) +
|
||||
(4 * max(0, n - 7))` plus that scratch space. For example, for a 1 MiB
|
||||
dictionary that is `D` plus about 9.8 MiB, and for a 40 MiB dictionary `D`
|
||||
plus about 384 MiB. After the call, `D` grows by the dictionary's bytes and
|
||||
actual hash tables (e.g. by at most 5.3 MiB for 1 MiB of dictionary).
|
||||
|
||||
The compound dictionary then affects compression like in the C code:
|
||||
|
||||
- Qualities 0 and 1 (`BrotliEncoderCompressStreamFast`) ignore it.
|
||||
- At qualities 2 to 9, its total size (the C code's `gap`) is added to the
|
||||
static dictionary's base distance (`dictionary_start + gap` in
|
||||
`CreateBackwardReferences`), and distances up to `dictionary_start + gap`
|
||||
(which include compound dictionary references) update the distance cache.
|
||||
`ExtendLastCommand` can extend a copy from the compound dictionary.
|
||||
- Only the hashers that `BrotliCreateBackwardReferences` instantiates with
|
||||
`ENABLE_COMPOUND_DICTIONARY` search the dictionaries (with
|
||||
`LookupCompoundDictionaryMatch`, after the hasher's own search, for each
|
||||
chunk in order): H5, H6, H58, H68, H40, H41, H42, H55 and H65. So
|
||||
qualities 5 to 9 search them, quality 4 only with the large-window H55
|
||||
(lgwin above 24 and a big size hint), and qualities 2 and 3 (H2, H3, H35)
|
||||
and quality 4 with H4 or H54 never do (the C code falls back to its
|
||||
non-dictionary variant for those hashers). The search only finds matches
|
||||
that are within `params.dist.max_distance` (`BROTLI_MAX_DISTANCE`, or
|
||||
`BROTLI_MAX_ALLOWED_DISTANCE` for large-window brotli) and that do not
|
||||
cross a chunk's end.
|
||||
- With `QUIRK_STREAM_OFFSET`, `dictionary_start` is `min(position +
|
||||
stream_offset, max_backward_limit)`, as in the C code (whose output then
|
||||
continues another stream and can only be decoded after it).
|
||||
|
||||
`add_dictionary` statuses (the C code's functions return `NULL` or
|
||||
`BROTLI_FALSE`):
|
||||
|
||||
| Condition | Wuffs status | C code |
|
||||
| ------------------------------------------------------------ | ---------------------------------------------- | ------ |
|
||||
| `transform_io` has been called | `base."#bad call sequence"` | accepted (see below) |
|
||||
| `DICTIONARY_TYPE_SERIALIZED` | `"#unsupported serialized encoder dictionary"` | `BrotliEncoderPrepareDictionary` returns `NULL` |
|
||||
| unknown `dict_type` | `base."#bad argument"` | `BrotliEncoderPrepareDictionary` returns `NULL` |
|
||||
| a 16th dictionary | `"#too many compound dictionaries"` | `BrotliEncoderAttachPreparedDictionary` fails |
|
||||
| more than `0x7FFF_FFFF` bytes of dictionaries in total | `"#too much dictionary data"` | accepted |
|
||||
| `workbuf` shorter than `add_dictionary_workbuf_len(n)` | `base."#bad workbuf length"` | n/a |
|
||||
|
||||
An empty dictionary is accepted (and counts towards the 15), like in the C
|
||||
code. On failure, the encoder's state is unchanged. A `transform_io` call
|
||||
with a workbuf shorter than `D` returns `base."#bad workbuf length"`.
|
||||
|
||||
Differences from the C code:
|
||||
|
||||
- Serialized (shared) dictionaries are rejected, exactly like the default
|
||||
(non-`BROTLI_EXPERIMENTAL`) libbrotlienc build, whose
|
||||
`BrotliEncoderPrepareDictionary` returns `NULL` for them. (The decoder, in
|
||||
contrast, accepts them, like an experimental libbrotlidec build.) With an
|
||||
experimental build, the C encoder would also use their custom static
|
||||
dictionaries; that is not implemented.
|
||||
- The C code's `BrotliEncoderAttachPreparedDictionary` accepts a raw
|
||||
dictionary even after compression has started (it only checks
|
||||
`is_initialized_` for serialized dictionaries). The compound dictionary's
|
||||
size then changes mid-stream, so the static dictionary references before
|
||||
and after that point use different distance bases, and the output is
|
||||
generally not decodable. Here, `add_dictionary` returns `"#bad call
|
||||
sequence"` once `transform_io` has been called.
|
||||
- The C code holds each dictionary's length in a `uint32_t` (and item
|
||||
offsets in 31 bits). This package rejects more than `0x7FFF_FFFF` bytes
|
||||
of dictionaries in total (like its decoder).
|
||||
- When `ExtendLastCommand` extends a copy past the end of a compound
|
||||
dictionary chunk into an empty chunk, the C code reads (one or more
|
||||
bytes) past the end of that empty chunk's (zero-length) data. Here, the
|
||||
extension stops there. This needs an empty dictionary attached after a
|
||||
non-empty one and a copy extension that reaches the non-empty one's end
|
||||
(with empty dictionaries only before the non-empty ones, the behaviors are
|
||||
the same).
|
||||
- Like the C code's `BrotliEncoderPrepareDictionary` with the default
|
||||
allocator, there is no separate "prepared dictionary" object that could be
|
||||
shared between encoders: each encoder prepares its own copy (the C code's
|
||||
"lean" prepared dictionaries reference the caller's bytes instead).
|
||||
|
||||
|
||||
### Encoder Testing
|
||||
|
||||
`wuffs test -mimic std/brotli` checks byte-exact parity with libbrotlienc
|
||||
`wuffs test -mimic std/brotli` (including its encoder dictionary tests)
|
||||
checks byte-exact parity with libbrotlienc
|
||||
over a grid of qualities (0 to 11), lgwins, modes, input chunkings, flushes,
|
||||
metadata blocks, size hints and other parameters (lgblock, npostfix, ndirect,
|
||||
disabling literal context modeling, stream offset, large window), as well as
|
||||
@@ -641,7 +795,22 @@ processes, `-lw` runs only its large-window grid (lgwin 10 to 30 at
|
||||
qualities 2 to 9, with various chunkings, flushes, size hints and other
|
||||
parameters, each also with the shortest workbuf that should work) and `-big`
|
||||
replaces the inputs with synthetic ones of 3 to 70 MiB with repeats at
|
||||
distances of up to 40 MiB. With `-q=0-9` (the default) it runs 814,706
|
||||
distances of up to 40 MiB. `-dict` replaces the usual grid with a
|
||||
dictionary grid (see "Encoder Dictionaries" above): for each input, seven
|
||||
sets of raw dictionaries (a prefix of the input, the whole input,
|
||||
`test/data/enwik5`, noise plus the input's middle third, 15 pieces of the
|
||||
input, tiny and empty dictionaries, and the input's swapped halves plus some
|
||||
text), with qualities 0 to 9, lgwins, chunkings, flushes, metadata blocks,
|
||||
size hints, stream offsets, large window and other parameters, plus
|
||||
dictionaries of 2 MiB to 40 MiB (every hash table size) with a 1 MiB input.
|
||||
Each dictionary configuration's output is also decoded by libbrotlidec and
|
||||
by this package's decoder (with the same dictionaries), except with a
|
||||
stream offset, and the whole-input and big dictionaries check that the
|
||||
output is smaller than without them (at qualities 5 and above, unless it is
|
||||
already tiny). With `-dict` (and `-q=0-9`) it runs 419,802 configurations,
|
||||
all byte-exact against libbrotlienc v1.2.1 on x86-64, with 356,546 round
|
||||
trips through both decoders. With
|
||||
`-q=0-9` (the default) and without `-dict` it runs 814,706
|
||||
configurations (96,688 of them in the large-window grid), all byte-exact
|
||||
against libbrotlienc v1.2.1 on x86-64. With `-lw -big` it runs 720 more
|
||||
(on 3, 6 and 12 MiB inputs with the large-window grid and on 24, 40 and
|
||||
@@ -828,14 +997,17 @@ metablock) for as long as it needs the results.
|
||||
|
||||
### Encoder TODO
|
||||
|
||||
- Large-window encoding at qualities 3 and above (it works at quality 2).
|
||||
- `workbuf_len` is a worst case (or, at qualities 10 and 11, a practical
|
||||
bound). A future option could take the input length (like the C code's
|
||||
size hint) to give a tighter bound.
|
||||
- Speed: at qualities 10 and 11 the Wuffs encoder is several times slower
|
||||
than libbrotlienc, mostly because of the soft float arithmetic in the
|
||||
block splitter and histogram clustering.
|
||||
- Custom and shared dictionaries (`BrotliEncoderAttachPreparedDictionary`).
|
||||
Zopfli cost model, the block splitter and histogram clustering.
|
||||
- Compound dictionary matches at qualities 10 and 11 (the Zopfli path's
|
||||
`FindCompoundDictionaryMatch` lookups); attaching a dictionary at those
|
||||
qualities currently returns `"#unsupported encoder quality"`.
|
||||
- Serialized (shared) dictionaries, which only an experimental
|
||||
(`BROTLI_EXPERIMENTAL`) libbrotlienc build supports.
|
||||
- Changing parameters mid-stream.
|
||||
|
||||
|
||||
|
||||
@@ -224,10 +224,11 @@ pri func encoder.compute_distance_code(distance: base.u64, max_distance: base.u6
|
||||
}
|
||||
|
||||
// Mirrors CreateBackwardReferences in backward_references_inc.h (via
|
||||
// BrotliCreateBackwardReferences in backward_references.c), without a
|
||||
// compound dictionary and with the built-in static dictionary. New commands
|
||||
// are appended at this.num_cmds.
|
||||
pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.u64, position: base.u64) base.status {
|
||||
// BrotliCreateBackwardReferences in backward_references.c), with the
|
||||
// built-in static dictionary. Its ENABLE_COMPOUND_DICTIONARY variant (see
|
||||
// compound_dictionary_enabled) searches the attached dictionaries (in
|
||||
// args.dwb). New commands are appended at this.num_cmds.
|
||||
pri func encoder.create_backward_references!(wb: slice base.u8, dwb: roslice base.u8, num_bytes: base.u64, position: base.u64) base.status {
|
||||
var position : base.u64
|
||||
var max_backward_limit : base.u64
|
||||
var position_offset : base.u64
|
||||
@@ -250,6 +251,9 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
var range_end : base.u64
|
||||
var k_margin : base.u64
|
||||
var pos_jump : base.u64
|
||||
var gap : base.u64
|
||||
var use_compound : base.bool
|
||||
var data : roslice base.u8
|
||||
|
||||
position = args.position
|
||||
max_backward_limit = ((1 as base.u64) << this.lgwin) ~mod- 16
|
||||
@@ -266,6 +270,11 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
random_heuristics_window_size = 512
|
||||
}
|
||||
apply_random_heuristics = position ~mod+ random_heuristics_window_size
|
||||
gap = this.dict_total_size
|
||||
use_compound = this.compound_dictionary_enabled()
|
||||
if this.rb_buf_off <= args.wb.length() {
|
||||
data = args.wb[this.rb_buf_off ..]
|
||||
}
|
||||
|
||||
this.prepare_distance_cache!()
|
||||
|
||||
@@ -278,8 +287,13 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
this.sr_distance = 0
|
||||
this.sr_score = SCORE_BASE + 100
|
||||
this.find_longest_match!(wb: args.wb, cur_ix: position, max_length: max_length,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start ~mod+ gap,
|
||||
max_distance: this.dist_max_distance)
|
||||
if use_compound {
|
||||
this.lookup_compound_dictionary_match!(data: data, dwb: args.dwb,
|
||||
cur_ix: position, max_length: max_length,
|
||||
max_ring_buffer_distance: dictionary_start, max_distance: this.dist_max_distance)
|
||||
}
|
||||
if this.sr_score > (SCORE_BASE + 100) {
|
||||
// Found a match. Let's look for something even better ahead.
|
||||
delayed_backward_references = 0
|
||||
@@ -299,8 +313,13 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
max_distance = this.min64(a: position ~mod+ 1, b: max_backward_limit)
|
||||
dictionary_start = this.min64(a: (position ~mod+ 1) ~mod+ position_offset, b: max_backward_limit)
|
||||
this.find_longest_match!(wb: args.wb, cur_ix: position ~mod+ 1, max_length: max_length,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start ~mod+ gap,
|
||||
max_distance: this.dist_max_distance)
|
||||
if use_compound {
|
||||
this.lookup_compound_dictionary_match!(data: data, dwb: args.dwb,
|
||||
cur_ix: position ~mod+ 1, max_length: max_length,
|
||||
max_ring_buffer_distance: dictionary_start, max_distance: this.dist_max_distance)
|
||||
}
|
||||
if this.sr_score >= (sr_score ~mod+ 175) {
|
||||
// Ok, let's just write one byte for now and start a match
|
||||
// from the next byte.
|
||||
@@ -324,8 +343,8 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
dictionary_start = this.min64(a: position ~mod+ position_offset, b: max_backward_limit)
|
||||
// The first 16 codes are special short-codes, and the minimum
|
||||
// offset is 1.
|
||||
distance_code = this.compute_distance_code(distance: this.sr_distance, max_distance: dictionary_start)
|
||||
if (this.sr_distance <= dictionary_start) and (distance_code > 0) {
|
||||
distance_code = this.compute_distance_code(distance: this.sr_distance, max_distance: dictionary_start ~mod+ gap)
|
||||
if (this.sr_distance <= (dictionary_start ~mod+ gap)) and (distance_code > 0) {
|
||||
this.dist_cache[3] = this.dist_cache[2]
|
||||
this.dist_cache[2] = this.dist_cache[1]
|
||||
this.dist_cache[1] = this.dist_cache[0]
|
||||
|
||||
@@ -194,6 +194,19 @@ pub struct encoder? implements base.io_transformer(
|
||||
hr_next_ix : base.u64,
|
||||
hr_off : base.u64,
|
||||
|
||||
// The attached raw dictionaries (the C code's CompoundDictionary, see
|
||||
// encode_dictionary.wuffs). They use the first dict_end bytes of the
|
||||
// workbuf.
|
||||
dict_num_chunks : base.u32[..= 15],
|
||||
dict_total_size : base.u64,
|
||||
dict_end : base.u64,
|
||||
dict_chunk_offsets : array[16] base.u64,
|
||||
dc_src_off : array[15] base.u64,
|
||||
dc_size : array[15] base.u64,
|
||||
dc_bucket_bits : array[15] base.u32,
|
||||
dc_slot_bits : array[15] base.u32,
|
||||
dc_num_items : array[15] base.u64,
|
||||
|
||||
// The C code's HasherSearchResult.
|
||||
sr_len : base.u64,
|
||||
sr_distance : base.u64,
|
||||
@@ -358,6 +371,12 @@ pub struct encoder? implements base.io_transformer(
|
||||
// GetHashTable result), with up to (1 << 17) elements.
|
||||
hash_table : array[131072] base.u32,
|
||||
|
||||
// CreatePreparedDictionaryWithParams' slot_size and slot_limit
|
||||
// arrays, and its slot_offsets (see encode_dictionary.wuffs).
|
||||
dp_slot_size : array[4096] base.u32,
|
||||
dp_slot_limit : array[4096] base.u32,
|
||||
dp_slot_off : array[4096] base.u32,
|
||||
|
||||
// command_buf and literal_buf are the C code's command_buf_ and
|
||||
// literal_buf_ (quality 1 only).
|
||||
command_buf : array[131072] base.u32,
|
||||
@@ -477,6 +496,7 @@ pub func encoder.workbuf_len() base.range_ii_u64 {
|
||||
var q : base.u32[..= 11]
|
||||
var lgwin : base.u32[..= 30]
|
||||
var n : base.u64
|
||||
var r : base.range_ii_u64
|
||||
|
||||
q = this.sanitized_quality()
|
||||
lgwin = this.sanitized_lgwin(quality: q)
|
||||
@@ -486,8 +506,12 @@ pub func encoder.workbuf_len() base.range_ii_u64 {
|
||||
n = this.compute_layout_offset(quality: q, lgwin: lgwin,
|
||||
lgblock: this.computed_lgblock(quality: q, lgwin: lgwin), idx: LAYOUT_END)
|
||||
} else {
|
||||
return this.workbuf_len_hq(quality: q, lgwin: lgwin)
|
||||
r = this.workbuf_len_hq(quality: q, lgwin: lgwin)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: r.get_min_incl() ~sat+ this.dict_end,
|
||||
max_incl: r.get_max_incl() ~sat+ this.dict_end)
|
||||
}
|
||||
n ~sat+= this.dict_end
|
||||
return this.util.make_range_ii_u64(min_incl: n, max_incl: n)
|
||||
}
|
||||
|
||||
@@ -849,7 +873,31 @@ pri func encoder.write_metadata_header!(block_size: base.u32) base.u64 {
|
||||
// qualities 0 and 1, BrotliEncoderCompressStreamFast). Each (initial or
|
||||
// resumed) transform_io call corresponds to one or more C calls. See
|
||||
// README.md for the details.
|
||||
//
|
||||
// The attached dictionaries (see add_dictionary) are the first dict_end
|
||||
// bytes of the workbuf. do_transform_io gets the rest of the workbuf (whose
|
||||
// layout is described in README.md and encode_data.wuffs) and, as args.dwb,
|
||||
// the dictionaries.
|
||||
pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
if this.dict_end > args.workbuf.length() {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src,
|
||||
workbuf: args.workbuf[this.dict_end ..],
|
||||
dwb: args.workbuf[.. this.dict_end])
|
||||
if status.is_ok() {
|
||||
return ok
|
||||
} else if not status.is_suspension() {
|
||||
return status
|
||||
}
|
||||
yield? status
|
||||
}
|
||||
}
|
||||
|
||||
pri func encoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8, dwb: roslice base.u8) {
|
||||
var op : base.u32
|
||||
var avail : base.u64
|
||||
var block_size : base.u64
|
||||
@@ -868,6 +916,11 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
var total : base.u64
|
||||
|
||||
this.ensure_initialized!()
|
||||
if (this.quality >= 10) and (this.dict_num_chunks > 0) {
|
||||
// TODO: compound dictionaries at qualities 10 and 11 (the Zopfli
|
||||
// path's dictionary lookups).
|
||||
return "#unsupported encoder quality"
|
||||
}
|
||||
this.hq_extend_var_region!(wb_len: args.workbuf.length())
|
||||
if (this.quality >= 2) and (args.workbuf.length() < this.workbuf_end) {
|
||||
// A workbuf shorter than workbuf_len works if it is long enough for
|
||||
@@ -911,7 +964,7 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
continue
|
||||
}
|
||||
if (this.quality >= 2) and (this.input_pos <> this.last_flush_pos) {
|
||||
status = this.encode_data!(wb: args.workbuf, is_last: false, force_flush: true)
|
||||
status = this.encode_data!(wb: args.workbuf, dwb: args.dwb, is_last: false, force_flush: true)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
@@ -1027,7 +1080,7 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
force_flush = true
|
||||
}
|
||||
this.update_size_hint!(available_in: args.src.length())
|
||||
status = this.encode_data!(wb: args.workbuf, is_last: is_last, force_flush: force_flush)
|
||||
status = this.encode_data!(wb: args.workbuf, dwb: args.dwb, is_last: is_last, force_flush: force_flush)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
@@ -487,10 +487,11 @@ pri func encoder.update_size_hint!(available_in: base.u64) {
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors ExtendLastCommand in encode.c (without a compound dictionary). It
|
||||
// returns how many bytes it extended the last command by: the caller
|
||||
// subtracts that from bytes and adds it to wrapped_last_processed_pos.
|
||||
pri func encoder.extend_last_command!(wb: slice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64) base.u64 {
|
||||
// Mirrors ExtendLastCommand in encode.c (see extend_last_command_compound
|
||||
// for its compound dictionary branch). It returns how many bytes it extended
|
||||
// the last command by: the caller subtracts that from bytes and adds it to
|
||||
// wrapped_last_processed_pos.
|
||||
pri func encoder.extend_last_command!(wb: slice base.u8, dwb: roslice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64) base.u64 {
|
||||
var last : base.u64
|
||||
var max_backward_distance : base.u64
|
||||
var copy_len : base.u32
|
||||
@@ -519,6 +520,11 @@ pri func encoder.extend_last_command!(wb: slice base.u8, bytes: base.u64, wrappe
|
||||
n ~mod+= 1
|
||||
p = (p ~mod+ 1) & 0xFFFF_FFFF
|
||||
}
|
||||
} else {
|
||||
n = this.extend_last_command_compound(wb: args.wb, dwb: args.dwb, bytes: args.bytes,
|
||||
wrapped_last_processed_pos: args.wrapped_last_processed_pos,
|
||||
cmd_dist: cmd_dist, max_distance: max_distance, last_copy_len: last_copy_len)
|
||||
copy_len ~mod+= (n & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
// The copy length is at most the metablock size, and thus
|
||||
// expressible.
|
||||
@@ -876,7 +882,7 @@ pri func encoder.write_meta_block_internal!(wb: slice base.u8, last_flush_pos: b
|
||||
|
||||
// Mirrors EncodeData in encode.c, for qualities 2 to 9. On a new metablock,
|
||||
// it sets the pending output (this.out_etc) to the storage (or tiny_buf).
|
||||
pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush: base.bool) base.status {
|
||||
pri func encoder.encode_data!(wb: slice base.u8, dwb: roslice base.u8, is_last: base.bool, force_flush: base.bool) base.status {
|
||||
var status : base.status
|
||||
var delta : base.u64
|
||||
var bytes : base.u64
|
||||
@@ -941,7 +947,7 @@ pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush
|
||||
}
|
||||
|
||||
if (this.num_cmds > 0) and (this.last_insert_len == 0) {
|
||||
n = this.extend_last_command!(wb: args.wb, bytes: bytes,
|
||||
n = this.extend_last_command!(wb: args.wb, dwb: args.dwb, bytes: bytes,
|
||||
wrapped_last_processed_pos: wrapped_last_processed_pos)
|
||||
bytes ~mod-= n
|
||||
wrapped_last_processed_pos = (wrapped_last_processed_pos ~mod+ n) & 0xFFFF_FFFF
|
||||
@@ -952,7 +958,7 @@ pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush
|
||||
} else if this.quality == 11 {
|
||||
status = this.create_hq_zopfli_backward_references!(wb: args.wb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
} else {
|
||||
status = this.create_backward_references!(wb: args.wb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
status = this.create_backward_references!(wb: args.wb, dwb: args.dwb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
}
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
|
||||
@@ -0,0 +1,568 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// This file mirrors the encoder's raw (prefix) dictionaries: compound
|
||||
// dictionary.c (CreatePreparedDictionary and AttachPreparedDictionary), the
|
||||
// BROTLI_SHARED_DICTIONARY_RAW cases of BrotliEncoderPrepareDictionary and
|
||||
// BrotliEncoderAttachPreparedDictionary in encode.c and the compound
|
||||
// dictionary search of hash.h (FindCompoundDictionaryMatch and
|
||||
// LookupCompoundDictionaryMatch).
|
||||
//
|
||||
// The dictionaries live at the start of the workbuf (see README.md). Each
|
||||
// one (a "chunk", the C code's PreparedDictionary) is, starting at its
|
||||
// dc_src_off workbuf offset:
|
||||
// - the dictionary bytes (the C code's source, dc_size bytes),
|
||||
// - slot_offsets: (1 << dc_slot_bits) u32le values,
|
||||
// - heads: (1 << dc_bucket_bits) u16le values and
|
||||
// - items: dc_num_items u32le values.
|
||||
|
||||
pub status "#unsupported serialized encoder dictionary"
|
||||
|
||||
// ENCODER_DICTIONARY_CHUNKS_MAX mirrors SHARED_BROTLI_MAX_COMPOUND_DICTS in
|
||||
// shared_dictionary.h.
|
||||
pri const ENCODER_DICTIONARY_CHUNKS_MAX : base.u32 = 15
|
||||
|
||||
// Mirrors kPreparedDictionaryHashMul64Long in compound_dictionary.h.
|
||||
pri const PREPARED_DICTIONARY_HASH_MUL_64_LONG : base.u64 = 0x1FE3_5A7B_D357_9BD3
|
||||
|
||||
// add_dictionary attaches a raw (LZ77 prefix) dictionary, like
|
||||
// BrotliEncoderPrepareDictionary (with BROTLI_SHARED_DICTIONARY_RAW)
|
||||
// followed by BrotliEncoderAttachPreparedDictionary in encode.c. It must be
|
||||
// called before the first transform_io call. It can be called up to 15 times
|
||||
// (the C code's SHARED_BROTLI_MAX_COMPOUND_DICTS).
|
||||
//
|
||||
// args.dict_type is DICTIONARY_TYPE_RAW or DICTIONARY_TYPE_SERIALIZED. Like
|
||||
// a default (not BROTLI_EXPERIMENTAL) libbrotlienc build, whose
|
||||
// BrotliEncoderPrepareDictionary returns NULL for a serialized dictionary,
|
||||
// serialized dictionaries are rejected.
|
||||
//
|
||||
// Wuffs structs cannot hold pointers, so the dictionary's bytes are copied
|
||||
// into the workbuf, followed by its prepared hash tables (the C code's
|
||||
// PreparedDictionary), after the dictionaries added so far. args.workbuf must
|
||||
// be at least add_dictionary_workbuf_len(args.dict.length()) bytes long
|
||||
// (preparing the hash tables needs some scratch space after them) and every
|
||||
// later add_dictionary and transform_io call must be passed the same workbuf
|
||||
// (or at least one whose prefix holds the same bytes). After the last
|
||||
// add_dictionary call, workbuf_len gives the workbuf length that
|
||||
// transform_io needs.
|
||||
//
|
||||
// On failure, the encoder's state is unchanged.
|
||||
pub func encoder.add_dictionary!(dict_type: base.u32, dict: roslice base.u8, workbuf: slice base.u8) base.status {
|
||||
var n : base.u64
|
||||
var lo : base.u64
|
||||
var hi : base.u64
|
||||
var bucket_bits : base.u32[..= 22]
|
||||
var slot_bits : base.u32[..= 12]
|
||||
var num_items : base.u64
|
||||
var c : base.u32
|
||||
|
||||
if this.is_initialized {
|
||||
return base."#bad call sequence"
|
||||
} else if args.dict_type == DICTIONARY_TYPE_SERIALIZED {
|
||||
return "#unsupported serialized encoder dictionary"
|
||||
} else if args.dict_type <> DICTIONARY_TYPE_RAW {
|
||||
return base."#bad argument"
|
||||
} else if this.dict_num_chunks >= ENCODER_DICTIONARY_CHUNKS_MAX {
|
||||
// Mirrors AttachPreparedDictionary in compound_dictionary.c.
|
||||
return "#too many compound dictionaries"
|
||||
}
|
||||
n = args.dict.length()
|
||||
if (n > DICT_TOTAL_MAX) or ((this.dict_total_size ~sat+ n) > DICT_TOTAL_MAX) {
|
||||
return "#too much dictionary data"
|
||||
} else if this.add_dictionary_workbuf_len(dict_len: n) > args.workbuf.length() {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
|
||||
// Mirrors CreatePreparedDictionary in compound_dictionary.c.
|
||||
bucket_bits = 17
|
||||
while (((0x10 as base.u64) << bucket_bits) < n) and (bucket_bits < 22) {
|
||||
bucket_bits += 1
|
||||
}
|
||||
if bucket_bits >= 10 {
|
||||
slot_bits = bucket_bits - 10
|
||||
}
|
||||
|
||||
// Copy the dictionary bytes into the workbuf.
|
||||
lo = this.dict_end
|
||||
hi = lo ~mod+ n
|
||||
if (lo > hi) or (hi > args.workbuf.length()) {
|
||||
return "#internal error: inconsistent encoder state"
|
||||
}
|
||||
args.workbuf[lo .. hi].copy_from_slice!(s: args.dict)
|
||||
|
||||
num_items = this.prepare_dictionary!(wb: args.workbuf, src_off: lo, n: n,
|
||||
bucket_bits: bucket_bits, slot_bits: slot_bits)
|
||||
|
||||
// Mirrors AttachPreparedDictionary in compound_dictionary.c.
|
||||
c = this.dict_num_chunks
|
||||
if c >= 15 {
|
||||
return "#internal error: inconsistent encoder state"
|
||||
}
|
||||
this.dc_src_off[c] = lo
|
||||
this.dc_size[c] = n
|
||||
this.dc_bucket_bits[c] = bucket_bits
|
||||
this.dc_slot_bits[c] = slot_bits
|
||||
this.dc_num_items[c] = num_items
|
||||
this.dict_total_size ~mod+= n
|
||||
this.dict_chunk_offsets[c + 1] = this.dict_total_size
|
||||
this.dict_num_chunks = c + 1
|
||||
this.dict_end = ((hi ~mod+ ((4 as base.u64) << slot_bits)) ~mod+
|
||||
((2 as base.u64) << bucket_bits)) ~mod+ (4 ~mod* num_items)
|
||||
return ok
|
||||
}
|
||||
|
||||
// add_dictionary_workbuf_len returns the workbuf length that an
|
||||
// add_dictionary call, for a dictionary of dict_len bytes, needs: the
|
||||
// dictionaries added so far, plus that dictionary's bytes, its hash tables
|
||||
// (assuming the most items) and the scratch space for preparing them. It
|
||||
// returns 0xFFFF_FFFF_FFFF_FFFF if dict_len is too large.
|
||||
pub func encoder.add_dictionary_workbuf_len(dict_len: base.u64) base.u64 {
|
||||
var bucket_bits : base.u32[..= 22]
|
||||
var slot_bits : base.u32[..= 12]
|
||||
var n : base.u64
|
||||
var max_items : base.u64
|
||||
|
||||
if args.dict_len > DICT_TOTAL_MAX {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF
|
||||
}
|
||||
n = args.dict_len
|
||||
bucket_bits = 17
|
||||
while (((0x10 as base.u64) << bucket_bits) < n) and (bucket_bits < 22) {
|
||||
bucket_bits += 1
|
||||
}
|
||||
if bucket_bits >= 10 {
|
||||
slot_bits = bucket_bits - 10
|
||||
}
|
||||
if n > 7 {
|
||||
max_items = n - 7
|
||||
}
|
||||
// The dictionary bytes, slot_offsets, heads and (at most) items, then the
|
||||
// scratch space: num (u16), bucket_heads (u32) and next_bucket (u32).
|
||||
return ((((this.dict_end ~sat+ n) ~sat+ ((4 as base.u64) << slot_bits)) ~sat+
|
||||
((2 as base.u64) << bucket_bits)) ~sat+ (4 ~mod* max_items)) ~sat+
|
||||
(((6 as base.u64) << bucket_bits) ~sat+ (4 ~mod* n))
|
||||
}
|
||||
|
||||
// prepare_dictionary mirrors CreatePreparedDictionaryWithParams in
|
||||
// compound_dictionary.c, with hash_bits 40 and bucket_limit 32, for the n
|
||||
// dictionary bytes at args.wb[args.src_off ..]. It writes the slot_offsets,
|
||||
// heads and items arrays after those bytes (with the scratch space after the
|
||||
// largest possible items array) and returns the number of items.
|
||||
pri func encoder.prepare_dictionary!(wb: slice base.u8, src_off: base.u64, n: base.u64, bucket_bits: base.u32[..= 22], slot_bits: base.u32[..= 12]) base.u64 {
|
||||
var num_slots : base.u64
|
||||
var num_buckets : base.u64
|
||||
var slot_mask : base.u64
|
||||
var slot_off : base.u64
|
||||
var heads_off : base.u64
|
||||
var items_off : base.u64
|
||||
var num_off : base.u64
|
||||
var bh_off : base.u64
|
||||
var nb_off : base.u64
|
||||
var max_items : base.u64
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var h : base.u64
|
||||
var key : base.u64
|
||||
var count : base.u32
|
||||
var limit : base.u32
|
||||
var size : base.u32
|
||||
var overflow : base.bool
|
||||
var total_items : base.u64
|
||||
var slot : base.u64
|
||||
var cursor : base.u64
|
||||
var pos : base.u64
|
||||
var k : base.u32
|
||||
|
||||
num_slots = (1 as base.u64) << args.slot_bits
|
||||
num_buckets = (1 as base.u64) << args.bucket_bits
|
||||
slot_mask = num_slots - 1
|
||||
if args.n > 7 {
|
||||
max_items = args.n - 7
|
||||
}
|
||||
slot_off = args.src_off ~mod+ args.n
|
||||
heads_off = slot_off ~mod+ (4 ~mod* num_slots)
|
||||
items_off = heads_off ~mod+ (2 ~mod* num_buckets)
|
||||
num_off = items_off ~mod+ (4 ~mod* max_items)
|
||||
bh_off = num_off ~mod+ (2 ~mod* num_buckets)
|
||||
nb_off = bh_off ~mod+ (4 ~mod* num_buckets)
|
||||
|
||||
// Step 1: create "bloated" hasher.
|
||||
this.fill_bytes!(s: args.wb, i: num_off, n: 2 ~mod* num_buckets, v: 0)
|
||||
i = 0
|
||||
while (i ~mod+ 7) < args.n {
|
||||
h = (this.load64le(s: args.wb, i: args.src_off ~mod+ i) & 0xFF_FFFF_FFFF) ~mod*
|
||||
PREPARED_DICTIONARY_HASH_MUL_64_LONG
|
||||
key = (h >> 32) >> (32 - args.bucket_bits)
|
||||
count = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* key)) as base.u32
|
||||
if count == 0 {
|
||||
this.poke_u32!(s: args.wb, i: nb_off ~mod+ (4 ~mod* i), v: 0xFFFF_FFFF)
|
||||
} else {
|
||||
this.poke_u32!(s: args.wb, i: nb_off ~mod+ (4 ~mod* i),
|
||||
v: this.load32le(s: args.wb, i: bh_off ~mod+ (4 ~mod* key)))
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: bh_off ~mod+ (4 ~mod* key), v: (i & 0xFFFF_FFFF) as base.u32)
|
||||
count ~mod+= 1
|
||||
if count > 32 {
|
||||
count = 32
|
||||
}
|
||||
this.poke_u16!(s: args.wb, i: num_off ~mod+ (2 ~mod* key), v: (count & 0xFFFF) as base.u16)
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
// Step 2: find slot limits.
|
||||
i = 0
|
||||
while i < num_slots {
|
||||
limit = 32
|
||||
while true {
|
||||
count = 0
|
||||
overflow = false
|
||||
j = i
|
||||
while j < num_buckets {
|
||||
size = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* j)) as base.u32
|
||||
// Last chain may span behind 64K limit; overflow happens
|
||||
// only if we are about to use 0xFFFF+ as item offset.
|
||||
if count >= 0xFFFF {
|
||||
overflow = true
|
||||
break
|
||||
}
|
||||
if size > limit {
|
||||
size = limit
|
||||
}
|
||||
count ~mod+= size
|
||||
j ~mod+= num_slots
|
||||
}
|
||||
if not overflow {
|
||||
this.dp_slot_size[i & 4095] = count
|
||||
total_items ~mod+= count as base.u64
|
||||
break
|
||||
} else if limit == 0 {
|
||||
// Unreachable: with a zero limit, count stays zero.
|
||||
this.dp_slot_size[i & 4095] = 0
|
||||
break
|
||||
}
|
||||
limit -= 1
|
||||
}
|
||||
this.dp_slot_limit[i & 4095] = limit
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
// Step 3: transfer data to "slim" hasher.
|
||||
total_items = 0
|
||||
i = 0
|
||||
while i < num_slots {
|
||||
this.poke_u32!(s: args.wb, i: slot_off ~mod+ (4 ~mod* i), v: (total_items & 0xFFFF_FFFF) as base.u32)
|
||||
this.dp_slot_off[i & 4095] = (total_items & 0xFFFF_FFFF) as base.u32
|
||||
total_items ~mod+= this.dp_slot_size[i & 4095] as base.u64
|
||||
this.dp_slot_size[i & 4095] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
i = 0
|
||||
while i < num_buckets {
|
||||
slot = i & slot_mask & 4095
|
||||
count = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* i)) as base.u32
|
||||
if count > this.dp_slot_limit[slot] {
|
||||
count = this.dp_slot_limit[slot]
|
||||
}
|
||||
if count == 0 {
|
||||
this.poke_u16!(s: args.wb, i: heads_off ~mod+ (2 ~mod* i), v: 0xFFFF)
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
cursor = this.dp_slot_size[slot] as base.u64
|
||||
this.poke_u16!(s: args.wb, i: heads_off ~mod+ (2 ~mod* i), v: (cursor & 0xFFFF) as base.u16)
|
||||
cursor ~mod+= this.dp_slot_off[slot] as base.u64
|
||||
this.dp_slot_size[slot] ~mod+= count
|
||||
pos = this.load32le(s: args.wb, i: bh_off ~mod+ (4 ~mod* i)) as base.u64
|
||||
k = 0
|
||||
while k < count {
|
||||
this.poke_u32!(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor), v: (pos & 0xFFFF_FFFF) as base.u32)
|
||||
cursor ~mod+= 1
|
||||
pos = this.load32le(s: args.wb, i: nb_off ~mod+ (4 ~mod* pos)) as base.u64
|
||||
k ~mod+= 1
|
||||
}
|
||||
cursor ~mod-= 1
|
||||
this.poke_u32!(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor),
|
||||
v: this.load32le(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor)) | 0x8000_0000)
|
||||
i ~mod+= 1
|
||||
}
|
||||
return total_items
|
||||
}
|
||||
|
||||
// compound_dictionary_enabled returns whether BrotliCreateBackwardReferences
|
||||
// in backward_references.c uses its ENABLE_COMPOUND_DICTIONARY variant: when
|
||||
// any dictionary is attached and the hasher is H5, H6, H58, H68, H40, H41,
|
||||
// H42, H55 or H65 (H55 and H65 are the composite hashers H54 and H6 plus a
|
||||
// rolling hasher). The other hashers (H2, H3, H4, H54 and H35) ignore the
|
||||
// compound dictionary (but not its total size).
|
||||
pri func encoder.compound_dictionary_enabled() base.bool {
|
||||
var htype : base.u32
|
||||
|
||||
if this.dict_num_chunks == 0 {
|
||||
return false
|
||||
}
|
||||
htype = this.hasher_type
|
||||
return (htype == 5) or (htype == 6) or (htype == 58) or (htype == 68) or
|
||||
(htype == 40) or (htype == 41) or (htype == 42) or
|
||||
((htype == 54) and (this.hr_jump <> 0))
|
||||
}
|
||||
|
||||
// Mirrors LookupCompoundDictionaryMatch in hash.h. args.data is the ring
|
||||
// buffer (the C code's data) and args.dwb holds the dictionaries.
|
||||
pri func encoder.lookup_compound_dictionary_match!(data: roslice base.u8, dwb: roslice base.u8, cur_ix: base.u64, max_length: base.u64, max_ring_buffer_distance: base.u64, max_distance: base.u64) {
|
||||
var base_offset : base.u64
|
||||
var d : base.u32
|
||||
|
||||
base_offset = args.max_ring_buffer_distance ~mod+ this.dict_total_size
|
||||
d = 0
|
||||
while d < this.dict_num_chunks {
|
||||
assert d < 15 via "a < b: a < c; c <= b"(c: this.dict_num_chunks)
|
||||
this.find_compound_dictionary_match!(data: args.data, dwb: args.dwb, d: d,
|
||||
cur_ix: args.cur_ix, max_length: args.max_length,
|
||||
distance_offset: base_offset ~mod- this.dict_chunk_offsets[d],
|
||||
max_distance: args.max_distance)
|
||||
d += 1
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FindCompoundDictionaryMatch in hash.h, for the d'th chunk. The
|
||||
// search result is in (and starts from) the this.sr_etc fields.
|
||||
pri func encoder.find_compound_dictionary_match!(data: roslice base.u8, dwb: roslice base.u8, d: base.u32[..= 14], cur_ix: base.u64, max_length: base.u64, distance_offset: base.u64, max_distance: base.u64) {
|
||||
var source_size : base.u64
|
||||
var boundary : base.u64
|
||||
var src_off : base.u64
|
||||
var slot_off : base.u64
|
||||
var heads_off : base.u64
|
||||
var items_off : base.u64
|
||||
var source : roslice base.u8
|
||||
var cur : roslice base.u8
|
||||
var cur_ix_masked : base.u64
|
||||
var mask : base.u64
|
||||
var best_score : base.u64
|
||||
var best_len : base.u64
|
||||
var i : base.u32
|
||||
var h : base.u64
|
||||
var key : base.u64
|
||||
var slot : base.u64
|
||||
var head : base.u64
|
||||
var chain : base.u64
|
||||
var item : base.u32
|
||||
var distance : base.u64
|
||||
var offset : base.u64
|
||||
var limit : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
var num_items : base.u64
|
||||
var bb : base.u32[..= 22]
|
||||
var sb : base.u32[..= 12]
|
||||
|
||||
bb = this.dc_bucket_bits[args.d].min(no_more_than: 22)
|
||||
sb = this.dc_slot_bits[args.d].min(no_more_than: 12)
|
||||
num_items = this.dc_num_items[args.d]
|
||||
source_size = this.dc_size[args.d]
|
||||
boundary = args.distance_offset ~mod- source_size
|
||||
src_off = this.dc_src_off[args.d]
|
||||
slot_off = src_off ~mod+ source_size
|
||||
heads_off = slot_off ~mod+ ((4 as base.u64) << sb)
|
||||
items_off = heads_off ~mod+ ((2 as base.u64) << bb)
|
||||
if (src_off > slot_off) or (slot_off > args.dwb.length()) {
|
||||
return nothing
|
||||
}
|
||||
source = args.dwb[src_off .. slot_off]
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
cur_ix_masked = args.cur_ix & mask
|
||||
if cur_ix_masked > args.data.length() {
|
||||
return nothing
|
||||
}
|
||||
cur = args.data[cur_ix_masked ..]
|
||||
best_score = this.sr_score
|
||||
best_len = this.sr_len
|
||||
h = (this.load64le(s: args.data, i: cur_ix_masked) & 0xFF_FFFF_FFFF) ~mod*
|
||||
PREPARED_DICTIONARY_HASH_MUL_64_LONG
|
||||
key = (h >> 32) >> (32 - bb)
|
||||
slot = key & (((1 as base.u64) << sb) - 1)
|
||||
head = this.peek_u16(s: args.dwb, i: heads_off ~mod+ (2 ~mod* key)) as base.u64
|
||||
chain = (this.load32le(s: args.dwb, i: slot_off ~mod+ (4 ~mod* slot)) as base.u64) ~mod+ head
|
||||
item = 0
|
||||
if head == 0xFFFF {
|
||||
item = 1
|
||||
}
|
||||
|
||||
i = 0
|
||||
while i < 4 {
|
||||
distance = this.dist_cache_u64(i: i)
|
||||
if (distance <= boundary) or (distance > args.distance_offset) {
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
offset = args.distance_offset ~mod- distance
|
||||
limit = source_size ~mod- offset
|
||||
limit = limit.min(no_more_than: args.max_length)
|
||||
len = 0
|
||||
if offset <= source.length() {
|
||||
len = this.find_match_length_2(a: source[offset ..], b: cur, limit: limit)
|
||||
}
|
||||
if len >= 2 {
|
||||
score = this.backward_reference_score_using_last_distance(copy_length: len)
|
||||
if best_score < score {
|
||||
if i <> 0 {
|
||||
score ~mod-= this.backward_reference_penalty_using_last_distance(distance_short_code: i as base.u64)
|
||||
}
|
||||
if best_score < score {
|
||||
best_score = score
|
||||
if len > best_len {
|
||||
best_len = len
|
||||
}
|
||||
this.sr_len = len
|
||||
this.sr_len_code_delta = 0
|
||||
this.sr_distance = distance
|
||||
this.sr_score = best_score
|
||||
}
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
// we require matches of len >4, so increase best_len to 3, so we can
|
||||
// compare 4 bytes all the time.
|
||||
if best_len < 3 {
|
||||
best_len = 3
|
||||
}
|
||||
while item == 0 {
|
||||
if chain >= num_items {
|
||||
// Unreachable: every chain ends with a 0x8000_0000 bit.
|
||||
break
|
||||
}
|
||||
item = this.load32le(s: args.dwb, i: items_off ~mod+ (4 ~mod* chain))
|
||||
chain ~mod+= 1
|
||||
offset = (item & 0x7FFF_FFFF) as base.u64
|
||||
item &= 0x8000_0000
|
||||
distance = args.distance_offset ~mod- offset
|
||||
limit = source_size ~mod- offset
|
||||
limit = limit.min(no_more_than: args.max_length)
|
||||
if distance > args.max_distance {
|
||||
continue
|
||||
}
|
||||
if ((cur_ix_masked ~mod+ best_len) > mask) or (best_len >= limit) or
|
||||
(this.load32le(s: args.data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3) <>
|
||||
this.load32le(s: source, i: (offset ~mod+ best_len) ~mod- 3)) {
|
||||
continue
|
||||
}
|
||||
len = 0
|
||||
if offset <= source.length() {
|
||||
len = this.find_match_length_2(a: source[offset ..], b: cur, limit: limit)
|
||||
}
|
||||
if len >= 4 {
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: distance)
|
||||
if best_score < score {
|
||||
best_score = score
|
||||
best_len = len
|
||||
this.sr_len = best_len
|
||||
this.sr_len_code_delta = 0
|
||||
this.sr_distance = distance
|
||||
this.sr_score = best_score
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// find_match_length_2 mirrors FindMatchLengthWithLimit in
|
||||
// find_match_length.h: the length of the common prefix of a and b, up to
|
||||
// limit bytes.
|
||||
pri func encoder.find_match_length_2(a: roslice base.u8, b: roslice base.u8, limit: base.u64) base.u64 {
|
||||
var matched : base.u64
|
||||
var n : base.u64
|
||||
var a : roslice base.u8
|
||||
var b : roslice base.u8
|
||||
var x : base.u64
|
||||
|
||||
a = args.a
|
||||
b = args.b
|
||||
n = args.limit
|
||||
while (n >= 8) and (a.length() >= 8) and (b.length() >= 8) {
|
||||
x = a.peek_u64le() ^ b.peek_u64le()
|
||||
if x <> 0 {
|
||||
while (x & 0xFF) == 0 {
|
||||
matched ~mod+= 1
|
||||
x >>= 8
|
||||
}
|
||||
return matched
|
||||
}
|
||||
matched ~mod+= 8
|
||||
n -= 8
|
||||
a = a[8 ..]
|
||||
b = b[8 ..]
|
||||
}
|
||||
while (n > 0) and (a.length() > 0) and (b.length() > 0) {
|
||||
if a[0] <> b[0] {
|
||||
break
|
||||
}
|
||||
matched ~mod+= 1
|
||||
n -= 1
|
||||
a = a[1 ..]
|
||||
b = b[1 ..]
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
// extend_last_command_compound mirrors the compound dictionary branch of
|
||||
// ExtendLastCommand in encode.c: extending the last command's copy (of
|
||||
// distance cmd_dist, beyond max_distance) from the compound dictionary. It
|
||||
// returns how many bytes it extended the copy by.
|
||||
pri func encoder.extend_last_command_compound(wb: roslice base.u8, dwb: roslice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64, cmd_dist: base.u64, max_distance: base.u64, last_copy_len: base.u64) base.u64 {
|
||||
var address : base.u64
|
||||
var br_index : base.u32
|
||||
var br_offset : base.u64
|
||||
var chunk_length : base.u64
|
||||
var n : base.u64
|
||||
var p : base.u64
|
||||
|
||||
if ((args.cmd_dist ~mod- args.max_distance) ~mod- 1) >= this.dict_total_size {
|
||||
return 0
|
||||
} else if args.last_copy_len >= (args.cmd_dist ~mod- args.max_distance) {
|
||||
return 0
|
||||
}
|
||||
address = (this.dict_total_size ~mod- (args.cmd_dist ~mod- args.max_distance)) ~mod+ args.last_copy_len
|
||||
br_index = 0
|
||||
while (br_index < 15) and (address >= this.dict_chunk_offsets[br_index + 1]) {
|
||||
br_index += 1
|
||||
}
|
||||
if br_index >= 15 {
|
||||
return 0
|
||||
}
|
||||
br_offset = address ~mod- this.dict_chunk_offsets[br_index]
|
||||
chunk_length = this.dict_chunk_offsets[br_index + 1] ~mod- this.dict_chunk_offsets[br_index]
|
||||
p = args.wrapped_last_processed_pos
|
||||
while n < args.bytes {
|
||||
// The C code reads past an empty chunk's end. Here, such a read
|
||||
// stops the extension (see README.md).
|
||||
if (br_offset >= chunk_length) or (br_index >= 15) {
|
||||
break
|
||||
}
|
||||
if this.rb_byte(wb: args.wb, pos: p) <>
|
||||
this.byte_at(s: args.dwb, i: this.dc_src_off[br_index] ~mod+ br_offset) {
|
||||
break
|
||||
}
|
||||
n ~mod+= 1
|
||||
p = (p ~mod+ 1) & 0xFFFF_FFFF
|
||||
br_offset ~mod+= 1
|
||||
if br_offset == chunk_length {
|
||||
br_index += 1
|
||||
br_offset = 0
|
||||
if (br_index < this.dict_num_chunks) and (br_index < 15) {
|
||||
chunk_length = this.dict_chunk_offsets[br_index + 1] ~mod- this.dict_chunk_offsets[br_index]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "brotli/decode.h"
|
||||
#include "brotli/encode.h"
|
||||
#include "brotli/shared_dictionary.h"
|
||||
|
||||
// mimic_brotli_decode_error_code is like mimic_brotli_decode but also stores
|
||||
// the libbrotlidec error code (a BrotliDecoderErrorCode value) in
|
||||
@@ -91,6 +92,20 @@ mimic_brotli_decode(wuffs_base__io_buffer* dst,
|
||||
//
|
||||
// Each BrotliEncoderCompressStream call gets at most dst_chunk bytes of dst
|
||||
// (or all of dst's writable bytes, if dst_chunk is zero).
|
||||
const char* //
|
||||
mimic_brotli_encode_with_dictionaries(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
const uint32_t* param_keys,
|
||||
const uint32_t* param_values,
|
||||
size_t num_params,
|
||||
size_t num_dicts,
|
||||
const uint32_t* dict_types,
|
||||
const wuffs_base__slice_u8* dicts,
|
||||
const uint32_t* step_ops,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk);
|
||||
|
||||
const char* //
|
||||
mimic_brotli_encode(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
@@ -101,15 +116,62 @@ mimic_brotli_encode(wuffs_base__io_buffer* dst,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk) {
|
||||
return mimic_brotli_encode_with_dictionaries(
|
||||
dst, src, param_keys, param_values, num_params, 0, NULL, NULL, step_ops,
|
||||
step_lens, num_steps, dst_chunk);
|
||||
}
|
||||
|
||||
// mimic_brotli_encode_with_dictionaries is like mimic_brotli_encode but it
|
||||
// also prepares (with BrotliEncoderPrepareDictionary) and attaches (with
|
||||
// BrotliEncoderAttachPreparedDictionary), after setting the parameters and
|
||||
// before the first step, num_dicts dictionaries. Each dict_types[i] is a
|
||||
// BrotliSharedDictionaryType value: 0 for BROTLI_SHARED_DICTIONARY_RAW and 1
|
||||
// for BROTLI_SHARED_DICTIONARY_SERIALIZED. The quality passed to
|
||||
// BrotliEncoderPrepareDictionary is the last BROTLI_PARAM_QUALITY param (or
|
||||
// the default, 11), although the C code ignores it for raw dictionaries.
|
||||
//
|
||||
// libbrotlienc only supports serialized dictionaries when built with
|
||||
// BROTLI_EXPERIMENTAL (which is not the default). When preparing or
|
||||
// attaching a dictionary fails, this function returns
|
||||
// mimic_brotli_prepare_dictionary_failed_message or
|
||||
// mimic_brotli_attach_prepared_dictionary_failed_message.
|
||||
const char mimic_brotli_prepare_dictionary_failed_message[] =
|
||||
"libbrotlienc: BrotliEncoderPrepareDictionary failed";
|
||||
const char mimic_brotli_attach_prepared_dictionary_failed_message[] =
|
||||
"libbrotlienc: BrotliEncoderAttachPreparedDictionary failed";
|
||||
|
||||
#define MIMIC_BROTLI_ENCODE_MAX_DICTS 64
|
||||
|
||||
const char* //
|
||||
mimic_brotli_encode_with_dictionaries(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
const uint32_t* param_keys,
|
||||
const uint32_t* param_values,
|
||||
size_t num_params,
|
||||
size_t num_dicts,
|
||||
const uint32_t* dict_types,
|
||||
const wuffs_base__slice_u8* dicts,
|
||||
const uint32_t* step_ops,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk) {
|
||||
if (BrotliEncoderVersion() != 0x1002001) {
|
||||
return "libbrotlienc: unexpected version (want v1.2.1)";
|
||||
} else if (num_dicts > MIMIC_BROTLI_ENCODE_MAX_DICTS) {
|
||||
return "mimic_brotli_encode: too many dictionaries";
|
||||
}
|
||||
BrotliEncoderPreparedDictionary* prepared[MIMIC_BROTLI_ENCODE_MAX_DICTS];
|
||||
size_t num_prepared = 0;
|
||||
BrotliEncoderState* state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
|
||||
if (!state) {
|
||||
return "libbrotlienc: BrotliEncoderCreateInstance failed";
|
||||
}
|
||||
const char* ret = NULL;
|
||||
int quality = 11;
|
||||
for (size_t j = 0; j < num_params; j++) {
|
||||
if (param_keys[j] == BROTLI_PARAM_QUALITY) {
|
||||
quality = (int)(param_values[j]);
|
||||
}
|
||||
if (!BrotliEncoderSetParameter(state,
|
||||
(BrotliEncoderParameter)(param_keys[j]),
|
||||
param_values[j])) {
|
||||
@@ -117,6 +179,20 @@ mimic_brotli_encode(wuffs_base__io_buffer* dst,
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
for (size_t j = 0; j < num_dicts; j++) {
|
||||
prepared[j] = BrotliEncoderPrepareDictionary(
|
||||
(BrotliSharedDictionaryType)(dict_types[j]), dicts[j].len,
|
||||
dicts[j].ptr, quality, NULL, NULL, NULL);
|
||||
if (!prepared[j]) {
|
||||
ret = mimic_brotli_prepare_dictionary_failed_message;
|
||||
goto cleanup;
|
||||
}
|
||||
num_prepared++;
|
||||
if (!BrotliEncoderAttachPreparedDictionary(state, prepared[j])) {
|
||||
ret = mimic_brotli_attach_prepared_dictionary_failed_message;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < num_steps; i++) {
|
||||
size_t avail_in = step_lens[i];
|
||||
@@ -156,13 +232,14 @@ mimic_brotli_encode(wuffs_base__io_buffer* dst,
|
||||
|
||||
cleanup:
|
||||
BrotliEncoderDestroyInstance(state);
|
||||
for (size_t j = 0; j < num_prepared; j++) {
|
||||
BrotliEncoderDestroyPreparedDictionary(prepared[j]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// ---------------- Large Window and Dictionaries
|
||||
|
||||
#include "brotli/shared_dictionary.h"
|
||||
|
||||
// mimic_brotli_decode_with_options is like mimic_brotli_decode_error_code but
|
||||
// it also sets BROTLI_DECODER_PARAM_LARGE_WINDOW (if large_window is true) and
|
||||
// attaches dictionaries (with BrotliDecoderAttachDictionary). Each
|
||||
|
||||
+712
-15
@@ -854,6 +854,20 @@ brotli_enc_quirk_key(uint32_t param) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* //
|
||||
wuffs_brotli_encode_plan_with_dictionaries(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
const uint32_t* param_keys,
|
||||
const uint32_t* param_values,
|
||||
size_t num_params,
|
||||
size_t num_dicts,
|
||||
const uint32_t* dict_types,
|
||||
const wuffs_base__slice_u8* dicts,
|
||||
const uint32_t* step_ops,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk);
|
||||
|
||||
// wuffs_brotli_encode_plan is the Wuffs equivalent of mimic_brotli_encode
|
||||
// (in test/c/mimiclib/brotli.c).
|
||||
const char* //
|
||||
@@ -866,6 +880,29 @@ wuffs_brotli_encode_plan(wuffs_base__io_buffer* dst,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk) {
|
||||
return wuffs_brotli_encode_plan_with_dictionaries(
|
||||
dst, src, param_keys, param_values, num_params, 0, NULL, NULL, step_ops,
|
||||
step_lens, num_steps, dst_chunk);
|
||||
}
|
||||
|
||||
// wuffs_brotli_encode_plan_with_dictionaries is the Wuffs equivalent of
|
||||
// mimic_brotli_encode_with_dictionaries: it adds (with add_dictionary) the
|
||||
// num_dicts dictionaries after setting the quirks and before the first step.
|
||||
// Each dict_types[i] is WUFFS_BROTLI__DICTIONARY_TYPE_RAW or
|
||||
// WUFFS_BROTLI__DICTIONARY_TYPE_SERIALIZED.
|
||||
const char* //
|
||||
wuffs_brotli_encode_plan_with_dictionaries(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
const uint32_t* param_keys,
|
||||
const uint32_t* param_values,
|
||||
size_t num_params,
|
||||
size_t num_dicts,
|
||||
const uint32_t* dict_types,
|
||||
const wuffs_base__slice_u8* dicts,
|
||||
const uint32_t* step_ops,
|
||||
const size_t* step_lens,
|
||||
size_t num_steps,
|
||||
size_t dst_chunk) {
|
||||
// The encoder struct is large (more than 1 MiB), mostly arrays that do not
|
||||
// need zero-initializing, so don't use wuffs_brotli__encoder__alloc.
|
||||
wuffs_brotli__encoder* enc = malloc(sizeof__wuffs_brotli__encoder());
|
||||
@@ -891,25 +928,48 @@ wuffs_brotli_encode_plan(wuffs_base__io_buffer* dst,
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
wuffs_base__range_ii_u64 workbuf_len =
|
||||
wuffs_brotli__encoder__workbuf_len(enc);
|
||||
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
|
||||
// Use the minimum, so that the qualities 10 and 11 practical bound (see
|
||||
// the README) is exercised. At other qualities, min_incl == max_incl.
|
||||
// Large-window brotli at lgwin 30 needs more than 2 GiB.
|
||||
if ((workbuf_len.min_incl > (3ull << 30)) ||
|
||||
(workbuf_len.min_incl > SIZE_MAX)) {
|
||||
ret = "workbuf_len is too large";
|
||||
goto cleanup;
|
||||
} else if (workbuf_len.min_incl > g_work_slice_u8.len) {
|
||||
big_workbuf = malloc((size_t)workbuf_len.min_incl);
|
||||
if (!big_workbuf) {
|
||||
ret = "malloc failed";
|
||||
workbuf.len = 0;
|
||||
size_t big_workbuf_len = 0;
|
||||
for (size_t j = 0; j <= num_dicts; j++) {
|
||||
// Each add_dictionary call needs add_dictionary_workbuf_len bytes and
|
||||
// transform_io needs workbuf_len bytes, with the same dictionary bytes
|
||||
// at the start.
|
||||
uint64_t n = (j < num_dicts)
|
||||
? wuffs_brotli__encoder__add_dictionary_workbuf_len(
|
||||
enc, dicts[j].len)
|
||||
: wuffs_brotli__encoder__workbuf_len(enc).min_incl;
|
||||
// workbuf_len's minimum is used so that the qualities 10 and 11
|
||||
// practical bound (see the README) is exercised. At other qualities,
|
||||
// min_incl == max_incl. Large-window brotli at lgwin 30 needs more
|
||||
// than 2 GiB.
|
||||
if ((n > (3ull << 30)) || (n > SIZE_MAX)) {
|
||||
ret = "workbuf_len is too large";
|
||||
goto cleanup;
|
||||
} else if ((n > g_work_slice_u8.len) && (n > big_workbuf_len)) {
|
||||
uint8_t* p = realloc(big_workbuf, (size_t)n);
|
||||
if (!p) {
|
||||
ret = "malloc failed";
|
||||
goto cleanup;
|
||||
}
|
||||
if (!big_workbuf && workbuf.len) {
|
||||
// Keep the dictionaries added so far.
|
||||
memcpy(p, g_work_slice_u8.ptr, workbuf.len);
|
||||
}
|
||||
big_workbuf = p;
|
||||
big_workbuf_len = (size_t)n;
|
||||
workbuf.ptr = big_workbuf;
|
||||
}
|
||||
workbuf.len = (size_t)n;
|
||||
if (j < num_dicts) {
|
||||
wuffs_base__status status = wuffs_brotli__encoder__add_dictionary(
|
||||
enc, dict_types[j], dicts[j], workbuf);
|
||||
if (status.repr) {
|
||||
ret = status.repr;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
workbuf.ptr = big_workbuf;
|
||||
}
|
||||
workbuf.len = (size_t)workbuf_len.min_incl;
|
||||
|
||||
for (size_t i = 0; i < num_steps; i++) {
|
||||
size_t end = src->meta.ri + step_lens[i];
|
||||
@@ -1467,6 +1527,33 @@ test_wuffs_brotli_encode_parameters() {
|
||||
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_workbuf_length,
|
||||
"short workbuf at quality 10..11");
|
||||
}
|
||||
// Compound dictionaries are not supported (yet) at qualities 10 and 11.
|
||||
{
|
||||
free(enc);
|
||||
enc = wuffs_brotli__encoder__alloc();
|
||||
if (!enc) {
|
||||
RETURN_FAIL("alloc failed");
|
||||
}
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LGWIN, 16);
|
||||
static const char dict_bytes[] = "a raw dictionary, for quality 11";
|
||||
wuffs_base__slice_u8 d = wuffs_base__make_slice_u8(
|
||||
(uint8_t*)dict_bytes, sizeof dict_bytes - 1);
|
||||
uint64_t n = wuffs_brotli__encoder__add_dictionary_workbuf_len(enc, d.len);
|
||||
BROTLI_ENC_CHECK(n <= g_work_slice_u8.len, "add_dictionary_workbuf_len");
|
||||
status = wuffs_brotli__encoder__add_dictionary(
|
||||
enc, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, d,
|
||||
wuffs_base__make_slice_u8(g_work_slice_u8.ptr, (size_t)n));
|
||||
BROTLI_ENC_CHECK(!status.repr, "add_dictionary at quality 11");
|
||||
src = make_io_buffer_from_string("abc", 3);
|
||||
dst.meta.wi = 0;
|
||||
status = wuffs_brotli__encoder__transform_io(enc, &dst, &src,
|
||||
g_work_slice_u8);
|
||||
BROTLI_ENC_CHECK(
|
||||
status.repr == wuffs_brotli__error__unsupported_encoder_quality,
|
||||
"dictionary at quality 11");
|
||||
}
|
||||
src = make_io_buffer_from_string("", 0);
|
||||
dst.meta.wi = 0;
|
||||
src = make_io_buffer_from_string("", 0);
|
||||
dst.meta.wi = 0;
|
||||
|
||||
@@ -3639,6 +3726,610 @@ test_mimic_brotli_encode_large_window_far() {
|
||||
|
||||
#endif // WUFFS_MIMIC
|
||||
|
||||
// ---------------- Brotli Encoder Dictionary Tests
|
||||
|
||||
// brotli_enc_load_file returns a malloc'ed copy of the named file (of up to
|
||||
// 1 MiB) and sets *len to its length.
|
||||
uint8_t* //
|
||||
brotli_enc_load_file(const char* filename, size_t* len) {
|
||||
uint8_t* buf = malloc(1 << 20);
|
||||
if (!buf) {
|
||||
return NULL;
|
||||
}
|
||||
wuffs_base__io_buffer b = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8(buf, 1 << 20),
|
||||
});
|
||||
if (read_file(&b, filename)) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
*len = b.meta.wi;
|
||||
return buf;
|
||||
}
|
||||
|
||||
// brotli_enc_config_has_param returns whether c sets the key param to a
|
||||
// non-zero value.
|
||||
bool //
|
||||
brotli_enc_config_has_param(const brotli_enc_config* c, uint32_t key) {
|
||||
bool ret = false;
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
if (c->param_keys[j] == key) {
|
||||
ret = c->param_values[j] != 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// wuffs_brotli_decode_with_raw_dictionaries decodes src to dst with Wuffs'
|
||||
// std/brotli decoder, with the given raw dictionaries attached.
|
||||
const char* //
|
||||
wuffs_brotli_decode_with_raw_dictionaries(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__io_buffer* src,
|
||||
bool large_window,
|
||||
size_t num_dicts,
|
||||
const wuffs_base__slice_u8* dicts) {
|
||||
wuffs_brotli__decoder dec;
|
||||
CHECK_STATUS("initialize",
|
||||
wuffs_brotli__decoder__initialize(
|
||||
&dec, sizeof dec, WUFFS_VERSION,
|
||||
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
|
||||
CHECK_STATUS("set_quirk",
|
||||
wuffs_brotli__decoder__set_quirk(
|
||||
&dec, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, large_window));
|
||||
size_t total = 0;
|
||||
for (size_t j = 0; j < num_dicts; j++) {
|
||||
total += dicts[j].len;
|
||||
}
|
||||
size_t n = total + (large_window ? (1 << 30) : (1 << 24)) + 542;
|
||||
uint8_t* p = malloc(n);
|
||||
if (!p) {
|
||||
return "malloc failed";
|
||||
}
|
||||
wuffs_base__slice_u8 workbuf = wuffs_base__make_slice_u8(p, n);
|
||||
const char* ret = NULL;
|
||||
for (size_t j = 0; j < num_dicts; j++) {
|
||||
ret = wuffs_brotli__decoder__add_dictionary(
|
||||
&dec, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, dicts[j], workbuf)
|
||||
.repr;
|
||||
if (ret) {
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
if (wuffs_brotli__decoder__workbuf_len(&dec).max_incl > n) {
|
||||
ret = "decoder workbuf_len is too large";
|
||||
goto cleanup;
|
||||
}
|
||||
ret = wuffs_brotli__decoder__transform_io(&dec, dst, src, workbuf).repr;
|
||||
cleanup:
|
||||
free(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// do_test_brotli_encode_dictionary encodes data[:data_len] with the plan's
|
||||
// config c and the given raw dictionaries. Unless the config sets a stream
|
||||
// offset (whose output is the continuation of another stream), it checks
|
||||
// that Wuffs' decoder (and, if mimic is true, libbrotlidec), with the same
|
||||
// dictionaries, decodes the result back to data. If mimic is true, it also
|
||||
// checks that libbrotlienc's output is the same. It sets *compressed_len.
|
||||
const char* //
|
||||
do_test_brotli_encode_dictionary(const char* name,
|
||||
const brotli_enc_config* c,
|
||||
const uint8_t* data,
|
||||
size_t data_len,
|
||||
size_t num_dicts,
|
||||
const wuffs_base__slice_u8* dicts,
|
||||
bool mimic,
|
||||
size_t* compressed_len) {
|
||||
uint32_t dict_types[16] = {0};
|
||||
for (size_t j = 0; j < 16; j++) {
|
||||
dict_types[j] = WUFFS_BROTLI__DICTIONARY_TYPE_RAW;
|
||||
}
|
||||
if (num_dicts > 16) {
|
||||
return "too many dictionaries";
|
||||
}
|
||||
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||
.data = g_src_slice_u8,
|
||||
});
|
||||
CHECK_STRING(make_brotli_enc_plan(&src, c, data, data_len));
|
||||
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
|
||||
.data = g_have_slice_u8,
|
||||
});
|
||||
wuffs_base__io_buffer src0 = src;
|
||||
const char* status = wuffs_brotli_encode_plan_with_dictionaries(
|
||||
&have, &src0, c->param_keys, c->param_values, c->num_params, num_dicts,
|
||||
dict_types, dicts, g_brotli_enc_step_ops, g_brotli_enc_step_lens,
|
||||
g_brotli_enc_num_steps, c->dst_chunk);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: wuffs encode: %s", name, status);
|
||||
}
|
||||
have.meta.closed = true;
|
||||
*compressed_len = have.meta.wi;
|
||||
bool lw = brotli_enc_config_has_param(c, BROTLI_ENC_PARAM_LARGE_WINDOW);
|
||||
bool so = brotli_enc_config_has_param(c, BROTLI_ENC_PARAM_STREAM_OFFSET);
|
||||
|
||||
for (int k = 0; k < (mimic ? 3 : 1); k++) {
|
||||
wuffs_base__io_buffer want = ((wuffs_base__io_buffer){
|
||||
.data = g_want_slice_u8,
|
||||
});
|
||||
wuffs_base__io_buffer have1 = have;
|
||||
if (k == 0) {
|
||||
if (so) {
|
||||
continue;
|
||||
}
|
||||
status = wuffs_brotli_decode_with_raw_dictionaries(&want, &have1, lw,
|
||||
num_dicts, dicts);
|
||||
#ifdef WUFFS_MIMIC
|
||||
} else if (k == 1) {
|
||||
if (so) {
|
||||
continue;
|
||||
}
|
||||
int error_code = 0;
|
||||
status = mimic_brotli_decode_with_options(
|
||||
&want, &have1, lw, num_dicts, dict_types, dicts, &error_code);
|
||||
} else {
|
||||
wuffs_base__io_buffer src1 = src;
|
||||
status = mimic_brotli_encode_with_dictionaries(
|
||||
&want, &src1, c->param_keys, c->param_values, c->num_params,
|
||||
num_dicts, dict_types, dicts, g_brotli_enc_step_ops,
|
||||
g_brotli_enc_step_lens, g_brotli_enc_num_steps, c->dst_chunk);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: mimic encode: %s", name, status);
|
||||
}
|
||||
status = check_io_buffers_equal("", &have, &want);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: %s", name, status);
|
||||
}
|
||||
continue;
|
||||
#endif // WUFFS_MIMIC
|
||||
}
|
||||
const char* which = (k == 0) ? "wuffs" : "mimic";
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: %s decode: %s", name, which, status);
|
||||
} else if (have1.meta.ri != have1.meta.wi) {
|
||||
RETURN_FAIL("%s: %s decode: trailing data", name, which);
|
||||
} else if (want.meta.wi != data_len) {
|
||||
RETURN_FAIL("%s: %s decoded length: have %zu, want %zu", name, which,
|
||||
want.meta.wi, data_len);
|
||||
} else if (memcmp(want.data.ptr, data, data_len)) {
|
||||
RETURN_FAIL("%s: %s decoded bytes differ", name, which);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_add_dictionary() {
|
||||
CHECK_FOCUS(__func__);
|
||||
static const uint8_t text[] = "the quick brown fox jumps over the lazy dog";
|
||||
wuffs_base__slice_u8 dict =
|
||||
wuffs_base__make_slice_u8((uint8_t*)text, sizeof text - 1);
|
||||
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
|
||||
|
||||
wuffs_brotli__encoder enc;
|
||||
CHECK_STATUS("initialize",
|
||||
wuffs_brotli__encoder__initialize(
|
||||
&enc, sizeof enc, WUFFS_VERSION,
|
||||
WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED));
|
||||
CHECK_STATUS("set_quirk", wuffs_brotli__encoder__set_quirk(
|
||||
&enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, 5));
|
||||
uint64_t wlen0 = wuffs_brotli__encoder__workbuf_len(&enc).max_incl;
|
||||
|
||||
// Serialized dictionaries are rejected (like libbrotlienc's default,
|
||||
// non-experimental build) and so are unknown dictionary types.
|
||||
const char* have = wuffs_brotli__encoder__add_dictionary(
|
||||
&enc, WUFFS_BROTLI__DICTIONARY_TYPE_SERIALIZED, dict,
|
||||
workbuf)
|
||||
.repr;
|
||||
if (have != wuffs_brotli__error__unsupported_serialized_encoder_dictionary) {
|
||||
RETURN_FAIL("serialized: have \"%s\"", have);
|
||||
}
|
||||
have = wuffs_brotli__encoder__add_dictionary(&enc, 2, dict, workbuf).repr;
|
||||
if (have != wuffs_base__error__bad_argument) {
|
||||
RETURN_FAIL("unknown type: have \"%s\"", have);
|
||||
}
|
||||
|
||||
// The add_dictionary_workbuf_len formula: for a dictionary of n bytes (and
|
||||
// 17 bucket bits and 7 slot bits, for n up to 2 MiB), n bytes, (4 << 7)
|
||||
// slot offsets, (2 << 17) heads and up to 4 * (n - 7) items, then (6 <<
|
||||
// 17) + (4 * n) bytes of scratch space.
|
||||
uint64_t want_wlen =
|
||||
43 + (4 << 7) + (2 << 17) + (4 * 36) + (6 << 17) + (4 * 43);
|
||||
uint64_t have_wlen =
|
||||
wuffs_brotli__encoder__add_dictionary_workbuf_len(&enc, dict.len);
|
||||
if (have_wlen != want_wlen) {
|
||||
RETURN_FAIL("add_dictionary_workbuf_len: have %" PRIu64 ", want %" PRIu64,
|
||||
have_wlen, want_wlen);
|
||||
}
|
||||
workbuf.len = (size_t)(have_wlen - 1);
|
||||
have = wuffs_brotli__encoder__add_dictionary(
|
||||
&enc, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, dict, workbuf)
|
||||
.repr;
|
||||
if (have != wuffs_base__error__bad_workbuf_length) {
|
||||
RETURN_FAIL("short workbuf: have \"%s\"", have);
|
||||
}
|
||||
if (wuffs_brotli__encoder__workbuf_len(&enc).max_incl != wlen0) {
|
||||
RETURN_FAIL("workbuf_len changed after failed add_dictionary calls");
|
||||
}
|
||||
|
||||
// 15 dictionaries (the C code's SHARED_BROTLI_MAX_COMPOUND_DICTS), one of
|
||||
// them empty, can be added, but not 16.
|
||||
uint64_t dict_end = 0;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
wuffs_base__slice_u8 d = dict;
|
||||
if (i == 3) {
|
||||
d.len = 0;
|
||||
}
|
||||
workbuf.len = g_work_slice_u8.len;
|
||||
uint64_t n = wuffs_brotli__encoder__add_dictionary_workbuf_len(&enc, d.len);
|
||||
if (n != (dict_end + ((i == 3) ? ((4 << 7) + (8 << 17)) : want_wlen))) {
|
||||
RETURN_FAIL("i=%d: add_dictionary_workbuf_len: have %" PRIu64, i, n);
|
||||
}
|
||||
have = wuffs_brotli__encoder__add_dictionary(
|
||||
&enc, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, d, workbuf)
|
||||
.repr;
|
||||
if (i == 15) {
|
||||
if (have != wuffs_brotli__error__too_many_compound_dictionaries) {
|
||||
RETURN_FAIL("i=%d: have \"%s\"", i, have);
|
||||
}
|
||||
break;
|
||||
} else if (have) {
|
||||
RETURN_FAIL("i=%d: have \"%s\"", i, have);
|
||||
}
|
||||
// The dictionary bytes, (4 << 7) slot offsets, (2 << 17) heads and 4
|
||||
// bytes per item. Each of the (43 - 7) positions of "the quick brown fox
|
||||
// etc" has a distinct hash, so there are 36 items.
|
||||
dict_end += (i == 3) ? ((4 << 7) + (2 << 17))
|
||||
: (43 + (4 << 7) + (2 << 17) + (4 * 36));
|
||||
uint64_t wlen = wuffs_brotli__encoder__workbuf_len(&enc).max_incl;
|
||||
if (wlen != (wlen0 + dict_end)) {
|
||||
RETURN_FAIL("i=%d: workbuf_len: have %" PRIu64 ", want %" PRIu64, i,
|
||||
wlen, wlen0 + dict_end);
|
||||
}
|
||||
}
|
||||
|
||||
// Quirks can still be set after add_dictionary, until transform_io.
|
||||
CHECK_STATUS("set_quirk", wuffs_brotli__encoder__set_quirk(
|
||||
&enc, WUFFS_BROTLI__QUIRK_LGWIN, 16));
|
||||
uint64_t wlen = wuffs_brotli__encoder__workbuf_len(&enc).max_incl;
|
||||
if (wlen <= dict_end) {
|
||||
RETURN_FAIL("workbuf_len: have %" PRIu64, wlen);
|
||||
}
|
||||
|
||||
// A workbuf shorter than the dictionaries fails.
|
||||
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||
.data = wuffs_base__make_slice_u8((uint8_t*)text, sizeof text - 1),
|
||||
.meta = wuffs_base__make_io_buffer_meta(sizeof text - 1, 0, 0, true),
|
||||
});
|
||||
wuffs_base__io_buffer dst = ((wuffs_base__io_buffer){
|
||||
.data = g_have_slice_u8,
|
||||
});
|
||||
// (An error disables the encoder, so try this on a copy.)
|
||||
wuffs_brotli__encoder* copy = malloc(sizeof enc);
|
||||
if (!copy) {
|
||||
return "malloc failed";
|
||||
}
|
||||
memcpy(copy, &enc, sizeof enc);
|
||||
workbuf.len = (size_t)(dict_end - 1);
|
||||
have = wuffs_brotli__encoder__transform_io(copy, &dst, &src, workbuf).repr;
|
||||
free(copy);
|
||||
if (have != wuffs_base__error__bad_workbuf_length) {
|
||||
RETURN_FAIL("transform_io (short workbuf): have \"%s\"", have);
|
||||
}
|
||||
workbuf.len = (size_t)wlen;
|
||||
CHECK_STATUS("transform_io",
|
||||
wuffs_brotli__encoder__transform_io(&enc, &dst, &src, workbuf));
|
||||
if (dst.meta.wi >= (sizeof text - 1)) {
|
||||
RETURN_FAIL("compressed length: have %zu", dst.meta.wi);
|
||||
}
|
||||
|
||||
// After transform_io, add_dictionary (and set_quirk) fail. (The C code's
|
||||
// BrotliEncoderAttachPreparedDictionary accepts a raw dictionary at any
|
||||
// time, but attaching one after compression has started changes the
|
||||
// compound dictionary's size mid-stream, which is not supported here.)
|
||||
have = wuffs_brotli__encoder__add_dictionary(
|
||||
&enc, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, dict, workbuf)
|
||||
.repr;
|
||||
if (have != wuffs_base__error__bad_call_sequence) {
|
||||
RETURN_FAIL("after transform_io: have \"%s\"", have);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// brotli_enc_dict_set fills dicts with one of the dictionary test sets (for
|
||||
// the alice29.txt, asyoulik.txt and romeo.txt files a, b and r, each of
|
||||
// length a_len, b_len and r_len) and returns how many dictionaries it has.
|
||||
size_t //
|
||||
brotli_enc_dict_set(int which,
|
||||
wuffs_base__slice_u8* dicts,
|
||||
uint8_t* a,
|
||||
size_t a_len,
|
||||
uint8_t* b,
|
||||
size_t b_len,
|
||||
uint8_t* r,
|
||||
size_t r_len) {
|
||||
switch (which) {
|
||||
case 0: // One small dictionary.
|
||||
dicts[0] = wuffs_base__make_slice_u8(r, r_len);
|
||||
return 1;
|
||||
case 1: // The first half of alice29.txt.
|
||||
dicts[0] = wuffs_base__make_slice_u8(a, a_len / 2);
|
||||
return 1;
|
||||
case 2: // Several dictionaries, including tiny and empty ones.
|
||||
dicts[0] = wuffs_base__make_slice_u8(r, 0);
|
||||
dicts[1] = wuffs_base__make_slice_u8(b + 1000, 5);
|
||||
dicts[2] = wuffs_base__make_slice_u8(a + (a_len / 2), a_len / 4);
|
||||
dicts[3] = wuffs_base__make_slice_u8(b, 4000);
|
||||
dicts[4] = wuffs_base__make_slice_u8(r + 100, 300);
|
||||
return 5;
|
||||
case 3: // 15 (the maximum number of) pieces of alice29.txt.
|
||||
for (size_t j = 0; j < 15; j++) {
|
||||
dicts[j] = wuffs_base__make_slice_u8(a + (j * 10000), 9000);
|
||||
}
|
||||
return 15;
|
||||
}
|
||||
dicts[0] = wuffs_base__make_slice_u8(b, b_len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* //
|
||||
do_test_brotli_encode_dictionary_grid(bool mimic) {
|
||||
size_t a_len = 0;
|
||||
size_t b_len = 0;
|
||||
size_t r_len = 0;
|
||||
uint8_t* a = brotli_enc_load_file("test/data/brotli/alice29.txt", &a_len);
|
||||
uint8_t* b = brotli_enc_load_file("test/data/brotli/asyoulik.txt", &b_len);
|
||||
uint8_t* r = brotli_enc_load_file("test/data/romeo.txt", &r_len);
|
||||
const char* ret = NULL;
|
||||
if (!a || !b || !r || (a_len < 150000)) {
|
||||
ret = "could not load the test files";
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// Each config is (lgwin, chunking, flush, metadata, extra param, value).
|
||||
static const uint32_t configs[][6] = {
|
||||
{22, BROTLI_ENC_CHUNK_SINGLE, BROTLI_ENC_FLUSH_NONE, 0, 0, 0},
|
||||
{16, BROTLI_ENC_CHUNK_RANDOM, BROTLI_ENC_FLUSH_MIDDLE, 0, 0, 0},
|
||||
{10, BROTLI_ENC_CHUNK_RANDOM, BROTLI_ENC_FLUSH_EVERY_CHUNK, 0, 0, 0},
|
||||
{18, BROTLI_ENC_CHUNK_RANDOM, BROTLI_ENC_FLUSH_MIDDLE,
|
||||
BROTLI_ENC_METADATA_MIDDLE, 0, 0},
|
||||
{22, BROTLI_ENC_CHUNK_RANDOM, BROTLI_ENC_FLUSH_MIDDLE, 0,
|
||||
BROTLI_ENC_PARAM_STREAM_OFFSET, 1000},
|
||||
{24, BROTLI_ENC_CHUNK_SINGLE, BROTLI_ENC_FLUSH_NONE, 0,
|
||||
BROTLI_ENC_PARAM_STREAM_OFFSET, 1 << 30},
|
||||
{26, BROTLI_ENC_CHUNK_RANDOM, BROTLI_ENC_FLUSH_MIDDLE, 0,
|
||||
BROTLI_ENC_PARAM_LARGE_WINDOW, 1},
|
||||
{22, BROTLI_ENC_CHUNK_SINGLE, BROTLI_ENC_FLUSH_NONE, 0,
|
||||
BROTLI_ENC_PARAM_SIZE_HINT, 1 << 20},
|
||||
};
|
||||
// The input is the second half of alice29.txt, except for dictionary set
|
||||
// 4 (all of asyoulik.txt), whose input is romeo.txt.
|
||||
for (int set = 0; (set < 5) && !ret; set++) {
|
||||
wuffs_base__slice_u8 dicts[16];
|
||||
size_t num_dicts = brotli_enc_dict_set(set, dicts, a, a_len, b, b_len, r,
|
||||
r_len);
|
||||
const uint8_t* data = (set == 4) ? r : (a + (a_len / 2));
|
||||
size_t data_len = (set == 4) ? r_len : (a_len - (a_len / 2));
|
||||
for (uint32_t q = 0; (q <= 9) && !ret; q++) {
|
||||
for (size_t i = 0; (i < WUFFS_TESTLIB_ARRAY_SIZE(configs)) && !ret;
|
||||
i++) {
|
||||
if (!mimic && (i >= 2) && (q & 1)) {
|
||||
continue;
|
||||
}
|
||||
brotli_enc_config c = make_brotli_enc_config(q, configs[i][0]);
|
||||
if (configs[i][4]) {
|
||||
brotli_enc_config__add_param(&c, configs[i][4], configs[i][5]);
|
||||
}
|
||||
c.chunking = configs[i][1];
|
||||
c.flush = configs[i][2];
|
||||
c.metadata_position = configs[i][3];
|
||||
c.metadata_len = configs[i][3] ? 100 : 0;
|
||||
c.seed = (set << 16) | (q << 8) | i;
|
||||
char s[256];
|
||||
snprintf(s, sizeof s, "set=%d q=%u config=%zu", set, q, i);
|
||||
size_t have_len = 0;
|
||||
ret = do_test_brotli_encode_dictionary(s, &c, data, data_len,
|
||||
num_dicts, dicts, mimic,
|
||||
&have_len);
|
||||
if (ret || (set != 1) || (q < 5) || (i != 0)) {
|
||||
continue;
|
||||
}
|
||||
// Encoding the second half of alice29.txt with the first half as a
|
||||
// dictionary should be at least 5% better than without it.
|
||||
size_t without_len = 0;
|
||||
ret = do_test_brotli_encode_dictionary(s, &c, data, data_len, 0, NULL,
|
||||
false, &without_len);
|
||||
if (!ret && ((have_len * 100) >= (without_len * 95))) {
|
||||
snprintf(g_fail_msg, sizeof g_fail_msg,
|
||||
"%s: compressed length: have %zu, without dictionary %zu",
|
||||
s, have_len, without_len);
|
||||
ret = g_fail_msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
free(a);
|
||||
free(b);
|
||||
free(r);
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char* //
|
||||
do_test_brotli_encode_dictionary_big(bool mimic) {
|
||||
// Dictionaries of exactly 2 MiB (the largest with 17 bucket bits), one
|
||||
// byte more (18 bucket bits) and 5 and 33 MiB (19 and 22 bucket bits).
|
||||
// Each holds, at various offsets, pieces of the 1 MiB input.
|
||||
static const size_t sizes[] = {2 << 20, (2 << 20) + 1, (5 << 20) + 3,
|
||||
(33 << 20) + 5};
|
||||
const size_t len = 1 << 20;
|
||||
uint8_t* data = malloc(len);
|
||||
uint8_t* dict = malloc(sizes[3]);
|
||||
const char* ret = NULL;
|
||||
if (!data || !dict) {
|
||||
ret = "malloc failed";
|
||||
goto cleanup;
|
||||
}
|
||||
brotli_enc_fill_random(data, len, 0xDA7A);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
// Make it compressible: mostly lowercase letters.
|
||||
data[i] = 'a' + (data[i] % 13);
|
||||
}
|
||||
for (size_t k = 0; (k < WUFFS_TESTLIB_ARRAY_SIZE(sizes)) && !ret; k++) {
|
||||
size_t dlen = sizes[k];
|
||||
brotli_enc_fill_random(dict, dlen, 0xD1C7 + k);
|
||||
for (size_t j = 0; j < 3; j++) {
|
||||
size_t at = (j == 0) ? 999 : (j == 1) ? (dlen / 2) : (dlen - (65 << 10));
|
||||
memcpy(dict + at, data + (j * (300 << 10)), 64 << 10);
|
||||
}
|
||||
wuffs_base__slice_u8 dicts[2] = {
|
||||
wuffs_base__make_slice_u8(dict, dlen),
|
||||
wuffs_base__make_slice_u8(data + (len - 1000), 1000),
|
||||
};
|
||||
static const uint32_t qs[] = {2, 4, 5, 9};
|
||||
for (size_t i = 0; (i < WUFFS_TESTLIB_ARRAY_SIZE(qs)) && !ret; i++) {
|
||||
// Without mimic, and for the largest dictionary, a smaller grid.
|
||||
if ((!mimic && ((k & 1) == 0)) || ((!mimic || (k == 3)) && (i != 2))) {
|
||||
continue;
|
||||
}
|
||||
brotli_enc_config c = make_brotli_enc_config(qs[i], 24);
|
||||
if (i & 1) {
|
||||
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_SIZE_HINT, 1 << 20);
|
||||
c.chunking = BROTLI_ENC_CHUNK_RANDOM;
|
||||
c.flush = BROTLI_ENC_FLUSH_MIDDLE;
|
||||
}
|
||||
c.seed = (k << 8) | i;
|
||||
char s[256];
|
||||
snprintf(s, sizeof s, "dictionary of %zu bytes: q=%u", dlen, qs[i]);
|
||||
size_t have_len = 0;
|
||||
ret = do_test_brotli_encode_dictionary(s, &c, data, len, 2, dicts,
|
||||
mimic, &have_len);
|
||||
// The three 64 KiB pieces (of 3.7 bits of entropy per byte) should be
|
||||
// found at quality 5 and above, even when they are more than 16 MiB
|
||||
// (lgwin 24's window) into the dictionary.
|
||||
size_t want_max = 0;
|
||||
if (!ret && (qs[i] >= 5)) {
|
||||
c = make_brotli_enc_config(qs[i], 24);
|
||||
ret = do_test_brotli_encode_dictionary(s, &c, data, len, 0, NULL,
|
||||
false, &want_max);
|
||||
want_max -= 3 * (24 << 10);
|
||||
if (!ret && (have_len > want_max)) {
|
||||
snprintf(g_fail_msg, sizeof g_fail_msg,
|
||||
"%s: compressed length: have %zu, want at most %zu", s,
|
||||
have_len, want_max);
|
||||
ret = g_fail_msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cleanup:
|
||||
free(data);
|
||||
free(dict);
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_dictionary_big() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_dictionary_big(false);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_dictionary_grid() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_dictionary_grid(false);
|
||||
}
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
const char* //
|
||||
test_mimic_brotli_encode_dictionary_big() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_dictionary_big(true);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_mimic_brotli_encode_dictionary_grid() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_dictionary_grid(true);
|
||||
}
|
||||
|
||||
// test_mimic_brotli_encode_dictionary_rejections checks that the dictionaries
|
||||
// that libbrotlienc (a default, non-experimental build) rejects are also
|
||||
// rejected by Wuffs, and that an empty dictionary is accepted by both.
|
||||
const char* //
|
||||
test_mimic_brotli_encode_dictionary_rejections() {
|
||||
CHECK_FOCUS(__func__);
|
||||
static const uint8_t text[] = "the quick brown fox jumps over the lazy dog";
|
||||
// A serialized dictionary (an LZ77 prefix only), which an experimental
|
||||
// libbrotlienc build would accept.
|
||||
static const uint8_t serialized[] = {0x91, 0x00, 0x03, 'f', 'o', 'x', 0x00,
|
||||
0x00};
|
||||
static const struct {
|
||||
size_t num_dicts;
|
||||
uint32_t type; // The last dictionary's type.
|
||||
size_t len; // The last dictionary's length.
|
||||
const char* want_wuffs;
|
||||
const char* want_mimic;
|
||||
} cases[] = {
|
||||
{1, WUFFS_BROTLI__DICTIONARY_TYPE_SERIALIZED, sizeof serialized,
|
||||
wuffs_brotli__error__unsupported_serialized_encoder_dictionary,
|
||||
mimic_brotli_prepare_dictionary_failed_message},
|
||||
{16, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, 9,
|
||||
wuffs_brotli__error__too_many_compound_dictionaries,
|
||||
mimic_brotli_attach_prepared_dictionary_failed_message},
|
||||
{15, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, 9, NULL, NULL},
|
||||
{1, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, 0, NULL, NULL},
|
||||
{3, WUFFS_BROTLI__DICTIONARY_TYPE_RAW, 0, NULL, NULL},
|
||||
};
|
||||
for (size_t i = 0; i < WUFFS_TESTLIB_ARRAY_SIZE(cases); i++) {
|
||||
uint32_t dict_types[16];
|
||||
wuffs_base__slice_u8 dicts[16];
|
||||
for (size_t j = 0; j < cases[i].num_dicts; j++) {
|
||||
dict_types[j] = WUFFS_BROTLI__DICTIONARY_TYPE_RAW;
|
||||
dicts[j] = wuffs_base__make_slice_u8((uint8_t*)text + j, 20);
|
||||
}
|
||||
size_t last = cases[i].num_dicts - 1;
|
||||
dict_types[last] = cases[i].type;
|
||||
dicts[last] = wuffs_base__make_slice_u8(
|
||||
(uint8_t*)((cases[i].type == WUFFS_BROTLI__DICTIONARY_TYPE_RAW)
|
||||
? text
|
||||
: serialized),
|
||||
cases[i].len);
|
||||
|
||||
brotli_enc_config c = make_brotli_enc_config(5, 22);
|
||||
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||
.data = g_src_slice_u8,
|
||||
});
|
||||
CHECK_STRING(make_brotli_enc_plan(&src, &c, text, sizeof text - 1));
|
||||
wuffs_base__io_buffer dsts[2] = {
|
||||
((wuffs_base__io_buffer){.data = g_have_slice_u8}),
|
||||
((wuffs_base__io_buffer){.data = g_want_slice_u8}),
|
||||
};
|
||||
for (int k = 0; k < 2; k++) {
|
||||
wuffs_base__io_buffer src1 = src;
|
||||
const char* have =
|
||||
(k ? mimic_brotli_encode_with_dictionaries
|
||||
: wuffs_brotli_encode_plan_with_dictionaries)(
|
||||
&dsts[k], &src1, c.param_keys, c.param_values, c.num_params,
|
||||
cases[i].num_dicts, dict_types, dicts, g_brotli_enc_step_ops,
|
||||
g_brotli_enc_step_lens, g_brotli_enc_num_steps, c.dst_chunk);
|
||||
const char* want = k ? cases[i].want_mimic : cases[i].want_wuffs;
|
||||
if (have != want) {
|
||||
RETURN_FAIL("cases[%zu] (%s): have \"%s\", want \"%s\"", i,
|
||||
k ? "mimic" : "wuffs", have ? have : "",
|
||||
want ? want : "");
|
||||
}
|
||||
}
|
||||
if (!cases[i].want_wuffs) {
|
||||
CHECK_STRING(check_io_buffers_equal("", &dsts[0], &dsts[1]));
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif // WUFFS_MIMIC
|
||||
|
||||
// ---------------- Brotli Benches
|
||||
|
||||
const char* //
|
||||
@@ -3709,6 +4400,9 @@ proc g_tests[] = {
|
||||
|
||||
// Encoder tests.
|
||||
|
||||
test_wuffs_brotli_encode_add_dictionary,
|
||||
test_wuffs_brotli_encode_dictionary_big,
|
||||
test_wuffs_brotli_encode_dictionary_grid,
|
||||
test_wuffs_brotli_encode_interface,
|
||||
test_wuffs_brotli_encode_large_window_far,
|
||||
test_wuffs_brotli_encode_large_window_files,
|
||||
@@ -3720,6 +4414,9 @@ proc g_tests[] = {
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
test_mimic_brotli_encode_dictionary_big,
|
||||
test_mimic_brotli_encode_dictionary_grid,
|
||||
test_mimic_brotli_encode_dictionary_rejections,
|
||||
test_mimic_brotli_encode_large_window_far,
|
||||
test_mimic_brotli_encode_large_window_files,
|
||||
test_mimic_brotli_encode_parity_corpus,
|
||||
|
||||
Reference in new issue
Block a user