From 4add047d597555f481784ed159b63c62bd750fbd Mon Sep 17 00:00:00 2001 From: Mathis Rosenhauer Date: Wed, 12 Jul 2017 13:48:53 +0200 Subject: [PATCH] Limit scope --- Makefile.am | 2 +- src/decode.c | 122 +++++++++++++++++++-------------------------- src/decode.h | 3 +- src/encode.c | 94 +++++++++++++++------------------- src/encode_accessors.c | 45 +++++------------ src/sz_compat.c | 94 +++++++++++++--------------------- tests/check_aec.c | 33 ++++++------ tests/check_buffer_sizes.c | 21 +++----- tests/check_code_options.c | 32 ++++++------ tests/check_long_fs.c | 9 ++-- 10 files changed, 184 insertions(+), 271 deletions(-) diff --git a/Makefile.am b/Makefile.am index 975e308..2676248 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ ACLOCAL_AMFLAGS = -I m4 SUBDIRS = src tests EXTRA_DIST = doc/patent.txt CMakeLists.txt cmake/config.h.in \ -cmake/macros.cmake README.md README.SZIP CHANGELOG.am Copyright.txt data +cmake/macros.cmake README.md README.SZIP CHANGELOG.md Copyright.txt data sampledata = 121B2TestData sampledata_url = http://cwe.ccsds.org/sls/docs/SLS-DC/BB121B2TestData/$(sampledata).zip diff --git a/src/decode.c b/src/decode.c index 551ebe6..92b0657 100644 --- a/src/decode.c +++ b/src/decode.c @@ -213,10 +213,9 @@ static inline uint32_t direct_get(struct aec_stream *strm, int n) */ struct internal_state *state = strm->state; - int b; if (state->bitp < n) { - b = (63 - state->bitp) >> 3; + int b = (63 - state->bitp) >> 3; if (b == 6) { state->acc = (state->acc << 48) | ((uint64_t)strm->next_in[0] << 40) @@ -279,11 +278,6 @@ static inline uint32_t direct_get_fs(struct aec_stream *strm) */ uint32_t fs = 0; -#if HAVE_BSR64 - unsigned long i; -#else - int i; -#endif struct internal_state *state = strm->state; if (state->bitp) @@ -313,11 +307,12 @@ static inline uint32_t direct_get_fs(struct aec_stream *strm) #define __has_builtin(x) 0 /* Compatibility with non-clang compilers. */ #endif #if HAVE_DECL___BUILTIN_CLZLL || __has_builtin(__builtin_clzll) - i = 63 - __builtin_clzll(state->acc); + int i = 63 - __builtin_clzll(state->acc); #elif HAVE_BSR64 + unsigned long i; _BitScanReverse64(&i, state->acc); #else - i = state->bitp - 1; + int i = state->bitp - 1; while ((state->acc & (UINT64_C(1) << i)) == 0) i--; #endif @@ -437,7 +432,7 @@ static int m_split_output(struct aec_stream *strm) state->rsip++; strm->avail_out -= state->bytes_per_sample; bits_drop(strm, k); - } while(++state->i < state->n); + } while(++state->sample_counter < state->encoded_block_size); state->mode = m_next_cds; return M_CONTINUE; @@ -451,11 +446,11 @@ static int m_split_fs(struct aec_stream *strm) do { if (fs_ask(strm) == 0) return M_EXIT; - state->rsip[state->i] = state->fs << k; + state->rsip[state->sample_counter] = state->fs << k; fs_drop(strm); - } while(++state->i < state->n); + } while(++state->sample_counter < state->encoded_block_size); - state->i = 0; + state->sample_counter = 0; state->mode = m_split_output; return M_CONTINUE; @@ -463,26 +458,23 @@ static int m_split_fs(struct aec_stream *strm) static int m_split(struct aec_stream *strm) { - size_t i; - size_t binary_part; - int k; struct internal_state *state = strm->state; if (BUFFERSPACE(strm)) { - k = state->id - 1; - binary_part = (k * state->encoded_block_size) / 8 + 9; + int k = state->id - 1; + size_t binary_part = (k * state->encoded_block_size) / 8 + 9; if (state->ref) *state->rsip++ = direct_get(strm, strm->bits_per_sample); - for (i = 0; i < state->encoded_block_size; i++) + for (size_t i = 0; i < state->encoded_block_size; i++) state->rsip[i] = direct_get_fs(strm) << k; if (k) { if (strm->avail_in < binary_part) return M_ERROR; - for (i = 0; i < state->encoded_block_size; i++) + for (size_t i = 0; i < state->encoded_block_size; i++) *state->rsip++ += direct_get(strm, k); } else { state->rsip += state->encoded_block_size; @@ -493,8 +485,7 @@ static int m_split(struct aec_stream *strm) } else { if (state->ref && (copysample(strm) == 0)) return M_EXIT; - state->n = state->encoded_block_size; - state->i = 0; + state->sample_counter = 0; state->mode = m_split_fs; } return M_CONTINUE; @@ -508,7 +499,7 @@ static int m_zero_output(struct aec_stream *strm) if (strm->avail_out < state->bytes_per_sample) return M_EXIT; put_sample(strm, 0); - } while(--state->i); + } while(--state->sample_counter); state->mode = m_next_cds; return M_CONTINUE; @@ -516,46 +507,48 @@ static int m_zero_output(struct aec_stream *strm) static int m_zero_block(struct aec_stream *strm) { - uint32_t i, zero_blocks, b, zero_bytes; struct internal_state *state = strm->state; + uint32_t zero_blocks; + uint32_t zero_samples; + uint32_t zero_bytes; if (fs_ask(strm) == 0) return M_EXIT; + zero_blocks = state->fs + 1; fs_drop(strm); if (zero_blocks == ROS) { - b = (int)RSI_USED_SIZE(state) / strm->block_size; + int b = (int)RSI_USED_SIZE(state) / strm->block_size; zero_blocks = MIN(strm->rsi - b, 64 - (b % 64)); } else if (zero_blocks > ROS) { zero_blocks--; } - i = zero_blocks * strm->block_size - state->ref; - zero_bytes = i * state->bytes_per_sample; - - if (state->rsi_size - RSI_USED_SIZE(state) < i) + zero_samples = zero_blocks * strm->block_size - state->ref; + if (state->rsi_size - RSI_USED_SIZE(state) < zero_samples) return M_ERROR; + zero_bytes = zero_samples * state->bytes_per_sample; if (strm->avail_out >= zero_bytes) { - memset(state->rsip, 0, i * sizeof(uint32_t)); - state->rsip += i; + memset(state->rsip, 0, zero_samples * sizeof(uint32_t)); + state->rsip += zero_samples; strm->avail_out -= zero_bytes; state->mode = m_next_cds; - return M_CONTINUE; + } else { + state->sample_counter = zero_samples; + state->mode = m_zero_output; } - - state->i = i; - state->mode = m_zero_output; return M_CONTINUE; } static int m_se_decode(struct aec_stream *strm) { - int32_t m, d1; struct internal_state *state = strm->state; - while(state->i < strm->block_size) { + while(state->sample_counter < strm->block_size) { + int32_t m; + int32_t d1; if (fs_ask(strm) == 0) return M_EXIT; m = state->fs; @@ -563,17 +556,17 @@ static int m_se_decode(struct aec_stream *strm) return M_ERROR; d1 = m - state->se_table[2 * m + 1]; - if ((state->i & 1) == 0) { + if ((state->sample_counter & 1) == 0) { if (strm->avail_out < state->bytes_per_sample) return M_EXIT; put_sample(strm, state->se_table[2 * m] - d1); - state->i++; + state->sample_counter++; } if (strm->avail_out < state->bytes_per_sample) return M_EXIT; put_sample(strm, d1); - state->i++; + state->sample_counter++; fs_drop(strm); } @@ -583,16 +576,14 @@ static int m_se_decode(struct aec_stream *strm) static int m_se(struct aec_stream *strm) { - uint32_t i; - uint32_t m; - int32_t d1; struct internal_state *state = strm->state; if (BUFFERSPACE(strm)) { - i = state->ref; + uint32_t i = state->ref; while (i < strm->block_size) { - m = direct_get_fs(strm); + uint32_t m = direct_get_fs(strm); + int32_t d1; if (m > SE_TABLE_SIZE) return M_ERROR; @@ -609,7 +600,7 @@ static int m_se(struct aec_stream *strm) state->mode = m_next_cds; } else { state->mode = m_se_decode; - state->i = state->ref; + state->sample_counter = state->ref; } return M_CONTINUE; } @@ -621,12 +612,10 @@ static int m_low_entropy_ref(struct aec_stream *strm) if (state->ref && copysample(strm) == 0) return M_EXIT; - if (state->id == 1) { + if (state->id == 1) state->mode = m_se; - return M_CONTINUE; - } - - state->mode = m_zero_block; + else + state->mode = m_zero_block; return M_CONTINUE; } @@ -649,7 +638,7 @@ static int m_uncomp_copy(struct aec_stream *strm) do { if (copysample(strm) == 0) return M_EXIT; - } while(--state->i); + } while(--state->sample_counter); state->mode = m_next_cds; return M_CONTINUE; @@ -657,16 +646,15 @@ static int m_uncomp_copy(struct aec_stream *strm) static int m_uncomp(struct aec_stream *strm) { - size_t i; struct internal_state *state = strm->state; if (BUFFERSPACE(strm)) { - for (i = 0; i < strm->block_size; i++) + for (size_t i = 0; i < strm->block_size; i++) *state->rsip++ = direct_get(strm, strm->bits_per_sample); strm->avail_out -= state->out_blklen; state->mode = m_next_cds; } else { - state->i = strm->block_size; + state->sample_counter = strm->block_size; state->mode = m_uncomp_copy; } return M_CONTINUE; @@ -674,12 +662,10 @@ static int m_uncomp(struct aec_stream *strm) static void create_se_table(int *table) { - int i, j, k, ms; - - k = 0; - for (i = 0; i < 13; i++) { - ms = k; - for (j = 0; j <= i; j++) { + int k = 0; + for (int i = 0; i < 13; i++) { + int ms = k; + for (int j = 0; j <= i; j++) { table[2 * k] = i; table[2 * k + 1] = ms; k++; @@ -689,7 +675,6 @@ static void create_se_table(int *table) int aec_decode_init(struct aec_stream *strm) { - int i, modi; struct internal_state *state; if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0) @@ -760,13 +745,13 @@ int aec_decode_init(struct aec_stream *strm) state->in_blklen = (strm->block_size * strm->bits_per_sample + state->id_len) / 8 + 16; - modi = 1UL << state->id_len; + int modi = 1UL << state->id_len; state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *))); if (state->id_table == NULL) return AEC_MEM_ERROR; state->id_table[0] = m_low_entropy; - for (i = 1; i < modi - 1; i++) { + for (int i = 1; i < modi - 1; i++) { state->id_table[i] = m_split; } state->id_table[modi - 1] = m_uncomp; @@ -806,12 +791,11 @@ int aec_decode(struct aec_stream *strm, int flush) of the states are called. Inspired by zlib. */ - struct internal_state *state = strm->state; - int status; - strm->total_in += strm->avail_in; strm->total_out += strm->avail_out; + struct internal_state *state = strm->state; + int status; do { status = state->mode(strm); } while (status == M_CONTINUE); @@ -843,9 +827,7 @@ int aec_decode_end(struct aec_stream *strm) int aec_buffer_decode(struct aec_stream *strm) { - int status; - - status = aec_decode_init(strm); + int status = aec_decode_init(strm); if (status != AEC_OK) return status; diff --git a/src/decode.h b/src/decode.h index 3f604e5..609f949 100644 --- a/src/decode.h +++ b/src/decode.h @@ -97,8 +97,7 @@ struct internal_state { /* length of output block in bytes */ uint32_t out_blklen; - /* counter for samples */ - uint32_t n, i; + uint32_t sample_counter; /* accumulator for currently used bit sequence */ uint64_t acc; diff --git a/src/encode.c b/src/encode.c index d0b1659..47f2322 100644 --- a/src/encode.c +++ b/src/encode.c @@ -117,15 +117,12 @@ static inline void copy64(uint8_t *dst, uint64_t src) static inline void emitblock_fs(struct aec_stream *strm, int k, int ref) { - size_t i; - uint32_t used; /* used bits in 64 bit accumulator */ - uint64_t acc; /* accumulator */ struct internal_state *state = strm->state; - acc = (uint64_t)*state->cds << 56; - used = 7 - state->bits; + uint64_t acc = (uint64_t)*state->cds << 56; + uint32_t used = 7 - state->bits; /* used bits in 64 bit accumulator */ - for (i = ref; i < strm->block_size; i++) { + for (size_t i = ref; i < strm->block_size; i++) { used += (state->block[i] >> k) + 1; while (used > 63) { copy64(state->cds, acc); @@ -147,16 +144,14 @@ static inline void emitblock(struct aec_stream *strm, int k, int ref) Emit the k LSB of a whole block of input data. */ - uint64_t a; struct internal_state *state = strm->state; uint32_t *in = state->block + ref; uint32_t *in_end = state->block + strm->block_size; uint64_t mask = (UINT64_C(1) << k) - 1; uint8_t *o = state->cds; + uint64_t a = *o; int p = state->bits; - a = *o; - while(in < in_end) { a <<= 56; p = (p % 8) + 56; @@ -247,12 +242,11 @@ static void preprocess_unsigned(struct aec_stream *strm) uint32_t *restrict d = state->data_pp; uint32_t xmax = state->xmax; uint32_t rsi = strm->rsi * strm->block_size - 1; - size_t i; state->ref = 1; state->ref_sample = x[0]; d[0] = 0; - for (i = 0; i < rsi; i++) { + for (size_t i = 0; i < rsi; i++) { if (x[i + 1] >= x[i]) { D = x[i + 1] - x[i]; if (D <= x[i]) @@ -284,14 +278,13 @@ static void preprocess_signed(struct aec_stream *strm) int32_t xmin = (int32_t)state->xmin; uint32_t rsi = strm->rsi * strm->block_size - 1; uint32_t m = UINT64_C(1) << (strm->bits_per_sample - 1); - size_t i; state->ref = 1; state->ref_sample = x[0]; d[0] = 0; x[0] = (x[0] ^ m) - m; - for (i = 0; i < rsi; i++) { + for (size_t i = 0; i < rsi; i++) { x[i + 1] = (x[i + 1] ^ m) - m; if (x[i + 1] < x[i]) { D = (uint32_t)(x[i] - x[i + 1]); @@ -316,11 +309,10 @@ static inline uint64_t block_fs(struct aec_stream *strm, int k) Sum FS of all samples in block for given splitting position. */ - size_t i; - uint64_t fs = 0; struct internal_state *state = strm->state; + uint64_t fs = 0; - for (i = 0; i < strm->block_size; i++) + for (size_t i = 0; i < strm->block_size; i++) fs += (uint64_t)(state->block[i] >> k); return fs; @@ -353,26 +345,29 @@ static uint32_t assess_splitting_option(struct aec_stream *strm) analogue check can be done for decreasing k. */ - int k; - int k_min; - int this_bs; /* Block size of current block */ - int no_turn; /* 1 if we shouldn't reverse */ - int dir; /* Direction, 1 means increasing k, 0 decreasing k */ - uint64_t len; /* CDS length for current k */ - uint64_t len_min; /* CDS length minimum so far */ - uint64_t fs_len; /* Length of FS part (not including 1s) */ - struct internal_state *state = strm->state; - this_bs = strm->block_size - state->ref; - len_min = UINT64_MAX; - k = k_min = state->k; - no_turn = k == 0; - dir = 1; + /* Block size of current block */ + int this_bs = strm->block_size - state->ref; + + /* CDS length minimum so far */ + uint64_t len_min = UINT64_MAX; + + int k = state->k; + int k_min = k; + + /* 1 if we shouldn't reverse */ + int no_turn = (k == 0); + + /* Direction, 1 means increasing k, 0 decreasing k */ + int dir = 1; for (;;) { - fs_len = block_fs(strm, k); - len = fs_len + this_bs * (k + 1); + /* Length of FS part (not including 1s) */ + uint64_t fs_len = block_fs(strm, k); + + /* CDS length for current k */ + uint64_t len = fs_len + this_bs * (k + 1); if (len < len_min) { if (len_min < UINT64_MAX) @@ -417,15 +412,12 @@ static uint32_t assess_se_option(struct aec_stream *strm) If length is above limit just return UINT32_MAX. */ - size_t i; - uint64_t len, d; struct internal_state *state = strm->state; uint32_t *block = state->block; + uint64_t len = 1; - len = 1; - - for (i = 0; i < strm->block_size; i += 2) { - d = (uint64_t)block[i] + (uint64_t)block[i + 1]; + for (size_t i = 0; i < strm->block_size; i += 2) { + uint64_t d = (uint64_t)block[i] + (uint64_t)block[i + 1]; len += d * (d + 1) / 2 + block[i + 1] + 1; if (len > state->uncomp_len) return UINT32_MAX; @@ -493,7 +485,6 @@ static int m_flush_block(struct aec_stream *strm) Fall back to slow flushing if in buffered mode. */ - int n; struct internal_state *state = strm->state; #ifdef ENABLE_RSI_PADDING @@ -505,7 +496,7 @@ static int m_flush_block(struct aec_stream *strm) #endif if (state->direct_out) { - n = (int)(state->cds - strm->next_out); + int n = (int)(state->cds - strm->next_out); strm->next_out += n; strm->avail_out -= n; state->mode = m_get_block; @@ -546,16 +537,14 @@ static int m_encode_uncomp(struct aec_stream *strm) static int m_encode_se(struct aec_stream *strm) { - size_t i; - uint32_t d; struct internal_state *state = strm->state; emit(state, 1, state->id_len + 1); if (state->ref) emit(state, state->ref_sample, strm->bits_per_sample); - for (i = 0; i < strm->block_size; i+= 2) { - d = state->block[i] + state->block[i + 1]; + for (size_t i = 0; i < strm->block_size; i+= 2) { + uint32_t d = state->block[i] + state->block[i + 1]; emitfs(state, d * (d + 1) / 2 + state->block[i + 1]); } @@ -588,15 +577,14 @@ static int m_select_code_option(struct aec_stream *strm) Decide which code option to use. */ - uint32_t split_len; - uint32_t se_len; struct internal_state *state = strm->state; + uint32_t split_len; if (state->id_len > 1) split_len = assess_splitting_option(strm); else split_len = UINT32_MAX; - se_len = assess_se_option(strm); + uint32_t se_len = assess_se_option(strm); if (split_len < state->uncomp_len) { if (split_len < se_len) @@ -620,10 +608,10 @@ static int m_check_zero_block(struct aec_stream *strm) end of a segment or RSI. */ - size_t i; struct internal_state *state = strm->state; uint32_t *p = state->block; + size_t i; for (i = 0; i < strm->block_size; i++) if (p[i] != 0) break; @@ -912,7 +900,6 @@ int aec_encode(struct aec_stream *strm, int flush) Finite-state machine implementation of the adaptive entropy encoder. */ - int n; struct internal_state *state = strm->state; state->flush = flush; @@ -922,7 +909,7 @@ int aec_encode(struct aec_stream *strm, int flush) while (state->mode(strm) == M_CONTINUE); if (state->direct_out) { - n = (int)(state->cds - strm->next_out); + int n = (int)(state->cds - strm->next_out); strm->next_out += n; strm->avail_out -= n; @@ -938,9 +925,8 @@ int aec_encode(struct aec_stream *strm, int flush) int aec_encode_end(struct aec_stream *strm) { struct internal_state *state = strm->state; - int status; - status = AEC_OK; + int status = AEC_OK; if (state->flush == AEC_FLUSH && state->flushed == 0) status = AEC_STREAM_ERROR; cleanup(strm); @@ -949,9 +935,7 @@ int aec_encode_end(struct aec_stream *strm) int aec_buffer_encode(struct aec_stream *strm) { - int status; - - status = aec_encode_init(strm); + int status = aec_encode_init(strm); if (status != AEC_OK) return status; status = aec_encode(strm, AEC_FLUSH); diff --git a/src/encode_accessors.c b/src/encode_accessors.c index 89d74e0..5a6cd96 100644 --- a/src/encode_accessors.c +++ b/src/encode_accessors.c @@ -66,9 +66,7 @@ uint32_t aec_get_8(struct aec_stream *strm) uint32_t aec_get_lsb_16(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[1] << 8) + uint32_t data = ((uint32_t)strm->next_in[1] << 8) | (uint32_t)strm->next_in[0]; strm->next_in += 2; @@ -78,9 +76,7 @@ uint32_t aec_get_lsb_16(struct aec_stream *strm) uint32_t aec_get_msb_16(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[0] << 8) + uint32_t data = ((uint32_t)strm->next_in[0] << 8) | (uint32_t)strm->next_in[1]; strm->next_in += 2; @@ -90,9 +86,7 @@ uint32_t aec_get_msb_16(struct aec_stream *strm) uint32_t aec_get_lsb_24(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[2] << 16) + uint32_t data = ((uint32_t)strm->next_in[2] << 16) | ((uint32_t)strm->next_in[1] << 8) | (uint32_t)strm->next_in[0]; @@ -103,9 +97,7 @@ uint32_t aec_get_lsb_24(struct aec_stream *strm) uint32_t aec_get_msb_24(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[0] << 16) + uint32_t data = ((uint32_t)strm->next_in[0] << 16) | ((uint32_t)strm->next_in[1] << 8) | (uint32_t)strm->next_in[2]; @@ -116,9 +108,7 @@ uint32_t aec_get_msb_24(struct aec_stream *strm) uint32_t aec_get_lsb_32(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[3] << 24) + uint32_t data = ((uint32_t)strm->next_in[3] << 24) | ((uint32_t)strm->next_in[2] << 16) | ((uint32_t)strm->next_in[1] << 8) | (uint32_t)strm->next_in[0]; @@ -130,9 +120,7 @@ uint32_t aec_get_lsb_32(struct aec_stream *strm) uint32_t aec_get_msb_32(struct aec_stream *strm) { - uint32_t data; - - data = ((uint32_t)strm->next_in[0] << 24) + uint32_t data = ((uint32_t)strm->next_in[0] << 24) | ((uint32_t)strm->next_in[1] << 16) | ((uint32_t)strm->next_in[2] << 8) | (uint32_t)strm->next_in[3]; @@ -144,12 +132,11 @@ uint32_t aec_get_msb_32(struct aec_stream *strm) void aec_get_rsi_8(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; unsigned const char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = (uint32_t)in[i]; strm->next_in += rsi; @@ -158,12 +145,11 @@ void aec_get_rsi_8(struct aec_stream *strm) void aec_get_rsi_lsb_16(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = (uint32_t)in[2 * i] | ((uint32_t)in[2 * i + 1] << 8); strm->next_in += 2 * rsi; @@ -172,12 +158,11 @@ void aec_get_rsi_lsb_16(struct aec_stream *strm) void aec_get_rsi_msb_16(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = ((uint32_t)in[2 * i] << 8) | (uint32_t)in[2 * i + 1]; strm->next_in += 2 * rsi; @@ -186,12 +171,11 @@ void aec_get_rsi_msb_16(struct aec_stream *strm) void aec_get_rsi_lsb_24(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = (uint32_t)in[3 * i] | ((uint32_t)in[3 * i + 1] << 8) | ((uint32_t)in[3 * i + 2] << 16); @@ -202,12 +186,11 @@ void aec_get_rsi_lsb_24(struct aec_stream *strm) void aec_get_rsi_msb_24(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = ((uint32_t)in[3 * i] << 16) | ((uint32_t)in[3 * i + 1] << 8) | (uint32_t)in[3 * i + 2]; @@ -229,12 +212,11 @@ void aec_get_rsi_msb_24(struct aec_stream *strm) #ifdef WORDS_BIGENDIAN void aec_get_rsi_lsb_32(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = (uint32_t)in[4 * i] | ((uint32_t)in[4 * i + 1] << 8) | ((uint32_t)in[4 * i + 2] << 16) @@ -249,7 +231,6 @@ AEC_GET_RSI_NATIVE_32(msb); #else /* !WORDS_BIGENDIAN */ void aec_get_rsi_msb_32(struct aec_stream *strm) { - int i; uint32_t *restrict out = strm->state->data_raw; const unsigned char *restrict in = strm->next_in; int rsi = strm->rsi * strm->block_size; @@ -257,7 +238,7 @@ void aec_get_rsi_msb_32(struct aec_stream *strm) strm->next_in += 4 * rsi; strm->avail_in -= 4 * rsi; - for (i = 0; i < rsi; i++) + for (int i = 0; i < rsi; i++) out[i] = ((uint32_t)in[4 * i] << 24) | ((uint32_t)in[4 * i + 1] << 16) | ((uint32_t)in[4 * i + 2] << 8) diff --git a/src/sz_compat.c b/src/sz_compat.c index f06c697..c64882e 100644 --- a/src/sz_compat.c +++ b/src/sz_compat.c @@ -12,14 +12,13 @@ static int convert_options(int sz_opts) { int co[NOPTS]; - int i; int opts = 0; memset(co, 0, sizeof(int) * NOPTS); co[SZ_MSB_OPTION_MASK] = AEC_DATA_MSB; co[SZ_NN_OPTION_MASK] = AEC_DATA_PREPROCESS; - for (i = 1; i < NOPTS; i <<= 1) + for (int i = 1; i < NOPTS; i <<= 1) if (sz_opts & i) opts |= co[i]; @@ -39,32 +38,22 @@ static int bits_to_bytes(int bit_length) static void interleave_buffer(void *dest, const void *src, size_t n, int wordsize) { - size_t i; - int j; - const unsigned char *src8; - unsigned char *dest8; + const unsigned char *src8 = (unsigned char *)src; + unsigned char *dest8 = (unsigned char *)dest; - src8 = (unsigned char *)src; - dest8 = (unsigned char *)dest; - - for (i = 0; i < n / wordsize; i++) - for (j = 0; j < wordsize; j++) + for (size_t i = 0; i < n / wordsize; i++) + for (size_t j = 0; j < wordsize; j++) dest8[j * (n / wordsize) + i] = src8[i * wordsize + j]; } static void deinterleave_buffer(void *dest, const void *src, size_t n, int wordsize) { - size_t i; - int j; - const unsigned char *src8; - unsigned char *dest8; - - src8 = (unsigned char *)src; - dest8 = (unsigned char *)dest; + const unsigned char *src8 = (unsigned char *)src; + unsigned char *dest8 = (unsigned char *)dest; - for (i = 0; i < n / wordsize; i++) - for (j = 0; j < wordsize; j++) + for (size_t i = 0; i < n / wordsize; i++) + for (size_t j = 0; j < wordsize; j++) dest8[i * wordsize + j] = src8[j * (n / wordsize) + i]; } @@ -72,22 +61,21 @@ static void add_padding(void *dest, const void *src, size_t src_length, size_t line_size, size_t padding_size, int pixel_size, int pp) { - size_t i, j, k, ls, ps; - const char *pixel; const char zero_pixel[] = {0, 0, 0, 0}; - pixel = zero_pixel; - j = 0; - i = 0; + const char *pixel = zero_pixel; + size_t j = 0; + size_t i = 0; while (i < src_length) { - ls = MIN(src_length - i, line_size); + size_t ps; + size_t ls = MIN(src_length - i, line_size); memcpy((char *)dest + j, (char *)src + i, ls); j += ls; i += ls; if (pp) pixel = (char *)src + i - pixel_size; ps = line_size + padding_size - ls; - for (k = 0; k < ps; k += pixel_size) + for (size_t k = 0; k < ps; k += pixel_size) memcpy((char *)dest + j + k, pixel, pixel_size); j += ps; } @@ -97,11 +85,10 @@ static void remove_padding(void *buf, size_t buf_length, size_t line_size, size_t padding_size, int pixel_size) { - size_t i, j; size_t padded_line_size = line_size + padding_size; - i = line_size; - for (j = padded_line_size; j < buf_length; j += padded_line_size) { + size_t i = line_size; + for (size_t j = padded_line_size; j < buf_length; j += padded_line_size) { memmove((char *)buf + i, (char *)buf + j, line_size); i += line_size; } @@ -113,14 +100,6 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, { struct aec_stream strm; int status; - int aec_status; - void *padbuf; - void *buf; - size_t padding_size; - size_t scanlines; - size_t padbuf_size; - int pixel_size; - int interleave; strm.block_size = param->pixels_per_block; strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1) @@ -128,10 +107,10 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask); strm.avail_out = *destLen; strm.next_out = dest; - buf = 0; - padbuf = 0; + void *buf = 0; + void *padbuf = 0; - interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64; + int interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64; if (interleave) { strm.bits_per_sample = 8; buf = malloc(sourceLen); @@ -145,18 +124,18 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, buf = (void *)source; } - pixel_size = bits_to_bytes(strm.bits_per_sample); + int pixel_size = bits_to_bytes(strm.bits_per_sample); - scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1) + size_t scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1) / param->pixels_per_scanline; - padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines; + size_t padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines; padbuf = malloc(padbuf_size); if (padbuf == NULL) { status = SZ_MEM_ERROR; goto CLEANUP; } - padding_size = + size_t padding_size = (strm.rsi * strm.block_size - param->pixels_per_scanline) * pixel_size; @@ -167,7 +146,7 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, strm.next_in = padbuf; strm.avail_in = padbuf_size; - aec_status = aec_buffer_encode(&strm); + int aec_status = aec_buffer_encode(&strm); if (aec_status == AEC_STREAM_ERROR) status = SZ_OUTBUFF_FULL; else @@ -188,14 +167,8 @@ int SZ_BufftoBuffDecompress(void *dest, size_t *destLen, { struct aec_stream strm; int status; - void *buf; - size_t padding_size; + size_t total_out; size_t scanlines; - size_t buf_size, total_out; - int pixel_size; - int pad_scanline; - int deinterleave; - int extra_buffer; strm.block_size = param->pixels_per_block; strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1) @@ -203,20 +176,23 @@ int SZ_BufftoBuffDecompress(void *dest, size_t *destLen, strm.flags = convert_options(param->options_mask); strm.avail_in = sourceLen; strm.next_in = source; - buf = 0; + void *buf = 0; - pad_scanline = param->pixels_per_scanline % param->pixels_per_block; - deinterleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64; - extra_buffer = pad_scanline || deinterleave; + int pad_scanline = param->pixels_per_scanline % param->pixels_per_block; + int deinterleave = (param->bits_per_pixel == 32 + || param->bits_per_pixel == 64); + int extra_buffer = pad_scanline || deinterleave; + int pixel_size; if (deinterleave) strm.bits_per_sample = 8; else strm.bits_per_sample = param->bits_per_pixel; - pixel_size = bits_to_bytes(strm.bits_per_sample); + if (extra_buffer) { + size_t buf_size; if (pad_scanline) { scanlines = (*destLen / pixel_size + param->pixels_per_scanline - 1) / param->pixels_per_scanline; @@ -241,7 +217,7 @@ int SZ_BufftoBuffDecompress(void *dest, size_t *destLen, goto CLEANUP; if (pad_scanline) { - padding_size = + size_t padding_size = (strm.rsi * strm.block_size - param->pixels_per_scanline) * pixel_size; remove_padding(buf, strm.total_out, diff --git a/tests/check_aec.c b/tests/check_aec.c index c9d9413..b0bb73d 100644 --- a/tests/check_aec.c +++ b/tests/check_aec.c @@ -5,17 +5,13 @@ static void out_lsb(unsigned char *dest, unsigned long long int val, int size) { - int i; - - for (i = 0; i < size; i++) + for (int i = 0; i < size; i++) dest[i] = (unsigned char)(val >> (8 * i)); } static void out_msb(unsigned char *dest, unsigned long long int val, int size) { - int i; - - for (i = 0; i < size; i++) + for (int i = 0; i < size; i++) dest[i] = (unsigned char)(val >> (8 * (size - 1 - i))); } @@ -58,12 +54,11 @@ int update_state(struct test_state *state) int encode_decode_small(struct test_state *state) { - int status, i; size_t compressed_size; size_t n_in, avail_in, avail_out, total_out; struct aec_stream *strm = state->strm; - status = aec_encode_init(strm); + int status = aec_encode_init(strm); if (status != AEC_OK) { printf("Init failed.\n"); return 99; @@ -175,19 +170,19 @@ int encode_decode_small(struct test_state *state) printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL); printf("\nuncompressed buf"); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n"); printf("%02x ", state->ubuf[i]); } printf("\n\ncompressed buf len %li", compressed_size); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n"); printf("%02x ", state->cbuf[i]); } printf("\n\ndecompressed buf"); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n%04i ", i); printf("%02x ", state->obuf[i]); @@ -201,12 +196,10 @@ int encode_decode_small(struct test_state *state) int encode_decode_large(struct test_state *state) { - int status, i; - char fbase[1024]; - char fname[1024]; - FILE *fp; + int status; int bflags; size_t to; + char fbase[1024]; struct aec_stream *strm = state->strm; strm->avail_in = state->ibuf_len; @@ -220,6 +213,8 @@ int encode_decode_large(struct test_state *state) return 99; } if (state->dump) { + char fname[1024]; + FILE *fp; snprintf(fbase, sizeof(fbase), "BPS%02iID%iBS%02iRSI%04iFLG%04i", strm->bits_per_sample, state->id, @@ -254,6 +249,8 @@ int encode_decode_large(struct test_state *state) aec_encode_end(strm); if (state->dump) { + char fname[1024]; + FILE *fp; snprintf(fname, sizeof(fname), "%s.rz", fbase); if ((fp = fopen(fname, "wb")) == NULL) { fprintf(stderr, "ERROR: cannot open dump file %s\n", fname); @@ -288,19 +285,19 @@ int encode_decode_large(struct test_state *state) printf("\n%s: Uncompressed output differs from input.\n", CHECK_FAIL); printf("\nuncompressed buf"); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n"); printf("%02x ", state->ubuf[i]); } printf("\n\ncompressed buf len %li", to); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n"); printf("%02x ", state->cbuf[i]); } printf("\n\ndecompressed buf"); - for (i = 0; i < 80; i++) { + for (int i = 0; i < 80; i++) { if (i % 8 == 0) printf("\n"); printf("%02x ", state->obuf[i]); diff --git a/tests/check_buffer_sizes.c b/tests/check_buffer_sizes.c index da9bf1e..38921a8 100644 --- a/tests/check_buffer_sizes.c +++ b/tests/check_buffer_sizes.c @@ -7,9 +7,8 @@ int check_block_sizes(struct test_state *state) { - int bs, status; - - for (bs = 8; bs <= 64; bs *= 2) { + for (int bs = 8; bs <= 64; bs *= 2) { + int status; state->strm->block_size = bs; state->strm->rsi = (int)(state->buf_len / (bs * state->bytes_per_sample)); @@ -23,11 +22,9 @@ int check_block_sizes(struct test_state *state) int check_block_sizes_short(struct test_state *state) { - int bs, status; - size_t tmp; - - tmp = state->ibuf_len; - for (bs = 8; bs <= 64; bs *= 2) { + size_t tmp = state->ibuf_len; + for (int bs = 8; bs <= 64; bs *= 2) { + int status; state->strm->block_size = bs; state->strm->rsi = (int)(state->buf_len / (bs * state->bytes_per_sample)); @@ -48,12 +45,10 @@ int check_block_sizes_short(struct test_state *state) int check_rsi(struct test_state *state) { - int status, size; - unsigned char *tmp; - - size = state->bytes_per_sample; + int status; + int size = state->bytes_per_sample; - for (tmp = state->ubuf; + for (unsigned char *tmp = state->ubuf; tmp < state->ubuf + state->buf_len; tmp += 2 * state->bytes_per_sample) { state->out(tmp, state->xmax, size); diff --git a/tests/check_code_options.c b/tests/check_code_options.c index 4f013b0..dea916f 100644 --- a/tests/check_code_options.c +++ b/tests/check_code_options.c @@ -7,17 +7,17 @@ int check_block_sizes(struct test_state *state, int id, int id_len) { - int bs, status, rsi, max_rsi; - state->id = id; - for (bs = 8; bs <= 64; bs *= 2) { - state->strm->block_size = bs; + for (int bs = 8; bs <= 64; bs *= 2) { + int max_rsi; + state->strm->block_size = bs; max_rsi = (int)(state->buf_len / (bs * state->bytes_per_sample)); if (max_rsi > 4096) max_rsi = 4096; - for (rsi = 1; rsi <= max_rsi; rsi++) { + for (int rsi = 1; rsi <= max_rsi; rsi++) { + int status; state->strm->rsi = rsi; status = state->codec(state); if (status) @@ -55,10 +55,10 @@ int check_zero(struct test_state *state) int check_splitting(struct test_state *state, int k) { - int status, size; + int status; unsigned char *tmp; - size = state->bytes_per_sample; + int size = state->bytes_per_sample; if (state->strm->flags & AEC_DATA_PREPROCESS) { for (tmp = state->ubuf; @@ -92,10 +92,10 @@ int check_splitting(struct test_state *state, int k) int check_uncompressed(struct test_state *state) { - int status, size; + int status; unsigned char *tmp; - size = state->bytes_per_sample; + int size = state->bytes_per_sample; for (tmp = state->ubuf; tmp < state->ubuf + state->buf_len; @@ -117,10 +117,10 @@ int check_uncompressed(struct test_state *state) int check_fs(struct test_state *state) { - int status, size; + int status; unsigned char *tmp; - size = state->bytes_per_sample; + int size = state->bytes_per_sample; if (state->strm->flags & AEC_DATA_PREPROCESS) { for (tmp = state->ubuf; @@ -153,10 +153,10 @@ int check_fs(struct test_state *state) int check_se(struct test_state *state) { - int status, size; + int status; unsigned char *tmp; - size = state->bytes_per_sample; + int size = state->bytes_per_sample; if (state->strm->flags & AEC_DATA_PREPROCESS) { for (tmp = state->ubuf; @@ -197,9 +197,9 @@ int check_se(struct test_state *state) int check_bps(struct test_state *state) { - int k, status, bps; + int status; - for (bps = 8; bps <= 32; bps += 8) { + for (int bps = 8; bps <= 32; bps += 8) { state->strm->bits_per_sample = bps; if (bps == 24) state->strm->flags |= AEC_DATA_3BYTE; @@ -224,7 +224,7 @@ int check_bps(struct test_state *state) if (status) return status; - for (k = 1; k < bps - 2; k++) { + for (int k = 1; k < bps - 2; k++) { status = check_splitting(state, k); if (status) return status; diff --git a/tests/check_long_fs.c b/tests/check_long_fs.c index 75206b9..4b000db 100644 --- a/tests/check_long_fs.c +++ b/tests/check_long_fs.c @@ -7,12 +7,11 @@ int check_long_fs(struct test_state *state) { - int status, size, i, bs; - - size = state->bytes_per_sample; - bs = state->strm->block_size; + int status; + int size = state->bytes_per_sample; + int bs = state->strm->block_size; - for (i = 0; i < bs / 2; i++) { + for (int i = 0; i < bs / 2; i++) { state->out(state->ubuf + size * i, state->xmin, size); state->out(state->ubuf + bs * size / 2 + size * i, 65000, size); } -- 2.7.4