+/* Update the cost model based on the codeword lengths @c->lens. */
+static void
+xpress_update_costs(struct xpress_compressor *c)
+{
+ for (unsigned i = 0; i < XPRESS_NUM_SYMBOLS; i++)
+ c->costs[i] = c->lens[i] ? c->lens[i] : XPRESS_MAX_CODEWORD_LEN;
+}
+
+/*
+ * Follow the minimum cost path in the graph of possible match/literal choices
+ * and compute the frequencies of the Huffman symbols that are needed to output
+ * those matches and literals.
+ */
+static void
+xpress_tally_item_list(struct xpress_compressor *c,
+ struct xpress_optimum_node *end_optimum_ptr)
+{
+ struct xpress_optimum_node *cur_optimum_ptr = c->optimum_nodes;
+
+ do {
+ unsigned length = cur_optimum_ptr->item & OPTIMUM_LEN_MASK;
+ unsigned offset = cur_optimum_ptr->item >> OPTIMUM_OFFSET_SHIFT;
+
+ if (length == 1) {
+ /* Literal */
+ unsigned literal = offset;
+
+ c->freqs[literal]++;
+ } else {
+ /* Match */
+ unsigned adjusted_len;
+ unsigned offset_high_bit;
+ unsigned len_hdr;
+ unsigned sym;
+
+ adjusted_len = length - XPRESS_MIN_MATCH_LEN;
+ offset_high_bit = fls32(offset);
+ len_hdr = min(0xF, adjusted_len);
+ sym = XPRESS_NUM_CHARS + ((offset_high_bit << 4) | len_hdr);
+
+ c->freqs[sym]++;
+ }
+ cur_optimum_ptr += length;
+ } while (cur_optimum_ptr != end_optimum_ptr);
+}
+
+/*
+ * Find a new minimum cost path through the graph of possible match/literal
+ * choices. We find the minimum cost path from 'c->optimum_nodes[0]', which
+ * represents the node at the beginning of the input buffer, to
+ * 'c->optimum_nodes[in_nbytes]', which represents the node at the end of the
+ * input buffer. Edge costs are evaluated using the cost model 'c->costs'.
+ *
+ * The algorithm works backward, starting at 'c->optimum_nodes[in_nbytes]' and
+ * proceeding backwards one position at a time. At each position, the minimum
+ * cost to reach 'c->optimum_nodes[in_nbytes]' from that position is computed
+ * and the match/literal choice is saved.
+ */
+static void
+xpress_find_min_cost_path(struct xpress_compressor *c, size_t in_nbytes,
+ struct lz_match *end_cache_ptr)
+{
+ struct xpress_optimum_node *cur_optimum_ptr = c->optimum_nodes + in_nbytes;
+ struct lz_match *cache_ptr = end_cache_ptr;
+
+ cur_optimum_ptr->cost_to_end = 0;
+ do {
+ unsigned literal;
+ u32 best_item;
+ u32 best_cost_to_end;
+ unsigned num_matches;
+ struct lz_match *match;
+ unsigned len;
+
+ cur_optimum_ptr--;
+ cache_ptr--;
+
+ literal = cache_ptr->offset;
+
+ /* Consider coding a literal. */
+ best_item = ((u32)literal << OPTIMUM_OFFSET_SHIFT) | 1;
+ best_cost_to_end = c->costs[literal] +
+ (cur_optimum_ptr + 1)->cost_to_end;
+
+ num_matches = cache_ptr->length;
+
+ if (num_matches == 0) {
+ /* No matches; the only choice is the literal. */
+ cur_optimum_ptr->cost_to_end = best_cost_to_end;
+ cur_optimum_ptr->item = best_item;
+ continue;
+ }
+
+ /*
+ * Consider each match length from the minimum
+ * (XPRESS_MIN_MATCH_LEN) to the length of the longest match
+ * found at this position. For each length, consider only the
+ * smallest offset for which that length is available. Although
+ * this is not guaranteed to be optimal due to the possibility
+ * of a larger offset costing less than a smaller offset to
+ * code, this is a very useful heuristic.
+ */
+ match = cache_ptr - num_matches;
+ len = XPRESS_MIN_MATCH_LEN;
+ if (cache_ptr[-1].length < 0xF + XPRESS_MIN_MATCH_LEN) {
+ /* All lengths are small. Optimize accordingly. */
+ do {
+ unsigned offset;
+ unsigned offset_high_bit;
+ u32 offset_cost;
+
+ offset = match->offset;
+ offset_high_bit = fls32(offset);
+ offset_cost = offset_high_bit;
+ do {
+ unsigned len_hdr;
+ unsigned sym;
+ u32 cost_to_end;
+
+ len_hdr = len - XPRESS_MIN_MATCH_LEN;
+ sym = XPRESS_NUM_CHARS +
+ ((offset_high_bit << 4) | len_hdr);
+ cost_to_end =
+ offset_cost + c->costs[sym] +
+ (cur_optimum_ptr + len)->cost_to_end;
+ if (cost_to_end < best_cost_to_end) {
+ best_cost_to_end = cost_to_end;
+ best_item =
+ ((u32)offset <<
+ OPTIMUM_OFFSET_SHIFT) | len;
+ }
+ } while (++len <= match->length);
+ } while (++match != cache_ptr);
+ } else {
+ /* Some lengths are big. */
+ do {
+ unsigned offset;
+ unsigned offset_high_bit;
+ u32 offset_cost;
+
+ offset = match->offset;
+ offset_high_bit = fls32(offset);
+ offset_cost = offset_high_bit;
+ do {
+ unsigned adjusted_len;
+ unsigned len_hdr;
+ unsigned sym;
+ u32 cost_to_end;
+
+ adjusted_len = len - XPRESS_MIN_MATCH_LEN;
+ len_hdr = min(adjusted_len, 0xF);
+ sym = XPRESS_NUM_CHARS +
+ ((offset_high_bit << 4) | len_hdr);
+ cost_to_end =
+ offset_cost + c->costs[sym] +
+ (cur_optimum_ptr + len)->cost_to_end;
+ if (adjusted_len >= 0xF) {
+ cost_to_end += 8;
+ if (adjusted_len - 0xF >= 0xFF)
+ cost_to_end += 16;
+ }
+ if (cost_to_end < best_cost_to_end) {
+ best_cost_to_end = cost_to_end;
+ best_item =
+ ((u32)offset <<
+ OPTIMUM_OFFSET_SHIFT) | len;
+ }
+ } while (++len <= match->length);
+ } while (++match != cache_ptr);
+ }
+ cache_ptr -= num_matches;
+ cur_optimum_ptr->cost_to_end = best_cost_to_end;
+ cur_optimum_ptr->item = best_item;
+ } while (cur_optimum_ptr != c->optimum_nodes);
+}
+
+/*
+ * This routine finds matches at each position in the buffer in[0...in_nbytes].
+ * The matches are cached in the array c->match_cache, and the return value is a
+ * pointer past the last slot in this array that was filled.
+ */
+static struct lz_match *
+xpress_find_matches(struct xpress_compressor * restrict c,
+ const void * restrict in, size_t in_nbytes)
+{
+ const u8 * const in_base = in;
+ const u8 *in_next = in_base;
+ const u8 * const in_end = in_base + in_nbytes;
+ struct lz_match *cache_ptr = c->match_cache;
+ unsigned long prev_hash = 0;
+
+ bt_matchfinder_init(&c->bt_mf);
+
+ do {
+ unsigned num_matches;
+
+ /* If we've found so many matches that the cache might overflow
+ * if we keep finding more, then stop finding matches. This
+ * case is very unlikely. */
+ if (unlikely(cache_ptr >= c->cache_overflow_mark)) {
+ do {
+ cache_ptr->length = 0;
+ cache_ptr->offset = *in_next++;
+ cache_ptr++;
+ } while (in_next != in_end);
+ return cache_ptr;
+ }
+
+ /* Find matches with the current position using the binary tree
+ * matchfinder and save them in the next available slots in
+ * the match cache. */
+ num_matches =
+ bt_matchfinder_get_matches(&c->bt_mf,
+ in_base,
+ in_next,
+ XPRESS_MIN_MATCH_LEN,
+ in_end - in_next,
+ min(in_end - in_next, c->nice_match_length),
+ c->max_search_depth,
+ &prev_hash,
+ cache_ptr);
+ cache_ptr += num_matches;
+ cache_ptr->length = num_matches;
+ cache_ptr->offset = *in_next;
+ in_next++;
+ cache_ptr++;
+
+ if (num_matches) {
+ /*
+ * If there was a very long match found, then don't
+ * cache any matches for the bytes covered by that
+ * match. This avoids degenerate behavior when
+ * compressing highly redundant data, where the number
+ * of matches can be very large.
+ *
+ * This heuristic doesn't actually hurt the compression
+ * ratio very much. If there's a long match, then the
+ * data must be highly compressible, so it doesn't
+ * matter as much what we do.
+ */
+ unsigned best_len = cache_ptr[-2].length;
+ if (best_len >= c->nice_match_length) {
+ --best_len;
+ do {
+ bt_matchfinder_skip_position(&c->bt_mf,
+ in_base,
+ in_next,
+ in_end,
+ min(in_end - in_next,
+ c->nice_match_length),
+ c->max_search_depth,
+ &prev_hash);
+
+ cache_ptr->length = 0;
+ cache_ptr->offset = *in_next++;
+ cache_ptr++;
+ } while (--best_len);
+ }
+ }
+ } while (in_next != in_end);
+
+ return cache_ptr;
+}
+
+/*
+ * This is the "near-optimal" XPRESS compressor. It computes a compressed
+ * representation of the input buffer by executing a minimum cost path search
+ * over the graph of possible match/literal choices, assuming a certain cost for
+ * each Huffman symbol. The result is usually close to optimal, but it is *not*
+ * guaranteed to be optimal because of (a) heuristic restrictions in which
+ * matches are considered, and (b) symbol costs are unknown until those symbols
+ * have already been chosen --- so iterative optimization must be used, and the
+ * algorithm might converge on a local optimum rather than a global optimum.
+ */
+static size_t
+xpress_compress_near_optimal(struct xpress_compressor * restrict c,
+ const void * restrict in, size_t in_nbytes,
+ void * restrict out, size_t out_nbytes_avail)
+{
+ struct lz_match *end_cache_ptr;
+ unsigned num_passes_remaining = c->num_optim_passes;
+
+ /* Run the input buffer through the matchfinder and save the results. */
+ end_cache_ptr = xpress_find_matches(c, in, in_nbytes);
+
+ /* The first optimization pass uses a default cost model. Each
+ * additional optimization pass uses a cost model derived from the
+ * Huffman code computed in the previous pass. */
+ xpress_set_default_costs(c);
+ do {
+ xpress_find_min_cost_path(c, in_nbytes, end_cache_ptr);
+ xpress_tally_item_list(c, c->optimum_nodes + in_nbytes);
+ if (num_passes_remaining > 1) {
+ c->freqs[XPRESS_END_OF_DATA]++;
+ xpress_make_huffman_code(c);
+ xpress_update_costs(c);
+ xpress_reset_symbol_frequencies(c);
+ }
+ } while (--num_passes_remaining);
+
+ return xpress_write(c, out, out_nbytes_avail, in_nbytes, true);
+}
+
+#endif /* SUPPORT_NEAR_OPTIMAL_PARSING */
+
+static u64
+xpress_get_needed_memory(size_t max_bufsize, unsigned compression_level)
+{
+ size_t size = 0;
+
+ if (max_bufsize > XPRESS_MAX_BUFSIZE)
+ return 0;
+
+ if (compression_level < MIN_LEVEL_FOR_NEAR_OPTIMAL ||
+ !SUPPORT_NEAR_OPTIMAL_PARSING) {
+ size += offsetof(struct xpress_compressor, nonoptimal_end);
+ size += max_bufsize * sizeof(struct xpress_item);