=========================================================================== * Set match_start to the longest match starting at the given string and * return its length. Matches shorter or equal to prev_length are discarded, * in which case the result is equal to prev_length and match_start is * garbage. * IN assertions: cur_match is the head of the hash chain for the current * string (strs
| 1346 | * OUT assertion: the match length is not greater than s->lookahead. |
| 1347 | */ |
| 1348 | local uInt longest_match(deflate_state *s, IPos cur_match) { |
| 1349 | unsigned chain_length = s->max_chain_length;/* max hash chain length */ |
| 1350 | register Bytef *scan = s->window + s->strstart; /* current string */ |
| 1351 | register Bytef *match; /* matched string */ |
| 1352 | register int len; /* length of current match */ |
| 1353 | int best_len = (int)s->prev_length; /* best match length so far */ |
| 1354 | int nice_match = s->nice_match; /* stop if match long enough */ |
| 1355 | IPos limit = s->strstart > (IPos)MAX_DIST(s) ? |
| 1356 | s->strstart - (IPos)MAX_DIST(s) : NIL; |
| 1357 | /* Stop when cur_match becomes <= limit. To simplify the code, |
| 1358 | * we prevent matches with the string of window index 0. |
| 1359 | */ |
| 1360 | Posf *prev = s->prev; |
| 1361 | uInt wmask = s->w_mask; |
| 1362 | |
| 1363 | #ifdef UNALIGNED_OK |
| 1364 | /* Compare two bytes at a time. Note: this is not always beneficial. |
| 1365 | * Try with and without -DUNALIGNED_OK to check. |
| 1366 | */ |
| 1367 | register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; |
| 1368 | register ush scan_start = *(ushf*)scan; |
| 1369 | register ush scan_end = *(ushf*)(scan + best_len - 1); |
| 1370 | #else |
| 1371 | register Bytef *strend = s->window + s->strstart + MAX_MATCH; |
| 1372 | register Byte scan_end1 = scan[best_len - 1]; |
| 1373 | register Byte scan_end = scan[best_len]; |
| 1374 | #endif |
| 1375 | |
| 1376 | /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. |
| 1377 | * It is easy to get rid of this optimization if necessary. |
| 1378 | */ |
| 1379 | Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); |
| 1380 | |
| 1381 | /* Do not waste too much time if we already have a good match: */ |
| 1382 | if (s->prev_length >= s->good_match) { |
| 1383 | chain_length >>= 2; |
| 1384 | } |
| 1385 | /* Do not look for matches beyond the end of the input. This is necessary |
| 1386 | * to make deflate deterministic. |
| 1387 | */ |
| 1388 | if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; |
| 1389 | |
| 1390 | Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, |
| 1391 | "need lookahead"); |
| 1392 | |
| 1393 | do { |
| 1394 | Assert(cur_match < s->strstart, "no future"); |
| 1395 | match = s->window + cur_match; |
| 1396 | |
| 1397 | /* Skip to next match if the match length cannot increase |
| 1398 | * or if the match length is less than 2. Note that the checks below |
| 1399 | * for insufficient lookahead only occur occasionally for performance |
| 1400 | * reasons. Therefore uninitialized memory will be accessed, and |
| 1401 | * conditional jumps will be made that depend on those values. |
| 1402 | * However the length of the match is limited to the lookahead, so |
| 1403 | * the output of deflate is not affected by the uninitialized values. |
| 1404 | */ |
| 1405 | #if (defined(UNALIGNED_OK) && MAX_MATCH == 258) |
no outgoing calls
no test coverage detected