2015-11-27 10:27:11 +00:00
|
|
|
/* Copyright 2013 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
|
|
|
/* Function to find backward reference copies. */
|
2013-10-23 11:06:13 +00:00
|
|
|
|
|
|
|
#include "./backward_references.h"
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#include "../common/constants.h"
|
2019-04-12 11:57:42 +00:00
|
|
|
#include "../common/context.h"
|
2017-03-06 13:22:45 +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 "./command.h"
|
2017-03-06 13:22:45 +00:00
|
|
|
#include "./dictionary_hash.h"
|
2016-06-13 09:01:04 +00:00
|
|
|
#include "./memory.h"
|
2016-07-26 12:41:59 +00:00
|
|
|
#include "./quality.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
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance,
|
|
|
|
size_t max_distance,
|
|
|
|
const int* dist_cache) {
|
2015-06-12 14:25:41 +00:00
|
|
|
if (distance <= max_distance) {
|
2016-07-26 12:41:59 +00:00
|
|
|
size_t distance_plus_3 = distance + 3;
|
|
|
|
size_t offset0 = distance_plus_3 - (size_t)dist_cache[0];
|
|
|
|
size_t offset1 = distance_plus_3 - (size_t)dist_cache[1];
|
2016-06-13 09:01:04 +00:00
|
|
|
if (distance == (size_t)dist_cache[0]) {
|
2015-06-12 14:25:41 +00:00
|
|
|
return 0;
|
2016-06-13 09:01:04 +00:00
|
|
|
} else if (distance == (size_t)dist_cache[1]) {
|
2015-06-12 14:25:41 +00:00
|
|
|
return 1;
|
2016-07-26 12:41:59 +00:00
|
|
|
} else if (offset0 < 7) {
|
|
|
|
return (0x9750468 >> (4 * offset0)) & 0xF;
|
|
|
|
} else if (offset1 < 7) {
|
|
|
|
return (0xFDB1ACE >> (4 * offset1)) & 0xF;
|
2016-06-13 09:01:04 +00:00
|
|
|
} else if (distance == (size_t)dist_cache[2]) {
|
2015-06-12 14:25:41 +00:00
|
|
|
return 2;
|
2016-06-13 09:01:04 +00:00
|
|
|
} else if (distance == (size_t)dist_cache[3]) {
|
2015-06-12 14:25:41 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 15:33:12 +00:00
|
|
|
return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1;
|
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())
|
2017-08-04 08:02:56 +00:00
|
|
|
#define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER()))
|
2018-03-20 11:37:41 +00:00
|
|
|
|
2017-08-04 08:02:56 +00:00
|
|
|
#define PREFIX() N
|
2016-06-13 09:01:04 +00:00
|
|
|
|
|
|
|
#define HASHER() H2
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H3
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H4
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H5
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H6
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
2016-07-26 12:41:59 +00:00
|
|
|
#define HASHER() H40
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H41
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H42
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
2017-02-06 13:20:43 +00:00
|
|
|
#define HASHER() H54
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
2018-06-20 13:14:10 +00:00
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H35
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H55
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
|
|
|
#undef HASHER
|
|
|
|
|
|
|
|
#define HASHER() H65
|
|
|
|
/* NOLINTNEXTLINE(build/include) */
|
|
|
|
#include "./backward_references_inc.h"
|
2017-02-06 13:20:43 +00:00
|
|
|
#undef HASHER
|
|
|
|
|
2017-08-04 08:02:56 +00:00
|
|
|
#undef PREFIX
|
2018-03-20 11:37:41 +00:00
|
|
|
|
2017-08-04 08:02:56 +00:00
|
|
|
#undef EXPORT_FN
|
2016-06-13 09:01:04 +00:00
|
|
|
#undef FN
|
|
|
|
#undef CAT
|
|
|
|
#undef EXPAND_CAT
|
|
|
|
|
2019-04-12 11:57:42 +00:00
|
|
|
void BrotliCreateBackwardReferences(size_t num_bytes,
|
|
|
|
size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
|
|
|
|
ContextLut literal_context_lut, const BrotliEncoderParams* params,
|
|
|
|
Hasher* hasher, int* dist_cache, size_t* last_insert_len,
|
2018-02-26 14:04:36 +00:00
|
|
|
Command* commands, size_t* num_commands, size_t* num_literals) {
|
2017-03-06 13:22:45 +00:00
|
|
|
switch (params->hasher.type) {
|
|
|
|
#define CASE_(N) \
|
|
|
|
case N: \
|
2019-04-12 11:57:42 +00:00
|
|
|
CreateBackwardReferencesNH ## N(num_bytes, \
|
|
|
|
position, ringbuffer, ringbuffer_mask, \
|
|
|
|
literal_context_lut, params, hasher, dist_cache, \
|
2017-03-06 13:22:45 +00:00
|
|
|
last_insert_len, commands, num_commands, num_literals); \
|
2017-08-04 08:02:56 +00:00
|
|
|
return;
|
2016-08-22 11:28:22 +00:00
|
|
|
FOR_GENERIC_HASHERS(CASE_)
|
|
|
|
#undef CASE_
|
2014-03-20 13:32:35 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:01:04 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif
|