2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2010 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
|
|
|
*/
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* A (forgetful) hash table to the data seen by the compressor, to
|
|
|
|
help create backward references to previous data. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#ifndef BROTLI_ENC_HASH_H_
|
|
|
|
#define BROTLI_ENC_HASH_H_
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#include <string.h> /* memcmp, memset */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-07-26 12:41:59 +00:00
|
|
|
#include "../common/constants.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#include "../common/dictionary.h"
|
2017-12-12 13:33:12 +00:00
|
|
|
#include "../common/platform.h"
|
2016-08-23 12:40:33 +00:00
|
|
|
#include <brotli/types.h>
|
2013-10-23 11:06:13 +00:00
|
|
|
#include "./fast_log.h"
|
|
|
|
#include "./find_match_length.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#include "./memory.h"
|
2016-07-26 12:41:59 +00:00
|
|
|
#include "./quality.h"
|
2014-03-20 13:32:35 +00:00
|
|
|
#include "./static_dict.h"
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
/* Pointer to hasher data.
|
|
|
|
*
|
|
|
|
* Excluding initialization and destruction, hasher can be passed as
|
|
|
|
* HasherHandle by value.
|
|
|
|
*
|
|
|
|
* Typically hasher data consists of 3 sections:
|
|
|
|
* * HasherCommon structure
|
|
|
|
* * private structured hasher data, depending on hasher type
|
|
|
|
* * private dynamic hasher data, depending on hasher type and parameters
|
|
|
|
*/
|
|
|
|
typedef uint8_t* HasherHandle;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BrotliHasherParams params;
|
|
|
|
|
|
|
|
/* False if hasher needs to be "prepared" before use. */
|
|
|
|
BROTLI_BOOL is_prepared_;
|
|
|
|
|
|
|
|
size_t dict_num_lookups;
|
|
|
|
size_t dict_num_matches;
|
|
|
|
} HasherCommon;
|
|
|
|
|
|
|
|
static BROTLI_INLINE HasherCommon* GetHasherCommon(HasherHandle handle) {
|
|
|
|
return (HasherCommon*)handle;
|
|
|
|
}
|
2016-01-27 08:50:39 +00:00
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
#define score_t size_t
|
2014-10-28 12:25:22 +00:00
|
|
|
|
2016-01-07 15:27:49 +00:00
|
|
|
static const uint32_t kCutoffTransformsCount = 10;
|
2017-03-06 13:22:45 +00:00
|
|
|
/* 0, 12, 27, 23, 42, 63, 56, 48, 59, 64 */
|
|
|
|
/* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */
|
|
|
|
static const uint64_t kCutoffTransforms =
|
|
|
|
BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200);
|
2015-08-10 11:13:58 +00:00
|
|
|
|
2016-07-26 12:41:59 +00:00
|
|
|
typedef struct HasherSearchResult {
|
|
|
|
size_t len;
|
|
|
|
size_t distance;
|
|
|
|
score_t score;
|
2017-07-21 08:07:24 +00:00
|
|
|
int len_code_delta; /* == len_code - len */
|
2016-07-26 12:41:59 +00:00
|
|
|
} HasherSearchResult;
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* kHashMul32 multiplier has these properties:
|
|
|
|
* The multiplier must be odd. Otherwise we may lose the highest bit.
|
2016-10-31 13:33:59 +00:00
|
|
|
* No long streaks of ones or zeros.
|
2016-06-03 09:19:23 +00:00
|
|
|
* There is no effort to ensure that it is a prime, the oddity is enough
|
|
|
|
for this use.
|
|
|
|
* The number has been tuned heuristically against compression benchmarks. */
|
2013-10-23 11:06:13 +00:00
|
|
|
static const uint32_t kHashMul32 = 0x1e35a7bd;
|
2017-02-06 13:20:43 +00:00
|
|
|
static const uint64_t kHashMul64 = BROTLI_MAKE_UINT64_T(0x1e35a7bd, 0x1e35a7bd);
|
2017-03-06 13:22:45 +00:00
|
|
|
static const uint64_t kHashMul64Long =
|
|
|
|
BROTLI_MAKE_UINT64_T(0x1fe35a7bU, 0xd3579bd3U);
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE uint32_t Hash14(const uint8_t* data) {
|
2017-12-12 13:33:12 +00:00
|
|
|
uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
|
2016-06-03 09:19:23 +00:00
|
|
|
/* The higher bits contain more mixture from the multiplication,
|
|
|
|
so we take our results from there. */
|
2016-06-13 09:01:04 +00:00
|
|
|
return h >> (32 - 14);
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE void PrepareDistanceCache(
|
|
|
|
int* BROTLI_RESTRICT distance_cache, const int num_distances) {
|
|
|
|
if (num_distances > 4) {
|
|
|
|
int last_distance = distance_cache[0];
|
|
|
|
distance_cache[4] = last_distance - 1;
|
|
|
|
distance_cache[5] = last_distance + 1;
|
|
|
|
distance_cache[6] = last_distance - 2;
|
|
|
|
distance_cache[7] = last_distance + 2;
|
|
|
|
distance_cache[8] = last_distance - 3;
|
|
|
|
distance_cache[9] = last_distance + 3;
|
|
|
|
if (num_distances > 10) {
|
|
|
|
int next_last_distance = distance_cache[1];
|
|
|
|
distance_cache[10] = next_last_distance - 1;
|
|
|
|
distance_cache[11] = next_last_distance + 1;
|
|
|
|
distance_cache[12] = next_last_distance - 2;
|
|
|
|
distance_cache[13] = next_last_distance + 2;
|
|
|
|
distance_cache[14] = next_last_distance - 3;
|
|
|
|
distance_cache[15] = next_last_distance + 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BROTLI_LITERAL_BYTE_SCORE 135
|
|
|
|
#define BROTLI_DISTANCE_BIT_PENALTY 30
|
2016-07-26 12:41:59 +00:00
|
|
|
/* Score must be positive after applying maximal penalty. */
|
|
|
|
#define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t))
|
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
/* Usually, we always choose the longest backward reference. This function
|
|
|
|
allows for the exception of that rule.
|
|
|
|
|
|
|
|
If we choose a backward reference that is further away, it will
|
|
|
|
usually be coded with more bits. We approximate this by assuming
|
|
|
|
log2(distance). If the distance can be expressed in terms of the
|
|
|
|
last four distances, we use some heuristic constants to estimate
|
|
|
|
the bits cost. For the first up to four literals we use the bit
|
|
|
|
cost of the literals from the literal cost model, after that we
|
|
|
|
use the average bit cost of the cost model.
|
|
|
|
|
|
|
|
This function is used to sometimes discard a longer backward reference
|
|
|
|
when it is not much longer and the bit cost for encoding it is more
|
|
|
|
than the saved literals.
|
|
|
|
|
|
|
|
backward_reference_offset MUST be positive. */
|
2016-07-26 12:41:59 +00:00
|
|
|
static BROTLI_INLINE score_t BackwardReferenceScore(
|
2016-06-13 09:01:04 +00:00
|
|
|
size_t copy_length, size_t backward_reference_offset) {
|
2016-07-26 12:41:59 +00:00
|
|
|
return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length -
|
|
|
|
BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset);
|
2014-03-20 13:32:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 12:41:59 +00:00
|
|
|
static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance(
|
2017-03-06 13:22:45 +00:00
|
|
|
size_t copy_length) {
|
2016-07-26 12:41:59 +00:00
|
|
|
return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length +
|
2017-03-06 13:22:45 +00:00
|
|
|
BROTLI_SCORE_BASE + 15;
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance(
|
|
|
|
size_t distance_short_code) {
|
|
|
|
return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE);
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem(
|
2017-03-06 13:22:45 +00:00
|
|
|
const BrotliDictionary* dictionary, size_t item, const uint8_t* data,
|
|
|
|
size_t max_length, size_t max_backward, HasherSearchResult* out) {
|
2016-07-26 12:41:59 +00:00
|
|
|
size_t len;
|
|
|
|
size_t dist;
|
|
|
|
size_t offset;
|
|
|
|
size_t matchlen;
|
|
|
|
size_t backward;
|
|
|
|
score_t score;
|
2017-01-26 10:32:18 +00:00
|
|
|
len = item & 0x1F;
|
2016-07-26 12:41:59 +00:00
|
|
|
dist = item >> 5;
|
2017-03-06 13:22:45 +00:00
|
|
|
offset = dictionary->offsets_by_length[len] + len * dist;
|
2016-07-26 12:41:59 +00:00
|
|
|
if (len > max_length) {
|
|
|
|
return BROTLI_FALSE;
|
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
matchlen =
|
|
|
|
FindMatchLengthWithLimit(data, &dictionary->data[offset], len);
|
2016-07-26 12:41:59 +00:00
|
|
|
if (matchlen + kCutoffTransformsCount <= len || matchlen == 0) {
|
|
|
|
return BROTLI_FALSE;
|
|
|
|
}
|
|
|
|
{
|
2017-03-06 13:22:45 +00:00
|
|
|
size_t cut = len - matchlen;
|
|
|
|
size_t transform_id =
|
|
|
|
(cut << 2) + (size_t)((kCutoffTransforms >> (cut * 6)) & 0x3F);
|
2016-07-26 12:41:59 +00:00
|
|
|
backward = max_backward + dist + 1 +
|
2017-03-06 13:22:45 +00:00
|
|
|
(transform_id << dictionary->size_bits_by_length[len]);
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
2017-08-04 08:02:56 +00:00
|
|
|
if (backward >= BROTLI_MAX_DISTANCE) {
|
|
|
|
return BROTLI_FALSE;
|
|
|
|
}
|
2016-07-26 12:41:59 +00:00
|
|
|
score = BackwardReferenceScore(matchlen, backward);
|
|
|
|
if (score < out->score) {
|
|
|
|
return BROTLI_FALSE;
|
|
|
|
}
|
|
|
|
out->len = matchlen;
|
2017-07-21 08:07:24 +00:00
|
|
|
out->len_code_delta = (int)len - (int)matchlen;
|
2016-07-26 12:41:59 +00:00
|
|
|
out->distance = backward;
|
|
|
|
out->score = score;
|
|
|
|
return BROTLI_TRUE;
|
|
|
|
}
|
|
|
|
|
2017-07-21 08:07:24 +00:00
|
|
|
static BROTLI_INLINE void SearchInStaticDictionary(
|
2017-03-06 13:22:45 +00:00
|
|
|
const BrotliDictionary* dictionary, const uint16_t* dictionary_hash,
|
|
|
|
HasherHandle handle, const uint8_t* data, size_t max_length,
|
2016-07-26 12:41:59 +00:00
|
|
|
size_t max_backward, HasherSearchResult* out, BROTLI_BOOL shallow) {
|
|
|
|
size_t key;
|
|
|
|
size_t i;
|
2017-03-06 13:22:45 +00:00
|
|
|
HasherCommon* self = GetHasherCommon(handle);
|
|
|
|
if (self->dict_num_matches < (self->dict_num_lookups >> 7)) {
|
2017-07-21 08:07:24 +00:00
|
|
|
return;
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
|
|
|
key = Hash14(data) << 1;
|
2016-09-21 15:20:36 +00:00
|
|
|
for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) {
|
2017-03-06 13:22:45 +00:00
|
|
|
size_t item = dictionary_hash[key];
|
|
|
|
self->dict_num_lookups++;
|
|
|
|
if (item != 0) {
|
|
|
|
BROTLI_BOOL item_matches = TestStaticDictionaryItem(
|
|
|
|
dictionary, item, data, max_length, max_backward, out);
|
|
|
|
if (item_matches) {
|
|
|
|
self->dict_num_matches++;
|
|
|
|
}
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
typedef struct BackwardMatch {
|
|
|
|
uint32_t distance;
|
|
|
|
uint32_t length_and_code;
|
|
|
|
} BackwardMatch;
|
2015-06-12 14:25:41 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self,
|
|
|
|
size_t dist, size_t len) {
|
|
|
|
self->distance = (uint32_t)dist;
|
|
|
|
self->length_and_code = (uint32_t)(len << 5);
|
|
|
|
}
|
2015-06-12 14:25:41 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self,
|
|
|
|
size_t dist, size_t len, size_t len_code) {
|
|
|
|
self->distance = (uint32_t)dist;
|
|
|
|
self->length_and_code =
|
|
|
|
(uint32_t)((len << 5) | (len == len_code ? 0 : len_code));
|
|
|
|
}
|
2015-06-12 14:25:41 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) {
|
|
|
|
return self->length_and_code >> 5;
|
|
|
|
}
|
2015-06-12 14:25:41 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) {
|
|
|
|
size_t code = self->length_and_code & 31;
|
|
|
|
return code ? code : BackwardMatchLength(self);
|
|
|
|
}
|
2015-06-12 14:25:41 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#define EXPAND_CAT(a, b) CAT(a, b)
|
|
|
|
#define CAT(a, b) a ## b
|
|
|
|
#define FN(X) EXPAND_CAT(X, HASHER())
|
2014-10-28 12:25:22 +00:00
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#define HASHER() H10
|
|
|
|
#define BUCKET_BITS 17
|
2017-02-06 13:20:43 +00:00
|
|
|
#define MAX_TREE_SEARCH_DEPTH 64
|
|
|
|
#define MAX_TREE_COMP_LENGTH 128
|
|
|
|
#include "./hash_to_binary_tree_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef MAX_TREE_SEARCH_DEPTH
|
|
|
|
#undef MAX_TREE_COMP_LENGTH
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef BUCKET_BITS
|
|
|
|
#undef HASHER
|
2017-02-06 13:20:43 +00:00
|
|
|
/* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */
|
|
|
|
#define MAX_NUM_MATCHES_H10 128
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
/* For BUCKET_SWEEP == 1, enabling the dictionary lookup makes compression
|
|
|
|
a little faster (0.5% - 1%) and it compresses 0.15% better on small text
|
2016-10-31 13:33:59 +00:00
|
|
|
and HTML inputs. */
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
#define HASHER() H2
|
|
|
|
#define BUCKET_BITS 16
|
|
|
|
#define BUCKET_SWEEP 1
|
2017-02-06 13:20:43 +00:00
|
|
|
#define HASH_LEN 5
|
2016-06-13 09:01:04 +00:00
|
|
|
#define USE_DICTIONARY 1
|
|
|
|
#include "./hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef BUCKET_SWEEP
|
|
|
|
#undef USE_DICTIONARY
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H3
|
|
|
|
#define BUCKET_SWEEP 2
|
|
|
|
#define USE_DICTIONARY 0
|
|
|
|
#include "./hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef USE_DICTIONARY
|
|
|
|
#undef BUCKET_SWEEP
|
|
|
|
#undef BUCKET_BITS
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H4
|
|
|
|
#define BUCKET_BITS 17
|
|
|
|
#define BUCKET_SWEEP 4
|
|
|
|
#define USE_DICTIONARY 1
|
|
|
|
#include "./hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef USE_DICTIONARY
|
2017-02-06 13:20:43 +00:00
|
|
|
#undef HASH_LEN
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef BUCKET_SWEEP
|
|
|
|
#undef BUCKET_BITS
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H5
|
|
|
|
#include "./hash_longest_match_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H6
|
2017-03-06 13:22:45 +00:00
|
|
|
#include "./hash_longest_match64_inc.h" /* NOLINT(build/include) */
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef HASHER
|
|
|
|
|
2016-07-26 12:41:59 +00:00
|
|
|
#define BUCKET_BITS 15
|
|
|
|
|
|
|
|
#define NUM_LAST_DISTANCES_TO_CHECK 4
|
|
|
|
#define NUM_BANKS 1
|
|
|
|
#define BANK_BITS 16
|
|
|
|
#define HASHER() H40
|
|
|
|
#include "./hash_forgetful_chain_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef HASHER
|
|
|
|
#undef NUM_LAST_DISTANCES_TO_CHECK
|
|
|
|
|
|
|
|
#define NUM_LAST_DISTANCES_TO_CHECK 10
|
|
|
|
#define HASHER() H41
|
|
|
|
#include "./hash_forgetful_chain_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef HASHER
|
|
|
|
#undef NUM_LAST_DISTANCES_TO_CHECK
|
|
|
|
#undef NUM_BANKS
|
|
|
|
#undef BANK_BITS
|
|
|
|
|
|
|
|
#define NUM_LAST_DISTANCES_TO_CHECK 16
|
|
|
|
#define NUM_BANKS 512
|
|
|
|
#define BANK_BITS 9
|
|
|
|
#define HASHER() H42
|
|
|
|
#include "./hash_forgetful_chain_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef HASHER
|
|
|
|
#undef NUM_LAST_DISTANCES_TO_CHECK
|
|
|
|
#undef NUM_BANKS
|
|
|
|
#undef BANK_BITS
|
|
|
|
|
|
|
|
#undef BUCKET_BITS
|
|
|
|
|
2017-02-06 13:20:43 +00:00
|
|
|
#define HASHER() H54
|
|
|
|
#define BUCKET_BITS 20
|
|
|
|
#define BUCKET_SWEEP 4
|
|
|
|
#define HASH_LEN 7
|
|
|
|
#define USE_DICTIONARY 0
|
|
|
|
#include "./hash_longest_match_quickly_inc.h" /* NOLINT(build/include) */
|
|
|
|
#undef USE_DICTIONARY
|
|
|
|
#undef HASH_LEN
|
|
|
|
#undef BUCKET_SWEEP
|
|
|
|
#undef BUCKET_BITS
|
|
|
|
#undef HASHER
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef FN
|
|
|
|
#undef CAT
|
|
|
|
#undef EXPAND_CAT
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
#define FOR_GENERIC_HASHERS(H) H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)
|
2016-07-26 12:41:59 +00:00
|
|
|
#define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10)
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE void DestroyHasher(
|
|
|
|
MemoryManager* m, HasherHandle* handle) {
|
|
|
|
if (*handle == NULL) return;
|
|
|
|
BROTLI_FREE(m, *handle);
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE void HasherReset(HasherHandle handle) {
|
|
|
|
if (handle == NULL) return;
|
|
|
|
GetHasherCommon(handle)->is_prepared_ = BROTLI_FALSE;
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE size_t HasherSize(const BrotliEncoderParams* params,
|
|
|
|
BROTLI_BOOL one_shot, const size_t input_size) {
|
|
|
|
size_t result = sizeof(HasherCommon);
|
|
|
|
switch (params->hasher.type) {
|
|
|
|
#define SIZE_(N) \
|
|
|
|
case N: \
|
|
|
|
result += HashMemAllocInBytesH ## N(params, one_shot, input_size); \
|
|
|
|
break;
|
|
|
|
FOR_ALL_HASHERS(SIZE_)
|
|
|
|
#undef SIZE_
|
|
|
|
default:
|
|
|
|
break;
|
2016-07-26 12:41:59 +00:00
|
|
|
}
|
2017-03-06 13:22:45 +00:00
|
|
|
return result;
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
static BROTLI_INLINE void HasherSetup(MemoryManager* m, HasherHandle* handle,
|
|
|
|
BrotliEncoderParams* params, const uint8_t* data, size_t position,
|
|
|
|
size_t input_size, BROTLI_BOOL is_last) {
|
|
|
|
HasherHandle self = NULL;
|
|
|
|
HasherCommon* common = NULL;
|
|
|
|
BROTLI_BOOL one_shot = (position == 0 && is_last);
|
|
|
|
if (*handle == NULL) {
|
|
|
|
size_t alloc_size;
|
|
|
|
ChooseHasher(params, ¶ms->hasher);
|
|
|
|
alloc_size = HasherSize(params, one_shot, input_size);
|
|
|
|
self = BROTLI_ALLOC(m, uint8_t, alloc_size);
|
|
|
|
if (BROTLI_IS_OOM(m)) return;
|
|
|
|
*handle = self;
|
|
|
|
common = GetHasherCommon(self);
|
|
|
|
common->params = params->hasher;
|
|
|
|
switch (common->params.type) {
|
|
|
|
#define INITIALIZE_(N) \
|
|
|
|
case N: \
|
|
|
|
InitializeH ## N(*handle, params); \
|
|
|
|
break;
|
|
|
|
FOR_ALL_HASHERS(INITIALIZE_);
|
2017-02-06 13:20:43 +00:00
|
|
|
#undef INITIALIZE_
|
2017-03-06 13:22:45 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
HasherReset(*handle);
|
2017-02-06 13:20:43 +00:00
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
|
2017-03-06 13:22:45 +00:00
|
|
|
self = *handle;
|
|
|
|
common = GetHasherCommon(self);
|
|
|
|
if (!common->is_prepared_) {
|
|
|
|
switch (common->params.type) {
|
|
|
|
#define PREPARE_(N) \
|
|
|
|
case N: \
|
|
|
|
PrepareH ## N(self, one_shot, input_size, data); \
|
|
|
|
break;
|
|
|
|
FOR_ALL_HASHERS(PREPARE_)
|
|
|
|
#undef PREPARE_
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (position == 0) {
|
|
|
|
common->dict_num_lookups = 0;
|
|
|
|
common->dict_num_matches = 0;
|
|
|
|
}
|
|
|
|
common->is_prepared_ = BROTLI_TRUE;
|
|
|
|
}
|
2016-06-13 09:01:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 13:20:43 +00:00
|
|
|
static BROTLI_INLINE void InitOrStitchToPreviousBlock(
|
2017-03-06 13:22:45 +00:00
|
|
|
MemoryManager* m, HasherHandle* handle, const uint8_t* data, size_t mask,
|
|
|
|
BrotliEncoderParams* params, size_t position, size_t input_size,
|
|
|
|
BROTLI_BOOL is_last) {
|
|
|
|
HasherHandle self;
|
|
|
|
HasherSetup(m, handle, params, data, position, input_size, is_last);
|
|
|
|
if (BROTLI_IS_OOM(m)) return;
|
|
|
|
self = *handle;
|
|
|
|
switch (GetHasherCommon(self)->params.type) {
|
|
|
|
#define INIT_(N) \
|
|
|
|
case N: \
|
|
|
|
StitchToPreviousBlockH ## N(self, input_size, position, data, mask); \
|
2017-02-06 13:20:43 +00:00
|
|
|
break;
|
|
|
|
FOR_ALL_HASHERS(INIT_)
|
|
|
|
#undef INIT_
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|
2013-10-23 11:06:13 +00:00
|
|
|
|
2016-06-03 09:19:23 +00:00
|
|
|
#endif /* BROTLI_ENC_HASH_H_ */
|