MCPcopy Create free account
hub / github.com/F-Stack/f-stack / deflate_rle

Function deflate_rle

freebsd/contrib/zlib/deflate.c:2062–2129  ·  view source on GitHub ↗

=========================================================================== * For Z_RLE, simply look for runs of bytes, generate matches only of distance * one. Do not maintain a hash table. (It will be regenerated if this run of * deflate switches away from Z_RLE.) */

(s, flush)

Source from the content-addressed store, hash-verified

2060 * deflate switches away from Z_RLE.)
2061 */
2062local block_state deflate_rle(s, flush)
2063 deflate_state *s;
2064 int flush;
2065{
2066 int bflush; /* set if current block must be flushed */
2067 uInt prev; /* byte at distance one to match */
2068 Bytef *scan, *strend; /* scan goes up to strend for length of run */
2069
2070 for (;;) {
2071 /* Make sure that we always have enough lookahead, except
2072 * at the end of the input file. We need MAX_MATCH bytes
2073 * for the longest run, plus one for the unrolled loop.
2074 */
2075 if (s->lookahead <= MAX_MATCH) {
2076 fill_window(s);
2077 if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
2078 return need_more;
2079 }
2080 if (s->lookahead == 0) break; /* flush the current block */
2081 }
2082
2083 /* See how many times the previous byte repeats */
2084 s->match_length = 0;
2085 if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
2086 scan = s->window + s->strstart - 1;
2087 prev = *scan;
2088 if (prev == *++scan && prev == *++scan && prev == *++scan) {
2089 strend = s->window + s->strstart + MAX_MATCH;
2090 do {
2091 } while (prev == *++scan && prev == *++scan &&
2092 prev == *++scan && prev == *++scan &&
2093 prev == *++scan && prev == *++scan &&
2094 prev == *++scan && prev == *++scan &&
2095 scan < strend);
2096 s->match_length = MAX_MATCH - (uInt)(strend - scan);
2097 if (s->match_length > s->lookahead)
2098 s->match_length = s->lookahead;
2099 }
2100 Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan");
2101 }
2102
2103 /* Emit match if have run of MIN_MATCH or longer, else emit literal */
2104 if (s->match_length >= MIN_MATCH) {
2105 check_match(s, s->strstart, s->strstart - 1, s->match_length);
2106
2107 _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
2108
2109 s->lookahead -= s->match_length;
2110 s->strstart += s->match_length;
2111 s->match_length = 0;
2112 } else {
2113 /* No match, output a literal byte */
2114 Tracevv((stderr,"%c", s->window[s->strstart]));
2115 _tr_tally_lit (s, s->window[s->strstart], bflush);
2116 s->lookahead--;
2117 s->strstart++;
2118 }
2119 if (bflush) FLUSH_BLOCK(s, 0);

Callers 1

deflateFunction · 0.85

Calls 2

fill_windowFunction · 0.85
check_matchFunction · 0.70

Tested by

no test coverage detected