]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Inline read_buf
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Mon, 18 Aug 2025 13:19:50 +0000 (15:19 +0200)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Sat, 23 Aug 2025 17:17:16 +0000 (19:17 +0200)
deflate.c
deflate_p.h
deflate_stored.c

index 59ebbf6f91d3dfd832d76cf39d5398f6cb5ddf82..8b198ec1592c12c2d7f37c080ebc254f434d3554 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -48,9 +48,9 @@
  */
 
 #include "zbuild.h"
+#include "functable.h"
 #include "deflate.h"
 #include "deflate_p.h"
-#include "functable.h"
 
 /* Avoid conflicts with zlib.h macros */
 #ifdef ZLIB_COMPAT
@@ -81,7 +81,6 @@ Z_INTERNAL block_state deflate_rle   (deflate_state *s, int flush);
 Z_INTERNAL block_state deflate_huff  (deflate_state *s, int flush);
 static void lm_set_level         (deflate_state *s, int level);
 static void lm_init              (deflate_state *s);
-Z_INTERNAL unsigned read_buf  (PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
 
 /* ===========================================================================
  * Local data
@@ -1141,37 +1140,6 @@ int32_t Z_EXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *sou
     return Z_OK;
 }
 
-/* ===========================================================================
- * Read a new buffer from the current input stream, update the adler32
- * and total number of bytes read.  All deflate() input goes through
- * this function so some applications may wish to modify it to avoid
- * allocating a large strm->next_in buffer and copying from it.
- * (See also flush_pending()).
- */
-Z_INTERNAL unsigned PREFIX(read_buf)(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
-    uint32_t len = MIN(strm->avail_in, size);
-    if (len == 0)
-        return 0;
-
-    strm->avail_in  -= len;
-
-    if (!DEFLATE_NEED_CHECKSUM(strm)) {
-        memcpy(buf, strm->next_in, len);
-#ifdef GZIP
-    } else if (strm->state->wrap == 2) {
-        FUNCTABLE_CALL(crc32_fold_copy)(&strm->state->crc_fold, buf, strm->next_in, len);
-#endif
-    } else if (strm->state->wrap == 1) {
-        strm->adler = FUNCTABLE_CALL(adler32_fold_copy)(strm->adler, buf, strm->next_in, len);
-    } else {
-        memcpy(buf, strm->next_in, len);
-    }
-    strm->next_in  += len;
-    strm->total_in += len;
-
-    return len;
-}
-
 /* ===========================================================================
  * Set longest match variables based on level configuration
  */
@@ -1274,7 +1242,7 @@ void Z_INTERNAL PREFIX(fill_window)(deflate_state *s) {
          */
         Assert(more >= 2, "more < 2");
 
-        n = PREFIX(read_buf)(s->strm, s->window + s->strstart + s->lookahead, more);
+        n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
         s->lookahead += n;
 
         /* Initialize the hash value now that we have some input: */
index 24d1c35b1ddcaaef541dc8cca4ff1f19cd265e73..6f690702b2d658b5820228b898e28f982df3ebfa 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef DEFLATE_P_H
 #define DEFLATE_P_H
 
+#include "functable.h"
+
 /* Forward declare common non-inlined functions declared in deflate.c */
 
 #ifdef ZLIB_DEBUG
@@ -48,7 +50,6 @@ static inline void check_match(deflate_state *s, Pos start, Pos match, int lengt
 #endif
 
 Z_INTERNAL void PREFIX(flush_pending)(PREFIX3(stream) *strm);
-Z_INTERNAL unsigned PREFIX(read_buf)(PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
 
 /* ===========================================================================
  * Save the match info and tally the frequency counts. Return true if
@@ -109,6 +110,36 @@ static inline uint16_t bi_reverse(unsigned code, int len) {
     return (bitrev8(code >> 8) | (uint16_t)bitrev8(code) << 8) >> (16 - len);
 }
 
+/* ===========================================================================
+ * Read a new buffer from the current input stream, update the adler32 and total number of
+ * bytes read.  All deflate() input goes through this function so some applications may wish
+ * to modify it to avoid allocating a large strm->next_in buffer and copying from it.
+ * See also flush_pending_inline().
+ */
+Z_FORCEINLINE static unsigned read_buf(PREFIX3(stream) *strm, unsigned char *buf, unsigned size) {
+    uint32_t len = MIN(strm->avail_in, size);
+
+    if (len == 0)
+        return 0;
+
+    if (!DEFLATE_NEED_CHECKSUM(strm)) {
+        memcpy(buf, strm->next_in, len);
+#ifdef GZIP
+    } else if (strm->state->wrap == 2) {
+        FUNCTABLE_CALL(crc32_fold_copy)(&strm->state->crc_fold, buf, strm->next_in, len);
+#endif
+    } else if (strm->state->wrap == 1) {
+        strm->adler = FUNCTABLE_CALL(adler32_fold_copy)(strm->adler, buf, strm->next_in, len);
+    } else {
+        memcpy(buf, strm->next_in, len);
+    }
+
+    strm->avail_in -= len;
+    strm->next_in  += len;
+    strm->total_in += len;
+    return len;
+}
+
 /* ===========================================================================
  * Flush the current block, with given end-of-file flag.
  * IN assertion: strstart is set to the end of the current match.
index 9e5acfbf9661097d697b6c857e19e7b96525d0b3..eaa16f1687346b09ca049abe09bddfde6631a7ec 100644 (file)
@@ -94,7 +94,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
          * the check value.
          */
         if (len) {
-            PREFIX(read_buf)(s->strm, s->strm->next_out, len);
+            read_buf(s->strm, s->strm->next_out, len);
             s->strm->next_out += len;
             s->strm->avail_out -= len;
             s->strm->total_out += len;
@@ -157,7 +157,7 @@ Z_INTERNAL block_state deflate_stored(deflate_state *s, int flush) {
 
     have = MIN(have, s->strm->avail_in);
     if (have) {
-        PREFIX(read_buf)(s->strm, s->window + s->strstart, have);
+        read_buf(s->strm, s->window + s->strstart, have);
         s->strstart += have;
         s->insert += MIN(have, s->w_size - s->insert);
     }