summaryrefslogtreecommitdiff
path: root/deflate.h
diff options
context:
space:
mode:
authorNathan Moinvaziri <nathan@nathanm.com>2020-01-26 12:34:54 -0800
committerHans Kristian Rosbach <hk-github@circlestorm.org>2020-02-07 18:36:08 +0100
commitd06d965ff0fe143b54f162a7970285c88460f34f (patch)
tree3ca34efda351322d363b1864d1017e25afbe8c00 /deflate.h
parent840e931ad341169830733a35387c14c6559f2829 (diff)
Added better aligned access support for put_short.
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.
Diffstat (limited to 'deflate.h')
-rw-r--r--deflate.h53
1 files changed, 48 insertions, 5 deletions
diff --git a/deflate.h b/deflate.h
index 2806d92..f7503f1 100644
--- 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)