MCPcopy Create free account
hub / github.com/RsyncProject/rsync / longest_match

Function longest_match

zlib/deflate.c:1140–1279  ·  view source on GitHub ↗

For 80x86 and 680x0, an optimized version will be provided in match.asm or * match.S. The code will be functionally equivalent. */

Source from the content-addressed store, hash-verified

1138 * match.S. The code will be functionally equivalent.
1139 */
1140local uInt longest_match(deflate_state *s, IPos cur_match)
1141{
1142 unsigned chain_length = s->max_chain_length;/* max hash chain length */
1143 register Bytef *scan = s->window + s->strstart; /* current string */
1144 register Bytef *match; /* matched string */
1145 register int len; /* length of current match */
1146 int best_len = s->prev_length; /* best match length so far */
1147 int nice_match = s->nice_match; /* stop if match long enough */
1148 IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
1149 s->strstart - (IPos)MAX_DIST(s) : NIL;
1150 /* Stop when cur_match becomes <= limit. To simplify the code,
1151 * we prevent matches with the string of window index 0.
1152 */
1153 Posf *prev = s->prev;
1154 uInt wmask = s->w_mask;
1155
1156#ifdef UNALIGNED_OK
1157 /* Compare two bytes at a time. Note: this is not always beneficial.
1158 * Try with and without -DUNALIGNED_OK to check.
1159 */
1160 register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
1161 register ush scan_start = *(ushf*)scan;
1162 register ush scan_end = *(ushf*)(scan+best_len-1);
1163#else
1164 register Bytef *strend = s->window + s->strstart + MAX_MATCH;
1165 register Byte scan_end1 = scan[best_len-1];
1166 register Byte scan_end = scan[best_len];
1167#endif
1168
1169 /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
1170 * It is easy to get rid of this optimization if necessary.
1171 */
1172 Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
1173
1174 /* Do not waste too much time if we already have a good match: */
1175 if (s->prev_length >= s->good_match) {
1176 chain_length >>= 2;
1177 }
1178 /* Do not look for matches beyond the end of the input. This is necessary
1179 * to make deflate deterministic.
1180 */
1181 if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
1182
1183 Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
1184
1185 do {
1186 Assert(cur_match < s->strstart, "no future");
1187 match = s->window + cur_match;
1188
1189 /* Skip to next match if the match length cannot increase
1190 * or if the match length is less than 2. Note that the checks below
1191 * for insufficient lookahead only occur occasionally for performance
1192 * reasons. Therefore uninitialized memory will be accessed, and
1193 * conditional jumps will be made that depend on those values.
1194 * However the length of the match is limited to the lookahead, so
1195 * the output of deflate is not affected by the uninitialized values.
1196 */
1197#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)

Callers 2

deflate_fastFunction · 0.85
deflate_slowFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected