diff --git a/igzip/bitbuf2.h b/igzip/bitbuf2.h index a0a0aeb..51bd752 100644 --- a/igzip/bitbuf2.h +++ b/igzip/bitbuf2.h @@ -30,16 +30,7 @@ #define BITBUF2_H #include "igzip_lib.h" - -#if defined (__unix__) || (__APPLE__) || (__MINGW32__) -#define _mm_stream_si64x(dst, src) *((uint64_t*)dst) = src -#else -#include -#endif - -#ifdef _WIN64 -#pragma warning(disable: 4996) -#endif +#include "unaligned.h" #ifdef _MSC_VER #define inline __inline @@ -87,7 +78,7 @@ static inline uint32_t buffer_bits_used(struct BitBuf2 *me) static inline void flush_bits(struct BitBuf2 *me) { uint32_t bits; - _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits); + store_u64(me->m_out_buf, me->m_bits); bits = me->m_bit_count & ~7; me->m_bit_count -= bits; me->m_out_buf += bits/8; @@ -100,7 +91,7 @@ static inline void flush(struct BitBuf2 *me) { uint32_t bytes; if (me->m_bit_count) { - _mm_stream_si64x((int64_t *) me->m_out_buf, me->m_bits); + store_u64(me->m_out_buf, me->m_bits); bytes = (me->m_bit_count + 7) / 8; me->m_out_buf += bytes; } diff --git a/igzip/igzip.c b/igzip/igzip.c index 5776f13..9688418 100644 --- a/igzip/igzip.c +++ b/igzip/igzip.c @@ -1091,13 +1091,13 @@ uint32_t isal_write_gzip_header(struct isal_zstream *stream, struct isal_gzip_he out_buf[1] = 0x8b; out_buf[2] = DEFLATE_METHOD; out_buf[3] = flags; - *(uint32_t *) (out_buf + 4) = gz_hdr->time; + store_u32(out_buf + 4, gz_hdr->time); out_buf[8] = gz_hdr->xflags; out_buf[9] = gz_hdr->os; out_buf += GZIP_HDR_BASE; if (flags & EXTRA_FLAG) { - *(uint16_t *) out_buf = gz_hdr->extra_len; + store_u16(out_buf, gz_hdr->extra_len); out_buf += GZIP_EXTRA_LEN; memcpy(out_buf, gz_hdr->extra, gz_hdr->extra_len); @@ -1116,7 +1116,7 @@ uint32_t isal_write_gzip_header(struct isal_zstream *stream, struct isal_gzip_he if (flags & HCRC_FLAG) { hcrc = crc32_gzip_refl(0, out_buf_start, out_buf - out_buf_start); - *(uint16_t *) out_buf = hcrc; + store_u16(out_buf, hcrc); out_buf += GZIP_HCRC_LEN; } @@ -1149,7 +1149,7 @@ uint32_t isal_write_zlib_header(struct isal_zstream * stream, struct isal_zlib_h out_buf[1] = flg; if (dict_flag) - *(uint32_t *) (out_buf + 2) = z_hdr->dict_id; + store_u32(out_buf + 2, z_hdr->dict_id); stream->next_out += hdr_size; stream->total_out += hdr_size; @@ -1893,8 +1893,7 @@ static void write_trailer(struct isal_zstream *stream) case IGZIP_GZIP: case IGZIP_GZIP_NO_HDR: if (stream->avail_out - bytes >= gzip_trl_bytes) { - *(uint64_t *) stream->next_out = - ((uint64_t) stream->total_in << 32) | crc; + store_u64(stream->next_out, ((uint64_t) stream->total_in << 32) | crc); stream->next_out += gzip_trl_bytes; bytes += gzip_trl_bytes; state->state = ZSTATE_END; @@ -1904,8 +1903,9 @@ static void write_trailer(struct isal_zstream *stream) case IGZIP_ZLIB: case IGZIP_ZLIB_NO_HDR: if (stream->avail_out - bytes >= zlib_trl_bytes) { - *(uint32_t *) stream->next_out = - to_be32((crc & 0xFFFF0000) | ((crc & 0xFFFF) + 1) % ADLER_MOD); + store_u32(stream->next_out, + to_be32((crc & 0xFFFF0000) | ((crc & 0xFFFF) + 1) % + ADLER_MOD)); stream->next_out += zlib_trl_bytes; bytes += zlib_trl_bytes; state->state = ZSTATE_END; diff --git a/igzip/igzip_icf_body.c b/igzip/igzip_icf_body.c index d7f9896..375d4d4 100644 --- a/igzip/igzip_icf_body.c +++ b/igzip/igzip_icf_body.c @@ -20,8 +20,8 @@ static inline void write_deflate_icf(struct deflate_icf *icf, uint32_t lit_len, /* icf->lit_dist = lit_dist; */ /* icf->dist_extra = extra_bits; */ - *(uint32_t *) icf = lit_len | (lit_dist << LIT_LEN_BIT_COUNT) - | (extra_bits << (LIT_LEN_BIT_COUNT + DIST_LIT_BIT_COUNT)); + store_u32((uint8_t *) icf, lit_len | (lit_dist << LIT_LEN_BIT_COUNT) + | (extra_bits << (LIT_LEN_BIT_COUNT + DIST_LIT_BIT_COUNT))); } void set_long_icf_fg_base(uint8_t * next_in, uint64_t processed, uint64_t input_size, @@ -153,7 +153,7 @@ static struct deflate_icf *compress_icf_map_g(struct isal_zstream *stream, level_buf->hist.ll_hist[lit_len]++; if (lit_len >= LEN_START) { - *(uint32_t *) level_buf->icf_buf_next = code; + store_u32((uint8_t *) level_buf->icf_buf_next, code); level_buf->icf_buf_next++; dist = (code >> ICF_DIST_OFFSET) & DIST_LIT_MASK; @@ -162,7 +162,7 @@ static struct deflate_icf *compress_icf_map_g(struct isal_zstream *stream, matches_next += lit_len; } else if (lit_len2 >= LEN_START) { - *(uint64_t *) level_buf->icf_buf_next = code; + store_u64((uint8_t *) level_buf->icf_buf_next, code); level_buf->icf_buf_next += 2; level_buf->hist.ll_hist[lit_len2]++; @@ -174,7 +174,7 @@ static struct deflate_icf *compress_icf_map_g(struct isal_zstream *stream, } else { code = ((lit_len2 + LIT_START) << ICF_DIST_OFFSET) | lit_len; - *(uint32_t *) level_buf->icf_buf_next = code; + store_u32((uint8_t *) level_buf->icf_buf_next, code); level_buf->icf_buf_next++; level_buf->hist.ll_hist[lit_len2]++; @@ -186,7 +186,7 @@ static struct deflate_icf *compress_icf_map_g(struct isal_zstream *stream, while (matches_next < matches_end && level_buf->icf_buf_next < icf_buf_end) { code = load_u32((uint8_t *) matches_next); lit_len = code & LIT_LEN_MASK; - *(uint32_t *) level_buf->icf_buf_next = code; + store_u32((uint8_t *) level_buf->icf_buf_next, code); level_buf->icf_buf_next++; level_buf->hist.ll_hist[lit_len]++; diff --git a/igzip/igzip_inflate.c b/igzip/igzip_inflate.c index 617d5bc..775e8f3 100644 --- a/igzip/igzip_inflate.c +++ b/igzip/igzip_inflate.c @@ -1865,8 +1865,8 @@ static int check_gzip_checksum(struct inflate_state *state) byte_count = state->read_in_length / 8; offset = state->read_in_length % 8; - *(uint64_t *) (state->tmp_in_buffer + tmp_in_size) = - state->read_in >> offset; + store_u64(state->tmp_in_buffer + tmp_in_size, + state->read_in >> offset); state->read_in = 0; state->read_in_length = 0; @@ -1916,8 +1916,8 @@ static int check_zlib_checksum(struct inflate_state *state) byte_count = state->read_in_length / 8; offset = state->read_in_length % 8; - *(uint64_t *) (state->tmp_in_buffer + tmp_in_size) = - state->read_in >> offset; + store_u64(state->tmp_in_buffer + tmp_in_size, + state->read_in >> offset); state->read_in = 0; state->read_in_length = 0; @@ -2287,7 +2287,7 @@ int isal_inflate(struct inflate_state *state) /* Copy valid data from internal buffer into out_buffer */ if (state->write_overflow_len != 0) { - *(uint32_t *) state->next_out = state->write_overflow_lits; + store_u32(state->next_out, state->write_overflow_lits); state->next_out += state->write_overflow_len; state->total_out += state->write_overflow_len; state->write_overflow_lits = 0; @@ -2385,8 +2385,8 @@ int isal_inflate(struct inflate_state *state) /* Write overflow data into tmp buffer */ if (state->write_overflow_len != 0) { - *(uint32_t *) & state->tmp_out_buffer[state->tmp_out_valid] = - state->write_overflow_lits; + store_u32(&state->tmp_out_buffer[state->tmp_out_valid], + state->write_overflow_lits); state->tmp_out_valid += state->write_overflow_len; state->total_out += state->write_overflow_len; state->write_overflow_lits = 0; diff --git a/igzip/proc_heap_base.c b/igzip/proc_heap_base.c index 1c83788..777bd0f 100644 --- a/igzip/proc_heap_base.c +++ b/igzip/proc_heap_base.c @@ -29,6 +29,7 @@ #include "igzip_lib.h" #include "huff_codes.h" +#include "unaligned.h" static inline void heapify(uint64_t * heap, uint64_t heap_size, uint64_t index) { @@ -73,12 +74,12 @@ uint32_t build_huff_tree(struct heap_tree *heap_space, uint64_t heap_size, uint6 heapify(heap, heap_size, 1); - *(uint16_t *) (&heap[node_ptr]) = h1; - *(uint16_t *) (&heap[node_ptr - 1]) = h2; + store_u16((uint8_t *) & heap[node_ptr], h1); + store_u16((uint8_t *) & heap[node_ptr - 1], h2); node_ptr -= 2; } h1 = heap[1]; - *(uint16_t *) (&heap[node_ptr]) = h1; + store_u16((uint8_t *) & heap[node_ptr], h1); return node_ptr; } diff --git a/include/unaligned.h b/include/unaligned.h index f9437df..f7b1ed8 100644 --- a/include/unaligned.h +++ b/include/unaligned.h @@ -57,4 +57,20 @@ static inline uintmax_t load_umax(uint8_t * buf) { return ret; } +static inline void store_u16(uint8_t * buf, uint16_t val) { + memcpy(buf, &val, sizeof(val)); +} + +static inline void store_u32(uint8_t * buf, uint32_t val) { + memcpy(buf, &val, sizeof(val)); +} + +static inline void store_u64(uint8_t * buf, uint64_t val) { + memcpy(buf, &val, sizeof(val)); +} + +static inline void store_umax(uint8_t * buf, uintmax_t val) { + memcpy(buf, &val, sizeof(val)); +} + #endif