Results 1 to 9 of 9

Thread: zlib 1.2.4 LC2 patch

  1. #1
    Member
    Join Date
    May 2008
    Location
    HK
    Posts
    160
    Thanks
    4
    Thanked 25 Times in 15 Posts

    zlib 1.2.4 LC2 patch

    Updated LC2 patch:
    Code:
    --- deflate.c.org	2010-02-28 01:07:26.000000000 +0800
    +++ deflate.c	2010-03-28 12:04:04.985125000 +0800
    @@ -138,11 +138,11 @@
     /* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
     /* 5 */ {8,   16, 32,   32, deflate_slow},
     /* 6 */ {8,   16, 128, 128, deflate_slow},
     /* 7 */ {8,   32, 128, 256, deflate_slow},
     /* 8 */ {32, 128, 258, 1024, deflate_slow},
    -/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
    +/* 9 */ {32, 258, 258, 8192, deflate_fast}}; /* max compression */
     #endif
     
     /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
      * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
      * meaning.
    @@ -1554,11 +1554,49 @@
                  * of window index 0 (in particular we have to avoid a match
                  * of the string with itself at the start of the input file).
                  */
                 s->match_length = longest_match (s, hash_head);
                 /* longest_match() sets match_start */
    +            if ((s->level == 9) && (s->match_length == MIN_MATCH) && ((s->strstart-s->match_start) > (1 << 14))){
    +                s->match_length = 0; /* discard a small match, if it too distant */
    +            }
             }
    +
    +        /* a small HACK written in 5 minutes
    +        TODO: write more clever implementation
    +        */
    +        if (s->match_length >= MIN_MATCH && s->level == 9) {
    +          int tmp_strstart = s->strstart; /* store global variables */
    +          int tmp_match_start = s->match_start;
    +          int dist = s->strstart-s->match_start;
    +          int next_len;
    +          int next_dist;
    +          unsigned hash = s->ins_h;
    +          int i;
    +
    +          /* lazy matching with 2 byte lookahead */
    +          for (i = 0; i < 2; i++) {
    +            UPDATE_HASH(s, hash, s->window[(++s->strstart) + MIN_MATCH-1]);
    +
    +            next_len = longest_match (s, s->head[hash]); /* get match length and distance */
    +            next_dist = s->strstart-s->match_start;
    +
    +            /* check for a better match,
    +            also check the distance of the followed match */
    +            if ((next_len > ((s->match_length + 1) + i)) 
    +              || ((next_len > (s->match_length + i)) && ((next_dist >> 3) < dist))) {
    +
    +              s->match_length = 0; /* discard current match */
    +              break;
    +            }
    +          }
    +
    +          s->strstart = tmp_strstart; /* restore values */
    +          s->match_start = tmp_match_start;
    +        }
    +		/* End of hack */
    +
             if (s->match_length >= MIN_MATCH) {
                 check_match(s, s->strstart, s->match_start, s->match_length);
     
                 _tr_tally_dist(s, s->strstart - s->match_start,
                                s->match_length - MIN_MATCH, bflush);

  2. #2
    Member
    Join Date
    Jun 2008
    Location
    USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, dumb question, but what is "LC2"? This looks like Ilia's Gzip 1.2.4 "fast -9" hack from a few years ago. And yes, I see now that ZLIB 1.2.4 has actually been released two weeks ago! (Finally!!)

  3. #3
    Member
    Join Date
    May 2008
    Location
    HK
    Posts
    160
    Thanks
    4
    Thanked 25 Times in 15 Posts
    LC2 = Lazy lookahead 2 bytes IIRC. ("LC" sounds like "lazy", and without confusing with LZ algorithms)
    And it is just a port from my patch for zlib 1.2.3 to 1.2.4.
    As optipng-0.6.4 use zlib-1.2.4 now.

  4. #4
    The Founder encode's Avatar
    Join Date
    May 2006
    Location
    Moscow, Russia
    Posts
    3,954
    Thanks
    359
    Thanked 332 Times in 131 Posts
    Guess who implemented a 2-byte lookahead deflate first...

  5. #5
    Member
    Join Date
    Jun 2008
    Location
    USA
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by encode View Post
    Guess who implemented a 2-byte lookahead deflate first...
    Alan Smithee?

    P.S. Here's another patch for ZLIB (well, to be more precise, for /contrib/puff/puff.c ):

    Code:
    From: Mark Adler         Sun, Apr 4, 2010 at 1:28 PM
    To: Rugxulo
    Cc: jloup
    
    On Apr 4, 2010, at 8:35 AM, Rugxulo wrote:
    > PUFF.C(646): Error! E1054: Expression must be constant
    
    Below is a patch to fix the problem.
    
    Mark
    
    --- ../../../zlib-1.2.4/contrib/puff/puff.c     2008-07-27 11:58:53.000000000 -0700
    +++ puff.c      2010-04-04 11:23:58.000000000 -0700
    @@ -1,8 +1,8 @@
     /*
     * puff.c
    - * Copyright (C) 2002-2008 Mark Adler
    + * Copyright (C) 2002-2010 Mark Adler
     * For conditions of distribution and use, see copyright notice in puff.h
    - * version 2.0, 25 Jul 2008
    + * version 2.1, 4 Apr 2010
     *
     * puff.c is a simple inflate written to be an unambiguous way to specify the
     * deflate format.  It is not written for speed but rather simplicity.  As a
    @@ -67,6 +67,8 @@
     *                      - Add option in TEST code for puff to write the data
     *                      - Add option in TEST code to skip input bytes
     *                      - Allow TEST code to read from piped stdin
    + * 2.1   4 Apr 2010     - Avoid variable initialization for happier compilers
    + *                      - Avoid unsigned comparisons for even happier compilers
     */
    
     #include <setjmp.h>             /* for setjmp(), longjmp(), and jmp_buf */
    @@ -516,8 +518,7 @@
        static int virgin = 1;
        static short lencnt[MAXBITS+1], lensym[FIXLCODES];
        static short distcnt[MAXBITS+1], distsym[MAXDCODES];
    -    static struct huffman lencode = {lencnt, lensym};
    -    static struct huffman distcode = {distcnt, distsym};
    +    static struct huffman lencode, distcode;
    
        /* build fixed huffman tables if first call (may not be thread safe) */
        if (virgin) {
    @@ -540,6 +541,12 @@
                lengths[symbol] = 5;
            construct(&distcode, lengths, MAXDCODES);
    
    +        /* construct lencode and distcode */
    +        lencode.count = lencnt;
    +        lencode.symbol = lensym;
    +        distcode.count = distcnt;
    +        distcode.symbol = distsym;
    +
            /* do this just once */
            virgin = 0;
        }
    @@ -643,11 +650,16 @@
        short lengths[MAXCODES];            /* descriptor code lengths */
        short lencnt[MAXBITS+1], lensym[MAXLCODES];         /* lencode memory */
        short distcnt[MAXBITS+1], distsym[MAXDCODES];       /* distcode memory */
    -    struct huffman lencode = {lencnt, lensym};          /* length code */
    -    struct huffman distcode = {distcnt, distsym};       /* distance code */
    +    struct huffman lencode, distcode;   /* length and distance codes */
        static const short order[19] =      /* permutation of code length codes */
            {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
    
    +    /* construct lencode and distcode */
    +    lencode.count = lencnt;
    +    lencode.symbol = lensym;
    +    distcode.count = distcnt;
    +    distcode.symbol = distsym;
    +
        /* get number of lengths in each table, check lengths */
        nlen = bits(s, 5) + 257;
        ndist = bits(s, 5) + 1;
    @@ -869,7 +881,8 @@
    
     int main(int argc, char **argv)
     {
    -    int ret, skip = 0, put = 0;
    +    int ret, put = 0;
    +    unsigned skip = 0;
        char *arg, *name = NULL;
        unsigned char *source = NULL, *dest;
        size_t len = 0;
    @@ -881,7 +894,7 @@
                if (arg[1] == 'w' && arg[2] == 0)
                    put = 1;
                else if (arg[1] >= '0' && arg[1] <= '9')
    -                skip = atoi(arg + 1);
    +                skip = (unsigned)atoi(arg + 1);
                else {
                    fprintf(stderr, "invalid option %s\n", arg);
                    return 3;
    --- ../../../zlib-1.2.4/contrib/puff/puff.h     2008-01-10 22:05:09.000000000 -0800
    +++ puff.h      2010-04-04 11:12:28.000000000 -0700
    @@ -1,6 +1,6 @@
     /* puff.h
    -  Copyright (C) 2002-2008 Mark Adler, all rights reserved
    -  version 1.9, 10 Jan 2008
    +  Copyright (C) 2002-2010 Mark Adler, all rights reserved
    +  version 2.1, 4 Apr 2010
    
      This software is provided 'as-is', without any express or implied
      warranty.  In no event will the author be held liable for any damages

  6. #6

  7. #7
    Member
    Join Date
    May 2008
    Location
    HK
    Posts
    160
    Thanks
    4
    Thanked 25 Times in 15 Posts
    zlib 1.2.5 is out.

    LC2 patch can apply without change.

  8. #8
    Member
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    168
    Thanks
    163
    Thanked 9 Times in 8 Posts
    please update the patch for the version of zlib-develop.

  9. #9
    The Founder encode's Avatar
    Join Date
    May 2006
    Location
    Moscow, Russia
    Posts
    3,954
    Thanks
    359
    Thanked 332 Times in 131 Posts
    Dunno if this code will actually work, but, here is what I've typed on my iPad:
    Attached Files Attached Files

  10. The Following User Says Thank You to encode For This Useful Post:

    lorents17 (5th December 2016)

Similar Threads

  1. Executable patch generation methods
    By Shelwien in forum Data Compression
    Replies: 2
    Last Post: 2nd April 2010, 09:13
  2. inflate for zlib v1.2.3
    By bartek in forum Data Compression
    Replies: 1
    Last Post: 15th December 2009, 13:18
  3. zlib-compatible alternatives
    By Cyan in forum Data Compression
    Replies: 0
    Last Post: 12th May 2009, 01:28
  4. DEFLATE/zlib implementations
    By GerryB in forum Data Compression
    Replies: 10
    Last Post: 7th May 2009, 17:03
  5. QuickLZ-zlib wrapper
    By Lasse Reinhold in forum Data Compression
    Replies: 1
    Last Post: 19th March 2009, 10:25

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •