]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Added better aligned access support for put_short.
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 26 Jan 2020 20:34:54 +0000 (12:34 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Fri, 7 Feb 2020 17:36:08 +0000 (18:36 +0100)
Renamed putShortMSB to put_short_msb and moved to deflate.h with the rest of the put functions.
Added put_uint32 and put_uint32_msb and replaced instances of put_byte with put_short or put_uint32.

deflate.c
deflate.h
trees.c

index 40f037fd73deeb4e2fb83e77303078a4b6ee1fab..91f3bb8acd712e38755915adb34a9ab90a5fb586 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -115,7 +115,6 @@ ZLIB_INTERNAL block_state deflate_slow         (deflate_state *s, int flush);
 static block_state deflate_rle   (deflate_state *s, int flush);
 static block_state deflate_huff  (deflate_state *s, int flush);
 static void lm_init              (deflate_state *s);
-static void putShortMSB          (deflate_state *s, uint16_t b);
 ZLIB_INTERNAL unsigned read_buf  (PREFIX3(stream) *strm, unsigned char *buf, unsigned size);
 
 extern void crc_reset(deflate_state *const s);
@@ -744,16 +743,6 @@ unsigned long ZEXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long
     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + (sourceLen >> 25) + 13 - 6 + wraplen;
 }
 
-/* =========================================================================
- * Put a short in the pending buffer. The 16-bit value is put in MSB order.
- * IN assertion: the stream state is correct and there is enough room in
- * pending_buf.
- */
-static void putShortMSB(deflate_state *s, uint16_t b) {
-    put_byte(s, (unsigned char)(b >> 8));
-    put_byte(s, (unsigned char)(b & 0xff));
-}
-
 /* =========================================================================
  * Flush as much pending output as possible. All deflate() output, except for
  * some deflate_stored() output, goes through this function so some
@@ -859,12 +848,11 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
         if (s->strstart != 0) header |= PRESET_DICT;
         header += 31 - (header % 31);
 
-        putShortMSB(s, header);
+        put_short_msb(s, header);
 
         /* Save the adler32 of the preset dictionary: */
         if (s->strstart != 0) {
-            putShortMSB(s, (uint16_t)(strm->adler >> 16));
-            putShortMSB(s, (uint16_t)(strm->adler));
+            put_uint32_msb(s, strm->adler);
         }
         strm->adler = functable.adler32(0L, NULL, 0);
         s->status = BUSY_STATE;
@@ -884,10 +872,7 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
         put_byte(s, 139);
         put_byte(s, 8);
         if (s->gzhead == NULL) {
-            put_byte(s, 0);
-            put_byte(s, 0);
-            put_byte(s, 0);
-            put_byte(s, 0);
+            put_uint32(s, 0);
             put_byte(s, 0);
             put_byte(s, s->level == 9 ? 2 :
                      (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
@@ -907,16 +892,12 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
                      (s->gzhead->name == NULL ? 0 : 8) +
                      (s->gzhead->comment == NULL ? 0 : 16)
                      );
-            put_byte(s, (unsigned char)(s->gzhead->time & 0xff));
-            put_byte(s, (unsigned char)((s->gzhead->time >> 8) & 0xff));
-            put_byte(s, (unsigned char)((s->gzhead->time >> 16) & 0xff));
-            put_byte(s, (unsigned char)((s->gzhead->time >> 24) & 0xff));
+            put_uint32(s, s->gzhead->time);
             put_byte(s, s->level == 9 ? 2 :
                      (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? 4 : 0));
             put_byte(s, s->gzhead->os & 0xff);
             if (s->gzhead->extra != NULL) {
-                put_byte(s, s->gzhead->extra_len & 0xff);
-                put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
+                put_short(s, s->gzhead->extra_len);
             }
             if (s->gzhead->hcrc)
                 strm->adler = PREFIX(crc32)(strm->adler, s->pending_buf, s->pending);
@@ -953,7 +934,7 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
     if (s->status == NAME_STATE) {
         if (s->gzhead->name != NULL) {
             uint32_t beg = s->pending;   /* start of bytes to update crc */
-            int val;
+            unsigned char val;
 
             do {
                 if (s->pending == s->pending_buf_size) {
@@ -976,7 +957,7 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
     if (s->status == COMMENT_STATE) {
         if (s->gzhead->comment != NULL) {
             uint32_t beg = s->pending;  /* start of bytes to update crc */
-            int val;
+            unsigned char val;
 
             do {
                 if (s->pending == s->pending_buf_size) {
@@ -1004,8 +985,7 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
                     return Z_OK;
                 }
             }
-            put_byte(s, (unsigned char)(strm->adler & 0xff));
-            put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
+            put_short(s, (uint16_t)strm->adler);
             crc_reset(s);
         }
         s->status = BUSY_STATE;
@@ -1085,19 +1065,12 @@ int ZEXPORT PREFIX(deflate)(PREFIX3(stream) *strm, int flush) {
 #  ifdef X86_PCLMULQDQ_CRC
         crc_finalize(s);
 #  endif
-        put_byte(s, (unsigned char)(strm->adler & 0xff));
-        put_byte(s, (unsigned char)((strm->adler >> 8) & 0xff));
-        put_byte(s, (unsigned char)((strm->adler >> 16) & 0xff));
-        put_byte(s, (unsigned char)((strm->adler >> 24) & 0xff));
-        put_byte(s, (unsigned char)(strm->total_in & 0xff));
-        put_byte(s, (unsigned char)((strm->total_in >> 8) & 0xff));
-        put_byte(s, (unsigned char)((strm->total_in >> 16) & 0xff));
-        put_byte(s, (unsigned char)((strm->total_in >> 24) & 0xff));
+        put_uint32(s, strm->adler);
+        put_uint32(s, strm->total_in);
     } else
 #endif
     {
-        putShortMSB(s, (uint16_t)(strm->adler >> 16));
-        putShortMSB(s, (uint16_t)strm->adler);
+        put_uint32_msb(s, strm->adler);
     }
     flush_pending(strm);
     /* If avail_out is zero, the application will call deflate again
index 2806d92ad3e271e5c8b125a52ac80b88aa1e705b..f7503f10ff48a5d9f1c1ec3dc50972e1a5841d2c 100644 (file)
--- a/deflate.h
+++ b/deflate.h
@@ -306,14 +306,57 @@ typedef enum {
 
 /* ===========================================================================
  * Output a short LSB first on the stream.
- * IN assertion: there is enough room in pendingBuf.
+ * IN assertion: there is enough room in pending_buf.
  */
 static inline void put_short(deflate_state *s, uint16_t w) {
-#if BYTE_ORDER == BIG_ENDIAN
-    w = ZSWAP16(w);
-#endif
-    memcpy(&(s->pending_buf[s->pending]), &w, sizeof(uint16_t));
+#if defined(UNALIGNED_OK)
+    *(uint16_t *)(&s->pending_buf[s->pending]) = w;
     s->pending += 2;
+#else
+    put_byte(s, (w & 0xff));
+    put_byte(s, ((w >> 8) & 0xff));
+#endif
+}
+
+/* ===========================================================================
+ * Output a short MSB first on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+static inline void put_short_msb(deflate_state *s, uint16_t w) {
+    put_byte(s, ((w >> 8) & 0xff));
+    put_byte(s, (w & 0xff));
+}
+
+/* ===========================================================================
+ * Output a 32-bit unsigned int LSB first on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+static inline void put_uint32(deflate_state *s, uint32_t dw) {
+#if defined(UNALIGNED_OK)
+    *(uint32_t *)(&s->pending_buf[s->pending]) = dw;
+    s->pending += 4;
+#else
+    put_byte(s, (dw & 0xff));
+    put_byte(s, ((dw >> 8) & 0xff));
+    put_byte(s, ((dw >> 16) & 0xff));
+    put_byte(s, ((dw >> 24) & 0xff));
+#endif
+}
+
+/* ===========================================================================
+ * Output a 32-bit unsigned int MSB first on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+static inline void put_uint32_msb(deflate_state *s, uint32_t dw) {
+#if defined(UNALIGNED_OK)
+    *(uint32_t *)(&s->pending_buf[s->pending]) = ZSWAP32(dw);
+    s->pending += 4;
+#else
+    put_byte(s, ((dw >> 24) & 0xff));
+    put_byte(s, ((dw >> 16) & 0xff));
+    put_byte(s, ((dw >> 8) & 0xff));
+    put_byte(s, (dw & 0xff));
+#endif
 }
 
 #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
diff --git a/trees.c b/trees.c
index a948b96c0f74abb5ef6742c3dc9df7bc8fca99cd..c4d68824391cce0f4b291c100502c2c97a5fe77c 100644 (file)
--- a/trees.c
+++ b/trees.c
@@ -799,7 +799,7 @@ static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data
             } /* literal or match pair ? */
 
             /* Check that the overlay between pending_buf and sym_buf is ok: */
-            Assert(s->pending < s->lit_bufsize + sx, "pendingBuf overflow");
+            Assert(s->pending < s->lit_bufsize + sx, "pending_buf overflow");
         } while (sx < s->sym_next);
     }