From: Hans Kristian Rosbach Date: Mon, 18 Aug 2025 13:19:50 +0000 (+0200) Subject: Inline read_buf X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=416314d8666eeae2a1e4ce9a929c5c4cd2504d0e;p=thirdparty%2Fzlib-ng.git Inline read_buf --- diff --git a/deflate.c b/deflate.c index 59ebbf6f..8b198ec1 100644 --- 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: */ diff --git a/deflate_p.h b/deflate_p.h index 24d1c35b..6f690702 100644 --- a/deflate_p.h +++ b/deflate_p.h @@ -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. diff --git a/deflate_stored.c b/deflate_stored.c index 9e5acfbf..eaa16f16 100644 --- a/deflate_stored.c +++ b/deflate_stored.c @@ -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); }