| 1 | /* Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | Distributed under MIT license. |
| 4 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 | */ |
| 6 | |
| 7 | /* Function to find backward reference copies. */ |
| 8 | |
| 9 | #include "./backward_references.h" |
| 10 | |
| 11 | #include "../common/constants.h" |
| 12 | #include "../common/dictionary.h" |
| 13 | #include "../common/platform.h" |
| 14 | #include <brotli/types.h> |
| 15 | #include "./command.h" |
| 16 | #include "./dictionary_hash.h" |
| 17 | #include "./memory.h" |
| 18 | #include "./quality.h" |
| 19 | |
| 20 | #if defined(__cplusplus) || defined(c_plusplus) |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance, |
| 25 | size_t max_distance, |
| 26 | const int* dist_cache) { |
| 27 | if (distance <= max_distance) { |
| 28 | size_t distance_plus_3 = distance + 3; |
| 29 | size_t offset0 = distance_plus_3 - (size_t)dist_cache[0]; |
| 30 | size_t offset1 = distance_plus_3 - (size_t)dist_cache[1]; |
| 31 | if (distance == (size_t)dist_cache[0]) { |
| 32 | return 0; |
| 33 | } else if (distance == (size_t)dist_cache[1]) { |
| 34 | return 1; |
| 35 | } else if (offset0 < 7) { |
| 36 | return (0x9750468 >> (4 * offset0)) & 0xF; |
| 37 | } else if (offset1 < 7) { |
| 38 | return (0xFDB1ACE >> (4 * offset1)) & 0xF; |
| 39 | } else if (distance == (size_t)dist_cache[2]) { |
| 40 | return 2; |
| 41 | } else if (distance == (size_t)dist_cache[3]) { |
| 42 | return 3; |
| 43 | } |
| 44 | } |
| 45 | return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1; |
| 46 | } |
| 47 | |
| 48 | #define EXPAND_CAT(a, b) CAT(a, b) |
| 49 | #define CAT(a, b) a ## b |
| 50 | #define FN(X) EXPAND_CAT(X, HASHER()) |
| 51 | #define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER())) |
| 52 | |
| 53 | #define PREFIX() N |
| 54 | |
| 55 | #define HASHER() H2 |
| 56 | /* NOLINTNEXTLINE(build/include) */ |
| 57 | #include "./backward_references_inc.h" |
| 58 | #undef HASHER |
| 59 | |
| 60 | #define HASHER() H3 |
| 61 | /* NOLINTNEXTLINE(build/include) */ |
| 62 | #include "./backward_references_inc.h" |
| 63 | #undef HASHER |
| 64 | |
| 65 | #define HASHER() H4 |
| 66 | /* NOLINTNEXTLINE(build/include) */ |
| 67 | #include "./backward_references_inc.h" |
| 68 | #undef HASHER |
| 69 | |
| 70 | #define HASHER() H5 |
| 71 | /* NOLINTNEXTLINE(build/include) */ |
| 72 | #include "./backward_references_inc.h" |
| 73 | #undef HASHER |
| 74 | |
| 75 | #define HASHER() H6 |
| 76 | /* NOLINTNEXTLINE(build/include) */ |
| 77 | #include "./backward_references_inc.h" |
| 78 | #undef HASHER |
| 79 | |
| 80 | #define HASHER() H40 |
| 81 | /* NOLINTNEXTLINE(build/include) */ |
| 82 | #include "./backward_references_inc.h" |
| 83 | #undef HASHER |
| 84 | |
| 85 | #define HASHER() H41 |
| 86 | /* NOLINTNEXTLINE(build/include) */ |
| 87 | #include "./backward_references_inc.h" |
| 88 | #undef HASHER |
| 89 | |
| 90 | #define HASHER() H42 |
| 91 | /* NOLINTNEXTLINE(build/include) */ |
| 92 | #include "./backward_references_inc.h" |
| 93 | #undef HASHER |
| 94 | |
| 95 | #define HASHER() H54 |
| 96 | /* NOLINTNEXTLINE(build/include) */ |
| 97 | #include "./backward_references_inc.h" |
| 98 | #undef HASHER |
| 99 | |
| 100 | #define HASHER() H35 |
| 101 | /* NOLINTNEXTLINE(build/include) */ |
| 102 | #include "./backward_references_inc.h" |
| 103 | #undef HASHER |
| 104 | |
| 105 | #define HASHER() H55 |
| 106 | /* NOLINTNEXTLINE(build/include) */ |
| 107 | #include "./backward_references_inc.h" |
| 108 | #undef HASHER |
| 109 | |
| 110 | #define HASHER() H65 |
| 111 | /* NOLINTNEXTLINE(build/include) */ |
| 112 | #include "./backward_references_inc.h" |
| 113 | #undef HASHER |
| 114 | |
| 115 | #undef PREFIX |
| 116 | |
| 117 | #undef EXPORT_FN |
| 118 | #undef FN |
| 119 | #undef CAT |
| 120 | #undef EXPAND_CAT |
| 121 | |
| 122 | void BrotliCreateBackwardReferences( |
| 123 | size_t num_bytes, size_t position, const uint8_t* ringbuffer, |
| 124 | size_t ringbuffer_mask, const BrotliEncoderParams* params, |
| 125 | HasherHandle hasher, int* dist_cache, size_t* last_insert_len, |
| 126 | Command* commands, size_t* num_commands, size_t* num_literals) { |
| 127 | switch (params->hasher.type) { |
| 128 | #define CASE_(N) \ |
| 129 | case N: \ |
| 130 | CreateBackwardReferencesNH ## N( \ |
| 131 | num_bytes, position, ringbuffer, \ |
| 132 | ringbuffer_mask, params, hasher, dist_cache, \ |
| 133 | last_insert_len, commands, num_commands, num_literals); \ |
| 134 | return; |
| 135 | FOR_GENERIC_HASHERS(CASE_) |
| 136 | #undef CASE_ |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #if defined(__cplusplus) || defined(c_plusplus) |
| 143 | } /* extern "C" */ |
| 144 | #endif |
| 145 | |