]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Style cleanup for compress/uncompress and longest_match code
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Mon, 25 May 2015 21:04:39 +0000 (23:04 +0200)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Mon, 25 May 2015 21:04:39 +0000 (23:04 +0200)
compress.c
match.c
match.h
uncompr.c

index 679c50381d191a0bc1ccfd8f3ab2caf415811a4f..0bf1b0fc28cd6e7d7c36a1988cfa1e096655dcab 100644 (file)
@@ -19,9 +19,8 @@
    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
    Z_STREAM_ERROR if the level parameter is invalid.
 */
-int ZEXPORT compress2 (unsigned char *dest, uLong *destLen, const unsigned char *source,
-                        uLong sourceLen, int level)
-{
+int ZEXPORT compress2(unsigned char *dest, uLong *destLen, const unsigned char *source,
+                        uLong sourceLen, int level) {
     z_stream stream;
     int err;
 
@@ -29,14 +28,16 @@ int ZEXPORT compress2 (unsigned char *dest, uLong *destLen, const unsigned char
     stream.avail_in = (uInt)sourceLen;
     stream.next_out = dest;
     stream.avail_out = (uInt)*destLen;
-    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+    if ((uLong)stream.avail_out != *destLen)
+        return Z_BUF_ERROR;
 
     stream.zalloc = (alloc_func)0;
     stream.zfree = (free_func)0;
     stream.opaque = NULL;
 
     err = deflateInit(&stream, level);
-    if (err != Z_OK) return err;
+    if (err != Z_OK)
+        return err;
 
     err = deflate(&stream, Z_FINISH);
     if (err != Z_STREAM_END) {
@@ -51,8 +52,7 @@ int ZEXPORT compress2 (unsigned char *dest, uLong *destLen, const unsigned char
 
 /* ===========================================================================
  */
-int ZEXPORT compress (unsigned char *dest, uLong *destLen, const unsigned char *source, uLong sourceLen)
-{
+int ZEXPORT compress(unsigned char *dest, uLong *destLen, const unsigned char *source, uLong sourceLen) {
     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
 }
 
@@ -60,8 +60,7 @@ int ZEXPORT compress (unsigned char *dest, uLong *destLen, const unsigned char *
      If the default memLevel or windowBits for deflateInit() is changed, then
    this function needs to be updated.
  */
-uLong ZEXPORT compressBound (uLong sourceLen)
-{
+uLong ZEXPORT compressBound(uLong sourceLen) {
     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
            (sourceLen >> 25) + 13;
 }
diff --git a/match.c b/match.c
index ae9256692404525be5709f6d27e45f01278573a9..f8afa16ba8d2832c9b3b50c6df80dc5b6a9fb154 100644 (file)
--- a/match.c
+++ b/match.c
@@ -4,7 +4,7 @@
  * 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 (strstart) and its distance is <= MAX_DIST, and prev_length >=1
+ * string (strstart) and its distance is <= MAX_DIST, and prev_length >=1
  * OUT assertion: the match length is not greater than s->lookahead
  */
 
@@ -14,7 +14,8 @@
 
    /* Only use std3_longest_match for little_endian systems, also avoid using it with
       non-gcc compilers since the __builtin_ctzl() function might not be optimized. */
-#  if defined(__GNUC__) && defined(HAVE_BUILTIN_CTZL) && ((__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN__))
+#  if defined(__GNUC__) && defined(HAVE_BUILTIN_CTZL) && ((__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \
+        || defined(__LITTLE_ENDIAN__))
 #    define std3_longest_match
 #  else
 #    define std2_longest_match
  * Standard longest_match
  *
  */
-ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
-{
-       const unsigned wmask = s->w_mask;
-       const Pos *prev = s->prev;
-
-       unsigned chain_length;
-       IPos limit;
-       unsigned int len, best_len, nice_match;
-       unsigned char *scan, *match, *strend, scan_end, scan_end1;
-       
-       /*
-        * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple
-        * of 16. It is easy to get rid of this optimization if necessary.
-        */
-       Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
-
-       /*
-        * Do not waste too much time if we already have a good match
-        */
-       best_len = s->prev_length;
-       chain_length = s->max_chain_length;
-       if (best_len >= s->good_match)
-               chain_length >>= 2;
-
-       /*
-        * Do not looks for matches beyond the end of the input. This is
-        * necessary to make deflate deterministic
-        */
-       nice_match = (uInt)s->nice_match > s->lookahead ? s->lookahead : s->nice_match;
-
-       /*
-        * Stop when cur_match becomes <= limit. To simplify the code,
-        * we prevent matches with the string of window index 0
-        */
-       limit = s->strstart > MAX_DIST(s) ? s->strstart - MAX_DIST(s) : 0;
-
-       scan = s->window + s->strstart;
-       strend = s->window + s->strstart + MAX_MATCH;
-       scan_end1 = scan[best_len-1];
-       scan_end = scan[best_len];
-
-       Assert((size_t)s->strstart <= s->window_size - MIN_LOOKAHEAD,
-               "need lookahead");
-       do {
-               Assert(cur_match < s->strstart, "no future");
-               match = s->window + cur_match;
-
-               /*
-                * Skip to next match if the match length cannot increase
-                * or if the match length is less than 2. Note that the checks
-                * below for insufficient lookahead only occur occasionally
-                * for performance reasons. Therefore uninitialized memory
-                * will be accessed and conditional jumps will be made that
-                * depend on those values. However the length of the match
-                * is limited to the lookahead, so the output of deflate is not
-                * affected by the uninitialized values.
-                */
-               if (match[best_len] != scan_end ||
-               match[best_len-1] != scan_end1 ||
-               *match != *scan ||
-               *++match != scan[1])
-                       continue;
-
-               /*
-                * The check at best_len-1 can be removed because it will
-                * be made again later. (This heuristic is not always a win.)
-                * It is not necessary to compare scan[2] and match[2] since
-                * they are always equal when the other bytes match, given
-                * that the hash keys are equal and that HASH_BITS >= 8.
-                */
-               scan += 2;
-               match++;
-               Assert(*scan == *match, "match[2]?");
-
-               /*
-                * We check for insufficient lookahead only every 8th
-                * comparision; the 256th check will be made at strstart + 258.
-                */
-               do {
-               } while (*++scan == *++match && *++scan == *++match &&
-                        *++scan == *++match && *++scan == *++match &&
-                        *++scan == *++match && *++scan == *++match &&
-                        *++scan == *++match && *++scan == *++match &&
-                        scan < strend);
-
-               Assert(scan <= s->window+(unsigned int)(s->window_size-1),
-                       "wild scan");
-
-               len = MAX_MATCH - (int)(strend - scan);
-               scan = strend - MAX_MATCH;
-
-               if (len > best_len) {
-                       s->match_start = cur_match;
-                       best_len = len;
-                       if (len >= nice_match)
-                               break;
-                       scan_end1 = scan[best_len-1];
-                       scan_end = scan[best_len];
-               } else {
-                       /*
-                        * The probability of finding a match later if we here
-                        * is pretty low, so for performance it's best to
-                        * outright stop here for the lower compression levels
-                        */
-                       if (s->level < 6)
-                               break;
-               }
-       } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length);
-
-       if ((unsigned int)best_len <= s->lookahead)
-               return best_len;
-       return s->lookahead;
+ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
+    const unsigned wmask = s->w_mask;
+    const Pos *prev = s->prev;
+
+    unsigned chain_length;
+    IPos limit;
+    unsigned int len, best_len, nice_match;
+    unsigned char *scan, *match, *strend, scan_end, scan_end1;
+
+    /*
+     * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple
+     * of 16. It is easy to get rid of this optimization if necessary.
+     */
+    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+    /*
+     * Do not waste too much time if we already have a good match
+     */
+    best_len = s->prev_length;
+    chain_length = s->max_chain_length;
+    if (best_len >= s->good_match)
+        chain_length >>= 2;
+
+    /*
+     * Do not looks for matches beyond the end of the input. This is
+     * necessary to make deflate deterministic
+     */
+    nice_match = (uInt)s->nice_match > s->lookahead ? s->lookahead : s->nice_match;
+
+    /*
+     * Stop when cur_match becomes <= limit. To simplify the code,
+     * we prevent matches with the string of window index 0
+     */
+    limit = s->strstart > MAX_DIST(s) ? s->strstart - MAX_DIST(s) : 0;
+
+    scan = s->window + s->strstart;
+    strend = s->window + s->strstart + MAX_MATCH;
+    scan_end1 = scan[best_len-1];
+    scan_end = scan[best_len];
+
+    Assert((size_t)s->strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
+    do {
+        Assert(cur_match < s->strstart, "no future");
+        match = s->window + cur_match;
+
+        /*
+         * Skip to next match if the match length cannot increase
+         * or if the match length is less than 2. Note that the checks
+         * below for insufficient lookahead only occur occasionally
+         * for performance reasons. Therefore uninitialized memory
+         * will be accessed and conditional jumps will be made that
+         * depend on those values. However the length of the match
+         * is limited to the lookahead, so the output of deflate is not
+         * affected by the uninitialized values.
+         */
+        if (match[best_len] != scan_end ||
+            match[best_len-1] != scan_end1 ||
+            *match != *scan ||
+            *++match != scan[1])
+            continue;
+
+        /*
+         * The check at best_len-1 can be removed because it will
+         * be made again later. (This heuristic is not always a win.)
+         * It is not necessary to compare scan[2] and match[2] since
+         * they are always equal when the other bytes match, given
+         * that the hash keys are equal and that HASH_BITS >= 8.
+         */
+        scan += 2;
+        match++;
+        Assert(*scan == *match, "match[2]?");
+
+        /*
+         * We check for insufficient lookahead only every 8th
+         * comparision; the 256th check will be made at strstart + 258.
+         */
+        do {
+        } while (*++scan == *++match && *++scan == *++match &&
+             *++scan == *++match && *++scan == *++match &&
+             *++scan == *++match && *++scan == *++match &&
+             *++scan == *++match && *++scan == *++match &&
+             scan < strend);
+
+        Assert(scan <= s->window+(unsigned int)(s->window_size-1), "wild scan");
+
+        len = MAX_MATCH - (int)(strend - scan);
+        scan = strend - MAX_MATCH;
+
+        if (len > best_len) {
+            s->match_start = cur_match;
+            best_len = len;
+            if (len >= nice_match)
+                break;
+            scan_end1 = scan[best_len-1];
+            scan_end = scan[best_len];
+        } else {
+            /*
+             * The probability of finding a match later if we here
+             * is pretty low, so for performance it's best to
+             * outright stop here for the lower compression levels
+             */
+            if (s->level < 6)
+                break;
+        }
+    } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length);
+
+    if ((unsigned int)best_len <= s->lookahead)
+        return best_len;
+    return s->lookahead;
 }
 #endif
 
@@ -151,121 +149,119 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
  * UNALIGNED_OK longest_match
  *
  */
-ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
-{
-       const unsigned wmask = s->w_mask;
-       const Pos *prev = s->prev;
-
-       uint16_t scan_start, scan_end;
-       unsigned chain_length;
-       IPos limit;
-       unsigned int len, best_len, nice_match;
-       unsigned char *scan, *strend;
-       
-       /*
-        * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple
-        * of 16. It is easy to get rid of this optimization if necessary.
-        */
-       Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
-
-       /*
-        * Do not waste too much time if we already have a good match
-        */
-       best_len = s->prev_length;
-       chain_length = s->max_chain_length;
-       if (best_len >= s->good_match)
-               chain_length >>= 2;
-
-       /*
-        * Do not looks for matches beyond the end of the input. This is
-        * necessary to make deflate deterministic
-        */
-       nice_match = (uInt)s->nice_match > s->lookahead ? s->lookahead : s->nice_match;
-
-       /*
-        * Stop when cur_match becomes <= limit. To simplify the code,
-        * we prevent matches with the string of window index 0
-        */
-       limit = s->strstart > MAX_DIST(s) ? s->strstart - MAX_DIST(s) : 0;
-
-       scan = s->window + s->strstart;
-       strend = s->window + s->strstart + MAX_MATCH - 1;
-       scan_start = *(uint16_t *)scan;
-       scan_end = *(uint16_t *)(scan + best_len-1);
-
-       Assert((size_t)s->strstart <= s->window_size - MIN_LOOKAHEAD,
-               "need lookahead");
-       do {
-               unsigned char *match;
-               Assert(cur_match < s->strstart, "no future");
-               match = s->window + cur_match;
-               
-               /*
-                * Skip to next match if the match length cannot increase
-                * or if the match length is less than 2. Note that the checks
-                * below for insufficient lookahead only occur occasionally
-                * for performance reasons. Therefore uninitialized memory
-                * will be accessed and conditional jumps will be made that
-                * depend on those values. However the length of the match
-                * is limited to the lookahead, so the output of deflate is not
-                * affected by the uninitialized values.
-                */
-               if (likely((*(uint16_t *)(match + best_len - 1) != scan_end)))
-                       continue;
-               if (*(uint16_t *)match != scan_start)
-                       continue;
-
-               /* It is not necessary to compare scan[2] and match[2] since
-                * they are always equal when the other bytes match, given that
-                * the hash keys are equal and that HASH_BITS >= 8. Compare 2
-                * bytes at a time at strstart+3, +5, ... up to strstart+257.
-                * We check for insufficient lookahead only every 4th
-                * comparison; the 128th check will be made at strstart+257.
-                * If MAX_MATCH-2 is not a multiple of 8, it is necessary to
-                * put more guard bytes at the end of the window, or to check
-                * more often for insufficient lookahead.
-                */
-               Assert(scan[2] == match[2], "scan[2]?");
-               scan++;
-               match++;
-
-               do {
-               } while (*(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2)&&
-                        *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2)&&
-                        *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2)&&
-                        *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2)&&
-                        scan < strend);
-
-               /*
-                * Here, scan <= window + strstart + 257
-                */
-               Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
-               if (*scan == *match)
-                       scan++;
-
-               len = (MAX_MATCH -1) - (int)(strend-scan);
-               scan = strend - (MAX_MATCH-1);
-
-               if (len > best_len) {
-                       s->match_start = cur_match;
-                       best_len = len;
-                       if (len >= nice_match)
-                               break;
-                       scan_end = *(uint16_t *)(scan + best_len - 1);
-               } else {
-                       /*
-                        * The probability of finding a match later if we here
-                        * is pretty low, so for performance it's best to
-                        * outright stop here for the lower compression levels
-                        */
-                       if (s->level < 6)
+ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
+    const unsigned wmask = s->w_mask;
+    const Pos *prev = s->prev;
+
+    uint16_t scan_start, scan_end;
+    unsigned chain_length;
+    IPos limit;
+    unsigned int len, best_len, nice_match;
+    unsigned char *scan, *strend;
+
+    /*
+     * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple
+     * of 16. It is easy to get rid of this optimization if necessary.
+     */
+    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+    /*
+     * Do not waste too much time if we already have a good match
+     */
+    best_len = s->prev_length;
+    chain_length = s->max_chain_length;
+    if (best_len >= s->good_match)
+        chain_length >>= 2;
+
+    /*
+     * Do not looks for matches beyond the end of the input. This is
+     * necessary to make deflate deterministic
+     */
+    nice_match = (uInt)s->nice_match > s->lookahead ? s->lookahead : s->nice_match;
+
+    /*
+     * Stop when cur_match becomes <= limit. To simplify the code,
+     * we prevent matches with the string of window index 0
+     */
+    limit = s->strstart > MAX_DIST(s) ? s->strstart - MAX_DIST(s) : 0;
+
+    scan = s->window + s->strstart;
+    strend = s->window + s->strstart + MAX_MATCH - 1;
+    scan_start = *(uint16_t *)scan;
+    scan_end = *(uint16_t *)(scan + best_len-1);
+
+    Assert((size_t)s->strstart <= s->window_size - MIN_LOOKAHEAD, "need lookahead");
+    do {
+        unsigned char *match;
+        Assert(cur_match < s->strstart, "no future");
+        match = s->window + cur_match;
+
+        /*
+         * Skip to next match if the match length cannot increase
+         * or if the match length is less than 2. Note that the checks
+         * below for insufficient lookahead only occur occasionally
+         * for performance reasons. Therefore uninitialized memory
+         * will be accessed and conditional jumps will be made that
+         * depend on those values. However the length of the match
+         * is limited to the lookahead, so the output of deflate is not
+         * affected by the uninitialized values.
+         */
+        if (likely((*(uint16_t *)(match + best_len - 1) != scan_end)))
+            continue;
+        if (*(uint16_t *)match != scan_start)
+            continue;
+
+        /* It is not necessary to compare scan[2] and match[2] since
+         * they are always equal when the other bytes match, given that
+         * the hash keys are equal and that HASH_BITS >= 8. Compare 2
+         * bytes at a time at strstart+3, +5, ... up to strstart+257.
+         * We check for insufficient lookahead only every 4th
+         * comparison; the 128th check will be made at strstart+257.
+         * If MAX_MATCH-2 is not a multiple of 8, it is necessary to
+         * put more guard bytes at the end of the window, or to check
+         * more often for insufficient lookahead.
+         */
+        Assert(scan[2] == match[2], "scan[2]?");
+        scan++;
+        match++;
+
+        do {
+        } while (*(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2) &&
+                 *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2) &&
+                 *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2) &&
+                 *(uint16_t *)(scan += 2)== *(uint16_t *)(match += 2) &&
+                 scan < strend);
+
+        /*
+         * Here, scan <= window + strstart + 257
+         */
+        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+        if (*scan == *match)
+            scan++;
+
+        len = (MAX_MATCH -1) - (int)(strend-scan);
+        scan = strend - (MAX_MATCH-1);
+
+        if (len > best_len) {
+            s->match_start = cur_match;
+            best_len = len;
+            if (len >= nice_match)
                 break;
-               }
-       } while (--chain_length && (cur_match = prev[cur_match & wmask]) > limit);
+            scan_end = *(uint16_t *)(scan + best_len - 1);
+        } else {
+            /*
+             * The probability of finding a match later if we here
+             * is pretty low, so for performance it's best to
+             * outright stop here for the lower compression levels
+             */
+            if (s->level < 6)
+                break;
+        }
+    } while (--chain_length && (cur_match = prev[cur_match & wmask]) > limit);
 
-       if ((unsigned)best_len <= s->lookahead)
-               return best_len;
-       return s->lookahead;
+    if ((unsigned)best_len <= s->lookahead)
+        return best_len;
+    return s->lookahead;
 }
 #endif
 
@@ -334,8 +330,7 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
  *       -------------------------------------------------
  */
 
-ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
-{
+ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) {
     unsigned chain_length = s->max_chain_length;/* max hash chain length */
     register unsigned char *scan = s->window + s->strstart; /* current string */
     register unsigned char *match;                       /* matched string */
@@ -389,8 +384,9 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
                 if ((cur_match = prev[cur_match & wmask]) > limit
                     && --chain_length != 0) {
                     continue;
-                } else
+                } else {
                     cont = 0;
+                }
             }
             break;
         } while (1);
@@ -438,7 +434,8 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
         if (len > best_len) {
             s->match_start = cur_match;
             best_len = len;
-            if (len >= nice_match) break;
+            if (len >= nice_match)
+                break;
             scan_end = *(uint16_t*)(scan+best_len-1);
         } else {
             /*
@@ -449,10 +446,10 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match)
             if (s->level < 6)
                 break;
         }
-    } while ((cur_match = prev[cur_match & wmask]) > limit
-             && --chain_length != 0);
+    } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length != 0);
 
-    if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
+    if ((uInt)best_len <= s->lookahead)
+        return (uInt)best_len;
     return s->lookahead;
 }
 #endif
diff --git a/match.h b/match.h
index d0f38504735941e8e5166123c7d7cdad06884d6f..69b34c1f132cb77b7a9061923c6831a14537147b 100644 (file)
--- a/match.h
+++ b/match.h
@@ -1,2 +1,6 @@
+#ifndef MATCH_H_
+#define MATCH_H_
 
 uInt longest_match  (deflate_state *s, IPos cur_match);
+
+#endif /* MATCH_H_ */
index 2a3eee48deaafd7088549149c7260b115d0b1af5..d53e120bf9928fa6f763dfde52aef27f0f326390 100644 (file)
--- a/uncompr.c
+++ b/uncompr.c
@@ -21,8 +21,7 @@
    enough memory, Z_BUF_ERROR if there was not enough room in the output
    buffer, or Z_DATA_ERROR if the input data was corrupted.
 */
-int ZEXPORT uncompress (unsigned char *dest, uLong *destLen, const unsigned char *source, uLong sourceLen)
-{
+int ZEXPORT uncompress(unsigned char *dest, uLong *destLen, const unsigned char *source, uLong sourceLen) {
     z_stream stream;
     int err;