如何修改zlib MIN_MATCH值?

时间:2021-02-18 04:14:49

I'm working with zlib 1.2.8 source code from http://zlib.net/
In the code, the minimum match value 'MIN_MATCH' is '3' (in zutil.h)

我正在使用来自http://zlib.net/的zlib 1.2.8源代码。在代码中,最小匹配值'MIN_MATCH'是'3'(在zutil.h中)

Now I want to modify that value from 3 to 4, so I modified the zutil.h

现在我想将该值从3修改为4,所以我修改了zutil.h

and there is a code in deflate.c

并且deflate.c中有一个代码

    /* Initialize the hash value now that we have some input: */
    if (s->lookahead + s->insert >= MIN_MATCH) {
        uInt str = s->strstart - s->insert;
        s->ins_h = s->window[str];
        UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
#if MIN_MATCH != 3
        Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
        while (s->insert) {
            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
#ifndef FASTEST
            s->prev[str & s->w_mask] = s->head[s->ins_h];
#endif
            s->head[s->ins_h] = (Pos)str;
            str++;
            s->insert--;
            if (s->lookahead + s->insert < MIN_MATCH)
                break;
        }
    }

and

            s->strstart += s->match_length;
            s->match_length = 0;
            s->ins_h = s->window[s->strstart];
            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
            Call UPDATE_HASH() MIN_MATCH-3 more times
#endif
            /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
             * matter since it will be recomputed at next deflate call.
             */

so I modified the code like below

所以我修改了下面的代码

    /* Initialize the hash value now that we have some input: */
    if (s->lookahead + s->insert >= MIN_MATCH) {
        uInt str = s->strstart - s->insert;
        s->ins_h = s->window[str];
        UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
#if MIN_MATCH != 3
        //Call UPDATE_HASH() MIN_MATCH-3 more times
        UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
#endif
        while (s->insert) {
            UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
#ifndef FASTEST
            s->prev[str & s->w_mask] = s->head[s->ins_h];
#endif
            s->head[s->ins_h] = (Pos)str;
            str++;
            s->insert--;
            if (s->lookahead + s->insert < MIN_MATCH)
                break;
        }
    }

and

            s->strstart += s->match_length;
            s->match_length = 0;
            s->ins_h = s->window[s->strstart];
            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#if MIN_MATCH != 3
            //Call UPDATE_HASH() MIN_MATCH-3 more times
            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
#endif
            /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
             * matter since it will be recomputed at next deflate call.
             */

(I put the UPDATE_HASH() function under the 'if MIN_MATCH != 3' line)

(我把UPDATE_HASH()函数放在'if MIN_MATCH!= 3'行下面)

and I compile, run the 'minigzip' test program with The Canterbury Corpus benchmark file

然后我编译,用Canterbury Corpus基准文件运行'minigzip'测试程序

compress works without error, but decompress not working with 'incorrect data check' error message

压缩工作没有错误,但解压缩不使用'错误的数据检查'错误消息

How should I modify the code? Anybody have idea?

我该如何修改代码?有人有想法吗?

thanks

1 个解决方案

#1


The MIN_MATCH of 3 is built deeply into the code, so the one comment about repeating an update doesn't represent all the changes that would be required. You would have to read and understand the algorithms used, make a fair number of changes, and then test them extensively to make sure you did it right.

MIN_MATCH为3深深地构建在代码中,因此关于重复更新的一个注释并不代表所需的所有更改。您必须阅读并理解所使用的算法,进行相当多的更改,然后对其进行广泛测试以确保您做得正确。

If all you're trying to do is emit a literal instead of a match of 3, then you could change if (s->match_length >= MIN_MATCH) to if (s->match_length >= 4).

如果您要做的只是发出文字而不是3的匹配,那么您可以将if(s-> match_length> = MIN_MATCH)更改为if(s-> match_length> = 4)。

#1


The MIN_MATCH of 3 is built deeply into the code, so the one comment about repeating an update doesn't represent all the changes that would be required. You would have to read and understand the algorithms used, make a fair number of changes, and then test them extensively to make sure you did it right.

MIN_MATCH为3深深地构建在代码中,因此关于重复更新的一个注释并不代表所需的所有更改。您必须阅读并理解所使用的算法,进行相当多的更改,然后对其进行广泛测试以确保您做得正确。

If all you're trying to do is emit a literal instead of a match of 3, then you could change if (s->match_length >= MIN_MATCH) to if (s->match_length >= 4).

如果您要做的只是发出文字而不是3的匹配,那么您可以将if(s-> match_length> = MIN_MATCH)更改为if(s-> match_length> = 4)。